/** * The agent-backed filesystem substrate: the provider-shaped operations the * model's file tools call, orchestrated over the distro's FS frames. * * This is the `ctx.fs` seam's mechanics for `substrate: "agent"` — resolve, * stat, reads, and the write/edit orchestration the host backend owns on a * share-backed session (probe, guards, diff basis, publication, outcome * shape), mirroring `@deepseek-ai/dsh-fs-local`'s `writeText` and `editText` * byte-for-byte in message and code. Two deliberate differences from the share * backend it replaces: * * 1. **The mutation guard survives to the write — per intent.** A guarded * create publishes with the distro-side no-replace link, a real atomic * primitive that refuses to replace. An overwrite or edit carries the * version it was based on INTO the write op, and the agent re-verifies it * with a fresh stat before the rename: an earlier conflicting write is * refused with `FS_STALE_VERSION`. A writer between that stat and the * rename can still be overwritten; only guarded create is atomic. * 2. A symlink needs no retry machinery: every identity is resolved where the * symlinks live. * * Peer-free by construction: refusals are `FsCodedError`s from * `lib/fsio-text.js`, mapped onto the peer's `FsError` at the provider * boundary, so this module is unit-testable in a bare checkout against the * real agent script. * * This is a TypeScript source built to `lib/fs-substrate.js`; edit THIS file * and run `pnpm run build` — the artifact under `lib/` is generated, and * `pnpm test` fails when it drifts. * * @module dsh-plugin-wsl-env/fs-substrate */ import type { PatchHunk } from "@deepseek-ai/dsh-fs-patch"; import { type ResolvedTarget } from "./fsio-agent.js"; import { DistroFs } from "./fsio-agent.js"; import type { WslAgent } from "./agent.js"; /** * One distro's provider-shaped filesystem face. */ export declare class AgentSubstrate { /** The pinned distro name, for UNC identity. */ distro: string; /** The overwrite-diff bound, mirroring the provider config. */ diffBasisMaxBytes: number; /** The resident in-distro agent reads ride. */ agent: WslAgent; /** Stage-two routing: the agent a mutation must run on for the resolved policy, when wired. */ agentFor: ((policy: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined) => Promise) | undefined; /** The distro-side filesystem face. */ fs: DistroFs; /** Per-targetKey tail promise: the host backend's mutation serialization. */ locks: Map>; /** * @param deps - the substrate's collaborators. */ constructor({ agent, agentFor, distro, diffBasisMaxBytes }: { /** The resident in-distro agent reads ride. */ agent: WslAgent; /** The pinned distro name, for UNC identity. */ distro: string; /** The overwrite-diff bound, mirroring the provider config. */ diffBasisMaxBytes?: number; /** * Stage-two routing: the agent a MUTATION must run on for this resolved * policy — the confined resident whose mount table matches the granted * rights. Omitted (or resolving to `deps.agent`) keeps every op on the * plain resident, which is stage one's posture. */ agentFor?: (policy: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined) => Promise; }); /** * The agent a mutation runs on under this resolved policy. * * @param policy - the resolved file-effect policy, or undefined when the * caller pre-checked. * @returns the agent to publish through. */ mutationAgent(policy: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined): Promise; /** * Run `op` with exclusive access to `targetKey` (FIFO per key). * * @template T * @param targetKey - the mutation's serialization key. * @param op - the exclusive section. * @returns the section's outcome. */ lock(targetKey: string, op: () => Promise): Promise; /** * The Linux path a resolved target names, falling back to its display. * * @param target - the resolved target. * @returns the Linux path to operate on. */ linuxOf(target: ResolvedTarget): string; /** * Resolve one absolute Linux path to the target shape the provider returns: * the canonical Linux spelling the model sees and the UNC identity the * harness keys on. Symlinks resolve here, where they live. * * @param linuxPath - the absolute Linux path to resolve. * @param signal - cancels the round trip. */ resolve(linuxPath: string, signal?: AbortSignal): Promise<{ displayPath: string; targetKey: string; }>; /** * Follow-stat one target; `undefined` when absent. * * @param target - the resolved target. * @param signal - cancels the round trip. */ stat(target: ResolvedTarget, signal?: AbortSignal): Promise<{ version: string; type: string; size: number; } | undefined>; /** * No-follow stat one path; `undefined` when absent, `symlink` for a link. * * @param linuxPath - the absolute Linux path to inspect. * @param signal - cancels the round trip. */ lstat(linuxPath: string, signal?: AbortSignal): Promise<{ version: string; type: string; size: number; } | undefined>; /** * Read a whole regular UTF-8 file. * * @param target - the resolved target. * @param signal - aborts the read. * @returns the full decoded text. */ readText(target: ResolvedTarget, signal?: AbortSignal): Promise; /** * Stream a whole regular UTF-8 file as decoded chunks. * * @param target - the resolved target. * @param signal - aborts the stream. * @returns the chunk stream. */ streamText(target: ResolvedTarget, signal?: AbortSignal): Promise>; /** * Read a whole regular file as raw bytes, bounded. * * @param target - the resolved target. * @param signal - aborts the read. * @param maxBytes - inclusive byte cap. * @returns the bytes. */ readBytes(target: ResolvedTarget, signal: AbortSignal | undefined, maxBytes: number): Promise; /** * Read one byte window of a regular file. * * @param target - the resolved target. * @param range - the window. * @param signal - aborts the read. * @returns the window's bytes. */ readByteRange(target: ResolvedTarget, range: { offset: number; length: number; }, signal?: AbortSignal): Promise; /** * List one directory's direct children. * * @param target - the resolved directory. * @param signal - aborts between children. */ listDir(target: ResolvedTarget, signal?: AbortSignal): Promise>; /** * Write a whole text file with the host backend's orchestration: probe, * guard, best-effort diff basis, atomic publication, outcome shape. The * guard is FORWARDED — a guarded create publishes with the distro-side * no-replace link (atomic), and a `replaceIfVersion` write carries its * version into the op, where the agent re-verifies it before * the rename and refuses with `FS_STALE_VERSION` on a mismatch. * * @param target - the resolved target. * @param content - the full new file content. * @param expected - the write intent. * @param signal - cancels the write. * @param policy - the resolved policy this write was fenced with; selects * the confined resident when stage-two routing is wired. */ writeText(target: ResolvedTarget, content: string, expected: { kind: string; version?: string; } | undefined, signal?: AbortSignal, policy?: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined): Promise<{ operation: string; version: string; before: string | null; after: string; }>; /** * Edit a text file with the host backend's orchestration: probe, version * guard, LF-normalized read, literal replacement, line-ending restore, * atomic publication, outcome shape. The version the edit was based on * rides the write op and is re-verified distro-side before the * rename, narrowing but not eliminating the concurrent-write race. * * @param target - the resolved target. * @param edit - the literal request. * @param expected - the version guard. * @param signal - cancels the edit. * @param policy - the resolved policy this edit was fenced with; selects * the confined resident when stage-two routing is wired. */ editText(target: ResolvedTarget, edit: { oldString: string; newString: string; replaceAll?: boolean; }, expected: { version: string; } | undefined, signal?: AbortSignal, policy?: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined): Promise<{ version: string; before: string; after: string; }>; /** * Apply parsed hunks to a text file (protocol 7): the peer's `patchText` * semantics, using the peer's own `applyHunks` for matching and its exact * `FS_PATCH_MISMATCH` dialect, then the same guarded atomic publication a * write takes. The hunks travel in-process; the distro sees one read and * one guarded replace. * * @param target - the resolved regular file. * @param hunks - ordered hunks from the caller's patch parse. * @param expected - the version the patch was based on. * @param signal - cancels the patch. * @param policy - the resolved policy this patch was fenced with. * @returns the pre/post text and the published version. */ patchText(target: ResolvedTarget, hunks: readonly PatchHunk[], expected: { version: string; } | undefined, signal?: AbortSignal, policy?: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined): Promise<{ version: string; before: string; after: string; }>; /** * Remove one regular file (protocol 7), mirroring the peer's * `deleteFile`: missing/stale/mismatched targets refuse, the read-basis is * best effort and bounded by `diffBasisMaxBytes`, and the removal itself is * re-guarded distro-side by the expected version. * * @param target - the resolved file. * @param expected - the version the removal was based on. * @param signal - cancels the removal. * @param policy - the resolved policy this removal was fenced with. * @returns the bounded pre-removal text basis. */ deleteFile(target: ResolvedTarget, expected: { version: string; } | undefined, signal?: AbortSignal, policy?: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined): Promise<{ before: string | null; }>; /** * Move a regular file (protocol 7), mirroring the peer's `moveFile`: the * source passes `requireFile`, the destination `writeTarget`, an optional * hunk set transforms the text on the way, and the two effects (publish, * remove) are reported separately — a destination that landed while the * source survived returns `sourceDeleted: false` plus the failure. * * @param source - the resolved source. * @param destination - the resolved destination. * @param options - the hunks and the source/destination guards. * @param signal - cancels before publication. * @param policy - the resolved policy both paths run under. * @returns the bounded source basis and the destination outcome. */ moveFile(source: ResolvedTarget, destination: ResolvedTarget, options: { hunks?: readonly PatchHunk[]; expectedSource?: { version: string; }; expectedDestination?: { kind: string; version?: string; }; }, signal?: AbortSignal, policy?: { mode: string; workspaceRoot: string; sessionId?: string; } | undefined): Promise<{ sourceBefore: string | null; sourceDeleted: boolean; sourceFailure?: { code: string; message: string; }; destination: { operation: string; version: string; before: string | null; after: string; }; }>; }