Skip to content

Commit 6d8a084

Browse files
authored
fix(actions): keep prepared review alive across lease handoff (#120)
* fix(actions): keep prepared review alive across lease handoff * test(actions): cover lease handoff during review prep * test(actions): refresh lease-handoff fixture types * fix(actions): distinguish task stop from lease abort * test: cover aborted lease loss versus durable task stop * style: format lease-loss regression
1 parent cef3378 commit 6d8a084

2 files changed

Lines changed: 149 additions & 1 deletion

File tree

‎apps/server/src/engine/service.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,11 @@ export class AgentService {
740740
try {
741741
await context.checkpoint({ actionId: proposal.id });
742742
} catch (error) {
743-
if (proposal.status === "awaiting_review")
743+
const current = await this.db.get<AgentTask>(owner, "tasks", task.id);
744+
if (
745+
proposal.status === "awaiting_review" &&
746+
(current?.status === "paused" || current?.status === "cancelled")
747+
)
744748
await this.actions.decide(owner, proposal.id, proposal.hash, "deny");
745749
throw error;
746750
}

‎tests/prepare-lease-loss.test.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import assert from "node:assert/strict";
2+
import { mkdtemp, rm } from "node:fs/promises";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { test } from "node:test";
6+
import { ActionService } from "../apps/server/src/actions.ts";
7+
import { createStore } from "../apps/server/src/db.ts";
8+
import { AgentService } from "../apps/server/src/engine/service.ts";
9+
import { LostLeaseError, type TaskContext } from "../apps/server/src/engine/worker.ts";
10+
import type { AgentTask } from "../packages/domain/src/agent.ts";
11+
import type { ProposalInput } from "../packages/domain/src/index.ts";
12+
13+
const owner = "owner-1";
14+
const input: ProposalInput = {
15+
kind: "email.send",
16+
data: {
17+
to: ["a@example.com"],
18+
cc: [],
19+
bcc: [],
20+
subject: "Hello",
21+
body: "World",
22+
attachmentIds: [],
23+
},
24+
};
25+
26+
const baseTask = (): AgentTask => ({
27+
id: "task-1",
28+
title: "t",
29+
prompt: "p",
30+
kind: "agent",
31+
status: "running",
32+
plan: [],
33+
evidence: [],
34+
input: {},
35+
state: { connectionId: "conn-1" },
36+
createdAt: "2026-01-01T00:00:00.000Z",
37+
updatedAt: "2026-01-01T00:00:00.000Z",
38+
attempts: 1,
39+
leaseId: "lease-1",
40+
leaseUntil: null,
41+
artifactIds: [],
42+
});
43+
44+
async function setup() {
45+
const root = await mkdtemp(join(tmpdir(), "openmuse-prepare-"));
46+
const db = await createStore({ dataDir: join(root, "pg") });
47+
const actions = new ActionService(db, {
48+
execute: async () => "sent",
49+
connected: async () => true,
50+
});
51+
const service = new AgentService(
52+
db,
53+
{} as never,
54+
{ connection: async () => ({ id: "conn-1" }) } as never,
55+
{} as never,
56+
actions,
57+
{} as never,
58+
{} as never,
59+
);
60+
await db.put(owner, "tasks", baseTask());
61+
return { root, db, actions, service };
62+
}
63+
64+
const context = (overrides: Partial<TaskContext> = {}): TaskContext => ({
65+
signal: new AbortController().signal,
66+
guard: async () => {},
67+
checkpoint: async (patch: Partial<AgentTask>) => ({ ...baseTask(), ...patch }) as AgentTask,
68+
event: async () => {},
69+
...overrides,
70+
});
71+
72+
test("lease loss with an aborted worker signal leaves the proposal awaiting review", async () => {
73+
const { root, db, service } = await setup();
74+
try {
75+
const controller = new AbortController();
76+
controller.abort();
77+
const ctx = context({
78+
signal: controller.signal,
79+
checkpoint: async () => {
80+
throw new LostLeaseError();
81+
},
82+
});
83+
await assert.rejects(service.prepare(owner, baseTask(), input, "k1", ctx), LostLeaseError);
84+
const proposals = await db.list<{ id: string; status: string }>(owner, "actions");
85+
assert.equal(proposals.length, 1);
86+
assert.equal(
87+
proposals[0]?.status,
88+
"awaiting_review",
89+
"takeover retry must find the proposal still reviewable",
90+
);
91+
} finally {
92+
await db.close();
93+
await rm(root, { recursive: true, force: true });
94+
}
95+
});
96+
97+
test("the taking-over worker reuses the surviving proposal instead of failing", async () => {
98+
const { root, db, service } = await setup();
99+
try {
100+
const first = context({
101+
checkpoint: async () => {
102+
throw new Error("lease lost");
103+
},
104+
});
105+
await assert.rejects(service.prepare(owner, baseTask(), input, "k2", first));
106+
// Second worker, same task and key: must get the same live proposal back.
107+
const proposal = await service.prepare(owner, baseTask(), input, "k2", context());
108+
assert.equal(proposal.status, "awaiting_review");
109+
assert.equal((await db.list(owner, "actions")).length, 1, "no duplicate proposal minted");
110+
} finally {
111+
await db.close();
112+
await rm(root, { recursive: true, force: true });
113+
}
114+
});
115+
116+
for (const status of ["paused", "cancelled"] as const) {
117+
test(`a durably ${status} task auto-denies its orphaned proposal`, async () => {
118+
const { root, db, service } = await setup();
119+
try {
120+
await db.put(owner, "tasks", { ...baseTask(), status });
121+
const controller = new AbortController();
122+
controller.abort();
123+
const ctx = context({
124+
signal: controller.signal,
125+
checkpoint: async () => {
126+
throw new LostLeaseError();
127+
},
128+
});
129+
await assert.rejects(
130+
service.prepare(owner, baseTask(), input, `k3-${status}`, ctx),
131+
LostLeaseError,
132+
);
133+
const proposals = await db.list<{ status: string }>(owner, "actions");
134+
assert.equal(
135+
proposals[0]?.status,
136+
"denied",
137+
"a real pause/cancel must still clean up the orphaned review",
138+
);
139+
} finally {
140+
await db.close();
141+
await rm(root, { recursive: true, force: true });
142+
}
143+
});
144+
}

0 commit comments

Comments
 (0)