Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
62 changes: 62 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,68 @@ describe("CodexAdapterV2 post-settle continuation", () => {
),
);

// regression: #36520 MCP servers can tell a delegate_task child from a user thread
it.effect("starts delegate_task children with the subagent thread source", () =>
Effect.gen(function* () {
const scenario = "delegated-thread-source";
const preamble = codexReplayPreamble({
nativeThreadId: "delegated-parent-thread",
nativeTurnId: "unused",
prompt: "unused",
}).slice(0, 5);
const childStart = codexReplayPreamble({
nativeThreadId: "delegated-child-thread",
nativeTurnId: "unused",
prompt: "unused",
})
.slice(3, 5)
.map((entry) => withReplayRequestId(entry, 3))
.map((entry) =>
entry.type === "expect_outbound" &&
Predicate.isObject(entry.frame) &&
Predicate.isObject(entry.frame["params"])
? {
...entry,
frame: {
...entry.frame,
params: { ...entry.frame["params"], threadSource: "subagent" },
},
}
: entry,
);
const threadSources: Array<unknown> = [];
const harness = yield* makeCodexReplayHarness(
makeCodexReplayTranscript({ scenario, entries: [...preamble, ...childStart] }),
() => Effect.void,
(method, params) =>
Effect.sync(() => {
if (method === "thread/start" && Predicate.isObject(params)) {
threadSources.push(params["threadSource"]);
}
}),
);
const childThreadId = (yield* IdAllocator.IdAllocatorV2).derive.delegatedTaskThread({
commandId: CommandId.make(`mcp:${scenario}:delegate-task:child`),
});
yield* harness.runtime.ensureThread({
threadId: childThreadId,
modelSelection: CODEX_TEST_MODEL_SELECTION,
runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
});
assert.deepEqual(threadSources, [undefined, "subagent"]);
}).pipe(
Effect.scoped,
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
NodeCrypto.layer,
),
),
),
);

// regression: #15839 launch-arg MCP servers survive the thread's t3-code server
it.effect("replays historical and dotted MCP config while enforcing other thread config", () =>
Effect.gen(function* () {
Expand Down
11 changes: 7 additions & 4 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7701,15 +7701,18 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
ensureInitialized.pipe(
Effect.andThen(mcpSessions.read(threadInput.threadId)),
Effect.flatMap((mcpSession) =>
client.request(
"thread/start",
codexThreadRuntimeParams({
client.request("thread/start", {
...codexThreadRuntimeParams({
threadId: threadInput.threadId,
mcpSession,
modelSelection: threadInput.modelSelection,
runtimePolicy: threadInput.runtimePolicy,
}),
),
// Codex tags the child's MCP calls with thread_source "subagent".
...(IdAllocator.isDelegatedTaskThread(threadInput.threadId)
? { threadSource: "subagent" }
: {}),
}),
),
Effect.map((response): OrchestrationV2ProviderThread =>
providerThreadFromCodexThread({
Expand Down
7 changes: 6 additions & 1 deletion packages/provider-core/src/server/IdAllocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,19 @@ const randomId =
),
);

const DELEGATED_TASK_THREAD_PREFIX = joinId("thread", "delegated-task", "");

export const isDelegatedTaskThread = (threadId: ThreadId): boolean =>
threadId.startsWith(DELEGATED_TASK_THREAD_PREFIX);

export const derive: IdAllocatorV2Derive = {
providerSession: (input) =>
ProviderSessionId.make(
joinId("provider-session", "provider-instance", input.providerInstanceId, "shared"),
),
delegatedTaskNode: (input) => NodeId.make(joinId("node", "delegated-task", input.commandId)),
delegatedTaskThread: (input) =>
ThreadId.make(joinId("thread", "delegated-task", input.commandId)),
ThreadId.make(`${DELEGATED_TASK_THREAD_PREFIX}${encodePart(input.commandId)}`),
delegatedTaskMessage: (input) =>
MessageId.make(joinId("message", "delegated-task", input.commandId)),
delegatedTaskTurnItem: (input) =>
Expand Down
Loading