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
16const workflow = (
17 overrides: Partial<NonNullable<OrchestrationV2Subagent["workflow"]>> = {},
18): NonNullable<OrchestrationV2Subagent["workflow"]> => ({
19 kind: "workflow",
20 workflowName: "audit_auth_flow",
21 phases: [],
22 phaseIndex: null,
23 phaseTitle: null,
24 agentIndex: null,
25 runId: "wf-run-1",
26 ...overrides,
27});
28
29function subagent(
30 overrides: Omit<Partial<OrchestrationV2Subagent>, "id"> & { readonly id: string },
31): OrchestrationV2Subagent {
32 const { id, ...rest } = overrides;
33 return {
34 id: NodeId.make(id),
35 threadId: ThreadId.make("thread-1"),
36 runId: RunId.make("run-1"),
37 parentNodeId: NodeId.make("node-1"),
38 origin: "app_owned",
39 createdBy: "agent",
40 driver: ProviderDriverKind.make("codex"),
41 providerInstanceId: ProviderInstanceId.make("codex"),
42 providerThreadId: null,
43 childThreadId: ThreadId.make("thread-child"),
44 nativeTaskRef: null,
45 prompt: "Do the thing",
46 title: "Worker",
47 model: "gpt-5.4",
48 status: "running",
49 result: null,
50 startedAt: at("2026-06-20T00:00:00.000Z"),
51 completedAt: null,
52 updatedAt: at("2026-06-20T00:00:01.000Z"),
53 ...rest,
54 };
55}
56
57const run = (id: string, status: "running" | "completed") =>
58 ({ id: RunId.make(id), status }) as never;
59const activeRun = run("run-1", "running");
60const finishedRun = run("run-1", "completed");
61
62describe("deriveThreadTurnSubagents", () => {
63 it("returns null when the thread has never spawned an agent", () => {
64 expect(deriveThreadTurnSubagents({ runs: [activeRun], subagents: [] })).toBeNull();
65 });
66
67 it("scopes the roster to the active run and orders it by start time", () => {
68 const turn = deriveThreadTurnSubagents({
69 runs: [run("run-0", "completed"), activeRun],
70 subagents: [
71 subagent({ id: "b", startedAt: at("2026-06-20T00:00:05.000Z") }),
72 subagent({ id: "old", runId: RunId.make("run-0") }),
73 subagent({ id: "a", startedAt: at("2026-06-20T00:00:02.000Z") }),
74 ],
75 });
76
77 expect(turn?.runId).toBe("run-1");
78 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["a", "b"]);
79 expect(turn?.turnActive).toBe(true);
80 });
81
82 it("falls back to the most recently updated agent's run once the turn settles", () => {
83 const turn = deriveThreadTurnSubagents({
84 runs: [finishedRun, run("run-2", "completed")],
85 subagents: [
86 subagent({ id: "old", status: "completed", updatedAt: at("2026-06-20T00:00:01.000Z") }),
87 subagent({
88 id: "recent",
89 runId: RunId.make("run-2"),
90 status: "completed",
91 updatedAt: at("2026-06-20T00:00:09.000Z"),
92 }),
93 ],
94 });
95
96 expect(turn?.runId).toBe("run-2");
97 expect(turn?.turnActive).toBe(false);
98 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["recent"]);
99 });
100
101 it("counts idle as neither working nor done", () => {
102 const turn = deriveThreadTurnSubagents({
103 runs: [activeRun],
104 subagents: [
105 subagent({ id: "a", status: "running" }),
106 subagent({ id: "b", status: "idle" }),
107 subagent({ id: "c", status: "failed" }),
108 ],
109 });
110
111 expect(turn?.liveCount).toBe(1);
112 expect(turn?.settledCount).toBe(1);
113 expect(turn?.subagents).toHaveLength(3);
114 });
115
116 it("ignores workflow rows even though they carry no run id", () => {
117 const turn = deriveThreadTurnSubagents({
118 runs: [finishedRun],
119 subagents: [
120 subagent({ id: "a", status: "completed" }),
121 // The workflow sweep updates these rows on a heartbeat, so the newest
122 // row in the thread is often the one with no run id.
123 subagent({
124 id: "wf",
125 runId: null,
126 status: "running",
127 workflow: workflow(),
128 updatedAt: at("2026-06-20T00:00:09.000Z"),
129 }),
130 ],
131 });
132
133 // The workflow row must not hijack the settled turn's roster.
134 expect(turn?.runId).toBe("run-1");
135 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["a"]);
136 });
137
138 it("keeps a runless subagent that is not a workflow in the roster", () => {
139 const turn = deriveThreadTurnSubagents({
140 runs: [finishedRun],
141 subagents: [
142 subagent({
143 id: "runless",
144 runId: null,
145 status: "completed",
146 updatedAt: at("2026-06-20T00:00:05.000Z"),
147 }),
148 ],
149 });
150
151 expect(turn?.runId).toBeNull();
152 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["runless"]);
153 });
154
155 it("scopes a runless subagent out of a live run's roster", () => {
156 const turn = deriveThreadTurnSubagents({
157 runs: [activeRun],
158 subagents: [
159 subagent({ id: "active" }),
160 subagent({
161 id: "runless",
162 runId: null,
163 updatedAt: at("2026-06-20T00:00:09.000Z"),
164 }),
165 ],
166 });
167
168 expect(turn?.runId).toBe("run-1");
169 expect(turn?.subagents.map((entry) => entry.id)).toEqual(["active"]);
170 });
171
172 it("returns null when the thread only ever ran a workflow", () => {
173 expect(
174 deriveThreadTurnSubagents({
175 runs: [finishedRun],
176 subagents: [subagent({ id: "wf", runId: null, status: "completed", workflow: workflow() })],
177 }),
178 ).toBeNull();
179 });
180});
181
182describe("resolveSubagentPillSegment", () => {
183 it("shows how many of the turn's agents are still working", () => {
184 const turn = deriveThreadTurnSubagents({
185 runs: [activeRun],
186 subagents: [
187 subagent({ id: "a", status: "running" }),
188 subagent({ id: "b", status: "waiting" }),
189 subagent({ id: "c", status: "completed" }),
190 ],
191 });
192
193 expect(resolveSubagentPillSegment(turn)).toEqual({
194 label: "2/3",
195 accessibilityLabel: "2 of 3 agents working",
196 });
197 });
198
199 it("reports the finished roster while the turn is still running", () => {
200 const turn = deriveThreadTurnSubagents({
201 runs: [activeRun],
202 subagents: [subagent({ id: "a", status: "completed" })],
203 });
204
205 expect(resolveSubagentPillSegment(turn)).toEqual({
206 label: "1 done",
207 accessibilityLabel: "1 agent done",
208 });
209 });
210
211 it("hides itself once the turn is over and nothing is working", () => {
212 const turn = deriveThreadTurnSubagents({
213 runs: [finishedRun],
214 subagents: [subagent({ id: "a", status: "completed" })],
215 });
216
217 expect(resolveSubagentPillSegment(turn)).toBeNull();
218 expect(resolveSubagentPillSegment(null)).toBeNull();
219 });
220
221 it("stays visible after the turn ends while an agent is still working", () => {
222 const turn = deriveThreadTurnSubagents({
223 runs: [finishedRun],
224 subagents: [subagent({ id: "a", status: "running" })],
225 });
226
227 expect(resolveSubagentPillSegment(turn)?.label).toBe("1/1");
228 });
229});