@neuralnomads/codenomad 0.17.0 → 0.19.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/README.md +12 -0
- package/THIRD_PARTY_NOTICES.md +26 -0
- package/dist/auth/manager.js +1 -1
- package/dist/background-processes/manager.js +15 -30
- package/dist/background-processes/manager.test.js +123 -0
- package/dist/config/schema.js +2 -1
- package/dist/config/schema.test.js +13 -0
- package/dist/events/bus.js +17 -0
- package/dist/events/bus.test.js +37 -0
- package/dist/filesystem/__tests__/search-cache.test.js +20 -8
- package/dist/filesystem/__tests__/search.test.js +53 -0
- package/dist/filesystem/search-cache.js +6 -10
- package/dist/filesystem/search.js +22 -9
- package/dist/index.js +77 -58
- package/dist/index.test.js +62 -0
- package/dist/opencode-plugin/codenomad-opencode-plugin.tgz +0 -0
- package/dist/opencode-update/service.js +150 -0
- package/dist/opencode-update/service.test.js +107 -0
- package/dist/permissions/auto-accept-manager.js +380 -0
- package/dist/permissions/auto-accept-manager.test.js +736 -0
- package/dist/permissions/auto-accept-store.js +118 -0
- package/dist/permissions/auto-accept-store.test.js +157 -0
- package/dist/permissions/opencode-replier.js +31 -0
- package/dist/permissions/opencode-yolo-metadata.js +81 -0
- package/dist/permissions/opencode-yolo-metadata.test.js +54 -0
- package/dist/server/__tests__/listener-retry.test.js +15 -0
- package/dist/server/__tests__/remote-proxy.test.js +151 -47
- package/dist/server/http-server.js +22 -9
- package/dist/server/remote-proxy.js +91 -34
- package/dist/server/routes/auth-pages/login.html +3 -3
- package/dist/server/routes/auth.js +1 -1
- package/dist/server/routes/auth.test.js +21 -0
- package/dist/server/routes/opencode-update.js +37 -0
- package/dist/server/routes/opencode-update.test.js +34 -0
- package/dist/server/routes/remote-servers.js +1 -1
- package/dist/server/routes/settings.js +48 -2
- package/dist/server/routes/settings.test.js +58 -0
- package/dist/server/routes/usage.js +18 -0
- package/dist/server/routes/usage.test.js +26 -0
- package/dist/server/routes/workspaces.js +34 -2
- package/dist/server/routes/workspaces.test.js +113 -0
- package/dist/server/routes/worktrees.js +19 -0
- package/dist/server/routes/yolo.js +13 -0
- package/dist/server/tls.js +2 -2
- package/dist/settings/binaries.js +4 -1
- package/dist/settings/binaries.test.js +19 -0
- package/dist/settings/public-config.js +14 -0
- package/dist/settings/public-config.test.js +56 -0
- package/dist/settings/service.js +17 -5
- package/dist/settings/service.test.js +51 -0
- package/dist/settings/yaml-doc-store.js +55 -4
- package/dist/settings/yaml-doc-store.test.js +78 -0
- package/dist/shutdown.js +83 -0
- package/dist/shutdown.test.js +148 -0
- package/dist/sidecars/manager.js +26 -3
- package/dist/sidecars/manager.test.js +42 -0
- package/dist/speech/service.js +121 -10
- package/dist/speech/service.test.js +247 -0
- package/dist/ui/remote-ui.js +2 -2
- 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 +76 -0
- package/dist/usage/providers/special.js +148 -0
- package/dist/usage/service.js +83 -0
- package/dist/usage/service.test.js +61 -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 +163 -0
- package/dist/workspaces/binary-path.test.js +10 -0
- package/dist/workspaces/instance-client.js +42 -0
- package/dist/workspaces/instance-client.test.js +149 -0
- package/dist/workspaces/instance-events.js +2 -2
- package/dist/workspaces/launch-cleanup.test.js +29 -0
- package/dist/workspaces/loopback.js +8 -0
- package/dist/workspaces/manager.js +473 -128
- package/dist/workspaces/manager.test.js +335 -0
- package/dist/workspaces/process-identity.darwin.test.js +88 -0
- package/dist/workspaces/process-identity.js +449 -0
- package/dist/workspaces/process-identity.test.js +140 -0
- package/dist/workspaces/runtime.js +471 -138
- package/dist/workspaces/runtime.test.js +286 -0
- package/dist/workspaces/spawn.js +86 -15
- package/dist/workspaces/workspace-identity.js +27 -0
- package/package.json +2 -1
- package/public/assets/FilesTab-Cbs7b7y9.js +2 -0
- package/public/assets/{GitChangesTab-BksCi53d.js → GitChangesTab-D3H4cqJJ.js} +2 -2
- package/public/assets/{SplitFilePanel-CrSfR22r.js → SplitFilePanel-C_Bcle0r.js} +1 -1
- package/public/assets/StatusTab-D1-tfkgN.js +1 -0
- package/public/assets/{align-justify-hRtBz8xe.js → align-justify-B5HUL_LR.js} +1 -1
- package/public/assets/{bundle-full-BVDA-a9Y.js → bundle-full-CEFzES5w.js} +1 -1
- package/public/assets/{diff-viewer-BCAI0Cw8.js → diff-viewer-BBC2_yI1.js} +1 -1
- package/public/assets/{git-diff-vendor-CSgooKT_.js → git-diff-vendor-CYS3ApzA.js} +1 -1
- package/public/assets/{index-DnCBfFB3.js → index-B81f0aWL.js} +1 -1
- package/public/assets/index-BHlYVddm.js +1 -0
- package/public/assets/index-Best16ZB.js +2 -0
- package/public/assets/index-Boq37ErX.js +1 -0
- package/public/assets/index-By9pgzQk.css +1 -0
- package/public/assets/index-C0caoeJg.js +1 -0
- package/public/assets/index-Dqg2UVvS.js +1 -0
- package/public/assets/index-it6EGJV6.js +1 -0
- package/public/assets/{index-Ddsv8Uc0.js → index-lORR8OnW.js} +1 -1
- package/public/assets/index-ppe6yWaU.js +1 -0
- package/public/assets/index-wEqeBwqT.js +1 -0
- package/public/assets/index-yEcajWX_.js +1 -0
- package/public/assets/{loading-bD3ppK2l.js → loading-CbUWFqQ_.js} +1 -1
- package/public/assets/main-DfSREDgt.js +69 -0
- package/public/assets/{markdown-DJuaMC5H.js → markdown-CEtfpKK-.js} +60 -47
- package/public/assets/monaco-viewer-NKl8kwH2.js +26 -0
- package/public/assets/terminal-square-q-by1uT7.js +1 -0
- package/public/assets/tool-call-CZL2uqIM.js +59 -0
- package/public/assets/unified-picker-CvJXD-j8.js +1 -0
- package/public/assets/{wrap-text-BkcW_eTA.js → wrap-text-Bo7xr74P.js} +1 -1
- package/public/index.html +5 -5
- package/public/loading.html +5 -5
- package/public/sw.js +1 -1
- package/public/ui-version.json +1 -1
- package/public/assets/FilesTab-B5ysoRLh.js +0 -2
- package/public/assets/StatusTab-Dgqekgjn.js +0 -1
- package/public/assets/index-B2Qd6xQq.css +0 -1
- package/public/assets/index-BQpEhdk5.js +0 -1
- package/public/assets/index-BXIEm2zT.js +0 -1
- package/public/assets/index-CWjU9mAF.js +0 -1
- package/public/assets/index-Cvtlo58a.js +0 -1
- package/public/assets/index-DFlxnvHT.js +0 -2
- package/public/assets/index-Dn7woJnK.js +0 -1
- package/public/assets/index-DnXT1F8T.js +0 -1
- package/public/assets/index-Kv-3BRTi.js +0 -1
- package/public/assets/index-g-G1RiME.js +0 -1
- package/public/assets/main-BsbMOeQg.js +0 -65
- package/public/assets/monaco-viewer-BFNhPQX7.js +0 -26
- package/public/assets/tool-call-BSJluZRW.js +0 -59
- package/public/assets/unified-picker-DmFyrIis.js +0 -1
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import pino from "pino";
|
|
4
|
+
import { EventBus } from "../events/bus";
|
|
5
|
+
import { WorkspaceWindowsTreeCleanupIncompleteError, } from "./runtime";
|
|
6
|
+
import { WorkspaceCleanupTimeoutError, WorkspaceLaunchCancelledError, WorkspaceLaunchTimeoutError, WorkspaceManager, WorkspaceShutdownError, } from "./manager";
|
|
7
|
+
function deferred() {
|
|
8
|
+
let resolve;
|
|
9
|
+
let reject;
|
|
10
|
+
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
11
|
+
resolve = resolvePromise;
|
|
12
|
+
reject = rejectPromise;
|
|
13
|
+
});
|
|
14
|
+
return { promise, resolve, reject };
|
|
15
|
+
}
|
|
16
|
+
class ControlledRuntime {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.launchResult = deferred();
|
|
19
|
+
this.launchCalled = deferred();
|
|
20
|
+
this.active = new Set();
|
|
21
|
+
this.stopCalls = 0;
|
|
22
|
+
this.failStops = 0;
|
|
23
|
+
this.launch = (options) => {
|
|
24
|
+
this.active.add(options.workspaceId);
|
|
25
|
+
this.onExit = options.onExit;
|
|
26
|
+
this.launchCalled.resolve(options.workspaceId);
|
|
27
|
+
options.signal?.addEventListener("abort", () => this.launchResult.reject(options.signal?.reason), { once: true });
|
|
28
|
+
return this.launchResult.promise;
|
|
29
|
+
};
|
|
30
|
+
this.stop = async (workspaceId) => {
|
|
31
|
+
this.stopCalls += 1;
|
|
32
|
+
if (this.failStops-- > 0)
|
|
33
|
+
throw new Error("controlled stop failure");
|
|
34
|
+
this.active.delete(workspaceId);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
resolveLaunch() {
|
|
38
|
+
this.launchResult.resolve({
|
|
39
|
+
pid: 1234,
|
|
40
|
+
port: 4321,
|
|
41
|
+
exitPromise: new Promise(() => undefined),
|
|
42
|
+
getLastOutput: () => "",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function createHarness(options = {}) {
|
|
47
|
+
const { stubReadiness = true, ...managerOptions } = options;
|
|
48
|
+
const eventBus = new EventBus();
|
|
49
|
+
const runtime = new ControlledRuntime();
|
|
50
|
+
const readiness = deferred();
|
|
51
|
+
const started = [];
|
|
52
|
+
const stopped = [];
|
|
53
|
+
eventBus.on("workspace.started", (event) => started.push(event.workspace.id));
|
|
54
|
+
eventBus.on("workspace.stopped", (event) => stopped.push(event.workspaceId));
|
|
55
|
+
const manager = new WorkspaceManager({
|
|
56
|
+
rootDir: process.cwd(),
|
|
57
|
+
settings: { getOwner: () => ({}) },
|
|
58
|
+
binaryResolver: { resolve: () => ({ path: "test-opencode", label: "test-opencode" }) },
|
|
59
|
+
eventBus,
|
|
60
|
+
logger: pino({ level: "silent" }),
|
|
61
|
+
getServerBaseUrl: () => "http://127.0.0.1:4000",
|
|
62
|
+
runtime,
|
|
63
|
+
...managerOptions,
|
|
64
|
+
});
|
|
65
|
+
if (stubReadiness) {
|
|
66
|
+
;
|
|
67
|
+
manager.waitForWorkspaceReadiness = ({ signal }) => Promise.race([
|
|
68
|
+
readiness.promise,
|
|
69
|
+
new Promise((_resolve, reject) => {
|
|
70
|
+
const cancel = () => reject(signal?.reason);
|
|
71
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
72
|
+
if (signal?.aborted)
|
|
73
|
+
cancel();
|
|
74
|
+
}),
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
return { manager, runtime, readiness, started, stopped };
|
|
78
|
+
}
|
|
79
|
+
async function createReady(harness) {
|
|
80
|
+
const creation = harness.manager.create(process.cwd());
|
|
81
|
+
const workspaceId = await harness.runtime.launchCalled.promise;
|
|
82
|
+
harness.runtime.resolveLaunch();
|
|
83
|
+
harness.readiness.resolve(undefined);
|
|
84
|
+
await creation;
|
|
85
|
+
return workspaceId;
|
|
86
|
+
}
|
|
87
|
+
describe("workspace manager lifecycle", () => {
|
|
88
|
+
it("rejects a healthy workspace whose OpenCode configuration is invalid", async () => {
|
|
89
|
+
const originalFetch = globalThis.fetch;
|
|
90
|
+
const requests = [];
|
|
91
|
+
const configError = JSON.stringify({
|
|
92
|
+
name: "ConfigInvalidError",
|
|
93
|
+
data: {
|
|
94
|
+
path: "C:\\Users\\dev\\.config\\opencode\\agents\\invalid.md",
|
|
95
|
+
issues: [{ path: ["tools", "bash"], message: 'Expected boolean, got "ask"' }],
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
globalThis.fetch = (async (input) => {
|
|
99
|
+
const url = String(input);
|
|
100
|
+
requests.push(url);
|
|
101
|
+
if (url.includes("/global/health")) {
|
|
102
|
+
return new Response(JSON.stringify({ healthy: true, version: "1.18.5" }), {
|
|
103
|
+
headers: { "Content-Type": "application/json" },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return new Response(configError, { status: 400, headers: { "Content-Type": "application/json" } });
|
|
107
|
+
});
|
|
108
|
+
try {
|
|
109
|
+
const harness = createHarness({ stubReadiness: false });
|
|
110
|
+
harness.manager.waitForPortAvailability = async () => undefined;
|
|
111
|
+
const creation = harness.manager.create(process.cwd());
|
|
112
|
+
const workspaceId = await harness.runtime.launchCalled.promise;
|
|
113
|
+
harness.runtime.resolveLaunch();
|
|
114
|
+
await assert.rejects(creation, (error) => {
|
|
115
|
+
assert.ok(error instanceof Error);
|
|
116
|
+
assert.equal(error.message, configError);
|
|
117
|
+
return true;
|
|
118
|
+
});
|
|
119
|
+
assert.deepEqual(requests.map((url) => new URL(url).pathname), ["/global/health", "/config"]);
|
|
120
|
+
assert.equal(new URL(requests[1]).search, "");
|
|
121
|
+
assert.equal(harness.runtime.active.has(workspaceId), false);
|
|
122
|
+
assert.deepEqual(harness.started, []);
|
|
123
|
+
assert.deepEqual(harness.manager.list(), []);
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
globalThis.fetch = originalFetch;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
for (const boundary of ["launch", "readiness", "shutdown"]) {
|
|
130
|
+
it(`cancels and cleans a workspace during ${boundary}`, async () => {
|
|
131
|
+
const harness = createHarness();
|
|
132
|
+
const creation = harness.manager.create(process.cwd());
|
|
133
|
+
const workspaceId = await harness.runtime.launchCalled.promise;
|
|
134
|
+
let cleanup;
|
|
135
|
+
if (boundary === "readiness") {
|
|
136
|
+
harness.runtime.resolveLaunch();
|
|
137
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
138
|
+
cleanup = harness.manager.delete(workspaceId);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
cleanup = boundary === "shutdown" ? harness.manager.shutdown() : harness.manager.delete(workspaceId);
|
|
142
|
+
harness.runtime.resolveLaunch();
|
|
143
|
+
}
|
|
144
|
+
await assert.rejects(creation, WorkspaceLaunchCancelledError);
|
|
145
|
+
await cleanup;
|
|
146
|
+
assert.deepEqual([harness.runtime.active.size, harness.started, harness.manager.list(), harness.stopped], [0, [], [], boundary === "readiness" ? [workspaceId] : []]);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
it("shares failed cleanup and allows a later delete retry", async () => {
|
|
150
|
+
const harness = createHarness();
|
|
151
|
+
const workspaceId = await createReady(harness);
|
|
152
|
+
harness.runtime.failStops = 2;
|
|
153
|
+
const first = harness.manager.delete(workspaceId);
|
|
154
|
+
const concurrent = harness.manager.delete(workspaceId);
|
|
155
|
+
assert.strictEqual(first, concurrent);
|
|
156
|
+
const failures = await Promise.allSettled([first, concurrent]);
|
|
157
|
+
assert.deepEqual(failures.map((result) => result.status), ["rejected", "rejected"]);
|
|
158
|
+
assert.equal(harness.runtime.active.has(workspaceId), true);
|
|
159
|
+
await harness.manager.delete(workspaceId);
|
|
160
|
+
assert.equal(harness.runtime.active.has(workspaceId), false);
|
|
161
|
+
assert.equal(harness.manager.get(workspaceId), undefined);
|
|
162
|
+
});
|
|
163
|
+
it("retries cancellation deletion for an already-cancelled request", async () => {
|
|
164
|
+
const harness = createHarness();
|
|
165
|
+
const creation = harness.manager.create(process.cwd(), undefined, { requestId: "retry-cancel" });
|
|
166
|
+
const workspaceId = await harness.runtime.launchCalled.promise;
|
|
167
|
+
harness.runtime.resolveLaunch();
|
|
168
|
+
harness.readiness.resolve(undefined);
|
|
169
|
+
await creation;
|
|
170
|
+
harness.runtime.failStops = 2;
|
|
171
|
+
await assert.rejects(harness.manager.cancelCreationRequest("retry-cancel"), /controlled stop failure/);
|
|
172
|
+
assert.equal(harness.manager.get(workspaceId)?.id, workspaceId);
|
|
173
|
+
assert.equal(harness.runtime.active.has(workspaceId), true);
|
|
174
|
+
await harness.manager.cancelCreationRequest("retry-cancel");
|
|
175
|
+
assert.equal(harness.manager.get(workspaceId), undefined);
|
|
176
|
+
assert.equal(harness.runtime.active.has(workspaceId), false);
|
|
177
|
+
assert.deepEqual(harness.stopped, [workspaceId]);
|
|
178
|
+
});
|
|
179
|
+
it("gives release and cancellation one terminal winner", async () => {
|
|
180
|
+
const cancelled = createHarness();
|
|
181
|
+
const cancelledCreation = cancelled.manager.create(process.cwd(), undefined, { requestId: "cancel-wins" });
|
|
182
|
+
const cancelledId = await cancelled.runtime.launchCalled.promise;
|
|
183
|
+
cancelled.runtime.resolveLaunch();
|
|
184
|
+
cancelled.readiness.resolve(undefined);
|
|
185
|
+
await cancelledCreation;
|
|
186
|
+
const stopStarted = deferred();
|
|
187
|
+
const finishStop = deferred();
|
|
188
|
+
const originalStop = cancelled.runtime.stop;
|
|
189
|
+
cancelled.runtime.stop = async (workspaceId) => {
|
|
190
|
+
stopStarted.resolve();
|
|
191
|
+
await finishStop.promise;
|
|
192
|
+
await originalStop(workspaceId);
|
|
193
|
+
};
|
|
194
|
+
const cancellation = cancelled.manager.cancelCreationRequest("cancel-wins");
|
|
195
|
+
await stopStarted.promise;
|
|
196
|
+
assert.equal(cancelled.manager.releaseCreationRequest(cancelledId, "cancel-wins"), false);
|
|
197
|
+
assert.equal(cancelled.manager.get(cancelledId)?.id, cancelledId);
|
|
198
|
+
finishStop.resolve();
|
|
199
|
+
await cancellation;
|
|
200
|
+
assert.equal(cancelled.manager.get(cancelledId), undefined);
|
|
201
|
+
const released = createHarness();
|
|
202
|
+
const releasedCreation = released.manager.create(process.cwd(), undefined, { requestId: "release-wins" });
|
|
203
|
+
const releasedId = await released.runtime.launchCalled.promise;
|
|
204
|
+
released.runtime.resolveLaunch();
|
|
205
|
+
released.readiness.resolve(undefined);
|
|
206
|
+
await releasedCreation;
|
|
207
|
+
assert.equal(released.manager.releaseCreationRequest(releasedId, "release-wins"), true);
|
|
208
|
+
await released.manager.cancelCreationRequest("release-wins");
|
|
209
|
+
assert.equal(released.manager.releaseCreationRequest(releasedId, "release-wins"), true);
|
|
210
|
+
assert.equal(released.manager.get(releasedId)?.id, releasedId);
|
|
211
|
+
assert.equal(released.runtime.active.has(releasedId), true);
|
|
212
|
+
});
|
|
213
|
+
it("retains unresolved pre-creation cancellation until its delayed create", async () => {
|
|
214
|
+
const harness = createHarness();
|
|
215
|
+
const requestIds = Array.from({ length: 1_025 }, (_, index) => `pending-cancel-${index}`);
|
|
216
|
+
await Promise.all(requestIds.map((requestId) => harness.manager.cancelCreationRequest(requestId)));
|
|
217
|
+
assert.equal(harness.manager.cancelledCreationRequests.size, requestIds.length);
|
|
218
|
+
await assert.rejects(harness.manager.create(process.cwd(), undefined, { requestId: requestIds[0] }), /was cancelled/);
|
|
219
|
+
assert.equal(harness.manager.cancelledCreationRequests.has(requestIds[0]), false);
|
|
220
|
+
assert.equal(harness.manager.cancelledCreationRequests.size, requestIds.length - 1);
|
|
221
|
+
});
|
|
222
|
+
it("returns scoped correlation while an ordinary shared launch remains retained", async () => {
|
|
223
|
+
const harness = createHarness();
|
|
224
|
+
const ordinary = harness.manager.create(process.cwd());
|
|
225
|
+
const workspaceId = await harness.runtime.launchCalled.promise;
|
|
226
|
+
const scoped = harness.manager.create(process.cwd(), undefined, { requestId: "restore-shared" });
|
|
227
|
+
harness.runtime.resolveLaunch();
|
|
228
|
+
harness.readiness.resolve(undefined);
|
|
229
|
+
const [ordinaryResult, scopedResult] = await Promise.all([ordinary, scoped]);
|
|
230
|
+
assert.equal(ordinaryResult.created, true);
|
|
231
|
+
assert.equal(ordinaryResult.workspace.requestId, undefined);
|
|
232
|
+
assert.equal(scopedResult.created, false);
|
|
233
|
+
assert.equal(scopedResult.workspace.id, workspaceId);
|
|
234
|
+
assert.equal(scopedResult.workspace.requestId, "restore-shared");
|
|
235
|
+
assert.equal(harness.manager.releaseCreationRequest(workspaceId, "restore-shared"), true);
|
|
236
|
+
assert.equal(harness.manager.get(workspaceId)?.id, workspaceId);
|
|
237
|
+
assert.equal(harness.runtime.active.has(workspaceId), true);
|
|
238
|
+
const reused = await harness.manager.create(process.cwd(), undefined, { requestId: "restore-reused" });
|
|
239
|
+
assert.equal(reused.workspace.requestId, "restore-reused");
|
|
240
|
+
await harness.manager.cancelCreationRequest("restore-reused");
|
|
241
|
+
assert.equal(harness.manager.get(workspaceId)?.id, workspaceId);
|
|
242
|
+
assert.equal(harness.runtime.active.has(workspaceId), true);
|
|
243
|
+
await assert.rejects(harness.manager.create(process.cwd(), undefined, { requestId: "restore-reused" }), /was cancelled/);
|
|
244
|
+
assert.equal(harness.manager.releaseCreationRequest(workspaceId, "restore-reused"), false);
|
|
245
|
+
});
|
|
246
|
+
for (const boundary of ["runtime launch", "health readiness"]) {
|
|
247
|
+
it(`applies one shared end-to-end deadline during ${boundary} and cleans up`, async () => {
|
|
248
|
+
const deadlines = [];
|
|
249
|
+
const harness = createHarness({
|
|
250
|
+
launchTimeoutMs: 25,
|
|
251
|
+
setTimeout: ((callback) => {
|
|
252
|
+
const timer = { active: true };
|
|
253
|
+
deadlines.push(() => { if (timer.active)
|
|
254
|
+
callback(); });
|
|
255
|
+
return timer;
|
|
256
|
+
}),
|
|
257
|
+
clearTimeout: ((timer) => { timer.active = false; }),
|
|
258
|
+
});
|
|
259
|
+
const first = harness.manager.create(process.cwd(), undefined, { requestId: "deadline-one" });
|
|
260
|
+
const workspaceId = await harness.runtime.launchCalled.promise;
|
|
261
|
+
const shared = harness.manager.create(process.cwd(), undefined, { requestId: "deadline-two" });
|
|
262
|
+
while ([...harness.manager.pendingWorkspaceCreations.values()][0]?.ownership.size !== 2) {
|
|
263
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
264
|
+
}
|
|
265
|
+
if (boundary === "health readiness") {
|
|
266
|
+
harness.runtime.resolveLaunch();
|
|
267
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
268
|
+
}
|
|
269
|
+
for (const fire of deadlines)
|
|
270
|
+
fire();
|
|
271
|
+
const outcomes = await Promise.allSettled([first, shared]);
|
|
272
|
+
assert.deepEqual(outcomes.map((outcome) => outcome.status), ["rejected", "rejected"]);
|
|
273
|
+
assert.ok(outcomes.every((outcome) => outcome.status === "rejected" && outcome.reason instanceof WorkspaceLaunchTimeoutError));
|
|
274
|
+
assert.strictEqual(outcomes[0].reason, outcomes[1].reason);
|
|
275
|
+
assert.equal(harness.runtime.active.has(workspaceId), false);
|
|
276
|
+
assert.equal(harness.runtime.stopCalls >= 1, true);
|
|
277
|
+
assert.deepEqual(harness.manager.list(), []);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
it("bounds shutdown instead of waiting forever", async () => {
|
|
281
|
+
let fireDeadline;
|
|
282
|
+
let cleared = 0;
|
|
283
|
+
const harness = createHarness({
|
|
284
|
+
shutdownTimeoutMs: 25,
|
|
285
|
+
setTimeout: ((callback) => {
|
|
286
|
+
fireDeadline = callback;
|
|
287
|
+
return {};
|
|
288
|
+
}),
|
|
289
|
+
clearTimeout: () => { cleared += 1; },
|
|
290
|
+
});
|
|
291
|
+
const workspaceId = await createReady(harness);
|
|
292
|
+
cleared = 0;
|
|
293
|
+
harness.runtime.stop = () => new Promise(() => undefined);
|
|
294
|
+
const shutdown = harness.manager.shutdown();
|
|
295
|
+
fireDeadline();
|
|
296
|
+
await assert.rejects(shutdown, WorkspaceCleanupTimeoutError);
|
|
297
|
+
assert.equal(harness.manager.get(workspaceId)?.status, "ready");
|
|
298
|
+
assert.equal(cleared, 1);
|
|
299
|
+
});
|
|
300
|
+
it("publishes stopped exactly once for normal exit, readiness failure, and manager cleanup", async () => {
|
|
301
|
+
const normal = createHarness();
|
|
302
|
+
const normalId = await createReady(normal);
|
|
303
|
+
normal.runtime.onExit?.({ workspaceId: normalId, code: 0, signal: null, requested: false });
|
|
304
|
+
await normal.manager.delete(normalId);
|
|
305
|
+
assert.deepEqual(normal.stopped, [normalId]);
|
|
306
|
+
const failed = createHarness();
|
|
307
|
+
const failedCreation = failed.manager.create(process.cwd());
|
|
308
|
+
const failedId = await failed.runtime.launchCalled.promise;
|
|
309
|
+
failed.runtime.resolveLaunch();
|
|
310
|
+
failed.readiness.reject(new Error("not ready"));
|
|
311
|
+
await assert.rejects(failedCreation, /not ready/);
|
|
312
|
+
assert.deepEqual(failed.stopped, [failedId]);
|
|
313
|
+
const cleaned = createHarness();
|
|
314
|
+
const cleanedId = await createReady(cleaned);
|
|
315
|
+
await cleaned.manager.shutdown();
|
|
316
|
+
assert.deepEqual(cleaned.stopped, [cleanedId]);
|
|
317
|
+
});
|
|
318
|
+
for (const [name, failure] of [
|
|
319
|
+
["cleanup failures", new Error("stop failed")],
|
|
320
|
+
["incomplete Windows tree cleanup", new WorkspaceWindowsTreeCleanupIncompleteError("workspace", 4242, ["taskkill failed"])],
|
|
321
|
+
]) {
|
|
322
|
+
it(`aggregates ${name} during shutdown`, async () => {
|
|
323
|
+
const harness = createHarness();
|
|
324
|
+
const workspaceId = await createReady(harness);
|
|
325
|
+
harness.runtime.stop = async () => { throw failure; };
|
|
326
|
+
await assert.rejects(harness.manager.shutdown(), (error) => {
|
|
327
|
+
assert.ok(error instanceof WorkspaceShutdownError);
|
|
328
|
+
assert.strictEqual(error.errors[0], failure);
|
|
329
|
+
return true;
|
|
330
|
+
});
|
|
331
|
+
assert.equal(harness.manager.get(workspaceId)?.status, "ready");
|
|
332
|
+
assert.equal(harness.runtime.active.has(workspaceId), true);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import { once } from "node:events";
|
|
4
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
+
import { it } from "node:test";
|
|
6
|
+
import { LAUNCH_CLEANUP_TOKEN_ENV, probePosixProcesses, signalOwnedPosixProcessGroup, signalPosixProcesses, } from "./process-identity";
|
|
7
|
+
const darwinOnly = { skip: process.platform !== "darwin", timeout: 10_000 };
|
|
8
|
+
async function spawnDetachedGroup(cleanupToken) {
|
|
9
|
+
const leader = spawn(process.execPath, ["-e", `
|
|
10
|
+
const { spawn } = require("node:child_process")
|
|
11
|
+
spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" })
|
|
12
|
+
process.stdout.write("ready\\n")
|
|
13
|
+
setInterval(() => {}, 1000)
|
|
14
|
+
`], {
|
|
15
|
+
detached: true,
|
|
16
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
17
|
+
env: { ...process.env, ...(cleanupToken ? { [LAUNCH_CLEANUP_TOKEN_ENV]: cleanupToken } : {}) },
|
|
18
|
+
});
|
|
19
|
+
assert.ok(leader.pid);
|
|
20
|
+
await once(leader.stdout, "data");
|
|
21
|
+
return leader;
|
|
22
|
+
}
|
|
23
|
+
async function assertGroupGone(groupId) {
|
|
24
|
+
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
25
|
+
const remaining = probePosixProcesses(spawnSync, 1_000, "darwin", { groupId });
|
|
26
|
+
if (remaining.ok && remaining.processes.size === 0)
|
|
27
|
+
return;
|
|
28
|
+
await delay(50);
|
|
29
|
+
}
|
|
30
|
+
assert.fail("owned Darwin process group remained alive after signaling");
|
|
31
|
+
}
|
|
32
|
+
it("uses real Darwin ps identities to stop an owned detached process group", darwinOnly, async () => {
|
|
33
|
+
const leader = await spawnDetachedGroup();
|
|
34
|
+
try {
|
|
35
|
+
const snapshot = probePosixProcesses(spawnSync, 1_000, "darwin", {
|
|
36
|
+
pids: [leader.pid],
|
|
37
|
+
groupId: leader.pid,
|
|
38
|
+
});
|
|
39
|
+
assert.equal(snapshot.ok, true);
|
|
40
|
+
assert.equal(snapshot.ok && snapshot.processes.get(leader.pid)?.groupId, leader.pid);
|
|
41
|
+
assert.equal(snapshot.ok && snapshot.processes.size >= 2, true);
|
|
42
|
+
const signaled = signalOwnedPosixProcessGroup(spawnSync, leader.pid, "SIGTERM", 1_000);
|
|
43
|
+
assert.equal(signaled.ok && signaled.matched, true);
|
|
44
|
+
assert.equal(signaled.ok && signaled.signalSent, true);
|
|
45
|
+
await assertGroupGone(leader.pid);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
try {
|
|
49
|
+
process.kill(-leader.pid, "SIGKILL");
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// The successful path has already removed the process group.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
it("uses a retained real Darwin identity anchor after the group leader exits", darwinOnly, async () => {
|
|
57
|
+
const cleanupToken = "darwin-integration-cleanup-token";
|
|
58
|
+
const leader = await spawnDetachedGroup(cleanupToken);
|
|
59
|
+
try {
|
|
60
|
+
const snapshot = probePosixProcesses(spawnSync, 1_000, "darwin", { groupId: leader.pid });
|
|
61
|
+
assert.equal(snapshot.ok, true);
|
|
62
|
+
const leaderIdentity = snapshot.ok ? snapshot.processes.get(leader.pid) : undefined;
|
|
63
|
+
assert.ok(leaderIdentity);
|
|
64
|
+
assert.equal(snapshot.ok && snapshot.processes.size >= 2, true);
|
|
65
|
+
leader.kill("SIGTERM");
|
|
66
|
+
if (leader.exitCode === null)
|
|
67
|
+
await once(leader, "exit");
|
|
68
|
+
const signaled = signalPosixProcesses(spawnSync, {
|
|
69
|
+
leader: leaderIdentity,
|
|
70
|
+
groupId: leader.pid,
|
|
71
|
+
members: [],
|
|
72
|
+
signal: "SIGTERM",
|
|
73
|
+
allowLeaderlessGroup: true,
|
|
74
|
+
cleanupToken,
|
|
75
|
+
}, 1_000, "darwin");
|
|
76
|
+
assert.equal(signaled.ok && signaled.matched, true);
|
|
77
|
+
assert.equal(signaled.ok && signaled.signalSent, true);
|
|
78
|
+
await assertGroupGone(leader.pid);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
try {
|
|
82
|
+
process.kill(-leader.pid, "SIGKILL");
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// The successful path has already removed the process group.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|