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.
Files changed (123) hide show
  1. package/CHANGELOG.md +274 -0
  2. package/CONTRIBUTING.md +37 -5
  3. package/README.md +60 -5
  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/client.d.ts +5 -0
  9. package/client.js +98 -0
  10. package/cordis.patch.yml +66 -1
  11. package/docs/AGENT-PROTOCOL-V7.md +175 -0
  12. package/docs/API.md +25 -0
  13. package/docs/ARCHITECTURE.md +14 -1
  14. package/docs/CONFIGURATION.md +47 -6
  15. package/docs/CONFIGURATION.zh.md +20 -4
  16. package/docs/DIAGNOSTICS.md +83 -0
  17. package/docs/DIAGNOSTICS.zh.md +73 -0
  18. package/docs/LIMITATIONS.md +41 -4
  19. package/docs/LIMITATIONS.zh.md +27 -11
  20. package/docs/MIGRATION.md +4 -4
  21. package/docs/PEER-PARITY.md +50 -33
  22. package/docs/QUICKSTART.md +37 -4
  23. package/docs/QUICKSTART.zh.md +16 -1
  24. package/docs/README.zh.md +43 -4
  25. package/docs/RELEASING.md +4 -1
  26. package/docs/REQUIREMENTS.zh.md +3 -2
  27. package/docs/ROADMAP-1.0.md +75 -19
  28. package/docs/UPSTREAM-FILE-REFERENCE-SEAM.md +8 -1
  29. package/docs/UPSTREAM-OPEN-IN-APP.md +33 -0
  30. package/docs/UPSTREAM-SESSION-NOTICE.md +3 -1
  31. package/docs/UPSTREAM-TERMINAL-TITLE.md +3 -0
  32. package/docs/root-fs-routing.md +3 -3
  33. package/lib/agent-confined.js +4 -4
  34. package/lib/agent-errors.d.ts +1 -1
  35. package/lib/agent-errors.js +1 -1
  36. package/lib/agent-exec.d.ts +1 -1
  37. package/lib/agent-exec.js +1 -1
  38. package/lib/agent-protocol.d.ts +58 -8
  39. package/lib/agent-protocol.js +40 -7
  40. package/lib/agent-shared.d.ts +1 -1
  41. package/lib/agent-shared.js +1 -1
  42. package/lib/agent.d.ts +171 -1
  43. package/lib/agent.js +448 -288
  44. package/lib/auto-preset.d.ts +1 -1
  45. package/lib/auto-preset.js +1 -1
  46. package/lib/bwrap.d.ts +1 -1
  47. package/lib/bwrap.js +1 -1
  48. package/lib/client-host.d.ts +29 -0
  49. package/lib/client-host.js +29 -0
  50. package/lib/cwd-prompt-tool.d.ts +88 -0
  51. package/lib/cwd-prompt-tool.js +94 -0
  52. package/lib/cwd-prompt.d.ts +166 -0
  53. package/lib/cwd-prompt.js +203 -0
  54. package/lib/distro-route.d.ts +1 -1
  55. package/lib/distro-route.js +1 -1
  56. package/lib/doctor-live.d.ts +57 -0
  57. package/lib/doctor-live.js +38 -0
  58. package/lib/file-reference-wsl.d.ts +1 -1
  59. package/lib/file-reference-wsl.js +1 -1
  60. package/lib/fs-decisions.d.ts +1 -1
  61. package/lib/fs-decisions.js +1 -1
  62. package/lib/fs-routing.d.ts +1 -1
  63. package/lib/fs-routing.js +1 -1
  64. package/lib/fs-substrate.d.ts +90 -11
  65. package/lib/fs-substrate.js +202 -1
  66. package/lib/fs.d.ts +1 -1
  67. package/lib/fs.js +1 -1
  68. package/lib/fsio-agent.d.ts +128 -4
  69. package/lib/fsio-agent.js +224 -15
  70. package/lib/fsio-text.d.ts +1 -1
  71. package/lib/fsio-text.js +1 -1
  72. package/lib/git-exec.d.ts +1 -1
  73. package/lib/git-exec.js +1 -1
  74. package/lib/git-route.d.ts +1 -1
  75. package/lib/git-route.js +1 -1
  76. package/lib/index.d.ts +105 -30
  77. package/lib/index.js +272 -130
  78. package/lib/listing.d.ts +1 -1
  79. package/lib/listing.js +1 -1
  80. package/lib/messages.d.ts +157 -0
  81. package/lib/messages.js +199 -0
  82. package/lib/native-agent.d.ts +1 -1
  83. package/lib/native-agent.js +2 -2
  84. package/lib/paths.d.ts +6 -3
  85. package/lib/paths.js +4 -4
  86. package/lib/picker.d.ts +1 -1
  87. package/lib/picker.js +7 -7
  88. package/lib/ports.d.ts +1 -1
  89. package/lib/ports.js +1 -1
  90. package/lib/preflight.d.ts +25 -1
  91. package/lib/preflight.js +112 -12
  92. package/lib/sandbox-core.d.ts +1 -1
  93. package/lib/sandbox-core.js +9 -16
  94. package/lib/sandbox.d.ts +1 -1
  95. package/lib/sandbox.js +1 -1
  96. package/lib/search-exec.d.ts +1 -1
  97. package/lib/search-exec.js +1 -1
  98. package/lib/search-route.d.ts +1 -1
  99. package/lib/search-route.js +1 -1
  100. package/lib/session-runtime.d.ts +1 -1
  101. package/lib/session-runtime.js +2 -2
  102. package/lib/session-scope.d.ts +1 -1
  103. package/lib/session-scope.js +11 -6
  104. package/lib/shell-env.d.ts +26 -1
  105. package/lib/shell-env.js +57 -1
  106. package/lib/shell.d.ts +4 -2
  107. package/lib/shell.js +4 -2
  108. package/lib/subprocess.d.ts +1 -1
  109. package/lib/subprocess.js +2 -2
  110. package/lib/terminal-activity.d.ts +1 -1
  111. package/lib/terminal-activity.js +1 -1
  112. package/lib/terminal-route.d.ts +1 -1
  113. package/lib/terminal-route.js +1 -1
  114. package/lib/update-check.d.ts +197 -0
  115. package/lib/update-check.js +211 -0
  116. package/lib/watcher.d.ts +1 -1
  117. package/lib/watcher.js +1 -1
  118. package/lib/wsl-doctor-tool.d.ts +122 -0
  119. package/lib/wsl-doctor-tool.js +161 -0
  120. package/lib/wsl.d.ts +1 -31
  121. package/lib/wsl.js +1 -21
  122. package/package.json +55 -6
  123. package/scripts/doctor.mjs +1196 -166
@@ -3,7 +3,9 @@
3
3
  Status: measured and deferred (2026-10-19; checked against a
4
4
  `dsh-v0.2.1-rc.2`-era upstream — the checked version was not recorded at
5
5
  the time. Plugin-side behavior re-verified 2026-11-02 against the current
6
- peer floor, `>=0.2.1-rc.3`). The `@` completion in
6
+ peer floor, `>=0.2.1-rc.3`). Online check 2026-11-06: upstream `v0.2.2-rc.1` (2026-11-04) still mounts no
7
+ `FileReferenceTraversal`; the interface lives in open discussion
8
+ [#10264](https://github.com/deepseek-ai/deepseek-harness/discussions/10264) with no implementation. The `@` completion in
7
9
  `@deepseek-ai/dsh-file-reference-local` walks the workspace with
8
10
  `node:fs/promises` (`readdir` + `lstat`) from the **Windows** harness process.
9
11
  For a WSL workspace that walk crosses the 9p share on every directory; the
@@ -71,6 +73,11 @@ or re-implementing the ranking engine — the regression path the RFC rules out.
71
73
  **Fallback: keep the 9p walk.** `@` completion works today; it pays the
72
74
  per-directory share cost above. There is no plugin-side optimization that is
73
75
  safe without the seam, and the plugin does not replace the upstream module.
76
+ Maintainer observation (2026-11-06, real use): completion latency is
77
+ acceptable in practice — the human scanning the candidate list dominates the
78
+ walk, so the measured 56× is not the pain point. The seam stays a passive
79
+ wire-up when it lands (the canary watches the peer line); no active upstream
80
+ push is planned.
74
81
 
75
82
  Wire `file-reference-wsl` when either happens:
76
83
 
@@ -7,6 +7,39 @@ start as Node, exit 1, and the UI reports launch-failed. File Explorer and
7
7
  other non-Electron launchers work. The failure is the same on a Windows
8
8
  folder and on a WSL UNC workspace.
9
9
 
10
+ Update 2026-11-06: the `0.2.1-rc.5` line cleared the flag in
11
+ `launchDetachedApp` (`clearedElectronFlag`, extracted in
12
+ [app.asar.0.2.1-rc.5.open-in-app.index.js](upstream/app.asar.0.2.1-rc.5.open-in-app.index.js)),
13
+ and **`0.2.2-rc.1` lost that clearing** — the regression is back on the
14
+ current runtime. The local machine carries a re-applied patch to the
15
+ installed `app.asar` (same helper, both `lib/index.js` and
16
+ `lib/types/resolver.js` of `@deepseek-ai/dsh-host-open-in-app`);
17
+ `test/probe/open-in-app-patch.sh` detects which state an install is in
18
+ (upstream-fixed / locally-patched / broken) and must be run after every app
19
+ upgrade, because an upgrade replaces the archive and silently drops the
20
+ local patch.
21
+
22
+ ### Local patch 2 — WSL workspaces open through `--folder-uri` (verified 2026-11-06)
23
+
24
+ Riding on the same re-patch: a WSL UNC workspace
25
+ (`\\wsl.localhost\<distro>\...`) passed as a plain path opens a plain-folder
26
+ window where every command runs on the Windows side, and VS Code only
27
+ *suggests* "Reopen Folder in WSL". The patched `launchArgs` converts a WSL
28
+ UNC path (both `wsl.localhost` and `wsl$` spellings) to
29
+ `vscode-remote://wsl%2B<distro>/<posix path>` and passes it as
30
+ `--folder-uri=<uri>` — but only when the launch command is an Electron
31
+ editor (`Code.exe`, `Code - Insiders.exe`, `Cursor.exe`, `Windsurf.exe`);
32
+ every other launcher (terminals, Explorer) keeps the literal path, and
33
+ drive-letter paths are never rewritten. Two shapes were tried before this
34
+ one settled: the URI as a positional argument opens an untitled *file* whose
35
+ content is the URI text, so the explicit `--folder-uri=` form is required.
36
+ The patched module is archived in
37
+ [app.asar.0.2.2-rc.1.open-in-app.index.js](upstream/app.asar.0.2.2-rc.1.open-in-app.index.js).
38
+
39
+ Upstream ask, extended: besides clearing `ELECTRON_RUN_AS_NODE`, resolve a
40
+ WSL UNC workspace to its `vscode-remote` URI for Electron editors, so the
41
+ launched window is already in Remote-WSL.
42
+
10
43
  User-facing note: [LIMITATIONS.md](LIMITATIONS.md#host-open-in-app).
11
44
 
12
45
  ## Environment
@@ -3,7 +3,9 @@
3
3
  Status: filed upstream as
4
4
  [deepseek-ai/deepseek-harness#10263](https://github.com/deepseek-ai/deepseek-harness/discussions/10263)
5
5
  (version line `0.2.1-rc.x`; the gap was checked
6
- against `dsh-v0.2.1-rc.2` on 2026-10-19). The exact filing payload (title, body,
6
+ against `dsh-v0.2.1-rc.2` on 2026-10-19). Online check 2026-11-06: upstream `v0.2.2-rc.1`
7
+ (2026-11-04) still ships no session-notice seam, and the discussion is open with no
8
+ maintainer response. The exact filing payload (title, body,
7
9
  command) is [proposal-session-notice.md](https://github.com/Big-Dao/dsh-plugin-wsl-env/blob/main/docs/upstream/proposal-session-notice.md). The plugin ships the workaround
8
10
  described at the end: the advisory goes to the log **and** into the session's
9
11
  runtime context, which is model-visible and appears in the session transcript,
@@ -4,6 +4,9 @@ Status: proposed upstream as
4
4
  [deepseek-ai/deepseek-harness#8769](https://github.com/deepseek-ai/deepseek-harness/discussions/8769)
5
5
  (targeting `packages/api/terminal-controller`, version line `0.2.1-rc.x`; the
6
6
  gap was re-checked against `dsh-v0.2.1-rc.2` on 2026-10-17 and is unchanged —
7
+ and an online check on 2026-11-06 found upstream `v0.2.2-rc.1` (2026-11-04)
8
+ still without it (release notes to that version add sidebar-terminal FONT
9
+ settings, not a display-name seam) —
7
10
  `packages/api/terminal-controller/src/index.ts:327` still builds the terminal
8
11
  info with `title: shell.name`, and the spawn spec still carries no display-name
9
12
  field). The
@@ -4,7 +4,7 @@
4
4
  > `lib/fs-routing.js` 是其编译产物;当前开发依赖及 peer 范围以
5
5
  > `package.json` 为准。
6
6
  > 时效:§5 的验证与回退小节于 2026-10-31 对照现行测试与探针清单复核
7
- > (组合测试 12 项、单测 4 项、真机套件 15 项含 `fs-routing.sh`);
7
+ > (组合测试 12 项、单测 4 项、真机套件 17 项含 `fs-routing.sh`、`two-distro.sh` 与 `agent-restart.sh`,2026-11-06 复核);
8
8
  > 其余章节保留设计时的历史表述。
9
9
  > 关联:promotion-map 附录 C(workspace-files 接管评审)、附录 B(剩余 9P
10
10
  > 消费者)、`docs/upstream/rfc-wsl-workspaces.md` 增补(根平面路由 fs 的
@@ -156,8 +156,8 @@ export `./fs-routing`)。config 承载两侧旋钮:宿主侧 `cwd`/`diffBasi
156
156
  `distroContains`/`inputIsDistro`/`linuxJoin`);类本体改由上一项的组合测试经
157
157
  服务代理覆盖。该文件自 2026-10-19 起已在 `test:unit` 与 `test:coverage`
158
158
  清单里(`docs/REQUIREMENTS.zh.md` 的 OI-7,已解决)。
159
- - 真机探针全套(`test/probe/run-all-when-closed.sh --include-fs`,14 项常规
160
- + `run.sh` 共 15 项)覆盖 agent、watch、substrate 等底座,并包含本路由的
159
+ - 真机探针全套(`test/probe/run-all-when-closed.sh --include-fs`,16 项常规
160
+ + `run.sh` 共 17 项,2026-11-06 起)覆盖 agent、watch、substrate 等底座,并包含本路由的
161
161
  `test/probe/fs-routing.sh`:临时 profile 以本路由为根 `fs`,经 UNC 坐标读
162
162
  distro 内 ext4 文件、经盘符路径读 Windows 文件,并拒绝另一 distro 的 UNC。
163
163
 
@@ -29,7 +29,7 @@
29
29
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
30
30
  * `pnpm test` fails when it drifts.
31
31
  *
32
- * @module dsh-plugin-wsl/agent-confined
32
+ * @module dsh-plugin-wsl-env/agent-confined
33
33
  */
34
34
  import { bwrapArgvPrefix } from "./bwrap.js";
35
35
  import { toLinuxPath } from "./paths.js";
@@ -91,10 +91,10 @@ export async function runSpawnChecklist({ wslPath, distro, root, runFn }) {
91
91
  }
92
92
  const named = /workspace-root|interpreter|base64|sha256sum/.exec(detail)?.[0];
93
93
  if (named !== undefined) {
94
- throw new Error(`spawn checklist step '${named}' failed in ${distro}`);
94
+ throw new Error(`dsh-plugin-wsl-env: the confined spawn checklist step '${named}' failed in ${distro}`);
95
95
  }
96
96
  if (detail !== "") {
97
- throw new Error(`spawn checklist could not run in ${distro}: ${detail.slice(0, 200)}`);
97
+ throw new Error(`dsh-plugin-wsl-env: the confined spawn checklist could not run in ${distro}: ${detail.slice(0, 200)}`);
98
98
  }
99
99
  }
100
100
  /**
@@ -109,7 +109,7 @@ export function confinedAgent({ distro, wslPath = "wsl.exe", policy, maskWindows
109
109
  const key = `${wslPath}\0${distro}\0${policy.mode}\0${policy.workspaceRoot}\0${maskWindowsDrive ? "masked" : "visible"}\0${realTmp ? "real" : "ephemeral"}`;
110
110
  let agent = confinedAgents.get(key);
111
111
  if (agent === undefined) {
112
- const linuxRoot = toLinuxPath(policy.workspaceRoot, { distro });
112
+ const linuxRoot = toLinuxPath(policy.workspaceRoot);
113
113
  agent = new WslAgent({
114
114
  distro,
115
115
  wslPath,
@@ -8,7 +8,7 @@
8
8
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
9
9
  * `pnpm test` fails when it drifts.
10
10
  *
11
- * @module dsh-plugin-wsl/agent-errors
11
+ * @module dsh-plugin-wsl-env/agent-errors
12
12
  */
13
13
  /**
14
14
  * The abort reason the execution layer uses when its timer fires. The timeout
@@ -8,7 +8,7 @@
8
8
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
9
9
  * `pnpm test` fails when it drifts.
10
10
  *
11
- * @module dsh-plugin-wsl/agent-errors
11
+ * @module dsh-plugin-wsl-env/agent-errors
12
12
  */
13
13
  /**
14
14
  * The abort reason the execution layer uses when its timer fires. The timeout
@@ -26,7 +26,7 @@
26
26
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
27
27
  * fails when it drifts.
28
28
  *
29
- * @module dsh-plugin-wsl/agent-exec
29
+ * @module dsh-plugin-wsl-env/agent-exec
30
30
  */
31
31
  /** The request one agent `exec` carries. */
32
32
  export interface AgentExecRequest {
package/lib/agent-exec.js CHANGED
@@ -26,7 +26,7 @@
26
26
  * run `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
27
27
  * fails when it drifts.
28
28
  *
29
- * @module dsh-plugin-wsl/agent-exec
29
+ * @module dsh-plugin-wsl-env/agent-exec
30
30
  */
31
31
  import { BASH_TIMEOUT_REASON, CwdError } from "./agent-errors.js";
32
32
  /**
@@ -11,10 +11,16 @@
11
11
  * to `lib/agent-protocol.js`; edit THIS file and run `pnpm run build` — the
12
12
  * artifact under `lib/` is generated, and `pnpm test` fails when it drifts.
13
13
  *
14
- * @module dsh-plugin-wsl/agent-protocol
14
+ * @module dsh-plugin-wsl-env/agent-protocol
15
15
  */
16
- /** The protocol version this host side speaks; the agent must match. */
17
- export declare const PROTOCOL_VERSION = 6;
16
+ /** The protocol version this host side speaks. */
17
+ export declare const PROTOCOL_VERSION = 7;
18
+ /**
19
+ * The oldest agent protocol this host still accepts. A v6 resident keeps
20
+ * working: every v7 frame is gated on the HELLO version, so a half-updated
21
+ * mirror (one side at 6) degrades to v6 behaviour instead of desyncing.
22
+ */
23
+ export declare const MIN_PROTOCOL_VERSION = 6;
18
24
  /**
19
25
  * Upper bound on one `read` op's payload, set by the HOST looping over windowed
20
26
  * requests. Keeping each frame's decoded payload here means the agent's RES
@@ -126,9 +132,10 @@ export interface FsFrameRequest {
126
132
  * count, followed by one base64 line per argument.
127
133
  *
128
134
  * Ops (see `agent/wsl-agent.sh` for the server side and the wire grammar):
129
- * `stat`, `lstat`, `list`, `realpath`, `read`, `write`. Responses reuse the
130
- * EXEC `ACK`/`RES`/`ERR` lines, so the request machinery (pending map, KILL,
131
- * the watchdog) is shared unchanged.
135
+ * `stat`, `lstat`, `list`, `realpath`, `read`, `write`, and — protocol 7 —
136
+ * `delete` and `move`. Responses reuse the EXEC `ACK`/`RES`/`ERR`/`FSERR`
137
+ * lines, so the request machinery (pending map, KILL, the watchdog) is
138
+ * shared unchanged.
132
139
  *
133
140
  * The argument contract: callers pass PLAINTEXT values here; this frame is the
134
141
  * single base64 boundary, and the agent decodes each argument once. A write's
@@ -139,8 +146,32 @@ export interface FsFrameRequest {
139
146
  * @returns the lines to write to the agent's stdin, in order.
140
147
  */
141
148
  export declare function encodeFsFrame({ id, op, args, timeoutMs }: FsFrameRequest): string[];
149
+ /** One op inside a batch frame — the same shape as an FS frame's op and args. */
150
+ export interface FsBatchOp {
151
+ /** The op name (`stat`, `lstat`, `list`, `read`, … — the FS vocabulary). */
152
+ op: string;
153
+ /** Op arguments, one wire line each. */
154
+ args: string[];
155
+ }
156
+ /**
157
+ * Build the request lines for one batched FS frame — protocol 7. The shape is
158
+ * a `FSB|<id>|<timeout seconds|0>|<n ops>` header followed by `n ops` records,
159
+ * each an `OP|<op>|<n args>` line plus its base64 arguments. The agent runs
160
+ * the ops in order in ONE worker and answers with one RES whose payload is
161
+ * the concatenated per-op records (`OK <i>|<b64 payload>` /
162
+ * `BAD <i>|<reason>|<b64 message>`) — so a GUI-preview-shaped
163
+ * stat+list+read costs one round trip instead of four to six.
164
+ *
165
+ * @param request - the batch request: an id, a timeout and the ordered ops.
166
+ * @returns the lines to write to the agent's stdin, in order.
167
+ */
168
+ export declare function encodeFsbFrame({ id, timeoutMs, ops }: {
169
+ id: string;
170
+ timeoutMs?: number;
171
+ ops: FsBatchOp[];
172
+ }): string[];
142
173
  /**
143
- * `HELLO|name|version|digest|pid` — `digest` is the sha256 of the script file
174
+ * `HELLO|name|version|digest|pid|caps` — `digest` is the sha256 of the script file
144
175
  * the agent is executing (empty when it could not hash it); the host compares
145
176
  * it against its own shipped copy. `pid` is the agent shell's PID inside the
146
177
  * distro, present from protocol 5 on: the host uses it to deliver an
@@ -148,6 +179,12 @@ export declare function encodeFsFrame({ id, op, args, timeoutMs }: FsFrameReques
148
179
  * the serial stdin loop cannot do by frame. It is absent when the agent
149
180
  * itself runs inside a PID namespace (the confined resident), where that PID
150
181
  * is not addressable from a second `wsl.exe`.
182
+ *
183
+ * `caps` (protocol 7) is a comma-separated capability token list — `conc`
184
+ * (FS ops may run concurrently and answer out of order), `batch` (the
185
+ * `FSB` batch frame is accepted). Absent or empty (a v6 peer, a fake)
186
+ * means no capabilities: the host never sends a v7 frame at a peer that
187
+ * did not advertise the capability.
151
188
  */
152
189
  export interface HelloMessage {
153
190
  type: "hello";
@@ -155,6 +192,7 @@ export interface HelloMessage {
155
192
  version: number;
156
193
  digest: string;
157
194
  pid?: number;
195
+ caps?: string[];
158
196
  }
159
197
  /** `PONG` — the liveness answer to `PING`. */
160
198
  export interface PongMessage {
@@ -190,6 +228,18 @@ export interface AgentErrorMessage {
190
228
  reason: string;
191
229
  message: string;
192
230
  }
231
+ /**
232
+ * `FSERR|id|reason|message` — protocol 7's structured FS failure: a failed FS
233
+ * op answers with this line instead of (or, from an older agent, in addition
234
+ * to) the `dsh-fs|reason|…` first-stderr-line convention. The host maps
235
+ * `reason` onto the `FsErrorCode` vocabulary directly.
236
+ */
237
+ export interface FsErrorMessage {
238
+ type: "fsError";
239
+ id: string;
240
+ reason: string;
241
+ message: string;
242
+ }
193
243
  /**
194
244
  * Any other line — never thrown; the host decides whether an unrecognized line
195
245
  * is fatal. (Anything during the handshake is; after it, garbage on stdout is
@@ -201,7 +251,7 @@ export interface UnknownMessage {
201
251
  line: string;
202
252
  }
203
253
  /** The line parser's verdict: the union of every message shape above. */
204
- export type AgentMessage = HelloMessage | PongMessage | AckMessage | ResultMessage | AgentErrorMessage | UnknownMessage;
254
+ export type AgentMessage = HelloMessage | PongMessage | AckMessage | ResultMessage | AgentErrorMessage | FsErrorMessage | UnknownMessage;
205
255
  /**
206
256
  * Parse one line of the agent's stdout.
207
257
  *
@@ -11,10 +11,16 @@
11
11
  * to `lib/agent-protocol.js`; edit THIS file and run `pnpm run build` — the
12
12
  * artifact under `lib/` is generated, and `pnpm test` fails when it drifts.
13
13
  *
14
- * @module dsh-plugin-wsl/agent-protocol
14
+ * @module dsh-plugin-wsl-env/agent-protocol
15
15
  */
16
- /** The protocol version this host side speaks; the agent must match. */
17
- export const PROTOCOL_VERSION = 6;
16
+ /** The protocol version this host side speaks. */
17
+ export const PROTOCOL_VERSION = 7;
18
+ /**
19
+ * The oldest agent protocol this host still accepts. A v6 resident keeps
20
+ * working: every v7 frame is gated on the HELLO version, so a half-updated
21
+ * mirror (one side at 6) degrades to v6 behaviour instead of desyncing.
22
+ */
23
+ export const MIN_PROTOCOL_VERSION = 6;
18
24
  /**
19
25
  * Upper bound on one `read` op's payload, set by the HOST looping over windowed
20
26
  * requests. Keeping each frame's decoded payload here means the agent's RES
@@ -102,9 +108,10 @@ export function encodeKill(id) {
102
108
  * count, followed by one base64 line per argument.
103
109
  *
104
110
  * Ops (see `agent/wsl-agent.sh` for the server side and the wire grammar):
105
- * `stat`, `lstat`, `list`, `realpath`, `read`, `write`. Responses reuse the
106
- * EXEC `ACK`/`RES`/`ERR` lines, so the request machinery (pending map, KILL,
107
- * the watchdog) is shared unchanged.
111
+ * `stat`, `lstat`, `list`, `realpath`, `read`, `write`, and — protocol 7 —
112
+ * `delete` and `move`. Responses reuse the EXEC `ACK`/`RES`/`ERR`/`FSERR`
113
+ * lines, so the request machinery (pending map, KILL, the watchdog) is
114
+ * shared unchanged.
108
115
  *
109
116
  * The argument contract: callers pass PLAINTEXT values here; this frame is the
110
117
  * single base64 boundary, and the agent decodes each argument once. A write's
@@ -121,6 +128,27 @@ export function encodeFsFrame({ id, op, args, timeoutMs = 0 }) {
121
128
  ...args.map((word) => encodeB64(word)),
122
129
  ];
123
130
  }
131
+ /**
132
+ * Build the request lines for one batched FS frame — protocol 7. The shape is
133
+ * a `FSB|<id>|<timeout seconds|0>|<n ops>` header followed by `n ops` records,
134
+ * each an `OP|<op>|<n args>` line plus its base64 arguments. The agent runs
135
+ * the ops in order in ONE worker and answers with one RES whose payload is
136
+ * the concatenated per-op records (`OK <i>|<b64 payload>` /
137
+ * `BAD <i>|<reason>|<b64 message>`) — so a GUI-preview-shaped
138
+ * stat+list+read costs one round trip instead of four to six.
139
+ *
140
+ * @param request - the batch request: an id, a timeout and the ordered ops.
141
+ * @returns the lines to write to the agent's stdin, in order.
142
+ */
143
+ export function encodeFsbFrame({ id, timeoutMs = 0, ops }) {
144
+ const timeoutSeconds = timeoutMs > 0 ? Math.ceil(timeoutMs / 1000) : 0;
145
+ const lines = [`FSB|${id}|${timeoutSeconds}|${ops.length}`];
146
+ for (const { op, args } of ops) {
147
+ lines.push(`OP|${op}|${args.length}`);
148
+ lines.push(...args.map((word) => encodeB64(word)));
149
+ }
150
+ return lines;
151
+ }
124
152
  /**
125
153
  * Parse one line of the agent's stdout.
126
154
  *
@@ -129,7 +157,7 @@ export function encodeFsFrame({ id, op, args, timeoutMs = 0 }) {
129
157
  */
130
158
  export function parseAgentLine(line) {
131
159
  if (line.startsWith("HELLO|")) {
132
- const [, name, version, digest, pid] = line.split("|");
160
+ const [, name, version, digest, pid, caps] = line.split("|");
133
161
  const agentPid = Number(pid);
134
162
  return {
135
163
  type: "hello",
@@ -137,6 +165,7 @@ export function parseAgentLine(line) {
137
165
  version: Number(version),
138
166
  digest: digest ?? "",
139
167
  ...(Number.isSafeInteger(agentPid) && agentPid > 0 ? { pid: agentPid } : {}),
168
+ ...(caps !== undefined && caps.length > 0 ? { caps: caps.split(",").filter((token) => token.length > 0) } : {}),
140
169
  };
141
170
  }
142
171
  if (line === "PONG")
@@ -163,5 +192,9 @@ export function parseAgentLine(line) {
163
192
  const [, id, reason, message] = line.split("|");
164
193
  return { type: "agentError", id: id ?? "", reason: reason ?? "protocol", message: decodeB64(message ?? "").toString("utf8") };
165
194
  }
195
+ if (line.startsWith("FSERR|")) {
196
+ const [, id, reason, message] = line.split("|");
197
+ return { type: "fsError", id: id ?? "", reason: reason ?? "io", message: decodeB64(message ?? "").toString("utf8") };
198
+ }
166
199
  return { type: "unknown", line };
167
200
  }
@@ -11,7 +11,7 @@
11
11
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
12
12
  * `pnpm test` fails when it drifts.
13
13
  *
14
- * @module dsh-plugin-wsl/agent-shared
14
+ * @module dsh-plugin-wsl-env/agent-shared
15
15
  */
16
16
  import { WslAgent } from "./agent.js";
17
17
  /** The agent script as a host-side path, for factories that need both spellings. */
@@ -11,7 +11,7 @@
11
11
  * and run `pnpm run build` — the artifact under `lib/` is generated, and
12
12
  * `pnpm test` fails when it drifts.
13
13
  *
14
- * @module dsh-plugin-wsl/agent-shared
14
+ * @module dsh-plugin-wsl-env/agent-shared
15
15
  */
16
16
  import { createHash } from "node:crypto";
17
17
  import { spawn } from "node:child_process";
package/lib/agent.d.ts CHANGED
@@ -37,7 +37,7 @@
37
37
  * `pnpm run build` — the artifact under `lib/` is generated, and `pnpm test`
38
38
  * fails when it drifts.
39
39
  *
40
- * @module dsh-plugin-wsl/agent
40
+ * @module dsh-plugin-wsl-env/agent
41
41
  */
42
42
  import type { ChildProcess } from "node:child_process";
43
43
  /** One settled command result, as `exec` resolves it. */
@@ -53,6 +53,12 @@ export interface ExecResult {
53
53
  stdout: boolean;
54
54
  stderr: boolean;
55
55
  };
56
+ /**
57
+ * Protocol 7's FS refusal token (`notfound`, `stale`, …), present only on a
58
+ * failed FS op answered with the structured FSERR line. FS consumers map it
59
+ * onto `FsErrorCode` directly; command results never carry one.
60
+ */
61
+ reason?: string;
56
62
  }
57
63
  /**
58
64
  * One request in flight on an agent: its frame, its settlement callbacks, and
@@ -248,6 +254,10 @@ export declare class WslAgent {
248
254
  process: ChildProcess | null;
249
255
  /** The lifecycle state. */
250
256
  state: "idle" | "starting" | "ready" | "dead";
257
+ /** The protocol version the resident announced in HELLO (0 before one). */
258
+ protocolVersion: number;
259
+ /** The capabilities the resident advertised in HELLO (`conc`, `batch`). */
260
+ caps: Set<string>;
251
261
  /** The in-flight start, shared by concurrent callers. */
252
262
  startPromise: Promise<void> | null;
253
263
  /** The in-flight start's settler, so close() can fail a start it interrupts. */
@@ -392,6 +402,29 @@ export declare class WslAgent {
392
402
  * @returns resolved once the state is `ready`.
393
403
  */
394
404
  private beginStart;
405
+ /**
406
+ * The transport's failure and exit wiring, in the order the spawn attaches
407
+ * it: a spawn error, the stdin EPIPE guard, the handshake stderr tail, the
408
+ * exit handler, the stdout reader, then the diagnostic stderr reader.
409
+ *
410
+ * @param child - the freshly spawned transport.
411
+ * @param current - whether this child is still the current generation.
412
+ * @param settleStart - the handshake settler.
413
+ * @param handshakeTimer - the handshake watchdog.
414
+ */
415
+ private wireAgentProcess;
416
+ /**
417
+ * The stdout line splitter: a bounded buffer instead of readline, which
418
+ * accumulates an over-long line whole before anyone can object — exactly the
419
+ * unbounded-capture hole. Protocol lines are ASCII by construction (ids,
420
+ * numbers, base64), so chunk-boundary decoding is not a concern.
421
+ *
422
+ * @param child - the transport to read.
423
+ * @param current - whether this child is still the current generation.
424
+ * @param settleStart - the handshake settler.
425
+ * @param handshakeTimer - the handshake watchdog.
426
+ */
427
+ private readAgentOutput;
395
428
  /**
396
429
  * One stdout line from the agent. The first must be a matching HELLO.
397
430
  *
@@ -400,6 +433,71 @@ export declare class WslAgent {
400
433
  * @param handshakeTimer - the handshake watchdog.
401
434
  */
402
435
  private handleLine;
436
+ /**
437
+ * The agent named itself. A refusal settles BEFORE killing: the exit event
438
+ * must not overwrite the real reason with the generic handshake message (the
439
+ * fake transports emit exit synchronously inside kill(); a real wsl.exe races
440
+ * it).
441
+ *
442
+ * @param message - the agent's HELLO.
443
+ * @param settleStart - the handshake settler.
444
+ * @param handshakeTimer - the handshake watchdog.
445
+ */
446
+ private acceptHandshake;
447
+ /**
448
+ * Why this HELLO is refused, or undefined when it is accepted: the name and
449
+ * version window first, then the digest of the script the distro actually
450
+ * read.
451
+ *
452
+ * @param message - the agent's HELLO.
453
+ * @returns the refusal reason, or undefined when the handshake is accepted.
454
+ */
455
+ private handshakeRefusal;
456
+ /**
457
+ * The agent dequeued this request: start its own budget clock NOW. Armed
458
+ * here instead of at submission, a queued request can no longer time out on
459
+ * a queue it never chose to join — and its watchdog can never kill the
460
+ * shared transport for a stall that was never its own. A request cancelled
461
+ * while it was queued is killed only now, when it is the one running.
462
+ *
463
+ * @param message - the agent's ACK.
464
+ */
465
+ private acceptAck;
466
+ /**
467
+ * Take the entry an answer settles, running the bookkeeping every answer
468
+ * shares: mark it answered, release its timers, drop it from the map, and
469
+ * move the queue on. An answer for an entry the caller already gave up on is
470
+ * noise: it cleans up and settles nothing.
471
+ *
472
+ * @param id - the request id the answer names.
473
+ * @returns the entry to settle, or undefined when there is nothing to settle.
474
+ */
475
+ private takeAnswered;
476
+ /**
477
+ * Settle a normal answer. A cancelled request keeps its entry only until this
478
+ * answer arrives.
479
+ *
480
+ * @param message - the agent's RES.
481
+ */
482
+ private settleResult;
483
+ /**
484
+ * Protocol 7's structured FS refusal: settle the pending FS request with the
485
+ * reason preserved, so the adapter maps the code directly instead of sniffing
486
+ * a synthesized stderr line.
487
+ *
488
+ * @param message - the agent's FSERR.
489
+ */
490
+ private settleFsError;
491
+ /**
492
+ * A refused request. A cwd rejection is the command's own failure, not the
493
+ * agent's — it becomes a CwdError so the execution layer can report it
494
+ * exactly like the one-shot path's relay text. A `spawn` rejection is the
495
+ * same class of failure on the native agent (it cannot distinguish a missing
496
+ * workdir), so it gets its own class and the executor retries it one-shot.
497
+ *
498
+ * @param message - the agent's ERR.
499
+ */
500
+ private settleAgentError;
403
501
  /**
404
502
  * The process died. Requests in flight ride exactly one rebuild; a second
405
503
  * death is permanent.
@@ -409,6 +507,33 @@ export declare class WslAgent {
409
507
  * @param signalName - the killing signal, null on a normal exit.
410
508
  */
411
509
  private handleProcessExit;
510
+ /**
511
+ * Sort the requests a dead transport left behind into the three fates the
512
+ * one rebuild allows. Only provably read-only FS requests ride it: whatever
513
+ * their first execution did, it changed nothing. A command or a write that
514
+ * was in flight has UNKNOWN state — replaying it would run non-idempotent
515
+ * work a second time, or report a guarded create that actually succeeded as a
516
+ * refusal — so it fails with that fact instead. An aborted or unsent entry
517
+ * was already answered, or never ran, and settles locally.
518
+ *
519
+ * @param carrying - the entries taken off the map before the rebuild.
520
+ * @returns the entries that ride the rebuild.
521
+ */
522
+ private partitionForRebuild;
523
+ /**
524
+ * Start the rebuild and hand it the replayable requests under fresh ids.
525
+ * A stale watchdog must not survive the rebuild: its closure kills
526
+ * `this.process`, which is by then the NEW transport.
527
+ *
528
+ * @param relayable - the requests `partitionForRebuild` cleared to replay.
529
+ */
530
+ private relayOverRebuild;
531
+ /**
532
+ * Fail every entry still on the map: the second death is permanent.
533
+ *
534
+ * @param message - the reason, already stored as `lastError`.
535
+ */
536
+ private rejectAllPending;
412
537
  /**
413
538
  * Register one request's pending entry and write its frame: the shared
414
539
  * request machinery (abort, submission, the ACK-armed watchdog). The
@@ -420,6 +545,25 @@ export declare class WslAgent {
420
545
  * @param request - the request to register.
421
546
  */
422
547
  private track;
548
+ /**
549
+ * Build one pending entry: the caller's settle wrappers (which record the
550
+ * metric once) and, when the request carries a budget, the stuck-request
551
+ * watchdog armed by the agent's ACK.
552
+ *
553
+ * @param request - the request being tracked.
554
+ * @param kind - the metric bucket, with the caller's default resolved.
555
+ * @returns the entry, not yet registered.
556
+ */
557
+ private newPendingRequest;
558
+ /**
559
+ * Wire the caller's abort. An unsent request dies immediately; a sent one
560
+ * writes the KILL frame and, when the request is running, arms the signal
561
+ * that interrupts the command where it lives.
562
+ *
563
+ * @param pending - the tracked request.
564
+ * @param signal - the caller's cancellation.
565
+ */
566
+ private attachCancellation;
423
567
  /** Send a tracked request, recording dispatch before synchronous responses. */
424
568
  private sendPending;
425
569
  /** Dispatch the next shell request only after the preceding answer. */
@@ -502,6 +646,32 @@ export declare class WslAgent {
502
646
  exitCode: number;
503
647
  stdout: Buffer;
504
648
  stderr: Buffer;
649
+ reason?: string;
650
+ }>;
651
+ /**
652
+ * Run one batched FS frame (protocol 7): the ordered ops execute in one
653
+ * worker and the resolution is ONE result whose stdout is the per-op
654
+ * record stream (`OK <i>|<b64>` / `BAD <i>|<reason>|<b64>`). The host sends
655
+ * this only to a resident that advertised `batch`; `DistroFs` gates it.
656
+ *
657
+ * @param request - the batch request.
658
+ * @returns the settled batch result.
659
+ */
660
+ fsb({ ops, timeoutMs, signal }: {
661
+ /** The ordered ops, each `{ op, args }` in the FS vocabulary. */
662
+ ops: Array<{
663
+ op: string;
664
+ args: string[];
665
+ }>;
666
+ /** In-distro timeout from the dequeue ACK; `<= 0` means none. */
667
+ timeoutMs?: number;
668
+ /** Cancels the request, sending KILL if it was already dispatched. */
669
+ signal?: AbortSignal;
670
+ }): Promise<{
671
+ exitCode: number;
672
+ stdout: Buffer;
673
+ stderr: Buffer;
674
+ reason?: string;
505
675
  }>;
506
676
  /**
507
677
  * Liveness probe, also used to warm the agent up.