Skip to content

Commit 0c77b14

Browse files
fix(server): t3-code tools keep working after a turn waits over a day
A run now refreshes its thread's MCP credential every hour while its provider event stream is open. A turn blocked on a user answer or an approval for more than 24 hours keeps its token, and a session that exits without a clean stop still expires after the 24-hour window. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5fe7568 commit 0c77b14

3 files changed

Lines changed: 123 additions & 3 deletions

File tree

‎apps/server/src/mcp/McpSessionRegistry.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ export interface McpSessionRegistryOptions {
7070
* session.
7171
*
7272
* Liveness is refreshed both by MCP traffic and by `touch` on every provider
73-
* turn, so a session that is still doing work never expires no matter how long
74-
* it goes between browser tool calls. This window therefore only bounds
73+
* turn and hourly while a turn's event stream is open, so a session that is
74+
* still doing work or waiting on the user never expires no matter how long it
75+
* goes between tool calls. This window therefore only bounds
7576
* credentials whose session died without a clean stop — the normal paths
7677
* (`stopSession`, `stopAll`) revoke eagerly and do not wait for it.
7778
*
@@ -249,7 +250,8 @@ export const issueActiveMcpCredential = (
249250

250251
/**
251252
* Refreshes the liveness of a thread's MCP credential. Called on every provider
252-
* turn so an active session is never mistaken for an abandoned one.
253+
* turn, and hourly while its event stream is open, so an active session is
254+
* never mistaken for an abandoned one.
253255
*/
254256
export const touchActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>
255257
activeMcpSessionRegistry ? activeMcpSessionRegistry.touch(threadId) : Effect.void;

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ChatAttachmentId,
55
ChatFileAttachment,
66
CommandId,
7+
EnvironmentId,
78
EventId,
89
MessageId,
910
NodeId,
@@ -36,7 +37,12 @@ import * as Layer from "effect/Layer";
3637
import * as Option from "effect/Option";
3738
import * as Ref from "effect/Ref";
3839
import * as Stream from "effect/Stream";
40+
import { HttpServer } from "effect/http";
41+
import * as NetAddress from "effect/net/NetAddress";
42+
import * as TestClock from "effect/testing/TestClock";
43+
import * as NodeServices from "@effect/platform-node/NodeServices";
3944

45+
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
4046
import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts";
4147
import * as ServerSettings from "../serverSettings.ts";
4248
import * as CheckpointService from "./CheckpointService.ts";
@@ -1030,6 +1036,109 @@ it.effect("refreshes MCP credential liveness before calling the provider", () =>
10301036
}).pipe(Effect.provide(layerRunExecutionTest)),
10311037
);
10321038

1039+
const layerMcpSessionRegistryTest = McpSessionRegistry.layer.pipe(
1040+
Layer.provide(
1041+
Layer.mergeAll(
1042+
NodeServices.layer,
1043+
Layer.succeed(
1044+
HttpServer.HttpServer,
1045+
HttpServer.HttpServer.of({
1046+
address: NetAddress.inetAddressFromIpStringUnsafe("127.0.0.1", 43123),
1047+
serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
1048+
}),
1049+
),
1050+
Layer.succeed(
1051+
ServerEnvironment.ServerEnvironment,
1052+
ServerEnvironment.ServerEnvironment.of({
1053+
getEnvironmentId: Effect.succeed(EnvironmentId.make("environment:mcp-heartbeat")),
1054+
getDescriptor: Effect.die("unused"),
1055+
}),
1056+
),
1057+
),
1058+
),
1059+
);
1060+
1061+
it.effect.each([
1062+
["keeps the MCP credential alive while the turn waits on the user", Stream.never, true],
1063+
[
1064+
"lets the MCP credential expire once the provider event stream ends",
1065+
// The provider exits two hours into the turn.
1066+
Stream.fromEffect(Effect.sleep("2 hours")).pipe(Stream.drain),
1067+
false,
1068+
],
1069+
] as const)("%s", ([, events, tokenSurvives]) =>
1070+
Effect.gen(function* () {
1071+
const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
1072+
const registry = yield* McpSessionRegistry.McpSessionRegistry;
1073+
const threadId = ThreadId.make("thread:run-execution-mcp-heartbeat");
1074+
const issued = yield* registry.issue({
1075+
threadId,
1076+
providerInstanceId: ProviderInstanceId.make("codex"),
1077+
capabilities: new Set(),
1078+
});
1079+
const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
1080+
1081+
yield* runExecution.startRootRun({
1082+
commandId: CommandId.make("command:run-execution-mcp-heartbeat"),
1083+
appThread: { id: threadId } as OrchestrationV2AppThread,
1084+
providerSessionId: ProviderSessionId.make(issued.config.providerSessionId),
1085+
session: {
1086+
events,
1087+
startTurn: () => Effect.void,
1088+
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime,
1089+
run: {
1090+
id: RunId.make("run:run-execution-mcp-heartbeat"),
1091+
threadId,
1092+
ordinal: 1,
1093+
providerInstanceId: ProviderInstanceId.make("codex"),
1094+
} as OrchestrationV2Run,
1095+
rootNode: {
1096+
id: NodeId.make("node:run-execution-mcp-heartbeat"),
1097+
} as OrchestrationV2ExecutionNode,
1098+
checkpointScope: {
1099+
id: CheckpointScopeId.make("checkpoint-scope:run-execution-mcp-heartbeat"),
1100+
} as OrchestrationV2CheckpointScope,
1101+
providerThread: {
1102+
id: ProviderThreadId.make("provider-thread:run-execution-mcp-heartbeat"),
1103+
driver,
1104+
} as OrchestrationV2ProviderThread,
1105+
attempt: {
1106+
id: RunAttemptId.make("attempt:run-execution-mcp-heartbeat"),
1107+
providerTurnId: null,
1108+
} as OrchestrationV2RunAttempt,
1109+
attemptId: RunAttemptId.make("attempt:run-execution-mcp-heartbeat"),
1110+
providerTurnOrdinal: 1,
1111+
message: {
1112+
messageId: MessageId.make("message:run-execution-mcp-heartbeat"),
1113+
text: "Ask me something and wait.",
1114+
attachments: [],
1115+
createdBy: "user",
1116+
creationSource: "web",
1117+
},
1118+
modelSelection: {
1119+
instanceId: ProviderInstanceId.make("codex"),
1120+
model: "gpt-5.4",
1121+
},
1122+
runtimePolicy: {
1123+
runtimeMode: "full-access",
1124+
interactionMode: "default",
1125+
cwd: process.cwd(),
1126+
approvalPolicy: "never",
1127+
sandboxPolicy: {
1128+
type: "readOnly",
1129+
access: { type: "fullAccess" },
1130+
networkAccess: false,
1131+
},
1132+
},
1133+
});
1134+
// The report in #14076 answered a question 44 hours into the turn.
1135+
yield* TestClock.adjust("44 hours");
1136+
1137+
const resolved = yield* registry.resolve(token);
1138+
assert.equal(resolved?.thread.threadId === threadId, tokenSurvives);
1139+
}).pipe(Effect.provide(Layer.merge(layerRunExecutionTest, layerMcpSessionRegistryTest))),
1140+
);
1141+
10331142
it.effect("starts the provider when checkpoint baseline capture fails", () =>
10341143
Effect.gen(function* () {
10351144
const threadId = ThreadId.make("thread:run-execution-baseline-failure");

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,14 @@ export const layer: Layer.Layer<
12241224
return true;
12251225
});
12261226
const filterAssistantEvent = makeAssistantStreamingFilter(responseStreamingMode);
1227+
// A turn can wait days on a user answer or approval without any MCP
1228+
// traffic. Keep the credential alive while its event stream is open;
1229+
// the stream ends once the run and its background work settle or the
1230+
// provider exits, so a dead session still expires. A failed touch
1231+
// must not end the race and abort event ingestion.
1232+
const keepMcpCredentialAlive = McpSessionRegistry.touchActiveMcpThread(
1233+
input.run.threadId,
1234+
).pipe(Effect.ignoreCause({ log: true }), Effect.delay("1 hour"), Effect.forever);
12271235
const providerEventFiber = yield* eventSubscription.events.pipe(
12281236
Stream.filterEffect((event) =>
12291237
Ref.modify(eventRouting, (state) => routeProviderEvent(event, routeIdentity, state)),
@@ -1379,6 +1387,7 @@ export const layer: Layer.Layer<
13791387
),
13801388
),
13811389
Effect.ensuring(eventSubscription.close),
1390+
Effect.raceFirst(keepMcpCredentialAlive),
13821391
Effect.forkDetach,
13831392
);
13841393

0 commit comments

Comments
 (0)