Map nested Codex subagent threads correctly

- Preserve app-thread projection when spawning Codex subagents
- Add replay fixture coverage for nested provider-native subagents
This commit is contained in:
Julius Marminge authored and maria-rcks committed 2026-08-03 11:45:06 -04:00
1 parent 9479d6f7cc
commit ff09cda0f8
5 files changed
+227 -27

No files matched your search

@@ -4,6 +4,7 @@ import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
import { resolveSpawnCommand } from "@t3tools/shared/shell";
import type {
ChatAttachment,
OrchestrationV2AppThread,
OrchestrationV2ConversationMessage,
OrchestrationV2ExecutionNode,
ModelSelection,
@@ -714,6 +715,7 @@ export const resolveCodexRollbackTurnCount = Effect.fn("CodexAdapterV2.resolveRo
interface ActiveCodexTurnContext {
readonly input: ProviderAdapterV2TurnInput;
readonly projectionAppThread: OrchestrationV2AppThread;
readonly projectionThreadId: ThreadId;
readonly projectionRunId: ProviderAdapterV2TurnInput["runId"] | null;
readonly nativeTurnId: string;
@@ -731,6 +733,7 @@ interface ActiveCodexTurnContext {
interface CodexSubagentThreadContext {
readonly parentContext: ActiveCodexTurnContext;
readonly providerThread: OrchestrationV2ProviderThread;
readonly childThread: OrchestrationV2AppThread;
readonly subagentNodeId: OrchestrationV2ExecutionNode["id"];
readonly childRootNodeId: OrchestrationV2ExecutionNode["id"];
readonly childThreadId: ThreadId;
@@ -1309,6 +1312,7 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
});
const context: ActiveCodexTurnContext = {
input: input.turnInput,
projectionAppThread: input.turnInput.appThread,
projectionThreadId: input.turnInput.threadId,
projectionRunId: input.turnInput.runId,
nativeTurnId: input.nativeTurnId,
@@ -1487,6 +1491,7 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
});
const activeContext: ActiveCodexTurnContext = {
input: subagent.parentContext.input,
projectionAppThread: subagent.childThread,
projectionThreadId: subagent.childThreadId,
projectionRunId: null,
nativeTurnId: turn.nativeTurnId,
@@ -1640,9 +1645,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
} satisfies OrchestrationV2ProviderThread;
const task = {
id: subagentNodeId,
threadId: input.context.input.threadId,
runId: input.context.input.runId,
parentNodeId: input.context.rootNodeId,
threadId: input.context.projectionThreadId,
runId: input.context.projectionRunId,
parentNodeId: input.context.itemParentNodeId,
origin: "provider_native",
createdBy: "agent",
driver: CODEX_PROVIDER,
@@ -1659,9 +1664,30 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
completedAt: null,
updatedAt: now,
} satisfies OrchestrationV2Subagent;
const childThread = makeSubagentChildThread({
parentThread: input.context.projectionAppThread,
childThreadId,
parentNodeId: subagentNodeId,
activeProviderThreadId: providerThread.id,
providerInstanceId: input.context.input.modelSelection.instanceId,
modelSelection: {
...input.context.input.modelSelection,
model: task.model ?? input.context.input.modelSelection.model,
},
title: subagentThreadTitle({
parentTitle: input.context.projectionAppThread.title,
prompt: task.prompt,
title: task.title,
ordinal: input.ordinal,
}),
now,
createdBy: "agent",
creationSource: "provider",
});
const subagent = {
parentContext: input.context,
providerThread,
childThread,
subagentNodeId,
childRootNodeId,
childThreadId,
@@ -1681,26 +1707,6 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
updated.set(input.nativeThreadId, subagent);
return updated;
});
const childThread = makeSubagentChildThread({
parentThread: input.context.input.appThread,
childThreadId,
parentNodeId: subagentNodeId,
activeProviderThreadId: providerThread.id,
providerInstanceId: input.context.input.modelSelection.instanceId,
modelSelection: {
...input.context.input.modelSelection,
model: task.model ?? input.context.input.modelSelection.model,
},
title: subagentThreadTitle({
parentTitle: input.context.input.appThread.title,
prompt: task.prompt,
title: task.title,
ordinal: input.ordinal,
}),
now,
createdBy: "agent",
creationSource: "provider",
});
yield* emitProviderEvent({
type: "app_thread.created",
driver: CODEX_PROVIDER,
@@ -1711,9 +1717,9 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
driver: CODEX_PROVIDER,
node: {
id: subagentNodeId,
threadId: input.context.input.threadId,
runId: input.context.input.runId,
parentNodeId: input.context.rootNodeId,
threadId: input.context.projectionThreadId,
runId: input.context.projectionRunId,
parentNodeId: input.context.itemParentNodeId,
rootNodeId: input.context.rootNodeId,
kind: "subagent",
status: "running",
@@ -4070,7 +4076,7 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi
id: context.subagent.subagentNodeId,
threadId: context.subagent.parentContext.projectionThreadId,
runId: context.subagent.parentContext.projectionRunId,
parentNodeId: context.subagent.parentContext.rootNodeId,
parentNodeId: context.subagent.parentContext.itemParentNodeId,
rootNodeId: context.subagent.parentContext.rootNodeId,
kind: "subagent",
status,
@@ -149,6 +149,23 @@ const scenarioExpectations = {
turnCompletedCount: 2,
approvalRequestCount: 0,
},
subagent_v2_nested: {
outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
incoming: [
"turn/started",
"item/completed/root-subagent",
"turn/started/child",
"item/completed/child-subagent",
"turn/started/grandchild",
"item/completed/grandchild-subagent",
"turn/started/leaf",
"item/completed/leaf-answer",
"turn/completed",
],
turnStartCount: 1,
turnCompletedCount: 4,
approvalRequestCount: 0,
},
multi_turn: {
outgoing: ["initialize", "initialized", "thread/start", "turn/start"],
incoming: ["turn/started", "turn/completed", "item/agentMessage/delta"],
@@ -31,6 +31,7 @@ import { assertSubagentContinueOutput } from "./subagent_continue/codex_output.t
import { subagentContinueInput } from "./subagent_continue/input.ts";
import { assertSubagentV2Output } from "./subagent_v2/codex_output.ts";
import { subagentV2Input } from "./subagent_v2/input.ts";
import { assertSubagentV2NestedOutput } from "./subagent_v2_nested/codex_output.ts";
import { assertClaudeThreadRollbackOutput } from "./thread_rollback/claude_output.ts";
import { assertThreadRollbackOutput } from "./thread_rollback/codex_output.ts";
import { threadRollbackInput } from "./thread_rollback/input.ts";
@@ -336,6 +337,18 @@ export const ORCHESTRATOR_REPLAY_FIXTURES = [
},
],
},
{
name: "subagent_v2_nested",
buildInput: subagentV2Input,
providers: [
{
driver: ProviderDriverKind.make("codex"),
transcriptFile: new URL("./subagent_v2_nested/codex_transcript.ndjson", import.meta.url),
modelSelection: CODEX_MODEL_SELECTION,
assertOutput: assertSubagentV2NestedOutput,
},
],
},
{
name: "opencode_subagent",
buildInput: openCodeSubagentInput,
@@ -0,0 +1,131 @@
import { assert } from "@effect/vitest";
import type {
OrchestrationV2ThreadProjection,
ProviderReplayTranscript,
ThreadId,
} from "@t3tools/contracts";
import type { OrchestratorV2ScenarioResult } from "../../OrchestratorScenario.ts";
import {
assertBaseProjection,
assertNoExtraAppRunsForProviderChildren,
assertRunProviderTurnCardinality,
assertSemanticProjectionIntegrity,
assertTurnItemTypes,
assertUserMessagesInclude,
projectionFor,
SUBAGENT_V2_PROMPT,
} from "../shared.ts";
function projectionById(
result: OrchestratorV2ScenarioResult,
threadId: ThreadId,
): OrchestrationV2ThreadProjection {
const projection = result.projections.get(threadId);
assert.isDefined(projection, `missing projection for ${threadId}`);
return projection;
}
function assertCompletedProviderNativeSubagent(input: {
readonly projection: OrchestrationV2ThreadProjection;
readonly title: string;
readonly result: string;
}) {
assert.lengthOf(input.projection.subagents, 1);
assert.lengthOf(
input.projection.turnItems.filter((item) => item.type === "subagent"),
1,
`${input.projection.thread.id} must only render its direct subagent`,
);
const subagent = input.projection.subagents[0]!;
assert.equal(subagent.origin, "provider_native");
assert.equal(subagent.createdBy, "agent");
assert.equal(subagent.driver, "codex");
assert.equal(subagent.title, input.title);
assert.equal(subagent.status, "completed");
assert.equal(subagent.result, input.result);
assert.isNotNull(subagent.childThreadId);
assert.isNotNull(subagent.providerThreadId);
assert.isNotNull(subagent.nativeTaskRef);
assert.isNotNull(subagent.completedAt);
return subagent;
}
export function assertSubagentV2NestedOutput(
result: OrchestratorV2ScenarioResult,
transcript: ProviderReplayTranscript,
) {
assertBaseProjection({
result,
transcript,
runCount: 1,
runStatuses: ["completed"],
});
const rootProjection = projectionFor(result, transcript.scenario);
assertSemanticProjectionIntegrity(rootProjection);
assertTurnItemTypes(rootProjection, ["user_message", "subagent", "assistant_message"]);
assertRunProviderTurnCardinality({ projection: rootProjection, rootRunCount: 1 });
assertNoExtraAppRunsForProviderChildren({ projection: rootProjection, expectedAppRuns: 1 });
assertUserMessagesInclude(rootProjection, [SUBAGENT_V2_PROMPT]);
assert.lengthOf(result.shellSnapshot.threads, 4);
const first = assertCompletedProviderNativeSubagent({
projection: rootProjection,
title: "/root/hello_agent",
result: "Subagent says: “Hello.”",
});
if (first.childThreadId === null) {
throw new Error("first nested fixture subagent is missing its child thread");
}
const firstProjection = projectionById(result, first.childThreadId);
assert.equal(firstProjection.thread.lineage.parentThreadId, rootProjection.thread.id);
assert.equal(firstProjection.thread.lineage.relationshipToParent, "subagent");
assert.equal(firstProjection.thread.lineage.rootThreadId, rootProjection.thread.id);
assert.lengthOf(firstProjection.runs, 0);
assert.lengthOf(firstProjection.providerTurns, 1);
assertTurnItemTypes(firstProjection, ["subagent", "assistant_message"]);
const second = assertCompletedProviderNativeSubagent({
projection: firstProjection,
title: "/root/hello_agent/hello_agent",
result: "Subagent says: “Hello.”",
});
if (second.childThreadId === null) {
throw new Error("second nested fixture subagent is missing its child thread");
}
const secondProjection = projectionById(result, second.childThreadId);
assert.equal(secondProjection.thread.lineage.parentThreadId, firstProjection.thread.id);
assert.equal(secondProjection.thread.lineage.relationshipToParent, "subagent");
assert.equal(secondProjection.thread.lineage.rootThreadId, rootProjection.thread.id);
assert.lengthOf(secondProjection.runs, 0);
assert.lengthOf(secondProjection.providerTurns, 1);
assertTurnItemTypes(secondProjection, ["subagent", "assistant_message"]);
const third = assertCompletedProviderNativeSubagent({
projection: secondProjection,
title: "/root/hello_agent/hello_agent/hello_agent",
result: "Hello.",
});
if (third.childThreadId === null) {
throw new Error("third nested fixture subagent is missing its child thread");
}
const thirdProjection = projectionById(result, third.childThreadId);
assert.equal(thirdProjection.thread.lineage.parentThreadId, secondProjection.thread.id);
assert.equal(thirdProjection.thread.lineage.relationshipToParent, "subagent");
assert.equal(thirdProjection.thread.lineage.rootThreadId, rootProjection.thread.id);
assert.lengthOf(thirdProjection.runs, 0);
assert.lengthOf(thirdProjection.providerTurns, 1);
assert.lengthOf(thirdProjection.subagents, 0);
assertTurnItemTypes(thirdProjection, ["assistant_message"]);
assert.isTrue(
thirdProjection.turnItems.some(
(item) => item.type === "assistant_message" && item.text === "Hello.",
),
"leaf child thread must contain the final assistant message",
);
}
@@ -0,0 +1,33 @@
{"type":"transcript_start","provider":"codex","protocol":"codex.app-server","version":"0.141.0","scenario":"subagent_v2_nested","metadata":{"source":"observed-codex-app-server-replay-fixture","description":"Agents-v2 Codex nested subagent spawn: root emits a subAgentActivity, that child emits another, and that grandchild emits a final nested child."}}
{"type":"expect_outbound","label":"initialize","frame":{"id":1,"method":"initialize","params":{"capabilities":{"experimentalApi":true},"clientInfo":{"name":"t3code_desktop","title":"T3 Code Desktop","version":"0.1.0"}}}}
{"type":"emit_inbound","label":"initialize","frame":{"id":1,"result":{"userAgent":"t3code_desktop/0.141.0","codexHome":"/tmp/codex-home","platformFamily":"unix","platformOs":"macos"}}}
{"type":"expect_outbound","label":"initialized","frame":{"method":"initialized"}}
{"type":"expect_outbound","label":"thread/start","frame":{"id":2,"method":"thread/start","params":{}}}
{"type":"emit_inbound","label":"thread/start","frame":{"id":2,"result":{"thread":{"id":"native-v2-nested-root-thread","sessionId":"native-v2-nested-root-thread","forkedFromId":null,"preview":"","ephemeral":false,"modelProvider":"openai","createdAt":1782622500,"updatedAt":1782622500,"status":{"type":"idle"},"path":"/tmp/root-thread.jsonl","cwd":"/tmp","cliVersion":"0.141.0","source":"vscode","threadSource":null,"agentNickname":null,"agentRole":null,"gitInfo":null,"name":null,"turns":[]},"model":"gpt-5.4","modelProvider":"openai","serviceTier":null,"cwd":"/tmp","instructionSources":[],"approvalPolicy":"on-request","approvalsReviewer":"user","sandbox":{"type":"workspaceWrite","writableRoots":[],"networkAccess":false},"reasoningEffort":"medium"}}}
{"type":"expect_outbound","label":"turn/start","frame":{"id":3,"method":"turn/start","params":{"approvalPolicy":"never","cwd":"<workspace>","input":[{"text":"just say hello","type":"text"}],"model":"gpt-5.4","sandboxPolicy":{"type":"dangerFullAccess"},"threadId":"native-v2-nested-root-thread"}}}
{"type":"emit_inbound","label":"turn/start","frame":{"id":3,"result":{"turn":{"id":"native-v2-nested-root-turn","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":null,"completedAt":null,"durationMs":null}}}}
{"type":"emit_inbound","label":"thread/status/changed/root-active","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-root-thread","status":{"type":"active","activeFlags":[]}}}}
{"type":"emit_inbound","label":"turn/started","frame":{"method":"turn/started","params":{"threadId":"native-v2-nested-root-thread","turn":{"id":"native-v2-nested-root-turn","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":1782622500,"completedAt":null,"durationMs":null}}}}
{"type":"emit_inbound","label":"item/completed/root-user","frame":{"method":"item/completed","params":{"item":{"type":"userMessage","id":"nested-root-user","clientId":null,"content":[{"type":"text","text":"just say hello","text_elements":[]}]},"threadId":"native-v2-nested-root-thread","turnId":"native-v2-nested-root-turn","completedAtMs":1782622500261}}}
{"type":"emit_inbound","label":"item/completed/root-subagent","frame":{"method":"item/completed","params":{"item":{"type":"subAgentActivity","id":"call-nested-root-child","kind":"started","agentThreadId":"native-v2-nested-child-thread","agentPath":"/root/hello_agent"},"threadId":"native-v2-nested-root-thread","turnId":"native-v2-nested-root-turn","completedAtMs":1782622502529}}}
{"type":"emit_inbound","label":"thread/status/changed/child-active","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-child-thread","status":{"type":"active","activeFlags":[]}}}}
{"type":"emit_inbound","label":"turn/started/child","frame":{"method":"turn/started","params":{"threadId":"native-v2-nested-child-thread","turn":{"id":"native-v2-nested-child-turn","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":1782622502,"completedAt":null,"durationMs":null}}}}
{"type":"emit_inbound","label":"item/completed/child-subagent","frame":{"method":"item/completed","params":{"item":{"type":"subAgentActivity","id":"call-nested-child-grandchild","kind":"started","agentThreadId":"native-v2-nested-grandchild-thread","agentPath":"/root/hello_agent/hello_agent"},"threadId":"native-v2-nested-child-thread","turnId":"native-v2-nested-child-turn","completedAtMs":1782622507262}}}
{"type":"emit_inbound","label":"thread/status/changed/grandchild-active","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-grandchild-thread","status":{"type":"active","activeFlags":[]}}}}
{"type":"emit_inbound","label":"turn/started/grandchild","frame":{"method":"turn/started","params":{"threadId":"native-v2-nested-grandchild-thread","turn":{"id":"native-v2-nested-grandchild-turn","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":1782622507,"completedAt":null,"durationMs":null}}}}
{"type":"emit_inbound","label":"item/completed/grandchild-subagent","frame":{"method":"item/completed","params":{"item":{"type":"subAgentActivity","id":"call-nested-grandchild-leaf","kind":"started","agentThreadId":"native-v2-nested-leaf-thread","agentPath":"/root/hello_agent/hello_agent/hello_agent"},"threadId":"native-v2-nested-grandchild-thread","turnId":"native-v2-nested-grandchild-turn","completedAtMs":1782622511426}}}
{"type":"emit_inbound","label":"thread/status/changed/leaf-active","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-leaf-thread","status":{"type":"active","activeFlags":[]}}}}
{"type":"emit_inbound","label":"turn/started/leaf","frame":{"method":"turn/started","params":{"threadId":"native-v2-nested-leaf-thread","turn":{"id":"native-v2-nested-leaf-turn","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":1782622511,"completedAt":null,"durationMs":null}}}}
{"type":"emit_inbound","label":"item/completed/leaf-answer","frame":{"method":"item/completed","params":{"item":{"type":"agentMessage","id":"nested-leaf-answer","text":"Hello.","phase":"final_answer","memoryCitation":null},"threadId":"native-v2-nested-leaf-thread","turnId":"native-v2-nested-leaf-turn","completedAtMs":1782622513754}}}
{"type":"emit_inbound","label":"thread/status/changed/leaf-idle","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-leaf-thread","status":{"type":"idle"}}}}
{"type":"emit_inbound","label":"turn/completed","frame":{"method":"turn/completed","params":{"threadId":"native-v2-nested-leaf-thread","turn":{"id":"native-v2-nested-leaf-turn","items":[],"itemsView":"notLoaded","status":"completed","error":null,"startedAt":1782622511,"completedAt":1782622513,"durationMs":2754}}}}
{"type":"emit_inbound","label":"item/completed/grandchild-answer","frame":{"method":"item/completed","params":{"item":{"type":"agentMessage","id":"nested-grandchild-answer","text":"Subagent says: “Hello.”","phase":"final_answer","memoryCitation":null},"threadId":"native-v2-nested-grandchild-thread","turnId":"native-v2-nested-grandchild-turn","completedAtMs":1782622514500}}}
{"type":"emit_inbound","label":"thread/status/changed/grandchild-idle","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-grandchild-thread","status":{"type":"idle"}}}}
{"type":"emit_inbound","label":"turn/completed","frame":{"method":"turn/completed","params":{"threadId":"native-v2-nested-grandchild-thread","turn":{"id":"native-v2-nested-grandchild-turn","items":[],"itemsView":"notLoaded","status":"completed","error":null,"startedAt":1782622507,"completedAt":1782622514,"durationMs":7500}}}}
{"type":"emit_inbound","label":"item/completed/child-answer","frame":{"method":"item/completed","params":{"item":{"type":"agentMessage","id":"nested-child-answer","text":"Subagent says: “Hello.”","phase":"final_answer","memoryCitation":null},"threadId":"native-v2-nested-child-thread","turnId":"native-v2-nested-child-turn","completedAtMs":1782622515200}}}
{"type":"emit_inbound","label":"thread/status/changed/child-idle","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-child-thread","status":{"type":"idle"}}}}
{"type":"emit_inbound","label":"turn/completed","frame":{"method":"turn/completed","params":{"threadId":"native-v2-nested-child-thread","turn":{"id":"native-v2-nested-child-turn","items":[],"itemsView":"notLoaded","status":"completed","error":null,"startedAt":1782622502,"completedAt":1782622515,"durationMs":13200}}}}
{"type":"emit_inbound","label":"item/completed/root-answer","frame":{"method":"item/completed","params":{"item":{"type":"agentMessage","id":"nested-root-answer","text":"Subagent says: “Hello.”","phase":"final_answer","memoryCitation":null},"threadId":"native-v2-nested-root-thread","turnId":"native-v2-nested-root-turn","completedAtMs":1782622515859}}}
{"type":"emit_inbound","label":"thread/status/changed/root-idle","frame":{"method":"thread/status/changed","params":{"threadId":"native-v2-nested-root-thread","status":{"type":"idle"}}}}
{"type":"emit_inbound","label":"turn/completed","frame":{"method":"turn/completed","params":{"threadId":"native-v2-nested-root-thread","turn":{"id":"native-v2-nested-root-turn","items":[],"itemsView":"notLoaded","status":"completed","error":null,"startedAt":1782622500,"completedAt":1782622515,"durationMs":15598}}}}
{"type":"runtime_exit","status":"success"}