From 5ac9d9c30846e2a85ad6fcee67cb4195dac73ae7 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 18 Nov 2026 23:35:31 -0300 Subject: [PATCH 1/2] fix(server): preserve thread identity in Grok and Cursor agents --- .../Adapters/GrokAdapterV2.test.ts | 48 +++++++++++++++++ .../src/server/adapter.test.ts | 7 +++ .../provider-cursor/src/server/adapter.ts | 12 +++-- .../src/server/workspaceMcpServers.test.ts | 53 +++++++++++++++++++ .../src/server/workspaceMcpServers.ts | 2 + packages/provider-grok/src/server/adapter.ts | 4 +- 6 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 packages/provider-cursor/src/server/workspaceMcpServers.test.ts diff --git a/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts index dec55480af300..58144df2c0d0c 100644 --- a/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts @@ -1,6 +1,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { + EnvironmentId, ProjectId, MessageId, NodeId, @@ -289,6 +290,53 @@ describe("Grok launch permission mode", () => { ...override, }); + it.effect("passes the current thread identity to the Grok process", () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("grok-launch-test"); + const threadId = ThreadId.make("grok-launch-thread-env"); + const endpoint = "http://127.0.0.1:43123/mcp"; + yield* (yield* McpProviderSessions.McpProviderSessions).set({ + environmentId: EnvironmentId.make("grok-launch-environment"), + threadId, + providerSessionId: "grok-launch-mcp-session", + providerInstanceId: instanceId, + endpoint, + authorizationHeader: "Bearer synthetic-thread-token", + browserToolsAvailable: false, + }); + const environments: Array | undefined> = []; + const spawner = ChildProcessSpawner.make((command) => { + if (command._tag === "StandardCommand") environments.push(command.options.env); + return Effect.fail( + PlatformError.systemError({ + _tag: "NotFound", + module: "grok-launch-test", + method: "spawn", + }), + ); + }); + const adapter = yield* makeGrokAdapterV2({ + instanceId, + settings: LAUNCH_TEST_GROK_SETTINGS, + environment: { T3CODE_THREAD_ID: "stale-parent-thread", GROK_HOME: "profile" }, + hostPlatform: "darwin", + selfInvocation: yield* resolveSelfInvocation(), + }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)); + yield* adapter + .openSession({ + threadId, + providerSessionId: ProviderSessionId.make("grok-launch-provider-session"), + modelSelection: { instanceId, model: "grok-build" }, + runtimePolicy: policy("full-access"), + }) + .pipe(Effect.scoped, Effect.ignore); + assert.lengthOf(environments, 1); + assert.equal(environments[0]?.T3CODE_THREAD_ID, threadId); + assert.equal(environments[0]?.T3_ACP_MCP_ENDPOINT, endpoint); + assert.equal(environments[0]?.GROK_HOME, "profile"); + }).pipe(Effect.provideService(HostProcess.Platform, "darwin"), Effect.provide(layerTest)), + ); + it.effect.each( ( [ diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.ts index 919e5f3de6666..ca6b68923eddb 100644 --- a/packages/provider-cursor/src/server/adapter.test.ts +++ b/packages/provider-cursor/src/server/adapter.test.ts @@ -2662,6 +2662,7 @@ describe("CursorAdapterV2", () => { "env-only": { "command": "node", "args": ["server.mjs"], "env": { "PROJECT_ROOT": "\${workspaceFolder}" } }, "cwd-only": { "command": "node", "args": ["server.mjs"], "cwd": "\${workspaceFolder}" }, "unset-env": { "command": "node", "env": { "CONFIG": "\${env:T3_MCP_UNSET}" } }, + "session-env": { "command": "inbox-server", "env": { "T3CODE_THREAD_ID": "\${env:T3CODE_THREAD_ID}" } }, "wrapped": { "command": "proto", "args": ["run", "node", "--", "\${workspaceFolder}/server.mjs"] }, "label": { "command": "node", "args": ["server.mjs", "--label", "\${workspaceFolder}/client"] }, "env-file": { "command": "node", "args": ["\${workspaceFolder}/server.mjs"], "envFile": ".env.mcp" }, @@ -2723,6 +2724,7 @@ describe("CursorAdapterV2", () => { CURSOR_DATA_DIR: path.join(scratch, "override"), DOCS_TOKEN: "docs-token", MCP_TYPE: "stdio", + T3CODE_THREAD_ID: "stale-parent-thread", }, }).pipe( Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, { @@ -2817,6 +2819,11 @@ describe("CursorAdapterV2", () => { cwd: projectRoot, }, "cwd-only": { command: "node", args: ["server.mjs"], cwd: projectRoot }, + "session-env": { + command: "inbox-server", + env: { T3CODE_THREAD_ID: threadId }, + cwd: projectRoot, + }, }; // The agent is created with T3's server only; the resolved servers go with each turn. assert.deepEqual(openedWith?.mcpServers, t3Servers); diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index a72773c18ea3c..152e44e149ec1 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -2498,10 +2498,13 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( } }; - const resolveWorkspaceMcpServers = (cwd: string | null) => + const resolveWorkspaceMcpServers = (cwd: string | null, threadId: ThreadId) => cwd === null ? Effect.succeed({}) - : cursorWorkspaceMcpServers(cwd, adapterOptions.environment).pipe( + : cursorWorkspaceMcpServers(cwd, { + ...adapterOptions.environment, + T3CODE_THREAD_ID: threadId, + }).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), @@ -2762,7 +2765,10 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( // A send-time set replaces the create-time set, so the workspace servers go only here and are // read again each turn from mcp.json and mcp-disabled.json; both merge with settingSources. const turnMcpServers = { - ...(yield* resolveWorkspaceMcpServers(turnInput.runtimePolicy.cwd)), + ...(yield* resolveWorkspaceMcpServers( + turnInput.runtimePolicy.cwd, + turnInput.threadId, + )), ...mcpServers, }; const sdkRun = yield* agent.session diff --git a/packages/provider-cursor/src/server/workspaceMcpServers.test.ts b/packages/provider-cursor/src/server/workspaceMcpServers.test.ts new file mode 100644 index 0000000000000..176c7252583b5 --- /dev/null +++ b/packages/provider-cursor/src/server/workspaceMcpServers.test.ts @@ -0,0 +1,53 @@ +// Verifies conversation-specific MCP environments without starting a model. +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { cursorWorkspaceMcpServers } from "./workspaceMcpServers.ts"; + +it.effect("resolves each session's identity for stdio MCPs in the same folder", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-mcp-thread-" }); + yield* fileSystem.makeDirectory(path.join(root, ".cursor")); + yield* fileSystem.writeFileString( + path.join(root, ".cursor", "mcp.json"), + JSON.stringify({ + mcpServers: { + inbox: { + command: "inbox-server", + env: { + T3CODE_THREAD_ID: "${env:T3CODE_THREAD_ID}", + KEEP: "configured", + }, + }, + ordinary: { command: "other-server" }, + }, + }), + ); + for (const threadId of ["thread-one", "thread-two"]) { + const servers = yield* cursorWorkspaceMcpServers(root, { T3CODE_THREAD_ID: threadId }).pipe( + Effect.provideService(HostProcess.HomeDirectory, root), + Effect.provideService(HostProcess.Environment, {}), + ); + assert.deepEqual(servers.inbox, { + command: "inbox-server", + cwd: root, + env: { T3CODE_THREAD_ID: threadId, KEEP: "configured" }, + }); + assert.notProperty(servers, "ordinary"); + } + const unbound = yield* cursorWorkspaceMcpServers(root, {}).pipe( + Effect.provideService(HostProcess.HomeDirectory, root), + Effect.provideService(HostProcess.Environment, {}), + ); + assert.deepEqual(unbound.inbox, { + command: "inbox-server", + cwd: root, + env: { T3CODE_THREAD_ID: "", KEEP: "configured" }, + }); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); diff --git a/packages/provider-cursor/src/server/workspaceMcpServers.ts b/packages/provider-cursor/src/server/workspaceMcpServers.ts index f3e53b508e3b3..baf45e3ab1937 100644 --- a/packages/provider-cursor/src/server/workspaceMcpServers.ts +++ b/packages/provider-cursor/src/server/workspaceMcpServers.ts @@ -14,6 +14,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; const PLACEHOLDER_PATTERN = /\$\{(?:env:([A-Za-z_][A-Za-z0-9_]*)|([^:}]+)(?::-([^}]*))?)\}/g; const WORKSPACE_VARIABLE_PATTERN = /\$\{(?:workspaceFolder|workspaceFolderBasename|userHome|pathSeparator|\/)\}/; +const THREAD_VARIABLE_PATTERN = /\$\{(?:env:)?T3CODE_THREAD_ID\}/; const WORKSPACE_PATH_PATTERN = /^\$\{(?:workspaceFolder|userHome)\}[\\/]/; // These exit when their script argument is missing; other arguments may be labels a server ignores. const SCRIPT_INTERPRETERS = new Set(["node", "bun", "tsx", "python", "python3"]); @@ -61,6 +62,7 @@ function needsWorkspaceExpansion(server: unknown): boolean { if ("envFile" in server || "enabledTools" in server) return false; const { command, cwd, args } = server as Record; return ( + THREAD_VARIABLE_PATTERN.test(JSON.stringify(server)) || [command, cwd].some( (text) => typeof text === "string" && WORKSPACE_VARIABLE_PATTERN.test(text), ) || diff --git a/packages/provider-grok/src/server/adapter.ts b/packages/provider-grok/src/server/adapter.ts index db5a136697c10..b64688603b5f8 100644 --- a/packages/provider-grok/src/server/adapter.ts +++ b/packages/provider-grok/src/server/adapter.ts @@ -403,12 +403,12 @@ export function makeGrokAcpAdapterFlavor( ownedModelOptionIds: [GROK_REASONING_EFFORT_OPTION_ID], makeRuntime: options.makeRuntime ?? - (({ runtimePolicy, ...input }) => + (({ runtimePolicy, processEnvironment, ...input }) => makeGrokAcpRuntime({ ...input, interruptPromptOnCancel: input.interruptPromptOnCancel ?? false, grokSettings: options.settings, - environment: options.environment, + environment: { ...options.environment, ...processEnvironment }, runtimeMode: grokLaunchRuntimeMode(runtimePolicy), })), // Auto prompts represent classifier refusals and must reach the user. From 34d4abbe30d98c0366fb8b2205765b165bc706a9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 02:48:41 +0000 Subject: [PATCH 2/2] test(cursor): keep thread-id coverage in the adapter test The adapter test already proves a stdio MCP gets the turn's thread id; the separate resolver test repeated it one layer down. Note the thread-id case in the expansion comment. Co-Authored-By: Claude Opus 5.5 --- .../src/server/workspaceMcpServers.test.ts | 53 ------------------- .../src/server/workspaceMcpServers.ts | 5 +- 2 files changed, 3 insertions(+), 55 deletions(-) delete mode 100644 packages/provider-cursor/src/server/workspaceMcpServers.test.ts diff --git a/packages/provider-cursor/src/server/workspaceMcpServers.test.ts b/packages/provider-cursor/src/server/workspaceMcpServers.test.ts deleted file mode 100644 index 176c7252583b5..0000000000000 --- a/packages/provider-cursor/src/server/workspaceMcpServers.test.ts +++ /dev/null @@ -1,53 +0,0 @@ -// Verifies conversation-specific MCP environments without starting a model. -import * as NodeServices from "@effect/platform-node/NodeServices"; -import { assert, it } from "@effect/vitest"; -import * as Effect from "effect/Effect"; -import * as FileSystem from "effect/FileSystem"; -import * as Path from "effect/Path"; -import * as HostProcess from "@t3tools/shared/HostProcess"; -import { cursorWorkspaceMcpServers } from "./workspaceMcpServers.ts"; - -it.effect("resolves each session's identity for stdio MCPs in the same folder", () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-mcp-thread-" }); - yield* fileSystem.makeDirectory(path.join(root, ".cursor")); - yield* fileSystem.writeFileString( - path.join(root, ".cursor", "mcp.json"), - JSON.stringify({ - mcpServers: { - inbox: { - command: "inbox-server", - env: { - T3CODE_THREAD_ID: "${env:T3CODE_THREAD_ID}", - KEEP: "configured", - }, - }, - ordinary: { command: "other-server" }, - }, - }), - ); - for (const threadId of ["thread-one", "thread-two"]) { - const servers = yield* cursorWorkspaceMcpServers(root, { T3CODE_THREAD_ID: threadId }).pipe( - Effect.provideService(HostProcess.HomeDirectory, root), - Effect.provideService(HostProcess.Environment, {}), - ); - assert.deepEqual(servers.inbox, { - command: "inbox-server", - cwd: root, - env: { T3CODE_THREAD_ID: threadId, KEEP: "configured" }, - }); - assert.notProperty(servers, "ordinary"); - } - const unbound = yield* cursorWorkspaceMcpServers(root, {}).pipe( - Effect.provideService(HostProcess.HomeDirectory, root), - Effect.provideService(HostProcess.Environment, {}), - ); - assert.deepEqual(unbound.inbox, { - command: "inbox-server", - cwd: root, - env: { T3CODE_THREAD_ID: "", KEEP: "configured" }, - }); - }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), -); diff --git a/packages/provider-cursor/src/server/workspaceMcpServers.ts b/packages/provider-cursor/src/server/workspaceMcpServers.ts index baf45e3ab1937..f1c9b5153c7af 100644 --- a/packages/provider-cursor/src/server/workspaceMcpServers.ts +++ b/packages/provider-cursor/src/server/workspaceMcpServers.ts @@ -55,8 +55,9 @@ function expand(value: A, resolve: (text: string) => string): A { return value; } -// Only servers whose literal copy cannot start: the SDK also starts the file-defined copy and -// loses its handle when it keeps the inline one, and inline configs drop envFile and enabledTools. +// Only servers whose literal copy cannot start or would read another thread's id: the SDK also +// starts the file-defined copy and loses its handle when it keeps the inline one, and inline +// configs drop envFile and enabledTools. function needsWorkspaceExpansion(server: unknown): boolean { if (typeof server !== "object" || server === null) return false; if ("envFile" in server || "enabledTools" in server) return false;