dsh-plugin-wsl-env 0.24.0 → 0.25.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 +52 -0
- package/README.md +6 -3
- package/SECURITY.md +10 -0
- package/cordis.patch.yml +4 -0
- package/docs/ARCHITECTURE.md +28 -9
- package/docs/CONFIGURATION.md +43 -0
- package/docs/CONFIGURATION.zh.md +36 -0
- package/docs/DIAGNOSTICS.md +4 -2
- package/docs/DIAGNOSTICS.zh.md +3 -1
- package/docs/LIMITATIONS.md +17 -4
- package/docs/LIMITATIONS.zh.md +11 -3
- package/docs/MIGRATION.md +1 -1
- package/docs/PEER-PARITY.md +4 -4
- package/docs/QUICKSTART.md +12 -5
- package/docs/QUICKSTART.zh.md +8 -2
- package/docs/README.zh.md +6 -3
- package/docs/RELEASING.md +17 -13
- package/docs/REQUIREMENTS.zh.md +3 -2
- package/docs/UPSTREAM-FSIO-EXPORT.md +5 -4
- package/docs/UPSTREAM-PROMPT-ADAPTERS.md +85 -0
- package/docs/root-fs-routing.md +1 -1
- package/lib/agent-confined.d.ts +7 -1
- package/lib/agent-confined.js +3 -3
- package/lib/bwrap.d.ts +14 -0
- package/lib/bwrap.js +11 -0
- package/lib/cwd-prompt.d.ts +41 -3
- package/lib/cwd-prompt.js +125 -8
- package/lib/doctor-report.d.ts +50 -0
- package/lib/doctor-report.js +172 -0
- package/lib/fs-routing.d.ts +1 -1
- package/lib/fs-routing.js +1 -1
- package/lib/fs.d.ts +1 -1
- package/lib/fs.js +1 -1
- package/lib/index.d.ts +6 -1464
- package/lib/index.js +5 -2120
- package/lib/messages.d.ts +1 -1
- package/lib/messages.js +3 -3
- package/lib/preflight.d.ts +7 -4
- package/lib/preflight.js +27 -13
- package/lib/provider-common.d.ts +58 -0
- package/lib/provider-common.js +123 -0
- package/lib/sandbox-core.d.ts +1 -0
- package/lib/sandbox-core.js +4 -2
- package/lib/session-runtime.d.ts +7 -1
- package/lib/session-runtime.js +9 -2
- package/lib/shell.d.ts +1 -1
- package/lib/shell.js +1 -1
- package/lib/writable-roots.d.ts +109 -0
- package/lib/writable-roots.js +257 -0
- package/lib/wsl-doctor-tool.js +2 -2
- package/lib/wsl-filesystem.d.ts +692 -0
- package/lib/wsl-filesystem.js +897 -0
- package/lib/wsl-shell.d.ts +742 -0
- package/lib/wsl-shell.js +965 -0
- package/lib/wsl.d.ts +1 -1
- package/lib/wsl.js +1 -1
- package/package.json +4 -3
- package/scripts/doctor.mjs +1 -1
- 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
|
+
}
|
package/lib/wsl-doctor-tool.js
CHANGED
|
@@ -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 "./
|
|
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
|
|
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",
|