Skip to content

Commit 3ea562f

Browse files
jieyuexingclaude
authored andcommitted
fix(server): T3 tools work again after a thread switches back to a reused provider session
A thread keeps its old provider session attached after switching providers, and the new provider's attach rotates the thread's single MCP credential slot to its own scope. When the thread switched back, the reused session skipped credential preparation, so the returning provider ran with a credential scoped to the other provider and every live-caller check failed with parent_not_active (#16565). ensureThreadAttached now also prepares the credential when the thread is already attached but the slot belongs to another provider instance, and records the new credential on the session so release revokes the right one. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Rebased onto current main (history rewritten since the PR opened): the slot read now goes through the McpProviderSessions service, which replaced the readMcpProviderSession/setMcpProviderSession helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 10629af commit 3ea562f

2 files changed

Lines changed: 83 additions & 15 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,62 @@ it.effect(
21072107
}),
21082108
);
21092109

2110+
it.effect(
2111+
"ProviderSessionManagerV2 takes the thread's MCP credential back when a run returns to a still-attached session",
2112+
() =>
2113+
Effect.gen(function* () {
2114+
const state = yield* Ref.make(emptyState);
2115+
const effect = Effect.gen(function* () {
2116+
const eventSink = yield* EventSink.EventSinkV2;
2117+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2118+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
2119+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
2120+
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
2121+
const now = yield* DateTime.now;
2122+
const threadId = ThreadId.make("thread-provider-session-manager-returning-provider");
2123+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2124+
providerInstanceId: modelSelection.instanceId,
2125+
threadId,
2126+
});
2127+
const tokenOf = (config: McpProviderSession.McpProviderSessionConfig | undefined) =>
2128+
config?.authorizationHeader.replace(/^Bearer\s+/, "") ?? "";
2129+
2130+
yield* eventSink.write({
2131+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
2132+
});
2133+
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
2134+
2135+
// The thread switches to another provider, whose attach replaces the
2136+
// thread's credential while this session stays attached.
2137+
yield* registry.revokeThread(threadId);
2138+
const otherProvider = yield* registry.issue({
2139+
threadId,
2140+
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
2141+
});
2142+
yield* mcpSessions.set(otherProvider.config);
2143+
2144+
// The thread switches back and its next turn reuses this session.
2145+
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
2146+
assert.equal((yield* Ref.get(state)).openCount, 1);
2147+
2148+
const reclaimed = yield* mcpSessions.read(threadId);
2149+
assert.equal(reclaimed?.providerInstanceId, modelSelection.instanceId);
2150+
assert.equal(
2151+
(yield* registry.resolve(tokenOf(reclaimed)))?.thread.providerInstanceId,
2152+
modelSelection.instanceId,
2153+
);
2154+
assert.isUndefined(yield* registry.resolve(tokenOf(otherProvider.config)));
2155+
2156+
// The session's record follows the new credential, so release revokes it.
2157+
yield* manager.close(providerSessionId);
2158+
assert.isUndefined(yield* registry.resolve(tokenOf(reclaimed)));
2159+
assert.isUndefined(yield* mcpSessions.read(threadId));
2160+
});
2161+
2162+
yield* effect.pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1_000 })));
2163+
}),
2164+
);
2165+
21102166
it.effect(
21112167
"ProviderSessionManagerV2 protects a reused credential from a predecessor release during open",
21122168
() =>

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,22 +1328,34 @@ export const layerWithOptions = (
13281328
Effect.tap((runtime) => Effect.sync(() => (attachedTo = runtime))),
13291329
Effect.uninterruptible,
13301330
);
1331+
// A thread stays attached to the session it ran on before switching
1332+
// providers, and the other provider's attach took over the thread's
1333+
// credential slot. A run returning to this session must take it
1334+
// back, or the process calls T3 tools scoped to the other provider.
1335+
const reclaimsCredential =
1336+
attached === undefined &&
1337+
(yield* mcpSessions.read(input.threadId))?.providerInstanceId !==
1338+
input.providerInstanceId &&
1339+
(yield* Ref.get(sessions))
1340+
.get(sessionKey(input.providerSessionId))
1341+
?.attachedThreadIds.has(input.threadId) === true;
1342+
if (attached === undefined && !reclaimsCredential) return;
1343+
const prepared = yield* prepareMcpSession(input.threadId, input.providerInstanceId);
1344+
preparedForCleanup = prepared;
1345+
if (prepared.mcpCredentialId !== undefined) {
1346+
const mcpCredentialId = prepared.mcpCredentialId;
1347+
yield* Ref.update(sessions, (current) => {
1348+
const key = sessionKey(input.providerSessionId);
1349+
const entry = current.get(key);
1350+
if (entry === undefined) return current;
1351+
const mcpCredentialIdByThread = new Map(entry.mcpCredentialIdByThread);
1352+
mcpCredentialIdByThread.set(input.threadId, mcpCredentialId);
1353+
const updated = new Map(current);
1354+
updated.set(key, { ...entry, mcpCredentialIdByThread });
1355+
return updated;
1356+
});
1357+
}
13311358
if (attached !== undefined) {
1332-
const prepared = yield* prepareMcpSession(input.threadId, input.providerInstanceId);
1333-
preparedForCleanup = prepared;
1334-
if (prepared.mcpCredentialId !== undefined) {
1335-
const mcpCredentialId = prepared.mcpCredentialId;
1336-
yield* Ref.update(sessions, (current) => {
1337-
const key = sessionKey(input.providerSessionId);
1338-
const entry = current.get(key);
1339-
if (entry === undefined) return current;
1340-
const mcpCredentialIdByThread = new Map(entry.mcpCredentialIdByThread);
1341-
mcpCredentialIdByThread.set(input.threadId, mcpCredentialId);
1342-
const updated = new Map(current);
1343-
updated.set(key, { ...entry, mcpCredentialIdByThread });
1344-
return updated;
1345-
});
1346-
}
13471359
const entry = (yield* Ref.get(sessions)).get(sessionKey(input.providerSessionId));
13481360
if (entry !== undefined) {
13491361
yield* withActivityError(

0 commit comments

Comments
 (0)