Skip to content

Commit ed1d6b3

Browse files
committed
fix(codex): mirror V2 native subagent tasks
1 parent 37eb252 commit ed1d6b3

4 files changed

Lines changed: 238 additions & 34 deletions

File tree

‎extensions/codex/src/app-server/native-subagent-monitor.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,60 @@ describe("CodexNativeSubagentMonitor", () => {
233233
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
234234
});
235235

236+
it("registers Codex multi-agent V2 children from subagent activity", async () => {
237+
const client = createClient();
238+
const runtime = createRuntime();
239+
const monitor = new CodexNativeSubagentMonitor(client, runtime);
240+
monitor.registerParent({
241+
parentThreadId: "parent-thread",
242+
requesterSessionKey: "agent:main:main",
243+
taskRuntimeScope: createTaskScope("agent:main:main"),
244+
agentId: "main",
245+
});
246+
247+
await client.notify({
248+
method: "item/completed",
249+
params: {
250+
threadId: "parent-thread",
251+
item: {
252+
type: "subAgentActivity",
253+
id: "activity-started",
254+
kind: "started",
255+
agentThreadId: "child-v2",
256+
agentPath: "/root/researcher",
257+
},
258+
},
259+
});
260+
await client.notify(
261+
nativeCompletionNotification({
262+
agentPath: "/root/researcher",
263+
statusLabel: "completed",
264+
result: "child v2 result",
265+
}),
266+
);
267+
268+
expect(runtime.createRunningTaskRun).toHaveBeenCalledWith(
269+
expect.objectContaining({
270+
runId: "codex-thread:child-v2",
271+
task: "Codex native subagent /root/researcher",
272+
}),
273+
);
274+
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith(
275+
expect.objectContaining({
276+
runId: "codex-thread:child-v2",
277+
status: "succeeded",
278+
terminalSummary: "child v2 result",
279+
}),
280+
);
281+
expect(runtime.deliverAgentHarnessTaskCompletion).toHaveBeenCalledWith(
282+
expect.objectContaining({
283+
childSessionId: "child-v2",
284+
result: "child v2 result",
285+
}),
286+
);
287+
monitor.dispose();
288+
});
289+
236290
it.each([
237291
{ label: "remote V1", codexHome: undefined, finalizes: true },
238292
{ label: "local transcript-backed V1", codexHome: "/tmp/codex-home", finalizes: false },

‎extensions/codex/src/app-server/native-subagent-monitor.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,20 @@ export class CodexNativeSubagentMonitor {
321321
: undefined;
322322
const state = parentThreadId ? this.parentStates.get(parentThreadId) : undefined;
323323
if (state && parentThreadId) {
324+
// Codex multi-agent V2 exposes the child only through this parent-scoped
325+
// activity item; its later wait item has no receiver thread ids.
326+
if (
327+
notification.method === "item/completed" &&
328+
readString(item, "type") === "subAgentActivity"
329+
) {
330+
const childThreadId = readString(item, "agentThreadId")?.trim();
331+
if (childThreadId) {
332+
this.registerChildThread(parentThreadId, childThreadId, {
333+
agentPath: readString(item, "agentPath"),
334+
});
335+
}
336+
return state;
337+
}
324338
const isSpawnAgentTool = normalizeToolName(readString(item, "tool")) === "spawnagent";
325339
const childThreadIds = isSpawnAgentTool
326340
? new Set([

‎extensions/codex/src/app-server/native-subagent-task-mirror.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,79 @@ describe("CodexNativeSubagentTaskMirror", () => {
291291
expect(runtime.finalizeTaskRunByRunId).not.toHaveBeenCalled();
292292
});
293293

294+
it("mirrors Codex multi-agent V2 activity lifecycle", () => {
295+
const runtime = createRuntime();
296+
const mirror = new CodexNativeSubagentTaskMirror(
297+
{
298+
parentThreadId: "parent-thread",
299+
requesterSessionKey: "agent:main:main",
300+
agentId: "main",
301+
now: () => 41_000,
302+
},
303+
runtime,
304+
);
305+
for (const kind of ["started", "interacted", "interrupted"] as const) {
306+
for (const method of ["item/started", "item/completed"] as const) {
307+
mirror.handleNotification({
308+
method,
309+
params: {
310+
threadId: "parent-thread",
311+
item: {
312+
type: "subAgentActivity",
313+
id: `activity-${kind}`,
314+
kind,
315+
agentThreadId: "child-v2",
316+
agentPath: "/root/researcher",
317+
},
318+
},
319+
});
320+
}
321+
}
322+
for (const threadId of ["parent-thread", "other-parent"]) {
323+
mirror.handleNotification({
324+
method: "item/completed",
325+
params: {
326+
threadId,
327+
item: {
328+
type: "subAgentActivity",
329+
kind: "started",
330+
agentThreadId: threadId === "parent-thread" ? "child-v2" : "other-child",
331+
agentPath: "/root/researcher",
332+
},
333+
},
334+
});
335+
}
336+
337+
expect(runtime.tryCreateRunningTaskRun).toHaveBeenCalledTimes(1);
338+
expect(runtime.tryCreateRunningTaskRun).toHaveBeenCalledWith({
339+
sourceId: "codex-thread:child-v2",
340+
agentId: "main",
341+
runId: "codex-thread:child-v2",
342+
label: "Codex subagent",
343+
task: "Codex native subagent /root/researcher",
344+
notifyPolicy: "silent",
345+
deliveryStatus: "not_applicable",
346+
preferMetadata: true,
347+
startedAt: 41_000,
348+
lastEventAt: 41_000,
349+
progressSummary: "Codex native subagent started.",
350+
});
351+
expect(runtime.recordTaskRunProgressByRunId).toHaveBeenCalledWith({
352+
runId: "codex-thread:child-v2",
353+
lastEventAt: 41_000,
354+
progressSummary: "Codex native subagent received more input.",
355+
});
356+
expect(runtime.finalizeTaskRunByRunId).toHaveBeenCalledWith({
357+
runId: "codex-thread:child-v2",
358+
status: "cancelled",
359+
endedAt: 41_000,
360+
lastEventAt: 41_000,
361+
error: "Codex native subagent was interrupted.",
362+
progressSummary: "Codex native subagent was interrupted.",
363+
terminalSummary: "Codex native subagent was interrupted.",
364+
});
365+
});
366+
294367
it("uses the notification thread id when collab agent items omit sender thread id", () => {
295368
const runtime = createRuntime();
296369
const mirror = new CodexNativeSubagentTaskMirror(

‎extensions/codex/src/app-server/native-subagent-task-mirror.ts‎

Lines changed: 97 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export class CodexNativeSubagentTaskMirror {
7676
return;
7777
}
7878
if (notification.method === "item/started" || notification.method === "item/completed") {
79+
const item = isJsonObject(params.item) ? params.item : undefined;
80+
if (
81+
notification.method === "item/completed" &&
82+
item &&
83+
readString(item, "type") === "subAgentActivity"
84+
) {
85+
this.handleSubagentActivityItem(params);
86+
return;
87+
}
7988
this.handleCollabAgentItem(params);
8089
}
8190
}
@@ -91,11 +100,6 @@ export class CodexNativeSubagentTaskMirror {
91100
return;
92101
}
93102
const threadId = thread.id.trim();
94-
if (!threadId || this.mirrorStateByThreadId.get(threadId) === "mirrored") {
95-
return;
96-
}
97-
this.mirrorStateByThreadId.set(threadId, "mirrored");
98-
const runId = codexNativeSubagentRunId(threadId);
99103
const label =
100104
trimOptional(spawn.agent_nickname) ??
101105
trimOptional(thread.agentNickname) ??
@@ -106,25 +110,17 @@ export class CodexNativeSubagentTaskMirror {
106110
trimOptional(thread.preview) ??
107111
`Codex native subagent${label === "Codex subagent" ? "" : ` ${label}`}`;
108112
const createdAt = secondsToMillis(thread.createdAt) ?? this.now();
109-
const taskRecord = this.runtime.tryCreateRunningTaskRun({
110-
sourceId: runId,
111-
agentId: this.params.agentId,
112-
runId,
113-
label,
114-
task,
115-
notifyPolicy: "silent",
116-
deliveryStatus: "not_applicable",
117-
preferMetadata: true,
118-
startedAt: createdAt,
119-
lastEventAt: this.now(),
120-
progressSummary: "Codex native subagent started.",
121-
});
122-
if (!taskRecord) {
123-
this.mirrorStateByThreadId.set(threadId, "failed");
113+
if (
114+
!this.createRunningTask({
115+
threadId,
116+
label,
117+
task,
118+
startedAt: createdAt,
119+
progressSummary: "Codex native subagent started.",
120+
})
121+
) {
124122
return;
125123
}
126-
this.terminalRunIds.delete(runId);
127-
this.authoritativeRunIds.delete(runId);
128124
this.applyStatus(threadId, thread.status);
129125
}
130126

@@ -244,34 +240,94 @@ export class CodexNativeSubagentTaskMirror {
244240
}
245241
}
246242

247-
private createTaskFromCollabSpawnItem(threadId: string, item: JsonObject): void {
248-
const normalizedThreadId = threadId.trim();
249-
if (!normalizedThreadId || this.mirrorStateByThreadId.get(normalizedThreadId) === "mirrored") {
243+
private handleSubagentActivityItem(params: JsonObject): void {
244+
const item = isJsonObject(params.item) ? params.item : undefined;
245+
if (
246+
!item ||
247+
readString(item, "type") !== "subAgentActivity" ||
248+
readString(params, "threadId") !== this.params.parentThreadId
249+
) {
250+
return;
251+
}
252+
const threadId = trimOptional(readString(item, "agentThreadId"));
253+
const kind = normalizeSubagentActivityKind(readString(item, "kind"));
254+
if (!threadId || !kind) {
255+
return;
256+
}
257+
if (kind === "started") {
258+
this.createTaskFromSubagentActivity(threadId, trimOptional(readString(item, "agentPath")));
259+
return;
260+
}
261+
if (this.mirrorStateByThreadId.get(threadId) !== "mirrored") {
250262
return;
251263
}
252-
this.mirrorStateByThreadId.set(normalizedThreadId, "mirrored");
264+
const message =
265+
kind === "interacted"
266+
? "Codex native subagent received more input."
267+
: "Codex native subagent was interrupted.";
268+
this.applyCollabAgentStatus(
269+
threadId,
270+
kind === "interacted" ? "running" : "interrupted",
271+
message,
272+
);
273+
}
274+
275+
private createTaskFromSubagentActivity(threadId: string, agentPath: string | undefined): void {
276+
const eventAt = this.now();
277+
this.createRunningTask({
278+
threadId,
279+
label: "Codex subagent",
280+
task: agentPath ? `Codex native subagent ${agentPath}` : "Codex native subagent",
281+
startedAt: eventAt,
282+
progressSummary: "Codex native subagent started.",
283+
});
284+
}
285+
286+
private createTaskFromCollabSpawnItem(threadId: string, item: JsonObject): void {
253287
const prompt = trimOptional(readString(item, "prompt"));
254-
const runId = codexNativeSubagentRunId(normalizedThreadId);
255288
const createdAt = this.now();
289+
this.createRunningTask({
290+
threadId,
291+
label: "Codex subagent",
292+
task: prompt ?? "Codex native subagent",
293+
startedAt: createdAt,
294+
progressSummary: "Codex native subagent spawned.",
295+
});
296+
}
297+
298+
private createRunningTask(params: {
299+
threadId: string;
300+
label: string;
301+
task: string;
302+
startedAt: number;
303+
progressSummary: string;
304+
}): boolean {
305+
const threadId = params.threadId.trim();
306+
if (!threadId || this.mirrorStateByThreadId.get(threadId) === "mirrored") {
307+
return false;
308+
}
309+
this.mirrorStateByThreadId.set(threadId, "mirrored");
310+
const runId = codexNativeSubagentRunId(threadId);
256311
const taskRecord = this.runtime.tryCreateRunningTaskRun({
257312
sourceId: runId,
258313
agentId: this.params.agentId,
259314
runId,
260-
label: "Codex subagent",
261-
task: prompt ?? "Codex native subagent",
315+
label: params.label,
316+
task: params.task,
262317
notifyPolicy: "silent",
263318
deliveryStatus: "not_applicable",
264319
preferMetadata: true,
265-
startedAt: createdAt,
266-
lastEventAt: createdAt,
267-
progressSummary: "Codex native subagent spawned.",
320+
startedAt: params.startedAt,
321+
lastEventAt: this.now(),
322+
progressSummary: params.progressSummary,
268323
});
269324
if (!taskRecord) {
270-
this.mirrorStateByThreadId.set(normalizedThreadId, "failed");
271-
return;
325+
this.mirrorStateByThreadId.set(threadId, "failed");
326+
return false;
272327
}
273328
this.terminalRunIds.delete(runId);
274329
this.authoritativeRunIds.delete(runId);
330+
return true;
275331
}
276332

277333
private applyCollabAgentStatus(
@@ -451,6 +507,13 @@ function normalizeToolName(value: string | undefined): string | undefined {
451507
return value?.replace(/[^a-z0-9]/giu, "").toLowerCase();
452508
}
453509

510+
function normalizeSubagentActivityKind(
511+
value: string | undefined,
512+
): "started" | "interacted" | "interrupted" | undefined {
513+
const key = value?.replace(/[^a-z]/giu, "").toLowerCase();
514+
return key === "started" || key === "interacted" || key === "interrupted" ? key : undefined;
515+
}
516+
454517
function normalizeCollabToolCallStatus(value: string | undefined): string | undefined {
455518
const key = value?.replace(/[^a-z0-9]/giu, "").toLowerCase();
456519
if (key === "completed" || key === "succeeded" || key === "success") {

0 commit comments

Comments
 (0)