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 = (id: string, status: "running" | "completed" | "rolled_back") =>
45 ({ id: RunId.make(id), status }) as never;
46const activeRun = run("run-1", "running");
47const finishedRun = run("run-1", "completed");
48
49describe("deriveThreadTurnSubagents", () => {
50 it("returns null when the thread has never spawned an agent", () => {
51 expect(deriveThreadTurnSubagents({ runs: [activeRun], subagents: [] })).toBeNull();
52 });
53
54 it("scopes the roster to the active run and orders it by start time", () => {
55 const turn = deriveThreadTurnSubagents({
56 runs: [run("run-0", "completed"), activeRun],
57 subagents: [
58 subagent({ id: "b", startedAt: at("2026-06-20T00:00:05.000Z") }),
59 subagent({ id: "old", runId: RunId.make("run-0"), status: "completed" }),
60 subagent({ id: "a", startedAt: at("2026-06-20T00:00:02.000Z") }),
61 ],
62 });
63
64 expect(turn?.runId).toBe("run-1");
65 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["a", "b"]);
66 expect(turn?.turnActive).toBe(true);
67 });
68
69 it("falls back to the most recently updated agent's run once the turn settles", () => {
70 const turn = deriveThreadTurnSubagents({
71 runs: [finishedRun, run("run-2", "completed")],
72 subagents: [
73 subagent({ id: "old", status: "completed", updatedAt: at("2026-06-20T00:00:01.000Z") }),
74 subagent({
75 id: "recent",
76 runId: RunId.make("run-2"),
77 status: "completed",
78 updatedAt: at("2026-06-20T00:00:09.000Z"),
79 }),
80 ],
81 });
82
83 expect(turn?.runId).toBe("run-2");
84 expect(turn?.turnActive).toBe(false);
85 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["recent"]);
86 });
87
88 it("lists every live agent from earlier turns once no turn is running", () => {
89 const turn = deriveThreadTurnSubagents({
90 runs: ["run-103", "run-105", "run-107", "run-109"].map((id) => run(id, "completed")),
91 subagents: [
92 subagent({
93 id: "settled-103",
94 runId: RunId.make("run-103"),
95 status: "completed",
96 startedAt: at("2026-06-20T00:00:00.000Z"),
97 updatedAt: at("2026-06-20T00:00:04.000Z"),
98 }),
99 subagent({
100 id: "bg-103",
101 runId: RunId.make("run-103"),
102 startedAt: at("2026-06-20T00:00:01.000Z"),
103 updatedAt: at("2026-06-20T00:00:05.000Z"),
104 }),
105 subagent({
106 id: "bg-105",
107 runId: RunId.make("run-105"),
108 startedAt: at("2026-06-20T00:00:02.000Z"),
109 updatedAt: at("2026-06-20T00:00:06.000Z"),
110 }),
111 subagent({
112 id: "bg-107",
113 runId: RunId.make("run-107"),
114 startedAt: at("2026-06-20T00:00:03.000Z"),
115 updatedAt: at("2026-06-20T00:00:07.000Z"),
116 }),
117 ],
118 });
119
120 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["bg-103", "bg-105", "bg-107"]);
121 expect(resolveSubagentPillSegment(turn)?.label).toBe("3/3");
122 });
123
124 it("keeps the active turn's roster and adds a live agent from an earlier turn", () => {
125 const turn = deriveThreadTurnSubagents({
126 runs: [run("run-0", "completed"), activeRun],
127 subagents: [
128 subagent({ id: "background", runId: RunId.make("run-0") }),
129 subagent({ id: "earlier-done", runId: RunId.make("run-0"), status: "completed" }),
130 subagent({ id: "working", startedAt: at("2026-06-20T00:00:05.000Z") }),
131 subagent({
132 id: "done",
133 status: "completed",
134 startedAt: at("2026-06-20T00:00:06.000Z"),
135 }),
136 ],
137 });
138
139 expect(turn?.runId).toBe("run-1");
140 expect(turn?.turnActive).toBe(true);
141 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["background", "working", "done"]);
142 expect(resolveSubagentPillSegment(turn)?.label).toBe("2/3");
143 });
144
145 it("leaves out a live agent whose turn was rolled back", () => {
146 const turn = deriveThreadTurnSubagents({
147 runs: [run("run-0", "rolled_back"), finishedRun],
148 subagents: [
149 subagent({ id: "rolled-back", runId: RunId.make("run-0") }),
150 subagent({ id: "live", updatedAt: at("2026-06-20T00:00:09.000Z") }),
151 ],
152 });
153
154 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["live"]);
155 });
156
157 it("does not pick a rolled-back turn when its agent was updated last", () => {
158 const turn = deriveThreadTurnSubagents({
159 runs: [run("run-0", "rolled_back"), finishedRun],
160 subagents: [
161 subagent({
162 id: "rolled-back",
163 runId: RunId.make("run-0"),
164 updatedAt: at("2026-06-20T00:00:09.000Z"),
165 }),
166 subagent({
167 id: "done",
168 status: "completed",
169 updatedAt: at("2026-06-20T00:00:05.000Z"),
170 }),
171 ],
172 });
173
174 expect(turn?.runId).toBe("run-1");
175 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["done"]);
176 expect(
177 deriveThreadTurnSubagents({
178 runs: [run("run-0", "rolled_back")],
179 subagents: [subagent({ id: "rolled-back", runId: RunId.make("run-0") })],
180 }),
181 ).toBeNull();
182 });
183
184 it("lists a workflow coordinator's members, which carry no run", () => {
185 const turn = deriveThreadTurnSubagents({
186 runs: [],
187 subagents: [
188 subagent({ id: "member-0", runId: null, startedAt: at("2026-06-20T00:00:01.000Z") }),
189 subagent({
190 id: "member-1",
191 runId: null,
192 status: "completed",
193 startedAt: at("2026-06-20T00:00:02.000Z"),
194 }),
195 ],
196 });
197
198 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["member-0", "member-1"]);
199 expect(resolveSubagentPillSegment(turn)?.label).toBe("1/2");
200 });
201
202 it("counts idle as neither working nor done", () => {
203 const turn = deriveThreadTurnSubagents({
204 runs: [activeRun],
205 subagents: [
206 subagent({ id: "a", status: "running" }),
207 subagent({ id: "b", status: "idle" }),
208 subagent({ id: "c", status: "failed" }),
209 ],
210 });
211
212 expect(turn?.liveCount).toBe(1);
213 expect(turn?.settledCount).toBe(1);
214 expect(turn?.subagents).toHaveLength(3);
215 });
216});
217
218describe("resolveSubagentPillSegment", () => {
219 it("shows how many of the turn's agents are still working", () => {
220 const turn = deriveThreadTurnSubagents({
221 runs: [activeRun],
222 subagents: [
223 subagent({ id: "a", status: "running" }),
224 subagent({ id: "b", status: "waiting" }),
225 subagent({ id: "c", status: "completed" }),
226 ],
227 });
228
229 expect(resolveSubagentPillSegment(turn)).toEqual({
230 label: "2/3",
231 accessibilityLabel: "2 of 3 agents working",
232 });
233 });
234
235 it("reports the finished roster while the turn is still running", () => {
236 const turn = deriveThreadTurnSubagents({
237 runs: [activeRun],
238 subagents: [subagent({ id: "a", status: "completed" })],
239 });
240
241 expect(resolveSubagentPillSegment(turn)).toEqual({
242 label: "1 done",
243 accessibilityLabel: "1 agent done",
244 });
245 });
246
247 it("hides itself once the turn is over and nothing is working", () => {
248 const turn = deriveThreadTurnSubagents({
249 runs: [finishedRun],
250 subagents: [subagent({ id: "a", status: "completed" })],
251 });
252
253 expect(resolveSubagentPillSegment(turn)).toBeNull();
254 expect(resolveSubagentPillSegment(null)).toBeNull();
255 });
256
257 it("stays visible after the turn ends while an agent is still working", () => {
258 const turn = deriveThreadTurnSubagents({
259 runs: [finishedRun],
260 subagents: [subagent({ id: "a", status: "running" })],
261 });
262
263 expect(resolveSubagentPillSegment(turn)?.label).toBe("1/1");
264 });
265});