A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.
TypeScript
fix(actions): keep prepared review alive across lease handoff (#120)
Kevin Rajan6d8a08420735
diff --git a/apps/server/src/engine/service.ts b/apps/server/src/engine/service.tsindex 003fea9..8cafc36 100644--- a/apps/server/src/engine/service.ts+++ b/apps/server/src/engine/service.ts@@ -740,7 +740,11 @@ export class AgentService {try {await context.checkpoint({ actionId: proposal.id });} catch (error) {- if (proposal.status === "awaiting_review")+ const current = await this.db.get<AgentTask>(owner, "tasks", task.id);+ if (+ proposal.status === "awaiting_review" &&+ (current?.status === "paused" || current?.status === "cancelled")+ )await this.actions.decide(owner, proposal.id, proposal.hash, "deny");throw error;}diff --git a/tests/prepare-lease-loss.test.ts b/tests/prepare-lease-loss.test.tsnew file mode 100644index 0000000..3b583dd--- /dev/null+++ b/tests/prepare-lease-loss.test.ts@@ -0,0 +1,144 @@+import assert from "node:assert/strict";+import { mkdtemp, rm } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { test } from "node:test";+import { ActionService } from "../apps/server/src/actions.ts";+import { createStore } from "../apps/server/src/db.ts";+import { AgentService } from "../apps/server/src/engine/service.ts";+import { LostLeaseError, type TaskContext } from "../apps/server/src/engine/worker.ts";+import type { AgentTask } from "../packages/domain/src/agent.ts";+import type { ProposalInput } from "../packages/domain/src/index.ts";++const owner = "owner-1";+const input: ProposalInput = {+ kind: "email.send",+ data: {+ to: ["[email protected]"],+ cc: [],+ bcc: [],+ subject: "Hello",+ body: "World",+ attachmentIds: [],+ },+};++const baseTask = (): AgentTask => ({+ id: "task-1",+ title: "t",+ prompt: "p",+ kind: "agent",+ status: "running",+ plan: [],+ evidence: [],+ input: {},+ state: { connectionId: "conn-1" },+ createdAt: "2026-01-01T00:00:00.000Z",+ updatedAt: "2026-01-01T00:00:00.000Z",+ attempts: 1,+ leaseId: "lease-1",+ leaseUntil: null,+ artifactIds: [],+});++async function setup() {+ const root = await mkdtemp(join(tmpdir(), "openmuse-prepare-"));+ const db = await createStore({ dataDir: join(root, "pg") });+ const actions = new ActionService(db, {+ execute: async () => "sent",+ connected: async () => true,+ });+ const service = new AgentService(+ db,+ {} as never,+ { connection: async () => ({ id: "conn-1" }) } as never,+ {} as never,+ actions,+ {} as never,+ {} as never,+ );+ await db.put(owner, "tasks", baseTask());+ return { root, db, actions, service };+}++const context = (overrides: Partial<TaskContext> = {}): TaskContext => ({+ signal: new AbortController().signal,+ guard: async () => {},+ checkpoint: async (patch: Partial<AgentTask>) => ({ ...baseTask(), ...patch }) as AgentTask,+ event: async () => {},+ ...overrides,+});++test("lease loss with an aborted worker signal leaves the proposal awaiting review", async () => {+ const { root, db, service } = await setup();+ try {+ const controller = new AbortController();+ controller.abort();+ const ctx = context({+ signal: controller.signal,+ checkpoint: async () => {+ throw new LostLeaseError();+ },+ });+ await assert.rejects(service.prepare(owner, baseTask(), input, "k1", ctx), LostLeaseError);+ const proposals = await db.list<{ id: string; status: string }>(owner, "actions");+ assert.equal(proposals.length, 1);+ assert.equal(+ proposals[0]?.status,+ "awaiting_review",+ "takeover retry must find the proposal still reviewable",+ );+ } finally {+ await db.close();+ await rm(root, { recursive: true, force: true });+ }+});++test("the taking-over worker reuses the surviving proposal instead of failing", async () => {+ const { root, db, service } = await setup();+ try {+ const first = context({+ checkpoint: async () => {+ throw new Error("lease lost");+ },+ });+ await assert.rejects(service.prepare(owner, baseTask(), input, "k2", first));+ // Second worker, same task and key: must get the same live proposal back.+ const proposal = await service.prepare(owner, baseTask(), input, "k2", context());+ assert.equal(proposal.status, "awaiting_review");+ assert.equal((await db.list(owner, "actions")).length, 1, "no duplicate proposal minted");+ } finally {+ await db.close();+ await rm(root, { recursive: true, force: true });+ }+});++for (const status of ["paused", "cancelled"] as const) {+ test(`a durably ${status} task auto-denies its orphaned proposal`, async () => {+ const { root, db, service } = await setup();+ try {+ await db.put(owner, "tasks", { ...baseTask(), status });+ const controller = new AbortController();+ controller.abort();+ const ctx = context({+ signal: controller.signal,+ checkpoint: async () => {+ throw new LostLeaseError();+ },+ });+ await assert.rejects(+ service.prepare(owner, baseTask(), input, `k3-${status}`, ctx),+ LostLeaseError,+ );+ const proposals = await db.list<{ status: string }>(owner, "actions");+ assert.equal(+ proposals[0]?.status,+ "denied",+ "a real pause/cancel must still clean up the orphaned review",+ );+ } finally {+ await db.close();+ await rm(root, { recursive: true, force: true });+ }+ });+}