The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import { |
| 3 | MessageId, |
| 4 | NodeId, |
| 5 | type OrchestrationV2Subagent, |
| 6 | type OrchestrationV2TurnItem, |
| 7 | RunId, |
| 8 | ThreadId, |
| 9 | TurnItemId, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import * as DateTime from "effect/DateTime"; |
| 12 | |
| 13 | import { backgroundWorkNotification, notificationTurnItem } from "./notification.ts"; |
| 14 | |
| 15 | const childThreadId = ThreadId.make("thread:child"); |
| 16 | |
| 17 | describe("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 | assert.equal( |
| 46 | backgroundWorkNotification([ |
| 47 | { kind: "command", label: `${"a".repeat(78)}😀 and more`, outcome: "completed" }, |
| 48 | ]).summary, |
| 49 | `Command "${"a".repeat(78)}…" finished`, |
| 50 | ); |
| 51 | }); |
| 52 | |
| 53 | it("names every piece of work that ended together, without opening one of them", () => { |
| 54 | const notification = backgroundWorkNotification([ |
| 55 | { kind: "subagent", label: "Agent B", outcome: "cancelled", childThreadId }, |
| 56 | { kind: "command", label: "Sleep 60 seconds", outcome: "cancelled" }, |
| 57 | ]); |
| 58 | assert.deepEqual(notification, { |
| 59 | source: { kind: "background_task" }, |
| 60 | outcome: "cancelled", |
| 61 | summary: 'Subagent "Agent B" and command "Sleep 60 seconds" were stopped', |
| 62 | }); |
| 63 | assert.deepInclude( |
| 64 | backgroundWorkNotification([ |
| 65 | { kind: "command", label: "npm test", outcome: "completed", exitCode: 0 }, |
| 66 | { kind: "command", label: "npm run lint", outcome: "failed", exitCode: 1 }, |
| 67 | ]), |
| 68 | { |
| 69 | outcome: "failed", |
| 70 | summary: 'Command "npm test" finished (exit 0) and command "npm run lint" failed (exit 1)', |
| 71 | }, |
| 72 | ); |
| 73 | }); |
| 74 | |
| 75 | it("counts work it cannot list", () => { |
| 76 | const reports = ["a", "b", "c", "d"].map((label) => ({ |
| 77 | kind: "subagent" as const, |
| 78 | label, |
| 79 | outcome: "completed" as const, |
| 80 | })); |
| 81 | assert.deepEqual(backgroundWorkNotification(reports), { |
| 82 | source: { kind: "subagent" }, |
| 83 | outcome: "completed", |
| 84 | summary: "4 subagents finished", |
| 85 | }); |
| 86 | assert.deepEqual( |
| 87 | backgroundWorkNotification( |
| 88 | reports.map((report, index) => |
| 89 | index === 0 ? { ...report, outcome: "failed" as const } : report, |
| 90 | ), |
| 91 | ), |
| 92 | { |
| 93 | source: { kind: "subagent" }, |
| 94 | outcome: "failed", |
| 95 | summary: "1 subagent failed and 3 finished", |
| 96 | }, |
| 97 | ); |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | describe("notificationTurnItem", () => { |
| 102 | const now = DateTime.makeUnsafe("2026-09-27T00:00:00.000Z"); |
| 103 | const threadId = ThreadId.make("thread:parent"); |
| 104 | const parentRunId = RunId.make("run:parent"); |
| 105 | const userMessage: OrchestrationV2TurnItem = { |
| 106 | id: TurnItemId.make("item:delivery"), |
| 107 | threadId, |
| 108 | runId: RunId.make("run:delivery"), |
| 109 | nodeId: null, |
| 110 | providerThreadId: null, |
| 111 | providerTurnId: null, |
| 112 | nativeItemRef: null, |
| 113 | parentItemId: null, |
| 114 | ordinal: 1, |
| 115 | status: "completed", |
| 116 | title: null, |
| 117 | startedAt: now, |
| 118 | completedAt: now, |
| 119 | updatedAt: now, |
| 120 | type: "user_message", |
| 121 | messageId: MessageId.make("message:delivery"), |
| 122 | inputIntent: "turn_start", |
| 123 | text: "Delegated tasks reached terminal states.", |
| 124 | attachments: [], |
| 125 | createdBy: "agent", |
| 126 | creationSource: "server", |
| 127 | }; |
| 128 | const task = (id: string, title: string, status: OrchestrationV2Subagent["status"]) => |
| 129 | ({ |
| 130 | id: NodeId.make(id), |
| 131 | runId: parentRunId, |
| 132 | origin: "app_owned", |
| 133 | title, |
| 134 | prompt: `${title} prompt`, |
| 135 | status, |
| 136 | childThreadId: ThreadId.make(`thread:${id}`), |
| 137 | }) as OrchestrationV2Subagent; |
| 138 | const delivery = (taskIds: ReadonlyArray<string>) => ({ |
| 139 | delegatedCompletion: { |
| 140 | parentRunId, |
| 141 | generation: 1, |
| 142 | taskIds: taskIds.map((taskId) => NodeId.make(taskId)), |
| 143 | }, |
| 144 | }); |
| 145 | const tasks = [ |
| 146 | task("a", "Review src/math.ts", "completed"), |
| 147 | task("b", "Write tests", "completed"), |
| 148 | task("c", "Update docs", "running"), |
| 149 | ]; |
| 150 | |
| 151 | it("opens the child thread of a single delegated task", () => { |
| 152 | const item = notificationTurnItem(userMessage, delivery(["a"]), tasks); |
| 153 | assert.deepInclude(item, { |
| 154 | type: "notification", |
| 155 | summary: 'Delegated task "Review src/math.ts" finished', |
| 156 | source: { |
| 157 | kind: "delegated_task", |
| 158 | taskIds: [NodeId.make("a")], |
| 159 | childThreadId: ThreadId.make("thread:a"), |
| 160 | }, |
| 161 | }); |
| 162 | }); |
| 163 | }); |