Skip to content

Commit 9594098

Browse files
fix(server): Antigravity Change account opens Google sign-in again
The agent signs in on its own whenever acp_token.json holds a valid token, so Sign in and Change account never reached Google and could not switch accounts. Browser sign-in now moves the saved token aside for the sign-in's scope. Success keeps the new token; failure, cancel, or timeout puts the old one back unless the agent already saved a new one. Driver start restores a token left aside by a sign-in that never finished. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 16918b5 commit 9594098

6 files changed

Lines changed: 212 additions & 2 deletions

File tree

‎apps/server/src/provider/AntigravityAuth.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const makeHarness = Effect.fn("makeAuthTestHarness")(function* (
5858
readonly supportsLogout?: boolean;
5959
readonly beforeInitialize?: Effect.Effect<void>;
6060
readonly forwardCallback?: Effect.Effect<void, ProviderSetupError>;
61+
readonly usesBrowser?: boolean;
62+
readonly setsAsideCredentials?: boolean;
6163
} = {},
6264
) {
6365
const authenticated = yield* Deferred.make<void, AcpErrors.AcpError>();
@@ -124,6 +126,20 @@ const makeHarness = Effect.fn("makeAuthTestHarness")(function* (
124126
Effect.sync(() => {
125127
forwarded += 1;
126128
}),
129+
...(options.usesBrowser === undefined ? {} : { usesBrowser: options.usesBrowser }),
130+
...(options.setsAsideCredentials
131+
? {
132+
setAsideCredentials: Effect.acquireRelease(
133+
Effect.sync(() => {
134+
events.push("credentials-set-aside");
135+
}),
136+
(_, exit) =>
137+
Effect.sync(() => {
138+
events.push(Exit.isSuccess(exit) ? "credentials-dropped" : "credentials-restored");
139+
}),
140+
),
141+
}
142+
: {}),
127143
});
128144
return {
129145
auth,
@@ -282,6 +298,44 @@ it.layer(NodeServices.layer)("AntigravityAuth", (it) => {
282298
}),
283299
);
284300

301+
it.effect("sets saved Google credentials aside so the agent asks Google again", () =>
302+
Effect.gen(function* () {
303+
const harness = yield* makeHarness({ setsAsideCredentials: true });
304+
yield* harness.auth.controller.start(owner);
305+
yield* phase(harness.auth, "waiting");
306+
assert.deepEqual(harness.events.slice(0, 2), ["credentials-set-aside", "process-open"]);
307+
yield* Deferred.succeed(harness.authenticated, undefined);
308+
yield* Deferred.succeed(harness.discovered, undefined);
309+
yield* phase(harness.auth, "succeeded");
310+
assert.deepEqual(harness.events.slice(-2), ["process-close", "credentials-dropped"]);
311+
}),
312+
);
313+
314+
it.effect("restores saved Google credentials after the process when sign-in is cancelled", () =>
315+
Effect.gen(function* () {
316+
const harness = yield* makeHarness({ setsAsideCredentials: true });
317+
const state = yield* harness.auth.controller.start(owner);
318+
yield* phase(harness.auth, "waiting");
319+
yield* harness.auth.controller.cancel(owner, state.flowId!);
320+
assert.deepEqual(harness.events.slice(-2), ["process-close", "credentials-restored"]);
321+
}),
322+
);
323+
324+
it.effect("leaves configured credentials alone when sign-in does not use a browser", () =>
325+
Effect.gen(function* () {
326+
const harness = yield* makeHarness({
327+
interactive: false,
328+
usesBrowser: false,
329+
setsAsideCredentials: true,
330+
});
331+
yield* harness.auth.controller.start(owner);
332+
yield* Deferred.succeed(harness.authenticated, undefined);
333+
yield* Deferred.succeed(harness.discovered, undefined);
334+
yield* phase(harness.auth, "succeeded");
335+
assert.notInclude(harness.events, "credentials-set-aside");
336+
}),
337+
);
338+
285339
it.effect("rejects mismatched callbacks without sending any HTTP request", () =>
286340
Effect.gen(function* () {
287341
const harness = yield* makeHarness();

‎apps/server/src/provider/AntigravityAuth.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export interface AntigravityAuthOptions<
8383
readonly onSignedOut: Effect.Effect<void>;
8484
readonly forwardCallback?: (callback: URL) => Effect.Effect<void, ProviderSetupError>;
8585
readonly usesBrowser?: boolean;
86+
/**
87+
* Hides the saved Google credentials for the sign-in's scope. The agent signs in silently
88+
* whenever they are valid, so without this "Change account" never reaches Google.
89+
*/
90+
readonly setAsideCredentials?: Effect.Effect<void, ProviderSetupError, Scope.Scope>;
8691
}
8792

8893
function visibleSnapshot(snapshot: AuthSnapshot, ownerSessionId: string): ProviderAuthState {
@@ -264,6 +269,7 @@ export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* <
264269
const runSignIn = (flow: AuthFlow, stopSessions: Effect.Effect<void, ProviderSetupError>) =>
265270
Effect.gen(function* () {
266271
yield* stopSessions.pipe(Effect.ensuring(stopOwnedProcesses));
272+
if (usesBrowser && options.setAsideCredentials) yield* options.setAsideCredentials;
267273
const runtime = yield* options.makeRuntime({
268274
onAuthorizationUrl: (url) => receiveAuthorizationUrl(flow, url),
269275
});

‎apps/server/src/provider/Drivers/AntigravityDriver.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
isAntigravitySignInRequiredError,
3737
prepareAntigravityProfile,
3838
resolveAntigravityInstanceDirectories,
39+
restoreAntigravityCredentials,
40+
setAsideAntigravityCredentials,
3941
type AntigravityAuthConfig,
4042
} from "../antigravityAuthSupport.ts";
4143
import {
@@ -147,6 +149,16 @@ export const AntigravityDriver: ProviderDriver<
147149
Effect.provideService(FileSystem.FileSystem, fileSystem),
148150
);
149151
}
152+
// A sign-in cut short by a crash leaves the previous token set aside.
153+
yield* restoreAntigravityCredentials(profileDirectory).pipe(
154+
Effect.provideService(FileSystem.FileSystem, fileSystem),
155+
Effect.provideService(Path.Path, path),
156+
Effect.catch((error) =>
157+
Effect.logWarning("Could not restore Antigravity credentials set aside for sign-in.", {
158+
error,
159+
}),
160+
),
161+
);
150162
const continuationIdentity = defaultProviderContinuationIdentity({
151163
driverKind: DRIVER,
152164
instanceId,
@@ -336,6 +348,19 @@ export const AntigravityDriver: ProviderDriver<
336348
onAuthenticated: publishCatalog,
337349
onSignedOut: Effect.suspend(() => provider.onSignedOut),
338350
usesBrowser: antigravityAuthUsesBrowser(auth.authMethod),
351+
setAsideCredentials: setAsideAntigravityCredentials(profileDirectory).pipe(
352+
Effect.provideService(FileSystem.FileSystem, fileSystem),
353+
Effect.provideService(Path.Path, path),
354+
Effect.mapError(
355+
(cause) =>
356+
new ProviderSetupError({
357+
instanceId,
358+
operation: "start",
359+
detail: "Could not clear the saved Google sign-in. Try again.",
360+
cause,
361+
}),
362+
),
363+
),
339364
});
340365

341366
// The probe must not spawn. The agent is a PyInstaller one-file bundle

‎apps/server/src/provider/antigravityAuthSupport.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
parseAntigravityAuthorizationUrl,
3737
prepareAntigravityProfile,
3838
resolveAntigravityInstanceDirectories,
39+
restoreAntigravityCredentials,
40+
setAsideAntigravityCredentials,
3941
} from "./antigravityAuthSupport.ts";
4042

4143
const authorizationUrl =
@@ -756,3 +758,73 @@ it.layer(NodeServices.layer)("Antigravity profile preparation", (it) => {
756758
}),
757759
);
758760
});
761+
762+
it.layer(NodeServices.layer)("Antigravity credentials during sign-in", (it) => {
763+
const makeProfile = Effect.gen(function* () {
764+
const fs = yield* FileSystem.FileSystem;
765+
const path = yield* Path.Path;
766+
const profileDirectory = yield* fs.makeTempDirectoryScoped();
767+
const acpDirectory = path.join(profileDirectory, "antigravity-acp");
768+
yield* fs.makeDirectory(acpDirectory, { recursive: true });
769+
const tokenPath = path.join(acpDirectory, "acp_token.json");
770+
const readToken = fs
771+
.exists(tokenPath)
772+
.pipe(
773+
Effect.flatMap((exists) => (exists ? fs.readFileString(tokenPath) : Effect.succeed(null))),
774+
);
775+
const remainingFiles = fs.readDirectory(acpDirectory).pipe(Effect.map((files) => files.sort()));
776+
return { fs, profileDirectory, tokenPath, readToken, remainingFiles };
777+
});
778+
779+
it.effect("hides the saved token while sign-in runs and keeps the new one", () =>
780+
Effect.gen(function* () {
781+
const profile = yield* makeProfile;
782+
yield* profile.fs.writeFileString(profile.tokenPath, "previous-account");
783+
yield* Effect.gen(function* () {
784+
yield* setAsideAntigravityCredentials(profile.profileDirectory);
785+
expect(yield* profile.readToken).toBeNull();
786+
yield* profile.fs.writeFileString(profile.tokenPath, "chosen-account");
787+
}).pipe(Effect.scoped);
788+
expect(yield* profile.readToken).toBe("chosen-account");
789+
expect(yield* profile.remainingFiles).toEqual(["acp_token.json"]);
790+
}),
791+
);
792+
793+
it.effect("restores the saved token when sign-in fails before Google saves a new one", () =>
794+
Effect.gen(function* () {
795+
const profile = yield* makeProfile;
796+
yield* profile.fs.writeFileString(profile.tokenPath, "previous-account");
797+
const result = yield* Effect.gen(function* () {
798+
yield* setAsideAntigravityCredentials(profile.profileDirectory);
799+
return yield* Effect.fail("cancelled");
800+
}).pipe(Effect.scoped, Effect.result);
801+
expect(Result.isFailure(result)).toBe(true);
802+
expect(yield* profile.readToken).toBe("previous-account");
803+
expect(yield* profile.remainingFiles).toEqual(["acp_token.json"]);
804+
}),
805+
);
806+
807+
it.effect("keeps the token Google saved even when a later step fails", () =>
808+
Effect.gen(function* () {
809+
const profile = yield* makeProfile;
810+
yield* profile.fs.writeFileString(profile.tokenPath, "previous-account");
811+
yield* Effect.gen(function* () {
812+
yield* setAsideAntigravityCredentials(profile.profileDirectory);
813+
yield* profile.fs.writeFileString(profile.tokenPath, "chosen-account");
814+
return yield* Effect.fail("model discovery failed");
815+
}).pipe(Effect.scoped, Effect.result);
816+
expect(yield* profile.readToken).toBe("chosen-account");
817+
expect(yield* profile.remainingFiles).toEqual(["acp_token.json"]);
818+
}),
819+
);
820+
821+
it.effect("restores a token left aside when the app stopped during sign-in", () =>
822+
Effect.gen(function* () {
823+
const profile = yield* makeProfile;
824+
yield* profile.fs.writeFileString(`${profile.tokenPath}.before-sign-in`, "previous-account");
825+
yield* restoreAntigravityCredentials(profile.profileDirectory);
826+
expect(yield* profile.readToken).toBe("previous-account");
827+
expect(yield* profile.remainingFiles).toEqual(["acp_token.json"]);
828+
}),
829+
);
830+
});

‎apps/server/src/provider/antigravityAuthSupport.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
66
import { resolveNodeExecutable, nodeRuntimeUnavailableMessage } from "@t3tools/shared/nodeRuntime";
77
import * as Crypto from "effect/Crypto";
88
import * as Effect from "effect/Effect";
9+
import * as Exit from "effect/Exit";
910
import * as Hex from "effect/encoding/Hex";
1011
import * as FileSystem from "effect/FileSystem";
1112
import * as Path from "effect/Path";
@@ -404,6 +405,58 @@ export const prepareAntigravityProfile = Effect.fn("prepareAntigravityProfile")(
404405
return profile;
405406
});
406407

408+
function antigravityCredentialPaths(path: Path.Path, profileDirectory: string) {
409+
const tokenPath = path.join(profileDirectory, "antigravity-acp", "acp_token.json");
410+
return { tokenPath, setAsidePath: `${tokenPath}.before-sign-in` };
411+
}
412+
413+
/** Puts back credentials a sign-in set aside, unless that sign-in already saved new ones. */
414+
export const restoreAntigravityCredentials = Effect.fn("restoreAntigravityCredentials")(function* (
415+
profileDirectory: string,
416+
) {
417+
const fs = yield* FileSystem.FileSystem;
418+
const { tokenPath, setAsidePath } = antigravityCredentialPaths(
419+
yield* Path.Path,
420+
profileDirectory,
421+
);
422+
if (!(yield* fs.exists(setAsidePath))) return;
423+
if (yield* fs.exists(tokenPath)) {
424+
yield* fs.remove(setAsidePath, { force: true });
425+
} else {
426+
yield* fs.rename(setAsidePath, tokenPath);
427+
}
428+
});
429+
430+
/**
431+
* Moves the saved token aside for the scope so the agent runs Google's browser sign-in instead of
432+
* reusing it. A sign-in that fails or is cancelled gets the previous token back.
433+
*/
434+
export const setAsideAntigravityCredentials = Effect.fn("setAsideAntigravityCredentials")(
435+
function* (profileDirectory: string) {
436+
const fs = yield* FileSystem.FileSystem;
437+
const { tokenPath, setAsidePath } = antigravityCredentialPaths(
438+
yield* Path.Path,
439+
profileDirectory,
440+
);
441+
yield* Effect.acquireRelease(
442+
Effect.gen(function* () {
443+
if (yield* fs.exists(tokenPath)) yield* fs.rename(tokenPath, setAsidePath);
444+
}),
445+
(_, exit) =>
446+
(Exit.isSuccess(exit)
447+
? fs.remove(setAsidePath, { force: true })
448+
: restoreAntigravityCredentials(profileDirectory)
449+
).pipe(
450+
Effect.catch((error) =>
451+
Effect.logWarning("Antigravity credentials set aside for sign-in were not settled.", {
452+
error,
453+
}),
454+
),
455+
),
456+
);
457+
},
458+
);
459+
407460
export function buildAntigravityAcpSpawnInput(input: {
408461
readonly installation: {
409462
readonly executablePath: string;

‎docs/user/providers-antigravity.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Choose **Sign-in method** in the Antigravity provider settings:
4646
The environment stores the Antigravity API key outside its settings file, and
4747
clients only see whether one is saved. The agent uses the method and credentials selected for this instance;
4848
ambient `GEMINI_API_KEY` and Google credential variables do not override them.
49-
Changing the method stops the instance's sessions. Sign out before replacing an
50-
account.
49+
Changing the method stops the instance's sessions. To switch Google accounts,
50+
choose **Change account**. If you cancel, T3 Code keeps the current account.
5151

5252
## Runtime installation
5353

0 commit comments

Comments
 (0)