@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.
Files changed (81) hide show
  1. package/README.md +12 -0
  2. package/THIRD_PARTY_NOTICES.md +26 -0
  3. package/dist/events/bus.js +13 -0
  4. package/dist/events/bus.test.js +37 -0
  5. package/dist/index.js +33 -56
  6. package/dist/index.test.js +32 -0
  7. package/dist/server/__tests__/remote-proxy.test.js +102 -6
  8. package/dist/server/http-server.js +2 -0
  9. package/dist/server/remote-proxy.js +96 -9
  10. package/dist/server/routes/usage.js +18 -0
  11. package/dist/server/routes/usage.test.js +26 -0
  12. package/dist/server/routes/workspaces.js +32 -1
  13. package/dist/server/routes/workspaces.test.js +75 -0
  14. package/dist/settings/binaries.js +4 -1
  15. package/dist/settings/binaries.test.js +24 -0
  16. package/dist/shutdown.js +75 -0
  17. package/dist/shutdown.test.js +111 -0
  18. package/dist/usage/providers/api-key.js +171 -0
  19. package/dist/usage/providers/google.js +171 -0
  20. package/dist/usage/providers/minimax.js +109 -0
  21. package/dist/usage/providers/oauth.js +111 -0
  22. package/dist/usage/providers/special.js +148 -0
  23. package/dist/usage/service.js +83 -0
  24. package/dist/usage/service.test.js +59 -0
  25. package/dist/usage/shared.js +130 -0
  26. package/dist/usage/shared.test.js +33 -0
  27. package/dist/usage/types.js +1 -0
  28. package/dist/workspaces/__tests__/spawn.test.js +64 -7
  29. package/dist/workspaces/__tests__/workspace-identity.test.js +177 -25
  30. package/dist/workspaces/launch-cleanup.js +132 -0
  31. package/dist/workspaces/launch-cleanup.test.js +33 -0
  32. package/dist/workspaces/manager.js +393 -135
  33. package/dist/workspaces/manager.test.js +318 -0
  34. package/dist/workspaces/process-identity.js +408 -0
  35. package/dist/workspaces/process-identity.test.js +165 -0
  36. package/dist/workspaces/runtime.js +567 -122
  37. package/dist/workspaces/runtime.test.js +603 -0
  38. package/dist/workspaces/spawn.js +86 -15
  39. package/package.json +1 -1
  40. package/public/assets/{FilesTab-EPvldvn1.js → FilesTab-BjztpSdj.js} +2 -2
  41. package/public/assets/{GitChangesTab-ofjovmv2.js → GitChangesTab-B9XraSoR.js} +2 -2
  42. package/public/assets/{SplitFilePanel-CyoPkcLd.js → SplitFilePanel-Cu_5-Z-E.js} +1 -1
  43. package/public/assets/StatusTab-D_IE79r-.js +1 -0
  44. package/public/assets/{align-justify-Bcf1-ZOl.js → align-justify-BKS9gyRk.js} +1 -1
  45. package/public/assets/{bundle-full-D5dzvToA.js → bundle-full-CXGXXlzR.js} +1 -1
  46. package/public/assets/{diff-viewer-DViXCI-4.js → diff-viewer-CSNOYoEB.js} +1 -1
  47. package/public/assets/{git-diff-vendor-CSgooKT_.js → git-diff-vendor-CYS3ApzA.js} +1 -1
  48. package/public/assets/index-B3YjNCh4.js +1 -0
  49. package/public/assets/index-B3ZD2_SU.js +1 -0
  50. package/public/assets/index-Bh2ofaQo.js +1 -0
  51. package/public/assets/index-CM0c9xRZ.js +1 -0
  52. package/public/assets/index-CdDxouJl.js +2 -0
  53. package/public/assets/index-D06Y2cFq.js +1 -0
  54. package/public/assets/{index-lf4nUOU1.js → index-DVc4ls8c.js} +1 -1
  55. package/public/assets/index-PmE51vPh.js +1 -0
  56. package/public/assets/{index-BMvFMsuy.css → index-Qj63IiSj.css} +1 -1
  57. package/public/assets/index-VY_zwmsG.js +1 -0
  58. package/public/assets/index-qiiH7egV.js +1 -0
  59. package/public/assets/{index-UbL8tz_V.js → index-sLe9hEfQ.js} +1 -1
  60. package/public/assets/{loading-CZ_tt3h0.js → loading-CDmt9OJU.js} +1 -1
  61. package/public/assets/main-CTN6Wwz1.js +68 -0
  62. package/public/assets/{markdown-G11aN6hz.js → markdown-1ZLSxCFT.js} +55 -44
  63. package/public/assets/monaco-viewer-D6JgH76W.js +26 -0
  64. package/public/assets/{tool-call-BsSXWErm.js → tool-call-CyXb63LH.js} +32 -32
  65. package/public/assets/{unified-picker-BRNI3UvE.js → unified-picker-DCCdSVwI.js} +1 -1
  66. package/public/assets/{wrap-text-B7up-uv7.js → wrap-text-pNMdTIq3.js} +1 -1
  67. package/public/index.html +5 -5
  68. package/public/loading.html +5 -5
  69. package/public/sw.js +1 -1
  70. package/public/assets/StatusTab-Bgu1Fh24.js +0 -1
  71. package/public/assets/index-3KsjbWGP.js +0 -1
  72. package/public/assets/index-B0WK8-cF.js +0 -1
  73. package/public/assets/index-BDv_iVtj.js +0 -1
  74. package/public/assets/index-BjxZvyb9.js +0 -2
  75. package/public/assets/index-BleKf9Nj.js +0 -1
  76. package/public/assets/index-CiIQ43NW.js +0 -1
  77. package/public/assets/index-DBquqL6Q.js +0 -1
  78. package/public/assets/index-yf9zolv4.js +0 -1
  79. package/public/assets/index-ypaL7i_s.js +0 -1
  80. package/public/assets/main-CAkg_fp_.js +0 -68
  81. package/public/assets/monaco-viewer-CZefBfct.js +0 -26
@@ -10,6 +10,37 @@ import { buildOpencodeConfigContent, getCodeNomadPluginUrl, resolveExistingOpenc
10
10
  import { OPENCODE_SERVER_BASE_URL_ENV, buildOpencodeBasicAuthHeader, OPENCODE_SERVER_PASSWORD_ENV, OPENCODE_SERVER_USERNAME_ENV, resolveOpencodeServerAuth, } from "./opencode-auth";
11
11
  import { resolveWorkspaceIdentity } from "./workspace-identity";
12
12
  const STARTUP_STABILITY_DELAY_MS = 1500;
13
+ export class WorkspaceLaunchCancelledError extends Error {
14
+ constructor(workspaceId) {
15
+ super(`Workspace ${workspaceId} launch was cancelled`);
16
+ this.name = "WorkspaceLaunchCancelledError";
17
+ }
18
+ }
19
+ export class WorkspaceShutdownTimeoutError extends Error {
20
+ constructor(timeoutMs) {
21
+ super(`Workspace shutdown did not finish within ${timeoutMs}ms; remaining workspace cleanup can be retried`);
22
+ this.code = "WORKSPACE_SHUTDOWN_TIMEOUT";
23
+ this.retryable = true;
24
+ this.name = "WorkspaceShutdownTimeoutError";
25
+ }
26
+ }
27
+ export class WorkspaceLaunchSettlementTimeoutError extends Error {
28
+ constructor(workspaceId, timeoutMs) {
29
+ super(`Workspace ${workspaceId} launch cancellation did not settle within ${timeoutMs}ms; cleanup can be retried`);
30
+ this.code = "WORKSPACE_LAUNCH_SETTLEMENT_TIMEOUT";
31
+ this.retryable = true;
32
+ this.name = "WorkspaceLaunchSettlementTimeoutError";
33
+ }
34
+ }
35
+ export class WorkspaceShutdownError extends Error {
36
+ constructor(errors) {
37
+ super(`Failed to stop ${errors.length} workspace${errors.length === 1 ? "" : "s"} during shutdown; cleanup can be retried`);
38
+ this.code = "WORKSPACE_SHUTDOWN_FAILED";
39
+ this.retryable = true;
40
+ this.name = "WorkspaceShutdownError";
41
+ this.errors = errors;
42
+ }
43
+ }
13
44
  export class WorkspaceManager {
14
45
  constructor(options) {
15
46
  this.options = options;
@@ -17,29 +48,43 @@ export class WorkspaceManager {
17
48
  this.workspaceIdentities = new Map();
18
49
  this.pendingWorkspaceCreations = new Map();
19
50
  this.pendingWorkspaceOwners = new Map();
20
- this.activeWorkspaceCreations = new Set();
21
- this.cancelledWorkspaceCreations = new Set();
51
+ this.cancelledCreationRequests = new Set();
22
52
  this.shuttingDown = false;
23
53
  this.opencodeAuth = new Map();
24
- this.runtime = new WorkspaceRuntime(this.options.eventBus, this.options.logger);
54
+ this.runtime = options.runtime ?? new WorkspaceRuntime(this.options.eventBus, this.options.logger);
25
55
  this.codeNomadPluginUrl = getCodeNomadPluginUrl();
56
+ this.shutdownTimeoutMs = Math.max(1, options.shutdownTimeoutMs ?? 10000);
57
+ this.launchSettlementTimeoutMs = Math.max(1, options.launchSettlementTimeoutMs ?? 5000);
58
+ this.scheduleTimeout = options.setTimeout ?? setTimeout;
59
+ this.cancelTimeout = options.clearTimeout ?? clearTimeout;
26
60
  }
27
61
  list() {
28
- return Array.from(this.workspaces.values());
62
+ return Array.from(this.workspaces.values())
63
+ .filter((record) => record.published)
64
+ .map((record) => record.descriptor);
29
65
  }
30
66
  get(id) {
31
- return this.workspaces.get(id);
67
+ const record = this.workspaces.get(id);
68
+ return record?.published ? record.descriptor : undefined;
32
69
  }
33
70
  getInstancePort(id) {
34
- return this.workspaces.get(id)?.port;
71
+ const record = this.workspaces.get(id);
72
+ return record?.published ? record.descriptor.port : undefined;
35
73
  }
36
74
  getInstanceAuthorizationHeader(id) {
37
- return this.opencodeAuth.get(id)?.authorization;
75
+ return this.workspaces.get(id)?.published ? this.opencodeAuth.get(id)?.authorization : undefined;
38
76
  }
39
- findReadyWorkspaceByIdentity(identityKey) {
40
- return Array.from(this.workspaces.values()).find((workspace) => {
41
- return workspace.status === "ready" && this.workspaceIdentities.get(workspace.id) === identityKey;
42
- });
77
+ findReadyWorkspaceByIdentity(identityKey, includeRestoreOwned) {
78
+ for (const [workspaceId, record] of this.workspaces) {
79
+ if (record.published
80
+ && !record.lifecycle.cancelled
81
+ && (includeRestoreOwned || !record.descriptor.requestId)
82
+ && record.descriptor.status === "ready"
83
+ && this.workspaceIdentities.get(workspaceId) === identityKey) {
84
+ return record.descriptor;
85
+ }
86
+ }
87
+ return undefined;
43
88
  }
44
89
  listFiles(workspaceId, relativePath = ".") {
45
90
  const workspace = this.requireWorkspace(workspaceId);
@@ -84,60 +129,77 @@ export class WorkspaceManager {
84
129
  const browser = new FileSystemBrowser({ rootDir: directory });
85
130
  browser.writeFile(relativePath, contents);
86
131
  }
87
- async create(folder, name, options) {
132
+ async create(folder, name, options = {}) {
88
133
  const { workspacePath, identityKey } = await resolveWorkspaceIdentity(folder, this.options.rootDir);
134
+ if (options.requestId && this.cancelledCreationRequests.delete(options.requestId)) {
135
+ throw new Error(`Workspace creation request ${options.requestId} was cancelled`);
136
+ }
89
137
  if (this.shuttingDown) {
90
138
  throw new Error("Workspace manager is shutting down");
91
139
  }
92
- if (options?.forceNew) {
93
- return this.trackWorkspaceCreation(this.createResolvedWorkspace(workspacePath, identityKey, name));
140
+ if (options.forceNew) {
141
+ const ownership = this.createOwnership(options.requestId);
142
+ const result = await this.createResolvedWorkspace(workspacePath, identityKey, name, options, ownership);
143
+ return this.resultForCreationOwner(result, options.requestId, ownership);
94
144
  }
95
- const existing = this.findReadyWorkspaceByIdentity(identityKey);
145
+ const existing = this.findReadyWorkspaceByIdentity(identityKey, Boolean(options.requestId));
96
146
  if (existing) {
97
147
  this.options.logger.info({ workspaceId: existing.id, folder: workspacePath }, "Reusing existing workspace");
148
+ const record = this.workspaces.get(existing.id);
149
+ if (options.requestId && record && record.creationOwnership.requestIds.size > 0) {
150
+ record.creationOwnership.requestIds.add(options.requestId);
151
+ this.syncCreationOwnership(record);
152
+ return this.resultForCreationOwner({ workspace: existing, created: false }, options.requestId, record.creationOwnership);
153
+ }
98
154
  return { workspace: existing, created: false };
99
155
  }
100
156
  const pending = this.pendingWorkspaceCreations.get(identityKey);
101
157
  if (pending) {
102
- const result = await pending;
103
- return { workspace: result.workspace, created: false };
158
+ pending.followerCount += 1;
159
+ if (options.requestId) {
160
+ pending.ownership.requestIds.add(options.requestId);
161
+ }
162
+ else {
163
+ const workspaceId = this.pendingWorkspaceOwners.get(identityKey);
164
+ if (workspaceId) {
165
+ this.retainCreationOwnership(this.workspaces.get(workspaceId));
166
+ }
167
+ else {
168
+ pending.ownership.sharedByNonRestoreCaller = true;
169
+ }
170
+ }
171
+ const result = await pending.promise;
172
+ return this.resultForCreationOwner({ workspace: result.workspace, created: false }, options.requestId, pending.ownership);
104
173
  }
105
- const creation = this.trackWorkspaceCreation(this.createResolvedWorkspace(workspacePath, identityKey, name, (workspaceId) => {
174
+ const ownership = this.createOwnership(options.requestId);
175
+ const creation = this.createResolvedWorkspace(workspacePath, identityKey, name, options, ownership, (workspaceId) => {
106
176
  this.pendingWorkspaceOwners.set(identityKey, workspaceId);
107
- }));
108
- this.pendingWorkspaceCreations.set(identityKey, creation);
177
+ });
178
+ const pendingCreation = { promise: creation, ownership, followerCount: 0 };
179
+ this.pendingWorkspaceCreations.set(identityKey, pendingCreation);
109
180
  try {
110
- return await creation;
181
+ const result = await creation;
182
+ return this.resultForCreationOwner(options.requestId && pendingCreation.ownership.sharedByNonRestoreCaller
183
+ ? { workspace: result.workspace, created: false }
184
+ : result, options.requestId, pendingCreation.ownership);
111
185
  }
112
186
  finally {
113
- if (this.pendingWorkspaceCreations.get(identityKey) === creation) {
187
+ if (this.pendingWorkspaceCreations.get(identityKey) === pendingCreation) {
114
188
  this.pendingWorkspaceCreations.delete(identityKey);
115
189
  this.pendingWorkspaceOwners.delete(identityKey);
116
190
  }
117
191
  }
118
192
  }
119
- trackWorkspaceCreation(creation) {
120
- let tracked;
121
- tracked = (async () => {
122
- try {
123
- return await creation;
124
- }
125
- finally {
126
- this.activeWorkspaceCreations.delete(tracked);
127
- }
128
- })();
129
- this.activeWorkspaceCreations.add(tracked);
130
- return tracked;
131
- }
132
- async createResolvedWorkspace(workspacePath, identityKey, name, onReserved) {
193
+ async createResolvedWorkspace(workspacePath, identityKey, name, options, creationOwnership, onReserved) {
133
194
  const id = randomUUID();
134
- const binary = this.options.binaryResolver.resolveDefault();
195
+ const binary = this.options.binaryResolver.resolve(options.binaryPath);
135
196
  const resolvedBinaryPath = this.resolveBinaryPath(binary.path);
136
197
  clearWorkspaceSearchCache(workspacePath);
137
198
  this.options.logger.info({ workspaceId: id, folder: workspacePath, binary: resolvedBinaryPath }, "Creating workspace");
138
199
  const proxyPath = `/workspaces/${id}/instance`;
139
200
  const descriptor = {
140
201
  id,
202
+ requestId: options.requestId,
141
203
  path: workspacePath,
142
204
  name,
143
205
  status: "starting",
@@ -148,11 +210,31 @@ export class WorkspaceManager {
148
210
  createdAt: new Date().toISOString(),
149
211
  updatedAt: new Date().toISOString(),
150
212
  };
151
- this.workspaces.set(id, descriptor);
213
+ let completeLaunch;
214
+ const completion = new Promise((resolve) => {
215
+ completeLaunch = resolve;
216
+ });
217
+ const record = {
218
+ descriptor,
219
+ lifecycle: {
220
+ cancelled: false,
221
+ settled: false,
222
+ completion,
223
+ complete: completeLaunch,
224
+ stoppedEventPublished: false,
225
+ },
226
+ published: false,
227
+ creationOwnership,
228
+ releasedCreationRequestIds: new Set(),
229
+ };
230
+ this.workspaces.set(id, record);
152
231
  this.workspaceIdentities.set(id, identityKey);
153
232
  onReserved?.(id);
233
+ if (options.requestId && this.cancelledCreationRequests.delete(options.requestId)) {
234
+ record.lifecycle.cancelled = true;
235
+ }
154
236
  try {
155
- this.options.eventBus.publish({ type: "workspace.created", workspace: descriptor });
237
+ this.throwIfCancelled(record);
156
238
  const serverConfig = this.options.settings.getOwner("config", "server");
157
239
  const envVars = serverConfig?.environmentVariables;
158
240
  const userEnvironment = envVars && typeof envVars === "object" && !Array.isArray(envVars) ? envVars : {};
@@ -180,8 +262,7 @@ export class WorkspaceManager {
180
262
  [OPENCODE_SERVER_PASSWORD_ENV]: opencodePassword,
181
263
  };
182
264
  const logLevel = serverConfig?.logLevel;
183
- this.throwIfWorkspaceCreationCancelled(id);
184
- const { pid, port, exitPromise, getLastOutput } = await this.runtime.launch({
265
+ const { pid, port, exitPromise, cancellationPromise, getLastOutput } = await this.runtime.launch({
185
266
  workspaceId: id,
186
267
  folder: workspacePath,
187
268
  binaryPath: resolvedBinaryPath,
@@ -191,12 +272,22 @@ export class WorkspaceManager {
191
272
  });
192
273
  descriptor.pid = pid;
193
274
  descriptor.port = port;
194
- this.throwIfWorkspaceCreationCancelled(id);
195
- const runtimeVersion = await this.waitForWorkspaceReadiness({ workspaceId: id, port, exitPromise, getLastOutput });
275
+ this.throwIfCancelled(record);
276
+ record.published = true;
277
+ this.options.eventBus.publish({ type: "workspace.created", workspace: descriptor });
278
+ this.throwIfCancelled(record);
279
+ const readinessAbort = new AbortController();
280
+ const runtimeVersion = await Promise.race([
281
+ this.waitForWorkspaceReadiness({ workspaceId: id, port, exitPromise, getLastOutput, signal: readinessAbort.signal }),
282
+ cancellationPromise.then((error) => {
283
+ readinessAbort.abort(error);
284
+ throw error;
285
+ }),
286
+ ]);
287
+ this.throwIfCancelled(record);
196
288
  if (runtimeVersion) {
197
289
  descriptor.binaryVersion = runtimeVersion;
198
290
  }
199
- this.throwIfWorkspaceCreationCancelled(id);
200
291
  descriptor.status = "ready";
201
292
  descriptor.updatedAt = new Date().toISOString();
202
293
  this.options.eventBus.publish({ type: "workspace.started", workspace: descriptor });
@@ -204,119 +295,256 @@ export class WorkspaceManager {
204
295
  return { workspace: descriptor, created: true };
205
296
  }
206
297
  catch (error) {
298
+ if (record.lifecycle.cancelled) {
299
+ await this.stopRuntime(id);
300
+ throw error instanceof WorkspaceLaunchCancelledError ? error : new WorkspaceLaunchCancelledError(id);
301
+ }
302
+ if (!record.published) {
303
+ try {
304
+ await this.stopRuntime(id);
305
+ }
306
+ catch (cleanupError) {
307
+ this.options.logger.error({ workspaceId: id, err: cleanupError }, "Unpublished workspace cleanup remains pending");
308
+ throw cleanupError;
309
+ }
310
+ if (this.workspaces.get(id) === record) {
311
+ this.workspaces.delete(id);
312
+ this.workspaceIdentities.delete(id);
313
+ }
314
+ this.opencodeAuth.delete(id);
315
+ this.options.logger.error({ workspaceId: id, err: error }, "Workspace failed before identity publication");
316
+ throw error;
317
+ }
207
318
  let stopFailure;
208
- await this.runtime.stop(id).catch((stopError) => {
319
+ await this.stopRuntime(id).catch((stopError) => {
209
320
  stopFailure = stopError;
210
321
  this.options.logger.warn({ workspaceId: id, err: stopError }, "Failed to stop workspace after startup error");
211
322
  });
212
- const cancelled = this.cancelledWorkspaceCreations.delete(id) || !this.workspaces.has(id);
213
- if (cancelled && !stopFailure) {
214
- throw new Error("Workspace creation cancelled");
323
+ if (!stopFailure) {
324
+ this.publishStopped(record);
325
+ if (this.workspaces.get(id) === record) {
326
+ this.workspaces.delete(id);
327
+ this.workspaceIdentities.delete(id);
328
+ }
329
+ this.opencodeAuth.delete(id);
330
+ throw error;
215
331
  }
216
332
  descriptor.status = "error";
217
333
  descriptor.error = stopFailure instanceof Error
218
334
  ? `Workspace startup failed and its process could not be stopped: ${stopFailure.message}`
219
335
  : error instanceof Error ? error.message : String(error);
220
336
  descriptor.updatedAt = new Date().toISOString();
221
- if (this.workspaces.has(id)) {
337
+ if (this.workspaces.get(id) === record && record.published) {
222
338
  this.options.eventBus.publish({ type: "workspace.error", workspace: descriptor });
223
339
  }
224
340
  this.options.logger.error({ workspaceId: id, err: error }, "Workspace failed to start");
225
- if (cancelled) {
226
- throw new Error(descriptor.error);
227
- }
228
341
  throw error;
229
342
  }
230
- }
231
- async delete(id) {
232
- const workspace = this.workspaces.get(id);
233
- if (!workspace)
234
- return undefined;
235
- this.options.logger.info({ workspaceId: id }, "Stopping workspace");
236
- const wasStarting = workspace.status === "starting";
237
- const wasRunning = Boolean(workspace.pid);
238
- if (wasStarting) {
239
- this.cancelledWorkspaceCreations.add(id);
240
- }
241
- if (wasStarting || wasRunning) {
242
- try {
243
- await this.runtime.stop(id);
244
- }
245
- catch (error) {
246
- this.options.logger.warn({ workspaceId: id, err: error }, "Failed to stop workspace process cleanly");
247
- workspace.status = "error";
248
- workspace.error = error instanceof Error ? error.message : String(error);
249
- workspace.updatedAt = new Date().toISOString();
250
- this.options.eventBus.publish({ type: "workspace.error", workspace });
251
- throw error;
252
- }
343
+ finally {
344
+ record.lifecycle.settled = true;
345
+ record.lifecycle.complete();
253
346
  }
347
+ }
348
+ delete(id) {
349
+ const record = this.workspaces.get(id);
350
+ if (!record)
351
+ return Promise.resolve(undefined);
352
+ record.lifecycle.cancelled = true;
254
353
  const identityKey = this.workspaceIdentities.get(id);
255
354
  if (identityKey && this.pendingWorkspaceOwners.get(identityKey) === id) {
256
355
  this.pendingWorkspaceCreations.delete(identityKey);
257
356
  this.pendingWorkspaceOwners.delete(identityKey);
258
357
  }
259
- const stoppedEventPublished = workspace.status === "stopped";
260
- this.workspaces.delete(id);
261
- this.workspaceIdentities.delete(id);
262
- this.opencodeAuth.delete(id);
263
- if (!wasStarting) {
264
- this.cancelledWorkspaceCreations.delete(id);
358
+ if (!record.lifecycle.deletePromise) {
359
+ let deletePromise;
360
+ deletePromise = this.cleanupDeletedWorkspace(id, record).catch((error) => {
361
+ if (record.lifecycle.deletePromise === deletePromise) {
362
+ record.lifecycle.deletePromise = undefined;
363
+ }
364
+ throw error;
365
+ });
366
+ record.lifecycle.deletePromise = deletePromise;
265
367
  }
266
- clearWorkspaceSearchCache(workspace.path);
267
- if (!stoppedEventPublished) {
268
- this.options.eventBus.publish({ type: "workspace.stopped", workspaceId: id });
368
+ return record.lifecycle.deletePromise;
369
+ }
370
+ releaseCreationRequest(id, requestId) {
371
+ const record = this.workspaces.get(id);
372
+ if (!record?.published)
373
+ return false;
374
+ if (record.releasedCreationRequestIds.has(requestId))
375
+ return true;
376
+ if (!record.creationOwnership.requestIds.has(requestId))
377
+ return false;
378
+ this.retainCreationOwnership(record);
379
+ return true;
380
+ }
381
+ async cancelCreationRequest(requestId) {
382
+ for (const [workspaceId, record] of this.workspaces) {
383
+ if (record.releasedCreationRequestIds.has(requestId))
384
+ return;
385
+ if (!record.creationOwnership.requestIds.delete(requestId))
386
+ continue;
387
+ record.creationOwnership.cancelledRequestIds.add(requestId);
388
+ record.releasedCreationRequestIds.add(requestId);
389
+ this.syncCreationOwnership(record);
390
+ if (record.creationOwnership.requestIds.size > 0 || record.creationOwnership.sharedByNonRestoreCaller) {
391
+ return;
392
+ }
393
+ await this.delete(workspaceId);
394
+ return;
395
+ }
396
+ if (this.cancelledCreationRequests.size >= 1024) {
397
+ const oldestRequestId = this.cancelledCreationRequests.values().next().value;
398
+ if (oldestRequestId)
399
+ this.cancelledCreationRequests.delete(oldestRequestId);
269
400
  }
270
- return workspace;
401
+ this.cancelledCreationRequests.add(requestId);
402
+ }
403
+ createOwnership(requestId) {
404
+ return {
405
+ requestIds: new Set(requestId ? [requestId] : []),
406
+ cancelledRequestIds: new Set(),
407
+ sharedByNonRestoreCaller: !requestId,
408
+ };
409
+ }
410
+ resultForCreationOwner(result, requestId, ownership) {
411
+ if (requestId && ownership.cancelledRequestIds.has(requestId)) {
412
+ throw new Error(`Workspace creation request ${requestId} was cancelled`);
413
+ }
414
+ return {
415
+ workspace: requestId && !ownership.sharedByNonRestoreCaller
416
+ ? { ...result.workspace, requestId }
417
+ : result.workspace,
418
+ created: result.created,
419
+ };
420
+ }
421
+ syncCreationOwnership(record) {
422
+ if (!record)
423
+ return;
424
+ record.descriptor.requestId = record.creationOwnership.sharedByNonRestoreCaller
425
+ ? undefined
426
+ : record.creationOwnership.requestIds.values().next().value;
427
+ }
428
+ retainCreationOwnership(record) {
429
+ if (!record)
430
+ return;
431
+ for (const requestId of record.creationOwnership.requestIds) {
432
+ record.releasedCreationRequestIds.add(requestId);
433
+ }
434
+ record.creationOwnership.requestIds.clear();
435
+ record.creationOwnership.sharedByNonRestoreCaller = true;
436
+ this.syncCreationOwnership(record);
271
437
  }
272
438
  async shutdown() {
273
439
  this.shuttingDown = true;
274
440
  this.options.logger.info("Shutting down all workspaces");
275
- const stopTasks = [];
276
- for (const [id, workspace] of this.workspaces) {
277
- if (workspace.status === "starting") {
278
- this.cancelledWorkspaceCreations.add(id);
279
- }
280
- if (!workspace.pid && workspace.status !== "starting") {
281
- this.options.logger.debug({ workspaceId: id }, "Workspace already stopped");
282
- continue;
283
- }
284
- this.options.logger.info({ workspaceId: id }, "Stopping workspace during shutdown");
285
- stopTasks.push(this.runtime.stop(id).catch((error) => {
286
- this.options.logger.error({ workspaceId: id, err: error }, "Failed to stop workspace during shutdown");
287
- throw error;
288
- }));
289
- }
441
+ const stopTasks = Array.from(this.workspaces.keys(), (id) => this.delete(id));
442
+ let stopFailures = [];
290
443
  if (stopTasks.length > 0) {
291
- const stopResults = await Promise.allSettled(stopTasks);
292
- const failedStop = stopResults.find((result) => result.status === "rejected");
293
- if (failedStop) {
294
- throw failedStop.reason;
444
+ const results = await this.withShutdownTimeout(Promise.allSettled(stopTasks));
445
+ stopFailures = results.flatMap((result) => (result.status === "rejected" ? [result.reason] : []));
446
+ }
447
+ if (this.workspaces.size === 0) {
448
+ this.workspaceIdentities.clear();
449
+ this.pendingWorkspaceCreations.clear();
450
+ this.pendingWorkspaceOwners.clear();
451
+ this.cancelledCreationRequests.clear();
452
+ this.options.logger.info("All workspaces cleared");
453
+ }
454
+ else {
455
+ this.options.logger.warn({ workspaceIds: Array.from(this.workspaces.keys()) }, "Some workspace records remain after failed shutdown cleanup");
456
+ if (stopFailures.length === 0) {
457
+ stopFailures.push(new Error(`Workspace cleanup remains incomplete for: ${Array.from(this.workspaces.keys()).join(", ")}`));
295
458
  }
296
459
  }
297
- if (this.activeWorkspaceCreations.size > 0) {
298
- await Promise.allSettled(this.activeWorkspaceCreations);
460
+ if (stopFailures.length > 0) {
461
+ throw new WorkspaceShutdownError(stopFailures);
462
+ }
463
+ }
464
+ async withShutdownTimeout(operation) {
465
+ let timeout = null;
466
+ const deadline = new Promise((_resolve, reject) => {
467
+ timeout = this.scheduleTimeout(() => {
468
+ timeout = null;
469
+ reject(new WorkspaceShutdownTimeoutError(this.shutdownTimeoutMs));
470
+ }, this.shutdownTimeoutMs);
471
+ });
472
+ try {
473
+ return await Promise.race([operation, deadline]);
474
+ }
475
+ finally {
476
+ if (timeout) {
477
+ this.cancelTimeout(timeout);
478
+ }
299
479
  }
300
- this.workspaces.clear();
301
- this.workspaceIdentities.clear();
302
- this.pendingWorkspaceCreations.clear();
303
- this.pendingWorkspaceOwners.clear();
304
- this.activeWorkspaceCreations.clear();
305
- this.cancelledWorkspaceCreations.clear();
306
- this.opencodeAuth.clear();
307
- this.options.logger.info("All workspaces cleared");
308
480
  }
309
481
  requireWorkspace(id) {
310
- const workspace = this.workspaces.get(id);
311
- if (!workspace) {
482
+ const record = this.workspaces.get(id);
483
+ if (!record?.published) {
312
484
  throw new Error("Workspace not found");
313
485
  }
314
- return workspace;
486
+ return record.descriptor;
487
+ }
488
+ throwIfCancelled(record) {
489
+ if (record.lifecycle.cancelled) {
490
+ throw new WorkspaceLaunchCancelledError(record.descriptor.id);
491
+ }
492
+ }
493
+ async stopRuntime(workspaceId) {
494
+ try {
495
+ await this.runtime.stop(workspaceId);
496
+ }
497
+ catch (error) {
498
+ this.options.logger.warn({ workspaceId, err: error }, "Failed to stop workspace process cleanly");
499
+ throw error;
500
+ }
315
501
  }
316
- throwIfWorkspaceCreationCancelled(id) {
317
- if (this.shuttingDown || this.cancelledWorkspaceCreations.has(id) || !this.workspaces.has(id)) {
318
- throw new Error("Workspace creation cancelled");
502
+ async cleanupDeletedWorkspace(id, record) {
503
+ this.options.logger.info({ workspaceId: id }, "Stopping workspace");
504
+ // Stop once immediately, then again after launch settlement to cover a child
505
+ // that became available while cancellation was propagating.
506
+ await this.stopRuntime(id);
507
+ if (!record.lifecycle.settled) {
508
+ await this.withLaunchSettlementTimeout(id, record.lifecycle.completion);
509
+ }
510
+ try {
511
+ await this.runtime.stop(id);
319
512
  }
513
+ catch (error) {
514
+ this.options.logger.warn({ workspaceId: id, err: error }, "Failed final workspace process cleanup");
515
+ throw error;
516
+ }
517
+ if (this.workspaces.get(id) === record) {
518
+ this.workspaces.delete(id);
519
+ this.workspaceIdentities.delete(id);
520
+ this.opencodeAuth.delete(id);
521
+ clearWorkspaceSearchCache(record.descriptor.path);
522
+ this.publishStopped(record);
523
+ }
524
+ return record.descriptor;
525
+ }
526
+ async withLaunchSettlementTimeout(workspaceId, completion) {
527
+ let timeout = null;
528
+ const deadline = new Promise((_resolve, reject) => {
529
+ timeout = this.scheduleTimeout(() => {
530
+ timeout = null;
531
+ reject(new WorkspaceLaunchSettlementTimeoutError(workspaceId, this.launchSettlementTimeoutMs));
532
+ }, this.launchSettlementTimeoutMs);
533
+ });
534
+ try {
535
+ await Promise.race([completion, deadline]);
536
+ }
537
+ finally {
538
+ if (timeout) {
539
+ this.cancelTimeout(timeout);
540
+ }
541
+ }
542
+ }
543
+ publishStopped(record) {
544
+ if (record.lifecycle.stoppedEventPublished)
545
+ return;
546
+ record.lifecycle.stoppedEventPublished = true;
547
+ this.options.eventBus.publish({ type: "workspace.stopped", workspaceId: record.descriptor.id });
320
548
  }
321
549
  resolveBinaryPath(identifier) {
322
550
  if (!identifier) {
@@ -365,14 +593,14 @@ export class WorkspaceManager {
365
593
  }
366
594
  async waitForWorkspaceReadiness(params) {
367
595
  await Promise.race([
368
- this.waitForPortAvailability(params.port),
596
+ this.waitForPortAvailability(params.port, 5000, params.signal),
369
597
  params.exitPromise.then((info) => {
370
598
  throw this.buildStartupError(params.workspaceId, "exited before becoming ready", info, params.getLastOutput());
371
599
  }),
372
600
  ]);
373
601
  const version = await this.waitForInstanceHealth(params);
374
602
  await Promise.race([
375
- this.delay(STARTUP_STABILITY_DELAY_MS),
603
+ this.delay(STARTUP_STABILITY_DELAY_MS, params.signal),
376
604
  params.exitPromise.then((info) => {
377
605
  throw this.buildStartupError(params.workspaceId, "exited shortly after start", info, params.getLastOutput());
378
606
  }),
@@ -381,7 +609,7 @@ export class WorkspaceManager {
381
609
  }
382
610
  async waitForInstanceHealth(params) {
383
611
  const probeResult = await Promise.race([
384
- this.probeInstance(params.workspaceId, params.port),
612
+ this.probeInstance(params.workspaceId, params.port, params.signal),
385
613
  params.exitPromise.then((info) => {
386
614
  throw this.buildStartupError(params.workspaceId, "exited during health checks", info, params.getLastOutput());
387
615
  }),
@@ -396,7 +624,7 @@ export class WorkspaceManager {
396
624
  const reason = probeResult.reason ?? "Health check failed";
397
625
  throw new Error(`Workspace ${params.workspaceId} failed health check: ${reason}.`);
398
626
  }
399
- async probeInstance(workspaceId, port) {
627
+ async probeInstance(workspaceId, port, signal) {
400
628
  const url = `http://127.0.0.1:${port}/global/health`;
401
629
  try {
402
630
  const headers = {};
@@ -404,7 +632,7 @@ export class WorkspaceManager {
404
632
  if (authHeader) {
405
633
  headers["Authorization"] = authHeader;
406
634
  }
407
- const response = await fetch(url, { headers });
635
+ const response = await fetch(url, { headers, signal });
408
636
  if (!response.ok) {
409
637
  const reason = `/global/health returned HTTP ${response.status}`;
410
638
  this.options.logger.debug({ workspaceId, status: response.status }, "Health probe returned server error");
@@ -432,29 +660,38 @@ export class WorkspaceManager {
432
660
  const outputDetails = trimmedOutput ? ` Last output: ${trimmedOutput}` : "";
433
661
  return new Error(`Workspace ${workspaceId} ${phase} (${exitDetails}).${outputDetails}`);
434
662
  }
435
- waitForPortAvailability(port, timeoutMs = 5000) {
663
+ waitForPortAvailability(port, timeoutMs = 5000, signal) {
436
664
  return new Promise((resolve, reject) => {
437
665
  const deadline = Date.now() + timeoutMs;
438
666
  let settled = false;
439
667
  let retryTimer = null;
668
+ let socket = null;
440
669
  const cleanup = () => {
441
670
  settled = true;
442
671
  if (retryTimer) {
443
672
  clearTimeout(retryTimer);
444
673
  retryTimer = null;
445
674
  }
675
+ signal?.removeEventListener("abort", onAbort);
676
+ };
677
+ const onAbort = () => {
678
+ if (settled)
679
+ return;
680
+ cleanup();
681
+ socket?.destroy();
682
+ reject(signal?.reason instanceof Error ? signal.reason : new Error("Workspace readiness was cancelled"));
446
683
  };
447
684
  const tryConnect = () => {
448
685
  if (settled) {
449
686
  return;
450
687
  }
451
- const socket = connect({ port, host: "127.0.0.1" }, () => {
688
+ socket = connect({ port, host: "127.0.0.1" }, () => {
452
689
  cleanup();
453
- socket.end();
690
+ socket?.end();
454
691
  resolve();
455
692
  });
456
693
  socket.once("error", () => {
457
- socket.destroy();
694
+ socket?.destroy();
458
695
  if (settled) {
459
696
  return;
460
697
  }
@@ -470,14 +707,34 @@ export class WorkspaceManager {
470
707
  }
471
708
  });
472
709
  };
710
+ if (signal?.aborted) {
711
+ onAbort();
712
+ return;
713
+ }
714
+ signal?.addEventListener("abort", onAbort, { once: true });
473
715
  tryConnect();
474
716
  });
475
717
  }
476
- delay(durationMs) {
718
+ delay(durationMs, signal) {
477
719
  if (durationMs <= 0) {
478
720
  return Promise.resolve();
479
721
  }
480
- return new Promise((resolve) => setTimeout(resolve, durationMs));
722
+ return new Promise((resolve, reject) => {
723
+ const timer = setTimeout(() => {
724
+ signal?.removeEventListener("abort", onAbort);
725
+ resolve();
726
+ }, durationMs);
727
+ const onAbort = () => {
728
+ clearTimeout(timer);
729
+ signal?.removeEventListener("abort", onAbort);
730
+ reject(signal?.reason instanceof Error ? signal.reason : new Error("Workspace readiness was cancelled"));
731
+ };
732
+ if (signal?.aborted) {
733
+ onAbort();
734
+ return;
735
+ }
736
+ signal?.addEventListener("abort", onAbort, { once: true });
737
+ });
481
738
  }
482
739
  describeExit(info) {
483
740
  if (info.signal) {
@@ -489,18 +746,19 @@ export class WorkspaceManager {
489
746
  return "unknown reason";
490
747
  }
491
748
  handleProcessExit(workspaceId, info) {
492
- const workspace = this.workspaces.get(workspaceId);
493
- if (!workspace)
749
+ const record = this.workspaces.get(workspaceId);
750
+ if (!record)
494
751
  return;
752
+ const workspace = record.descriptor;
495
753
  this.opencodeAuth.delete(workspaceId);
496
754
  this.options.logger.info({ workspaceId, ...info }, "Workspace process exited");
497
755
  workspace.pid = undefined;
498
756
  workspace.port = undefined;
499
757
  workspace.updatedAt = new Date().toISOString();
500
- if (info.requested || info.code === 0) {
758
+ if (record.lifecycle.cancelled || info.requested || info.code === 0) {
501
759
  workspace.status = "stopped";
502
760
  workspace.error = undefined;
503
- this.options.eventBus.publish({ type: "workspace.stopped", workspaceId });
761
+ this.publishStopped(record);
504
762
  }
505
763
  else {
506
764
  workspace.status = "error";