@neuralnomads/codenomad-dev 0.18.0-dev-20260715-07e8ce68 → 0.18.0-dev-20260716-40cee071
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/README.md +12 -0
- package/THIRD_PARTY_NOTICES.md +26 -0
- package/dist/events/bus.js +13 -0
- package/dist/events/bus.test.js +37 -0
- package/dist/index.js +33 -56
- package/dist/index.test.js +32 -0
- package/dist/server/__tests__/remote-proxy.test.js +102 -6
- package/dist/server/http-server.js +2 -0
- package/dist/server/remote-proxy.js +96 -9
- package/dist/server/routes/usage.js +18 -0
- package/dist/server/routes/usage.test.js +26 -0
- package/dist/server/routes/workspaces.js +32 -1
- package/dist/server/routes/workspaces.test.js +75 -0
- package/dist/settings/binaries.js +4 -1
- package/dist/settings/binaries.test.js +24 -0
- package/dist/shutdown.js +75 -0
- package/dist/shutdown.test.js +111 -0
- package/dist/usage/providers/api-key.js +171 -0
- package/dist/usage/providers/google.js +171 -0
- package/dist/usage/providers/minimax.js +109 -0
- package/dist/usage/providers/oauth.js +111 -0
- package/dist/usage/providers/special.js +148 -0
- package/dist/usage/service.js +83 -0
- package/dist/usage/service.test.js +59 -0
- package/dist/usage/shared.js +130 -0
- package/dist/usage/shared.test.js +33 -0
- package/dist/usage/types.js +1 -0
- package/dist/workspaces/__tests__/spawn.test.js +64 -7
- package/dist/workspaces/__tests__/workspace-identity.test.js +177 -25
- package/dist/workspaces/launch-cleanup.js +132 -0
- package/dist/workspaces/launch-cleanup.test.js +33 -0
- package/dist/workspaces/manager.js +393 -135
- package/dist/workspaces/manager.test.js +318 -0
- package/dist/workspaces/process-identity.js +408 -0
- package/dist/workspaces/process-identity.test.js +165 -0
- package/dist/workspaces/runtime.js +567 -122
- package/dist/workspaces/runtime.test.js +603 -0
- package/dist/workspaces/spawn.js +86 -15
- package/package.json +1 -1
- package/public/assets/{FilesTab-EPvldvn1.js → FilesTab-BjztpSdj.js} +2 -2
- package/public/assets/{GitChangesTab-ofjovmv2.js → GitChangesTab-B9XraSoR.js} +2 -2
- package/public/assets/{SplitFilePanel-CyoPkcLd.js → SplitFilePanel-Cu_5-Z-E.js} +1 -1
- package/public/assets/StatusTab-D_IE79r-.js +1 -0
- package/public/assets/{align-justify-Bcf1-ZOl.js → align-justify-BKS9gyRk.js} +1 -1
- package/public/assets/{bundle-full-D5dzvToA.js → bundle-full-CXGXXlzR.js} +1 -1
- package/public/assets/{diff-viewer-DViXCI-4.js → diff-viewer-CSNOYoEB.js} +1 -1
- package/public/assets/{git-diff-vendor-CSgooKT_.js → git-diff-vendor-CYS3ApzA.js} +1 -1
- package/public/assets/index-B3YjNCh4.js +1 -0
- package/public/assets/index-B3ZD2_SU.js +1 -0
- package/public/assets/index-Bh2ofaQo.js +1 -0
- package/public/assets/index-CM0c9xRZ.js +1 -0
- package/public/assets/index-CdDxouJl.js +2 -0
- package/public/assets/index-D06Y2cFq.js +1 -0
- package/public/assets/{index-lf4nUOU1.js → index-DVc4ls8c.js} +1 -1
- package/public/assets/index-PmE51vPh.js +1 -0
- package/public/assets/{index-BMvFMsuy.css → index-Qj63IiSj.css} +1 -1
- package/public/assets/index-VY_zwmsG.js +1 -0
- package/public/assets/index-qiiH7egV.js +1 -0
- package/public/assets/{index-UbL8tz_V.js → index-sLe9hEfQ.js} +1 -1
- package/public/assets/{loading-CZ_tt3h0.js → loading-CDmt9OJU.js} +1 -1
- package/public/assets/main-CTN6Wwz1.js +68 -0
- package/public/assets/{markdown-G11aN6hz.js → markdown-1ZLSxCFT.js} +55 -44
- package/public/assets/monaco-viewer-D6JgH76W.js +26 -0
- package/public/assets/{tool-call-BsSXWErm.js → tool-call-CyXb63LH.js} +32 -32
- package/public/assets/{unified-picker-BRNI3UvE.js → unified-picker-DCCdSVwI.js} +1 -1
- package/public/assets/{wrap-text-B7up-uv7.js → wrap-text-pNMdTIq3.js} +1 -1
- package/public/index.html +5 -5
- package/public/loading.html +5 -5
- package/public/sw.js +1 -1
- package/public/assets/StatusTab-Bgu1Fh24.js +0 -1
- package/public/assets/index-3KsjbWGP.js +0 -1
- package/public/assets/index-B0WK8-cF.js +0 -1
- package/public/assets/index-BDv_iVtj.js +0 -1
- package/public/assets/index-BjxZvyb9.js +0 -2
- package/public/assets/index-BleKf9Nj.js +0 -1
- package/public/assets/index-CiIQ43NW.js +0 -1
- package/public/assets/index-DBquqL6Q.js +0 -1
- package/public/assets/index-yf9zolv4.js +0 -1
- package/public/assets/index-ypaL7i_s.js +0 -1
- package/public/assets/main-CAkg_fp_.js +0 -68
- package/public/assets/monaco-viewer-CZefBfct.js +0 -26
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import { PassThrough } from "node:stream";
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import pino from "pino";
|
|
6
|
+
import { EventBus } from "../events/bus";
|
|
7
|
+
import { WorkspaceRuntime, WorkspaceRuntimeIdentityCaptureError, WorkspaceRuntimeLaunchCancelledError, WorkspaceStopTimeoutError, WorkspaceWindowsTreeCleanupIncompleteError, } from "./runtime";
|
|
8
|
+
class ManualTimers {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.nextId = 1;
|
|
11
|
+
this.pending = new Map();
|
|
12
|
+
this.setTimeout = (callback, delayMs) => {
|
|
13
|
+
const id = this.nextId++;
|
|
14
|
+
this.pending.set(id, { callback, delayMs });
|
|
15
|
+
return id;
|
|
16
|
+
};
|
|
17
|
+
this.clearTimeout = (timer) => {
|
|
18
|
+
this.pending.delete(timer);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
runNext() {
|
|
22
|
+
const next = Array.from(this.pending.entries()).sort((left, right) => left[1].delayMs - right[1].delayMs || left[0] - right[0])[0];
|
|
23
|
+
assert.ok(next, "expected a pending timer");
|
|
24
|
+
this.pending.delete(next[0]);
|
|
25
|
+
next[1].callback();
|
|
26
|
+
}
|
|
27
|
+
get size() {
|
|
28
|
+
return this.pending.size;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
class FakeChild extends EventEmitter {
|
|
32
|
+
constructor(pid = 4242) {
|
|
33
|
+
super();
|
|
34
|
+
this.pid = pid;
|
|
35
|
+
this.stdout = new PassThrough();
|
|
36
|
+
this.stderr = new PassThrough();
|
|
37
|
+
this.liveSignals = [];
|
|
38
|
+
this.exitCode = null;
|
|
39
|
+
this.signalCode = null;
|
|
40
|
+
}
|
|
41
|
+
kill(signal = "SIGTERM") {
|
|
42
|
+
this.liveSignals.push(signal);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
exit(code, signal) {
|
|
46
|
+
this.exitCode = code;
|
|
47
|
+
this.signalCode = signal;
|
|
48
|
+
this.emit("exit", code, signal);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function result(stdout = "", status = 0, stderr = "") {
|
|
52
|
+
return { pid: 1, output: [null, stdout, stderr], stdout, stderr, status, signal: null };
|
|
53
|
+
}
|
|
54
|
+
function linuxRows(rows, bootId = "boot-a") {
|
|
55
|
+
return rows.map(([pid, parentPid, groupId, start]) => `${pid}|${parentPid}|${groupId}|${start}|${bootId}|${start}`).join("\n");
|
|
56
|
+
}
|
|
57
|
+
function windowsRows(rows) {
|
|
58
|
+
return rows.map(([pid, parentPid, start], index) => `${pid}|${parentPid}|0|${start}||${100 + index}`).join("\n");
|
|
59
|
+
}
|
|
60
|
+
function guardedRows(matched, rows, cutoff, bootId = "boot-a") {
|
|
61
|
+
const targets = rows.map(([pid, parentPid, groupId, start]) => `CODENOMAD_TARGET|${pid}|${parentPid}|${groupId}|${start}|${bootId}|${start}`);
|
|
62
|
+
return [...targets, `CODENOMAD_RESULT|${matched ? "1" : "0"}|${cutoff ?? ""}|${targets.length > 0 ? "1" : "0"}`].join("\n");
|
|
63
|
+
}
|
|
64
|
+
function tokenSignalRows(rows, bootId = "boot-a") {
|
|
65
|
+
return [
|
|
66
|
+
...rows.map(([pid, parentPid, groupId, start]) => `CODENOMAD_TARGET|${pid}|${parentPid}|${groupId}|${start}|${bootId}|${start}`),
|
|
67
|
+
`CODENOMAD_RESULT|${rows.length > 0 ? "1" : "0"}`,
|
|
68
|
+
].join("\n");
|
|
69
|
+
}
|
|
70
|
+
function isTokenCleanup(args) {
|
|
71
|
+
return args.includes("codenomad-token-cleanup");
|
|
72
|
+
}
|
|
73
|
+
function isTokenSignal(args) {
|
|
74
|
+
return isTokenCleanup(args) && args.some((arg) => arg.includes("for pass in 1 2 3"));
|
|
75
|
+
}
|
|
76
|
+
function isGuarded(args) {
|
|
77
|
+
return !isTokenCleanup(args) && args.some((arg) => arg.includes("guarded-signal") || arg.includes("CODENOMAD_RESULT"));
|
|
78
|
+
}
|
|
79
|
+
async function createRuntime(options = {}, reportPort = true, binaryPath = "opencode") {
|
|
80
|
+
const child = new FakeChild();
|
|
81
|
+
const timers = new ManualTimers();
|
|
82
|
+
const platform = options.platform ?? "linux";
|
|
83
|
+
const defaultCommand = ((command, args) => {
|
|
84
|
+
if (isTokenCleanup(args)) {
|
|
85
|
+
return isTokenSignal(args)
|
|
86
|
+
? result(tokenSignalRows([[4242, 1, 4242, "100"]]))
|
|
87
|
+
: result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
88
|
+
}
|
|
89
|
+
if (isGuarded(args)) {
|
|
90
|
+
return platform === "win32"
|
|
91
|
+
? result("CODENOMAD_TARGET|4242|1|0|win-start||100\nCODENOMAD_RESULT|1||1")
|
|
92
|
+
: result(guardedRows(true, [[4242, 1, 4242, "100"]], "200"));
|
|
93
|
+
}
|
|
94
|
+
return platform === "win32"
|
|
95
|
+
? result(windowsRows([[4242, 1, "win-start"]]))
|
|
96
|
+
: result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
97
|
+
});
|
|
98
|
+
const runtime = new WorkspaceRuntime(new EventBus(), pino({ level: "silent" }), {
|
|
99
|
+
gracefulStopTimeoutMs: 10,
|
|
100
|
+
forcedStopTimeoutMs: 10,
|
|
101
|
+
spawnSync: defaultCommand,
|
|
102
|
+
...options,
|
|
103
|
+
setTimeout: timers.setTimeout,
|
|
104
|
+
clearTimeout: timers.clearTimeout,
|
|
105
|
+
spawn: (() => {
|
|
106
|
+
if (reportPort)
|
|
107
|
+
queueMicrotask(() => child.stdout.write("opencode server listening on http://127.0.0.1:4321\n"));
|
|
108
|
+
return child;
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
const launch = runtime.launch({ workspaceId: "workspace-1", folder: process.cwd(), binaryPath });
|
|
112
|
+
if (reportPort)
|
|
113
|
+
await launch;
|
|
114
|
+
return { runtime, child, timers, launch };
|
|
115
|
+
}
|
|
116
|
+
function setWslIdentity(runtime) {
|
|
117
|
+
const managed = runtime.processes.get("workspace-1");
|
|
118
|
+
assert.ok(managed);
|
|
119
|
+
managed.processKind = "wsl";
|
|
120
|
+
managed.wsl = {
|
|
121
|
+
distro: "Ubuntu",
|
|
122
|
+
linuxPid: 99,
|
|
123
|
+
linuxPgid: 99,
|
|
124
|
+
leaderStartTime: "50",
|
|
125
|
+
bootId: "wsl-boot",
|
|
126
|
+
members: new Map([[99, { pid: 99, parentPid: 1, groupId: 99, startTime: "50", bootId: "wsl-boot", startOrder: "50" }]]),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
describe("workspace runtime verified stop", () => {
|
|
130
|
+
it("keeps an error listener on children rejected before launch handlers are installed", async () => {
|
|
131
|
+
const child = new FakeChild(undefined);
|
|
132
|
+
const runtime = new WorkspaceRuntime(new EventBus(), pino({ level: "silent" }), {
|
|
133
|
+
platform: "linux",
|
|
134
|
+
spawn: (() => child),
|
|
135
|
+
spawnSync: (() => result()),
|
|
136
|
+
});
|
|
137
|
+
await assert.rejects(runtime.launch({ workspaceId: "missing-pid", folder: process.cwd(), binaryPath: "missing-opencode" }), WorkspaceRuntimeIdentityCaptureError);
|
|
138
|
+
assert.equal(child.listenerCount("error"), 1);
|
|
139
|
+
assert.doesNotThrow(() => child.emit("error", new Error("ENOENT")));
|
|
140
|
+
});
|
|
141
|
+
it("rejects a direct Windows launch without immutable process identity", async () => {
|
|
142
|
+
let commandCalls = 0;
|
|
143
|
+
const harness = await createRuntime({
|
|
144
|
+
platform: "win32",
|
|
145
|
+
spawnSync: (() => {
|
|
146
|
+
commandCalls += 1;
|
|
147
|
+
return result("", 1, "CIM unavailable");
|
|
148
|
+
}),
|
|
149
|
+
}, false, "opencode.exe");
|
|
150
|
+
await assert.rejects(harness.launch, WorkspaceRuntimeIdentityCaptureError);
|
|
151
|
+
assert.ok(commandCalls >= 1);
|
|
152
|
+
assert.deepEqual(harness.child.liveSignals, ["SIGTERM"]);
|
|
153
|
+
harness.child.exit(null, "SIGTERM");
|
|
154
|
+
});
|
|
155
|
+
it("stops an identity-matched direct Windows process tree", async () => {
|
|
156
|
+
let alive = true;
|
|
157
|
+
const commands = [];
|
|
158
|
+
const harness = await createRuntime({
|
|
159
|
+
platform: "win32",
|
|
160
|
+
spawnSync: ((command, args) => {
|
|
161
|
+
commands.push({ command, args: [...args] });
|
|
162
|
+
if (isGuarded(args)) {
|
|
163
|
+
alive = false;
|
|
164
|
+
return result([
|
|
165
|
+
"CODENOMAD_TARGET|4243|4242|0|descendant-start||101",
|
|
166
|
+
"CODENOMAD_TARGET|4242|1|0|win-start||100",
|
|
167
|
+
"CODENOMAD_RESULT|1||1",
|
|
168
|
+
].join("\n"));
|
|
169
|
+
}
|
|
170
|
+
return result(alive
|
|
171
|
+
? windowsRows([[4242, 1, "win-start"], [4243, 4242, "descendant-start"]])
|
|
172
|
+
: windowsRows([[7, 1, "other-start"]]));
|
|
173
|
+
}),
|
|
174
|
+
}, true, "opencode.exe");
|
|
175
|
+
await harness.runtime.stop("workspace-1");
|
|
176
|
+
assert.equal(commands.some(({ command }) => command === "taskkill.exe"), false);
|
|
177
|
+
assert.equal(commands.filter(({ args }) => isGuarded(args)).length, 1);
|
|
178
|
+
assert.deepEqual(harness.child.liveSignals, []);
|
|
179
|
+
});
|
|
180
|
+
it("keeps direct Windows stop bounded when the child ignores termination", async () => {
|
|
181
|
+
const commands = [];
|
|
182
|
+
const harness = await createRuntime({
|
|
183
|
+
platform: "win32",
|
|
184
|
+
spawnSync: ((command, args) => {
|
|
185
|
+
commands.push({ command, args: [...args] });
|
|
186
|
+
return isGuarded(args)
|
|
187
|
+
? result("CODENOMAD_TARGET|4242|1|0|win-start||100\nCODENOMAD_RESULT|1||1")
|
|
188
|
+
: result(windowsRows([[4242, 1, "win-start"]]));
|
|
189
|
+
}),
|
|
190
|
+
}, true, "opencode.exe");
|
|
191
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
192
|
+
harness.timers.runNext();
|
|
193
|
+
harness.timers.runNext();
|
|
194
|
+
await assert.rejects(stop, WorkspaceStopTimeoutError);
|
|
195
|
+
assert.equal(commands.filter(({ args }) => isGuarded(args)).length, 2);
|
|
196
|
+
assert.equal(commands.some(({ command }) => command === "taskkill.exe"), false);
|
|
197
|
+
assert.deepEqual(harness.child.liveSignals, []);
|
|
198
|
+
});
|
|
199
|
+
it("keeps tracking a descendant after partial guarded Windows termination", async () => {
|
|
200
|
+
let stage = "tree";
|
|
201
|
+
let guardedCalls = 0;
|
|
202
|
+
const harness = await createRuntime({
|
|
203
|
+
platform: "win32",
|
|
204
|
+
spawnSync: ((_command, args) => {
|
|
205
|
+
if (isGuarded(args)) {
|
|
206
|
+
guardedCalls += 1;
|
|
207
|
+
if (guardedCalls === 1) {
|
|
208
|
+
stage = "descendant";
|
|
209
|
+
return result([
|
|
210
|
+
"CODENOMAD_TARGET|4242|1|0|win-start||100",
|
|
211
|
+
"CODENOMAD_TARGET|4243|4242|0|descendant-start||101",
|
|
212
|
+
].join("\n"), 1, "descendant termination failed");
|
|
213
|
+
}
|
|
214
|
+
stage = "gone";
|
|
215
|
+
return result("CODENOMAD_TARGET|4243|1|0|descendant-start||101\nCODENOMAD_RESULT|0||1");
|
|
216
|
+
}
|
|
217
|
+
if (stage === "tree") {
|
|
218
|
+
return result(windowsRows([[4242, 1, "win-start"], [4243, 4242, "descendant-start"]]));
|
|
219
|
+
}
|
|
220
|
+
if (stage === "descendant") {
|
|
221
|
+
return result(windowsRows([[4243, 1, "descendant-start"]]));
|
|
222
|
+
}
|
|
223
|
+
return result(windowsRows([[7, 1, "other-start"]]));
|
|
224
|
+
}),
|
|
225
|
+
}, true, "opencode.exe");
|
|
226
|
+
let settled = false;
|
|
227
|
+
const stop = harness.runtime.stop("workspace-1").finally(() => { settled = true; });
|
|
228
|
+
await Promise.resolve();
|
|
229
|
+
assert.equal(settled, false);
|
|
230
|
+
harness.timers.runNext();
|
|
231
|
+
harness.timers.runNext();
|
|
232
|
+
await stop;
|
|
233
|
+
assert.equal(guardedCalls, 2);
|
|
234
|
+
});
|
|
235
|
+
it("cleans a token-matching descendant that survives its unidentified wrapper", async () => {
|
|
236
|
+
const child = new FakeChild();
|
|
237
|
+
const timers = new ManualTimers();
|
|
238
|
+
let tokenAlive = true;
|
|
239
|
+
let spawnedToken = "";
|
|
240
|
+
let logs = "";
|
|
241
|
+
const tokenSignals = [];
|
|
242
|
+
const runtime = new WorkspaceRuntime(new EventBus(), pino({ level: "trace" }, {
|
|
243
|
+
write: (chunk) => { logs += chunk; },
|
|
244
|
+
}), {
|
|
245
|
+
platform: "linux",
|
|
246
|
+
spawnSync: ((_command, args) => {
|
|
247
|
+
if (isTokenCleanup(args)) {
|
|
248
|
+
if (isTokenSignal(args)) {
|
|
249
|
+
const rows = tokenAlive
|
|
250
|
+
? [[5000, 1, 4242, "150"]]
|
|
251
|
+
: [];
|
|
252
|
+
tokenSignals.push(rows.map(([pid]) => pid));
|
|
253
|
+
tokenAlive = false;
|
|
254
|
+
return result(tokenSignalRows(rows));
|
|
255
|
+
}
|
|
256
|
+
return result(tokenAlive ? linuxRows([[5000, 1, 4242, "150"]]) : "");
|
|
257
|
+
}
|
|
258
|
+
return result("", 1, "proc unavailable");
|
|
259
|
+
}),
|
|
260
|
+
spawn: ((_command, _args, options) => {
|
|
261
|
+
spawnedToken = String(options?.env?.CODENOMAD_LAUNCH_CLEANUP_TOKEN ?? "");
|
|
262
|
+
return child;
|
|
263
|
+
}),
|
|
264
|
+
setTimeout: timers.setTimeout,
|
|
265
|
+
clearTimeout: timers.clearTimeout,
|
|
266
|
+
});
|
|
267
|
+
await assert.rejects(runtime.launch({ workspaceId: "workspace-1", folder: process.cwd(), binaryPath: "opencode" }), WorkspaceRuntimeIdentityCaptureError);
|
|
268
|
+
assert.equal(runtime.processes.size, 0);
|
|
269
|
+
assert.deepEqual(child.liveSignals, ["SIGTERM"]);
|
|
270
|
+
assert.match(spawnedToken, /^[a-f0-9]{64}$/);
|
|
271
|
+
assert.equal(logs.includes(spawnedToken), false);
|
|
272
|
+
assert.match(logs, /\[REDACTED\]/);
|
|
273
|
+
assert.deepEqual(tokenSignals, [[5000]]);
|
|
274
|
+
});
|
|
275
|
+
it("uses one guarded command per signal", async () => {
|
|
276
|
+
const guardedInvocations = [];
|
|
277
|
+
const mutableInvocations = guardedInvocations;
|
|
278
|
+
const spawnCommand = ((_command, args) => {
|
|
279
|
+
if (isTokenCleanup(args)) {
|
|
280
|
+
return isTokenSignal(args)
|
|
281
|
+
? result(tokenSignalRows([[4242, 1, 4242, "100"]]))
|
|
282
|
+
: result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
283
|
+
}
|
|
284
|
+
if (isGuarded(args)) {
|
|
285
|
+
mutableInvocations.push([...args]);
|
|
286
|
+
return result(guardedRows(true, [[4242, 1, 4242, "100"]], "200"));
|
|
287
|
+
}
|
|
288
|
+
return result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
289
|
+
});
|
|
290
|
+
const harness = await createRuntime({
|
|
291
|
+
platform: "linux",
|
|
292
|
+
spawnSync: spawnCommand,
|
|
293
|
+
});
|
|
294
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
295
|
+
harness.timers.runNext();
|
|
296
|
+
harness.timers.runNext();
|
|
297
|
+
await assert.rejects(stop, WorkspaceStopTimeoutError);
|
|
298
|
+
assert.equal(guardedInvocations.length, 2);
|
|
299
|
+
assert.ok(guardedInvocations.every((args) => args.includes("codenomad-guarded-signal")));
|
|
300
|
+
});
|
|
301
|
+
it("sends no second command when the guarded operation reports identity mismatch", async () => {
|
|
302
|
+
let launchProbe = true;
|
|
303
|
+
let guardedCommands = 0;
|
|
304
|
+
const spawnCommand = ((_command, args) => {
|
|
305
|
+
if (isTokenCleanup(args))
|
|
306
|
+
return result(isTokenSignal(args) ? tokenSignalRows([]) : "");
|
|
307
|
+
if (isGuarded(args)) {
|
|
308
|
+
guardedCommands += 1;
|
|
309
|
+
return result("CODENOMAD_RESULT|0||0");
|
|
310
|
+
}
|
|
311
|
+
if (launchProbe) {
|
|
312
|
+
launchProbe = false;
|
|
313
|
+
return result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
314
|
+
}
|
|
315
|
+
return result(linuxRows([[4242, 1, 4242, "300"]]));
|
|
316
|
+
});
|
|
317
|
+
const harness = await createRuntime({ platform: "linux", spawnSync: spawnCommand });
|
|
318
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
319
|
+
await stop;
|
|
320
|
+
assert.equal(guardedCommands, 1);
|
|
321
|
+
});
|
|
322
|
+
it("tracks a descendant forked between the precheck and SIGTERM dispatch", async () => {
|
|
323
|
+
let phase = "launch";
|
|
324
|
+
const guardedTargets = [];
|
|
325
|
+
const spawnCommand = ((_command, args) => {
|
|
326
|
+
if (isTokenCleanup(args)) {
|
|
327
|
+
const rows = phase === "after-term" ? [[5000, 4242, 4242, "120"]] : [];
|
|
328
|
+
return result(isTokenSignal(args) ? tokenSignalRows(rows) : linuxRows(rows));
|
|
329
|
+
}
|
|
330
|
+
if (isGuarded(args)) {
|
|
331
|
+
guardedTargets.push([...args]);
|
|
332
|
+
if (phase === "launch") {
|
|
333
|
+
phase = "after-term";
|
|
334
|
+
return result(guardedRows(true, [[4242, 1, 4242, "100"], [5000, 4242, 4242, "120"]], "200"));
|
|
335
|
+
}
|
|
336
|
+
phase = "after-kill";
|
|
337
|
+
return result(guardedRows(false, [[5000, 4242, 4242, "120"]]));
|
|
338
|
+
}
|
|
339
|
+
if (phase === "launch")
|
|
340
|
+
return result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
341
|
+
if (phase === "after-term")
|
|
342
|
+
return result(linuxRows([[5000, 1, 4242, "120"]]));
|
|
343
|
+
return result(linuxRows([[7, 1, 7, "10"]]));
|
|
344
|
+
});
|
|
345
|
+
const harness = await createRuntime({ platform: "linux", spawnSync: spawnCommand });
|
|
346
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
347
|
+
harness.timers.runNext();
|
|
348
|
+
harness.timers.runNext();
|
|
349
|
+
await stop;
|
|
350
|
+
assert.ok(guardedTargets[1]?.includes("5000"));
|
|
351
|
+
assert.ok(guardedTargets[1]?.includes("120"));
|
|
352
|
+
});
|
|
353
|
+
it("adopts a descendant forked after SIGTERM and before the returned cutoff", async () => {
|
|
354
|
+
let phase = "launch";
|
|
355
|
+
const guardedTargets = [];
|
|
356
|
+
const spawnCommand = ((_command, args) => {
|
|
357
|
+
if (isTokenCleanup(args)) {
|
|
358
|
+
const rows = phase === "after-term" ? [[5000, 1, 4242, "150"]] : [];
|
|
359
|
+
return result(isTokenSignal(args) ? tokenSignalRows(rows) : linuxRows(rows));
|
|
360
|
+
}
|
|
361
|
+
if (isGuarded(args)) {
|
|
362
|
+
guardedTargets.push([...args]);
|
|
363
|
+
if (phase === "launch") {
|
|
364
|
+
phase = "after-term";
|
|
365
|
+
return result(guardedRows(true, [[4242, 1, 4242, "100"]], "200"));
|
|
366
|
+
}
|
|
367
|
+
phase = "after-kill";
|
|
368
|
+
return result(guardedRows(false, [[5000, 4242, 4242, "150"]]));
|
|
369
|
+
}
|
|
370
|
+
if (phase === "launch")
|
|
371
|
+
return result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
372
|
+
if (phase === "after-term") {
|
|
373
|
+
return result(linuxRows([[5000, 1, 4242, "150"]]));
|
|
374
|
+
}
|
|
375
|
+
return result(linuxRows([[7, 1, 7, "10"]]));
|
|
376
|
+
});
|
|
377
|
+
const harness = await createRuntime({ platform: "linux", spawnSync: spawnCommand });
|
|
378
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
379
|
+
harness.child.exit(0, null);
|
|
380
|
+
harness.timers.runNext();
|
|
381
|
+
harness.timers.runNext();
|
|
382
|
+
await stop;
|
|
383
|
+
assert.equal(guardedTargets.length, 2);
|
|
384
|
+
assert.ok(guardedTargets[1]?.includes("5000"), "SIGKILL must include the newly tracked immutable descendant");
|
|
385
|
+
assert.ok(guardedTargets[1]?.includes("150"));
|
|
386
|
+
});
|
|
387
|
+
it("does not adopt a newly reused process group after original ownership is lost", async () => {
|
|
388
|
+
let launched = false;
|
|
389
|
+
let termSent = false;
|
|
390
|
+
let guardedCommands = 0;
|
|
391
|
+
const spawnCommand = ((_command, args) => {
|
|
392
|
+
if (isTokenCleanup(args))
|
|
393
|
+
return result(isTokenSignal(args) ? tokenSignalRows([]) : "");
|
|
394
|
+
if (isGuarded(args)) {
|
|
395
|
+
guardedCommands += 1;
|
|
396
|
+
termSent = true;
|
|
397
|
+
return result(guardedRows(true, [[4242, 1, 4242, "100"]], "200"));
|
|
398
|
+
}
|
|
399
|
+
if (!launched) {
|
|
400
|
+
launched = true;
|
|
401
|
+
return result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
402
|
+
}
|
|
403
|
+
return termSent
|
|
404
|
+
? result(linuxRows([[4242, 1, 4242, "300"], [6000, 4242, 4242, "150"]]))
|
|
405
|
+
: result(linuxRows([[4242, 1, 4242, "100"]]));
|
|
406
|
+
});
|
|
407
|
+
const harness = await createRuntime({ platform: "linux", spawnSync: spawnCommand });
|
|
408
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
409
|
+
await stop;
|
|
410
|
+
assert.equal(guardedCommands, 1);
|
|
411
|
+
});
|
|
412
|
+
it("uses a guarded WSL command and retains immutable Linux identities", async () => {
|
|
413
|
+
let alive = true;
|
|
414
|
+
const guardedCommands = [];
|
|
415
|
+
const spawnCommand = ((command, args) => {
|
|
416
|
+
if (command === "powershell.exe")
|
|
417
|
+
return result(windowsRows([[4242, 1, "host-start"]]));
|
|
418
|
+
if (isTokenCleanup(args)) {
|
|
419
|
+
const rows = alive ? [[99, 1, 99, "50"]] : [];
|
|
420
|
+
if (isTokenSignal(args))
|
|
421
|
+
alive = false;
|
|
422
|
+
return result(isTokenSignal(args) ? tokenSignalRows(rows, "wsl-boot") : linuxRows(rows, "wsl-boot"));
|
|
423
|
+
}
|
|
424
|
+
if (args.includes("codenomad-wsl-guarded-signal")) {
|
|
425
|
+
guardedCommands.push([...args]);
|
|
426
|
+
alive = false;
|
|
427
|
+
return result(guardedRows(true, [[99, 1, 99, "50"]], "80", "wsl-boot"));
|
|
428
|
+
}
|
|
429
|
+
if (args.includes("codenomad-wsl-identity")) {
|
|
430
|
+
return result(alive ? linuxRows([[99, 1, 99, "50"]], "wsl-boot") : linuxRows([[7, 1, 7, "10"]], "wsl-boot"));
|
|
431
|
+
}
|
|
432
|
+
return result();
|
|
433
|
+
});
|
|
434
|
+
const harness = await createRuntime({ platform: "win32", spawnSync: spawnCommand }, true, "opencode.cmd");
|
|
435
|
+
setWslIdentity(harness.runtime);
|
|
436
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
437
|
+
await stop;
|
|
438
|
+
assert.equal(guardedCommands.length, 1);
|
|
439
|
+
assert.ok(guardedCommands[0]?.includes("wsl-boot"));
|
|
440
|
+
});
|
|
441
|
+
it("launches and stops a bare opencode cmd shim without PowerShell", async () => {
|
|
442
|
+
const commands = [];
|
|
443
|
+
const spawnCommand = ((command, args) => {
|
|
444
|
+
commands.push({ command, args: [...args] });
|
|
445
|
+
return command === "powershell.exe" ? result("", 1, "ETIMEDOUT") : result();
|
|
446
|
+
});
|
|
447
|
+
const harness = await createRuntime({ platform: "win32", spawnSync: spawnCommand }, true, "opencode");
|
|
448
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
449
|
+
assert.deepEqual(commands, [{ command: "taskkill.exe", args: ["/PID", "4242", "/T"] }]);
|
|
450
|
+
harness.child.exit(0, null);
|
|
451
|
+
await stop;
|
|
452
|
+
assert.equal(commands.some(({ command }) => command === "powershell.exe"), false);
|
|
453
|
+
});
|
|
454
|
+
it("adds force only when Windows wrapper cleanup escalates", async () => {
|
|
455
|
+
const invocations = [];
|
|
456
|
+
const harness = await createRuntime({
|
|
457
|
+
platform: "win32",
|
|
458
|
+
spawnSync: ((_command, args) => {
|
|
459
|
+
invocations.push([...args]);
|
|
460
|
+
return result();
|
|
461
|
+
}),
|
|
462
|
+
}, true, "opencode.cmd");
|
|
463
|
+
const stop = harness.runtime.stop("workspace-1");
|
|
464
|
+
harness.timers.runNext();
|
|
465
|
+
assert.deepEqual(invocations, [
|
|
466
|
+
["/PID", "4242", "/T"],
|
|
467
|
+
["/PID", "4242", "/T", "/F"],
|
|
468
|
+
]);
|
|
469
|
+
harness.child.exit(0, null);
|
|
470
|
+
await stop;
|
|
471
|
+
});
|
|
472
|
+
it("keeps failed Windows wrapper cleanup bounded and retryable", async () => {
|
|
473
|
+
let available = false;
|
|
474
|
+
const harness = await createRuntime({
|
|
475
|
+
platform: "win32",
|
|
476
|
+
spawnSync: (() => available ? result() : result("", 1, "taskkill unavailable")),
|
|
477
|
+
}, true, "opencode.cmd");
|
|
478
|
+
const first = harness.runtime.stop("workspace-1");
|
|
479
|
+
harness.timers.runNext();
|
|
480
|
+
harness.timers.runNext();
|
|
481
|
+
await assert.rejects(first, (error) => {
|
|
482
|
+
assert.ok(error instanceof WorkspaceStopTimeoutError);
|
|
483
|
+
assert.match(error.message, /taskkill \/T failed: taskkill unavailable/);
|
|
484
|
+
assert.match(error.message, /taskkill \/T \/F failed: taskkill unavailable/);
|
|
485
|
+
return true;
|
|
486
|
+
});
|
|
487
|
+
available = true;
|
|
488
|
+
const retry = harness.runtime.stop("workspace-1");
|
|
489
|
+
harness.child.exit(0, null);
|
|
490
|
+
await retry;
|
|
491
|
+
});
|
|
492
|
+
it("never signals an exited wrapper PID after tree cleanup was not confirmed", async () => {
|
|
493
|
+
const invocations = [];
|
|
494
|
+
const harness = await createRuntime({
|
|
495
|
+
platform: "win32",
|
|
496
|
+
spawnSync: ((_command, args) => {
|
|
497
|
+
invocations.push([...args]);
|
|
498
|
+
return result("", 1, "taskkill unavailable");
|
|
499
|
+
}),
|
|
500
|
+
}, true, "opencode");
|
|
501
|
+
const first = harness.runtime.stop("workspace-1");
|
|
502
|
+
harness.child.exit(1, null);
|
|
503
|
+
await assert.rejects(first, WorkspaceWindowsTreeCleanupIncompleteError);
|
|
504
|
+
assert.equal(harness.runtime.processes.size, 1);
|
|
505
|
+
assert.deepEqual(invocations, [["/PID", "4242", "/T"]]);
|
|
506
|
+
const second = harness.runtime.stop("workspace-1");
|
|
507
|
+
await assert.rejects(second, (error) => {
|
|
508
|
+
assert.ok(error instanceof WorkspaceWindowsTreeCleanupIncompleteError);
|
|
509
|
+
assert.match(error.message, /taskkill unavailable/);
|
|
510
|
+
return true;
|
|
511
|
+
});
|
|
512
|
+
assert.equal(harness.runtime.processes.size, 1);
|
|
513
|
+
assert.deepEqual(invocations, [["/PID", "4242", "/T"]]);
|
|
514
|
+
});
|
|
515
|
+
it("persists confirmed wrapper tree cleanup across a later exit and retry", async () => {
|
|
516
|
+
const invocations = [];
|
|
517
|
+
const harness = await createRuntime({
|
|
518
|
+
platform: "win32",
|
|
519
|
+
spawnSync: ((_command, args) => {
|
|
520
|
+
invocations.push([...args]);
|
|
521
|
+
return result();
|
|
522
|
+
}),
|
|
523
|
+
}, true, "opencode.cmd");
|
|
524
|
+
const first = harness.runtime.stop("workspace-1");
|
|
525
|
+
harness.timers.runNext();
|
|
526
|
+
harness.timers.runNext();
|
|
527
|
+
await assert.rejects(first, WorkspaceStopTimeoutError);
|
|
528
|
+
harness.child.exit(0, null);
|
|
529
|
+
await harness.runtime.stop("workspace-1");
|
|
530
|
+
assert.deepEqual(invocations, [
|
|
531
|
+
["/PID", "4242", "/T"],
|
|
532
|
+
["/PID", "4242", "/T", "/F"],
|
|
533
|
+
]);
|
|
534
|
+
assert.equal(harness.runtime.processes.size, 0);
|
|
535
|
+
});
|
|
536
|
+
it("retains launch identity so a transient stop failure can recover on retry", async () => {
|
|
537
|
+
let available = true;
|
|
538
|
+
let alive = true;
|
|
539
|
+
let guardedCommands = 0;
|
|
540
|
+
const spawnCommand = ((_command, args) => {
|
|
541
|
+
if (isTokenCleanup(args)) {
|
|
542
|
+
if (!available)
|
|
543
|
+
return result("", 1, "identity service unavailable");
|
|
544
|
+
const rows = alive ? [[4242, 1, 4242, "100"]] : [];
|
|
545
|
+
if (isTokenSignal(args))
|
|
546
|
+
alive = false;
|
|
547
|
+
return result(isTokenSignal(args) ? tokenSignalRows(rows) : linuxRows(rows));
|
|
548
|
+
}
|
|
549
|
+
if (isGuarded(args)) {
|
|
550
|
+
guardedCommands += 1;
|
|
551
|
+
if (!available)
|
|
552
|
+
return result("", 1, "identity service unavailable");
|
|
553
|
+
alive = false;
|
|
554
|
+
return result(guardedRows(true, [[4242, 1, 4242, "100"]], "200"));
|
|
555
|
+
}
|
|
556
|
+
if (!available)
|
|
557
|
+
return result("", 1, "identity service unavailable");
|
|
558
|
+
return result(alive ? linuxRows([[4242, 1, 4242, "100"]]) : linuxRows([[7, 1, 7, "10"]]));
|
|
559
|
+
});
|
|
560
|
+
const harness = await createRuntime({ platform: "linux", spawnSync: spawnCommand });
|
|
561
|
+
available = false;
|
|
562
|
+
const first = harness.runtime.stop("workspace-1");
|
|
563
|
+
harness.timers.runNext();
|
|
564
|
+
harness.timers.runNext();
|
|
565
|
+
await assert.rejects(first, /cleanup could not be confirmed/);
|
|
566
|
+
available = true;
|
|
567
|
+
const retry = harness.runtime.stop("workspace-1");
|
|
568
|
+
await retry;
|
|
569
|
+
assert.equal(guardedCommands, 3);
|
|
570
|
+
});
|
|
571
|
+
it("shares one bounded stop operation across concurrent callers", async () => {
|
|
572
|
+
const harness = await createRuntime({ platform: "linux" });
|
|
573
|
+
const first = harness.runtime.stop("workspace-1");
|
|
574
|
+
const second = harness.runtime.stop("workspace-1");
|
|
575
|
+
assert.strictEqual(first, second);
|
|
576
|
+
harness.timers.runNext();
|
|
577
|
+
harness.timers.runNext();
|
|
578
|
+
const results = await Promise.allSettled([first, second]);
|
|
579
|
+
assert.equal(results[0].status, "rejected");
|
|
580
|
+
assert.equal(results[1].status, "rejected");
|
|
581
|
+
});
|
|
582
|
+
it("cancels a no-port launch while retaining retryable stop state", async () => {
|
|
583
|
+
let alive = true;
|
|
584
|
+
const spawnCommand = ((_command, args) => {
|
|
585
|
+
if (isTokenCleanup(args)) {
|
|
586
|
+
const rows = alive ? [[4242, 1, 4242, "100"]] : [];
|
|
587
|
+
return result(isTokenSignal(args) ? tokenSignalRows(rows) : linuxRows(rows));
|
|
588
|
+
}
|
|
589
|
+
if (isGuarded(args))
|
|
590
|
+
return result(guardedRows(true, [[4242, 1, 4242, "100"]], "200"));
|
|
591
|
+
return result(alive ? linuxRows([[4242, 1, 4242, "100"]]) : linuxRows([[7, 1, 7, "10"]]));
|
|
592
|
+
});
|
|
593
|
+
const harness = await createRuntime({ platform: "linux", spawnSync: spawnCommand }, false);
|
|
594
|
+
const firstStop = harness.runtime.stop("workspace-1");
|
|
595
|
+
await assert.rejects(harness.launch, WorkspaceRuntimeLaunchCancelledError);
|
|
596
|
+
harness.timers.runNext();
|
|
597
|
+
harness.timers.runNext();
|
|
598
|
+
await assert.rejects(firstStop, WorkspaceStopTimeoutError);
|
|
599
|
+
alive = false;
|
|
600
|
+
const retry = harness.runtime.stop("workspace-1");
|
|
601
|
+
await retry;
|
|
602
|
+
});
|
|
603
|
+
});
|