Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import type { OrchestrationV2Subagent, OrchestrationV2SubagentWorkflow } from "@t3tools/contracts";
3import * as DateTime from "effect/DateTime";
4
5import { deriveWorkflowGroups, projectedSubagentsToRuntime } from "./subagentRuntime.ts";
6
7const at = (iso: string) => DateTime.makeUnsafe(iso);
8
9type ProjectedSubagent = Parameters<typeof projectedSubagentsToRuntime>[0][number];
10
11function subagent(overrides: Partial<ProjectedSubagent> = {}): ProjectedSubagent {
12 return {
13 id: "coordinator",
14 title: "probe",
15 prompt: "export const meta = …",
16 model: "claude-opus-5[1m]",
17 status: "running" satisfies OrchestrationV2Subagent["status"],
18 result: null,
19 startedAt: at("2026-08-01T10:00:00.000Z"),
20 completedAt: null,
21 updatedAt: at("2026-08-01T10:00:05.000Z"),
22 ...overrides,
23 };
24}
25
26const member = (
27 overrides: Partial<OrchestrationV2SubagentWorkflow["agents"][number]> = {},
28): OrchestrationV2SubagentWorkflow["agents"][number] => ({
29 index: 1,
30 label: "alpha:one",
31 state: "running",
32 phaseIndex: 1,
33 phaseTitle: "Alpha",
34 ...overrides,
35});
36
37const groupsOf = (subagents: ReadonlyArray<ProjectedSubagent>) =>
38 deriveWorkflowGroups(projectedSubagentsToRuntime(subagents));
39
40describe("projectedSubagentsToRuntime workflow expansion", () => {
41 it("leaves a plain subagent flat", () => {
42 const [agent, ...rest] = projectedSubagentsToRuntime([subagent()]);
43 expect(rest).toEqual([]);
44 expect(agent?.kind).toBe("subagent");
45 expect(agent?.phases).toEqual([]);
46 });
47
48 it("projects a coordinator plus one row per member", () => {
49 const rows = projectedSubagentsToRuntime([
50 subagent({
51 workflow: {
52 name: "probe-wf",
53 phases: [
54 { index: 1, title: "Alpha" },
55 { index: 2, title: "Beta" },
56 ],
57 agents: [member(), member({ index: 2, label: "alpha:two", state: "queued" })],
58 totalTokens: 1000,
59 },
60 }),
61 ]);
62 expect(rows.map((row) => row.kind)).toEqual(["workflow", "workflow_agent", "workflow_agent"]);
63 expect(rows[0]?.workflowName).toBe("probe-wf");
64 expect(rows[0]?.usage?.totalTokens).toBe(1000);
65 expect(rows[1]).toMatchObject({
66 id: "coordinator:agent:1",
67 title: "alpha:one",
68 parentAgentId: "coordinator",
69 phaseIndex: 1,
70 phaseTitle: "Alpha",
71 status: "running",
72 });
73 expect(rows[2]?.status).toBe("pending");
74 expect(rows[2]?.startedAt).toBeNull();
75 });
76
77 it("derives a settled member's end from its reported duration", () => {
78 const [, settled] = projectedSubagentsToRuntime([
79 subagent({
80 workflow: {
81 phases: [],
82 agents: [
83 member({
84 state: "completed",
85 startedAt: Date.UTC(2026, 7, 1, 10, 0, 0),
86 durationMs: 1500,
87 totalTokens: 71_141,
88 toolCalls: 3,
89 prompt: "Reply with exactly: A1",
90 result: "A1",
91 }),
92 ],
93 },
94 }),
95 ]);
96 expect(settled?.status).toBe("completed");
97 expect(settled?.completedAt).toBe("2026-08-01T10:00:01.500Z");
98 expect(settled?.result).toBe("A1");
99 expect(settled?.progress).toBe("Reply with exactly: A1");
100 expect(settled?.usage).toEqual({ totalTokens: 71_141, toolUses: 3, durationMs: 1500 });
101 });
102
103 it.each(["completed", "cancelled", "failed"] as const)(
104 "settles unfinished members when the coordinator is %s",
105 (status) => {
106 const groups = groupsOf([
107 subagent({
108 status,
109 completedAt: at("2026-08-01T10:00:09.000Z"),
110 workflow: {
111 phases: [],
112 agents: [
113 member(),
114 member({ index: 2, state: "queued" }),
115 member({ index: 3, state: "completed" }),
116 ],
117 },
118 }),
119 ]);
120 expect(groups[0]?.workflow.status).toBe(status);
121 const members = groups[0]?.phases.flatMap((phase) => phase.members);
122 expect(members?.map((agent) => agent.status)).toEqual([status, status, "completed"]);
123 expect(members?.[0]?.completedAt).toBe("2026-08-01T10:00:09.000Z");
124 },
125 );
126
127 it("surfaces a retried member through the row's activation count", () => {
128 const [, retried] = projectedSubagentsToRuntime([
129 subagent({ workflow: { phases: [], agents: [member({ attempt: 3 })] } }),
130 ]);
131 expect(retried?.activationCount).toBe(3);
132 expect(retried?.attempt).toBe(3);
133 });
134});
135
136describe("deriveWorkflowGroups over a dynamic workflow", () => {
137 it("groups members under their phases and counts the run", () => {
138 const groups = groupsOf([
139 subagent({
140 workflow: {
141 name: "probe-wf",
142 phases: [
143 { index: 1, title: "Alpha" },
144 { index: 2, title: "Beta" },
145 ],
146 agents: [
147 member({ state: "completed" }),
148 member({ index: 2, label: "alpha:two", state: "failed" }),
149 member({ index: 3, label: "beta:one", phaseIndex: 2, phaseTitle: "Beta" }),
150 member({
151 index: 4,
152 label: "beta:two",
153 phaseIndex: 2,
154 phaseTitle: "Beta",
155 state: "queued",
156 }),
157 ],
158 },
159 }),
160 ]);
161 const group = groups[0];
162 expect(group?.phases.map((phase) => [phase.title, phase.state])).toEqual([
163 ["Alpha", "done"],
164 ["Beta", "running"],
165 ]);
166 expect(group?.phases.map((phase) => phase.settledCount)).toEqual([2, 0]);
167 expect(group?.phases.flatMap((phase) => phase.members)).toHaveLength(4);
168 expect(group?.unphasedMembers).toEqual([]);
169 });
170
171 it("shows a phase the script only reached at runtime", () => {
172 const groups = groupsOf([
173 subagent({
174 workflow: {
175 phases: [{ index: 1, title: "Alpha" }],
176 agents: [
177 member({ state: "completed" }),
178 member({ index: 2, label: "dynamic:late", phaseIndex: 3, phaseTitle: "Gamma" }),
179 ],
180 },
181 }),
182 ]);
183 const group = groups[0];
184 expect(group?.phases.map((phase) => phase.title)).toEqual(["Alpha", "Gamma"]);
185 expect(group?.unphasedMembers).toEqual([]);
186 });
187
188 it.each([false, true])("keeps a reported phase title, missing title first=%s", (missingFirst) => {
189 const agents = [member({ phaseTitle: "Review" }), member({ index: 2, phaseTitle: undefined })];
190 const groups = groupsOf([
191 subagent({ workflow: { phases: [], agents: missingFirst ? agents.toReversed() : agents } }),
192 ]);
193 expect(groups[0]?.phases[0]?.title).toBe("Review");
194 });
195
196 it("keeps unphased members with their workflow and excludes unrelated agents", () => {
197 const runtime = projectedSubagentsToRuntime([
198 subagent({ id: "ordinary" }),
199 subagent({
200 id: "__proto__",
201 workflow: {
202 phases: [],
203 agents: [member({ phaseIndex: undefined, phaseTitle: undefined })],
204 },
205 }),
206 subagent({ id: "constructor", workflow: { phases: [], agents: [] } }),
207 subagent({ id: "orphan", workflow: { phases: [], agents: [member()] } }),
208 ]);
209 const groups = deriveWorkflowGroups(runtime.filter((agent) => agent.id !== "orphan"));
210 expect(groups.map((group) => group.workflow.id)).toEqual(["__proto__", "constructor"]);
211 expect(groups[0]?.phases).toEqual([]);
212 expect(groups[0]?.unphasedMembers.map((agent) => agent.title)).toEqual(["alpha:one"]);
213 expect(groups[1]?.phases).toEqual([]);
214 expect(groups[1]?.unphasedMembers).toEqual([]);
215 });
216});