Skip to content

Commit 99071c4

Browse files
fix(codex): delegate_task children start as Codex subagents (#36525)
1 parent 83d2382 commit 99071c4

4 files changed

Lines changed: 76 additions & 6 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,68 @@ describe("CodexAdapterV2 post-settle continuation", () => {
18721872
),
18731873
);
18741874

1875+
// regression: #36520 MCP servers can tell a delegate_task child from a user thread
1876+
it.effect("starts delegate_task children with the subagent thread source", () =>
1877+
Effect.gen(function* () {
1878+
const scenario = "delegated-thread-source";
1879+
const preamble = codexReplayPreamble({
1880+
nativeThreadId: "delegated-parent-thread",
1881+
nativeTurnId: "unused",
1882+
prompt: "unused",
1883+
}).slice(0, 5);
1884+
const childStart = codexReplayPreamble({
1885+
nativeThreadId: "delegated-child-thread",
1886+
nativeTurnId: "unused",
1887+
prompt: "unused",
1888+
})
1889+
.slice(3, 5)
1890+
.map((entry) => withReplayRequestId(entry, 3))
1891+
.map((entry) =>
1892+
entry.type === "expect_outbound" &&
1893+
Predicate.isObject(entry.frame) &&
1894+
Predicate.isObject(entry.frame["params"])
1895+
? {
1896+
...entry,
1897+
frame: {
1898+
...entry.frame,
1899+
params: { ...entry.frame["params"], threadSource: "subagent" },
1900+
},
1901+
}
1902+
: entry,
1903+
);
1904+
const threadSources: Array<unknown> = [];
1905+
const harness = yield* makeCodexReplayHarness(
1906+
makeCodexReplayTranscript({ scenario, entries: [...preamble, ...childStart] }),
1907+
() => Effect.void,
1908+
(method, params) =>
1909+
Effect.sync(() => {
1910+
if (method === "thread/start" && Predicate.isObject(params)) {
1911+
threadSources.push(params["threadSource"]);
1912+
}
1913+
}),
1914+
);
1915+
const childThreadId = (yield* IdAllocator.IdAllocatorV2).derive.delegatedTaskThread({
1916+
commandId: CommandId.make(`mcp:${scenario}:delegate-task:child`),
1917+
});
1918+
yield* harness.runtime.ensureThread({
1919+
threadId: childThreadId,
1920+
modelSelection: CODEX_TEST_MODEL_SELECTION,
1921+
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
1922+
});
1923+
assert.deepEqual(threadSources, [undefined, "subagent"]);
1924+
}).pipe(
1925+
Effect.scoped,
1926+
Effect.provide(
1927+
Layer.mergeAll(
1928+
IdAllocator.layer,
1929+
McpProviderSessions.layer,
1930+
NodeServices.layer,
1931+
NodeCrypto.layer,
1932+
),
1933+
),
1934+
),
1935+
);
1936+
18751937
// regression: #15839 launch-arg MCP servers survive the thread's t3-code server
18761938
it.effect("replays historical and dotted MCP config while enforcing other thread config", () =>
18771939
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7701,15 +7701,18 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
77017701
ensureInitialized.pipe(
77027702
Effect.andThen(mcpSessions.read(threadInput.threadId)),
77037703
Effect.flatMap((mcpSession) =>
7704-
client.request(
7705-
"thread/start",
7706-
codexThreadRuntimeParams({
7704+
client.request("thread/start", {
7705+
...codexThreadRuntimeParams({
77077706
threadId: threadInput.threadId,
77087707
mcpSession,
77097708
modelSelection: threadInput.modelSelection,
77107709
runtimePolicy: threadInput.runtimePolicy,
77117710
}),
7712-
),
7711+
// Codex tags the child's MCP calls with thread_source "subagent".
7712+
...(IdAllocator.isDelegatedTaskThread(threadInput.threadId)
7713+
? { threadSource: "subagent" }
7714+
: {}),
7715+
}),
77137716
),
77147717
Effect.map((response): OrchestrationV2ProviderThread =>
77157718
providerThreadFromCodexThread({

‎apps/server/src/orchestration-v2/testkit/fixtures/delegated_task_status/codex_transcript.ndjson‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{"type":"emit_inbound","label":"parent.turn/start","frame":{"id":4,"result":{"turn":{"id":"019f1000-0000-7000-8000-000000000002","items":[],"status":"inProgress","error":null,"startedAt":null,"completedAt":null,"durationMs":null}}}}
1212
{"type":"emit_inbound","label":"parent.thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"019f1000-0000-7000-8000-000000000001","status":{"type":"active","activeFlags":[]}}}}
1313
{"type":"emit_inbound","label":"parent.turn/started","frame":{"method":"turn/started","params":{"threadId":"019f1000-0000-7000-8000-000000000001","turn":{"id":"019f1000-0000-7000-8000-000000000002","items":[],"status":"inProgress","error":null,"startedAt":1776739467,"completedAt":null,"durationMs":null}}}}
14-
{"type":"expect_outbound","label":"child.thread/start","frame":{"id":5,"method":"thread/start","params":{"config":{"tools.update_plan.enabled":true,"mcp_optional_startup_grace_ms":10000,"features.concurrent_reasoning_summaries":true}}}}
14+
{"type":"expect_outbound","label":"child.thread/start","frame":{"id":5,"method":"thread/start","params":{"config":{"tools.update_plan.enabled":true,"mcp_optional_startup_grace_ms":10000,"features.concurrent_reasoning_summaries":true},"threadSource":"subagent"}}}
1515
{"type":"emit_inbound","label":"child.thread/start","frame":{"id":5,"result":{"thread":{"id":"019f1000-0000-7000-8000-000000000003","forkedFromId":null,"preview":"","ephemeral":false,"modelProvider":"openai","createdAt":1776739468,"updatedAt":1776739468,"status":{"type":"idle"},"path":"/home/replay-user/.codex/sessions/delegated-task-status-child.jsonl","cwd":"/home/replay-user/t3code","cliVersion":"0.120.0","source":"vscode","agentNickname":null,"agentRole":null,"gitInfo":null,"name":null,"turns":[]},"model":"gpt-5.4","modelProvider":"openai","serviceTier":"fast","cwd":"/home/replay-user/t3code","approvalPolicy":"on-request","approvalsReviewer":"user","sandbox":{"type":"workspaceWrite","writableRoots":[],"readOnlyAccess":{"type":"fullAccess"},"networkAccess":false,"excludeTmpdirEnvVar":false,"excludeSlashTmp":false},"reasoningEffort":"xhigh"}}}
1616
{"type":"expect_outbound","label":"config/read","frame":{"id":6,"method":"config/read","params":{"cwd":"<workspace>","includeLayers":false}}}
1717
{"type":"emit_inbound","label":"config/read","frame":{"id":6,"result":{"config":{},"origins":{}}}}

‎packages/provider-core/src/server/IdAllocator.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,19 @@ const randomId =
271271
),
272272
);
273273

274+
const DELEGATED_TASK_THREAD_PREFIX = joinId("thread", "delegated-task", "");
275+
276+
export const isDelegatedTaskThread = (threadId: ThreadId): boolean =>
277+
threadId.startsWith(DELEGATED_TASK_THREAD_PREFIX);
278+
274279
export const derive: IdAllocatorV2Derive = {
275280
providerSession: (input) =>
276281
ProviderSessionId.make(
277282
joinId("provider-session", "provider-instance", input.providerInstanceId, "shared"),
278283
),
279284
delegatedTaskNode: (input) => NodeId.make(joinId("node", "delegated-task", input.commandId)),
280285
delegatedTaskThread: (input) =>
281-
ThreadId.make(joinId("thread", "delegated-task", input.commandId)),
286+
ThreadId.make(`${DELEGATED_TASK_THREAD_PREFIX}${encodePart(input.commandId)}`),
282287
delegatedTaskMessage: (input) =>
283288
MessageId.make(joinId("message", "delegated-task", input.commandId)),
284289
delegatedTaskTurnItem: (input) =>

0 commit comments

Comments
 (0)