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

threadExecution.test.ts

146 lines4.8 KB

1import { MessageId, RunId, type OrchestrationV2RunStatus } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import { describe, expect, it } from "vite-plus/test";
4​
5import { v2Projection } from "./orchestrationV2TestFixtures.ts";
6import {
7 deriveLatestThreadRun,
8 deriveThreadActivityRun,
9 deriveThreadRuntime,
10 threadRuntimeHasInterruptibleRun,
11} from "./threadExecution.ts";
12import { threadRuntimeCanArchive, type ThreadRuntimeSummary } from "./models.ts";
13​
14const now = DateTime.makeUnsafe("2026-07-28T10:00:00.000Z");
15​
16function run(id: string, ordinal: number, status: OrchestrationV2RunStatus) {
17 return {
18 id: RunId.make(id),
19 threadId: v2Projection.thread.id,
20 ordinal,
21 providerInstanceId: v2Projection.thread.providerInstanceId,
22 modelSelection: v2Projection.thread.modelSelection,
23 providerThreadId: null,
24 userMessageId: MessageId.make(`message-${id}`),
25 rootNodeId: null,
26 activeAttemptId: null,
27 status,
28 requestedAt: now,
29 startedAt: status === "queued" ? null : now,
30 completedAt: null,
31 checkpointId: null,
32 contextHandoffId: null,
33 };
35​
36describe("thread execution presentation", () => {
37 it("keeps live activity attached to an executing run when a newer run is queued", () => {
38 const runningRun = run("run-running", 1, "running");
39 const queuedRun = run("run-queued", 2, "queued");
40 const projection = { ...v2Projection, runs: [queuedRun, runningRun], updatedAt: now };
41​
42 expect(deriveLatestThreadRun(projection)?.runId).toBe(queuedRun.id);
43 expect(deriveThreadActivityRun(projection)).toMatchObject({
44 runId: runningRun.id,
45 status: "running",
46 });
47​
48 const runtime = deriveThreadRuntime(projection);
49 expect(runtime).toMatchObject({
50 status: "running",
51 activeRunId: runningRun.id,
52 });
53 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
54 });
55​
56 it("does not expose a queued-only run as interruptible", () => {
57 const queuedRun = run("run-queued", 1, "queued");
58 const projection = { ...v2Projection, runs: [queuedRun], updatedAt: now };
59​
60 expect(deriveThreadActivityRun(projection)).toMatchObject({
61 runId: queuedRun.id,
62 status: "queued",
63 });
64​
65 const runtime = deriveThreadRuntime(projection);
66 expect(runtime).toMatchObject({
67 status: "queued",
68 activeRunId: null,
69 });
70 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
71 });
72​
73 it("keeps checkpoint-wait activity visible without exposing a non-functional interrupt", () => {
74 const waitingRun = run("run-waiting", 1, "waiting");
75 const projection = { ...v2Projection, runs: [waitingRun], updatedAt: now };
76​
77 expect(deriveThreadActivityRun(projection)).toMatchObject({
78 runId: waitingRun.id,
79 status: "waiting",
80 });
81​
82 const runtime = deriveThreadRuntime(projection);
83 expect(runtime).toMatchObject({
84 status: "waiting",
85 activeRunId: null,
86 });
87 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
88 });
89​
90 it("does not expose a stale active run after the runtime parks at idle", () => {
91 const runtime = {
92 status: "idle" as const,
93 activeRunId: RunId.make("run-stale"),
94 providerInstanceId: v2Projection.thread.providerInstanceId,
95 providerName: null,
96 lastError: null,
97 updatedAt: DateTime.formatIso(now),
98 };
99​
100 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false);
101 });
102​
103 it.each(["preparing", "starting"] as const)("keeps an active %s run interruptible", (status) => {
104 const runtime = {
105 status,
106 activeRunId: RunId.make(`run-${status}`),
107 providerInstanceId: v2Projection.thread.providerInstanceId,
108 providerName: null,
109 lastError: null,
110 updatedAt: DateTime.formatIso(now),
111 };
112​
113 expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true);
114 });
115});
116​
117describe("threadRuntimeCanArchive", () => {
118 const runtime = (
119 status: ThreadRuntimeSummary["status"],
120 activeRunId: ThreadRuntimeSummary["activeRunId"],
121 ): ThreadRuntimeSummary => ({
122 status,
123 activeRunId,
124 providerInstanceId: v2Projection.thread.providerInstanceId,
125 providerName: null,
126 lastError: null,
127 updatedAt: DateTime.formatIso(now),
128 });
129​
130 it.each(["preparing", "starting", "running"] as const)(
131 "blocks archive while a provider is %s",
132 (status) => {
133 expect(threadRuntimeCanArchive(runtime(status, RunId.make(`run-${status}`)))).toBe(false);
134 },
135 );
136​
137 it("only blocks a queued runtime when a provider run remains attached", () => {
138 expect(threadRuntimeCanArchive(runtime("queued", RunId.make("run-queued")))).toBe(false);
139 expect(threadRuntimeCanArchive(runtime("queued", null))).toBe(true);
140 });
141​
142 it("allows waiting and idle threads", () => {
143 expect(threadRuntimeCanArchive(runtime("waiting", RunId.make("run-finished")))).toBe(true);
144 expect(threadRuntimeCanArchive(runtime("idle", null))).toBe(true);
145 });
146});