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.ts b/packages/provider-cursor/src/server/workspaceMcpServers.ts index f3e53b508e3b3..f1c9b5153c7af 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"]); @@ -54,13 +55,15 @@ 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; 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.