Compare commits

...
Author SHA1 Message Date
maria-rcks 355a1c5985 fix(server): dedupe codex subagent item events 2026-08-06 22:56:22 -04:00
2 changed files with 95 additions and 2 deletions

No files matched your search

@@ -34,6 +34,36 @@ const [CHILD_A, CHILD_B] = wireFixture.childThreadIds as [string, string];
*/
function buildScript() {
const captured = wireFixture.notifications;
// Re-address the capture's schema-valid tool lifecycle pair to child A.
// Hand-written item shapes can drift from the generated Codex schema.
const childToolLifecycle = captured
.filter(
(entry) =>
(entry.method === "item/started" || entry.method === "item/completed") &&
(entry.params as { item?: { type?: string } }).item?.type === "collabAgentToolCall",
)
.map((entry) => {
const params = entry.params as {
readonly threadId: string;
readonly turnId: string;
readonly item: Record<string, unknown>;
};
return {
...entry,
params: {
...params,
threadId: CHILD_A,
turnId: `${CHILD_A}-turn-1`,
item: {
...params.item,
id: "call_fixture_child_tool",
senderThreadId: CHILD_A,
},
},
};
});
const childCompletionOnly = childToolLifecycle.find((entry) => entry.method === "item/completed");
assert.isDefined(childCompletionOnly);
const extras = [
{
method: "item/completed",
@@ -49,6 +79,19 @@ function buildScript() {
},
},
},
// Codex emits started + completed for one child item. They are one
// logical activity and must not become two synthetic agent updates.
...childToolLifecycle,
{
...childCompletionOnly,
params: {
...childCompletionOnly.params,
item: {
...childCompletionOnly.params.item,
id: "call_fixture_child_completion_only",
},
},
},
// Child terminal lifecycle AFTER the receiver map knows the children —
// pre-fix, the legacy suppressor dropped these before interception saw
// them, so no synthetic agent events were emitted.
@@ -123,6 +166,29 @@ describe("CodexSessionRuntime collab integration", () => {
);
assert.isDefined(childClosed, "child B's close becomes an agent event");
const childToolItems = events.filter(
(event) =>
event.method === "collabAgent/item" &&
(event.payload as { item?: { id?: string } }).item?.id === "call_fixture_child_tool",
);
assert.lengthOf(childToolItems, 1, "one child item must emit one agent activity");
assert.equal(childToolItems[0]?.itemId, "call_fixture_child_tool");
assert.equal(
(childToolItems[0]?.payload as { itemLifecycle?: string }).itemLifecycle,
"started",
);
const completionOnly = events.find(
(event) =>
event.method === "collabAgent/item" &&
(event.payload as { item?: { id?: string } }).item?.id ===
"call_fixture_child_completion_only",
);
assert.equal(
(completionOnly?.payload as { itemLifecycle?: string }).itemLifecycle,
"completed",
);
// Parent-owned resolution passes through — not swallowed, not
// re-labelled as an agent event.
assert.include(methods, "serverRequest/resolved");
@@ -855,6 +855,8 @@ export const makeCodexSessionRuntime = (
const pendingUserInputsRef = yield* Ref.make(new Map<ApprovalRequestId, PendingUserInput>());
const collabReceiverTurnsRef = yield* Ref.make(new Map<string, TurnId>());
const collabChildAgentsRef = yield* Ref.make(new Map<string, CollabChildAgentState>());
/** Child provider-thread id → item ids already emitted during its lifetime. */
const collabChildSeenItemsRef = yield* Ref.make(new Map<string, ReadonlySet<string>>());
/** Child provider-thread id → its currently running provider turn id. */
const collabChildLiveTurnsRef = yield* Ref.make(new Map<string, string>());
const closedRef = yield* Ref.make(false);
@@ -1179,18 +1181,38 @@ export const makeCodexSessionRuntime = (
});
return true;
case "item/started":
case "item/completed":
case "item/completed": {
const item = notification.params.item;
const shouldEmit = yield* Ref.modify(collabChildSeenItemsRef, (current) => {
const seen = current.get(child.agentThreadId);
if (seen?.has(item.id) === true) {
return [false, current];
}
const next = new Map(current);
next.set(child.agentThreadId, new Set([...(seen ?? []), item.id]));
return [true, next];
});
// Codex emits both lifecycle notifications for the same child
// tool item. The adapter turns each synthetic event into agent
// activity, so only the first one is useful; completion-only
// items still pass through when no start was observed.
if (!shouldEmit) {
return true;
}
yield* emitEvent({
kind: "notification",
threadId: options.threadId,
itemId: ProviderItemId.make(item.id),
...(child.spawnTurnId ? { turnId: child.spawnTurnId } : {}),
method: "collabAgent/item",
payload: {
...childIdentity,
item: notification.params.item,
itemLifecycle: notification.method === "item/started" ? "started" : "completed",
item,
},
});
return true;
}
case "thread/closed":
// The child is gone: drop its live-turn entry so a later Stop
// doesn't waste a turn/interrupt RPC on a closed thread before
@@ -1200,6 +1222,11 @@ export const makeCodexSessionRuntime = (
next.delete(child.agentThreadId);
return next;
});
yield* Ref.update(collabChildSeenItemsRef, (current) => {
const next = new Map(current);
next.delete(child.agentThreadId);
return next;
});
yield* emitEvent({
kind: "notification",
threadId: options.threadId,