From a69b9299304042a62400cc0e3e03ddedf8233841 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Thu, 1 Oct 2026 04:05:01 -0500 Subject: [PATCH 1/3] fix(computer): never execute or overwrite dead commands --- apps/server/src/computer.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/apps/server/src/computer.ts b/apps/server/src/computer.ts index 15b19fb2..0b5a2163 100644 --- a/apps/server/src/computer.ts +++ b/apps/server/src/computer.ts @@ -634,13 +634,30 @@ export class ComputerService { active.token !== lease.token || active.stopping || active.expiresAt <= Date.now() - ) - return this.db.put(owner, "computer-commands", { + ) { + const stopped: ComputerCommand = { ...command, status: "interrupted", stderr: "Stopped before execution", completedAt: new Date().toISOString(), - }); + }; + const saved = await this.db.compareAndSwap( + owner, + "computer-commands", + id, + { status: "running" }, + { + status: "interrupted", + stderr: stopped.stderr, + completedAt: stopped.completedAt, + }, + ); + return ( + saved ?? (await this.db.get(owner, "computer-commands", id)) ?? stopped + ); + } + const receipt = await this.db.get(owner, "computer-commands", id); + if (receipt && receipt.status !== "running") return receipt; let result: DockerResult; try { result = await this.docker( From 519c42c952ee7b4f46d0b0cb3fa6842e9a68a22b Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Thu, 1 Oct 2026 04:05:07 -0500 Subject: [PATCH 2/3] test(computer): cover dead-command races --- tests/computer.test.ts | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tests/computer.test.ts b/tests/computer.test.ts index 94bd4d50..88fe2df8 100644 --- a/tests/computer.test.ts +++ b/tests/computer.test.ts @@ -302,3 +302,101 @@ test("an expired lease from a dead executor recovers as interrupted without repl assert.ok(!f.calls.some((call) => call.args[0] === "exec")); assert.equal((await service.start("owner")).status, "running"); }); + + +test("a Stop quarantine that lands before the lease check keeps its record", async () => { + const f = fixture(); + const service = new ComputerService(db, config, f.runner); + const cmdId = createHash("sha256").update("computer-command:quarantine-case").digest("hex"); + const originalGet = db.get.bind(db); + let intercepted = false; + db.get = (async (o: string, kind: string, id: string) => { + if (!intercepted && kind === "computer-state" && id === "lease") { + const lease = await originalGet<{ token: string }>(o, kind, id); + const row = await originalGet(o, "computer-commands", cmdId); + if (lease && row) { + intercepted = true; + await db.compareAndSwap( + o, + kind, + id, + { token: lease.token, stopping: false }, + { + stopping: true, + stopInFlight: true, + stopAttempt: "stop-attempt-1", + stopConfirmed: false, + expiresAt: Date.now() + 180000, + }, + ); + await db.compareAndSwap( + o, + "computer-commands", + cmdId, + { status: "running" }, + { + status: "interrupted", + completedAt: new Date().toISOString(), + stderr: "Stopped by the user. Inspect the workspace before repeating this command.", + }, + ); + } + } + return originalGet(o, kind, id); + }) as Store["get"]; + try { + const receipt = await service.execute( + "owner", + { command: "sleep 30" }, + { idempotencyKey: "quarantine-case" }, + ); + assert.equal(receipt.status, "interrupted"); + assert.match(receipt.stderr, /Stopped by the user/); + assert.ok(!f.calls.some((call) => call.args[0] === "exec")); + } finally { + db.get = originalGet; + await db.remove("owner", "computer-state", "lease"); + } +}); + +test("a command marked dead after the lease check is never executed", async () => { + const f = fixture({ command: async () => ok("should never run") }); + const service = new ComputerService(db, config, f.runner); + const cmdId = createHash("sha256").update("computer-command:dead-row-case").digest("hex"); + const originalGet = db.get.bind(db); + let intercepted = false; + db.get = (async (o: string, kind: string, id: string) => { + if (!intercepted && kind === "computer-state" && id === "lease") { + const lease = await originalGet<{ token: string }>(o, kind, id); + const row = await originalGet(o, "computer-commands", cmdId); + if (lease && row) { + intercepted = true; + await db.compareAndSwap( + o, + "computer-commands", + cmdId, + { status: "running" }, + { + status: "interrupted", + completedAt: new Date().toISOString(), + stderr: + "Execution was interrupted. Its outcome is unknown; inspect files before running it again.", + }, + ); + } + } + return originalGet(o, kind, id); + }) as Store["get"]; + try { + const receipt = await service.execute( + "owner", + { command: "touch /tmp/should-not-run" }, + { idempotencyKey: "dead-row-case" }, + ); + assert.equal(receipt.status, "interrupted"); + assert.equal(receipt.stdout, ""); + assert.ok(!f.calls.some((call) => call.args[0] === "exec"), "docker exec must not run"); + } finally { + db.get = originalGet; + } +}); From 0123535b001353ee450ce1ab113362c8ed96cb38 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Thu, 1 Oct 2026 04:07:20 -0500 Subject: [PATCH 3/3] style(test): satisfy biome --- tests/computer.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/computer.test.ts b/tests/computer.test.ts index 88fe2df8..3ca73b50 100644 --- a/tests/computer.test.ts +++ b/tests/computer.test.ts @@ -303,7 +303,6 @@ test("an expired lease from a dead executor recovers as interrupted without repl assert.equal((await service.start("owner")).status, "running"); }); - test("a Stop quarantine that lands before the lease check keeps its record", async () => { const f = fixture(); const service = new ComputerService(db, config, f.runner);