Skip to content

Commit 880157c

Browse files
fix(server): MCP app cards load their Codex thread after a restart
After a T3 restart, or an idle unload, the shared Codex app-server no longer has a thread loaded. MCP app card calls went straight to the runtime and Codex answered "thread not found", so the card showed "Not saved". The session manager now decorates the runtime's mcpApps like resumeThread: each call attaches the thread (so its T3 MCP session exists), holds it loaded, resumes it unless this session already has that provider thread loaded under any model or policy, runs the call, and then reschedules the idle unload. A card-made load records no model or policy, so the next turn's resume still runs. resumeThread and the MCP path share one loader. Fixes #29433 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6374320 commit 880157c

2 files changed

Lines changed: 167 additions & 57 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,6 +3783,80 @@ it.effect(
37833783
}),
37843784
);
37853785

3786+
it.effect(
3787+
"ProviderSessionManagerV2 MCP app calls reuse any load of the thread without hiding a turn's resume",
3788+
() =>
3789+
Effect.gen(function* () {
3790+
const state = yield* Ref.make(emptyState);
3791+
const runtimeCalls = yield* Ref.make<ReadonlyArray<string>>([]);
3792+
const effect = Effect.gen(function* () {
3793+
const eventSink = yield* EventSink.EventSinkV2;
3794+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
3795+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3796+
const now = yield* DateTime.now;
3797+
const turnThreadId = ThreadId.make("mcp-app-turn-thread");
3798+
const cardThreadId = ThreadId.make("mcp-app-card-thread");
3799+
const providerSessionId = idAllocator.derive.providerSession({
3800+
providerInstanceId: modelSelection.instanceId,
3801+
});
3802+
const turnProviderThread = makeProviderThread({
3803+
idAllocator,
3804+
threadId: turnThreadId,
3805+
providerSessionId,
3806+
now,
3807+
nativeThreadId: "native-turn",
3808+
});
3809+
const cardProviderThread = makeProviderThread({
3810+
idAllocator,
3811+
threadId: cardThreadId,
3812+
providerSessionId,
3813+
now,
3814+
nativeThreadId: "native-card",
3815+
});
3816+
yield* eventSink.write({
3817+
events: [
3818+
yield* makeThreadCreatedEvent({ idAllocator, threadId: turnThreadId, now }),
3819+
yield* makeThreadCreatedEvent({ idAllocator, threadId: cardThreadId, now }),
3820+
],
3821+
});
3822+
const runtime = yield* manager.open({
3823+
threadId: turnThreadId,
3824+
providerSessionId,
3825+
modelSelection,
3826+
runtimePolicy,
3827+
});
3828+
const resumeForTurn = (providerThread: typeof turnProviderThread, threadId: ThreadId) =>
3829+
runtime.resumeThread({ providerThread, threadId, modelSelection, runtimePolicy });
3830+
const mcpApps = runtime.mcpApps;
3831+
assert.isDefined(mcpApps);
3832+
3833+
yield* resumeForTurn(turnProviderThread, turnThreadId);
3834+
yield* mcpApps!.listTools({ providerThread: turnProviderThread, server: "rating" });
3835+
yield* mcpApps!.listTools({ providerThread: cardProviderThread, server: "rating" });
3836+
yield* resumeForTurn(cardProviderThread, cardThreadId);
3837+
3838+
assert.deepEqual(yield* Ref.get(runtimeCalls), [
3839+
"resume native-turn mcp=true",
3840+
"listTools native-turn",
3841+
"resume native-card mcp=true",
3842+
"listTools native-card",
3843+
"resume native-card mcp=true",
3844+
]);
3845+
});
3846+
3847+
yield* effect.pipe(
3848+
Effect.provide(
3849+
layerTest({
3850+
state,
3851+
idleTimeoutMs: 1000,
3852+
runtimeCalls,
3853+
hasPendingBackgroundWork: Effect.succeed(true),
3854+
}),
3855+
),
3856+
);
3857+
}),
3858+
);
3859+
37863860
it.effect(
37873861
"ProviderSessionManagerV2 rejects a second thread when the provider runtime is exclusive",
37883862
() =>

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 93 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class ProviderSessionManagerV2 extends Context.Service<
217217

218218
interface LiveSessionEntry {
219219
readonly attachedThreadIds: ReadonlySet<ThreadId>;
220-
readonly loadedProviderThreadKeyByThread: ReadonlyMap<ThreadId, string>;
220+
readonly loadedProviderThreadKeyByThread: ReadonlyMap<ThreadId, ProviderThreadLoadKey>;
221221
/** Scope revocation to recorded credential ids so superseded sessions cannot revoke replacements opened before detach. */
222222
readonly mcpCredentialIdByThread: ReadonlyMap<ThreadId, string>;
223223
readonly supportsMultipleProviderThreads: boolean;
@@ -301,18 +301,25 @@ function providerThreadRuntimeKey(
301301
: `${nativeThreadRef.driver}:${nativeThreadRef.nativeId}`;
302302
}
303303

304+
interface ProviderThreadLoadKey {
305+
readonly providerThread: string;
306+
readonly config: string;
307+
}
308+
304309
function providerThreadLoadKey(input: {
305310
readonly providerThread: Parameters<
306311
ProviderAdapter.ProviderAdapterV2SessionRuntime["resumeThread"]
307312
>[0]["providerThread"];
308313
readonly modelSelection?: ModelSelection;
309314
readonly runtimePolicy?: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
310-
}): string {
311-
return JSON.stringify({
315+
}): ProviderThreadLoadKey {
316+
return {
312317
providerThread: providerThreadRuntimeKey(input.providerThread),
313-
modelSelection: input.modelSelection ?? null,
314-
runtimePolicy: input.runtimePolicy ?? null,
315-
});
318+
config: JSON.stringify({
319+
modelSelection: input.modelSelection ?? null,
320+
runtimePolicy: input.runtimePolicy ?? null,
321+
}),
322+
};
316323
}
317324

318325
export const layerWithOptions = (
@@ -1297,24 +1304,22 @@ export const layerWithOptions = (
12971304
return updated;
12981305
});
12991306

1300-
const isProviderThreadLoaded = (input: {
1307+
const loadedProviderThreadKey = (input: {
13011308
readonly providerSessionId: ProviderSessionId;
13021309
readonly threadId: ThreadId;
1303-
readonly providerThreadKey: string;
13041310
}) =>
13051311
Ref.get(sessions).pipe(
1306-
Effect.map(
1307-
(current) =>
1308-
current
1309-
.get(sessionKey(input.providerSessionId))
1310-
?.loadedProviderThreadKeyByThread.get(input.threadId) === input.providerThreadKey,
1312+
Effect.map((current) =>
1313+
current
1314+
.get(sessionKey(input.providerSessionId))
1315+
?.loadedProviderThreadKeyByThread.get(input.threadId),
13111316
),
13121317
);
13131318

13141319
const markProviderThreadLoaded = (input: {
13151320
readonly providerSessionId: ProviderSessionId;
13161321
readonly threadId: ThreadId;
1317-
readonly providerThreadKey: string;
1322+
readonly providerThreadKey: ProviderThreadLoadKey;
13181323
}) =>
13191324
Ref.update(sessions, (current) => {
13201325
const key = sessionKey(input.providerSessionId);
@@ -1708,6 +1713,62 @@ export const layerWithOptions = (
17081713
modelFamily: normalizeModelMetricLabel(model),
17091714
},
17101715
});
1716+
const loadThread = (
1717+
input: Parameters<ProviderAdapter.ProviderAdapterV2SessionRuntime["resumeThread"]>[0] & {
1718+
readonly threadId: ThreadId;
1719+
},
1720+
isLoaded: (loaded: ProviderThreadLoadKey | undefined) => boolean,
1721+
) =>
1722+
observeActivity(
1723+
providerSessionId,
1724+
ensureThreadAttached({
1725+
providerSessionId,
1726+
threadId: input.threadId,
1727+
providerInstanceId: runtime.instanceId,
1728+
}),
1729+
).pipe(
1730+
Effect.andThen(
1731+
holdThreadLoaded({
1732+
providerSessionId,
1733+
threadId: input.threadId,
1734+
providerThread: input.providerThread,
1735+
}),
1736+
),
1737+
Effect.andThen(
1738+
loadedProviderThreadKey({ providerSessionId, threadId: input.threadId }),
1739+
),
1740+
Effect.flatMap((loaded) =>
1741+
isLoaded(loaded)
1742+
? Effect.succeed(input.providerThread)
1743+
: runtime.resumeThread(input).pipe(
1744+
Effect.tap((providerThread) =>
1745+
markProviderThreadLoaded({
1746+
providerSessionId,
1747+
threadId: input.threadId,
1748+
providerThreadKey: providerThreadLoadKey({ ...input, providerThread }),
1749+
}),
1750+
),
1751+
),
1752+
),
1753+
);
1754+
const mcpApps = runtime.mcpApps;
1755+
// Any load of this provider thread serves MCP calls; a load made here records no model or
1756+
// policy, so the next turn still resumes with its own.
1757+
const withThreadLoaded = <A>(
1758+
providerThread: OrchestrationV2ProviderThread,
1759+
call: Effect.Effect<A, ProviderAdapter.ProviderAdapterV2Error>,
1760+
) => {
1761+
const threadId = providerThread.appThreadId;
1762+
if (threadId === null) return call;
1763+
const runtimeKey = providerThreadRuntimeKey(providerThread);
1764+
return loadThread(
1765+
{ providerThread, threadId },
1766+
(loaded) => loaded?.providerThread === runtimeKey,
1767+
).pipe(
1768+
Effect.andThen(call),
1769+
Effect.ensuring(scheduleThreadUnload(providerSessionId, providerThread.id)),
1770+
);
1771+
};
17111772
return {
17121773
...runtime,
17131774
subscribeEvents,
@@ -1741,51 +1802,26 @@ export const layerWithOptions = (
17411802
if (threadId === null || threadId === undefined) {
17421803
return runtime.resumeThread(input);
17431804
}
1744-
const providerThreadKey = providerThreadLoadKey({
1745-
providerThread: input.providerThread,
1746-
...(input.modelSelection === undefined
1747-
? {}
1748-
: { modelSelection: input.modelSelection }),
1749-
...(input.runtimePolicy === undefined ? {} : { runtimePolicy: input.runtimePolicy }),
1750-
});
1751-
return observeActivity(
1752-
providerSessionId,
1753-
ensureThreadAttached({
1754-
providerSessionId,
1755-
threadId,
1756-
providerInstanceId: runtime.instanceId,
1757-
}),
1758-
).pipe(
1759-
Effect.andThen(
1760-
holdThreadLoaded({
1761-
providerSessionId,
1762-
threadId,
1763-
providerThread: input.providerThread,
1764-
}),
1765-
),
1766-
Effect.andThen(
1767-
isProviderThreadLoaded({ providerSessionId, threadId, providerThreadKey }),
1768-
),
1769-
Effect.flatMap((loaded) =>
1770-
loaded ? Effect.succeed(input.providerThread) : runtime.resumeThread(input),
1771-
),
1772-
Effect.tap((providerThread) =>
1773-
markProviderThreadLoaded({
1774-
providerSessionId,
1775-
threadId,
1776-
providerThreadKey: providerThreadLoadKey({
1777-
providerThread,
1778-
...(input.modelSelection === undefined
1779-
? {}
1780-
: { modelSelection: input.modelSelection }),
1781-
...(input.runtimePolicy === undefined
1782-
? {}
1783-
: { runtimePolicy: input.runtimePolicy }),
1784-
}),
1785-
}),
1786-
),
1805+
const providerThreadKey = providerThreadLoadKey(input);
1806+
return loadThread(
1807+
{ ...input, threadId },
1808+
(loaded) =>
1809+
loaded?.providerThread === providerThreadKey.providerThread &&
1810+
loaded.config === providerThreadKey.config,
17871811
);
17881812
},
1813+
...(mcpApps === undefined
1814+
? {}
1815+
: {
1816+
mcpApps: {
1817+
listTools: (input) =>
1818+
withThreadLoaded(input.providerThread, mcpApps.listTools(input)),
1819+
callTool: (input) =>
1820+
withThreadLoaded(input.providerThread, mcpApps.callTool(input)),
1821+
readResource: (input) =>
1822+
withThreadLoaded(input.providerThread, mcpApps.readResource(input)),
1823+
},
1824+
}),
17891825
forkThread: (input) =>
17901826
observeActivity(
17911827
providerSessionId,

0 commit comments

Comments
 (0)