@neuralnomads/codenomad-dev 0.18.0-dev-20260716-40cee071 → 0.18.0-dev-20260718-8f6aa44b
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/dist/auth/manager.js +1 -1
- package/dist/filesystem/__tests__/search-cache.test.js +5 -5
- package/dist/filesystem/search-cache.js +1 -1
- package/dist/index.js +25 -0
- package/dist/index.test.js +40 -10
- package/dist/opencode-update/service.js +150 -0
- package/dist/opencode-update/service.test.js +107 -0
- package/dist/server/__tests__/remote-proxy.test.js +64 -56
- package/dist/server/http-server.js +7 -1
- package/dist/server/remote-proxy.js +68 -98
- 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/workspaces.test.js +38 -0
- package/dist/server/tls.js +2 -2
- package/dist/settings/binaries.test.js +2 -7
- package/dist/shutdown.js +67 -59
- package/dist/shutdown.test.js +115 -78
- package/dist/ui/remote-ui.js +2 -2
- package/dist/usage/providers/api-key.js +3 -3
- package/dist/usage/providers/google.js +1 -1
- package/dist/usage/providers/special.js +3 -3
- package/dist/usage/service.js +1 -1
- package/dist/usage/service.test.js +1 -1
- package/dist/usage/shared.js +4 -4
- package/dist/usage/shared.test.js +1 -1
- package/dist/workspaces/__tests__/spawn.test.js +2 -2
- package/dist/workspaces/__tests__/workspace-identity.test.js +94 -286
- package/dist/workspaces/binary-path.test.js +10 -0
- package/dist/workspaces/instance-client.js +4 -3
- package/dist/workspaces/launch-cleanup.test.js +21 -25
- package/dist/workspaces/manager.js +300 -351
- package/dist/workspaces/manager.test.js +209 -237
- package/dist/workspaces/process-identity.darwin.test.js +88 -0
- package/dist/workspaces/process-identity.js +172 -149
- package/dist/workspaces/process-identity.test.js +86 -140
- package/dist/workspaces/runtime.js +261 -373
- package/dist/workspaces/runtime.test.js +222 -547
- package/package.json +1 -1
- package/public/assets/{FilesTab-BjztpSdj.js → FilesTab-B0-DlSCT.js} +2 -2
- package/public/assets/{GitChangesTab-B9XraSoR.js → GitChangesTab-CxjbNBVr.js} +2 -2
- package/public/assets/{SplitFilePanel-Cu_5-Z-E.js → SplitFilePanel-CLQb-6uX.js} +1 -1
- package/public/assets/{StatusTab-D_IE79r-.js → StatusTab-CBGAuhmp.js} +1 -1
- package/public/assets/{align-justify-BKS9gyRk.js → align-justify-s5mnjsP0.js} +1 -1
- package/public/assets/{bundle-full-CXGXXlzR.js → bundle-full-lwIMY5om.js} +1 -1
- package/public/assets/{diff-viewer-CSNOYoEB.js → diff-viewer-CDtmFe6e.js} +1 -1
- package/public/assets/index-6ABz_Izb.js +1 -0
- package/public/assets/index-BPdjM9ZI.css +1 -0
- package/public/assets/index-BQK8um1r.js +1 -0
- package/public/assets/index-BkUAI4ZY.js +1 -0
- package/public/assets/index-D8bkUvEd.js +2 -0
- package/public/assets/index-DY_sbCt9.js +1 -0
- package/public/assets/index-DhmUOdAI.js +1 -0
- package/public/assets/{index-sLe9hEfQ.js → index-DupnEKCQ.js} +1 -1
- package/public/assets/index-HhZzWWha.js +1 -0
- package/public/assets/{index-DVc4ls8c.js → index-lBAEWdAF.js} +1 -1
- package/public/assets/index-nVII4VJz.js +1 -0
- package/public/assets/index-p1Sble5V.js +1 -0
- package/public/assets/{loading-CDmt9OJU.js → loading-h8DvUqqc.js} +1 -1
- package/public/assets/main-BxUSjoWA.js +68 -0
- package/public/assets/{markdown-1ZLSxCFT.js → markdown-Djug_spc.js} +3 -3
- package/public/assets/{monaco-viewer-D6JgH76W.js → monaco-viewer-lkUcsK65.js} +2 -2
- package/public/assets/{tool-call-CyXb63LH.js → tool-call-C4YH896w.js} +3 -3
- package/public/assets/{unified-picker-DCCdSVwI.js → unified-picker-DwNZOVlY.js} +1 -1
- package/public/assets/{wrap-text-pNMdTIq3.js → wrap-text-BLVYxvcj.js} +1 -1
- package/public/index.html +4 -4
- package/public/loading.html +4 -4
- package/public/sw.js +1 -1
- package/dist/workspaces/launch-cleanup.js +0 -132
- package/public/assets/index-B3YjNCh4.js +0 -1
- package/public/assets/index-B3ZD2_SU.js +0 -1
- package/public/assets/index-Bh2ofaQo.js +0 -1
- package/public/assets/index-CM0c9xRZ.js +0 -1
- package/public/assets/index-CdDxouJl.js +0 -2
- package/public/assets/index-D06Y2cFq.js +0 -1
- package/public/assets/index-PmE51vPh.js +0 -1
- package/public/assets/index-Qj63IiSj.css +0 -1
- package/public/assets/index-VY_zwmsG.js +0 -1
- package/public/assets/index-qiiH7egV.js +0 -1
- package/public/assets/main-CTN6Wwz1.js +0 -68
|
@@ -19,6 +19,7 @@ import { registerBackgroundProcessRoutes } from "./routes/background-processes";
|
|
|
19
19
|
import { registerYoloRoutes } from "./routes/yolo";
|
|
20
20
|
import { registerWorktreeRoutes } from "./routes/worktrees";
|
|
21
21
|
import { registerSpeechRoutes } from "./routes/speech";
|
|
22
|
+
import { registerOpenCodeUpdateRoutes } from "./routes/opencode-update";
|
|
22
23
|
import { registerRemoteServerRoutes } from "./routes/remote-servers";
|
|
23
24
|
import { registerRemoteProxyRoutes } from "./routes/remote-proxy";
|
|
24
25
|
import { registerSideCarRoutes } from "./routes/sidecars";
|
|
@@ -29,6 +30,7 @@ import { AutoAcceptManager } from "../permissions/auto-accept-manager";
|
|
|
29
30
|
import { createOpencodePermissionReplier } from "../permissions/opencode-replier";
|
|
30
31
|
import { registerAuthRoutes } from "./routes/auth";
|
|
31
32
|
import { sendUnauthorized, wantsHtml } from "../auth/http-auth";
|
|
33
|
+
import { createOpenCodeUpdateService } from "../opencode-update/service";
|
|
32
34
|
export function createHttpServer(deps) {
|
|
33
35
|
// Fastify's type-level RawServer inference gets noisy when toggling HTTP vs HTTPS.
|
|
34
36
|
// We keep the runtime behavior correct and cast the instance to a generic FastifyInstance.
|
|
@@ -59,7 +61,7 @@ export function createHttpServer(deps) {
|
|
|
59
61
|
});
|
|
60
62
|
app.addHook("onResponse", (request, reply, done) => {
|
|
61
63
|
const meta = request.__logMeta;
|
|
62
|
-
const durationMs = meta ? Number((process.hrtime.bigint() - meta.start) / BigInt(
|
|
64
|
+
const durationMs = meta ? Number((process.hrtime.bigint() - meta.start) / BigInt(1_000_000)) : undefined;
|
|
63
65
|
const base = {
|
|
64
66
|
method: request.method,
|
|
65
67
|
url: request.url,
|
|
@@ -205,6 +207,10 @@ export function createHttpServer(deps) {
|
|
|
205
207
|
});
|
|
206
208
|
registerWorkspaceRoutes(app, { workspaceManager: deps.workspaceManager });
|
|
207
209
|
registerSettingsRoutes(app, { settings: deps.settings, logger: apiLogger });
|
|
210
|
+
registerOpenCodeUpdateRoutes(app, {
|
|
211
|
+
service: createOpenCodeUpdateService(deps.settings, deps.workspaceManager),
|
|
212
|
+
logger: apiLogger,
|
|
213
|
+
});
|
|
208
214
|
registerFilesystemRoutes(app, { fileSystemBrowser: deps.fileSystemBrowser });
|
|
209
215
|
registerConfigFileRoutes(app);
|
|
210
216
|
registerMetaRoutes(app, { serverMeta: deps.serverMeta });
|
|
@@ -6,36 +6,23 @@ import { Agent, fetch } from "undici";
|
|
|
6
6
|
const LOOPBACK_HOST = "127.0.0.1";
|
|
7
7
|
const BOOTSTRAP_PAGE_PATH = "/__codenomad/auth/token";
|
|
8
8
|
const BOOTSTRAP_EXCHANGE_PATH = "/__codenomad/api/auth/token";
|
|
9
|
-
const SESSION_IDLE_TTL_MS = 30 *
|
|
10
|
-
const SESSION_DISPOSAL_TIMEOUT_MS =
|
|
9
|
+
const SESSION_IDLE_TTL_MS = 30 * 60_000;
|
|
10
|
+
const SESSION_DISPOSAL_TIMEOUT_MS = 5_000;
|
|
11
11
|
export class RemoteProxySessionManager {
|
|
12
12
|
constructor(options) {
|
|
13
13
|
this.options = options;
|
|
14
14
|
this.sessions = new Map();
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
15
|
+
this.creations = new Set();
|
|
16
|
+
this.disposals = new Set();
|
|
17
|
+
this.sessionDisposals = new Map();
|
|
18
18
|
this.shuttingDown = false;
|
|
19
|
-
this.
|
|
20
|
-
this.cleanupTimer = setInterval(() => {
|
|
21
|
-
void this.cleanupExpiredSessions().catch((error) => {
|
|
22
|
-
this.options.logger.error({ err: error }, "Failed to dispose expired remote proxy session");
|
|
23
|
-
});
|
|
24
|
-
}, 60000);
|
|
19
|
+
this.cleanupTimer = setInterval(() => void this.cleanupExpiredSessions().catch((error) => this.options.logger.error({ err: error }, "Failed to dispose expired remote proxy session")), 60_000);
|
|
25
20
|
this.cleanupTimer.unref();
|
|
26
21
|
}
|
|
27
22
|
async createSession(baseUrl, skipTlsVerify) {
|
|
28
|
-
if (this.shuttingDown)
|
|
23
|
+
if (this.shuttingDown)
|
|
29
24
|
throw new Error("Remote proxy session manager is shutting down");
|
|
30
|
-
|
|
31
|
-
const creation = this.createSessionInternal(baseUrl, skipTlsVerify);
|
|
32
|
-
this.pendingCreations.add(creation);
|
|
33
|
-
try {
|
|
34
|
-
return await creation;
|
|
35
|
-
}
|
|
36
|
-
finally {
|
|
37
|
-
this.pendingCreations.delete(creation);
|
|
38
|
-
}
|
|
25
|
+
return this.track(this.creations, this.createSessionInternal(baseUrl, skipTlsVerify));
|
|
39
26
|
}
|
|
40
27
|
async createSessionInternal(baseUrl, skipTlsVerify) {
|
|
41
28
|
if (!this.options.httpsOptions) {
|
|
@@ -79,7 +66,7 @@ export class RemoteProxySessionManager {
|
|
|
79
66
|
session.lastAccessAt = Date.now();
|
|
80
67
|
reply.send({ ok: true });
|
|
81
68
|
});
|
|
82
|
-
|
|
69
|
+
const handleProxyRequest = async (request, reply) => {
|
|
83
70
|
if (!session) {
|
|
84
71
|
reply.code(503).send({ error: "Remote proxy session is unavailable" });
|
|
85
72
|
return;
|
|
@@ -90,78 +77,64 @@ export class RemoteProxySessionManager {
|
|
|
90
77
|
}
|
|
91
78
|
session.lastAccessAt = Date.now();
|
|
92
79
|
await proxyRequest({ request, reply, session, logger: this.options.logger });
|
|
93
|
-
}
|
|
94
|
-
app.
|
|
95
|
-
|
|
96
|
-
reply.code(503).send({ error: "Remote proxy session is unavailable" });
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if (!session.activated) {
|
|
100
|
-
reply.code(403).send({ error: "Remote proxy session is not activated" });
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
session.lastAccessAt = Date.now();
|
|
104
|
-
await proxyRequest({ request, reply, session, logger: this.options.logger });
|
|
105
|
-
});
|
|
80
|
+
};
|
|
81
|
+
app.all("/*", handleProxyRequest);
|
|
82
|
+
app.setNotFoundHandler(handleProxyRequest);
|
|
106
83
|
const addressInfo = await app.listen({ host: LOOPBACK_HOST, port: 0 });
|
|
107
|
-
if (this.shuttingDown) {
|
|
108
|
-
abortController.abort();
|
|
109
|
-
try {
|
|
110
|
-
await this.disposeResources(app, dispatcher);
|
|
111
|
-
}
|
|
112
|
-
catch (error) {
|
|
113
|
-
this.disposalFailures.push(error);
|
|
114
|
-
throw error;
|
|
115
|
-
}
|
|
116
|
-
throw new Error("Remote proxy session manager is shutting down");
|
|
117
|
-
}
|
|
118
84
|
const address = new URL(addressInfo);
|
|
119
85
|
const localBaseUrl = new URL(`https://${LOOPBACK_HOST}:${address.port}`);
|
|
120
86
|
const entryUrl = new URL(targetBaseUrl.pathname || "/", localBaseUrl);
|
|
121
87
|
const returnTo = buildReturnToTarget(entryUrl);
|
|
88
|
+
const bootstrapUrl = `${localBaseUrl.origin}${BOOTSTRAP_PAGE_PATH}?returnTo=${encodeURIComponent(returnTo)}#${encodeURIComponent(bootstrapToken)}`;
|
|
122
89
|
session = {
|
|
123
90
|
id: sessionId,
|
|
124
91
|
bootstrapToken,
|
|
125
92
|
targetBaseUrl,
|
|
126
|
-
skipTlsVerify,
|
|
127
93
|
localBaseUrl,
|
|
128
|
-
entryUrl,
|
|
129
|
-
bootstrapUrl: `${localBaseUrl.origin}${BOOTSTRAP_PAGE_PATH}?returnTo=${encodeURIComponent(returnTo)}#${encodeURIComponent(bootstrapToken)}`,
|
|
130
94
|
activated: false,
|
|
131
95
|
cookiePrefix: `cnrp_${randomBytes(6).toString("hex")}_`,
|
|
132
96
|
app,
|
|
133
97
|
dispatcher,
|
|
134
98
|
abortController,
|
|
135
|
-
createdAt: Date.now(),
|
|
136
99
|
lastAccessAt: Date.now(),
|
|
137
100
|
};
|
|
138
101
|
this.sessions.set(sessionId, session);
|
|
102
|
+
if (this.shuttingDown) {
|
|
103
|
+
await this.disposeSession(sessionId);
|
|
104
|
+
throw new Error("Remote proxy session manager is shutting down");
|
|
105
|
+
}
|
|
139
106
|
this.options.logger.info({ sessionId, targetBaseUrl: targetBaseUrl.toString(), localBaseUrl: localBaseUrl.toString() }, "Created remote proxy session");
|
|
140
|
-
return { sessionId, windowUrl:
|
|
107
|
+
return { sessionId, windowUrl: bootstrapUrl };
|
|
141
108
|
}
|
|
142
109
|
async deleteSession(sessionId) {
|
|
143
110
|
return this.disposeSession(sessionId);
|
|
144
111
|
}
|
|
145
|
-
|
|
112
|
+
shutdown() {
|
|
113
|
+
if (this.shutdownPromise)
|
|
114
|
+
return this.shutdownPromise;
|
|
146
115
|
this.shuttingDown = true;
|
|
147
116
|
clearInterval(this.cleanupTimer);
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
117
|
+
const shutdown = this.drainShutdown();
|
|
118
|
+
this.shutdownPromise = shutdown;
|
|
119
|
+
void shutdown.finally(() => {
|
|
120
|
+
if (this.shutdownPromise === shutdown)
|
|
121
|
+
this.shutdownPromise = undefined;
|
|
122
|
+
}).catch(() => undefined);
|
|
123
|
+
return shutdown;
|
|
124
|
+
}
|
|
125
|
+
async drainShutdown() {
|
|
126
|
+
const disposals = new Set(this.disposals);
|
|
127
|
+
while (this.creations.size > 0) {
|
|
128
|
+
await Promise.allSettled([...this.creations]);
|
|
129
|
+
for (const disposal of this.disposals)
|
|
155
130
|
disposals.add(disposal);
|
|
156
|
-
await Promise.allSettled(pending);
|
|
157
131
|
}
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
throw createDisposalError("Remote proxy shutdown failed", failures);
|
|
132
|
+
const pendingResults = await Promise.allSettled(disposals);
|
|
133
|
+
const results = await Promise.allSettled(Array.from(this.sessions.keys(), (id) => this.disposeSession(id)));
|
|
134
|
+
const failures = [...pendingResults, ...results]
|
|
135
|
+
.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
136
|
+
if (failures.length)
|
|
137
|
+
throw new AggregateError(failures, "Remote proxy shutdown failed");
|
|
165
138
|
}
|
|
166
139
|
async cleanupExpiredSessions() {
|
|
167
140
|
const now = Date.now();
|
|
@@ -173,48 +146,45 @@ export class RemoteProxySessionManager {
|
|
|
173
146
|
}
|
|
174
147
|
}
|
|
175
148
|
disposeSession(sessionId) {
|
|
149
|
+
const pending = this.sessionDisposals.get(sessionId);
|
|
150
|
+
if (pending)
|
|
151
|
+
return pending;
|
|
176
152
|
const session = this.sessions.get(sessionId);
|
|
177
|
-
if (!session)
|
|
153
|
+
if (!session)
|
|
178
154
|
return Promise.resolve(false);
|
|
179
|
-
}
|
|
180
|
-
this.sessions.delete(sessionId);
|
|
181
155
|
session.abortController.abort();
|
|
182
|
-
const disposal = this.disposeResources(session.app, session.dispatcher)
|
|
183
|
-
.
|
|
156
|
+
const disposal = this.trackDisposal(this.disposeResources(session.app, session.dispatcher).then(() => {
|
|
157
|
+
if (this.sessions.get(sessionId) === session)
|
|
158
|
+
this.sessions.delete(sessionId);
|
|
184
159
|
this.options.logger.info({ sessionId }, "Disposed remote proxy session");
|
|
185
160
|
return true;
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
void disposal.then(() => this.pendingDisposals.delete(disposal), () => this.pendingDisposals.delete(disposal));
|
|
161
|
+
}));
|
|
162
|
+
this.sessionDisposals.set(sessionId, disposal);
|
|
163
|
+
void disposal.finally(() => {
|
|
164
|
+
if (this.sessionDisposals.get(sessionId) === disposal)
|
|
165
|
+
this.sessionDisposals.delete(sessionId);
|
|
166
|
+
}).catch(() => undefined);
|
|
193
167
|
return disposal;
|
|
194
168
|
}
|
|
195
169
|
async disposeResources(app, dispatcher) {
|
|
196
170
|
app.server.closeAllConnections?.();
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
171
|
+
const results = await Promise.race([
|
|
172
|
+
Promise.allSettled([app.close(), dispatcher?.destroy()]),
|
|
173
|
+
new Promise((_resolve, reject) => AbortSignal.timeout(Math.max(1, this.options.disposalTimeoutMs ?? SESSION_DISPOSAL_TIMEOUT_MS)).addEventListener("abort", () => reject(new Error("Remote proxy disposal timed out")))),
|
|
174
|
+
]);
|
|
175
|
+
const failures = results.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
176
|
+
if (failures.length)
|
|
177
|
+
throw new AggregateError(failures, "Remote proxy disposal failed");
|
|
178
|
+
}
|
|
179
|
+
track(operations, operation) {
|
|
180
|
+
operations.add(operation);
|
|
181
|
+
void operation.finally(() => operations.delete(operation)).catch(() => undefined);
|
|
182
|
+
return operation;
|
|
183
|
+
}
|
|
184
|
+
trackDisposal(operation) {
|
|
185
|
+
return this.track(this.disposals, operation);
|
|
213
186
|
}
|
|
214
187
|
}
|
|
215
|
-
function createDisposalError(message, failures) {
|
|
216
|
-
return Object.assign(new Error(message), { failures });
|
|
217
|
-
}
|
|
218
188
|
function normalizeBaseUrl(input) {
|
|
219
189
|
const parsed = new URL(input.trim());
|
|
220
190
|
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { OpenCodeUpdateError } from "../../opencode-update/service";
|
|
2
|
+
function statusCode(error) {
|
|
3
|
+
if (error.code === "no_ready_instance")
|
|
4
|
+
return 409;
|
|
5
|
+
if (error.code === "binary_unavailable")
|
|
6
|
+
return 422;
|
|
7
|
+
return 502;
|
|
8
|
+
}
|
|
9
|
+
function requestError(error, fallback) {
|
|
10
|
+
if (error instanceof OpenCodeUpdateError)
|
|
11
|
+
return { status: statusCode(error), code: error.code };
|
|
12
|
+
return { status: 500, code: fallback };
|
|
13
|
+
}
|
|
14
|
+
export function registerOpenCodeUpdateRoutes(app, deps) {
|
|
15
|
+
app.get("/api/opencode/update", async (_request, reply) => {
|
|
16
|
+
try {
|
|
17
|
+
return await deps.service.getStatus();
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
deps.logger.warn({ err: error }, "Failed to check OpenCode update status");
|
|
21
|
+
const failure = requestError(error, "update_check_failed");
|
|
22
|
+
reply.code(failure.status);
|
|
23
|
+
return { error: failure.code };
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
app.post("/api/opencode/update", async (_request, reply) => {
|
|
27
|
+
try {
|
|
28
|
+
return await deps.service.upgrade();
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
deps.logger.warn({ err: error }, "Failed to update OpenCode");
|
|
32
|
+
const failure = requestError(error, "upgrade_failed");
|
|
33
|
+
reply.code(failure.status);
|
|
34
|
+
return { success: false, error: failure.code };
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import Fastify from "fastify";
|
|
4
|
+
import { registerOpenCodeUpdateRoutes } from "./opencode-update";
|
|
5
|
+
test("does not pass request-controlled binary paths to the update service", async () => {
|
|
6
|
+
const calls = [];
|
|
7
|
+
const service = {
|
|
8
|
+
getStatus: async (...args) => {
|
|
9
|
+
calls.push({ method: "status", args });
|
|
10
|
+
return {
|
|
11
|
+
currentVersion: "1.0.0",
|
|
12
|
+
latestVersion: "1.1.0",
|
|
13
|
+
updateAvailable: true,
|
|
14
|
+
canUpgrade: true,
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
upgrade: async (...args) => {
|
|
18
|
+
calls.push({ method: "upgrade", args });
|
|
19
|
+
return { success: true, version: "1.1.0" };
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
const app = Fastify();
|
|
23
|
+
registerOpenCodeUpdateRoutes(app, {
|
|
24
|
+
service,
|
|
25
|
+
logger: { warn() { } },
|
|
26
|
+
});
|
|
27
|
+
await app.inject({ method: "GET", url: "/api/opencode/update?binary=untrusted.cmd" });
|
|
28
|
+
await app.inject({ method: "POST", url: "/api/opencode/update", payload: { binary: "untrusted.cmd" } });
|
|
29
|
+
assert.deepEqual(calls, [
|
|
30
|
+
{ method: "status", args: [] },
|
|
31
|
+
{ method: "upgrade", args: [] },
|
|
32
|
+
]);
|
|
33
|
+
await app.close();
|
|
34
|
+
});
|
|
@@ -4,7 +4,7 @@ const ProbeSchema = z.object({
|
|
|
4
4
|
baseUrl: z.string().min(1),
|
|
5
5
|
skipTlsVerify: z.boolean().optional(),
|
|
6
6
|
});
|
|
7
|
-
const PROBE_TIMEOUT_MS =
|
|
7
|
+
const PROBE_TIMEOUT_MS = 8_000;
|
|
8
8
|
export function registerRemoteServerRoutes(app, deps) {
|
|
9
9
|
app.post("/api/remote-servers/probe", async (request, reply) => {
|
|
10
10
|
try {
|
|
@@ -72,4 +72,42 @@ describe("workspace routes", () => {
|
|
|
72
72
|
assert.equal(calls.length, 2);
|
|
73
73
|
await app.close();
|
|
74
74
|
});
|
|
75
|
+
it("rejects release after cancellation wins while deletion is still pending", async () => {
|
|
76
|
+
const app = Fastify({ logger: false });
|
|
77
|
+
let state = "active";
|
|
78
|
+
let cancellationStarted;
|
|
79
|
+
let finishDeletion;
|
|
80
|
+
const started = new Promise((resolve) => { cancellationStarted = resolve; });
|
|
81
|
+
const deletion = new Promise((resolve) => { finishDeletion = resolve; });
|
|
82
|
+
const workspaceManager = {
|
|
83
|
+
cancelCreationRequest: async () => {
|
|
84
|
+
state = "cancelled";
|
|
85
|
+
cancellationStarted();
|
|
86
|
+
await deletion;
|
|
87
|
+
},
|
|
88
|
+
releaseCreationRequest: () => {
|
|
89
|
+
if (state === "cancelled")
|
|
90
|
+
return false;
|
|
91
|
+
state = "released";
|
|
92
|
+
return true;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
registerWorkspaceRoutes(app, { workspaceManager });
|
|
96
|
+
const cancellation = app.inject({
|
|
97
|
+
method: "POST",
|
|
98
|
+
url: "/api/workspaces/creation/cancel",
|
|
99
|
+
payload: { requestId: "restore-request" },
|
|
100
|
+
});
|
|
101
|
+
await started;
|
|
102
|
+
const release = await app.inject({
|
|
103
|
+
method: "POST",
|
|
104
|
+
url: "/api/workspaces/workspace/creation/release",
|
|
105
|
+
payload: { requestId: "restore-request" },
|
|
106
|
+
});
|
|
107
|
+
assert.equal(release.statusCode, 404);
|
|
108
|
+
assert.equal(release.body, "Workspace creation request not found");
|
|
109
|
+
finishDeletion();
|
|
110
|
+
assert.equal((await cancellation).statusCode, 204);
|
|
111
|
+
await app.close();
|
|
112
|
+
});
|
|
75
113
|
});
|
package/dist/server/tls.js
CHANGED
|
@@ -115,7 +115,7 @@ function generateCaCertificate() {
|
|
|
115
115
|
cert.publicKey = keys.publicKey;
|
|
116
116
|
cert.serialNumber = crypto.randomBytes(16).toString("hex");
|
|
117
117
|
const now = new Date();
|
|
118
|
-
const notBefore = new Date(now.getTime() -
|
|
118
|
+
const notBefore = new Date(now.getTime() - 60_000);
|
|
119
119
|
const notAfter = new Date(now.getTime() + CA_VALIDITY_DAYS * 24 * 60 * 60 * 1000);
|
|
120
120
|
cert.validity.notBefore = notBefore;
|
|
121
121
|
cert.validity.notAfter = notAfter;
|
|
@@ -142,7 +142,7 @@ function generateServerCertificate(args) {
|
|
|
142
142
|
cert.publicKey = keys.publicKey;
|
|
143
143
|
cert.serialNumber = crypto.randomBytes(16).toString("hex");
|
|
144
144
|
const now = new Date();
|
|
145
|
-
const notBefore = new Date(now.getTime() -
|
|
145
|
+
const notBefore = new Date(now.getTime() - 60_000);
|
|
146
146
|
const notAfter = new Date(now.getTime() + LEAF_VALIDITY_DAYS * 24 * 60 * 60 * 1000);
|
|
147
147
|
cert.validity.notBefore = notBefore;
|
|
148
148
|
cert.validity.notAfter = notAfter;
|
|
@@ -7,18 +7,13 @@ describe("BinaryResolver", () => {
|
|
|
7
7
|
getOwner(scope, owner) {
|
|
8
8
|
if (scope === "config" && owner === "server")
|
|
9
9
|
return { opencodeBinary: "default-opencode" };
|
|
10
|
-
if (scope === "state" && owner === "ui")
|
|
10
|
+
if (scope === "state" && owner === "ui")
|
|
11
11
|
return { opencodeBinaries: [{ path: "saved-opencode", label: "Saved", version: "1.2.3" }] };
|
|
12
|
-
}
|
|
13
12
|
return {};
|
|
14
13
|
},
|
|
15
14
|
};
|
|
16
15
|
const resolver = new BinaryResolver(settings);
|
|
17
|
-
assert.deepEqual(resolver.resolve("saved-opencode"), {
|
|
18
|
-
path: "saved-opencode",
|
|
19
|
-
label: "Saved",
|
|
20
|
-
version: "1.2.3",
|
|
21
|
-
});
|
|
16
|
+
assert.deepEqual(resolver.resolve("saved-opencode"), { path: "saved-opencode", label: "Saved", version: "1.2.3" });
|
|
22
17
|
assert.equal(resolver.resolveDefault().path, "default-opencode");
|
|
23
18
|
});
|
|
24
19
|
});
|
package/dist/shutdown.js
CHANGED
|
@@ -1,75 +1,83 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
super(`Server shutdown failed while stopping: ${failures.map((failure) => failure.resource).join(", ")}`);
|
|
4
|
-
this.name = "ServerShutdownError";
|
|
5
|
-
this.failures = failures;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
1
|
+
export const SERVER_SHUTDOWN_COMPLETE = "CODENOMAD_SHUTDOWN_STATUS:complete";
|
|
2
|
+
export const SERVER_SHUTDOWN_INCOMPLETE = "CODENOMAD_SHUTDOWN_STATUS:incomplete";
|
|
8
3
|
export function createServerShutdownHandler(options) {
|
|
9
|
-
const forceExit = options.forceExit ??
|
|
10
|
-
const setExitCode = options.setExitCode ?? ((code) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let shutdownPromise;
|
|
4
|
+
const forceExit = options.forceExit ?? process.exit;
|
|
5
|
+
const setExitCode = options.setExitCode ?? ((code) => { process.exitCode = code; });
|
|
6
|
+
const reportStatus = options.reportStatus ?? ((status) => console.log(status));
|
|
7
|
+
let pending;
|
|
14
8
|
return (signal) => {
|
|
15
|
-
if (
|
|
9
|
+
if (pending) {
|
|
16
10
|
options.logger.error({ signal }, "Additional shutdown signal received; forcing nonzero exit");
|
|
17
11
|
forceExit(1);
|
|
18
|
-
return
|
|
12
|
+
return pending;
|
|
19
13
|
}
|
|
20
14
|
options.logger.info({ signal }, "Received shutdown signal, stopping workspaces and server");
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
15
|
+
pending = Promise.resolve().then(options.shutdown).then(() => {
|
|
16
|
+
options.logger.info({}, "Shutdown complete");
|
|
17
|
+
reportStatus(SERVER_SHUTDOWN_COMPLETE);
|
|
18
|
+
setExitCode(0);
|
|
19
|
+
}, async (error) => {
|
|
20
|
+
options.logger.error({ err: error }, "Server shutdown incomplete; retrying cleanup");
|
|
21
|
+
const retryAttempts = Math.max(0, Math.floor(options.retryAttempts ?? 3));
|
|
22
|
+
for (let attempt = 1; attempt <= retryAttempts; attempt += 1) {
|
|
23
|
+
await new Promise((resolve) => setTimeout(resolve, options.retryDelayMs ?? 250));
|
|
24
|
+
try {
|
|
25
|
+
await options.shutdown();
|
|
26
|
+
options.logger.info({}, "Shutdown cleanup retry completed");
|
|
27
|
+
reportStatus(SERVER_SHUTDOWN_COMPLETE);
|
|
28
|
+
setExitCode(0);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
catch (retryError) {
|
|
32
|
+
options.logger.warn({ err: retryError, attempt, attempts: retryAttempts }, "Shutdown cleanup retry remains incomplete");
|
|
33
|
+
}
|
|
30
34
|
}
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
options.logger.error({ attempts: retryAttempts }, "Shutdown cleanup retries exhausted; preserving process-tree containment");
|
|
36
|
+
reportStatus(SERVER_SHUTDOWN_INCOMPLETE);
|
|
37
|
+
setExitCode(1);
|
|
38
|
+
if (options.holdAfterFailure)
|
|
39
|
+
await options.holdAfterFailure();
|
|
40
|
+
});
|
|
41
|
+
return pending;
|
|
33
42
|
};
|
|
34
43
|
}
|
|
35
44
|
export async function orchestrateServerShutdown(operations, logger, workspaceAttempts = 2) {
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
const errors = [];
|
|
46
|
+
const namedError = (name, cause) => Object.assign(new Error(`${name} failed: ${cause instanceof Error ? cause.message : String(cause)}`), { cause });
|
|
47
|
+
const settle = async (pending) => {
|
|
48
|
+
const results = await Promise.allSettled(pending.map(([, run]) => Promise.resolve().then(run)));
|
|
49
|
+
for (let index = 0; index < results.length; index += 1) {
|
|
50
|
+
const result = results[index];
|
|
51
|
+
if (result.status !== "rejected")
|
|
52
|
+
continue;
|
|
53
|
+
const error = namedError(pending[index][0], result.reason);
|
|
54
|
+
errors.push(error);
|
|
55
|
+
logger.error({ err: error }, "Server resource shutdown failed");
|
|
44
56
|
}
|
|
45
57
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
53
|
-
try {
|
|
54
|
-
await operations.stopWorkspaces();
|
|
55
|
-
workspaceFailure = undefined;
|
|
56
|
-
logger.info({ attempt }, "Workspace manager shutdown complete");
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
workspaceFailure = error;
|
|
58
|
+
const workspaceShutdown = (async () => {
|
|
59
|
+
const attempts = Math.max(1, Math.floor(workspaceAttempts));
|
|
60
|
+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
61
|
+
const [result] = await Promise.allSettled([Promise.resolve().then(operations.stopWorkspaces)]);
|
|
62
|
+
if (result.status === "fulfilled")
|
|
63
|
+
break;
|
|
61
64
|
if (attempt < attempts) {
|
|
62
|
-
logger.warn({ err:
|
|
65
|
+
logger.warn({ err: result.reason, attempt, attempts }, "Workspace manager shutdown failed; retrying cleanup");
|
|
66
|
+
continue;
|
|
63
67
|
}
|
|
68
|
+
const error = namedError("stopWorkspaces", result.reason);
|
|
69
|
+
errors.push(error);
|
|
70
|
+
logger.error({ err: error, attempts }, "Workspace manager shutdown failed");
|
|
64
71
|
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
})();
|
|
73
|
+
await Promise.all([
|
|
74
|
+
settle([
|
|
75
|
+
["stopInstanceEventBridge", operations.stopInstanceEventBridge], ["stopSidecars", operations.stopSidecars],
|
|
76
|
+
["stopClientConnections", operations.stopClientConnections], ["stopRemoteProxySessions", operations.stopRemoteProxySessions],
|
|
77
|
+
]),
|
|
78
|
+
workspaceShutdown,
|
|
79
|
+
]);
|
|
80
|
+
await settle([["stopHttpServers", operations.stopHttpServers], ["stopReleaseMonitor", operations.stopReleaseMonitor]]);
|
|
81
|
+
if (errors.length)
|
|
82
|
+
throw new AggregateError(errors, "Server shutdown failed");
|
|
75
83
|
}
|