From 8da4f9e1d0de8b49d20c0f83b117abe127fb6232 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Thu, 2 Jul 2026 05:29:04 -0700 Subject: [PATCH] Make workflow completion order-robust against inverted sequences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A task.completed for a known workflow task now creates the run when its task.started has not been applied yet — adopted runs can carry reset provider sequences across CLI restarts, letting the completion sort first. The later-applied started only fills metadata and can never resurrect a terminal status, so derivation correctness no longer depends on the comparator at all. Co-Authored-By: Claude Fable 5 --- apps/web/src/workflow-logic.test.ts | 28 +++++++++++++++++++++++++++- apps/web/src/workflow-logic.ts | 10 ++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/apps/web/src/workflow-logic.test.ts b/apps/web/src/workflow-logic.test.ts index 006677293a..ee19347e4f 100644 --- a/apps/web/src/workflow-logic.test.ts +++ b/apps/web/src/workflow-logic.test.ts @@ -1,4 +1,9 @@ -import { EventId, type OrchestrationThreadActivity, TurnId } from "@t3tools/contracts"; +import { + NonNegativeInt, + EventId, + type OrchestrationThreadActivity, + TurnId, +} from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; import { @@ -20,6 +25,7 @@ function buildActivity(overrides: { tone?: OrchestrationThreadActivity["tone"]; payload?: Record; turnId?: string; + sequence?: number; }): OrchestrationThreadActivity { return { id: EventId.make(overrides.id ?? `activity-${nextActivityId++}`), @@ -30,6 +36,9 @@ function buildActivity(overrides: { tone: overrides.tone ?? "info", payload: overrides.payload ?? {}, turnId: overrides.turnId ? TurnId.make(overrides.turnId) : null, + ...(overrides.sequence !== undefined + ? { sequence: NonNegativeInt.make(overrides.sequence) } + : {}), }; } @@ -276,6 +285,23 @@ describe("deriveWorkflowRuns", () => { expect(run?.agentCounts).toEqual({ total: 3, queued: 0, running: 0, done: 1, error: 2 }); }); + it("applies a completion even when it sorts before its task.started", () => { + const completed = buildActivity({ + id: "completed-task-1", + kind: "task.completed", + createdAt: "2026-02-23T00:00:00.500Z", + sequence: 1, + payload: { taskId: "task-1", status: "completed", detail: "done" }, + }); + // Same-timestamp + inverted sequence (adopted runs can reset provider + // sequence): the started activity sorts after the completion. + const started = { ...workflowStartedActivity("task-1"), sequence: NonNegativeInt.make(5) }; + const runs = deriveWorkflowRuns([completed, started]); + expect(runs).toHaveLength(1); + expect(runs[0]?.status).toBe("completed"); + expect(runs[0]?.name).toBe("spec"); + }); + it("keeps a running run untouched while the session is active", () => { const runs = deriveWorkflowRuns( [ diff --git a/apps/web/src/workflow-logic.ts b/apps/web/src/workflow-logic.ts index 253db4edd3..c710a6977f 100644 --- a/apps/web/src/workflow-logic.ts +++ b/apps/web/src/workflow-logic.ts @@ -335,6 +335,7 @@ export function deriveWorkflowRuns( options?: { readonly sessionActive?: boolean | undefined }, ): WorkflowRun[] { const ordered = [...activities].toSorted(compareWorkflowActivityOrder); + const workflowTaskIds = collectWorkflowTaskIds(activities); const runs = new Map(); const ensureRun = (taskId: string, activity: OrchestrationThreadActivity): MutableWorkflowRun => { @@ -439,10 +440,15 @@ export function deriveWorkflowRuns( break; } case "task.completed": { - const run = runs.get(taskId); - if (!run) { + // Order-robust terminal handling: a completion for a known workflow + // task creates the run if its task.started has not been applied yet + // (adopted runs can carry inverted provider sequences across CLI + // restarts); the later-applied started only fills metadata and can + // never resurrect a terminal status. + if (!runs.has(taskId) && !workflowTaskIds.has(taskId)) { break; } + const run = ensureRun(taskId, activity); run.revision += 1; run.updatedAt = activity.createdAt; run.status =