Skip to content

Commit dafe23e

Browse files
ZJKungLi-Chieh Kungclaude
authored
fix: keep worker run records from sticking or losing their start time (#52)
* fix: keep worker run records from sticking or losing their start time - Record the run inside the try block so a failed insert still clears the task from the worker's active set; before, the task was never retried by this process and stop() waited forever. - Keep the run's start time instead of reading task.updatedAt after the final checkpoint, which recorded the finish time as the start time. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * style: keep multi-line run record format Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Li-Chieh Kung <lichieh@patagoniahealth.net> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b3f6722 commit dafe23e

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,7 @@ export class TaskWorker {
162162
detail,
163163
});
164164
};
165-
await this.db.put(owner, "runs", {
166-
id: leaseId,
167-
taskId,
168-
startedAt: new Date(this.now()).toISOString(),
169-
status: "running",
170-
});
165+
const startedAt = new Date(this.now()).toISOString();
171166
const heartbeat = setInterval(
172167
() => {
173168
void this.db
@@ -186,6 +181,12 @@ export class TaskWorker {
186181
Math.max(10, Math.floor(leaseMs / 3)),
187182
);
188183
try {
184+
await this.db.put(owner, "runs", {
185+
id: leaseId,
186+
taskId,
187+
startedAt,
188+
status: "running",
189+
});
189190
const result = await this.execute(owner, task, {
190191
signal: controller.signal,
191192
guard,
@@ -196,7 +197,7 @@ export class TaskWorker {
196197
await this.db.put(owner, "runs", {
197198
id: leaseId,
198199
taskId,
199-
startedAt: task.updatedAt,
200+
startedAt,
200201
finishedAt: new Date(this.now()).toISOString(),
201202
status: result.status ?? task.status,
202203
});

‎tests/engine.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,46 @@ test("pending reviews do not starve queued work", async () => {
168168
await db.close();
169169
}
170170
});
171+
test("run history keeps the time the run started", async () => {
172+
const db = await createStore();
173+
try {
174+
await db.put("owner", "tasks", task());
175+
let clock = Date.parse("2026-01-01T00:00:00.000Z");
176+
await new TaskWorker(
177+
db,
178+
async () => {
179+
clock += 60000;
180+
return { status: "succeeded" };
181+
},
182+
{ now: () => clock },
183+
).tick();
184+
const [run] = (await db.scan<{ startedAt: string; finishedAt: string }>("runs")).map(
185+
({ value }) => value,
186+
);
187+
assert.equal(run?.startedAt, "2026-01-01T00:00:00.000Z");
188+
assert.equal(run?.finishedAt, "2026-01-01T00:01:00.000Z");
189+
} finally {
190+
await db.close();
191+
}
192+
});
193+
test("a failed run record does not leave the task stuck in the worker", async () => {
194+
const db = await createStore();
195+
try {
196+
await db.put("owner", "tasks", task());
197+
const flaky = Object.create(db) as typeof db;
198+
flaky.put = (async (owner: string, kind: string, value: { id: string }) => {
199+
if (kind === "runs") throw new Error("database unavailable");
200+
return db.put(owner, kind, value);
201+
}) as typeof db.put;
202+
const worker = new TaskWorker(flaky, async () => ({ status: "succeeded" }));
203+
await worker.tick().catch(() => {});
204+
const stopped = await Promise.race([
205+
worker.stop().then(() => true),
206+
new Promise((r) => setTimeout(() => r(false), 500)),
207+
]);
208+
assert.equal(stopped, true);
209+
assert.equal((await db.get<AgentTask>("owner", "tasks", "task1"))?.status, "failed");
210+
} finally {
211+
await db.close();
212+
}
213+
});

0 commit comments

Comments
 (0)