From 133c04c80806e752d6b78ddc69a1c39e6f10ed80 Mon Sep 17 00:00:00 2001 From: Sharad-Patel1 <96814788+Sharad-Patel1@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:51:21 +0800 Subject: [PATCH 1/2] test(server): an MCP launch retried after credential reissue creates one thread Also correct the stale "Launch has no retry key" note: the launch tool already replays a retry that reuses its clientRequestId. Co-Authored-By: Claude Opus 5.5 --- .../ThreadLaunchService.test.ts | 135 ++++++++++++++++++ .../orchestrator-mcp-server.md | 6 +- 2 files changed, 139 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts index c3a6ad0a0a..1cc4b270f1 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts @@ -71,6 +71,14 @@ import * as ThreadTitleRegeneration from "./ThreadTitleRegenerationService.ts"; import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; import * as RemoteDelegation from "../peer/RemoteDelegation.ts"; import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; +import { HttpServer } from "effect/http"; +import * as NetAddress from "effect/net/NetAddress"; +import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; +import * as McpInvocationContext from "../mcp/McpInvocationContext.ts"; +import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts"; +import * as McpToolAccess from "../mcp/McpToolAccess.ts"; +import * as ProjectHandlers from "../mcp/toolkits/project/handlers.ts"; +import { ProjectToolkit } from "../mcp/toolkits/project/tools.ts"; const projectId = ProjectId.make("project:launch-test"); const otherProjectId = ProjectId.make("project:launch-other"); @@ -3215,3 +3223,130 @@ it.effect("replays an accepted send after the project disables the instance", () } }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerFiles))); }); + +it.effect( + "an MCP launch retried after its caller's credential is reissued creates one thread", + () => { + const harness = makeHarness(); + const layerMcp = Layer.mergeAll( + // The launches below call neither of these. + Layer.mock(GitVcsDriver.GitVcsDriver)({}), + Layer.mock(DocumentText.DocumentText)({}), + ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-reissue-" }).pipe( + Layer.provide(NodeServices.layer), + ), + NodeServices.layer, + NodeCrypto.layer, + ); + return Effect.gen(function* () { + const threads = yield* ThreadManagement.ThreadManagementService; + const callerThreadId = ThreadId.make("thread:mcp-reissue-caller"); + const otherCallerThreadId = ThreadId.make("thread:mcp-reissue-other-caller"); + const callers = [callerThreadId, otherCallerThreadId]; + // A caller needs a live run to write through MCP. + for (const threadId of callers) { + yield* threads.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`${threadId}:create`), + threadId, + projectId, + title: "Caller", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + yield* threads.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`${threadId}:start`), + threadId, + messageId: MessageId.make(`${threadId}:message`), + text: "Launch a thread.", + attachments: [], + dispatchMode: { type: "start_immediately" }, + }); + } + + const registry = yield* McpSessionRegistry.__testing.make().pipe( + Effect.provideService( + HttpServer.HttpServer, + HttpServer.HttpServer.of({ + address: NetAddress.inetAddressFromIpStringUnsafe("127.0.0.1", 43123), + serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"], + }), + ), + Effect.provideService( + ServerEnvironment.ServerEnvironment, + ServerEnvironment.ServerEnvironment.of({ + getEnvironmentId: Effect.succeed(EnvironmentId.make("environment")), + getDescriptor: Effect.die("unused"), + }), + ), + ); + // ProviderSessionManager revokes and reissues when a thread's credential no longer resolves. + const reissue = (threadId: ThreadId) => + Effect.gen(function* () { + yield* registry.revokeThread(threadId); + const issued = yield* registry.issue({ + threadId, + providerInstanceId: modelSelection.instanceId, + }); + const scope = yield* registry.resolve( + issued.config.authorizationHeader.replace(/^Bearer\s+/, ""), + ); + assert.isDefined(scope); + return scope; + }); + const toolkit = yield* ProjectToolkit.pipe( + Effect.provide(McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer)), + ); + const launch = (scope: McpInvocationContext.McpInvocationScope, clientRequestId: string) => + toolkit + .handle("t3_thread_launch", { + projectId, + title: "Fix", + message: "Fix the bug", + clientRequestId, + }) + .pipe( + Stream.unwrap, + Stream.runCollect, + Effect.map((chunks) => { + const result = chunks.at(-1)?.result; + assert(result !== undefined && "threadId" in result, "t3_thread_launch failed"); + return result.threadId; + }), + Effect.provideService(McpInvocationContext.McpInvocationContext, scope), + ); + const launchedThreadIds = threads + .listProjectThreads({ projectId, includeSubagents: false }) + .pipe( + Effect.map((entries) => + entries.map((entry) => entry.id).filter((id) => !callers.includes(id)), + ), + ); + + const before = yield* reissue(callerThreadId); + const first = yield* launch(before, "fix-1"); + const after = yield* reissue(callerThreadId); + assert.notEqual(after.thread.providerSessionId, before.thread.providerSessionId); + const retry = yield* launch(after, "fix-1"); + assert.deepEqual(yield* launchedThreadIds, [first]); + assert.equal(retry, first); + // The replay adds no second message or run to the launched thread. + const launched = yield* threads.getThreadProjection(first); + assert.lengthOf(launched.runs, 1); + assert.lengthOf(launched.messages, 1); + + // A new key, or the same key from another thread, is new work. + const fresh = yield* launch(after, "fix-2"); + const other = yield* launch(yield* reissue(otherCallerThreadId), "fix-1"); + assert.sameMembers([...(yield* launchedThreadIds)], [first, fresh, other]); + }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerMcp))); + }, +); diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index fa2770bb10..cccfdc2a54 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -352,8 +352,10 @@ its own under the environment's Scratch project. A new worktree with `startFromO `startFromOrigin: false`. The new thread may not run with broader runtime or interaction modes than the caller. As for `delegate_task`, a runtime mode the provider does not offer is refused, and an inherited or default one steps down -to the broadest mode it offers. Launch has no retry key, so inspect existing threads after a failed or lost response before -launching again. `create_threads` remains the batch option for a shared checkout. +to the broadest mode it offers. Retry a failed or lost launch with the same +`clientRequestId`; if the first call was accepted, the retry returns its thread +instead of launching another. `create_threads` remains the batch option for a +shared checkout. ### `t3_thread_list` -- 2.54.0 (Apple Git-157) From 52d18298ce3c7ba0523e75b9196b5b2595171874 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 22 Oct 2026 19:29:43 +0000 Subject: [PATCH 2/2] fix(server): MCP mutation retries survive credential reissue #23796's production change, unchanged (204dcdf681, a781b07cf1, 5923e2d85f): scope thread callers' request namespace to the calling thread instead of the per-credential id. Its delegate_task test is not included. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/McpSessionRegistry.ts | 4 +++- apps/server/src/mcp/OrchestratorMcpService.ts | 2 +- docs/orchestration-v2/orchestrator-mcp-server.md | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/server/src/mcp/McpSessionRegistry.ts b/apps/server/src/mcp/McpSessionRegistry.ts index ebc8ed74b9..f2c26fd8f3 100644 --- a/apps/server/src/mcp/McpSessionRegistry.ts +++ b/apps/server/src/mcp/McpSessionRegistry.ts @@ -127,7 +127,9 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* ( const browserToolsAvailable = request.browserToolsAvailable ?? true; const scope: McpInvocationContext.McpThreadInvocationScope = { environmentId, - requestNamespace: providerSessionId, + // Keyed by thread, not credential: a reissued credential (for example + // after a server restart) must replay the thread's earlier mutations. + requestNamespace: `thread:${request.threadId}`, thread: { threadId: ThreadId.make(request.threadId), providerSessionId, diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index d1bae5d859..d761da25eb 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -2262,7 +2262,7 @@ const make = Effect.gen(function* () { interactionMode: limits.interactionMode, createdBy: "agent", creationSource: "mcp", - // Scope the idempotency key by provider session so two callers + // Scope the idempotency key by caller namespace (thread or OAuth session) so two callers // reusing the same clientRequestId cannot collide on one task row. ...(input.clientRequestId === undefined ? {} diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index cccfdc2a54..20e41be08c 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -545,7 +545,9 @@ results use the latest assistant content from the final work turn. - A requested model must be advertised by the selected provider when the provider publishes a model list. - `clientRequestId` derives stable command, thread, and message IDs within the - provider session. Retrying the same call returns the same durable work. + caller's namespace: the calling thread for thread callers, so retries survive + a reissued MCP credential, and the OAuth session for client callers. + Retrying the same call returns the same durable work. - Calls without `clientRequestId` receive a generated request key and create new work. -- 2.54.0 (Apple Git-157)