dsh-plugin-wsl-env 0.24.0 → 0.26.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 (94) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/CODE_OF_CONDUCT.md +85 -0
  3. package/CODE_OF_CONDUCT.zh.md +85 -0
  4. package/CONTRIBUTING.md +19 -3
  5. package/CONTRIBUTING.zh.md +200 -0
  6. package/README.md +18 -6
  7. package/SECURITY.md +12 -0
  8. package/SECURITY.zh.md +108 -0
  9. package/SUPPORT.md +2 -0
  10. package/SUPPORT.zh.md +99 -0
  11. package/client.js +409 -8
  12. package/cordis.patch.yml +17 -0
  13. package/docs/AGENT-PROTOCOL-V7.md +2 -2
  14. package/docs/API.md +2 -0
  15. package/docs/API.zh.md +38 -0
  16. package/docs/ARCHITECTURE.md +30 -9
  17. package/docs/ARCHITECTURE.zh.md +511 -0
  18. package/docs/COMPATIBILITY.md +2 -0
  19. package/docs/COMPATIBILITY.zh.md +117 -0
  20. package/docs/CONFIGURATION.md +45 -2
  21. package/docs/CONFIGURATION.zh.md +39 -3
  22. package/docs/DIAGNOSTICS.md +12 -2
  23. package/docs/DIAGNOSTICS.zh.md +5 -1
  24. package/docs/LIMITATIONS.md +60 -9
  25. package/docs/LIMITATIONS.zh.md +45 -9
  26. package/docs/MIGRATION.md +6 -3
  27. package/docs/MIGRATION.zh.md +91 -0
  28. package/docs/PEER-PARITY.md +63 -51
  29. package/docs/PERFORMANCE.md +2 -0
  30. package/docs/PERFORMANCE.zh.md +59 -0
  31. package/docs/QUICKSTART.md +21 -6
  32. package/docs/QUICKSTART.zh.md +16 -4
  33. package/docs/README.zh.md +28 -19
  34. package/docs/RELEASING.md +19 -14
  35. package/docs/REQUIREMENTS.zh.md +6 -3
  36. package/docs/ROADMAP-1.0.md +1 -1
  37. package/docs/UPSTREAM-FILE-REFERENCE-SEAM.md +1 -1
  38. package/docs/UPSTREAM-FSIO-EXPORT.md +5 -4
  39. package/docs/UPSTREAM-OPEN-IN-APP.md +33 -11
  40. package/docs/UPSTREAM-PROMPT-ADAPTERS.md +85 -0
  41. package/docs/UPSTREAM-SESSION-NOTICE.md +7 -0
  42. package/docs/VALUE.md +146 -0
  43. package/docs/VALUE.zh.md +15 -13
  44. package/docs/root-fs-routing.md +1 -1
  45. package/lib/agent-confined.d.ts +7 -1
  46. package/lib/agent-confined.js +3 -3
  47. package/lib/auto-preset-seam.d.ts +45 -0
  48. package/lib/auto-preset-seam.js +47 -0
  49. package/lib/auto-preset.d.ts +33 -10
  50. package/lib/auto-preset.js +148 -44
  51. package/lib/bwrap.d.ts +14 -0
  52. package/lib/bwrap.js +11 -0
  53. package/lib/cwd-prompt.d.ts +41 -3
  54. package/lib/cwd-prompt.js +125 -8
  55. package/lib/doctor-live.d.ts +30 -0
  56. package/lib/doctor-live.js +37 -0
  57. package/lib/doctor-report.d.ts +50 -0
  58. package/lib/doctor-report.js +172 -0
  59. package/lib/fs-routing.d.ts +1 -1
  60. package/lib/fs-routing.js +1 -1
  61. package/lib/fs.d.ts +1 -1
  62. package/lib/fs.js +1 -1
  63. package/lib/index.d.ts +6 -1464
  64. package/lib/index.js +5 -2120
  65. package/lib/messages.d.ts +20 -1
  66. package/lib/messages.js +31 -3
  67. package/lib/open-in-app.d.ts +206 -0
  68. package/lib/open-in-app.js +300 -0
  69. package/lib/preflight.d.ts +7 -4
  70. package/lib/preflight.js +27 -13
  71. package/lib/preset-choice.d.ts +13 -0
  72. package/lib/preset-choice.js +18 -1
  73. package/lib/provider-common.d.ts +58 -0
  74. package/lib/provider-common.js +123 -0
  75. package/lib/sandbox-core.d.ts +1 -0
  76. package/lib/sandbox-core.js +4 -2
  77. package/lib/session-runtime.d.ts +7 -1
  78. package/lib/session-runtime.js +9 -2
  79. package/lib/shell.d.ts +1 -1
  80. package/lib/shell.js +1 -1
  81. package/lib/status.js +2 -1
  82. package/lib/writable-roots.d.ts +109 -0
  83. package/lib/writable-roots.js +257 -0
  84. package/lib/wsl-doctor-command.js +2 -1
  85. package/lib/wsl-doctor-tool.js +2 -2
  86. package/lib/wsl-filesystem.d.ts +692 -0
  87. package/lib/wsl-filesystem.js +897 -0
  88. package/lib/wsl-shell.d.ts +742 -0
  89. package/lib/wsl-shell.js +965 -0
  90. package/lib/wsl.d.ts +1 -1
  91. package/lib/wsl.js +1 -1
  92. package/package.json +48 -24
  93. package/scripts/doctor.mjs +1 -1
  94. package/scripts/profile-preset.mjs +17 -5
@@ -0,0 +1,257 @@
1
+ /**
2
+ * The extra directories a WSL confinement grants read-write under
3
+ * `workspace-write`, and the one-time check that they are usable.
4
+ *
5
+ * `workspace-write` grants the session workspace and the distro's temp areas —
6
+ * nothing under the user's home. Toolchains do not stay inside the workspace:
7
+ * npm writes `~/.npm`, pnpm writes its store under `~/.local/share/pnpm`,
8
+ * cargo writes `~/.cargo/registry`, and a linked Git worktree writes the
9
+ * shared `.git` directory. This module turns the operator's grants (the plugin
10
+ * rows' `writableRoots` plus the shared policy's `additionalWritableRoots`)
11
+ * into the Linux paths the bwrap profile binds.
12
+ *
13
+ * ## Why the roots are filtered
14
+ *
15
+ * A grant is bound rw into the distro's mount table. If the bound path could
16
+ * be replaced by a symlink after the check, the next command would bind the
17
+ * link's target — the classic grant-redirection escape. The model can write
18
+ * only the workspace and the temp areas, so a grant whose parent is writable
19
+ * is not merely redundant, it is a redirection risk. Candidates already
20
+ * writable (inside the workspace, `/tmp` or `/dev/shm`) and candidates nested
21
+ * inside another grant are therefore dropped, not bound. Every effective root
22
+ * has a read-only parent, which is what makes the one-time physical-path
23
+ * check below sufficient for the model threat model.
24
+ *
25
+ * This module is peer-free so the rules are unit-testable in a bare checkout;
26
+ * `lib/provider-common.js` converts {@link WritableRootError} into the peer's
27
+ * `SandboxInvalidRootError` at the call sites.
28
+ *
29
+ * This is a TypeScript source built to `lib/writable-roots.js`; edit THIS file
30
+ * and run `pnpm run build` — the artifact under `lib/` is generated, and
31
+ * `pnpm test` fails when it drifts.
32
+ *
33
+ * @module dsh-plugin-wsl-env/writable-roots
34
+ */
35
+ import { isPosixAbsolute, isWslUnc, uncToPosix } from "./paths.js";
36
+ import { runCapture } from "./wsl.js";
37
+ /** A configured grant that cannot be used, with the reason the model-facing error carries. */
38
+ export class WritableRootError extends Error {
39
+ /** The entry as configured (or the policy's spelling). */
40
+ root;
41
+ /** Why it was refused. */
42
+ reason;
43
+ /**
44
+ * @param root - the entry as configured.
45
+ * @param reason - the model-facing reason.
46
+ */
47
+ constructor(root, reason) {
48
+ super(`Invalid additional writable directory ${JSON.stringify(root)}: ${reason}`);
49
+ this.name = "WritableRootError";
50
+ this.root = root;
51
+ this.reason = reason;
52
+ }
53
+ }
54
+ /** The temp areas `workspace-write` makes writable in every confinement shape. */
55
+ const TEMP_ROOTS = ["/tmp", "/dev/shm"];
56
+ /**
57
+ * Strip duplicate and trailing separators and resolve `.`/`..` lexically.
58
+ * The result is absolute; the caller has already checked the spelling.
59
+ *
60
+ * @param value - an absolute Linux path.
61
+ * @returns the normalized path.
62
+ */
63
+ function normalizeLinux(value) {
64
+ const segments = [];
65
+ for (const segment of value.split("/")) {
66
+ if (segment === "" || segment === ".")
67
+ continue;
68
+ if (segment === "..")
69
+ segments.pop();
70
+ else
71
+ segments.push(segment);
72
+ }
73
+ return `/${segments.join("/")}`;
74
+ }
75
+ /**
76
+ * Whether one Linux path is the other or lies below it.
77
+ *
78
+ * @param path - the normalized candidate.
79
+ * @param parent - the normalized parent.
80
+ * @returns true when `path` is contained in `parent`.
81
+ */
82
+ function contains(path, parent) {
83
+ if (parent === "/")
84
+ return true;
85
+ return path === parent || path.startsWith(`${parent}/`);
86
+ }
87
+ /**
88
+ * Expand and normalize one plugin-configured entry. The entry is a distro
89
+ * path, so a Windows or UNC spelling is a configuration mistake rather than a
90
+ * path the plugin could translate.
91
+ *
92
+ * @param value - the configured entry.
93
+ * @param home - the distro home for `~`.
94
+ * @returns the normalized absolute Linux path.
95
+ * @throws {WritableRootError} when the entry is not a distro absolute path.
96
+ */
97
+ function configuredRoot(value, home) {
98
+ if (typeof value !== "string" || value.length === 0 || value.includes("\u0000")) {
99
+ throw new WritableRootError(String(value), "must be a non-empty Linux path");
100
+ }
101
+ if (value === "~" || value.startsWith("~/")) {
102
+ if (home.length === 0)
103
+ throw new WritableRootError(value, "cannot expand ~ without the distro home");
104
+ return normalizeLinux(value === "~" ? home : `${home}/${value.slice(2)}`);
105
+ }
106
+ if (value.startsWith("~"))
107
+ throw new WritableRootError(value, "use ~/... or an absolute Linux path; ~user grants are not supported");
108
+ if (!isPosixAbsolute(value))
109
+ throw new WritableRootError(value, "must be an absolute Linux path or start with ~/");
110
+ return normalizeLinux(value);
111
+ }
112
+ /**
113
+ * Translate one shared-policy entry. The policy carries execution-world
114
+ * spelling, so a same-distro UNC is the workspace coordinate of a Linux path;
115
+ * a Linux path is already right; everything else is a different world.
116
+ *
117
+ * @param value - the policy entry.
118
+ * @param distro - the distro the workspace belongs to.
119
+ * @returns the normalized Linux path.
120
+ * @throws {WritableRootError} when the entry names another world.
121
+ */
122
+ function policyRoot(value, distro) {
123
+ if (typeof value !== "string" || value.length === 0 || value.includes("\u0000")) {
124
+ throw new WritableRootError(String(value), "must be a non-empty path");
125
+ }
126
+ if (isPosixAbsolute(value))
127
+ return normalizeLinux(value);
128
+ if (isWslUnc(value)) {
129
+ const parsed = uncToPosix(value);
130
+ if (parsed !== undefined && parsed.distro.toLowerCase() === distro.toLowerCase())
131
+ return normalizeLinux(parsed.linuxPath);
132
+ }
133
+ throw new WritableRootError(value, `the workspace is inside distro ${JSON.stringify(distro)}; a Windows-path extra directory cannot be granted there`);
134
+ }
135
+ /**
136
+ * Validate the shape of a row's `writableRoots` before any distro is known.
137
+ * Called from the providers' constructors so a typo fails at boot instead of
138
+ * at the first command.
139
+ *
140
+ * @param values - the configured entries, if present.
141
+ * @throws {Error} naming the first entry that is not a distro path.
142
+ */
143
+ export function validateWritableRootsConfig(values) {
144
+ for (const value of values ?? []) {
145
+ if (typeof value !== "string" || value.length === 0 || value.includes("\u0000")) {
146
+ throw new Error(`writableRoots: every entry must be a non-empty Linux path, got ${JSON.stringify(value)}`);
147
+ }
148
+ if (value === "~" || value.startsWith("~/"))
149
+ continue;
150
+ if (value.startsWith("~"))
151
+ throw new Error(`writableRoots: use ~/... or an absolute Linux path, got ${JSON.stringify(value)}`);
152
+ if (!isPosixAbsolute(value))
153
+ throw new Error(`writableRoots: every entry must be an absolute Linux path or start with ~/, got ${JSON.stringify(value)}`);
154
+ }
155
+ }
156
+ /**
157
+ * Turn the configured and policy grants into the effective Linux roots.
158
+ *
159
+ * Entries already writable (inside the workspace, `/tmp` or `/dev/shm`) and
160
+ * entries nested inside another grant are dropped: they add no permission, and
161
+ * binding them would let the model redirect the bind through a symlink. The
162
+ * rest are deduplicated, sorted and returned.
163
+ *
164
+ * @param input - the configured grants, policy grants, home and workspace.
165
+ * @returns the effective roots, in deterministic order.
166
+ * @throws {WritableRootError} for an entry that names another world or is malformed.
167
+ */
168
+ export function resolveWritableRoots(input) {
169
+ const candidates = [];
170
+ for (const value of input.configured ?? [])
171
+ candidates.push(configuredRoot(value, input.home));
172
+ for (const value of input.policy ?? [])
173
+ candidates.push(policyRoot(value, input.distro));
174
+ const workspace = normalizeLinux(input.workspaceRoot);
175
+ const alreadyWritable = [workspace, ...TEMP_ROOTS.map(normalizeLinux)];
176
+ const effective = [];
177
+ for (const candidate of [...new Set(candidates)].sort((a, b) => a.length - b.length || a.localeCompare(b))) {
178
+ if (alreadyWritable.some((root) => contains(candidate, root)))
179
+ continue;
180
+ if (effective.some((root) => contains(candidate, root)))
181
+ continue;
182
+ effective.push(candidate);
183
+ }
184
+ return effective;
185
+ }
186
+ /** The one-time check script: missing, unreadable and symlinked roots report index|reason. */
187
+ const CHECK_SCRIPT = [
188
+ "i=0",
189
+ 'for r in "$@"; do',
190
+ ' if [ ! -d "$r" ]; then printf "%s|missing\\n" "$i"; exit 3; fi',
191
+ ' p=$(cd "$r" 2>/dev/null && pwd -P) || { printf "%s|unreadable\\n" "$i"; exit 4; }',
192
+ ' if [ "$p" != "$r" ]; then printf "%s|symlink -> %s\\n" "$i" "$p"; exit 5; fi',
193
+ " i=$((i+1))",
194
+ "done",
195
+ ].join("\n");
196
+ /** The argv of the one-time physical-directory check, exported for tests and diagnostics. */
197
+ export function writableRootsCheckArgv(wslPath, distro, roots) {
198
+ return [wslPath, "-d", distro, "--exec", "sh", "-c", CHECK_SCRIPT, "wsl-writable-roots", ...roots];
199
+ }
200
+ /** The successful checks, keyed by the exact root list; a success is durable. */
201
+ const checked = new Map();
202
+ /**
203
+ * Check once per process that every effective root exists, is readable and is
204
+ * its own physical directory. A missing root reports the operator-facing
205
+ * failure instead of letting bwrap die with an opaque mount error; a symlinked
206
+ * root is refused with the physical-path rule the peer's extra-root contract
207
+ * uses.
208
+ *
209
+ * A success is remembered for the process lifetime: the effective roots have
210
+ * read-only parents, so the model cannot replace them, and the common roots
211
+ * (`~/.npm`, `~/.cache`, …) are stable. A failed check is not remembered, so a
212
+ * directory the operator creates while the app runs is believed next time.
213
+ *
214
+ * @param options - the `wsl.exe` path, distro, effective roots and optional run override.
215
+ * @returns resolved when every root is usable.
216
+ * @throws {WritableRootError} naming the first unusable root.
217
+ */
218
+ export async function ensureWritableRoots(options) {
219
+ if (options.roots.length === 0)
220
+ return;
221
+ const key = `${options.wslPath}\u0000${options.distro}\u0000${options.roots.join("\u0000")}`;
222
+ const cached = checked.get(key);
223
+ if (cached !== undefined)
224
+ return cached;
225
+ const run = options.run ?? ((argv) => runCapture(argv, options.signal));
226
+ const pending = (async () => {
227
+ try {
228
+ await run(writableRootsCheckArgv(options.wslPath, options.distro, options.roots));
229
+ }
230
+ catch (error) {
231
+ const message = error instanceof Error ? error.message : String(error);
232
+ const match = /(\d+)\|(missing|unreadable|symlink)(?: -> (.*))?/u.exec(message);
233
+ const index = match === null ? Number.NaN : Number(match[1]);
234
+ const root = Number.isInteger(index) ? options.roots[index] : undefined;
235
+ if (root === undefined)
236
+ throw error;
237
+ const reason = match?.[2] === "missing"
238
+ ? "expected an existing directory; create it inside the distro first"
239
+ : match?.[2] === "unreadable"
240
+ ? "cannot read the directory"
241
+ : `use the physical directory path ${JSON.stringify(match?.[3] ?? "")}; symlinked grants are not supported`;
242
+ throw new WritableRootError(root, reason);
243
+ }
244
+ })();
245
+ checked.set(key, pending);
246
+ try {
247
+ await pending;
248
+ }
249
+ catch (error) {
250
+ checked.delete(key);
251
+ throw error;
252
+ }
253
+ }
254
+ /** Test hook: forget the remembered checks. */
255
+ export function resetWritableRootsChecks() {
256
+ checked.clear();
257
+ }
@@ -32,6 +32,7 @@ import { dirname, resolve } from "node:path";
32
32
  import { fileURLToPath } from "node:url";
33
33
  import { distroOfUnc } from "./distro-route.js";
34
34
  import { digestReport, renderDigest, SECTIONS } from "./wsl-doctor-tool.js";
35
+ import { mergeAutoPresetSeam } from "./doctor-live.js";
35
36
  export const name = "wsl-doctor-command";
36
37
  /**
37
38
  * No required inject: a headless deployment has no commands registry, and a
@@ -313,7 +314,7 @@ async function resolveDistro(ctx, invocation, distro) {
313
314
  */
314
315
  export function formatDoctorReport(json, distro) {
315
316
  const report = JSON.parse(json);
316
- const digest = digestReport(report, distro.length > 0 ? distro : undefined);
317
+ const digest = digestReport(mergeAutoPresetSeam(report), distro.length > 0 ? distro : undefined);
317
318
  return renderDigest(digest);
318
319
  }
319
320
  /**
@@ -26,7 +26,7 @@
26
26
  import { defineTool } from "@deepseek-ai/dsh-tools";
27
27
  import { distroOfUnc } from "./distro-route.js";
28
28
  import { t } from "./messages.js";
29
- import { doctorReport } from "./index.js";
29
+ import { doctorReport } from "./doctor-report.js";
30
30
  export const name = "wsl-doctor-tool";
31
31
  /**
32
32
  * The context properties `apply` reads eagerly: the tool registry and the
@@ -370,7 +370,7 @@ async function doctorCollector(ctx) {
370
370
  export async function apply(ctx) {
371
371
  ctx.tools.register(defineTool({
372
372
  name: "wsl_doctor",
373
- description: "Run the WSL environment doctor for this session's distro and return the verdict: ready (the first command should succeed), or blocked with per-failure copy-paste remedies. Interactive by design: start with depth \"quick\" (prerequisites only, ~10 s for the session's own distro) and narrate the result, then narrow with \"section\" (one of host/sandbox/session/conf/net/agent) or run depth \"full\" only when the user asked for a complete diagnosis — each call probes its scope and itemizes every check it tested with an explicit status marker, grouped by distro.",
373
+ description: "Run the WSL environment doctor for this session's distro. Default depth \"quick\" (~10 s); use \"full\" only when the user asks for a complete diagnosis. Returns the verdict, blockers, and report.",
374
374
  parameters: {
375
375
  distro: {
376
376
  type: "string",