diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index fc8a93f9e1bc6..4a71640aa2626 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -175,6 +175,107 @@ it.effect("reads a management key from its secret provider", () => { ); }); +it.effect("keeps an account's last good reading when the hub's next read of it is refused", () => { + let refused = true; + let workspace = "account-a"; + let listsIdentity = true; + const readAccount = Effect.gen(function* () { + const sources = yield* UsageLimitSources.make; + return Effect.gen(function* () { + yield* sources.refresh; + const [source] = yield* sources.current; + return source?.accounts[0]?.usageLimits; + }); + }); + return Effect.gen(function* () { + const read = yield* readAccount; + // Nothing to keep yet: the account stays listed as unreadable. + expect(yield* read).toMatchObject({ windows: [], unavailable: { reason: "probeFailed" } }); + refused = false; + const good = yield* read; + expect(good?.windows.map((window) => window.usedPercent)).toEqual([78]); + expect(good?.unavailable).toBeUndefined(); + refused = true; + expect(yield* read).toEqual(good); + // The same auth file now holds another login, whose reading is unknown. + workspace = "account-b"; + expect(yield* read).toMatchObject({ windows: [], unavailable: { reason: "probeFailed" } }); + // A file that lists no login cannot show whether the next one is the same. + listsIdentity = false; + refused = false; + yield* read; + refused = true; + expect(yield* read).toMatchObject({ windows: [], unavailable: { reason: "probeFailed" } }); + }).pipe( + Effect.provide([ + ServerSettings.layerTest({ + usageLimitSources: { + [UsageLimitSourceId.make("hub")]: { + kind: "cliproxy", + url: "http://hub.test:8317", + managementKey: "management-secret", + enabled: true, + }, + }, + }), + Layer.mock(BackgroundPolicy.BackgroundPolicy)({ + shouldRunScopeWork: () => Effect.succeed(false), + }), + Layer.succeed(SecretResolver.SecretResolver, { + label: (provider) => Effect.succeed(provider), + read: (references) => Effect.succeed(references.map(() => undefined)), + invalidate: () => Effect.void, + }), + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.sync(() => { + if (new URL(request.url).pathname.endsWith("/auth-files")) { + return HttpClientResponse.fromWeb( + request, + Response.json({ + files: [ + { + id: "first.json", + auth_index: "a", + provider: "codex", + ...(listsIdentity + ? { + email: "first@example.com", + id_token: { chatgpt_account_id: workspace }, + } + : {}), + }, + ], + }), + ); + } + const usage = { + plan_type: "pro", + rate_limit: { + secondary_window: { + used_percent: 78, + reset_at: 4070908800, + limit_window_seconds: 604800, + }, + }, + }; + return HttpClientResponse.fromWeb( + request, + Response.json( + refused + ? { status_code: 429, body: "{}" } + : { status_code: 200, body: JSON.stringify(usage) }, + ), + ); + }), + ), + ), + NodeCrypto.layer, + ]), + ); +}); + const NOW = Date.parse("2026-11-13T12:00:00.000Z"); const at = (hours: number) => DateTime.formatIso(DateTime.makeUnsafe(NOW + hours * 3_600_000)); const spent = (id: string, hours: number): ServerProviderUsageWindow => ({ diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index d6f867d621d23..ff54bb02781a9 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -52,6 +52,41 @@ function sourceLabel(id: string, config: UsageLimitSourceConfig): string { } } +/** + * A refused read of one account keeps its last good reading, as provider probes keep their bars + * (see ServerProviderUsageLimits); a reading with none to keep stays listed as unreadable. + */ +function keepLastGoodReadings( + previous: ReadonlyArray, + next: ReadonlyArray, +): ReadonlyArray { + return next.map((source) => { + // Auth file names can be reused, so only the same hub and login may lend a reading. + const before = previous.find( + (candidate) => candidate.id === source.id && candidate.endpoint === source.endpoint, + ); + if (!before) return source; + return { + ...source, + accounts: source.accounts.map((account) => { + if (account.usageLimits.unavailable?.reason !== "probeFailed") return account; + // Without a listed login, a reused auth file name cannot be told apart from the old one. + if (account.email === undefined && account.workspaceId === undefined) return account; + const last = before.accounts.find( + (candidate) => + candidate.id === account.id && + candidate.driver === account.driver && + // Fingerprinted Antigravity readings omit the listed identity, so they are never lent. + candidate.usageLimits.credentialFingerprint === undefined && + candidate.email === account.email && + candidate.workspaceId === account.workspaceId, + ); + return last && !last.usageLimits.unavailable ? last : account; + }), + }; + }); +} + function missingManagementKey(config: UsageLimitSourceConfig): string { return typeof config.managementKey === "string" ? "No management key configured." @@ -131,7 +166,7 @@ export const make = Effect.gen(function* () { ([id, config]) => readSource(id as UsageLimitSourceId, config), { concurrency: 4 }, ); - yield* publish(snapshots); + yield* publish(keepLastGoodReadings(yield* Ref.get(stateRef), snapshots)); }).pipe(refreshLock.withPermits(1), Effect.ignoreCause({ log: true })); // Shares the refresh lock so a stale in-flight read cannot overwrite a redemption. @@ -156,6 +191,7 @@ export const make = Effect.gen(function* () { input.accountId, input.creditId, ); + // A redemption changes the reading, so a refused re-read must not bring back the one before it. const snapshot = yield* readSource(input.sourceId, config); const previous = yield* Ref.get(stateRef); yield* publish(previous.map((source) => (source.id === input.sourceId ? snapshot : source)));