Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadExecution.test.ts

395 lines13.0 KB

1import {
2 TurnItemId,
3 NodeId,
4 MessageId,
5 ProviderInstanceId,
6 ProviderThreadId,
7 ProviderSessionId,
8 ProviderDriverKind,
9 ProviderTurnId,
10 RunAttemptId,
11 RunId,
12 ThreadId,
13 type OrchestrationV2ExecutionNode,
14 type OrchestrationV2RunStatus,
15} from "@t3tools/contracts";
16import * as DateTime from "effect/DateTime";
17import { usageLimitBlockedRun } from "@t3tools/shared/orchestrationV2ThreadError";
18import { describe, expect, it } from "vite-plus/test";
19​
20import { v2Projection } from "./orchestrationV2TestFixtures.ts";
21import {
22 deriveReportedModelSelection,
23 deriveLatestThreadRun,
24 deriveProviderSubagentStatus,
25 deriveRunlessWorkStartedAt,
26 deriveThreadActivityRun,
27 deriveThreadRuntime,
28 threadRuntimeHasInterruptibleRun,
29} from "./threadExecution.ts";
30​
31const now = DateTime.makeUnsafe("2026-07-28T10:00:00.000Z");
32​
33function run(id: string, ordinal: number, status: OrchestrationV2RunStatus) {
34 return {
35 id: RunId.make(id),
36 threadId: v2Projection.thread.id,
37 ordinal,
38 providerInstanceId: v2Projection.thread.providerInstanceId,
39 modelSelection: v2Projection.thread.modelSelection,
40 providerThreadId: null,
41 userMessageId: MessageId.make(`message-${id}`),
42 rootNodeId: null,
43 activeAttemptId: null,
44 status,
45 requestedAt: now,
46 startedAt: status === "queued" ? null : now,
47 completedAt: null,
48 checkpointId: null,
49 contextHandoffId: null,
50 };
52​
53describe("thread execution presentation", () => {
54 it("derives the current root failure without inheriting errors from children or previous runs", () => {
55 const failed = { ...run("limited", 1, "failed"), rootNodeId: NodeId.make("root") };
56 const item = {
57 id: TurnItemId.make("limit-error"),
58 threadId: v2Projection.thread.id,
59 runId: failed.id,
60 nodeId: failed.rootNodeId,
61 providerThreadId: null,
62 providerTurnId: null,
63 nativeItemRef: null,
64 parentItemId: null,
65 ordinal: 1,
66 type: "error" as const,
67 status: "failed" as const,
68 title: "Usage limit reached",
69 startedAt: now,
70 completedAt: now,
71 updatedAt: now,
72 failure: {
73 class: "usage_limit" as const,
74 message: "Plan limit reached",
75 code: "usageLimitExceeded",
76 retryable: null,
77 },
78 };
79 const projection = { ...v2Projection, runs: [failed], turnItems: [item] };
80 expect(deriveThreadRuntime(projection)).toMatchObject({
81 lastError: "Plan limit reached",
82 lastErrorClass: "usage_limit",
83 });
84 expect(
85 deriveThreadRuntime({
86 ...projection,
87 turnItems: [{ ...item, nodeId: NodeId.make("child") }],
88 }),
89 ).toMatchObject({ lastError: null, lastErrorClass: null });
90 expect(
91 deriveThreadRuntime({
92 ...projection,
93 runs: [{ ...failed, rootNodeId: NodeId.make("new-root") }],
94 }),
95 ).toMatchObject({ lastError: null, lastErrorClass: null });
96 expect(
97 deriveThreadRuntime({ ...projection, runs: [failed, run("new", 2, "running")] }),
98 ).toMatchObject({ status: "running", lastError: null, lastErrorClass: null });
99 });
100​
101 // regression: #15029 working timers no longer reset on every background wake
102 it("counts a wake run's activity from the start of the work it continues", () => {
103 const workStartedAt = DateTime.makeUnsafe("2026-07-28T09:20:00.000Z");
104 const wake = { ...run("wake", 2, "running"), workStartedAt };
105 expect(
106 deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "completed"), wake] })
107 ?.activityStartedAt,
108 ).toBe("2026-07-28T09:20:00.000Z");
109 expect(
110 deriveThreadRuntime({ ...v2Projection, runs: [run("prompt", 1, "running")] })
111 ?.activityStartedAt,
112 ).toBe("2026-07-28T10:00:00.000Z");
113 });
114​
115 it("keeps a subscription limit visible while later messages stay queued", () => {
116 const failed = {
117 ...run("limited", 1, "failed"),
118 rootNodeId: NodeId.make("root"),
119 completedAt: now,
120 };
121 const queued = run("queued", 2, "queued");
122 const item = {
123 id: TurnItemId.make("limit-error"),
124 threadId: v2Projection.thread.id,
125 runId: failed.id,
126 nodeId: failed.rootNodeId,
127 providerThreadId: null,
128 providerTurnId: null,
129 nativeItemRef: null,
130 parentItemId: null,
131 ordinal: 1,
132 type: "error" as const,
133 status: "failed" as const,
134 title: "Usage limit reached",
135 startedAt: now,
136 completedAt: now,
137 updatedAt: now,
138 failure: {
139 class: "usage_limit" as const,
140 message: "Plan limit reached",
141 code: "usageLimitExceeded",
142 retryable: null,
143 },
144 };
145 const projection = { ...v2Projection, runs: [failed, queued], turnItems: [item] };
146​
147 expect(deriveLatestThreadRun(projection)?.runId).toBe(failed.id);
148 expect(deriveThreadActivityRun(projection)?.runId).toBe(failed.id);
149 expect(deriveThreadRuntime(projection)).toMatchObject({
150 status: "failed",
151 lastError: "Plan limit reached",
152 lastErrorClass: "usage_limit",
153 });
154 const cancelledQueued = {
155 ...run("cancelled-queued", 3, "cancelled"),
156 startedAt: null,
157 completedAt: now,
158 };
159 expect(
160 usageLimitBlockedRun([failed, queued, cancelledQueued], projection.turnItems, null)?.id,
161 ).toBe(failed.id);
162 expect(
163 deriveThreadRuntime({ ...projection, runs: [failed, queued, cancelledQueued] }),
164 ).toMatchObject({ status: "failed", lastErrorClass: "usage_limit" });
165 expect(
166 deriveThreadRuntime({
167 ...projection,
168 turnItems: [{ ...item, failure: { ...item.failure, class: "provider_error" as const } }],
169 }),
170 ).toMatchObject({ status: "queued", lastErrorClass: null });
171 });
172​
173 it("keeps live activity attached to an executing run when a newer run is queued", () => {
174 const runningRun = run("run-running", 1, "running");
175 const queuedRun = run("run-queued", 2, "queued");
176 const projection = { ...v2Projection, runs: [queuedRun, runningRun], updatedAt: now };
177​
178 expect(deriveLatestThreadRun(projection)?.runId).toBe(queuedRun.id);
179 expect(deriveThreadActivityRun(projection)).toMatchObject({
180 runId: runningRun.id,
181 status: "running",
182 });
183​
184 const runtime = deriveThreadRuntime(projection);
185 expect(runtime).toMatchObject({
186 status: "running",
187 activeRunId: runningRun.id,
188 });
189 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
190 });
191​
192 it("marks an open run whose provider turn already ended", () => {
193 const attemptId = RunAttemptId.make("attempt-open");
194 const openRun = { ...run("run-open", 1, "running"), activeAttemptId: attemptId };
195 const providerTurn = (status: "running" | "failed") => ({
196 id: ProviderTurnId.make("provider-turn-open"),
197 providerThreadId: ProviderThreadId.make("provider-thread-open"),
198 nodeId: NodeId.make("node-open"),
199 runAttemptId: attemptId,
200 nativeTurnRef: null,
201 ordinal: 1,
202 status,
203 startedAt: now,
204 completedAt: status === "running" ? null : now,
205 });
206 const projection = (status: "running" | "failed") => ({
207 ...v2Projection,
208 runs: [openRun],
209 providerTurns: [providerTurn(status)],
210 updatedAt: now,
211 });
212​
213 expect(deriveThreadActivityRun(projection("running"))?.turnEnded).toBeUndefined();
214 expect(deriveThreadActivityRun(projection("failed"))).toMatchObject({
215 status: "running",
216 turnEnded: true,
217 });
218 });
219​
220 it("presents a held queue as the stopped run instead of queued work", () => {
221 const interrupted = { ...run("run-interrupted", 1, "interrupted"), completedAt: now };
222 const held = { ...run("run-held", 2, "queued"), queueHeld: true };
223 const projection = { ...v2Projection, runs: [interrupted, held], updatedAt: now };
224​
225 expect(deriveLatestThreadRun(projection)?.runId).toBe(interrupted.id);
226 expect(deriveThreadActivityRun(projection)?.runId).toBe(interrupted.id);
227 expect(deriveThreadRuntime(projection)).toMatchObject({
228 status: "interrupted",
229 activeRunId: null,
230 });
231​
232 const resumed = { ...projection, runs: [interrupted, { ...held, queueHeld: false }] };
233 expect(deriveThreadRuntime(resumed)).toMatchObject({ status: "queued" });
234​
235 const onlyHeld = {
236 ...projection,
237 runs: [held],
238 thread: { ...projection.thread, activeProviderThreadId: null },
239 };
240 expect(deriveLatestThreadRun(onlyHeld)).toBeNull();
241 expect(deriveThreadRuntime(onlyHeld)).toBeNull();
242 });
243​
244 it("does not expose a queued-only run as interruptible", () => {
245 const queuedRun = run("run-queued", 1, "queued");
246 const projection = { ...v2Projection, runs: [queuedRun], updatedAt: now };
247​
248 expect(deriveThreadActivityRun(projection)).toMatchObject({
249 runId: queuedRun.id,
250 status: "queued",
251 });
252​
253 const runtime = deriveThreadRuntime(projection);
254 expect(runtime).toMatchObject({
255 status: "queued",
256 activeRunId: null,
257 });
258 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
259 });
260});
261​
262describe("deriveRunlessWorkStartedAt", () => {
263 const later = DateTime.makeUnsafe("2026-07-28T10:05:00.000Z");
264 const rootTurn = (
265 status: OrchestrationV2ExecutionNode["status"],
266 startedAt = now,
267 ): OrchestrationV2ExecutionNode => ({
268 id: NodeId.make("child-root"),
269 threadId: v2Projection.thread.id,
270 runId: null,
271 parentNodeId: null,
272 rootNodeId: NodeId.make("child-root"),
273 kind: "root_turn",
274 status,
275 countsForRun: false,
276 providerThreadId: null,
277 providerTurnId: null,
278 nativeItemRef: null,
279 runtimeRequestId: null,
280 checkpointScopeId: null,
281 startedAt,
282 completedAt: null,
283 });
284​
285 const nativeChild = {
286 ...v2Projection,
287 thread: {
288 ...v2Projection.thread,
289 creationSource: "provider" as const,
290 lineage: {
291 parentThreadId: ThreadId.make("parent"),
292 relationshipToParent: "subagent" as const,
293 rootThreadId: ThreadId.make("parent"),
294 },
295 },
296 };
297​
298 it("times a provider-native subagent from its runless root turn while it works", () => {
299 const projection = { ...nativeChild, nodes: [rootTurn("running", later)] };
300 expect(deriveRunlessWorkStartedAt(projection)).toBe("2026-07-28T10:05:00.000Z");
301 expect(deriveThreadRuntime(projection)).toBeNull();
302 });
303​
304 it("ignores root turns that belong to a run, and threads the provider does not run", () => {
305 const owned = { ...rootTurn("running"), runId: RunId.make("run-1") };
306 expect(deriveRunlessWorkStartedAt({ ...nativeChild, nodes: [owned] })).toBeNull();
307 expect(
308 deriveRunlessWorkStartedAt({ ...v2Projection, nodes: [rootTurn("running")] }),
309 ).toBeNull();
310 });
311});
312​
313describe("deriveProviderSubagentStatus", () => {
314 const root = {
315 id: NodeId.make("child-root"),
316 threadId: v2Projection.thread.id,
317 runId: null,
318 parentNodeId: null,
319 rootNodeId: NodeId.make("child-root"),
320 kind: "root_turn" as const,
321 status: "completed" as const,
322 countsForRun: false,
323 providerThreadId: null,
324 providerTurnId: null,
325 nativeItemRef: null,
326 runtimeRequestId: null,
327 checkpointScopeId: null,
328 startedAt: now,
329 completedAt: now,
330 };
331 const child = (creationSource: "provider" | "mcp") => ({
332 ...v2Projection,
333 thread: {
334 ...v2Projection.thread,
335 creationSource,
336 lineage: {
337 parentThreadId: ThreadId.make("parent"),
338 relationshipToParent: "subagent" as const,
339 rootThreadId: ThreadId.make("parent"),
340 },
341 },
342 nodes: [root],
343 });
344​
345 it("leaves T3 delegated tasks and ordinary threads alone", () => {
346 expect(deriveProviderSubagentStatus(child("mcp"))).toBeNull();
347 expect(deriveProviderSubagentStatus({ ...v2Projection, nodes: [root] })).toBeNull();
348 });
349});
350​
351describe("provider-reported model selection", () => {
352 const selected = v2Projection.thread.modelSelection;
353 const reported = { ...selected, options: [{ id: "reasoningEffort", value: "default" }] };
354 const providerThread = {
355 id: ProviderThreadId.make("active"),
356 driver: ProviderDriverKind.make("codex"),
357 providerInstanceId: selected.instanceId,
358 providerSessionId: ProviderSessionId.make("session"),
359 appThreadId: v2Projection.thread.id,
360 ownerNodeId: null,
361 nativeThreadRef: null,
362 nativeConversationHeadRef: null,
363 status: "idle" as const,
364 firstRunOrdinal: null,
365 lastRunOrdinal: null,
366 handoffIds: [],
367 forkedFrom: null,
368 createdAt: now,
369 updatedAt: now,
370 nativeMetadata: { modelSelection: reported },
371 };
372 const projection = {
373 ...v2Projection,
374 thread: { ...v2Projection.thread, activeProviderThreadId: providerThread.id },
375 providerThreads: [providerThread],
376 };
377​
378 it("reads only the active provider thread's reported selection", () => {
379 expect(deriveReportedModelSelection(projection)).toBe(reported);
380 expect(
381 deriveReportedModelSelection({
382 ...projection,
383 thread: { ...projection.thread, activeProviderThreadId: null },
384 }),
385 ).toBeNull();
386 expect(
387 deriveReportedModelSelection({
388 ...projection,
389 providerThreads: [
390 { ...providerThread, providerInstanceId: ProviderInstanceId.make("other") },
391 ],
392 }),
393 ).toBeNull();
394 });
395});