Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CodexCollabWire.test.ts

180 lines7.0 KB

1/**
2 * Codex multi-agent wire fixtures.
3 *
4 * The child-interception path is the highest-blast-radius code in the
5 * subagent stack: it decides, per notification, whether traffic reaches the
6 * parent timeline at all. Three shipped bugs came from that decision being
7 * implicit (root thread registered as its own child; `error` and
8 * `serverRequest/resolved` swallowed by a catch-all). These tests pin the
9 * decision against a REAL capture rather than hand-written shapes.
10 *
11 * Fixture provenance: codex-cli 0.145.0 driven directly over stdio with
12 * gpt-5.6-luna at low effort, prompting a two-child fan-out (alpha, beta).
13 * See codexMultiAgentWire.json.
14 */
15import { assert, describe, it } from "vite-plus/test";
16​
17import fixture from "../testFixtures/codexMultiAgentWire.json" with { type: "json" };
18import { routeCodexChildNotification } from "./CodexSessionRuntime.ts";
19​
20interface WireNotification {
21 readonly method: string;
22 readonly params: Record<string, unknown>;
24​
25const notifications = fixture.notifications as ReadonlyArray<WireNotification>;
26const rootThreadId = fixture.rootThreadId;
27const childThreadIds = new Set(fixture.childThreadIds);
28​
29/** Mirrors readNotificationThreadId's addressing for the captured methods. */
30function notificationThreadId(entry: WireNotification): string | undefined {
31 const params = entry.params;
32 const thread = params.thread;
33 if (
34 typeof thread === "object" &&
35 thread !== null &&
36 typeof (thread as { id?: unknown }).id === "string"
37 ) {
38 return (thread as { id: string }).id;
39 }
40 return typeof params.threadId === "string" ? params.threadId : undefined;
42​
43function subAgentActivityItems(): ReadonlyArray<Record<string, unknown>> {
44 return notifications.flatMap((entry) => {
45 const item = entry.params.item;
46 if (typeof item !== "object" || item === null) return [];
47 const record = item as Record<string, unknown>;
48 return record.type === "subAgentActivity" ? [record] : [];
49 });
51​
52describe("codex multi-agent wire capture", () => {
53 it("captures a real two-child fan-out", () => {
54 assert.equal(fixture.capturedWith.model, "gpt-5.6-luna");
55 assert.equal(childThreadIds.size, 2);
56 const paths = subAgentActivityItems().map((item) => item.agentPath);
57 assert.include(paths, "/root/alpha");
58 assert.include(paths, "/root/beta");
59 });
60​
61 it("emits child traffic BEFORE the item that registers the child", () => {
62 // Ordering hazard: the child's own thread/status/changed arrives before
63 // the parent-side subAgentActivity naming it. Registration must tolerate
64 // child-first arrival, so unregistered child traffic passes through
65 // rather than being eaten (no regression vs. pre-feature behavior).
66 const firstChildTraffic = notifications.findIndex((entry) => {
67 const threadId = notificationThreadId(entry);
68 return threadId !== undefined && childThreadIds.has(threadId);
69 });
70 const firstRegistration = notifications.findIndex((entry) => {
71 const item = entry.params.item;
72 if (typeof item !== "object" || item === null) return false;
73 const record = item as Record<string, unknown>;
74 return record.type === "subAgentActivity" && record.kind === "started";
75 });
76 assert.isAtLeast(firstChildTraffic, 0);
77 assert.isAtLeast(firstRegistration, 0);
78 assert.isBelow(
79 firstChildTraffic,
80 firstRegistration,
81 "capture should exercise child-first ordering",
82 );
83 });
84​
85 it("contains a /root self-activity emitted from a CHILD thread", () => {
86 // The bug this guards: the wire reports subAgentActivity about the ROOT
87 // (agentPath "/root"). Registering it made the runtime intercept the
88 // parent's own final message and turn/completed, so the thread hung
89 // "working" forever. The root guard must key on agentPath/thread id,
90 // never on which thread the notification arrived from.
91 const rootSelfActivity = subAgentActivityItems().find((item) => item.agentPath === "/root");
92 assert.isDefined(rootSelfActivity, "capture should contain a /root self-activity");
93 assert.equal(rootSelfActivity?.agentThreadId, rootThreadId);
94 });
95​
96 it("routes every captured child method to a defined disposition", () => {
97 const childMethods = new Set(
98 notifications
99 .filter((entry) => {
100 const threadId = notificationThreadId(entry);
101 return threadId !== undefined && childThreadIds.has(threadId);
102 })
103 .map((entry) => entry.method),
104 );
105 assert.isAbove(childMethods.size, 0);
106 for (const method of childMethods) {
107 const route = routeCodexChildNotification(method);
108 // Child lifecycle traffic must become agent events — never silently
109 // dropped, never leaked to the parent timeline.
110 assert.equal(route, "agent-event", `${method} should map to an agent event`);
111 }
112 });
113});
114​
115describe("routeCodexChildNotification", () => {
116 it("maps child lifecycle to agent events", () => {
117 for (const method of [
118 "turn/started",
119 "turn/completed",
120 "thread/status/changed",
121 "thread/tokenUsage/updated",
122 "item/started",
123 "item/completed",
124 "thread/closed",
125 "error",
126 ]) {
127 assert.equal(routeCodexChildNotification(method), "agent-event", method);
128 }
129 });
130​
131 it("drops only enumerated child chatter", () => {
132 for (const method of [
133 "item/agentMessage/delta",
134 "item/reasoning/textDelta",
135 "item/commandExecution/outputDelta",
136 "turn/plan/updated",
137 "thread/name/updated",
138 ]) {
139 assert.equal(routeCodexChildNotification(method), "drop", method);
140 }
141 });
142​
143 it("never routes child-owned thread lifecycle to the parent", () => {
144 // These mutate PARENT thread state in CodexAdapter (archived/compacted),
145 // so a child emitting them must never reach the parent path. This list
146 // mirrors shouldSuppressChildConversationNotification (the v1 collab
147 // suppressor) — the two must not drift (review finding: the router
148 // initially omitted them and they leaked).
149 for (const method of [
150 "thread/started",
151 "thread/status/changed",
152 "thread/archived",
153 "thread/unarchived",
154 "thread/closed",
155 "thread/compacted",
156 "thread/name/updated",
157 "thread/tokenUsage/updated",
158 "turn/started",
159 "turn/completed",
160 "turn/plan/updated",
161 "item/plan/delta",
162 ]) {
163 assert.notEqual(
164 routeCodexChildNotification(method),
165 "parent",
166 `${method} is child-owned and must not reach the parent path`,
167 );
168 }
169 });
170​
171 it("sends parent-owned and UNKNOWN methods to the parent path", () => {
172 // serverRequest/resolved clears the parent's approval correlation:
173 // swallowing it left approvals stuck (shipped bug). Unknown methods take
174 // the same route by design — a codex update that adds a notification
175 // must degrade to "parent sees it", never to silent loss.
176 assert.equal(routeCodexChildNotification("serverRequest/resolved"), "parent");
177 assert.equal(routeCodexChildNotification("thread/somethingBrandNew"), "parent");
178 assert.equal(routeCodexChildNotification("account/rateLimits/updated"), "parent");
179 });
180});