Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 NodeId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 RunId,
7 ThreadId,
8 type OrchestrationV2Subagent,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11
12import { deriveThreadTurnSubagents, resolveSubagentPillSegment } from "./threadSubagents.ts";
13
14const at = (iso: string) => DateTime.makeUnsafe(iso);
15
16function subagent(
17 overrides: Omit<Partial<OrchestrationV2Subagent>, "id"> & { readonly id: string },
18): OrchestrationV2Subagent {
19 const { id, ...rest } = overrides;
20 return {
21 id: NodeId.make(id),
22 threadId: ThreadId.make("thread-1"),
23 runId: RunId.make("run-1"),
24 parentNodeId: NodeId.make("node-1"),
25 origin: "app_owned",
26 createdBy: "agent",
27 driver: ProviderDriverKind.make("codex"),
28 providerInstanceId: ProviderInstanceId.make("codex"),
29 providerThreadId: null,
30 childThreadId: ThreadId.make("thread-child"),
31 nativeTaskRef: null,
32 prompt: "Do the thing",
33 title: "Worker",
34 model: "gpt-5.4",
35 status: "running",
36 result: null,
37 startedAt: at("2026-06-20T00:00:00.000Z"),
38 completedAt: null,
39 updatedAt: at("2026-06-20T00:00:01.000Z"),
40 ...rest,
41 };
42}
43
44const run = (
45 id: string,
46 status: "queued" | "running" | "completed" | "cancelled" | "rolled_back",
47 overrides: { readonly ordinal?: number; readonly started?: boolean } = {},
48) =>
49 ({
50 id: RunId.make(id),
51 status,
52 ordinal: overrides.ordinal ?? Number(id.slice("run-".length)),
53 startedAt: overrides.started === false ? null : at("2026-06-20T00:00:00.000Z"),
54 }) as never;
55const activeRun = run("run-1", "running");
56const finishedRun = run("run-1", "completed");
57
58describe("deriveThreadTurnSubagents", () => {
59 it("scopes the roster to the active run and orders it by start time", () => {
60 const turn = deriveThreadTurnSubagents({
61 runs: [run("run-0", "completed"), activeRun],
62 subagents: [
63 subagent({ id: "b", startedAt: at("2026-06-20T00:00:05.000Z") }),
64 subagent({ id: "old", runId: RunId.make("run-0"), status: "completed" }),
65 subagent({ id: "a", startedAt: at("2026-06-20T00:00:02.000Z") }),
66 ],
67 });
68
69 expect(turn?.runId).toBe("run-1");
70 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["a", "b"]);
71 expect(turn?.turnActive).toBe(true);
72 });
73
74 it("falls back to the most recently updated agent's run once the turn settles", () => {
75 const turn = deriveThreadTurnSubagents({
76 runs: [finishedRun, run("run-2", "completed")],
77 subagents: [
78 subagent({ id: "old", status: "completed", updatedAt: at("2026-06-20T00:00:01.000Z") }),
79 subagent({
80 id: "recent",
81 runId: RunId.make("run-2"),
82 status: "completed",
83 updatedAt: at("2026-06-20T00:00:09.000Z"),
84 }),
85 ],
86 });
87
88 expect(turn?.runId).toBe("run-2");
89 expect(turn?.turnActive).toBe(false);
90 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["recent"]);
91 });
92
93 it("lists every live agent from earlier turns once no turn is running", () => {
94 const turn = deriveThreadTurnSubagents({
95 runs: ["run-103", "run-105", "run-107", "run-109"].map((id) => run(id, "completed")),
96 subagents: [
97 subagent({
98 id: "settled-103",
99 runId: RunId.make("run-103"),
100 status: "completed",
101 startedAt: at("2026-06-20T00:00:00.000Z"),
102 updatedAt: at("2026-06-20T00:00:04.000Z"),
103 }),
104 subagent({
105 id: "bg-103",
106 runId: RunId.make("run-103"),
107 startedAt: at("2026-06-20T00:00:01.000Z"),
108 updatedAt: at("2026-06-20T00:00:05.000Z"),
109 }),
110 subagent({
111 id: "bg-105",
112 runId: RunId.make("run-105"),
113 startedAt: at("2026-06-20T00:00:02.000Z"),
114 updatedAt: at("2026-06-20T00:00:06.000Z"),
115 }),
116 subagent({
117 id: "bg-107",
118 runId: RunId.make("run-107"),
119 startedAt: at("2026-06-20T00:00:03.000Z"),
120 updatedAt: at("2026-06-20T00:00:07.000Z"),
121 }),
122 ],
123 });
124
125 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["bg-103", "bg-105", "bg-107"]);
126 expect(resolveSubagentPillSegment(turn)?.label).toBe("3/3");
127 });
128
129 it("keeps the active turn's roster and adds a live agent from an earlier turn", () => {
130 const turn = deriveThreadTurnSubagents({
131 runs: [run("run-0", "completed"), activeRun],
132 subagents: [
133 subagent({ id: "background", runId: RunId.make("run-0") }),
134 subagent({ id: "earlier-done", runId: RunId.make("run-0"), status: "completed" }),
135 subagent({ id: "working", startedAt: at("2026-06-20T00:00:05.000Z") }),
136 subagent({
137 id: "done",
138 status: "completed",
139 startedAt: at("2026-06-20T00:00:06.000Z"),
140 }),
141 ],
142 });
143
144 expect(turn?.runId).toBe("run-1");
145 expect(turn?.turnActive).toBe(true);
146 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["background", "working", "done"]);
147 expect(resolveSubagentPillSegment(turn)?.label).toBe("2/3");
148 });
149
150 it("leaves out a live agent whose turn was rolled back", () => {
151 const turn = deriveThreadTurnSubagents({
152 runs: [run("run-0", "rolled_back"), finishedRun],
153 subagents: [
154 subagent({ id: "rolled-back", runId: RunId.make("run-0") }),
155 subagent({ id: "live", updatedAt: at("2026-06-20T00:00:09.000Z") }),
156 ],
157 });
158
159 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["live"]);
160 });
161
162 it("lists a workflow coordinator's members, which carry no run", () => {
163 const turn = deriveThreadTurnSubagents({
164 runs: [],
165 subagents: [
166 subagent({ id: "member-0", runId: null, startedAt: at("2026-06-20T00:00:01.000Z") }),
167 subagent({
168 id: "member-1",
169 runId: null,
170 status: "completed",
171 startedAt: at("2026-06-20T00:00:02.000Z"),
172 }),
173 ],
174 });
175
176 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["member-0", "member-1"]);
177 expect(resolveSubagentPillSegment(turn)?.label).toBe("1/2");
178 });
179
180 it("keeps the finished agents reachable while their turn is the latest", () => {
181 const turn = deriveThreadTurnSubagents({
182 runs: [finishedRun],
183 subagents: [
184 subagent({ id: "a", status: "completed" }),
185 subagent({ id: "b", status: "failed" }),
186 ],
187 });
188
189 expect(resolveSubagentPillSegment(turn)?.label).toBe("2 done");
190 });
191
192 it("drops finished agents from the pill once a later turn ran without any", () => {
193 const turn = deriveThreadTurnSubagents({
194 runs: [finishedRun, run("run-2", "completed")],
195 subagents: [subagent({ id: "a", status: "completed" })],
196 });
197
198 expect(resolveSubagentPillSegment(turn)).toBeNull();
199 });
200
201 it("shows finished agents again when the later turn was rolled back", () => {
202 const turn = deriveThreadTurnSubagents({
203 runs: [finishedRun, run("run-2", "rolled_back")],
204 subagents: [subagent({ id: "a", status: "completed" })],
205 });
206
207 expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
208 });
209
210 it("keeps finished agents while a later turn is only queued or was cancelled before starting", () => {
211 for (const later of [
212 run("run-2", "queued", { started: false }),
213 run("run-2", "cancelled", { started: false }),
214 ]) {
215 const turn = deriveThreadTurnSubagents({
216 runs: [finishedRun, later],
217 subagents: [subagent({ id: "a", status: "completed" })],
218 });
219
220 expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
221 }
222 });
223
224 it("finds the latest turn by ordinal when a reordered queue left runs out of order", () => {
225 const turn = deriveThreadTurnSubagents({
226 runs: [run("run-1", "completed", { ordinal: 3 }), run("run-2", "completed", { ordinal: 2 })],
227 subagents: [subagent({ id: "a", status: "completed" })],
228 });
229
230 expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
231 });
232
233 it("keeps the latest turn's agents when an earlier turn's background agent finishes later", () => {
234 const turn = deriveThreadTurnSubagents({
235 runs: [finishedRun, run("run-2", "completed")],
236 subagents: [
237 subagent({ id: "late", status: "completed", updatedAt: at("2026-06-20T00:00:09.000Z") }),
238 subagent({
239 id: "latest",
240 runId: RunId.make("run-2"),
241 status: "completed",
242 updatedAt: at("2026-06-20T00:00:05.000Z"),
243 }),
244 ],
245 });
246
247 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["latest"]);
248 expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
249 });
250
251 it("counts only terminal agents as done while a held agent remains", () => {
252 const turn = deriveThreadTurnSubagents({
253 runs: [finishedRun],
254 subagents: [
255 subagent({ id: "a", status: "completed" }),
256 subagent({ id: "b", status: "idle" }),
257 ],
258 });
259
260 expect(resolveSubagentPillSegment(turn)?.label).toBe("1 done");
261 });
262
263 it("counts idle as neither working nor done", () => {
264 const turn = deriveThreadTurnSubagents({
265 runs: [activeRun],
266 subagents: [
267 subagent({ id: "a", status: "running" }),
268 subagent({ id: "b", status: "idle" }),
269 subagent({ id: "c", status: "failed" }),
270 ],
271 });
272
273 expect(turn?.liveCount).toBe(1);
274 expect(turn?.settledCount).toBe(1);
275 expect(turn?.subagents).toHaveLength(3);
276 });
277
278 it("keeps settled workflows reachable without counting idle agents as done", () => {
279 const turn = deriveThreadTurnSubagents({
280 runs: [finishedRun, run("run-2", "completed")],
281 subagents: [
282 subagent({ id: "workflow", status: "completed", workflow: { phases: [], agents: [] } }),
283 subagent({
284 id: "idle",
285 runId: RunId.make("run-2"),
286 status: "idle",
287 updatedAt: at("2026-06-20T00:00:09.000Z"),
288 }),
289 ],
290 });
291
292 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["idle", "workflow"]);
293 expect(resolveSubagentPillSegment(turn)).toEqual({
294 label: "1 done",
295 accessibilityLabel: "1 agent done",
296 });
297 });
298});