dsh-plugin-wsl-env 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +152 -0
  2. package/CONTRIBUTING.md +37 -5
  3. package/README.md +59 -4
  4. package/SUPPORT.md +3 -3
  5. package/agent/native/linux-aarch64/dsh-wsl-agent +0 -0
  6. package/agent/native/linux-x86_64/dsh-wsl-agent +0 -0
  7. package/agent/wsl-agent.sh +331 -11
  8. package/docs/AGENT-PROTOCOL-V7.md +175 -0
  9. package/docs/API.md +24 -0
  10. package/docs/ARCHITECTURE.md +14 -1
  11. package/docs/CONFIGURATION.md +12 -5
  12. package/docs/CONFIGURATION.zh.md +5 -3
  13. package/docs/LIMITATIONS.md +16 -4
  14. package/docs/LIMITATIONS.zh.md +20 -11
  15. package/docs/MIGRATION.md +4 -4
  16. package/docs/PEER-PARITY.md +50 -33
  17. package/docs/QUICKSTART.md +18 -1
  18. package/docs/QUICKSTART.zh.md +16 -1
  19. package/docs/README.zh.md +43 -4
  20. package/docs/RELEASING.md +1 -1
  21. package/docs/REQUIREMENTS.zh.md +3 -2
  22. package/docs/ROADMAP-1.0.md +27 -16
  23. package/docs/UPSTREAM-FILE-REFERENCE-SEAM.md +3 -1
  24. package/docs/UPSTREAM-SESSION-NOTICE.md +3 -1
  25. package/docs/UPSTREAM-TERMINAL-TITLE.md +3 -0
  26. package/docs/root-fs-routing.md +3 -3
  27. package/lib/agent-confined.js +4 -4
  28. package/lib/agent-errors.d.ts +1 -1
  29. package/lib/agent-errors.js +1 -1
  30. package/lib/agent-exec.d.ts +1 -1
  31. package/lib/agent-exec.js +1 -1
  32. package/lib/agent-protocol.d.ts +58 -8
  33. package/lib/agent-protocol.js +40 -7
  34. package/lib/agent-shared.d.ts +1 -1
  35. package/lib/agent-shared.js +1 -1
  36. package/lib/agent.d.ts +171 -1
  37. package/lib/agent.js +448 -288
  38. package/lib/auto-preset.d.ts +1 -1
  39. package/lib/auto-preset.js +1 -1
  40. package/lib/bwrap.d.ts +1 -1
  41. package/lib/bwrap.js +1 -1
  42. package/lib/distro-route.d.ts +1 -1
  43. package/lib/distro-route.js +1 -1
  44. package/lib/doctor-live.d.ts +53 -0
  45. package/lib/doctor-live.js +37 -0
  46. package/lib/file-reference-wsl.d.ts +1 -1
  47. package/lib/file-reference-wsl.js +1 -1
  48. package/lib/fs-decisions.d.ts +1 -1
  49. package/lib/fs-decisions.js +1 -1
  50. package/lib/fs-routing.d.ts +1 -1
  51. package/lib/fs-routing.js +1 -1
  52. package/lib/fs-substrate.d.ts +90 -11
  53. package/lib/fs-substrate.js +202 -1
  54. package/lib/fs.d.ts +1 -1
  55. package/lib/fs.js +1 -1
  56. package/lib/fsio-agent.d.ts +128 -4
  57. package/lib/fsio-agent.js +224 -15
  58. package/lib/fsio-text.d.ts +1 -1
  59. package/lib/fsio-text.js +1 -1
  60. package/lib/git-exec.d.ts +1 -1
  61. package/lib/git-exec.js +1 -1
  62. package/lib/git-route.d.ts +1 -1
  63. package/lib/git-route.js +1 -1
  64. package/lib/index.d.ts +105 -30
  65. package/lib/index.js +266 -122
  66. package/lib/listing.d.ts +1 -1
  67. package/lib/listing.js +1 -1
  68. package/lib/native-agent.d.ts +1 -1
  69. package/lib/native-agent.js +2 -2
  70. package/lib/paths.d.ts +6 -3
  71. package/lib/paths.js +4 -4
  72. package/lib/picker.d.ts +1 -1
  73. package/lib/picker.js +7 -7
  74. package/lib/ports.d.ts +1 -1
  75. package/lib/ports.js +1 -1
  76. package/lib/preflight.d.ts +1 -1
  77. package/lib/preflight.js +1 -1
  78. package/lib/sandbox-core.d.ts +1 -1
  79. package/lib/sandbox-core.js +2 -2
  80. package/lib/sandbox.d.ts +1 -1
  81. package/lib/sandbox.js +1 -1
  82. package/lib/search-exec.d.ts +1 -1
  83. package/lib/search-exec.js +1 -1
  84. package/lib/search-route.d.ts +1 -1
  85. package/lib/search-route.js +1 -1
  86. package/lib/session-runtime.d.ts +1 -1
  87. package/lib/session-runtime.js +2 -2
  88. package/lib/session-scope.d.ts +1 -1
  89. package/lib/session-scope.js +1 -1
  90. package/lib/shell-env.d.ts +1 -1
  91. package/lib/shell-env.js +1 -1
  92. package/lib/shell.d.ts +1 -1
  93. package/lib/shell.js +1 -1
  94. package/lib/subprocess.d.ts +1 -1
  95. package/lib/subprocess.js +2 -2
  96. package/lib/terminal-activity.d.ts +1 -1
  97. package/lib/terminal-activity.js +1 -1
  98. package/lib/terminal-route.d.ts +1 -1
  99. package/lib/terminal-route.js +1 -1
  100. package/lib/watcher.d.ts +1 -1
  101. package/lib/watcher.js +1 -1
  102. package/lib/wsl.d.ts +1 -31
  103. package/lib/wsl.js +1 -21
  104. package/package.json +15 -4
  105. package/scripts/doctor.mjs +34 -11
@@ -31,7 +31,7 @@
31
31
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
32
32
  * fails when it drifts.
33
33
  *
34
- * @module dsh-plugin-wsl/auto-preset
34
+ * @module dsh-plugin-wsl-env/auto-preset
35
35
  */
36
36
  import z from "@deepseek-ai/schemastery";
37
37
  import type { Context } from "@deepseek-ai/cordis";
@@ -31,7 +31,7 @@
31
31
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
32
32
  * fails when it drifts.
33
33
  *
34
- * @module dsh-plugin-wsl/auto-preset
34
+ * @module dsh-plugin-wsl-env/auto-preset
35
35
  */
36
36
  import { appendFileSync } from "node:fs";
37
37
  import z from "@deepseek-ai/schemastery";
package/lib/bwrap.d.ts CHANGED
@@ -21,7 +21,7 @@
21
21
  * `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
22
22
  * fails when it drifts.
23
23
  *
24
- * @module dsh-plugin-wsl/bwrap
24
+ * @module dsh-plugin-wsl-env/bwrap
25
25
  */
26
26
  /** Profile options shared by the two confinement sites. */
27
27
  export interface BwrapProfileOptions {
package/lib/bwrap.js CHANGED
@@ -21,7 +21,7 @@
21
21
  * `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
22
22
  * fails when it drifts.
23
23
  *
24
- * @module dsh-plugin-wsl/bwrap
24
+ * @module dsh-plugin-wsl-env/bwrap
25
25
  */
26
26
  /**
27
27
  * Build the bwrap profile arguments for one file-effect policy, exactly as
@@ -14,7 +14,7 @@
14
14
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
15
15
  * fails when it drifts.
16
16
  *
17
- * @module dsh-plugin-wsl/distro-route
17
+ * @module dsh-plugin-wsl-env/distro-route
18
18
  */
19
19
  /** Why a distro was selected, for diagnostics and for the docs. */
20
20
  export type DistroSource = "pinned" | "workspace" | "default";
@@ -14,7 +14,7 @@
14
14
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
15
15
  * fails when it drifts.
16
16
  *
17
- * @module dsh-plugin-wsl/distro-route
17
+ * @module dsh-plugin-wsl-env/distro-route
18
18
  */
19
19
  import { uncToPosix } from "./paths.js";
20
20
  /**
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The doctor's live-resident merge (WSL-313): the one place where an
3
+ * in-process {@link AgentStatus} may be folded into a doctor report.
4
+ *
5
+ * The doctor CLI (`scripts/doctor.mjs`) is a separate process from the app,
6
+ * so its handshake check can only ever probe a fresh resident; a live
7
+ * resident's `deaths` and `lastError` are host-process state with no IPC to
8
+ * reach them. This module is the sanctioned alternative: callers that hold
9
+ * the plugin's context (the app process itself, through
10
+ * {@link doctorReport}) merge the resident's own `status()` snapshot into a
11
+ * collected report — no transport is invented, and a status that answers for
12
+ * no resident (pid unknown) changes nothing.
13
+ *
14
+ * @module dsh-plugin-wsl-env/doctor-live
15
+ */
16
+ import type { AgentStatus } from "./agent.js";
17
+ /** One check line of the doctor's versioned JSON report. */
18
+ export interface DoctorCheck {
19
+ /** The stable check id, e.g. `agent.handshake`. */
20
+ id: string;
21
+ /** `ok` | `warn` | `fail` | `info`. */
22
+ status: string;
23
+ /** The human detail line. */
24
+ detail?: string;
25
+ /** The copy-paste remedy, when the status is not `ok`. */
26
+ remedy?: string;
27
+ }
28
+ /** The doctor's versioned JSON report shape (`schemaVersion`). */
29
+ export interface DoctorReport {
30
+ /** The report schema version. */
31
+ schemaVersion: number;
32
+ /** Every check, in report order. */
33
+ checks: DoctorCheck[];
34
+ /** Per-status tally. */
35
+ summary: Record<string, number>;
36
+ }
37
+ /**
38
+ * Fold an in-process resident's lifecycle snapshot into a doctor report's
39
+ * `agent.handshake` check.
40
+ *
41
+ * The merge fires only when the status answers for a resident this process
42
+ * actually holds (`pid` known) — a never-started singleton's snapshot says
43
+ * nothing about the app's resident and is ignored, keeping the fresh-probe
44
+ * report honest. A `dead` resident fails the check; a `lastError` on a
45
+ * running resident surfaces as `warn` (the revival budget may already have
46
+ * recovered it); the digest facts the fresh probe established are kept.
47
+ *
48
+ * @param report - the collected doctor report (not mutated).
49
+ * @param status - the in-process `status()` snapshot, when reachable.
50
+ * @returns the report with the merged handshake check; the input object when
51
+ * there is nothing to merge.
52
+ */
53
+ export declare function mergeLiveResidentStatus(report: DoctorReport, status: AgentStatus | undefined): DoctorReport;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Fold an in-process resident's lifecycle snapshot into a doctor report's
3
+ * `agent.handshake` check.
4
+ *
5
+ * The merge fires only when the status answers for a resident this process
6
+ * actually holds (`pid` known) — a never-started singleton's snapshot says
7
+ * nothing about the app's resident and is ignored, keeping the fresh-probe
8
+ * report honest. A `dead` resident fails the check; a `lastError` on a
9
+ * running resident surfaces as `warn` (the revival budget may already have
10
+ * recovered it); the digest facts the fresh probe established are kept.
11
+ *
12
+ * @param report - the collected doctor report (not mutated).
13
+ * @param status - the in-process `status()` snapshot, when reachable.
14
+ * @returns the report with the merged handshake check; the input object when
15
+ * there is nothing to merge.
16
+ */
17
+ export function mergeLiveResidentStatus(report, status) {
18
+ if (!status || status.pid == null)
19
+ return report;
20
+ const fresh = report.checks.find((check) => check.id === "agent.handshake");
21
+ if (!fresh)
22
+ return report;
23
+ const errorLine = status.lastError.length > 0 ? `; lastError: ${status.lastError}` : "";
24
+ const merged = {
25
+ id: fresh.id,
26
+ status: status.state === "dead" ? "fail" : status.lastError.length > 0 ? "warn" : fresh.status,
27
+ detail: `probed: the live resident (pid ${status.pid}, state ${status.state}, deaths ${status.deaths}, pending ${status.pending})${errorLine}; fresh-probe facts: ${fresh.detail ?? ""}`,
28
+ ...(fresh.remedy !== undefined ? { remedy: fresh.remedy } : {}),
29
+ };
30
+ const checks = report.checks.map((check) => (check === fresh ? merged : check));
31
+ const summary = { ...report.summary };
32
+ if (merged.status !== fresh.status) {
33
+ summary[fresh.status] = Math.max(0, (summary[fresh.status] ?? 0) - 1);
34
+ summary[merged.status] = (summary[merged.status] ?? 0) + 1;
35
+ }
36
+ return { ...report, checks, summary };
37
+ }
@@ -42,7 +42,7 @@
42
42
  * file and run `pnpm run build` — the artifact under `lib/` is generated, and
43
43
  * `pnpm test` fails when it drifts.
44
44
  *
45
- * @module dsh-plugin-wsl/file-reference-wsl
45
+ * @module dsh-plugin-wsl-env/file-reference-wsl
46
46
  */
47
47
  import type { WslAgent } from "./agent.js";
48
48
  /**
@@ -42,7 +42,7 @@
42
42
  * file and run `pnpm run build` — the artifact under `lib/` is generated, and
43
43
  * `pnpm test` fails when it drifts.
44
44
  *
45
- * @module dsh-plugin-wsl/file-reference-wsl
45
+ * @module dsh-plugin-wsl-env/file-reference-wsl
46
46
  */
47
47
  import { isWslUnc, posixToUnc, uncToPosix } from "./paths.js";
48
48
  /** Upstream's default cap on one workspace index; the per-directory listing
@@ -15,7 +15,7 @@
15
15
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
16
16
  * `pnpm test` fails when it drifts.
17
17
  *
18
- * @module dsh-plugin-wsl/fs-decisions
18
+ * @module dsh-plugin-wsl-env/fs-decisions
19
19
  */
20
20
  /** A refusal descriptor the provider wraps in `FsError` at one choke point. */
21
21
  export interface Refusal {
@@ -15,7 +15,7 @@
15
15
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
16
16
  * `pnpm test` fails when it drifts.
17
17
  *
18
- * @module dsh-plugin-wsl/fs-decisions
18
+ * @module dsh-plugin-wsl-env/fs-decisions
19
19
  */
20
20
  import { isAnotherDistrosUnc } from "./paths.js";
21
21
  /**
@@ -28,7 +28,7 @@
28
28
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
29
29
  * fails when it drifts.
30
30
  *
31
- * @module dsh-plugin-wsl/fs-routing
31
+ * @module dsh-plugin-wsl-env/fs-routing
32
32
  */
33
33
  import z from "@deepseek-ai/schemastery";
34
34
  import { LocalFileSystem } from "@deepseek-ai/dsh-fs-local";
package/lib/fs-routing.js CHANGED
@@ -28,7 +28,7 @@
28
28
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
29
29
  * fails when it drifts.
30
30
  *
31
- * @module dsh-plugin-wsl/fs-routing
31
+ * @module dsh-plugin-wsl-env/fs-routing
32
32
  */
33
33
  import { isWslUnc, uncToPosix } from "./paths.js";
34
34
  import z from "@deepseek-ai/schemastery";
@@ -28,20 +28,12 @@
28
28
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
29
29
  * `pnpm test` fails when it drifts.
30
30
  *
31
- * @module dsh-plugin-wsl/fs-substrate
31
+ * @module dsh-plugin-wsl-env/fs-substrate
32
32
  */
33
+ import type { PatchHunk } from "@deepseek-ai/dsh-fs-patch";
34
+ import { ResolvedTarget } from "./fsio-agent.js";
33
35
  import { DistroFs } from "./fsio-agent.js";
34
36
  import type { WslAgent } from "./agent.js";
35
- /**
36
- * A resolved target: the Linux spelling the model sees and the UNC identity
37
- * the harness keys on, as returned by {@linkcode AgentSubstrate#resolve}.
38
- */
39
- export interface ResolvedTarget {
40
- /** The canonical Linux path spelling. */
41
- displayPath: string;
42
- /** The UNC identity this target keys on. */
43
- targetKey: string;
44
- }
45
37
  /**
46
38
  * One distro's provider-shaped filesystem face.
47
39
  */
@@ -256,5 +248,92 @@ export declare class AgentSubstrate {
256
248
  before: string;
257
249
  after: string;
258
250
  }>;
251
+ /**
252
+ * Apply parsed hunks to a text file (protocol 7): the peer's `patchText`
253
+ * semantics, using the peer's own `applyHunks` for matching and its exact
254
+ * `FS_PATCH_MISMATCH` dialect, then the same guarded atomic publication a
255
+ * write takes. The hunks travel in-process; the distro sees one read and
256
+ * one guarded replace.
257
+ *
258
+ * @param target - the resolved regular file.
259
+ * @param hunks - ordered hunks from the caller's patch parse.
260
+ * @param expected - the version the patch was based on.
261
+ * @param signal - cancels the patch.
262
+ * @param policy - the resolved policy this patch was fenced with.
263
+ * @returns the pre/post text and the published version.
264
+ */
265
+ patchText(target: ResolvedTarget, hunks: readonly PatchHunk[], expected: {
266
+ version: string;
267
+ } | undefined, signal?: AbortSignal, policy?: {
268
+ mode: string;
269
+ workspaceRoot: string;
270
+ sessionId?: string;
271
+ } | undefined): Promise<{
272
+ version: string;
273
+ before: string;
274
+ after: string;
275
+ }>;
276
+ /**
277
+ * Remove one regular file (protocol 7), mirroring the peer's
278
+ * `deleteFile`: missing/stale/mismatched targets refuse, the read-basis is
279
+ * best effort and bounded by `diffBasisMaxBytes`, and the removal itself is
280
+ * re-guarded distro-side by the expected version.
281
+ *
282
+ * @param target - the resolved file.
283
+ * @param expected - the version the removal was based on.
284
+ * @param signal - cancels the removal.
285
+ * @param policy - the resolved policy this removal was fenced with.
286
+ * @returns the bounded pre-removal text basis.
287
+ */
288
+ deleteFile(target: ResolvedTarget, expected: {
289
+ version: string;
290
+ } | undefined, signal?: AbortSignal, policy?: {
291
+ mode: string;
292
+ workspaceRoot: string;
293
+ sessionId?: string;
294
+ } | undefined): Promise<{
295
+ before: string | null;
296
+ }>;
297
+ /**
298
+ * Move a regular file (protocol 7), mirroring the peer's `moveFile`: the
299
+ * source passes `requireFile`, the destination `writeTarget`, an optional
300
+ * hunk set transforms the text on the way, and the two effects (publish,
301
+ * remove) are reported separately — a destination that landed while the
302
+ * source survived returns `sourceDeleted: false` plus the failure.
303
+ *
304
+ * @param source - the resolved source.
305
+ * @param destination - the resolved destination.
306
+ * @param options - the hunks and the source/destination guards.
307
+ * @param signal - cancels before publication.
308
+ * @param policy - the resolved policy both paths run under.
309
+ * @returns the bounded source basis and the destination outcome.
310
+ */
311
+ moveFile(source: ResolvedTarget, destination: ResolvedTarget, options: {
312
+ hunks?: readonly PatchHunk[];
313
+ expectedSource?: {
314
+ version: string;
315
+ };
316
+ expectedDestination?: {
317
+ kind: string;
318
+ version?: string;
319
+ };
320
+ }, signal?: AbortSignal, policy?: {
321
+ mode: string;
322
+ workspaceRoot: string;
323
+ sessionId?: string;
324
+ } | undefined): Promise<{
325
+ sourceBefore: string | null;
326
+ sourceDeleted: boolean;
327
+ sourceFailure?: {
328
+ code: string;
329
+ message: string;
330
+ };
331
+ destination: {
332
+ operation: string;
333
+ version: string;
334
+ before: string | null;
335
+ after: string;
336
+ };
337
+ }>;
259
338
  }
260
339
  export default AgentSubstrate;
@@ -28,11 +28,17 @@
28
28
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
29
29
  * `pnpm test` fails when it drifts.
30
30
  *
31
- * @module dsh-plugin-wsl/fs-substrate
31
+ * @module dsh-plugin-wsl-env/fs-substrate
32
32
  */
33
+ import { applyHunks } from "@deepseek-ai/dsh-fs-patch";
34
+ import { reasonToCode } from "./fsio-agent.js";
33
35
  import { applyLiteralEdit, FsCodedError, normalizeLineEndings, restoreLineEndings } from "./fsio-text.js";
34
36
  import { DistroFs } from "./fsio-agent.js";
35
37
  import { posixToUnc, uncToPosix } from "./paths.js";
38
+ /**
39
+ * A resolved target: the Linux spelling the model sees and the UNC identity
40
+ * the harness keys on, as returned by {@linkcode AgentSubstrate#resolve}.
41
+ */
36
42
  /**
37
43
  * The provider's stat/lstat row, type-spelled the way the tool layer reads it.
38
44
  *
@@ -287,5 +293,200 @@ export class AgentSubstrate {
287
293
  };
288
294
  });
289
295
  }
296
+ /**
297
+ * Apply parsed hunks to a text file (protocol 7): the peer's `patchText`
298
+ * semantics, using the peer's own `applyHunks` for matching and its exact
299
+ * `FS_PATCH_MISMATCH` dialect, then the same guarded atomic publication a
300
+ * write takes. The hunks travel in-process; the distro sees one read and
301
+ * one guarded replace.
302
+ *
303
+ * @param target - the resolved regular file.
304
+ * @param hunks - ordered hunks from the caller's patch parse.
305
+ * @param expected - the version the patch was based on.
306
+ * @param signal - cancels the patch.
307
+ * @param policy - the resolved policy this patch was fenced with.
308
+ * @returns the pre/post text and the published version.
309
+ */
310
+ async patchText(target, hunks, expected, signal, policy) {
311
+ const mutationAgent = await this.mutationAgent(policy);
312
+ return this.lock(target.targetKey, async () => {
313
+ const linuxPath = this.linuxOf(target);
314
+ const existing = await this.fs.stat(linuxPath, { signal });
315
+ if (!existing)
316
+ throw new FsCodedError(`cannot patch "${target.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
317
+ if (existing.type !== "f")
318
+ throw new FsCodedError(`cannot patch "${target.displayPath}": not a regular file`, "FS_NOT_REGULAR_FILE");
319
+ if (expected && existing.version !== expected.version) {
320
+ throw new FsCodedError(`cannot patch "${target.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
321
+ }
322
+ const original = await this.fs.readForEdit(target, signal);
323
+ const result = applyHunks(original.content, hunks);
324
+ if (!result.ok) {
325
+ throw new FsCodedError(`cannot patch "${target.displayPath}": hunk at patch line ${result.failure.hunkLineNumber} (${result.failure.stage}) does not match`, "FS_PATCH_MISMATCH");
326
+ }
327
+ const content = restoreLineEndings(result.after, original.lineEndings);
328
+ await this.fs.writeFileAtomic(linuxPath, content, { mode: existing.mode, expectedVersion: existing.version, signal, agent: mutationAgent });
329
+ const after = await this.fs.stat(linuxPath, { signal });
330
+ return {
331
+ version: after ? after.version : `missing:${target.targetKey}`,
332
+ before: original.content,
333
+ after: result.after,
334
+ };
335
+ });
336
+ }
337
+ /**
338
+ * Remove one regular file (protocol 7), mirroring the peer's
339
+ * `deleteFile`: missing/stale/mismatched targets refuse, the read-basis is
340
+ * best effort and bounded by `diffBasisMaxBytes`, and the removal itself is
341
+ * re-guarded distro-side by the expected version.
342
+ *
343
+ * @param target - the resolved file.
344
+ * @param expected - the version the removal was based on.
345
+ * @param signal - cancels the removal.
346
+ * @param policy - the resolved policy this removal was fenced with.
347
+ * @returns the bounded pre-removal text basis.
348
+ */
349
+ async deleteFile(target, expected, signal, policy) {
350
+ const mutationAgent = await this.mutationAgent(policy);
351
+ if (!this.fs.supportsDestructive(mutationAgent)) {
352
+ throw new FsCodedError(`cannot delete "${target.displayPath}": the resident agent does not serve protocol-7 operations — restart the app to rebuild the resident`, "FS_IO_ERROR");
353
+ }
354
+ return this.lock(target.targetKey, async () => {
355
+ const linuxPath = this.linuxOf(target);
356
+ const existing = await this.fs.stat(linuxPath, { signal });
357
+ if (!existing)
358
+ throw new FsCodedError(`cannot delete "${target.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
359
+ if (existing.type !== "f")
360
+ throw new FsCodedError(`cannot delete "${target.displayPath}": not a regular file`, "FS_NOT_REGULAR_FILE");
361
+ if (expected && existing.version !== expected.version) {
362
+ throw new FsCodedError(`cannot delete "${target.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
363
+ }
364
+ const before = await this.fs.readTextForDiff(target, this.diffBasisMaxBytes, signal);
365
+ await this.fs.removeFile(linuxPath, expected?.version, signal, mutationAgent);
366
+ return { before };
367
+ });
368
+ }
369
+ /**
370
+ * Move a regular file (protocol 7), mirroring the peer's `moveFile`: the
371
+ * source passes `requireFile`, the destination `writeTarget`, an optional
372
+ * hunk set transforms the text on the way, and the two effects (publish,
373
+ * remove) are reported separately — a destination that landed while the
374
+ * source survived returns `sourceDeleted: false` plus the failure.
375
+ *
376
+ * @param source - the resolved source.
377
+ * @param destination - the resolved destination.
378
+ * @param options - the hunks and the source/destination guards.
379
+ * @param signal - cancels before publication.
380
+ * @param policy - the resolved policy both paths run under.
381
+ * @returns the bounded source basis and the destination outcome.
382
+ */
383
+ async moveFile(source, destination, options, signal, policy) {
384
+ if (source.targetKey === destination.targetKey) {
385
+ throw new FsCodedError(`cannot move "${source.displayPath}" to itself`, "FS_SAME_TARGET");
386
+ }
387
+ const mutationAgent = await this.mutationAgent(policy);
388
+ if (!this.fs.supportsDestructive(mutationAgent)) {
389
+ throw new FsCodedError(`cannot move "${source.displayPath}": the resident agent does not serve protocol-7 operations — restart the app to rebuild the resident`, "FS_IO_ERROR");
390
+ }
391
+ const keys = [source.targetKey, destination.targetKey].sort();
392
+ return this.lock(keys[0], () => this.lock(keys[1], async () => {
393
+ const sourcePath = this.linuxOf(source);
394
+ const destinationPath = this.linuxOf(destination);
395
+ const sourceInfo = await this.fs.stat(sourcePath, { signal });
396
+ if (!sourceInfo)
397
+ throw new FsCodedError(`cannot move "${source.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
398
+ if (sourceInfo.type !== "f")
399
+ throw new FsCodedError(`cannot move "${source.displayPath}": not a regular file`, "FS_NOT_REGULAR_FILE");
400
+ if (options.expectedSource && sourceInfo.version !== options.expectedSource.version) {
401
+ throw new FsCodedError(`cannot move "${source.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
402
+ }
403
+ const destinationExisting = await this.fs.stat(destinationPath, { signal });
404
+ if (destinationExisting && destinationExisting.type !== "f") {
405
+ throw new FsCodedError(`cannot write "${destination.displayPath}": not a regular file`, "FS_NOT_REGULAR_FILE");
406
+ }
407
+ if (options.expectedDestination?.kind === "replaceIfVersion") {
408
+ if (!destinationExisting)
409
+ throw new FsCodedError(`cannot write "${destination.displayPath}": file no longer exists`, "FS_STALE_VERSION");
410
+ if (destinationExisting.version !== options.expectedDestination.version) {
411
+ throw new FsCodedError(`cannot write "${destination.displayPath}": file changed since it was read`, "FS_STALE_VERSION");
412
+ }
413
+ }
414
+ else if (options.expectedDestination?.kind === "createIfAbsent" && destinationExisting) {
415
+ throw new FsCodedError(`cannot overwrite existing "${destination.displayPath}" without reading it first`, "FS_NOT_OBSERVED");
416
+ }
417
+ const original = await this.fs.readForEdit(source, signal);
418
+ let after = original.content;
419
+ let content = original.content;
420
+ if (options.hunks !== undefined) {
421
+ const patched = applyHunks(original.content, options.hunks);
422
+ if (!patched.ok) {
423
+ throw new FsCodedError(`cannot patch "${source.displayPath}": hunk at patch line ${patched.failure.hunkLineNumber} (${patched.failure.stage}) does not match`, "FS_PATCH_MISMATCH");
424
+ }
425
+ after = patched.after;
426
+ content = restoreLineEndings(after, original.lineEndings);
427
+ }
428
+ const destinationBefore = destinationExisting !== null && Buffer.byteLength(content, "utf8") < this.diffBasisMaxBytes
429
+ ? await this.fs.readTextForDiff(destination, this.diffBasisMaxBytes, signal)
430
+ : null;
431
+ // A hunk set transforms the text on the way, so the agent cannot copy
432
+ // the source bytes: publish the patched content through the guarded
433
+ // write, then remove the source (the two effects stay separate).
434
+ let destinationRow;
435
+ let sourceFailure;
436
+ if (options.hunks !== undefined) {
437
+ try {
438
+ await this.fs.writeFileAtomic(destinationPath, content, {
439
+ mode: sourceInfo.mode,
440
+ createIfAbsent: options.expectedDestination?.kind === "createIfAbsent" ? { displayPath: destination.displayPath } : undefined,
441
+ expectedVersion: options.expectedDestination?.kind === "replaceIfVersion" ? options.expectedDestination.version : undefined,
442
+ signal,
443
+ agent: mutationAgent,
444
+ });
445
+ const published = await this.fs.stat(destinationPath, { signal });
446
+ destinationRow = { version: published ? published.version : `missing:${destination.targetKey}` };
447
+ }
448
+ catch (error) {
449
+ throw error;
450
+ }
451
+ try {
452
+ await this.fs.removeFile(sourcePath, sourceInfo.version, signal, mutationAgent);
453
+ }
454
+ catch (error) {
455
+ sourceFailure = {
456
+ reason: error instanceof FsCodedError && error.code === "FS_STALE_VERSION" ? "stale" : "io",
457
+ message: error instanceof Error ? error.message : String(error),
458
+ };
459
+ }
460
+ }
461
+ else {
462
+ const moved = await this.fs.moveAtomic(sourcePath, destinationPath, {
463
+ mode: sourceInfo.mode,
464
+ guard: options.expectedDestination?.kind === "createIfAbsent" ? "no-replace" : "replace",
465
+ sourceVersion: sourceInfo.version,
466
+ destinationVersion: options.expectedDestination?.kind === "replaceIfVersion" ? options.expectedDestination.version : undefined,
467
+ signal,
468
+ agent: mutationAgent,
469
+ });
470
+ destinationRow = { version: moved.destination.version };
471
+ sourceFailure = moved.sourceFailure;
472
+ }
473
+ const destinationOutcome = {
474
+ operation: destinationExisting ? "update" : "create",
475
+ version: destinationRow.version,
476
+ before: destinationBefore,
477
+ after: normalizeLineEndings(after),
478
+ };
479
+ const sourceBefore = Buffer.byteLength(original.content, "utf8") < this.diffBasisMaxBytes ? original.content : null;
480
+ if (sourceFailure !== undefined) {
481
+ return {
482
+ sourceBefore,
483
+ destination: destinationOutcome,
484
+ sourceDeleted: false,
485
+ sourceFailure: { code: reasonToCode(sourceFailure.reason), message: sourceFailure.message },
486
+ };
487
+ }
488
+ return { sourceBefore, destination: destinationOutcome, sourceDeleted: true };
489
+ }));
490
+ }
290
491
  }
291
492
  export default AgentSubstrate;
package/lib/fs.d.ts CHANGED
@@ -5,6 +5,6 @@
5
5
  * `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
6
6
  * fails when it drifts.
7
7
  *
8
- * @module dsh-plugin-wsl/fs
8
+ * @module dsh-plugin-wsl-env/fs
9
9
  */
10
10
  export { WslFileSystem as default, WslFileSystem } from "./index.js";
package/lib/fs.js CHANGED
@@ -5,6 +5,6 @@
5
5
  * `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
6
6
  * fails when it drifts.
7
7
  *
8
- * @module dsh-plugin-wsl/fs
8
+ * @module dsh-plugin-wsl-env/fs
9
9
  */
10
10
  export { WslFileSystem as default, WslFileSystem } from "./index.js";