Skip to content

Commit f0589f4

Browse files
nsxdavidjuliusmarminge
authored andcommitted
fix(server): keep thread MCP credentials stable across session detach and re-attach
1 parent 88a9c95 commit f0589f4

5 files changed

Lines changed: 616 additions & 43 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
2828
type: Schema.Literal("provider-session.detach"),
2929
providerSessionId: ProviderSessionId,
3030
detail: Schema.optional(Schema.String),
31+
/** Set on terminal detaches (thread archive/delete): revoke the thread's MCP credentials. */
32+
revokeMcpCredential: Schema.optional(Schema.Boolean),
3133
}),
3234
Schema.Struct({
3335
type: Schema.Literal("provider-turn.start"),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ export const executorLayer: Layer.Layer<
8888
providerSessionId: effect.request.providerSessionId,
8989
threadId: effect.threadId,
9090
...(effect.request.detail === undefined ? {} : { detail: effect.request.detail }),
91+
...(effect.request.revokeMcpCredential === undefined
92+
? {}
93+
: { revokeMcpCredential: effect.request.revokeMcpCredential }),
9194
})
9295
.pipe(
9396
Effect.mapError(

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11041104
: command.type === "thread.runtime-mode.set"
11051105
? "Runtime mode changed."
11061106
: "Provider or model selection changed.",
1107+
// Terminal detaches revoke the thread's MCP credentials; other
1108+
// detach reasons keep them so a re-attaching provider process
1109+
// stays authorized.
1110+
...(command.type === "thread.archive" || command.type === "thread.delete"
1111+
? { revokeMcpCredential: true }
1112+
: {}),
11071113
},
11081114
} satisfies PendingOrchestrationEffectV2;
11091115
yield* Ref.update(effects, (existing) => [...existing, pendingEffect]);

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

Lines changed: 328 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ it.effect(
764764
assert.isDefined(token);
765765
const resolved = yield* registry.resolve(token!);
766766
assert.equal(resolved?.threadId, threadId);
767-
assert.deepEqual(resolved?.capabilities, new Set(["preview", "orchestration"]));
767+
assert.deepEqual(resolved?.capabilities, new Set(["preview", "orchestration", "worktree"]));
768768

769769
yield* manager.close(providerSessionId);
770770
assert.isUndefined(McpProviderSession.readMcpProviderSession(threadId));
@@ -905,6 +905,333 @@ it.effect("ProviderSessionManagerV2 duplicate detach preserves replacement MCP c
905905
}),
906906
);
907907

908+
it.effect(
909+
"ProviderSessionManagerV2 detach of a superseded live session preserves replacement MCP credentials",
910+
() =>
911+
Effect.gen(function* () {
912+
const state = yield* Ref.make(emptyState);
913+
const mcpConfigs = yield* Ref.make<
914+
ReadonlyArray<McpProviderSession.McpProviderSessionConfig | undefined>
915+
>([]);
916+
const effect = Effect.gen(function* () {
917+
const eventSink = yield* EventSinkV2;
918+
const idAllocator = yield* IdAllocatorV2;
919+
const manager = yield* ProviderSessionManagerV2;
920+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
921+
const now = yield* DateTime.now;
922+
const threadId = ThreadId.make("thread-provider-session-manager-superseded-mcp");
923+
const oldSessionId = yield* idAllocator.allocate.providerSession({
924+
providerInstanceId: modelSelection.instanceId,
925+
threadId,
926+
});
927+
const replacementSessionId = yield* idAllocator.allocate.providerSession({
928+
providerInstanceId: modelSelection.instanceId,
929+
threadId,
930+
});
931+
932+
yield* eventSink.write({
933+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
934+
});
935+
yield* manager.open({
936+
threadId,
937+
providerSessionId: oldSessionId,
938+
modelSelection,
939+
runtimePolicy,
940+
});
941+
// The replacement opens while the old session is still attached: this is
942+
// the workspace-handoff sequence, where the queued continuation run can
943+
// start its session before the outbox executes the old session's detach.
944+
yield* manager.open({
945+
threadId,
946+
providerSessionId: replacementSessionId,
947+
modelSelection,
948+
runtimePolicy,
949+
});
950+
951+
const replacement = (yield* Ref.get(mcpConfigs)).at(-1);
952+
assert.isDefined(replacement);
953+
const replacementToken = replacement?.authorizationHeader.replace(/^Bearer\s+/, "");
954+
assert.isDefined(replacementToken);
955+
assert.equal(
956+
McpProviderSession.readMcpProviderSession(threadId)?.providerSessionId,
957+
replacement?.providerSessionId,
958+
);
959+
960+
// First (non-duplicate) detach of the superseded session must not revoke
961+
// the replacement's credential or clear its config slot.
962+
yield* manager.detach({ providerSessionId: oldSessionId, threadId });
963+
964+
assert.equal(
965+
McpProviderSession.readMcpProviderSession(threadId)?.providerSessionId,
966+
replacement?.providerSessionId,
967+
);
968+
assert.equal((yield* registry.resolve(replacementToken!))?.threadId, threadId);
969+
});
970+
971+
yield* effect.pipe(
972+
Effect.provide(
973+
makeTestLayer({
974+
state,
975+
idleTimeoutMs: 1_000,
976+
capabilities: ExclusiveCapabilities,
977+
mcpConfigs,
978+
}),
979+
),
980+
);
981+
}),
982+
);
983+
984+
it.effect(
985+
"ProviderSessionManagerV2 keeps a thread's MCP credential stable across detach and re-attach on a shared session",
986+
() =>
987+
Effect.gen(function* () {
988+
const state = yield* Ref.make(emptyState);
989+
const mcpConfigs = yield* Ref.make<
990+
ReadonlyArray<McpProviderSession.McpProviderSessionConfig | undefined>
991+
>([]);
992+
const effect = Effect.gen(function* () {
993+
const eventSink = yield* EventSinkV2;
994+
const idAllocator = yield* IdAllocatorV2;
995+
const manager = yield* ProviderSessionManagerV2;
996+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
997+
const now = yield* DateTime.now;
998+
const threadId = ThreadId.make("thread-provider-session-manager-stable-mcp");
999+
const providerSessionId = yield* idAllocator.allocate.providerSession({
1000+
providerInstanceId: modelSelection.instanceId,
1001+
threadId,
1002+
});
1003+
1004+
yield* eventSink.write({
1005+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
1006+
});
1007+
yield* manager.open({
1008+
threadId,
1009+
providerSessionId,
1010+
modelSelection,
1011+
runtimePolicy,
1012+
});
1013+
1014+
const original = (yield* Ref.get(mcpConfigs)).at(-1);
1015+
assert.isDefined(original);
1016+
const originalToken = original?.authorizationHeader.replace(/^Bearer\s+/, "");
1017+
assert.isDefined(originalToken);
1018+
1019+
// Workspace-change handoff on a shared multi-thread session (codex):
1020+
// the thread detaches while the provider process keeps running, and the
1021+
// process's MCP client keeps using the credential it was started with.
1022+
yield* manager.detach({ providerSessionId, threadId, detail: "Workspace changed." });
1023+
assert.equal(
1024+
(yield* registry.resolve(originalToken!))?.threadId,
1025+
threadId,
1026+
"detach must not revoke the credential the live provider process still holds",
1027+
);
1028+
1029+
// The continuation run re-attaches the same thread to the same session;
1030+
// the credential must be reused, not rotated, so the provider process's
1031+
// long-lived MCP client stays authorized.
1032+
yield* manager.open({
1033+
threadId,
1034+
providerSessionId,
1035+
modelSelection,
1036+
runtimePolicy,
1037+
});
1038+
assert.equal(
1039+
McpProviderSession.readMcpProviderSession(threadId)?.providerSessionId,
1040+
original?.providerSessionId,
1041+
"re-attach must reuse the existing credential, not rotate it",
1042+
);
1043+
assert.equal((yield* registry.resolve(originalToken!))?.threadId, threadId);
1044+
1045+
// Releasing the session (provider process gone) still revokes.
1046+
yield* manager.close(providerSessionId);
1047+
assert.isUndefined(yield* registry.resolve(originalToken!));
1048+
});
1049+
1050+
yield* effect.pipe(
1051+
Effect.provide(
1052+
makeTestLayer({
1053+
state,
1054+
idleTimeoutMs: 1_000,
1055+
mcpConfigs,
1056+
}),
1057+
),
1058+
);
1059+
}),
1060+
);
1061+
1062+
it.effect(
1063+
"ProviderSessionManagerV2 revokes a rotated credential despite a stale record on another live session",
1064+
() =>
1065+
Effect.gen(function* () {
1066+
const state = yield* Ref.make(emptyState);
1067+
const mcpConfigs = yield* Ref.make<
1068+
ReadonlyArray<McpProviderSession.McpProviderSessionConfig | undefined>
1069+
>([]);
1070+
const effect = Effect.gen(function* () {
1071+
const eventSink = yield* EventSinkV2;
1072+
const idAllocator = yield* IdAllocatorV2;
1073+
const manager = yield* ProviderSessionManagerV2;
1074+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
1075+
const now = yield* DateTime.now;
1076+
const threadId = ThreadId.make("thread-provider-session-manager-stale-record");
1077+
const s1 = yield* idAllocator.allocate.providerSession({
1078+
providerInstanceId: modelSelection.instanceId,
1079+
threadId,
1080+
});
1081+
const s2 = yield* idAllocator.allocate.providerSession({
1082+
providerInstanceId: modelSelection.instanceId,
1083+
threadId,
1084+
});
1085+
1086+
yield* eventSink.write({
1087+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
1088+
});
1089+
// S1 (shared session) records credential C1 for the thread, then the
1090+
// thread detaches; S1 stays alive with the stale record.
1091+
yield* manager.open({ threadId, providerSessionId: s1, modelSelection, runtimePolicy });
1092+
yield* manager.detach({ providerSessionId: s1, threadId });
1093+
1094+
// The credential dies externally, so S2's attach must rotate to C2.
1095+
yield* registry.revokeThread(threadId);
1096+
yield* manager.open({ threadId, providerSessionId: s2, modelSelection, runtimePolicy });
1097+
const rotated = McpProviderSession.readMcpProviderSession(threadId);
1098+
assert.isDefined(rotated);
1099+
const rotatedToken = rotated?.authorizationHeader.replace(/^Bearer\s+/, "");
1100+
assert.isDefined(yield* registry.resolve(rotatedToken!));
1101+
1102+
// Releasing S2 must revoke C2 even though S1 still carries a stale
1103+
// record (of dead C1) for the same thread.
1104+
yield* manager.close(s2);
1105+
assert.isUndefined(
1106+
yield* registry.resolve(rotatedToken!),
1107+
"stale record on S1 must not veto revoking S2's rotated credential",
1108+
);
1109+
yield* manager.close(s1);
1110+
});
1111+
1112+
yield* effect.pipe(
1113+
Effect.provide(makeTestLayer({ state, idleTimeoutMs: 1_000, mcpConfigs })),
1114+
);
1115+
}),
1116+
);
1117+
1118+
it.effect(
1119+
"ProviderSessionManagerV2 protects a reused credential from a predecessor release during open",
1120+
() =>
1121+
Effect.gen(function* () {
1122+
const state = yield* Ref.make(emptyState);
1123+
const mcpConfigs = yield* Ref.make<
1124+
ReadonlyArray<McpProviderSession.McpProviderSessionConfig | undefined>
1125+
>([]);
1126+
const duringOpen = yield* Ref.make<Effect.Effect<void>>(Effect.void);
1127+
const effect = Effect.gen(function* () {
1128+
const eventSink = yield* EventSinkV2;
1129+
const idAllocator = yield* IdAllocatorV2;
1130+
const manager = yield* ProviderSessionManagerV2;
1131+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
1132+
const now = yield* DateTime.now;
1133+
const threadId = ThreadId.make("thread-provider-session-manager-open-race");
1134+
const s1 = yield* idAllocator.allocate.providerSession({
1135+
providerInstanceId: modelSelection.instanceId,
1136+
threadId,
1137+
});
1138+
const s2 = yield* idAllocator.allocate.providerSession({
1139+
providerInstanceId: modelSelection.instanceId,
1140+
threadId,
1141+
});
1142+
1143+
yield* eventSink.write({
1144+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
1145+
});
1146+
yield* manager.open({ threadId, providerSessionId: s1, modelSelection, runtimePolicy });
1147+
const original = (yield* Ref.get(mcpConfigs)).at(-1);
1148+
const originalToken = original?.authorizationHeader.replace(/^Bearer\s+/, "");
1149+
assert.isDefined(originalToken);
1150+
yield* manager.detach({ providerSessionId: s1, threadId });
1151+
1152+
// While S2's provider process is spawning (after prepare reused the
1153+
// credential, before the entry is visible), the predecessor session
1154+
// releases. Eager adapters (ACP, OpenCode) bake the credential into
1155+
// the process during openSession, so the release must not revoke it;
1156+
// rotating afterwards cannot repair those adapters.
1157+
yield* Ref.set(duringOpen, manager.close(s1).pipe(Effect.orDie));
1158+
yield* manager.open({ threadId, providerSessionId: s2, modelSelection, runtimePolicy });
1159+
1160+
const slot = McpProviderSession.readMcpProviderSession(threadId);
1161+
assert.equal(
1162+
slot?.providerSessionId,
1163+
original?.providerSessionId,
1164+
"the credential the adapter was configured with must remain current",
1165+
);
1166+
assert.equal(
1167+
(yield* registry.resolve(originalToken!))?.threadId,
1168+
threadId,
1169+
"the predecessor release must not revoke a credential reserved by an in-flight open",
1170+
);
1171+
yield* manager.close(s2);
1172+
});
1173+
1174+
yield* effect.pipe(
1175+
Effect.provide(
1176+
makeTestLayer({
1177+
state,
1178+
idleTimeoutMs: 1_000,
1179+
mcpConfigs,
1180+
beforeOpen: (input) =>
1181+
input.providerSessionId === undefined
1182+
? Effect.void
1183+
: Ref.get(duringOpen).pipe(
1184+
Effect.flatten,
1185+
Effect.tap(() => Ref.set(duringOpen, Effect.void)),
1186+
),
1187+
}),
1188+
),
1189+
);
1190+
}),
1191+
);
1192+
1193+
it.effect("ProviderSessionManagerV2 terminal detach revokes the thread's MCP credential", () =>
1194+
Effect.gen(function* () {
1195+
const state = yield* Ref.make(emptyState);
1196+
const mcpConfigs = yield* Ref.make<
1197+
ReadonlyArray<McpProviderSession.McpProviderSessionConfig | undefined>
1198+
>([]);
1199+
const effect = Effect.gen(function* () {
1200+
const eventSink = yield* EventSinkV2;
1201+
const idAllocator = yield* IdAllocatorV2;
1202+
const manager = yield* ProviderSessionManagerV2;
1203+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
1204+
const now = yield* DateTime.now;
1205+
const threadId = ThreadId.make("thread-provider-session-manager-terminal-detach");
1206+
const providerSessionId = yield* idAllocator.allocate.providerSession({
1207+
providerInstanceId: modelSelection.instanceId,
1208+
threadId,
1209+
});
1210+
1211+
yield* eventSink.write({
1212+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
1213+
});
1214+
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
1215+
const issued = (yield* Ref.get(mcpConfigs)).at(-1);
1216+
const token = issued?.authorizationHeader.replace(/^Bearer\s+/, "");
1217+
assert.isDefined(yield* registry.resolve(token!));
1218+
1219+
// Archive/delete detaches carry revokeMcpCredential: the token must die
1220+
// with the thread even though the shared provider process lives on.
1221+
yield* manager.detach({
1222+
providerSessionId,
1223+
threadId,
1224+
detail: "Thread deleted.",
1225+
revokeMcpCredential: true,
1226+
});
1227+
assert.isUndefined(yield* registry.resolve(token!));
1228+
assert.isUndefined(McpProviderSession.readMcpProviderSession(threadId));
1229+
});
1230+
1231+
yield* effect.pipe(Effect.provide(makeTestLayer({ state, idleTimeoutMs: 1_000, mcpConfigs })));
1232+
}),
1233+
);
1234+
9081235
it.effect("ProviderSessionManagerV2 releases idle sessions without sweeping all sessions", () =>
9091236
Effect.gen(function* () {
9101237
const state = yield* Ref.make(emptyState);

0 commit comments

Comments
 (0)