diff --git a/server/src/__tests__/workspace-runtime.test.ts b/server/src/__tests__/workspace-runtime.test.ts index 61c612bfdc..ff33318d5f 100644 --- a/server/src/__tests__/workspace-runtime.test.ts +++ b/server/src/__tests__/workspace-runtime.test.ts @@ -3294,6 +3294,58 @@ describe("ensureRuntimeServicesForRun", () => { expect(services).toEqual([]); }); + it("requires Paperclip dev runtime services to pass /api/health readiness", async () => { + const workspaceRoot = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-runtime-health-")); + const workspace = buildWorkspace(workspaceRoot); + const runId = "run-paperclip-health"; + const serviceCommand = + "node -e \"const http=require('node:http'); http.createServer((req,res)=>{ if (req.url==='/api/health') { res.statusCode=503; res.end('database_unreachable'); return; } res.end('ok'); }).listen(Number(process.env.PORT), '127.0.0.1')\""; + + try { + await expect( + ensureRuntimeServicesForRun({ + runId, + agent: { + id: "agent-1", + name: "Codex Coder", + companyId: "company-1", + }, + issue: null, + workspace, + config: { + workspaceRuntime: { + services: [ + { + name: "paperclip-dev", + command: serviceCommand, + cwd: ".", + port: { type: "auto" }, + readiness: { + type: "http", + urlTemplate: "http://127.0.0.1:{{port}}", + timeoutSec: 3, + intervalMs: 100, + }, + expose: { + type: "url", + urlTemplate: "http://127.0.0.1:{{port}}", + }, + lifecycle: "shared", + stopPolicy: { + type: "manual", + }, + }, + ], + }, + }, + adapterEnv: {}, + }), + ).rejects.toThrow(/Readiness check failed for http:\/\/127\.0\.0\.1:\d+\/api\/health: received HTTP 503/); + } finally { + await releaseRuntimeServicesForRun(runId); + } + }); + it("reuses shared runtime services across runs and starts a new service after release", async () => { const workspaceRoot = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-runtime-workspace-")); const workspace = buildWorkspace(workspaceRoot); diff --git a/server/src/services/workspace-runtime.ts b/server/src/services/workspace-runtime.ts index 3a062137be..d32ae03c6c 100644 --- a/server/src/services/workspace-runtime.ts +++ b/server/src/services/workspace-runtime.ts @@ -2867,18 +2867,27 @@ export function resolveWorkspaceRuntimeReadinessTimeoutSec(service: Record; + serviceName?: string | null; + command?: string | null; url: string | null; }) { const readiness = parseObject(input.service.readiness); const readinessType = asString(readiness.type, ""); if (readinessType !== "http" || !input.url) return; + const readinessUrl = resolveRuntimeServiceHealthUrl(input.url, { + serviceName: input.serviceName, + command: input.command, + }); + if (!readinessUrl) { + throw new Error(`Readiness check failed: could not resolve health URL for ${input.url}`); + } const timeoutSec = resolveWorkspaceRuntimeReadinessTimeoutSec(input.service); const intervalMs = Math.max(100, asNumber(readiness.intervalMs, 500)); const deadline = Date.now() + timeoutSec * 1000; let lastError = "service did not become ready"; while (Date.now() < deadline) { try { - const response = await fetch(input.url); + const response = await fetch(readinessUrl); if (response.ok) return; lastError = `received HTTP ${response.status}`; } catch (err) { @@ -2886,7 +2895,7 @@ async function waitForReadiness(input: { } await delay(intervalMs); } - throw new Error(`Readiness check failed for ${input.url}: ${lastError}`); + throw new Error(`Readiness check failed for ${readinessUrl}: ${lastError}`); } function isPaperclipDevRuntimeService(input: { serviceName?: string | null; command?: string | null }) { @@ -2924,7 +2933,7 @@ async function isRuntimeServiceUrlHealthy( ) { if (!url) return true; const healthUrl = resolveRuntimeServiceHealthUrl(url, input); - if (!healthUrl) return true; + if (!healthUrl) return false; try { const response = await fetch(healthUrl, { signal: AbortSignal.timeout(2_000) }); return response.ok; @@ -3294,7 +3303,7 @@ async function startLocalRuntimeService(input: { try { await Promise.race([ - waitForReadiness({ service: input.service, url }), + waitForReadiness({ service: input.service, serviceName, command, url }), spawnErrorPromise, ]); } catch (err) {