From b4f63285e4a20e1b102c8f4f3d8229d6b7f40966 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Sat, 21 Nov 2026 18:23:57 +0000 Subject: [PATCH] fix(usage): refresh Magpie limits live and re-probe exhaustion A manual Limits refresh was reading Magpie's cached card list, so the panel kept stale cards or said instances had no quota when Magpie had cards. It now posts the live quota reread and ignores a cached object body. The same click clears in-memory exhaustion and marks a vendor that is still out. Automatic refreshes still share one in-flight read and wait five minutes. A manual click waits out an automatic read, then forces the live reread. The pre-commit knip check fails on four unused files this change does not touch: .github/scripts/mirror-play-apk.cjs, apps/web/scripts/verify-built-boot.ts, scripts/resolve-nightly-release.ts, and scripts/smoke-cli-archive.ts. Fmt and lint passed. Model: grok-4.7 Task: implement Agent: grok Harness: grok --- .../src/features/usage/usageLimitsRefresh.ts | 2 +- .../ModelAlertReactor.test.ts | 12 + .../orchestration-v2/ProviderExhaustion.ts | 9 + .../src/usage/UsageLimitSources.test.ts | 116 +++++++++ apps/server/src/usage/UsageLimitSources.ts | 217 +++++++++++++++-- apps/server/src/usage/magpieQuotas.test.ts | 83 +++++++ apps/server/src/usage/magpieQuotas.ts | 223 +++++++++++++++--- apps/server/src/ws.ts | 4 +- apps/web/src/components/usage/UsagePage.tsx | 6 +- .../client-runtime/src/state/usage.test.ts | 22 +- packages/client-runtime/src/state/usage.ts | 49 ++-- packages/contracts/src/rpc.ts | 5 + packages/shared/src/usageLimits.test.ts | 35 ++- packages/shared/src/usageLimits.ts | 31 +++ 14 files changed, 724 insertions(+), 90 deletions(-) diff --git a/apps/mobile/src/features/usage/usageLimitsRefresh.ts b/apps/mobile/src/features/usage/usageLimitsRefresh.ts index 1966873513728..f74438aa5b116 100644 --- a/apps/mobile/src/features/usage/usageLimitsRefresh.ts +++ b/apps/mobile/src/features/usage/usageLimitsRefresh.ts @@ -26,7 +26,7 @@ export async function refreshEnvironmentLimits( runAtomCommand( registry, serverEnvironment.refreshProviders, - { environmentId, input: {} }, + { environmentId, input: options.automatic ? {} : { liveQuotaRefresh: true } }, { reportFailure: false }, ), options.automatic, diff --git a/apps/server/src/orchestration-v2/ModelAlertReactor.test.ts b/apps/server/src/orchestration-v2/ModelAlertReactor.test.ts index 58fe1d3ff5feb..8a513afe7f98d 100644 --- a/apps/server/src/orchestration-v2/ModelAlertReactor.test.ts +++ b/apps/server/src/orchestration-v2/ModelAlertReactor.test.ts @@ -692,6 +692,18 @@ it("a provider out of usage is tracked until its reset, and a healthy run clears assert.equal(ProviderExhaustion.until("magpie-primary-codex", 1_000), null); }); +it("a quota reread clears exhaustion, then marks a vendor that is still out", () => { + ProviderExhaustion.clear(); + ProviderExhaustion.mark("magpie-primary-codex", 5_000); + ProviderExhaustion.reapply("magpie-primary-codex", () => { + assert.equal(ProviderExhaustion.until("magpie-primary-codex", 1_000), null); + return 9_000; + }); + assert.equal(ProviderExhaustion.until("magpie-primary-codex", 1_000), 9_000); + ProviderExhaustion.reapply("magpie-primary-codex", () => null); + assert.equal(ProviderExhaustion.until("magpie-primary-codex", 1_000), null); +}); + const linkAlert = (at: number): PeerLinkRenewalAlert => ({ type: "peer-link-renewal", id: `peer-link-renewal:environment-box:${at}`, diff --git a/apps/server/src/orchestration-v2/ProviderExhaustion.ts b/apps/server/src/orchestration-v2/ProviderExhaustion.ts index abb46b0087915..c5075fdd766e6 100644 --- a/apps/server/src/orchestration-v2/ProviderExhaustion.ts +++ b/apps/server/src/orchestration-v2/ProviderExhaustion.ts @@ -30,4 +30,13 @@ export const ProviderExhaustion = { if (instanceId === undefined) exhaustedUntil.clear(); else exhaustedUntil.delete(instanceId); }, + /** + * Drop the remembered limit, then mark it again when the probe still sees one. + * The probe runs after the clear, so a vendor that is still out is not left available. + */ + reapply(instanceId: string, probe: () => number | null): void { + ProviderExhaustion.clear(instanceId); + const until = probe(); + if (until !== null) ProviderExhaustion.mark(instanceId, until); + }, }; diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 60a225c2b9335..91c8b385a6598 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { expect, it } from "@effect/vitest"; import { ProviderDriverKind, + ProviderInstanceId, SecretProviderId, ServerConfigStreamEvent, UsageLimitSourceId, @@ -12,6 +13,7 @@ import { import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; @@ -20,6 +22,7 @@ import * as TestClock from "effect/testing/TestClock"; import { HttpClient, HttpClientResponse } from "effect/http"; import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; +import { ProviderExhaustion } from "../orchestration-v2/ProviderExhaustion.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as UsageLimitSources from "./UsageLimitSources.ts"; @@ -267,6 +270,119 @@ it.each([ ).toBe(expected); }); +it.each([ + { + name: "the earliest future reset when every account is spent", + accounts: [{ windows: [spent("five_hour", 2)] }, { windows: [spent("five_hour", 4)] }], + expected: Date.parse(at(2)), + }, + { + name: "room when an account is under the limit", + accounts: [{ windows: [{ ...spent("five_hour", 2), usedPercent: 40 }] }], + expected: null, + }, + { + name: "unknown when an account could not be read", + accounts: [{ windows: [spent("five_hour", 2)] }, { unavailable: true as const }], + expected: undefined, + }, + { + name: "unknown when an account has no window", + accounts: [{ windows: [] }], + expected: undefined, + }, + { + name: "unknown when the driver has no account", + accounts: [{ driver: "codex", windows: [spent("five_hour", 2)] }], + expected: undefined, + }, +])("magpie exhaustion: $name", ({ accounts, expected }) => { + expect( + UsageLimitSources.magpieExhaustionDecision( + [hub(accounts)], + ProviderDriverKind.make("claudeAgent"), + NOW, + ), + ).toBe(expected); +}); + +it.effect("a manual refresh rereads Magpie live and marks a vendor that is still exhausted", () => { + const instanceId = "magpie-codex"; + const stale = Date.parse("2026-12-01T00:00:00.000Z"); + const reset = Date.parse("2027-01-01T00:00:00.000Z"); + const urls: string[] = []; + const card = { + provider: "codex", + user: "first@example.com", + windows: [{ name: "5 hours", used: 100, resetsAt: "2027-01-01T00:00:00.000Z" }], + }; + const toml = 'model_provider = "m"\n[model_providers.m]\nbase_url = "http://127.0.0.1:3425/v1"\n'; + return Effect.gen(function* () { + ProviderExhaustion.mark(instanceId, stale); + const sources = yield* UsageLimitSources.make.pipe( + Effect.provideService( + FileSystem.FileSystem, + FileSystem.makeNoop({ + readFileString: (path) => Effect.succeed(path.endsWith("config.toml") ? toml : ""), + }), + ), + ); + yield* sources.refreshLive; + expect( + urls.some((url) => + url.includes("/api/usage/quotas/refresh?provider=codex&user=first%40example.com"), + ), + ).toBe(true); + expect(urls.some((url) => url.includes("/v1/magpie/quotas/refresh"))).toBe(false); + expect(ProviderExhaustion.until(instanceId, Date.parse("2026-11-21T00:00:00.000Z"))).toBe( + reset, + ); + }).pipe( + Effect.provide([ + ServerSettings.layerTest({ + usageLimitSources: { + [UsageLimitSourceId.make("magpie")]: { + kind: "magpie", + url: "http://127.0.0.1:3425", + managementKey: "", + enabled: true, + }, + }, + providerInstances: { + [ProviderInstanceId.make(instanceId)]: { + driver: ProviderDriverKind.make("codex"), + enabled: true, + config: { homePath: "/codex-home" }, + }, + }, + }), + 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(() => { + urls.push(request.url); + const body = request.url.includes("/api/usage/quotas/refresh") + ? [card] + : { data: [card] }; + return HttpClientResponse.fromWeb(request, Response.json(body)); + }), + ), + ), + NodeCrypto.layer, + NodeServices.layer, + ]), + Effect.ensuring(Effect.sync(() => ProviderExhaustion.clear(instanceId))), + ); +}); + it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold the failure", () => Effect.gen(function* () { yield* TestClock.setTime(NOW); diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 4e45ab7990131..5fad5410aa0da 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -24,14 +24,22 @@ import * as Ref from "effect/Ref"; import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; -import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; + +import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; +import { ProviderExhaustion } from "../orchestration-v2/ProviderExhaustion.ts"; +import { windowLimitsModel } from "../provider/claudeUsageLimits.ts"; import * as SecretResolverLayer from "../secrets/SecretResolver.ts"; import { resolveSettingsSecrets } from "../secrets/settingsSecretValue.ts"; import * as Settings from "../serverSettings.ts"; -import { windowLimitsModel } from "../provider/claudeUsageLimits.ts"; import { makeCliproxyApi } from "./cliproxyApi.ts"; -import { discoverGatewayOrigins, gatewayOrigin, makeMagpieApi } from "./magpieQuotas.ts"; +import { + discoverGatewayOrigins, + discoverMagpieRoutes, + gatewayOrigin, + makeMagpieApi, + type MagpieGatewayRoute, +} from "./magpieQuotas.ts"; export class UsageLimitSources extends Context.Service< UsageLimitSources, @@ -39,6 +47,8 @@ export class UsageLimitSources extends Context.Service< readonly current: Effect.Effect>; readonly streamChanges: Stream.Stream>; readonly refresh: Effect.Effect; + /** Manual Limits refresh: reread Magpie cards live and re-probe exhaustion. */ + readonly refreshLive: Effect.Effect; readonly consumeResetCredit: ( input: UsageLimitSourceConsumeResetCreditInput, ) => Effect.Effect; @@ -61,6 +71,37 @@ function missingManagementKey(config: UsageLimitSourceConfig): string { : "Could not read the management key from its secret provider."; } +function sourceSnapshotBase( + id: UsageLimitSourceId, + config: UsageLimitSourceConfig, + checkedAt: string, +) { + const url = URL.parse(config.url); + return { + id, + kind: config.kind, + label: sourceLabel(id, config), + ...(url && /^https?:$/.test(url.protocol) + ? { + origin: url.origin, + endpoint: `${url.origin}${url.pathname.replace(/(\/management\.html)?\/*$/i, "")}`, + } + : {}), + checkedAt, + } as const; +} + +function labelDiscoveredMagpie( + discovered: ReadonlyArray, +): ReadonlyArray { + return discovered.length > 1 + ? discovered.map((source) => ({ + ...source, + label: `Magpie · ${source.id.slice("magpie:".length)}`, + })) + : discovered; +} + /** @public */ export const make = Effect.gen(function* () { const api = yield* makeCliproxyApi; @@ -87,19 +128,7 @@ export const make = Effect.gen(function* () { config: UsageLimitSourceConfig, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); - const url = URL.parse(config.url); - const base = { - id, - kind: config.kind, - label: sourceLabel(id, config), - ...(url && /^https?:$/.test(url.protocol) - ? { - origin: url.origin, - endpoint: `${url.origin}${url.pathname.replace(/(\/management\.html)?\/*$/i, "")}`, - } - : {}), - checkedAt, - } as const; + const base = sourceSnapshotBase(id, config, checkedAt); const managementKey = yield* readManagementKey(config); // A Magpie gateway on loopback answers without a key. if (config.kind === "cliproxy" && managementKey.length === 0) { @@ -163,12 +192,7 @@ export const make = Effect.gen(function* () { accounts: read.value.accounts, }); } - return discovered.length > 1 - ? discovered.map((source) => ({ - ...source, - label: `Magpie · ${source.id.slice("magpie:".length)}`, - })) - : discovered; + return labelDiscoveredMagpie(discovered); }, Effect.orElseSucceed((): ReadonlyArray => []), ); @@ -199,6 +223,125 @@ export const make = Effect.gen(function* () { yield* publish([...snapshots, ...(yield* discoverMagpieSources(settings, snapshots))]); }).pipe(refreshLock.withPermits(1), Effect.ignoreCause({ log: true })); + const readLiveMagpie = Effect.fn("UsageLimitSources.readLiveMagpie")(function* ( + id: UsageLimitSourceId, + config: UsageLimitSourceConfig, + drivers: ReadonlyArray, + ) { + const checkedAt = DateTime.formatIso(yield* DateTime.now); + const managementKey = yield* readManagementKey(config); + const base = sourceSnapshotBase(id, config, checkedAt); + const accounts = yield* magpie + .refreshLive({ url: config.url, managementKey, drivers }) + .pipe(Effect.result); + if (accounts._tag === "Failure") { + yield* Effect.logDebug("usage limit source live reread failed", { + id, + cause: accounts.failure, + }); + return { + live: false as const, + snapshot: { ...base, accounts: [], error: accounts.failure.detail }, + }; + } + return { live: true as const, snapshot: { ...base, accounts: accounts.success } }; + }); + + // Same lock as the cached refresh, so a slow poll cannot put the cache back after a reread. + const refreshLive = Effect.gen(function* () { + const settings = yield* settingsService.getSettings.pipe( + Effect.orElseSucceed((): ServerSettings | null => null), + ); + const entries = Object.entries(settings?.usageLimitSources ?? {}).filter( + ([, config]) => config.enabled, + ); + const snapshots = yield* Effect.forEach( + entries.filter(([, config]) => config.kind !== "magpie"), + ([id, config]) => readSource(id as UsageLimitSourceId, config), + { concurrency: 4 }, + ); + const routes = + settings === null + ? [] + : yield* discoverMagpieRoutes(settings, process.env).pipe( + Effect.provideService(FileSystem.FileSystem, fileSystem), + Effect.provideService(Path.Path, path), + Effect.orElseSucceed((): ReadonlyArray => []), + ); + const driversFor = (origin: string | null) => { + const seen = new Set(); + const drivers: ProviderDriverKind[] = []; + for (const route of routes) { + if (origin === null || route.origin !== origin || seen.has(route.driver)) continue; + seen.add(route.driver); + drivers.push(route.driver); + } + return drivers; + }; + const configured = entries.filter(([, config]) => config.kind === "magpie"); + const configuredReads = yield* Effect.forEach( + configured, + ([id, config]) => + readLiveMagpie(id as UsageLimitSourceId, config, driversFor(gatewayOrigin(config.url))), + { concurrency: 1 }, + ); + const explicit = new Set(configured.map(([, config]) => gatewayOrigin(config.url))); + const discoveredOrigins = [...new Set(routes.map((route) => route.origin))].filter( + (origin) => !explicit.has(origin), + ); + const discoveredReads = yield* Effect.forEach( + discoveredOrigins, + (origin) => + magpie.refreshLive({ url: origin, managementKey: "", drivers: driversFor(origin) }).pipe( + Effect.map((accounts) => ({ origin, accounts })), + Effect.option, + ), + { concurrency: 1 }, + ); + const checkedAt = DateTime.formatIso(yield* DateTime.now); + const seenAccounts = new Set([ + ...snapshots.map((source) => accountSet(source.accounts)), + ...configuredReads.map((read) => accountSet(read.snapshot.accounts)), + ]); + const discovered: UsageLimitSourceSnapshot[] = []; + const liveByOrigin = new Map(); + for (const read of configuredReads) { + const origin = read.snapshot.origin; + if (read.live && origin) liveByOrigin.set(origin, read.snapshot); + } + for (const read of discoveredReads) { + if (read._tag === "None") continue; + const key = accountSet(read.value.accounts); + if (seenAccounts.has(key)) continue; + seenAccounts.add(key); + const snapshot: UsageLimitSourceSnapshot = { + id: `magpie:${new URL(read.value.origin).host}` as UsageLimitSourceId, + kind: "magpie", + label: "Magpie", + origin: read.value.origin, + endpoint: read.value.origin, + checkedAt, + accounts: read.value.accounts, + }; + discovered.push(snapshot); + liveByOrigin.set(read.value.origin, snapshot); + } + yield* publish([ + ...snapshots, + ...configuredReads.map((read) => read.snapshot), + ...labelDiscoveredMagpie(discovered), + ]); + const nowMs = DateTime.toEpochMillis(yield* DateTime.now); + for (const route of routes) { + const snapshot = liveByOrigin.get(route.origin); + if (snapshot === undefined) continue; + const decision = magpieExhaustionDecision([snapshot], route.driver, nowMs); + if (decision === undefined) continue; + const until = decision; + ProviderExhaustion.reapply(route.instanceId, () => until); + } + }).pipe(refreshLock.withPermits(1), Effect.ignoreCause({ log: true })); + // Shares the refresh lock so a stale in-flight read cannot overwrite a redemption. const consumeResetCredit = Effect.fn("UsageLimitSources.consumeResetCredit")(function* ( input: UsageLimitSourceConsumeResetCreditInput, @@ -260,6 +403,7 @@ export const make = Effect.gen(function* () { current: Ref.get(stateRef), consumeResetCredit, refresh, + refreshLive, get streamChanges() { return Stream.unwrap( Effect.gen(function* () { @@ -312,6 +456,35 @@ export function hubPoolResetAt( return earliest === null ? null : DateTime.formatIso(DateTime.makeUnsafe(earliest)); } +/** + * What a live Magpie reread says about one driver. + * `undefined` means the cards did not decide, so the remembered limit stays. + * `null` means a readable account has room. A number is the epoch ms every account is out until. + */ +export function magpieExhaustionDecision( + sources: ReadonlyArray, + driver: ProviderDriverKind, + nowMs: number, +): number | null | undefined { + const accounts = sources.flatMap((source) => + source.error ? [] : source.accounts.filter((account) => account.driver === driver), + ); + if (accounts.length === 0) return undefined; + if (accounts.some((account) => account.usageLimits.unavailable !== undefined)) return undefined; + let earliest: number | null = null; + for (const account of accounts) { + const windows = account.usageLimits.windows.filter((window) => windowLimitsModel(window, null)); + if (windows.length === 0) return undefined; + if (windows.some((window) => window.usedPercent < 100)) return null; + const resets = windows.map((window) => Date.parse(window.resetsAt ?? "")); + if (resets.some((reset) => !Number.isFinite(reset))) return undefined; + const recoversAt = Math.max(...resets); + if (recoversAt <= nowMs) return null; + earliest = earliest === null ? recoversAt : Math.min(earliest, recoversAt); + } + return earliest === null ? undefined : earliest; +} + export class UsageLimitResetFallback extends Context.Reference<{ readonly resetAt: ( driver: ProviderDriverKind, diff --git a/apps/server/src/usage/magpieQuotas.test.ts b/apps/server/src/usage/magpieQuotas.test.ts index b11ac499b1035..2fe14973c22f5 100644 --- a/apps/server/src/usage/magpieQuotas.test.ts +++ b/apps/server/src/usage/magpieQuotas.test.ts @@ -14,6 +14,7 @@ import { gatewayOrigin, isPrivateOrigin, magpieQuotasToAccounts, + magpieRefreshTargets, makeMagpieApi, openCodeBaseUrls, } from "./magpieQuotas.ts"; @@ -137,6 +138,8 @@ describe("magpieQuotasToAccounts", () => { "magpie-go:oc-billed", "magpie-go:oc-other", "claude:second@example.com", + "grok:jeffrey.smith@closura.ai", + "grok:kameron.smith@closura.ai", ]); const team = accounts[2]!; const billed = accounts[3]!; @@ -334,4 +337,84 @@ describe("makeMagpieApi", () => { expect(result._tag === "Failure" && result.failure.detail).toMatch(/refused the key/); }), ); + + it("rereads listed cards, or the driver's provider when none are listed", () => { + expect( + magpieRefreshTargets(entries.slice(0, 2), [ProviderDriverKind.make("claudeAgent")]), + ).toEqual([{ provider: "claude", user: "first@example.com" }]); + expect(magpieRefreshTargets([], [ProviderDriverKind.make("codex")])).toEqual([ + { provider: "codex" }, + ]); + expect(magpieRefreshTargets([], [])).toEqual([]); + }); + + it.effect("posts the live quota refresh and keeps the last card list", () => + Effect.gen(function* () { + const seen: Array<{ url: string; authorization: string | undefined }> = []; + const claude = entries[0]!; + const codex = entries[1]!; + const http = HttpClient.make((request) => + Effect.sync(() => { + seen.push({ url: request.url, authorization: request.headers.authorization }); + const body = request.url.includes("/api/usage/quotas/refresh") + ? request.url.includes("provider=codex") + ? [codex] + : [claude, codex] + : { object: "list", data: [claude, codex] }; + return HttpClientResponse.fromWeb(request, Response.json(body)); + }), + ); + const result = yield* makeMagpieApi.pipe( + Effect.flatMap((api) => + api.refreshLive({ + url: "http://127.0.0.1:3425/v1", + managementKey: "gateway-key", + drivers: [ProviderDriverKind.make("claudeAgent"), ProviderDriverKind.make("codex")], + }), + ), + Effect.result, + Effect.provideService(HttpClient.HttpClient, http), + ); + expect(seen.map((request) => request.url)).toEqual([ + "http://127.0.0.1:3425/v1/magpie/quotas", + "http://127.0.0.1:3425/api/usage/quotas/refresh?provider=claude&user=first%40example.com", + "http://127.0.0.1:3425/api/usage/quotas/refresh?provider=codex&user=first%40example.com", + ]); + expect(seen.every((request) => request.authorization === "Bearer gateway-key")).toBe(true); + expect(result._tag === "Success" && result.success.map((account) => account.id)).toEqual([ + "codex:first@example.com", + ]); + }), + ); + + it.effect("rejects a cached quota list from the live refresh", () => + Effect.gen(function* () { + const seen: string[] = []; + const http = HttpClient.make((request) => + Effect.sync(() => { + seen.push(request.url); + const body = request.url.includes("/api/usage/quotas/refresh") + ? { data: entries.slice(0, 1) } + : { object: "list", data: entries.slice(0, 1) }; + return HttpClientResponse.fromWeb(request, Response.json(body)); + }), + ); + const result = yield* makeMagpieApi.pipe( + Effect.flatMap((api) => + api.refreshLive({ + url: "http://127.0.0.1:3425/v1", + managementKey: "", + drivers: [ProviderDriverKind.make("claudeAgent")], + }), + ), + Effect.result, + Effect.provideService(HttpClient.HttpClient, http), + ); + expect(seen.some((url) => url.includes("/v1/magpie/quotas/refresh"))).toBe(false); + expect(seen.some((url) => url.includes("/api/usage/quotas/refresh?provider=claude"))).toBe( + true, + ); + expect(result._tag === "Failure" && result.failure.detail).toMatch(/cached quota list/); + }), + ); }); diff --git a/apps/server/src/usage/magpieQuotas.ts b/apps/server/src/usage/magpieQuotas.ts index 322bbaa1cfe8e..4d79428d9019a 100644 --- a/apps/server/src/usage/magpieQuotas.ts +++ b/apps/server/src/usage/magpieQuotas.ts @@ -1,7 +1,6 @@ /** - * Magpie gateway quotas: `GET /v1/magpie/quotas` reports every pooled account. - * Gateways are found from the base URLs enabled instances already use; an - * origin that does not answer the route is not a gateway and is dropped. + * Live reread is `POST /api/usage/quotas/refresh`. An object body is the cached list, not a success. + * Gateways come from enabled instances; an origin that does not answer is dropped. */ import * as NodeOS from "node:os"; @@ -75,6 +74,22 @@ function driverFor(provider: string): ProviderDriverKind | null { } } +/** The gateway provider `magpie quota refresh ` rereads for a driver. */ +function magpieProviderForDriver(driver: ProviderDriverKind): string | null { + switch (driver) { + case "claudeAgent": + return "claude"; + case "codex": + return "codex"; + case "opencode": + return "opencode"; + case "grok": + return "grok"; + default: + return null; + } +} + function slug(value: string): string { return value .toLowerCase() @@ -95,9 +110,7 @@ function magpieWindow( const [span = window.name, ...scopeParts] = window.name.split("·").map((part) => part.trim()); const scope = scopeParts.join(" · "); const match = /^(?:(\d+(?:\.\d+)?)\s+)?(minute|hour|day|week|month)s?$/i.exec(span); - const mins = match - ? Number(match[1] ?? 1) * MINUTES[match[2]!.toLowerCase()]! - : undefined; + const mins = match ? Number(match[1] ?? 1) * MINUTES[match[2]!.toLowerCase()]! : undefined; const kind: ServerProviderUsageWindow["kind"] = mins === undefined ? "other" @@ -206,21 +219,55 @@ export function magpieQuotasToAccounts( return accounts; } +export interface MagpieQuotaTarget { + readonly provider: string; + readonly user?: string | undefined; +} + +const QuotaList = Schema.Array(QuotaEntry); +const decodeQuotaList = Schema.decodeUnknownEffect(QuotaList); +const CACHED_QUOTA_LIST = + "The Magpie gateway returned a cached quota list instead of rereading it."; + +/** Cards to reread: known accounts for the drivers, or the driver's own provider when none are listed. */ +export function magpieRefreshTargets( + entries: ReadonlyArray, + drivers: ReadonlyArray, +): ReadonlyArray { + const wanted = new Set(drivers); + const seen = new Set(); + const targets: MagpieQuotaTarget[] = []; + const add = (provider: string, user?: string) => { + const key = `${provider}\n${user ?? ""}`; + if (seen.has(key)) return; + seen.add(key); + targets.push(user ? { provider, user } : { provider }); + }; + for (const entry of entries) { + const driver = driverFor(entry.provider); + if (!driver || (wanted.size > 0 && !wanted.has(driver))) continue; + const user = entry.user?.trim(); + add(entry.provider, user || undefined); + } + if (targets.length === 0) { + for (const driver of drivers) { + const provider = magpieProviderForDriver(driver); + if (provider) add(provider); + } + } + return targets; +} + export const makeMagpieApi = Effect.gen(function* () { const client = yield* HttpClient.HttpClient; - const readAccounts = Effect.fn("MagpieApi.readAccounts")(function* (config: { - readonly url: string; - readonly managementKey: string; - }): Effect.fn.Return, UsageLimitSourceError> { - const origin = gatewayOrigin(config.url); - if (!origin) { - return yield* new UsageLimitSourceError({ detail: "The Magpie gateway URL is not valid." }); - } - const request = HttpClientRequest.get(`${origin}/v1/magpie/quotas`).pipe( - config.managementKey ? HttpClientRequest.bearerToken(config.managementKey) : (r) => r, - ); - // One deadline covers headers and body: a stalled body must not hold the refresh lock. - const body = yield* client.execute(request).pipe( + const quotaRequest = (request: HttpClientRequest.HttpClientRequest, managementKey: string) => + managementKey ? HttpClientRequest.bearerToken(managementKey)(request) : request; + const readJson = ( + request: HttpClientRequest.HttpClientRequest, + schema: S, + timeout: "5 seconds" | "45 seconds", + ) => + client.execute(request).pipe( Effect.flatMap((response) => response.status === 401 || response.status === 403 ? Effect.fail( @@ -229,25 +276,93 @@ export const makeMagpieApi = Effect.gen(function* () { }), ) : HttpClientResponse.filterStatusOk(response).pipe( - Effect.flatMap(HttpClientResponse.schemaBodyJson(QuotasResponse)), - Effect.mapError( - () => - new UsageLimitSourceError({ - detail: "The Magpie gateway did not report quotas.", - }), + Effect.flatMap((ok) => HttpClientResponse.schemaBodyJson(schema)(ok)), + Effect.mapError((error) => + isUsageLimitSourceError(error) + ? error + : new UsageLimitSourceError({ + detail: "The Magpie gateway did not report quotas.", + }), ), ), ), - Effect.timeout("5 seconds"), + Effect.timeout(timeout), Effect.mapError((error) => isUsageLimitSourceError(error) ? error : new UsageLimitSourceError({ detail: "The Magpie gateway did not answer." }), ), ); - return magpieQuotasToAccounts(body.data, DateTime.formatIso(yield* DateTime.now)); + const readEntries = Effect.fn("MagpieApi.readEntries")(function* (config: { + readonly url: string; + readonly managementKey: string; + }) { + const origin = gatewayOrigin(config.url); + if (!origin) { + return yield* new UsageLimitSourceError({ detail: "The Magpie gateway URL is not valid." }); + } + const body = yield* readJson( + quotaRequest(HttpClientRequest.get(`${origin}/v1/magpie/quotas`), config.managementKey), + QuotasResponse, + "5 seconds", + ); + return body.data; }); - return { readAccounts }; + const readAccounts = Effect.fn("MagpieApi.readAccounts")(function* (config: { + readonly url: string; + readonly managementKey: string; + }): Effect.fn.Return, UsageLimitSourceError> { + const entries = yield* readEntries(config); + return magpieQuotasToAccounts(entries, DateTime.formatIso(yield* DateTime.now)); + }); + // `magpie quota refresh []`: POST /api/usage/quotas/refresh. + // POST /v1/magpie/quotas/refresh answers 200 with the cached list and does not + // run Claude Code /usage, so an object body is not a successful reread. + const refreshLive = Effect.fn("MagpieApi.refreshLive")(function* (config: { + readonly url: string; + readonly managementKey: string; + readonly drivers: ReadonlyArray; + }): Effect.fn.Return, UsageLimitSourceError> { + const origin = gatewayOrigin(config.url); + if (!origin) { + return yield* new UsageLimitSourceError({ detail: "The Magpie gateway URL is not valid." }); + } + const listed = yield* readEntries(config).pipe( + Effect.orElseSucceed((): ReadonlyArray => []), + ); + const targets = magpieRefreshTargets(listed, config.drivers); + if (targets.length === 0) { + return yield* new UsageLimitSourceError({ + detail: "The Magpie gateway has no quota cards to reread.", + }); + } + let latest: ReadonlyArray | null = null; + for (const target of targets) { + const query = `provider=${encodeURIComponent(target.provider)}${ + target.user ? `&user=${encodeURIComponent(target.user)}` : "" + }`; + const request = quotaRequest( + HttpClientRequest.post(`${origin}/api/usage/quotas/refresh?${query}`).pipe( + HttpClientRequest.bodyJsonUnsafe({}), + ), + config.managementKey, + ); + const parsed = yield* readJson(request, Schema.Unknown, "45 seconds"); + if (!Array.isArray(parsed)) { + return yield* new UsageLimitSourceError({ detail: CACHED_QUOTA_LIST }); + } + latest = yield* decodeQuotaList(parsed).pipe( + Effect.mapError( + () => new UsageLimitSourceError({ detail: "The Magpie gateway did not report quotas." }), + ), + ); + } + if (!latest) { + return yield* new UsageLimitSourceError({ detail: CACHED_QUOTA_LIST }); + } + return magpieQuotasToAccounts(latest, DateTime.formatIso(yield* DateTime.now)); + }); + return { readAccounts, refreshLive }; }); /** `http://127.0.0.1:3425/v1` → `http://127.0.0.1:3425`; anything not http(s) is no gateway. */ @@ -325,11 +440,18 @@ function stringField(record: Record, key: string): string | und return typeof value === "string" && value.trim() ? value.trim() : undefined; } +export interface MagpieGatewayRoute { + readonly instanceId: string; + readonly driver: ProviderDriverKind; + readonly origin: string; +} + /** - * Distinct gateway origins the enabled provider instances send requests to. - * A file that is missing or unreadable contributes nothing. + * Enabled instances and the private gateway each one sends requests to. + * A file that is missing or unreadable contributes nothing. One instance keeps + * the first private origin it names. */ -export const discoverGatewayOrigins = Effect.fn("discoverGatewayOrigins")(function* ( +export const discoverMagpieRoutes = Effect.fn("discoverMagpieRoutes")(function* ( settings: Pick, env: NodeJS.ProcessEnv, ) { @@ -337,8 +459,16 @@ export const discoverGatewayOrigins = Effect.fn("discoverGatewayOrigins")(functi const path = yield* Path.Path; const home = env.HOME || env.USERPROFILE || NodeOS.homedir(); const read = (file: string) => fs.readFileString(file).pipe(Effect.orElseSucceed(() => "")); - const urls: string[] = []; - for (const instance of Object.values(settings.providerInstances)) { + const routes: MagpieGatewayRoute[] = []; + const seen = new Set(); + const consider = (instanceId: string, driver: ProviderDriverKind, url: string) => { + if (seen.has(instanceId)) return; + const origin = gatewayOrigin(url); + if (!origin || !isPrivateOrigin(origin)) return; + seen.add(instanceId); + routes.push({ instanceId, driver, origin }); + }; + for (const [instanceId, instance] of Object.entries(settings.providerInstances)) { if (instance.enabled === false) continue; const config = instanceConfig(instance.config); const launchArgs = stringField(config, "launchArgs"); @@ -352,7 +482,7 @@ export const discoverGatewayOrigins = Effect.fn("discoverGatewayOrigins")(functi const codexHome = stringField(config, "homePath") ?? envValue("CODEX_HOME") ?? path.join(home, ".codex"); const url = codexBaseUrl(yield* read(path.join(codexHome, "config.toml")), launchArgs); - if (url) urls.push(url); + if (url) consider(instanceId, instance.driver, url); break; } case "opencode": { @@ -363,12 +493,16 @@ export const discoverGatewayOrigins = Effect.fn("discoverGatewayOrigins")(functi : ["opencode.jsonc", "opencode.json"].map((name) => path.join(configHome, "opencode", name), ); - for (const file of files) urls.push(...openCodeBaseUrls(yield* read(file))); + for (const file of files) { + for (const url of openCodeBaseUrls(yield* read(file))) { + consider(instanceId, instance.driver, url); + } + } break; } case "claudeAgent": { const fromEnv = envValue("ANTHROPIC_BASE_URL"); - if (fromEnv) urls.push(fromEnv); + if (fromEnv) consider(instanceId, instance.driver, fromEnv); const files = [settingsArg(launchArgs), stringField(config, "homePath")] .filter((file): file is string => Boolean(file)) .map((file) => (file.endsWith(".json") ? file : path.join(file, "settings.json"))); @@ -376,12 +510,23 @@ export const discoverGatewayOrigins = Effect.fn("discoverGatewayOrigins")(functi const parsed: unknown = parseJsonc(yield* read(file)); const url = (parsed as { env?: { ANTHROPIC_BASE_URL?: unknown } } | null)?.env ?.ANTHROPIC_BASE_URL; - if (typeof url === "string") urls.push(url); + if (typeof url === "string") consider(instanceId, instance.driver, url); } break; } } } - const origins = urls.map(gatewayOrigin).filter((origin): origin is string => !!origin); - return [...new Set(origins)].filter(isPrivateOrigin); + return routes; +}); + +/** + * Distinct gateway origins the enabled provider instances send requests to. + * A file that is missing or unreadable contributes nothing. + */ +export const discoverGatewayOrigins = Effect.fn("discoverGatewayOrigins")(function* ( + settings: Pick, + env: NodeJS.ProcessEnv, +) { + const routes = yield* discoverMagpieRoutes(settings, env); + return [...new Set(routes.map((route) => route.origin))]; }); diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index fe0cf3b0e17c4..6d49e3380fedc 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -2613,7 +2613,9 @@ export const layerWsRpc = ( } // Await refresh; RPC scope closure would interrupt a fork before the hub answered. if (input.instanceId === undefined) { - yield* usageLimitSources.refresh; + yield* input.liveQuotaRefresh === true + ? usageLimitSources.refreshLive + : usageLimitSources.refresh; } let providers = yield* input.cwd !== undefined && input.instanceId !== undefined ? providerRegistry.refreshWorkspaceSnapshot({ diff --git a/apps/web/src/components/usage/UsagePage.tsx b/apps/web/src/components/usage/UsagePage.tsx index b60ae71d9ad52..2da7f648e38b9 100644 --- a/apps/web/src/components/usage/UsagePage.tsx +++ b/apps/web/src/components/usage/UsagePage.tsx @@ -256,7 +256,11 @@ export function UsagePage() { if (presentation.connection.phase === "connected" && presentation.serverConfig !== null) { return refreshUsageLimits( environmentId, - () => refreshProviders({ environmentId, input: {} }), + () => + refreshProviders({ + environmentId, + input: automatic ? {} : { liveQuotaRefresh: true }, + }), automatic, ); } diff --git a/packages/client-runtime/src/state/usage.test.ts b/packages/client-runtime/src/state/usage.test.ts index 380e2de731b5d..267779f3158ab 100644 --- a/packages/client-runtime/src/state/usage.test.ts +++ b/packages/client-runtime/src/state/usage.test.ts @@ -191,17 +191,23 @@ describe("manual usage refresh", () => { }); describe("limits refresh cooldown", () => { - it("shares an in-flight check and skips duplicate automatic refreshes", async () => { + it("lets a manual refresh reread after an automatic one, and still coalesces automatic refreshes", async () => { const id = EnvironmentId.make("limits-in-flight"); const oldCheck = Promise.withResolvers(); - const first = refreshUsageLimits(id, () => oldCheck.promise, true); - const newCheck = vi.fn(async () => "new limits"); - const manual = refreshUsageLimits(id, newCheck); - expect(await refreshUsageLimits(id, newCheck, true)).toBeUndefined(); - expect(newCheck).not.toHaveBeenCalled(); + const automaticRefresh = vi.fn(() => oldCheck.promise); + const first = refreshUsageLimits(id, automaticRefresh, true); + const live = vi.fn(async () => "live limits"); + const manual = refreshUsageLimits(id, live); + expect(live).not.toHaveBeenCalled(); + expect(await refreshUsageLimits(id, live, true)).toBeUndefined(); oldCheck.resolve("old limits"); expect(await first).toBe("old limits"); - expect(await manual).toBe("old limits"); - expect(newCheck).not.toHaveBeenCalled(); + expect(await manual).toBe("live limits"); + expect(live).toHaveBeenCalledOnce(); + expect(await refreshUsageLimits(id, live, true)).toBeUndefined(); + expect(live).toHaveBeenCalledOnce(); + const later = vi.fn(async () => "later limits"); + expect(await refreshUsageLimits(id, later)).toBe("later limits"); + expect(later).toHaveBeenCalledOnce(); }); }); diff --git a/packages/client-runtime/src/state/usage.ts b/packages/client-runtime/src/state/usage.ts index f302ffbdfd85c..4851611da4671 100644 --- a/packages/client-runtime/src/state/usage.ts +++ b/packages/client-runtime/src/state/usage.ts @@ -18,29 +18,50 @@ export function usageSummaryInput( } const limitsRefreshAfter = new Map(); -const limitsRefreshes = new Map>(); +/** In-flight limits refresh. `live` is a manual reread; automatic refreshes share or skip it. */ +const limitsRefreshes = new Map< + EnvironmentId, + { readonly live: boolean; readonly promise: Promise } +>(); -export async function refreshUsageLimits( +function beginLimitsRefresh( environmentId: EnvironmentId, refresh: () => Promise, - automatic = false, -): Promise { - const pending = limitsRefreshes.get(environmentId); - if (pending !== undefined) { - return automatic ? undefined : ((await pending) as A); - } - const refreshAfter = limitsRefreshAfter.get(environmentId) ?? 0; - // @effect-diagnostics-next-line globalDate:off - if (automatic && Date.now() < refreshAfter) return; + live: boolean, +): Promise { const current = Promise.resolve() .then(refresh) .finally(() => { - limitsRefreshes.delete(environmentId); + if (limitsRefreshes.get(environmentId)?.promise === current) { + limitsRefreshes.delete(environmentId); + } // @effect-diagnostics-next-line globalDate:off limitsRefreshAfter.set(environmentId, Date.now() + 5 * 60_000); }); - limitsRefreshes.set(environmentId, current); - return await current; + limitsRefreshes.set(environmentId, { live, promise: current }); + return current; +} + +export async function refreshUsageLimits( + environmentId: EnvironmentId, + refresh: () => Promise, + automatic = false, +): Promise { + const pending = limitsRefreshes.get(environmentId); + if (automatic) { + if (pending !== undefined) return; + // @effect-diagnostics-next-line globalDate:off + if (Date.now() < (limitsRefreshAfter.get(environmentId) ?? 0)) return; + return await beginLimitsRefresh(environmentId, refresh, false); + } + // A click during an automatic poll waits, then rereads. It does not keep that poll's result. + if (pending?.live) return (await pending.promise) as A; + if (pending) { + await pending.promise.catch(() => undefined); + const started = limitsRefreshes.get(environmentId); + if (started?.live) return (await started.promise) as A; + } + return await beginLimitsRefresh(environmentId, refresh, true); } export async function refreshUsage({ diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index 7bb56b6561f38..9a4133547412d 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -688,6 +688,11 @@ export const WsServerRefreshProvidersRpc = Rpc.make(WS_METHODS.serverRefreshProv workspaceSnapshotsOnDemand: Schema.optional(Schema.Boolean), /** As on `subscribeServerConfig`: add /usage-limits to the replied snapshot. */ usageLimitsCommand: Schema.optional(Schema.Boolean), + /** + * Usage Limits refresh: reread Magpie quotas live and re-probe exhaustion. + * Automatic refreshes omit this and keep the cached gateway read. + */ + liveQuotaRefresh: Schema.optional(Schema.Boolean), /** As on `subscribeServerConfig`: a failed cwd scan replies with the machine catalog as a pending snapshot. */ providerCatalogsOnDemand: Schema.optional(Schema.Boolean), /** diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 7dd8084e302c7..35bbecf111d0c 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1981,17 +1981,40 @@ describe("owners and not-applicable usage", () => { }); it("lists not-applicable usage apart from failures, and a billing refusal as a notice", () => { - const view = presentations(); - expect(collectNotApplicable(view)).toEqual([ + const covered = presentations(); + expect(collectNotApplicable(covered)).toEqual([]); + const host = covered.get(EnvironmentId.make("env-a"))!; + const uncovered = new Map([ + [ + EnvironmentId.make("env-a"), + { + ...host, + serverConfig: { ...host.serverConfig, usageLimitSources: [] }, + }, + ], + ]); + expect(collectNotApplicable(uncovered)).toEqual([ { environment: "Crown", subject: "Magpie Codex", reason: "gateway reports it." }, ]); - expect(collectLimitNotices(view)).toEqual([ + expect(collectLimitNotices(covered)).toEqual([ "Antigravity: Sign in with Google to see limits.", "Magpie · owner.a@example.com · Team: the vendor says payment is required, so this subscription's billing needs attention.", "Magpie · k: Upstream timed out.", ]); }); + it("drops a gateway instance from the empty count once Magpie has its cards", () => { + const host = presentations().get(EnvironmentId.make("env-a"))!; + const withoutCards = new Map([ + [ + EnvironmentId.make("env-a"), + { ...host, serverConfig: { ...host.serverConfig, usageLimitSources: [] } }, + ], + ]); + expect(collectNotApplicable(withoutCards)).toHaveLength(1); + expect(collectNotApplicable(presentations())).toHaveLength(0); + }); + it("names a billing refusal once however many machines see it", () => { const view = new Map([ [EnvironmentId.make("env-a"), presentations().get(EnvironmentId.make("env-a"))!], @@ -2009,10 +2032,14 @@ describe("owners and not-applicable usage", () => { it("groups one instance and reason across machines into one row", () => { const base = presentations().get(EnvironmentId.make("env-a"))!; + const uncovered = { + ...base, + serverConfig: { ...base.serverConfig, usageLimitSources: [] }, + }; const view = new Map( ["r720", "asus", "laptop"].map((label) => [ EnvironmentId.make(label), - { ...base, entry: { target: { label } } }, + { ...uncovered, entry: { target: { label } } }, ]), ); const entries = collectNotApplicable(view); diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 7c1d84e498db2..dd11d9671d268 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -553,6 +553,27 @@ export interface NotApplicableEntry { readonly reason: string; } +const GATEWAY_REPORTED_NOT_APPLICABLE = /custom model provider|gateway reports/i; + +function magpieHasReadableAccount( + sources: ReadonlyArray<{ + readonly kind: string; + readonly accounts: ReadonlyArray<{ + readonly driver: string; + readonly usageLimits: { readonly unavailable?: unknown }; + }>; + }>, + driver: string, +): boolean { + return sources.some( + (source) => + source.kind === "magpie" && + source.accounts.some( + (account) => account.driver === driver && account.usageLimits.unavailable === undefined, + ), + ); +} + /** * Usage that cannot be read on its current setup (a key with no subscription, * an instance whose gateway reports for it), so a missing bar reads as "not @@ -582,6 +603,16 @@ export function collectNotApplicable( ) { continue; } + // Magpie already published this driver's cards, so the instance is not an empty row. + if ( + GATEWAY_REPORTED_NOT_APPLICABLE.test(unavailable.message) && + magpieHasReadableAccount( + presentation.serverConfig?.usageLimitSources ?? [], + provider.driver, + ) + ) { + continue; + } add( environment, provider.displayName?.trim() || String(provider.driver),