Resolve Waiting when the last task is stopped rather than completed

A completed/failed final task wakes the agent (its notification is the
wake signal), so Waiting flips straight to Working. A stopped final task
is a kill — no wake follows — and previously left a live session parked
on Waiting until teardown. Flip that case back to ready, and tombstone
completions that outrun their start event.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-24 00:21:02 -07:00
1 parent 528adb2fe0
commit 514fccf529
2 files changed
+143 -4

No files matched your search

@@ -3327,6 +3327,105 @@ describe("ProviderRuntimeIngestion", () => {
await waitForThread(harness.readModel, (entry) => entry.session?.status === "idle");
});
it("returns a waiting thread to ready when its last task is stopped", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
harness.emit({
type: "task.started",
eventId: asEventId("evt-stopped-task-started"),
provider: ProviderDriverKind.make("codex"),
createdAt: now,
threadId: asThreadId("thread-1"),
payload: {
taskId: "stopped-task-1",
},
});
harness.emit({
type: "turn.completed",
eventId: asEventId("evt-stopped-task-turn-completed"),
provider: ProviderDriverKind.make("codex"),
threadId: asThreadId("thread-1"),
createdAt: "2026-01-01T00:00:01.000Z",
turnId: asTurnId("turn-stopped-task"),
payload: {
state: "completed",
},
});
await waitForThread(harness.readModel, (entry) => entry.session?.status === "idle");
// A stopped task never wakes the agent, so Waiting must resolve here.
harness.emit({
type: "task.completed",
eventId: asEventId("evt-stopped-task-completed"),
provider: ProviderDriverKind.make("codex"),
createdAt: "2026-01-01T00:00:02.000Z",
threadId: asThreadId("thread-1"),
payload: {
taskId: "stopped-task-1",
status: "stopped",
},
});
await waitForThread(harness.readModel, (entry) => entry.session?.status === "ready");
});
it("keeps a waiting thread waiting when its last task completes (the wake turn follows)", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
harness.emit({
type: "task.started",
eventId: asEventId("evt-wake-task-started"),
provider: ProviderDriverKind.make("codex"),
createdAt: now,
threadId: asThreadId("thread-1"),
payload: {
taskId: "wake-task-1",
},
});
harness.emit({
type: "turn.completed",
eventId: asEventId("evt-wake-task-turn-completed"),
provider: ProviderDriverKind.make("codex"),
threadId: asThreadId("thread-1"),
createdAt: "2026-01-01T00:00:01.000Z",
turnId: asTurnId("turn-wake-task"),
payload: {
state: "completed",
},
});
await waitForThread(harness.readModel, (entry) => entry.session?.status === "idle");
// A successful completion wakes the agent; the status must go straight
// to running with no intermediate ready flash.
harness.emit({
type: "task.completed",
eventId: asEventId("evt-wake-task-completed"),
provider: ProviderDriverKind.make("codex"),
createdAt: "2026-01-01T00:00:02.000Z",
threadId: asThreadId("thread-1"),
payload: {
taskId: "wake-task-1",
status: "completed",
},
});
harness.emit({
type: "turn.started",
eventId: asEventId("evt-wake-task-wake-turn"),
provider: ProviderDriverKind.make("codex"),
threadId: asThreadId("thread-1"),
createdAt: "2026-01-01T00:00:03.000Z",
turnId: asTurnId("turn-wake-follow-up"),
});
const thread = await waitForThread(
harness.readModel,
(entry) => entry.session?.status === "running",
);
expect(thread.session?.activeTurnId).toBe("turn-wake-follow-up");
});
it("ignores late task progress for a completed task", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
@@ -802,13 +802,27 @@ const make = Effect.gen(function* () {
return wasEmpty;
};
/** Returns true when this completion closed the instance's last open task. */
const trackTaskCompleted = (threadId: ThreadId, event: ProviderRuntimeEvent, taskId: string) => {
const instanceKey = taskInstanceKey(event);
const entry = openTasksByThreadId.get(threadId);
if (!entry || entry.instanceKey !== taskInstanceKey(event)) {
return;
if (!entry) {
// Completion outrunning its start: tombstone it anyway, or the late
// start would open a phantom task and park the thread on Waiting for
// a completion that already happened.
openTasksByThreadId.set(threadId, {
instanceKey,
taskIds: new Set(),
closedTaskIds: new Set([taskId]),
});
return false;
}
entry.taskIds.delete(taskId);
if (entry.instanceKey !== instanceKey) {
return false;
}
const removed = entry.taskIds.delete(taskId);
entry.closedTaskIds.add(taskId);
return removed && entry.taskIds.size === 0;
};
const hasOpenTasks = (threadId: ThreadId, event: ProviderRuntimeEvent) => {
@@ -1885,7 +1899,33 @@ const make = Effect.gen(function* () {
}
let taskTitle: string | undefined;
if (event.type === "task.completed") {
trackTaskCompleted(thread.id, event, event.payload.taskId);
const closedLastTask = trackTaskCompleted(thread.id, event, event.payload.taskId);
// A completed/failed final task wakes the agent — its notification is
// the wake signal — so the thread goes Waiting → Working directly and
// flipping to ready here would flash the very Done this feature
// removes. A STOPPED final task is the opposite: someone killed it,
// no wake follows, and a live session would sit on Waiting forever.
// Only that case flips back to ready.
if (
closedLastTask &&
event.payload.status === "stopped" &&
thread.session?.status === "idle" &&
thread.session.activeTurnId === null &&
sessionInstanceKeyFor(thread.session) === taskInstanceKey(event)
) {
yield* orchestrationEngine.dispatch({
type: "thread.session.set",
commandId: yield* providerCommandId(event, "last-task-session-ready"),
threadId: thread.id,
session: {
...thread.session,
status: "ready",
lastError: null,
updatedAt: now,
},
createdAt: now,
});
}
taskTitle = yield* lookupTaskDescription(thread.id, event.payload.taskId);
if (!taskTitle) {
const threadDetail = yield* getLoadedThreadDetail();