Skip to content

Commit b539134

Browse files
author
PR Batch Tester
committed
fix(server): retain native thread callers for the provider session
1 parent f468370 commit b539134

2 files changed

Lines changed: 110 additions & 33 deletions

File tree

‎packages/provider-core/src/server/McpProviderSessions.test.ts‎

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,101 @@ const session = (providerSessionId: string) => ({
1717
browserToolsAvailable: false,
1818
});
1919

20-
it.effect("a call waiting on its caller never resolves through the next credential", () =>
21-
Effect.gen(function* () {
22-
const sessions = yield* McpProviderSessions.McpProviderSessions;
23-
yield* sessions.set(session("credential:old"));
24-
const pending = yield* Effect.forkChild(
25-
sessions.resolveNativeCaller({
20+
it.effect.each(["codex-thread:native", "claude-tool:native"])(
21+
"a call waiting on its caller never resolves through the next credential: %s",
22+
(callId) =>
23+
Effect.gen(function* () {
24+
const sessions = yield* McpProviderSessions.McpProviderSessions;
25+
yield* sessions.set(session("credential:old"));
26+
const pending = yield* Effect.forkChild(
27+
sessions.resolveNativeCaller({
28+
threadId,
29+
providerSessionId: "credential:old",
30+
callId,
31+
}),
32+
{ startImmediately: true },
33+
);
34+
yield* sessions.set(session("credential:new"));
35+
yield* sessions.bindNativeCaller({
2636
threadId,
27-
providerSessionId: "credential:old",
28-
callId: "codex-thread:native",
29-
}),
30-
{ startImmediately: true },
31-
);
32-
// A resumed Codex session binds the same native thread id under its new credential.
33-
yield* sessions.set(session("credential:new"));
34-
yield* sessions.bindNativeCaller({
35-
threadId,
36-
callId: "codex-thread:native",
37-
callerThreadId: ThreadId.make("thread:native-child"),
38-
});
39-
assert.isUndefined(yield* Fiber.join(pending));
40-
assert.equal(
41-
yield* sessions.resolveNativeCaller({
37+
callId,
38+
callerThreadId: ThreadId.make("thread:native-child"),
39+
});
40+
assert.isUndefined(yield* Fiber.join(pending));
41+
assert.equal(
42+
yield* sessions.resolveNativeCaller({
43+
threadId,
44+
providerSessionId: "credential:new",
45+
callId,
46+
}),
47+
ThreadId.make("thread:native-child"),
48+
);
49+
for (const reset of ["replace", "clear"] as const) {
50+
if (reset === "clear") yield* sessions.clear(threadId);
51+
yield* sessions.set(session(`credential:${reset}`));
52+
const missing = yield* Effect.forkChild(
53+
sessions.resolveNativeCaller({
54+
threadId,
55+
providerSessionId: `credential:${reset}`,
56+
callId,
57+
}),
58+
{ startImmediately: true },
59+
);
60+
yield* TestClock.adjust("5 seconds");
61+
assert.isUndefined(yield* Fiber.join(missing));
62+
yield* sessions.bindNativeCaller({
63+
threadId,
64+
callId,
65+
callerThreadId: ThreadId.make("thread:native-child"),
66+
});
67+
}
68+
}).pipe(Effect.provide(McpProviderSessions.layer)),
69+
);
70+
71+
it.effect.each(["codex-thread:", "claude-tool:"])(
72+
"keeps active Codex callers after many %s bindings",
73+
(prefix) =>
74+
Effect.gen(function* () {
75+
const sessions = yield* McpProviderSessions.McpProviderSessions;
76+
yield* sessions.set(session("credential:current"));
77+
const callerThreadId = ThreadId.make("thread:native-child");
78+
yield* sessions.bindNativeCaller({
4279
threadId,
43-
providerSessionId: "credential:new",
44-
callId: "codex-thread:native",
45-
}),
46-
ThreadId.make("thread:native-child"),
47-
);
48-
}).pipe(Effect.provide(McpProviderSessions.layer)),
80+
callId: "codex-thread:root",
81+
callerThreadId: threadId,
82+
});
83+
yield* sessions.bindNativeCaller({ threadId, callId: "codex-thread:active", callerThreadId });
84+
for (let index = 0; index < 1_025; index += 1) {
85+
yield* sessions.bindNativeCaller({ threadId, callId: `${prefix}${index}`, callerThreadId });
86+
}
87+
for (const [callId, expected] of [
88+
["codex-thread:root", threadId],
89+
["codex-thread:active", callerThreadId],
90+
] as const) {
91+
const resolved = yield* Effect.forkChild(
92+
sessions.resolveNativeCaller({
93+
threadId,
94+
providerSessionId: "credential:current",
95+
callId,
96+
}),
97+
{ startImmediately: true },
98+
);
99+
yield* TestClock.adjust("5 seconds");
100+
assert.equal(yield* Fiber.join(resolved), expected);
101+
}
102+
if (prefix === "claude-tool:") {
103+
const expired = yield* Effect.forkChild(
104+
sessions.resolveNativeCaller({
105+
threadId,
106+
providerSessionId: "credential:current",
107+
callId: "claude-tool:0",
108+
}),
109+
{ startImmediately: true },
110+
);
111+
yield* TestClock.adjust("5 seconds");
112+
assert.isUndefined(yield* Fiber.join(expired));
113+
}
114+
}).pipe(Effect.provide(McpProviderSessions.layer)),
49115
);
50116

51117
it.effect.each(["interrupted", "timed out"] as const)(

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ const make = Effect.gen(function* () {
104104
const htmlRenders = yield* Ref.make(new Map<ThreadId, ReadonlyArray<KeptHtmlRender>>());
105105
// A replaced or cleared credential drops its thread's bindings, so none answer for the next.
106106
const nativeCallers = new Map<ThreadId, Map<string, ThreadId>>();
107+
// Codex thread identities stay valid across calls, unlike Claude's per-call tool-use ids.
108+
const nativeThreadCallers = new Map<ThreadId, Map<string, ThreadId>>();
107109
const nativeCallerWaiters = new Map<
108110
string,
109111
{ readonly deferred: Deferred.Deferred<ThreadId>; pending: number }
@@ -115,6 +117,7 @@ const make = Effect.gen(function* () {
115117
const replaced = current.get(config.threadId)?.providerSessionId;
116118
if (replaced !== undefined && replaced !== config.providerSessionId) {
117119
nativeCallers.delete(config.threadId);
120+
nativeThreadCallers.delete(config.threadId);
118121
}
119122
if (replaced !== config.providerSessionId) {
120123
yield* Ref.update(htmlRenders, (kept) => {
@@ -131,6 +134,7 @@ const make = Effect.gen(function* () {
131134
clear: Effect.fn("McpProviderSessions.clear")(function (threadId) {
132135
return Ref.update(sessions, (current) => {
133136
nativeCallers.delete(threadId);
137+
nativeThreadCallers.delete(threadId);
134138
const next = new Map(current);
135139
next.delete(threadId);
136140
return next;
@@ -145,14 +149,19 @@ const make = Effect.gen(function* () {
145149
);
146150
}),
147151
bindNativeCaller: Effect.fn("McpProviderSessions.bindNativeCaller")(function* (input) {
148-
const bound = nativeCallers.get(input.threadId) ?? new Map<string, ThreadId>();
152+
const callers = input.callId.startsWith("codex-thread:")
153+
? nativeThreadCallers
154+
: nativeCallers;
155+
const bound = callers.get(input.threadId) ?? new Map<string, ThreadId>();
149156
bound.delete(input.callId);
150157
bound.set(input.callId, input.callerThreadId);
151-
for (const oldest of bound.keys()) {
152-
if (bound.size <= MAX_KEPT_NATIVE_CALLERS) break;
153-
bound.delete(oldest);
158+
if (callers === nativeCallers) {
159+
for (const oldest of bound.keys()) {
160+
if (bound.size <= MAX_KEPT_NATIVE_CALLERS) break;
161+
bound.delete(oldest);
162+
}
154163
}
155-
nativeCallers.set(input.threadId, bound);
164+
callers.set(input.threadId, bound);
156165
const waiter = nativeCallerWaiters.get(waiterKey(input.threadId, input.callId));
157166
if (waiter !== undefined) yield* Deferred.succeed(waiter.deferred, input.callerThreadId);
158167
}),
@@ -162,7 +171,9 @@ const make = Effect.gen(function* () {
162171
Effect.map((all) => all.get(input.threadId)?.providerSessionId === input.providerSessionId),
163172
);
164173
if (!(yield* credentialCurrent)) return undefined;
165-
const bound = nativeCallers.get(input.threadId)?.get(input.callId);
174+
const bound =
175+
nativeThreadCallers.get(input.threadId)?.get(input.callId) ??
176+
nativeCallers.get(input.threadId)?.get(input.callId);
166177
if (bound !== undefined) return bound;
167178
const key = waiterKey(input.threadId, input.callId);
168179
const waiter = nativeCallerWaiters.get(key) ?? {

0 commit comments

Comments
 (0)