Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/server/scripts/acp-mock-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const permissionOptionIds = {
allowAlways: "allow-always",
rejectOnce: "reject-once",
};
const sessionId = "mock-session-1";
const sessionId = process.env.T3_ACP_SESSION_ID ?? "mock-session-1";

let currentModeId = antigravityProfile
? (process.env.T3_ACP_INITIAL_MODE ?? "default")
Expand Down Expand Up @@ -909,9 +909,9 @@ const program = Effect.gen(function* () {
}),
);

yield* agent.handleCancel(({ sessionId }) =>
yield* agent.handleCancel(({ sessionId: requestSessionId }) =>
Effect.gen(function* () {
const cancelledSessionId = String(sessionId ?? "mock-session-1");
const cancelledSessionId = String(requestSessionId ?? sessionId);
cancelledSessions.add(cancelledSessionId);
if (completeFirstPromptOnCancel) {
yield* Deferred.succeed(nativeCancelRequested, undefined);
Expand Down
58 changes: 58 additions & 0 deletions apps/server/src/provider/Drivers/AntigravityDriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* (
readonly newSessionErrorData?: unknown;
readonly initialMode?: string;
readonly agentVersion?: string;
readonly sessionId?: string;
readonly promptResponseText?: string;
} = {},
) {
const directory = path.join(root, name);
Expand All @@ -115,6 +117,12 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* (
...(options.initialMode === undefined
? []
: [`export T3_ACP_INITIAL_MODE=${shellQuote(options.initialMode)}`]),
...(options.sessionId === undefined
? []
: [`export T3_ACP_SESSION_ID=${shellQuote(options.sessionId)}`]),
...(options.promptResponseText === undefined
? []
: [`export T3_ACP_PROMPT_RESPONSE_TEXT=${shellQuote(options.promptResponseText)}`]),
`export T3_ACP_AGENT_VERSION=${shellQuote(options.agentVersion ?? "1.5.12")}`,
`exec ${shellQuote(nodePath)} ${shellQuote(mockAgentPath)} "$@"`,
"",
Expand Down Expand Up @@ -666,6 +674,56 @@ it.layer(layerTest)("AntigravityDriver", (it) => {
}),
);

it.effect.skipIf(windowsHost)(
"runs text generation in an isolated profile even when the instance profile has hooks and MCP",
() =>
Effect.gen(function* () {
const h = yield* makeHarness();
const configDirectory = h.path.join(h.profileDirectory, "config");
const acpDirectory = h.path.join(h.profileDirectory, "antigravity-acp");
yield* h.fs.makeDirectory(configDirectory, { recursive: true });
yield* h.fs.makeDirectory(acpDirectory, { recursive: true });
yield* h.fs.writeFileString(
h.path.join(configDirectory, "hooks.json"),
'{"guard":{"PreInvocation":[{"command":"exit 1"}]}}',
);
yield* h.fs.writeFileString(
h.path.join(configDirectory, "mcp_config.json"),
'{"mcpServers":{"ai-memory":{"command":"mem"}}}',
);
yield* h.fs.writeFileString(
h.path.join(acpDirectory, "antigravity-oauth-token"),
'{"token":{"access_token":"tok"}}',
);

const textExecutable = yield* h.makeExecutable("runtime text-gen", {
sessionId: "047c62f6-607b-44db-bfbe-f83b67e9e8b1",
promptResponseText: '{"title":"Isolated title"}',
});
h.controls.selected = textExecutable;

yield* h.refresh();
const snapshot = yield* h.instance.snapshot.getSnapshot;
expect(snapshot.supportsTextGeneration).toBe(true);

const launchesBefore = h.launches.length;
const generated = yield* h.instance.textGeneration.generateThreadTitle({
cwd: h.profileDirectory,
message: "Name this thread",
modelSelection: { instanceId: h.instance.instanceId, model: "gemini-test-low" },
});
expect(generated).toEqual({ title: "Isolated title" });
const textLaunch = h.launches
.slice(launchesBefore)
.find((launch) => launch.command === textExecutable.executablePath);
expect(textLaunch).toBeDefined();
expect(textLaunch?.profileDirectory).toBeDefined();
expect(textLaunch?.profileDirectory).not.toBe(h.profileDirectory);
expect(yield* h.fs.exists(textLaunch!.profileDirectory!)).toBe(false);
yield* h.assertClosed;
}),
);

it.effect.skipIf(windowsHost)(
"removes runtime temp directories left by a previous server on create",
() =>
Expand Down
42 changes: 30 additions & 12 deletions apps/server/src/provider/Drivers/AntigravityDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ import { HttpClient } from "effect/http";
import * as AcpErrors from "effect-acp/errors";

import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
import {
isAntigravityTextGenerationAvailable,
makeAntigravityTextGeneration,
} from "../../textGeneration/AntigravityTextGeneration.ts";
import { makeAntigravityTextGeneration } from "../../textGeneration/AntigravityTextGeneration.ts";
import { makeAntigravityAuth, type AntigravityAuth } from "../AntigravityAuth.ts";
import * as AntigravityInstallation from "../AntigravityInstallation.ts";
import { brokenRuntimeError } from "../providerCompatibility.ts";
Expand Down Expand Up @@ -227,6 +224,7 @@ export const AntigravityDriver: ProviderDriver<
const prepareSpawn = Effect.fn("AntigravityDriver.prepareSpawn")(function* (
cwd: string,
baseEnv: NodeJS.ProcessEnv,
options?: { readonly isolatedTextProfile?: boolean },
): Effect.fn.Return<
{
readonly executable: AntigravityInstallation.AntigravityExecutable;
Expand All @@ -253,12 +251,32 @@ export const AntigravityDriver: ProviderDriver<
}),
),
);
const targetProfileDirectory =
options?.isolatedTextProfile === true
? yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-ag-text-" }).pipe(
Effect.mapError(
(cause) =>
new ProviderSetupError({
instanceId,
operation: "start",
detail: "Could not create an isolated Antigravity text profile directory.",
cause,
}),
),
)
: profileDirectory;
const profile = yield* prepareAntigravityProfile({
profileDirectory,
profileDirectory: targetProfileDirectory,
baseEnv: processEnvironment,
auth,
userHome,
tempDirectory: directories.runtimeTemp,
...(options?.isolatedTextProfile === true
? {
isolateUserConfig: true,
copyAuthFromProfileDirectory: profileDirectory,
}
: {}),
}).pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
Expand Down Expand Up @@ -312,20 +330,21 @@ export const AntigravityDriver: ProviderDriver<
});

const makeRuntime = Effect.fn("AntigravityDriver.makeRuntime")(function* (
input: AntigravityAdapterV2RuntimeInput,
input: AntigravityAdapterV2RuntimeInput & { readonly isolatedTextProfile?: boolean },
): Effect.fn.Return<
AcpSessionRuntime.AcpSessionRuntime["Service"],
AcpErrors.AcpError | ProviderSetupError,
Scope.Scope
> {
// `buildAntigravityAcpSpawnInput` sets `extendEnv: false`, so thread variables only reach the
// agent's shell through `baseEnv`.
const { processEnvironment: threadEnvironment, ...runtimeInput } = input;
const { processEnvironment: threadEnvironment, isolatedTextProfile, ...runtimeInput } = input;
const { executable, spawn } = yield* prepareSpawn(
runtimeInput.cwd,
threadEnvironment === undefined
? processEnvironment
: { ...processEnvironment, ...threadEnvironment },
isolatedTextProfile === true ? { isolatedTextProfile: true } : undefined,
);
const runtime = yield* makeAntigravityAcpRuntime({
...runtimeInput,
Expand Down Expand Up @@ -602,11 +621,8 @@ export const AntigravityDriver: ProviderDriver<
Effect.provideService(Path.Path, path),
Effect.catch(() => Effect.logWarning("Could not save Antigravity permission modes.")),
),
supportsTextGeneration: isAntigravityTextGenerationAvailable(profileDirectory).pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
Effect.orElseSucceed(() => false),
),
// Text generation always runs in an isolated ephemeral GEMINI_HOME without hooks or MCP.
supportsTextGeneration: Effect.succeed(true),
}).pipe(
Effect.mapError(
(cause) =>
Expand Down Expand Up @@ -651,12 +667,14 @@ export const AntigravityDriver: ProviderDriver<
});
const textGeneration = yield* makeAntigravityTextGeneration({
profileDirectory,
isolatedProfile: true,
defaultModel,
withProcess: authFlow.withProcess,
makeRuntime: (cwd) =>
makeRuntime({
cwd,
mcpServers: [],
isolatedTextProfile: true,
}),
});

Expand Down
136 changes: 136 additions & 0 deletions apps/server/src/provider/antigravityAuthSupport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,142 @@ it.layer(NodeServices.layer)("Antigravity profile preparation", (it) => {
}),
);

it.effect(
"prepares an isolated profile with empty config sentinels and copied auth files only",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped();
const userHome = path.join(root, "home");
const sourceProfile = path.join(root, "source-profile");
const isolatedProfile = path.join(root, "isolated-profile");
yield* fs.makeDirectory(path.join(userHome, ".gemini", "config", "skills", "s1"), {
recursive: true,
});
yield* fs.writeFileString(path.join(userHome, ".gemini", "AGENTS.md"), "global rule");
yield* fs.makeDirectory(path.join(sourceProfile, "antigravity-acp"), { recursive: true });
yield* fs.makeDirectory(path.join(sourceProfile, "config"), { recursive: true });
yield* fs.writeFileString(
path.join(sourceProfile, "antigravity-acp", "acp_token.json"),
'{"refresh_token":"legacy"}',
);
yield* fs.writeFileString(
path.join(sourceProfile, "antigravity-acp", "antigravity-oauth-token"),
'{"token":{"access_token":"tok"}}',
);
yield* fs.writeFileString(
path.join(sourceProfile, "antigravity-acp", "installation_id"),
"install-1",
);
yield* fs.writeFileString(
path.join(sourceProfile, "config", "hooks.json"),
'{"guard":{"PreInvocation":[{"command":"echo hi"}]}}',
);
yield* fs.writeFileString(
path.join(sourceProfile, "config", "mcp_config.json"),
'{"mcpServers":{"ai-memory":{"command":"mem"}}}',
);

const helper = preflightHelper(Effect.void);
yield* prepareAntigravityProfile({
profileDirectory: isolatedProfile,
userHome,
runtimeExecutablePath: "/fake/isolated-node",
platform: "linux",
baseEnv: {},
isolateUserConfig: true,
copyAuthFromProfileDirectory: sourceProfile,
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, helper.spawner));

expect(
yield* fs.exists(path.join(isolatedProfile, "antigravity-acp", "acp_token.json")),
).toBe(false);
expect(
yield* fs.readFileString(
path.join(isolatedProfile, "antigravity-acp", "antigravity-oauth-token"),
),
).toBe('{"token":{"access_token":"tok"}}');
expect(
yield* fs.readFileString(
path.join(isolatedProfile, "antigravity-acp", "installation_id"),
),
).toBe("install-1");
expect(yield* fs.exists(path.join(isolatedProfile, "config", ".migrated"))).toBe(true);
expect(yield* fs.readFileString(path.join(isolatedProfile, "config", "hooks.json"))).toBe(
"{}\n",
);
expect(
yield* fs.readFileString(path.join(isolatedProfile, "config", "mcp_config.json")),
).toBe('{"mcpServers":{}}\n');
expect(yield* fs.readFileString(path.join(isolatedProfile, "config", "config.json"))).toBe(
"{}\n",
);
expect(yield* fs.exists(path.join(isolatedProfile, "AGENTS.md"))).toBe(false);
expect(yield* fs.exists(path.join(isolatedProfile, "config", "skills"))).toBe(false);

// Oversized preferred token falls back to the legacy token.
const sourceOauthToken = path.join(
sourceProfile,
"antigravity-acp",
"antigravity-oauth-token",
);
yield* fs.writeFileString(sourceOauthToken, "x".repeat(64 * 1024 + 1));
const oversizedFallbackProfile = path.join(root, "isolated-profile-oversized-fallback");
yield* prepareAntigravityProfile({
profileDirectory: oversizedFallbackProfile,
userHome,
runtimeExecutablePath: "/fake/isolated-node",
platform: "linux",
baseEnv: {},
isolateUserConfig: true,
copyAuthFromProfileDirectory: sourceProfile,
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, helper.spawner));
expect(
yield* fs.exists(
path.join(oversizedFallbackProfile, "antigravity-acp", "antigravity-oauth-token"),
),
).toBe(false);
expect(
yield* fs.readFileString(
path.join(oversizedFallbackProfile, "antigravity-acp", "acp_token.json"),
),
).toBe('{"refresh_token":"legacy"}');
expect(
yield* fs.readFileString(
path.join(oversizedFallbackProfile, "antigravity-acp", "installation_id"),
),
).toBe("install-1");

if (symlinksSupported) {
const externalSecret = path.join(root, "external-secret");
yield* fs.writeFileString(externalSecret, '{"token":{"access_token":"outside"}}');
yield* fs.remove(sourceOauthToken);
yield* fs.symlink(externalSecret, sourceOauthToken);
const symlinkFallbackProfile = path.join(root, "isolated-profile-symlink-fallback");
yield* prepareAntigravityProfile({
profileDirectory: symlinkFallbackProfile,
userHome,
runtimeExecutablePath: "/fake/isolated-node",
platform: "linux",
baseEnv: {},
isolateUserConfig: true,
copyAuthFromProfileDirectory: sourceProfile,
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, helper.spawner));
expect(
yield* fs.exists(
path.join(symlinkFallbackProfile, "antigravity-acp", "antigravity-oauth-token"),
),
).toBe(false);
expect(
yield* fs.readFileString(
path.join(symlinkFallbackProfile, "antigravity-acp", "acp_token.json"),
),
).toBe('{"refresh_token":"legacy"}');
}
}),
);

it.effect("rejects Python BROWSER delimiter collisions before starting a helper", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
Expand Down
Loading
Loading