Open History to see the last commit
1import {
2 TurnItemId,
3 NodeId,
4 MessageId,
5 RunId,
6 type OrchestrationV2RunStatus,
7} from "@t3tools/contracts";
8import * as DateTime from "effect/DateTime";
9import { describe, expect, it } from "vite-plus/test";
10
11import { v2Projection } from "./orchestrationV2TestFixtures.ts";
12import {
13 deriveLatestThreadRun,
14 deriveThreadActivityRun,
15 deriveThreadRuntime,
16 threadRuntimeHasInterruptibleRun,
17} from "./threadExecution.ts";
18import { threadRuntimeCanArchive, type ThreadRuntimeSummary } from "./models.ts";
19
20const now = DateTime.makeUnsafe("2026-07-28T10:00:00.000Z");
21
22function run(id: string, ordinal: number, status: OrchestrationV2RunStatus) {
23 return {
24 id: RunId.make(id),
25 threadId: v2Projection.thread.id,
26 ordinal,
27 providerInstanceId: v2Projection.thread.providerInstanceId,
28 modelSelection: v2Projection.thread.modelSelection,
29 providerThreadId: null,
30 userMessageId: MessageId.make(`message-${id}`),
31 rootNodeId: null,
32 activeAttemptId: null,
33 status,
34 requestedAt: now,
35 startedAt: status === "queued" ? null : now,
36 completedAt: null,
37 checkpointId: null,
38 contextHandoffId: null,
39 };
40}
41
42describe("thread execution presentation", () => {
43 it("derives the current root failure without inheriting errors from children or previous runs", () => {
44 const failed = { ...run("limited", 1, "failed"), rootNodeId: NodeId.make("root") };
45 const item = {
46 id: TurnItemId.make("limit-error"),
47 threadId: v2Projection.thread.id,
48 runId: failed.id,
49 nodeId: failed.rootNodeId,
50 providerThreadId: null,
51 providerTurnId: null,
52 nativeItemRef: null,
53 parentItemId: null,
54 ordinal: 1,
55 type: "error" as const,
56 status: "failed" as const,
57 title: "Usage limit reached",
58 startedAt: now,
59 completedAt: now,
60 updatedAt: now,
61 failure: {
62 class: "usage_limit" as const,
63 message: "Plan limit reached",
64 code: "usageLimitExceeded",
65 retryable: null,
66 },
67 };
68 const projection = { ...v2Projection, runs: [failed], turnItems: [item] };
69 expect(deriveThreadRuntime(projection)).toMatchObject({
70 lastError: "Plan limit reached",
71 lastErrorClass: "usage_limit",
72 });
73 expect(
74 deriveThreadRuntime({
75 ...projection,
76 turnItems: [{ ...item, nodeId: NodeId.make("child") }],
77 }),
78 ).toMatchObject({ lastError: null, lastErrorClass: null });
79 expect(
80 deriveThreadRuntime({
81 ...projection,
82 runs: [{ ...failed, rootNodeId: NodeId.make("new-root") }],
83 }),
84 ).toMatchObject({ lastError: null, lastErrorClass: null });
85 expect(
86 deriveThreadRuntime({ ...projection, runs: [failed, run("new", 2, "running")] }),
87 ).toMatchObject({ status: "running", lastError: null, lastErrorClass: null });
88 });
89
90 it("keeps live activity attached to an executing run when a newer run is queued", () => {
91 const runningRun = run("run-running", 1, "running");
92 const queuedRun = run("run-queued", 2, "queued");
93 const projection = { ...v2Projection, runs: [queuedRun, runningRun], updatedAt: now };
94
95 expect(deriveLatestThreadRun(projection)?.runId).toBe(queuedRun.id);
96 expect(deriveThreadActivityRun(projection)).toMatchObject({
97 runId: runningRun.id,
98 status: "running",
99 });
100
101 const runtime = deriveThreadRuntime(projection);
102 expect(runtime).toMatchObject({
103 status: "running",
104 activeRunId: runningRun.id,
105 });
106 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
107 });
108
109 it("does not expose a queued-only run as interruptible", () => {
110 const queuedRun = run("run-queued", 1, "queued");
111 const projection = { ...v2Projection, runs: [queuedRun], updatedAt: now };
112
113 expect(deriveThreadActivityRun(projection)).toMatchObject({
114 runId: queuedRun.id,
115 status: "queued",
116 });
117
118 const runtime = deriveThreadRuntime(projection);
119 expect(runtime).toMatchObject({
120 status: "queued",
121 activeRunId: null,
122 });
123 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
124 });
125
126 it("keeps checkpoint-wait activity visible without exposing a non-functional interrupt", () => {
127 const waitingRun = run("run-waiting", 1, "waiting");
128 const projection = { ...v2Projection, runs: [waitingRun], updatedAt: now };
129
130 expect(deriveThreadActivityRun(projection)).toMatchObject({
131 runId: waitingRun.id,
132 status: "waiting",
133 });
134
135 const runtime = deriveThreadRuntime(projection);
136 expect(runtime).toMatchObject({
137 status: "waiting",
138 activeRunId: null,
139 });
140 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
141 });
142
143 it("does not expose a stale active run after the runtime parks at idle", () => {
144 const runtime = {
145 status: "idle" as const,
146 activeRunId: RunId.make("run-stale"),
147 providerInstanceId: v2Projection.thread.providerInstanceId,
148 providerName: null,
149 lastError: null,
150 updatedAt: DateTime.formatIso(now),
151 };
152
153 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
154 });
155
156 it.each(["preparing", "starting"] as const)("keeps an active %s run interruptible", (status) => {
157 const runtime = {
158 status,
159 activeRunId: RunId.make(`run-${status}`),
160 providerInstanceId: v2Projection.thread.providerInstanceId,
161 providerName: null,
162 lastError: null,
163 updatedAt: DateTime.formatIso(now),
164 };
165
166 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
167 });
168});
169
170describe("threadRuntimeCanArchive", () => {
171 const runtime = (
172 status: ThreadRuntimeSummary["status"],
173 activeRunId: ThreadRuntimeSummary["activeRunId"],
174 ): ThreadRuntimeSummary => ({
175 status,
176 activeRunId,
177 providerInstanceId: v2Projection.thread.providerInstanceId,
178 providerName: null,
179 lastError: null,
180 updatedAt: DateTime.formatIso(now),
181 });
182
183 it.each(["preparing", "starting", "running"] as const)(
184 "blocks archive while a provider is %s",
185 (status) => {
186 expect(threadRuntimeCanArchive(runtime(status, RunId.make(`run-${status}`)))).toBe(false);
187 },
188 );
189
190 it("only blocks a queued runtime when a provider run remains attached", () => {
191 expect(threadRuntimeCanArchive(runtime("queued", RunId.make("run-queued")))).toBe(false);
192 expect(threadRuntimeCanArchive(runtime("queued", null))).toBe(true);
193 });
194
195 it("allows waiting and idle threads", () => {
196 expect(threadRuntimeCanArchive(runtime("waiting", RunId.make("run-finished")))).toBe(true);
197 expect(threadRuntimeCanArchive(runtime("idle", null))).toBe(true);
198 });
199});