Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("CodexAppServerAgent", () => {
});
});

it("isolates subagent output, usage, compaction, and completion", async () => {
it("surfaces subagent activity while isolating its lifecycle state", async () => {
const stub = makeStubRpc({
initialize: {},
"thread/start": { thread: { id: "thr_1" } },
Expand Down Expand Up @@ -191,7 +191,6 @@ describe("CodexAppServerAgent", () => {
prompt: "Review the implementation",
},
});
const sessionUpdateCount = sessionUpdates.length;
const extNotificationCount = extNotifications.length;

stub.emit("item/agentMessage/delta", {
Expand All @@ -215,6 +214,16 @@ describe("CodexAppServerAgent", () => {
text: '{"source":"child"}',
},
});
stub.emit("item/started", {
threadId: "subagent_1",
turnId: "subagent_turn_1",
item: {
type: "commandExecution",
id: "shared_command_id",
command: "echo child",
status: "inProgress",
},
});
stub.emit("item/commandExecution/outputDelta", {
threadId: "subagent_1",
turnId: "subagent_turn_1",
Expand Down Expand Up @@ -251,11 +260,9 @@ describe("CodexAppServerAgent", () => {
expect({
extNotifications: extNotifications.length,
promptSettled,
sessionUpdates: sessionUpdates.length,
}).toEqual({
extNotifications: extNotificationCount,
promptSettled: false,
sessionUpdates: sessionUpdateCount,
});

stub.emit("item/agentMessage/delta", {
Expand Down Expand Up @@ -292,10 +299,14 @@ describe("CodexAppServerAgent", () => {
await expect(promptDone).resolves.toMatchObject({ stopReason: "end_turn" });
const serializedUpdates = JSON.stringify(sessionUpdates);
expect(serializedUpdates).toContain("spawn_agent");
expect(serializedUpdates).toContain("subagent prose");
expect(serializedUpdates).toContain("subagent reasoning");
expect(serializedUpdates).toContain("child command output");
expect(serializedUpdates).toContain(
"subagent:subagent_1:shared_command_id",
);
expect(serializedUpdates).toContain('"parentToolCallId":"spawn_1"');
expect(serializedUpdates).toContain("parent response");
expect(serializedUpdates).not.toContain("subagent prose");
expect(serializedUpdates).not.toContain("subagent reasoning");
expect(serializedUpdates).not.toContain("child command output");
expect(structuredOutputs).toEqual([{ source: "parent" }]);
expect(
extNotifications.filter(
Expand All @@ -304,6 +315,142 @@ describe("CodexAppServerAgent", () => {
).toHaveLength(1);
});

it.each(["resumeAgent", "sendInput"])(
"attaches child activity to the current %s call",
async (collaborationTool) => {
let turnNumber = 0;
const stub = makeStubRpc({
initialize: {},
"thread/start": { thread: { id: "thr_1" } },
"turn/start": () => ({
turn: {
id: `turn_${++turnNumber}`,
status: "inProgress",
},
}),
});
const { client, sessionUpdates } = makeFakeClient();
const agent = new CodexAppServerAgent(client, {
processOptions: { binaryPath: "/bundle/codex" },
model: "gpt-5.5",
rpcFactory: stub.factory,
});

await agent.initialize(init);
await agent.newSession({ cwd: "/repo" } as unknown as NewSessionRequest);

const firstPrompt = agent.prompt({
sessionId: "thr_1",
prompt: [{ type: "text", text: "spawn" }],
} as unknown as PromptRequest);
stub.emit("item/started", {
threadId: "thr_1",
turnId: "turn_1",
item: {
type: "collabAgentToolCall",
id: "spawn_1",
tool: "spawnAgent",
receiverThreadIds: ["subagent_1"],
status: "inProgress",
},
});
stub.emit("turn/completed", {
threadId: "thr_1",
turn: { id: "turn_1", status: "completed" },
});
await firstPrompt;

const secondPrompt = agent.prompt({
sessionId: "thr_1",
prompt: [{ type: "text", text: "continue" }],
} as unknown as PromptRequest);
const currentCallId = `${collaborationTool}_1`;
stub.emit("item/started", {
threadId: "thr_1",
turnId: "turn_2",
item: {
type: "collabAgentToolCall",
id: currentCallId,
tool: collaborationTool,
receiverThreadIds: ["subagent_1"],
status: "inProgress",
},
});
stub.emit("item/agentMessage/delta", {
threadId: "subagent_1",
turnId: "subagent_turn_2",
itemId: "message_2",
delta: "continued work",
});

expect(JSON.stringify(sessionUpdates)).toContain(
`"parentToolCallId":"${currentCallId}"`,
);

stub.emit("turn/completed", {
threadId: "thr_1",
turn: { id: "turn_2", status: "completed" },
});
await secondPrompt;
},
);

it("buffers child activity until its parent tool call arrives", async () => {
const stub = makeStubRpc({
initialize: {},
"thread/start": { thread: { id: "thr_1" } },
"turn/start": { turn: { id: "turn_1", status: "inProgress" } },
});
const { client, sessionUpdates } = makeFakeClient();
const agent = new CodexAppServerAgent(client, {
processOptions: { binaryPath: "/bundle/codex" },
model: "gpt-5.5",
rpcFactory: stub.factory,
});

await agent.initialize(init);
await agent.newSession({ cwd: "/repo" } as unknown as NewSessionRequest);
const promptDone = agent.prompt({
sessionId: "thr_1",
prompt: [{ type: "text", text: "delegate" }],
} as unknown as PromptRequest);

stub.emit("item/agentMessage/delta", {
threadId: "subagent_1",
turnId: "subagent_turn_1",
itemId: "message_1",
delta: "early child activity",
});
expect(JSON.stringify(sessionUpdates)).not.toContain(
"early child activity",
);

stub.emit("item/started", {
threadId: "thr_1",
turnId: "turn_1",
item: {
type: "collabAgentToolCall",
id: "spawn_1",
tool: "spawnAgent",
receiverThreadIds: ["subagent_1"],
status: "inProgress",
},
});

const serializedUpdates = JSON.stringify(sessionUpdates);
expect(serializedUpdates).toContain("early child activity");
expect(serializedUpdates).toContain('"parentToolCallId":"spawn_1"');
expect(serializedUpdates.indexOf("spawn_agent")).toBeLessThan(
serializedUpdates.indexOf("early child activity"),
);

stub.emit("turn/completed", {
threadId: "thr_1",
turn: { id: "turn_1", status: "completed" },
});
await promptDone;
});

it.each([
{
label: "reads an empty goal",
Expand Down Expand Up @@ -2466,6 +2613,53 @@ describe("CodexAppServerAgent", () => {
});
});

it("restores subagent relationships from resumed thread history", async () => {
const stub = makeStubRpc({
initialize: {},
"thread/resume": {
thread: {
id: "t1",
turns: [
{
items: [
{
type: "collabAgentToolCall",
id: "spawn_1",
tool: "spawnAgent",
receiverThreadIds: ["subagent_1"],
status: "completed",
},
],
},
],
},
},
});
const { client, sessionUpdates } = makeFakeClient();
const agent = new CodexAppServerAgent(client, {
processOptions: { binaryPath: "/x/codex" },
model: "gpt-5.5",
rpcFactory: stub.factory,
});
await agent.initialize(init);
await agent.resumeSession({
sessionId: "t1",
cwd: "/r",
mcpServers: [],
} as unknown as Parameters<typeof agent.resumeSession>[0]);

stub.emit("item/agentMessage/delta", {
threadId: "subagent_1",
turnId: "subagent_turn_1",
itemId: "message_1",
delta: "still working",
});

expect(JSON.stringify(sessionUpdates)).toContain(
'"parentToolCallId":"spawn_1"',
);
});

it("forwards additionalDirectories to thread/start as writable_roots", async () => {
const stub = makeStubRpc({ "thread/start": { thread: { id: "t" } } });
const { client } = makeFakeClient();
Expand Down
Loading
Loading