From 63b75455d849bf8e951cfeec41d2463f441cf155 Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 18 Sep 2026 01:05:54 +0000 Subject: [PATCH 1/2] Harden job queue recovery and add run kill --- packages/app/src/flow-service.ts | 1 + packages/app/src/protected-api.ts | 3 + packages/app/src/tenant.ts | 124 ++++++++++++++---- packages/app/src/ui.ts | 2 +- .../app/test/exe-run-lifecycle.e2e.test.ts | 51 ++++++- packages/docs/api/reference.mdx | 1 + packages/docs/mcp/tools.mdx | 1 + packages/docs/openapi.yaml | 1 + packages/docs/quickstart.mdx | 2 +- 9 files changed, 157 insertions(+), 29 deletions(-) diff --git a/packages/app/src/flow-service.ts b/packages/app/src/flow-service.ts index 9eee78a5..172f0d20 100644 --- a/packages/app/src/flow-service.ts +++ b/packages/app/src/flow-service.ts @@ -52,6 +52,7 @@ export class ApiService { getRun(runId: string) { return this.call("runs:read", `/v1/runs/${encodeURIComponent(runId)}`); } getRunDiagnostics(runId: string) { return this.call("runs:read", `/v1/runs/${encodeURIComponent(runId)}/diagnostics`); } stopRun(runId: string) { return this.call("runs:write", `/v1/runs/${encodeURIComponent(runId)}/stop`, { method: "POST" }); } + killRun(runId: string) { return this.call("runs:write", `/v1/runs/${encodeURIComponent(runId)}/kill`, { method: "POST" }); } async listJobs() { return (await this.call("flows:read", "/v1/jobs") as any[]).map(value => this.publicJob(value)); } async getJob(jobId: string) { return this.publicJob(await this.call("flows:read", `/v1/jobs/${encodeURIComponent(jobId)}`)); } listJobEvents(jobId: string, limit = 50) { return this.call("runs:read", `/v1/jobs/${encodeURIComponent(jobId)}/events?limit=${limit}`); } diff --git a/packages/app/src/protected-api.ts b/packages/app/src/protected-api.ts index 9a7973de..1483dde1 100644 --- a/packages/app/src/protected-api.ts +++ b/packages/app/src/protected-api.ts @@ -70,6 +70,8 @@ export async function protectedApiFetch(request: Request, env: Env, auth: OAuthP if (runDiagnosticMatch && request.method === "GET") return Response.json(await service.getRunDiagnostics(decodeURIComponent(runDiagnosticMatch[1]))); const stopMatch = path.match(/^\/api\/v1\/runs\/([^/]+)\/stop$/); if (stopMatch && request.method === "POST") return Response.json(await service.stopRun(decodeURIComponent(stopMatch[1]))); + const killMatch = path.match(/^\/api\/v1\/runs\/([^/]+)\/kill$/); + if (killMatch && request.method === "POST") return Response.json(await service.killRun(decodeURIComponent(killMatch[1]))); return Response.json({ error: { code: "not_found", message: "Not found" } }, { status: 404 }); } catch (error) { return errorResponse(error); } } @@ -88,6 +90,7 @@ function createMcpServer(service: ApiService): McpServer { tool("get_run", "Get a run with its rendered prompt, full triggered context, invocation metadata, execution metadata, output, and activity. For manual invocations, invocation.idempotency_key is the client-reusable value; invocation.claim_key is internal.", runIdSchema, ({ runId }: any) => service.getRun(runId)); tool("get_run_diagnostics", "Get credential-safe launch, prompt delivery, output capture, claim release, cleanup, and activity diagnostics for a run.", runIdSchema, ({ runId }: any) => service.getRunDiagnostics(runId)); tool("stop_run", "Stop an active run", runIdSchema, ({ runId }: any) => service.stopRun(runId)); + tool("kill_run", "Force a run in any non-terminal state to stop immediately, release its concurrency slot, and clean up its backend asynchronously.", runIdSchema, ({ runId }: any) => service.killRun(runId)); tool("list_jobs", "List jobs with their current run count and maximum concurrency", z.object({}), () => service.listJobs()); tool("get_job", "Get a job with its current run count and maximum concurrency", jobIdSchema, ({ jobId }: any) => service.getJob(jobId)); tool("create_job", "Create a job using an executionTargetId returned by list_execution_targets. Credentials are never accepted or returned.", jobInputSchema, (input: any) => service.createJob(input)); diff --git a/packages/app/src/tenant.ts b/packages/app/src/tenant.ts index 46414009..cb5e9749 100644 --- a/packages/app/src/tenant.ts +++ b/packages/app/src/tenant.ts @@ -10,7 +10,7 @@ import { githubClaimKey, githubCollection, githubHeaders, installationToken, nor import type { WorkItem } from "./types"; import { invokeCustomHandler } from "./custom-handler"; import { generateTailSecret, sanitizeTailEvent, signTailDelivery, suppressTailEvent, tailFingerprint, verifyTailDelivery } from "./cloudflare-tail"; -import { ExeVmBackend } from "./exe-vm-backend"; +import { ExeVmBackend, vmNameFor } from "./exe-vm-backend"; import { AmpBackend, type AmpConnection } from "./amp-backend"; import { DEFAULT_CONTEXT_TEMPLATE, LinearSourceAdapter, linearTicketPrompt, renderContextTemplate } from "./linear-source"; import { JOB_SCHEMA, renderJobPrompt, renderRunName } from "./job-domain"; @@ -31,6 +31,8 @@ const now = () => new Date().toISOString(); const id = () => crypto.randomUUID(); const FLOW_NAME = /^[a-z][a-z0-9_-]{0,29}$/; const JOB_SLUG = /^[a-z][a-z0-9_-]{0,29}$/; +const QUEUE_WATCHDOG_MS = 15_000; +const STARTING_LEASE_MS = 2 * 60_000; // Keep the index definitions next to the query they support. Every listRuns // variant can use the same descending keyset order without a table scan or a @@ -212,6 +214,7 @@ export class TenantV2 extends DurableObject { if (request.method === "GET" && /^\/v1\/runs\/[^/]+$/.test(url.pathname)) return await this.getRun(decodeURIComponent(url.pathname.split("/")[3] ?? "")); if (request.method === "GET" && /^\/v1\/runs\/[^/]+\/diagnostics$/.test(url.pathname)) return this.getRunDiagnostics(decodeURIComponent(url.pathname.split("/")[3] ?? "")); if (request.method === "POST" && /^\/v1\/runs\/[^/]+\/stop$/.test(url.pathname)) return await this.stopRun(decodeURIComponent(url.pathname.split("/")[3] ?? "")); + if (request.method === "POST" && /^\/v1\/runs\/[^/]+\/kill$/.test(url.pathname)) return await this.killRun(decodeURIComponent(url.pathname.split("/")[3] ?? "")); if (request.method === "GET" && url.pathname === "/v1/execution-targets") return await this.executionTargets(); const exeDiagnosticMatch = url.pathname.match(/^\/v1\/integrations\/exe\/([^/]+)\/diagnostics$/); if (request.method === "POST" && exeDiagnosticMatch) return await this.exeIntegrationDiagnostics(decodeURIComponent(exeDiagnosticMatch[1])); @@ -283,37 +286,70 @@ export class TenantV2 extends DurableObject { } catch (error) { console.error(JSON.stringify({ event: "factorize_unexpected_failure", component: "tenant", path: url.pathname, message: error instanceof Error ? error.message : "Unknown error", factorizeTailSuppressed: url.pathname.startsWith("/webhook/cloudflare/") })); return Response.json({ error: error instanceof Error ? error.message : "Unknown error" }, { status: 400 }); + } finally { + // Persisted work is authoritative. Any API traffic repairs a missing + // wake-up without doing launch work in the request itself. + if (url.pathname.startsWith("/v1/")) await this.ensureAlarm().catch(error => console.error(JSON.stringify({ event: "factorize_alarm_reconciliation_failed", message: error instanceof Error ? error.message : "Unknown error" }))); } } async alarm(): Promise { - await this.processGitHubVerifications(); - await this.processDueSchedules(); - const cleanup = this.rows("SELECT * FROM runs WHERE execution_backend_kind='exe-vm' AND execution_handle IS NOT NULL AND vm_cleanup_complete=0 AND vm_cleanup_attempt<8 AND COALESCE(vm_cleanup_next_at,0)<=? AND state IN ('done','failed','stopped','stopping') ORDER BY updated_at LIMIT 20", Date.now()); - for (const run of cleanup) await this.cleanupRunVm(run); - // Release completed slots before looking at the queue, so capacity is used - // immediately rather than waiting for the next polling alarm. - const running = this.rows("SELECT * FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state IN ('starting','running','blocked') ORDER BY updated_at LIMIT 40"); - for (const run of running) await this.pollRun(run); - const queued = this.rows("SELECT * FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state = 'queued' ORDER BY created_at LIMIT 20"); - for (const run of queued) { - const pipe = this.executionConfig(run.pipe_id); - if (!pipe) continue; - const active = this.one("SELECT count(*) AS count FROM runs WHERE pipe_id = ? AND state IN ('starting','running','blocked')", pipe.id) as Row; - if (Number(active.count) >= Number(pipe.max_concurrency)) continue; - const name = `${String(pipe.workspace_name).slice(0, 20)}-${String(run.id).replaceAll("-", "").slice(0, 8)}`; - this.ctx.storage.sql.exec("UPDATE runs SET state = 'starting', agent_name = ?, workspace_name = ?, updated_at = ? WHERE id = ? AND state = 'queued'", name, pipe.workspace_name, now(), run.id); - this.ctx.storage.sql.exec("UPDATE job_runs SET state='running',started_at=?,updated_at=? WHERE id=? AND state='queued'", now(), now(), run.id); - await this.startRun({ ...run, agent_name: name, workspace_name: pipe.workspace_name }, pipe); + try { + await this.processGitHubVerifications(); + await this.processDueSchedules(); + await this.requeueStaleStarts(); + const cleanup = this.rows("SELECT * FROM runs WHERE execution_backend_kind='exe-vm' AND execution_handle IS NOT NULL AND vm_cleanup_complete=0 AND vm_cleanup_attempt<8 AND COALESCE(vm_cleanup_next_at,0)<=? AND state IN ('done','failed','stopped','stopping') ORDER BY updated_at LIMIT 20", Date.now()); + for (const run of cleanup) await this.cleanupRunVm(run); + // Release completed slots before looking at the queue, so capacity is used + // immediately rather than waiting for the next polling alarm. + // A starting run has no authoritative backend handle until launch is + // acknowledged. Leave it to the launch lease instead of mis-polling a + // derived handle after an interrupted deployment. + const running = this.rows("SELECT * FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state IN ('running','blocked') ORDER BY updated_at LIMIT 40"); + for (const run of running) await this.pollRun(run); + const queued = this.rows("SELECT * FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state = 'queued' ORDER BY created_at LIMIT 20"); + for (const run of queued) { + const pipe = this.executionConfig(run.pipe_id); + if (!pipe) continue; + const active = this.one("SELECT count(*) AS count FROM runs WHERE pipe_id = ? AND state IN ('starting','running','blocked')", pipe.id) as Row; + if (Number(active.count) >= Number(pipe.max_concurrency)) continue; + const name = `${String(pipe.workspace_name).slice(0, 20)}-${String(run.id).replaceAll("-", "").slice(0, 8)}`; + const startedAt = now(); + this.ctx.storage.sql.exec("UPDATE runs SET state = 'starting', agent_name = ?, workspace_name = ?, updated_at = ? WHERE id = ? AND state = 'queued'", name, pipe.workspace_name, startedAt, run.id); + this.ctx.storage.sql.exec("UPDATE job_runs SET state='running',started_at=?,updated_at=? WHERE id=? AND state='queued'", startedAt, startedAt, run.id); + await this.startRun({ ...run, state: "starting", agent_name: name, workspace_name: pipe.workspace_name, updated_at: startedAt }, pipe); + } + } finally { + // Even if polling or launch throws, unfinished persisted work retains a + // watchdog. This also repairs wake-ups interrupted by deployments. + await this.ensureAlarm(); } + } - const pending = this.one("SELECT count(*) AS count FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state IN ('queued','starting','running','blocked')") as Row; - const verification = this.one("SELECT min(next_attempt_at) AS next_attempt_at FROM pending_verifications") as Row; - const nextVerification = Number(verification?.next_attempt_at || 0); - const schedule = this.one("SELECT min(next_run_at) AS next_run_at FROM schedule_state") as Row; - const cleanupDue = this.one("SELECT min(vm_cleanup_next_at) AS next_at FROM runs WHERE vm_cleanup_complete=0 AND vm_cleanup_attempt<8 AND vm_cleanup_next_at IS NOT NULL") as Row; - const candidates = [Number(pending.count) > 0 ? Date.now() + 15_000 : 0, nextVerification, Number(schedule?.next_run_at || 0), Number(cleanupDue?.next_at || 0)].filter(Boolean); - if (candidates.length) await this.ctx.storage.setAlarm(Math.min(...candidates)); + private async ensureAlarm(): Promise { + const pending = this.one("SELECT count(*) AS count FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state IN ('queued','starting','running','blocked','stopping')") as Row; + const verification = this.one("SELECT min(next_attempt_at) AS next_at FROM pending_verifications") as Row; + const schedule = this.one("SELECT min(next_run_at) AS next_at FROM schedule_state") as Row; + const cleanup = this.one("SELECT min(vm_cleanup_next_at) AS next_at FROM runs WHERE vm_cleanup_complete=0 AND vm_cleanup_attempt<8 AND vm_cleanup_next_at IS NOT NULL") as Row; + const candidates = [Number(pending.count) > 0 ? Date.now() + QUEUE_WATCHDOG_MS : 0, Number(verification?.next_at || 0), Number(schedule?.next_at || 0), Number(cleanup?.next_at || 0)].filter(value => value > 0); + if (!candidates.length) return; + const desired = Math.min(...candidates), current = await this.ctx.storage.getAlarm(); + if (current === null || current > desired) await this.ctx.storage.setAlarm(desired); + } + + private async requeueStaleStarts(): Promise { + const cutoff = new Date(Date.now() - STARTING_LEASE_MS).toISOString(); + const stale = this.rows("SELECT * FROM runs WHERE pipe_id IN (SELECT id FROM jobs) AND state='starting' AND prompt_accepted=0 AND updated_at<=?", cutoff); + for (const run of stale) { + if (String(run.execution_backend_kind || "exe-vm") === "exe-vm") { + const pipe = this.executionConfig(run.pipe_id), connection = pipe ? await this.connectionForPipe(pipe) : null; + if (connection) await new ExeVmBackend(connection).stop({ backendKind: "exe-vm", id: vmNameFor(String(run.id)) }).catch(() => undefined); + } + const timestamp = now(); + this.ctx.storage.sql.exec("UPDATE runs SET state='queued',execution_handle=NULL,prompt_delivery_state='pending',updated_at=? WHERE id=? AND state='starting' AND prompt_accepted=0", timestamp, run.id); + this.ctx.storage.sql.exec("UPDATE job_runs SET state='queued',started_at=NULL,updated_at=? WHERE id=? AND state='running'", timestamp, run.id); + this.activity(run.id, "launch_lease_recovered", "A stale unacknowledged launch was returned to the queue."); + } } private async saveLinear(input: unknown): Promise { @@ -1148,6 +1184,42 @@ export class TenantV2 extends DurableObject { return json({ id: runId, state: "stopped", stopped: true }); } + private async killRun(runId: string): Promise { + let run = this.one("SELECT * FROM runs WHERE id = ?", runId); + if (!run) return new Response("Not found", { status: 404 }); + if (["done", "failed", "stopped", "succeeded"].includes(String(run.state))) return json({ id: runId, state: normalizeExecutionState(run.state), killed: false, alreadyTerminal: true }); + + const priorState = String(run.state), timestamp = now(); + if (String(run.execution_backend_kind || "exe-vm") === "exe-vm" && priorState !== "queued" && !run.execution_handle) { + const handle = { backendKind: "exe-vm", id: vmNameFor(runId) }; + this.ctx.storage.sql.exec("UPDATE runs SET execution_handle=? WHERE id=?", JSON.stringify(handle), runId); + run = { ...run, execution_handle: JSON.stringify(handle) }; + } + + // Terminalize first. Backend deletion is cleanup work and must never keep a + // dead run holding a concurrency slot. + const result = await encrypt("Killed by user", this.env.CREDENTIAL_ENCRYPTION_KEY); + this.ctx.storage.sql.exec("UPDATE runs SET state='stopped',result=?,claim_released=1,updated_at=? WHERE id=?", result, timestamp, runId); + this.ctx.storage.sql.exec("UPDATE job_runs SET state='stopped',updated_at=? WHERE id=?", timestamp, runId); + this.ctx.storage.sql.exec("DELETE FROM active_claims WHERE run_id=?", runId); + this.activity(runId, "run_killed", `Killed by user while ${priorState}. The concurrency slot was released before backend cleanup.`); + await this.afterJobTerminal(String(run.pipe_id), runId, "stopped", timestamp); + + let cleanupPending = false; + if (priorState !== "queued") { + const pipe = this.executionConfig(run.pipe_id); + if (pipe && String(pipe.execution_backend_kind) === "amp") { + const connection = await this.connection(`amp:${String(pipe.execution_connection_id)}`); + if (connection) { + const stopped = await new AmpBackend(connection).stop(this.executionHandle(run, "amp")).catch(() => ({ state: "failed" as const })); + cleanupPending = stopped.state !== "stopped"; + } else cleanupPending = true; + } else if (run.execution_handle) cleanupPending = !await this.cleanupRunVm({ ...run, state: "stopped" }); + } + await this.ctx.storage.setAlarm(Date.now()); + return json({ id: runId, state: "stopped", killed: true, cleanupPending }); + } + private async linearProjects(): Promise { const linear = await this.connection<{ accessToken: string }>("linear"); if (!linear) throw new Error("Connect Linear first"); diff --git a/packages/app/src/ui.ts b/packages/app/src/ui.ts index b0bfd639..8f7205c2 100644 --- a/packages/app/src/ui.ts +++ b/packages/app/src/ui.ts @@ -186,7 +186,7 @@ function elapsed(r){const start=new Date(r.created_at).getTime(),end=activeState function updateElapsed(r){const label=$('[data-run-elapsed]');if(label)label.textContent=elapsed(r);clearInterval(elapsedTimer);if(activeStates.includes(r.state))elapsedTimer=setInterval(()=>{const current=$('[data-run-elapsed]');if(current)current.textContent=elapsed(r)},1000)} function scheduleRefresh(r){clearTimeout(refreshTimer);if(activeStates.includes(r.state)&&(r.capabilities||[]).includes('output'))refreshTimer=setTimeout(refreshOutput,3000)} async function refreshOutput(){try{const r=await request('/api/v1/runs/'+encodeURIComponent(id)),output=$('[data-run-output]'),state=$('[data-run-state]');if(output){const follow=output.scrollHeight-output.scrollTop-output.clientHeight<24;output.innerHTML=ansiToHtml(outputText(r));if(follow)output.scrollTop=output.scrollHeight}if(state)state.innerHTML=stateBadge(r.state);updateElapsed(r);scheduleRefresh(r)}catch{refreshTimer=setTimeout(refreshOutput,5000)}} -request('/api/v1/runs/'+encodeURIComponent(id)).then(async r=>({r,j:await request('/api/v1/jobs/'+encodeURIComponent(r.job_id))})).then(({r,j})=>{const caps=r.capabilities||[],canOutput=caps.includes('output'),canStop=caps.includes('stop')&&['starting','running','blocked'].includes(r.state),runLabel='Run '+r.id.slice(0,8);root.innerHTML='

'+esc(runLabel)+'

'+elapsed(r)+'

'+stateBadge(r.state)+''+(canStop?'':'')+'
Prompt
'+esc(r.prompt||'Prompt is not available for this run.')+'
'+(canOutput?'

Output

'+(activeStates.includes(r.state)?'Live · refreshes automatically':'')+'
'+ansiToHtml(outputText(r))+'
':'
Output is not available for this run.
');const output=$('[data-run-output]');if(output)output.scrollTop=output.scrollHeight;if(canStop)$('#stop-run').onclick=async()=>{await request('/api/v1/runs/'+encodeURIComponent(id)+'/stop',{method:'POST'});location.reload()};updateElapsed(r);scheduleRefresh(r)}).catch(e=>root.innerHTML='

'+esc(e.message)+'

');`); +request('/api/v1/runs/'+encodeURIComponent(id)).then(async r=>({r,j:await request('/api/v1/jobs/'+encodeURIComponent(r.job_id))})).then(({r,j})=>{const caps=r.capabilities||[],canOutput=caps.includes('output'),canKill=!['done','failed','stopped','succeeded'].includes(r.state),runLabel='Run '+r.id.slice(0,8);root.innerHTML='

'+esc(runLabel)+'

'+elapsed(r)+'

'+stateBadge(r.state)+''+(canKill?'':'')+'
Prompt
'+esc(r.prompt||'Prompt is not available for this run.')+'
'+(canOutput?'

Output

'+(activeStates.includes(r.state)?'Live · refreshes automatically':'')+'
'+ansiToHtml(outputText(r))+'
':'
Output is not available for this run.
');const output=$('[data-run-output]');if(output)output.scrollTop=output.scrollHeight;if(canKill)$('#kill-run').onclick=async()=>{if(!confirm('Kill this run and release its concurrency slot?'))return;await request('/api/v1/runs/'+encodeURIComponent(id)+'/kill',{method:'POST'});location.reload()};updateElapsed(r);scheduleRefresh(r)}).catch(e=>root.innerHTML='

'+esc(e.message)+'

');`); const settingsLayout = (active: "integrations" | "api-keys", content: string) => `