Make workflow completion order-robust against inverted sequences

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 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-02 05:29:04 -07:00
1 parent cb5a39a68c
commit 8da4f9e1d0
2 files changed
+35 -3

No files matched your search

+27 -1
View File
@@ -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<string, unknown>;
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(
[
+8 -2
View File
@@ -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<string, MutableWorkflowRun>();
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 =