Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import {
3 MessageId,
4 NodeId,
5 type OrchestrationV2Subagent,
6 type OrchestrationV2TurnItem,
7 RunId,
8 ThreadId,
9 TurnItemId,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12
13import { backgroundWorkNotification, notificationTurnItem } from "./notification.ts";
14
15const childThreadId = ThreadId.make("thread:child");
16
17describe("backgroundWorkNotification", () => {
18 it("names one piece of work and the thread of a subagent", () => {
19 assert.deepEqual(
20 backgroundWorkNotification([
21 {
22 kind: "subagent",
23 label: "Review src/math.ts",
24 outcome: "completed",
25 childThreadId,
26 },
27 ]),
28 {
29 source: { kind: "subagent", childThreadId },
30 outcome: "completed",
31 summary: 'Subagent "Review src/math.ts" finished',
32 },
33 );
34 assert.equal(
35 backgroundWorkNotification([
36 { kind: "command", label: "npm test\nnpm run lint", outcome: "failed", exitCode: 1 },
37 ]).summary,
38 'Command "npm test" failed (exit 1)',
39 );
40 assert.equal(
41 backgroundWorkNotification([{ kind: "monitor", label: "Three ticks", outcome: "updated" }])
42 .summary,
43 'Monitor "Three ticks" reported new output',
44 );
45 });
46
47 it("names every piece of work that ended together, without opening one of them", () => {
48 const notification = backgroundWorkNotification([
49 { kind: "subagent", label: "Agent B", outcome: "cancelled", childThreadId },
50 { kind: "command", label: "Sleep 60 seconds", outcome: "cancelled" },
51 ]);
52 assert.deepEqual(notification, {
53 source: { kind: "background_task" },
54 outcome: "cancelled",
55 summary: 'Subagent "Agent B" and command "Sleep 60 seconds" were stopped',
56 });
57 });
58
59 it("counts work it cannot list", () => {
60 const reports = ["a", "b", "c", "d"].map((label) => ({
61 kind: "subagent" as const,
62 label,
63 outcome: "completed" as const,
64 }));
65 assert.deepEqual(backgroundWorkNotification(reports), {
66 source: { kind: "subagent" },
67 outcome: "completed",
68 summary: "4 subagents finished",
69 });
70 });
71});
72
73describe("notificationTurnItem", () => {
74 const now = DateTime.makeUnsafe("2026-09-27T00:00:00.000Z");
75 const threadId = ThreadId.make("thread:parent");
76 const parentRunId = RunId.make("run:parent");
77 const userMessage: OrchestrationV2TurnItem = {
78 id: TurnItemId.make("item:delivery"),
79 threadId,
80 runId: RunId.make("run:delivery"),
81 nodeId: null,
82 providerThreadId: null,
83 providerTurnId: null,
84 nativeItemRef: null,
85 parentItemId: null,
86 ordinal: 1,
87 status: "completed",
88 title: null,
89 startedAt: now,
90 completedAt: now,
91 updatedAt: now,
92 type: "user_message",
93 messageId: MessageId.make("message:delivery"),
94 inputIntent: "turn_start",
95 text: "Delegated tasks reached terminal states.",
96 attachments: [],
97 createdBy: "agent",
98 creationSource: "server",
99 };
100 const task = (id: string, title: string, status: OrchestrationV2Subagent["status"]) =>
101 ({
102 id: NodeId.make(id),
103 runId: parentRunId,
104 origin: "app_owned",
105 title,
106 prompt: `${title} prompt`,
107 status,
108 childThreadId: ThreadId.make(`thread:${id}`),
109 }) as OrchestrationV2Subagent;
110 const delivery = (taskIds: ReadonlyArray<string>) => ({
111 delegatedCompletion: {
112 parentRunId,
113 generation: 1,
114 taskIds: taskIds.map((taskId) => NodeId.make(taskId)),
115 },
116 });
117 const tasks = [
118 task("a", "Review src/math.ts", "completed"),
119 task("b", "Write tests", "completed"),
120 task("c", "Update docs", "running"),
121 ];
122
123 it("opens the child thread of a single delegated task", () => {
124 const item = notificationTurnItem(userMessage, delivery(["a"]), tasks);
125 assert.deepInclude(item, {
126 type: "notification",
127 summary: 'Delegated task "Review src/math.ts" finished',
128 source: {
129 kind: "delegated_task",
130 taskIds: [NodeId.make("a")],
131 childThreadId: ThreadId.make("thread:a"),
132 },
133 });
134 });
135});