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

CodexCollabWire.test.ts

184 lines7.2 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 "thread/settings/updated",
123 "model/rerouted",
124 "item/started",
125 "item/completed",
126 "thread/closed",
127 "error",
128 ]) {
129 assert.equal(routeCodexChildNotification(method), "agent-event", method);
130 }
131 });
132​
133 it("drops only enumerated child chatter", () => {
134 for (const method of [
135 "item/agentMessage/delta",
136 "item/reasoning/textDelta",
137 "item/commandExecution/outputDelta",
138 "turn/plan/updated",
139 "thread/name/updated",
140 ]) {
141 assert.equal(routeCodexChildNotification(method), "drop", method);
142 }
143 });
144​
145 it("never routes child-owned thread lifecycle to the parent", () => {
146 // These mutate PARENT thread state in CodexAdapter (archived/compacted),
147 // so a child emitting them must never reach the parent path. This list
148 // mirrors shouldSuppressChildConversationNotification (the v1 collab
149 // suppressor) — the two must not drift (review finding: the router
150 // initially omitted them and they leaked).
151 for (const method of [
152 "thread/started",
153 "thread/status/changed",
154 "thread/archived",
155 "thread/unarchived",
156 "thread/closed",
157 "thread/compacted",
158 "thread/name/updated",
159 "thread/tokenUsage/updated",
160 "turn/started",
161 "turn/completed",
162 "turn/plan/updated",
163 "item/plan/delta",
164 "thread/settings/updated",
165 "model/rerouted",
166 ]) {
167 assert.notEqual(
168 routeCodexChildNotification(method),
169 "parent",
170 `${method} is child-owned and must not reach the parent path`,
171 );
172 }
173 });
174​
175 it("sends parent-owned and UNKNOWN methods to the parent path", () => {
176 // serverRequest/resolved clears the parent's approval correlation:
177 // swallowing it left approvals stuck (shipped bug). Unknown methods take
178 // the same route by design — a codex update that adds a notification
179 // must degrade to "parent sees it", never to silent loss.
180 assert.equal(routeCodexChildNotification("serverRequest/resolved"), "parent");
181 assert.equal(routeCodexChildNotification("thread/somethingBrandNew"), "parent");
182 assert.equal(routeCodexChildNotification("account/rateLimits/updated"), "parent");
183 });
184});