diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts index b6f746269a6f2..8566cbb83f9e9 100644 --- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.ts @@ -70,11 +70,17 @@ const AntigravityProviderCapabilitiesV2 = { }, } satisfies OrchestrationV2ProviderCapabilities; +/** Thread variables reach the driver here; `makeAntigravityAcpRuntime` never reads them. */ +export type AntigravityAdapterV2RuntimeInput = Omit< + AntigravityAcpRuntimeInput, + "spawn" | "childProcessSpawner" +> & { readonly processEnvironment?: NodeJS.ProcessEnv }; + export interface AntigravityAdapterV2Options { readonly instanceId: ProviderInstanceId; readonly selfInvocation: SelfInvocation; readonly makeRuntime: ( - input: Omit, + input: AntigravityAdapterV2RuntimeInput, ) => Effect.Effect< AcpSessionRuntime.AcpSessionRuntime["Service"], EffectAcpErrors.AcpError | ProviderSetupError, diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.test.ts b/apps/server/src/provider/Drivers/AntigravityDriver.test.ts index 447745fc673a0..fe33021057651 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.test.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.test.ts @@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { expect, it } from "@effect/vitest"; import { ANTIGRAVITY_DEFAULT_MODEL, + EnvironmentId, ProviderInstanceId, ProviderSessionId, ThreadId, @@ -57,7 +58,12 @@ function shellQuote(value: string): string { } const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* ( - options: { readonly config?: Partial; readonly enabled?: boolean } = {}, + options: { + readonly config?: Partial; + readonly enabled?: boolean; + /** Extra instance variables, such as ones a parent T3 turn already exported. */ + readonly environment?: ReadonlyArray<{ readonly name: string; readonly value: string }>; + } = {}, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -136,6 +142,7 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* ( args: ReadonlyArray; cwd: string | undefined; extendEnv: boolean | undefined; + environment: Record; profileDirectory: string | undefined; harnessPath: string | undefined; forceFileStorage: string | undefined; @@ -191,6 +198,7 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* ( args: command.args.map(String), cwd: command.options.cwd, extendEnv: command.options.extendEnv, + environment: Object.fromEntries(Object.keys(environment).map((name) => [name, env(name)])), profileDirectory: env("GEMINI_HOME"), harnessPath: env("ANTIGRAVITY_HARNESS_PATH"), forceFileStorage: env("AGY_ACP_FORCE_FILE_STORAGE"), @@ -221,6 +229,7 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* ( { name: "GEMINI_HOME", value: "/must-not-be-used" }, { name: "ANTIGRAVITY_HARNESS_PATH", value: "/must-not-be-used" }, { name: "BROWSER", value: "must-not-run" }, + ...(options.environment ?? []), ].map((variable) => ({ ...variable, sensitive: false })), }).pipe( Effect.provide(layerInstallation), @@ -447,6 +456,79 @@ it.layer(layerTest)("AntigravityDriver", (it) => { }), ); + it.effect.skipIf(windowsHost)( + "gives a thread session its own T3 thread and MCP fallback environment", + () => + Effect.gen(function* () { + const h = yield* makeHarness({ + environment: [ + { name: "T3CODE_THREAD_ID", value: "thread-antigravity-ambient" }, + { name: "T3_ACP_MCP_ENDPOINT", value: "http://127.0.0.1:9/ambient" }, + { name: "T3_ACP_MCP_AUTHORIZATION", value: "Bearer ambient-token" }, + { name: "T3_ACP_MCP_NODE", value: "/ambient/node" }, + { name: "T3_ACP_MCP_ENTRYPOINT", value: "/ambient/entrypoint.mjs" }, + ], + }); + yield* h.refresh(); + const endpoint = "http://127.0.0.1:43126/mcp"; + const threadId = ThreadId.make("thread-antigravity-process-environment"); + yield* (yield* McpProviderSessions.McpProviderSessions).set({ + environmentId: EnvironmentId.make("environment-antigravity-process-environment"), + threadId, + providerSessionId: "mcp-session-antigravity-process-environment", + providerInstanceId: h.instance.instanceId, + endpoint, + authorizationHeader: "Bearer antigravity-thread-token", + browserToolsAvailable: false, + }); + // Running from source, the fallback entrypoint is this process's script. + const entrypoint = h.path.resolve(process.argv[1]!); + yield* Effect.scoped( + Effect.gen(function* () { + const workspace = yield* h.fs.makeTempDirectoryScoped({ prefix: "t3-ag-environment-" }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "approval-required", + interactionMode: "default", + cwd: workspace, + }); + const modelSelection = { instanceId: h.instance.instanceId, model: "gemini-test-low" }; + const session = yield* h.instance.orchestrationAdapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make( + "provider-session-antigravity-process-environment", + ), + modelSelection, + runtimePolicy, + }); + yield* session.ensureThread({ threadId, modelSelection, runtimePolicy }); + }), + ); + + // Gemini Enterprise can strip the native MCP servers, and the documented `acp-mcp-call` + // fallback reads these from the shell. + const agentLaunches = h.launches.filter( + (launch) => launch.command === h.first.executablePath, + ); + expect(agentLaunches).toHaveLength(2); + expect(agentLaunches[1]?.environment).toMatchObject({ + T3CODE_THREAD_ID: threadId, + T3_ACP_MCP_ENDPOINT: endpoint, + T3_ACP_MCP_AUTHORIZATION: "Bearer antigravity-thread-token", + T3_ACP_MCP_NODE: process.execPath, + T3_ACP_MCP_ENTRYPOINT: entrypoint, + }); + // Model refresh and sign-in launches are not part of a thread. + expect(agentLaunches[0]?.environment).toMatchObject({ + T3CODE_THREAD_ID: "thread-antigravity-ambient", + T3_ACP_MCP_ENDPOINT: "http://127.0.0.1:9/ambient", + T3_ACP_MCP_AUTHORIZATION: "Bearer ambient-token", + T3_ACP_MCP_NODE: "/ambient/node", + T3_ACP_MCP_ENTRYPOINT: "/ambient/entrypoint.mjs", + }); + yield* h.assertClosed; + }), + ); + it.effect.skipIf(windowsHost)( "closes refresh processes and clears account metadata when Google sign-in is required", () => diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.ts b/apps/server/src/provider/Drivers/AntigravityDriver.ts index 75bca5acc87be..2491b5ef46442 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.ts @@ -58,7 +58,10 @@ import { } from "../acp/AntigravitySessionFiles.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; -import { makeAntigravityAdapterV2 } from "../../orchestration-v2/Adapters/AntigravityAdapterV2.ts"; +import { + makeAntigravityAdapterV2, + type AntigravityAdapterV2RuntimeInput, +} from "../../orchestration-v2/Adapters/AntigravityAdapterV2.ts"; import { makeAcpNativeLoggerFactory } from "@t3tools/provider-acp/server/nativeLogging"; import { ProviderDriverError } from "../Errors.ts"; import { makeAntigravityProvider } from "../AntigravityProvider.ts"; @@ -198,7 +201,7 @@ export const AntigravityDriver: ProviderDriver< ); const makeRuntime = Effect.fn("AntigravityDriver.makeRuntime")(function* ( - input: Omit, + input: AntigravityAdapterV2RuntimeInput, ): Effect.fn.Return< AcpSessionRuntime.AcpSessionRuntime["Service"], AcpErrors.AcpError | ProviderSetupError, @@ -267,16 +270,23 @@ export const AntigravityDriver: ProviderDriver< ), ), ); + // `buildAntigravityAcpSpawnInput` sets `extendEnv: false`, so thread variables only reach the + // agent's shell through `baseEnv`. + const { processEnvironment: threadEnvironment, ...runtimeInput } = input; + const spawnEnvironment = + threadEnvironment === undefined + ? processEnvironment + : { ...processEnvironment, ...threadEnvironment }; const runtime = yield* makeAntigravityAcpRuntime({ - ...input, + ...runtimeInput, runtimeVersion: executable.version, authMethod: auth.authMethod, childProcessSpawner: spawner, spawn: buildAntigravityAcpSpawnInput({ installation: executable, profile, - cwd: input.cwd, - baseEnv: processEnvironment, + cwd: runtimeInput.cwd, + baseEnv: spawnEnvironment, auth, runtimeTempDirectory, }), @@ -291,7 +301,8 @@ export const AntigravityDriver: ProviderDriver< .start() .pipe( Effect.tapError((cause): Effect.Effect => - input.onAuthorizationUrl === undefined && isAntigravitySignInRequiredError(cause) + runtimeInput.onAuthorizationUrl === undefined && + isAntigravitySignInRequiredError(cause) ? provider.onAuthRequired : Effect.void, ),