/** * The harness store: read/merge/apply/rollback over local and global state * files, trajectory serialization, and the commit path that persists each * refinement and emits the scoped `harness/refined` event. A plain class * owned by the plugin's `apply`; not a Cordis service. * @module dsh-continual-harness */ import type { Context } from '@deepseek-ai/cordis'; import type { Agent } from '@deepseek-ai/dsh-agent'; import type { Message } from '@deepseek-ai/dsh-llm'; import type { Session } from '@deepseek-ai/dsh-session'; import type { HarnessSnapshot } from './benchmark.ts'; import { type InjectionAnchor } from './render.ts'; import type { SkillBundleLimits } from './skills.ts'; import type { SessionProjectionState } from './session-state.ts'; import type { HarnessState, MaterializationResult, RefinementKind, RefinementProposal, RefinementResult } from './types.ts'; /** Default tail-biased trajectory window for planning (spec §2.3: 12k). */ export declare const DEFAULT_TRAJECTORY_MAX_CHARS = 12000; /** Default fraction of the trajectory budget kept verbatim (spec §2.3). */ export declare const DEFAULT_TRAJECTORY_SIGNAL_RATIO = 0.5; /** Per-role char caps shared by the trajectory digest and signal layers. */ export declare const TRAJECTORY_ROLE_CAPS: { readonly user: 300; readonly assistant: 200; }; /** Options for a store commit. */ export interface CommitOptions { /** Commit to the cross-session global store instead of the session store. */ global?: boolean; /** When set, the commit is recorded as the rollback of this refinement id. */ rollbackOf?: string; /** Marks the commit as riding the automatic path (gate), enabling protected-layer checks. */ automatic?: boolean; /** * Target-store state captured at planning time, used for baseline conflict * detection: an edit whose target entry changed between planning and commit * is rejected. When absent (rollback/promote), the commit-time read is used. */ baseline?: HarnessState; } /** Persistent harness store owned by the plugin. */ export declare class HarnessStore { private readonly ctx; /** Harness home directory (defaults under the dsh home). */ readonly home: string; /** Skills directory where effective skill entries materialize as SKILL.md. */ readonly skillsDir: string; /** Per-commit entry growth fraction cap; 0 (default) disables the check. */ private readonly maxEntryGrowth; /** Kinds protected from the automatic path; plumbed for Config wiring. */ private readonly protectedKinds; /** Skill bundle limits used by L1 files validation (spec §7.10). */ private readonly skillBundleLimits; /** Per-kind cap for ranked prompt injection. */ private readonly maxInjectedEntriesPerKind; /** What the injected block is ranked against: the live question or a session-stable anchor. */ private readonly injectionAnchor; private readonly workspacePathFor; /** In-memory injection telemetry, loaded once from usage.events.jsonl. */ private usage; /** Per-session projection facts observed while the plugin runs; nothing is persisted. */ private readonly projections; constructor(ctx: Context, options?: { harnessRoot?: string; skillsDir?: string; maxEntryGrowth?: number; protectedKinds?: readonly RefinementKind[]; skillBundleLimits?: SkillBundleLimits; maxInjectedEntriesPerKind?: number; injectionAnchor?: InjectionAnchor; /** Canonical directory of the Workspace accounting a session, when the deployment has Workspaces. */ workspacePathFor?: (sessionId: string) => string | undefined; }); /** The session-local state for an agent; migration diagnostics are logged. */ localState(agent: Agent): HarnessState; /** * The session's tracked projection facts; empty until the observer records * one. Facts are derivable from committed events and from current surface * state, so nothing is read from or written to disk here. */ sessionProjection(session: Session): SessionProjectionState; /** Merge newly observed projection facts; an unchanged patch is a no-op. */ recordSessionProjection(session: Session, patch: SessionProjectionState): void; /** The cross-session global state; migration diagnostics are logged. */ globalState(): HarnessState; private logMigration; /** The merged view the model sees: local shadows same-id global entries. */ state(agent: Agent): HarnessState; /** * Capture a read-only snapshot of the merged local/global state without * persisting anything: no files are written, no entries are mutated or * stamped, and no injection or usage tracking is triggered. The returned * snapshot's `state` is a structured-clone copy of the merged view and its * `layers` retain the two source stores, so a candidate can be derived by * applying a refinement to its own layer; persist it with * `captureReferenceSnapshot`. */ captureSnapshot(agent: Agent, snapshotId: string, refinementId?: string): HarnessSnapshot; /** * Merged refinement history: the session journal (full records, first for * dedupe-by-id fidelity), then the session state copy (conclusion-only), * then the cross-session history (full records). */ history(agent: Agent): RefinementResult[]; /** fs-backed create-conflict gate: a create onto a non-harness-owned bundle is rejected (spec §7.4). */ private createConflictError; /** Structured overview + injected keys + the merged state behind them, so one read serves both. */ render(agent: Agent): { overview: string; injectedKeys: string[]; matchedKeys: string[]; state: HarnessState; }; /** Lazy-load injection telemetry into memory. */ private usageStats; /** Record one injection per key: batch-append the events and update memory; never blocks injection. */ recordInjections(agent: Agent, injectedKeys: string[]): void; /** Aggregate stats for one usage key (for wrap-up suggestions). */ usageStatsFor(key: string): { injectionCount: number; lastInjectedAt?: string; } | undefined; /** Tail-biased trajectory serialization for the planner. */ trajectory(agent: Agent, maxChars?: number, signalRatio?: number): string; /** Project name of the session: DSH's Workspace when it accounts the session, else the repo at cwd. */ private projectTagOf; /** The tag as a compact spread, so callers never pass `project: undefined`. */ private stampFor; /** * Commit a planned refinement: apply to the target store with baseline * conflict detection, persist, append the global history when global, and * emit the scoped `harness/refined` event. * The baseline defaults to the commit-time read; callers that planned * against an earlier snapshot pass it via `options.baseline` so edits over * entries changed during planning are rejected. */ applyRefinement(agent: Agent, plan: RefinementProposal, options?: CommitOptions): RefinementResult & { materialization: MaterializationResult; }; /** Promote a local entry to global by copy: local stays unchanged; a same-id * global is a deterministic conflict. Not a cross-store transaction. */ promoteEntry(agent: Agent, id: string): { applied: boolean; error?: string; }; /** * Materialize the SKILL.md bundles for skill ids touched by a committed * refinement, from the effective merged view, so dsh's filesystem skill * provider picks the generated skills up live. Write faults are collected * into the returned MaterializationResult; the already-persisted commit is * never failed (spec §7.5/§7.7). */ private materializeSkills; /** Roll back a committed refinement by id from the merged history. */ rollbackRefinement(agent: Agent, rollbackId: string, options?: CommitOptions): RefinementResult & { materialization: MaterializationResult; }; } /** * Two-layer tail-biased trajectory (spec §2.3): the signal layer keeps the * most recent messages verbatim up to `maxChars * signalRatio`; the digest * layer truncates everything older (user 300 / assistant 200 chars + tool * names). Input is the surface-ordered derived history — the same messages * the host loop already sent — so no event-structure re-parsing is needed. * * Compatibility: the signal layer labels lines `[user/message]` / * `[assistant/message]` exactly like the pre-refactor serializer, so the * existing store.spec assertions keep passing; the digest layer uses the * shorter `[user]` / `[assistant]` tags. */ export declare function serializeTrajectory(session: Session, maxChars: number, signalRatio?: number): string; /** * Serialized text of one derived message (text blocks only). A tool result is * its own tool-role message in dsh 0.1.7 — no longer a `tool-result` first * block — and contributes no text to the trajectory or the char budget either * way, hence the role check. `role` stays optional because the digest layer * passes bare blocks. */ export declare function messageText(message: Pick & { role?: Message['role']; }): string; //# sourceMappingURL=store.d.ts.map