dsh-plugin-wsl-env 0.29.0 → 0.29.1
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 +342 -0
- package/CONTRIBUTING.md +68 -12
- package/CONTRIBUTING.zh.md +34 -4
- package/README.md +1 -1
- 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 +15 -4
- package/client.js +454 -110
- package/cordis.patch.yml +28 -7
- package/docs/AGENT-PROTOCOL-V7.md +4 -2
- package/docs/ARCHITECTURE.md +3 -1
- package/docs/ARCHITECTURE.zh.md +3 -1
- package/docs/COMPATIBILITY.md +1 -1
- package/docs/COMPATIBILITY.zh.md +1 -1
- package/docs/CONFIGURATION.md +19 -7
- package/docs/CONFIGURATION.zh.md +13 -5
- package/docs/DIAGNOSTICS.md +31 -5
- package/docs/DIAGNOSTICS.zh.md +22 -5
- package/docs/LIMITATIONS.md +65 -6
- package/docs/LIMITATIONS.zh.md +51 -6
- package/docs/PERFORMANCE.md +29 -1
- package/docs/PERFORMANCE.zh.md +23 -1
- package/docs/QUICKSTART.md +1 -1
- package/docs/QUICKSTART.zh.md +1 -1
- package/docs/README.zh.md +1 -1
- package/docs/RELEASING.md +29 -6
- package/docs/RELEASING.zh.md +9 -0
- package/docs/adr/0002-native-agent.md +50 -2
- package/lib/agent-confined.js +15 -4
- package/lib/agent-errors.d.ts +16 -0
- package/lib/agent-errors.js +19 -0
- package/lib/agent-exec.d.ts +8 -1
- package/lib/agent-exec.js +2 -1
- package/lib/agent-shared.d.ts +85 -2
- package/lib/agent-shared.js +167 -38
- package/lib/agent.d.ts +60 -6
- package/lib/agent.js +177 -28
- package/lib/auto-preset.js +7 -2
- package/lib/doctor-live.d.ts +36 -7
- package/lib/doctor-live.js +61 -12
- package/lib/doctor-process.d.ts +68 -0
- package/lib/doctor-process.js +139 -0
- package/lib/doctor-report.js +11 -109
- package/lib/fs-substrate.js +3 -3
- package/lib/fsio-agent.d.ts +57 -6
- package/lib/fsio-agent.js +168 -75
- package/lib/git-exec.d.ts +7 -1
- package/lib/git-exec.js +14 -2
- package/lib/native-agent.d.ts +25 -4
- package/lib/native-agent.js +52 -9
- package/lib/picker.d.ts +2 -1
- package/lib/picker.js +30 -13
- package/lib/plugin-update.js +18 -14
- package/lib/provider-common.d.ts +112 -0
- package/lib/provider-common.js +171 -0
- package/lib/sandbox-core.js +52 -13
- package/lib/search-exec.d.ts +6 -0
- package/lib/search-exec.js +10 -2
- package/lib/session-runtime.d.ts +46 -1
- package/lib/session-runtime.js +57 -7
- package/lib/session-scope.d.ts +22 -1
- package/lib/session-scope.js +33 -7
- package/lib/shell-env.js +125 -12
- package/lib/socket-isolation.d.ts +12 -4
- package/lib/socket-isolation.js +27 -10
- package/lib/status.js +53 -4
- package/lib/subprocess.js +3 -3
- package/lib/terminal-activity.js +6 -9
- package/lib/wsl-doctor-command.js +36 -80
- package/lib/wsl-filesystem.d.ts +9 -4
- package/lib/wsl-filesystem.js +26 -10
- package/lib/wsl-shell.d.ts +27 -17
- package/lib/wsl-shell.js +83 -24
- package/lib/wsl.d.ts +3 -1
- package/lib/wsl.js +82 -7
- package/package.json +5 -4
- package/scripts/bootstrap.sh +36 -12
- package/scripts/doctor.mjs +514 -201
package/lib/plugin-update.js
CHANGED
|
@@ -179,22 +179,26 @@ export function createPluginUpdateHandler(options) {
|
|
|
179
179
|
if (updater === undefined) {
|
|
180
180
|
return json({ error: "This plugin-manager cannot update bundles; update from the Plugins page" }, 501);
|
|
181
181
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
catch (error) {
|
|
187
|
-
return json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
188
|
-
}
|
|
189
|
-
if (check.status === "current")
|
|
190
|
-
return json({ error: "The installed version is up to date" }, 409);
|
|
191
|
-
if (check.status === "unavailable")
|
|
192
|
-
return json({ error: `No update target: ${check.reason}` }, 409);
|
|
193
|
-
if (check.latest.incompatible !== undefined) {
|
|
194
|
-
return json({ error: "The latest release requires a newer DSH; upgrade DeepSeek Harness first" }, 409);
|
|
195
|
-
}
|
|
182
|
+
// The lock is taken BEFORE the first await. Taken after the version
|
|
183
|
+
// check it guarded only the install: that check is a registry round trip
|
|
184
|
+
// — seconds, on a cold or stale cache — and two clicks inside it each
|
|
185
|
+
// passed the test above and each ran their own removal and install.
|
|
196
186
|
updating = true;
|
|
197
187
|
try {
|
|
188
|
+
let check;
|
|
189
|
+
try {
|
|
190
|
+
check = (await currentCheck(false)).check;
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
return json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
194
|
+
}
|
|
195
|
+
if (check.status === "current")
|
|
196
|
+
return json({ error: "The installed version is up to date" }, 409);
|
|
197
|
+
if (check.status === "unavailable")
|
|
198
|
+
return json({ error: `No update target: ${check.reason}` }, 409);
|
|
199
|
+
if (check.latest.incompatible !== undefined) {
|
|
200
|
+
return json({ error: "The latest release requires a newer DSH; upgrade DeepSeek Harness first" }, 409);
|
|
201
|
+
}
|
|
198
202
|
const result = await updater.updateBundle(PACKAGE, check.latest.version, { registry: check.registry });
|
|
199
203
|
cache = undefined;
|
|
200
204
|
const application = result.result.application;
|
package/lib/provider-common.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import type { SandboxExecutionPolicy } from "@deepseek-ai/dsh-sandbox";
|
|
2
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
2
3
|
import type { SessionRuntimeEntry } from "./session-runtime.js";
|
|
4
|
+
/**
|
|
5
|
+
* How long a session runtime must have been quiet before the sweep may end
|
|
6
|
+
* the session of one that no live agent holds any more. The same window the
|
|
7
|
+
* shared resident idles out after; a live session's runtime is never subject
|
|
8
|
+
* to it, however long it sits quiet.
|
|
9
|
+
*/
|
|
10
|
+
export declare const ORPHAN_RUNTIME_IDLE_MS = 600000;
|
|
11
|
+
/** How often a provider sweeps its session runtimes for unheld ones. */
|
|
12
|
+
export declare const ORPHAN_SWEEP_MS = 60000;
|
|
3
13
|
interface DistroHomeOwner {
|
|
4
14
|
config: unknown;
|
|
5
15
|
resolvedDistro: string;
|
|
@@ -77,4 +87,106 @@ export declare class ProviderRuntimeBook {
|
|
|
77
87
|
checkedAt: number;
|
|
78
88
|
}>;
|
|
79
89
|
}
|
|
90
|
+
/** The lifecycle slice the session-end wiring listens on. */
|
|
91
|
+
export interface SessionLifecycle {
|
|
92
|
+
/** The harness's agent-disposed event, carrying the agent that left. */
|
|
93
|
+
on: (event: "agent/disposed", listener: (payload: {
|
|
94
|
+
agent?: {
|
|
95
|
+
id: string;
|
|
96
|
+
} | undefined;
|
|
97
|
+
} | undefined) => void | Promise<void>) => () => boolean;
|
|
98
|
+
}
|
|
99
|
+
/** The registry slice a provider's session-end wiring drives. */
|
|
100
|
+
interface SessionRuntimeRegistry {
|
|
101
|
+
/** Release one reference. */
|
|
102
|
+
release: (key: string) => Promise<void>;
|
|
103
|
+
/** Whether the registry still keeps a cached entry. */
|
|
104
|
+
holds: (entry: SessionRuntimeEntry) => boolean;
|
|
105
|
+
/** End one session for every holder. */
|
|
106
|
+
releaseSession: (sessionId: string) => Promise<void>;
|
|
107
|
+
}
|
|
108
|
+
/** The provider state the session-end wiring reads and keeps. */
|
|
109
|
+
export interface SessionEndOwner {
|
|
110
|
+
/** The provider's book. */
|
|
111
|
+
runtimeBook: ProviderRuntimeBook;
|
|
112
|
+
/** The registry; tests substitute it after construction. */
|
|
113
|
+
sessionRuntimes: SessionRuntimeRegistry;
|
|
114
|
+
/** The provider's token for requests that carry no session identity. */
|
|
115
|
+
sessionRuntimeOwner: string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The sessions to end: every one whose runtimes are quiet past the idle
|
|
119
|
+
* window and that nothing holds any more — no live agent serves it, or it
|
|
120
|
+
* was never a session at all (the provider's own token).
|
|
121
|
+
*
|
|
122
|
+
* The sweep is the safety net under `agent/disposed`, not a second way to end
|
|
123
|
+
* a session: a runtime used inside the window is spared whatever its session
|
|
124
|
+
* looks like, and when the harness exposes no agent registry to read, every
|
|
125
|
+
* runtime is spared — an unreadable fact is never read as "gone".
|
|
126
|
+
*
|
|
127
|
+
* @param object - the held runtimes and the facts the verdict needs.
|
|
128
|
+
* @returns the session ids to end, without duplicates.
|
|
129
|
+
*/
|
|
130
|
+
export declare function orphanedSessions({ entries, sessionless, sessionLive, now, idleMs }: {
|
|
131
|
+
/** The runtimes a provider holds. */
|
|
132
|
+
entries: Iterable<SessionRuntimeEntry>;
|
|
133
|
+
/** The token the provider keys sessionless requests by. */
|
|
134
|
+
sessionless: string;
|
|
135
|
+
/**
|
|
136
|
+
* Whether a live agent still serves a session; `undefined` when that cannot
|
|
137
|
+
* be read.
|
|
138
|
+
*/
|
|
139
|
+
sessionLive: (sessionId: string) => boolean | undefined;
|
|
140
|
+
/** The current time, epoch ms. */
|
|
141
|
+
now: number;
|
|
142
|
+
/** How long a runtime must have been quiet; defaults to {@link ORPHAN_RUNTIME_IDLE_MS}. */
|
|
143
|
+
idleMs?: number;
|
|
144
|
+
}): string[];
|
|
145
|
+
/**
|
|
146
|
+
* Tie one provider's session runtimes to the sessions they belong to.
|
|
147
|
+
*
|
|
148
|
+
* The preset realm both providers live in is shared by every session and
|
|
149
|
+
* outlives all of them: upstream disposes its scope only when the preset
|
|
150
|
+
* definition is unregistered, so nothing registered there runs "when the
|
|
151
|
+
* session ends". What fires per session is the harness's `agent/disposed` —
|
|
152
|
+
* the rows inside a preset inherit the preset scope's tag, and an agent's
|
|
153
|
+
* events are dispatched up the scope chain — so that event ends the session
|
|
154
|
+
* here. A periodic sweep underneath ends the sessions whose runtimes sit
|
|
155
|
+
* quiet with no live agent behind them: it covers a session end the event did
|
|
156
|
+
* not deliver, and requests that never named a session at all.
|
|
157
|
+
*/
|
|
158
|
+
export declare class SessionEndWatch {
|
|
159
|
+
/** The provider whose runtimes this watch ends. */
|
|
160
|
+
private readonly owner;
|
|
161
|
+
/** Whether a live agent still serves a session; `undefined` when unreadable. */
|
|
162
|
+
private readonly sessionLive;
|
|
163
|
+
/** The unheld-session sweep, running while the provider holds a runtime. */
|
|
164
|
+
private sweep;
|
|
165
|
+
/** Detaches the `agent/disposed` listener. */
|
|
166
|
+
private readonly off;
|
|
167
|
+
/**
|
|
168
|
+
* @param owner - the provider; read at call time, so a test may substitute
|
|
169
|
+
* its registry after construction.
|
|
170
|
+
* @param ctx - the provider's composition context.
|
|
171
|
+
*/
|
|
172
|
+
constructor(owner: SessionEndOwner, ctx: Context);
|
|
173
|
+
/**
|
|
174
|
+
* End one session for this provider: its own references go first, so its
|
|
175
|
+
* book never names a runtime being torn down, then the registry ends the
|
|
176
|
+
* session for every holder.
|
|
177
|
+
*
|
|
178
|
+
* @param sessionId - the session that ended.
|
|
179
|
+
* @returns resolved once the session's runtimes are torn down.
|
|
180
|
+
*/
|
|
181
|
+
endSession(sessionId: string): Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Make sure the unheld-session sweep runs; called once the provider holds a
|
|
184
|
+
* runtime. The timer never keeps the process alive.
|
|
185
|
+
*/
|
|
186
|
+
ensureSweep(): void;
|
|
187
|
+
/** Stop the sweep while the provider holds nothing; a later acquire restarts it. */
|
|
188
|
+
stopSweep(): void;
|
|
189
|
+
/** Detach the listener and stop the sweep: the provider is going away. */
|
|
190
|
+
dispose(): void;
|
|
191
|
+
}
|
|
80
192
|
export {};
|
package/lib/provider-common.js
CHANGED
|
@@ -4,6 +4,15 @@ import { distroOfUnc, isDefaultDistro, pinnedDistro } from "./distro-route.js";
|
|
|
4
4
|
import { linuxHomePath } from "./wsl.js";
|
|
5
5
|
import { WritableRootError, ensureWritableRoots, resolveWritableRoots } from "./writable-roots.js";
|
|
6
6
|
const AUTO_SCOPE_RECHECK_MS = 60_000;
|
|
7
|
+
/**
|
|
8
|
+
* How long a session runtime must have been quiet before the sweep may end
|
|
9
|
+
* the session of one that no live agent holds any more. The same window the
|
|
10
|
+
* shared resident idles out after; a live session's runtime is never subject
|
|
11
|
+
* to it, however long it sits quiet.
|
|
12
|
+
*/
|
|
13
|
+
export const ORPHAN_RUNTIME_IDLE_MS = 600_000;
|
|
14
|
+
/** How often a provider sweeps its session runtimes for unheld ones. */
|
|
15
|
+
export const ORPHAN_SWEEP_MS = 60_000;
|
|
7
16
|
/**
|
|
8
17
|
* The configured default workdir, or an empty string when the operator left it
|
|
9
18
|
* unset. `cwd` is a volatile field on both providers, so it is read through
|
|
@@ -139,3 +148,165 @@ export class ProviderRuntimeBook {
|
|
|
139
148
|
/** Per-distro `auto` verdicts, re-probed when they were negative and stale. */
|
|
140
149
|
autoSessionScope = new Map();
|
|
141
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Drop one provider's references to every runtime a session used. The book
|
|
153
|
+
* entries go first and together — a request for that session arriving
|
|
154
|
+
* meanwhile acquires a fresh runtime instead of reusing one being torn down —
|
|
155
|
+
* and the references are released afterwards.
|
|
156
|
+
*
|
|
157
|
+
* @param owner - the provider dropping its references.
|
|
158
|
+
* @param sessionId - the session that ended.
|
|
159
|
+
* @returns resolved once every dropped reference is released.
|
|
160
|
+
*/
|
|
161
|
+
async function dropSessionRuntimes(owner, sessionId) {
|
|
162
|
+
const keys = [...owner.runtimeBook.sessionRuntimeEntries]
|
|
163
|
+
.filter(([, entry]) => entry.sessionId === sessionId)
|
|
164
|
+
.map(([key]) => key);
|
|
165
|
+
for (const key of keys)
|
|
166
|
+
owner.runtimeBook.sessionRuntimeEntries.delete(key);
|
|
167
|
+
for (const key of keys)
|
|
168
|
+
await owner.sessionRuntimes.release(key);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The sessions to end: every one whose runtimes are quiet past the idle
|
|
172
|
+
* window and that nothing holds any more — no live agent serves it, or it
|
|
173
|
+
* was never a session at all (the provider's own token).
|
|
174
|
+
*
|
|
175
|
+
* The sweep is the safety net under `agent/disposed`, not a second way to end
|
|
176
|
+
* a session: a runtime used inside the window is spared whatever its session
|
|
177
|
+
* looks like, and when the harness exposes no agent registry to read, every
|
|
178
|
+
* runtime is spared — an unreadable fact is never read as "gone".
|
|
179
|
+
*
|
|
180
|
+
* @param object - the held runtimes and the facts the verdict needs.
|
|
181
|
+
* @returns the session ids to end, without duplicates.
|
|
182
|
+
*/
|
|
183
|
+
export function orphanedSessions({ entries, sessionless, sessionLive, now, idleMs = ORPHAN_RUNTIME_IDLE_MS }) {
|
|
184
|
+
const sessions = new Set();
|
|
185
|
+
for (const entry of entries) {
|
|
186
|
+
if (!isQuiet(entry.agent, now, idleMs))
|
|
187
|
+
continue;
|
|
188
|
+
if (entry.sessionId !== sessionless && sessionLive(entry.sessionId) !== false)
|
|
189
|
+
continue;
|
|
190
|
+
sessions.add(entry.sessionId);
|
|
191
|
+
}
|
|
192
|
+
return [...sessions];
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Whether a runtime's resident has been quiet for the idle window. A resident
|
|
196
|
+
* that reports no activity time is never called quiet.
|
|
197
|
+
*
|
|
198
|
+
* @param agent - the runtime's resident.
|
|
199
|
+
* @param now - the current time, epoch ms.
|
|
200
|
+
* @param idleMs - the idle window.
|
|
201
|
+
* @returns true when the window has fully elapsed since its last exchange.
|
|
202
|
+
*/
|
|
203
|
+
function isQuiet(agent, now, idleMs) {
|
|
204
|
+
return typeof agent.lastActivityAt === "number" && now - agent.lastActivityAt >= idleMs;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Tie one provider's session runtimes to the sessions they belong to.
|
|
208
|
+
*
|
|
209
|
+
* The preset realm both providers live in is shared by every session and
|
|
210
|
+
* outlives all of them: upstream disposes its scope only when the preset
|
|
211
|
+
* definition is unregistered, so nothing registered there runs "when the
|
|
212
|
+
* session ends". What fires per session is the harness's `agent/disposed` —
|
|
213
|
+
* the rows inside a preset inherit the preset scope's tag, and an agent's
|
|
214
|
+
* events are dispatched up the scope chain — so that event ends the session
|
|
215
|
+
* here. A periodic sweep underneath ends the sessions whose runtimes sit
|
|
216
|
+
* quiet with no live agent behind them: it covers a session end the event did
|
|
217
|
+
* not deliver, and requests that never named a session at all.
|
|
218
|
+
*/
|
|
219
|
+
export class SessionEndWatch {
|
|
220
|
+
/** The provider whose runtimes this watch ends. */
|
|
221
|
+
owner;
|
|
222
|
+
/** Whether a live agent still serves a session; `undefined` when unreadable. */
|
|
223
|
+
sessionLive;
|
|
224
|
+
/** The unheld-session sweep, running while the provider holds a runtime. */
|
|
225
|
+
sweep = null;
|
|
226
|
+
/** Detaches the `agent/disposed` listener. */
|
|
227
|
+
off;
|
|
228
|
+
/**
|
|
229
|
+
* @param owner - the provider; read at call time, so a test may substitute
|
|
230
|
+
* its registry after construction.
|
|
231
|
+
* @param ctx - the provider's composition context.
|
|
232
|
+
*/
|
|
233
|
+
constructor(owner, ctx) {
|
|
234
|
+
this.owner = owner;
|
|
235
|
+
this.sessionLive = sessionLiveReader(ctx);
|
|
236
|
+
// The promise is returned, not discarded: the harness contains and logs a
|
|
237
|
+
// listener's rejected teardown instead of turning it into an unhandled
|
|
238
|
+
// rejection.
|
|
239
|
+
this.off = ctx.on("agent/disposed", (payload) => {
|
|
240
|
+
const id = payload?.agent?.id;
|
|
241
|
+
return id === undefined ? undefined : this.endSession(id);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* End one session for this provider: its own references go first, so its
|
|
246
|
+
* book never names a runtime being torn down, then the registry ends the
|
|
247
|
+
* session for every holder.
|
|
248
|
+
*
|
|
249
|
+
* @param sessionId - the session that ended.
|
|
250
|
+
* @returns resolved once the session's runtimes are torn down.
|
|
251
|
+
*/
|
|
252
|
+
async endSession(sessionId) {
|
|
253
|
+
await dropSessionRuntimes(this.owner, sessionId);
|
|
254
|
+
await this.owner.sessionRuntimes.releaseSession(sessionId);
|
|
255
|
+
if (this.owner.runtimeBook.sessionRuntimeEntries.size === 0)
|
|
256
|
+
this.stopSweep();
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Make sure the unheld-session sweep runs; called once the provider holds a
|
|
260
|
+
* runtime. The timer never keeps the process alive.
|
|
261
|
+
*/
|
|
262
|
+
ensureSweep() {
|
|
263
|
+
if (this.sweep !== null)
|
|
264
|
+
return;
|
|
265
|
+
const timer = setInterval(() => {
|
|
266
|
+
for (const sessionId of orphanedSessions({
|
|
267
|
+
entries: this.owner.runtimeBook.sessionRuntimeEntries.values(),
|
|
268
|
+
sessionless: this.owner.sessionRuntimeOwner,
|
|
269
|
+
sessionLive: this.sessionLive,
|
|
270
|
+
now: Date.now(),
|
|
271
|
+
})) {
|
|
272
|
+
void this.endSession(sessionId).catch(() => { });
|
|
273
|
+
}
|
|
274
|
+
}, ORPHAN_SWEEP_MS);
|
|
275
|
+
timer.unref?.();
|
|
276
|
+
this.sweep = timer;
|
|
277
|
+
}
|
|
278
|
+
/** Stop the sweep while the provider holds nothing; a later acquire restarts it. */
|
|
279
|
+
stopSweep() {
|
|
280
|
+
if (this.sweep !== null)
|
|
281
|
+
clearInterval(this.sweep);
|
|
282
|
+
this.sweep = null;
|
|
283
|
+
}
|
|
284
|
+
/** Detach the listener and stop the sweep: the provider is going away. */
|
|
285
|
+
dispose() {
|
|
286
|
+
this.off();
|
|
287
|
+
this.stopSweep();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Whether a live agent still serves a session, read from the harness's agent
|
|
292
|
+
* registry. `agents` is read with `ctx.get` because the providers do not
|
|
293
|
+
* inject it — a property read of an uninjected service throws in cordis 4 —
|
|
294
|
+
* and an unreadable registry answers `undefined`, which the sweep takes as
|
|
295
|
+
* "do not know", never as "gone".
|
|
296
|
+
*
|
|
297
|
+
* @param ctx - the provider's composition context.
|
|
298
|
+
* @returns the liveness reader.
|
|
299
|
+
*/
|
|
300
|
+
function sessionLiveReader(ctx) {
|
|
301
|
+
return (sessionId) => {
|
|
302
|
+
try {
|
|
303
|
+
const agents = ctx.get("agents");
|
|
304
|
+
if (typeof agents?.get !== "function")
|
|
305
|
+
return undefined;
|
|
306
|
+
return agents.get(sessionId) !== undefined;
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
package/lib/sandbox-core.js
CHANGED
|
@@ -73,13 +73,30 @@ const probeKey = (wslPath, distro) => `${wslPath}\u0000${distro}`;
|
|
|
73
73
|
* The bootstrap script remains the first-choice path — it detects the family
|
|
74
74
|
* itself — but a user pasting the message on a dnf or pacman distro must not
|
|
75
75
|
* be handed an `apt-get` line.
|
|
76
|
+
*
|
|
77
|
+
* apt refreshes its package index first: a freshly installed Debian or Ubuntu
|
|
78
|
+
* has none, and `install` alone answers `E: Unable to locate package`. That
|
|
79
|
+
* command contains a `&&`, so it carries its own `sh -c "…"` wrapper — the
|
|
80
|
+
* message appends it bare after `wsl.exe … --`, and the wrapper is what keeps
|
|
81
|
+
* the shell operator inside the distro when the line is pasted into cmd,
|
|
82
|
+
* PowerShell 5.1 or PowerShell 7. pacman takes `-S`, not `-Sy`: refreshing the
|
|
83
|
+
* index without upgrading the system is the partial-upgrade form Arch advises
|
|
84
|
+
* against.
|
|
76
85
|
*/
|
|
77
86
|
const FAMILY_INSTALL = {
|
|
78
|
-
apt: "apt-get install -y bubblewrap"
|
|
87
|
+
apt: `sh -c "apt-get update -qq && apt-get install -y --no-install-recommends bubblewrap"`,
|
|
79
88
|
dnf: "dnf install -y bubblewrap",
|
|
80
|
-
pacman: "pacman -S --noconfirm bubblewrap",
|
|
89
|
+
pacman: "pacman -S --noconfirm --needed bubblewrap",
|
|
81
90
|
zypper: "zypper --non-interactive install bubblewrap",
|
|
82
91
|
};
|
|
92
|
+
/**
|
|
93
|
+
* The package family already detected per `(wslPath, distro)`. The family
|
|
94
|
+
* probe is only consulted after a failed bwrap probe, and a broken distro is
|
|
95
|
+
* probed on every command; without this memo each such command spent an extra
|
|
96
|
+
* `wsl.exe` round trip on an answer that does not change. Only a found manager
|
|
97
|
+
* is remembered.
|
|
98
|
+
*/
|
|
99
|
+
const packageFamilies = new Map();
|
|
83
100
|
/** How many characters of the failed probe's output the remedy quotes. */
|
|
84
101
|
const PROBE_OUTPUT_CAP = 300;
|
|
85
102
|
/**
|
|
@@ -100,6 +117,19 @@ function classifyProbeOutput(output) {
|
|
|
100
117
|
return "missing";
|
|
101
118
|
return "unknown";
|
|
102
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* The spawn seam's one test concession: a `wslPath` ending in `.mjs` names a
|
|
122
|
+
* Node script, run under this runtime. Windows resolves the real
|
|
123
|
+
* `System32\wsl.exe` before `PATH`, so a cross-platform test fake cannot
|
|
124
|
+
* shadow it by name — it has to be a script a Node child can execute. No
|
|
125
|
+
* production path names a `.mjs`; a nonsensical value fails closed at spawn.
|
|
126
|
+
*
|
|
127
|
+
* @param wslPath - the configured `wsl.exe` path.
|
|
128
|
+
* @returns the argv prefix the spawn uses.
|
|
129
|
+
*/
|
|
130
|
+
function wslCommand(wslPath) {
|
|
131
|
+
return wslPath.endsWith(".mjs") ? [process.execPath, wslPath] : [wslPath];
|
|
132
|
+
}
|
|
103
133
|
/**
|
|
104
134
|
* The package manager the distro offers, for the remedy's direct command. Only
|
|
105
135
|
* consulted on a FAILED probe, so a healthy distro never pays for it; a probe
|
|
@@ -111,20 +141,29 @@ function classifyProbeOutput(output) {
|
|
|
111
141
|
* @returns the first family whose manager exists, or undefined.
|
|
112
142
|
*/
|
|
113
143
|
async function detectPackageFamily(wslPath, distro, signal) {
|
|
144
|
+
const key = `${wslPath}\u0000${distro}`;
|
|
145
|
+
const remembered = packageFamilies.get(key);
|
|
146
|
+
if (remembered !== undefined)
|
|
147
|
+
return remembered;
|
|
114
148
|
try {
|
|
115
149
|
const script = "for m in apt-get dnf pacman zypper; do command -v $m && break; done";
|
|
116
150
|
// `command -v` answers with the PATH-resolved path, so matching on the
|
|
117
151
|
// trailing name is exact — a bare manager name never comes back.
|
|
118
|
-
const manager = (await runCapture([wslPath, "-d", distro, "--exec", "sh", "-c", script], signal)).trim().split("\n")[0]?.trim() ?? "";
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
152
|
+
const manager = (await runCapture([...wslCommand(wslPath), "-d", distro, "--exec", "sh", "-c", script], signal)).trim().split("\n")[0]?.trim() ?? "";
|
|
153
|
+
const family = manager.endsWith("/apt-get")
|
|
154
|
+
? "apt"
|
|
155
|
+
: manager.endsWith("/dnf")
|
|
156
|
+
? "dnf"
|
|
157
|
+
: manager.endsWith("/pacman")
|
|
158
|
+
? "pacman"
|
|
159
|
+
: manager.endsWith("/zypper")
|
|
160
|
+
? "zypper"
|
|
161
|
+
: undefined;
|
|
162
|
+
// Only a real answer is remembered: an unreachable distro says nothing
|
|
163
|
+
// about which manager it has, and the next probe may reach it.
|
|
164
|
+
if (family !== undefined)
|
|
165
|
+
packageFamilies.set(key, family);
|
|
166
|
+
return family;
|
|
128
167
|
}
|
|
129
168
|
catch {
|
|
130
169
|
return undefined;
|
|
@@ -220,7 +259,7 @@ export async function bwrapUsable(options) {
|
|
|
220
259
|
return probeVerdicts.run(probeKey(wslPath, distro), async () => {
|
|
221
260
|
const prefix = bwrapArgvPrefix({ mode: "read-only", workspaceRoot: "/" });
|
|
222
261
|
try {
|
|
223
|
-
await runCapture([wslPath, "-d", distro, "--exec", ...prefix, "true"], signal);
|
|
262
|
+
await runCapture([...wslCommand(wslPath), "-d", distro, "--exec", ...prefix, "true"], signal);
|
|
224
263
|
return true;
|
|
225
264
|
}
|
|
226
265
|
catch (cause) {
|
package/lib/search-exec.d.ts
CHANGED
|
@@ -43,6 +43,12 @@
|
|
|
43
43
|
* Everything else — a genuine agent failure that is not `AgentUnavailableError`
|
|
44
44
|
* — still propagates through `done` as a rejection.
|
|
45
45
|
*
|
|
46
|
+
* The agent's capture cap is the tool's raw-output budget. A stream the
|
|
47
|
+
* agent cut there is reported `lossy`, exactly as the one-shot delegate's
|
|
48
|
+
* own reader reports it — the cut leaves the stream at exactly the budget,
|
|
49
|
+
* so a length cannot show it, and a result read as whole was missing every
|
|
50
|
+
* match past the cap.
|
|
51
|
+
*
|
|
46
52
|
* The one-shot delegate is post-processed the same way, because the agent
|
|
47
53
|
* being out must not change what a broken environment looks like: a missing
|
|
48
54
|
* program's `execvpe(rg) failed` line (exit 1) becomes the same 127, and the
|
package/lib/search-exec.js
CHANGED
|
@@ -43,6 +43,12 @@
|
|
|
43
43
|
* Everything else — a genuine agent failure that is not `AgentUnavailableError`
|
|
44
44
|
* — still propagates through `done` as a rejection.
|
|
45
45
|
*
|
|
46
|
+
* The agent's capture cap is the tool's raw-output budget. A stream the
|
|
47
|
+
* agent cut there is reported `lossy`, exactly as the one-shot delegate's
|
|
48
|
+
* own reader reports it — the cut leaves the stream at exactly the budget,
|
|
49
|
+
* so a length cannot show it, and a result read as whole was missing every
|
|
50
|
+
* match past the cap.
|
|
51
|
+
*
|
|
46
52
|
* The one-shot delegate is post-processed the same way, because the agent
|
|
47
53
|
* being out must not change what a broken environment looks like: a missing
|
|
48
54
|
* program's `execvpe(rg) failed` line (exit 1) becomes the same 127, and the
|
|
@@ -102,7 +108,7 @@ function workdirFailureText(linuxPath) {
|
|
|
102
108
|
* @returns the search handle (`done`, `collected`, `kill`).
|
|
103
109
|
*/
|
|
104
110
|
export function searchExecutionHandle({ agent, cwd, argv, distro, maxOutputBytes = 0, signal, spawnFallback }) {
|
|
105
|
-
const state = { delegate: undefined, stdout: undefined, stderr: undefined };
|
|
111
|
+
const state = { delegate: undefined, stdout: undefined, stderr: undefined, lossy: { stdout: false, stderr: false } };
|
|
106
112
|
const controller = new AbortController();
|
|
107
113
|
if (signal?.aborted)
|
|
108
114
|
controller.abort(signal.reason);
|
|
@@ -113,7 +119,7 @@ export function searchExecutionHandle({ agent, cwd, argv, distro, maxOutputBytes
|
|
|
113
119
|
readFrom(from) {
|
|
114
120
|
const buffer = state[slot] ?? Buffer.alloc(0);
|
|
115
121
|
const start = Math.min(from, buffer.length);
|
|
116
|
-
return { text: buffer.subarray(start).toString("utf8"), lossy:
|
|
122
|
+
return { text: buffer.subarray(start).toString("utf8"), lossy: state.lossy[slot], nextOffset: buffer.length };
|
|
117
123
|
},
|
|
118
124
|
});
|
|
119
125
|
/**
|
|
@@ -128,6 +134,7 @@ export function searchExecutionHandle({ agent, cwd, argv, distro, maxOutputBytes
|
|
|
128
134
|
state.delegate = undefined;
|
|
129
135
|
state.stdout = Buffer.alloc(0);
|
|
130
136
|
state.stderr = Buffer.from(stderr, "utf8");
|
|
137
|
+
state.lossy = { stdout: false, stderr: false };
|
|
131
138
|
return { exitCode, signal: null };
|
|
132
139
|
};
|
|
133
140
|
/**
|
|
@@ -185,6 +192,7 @@ export function searchExecutionHandle({ agent, cwd, argv, distro, maxOutputBytes
|
|
|
185
192
|
result = await agent.exec({ cwd, argv, maxOutputBytes, signal: controller.signal });
|
|
186
193
|
state.stdout = result.stdout;
|
|
187
194
|
state.stderr = result.stderr;
|
|
195
|
+
state.lossy = { stdout: result.truncated?.stdout === true, stderr: result.truncated?.stderr === true };
|
|
188
196
|
}
|
|
189
197
|
catch (error) {
|
|
190
198
|
if (controller.signal.aborted) {
|
package/lib/session-runtime.d.ts
CHANGED
|
@@ -8,6 +8,18 @@
|
|
|
8
8
|
* behind). The agent runs with no idle close — its lifetime is the session's —
|
|
9
9
|
* but an explicit client lease still catches a wedged host.
|
|
10
10
|
*
|
|
11
|
+
* The session's end is the harness's `agent/disposed`: a provider in the
|
|
12
|
+
* preset's realm hears it (rows there inherit the preset scope's tag, and
|
|
13
|
+
* agent events flow up the scope chain), and calls {@link
|
|
14
|
+
* SessionRuntimes.releaseSession}, which tears the session's runtimes down
|
|
15
|
+
* however many references they carry. Each holder drops its own book for the
|
|
16
|
+
* session on the same event, before it calls — and a holder that finds the
|
|
17
|
+
* registry no longer keeps an entry it cached ({@link SessionRuntimes.holds})
|
|
18
|
+
* acquires afresh rather than reusing a closed agent. The preset's own scope
|
|
19
|
+
* is NOT the session's: upstream disposes it only when the preset definition
|
|
20
|
+
* is unregistered, so a disposer registered by a provider there runs once per
|
|
21
|
+
* process, never once per session.
|
|
22
|
+
*
|
|
11
23
|
* The confinement here is the agent's own bwrap mount profile, WITHOUT a PID
|
|
12
24
|
* namespace: commands are the agent's children, so a background process
|
|
13
25
|
* survives the command, `/tmp` is the distro's real `/tmp` under
|
|
@@ -77,7 +89,7 @@ interface SessionRuntimeRequest {
|
|
|
77
89
|
export interface SessionRuntimeEntry {
|
|
78
90
|
/** The registry key (`distro`, workspace, mode, mask). */
|
|
79
91
|
key: string;
|
|
80
|
-
/** The systemd
|
|
92
|
+
/** The bare systemd unit name the scope was started with; the `.scope` suffix is systemd's to add. */
|
|
81
93
|
unit: string;
|
|
82
94
|
/** The resident agent. */
|
|
83
95
|
agent: WslAgent;
|
|
@@ -87,6 +99,12 @@ export interface SessionRuntimeEntry {
|
|
|
87
99
|
wslPath: string;
|
|
88
100
|
/** How many providers hold the runtime. */
|
|
89
101
|
refs: number;
|
|
102
|
+
/**
|
|
103
|
+
* The session the runtime belongs to, as the key's first component spells
|
|
104
|
+
* it. A caller with no session identity supplies a token of its own, which
|
|
105
|
+
* no `agent/disposed` ever names.
|
|
106
|
+
*/
|
|
107
|
+
sessionId: string;
|
|
90
108
|
}
|
|
91
109
|
/** The injectable edges, so the registry is testable without systemd. */
|
|
92
110
|
interface SessionRuntimeOptions {
|
|
@@ -166,6 +184,33 @@ export declare class SessionRuntimes {
|
|
|
166
184
|
* @returns resolved once the last holder's teardown finished.
|
|
167
185
|
*/
|
|
168
186
|
release(key: string): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Whether the registry still keeps THIS entry. A holder caches the entry it
|
|
189
|
+
* acquired; a session end tears the entry down whatever references remain,
|
|
190
|
+
* so a holder that did not drop its cache itself asks here before reusing
|
|
191
|
+
* it — a closed agent must never be handed out as a live runtime.
|
|
192
|
+
*
|
|
193
|
+
* @param entry - the entry the holder cached.
|
|
194
|
+
* @returns true while the registry tracks that exact entry.
|
|
195
|
+
*/
|
|
196
|
+
holds(entry: SessionRuntimeEntry): boolean;
|
|
197
|
+
/**
|
|
198
|
+
* End one session. Every runtime it named is torn down regardless of its
|
|
199
|
+
* reference count: the session is over, and a reference nobody will release
|
|
200
|
+
* again is not a reason to keep a scope. Holders drop their own books on the
|
|
201
|
+
* same event; a holder that did not notices through {@link
|
|
202
|
+
* SessionRuntimes.holds}. Idempotent: a second call finds nothing.
|
|
203
|
+
*
|
|
204
|
+
* @param sessionId - the session that ended.
|
|
205
|
+
* @returns resolved once every runtime of the session is torn down.
|
|
206
|
+
*/
|
|
207
|
+
releaseSession(sessionId: string): Promise<void>;
|
|
208
|
+
/**
|
|
209
|
+
* Tear one runtime down: close the agent, then stop the scope.
|
|
210
|
+
*
|
|
211
|
+
* @param entry - the runtime, already removed from the map.
|
|
212
|
+
*/
|
|
213
|
+
private teardown;
|
|
169
214
|
/**
|
|
170
215
|
* Release every runtime, for a host shutdown or a test. Sequential on
|
|
171
216
|
* purpose: concurrent closes would race the shared agent's state.
|