Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { classifyTaskAgentKind, type OrchestrationThreadActivity } from "@t3tools/contracts";
3import {
4 deriveAgentPanelModel,
5 foldSubagentActivities,
6 formatSubagentModelLabel,
7 formatSubagentTokenCount,
8 isAgentAttributedToolActivity,
9 isSubagentActivityKind,
10 isTimelineBypassActivity,
11 workflowCardMembers,
12} from "./subagentRuntime.ts";
13
14let sequence = 0;
15/**
16 * Fixtures model POST-INGESTION rows: ingestion stamps agentKind on every
17 * task.* payload, so the helper stamps too (same classifier). Pass an
18 * explicit agentKind (or agentKind: undefined via legacy()) to override.
19 */
20function activity(
21 kind: string,
22 payload: Record<string, unknown>,
23 at = `2026-08-01T10:00:${String(sequence).padStart(2, "0")}.000Z`,
24): OrchestrationThreadActivity {
25 sequence += 1;
26 const stamped =
27 kind.startsWith("task.") && !("agentKind" in payload)
28 ? {
29 ...payload,
30 agentKind: classifyTaskAgentKind({
31 taskType: typeof payload.taskType === "string" ? payload.taskType : undefined,
32 agentId: typeof payload.agentId === "string" ? payload.agentId : undefined,
33 }),
34 }
35 : payload;
36 return {
37 id: `activity-${sequence}`,
38 tone: "info",
39 kind,
40 summary: kind,
41 payload: stamped,
42 turnId: null,
43 createdAt: at,
44 } as unknown as OrchestrationThreadActivity;
45}
46
47/** A pre-stamp row (legacy thread / old server): no agentKind at all. */
48function legacyActivity(
49 kind: string,
50 payload: Record<string, unknown>,
51): OrchestrationThreadActivity {
52 sequence += 1;
53 return {
54 id: `activity-${sequence}`,
55 tone: "info",
56 kind,
57 summary: kind,
58 payload,
59 turnId: null,
60 createdAt: `2026-08-01T10:00:${String(sequence).padStart(2, "0")}.000Z`,
61 } as unknown as OrchestrationThreadActivity;
62}
63
64function fold(rows: ReadonlyArray<OrchestrationThreadActivity>) {
65 return foldSubagentActivities(rows);
66}
67
68describe("foldSubagentActivities", () => {
69 it("builds an agent from start → progress → completion", () => {
70 const agents = fold([
71 activity("task.started", {
72 taskId: "task-1",
73 title: "Audit auth flow",
74 role: "explorer",
75 }),
76 activity("task.progress", {
77 taskId: "task-1",
78 lastToolName: "Read",
79 typedUsage: { totalTokens: 1200, toolUses: 3 },
80 }),
81 activity("task.completed", {
82 taskId: "task-1",
83 status: "completed",
84 summary: "Found 2 issues",
85 typedUsage: { totalTokens: 5000, toolUses: 9 },
86 }),
87 ]);
88 expect(agents).toHaveLength(1);
89 const agent = agents[0]!;
90 expect(agent.title).toBe("Audit auth flow");
91 expect(agent.role).toBe("explorer");
92 expect(agent.status).toBe("completed");
93 expect(agent.result).toBe("Found 2 issues");
94 expect(agent.usage?.totalTokens).toBe(5000);
95 expect(agent.activationCount).toBe(1);
96 expect(agent.completedAt).not.toBeNull();
97 });
98
99 it("progress can create an agent when its start row aged out of retention", () => {
100 const agents = fold([
101 activity("task.progress", {
102 taskId: "task-orphan",
103 title: "Recovered agent",
104 role: "verifier",
105 typedUsage: { totalTokens: 100 },
106 }),
107 ]);
108 expect(agents).toHaveLength(1);
109 expect(agents[0]!.title).toBe("Recovered agent");
110 expect(agents[0]!.status).toBe("running");
111 });
112
113 it("completion before start stays terminal; a late start only fills metadata", () => {
114 const agents = fold([
115 activity("task.completed", {
116 taskId: "task-2",
117 status: "failed",
118 summary: "boom",
119 role: "fixer",
120 }),
121 activity("task.started", { taskId: "task-2", title: "Late metadata", role: "fixer" }),
122 ]);
123 expect(agents).toHaveLength(1);
124 const agent = agents[0]!;
125 expect(agent.title).toBe("Late metadata");
126 expect(agent.role).toBe("fixer");
127 // The late start must NOT reopen the terminal activation as a new run.
128 expect(agent.status).toBe("failed");
129 expect(agent.error).toBe("boom");
130 });
131
132 it("duplicate terminal events are idempotent (timestamps do not slide)", () => {
133 const agents = fold([
134 activity("task.started", { taskId: "task-3", taskType: "local_agent" }),
135 activity(
136 "task.completed",
137 { taskId: "task-3", status: "completed" },
138 "2026-08-01T11:00:00.000Z",
139 ),
140 activity(
141 "task.completed",
142 { taskId: "task-3", status: "completed" },
143 "2026-08-01T12:00:00.000Z",
144 ),
145 ]);
146 expect(agents[0]!.completedAt).toBe("2026-08-01T11:00:00.000Z");
147 });
148
149 it("reactivation increments the run count and clears result/error", () => {
150 const agents = fold([
151 activity("task.started", { taskId: "task-4", taskType: "local_agent" }),
152 activity("task.completed", { taskId: "task-4", status: "completed", summary: "run 1 done" }),
153 activity("task.updated", { taskId: "task-4", status: "running" }),
154 ]);
155 const agent = agents[0]!;
156 expect(agent.activationCount).toBe(2);
157 expect(agent.result).toBeNull();
158 expect(agent.completedAt).toBeNull();
159 expect(agent.status).toBe("running");
160 });
161
162 it("idle is nonterminal: an idle agent resumes without losing identity", () => {
163 const agents = fold([
164 activity("task.started", { taskId: "codex-child-1", title: "Marlow", role: "explorer" }),
165 activity("task.updated", { taskId: "codex-child-1", status: "idle" }),
166 activity("task.updated", { taskId: "codex-child-1", status: "running" }),
167 ]);
168 expect(agents).toHaveLength(1);
169 expect(agents[0]!.activationCount).toBe(2);
170 expect(agents[0]!.status).toBe("running");
171 });
172
173 it("cumulative usage max-merges: duplicate and late frames never shrink or double-count", () => {
174 const agents = fold([
175 activity("task.started", { taskId: "task-5", taskType: "local_agent" }),
176 activity("task.progress", {
177 taskId: "task-5",
178 typedUsage: { totalTokens: 900, inputTokens: 700 },
179 }),
180 activity("task.progress", {
181 taskId: "task-5",
182 typedUsage: { totalTokens: 900, inputTokens: 700 },
183 }),
184 activity("task.progress", { taskId: "task-5", typedUsage: { totalTokens: 500 } }),
185 ]);
186 expect(agents[0]!.usage).toEqual({ totalTokens: 900, inputTokens: 700 });
187 });
188
189 it("partial terminal usage preserves known breakdown fields", () => {
190 const agents = fold([
191 activity("task.started", { taskId: "task-6", taskType: "local_agent" }),
192 activity("task.progress", {
193 taskId: "task-6",
194 typedUsage: { totalTokens: 800, inputTokens: 600, outputTokens: 150 },
195 }),
196 activity("task.completed", {
197 taskId: "task-6",
198 status: "completed",
199 typedUsage: { totalTokens: 1000 },
200 }),
201 ]);
202 expect(agents[0]!.usage).toEqual({ totalTokens: 1000, inputTokens: 600, outputTokens: 150 });
203 });
204
205 it("skips malformed rows individually without failing the fold", () => {
206 const agents = fold([
207 activity("task.started", { taskId: "task-7", title: "Good", taskType: "local_agent" }),
208 activity("task.progress", { bogus: true }),
209 activity("task.progress", { taskId: 42 }),
210 ]);
211 expect(agents).toHaveLength(1);
212 expect(agents[0]!.title).toBe("Good");
213 });
214
215 it("bounds repeated strings at 180 chars and the activity ring at 6 deduped entries", () => {
216 const long = "x".repeat(500);
217 const rows = [activity("task.started", { taskId: "task-8", taskType: "local_agent" })];
218 for (let i = 0; i < 10; i += 1) {
219 rows.push(activity("task.progress", { taskId: "task-8", summary: `${long}-${i}` }));
220 }
221 rows.push(activity("task.progress", { taskId: "task-8", summary: `${long}-9` }));
222 const agents = fold(rows);
223 const agent = agents[0]!;
224 expect(agent.recentActivity.length).toBeLessThanOrEqual(6);
225 for (const entry of agent.recentActivity) {
226 expect(entry.summary.length).toBeLessThanOrEqual(180);
227 }
228 // Consecutive identical summaries dedupe (truncation makes them equal).
229 const summaries = agent.recentActivity.map((entry) => entry.summary);
230 expect(new Set(summaries).size).toBe(summaries.length);
231 });
232
233 it("plan tasks are not agents", () => {
234 const agents = fold([activity("task.started", { taskId: "plan-1", taskType: "plan" })]);
235 expect(agents).toHaveLength(0);
236 });
237
238 it("workflow members key by stable slot and attach to their coordinator", () => {
239 const agents = fold([
240 activity("task.started", {
241 taskId: "wf-1",
242 taskType: "local_workflow",
243 title: "audit-auth-flow",
244 workflowName: "audit-auth-flow",
245 }),
246 activity("task.progress", {
247 taskId: "wf-1",
248 phases: [
249 { index: 0, title: "Audit" },
250 { index: 1, title: "Verify" },
251 ],
252 }),
253 activity("task.progress", {
254 taskId: "wf-1:wf:0",
255 title: "audit:entrypoints",
256 status: "running",
257 parentAgentId: "wf-1",
258 agentIndex: 0,
259 phaseIndex: 0,
260 phaseTitle: "Audit",
261 timelineBypass: true,
262 }),
263 ]);
264 const workflow = agents.find((agent) => agent.id === "wf-1");
265 const member = agents.find((agent) => agent.id === "wf-1:wf:0");
266 expect(workflow?.kind).toBe("workflow");
267 expect(workflow?.phases).toEqual([
268 { index: 0, title: "Audit" },
269 { index: 1, title: "Verify" },
270 ]);
271 expect(member?.kind).toBe("workflow_agent");
272 expect(member?.parentAgentId).toBe("wf-1");
273 });
274
275 it("a workflow member retry (attempt bump) is a reactivation of the same slot", () => {
276 const agents = fold([
277 activity("task.progress", {
278 taskId: "wf-2:wf:1",
279 title: "verify:refresh",
280 status: "failed",
281 error: "attempt 1 died",
282 parentAgentId: "wf-2",
283 attempt: 1,
284 }),
285 activity("task.progress", {
286 taskId: "wf-2:wf:1",
287 title: "verify:refresh",
288 status: "running",
289 parentAgentId: "wf-2",
290 attempt: 2,
291 }),
292 ]);
293 expect(agents).toHaveLength(1);
294 const member = agents[0]!;
295 expect(member.activationCount).toBeGreaterThanOrEqual(2);
296 expect(member.error).toBeNull();
297 expect(member.status).toBe("running");
298 });
299
300 it("drops non-http(s) session urls at the fold boundary", () => {
301 const agents = fold([
302 activity("task.started", {
303 taskId: "wf-3",
304 taskType: "local_workflow",
305 runHandles: { sessionUrl: "javascript:alert(1)", runId: "run-1" },
306 }),
307 ]);
308 expect(agents[0]!.runHandles?.sessionUrl).toBeUndefined();
309 expect(agents[0]!.runHandles?.runId).toBe("run-1");
310 });
311});
312
313describe("deriveAgentPanelModel", () => {
314 const roster = fold([
315 activity("task.started", { taskId: "wf-1", taskType: "local_workflow", title: "audit" }),
316 activity("task.progress", {
317 taskId: "wf-1",
318 phases: [
319 { index: 0, title: "Audit" },
320 { index: 1, title: "Verify" },
321 ],
322 }),
323 activity("task.progress", {
324 taskId: "wf-1:wf:0",
325 title: "audit:a",
326 status: "completed",
327 parentAgentId: "wf-1",
328 agentIndex: 0,
329 phaseIndex: 0,
330 }),
331 activity("task.completed", { taskId: "wf-1:wf:0", status: "completed", parentAgentId: "wf-1" }),
332 activity("task.progress", {
333 taskId: "wf-1:wf:1",
334 title: "verify:b",
335 status: "running",
336 parentAgentId: "wf-1",
337 agentIndex: 1,
338 phaseIndex: 1,
339 typedUsage: { totalTokens: 4000 },
340 }),
341 activity("task.started", { taskId: "direct-1", title: "Marlow", role: "explorer" }),
342 activity("task.updated", { taskId: "direct-1", status: "idle" }),
343 ]);
344
345 it("groups workflow members by phase and separates direct spawns", () => {
346 const model = deriveAgentPanelModel({ agents: roster });
347 expect(model.workflows).toHaveLength(1);
348 const group = model.workflows[0]!;
349 expect(group.phases).toHaveLength(2);
350 expect(group.phases[0]!.state).toBe("done");
351 expect(group.phases[1]!.state).toBe("running");
352 expect(model.directAgents.map((agent) => agent.id)).toEqual(["direct-1"]);
353 });
354
355 it("counts idle deliberately and waiting as active", () => {
356 const model = deriveAgentPanelModel({ agents: roster });
357 expect(model.idleCount).toBe(1);
358 // wf-1 coordinator + member 1 running.
359 expect(model.runningCount).toBeGreaterThanOrEqual(1);
360 expect(model.idleCount + model.runningCount + model.waitingCount + model.settledCount).toBe(
361 roster.length,
362 );
363 });
364
365 it("a phase with only pending members never reads as running", () => {
366 const pendingRoster = fold([
367 activity("task.started", { taskId: "wf-9", taskType: "local_workflow" }),
368 activity("task.progress", {
369 taskId: "wf-9",
370 phases: [{ index: 0, title: "Fix" }],
371 }),
372 activity("task.progress", {
373 taskId: "wf-9:wf:0",
374 title: "fixer",
375 status: "pending",
376 parentAgentId: "wf-9",
377 agentIndex: 0,
378 phaseIndex: 0,
379 }),
380 ]);
381 const model = deriveAgentPanelModel({ agents: pendingRoster });
382 // "pending" counts as active liveness (queued work), so the phase reads
383 // running only if a member is genuinely pending/running — this asserts
384 // the settled-count rule: no member settled, phase not done.
385 expect(model.workflows[0]!.phases[0]!.state).not.toBe("done");
386 });
387
388 it("v2 projection wins outright and sources are never merged", () => {
389 const v2Agent = { ...roster[0]!, id: "v2-only", title: "From v2" };
390 const model = deriveAgentPanelModel({ agents: roster, v2Projection: [v2Agent] });
391 const allIds = [
392 ...model.workflows.map((group) => group.workflow.id),
393 ...model.directAgents.map((agent) => agent.id),
394 ];
395 expect(allIds).toContain("v2-only");
396 expect(allIds).not.toContain("direct-1");
397 });
398
399 it("orphaned members fall back to the direct list", () => {
400 const orphans = fold([
401 activity("task.progress", {
402 taskId: "gone:wf:0",
403 title: "orphan",
404 status: "running",
405 parentAgentId: "gone",
406 }),
407 ]);
408 const model = deriveAgentPanelModel({ agents: orphans });
409 expect(model.workflows).toHaveLength(0);
410 expect(model.directAgents.map((agent) => agent.id)).toEqual(["gone:wf:0"]);
411 });
412});
413
414describe("workflowCardMembers", () => {
415 it("orders by urgency (failed, running, waiting) and reports overflow", () => {
416 const roster = fold([
417 activity("task.started", { taskId: "wf-1", taskType: "local_workflow" }),
418 ...[..."abcdefghij"].map((letter, index) =>
419 activity("task.progress", {
420 taskId: `wf-1:wf:${index}`,
421 title: `agent-${letter}`,
422 status: index === 3 ? "failed" : index < 3 ? "completed" : "running",
423 ...(index === 3 ? { error: "died" } : {}),
424 parentAgentId: "wf-1",
425 agentIndex: index,
426 phaseIndex: 0,
427 phaseTitle: "Work",
428 }),
429 ),
430 ]);
431 const model = deriveAgentPanelModel({ agents: roster });
432 const { visible, overflow } = workflowCardMembers(model.workflows[0]!, 8);
433 expect(visible).toHaveLength(8);
434 expect(overflow).toBe(2);
435 expect(visible[0]!.status).toBe("failed");
436 expect(visible.filter((agent) => agent.status === "completed").length).toBeLessThanOrEqual(2);
437 });
438});
439
440describe("timeline predicates", () => {
441 it("recognizes subagent activity kinds as fold input", () => {
442 for (const kind of [
443 "task.started",
444 "task.progress",
445 "task.updated",
446 "task.completed",
447 "tool.progress",
448 ]) {
449 expect(isSubagentActivityKind(kind)).toBe(true);
450 }
451 expect(isSubagentActivityKind("tool.completed")).toBe(false);
452 });
453
454 it("attributed tool rows are re-homed; unattributed rows stay in the timeline", () => {
455 expect(isAgentAttributedToolActivity(activity("tool.completed", { agentId: "task-1" }))).toBe(
456 true,
457 );
458 expect(isAgentAttributedToolActivity(activity("tool.completed", {}))).toBe(false);
459 expect(isAgentAttributedToolActivity(activity("tool.completed", { agentId: " " }))).toBe(
460 false,
461 );
462 });
463
464 it("timelineBypass rows never render in the parent chat", () => {
465 expect(isTimelineBypassActivity(activity("task.progress", { timelineBypass: true }))).toBe(
466 true,
467 );
468 expect(isTimelineBypassActivity(activity("task.progress", {}))).toBe(false);
469 });
470});
471
472describe("formatSubagentTokenCount", () => {
473 it("formats plain counters", () => {
474 expect(formatSubagentTokenCount(950)).toBe("950");
475 expect(formatSubagentTokenCount(41200)).toBe("41.2k");
476 expect(formatSubagentTokenCount(247000)).toBe("247k");
477 expect(formatSubagentTokenCount(1_400_000)).toBe("1.4M");
478 });
479});
480
481describe("model and effort attribution", () => {
482 it("carries model/effort from start rows and refines model from later rows", () => {
483 const agents = fold([
484 activity("task.started", {
485 taskId: "task-m",
486 title: "Verify math",
487 model: "sonnet",
488 effort: "high",
489 }),
490 // Later row refines with the authoritative API model id; effort absent
491 // must not clear the known value.
492 activity("task.progress", { taskId: "task-m", model: "claude-sonnet-5[1m]" }),
493 ]);
494 expect(agents).toHaveLength(1);
495 expect(agents[0]!.model).toBe("claude-sonnet-5[1m]");
496 expect(agents[0]!.effort).toBe("high");
497 });
498
499 it("formatSubagentModelLabel compacts ids and appends effort", () => {
500 expect(formatSubagentModelLabel("claude-sonnet-5[1m]", "high")).toBe("sonnet-5[1m] · high");
501 expect(formatSubagentModelLabel("claude-opus-4-20250514", null)).toBe("opus-4");
502 expect(formatSubagentModelLabel("gpt-5.6-sol", "low")).toBe("gpt-5.6-sol · low");
503 expect(formatSubagentModelLabel(null, "high")).toBeNull();
504 });
505});
506
507describe("background task exclusion", () => {
508 it("shells and monitors never join the roster (from any lifecycle row)", () => {
509 const agents = fold([
510 activity("task.started", { taskId: "shell-1", taskType: "shell", title: "Run 12s stall" }),
511 activity("task.progress", { taskId: "shell-2", taskType: "shell", title: "Run stall" }),
512 activity("task.completed", { taskId: "mon-1", taskType: "monitor", status: "completed" }),
513 activity("task.started", { taskId: "agent-1", taskType: "subagent", title: "Real agent" }),
514 ]);
515 expect(agents.map((agent) => agent.id)).toEqual(["agent-1"]);
516 });
517
518 it("rows without a taskType stay in the roster (workflow members, Codex children)", () => {
519 const agents = fold([
520 activity("task.progress", { taskId: "wf-1:wf:0", status: "running", parentAgentId: "wf-1" }),
521 ]);
522 expect(agents).toHaveLength(1);
523 });
524
525 it("the server stamp is the only classifier: no stamp means no roster row", () => {
526 const agents = fold([
527 // Stamped background: agent-looking fields don't matter.
528 activity("task.started", {
529 taskId: "bg-1",
530 agentKind: "background",
531 role: "watcher",
532 model: "sonnet",
533 }),
534 // Stamped agent: plain row still joins the roster.
535 activity("task.started", { taskId: "ag-1", agentKind: "agent", detail: "plain row" }),
536 // Legacy pre-stamp rows (old threads/servers) stay in the work log —
537 // exactly their pre-upgrade behavior.
538 legacyActivity("task.started", { taskId: "old-task", detail: "tailing logs" }),
539 legacyActivity("task.progress", { taskId: "old-task", summary: "still tailing" }),
540 ]);
541 expect(agents.map((agent) => agent.id)).toEqual(["ag-1"]);
542 });
543
544 it("membership is sticky: a stampless later row still reaches a known agent", () => {
545 const agents = fold([
546 activity("task.started", { taskId: "a1", taskType: "local_agent", title: "Agent" }),
547 // Terminal row missing the stamp (defensive: adapters synthesize some
548 // rows) — sticky membership still routes it to the agent.
549 legacyActivity("task.completed", { taskId: "a1", status: "completed", summary: "done" }),
550 ]);
551 expect(agents).toHaveLength(1);
552 expect(agents[0]!.status).toBe("completed");
553 expect(agents[0]!.result).toBe("done");
554 });
555});
556
557describe("session-derived interruption", () => {
558 it("dead session interrupts live agents but preserves idle and settled", () => {
559 const rows = [
560 activity("task.started", { taskId: "live-1", taskType: "local_agent" }),
561 activity("task.started", { taskId: "idle-1", taskType: "local_agent" }),
562 activity("task.updated", { taskId: "idle-1", status: "idle" }),
563 activity("task.started", { taskId: "done-1", taskType: "local_agent" }),
564 activity("task.completed", { taskId: "done-1", status: "completed" }),
565 ];
566 const dead = foldSubagentActivities(rows, { sessionLive: false });
567 expect(dead.find((agent) => agent.id === "live-1")?.status).toBe("interrupted");
568 expect(dead.find((agent) => agent.id === "idle-1")?.status).toBe("idle");
569 expect(dead.find((agent) => agent.id === "done-1")?.status).toBe("completed");
570 const alive = foldSubagentActivities(rows, { sessionLive: true });
571 expect(alive.find((agent) => agent.id === "live-1")?.status).toBe("running");
572 });
573});
574
575describe("terminal robustness", () => {
576 it("task.updated creating an agent (start row aged out) counts one activation", () => {
577 const agents = fold([
578 activity("task.updated", { taskId: "orphan-u", status: "running", role: "worker" }),
579 ]);
580 expect(agents).toHaveLength(1);
581 expect(agents[0]!.activationCount).toBe(1);
582 expect(agents[0]!.status).toBe("running");
583 });
584
585 it("a late start after a terminal task.updated does not reopen the run", () => {
586 const agents = fold([
587 activity("task.updated", { taskId: "t1", status: "failed", role: "worker" }),
588 activity("task.started", { taskId: "t1", taskType: "local_agent", title: "Late" }),
589 ]);
590 expect(agents).toHaveLength(1);
591 expect(agents[0]!.status).toBe("failed");
592 expect(agents[0]!.title).toBe("Late");
593 });
594
595 it("a completion after a terminal task.updated still enriches result and usage", () => {
596 // Claude commonly emits terminal task.updated before task.completed;
597 // the completion carries the summary and final usage the update lacked.
598 const agents = fold([
599 activity("task.started", { taskId: "te-1", taskType: "local_agent" }),
600 activity(
601 "task.updated",
602 { taskId: "te-1", status: "completed", endedAt: "2026-08-01T10:59:00.000Z" },
603 "2026-08-01T11:00:00.000Z",
604 ),
605 activity(
606 "task.completed",
607 {
608 taskId: "te-1",
609 status: "completed",
610 summary: "final answer",
611 typedUsage: { totalTokens: 4200, toolUses: 7 },
612 },
613 "2026-08-01T11:00:01.000Z",
614 ),
615 ]);
616 const agent = agents[0]!;
617 expect(agent.status).toBe("completed");
618 expect(agent.result).toBe("final answer");
619 expect(agent.usage?.totalTokens).toBe(4200);
620 // Timestamps stay pinned to the transition that settled the run.
621 expect(agent.completedAt).toBe("2026-08-01T10:59:00.000Z");
622 });
623
624 it("duplicate completions keep the FIRST result, not the last", () => {
625 const agents = fold([
626 activity("task.started", { taskId: "t2", taskType: "local_agent" }),
627 activity("task.completed", { taskId: "t2", status: "completed", summary: "first result" }),
628 activity("task.completed", { taskId: "t2", status: "completed", summary: "second result" }),
629 ]);
630 expect(agents[0]!.result).toBe("first result");
631 });
632
633 it("provider endedAt wins over ingestion time on the settling transition", () => {
634 const agents = fold([
635 activity("task.started", { taskId: "t3", taskType: "local_agent" }),
636 activity(
637 "task.updated",
638 { taskId: "t3", status: "failed", endedAt: "2026-08-01T09:59:59.000Z" },
639 "2026-08-01T10:00:30.000Z",
640 ),
641 ]);
642 expect(agents[0]!.completedAt).toBe("2026-08-01T09:59:59.000Z");
643 });
644
645 it("workflow retries count each attempt once", () => {
646 const agents = fold([
647 activity("task.progress", {
648 taskId: "wf-r:wf:0",
649 parentAgentId: "wf-r",
650 status: "running",
651 attempt: 1,
652 }),
653 activity("task.progress", {
654 taskId: "wf-r:wf:0",
655 parentAgentId: "wf-r",
656 status: "failed",
657 attempt: 1,
658 }),
659 activity("task.progress", {
660 taskId: "wf-r:wf:0",
661 parentAgentId: "wf-r",
662 status: "running",
663 attempt: 2,
664 }),
665 ]);
666 expect(agents[0]!.activationCount).toBe(2);
667 });
668});
669
670describe("phase membership", () => {
671 it("members with unknown phase indices land in unphasedMembers, never vanish", () => {
672 const model = deriveAgentPanelModel({
673 agents: fold([
674 activity("task.started", {
675 taskId: "wf-p",
676 taskType: "local_workflow",
677 phases: [{ index: 0, title: "Only phase" }],
678 }),
679 activity("task.progress", {
680 taskId: "wf-p:wf:0",
681 parentAgentId: "wf-p",
682 status: "running",
683 phaseIndex: 0,
684 }),
685 activity("task.progress", {
686 taskId: "wf-p:wf:9",
687 parentAgentId: "wf-p",
688 status: "running",
689 phaseIndex: 9,
690 }),
691 ]),
692 });
693 const group = model.workflows[0]!;
694 const visible = [
695 ...group.phases.flatMap((phase) => phase.members),
696 ...group.unphasedMembers,
697 ].map((member) => member.id);
698 expect(visible).toContain("wf-p:wf:0");
699 expect(visible).toContain("wf-p:wf:9");
700 });
701});
702
703describe("coordinator settle cascade", () => {
704 it("members without their own terminal row settle when the coordinator does", () => {
705 const agents = fold([
706 activity("task.started", { taskId: "wf-1", taskType: "local_workflow" }),
707 activity("task.progress", {
708 taskId: "wf-1:wf:0",
709 title: "stalled member",
710 status: "running",
711 parentAgentId: "wf-1",
712 }),
713 activity("task.completed", {
714 taskId: "wf-1",
715 status: "completed",
716 taskType: "local_workflow",
717 }),
718 ]);
719 const member = agents.find((agent) => agent.id === "wf-1:wf:0");
720 expect(member?.status).toBe("completed");
721 expect(member?.completedAt).not.toBeNull();
722 });
723
724 it("a failed coordinator marks unfinished members interrupted, not completed", () => {
725 const agents = fold([
726 activity("task.started", { taskId: "wf-2", taskType: "local_workflow" }),
727 activity("task.progress", {
728 taskId: "wf-2:wf:0",
729 status: "running",
730 parentAgentId: "wf-2",
731 }),
732 activity("task.completed", { taskId: "wf-2", status: "failed", taskType: "local_workflow" }),
733 ]);
734 const member = agents.find((agent) => agent.id === "wf-2:wf:0");
735 expect(member?.status).toBe("interrupted");
736 });
737});
738
739describe("task type classification is a denylist", () => {
740 it("unknown agent-flavored types (local_agent, future names) join the roster", () => {
741 const agents = fold([
742 activity("task.started", {
743 taskId: "a1",
744 taskType: "local_agent",
745 title: "Math test 1",
746 role: "claude",
747 }),
748 activity("task.started", { taskId: "a2", taskType: "some_future_agent_kind", title: "X" }),
749 ]);
750 expect(agents.map((agent) => agent.id).toSorted()).toEqual(["a1", "a2"]);
751 });
752});
753
754describe("nested agents vs subagent shells", () => {
755 it("a nested agent (agentId + agent taskType) stays in the roster; its shells do not", () => {
756 const agents = fold([
757 activity("task.started", {
758 taskId: "nested-1",
759 taskType: "local_agent",
760 agentId: "parent-agent",
761 title: "Nested researcher",
762 }),
763 activity("task.started", {
764 taskId: "shell-1",
765 taskType: "local_bash",
766 agentId: "parent-agent",
767 title: "Nested sleep",
768 }),
769 ]);
770 expect(agents.map((agent) => agent.id)).toEqual(["nested-1"]);
771 });
772});