import { ProviderSetupError, type ProviderAuthState, type ProviderInstanceId, } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; import * as SubscriptionRef from "effect/SubscriptionRef"; import * as AcpErrors from "effect-acp/errors"; import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; import { antigravityLoginResponse, parseAntigravityAuthorizationUrl, type AntigravityAuthorizationUrl, } from "./antigravityAuthSupport.ts"; import { forwardAntigravityCallback, validateAntigravityCallbackUrl, } from "./antigravityCallback.ts"; import type { ProviderAuthController } from "./ProviderAuthService.ts"; const AUTH_TIMEOUT_MS = 300_000; // Once initialized, a healthy agent shows the Google link within seconds; its bundled Python has no // IPv4 fallback, so broken IPv6 stalls each Google request. Startup is timed separately: the // one-file runtime unpacks on every launch (about a minute on Windows) before it answers initialize. const NO_RESPONSE_HINT_MS = 30_000; const STARTUP_HINT_MS = 90_000; const NOT_STARTED_MESSAGE = "Antigravity has not started after 90 seconds. Its runtime may be stuck starting on this machine."; const NOT_RESPONDING_MESSAGE = "Antigravity did not respond. Its runtime may not run on this machine."; const NO_RESPONSE_MESSAGE = "Antigravity has not reached Google after 30 seconds. Check this machine's network connection. Broken IPv6 is a common cause."; const UNREACHABLE_MESSAGE = "Antigravity could not reach Google. Check this machine's network connection, then start sign-in again."; const FORWARDING_FAILED_MESSAGE = "Could not deliver the sign-in response. Start sign-in again."; const LOGIN_CODE_FIELD = "code"; const isSetupError = Schema.is(ProviderSetupError); const isAcpRequestError = Schema.is(AcpErrors.AcpRequestError); const isAcpProcessExitedError = Schema.is(AcpErrors.AcpProcessExitedError); interface AuthSnapshot { readonly ownerSessionId: string | null; readonly state: ProviderAuthState; readonly codeLogin?: boolean; } interface AuthFlow { readonly id: string; readonly ownerSessionId: string; readonly expiresAtMillis: number; state: ProviderAuthState; pending: AntigravityAuthorizationUrl | undefined; initialized: boolean; callbackSent: boolean; codeLogin: boolean; submitCode: ((response: string) => Effect.Effect) | undefined; fiber: Fiber.Fiber | undefined; forwarding: Fiber.Fiber | undefined; } interface OwnedProcess { readonly stop: Effect.Effect; startup: Fiber.Fiber | undefined; } export interface AntigravityAuth { readonly controller: ProviderAuthController; readonly withProcess: ( stop: Effect.Effect, task: Effect.Effect, ) => Effect.Effect; /** A session started outside a flow, so an earlier flow's failure no longer describes sign-in. */ readonly onSignedInElsewhere: Effect.Effect; } /** `agy acp --login`: it prints a Google link and reads the code the Antigravity page shows. */ export interface AntigravityCodeLogin { readonly submit: (response: string) => Effect.Effect; /** Succeeds once the runtime has stored the sign-in. */ readonly completed: Effect.Effect; } export type AntigravityAuthRuntime = Pick< AcpSessionRuntime.AcpSessionRuntime["Service"], "initialize" | "start" | "request" >; export interface AntigravityAuthOptions< Runtime extends AntigravityAuthRuntime = AcpSessionRuntime.AcpSessionRuntime["Service"], > { readonly instanceId: ProviderInstanceId; readonly makeRuntime: (input: { readonly onAuthorizationUrl?: (url: string) => Effect.Effect; }) => Effect.Effect; readonly onAuthenticated: ( result: AcpSessionRuntime.AcpSessionRuntimeStartResult, runtime: Runtime, ) => Effect.Effect; readonly onSignedOut: Effect.Effect; readonly forwardCallback?: (callback: URL) => Effect.Effect; readonly usesBrowser?: boolean; /** Undefined when the runtime signs in through ACP `authenticate` and T3's browser helper. */ readonly codeLogin?: (input: { readonly onAuthorizationUrl: (url: string) => Effect.Effect; }) => Effect.Effect< AntigravityCodeLogin | undefined, AcpErrors.AcpError | ProviderSetupError, Scope.Scope >; } function visibleSnapshot(snapshot: AuthSnapshot, ownerSessionId: string): ProviderAuthState { if (snapshot.ownerSessionId === null || snapshot.ownerSessionId === ownerSessionId) { return snapshot.state; } const busy = ["starting", "waiting", "verifying"].includes(snapshot.state.phase); return { ...snapshot.state, flowId: null, authorizationUrl: null, interaction: null, expiresAt: null, ...(busy ? { message: "Sign-in is in progress in another client." } : {}), }; } function safeAuthFailure(cause: Cause.Cause, usesBrowser: boolean): string { const error = Cause.findErrorOption(cause); if (Option.isSome(error)) { if (isSetupError(error.value)) { return error.value.detail; } if (isAcpProcessExitedError(error.value)) { const { code, signal } = error.value; const reason = signal !== undefined ? ` after signal ${signal}` : code !== undefined ? ` with code ${code}` : ""; return `Antigravity exited unexpectedly${reason}. Start sign-in again.`; } if (isAcpRequestError(error.value)) { if (error.value.errorMessage.includes("SUBSCRIPTION_REQUIRED")) { return "Google requires an eligible Antigravity subscription for this account."; } if (/access_denied|denied access|cancelled/i.test(error.value.errorMessage)) { return "Google sign-in was not approved. Start sign-in again."; } if (error.value.method === "session/new" && error.value.code === -32603) { return "Antigravity authenticated, but could not initialize a session or load models."; } if (!usesBrowser && error.value.code === -32602) { return "Antigravity rejected the configured credentials. Check the provider settings."; } } } return usesBrowser ? "Google sign-in failed. Start sign-in again." : "Antigravity could not authenticate with the configured credentials."; } export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* < Runtime extends AntigravityAuthRuntime, >( options: AntigravityAuthOptions, ): Effect.fn.Return { const random = yield* SecureRandom.SecureRandom; const instanceScope = yield* Scope.Scope; const usesBrowser = options.usesBrowser ?? true; const lock = yield* Semaphore.make(1); const closed = yield* Deferred.make(); const emptyState: ProviderAuthState = { instanceId: options.instanceId, phase: "idle", flowId: null, authorizationUrl: null, expiresAt: null, message: null, }; const snapshot = yield* SubscriptionRef.make({ ownerSessionId: null, state: emptyState, }); const processes = new Set(); let activeFlow: AuthFlow | undefined; let operation: "idle" | "auth" | "logout" | "cancel" | "closed" = "idle"; const setupError = (name: string, detail: string) => new ProviderSetupError({ instanceId: options.instanceId, operation: name, detail }); const currentState = (ownerSessionId: string) => SubscriptionRef.get(snapshot).pipe( Effect.map((value) => visibleSnapshot(value, ownerSessionId)), ); const publishFlow = (flow: AuthFlow, state: ProviderAuthState) => { flow.state = state; return SubscriptionRef.set(snapshot, { ownerSessionId: flow.ownerSessionId, state, codeLogin: flow.codeLogin, }); }; const stopOwnedProcesses = Effect.suspend(() => Effect.forEach( Array.from(processes), (owned) => Effect.gen(function* () { if (owned.startup) { yield* Fiber.interrupt(owned.startup); } yield* owned.stop; }), { discard: true, concurrency: "unbounded" }, ), ); const withProcess: AntigravityAuth["withProcess"] = (stop, task) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () { const scope = yield* Scope.Scope; const owned: OwnedProcess = { stop, startup: undefined }; const fiber = yield* lock.withPermits(1)( Effect.gen(function* () { if (operation !== "idle") { return yield* setupError( "startProcess", "Antigravity sign-in or sign-out is in progress. Try again after it finishes.", ); } processes.add(owned); yield* Scope.addFinalizer( scope, Effect.sync(() => { processes.delete(owned); }), ); const child = yield* restore(task).pipe(Effect.forkIn(scope)); owned.startup = child; return child; }), ); // Propagate interruption after the exit wait so concurrent stop waiters stay attached. return yield* restore(Fiber.await(fiber)).pipe( Effect.flatten, Effect.ensuring(Fiber.interrupt(fiber)), Effect.ensuring( Effect.sync(() => { owned.startup = undefined; }), ), ); }), ); const finishFlow = (flow: AuthFlow, result: Exit.Exit) => lock.withPermits(1)( Effect.gen(function* () { if (activeFlow !== flow) return; activeFlow = undefined; operation = "idle"; flow.pending = undefined; yield* publishFlow(flow, { ...flow.state, phase: Exit.isSuccess(result) ? "succeeded" : "failed", authorizationUrl: null, expiresAt: null, message: Exit.isSuccess(result) ? usesBrowser ? "Signed in with Google." : "Connected to Antigravity." : safeAuthFailure(result.cause, usesBrowser), }); }), ); const receiveAuthorizationUrl = (flow: AuthFlow, url: string, redirect: "loopback" | "code") => parseAntigravityAuthorizationUrl(url, redirect).pipe( Effect.flatMap((authorization) => lock.withPermits(1)( Effect.gen(function* () { if (activeFlow !== flow || operation !== "auth") return; if (flow.pending) { if (flow.pending.authorizationUrl === authorization.authorizationUrl) return; return yield* new AcpErrors.AcpTransportError({ detail: "Antigravity started more than one Google sign-in request.", cause: undefined, }); } flow.pending = authorization; flow.codeLogin = redirect === "code"; yield* publishFlow(flow, { ...flow.state, phase: "waiting", authorizationUrl: authorization.authorizationUrl, message: redirect === "loopback" ? "Open the Google sign-in link. If you are remote, paste the redirect URL here." : "Open the Google sign-in link, then paste the code from the Antigravity page here.", }); }), ), ), ); const publishStartingHint = (flow: AuthFlow, delayMs: number, message: string) => Effect.sleep(delayMs).pipe( Effect.andThen( lock.withPermits(1)( Effect.gen(function* () { if (activeFlow !== flow || flow.state.phase !== "starting") return; yield* publishFlow(flow, { ...flow.state, message }); }), ), ), Effect.forkScoped, ); const runSignIn = Effect.fn("AntigravityAuth.runSignIn")( function* (flow: AuthFlow, stopSessions: Effect.Effect) { yield* stopSessions.pipe(Effect.ensuring(stopOwnedProcesses)); const signedIn = yield* Effect.scoped( Effect.gen(function* () { const login = yield* ( options.codeLogin?.({ onAuthorizationUrl: (url) => receiveAuthorizationUrl(flow, url, "code"), }) ?? Effect.undefined ); if (login === undefined) return false; flow.submitCode = login.submit; yield* login.completed; return true; }), ); const runtime = yield* options.makeRuntime( signedIn ? {} : { onAuthorizationUrl: (url) => receiveAuthorizationUrl(flow, url, "loopback") }, ); const startupHint = yield* publishStartingHint(flow, STARTUP_HINT_MS, NOT_STARTED_MESSAGE); // `start` reuses this memoized handshake. yield* runtime.initialize(); flow.initialized = true; yield* Fiber.interrupt(startupHint); yield* publishStartingHint(flow, NO_RESPONSE_HINT_MS, NO_RESPONSE_MESSAGE); const started = yield* runtime.start(); yield* lock.withPermits(1)( Effect.gen(function* () { if (activeFlow !== flow) return; flow.pending = undefined; yield* publishFlow(flow, { ...flow.state, phase: "verifying", authorizationUrl: null, message: "Checking Antigravity access and models.", }); }), ); yield* options.onAuthenticated(started, runtime); }, (effect, flow, _stopSessions) => effect.pipe( Effect.scoped, Effect.timeoutOrElse({ duration: AUTH_TIMEOUT_MS, orElse: () => Effect.fail( setupError( "start", flow.state.phase !== "starting" ? "Google sign-in expired. Start sign-in again." : flow.initialized ? UNREACHABLE_MESSAGE : NOT_RESPONDING_MESSAGE, ), ), }), Effect.exit, Effect.flatMap((result) => finishFlow(flow, result)), ), ); const stopFlow = (flow: AuthFlow, phase: "cancelled" | "failed", message: string) => Effect.uninterruptible( Effect.gen(function* () { const detached = yield* lock.withPermits(1)( Effect.gen(function* () { if (activeFlow !== flow) return false; activeFlow = undefined; operation = "cancel"; flow.pending = undefined; yield* publishFlow(flow, { ...flow.state, phase, authorizationUrl: null, expiresAt: null, message, }); return true; }), ); if (!detached) return; if (flow.forwarding) yield* Fiber.interrupt(flow.forwarding); if (flow.fiber) yield* Fiber.interrupt(flow.fiber); yield* lock.withPermits(1)( Effect.sync(() => { if (operation === "cancel") operation = "idle"; }), ); }), ); const requireFlow = Effect.fn("AntigravityAuth.requireFlow")(function* ( ownerSessionId: string, flowId: string, name: string, ) { const flow = activeFlow; if (!flow || flow.id !== flowId || flow.ownerSessionId !== ownerSessionId) { return yield* setupError(name, "This sign-in is no longer active in this client."); } const now = yield* Clock.currentTimeMillis; if (now >= flow.expiresAtMillis) { return yield* setupError(name, "Google sign-in expired. Start sign-in again."); } return flow; }); const controller: ProviderAuthController = { start: (ownerSessionId, stopSessions = Effect.void) => lock.withPermits(1)( Effect.uninterruptible( Effect.gen(function* () { if (activeFlow?.ownerSessionId === ownerSessionId && operation === "auth") { return activeFlow.state; } if (operation !== "idle") { return yield* setupError("start", "Antigravity setup is already in progress."); } const flowId = yield* random .uuidV4() .pipe( Effect.mapError(() => setupError("start", "Could not start Google sign-in. Try again."), ), ); const expiresAtMillis = (yield* Clock.currentTimeMillis) + AUTH_TIMEOUT_MS; const state: ProviderAuthState = { ...emptyState, phase: "starting", flowId, expiresAt: DateTime.formatIso(DateTime.makeUnsafe(expiresAtMillis)), message: usesBrowser ? "Starting Google sign-in." : "Checking credentials.", }; const flow: AuthFlow = { id: flowId, ownerSessionId, expiresAtMillis, state, pending: undefined, initialized: false, callbackSent: false, codeLogin: false, submitCode: undefined, fiber: undefined, forwarding: undefined, }; activeFlow = flow; operation = "auth"; yield* publishFlow(flow, state); flow.fiber = yield* runSignIn(flow, stopSessions).pipe( Effect.interruptible, Effect.forkIn(instanceScope), ); return state; }), ), ), complete: Effect.fn("AntigravityAuth.complete")(function* (ownerSessionId, input) { const pending = yield* lock.withPermits(1)( Effect.gen(function* () { const flow = yield* requireFlow(ownerSessionId, input.flowId, "complete"); if (!flow.pending || flow.callbackSent) { return yield* setupError( "complete", flow.callbackSent ? "The sign-in response was already sent. Wait for Google to finish." : "Wait for the Google sign-in link before you send a redirect URL.", ); } const callback = yield* validateAntigravityCallbackUrl( options.instanceId, flow.pending, input.callbackUrl, ); flow.callbackSent = true; yield* publishFlow(flow, { ...flow.state, phase: "verifying", authorizationUrl: null, message: "Waiting for Google to finish sign-in.", }); const forwarding = yield* ( options.forwardCallback?.(callback) ?? forwardAntigravityCallback(options.instanceId, callback) ).pipe( // stopFlow interrupts this fiber, so it runs from a sibling fiber. Effect.tapError(() => stopFlow(flow, "failed", FORWARDING_FAILED_MESSAGE).pipe( Effect.forkIn(instanceScope), ), ), Effect.interruptible, Effect.forkIn(instanceScope), ); flow.forwarding = forwarding; return { flow, forwarding }; }), ); const forwarded = yield* Fiber.await(pending.forwarding); if (Exit.isFailure(forwarded)) { return yield* setupError("complete", FORWARDING_FAILED_MESSAGE); } return pending.flow.state; }), respond: Effect.fn("AntigravityAuth.respond")(function* (ownerSessionId, input) { return yield* lock.withPermits(1)( Effect.gen(function* () { const flow = yield* requireFlow(ownerSessionId, input.flowId, "respond"); if (!flow.pending || !flow.submitCode || flow.callbackSent) { return yield* setupError( "respond", flow.callbackSent ? "The sign-in response was already sent. Wait for Google to finish." : "Wait for the Google sign-in link before you send a code.", ); } const response = input.interactionId === flow.id && input.response.type === "credentials" ? antigravityLoginResponse( flow.pending, input.response.values[LOGIN_CODE_FIELD] ?? "", ) : undefined; if (response === undefined) { return yield* setupError( "respond", "Paste the code from the Antigravity page, or that page's full URL.", ); } flow.callbackSent = true; yield* flow.submitCode(response); yield* publishFlow(flow, { ...flow.state, phase: "verifying", authorizationUrl: null, message: "Waiting for Google to finish sign-in.", }); return flow.state; }), ); }), cancel: Effect.fn("AntigravityAuth.cancel")(function* (ownerSessionId, flowId) { const flow = yield* lock.withPermits(1)(requireFlow(ownerSessionId, flowId, "cancel")); yield* stopFlow(flow, "cancelled", "Google sign-in was cancelled."); return flow.state; }), logout: Effect.fn("AntigravityAuth.logout")(function* (stopSessions) { const task = Effect.uninterruptibleMask((restore) => Effect.gen(function* () { const flow = yield* lock.withPermits(1)( Effect.gen(function* () { if (operation !== "idle" && operation !== "auth") { return yield* setupError("logout", "Antigravity setup is already stopping."); } operation = "logout"; const currentFlow = activeFlow; activeFlow = undefined; if (currentFlow) { currentFlow.pending = undefined; yield* publishFlow(currentFlow, { ...currentFlow.state, phase: "cancelled", authorizationUrl: null, expiresAt: null, message: "Google sign-in was cancelled by sign-out.", }); } return currentFlow; }), ); const stopRemaining = Effect.gen(function* () { if (flow?.forwarding) yield* Fiber.interrupt(flow.forwarding); if (flow?.fiber) yield* Fiber.interrupt(flow.fiber); yield* stopOwnedProcesses; }); const result = yield* restore( Effect.gen(function* () { yield* stopSessions.pipe(Effect.ensuring(stopRemaining)); const runtime = yield* options.makeRuntime({}); const initialized = yield* runtime.initialize(); if (!initialized.agentCapabilities?.auth?.logout) { return yield* setupError( "logout", "This Antigravity version does not support sign-out. Update the provider.", ); } yield* runtime.request("logout", {}); yield* options.onSignedOut; }).pipe( Effect.scoped, Effect.timeoutOrElse({ duration: "90 seconds", orElse: () => Effect.fail(setupError("logout", "Antigravity sign-out timed out.")), }), ), ).pipe(Effect.exit); yield* lock.withPermits(1)( Effect.gen(function* () { operation = "idle"; yield* SubscriptionRef.set(snapshot, { ownerSessionId: null, state: { ...emptyState, phase: Exit.isSuccess(result) ? "idle" : "failed", message: Exit.isSuccess(result) ? "Signed out of Google." : "Antigravity sign-out failed. Try again.", }, }); }), ); if (Exit.isFailure(result)) { const failure = Cause.findErrorOption(result.cause); return yield* Option.isSome(failure) && isSetupError(failure.value) ? failure.value : setupError("logout", "Antigravity sign-out failed. Try again."); } return yield* currentState(""); }), ); const worker = yield* task.pipe(Effect.forkIn(instanceScope)); return yield* Fiber.await(worker).pipe(Effect.flatten); }), subscribe: (ownerSessionId) => SubscriptionRef.changes(snapshot).pipe( Stream.map((value) => { const state = visibleSnapshot(value, ownerSessionId); return { ...state, credentialOwner: "provider" as const, interaction: state.phase === "waiting" && state.authorizationUrl && state.flowId ? value.codeLogin ? { type: "credentials" as const, id: state.flowId, fields: [ { name: LOGIN_CODE_FIELD, label: "Code from the Antigravity page", secret: false, }, ], } : { type: "browser" as const, id: state.flowId, url: state.authorizationUrl, requiresConsent: false, acceptsCallback: true, } : null, }; }), Stream.interruptWhen(Deferred.await(closed)), ), isLogoutPrompt: (text, hasAttachments) => !hasAttachments && text.trim() === "/logout", }; yield* Effect.addFinalizer(() => Effect.gen(function* () { operation = "closed"; const flow = activeFlow; activeFlow = undefined; if (flow) { flow.pending = undefined; if (flow.forwarding) yield* Fiber.interrupt(flow.forwarding); if (flow.fiber) yield* Fiber.interrupt(flow.fiber); } yield* stopOwnedProcesses; yield* Deferred.succeed(closed, undefined); }), ); const onSignedInElsewhere = SubscriptionRef.update(snapshot, (value) => activeFlow === undefined && operation === "idle" && (value.state.phase === "failed" || value.state.phase === "cancelled") ? { ownerSessionId: null, state: emptyState } : value, ); return { controller, withProcess, onSignedInElsewhere }; });