From c0447b511569d0b514605c1dc3a2cb7d33be3f03 Mon Sep 17 00:00:00 2001 From: Krarilotus Date: Wed, 18 Nov 2026 02:16:59 +0100 Subject: [PATCH 1/6] test(server): a hub account keeps its last good reading when the next read is refused --- .../src/usage/UsageLimitSources.test.ts | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index fc8a93f9e1bc6..e7baa6ac54e40 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -175,6 +175,92 @@ 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; + 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); + }).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", + email: "first@example.com", + id_token: { chatgpt_account_id: "account-a" }, + }, + ], + }), + ); + } + 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 => ({ From 2d9a3174ea3d183b80dcf5b9148e2c5739febe65 Mon Sep 17 00:00:00 2001 From: Krarilotus Date: Wed, 18 Nov 2026 02:17:00 +0100 Subject: [PATCH 2/6] fix(server): a refused hub read keeps the account's last good usage reading --- apps/server/src/usage/UsageLimitSources.ts | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index d6f867d621d23..b564781205951 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -52,6 +52,30 @@ 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) => { + const before = previous.find((candidate) => candidate.id === source.id); + if (!before) return source; + return { + ...source, + accounts: source.accounts.map((account) => { + if (account.usageLimits.unavailable?.reason !== "probeFailed") return account; + const last = before.accounts.find( + (candidate) => candidate.id === account.id && candidate.driver === account.driver, + ); + return last && !last.usageLimits.unavailable ? last : account; + }), + }; + }); +} + function missingManagementKey(config: UsageLimitSourceConfig): string { return typeof config.managementKey === "string" ? "No management key configured." @@ -131,7 +155,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 +180,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))); From 30e942b1fa4b35b68561e14630151de9aef577da Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 01:37:07 +0000 Subject: [PATCH 3/6] fix(server): a kept hub reading comes only from the same hub and login Auth file names are reused across logins and a source id can be pointed at another hub, so a refused read now borrows the last good reading only when the source endpoint and the account's email and workspace still match. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 6 +++++- apps/server/src/usage/UsageLimitSources.ts | 11 +++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index e7baa6ac54e40..f9e18ea488133 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -177,6 +177,7 @@ 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"; const readAccount = Effect.gen(function* () { const sources = yield* UsageLimitSources.make; return Effect.gen(function* () { @@ -195,6 +196,9 @@ it.effect("keeps an account's last good reading when the hub's next read of it i 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" } }); }).pipe( Effect.provide([ ServerSettings.layerTest({ @@ -229,7 +233,7 @@ it.effect("keeps an account's last good reading when the hub's next read of it i auth_index: "a", provider: "codex", email: "first@example.com", - id_token: { chatgpt_account_id: "account-a" }, + id_token: { chatgpt_account_id: workspace }, }, ], }), diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index b564781205951..1ea434c1dda53 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -61,14 +61,21 @@ function keepLastGoodReadings( next: ReadonlyArray, ): ReadonlyArray { return next.map((source) => { - const before = previous.find((candidate) => candidate.id === source.id); + // 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; const last = before.accounts.find( - (candidate) => candidate.id === account.id && candidate.driver === account.driver, + (candidate) => + candidate.id === account.id && + candidate.driver === account.driver && + candidate.email === account.email && + candidate.workspaceId === account.workspaceId, ); return last && !last.usageLimits.unavailable ? last : account; }), From 508ea27a54d32e12612df8d826dcccf12338af6b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 01:50:02 +0000 Subject: [PATCH 4/6] fix(server): fingerprinted Antigravity readings stay kept on a refused read A fingerprinted Antigravity reading carries no listed email or workspace, so the identity check now treats a field missing from the kept reading as unknown. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 1ea434c1dda53..d2ca4c8c5f1e6 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -74,8 +74,9 @@ function keepLastGoodReadings( (candidate) => candidate.id === account.id && candidate.driver === account.driver && - candidate.email === account.email && - candidate.workspaceId === account.workspaceId, + // Fingerprinted Antigravity readings omit the listed identity, so absent means unknown. + (candidate.email === undefined || candidate.email === account.email) && + (candidate.workspaceId === undefined || candidate.workspaceId === account.workspaceId), ); return last && !last.usageLimits.unavailable ? last : account; }), From 22f074f1e3714d90eeca6bbfd6f2aef9228c5c06 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 01:58:33 +0000 Subject: [PATCH 5/6] fix(server): a refused hub read never lends a fingerprinted Antigravity reading Fingerprinted Antigravity readings carry no listed email or workspace, so treating that absence as a match could lend one login's fingerprint and bars to another login under a reused auth file. Such readings are no longer kept, and identity must match exactly; a refused read of one shows as unreadable, as on main. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index d2ca4c8c5f1e6..e008b6b239834 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -74,9 +74,10 @@ function keepLastGoodReadings( (candidate) => candidate.id === account.id && candidate.driver === account.driver && - // Fingerprinted Antigravity readings omit the listed identity, so absent means unknown. - (candidate.email === undefined || candidate.email === account.email) && - (candidate.workspaceId === undefined || candidate.workspaceId === account.workspaceId), + // 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; }), From 4fb0466e81cbfffab7ed29cbdfec48e91ccf9c90 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 02:06:15 +0000 Subject: [PATCH 6/6] fix(server): a refused hub read keeps nothing for an account with no listed login An auth file that lists no email or workspace gives no way to tell a reused file name from the old login, so its refused read now shows as unreadable, as on main, instead of borrowing the previous reading. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 15 +++++++++++++-- apps/server/src/usage/UsageLimitSources.ts | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index f9e18ea488133..4a71640aa2626 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -178,6 +178,7 @@ 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* () { @@ -199,6 +200,12 @@ it.effect("keeps an account's last good reading when the hub's next read of it i // 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({ @@ -232,8 +239,12 @@ it.effect("keeps an account's last good reading when the hub's next read of it i id: "first.json", auth_index: "a", provider: "codex", - email: "first@example.com", - id_token: { chatgpt_account_id: workspace }, + ...(listsIdentity + ? { + email: "first@example.com", + id_token: { chatgpt_account_id: workspace }, + } + : {}), }, ], }), diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index e008b6b239834..ff54bb02781a9 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -70,6 +70,8 @@ function keepLastGoodReadings( ...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 &&