dsh-plugin-wsl-env 0.18.0 → 0.21.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.
- package/CHANGELOG.md +274 -0
- package/CONTRIBUTING.md +37 -5
- package/README.md +60 -5
- package/SUPPORT.md +3 -3
- package/agent/native/linux-aarch64/dsh-wsl-agent +0 -0
- package/agent/native/linux-x86_64/dsh-wsl-agent +0 -0
- package/agent/wsl-agent.sh +331 -11
- package/client.d.ts +5 -0
- package/client.js +98 -0
- package/cordis.patch.yml +66 -1
- package/docs/AGENT-PROTOCOL-V7.md +175 -0
- package/docs/API.md +25 -0
- package/docs/ARCHITECTURE.md +14 -1
- package/docs/CONFIGURATION.md +47 -6
- package/docs/CONFIGURATION.zh.md +20 -4
- package/docs/DIAGNOSTICS.md +83 -0
- package/docs/DIAGNOSTICS.zh.md +73 -0
- package/docs/LIMITATIONS.md +41 -4
- package/docs/LIMITATIONS.zh.md +27 -11
- package/docs/MIGRATION.md +4 -4
- package/docs/PEER-PARITY.md +50 -33
- package/docs/QUICKSTART.md +37 -4
- package/docs/QUICKSTART.zh.md +16 -1
- package/docs/README.zh.md +43 -4
- package/docs/RELEASING.md +4 -1
- package/docs/REQUIREMENTS.zh.md +3 -2
- package/docs/ROADMAP-1.0.md +75 -19
- package/docs/UPSTREAM-FILE-REFERENCE-SEAM.md +8 -1
- package/docs/UPSTREAM-OPEN-IN-APP.md +33 -0
- package/docs/UPSTREAM-SESSION-NOTICE.md +3 -1
- package/docs/UPSTREAM-TERMINAL-TITLE.md +3 -0
- package/docs/root-fs-routing.md +3 -3
- package/lib/agent-confined.js +4 -4
- package/lib/agent-errors.d.ts +1 -1
- package/lib/agent-errors.js +1 -1
- package/lib/agent-exec.d.ts +1 -1
- package/lib/agent-exec.js +1 -1
- package/lib/agent-protocol.d.ts +58 -8
- package/lib/agent-protocol.js +40 -7
- package/lib/agent-shared.d.ts +1 -1
- package/lib/agent-shared.js +1 -1
- package/lib/agent.d.ts +171 -1
- package/lib/agent.js +448 -288
- package/lib/auto-preset.d.ts +1 -1
- package/lib/auto-preset.js +1 -1
- package/lib/bwrap.d.ts +1 -1
- package/lib/bwrap.js +1 -1
- package/lib/client-host.d.ts +29 -0
- package/lib/client-host.js +29 -0
- package/lib/cwd-prompt-tool.d.ts +88 -0
- package/lib/cwd-prompt-tool.js +94 -0
- package/lib/cwd-prompt.d.ts +166 -0
- package/lib/cwd-prompt.js +203 -0
- package/lib/distro-route.d.ts +1 -1
- package/lib/distro-route.js +1 -1
- package/lib/doctor-live.d.ts +57 -0
- package/lib/doctor-live.js +38 -0
- package/lib/file-reference-wsl.d.ts +1 -1
- package/lib/file-reference-wsl.js +1 -1
- package/lib/fs-decisions.d.ts +1 -1
- package/lib/fs-decisions.js +1 -1
- package/lib/fs-routing.d.ts +1 -1
- package/lib/fs-routing.js +1 -1
- package/lib/fs-substrate.d.ts +90 -11
- package/lib/fs-substrate.js +202 -1
- package/lib/fs.d.ts +1 -1
- package/lib/fs.js +1 -1
- package/lib/fsio-agent.d.ts +128 -4
- package/lib/fsio-agent.js +224 -15
- package/lib/fsio-text.d.ts +1 -1
- package/lib/fsio-text.js +1 -1
- package/lib/git-exec.d.ts +1 -1
- package/lib/git-exec.js +1 -1
- package/lib/git-route.d.ts +1 -1
- package/lib/git-route.js +1 -1
- package/lib/index.d.ts +105 -30
- package/lib/index.js +272 -130
- package/lib/listing.d.ts +1 -1
- package/lib/listing.js +1 -1
- package/lib/messages.d.ts +157 -0
- package/lib/messages.js +199 -0
- package/lib/native-agent.d.ts +1 -1
- package/lib/native-agent.js +2 -2
- package/lib/paths.d.ts +6 -3
- package/lib/paths.js +4 -4
- package/lib/picker.d.ts +1 -1
- package/lib/picker.js +7 -7
- package/lib/ports.d.ts +1 -1
- package/lib/ports.js +1 -1
- package/lib/preflight.d.ts +25 -1
- package/lib/preflight.js +112 -12
- package/lib/sandbox-core.d.ts +1 -1
- package/lib/sandbox-core.js +9 -16
- package/lib/sandbox.d.ts +1 -1
- package/lib/sandbox.js +1 -1
- package/lib/search-exec.d.ts +1 -1
- package/lib/search-exec.js +1 -1
- package/lib/search-route.d.ts +1 -1
- package/lib/search-route.js +1 -1
- package/lib/session-runtime.d.ts +1 -1
- package/lib/session-runtime.js +2 -2
- package/lib/session-scope.d.ts +1 -1
- package/lib/session-scope.js +11 -6
- package/lib/shell-env.d.ts +26 -1
- package/lib/shell-env.js +57 -1
- package/lib/shell.d.ts +4 -2
- package/lib/shell.js +4 -2
- package/lib/subprocess.d.ts +1 -1
- package/lib/subprocess.js +2 -2
- package/lib/terminal-activity.d.ts +1 -1
- package/lib/terminal-activity.js +1 -1
- package/lib/terminal-route.d.ts +1 -1
- package/lib/terminal-route.js +1 -1
- package/lib/update-check.d.ts +197 -0
- package/lib/update-check.js +211 -0
- package/lib/watcher.d.ts +1 -1
- package/lib/watcher.js +1 -1
- package/lib/wsl-doctor-tool.d.ts +122 -0
- package/lib/wsl-doctor-tool.js +161 -0
- package/lib/wsl.d.ts +1 -31
- package/lib/wsl.js +1 -21
- package/package.json +55 -6
- package/scripts/doctor.mjs +1196 -166
|
@@ -0,0 +1,57 @@
|
|
|
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` | `skipped`. */
|
|
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
|
+
/** The distro the check ran against (schema 2, distro-scoped checks). */
|
|
28
|
+
distro?: string;
|
|
29
|
+
/** Whether a `fail` here blocks the first command (schema 2). */
|
|
30
|
+
blocksFirstCommand?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** The doctor's versioned JSON report shape (`schemaVersion`). */
|
|
33
|
+
export interface DoctorReport {
|
|
34
|
+
/** The report schema version. */
|
|
35
|
+
schemaVersion: number;
|
|
36
|
+
/** Every check, in report order. */
|
|
37
|
+
checks: DoctorCheck[];
|
|
38
|
+
/** Per-status tally. */
|
|
39
|
+
summary: Record<string, number>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Fold an in-process resident's lifecycle snapshot into a doctor report's
|
|
43
|
+
* `agent.handshake` check.
|
|
44
|
+
*
|
|
45
|
+
* The merge fires only when the status answers for a resident this process
|
|
46
|
+
* actually holds (`pid` known) — a never-started singleton's snapshot says
|
|
47
|
+
* nothing about the app's resident and is ignored, keeping the fresh-probe
|
|
48
|
+
* report honest. A `dead` resident fails the check; a `lastError` on a
|
|
49
|
+
* running resident surfaces as `warn` (the revival budget may already have
|
|
50
|
+
* recovered it); the digest facts the fresh probe established are kept.
|
|
51
|
+
*
|
|
52
|
+
* @param report - the collected doctor report (not mutated).
|
|
53
|
+
* @param status - the in-process `status()` snapshot, when reachable.
|
|
54
|
+
* @returns the report with the merged handshake check; the input object when
|
|
55
|
+
* there is nothing to merge.
|
|
56
|
+
*/
|
|
57
|
+
export declare function mergeLiveResidentStatus(report: DoctorReport, status: AgentStatus | undefined): DoctorReport;
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
// The fresh check's shape fields (distro, blocksFirstCommand) ride through:
|
|
25
|
+
// the merge rewrites the verdict and the evidence, never the schema.
|
|
26
|
+
const merged = {
|
|
27
|
+
...fresh,
|
|
28
|
+
status: status.state === "dead" ? "fail" : status.lastError.length > 0 ? "warn" : fresh.status,
|
|
29
|
+
detail: `probed: the live resident (pid ${status.pid}, state ${status.state}, deaths ${status.deaths}, pending ${status.pending})${errorLine}; fresh-probe facts: ${fresh.detail ?? ""}`,
|
|
30
|
+
};
|
|
31
|
+
const checks = report.checks.map((check) => (check === fresh ? merged : check));
|
|
32
|
+
const summary = { ...report.summary };
|
|
33
|
+
if (merged.status !== fresh.status) {
|
|
34
|
+
summary[fresh.status] = Math.max(0, (summary[fresh.status] ?? 0) - 1);
|
|
35
|
+
summary[merged.status] = (summary[merged.status] ?? 0) + 1;
|
|
36
|
+
}
|
|
37
|
+
return { ...report, checks, summary };
|
|
38
|
+
}
|
|
@@ -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
|
package/lib/fs-decisions.d.ts
CHANGED
|
@@ -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 {
|
package/lib/fs-decisions.js
CHANGED
|
@@ -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
|
/**
|
package/lib/fs-routing.d.ts
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 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";
|
package/lib/fs-substrate.d.ts
CHANGED
|
@@ -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;
|
package/lib/fs-substrate.js
CHANGED
|
@@ -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
package/lib/fs.js
CHANGED