From 514fccf5297ec832fdd1e5ef0a1d31de7958ec05 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Fri, 24 Jul 2026 00:21:02 -0700 Subject: [PATCH] Resolve Waiting when the last task is stopped rather than completed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../Layers/ProviderRuntimeIngestion.test.ts | 99 +++++++++++++++++++ .../Layers/ProviderRuntimeIngestion.ts | 48 ++++++++- 2 files changed, 143 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts index ab9db9cbb9..5ff794cc47 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts @@ -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"; diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts index b9a5b3d63e..4aa6b889e2 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -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();