diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.test.ts b/apps/server/src/provider/Drivers/AntigravityDriver.test.ts index 2ee99a6fbbfe3..6a54da635bde9 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.test.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.test.ts @@ -14,8 +14,10 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as Queue from "effect/Queue"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; +import * as TestClock from "effect/testing/TestClock"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import { HttpClient } from "effect/http"; @@ -63,6 +65,7 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* ( readonly enabled?: boolean; /** Extra instance variables, such as ones a parent T3 turn already exported. */ readonly environment?: ReadonlyArray<{ readonly name: string; readonly value: string }>; + readonly beforeAcquire?: Effect.Effect; } = {}, ) { const fs = yield* FileSystem.FileSystem; @@ -134,7 +137,11 @@ const makeHarness = Effect.fn("makeAntigravityDriverHarness")(function* ( selected: AntigravityInstallation.AntigravityExecutable; failResolution: boolean; beforeAcquire: Effect.Effect; - } = { selected: first, failResolution: false, beforeAcquire: Effect.void }; + } = { + selected: first, + failResolution: false, + beforeAcquire: options.beforeAcquire ?? Effect.void, + }; const acquisitions: Array<{ binaryPath: string | undefined; path: string | undefined }> = []; const releases: Array = []; const launches: Array<{ @@ -552,6 +559,24 @@ it.layer(layerTest)("AntigravityDriver", (it) => { }), ); + it.effect.skipIf(windowsHost)("health checks re-read the model list once it is stale", () => + Effect.gen(function* () { + const acquired = yield* Queue.unbounded(); + const h = yield* makeHarness({ + config: { authMethod: "gemini-api-key", apiKey: "fixture-gemini-key" }, + beforeAcquire: Queue.offer(acquired, undefined).pipe(Effect.asVoid), + }); + yield* h.instance.snapshot.refresh; + yield* Queue.take(acquired); + yield* TestClock.adjust("5 hours"); + yield* h.instance.snapshot.refresh; + yield* TestClock.adjust("1 hour"); + yield* h.instance.snapshot.refresh; + yield* Queue.take(acquired); + expect(h.acquisitions).toHaveLength(2); + }), + ); + it.effect.skipIf(windowsHost)("keeps the previous catalog when executable resolution fails", () => Effect.gen(function* () { const h = yield* makeHarness(); diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.ts b/apps/server/src/provider/Drivers/AntigravityDriver.ts index 1117fc3b9376c..d4f9945bb270e 100644 --- a/apps/server/src/provider/Drivers/AntigravityDriver.ts +++ b/apps/server/src/provider/Drivers/AntigravityDriver.ts @@ -13,9 +13,11 @@ import { } from "@t3tools/shared/nodeRuntime"; import * as Digest from "effect/crypto/Digest"; import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as FiberHandle from "effect/FiberHandle"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; @@ -84,6 +86,8 @@ const isAcpRequestError = Schema.is(AcpErrors.AcpRequestError); const isAcpErrorDetails = Schema.is(Schema.Struct({ details: Schema.String })); const REFRESH_FAILED = "Could not refresh Antigravity models. The previous model list is unchanged."; +// Models come only from a session, so a health check re-reads them at most this often. +const MODEL_REFRESH_INTERVAL_MS = 6 * 60 * 60 * 1_000; const PYTHON_SOCKS_REQUIRED = "python-socks is required to use a SOCKS proxy"; // Keep upstream diagnostics in cause; only recognized, fixed text reaches clients. @@ -354,6 +358,15 @@ export const AntigravityDriver: ProviderDriver< }; }); + let modelsReadAtMs = Number.NEGATIVE_INFINITY; + const onSessionStarted = Effect.fn("AntigravityDriver.onSessionStarted")(function* ( + started: AcpSessionRuntime.AcpSessionRuntimeStartResult, + cwd?: string, + ) { + modelsReadAtMs = yield* Clock.currentTimeMillis; + yield* provider.onSessionStarted(started, cwd); + }); + const publishCatalog = Effect.fn("AntigravityDriver.publishCatalog")(function* ( started: AcpSessionRuntime.AcpSessionRuntimeStartResult, runtime: Pick< @@ -361,7 +374,7 @@ export const AntigravityDriver: ProviderDriver< "getEvents" | "drainEvents" | "setModel" >, ) { - yield* provider.onSessionStarted(started); + yield* onSessionStarted(started); yield* Stream.runForEach(runtime.getEvents(), (event) => { if (event._tag === "EventStreamBarrier") { return Deferred.succeed(event.acknowledge, undefined).pipe(Effect.asVoid); @@ -398,9 +411,11 @@ export const AntigravityDriver: ProviderDriver< // The probe must not spawn. The agent is a PyInstaller one-file bundle // that unpacks about 1 GB per launch, and the health check runs every - // minute. + // minute. It only starts a background model refresh once the list is stale. + const refreshStaleModels = yield* Deferred.make>(); const probe = Effect.gen(function* () { yield* modelCatalog.refreshInBackground; + yield* Deferred.await(refreshStaleModels).pipe(Effect.flatten); if (authConfigIssue !== null) { return yield* new ProviderSetupError({ instanceId, @@ -480,7 +495,7 @@ export const AntigravityDriver: ProviderDriver< ), ), profileDirectory, - onSessionStarted: provider.onSessionStarted, + onSessionStarted, onSessionEvent: (event, cwd) => { if (event._tag === "ConfigOptionsUpdated") { return provider.onConfigOptionsUpdated(event.configOptions); @@ -555,6 +570,32 @@ export const AntigravityDriver: ProviderDriver< ), ); + // Credentials that need no sign-in click; otherwise the launch would only ask for one. + const canReadModels = + authConfigIssue !== null + ? Effect.succeed(false) + : !antigravityAuthUsesBrowser(auth.authMethod) + ? Effect.succeed(true) + : fileSystem + .exists(path.join(profileDirectory, "antigravity-acp", "acp_token.json")) + .pipe(Effect.orElseSucceed(() => false)); + const modelRefresh = yield* FiberHandle.make(); + yield* Deferred.succeed( + refreshStaleModels, + Effect.gen(function* () { + const now = yield* Clock.currentTimeMillis; + if (now - modelsReadAtMs < MODEL_REFRESH_INTERVAL_MS || !(yield* canReadModels)) return; + modelsReadAtMs = now; + yield* FiberHandle.run( + modelRefresh, + // A fixed message: the cause can carry the agent's raw reply to the key. + refreshModels().pipe( + Effect.catch(() => Effect.logWarning("Antigravity could not refresh its model list.")), + ), + ); + }), + ); + return { instanceId, driverKind: DRIVER,