From 307683d62e8fe16bca2148ca9c711ade0b381c8a Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 6 Nov 2026 23:26:59 +0100 Subject: [PATCH 01/30] fix(server): proxied usage limits keep a safe reset time --- .../Adapters/CodexAdapterV2.test.ts | 16 ++ .../Adapters/CodexAdapterV2.ts | 28 ++- .../ProviderEventIngestor.test.ts | 62 ++++- .../orchestration-v2/ProviderEventIngestor.ts | 20 +- apps/server/src/provider/CodexProvider.ts | 6 +- .../src/provider/ProviderRegistry.test.ts | 2 + .../src/provider/codexUsageLimits.test.ts | 52 ++++- apps/server/src/provider/codexUsageLimits.ts | 42 ++++ apps/server/src/server.ts | 1 + .../src/usage/UsageLimitSources.test.ts | 61 ++++- apps/server/src/usage/UsageLimitSources.ts | 52 +++++ .../chat/ThreadDetailsUsage.logic.test.ts | 218 ++++++++++++++++++ .../chat/ThreadDetailsUsage.logic.ts | 86 +++++++ .../components/chat/ThreadDetailsUsage.tsx | 114 +++++++-- docs/user/usage.md | 9 +- 15 files changed, 730 insertions(+), 39 deletions(-) create mode 100644 apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts create mode 100644 apps/web/src/components/chat/ThreadDetailsUsage.logic.ts diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 84c78b91453c..c9214dae6a7a 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -5597,6 +5597,20 @@ describe("CodexAdapterV2 post-settle continuation", () => { expectedClass: "usage_limit", }, { name: "retry", code: "usageLimitExceeded", notification: true, expectedClass: "usage_limit" }, + // regression: #26574 a proxy strips the rate-limit snapshot, so the reset is only in the text + { + name: "message-reset", + code: "usageLimitExceeded", + message: "You’ve hit your usage limit. Try again at May 19th, 2033 7:20 AM.", + notification: false, + expectedClass: "usage_limit", + expectedResetAt: DateTime.formatIso( + DateTime.makeZonedUnsafe("2033-05-19T07:21:00", { + timeZone: DateTime.zoneMakeLocal(), + adjustForTimeZone: true, + }), + ), + }, { name: "exhausted-429", code: "responseTooManyFailedAttempts", @@ -5859,6 +5873,8 @@ describe("CodexAdapterV2 post-settle continuation", () => { assert.equal(terminal.failure.class, scenario.expectedClass); assert.equal(terminal.threadDisposition, "reusable"); if (scenario.name === "deferred-reset") assert.equal(terminal.failure.resetAt, resetAt); + if ("expectedResetAt" in scenario) + assert.equal(terminal.failure.resetAt, scenario.expectedResetAt); if (scenario.name === "matching-details") assert.equal(terminal.failure.message, "Detailed provider allowance explanation."); if ("resetAfter" in scenario) { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 369608a289d2..6db79261c691 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -25,6 +25,7 @@ import { import { codexRateLimitsToUpdate, mergeCodexRateLimits, + codexMessageResetAt, codexUsageLimitResetAt, type CodexRateLimitSnapshot, } from "../../provider/codexUsageLimits.ts"; @@ -1169,11 +1170,14 @@ function withCodexManagedFailure( function failureResetAt( failure: OrchestrationV2ProviderFailure, snapshot: CodexRateLimitSnapshot | undefined, + now: DateTime.Utc, + codexTimeZone: string | undefined, ) { // Sharing caps have no reset timestamp in the ordinary Codex allowance snapshot. return classifyCodexManagedError(failure.code)?.usageLimit ? null - : codexUsageLimitResetAt(snapshot); + : (codexUsageLimitResetAt(snapshot) ?? + codexMessageResetAt(failure.message, now, codexTimeZone)); } interface ActiveCodexTurnContext { @@ -1754,6 +1758,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( settings: resolvedRuntime?.config ?? adapterOptions.settings, environment: resolvedRuntime?.environment ?? adapterOptions.environment, }); + const codexTimeZone = (resolvedRuntime?.environment ?? adapterOptions.environment).TZ; const additionalContextByThread = yield* Ref.make( new Map< string, @@ -4498,7 +4503,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ); const snapshot = yield* Ref.get(rateLimitSnapshot); for (const item of (yield* Ref.get(limitedTurnItems)).values()) { - const resetAt = failureResetAt(item.failure, snapshot); + const resetAt = failureResetAt( + item.failure, + snapshot, + yield* DateTime.now, + codexTimeZone, + ); if (resetAt === null || item.failure.resetAt != null) continue; const updated = { ...item, @@ -5843,7 +5853,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( failure.class === "usage_limit" ? { ...failure, - resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot)), + resetAt: failureResetAt( + failure, + yield* Ref.get(rateLimitSnapshot), + yield* DateTime.now, + codexTimeZone, + ), } : failure, ...(input.providerRetry === undefined @@ -5883,7 +5898,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ...event.failure, resetAt: event.failure.resetAt ?? - failureResetAt(event.failure, yield* Ref.get(rateLimitSnapshot)), + failureResetAt( + event.failure, + yield* Ref.get(rateLimitSnapshot), + yield* DateTime.now, + codexTimeZone, + ), }, } : event; diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index b6adf6371fb6..476070de5db4 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -23,6 +23,7 @@ import { RunAttemptId, RunId, RuntimeRequestId, + type ServerProvider, TurnItemId, } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; @@ -45,6 +46,7 @@ import * as EventSink from "./EventSink.ts"; import * as EventStore from "./EventStore.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProjectionStore from "./ProjectionStore.ts"; +import { UsageLimitResetFallback } from "../usage/UsageLimitSources.ts"; import * as ProviderEventIngestor from "./ProviderEventIngestor.ts"; import * as ProviderUsageAnalytics from "../telemetry/ProviderUsageAnalytics.ts"; import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts"; @@ -262,14 +264,17 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit }, snapshot: { refreshUsage: Deferred.succeed(refreshed, undefined).pipe(Effect.asVoid), - getSnapshot: Effect.die("unused"), + getSnapshot: Effect.sync(() => own as ServerProvider), refresh: Effect.die("unused"), resolveMaintenance: () => Effect.die("unused"), applyUsageLimits: () => Effect.die("unused"), streamChanges: Stream.empty, }, }; + let own: Partial | undefined; const lookups: ProviderInstanceId[] = []; + const hubLookups: string[] = []; + const hubResetAt = "2033-05-19T07:20:00.000Z"; const instances = Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ getInstance: (id) => Effect.sync(() => { @@ -292,7 +297,36 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit providerInstanceId: instance.instanceId, threadId: threadEvent.threadId, }; - for (const failureClass of ["validation_error", "usage_limit"] as const) { + const unread = { checkedAt: now.toString(), windows: [] }; + const cases = [ + { failureClass: "validation_error", provider: undefined, resetAt: undefined }, + { + failureClass: "usage_limit", + provider: { + auth: { status: "authenticated" }, + usageLimits: { ...unread, unavailable: { reason: "probeFailed" } }, + }, + resetAt: undefined, + }, + { + failureClass: "usage_limit", + provider: { + auth: { status: "authenticated", type: "customEndpoint" }, + usageLimits: { ...unread, unavailable: { reason: "probeFailed" } }, + }, + resetAt: hubResetAt, + }, + { + failureClass: "usage_limit", + provider: { + auth: { status: "unknown" }, + usageLimits: { ...unread, unavailable: { reason: "unsupported" } }, + }, + resetAt: hubResetAt, + }, + ] as const; + for (const [index, { failureClass, provider, resetAt }] of cases.entries()) { + own = provider; const stored = yield* ingestor.ingestNormalized({ ...input, event: { @@ -304,7 +338,7 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit }), providerTurnId: idAllocator.derive.providerTurn({ driver: instance.driverKind, - nativeTurnId: failureClass, + nativeTurnId: String(index), }), runOrdinal: 1, failureItemOrdinal: 1, @@ -318,10 +352,28 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit }); assert.equal(stored.length, 1); if (failureClass === "validation_error") assert.deepEqual(lookups, []); + // regression: #26574 a proxied CLI drops the reset, so the hub's pool refill fills it + const item = stored[0]?.event; + assert.equal( + item?.type === "turn-item.updated" && item.payload.type === "error" + ? item.payload.failure.resetAt + : undefined, + resetAt, + ); } - assert.deepEqual(lookups, [instance.instanceId]); + assert.deepEqual(lookups, Array(6).fill(instance.instanceId)); + assert.deepEqual(hubLookups, [instance.driverKind, instance.driverKind]); yield* Deferred.await(refreshed); - }).pipe(Effect.provide(makeLayerTest(layerTestEventSink, instances))); + }).pipe( + Effect.provide(makeLayerTest(layerTestEventSink, instances)), + Effect.provideService(UsageLimitResetFallback, { + resetAt: (driver) => + Effect.sync(() => { + hubLookups.push(driver); + return hubResetAt; + }), + }), + ); }), ), ); diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index f27580d909af..72805557f2d7 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -33,6 +33,7 @@ import { } from "../telemetry/ProviderUsageAnalytics.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; +import { UsageLimitResetFallback } from "../usage/UsageLimitSources.ts"; import * as EventSink from "./EventSink.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; @@ -255,6 +256,7 @@ export const layer: Layer.Layer< const analytics = yield* ProviderTurnAnalytics; const agentScope = yield* AgentScope; const instances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; + const usageLimitResets = yield* UsageLimitResetFallback; const scope = yield* Effect.scope; const completedTurnAnalytics = new Set(); @@ -627,9 +629,25 @@ export const layer: Layer.Layer< return dismissed; } const occurredAt = yield* DateTime.now; - const failure = (yield* agentScope.oomKilled(input.threadId)) + const reported = (yield* agentScope.oomKilled(input.threadId)) ? { ...input.event.failure, message: "Killed: out of memory.", code: "oom_kill" } : input.event.failure; + // A proxy's CLI can drop the reset time; its hub still knows when the pool refills. + const provider = + reported.class === "usage_limit" && reported.resetAt == null + ? yield* Effect.flatMap( + instances.getInstance(input.providerInstanceId), + (instance) => instance?.snapshot.getSnapshot ?? Effect.undefined, + ) + : undefined; + // A direct login whose own probe failed is not served by the hub. + const proxied = + provider?.auth.type === "customEndpoint" || + provider?.usageLimits?.unavailable?.reason === "unsupported"; + const hubResetAt = proxied + ? yield* usageLimitResets.resetAt(input.event.driver) + : null; + const failure = hubResetAt === null ? reported : { ...reported, resetAt: hubResetAt }; return [ ...dismissed, yield* makeDomainEvent(input, { diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 2d546b36a9b1..b0eaba2972dd 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -573,7 +573,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun ), // Usage is an enrichment: a failure or a slow answer degrades to "no // usage this probe" rather than costing the account and models. - input.skipNativeUsage + input.skipNativeUsage || !accountResponse.account ? Effect.undefined : withCodexDiscoveryDeadline( client.request("account/rateLimits/read", null), @@ -842,7 +842,9 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu ? { status: "ready" as const, auth: managedAuth, message: undefined } : accountProbeStatus(snapshot); const usageLimits = - snapshot.account.account?.type === "apiKey" + // A custom model provider has no subscription for Codex to read. + snapshot.account.account?.type === "apiKey" || + (!managedAuth && !snapshot.account.account && !snapshot.account.requiresOpenaiAuth) ? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" }) : snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits ? makeUnavailableUsageLimits({ diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 9b1187493967..0e4d297f216c 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -478,6 +478,8 @@ it.layer( assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.status, "unknown"); + // regression: #11873 a custom provider has no subscription to read, which is not a failure + assert.deepStrictEqual(status.usageLimits?.unavailable, { reason: "unsupported" }); }), ); diff --git a/apps/server/src/provider/codexUsageLimits.test.ts b/apps/server/src/provider/codexUsageLimits.test.ts index 9c863f54c086..a6cd0fa8d1a1 100644 --- a/apps/server/src/provider/codexUsageLimits.test.ts +++ b/apps/server/src/provider/codexUsageLimits.test.ts @@ -1,4 +1,5 @@ import * as CodexErrors from "effect-codex-app-server/errors"; +import * as DateTime from "effect/DateTime"; import { describe, expect, it } from "vite-plus/test"; import { @@ -261,7 +262,7 @@ describe("mergeCodexRateLimits", () => { }); }); -import { codexUsageLimitResetAt } from "./codexUsageLimits.ts"; +import { codexMessageResetAt, codexUsageLimitResetAt } from "./codexUsageLimits.ts"; describe("codexUsageLimitResetAt", () => { it("waits for every exhausted window and never invents an unknown reset", () => { @@ -277,3 +278,52 @@ describe("codexUsageLimitResetAt", () => { ).toBeNull(); }); }); + +describe("codexMessageResetAt", () => { + const local = (wallClock: string) => + DateTime.makeZonedUnsafe(wallClock, { + timeZone: DateTime.zoneMakeLocal(), + adjustForTimeZone: true, + }); + const now = DateTime.toUtc(local("2026-11-06T09:00:00")); + it.each([ + [ + "Plus", + "You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits or try again at Nov 11th, 2026 3:37 PM.", + "2026-11-11T15:38:00", + ], + ["today", "You’ve hit your usage limit. Try again at 12:05 PM.", "2026-11-06T12:06:00"], + [ + "midnight", + "You’ve hit your usage limit. Try again at Dec 1st, 2026 12:00 AM.", + "2026-12-01T00:01:00", + ], + ])("reads the local reset Codex words for %s, waiting out its minute", (_, message, expected) => { + expect(codexMessageResetAt(message, now, undefined)).toBe(DateTime.formatIso(local(expected))); + }); + + it("reads the wall time in the Codex process zone and never resolves a repeated hour early", () => { + const fallBack = DateTime.makeUnsafe("2026-11-01T05:10:00Z"); + const message = "You’ve hit your usage limit. Try again at 1:30 AM."; + expect(codexMessageResetAt(message, fallBack, "America/New_York")).toBe( + "2026-11-01T06:31:00.000Z", + ); + expect(codexMessageResetAt(message, fallBack, ":UTC")).toBeNull(); + }); + + it("ignores messages without a future reset", () => { + expect( + codexMessageResetAt("You’ve hit your usage limit. Try again later.", now, undefined), + ).toBeNull(); + expect( + codexMessageResetAt( + "exceeded retry limit, last status: 429 Too Many Requests", + now, + undefined, + ), + ).toBeNull(); + expect( + codexMessageResetAt("You’ve hit your usage limit. Try again at 8:15 AM.", now, undefined), + ).toBeNull(); + }); +}); diff --git a/apps/server/src/provider/codexUsageLimits.ts b/apps/server/src/provider/codexUsageLimits.ts index d4f9c621119d..7f9399e4bbe0 100644 --- a/apps/server/src/provider/codexUsageLimits.ts +++ b/apps/server/src/provider/codexUsageLimits.ts @@ -201,3 +201,45 @@ export function codexUsageLimitResetAt( null, ); } + +const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; + +/** + * Codex words a usage limit's reset in its own local time, to the minute, and omits the date for + * today. A proxy that strips the rate-limit snapshot leaves this text as the only reset time. + */ +export function codexMessageResetAt( + message: string, + now: DateTime.Utc, + codexTimeZone: string | undefined, +): string | null { + const match = + /try again at (?:([a-z]{3}) (\d{1,2})(?:st|nd|rd|th), (\d{4}) )?(\d{1,2}):(\d{2}) ([ap]m)\b/i.exec( + message, + ); + if (!match) return null; + const [, month, day, year, hour, minute, meridiem] = match; + const monthIndex = month ? MONTHS.indexOf(month.toLowerCase()) : 0; + if (monthIndex < 0) return null; + const timeZone = Option.getOrElse( + DateTime.zoneFromString(codexTimeZone?.replace(/^:/, "") ?? ""), + DateTime.zoneMakeLocal, + ); + const { weekDay: _, ...today } = DateTime.toParts(DateTime.makeZonedUnsafe(now, { timeZone })); + const reset = DateTime.makeZonedUnsafe( + { + ...today, + ...(year ? { year: Number(year), month: monthIndex + 1, day: Number(day) } : {}), + hour: (Number(hour) % 12) + (meridiem!.toLowerCase() === "pm" ? 12 : 0), + minute: Number(minute), + second: 0, + millisecond: 0, + }, + // A wall time repeated by a DST change resolves to its later instant, so recovery never runs early. + { timeZone, adjustForTimeZone: true, disambiguation: "later" }, + ).pipe( + // The minute is truncated, so wait it out rather than resume early. + DateTime.add({ minutes: 1 }), + ); + return DateTime.isGreaterThan(reset, now) ? DateTime.formatIso(reset) : null; +} diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 5c9ab9c433ff..aaaa0f333ef9 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -566,6 +566,7 @@ const layerScheduledTaskWebhookOrigin = Layer.effect( ); const layerOrchestrationV2Runtime = RuntimeLayer.layerProduction.pipe( + Layer.provide(UsageLimitSources.layerResetFallback), Layer.provide(layerScheduledTaskWebhookOrigin), Layer.provide(ProviderUsageAnalytics.layer), Layer.provide(layerCheckpointStore), diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 274e63b46c90..cda6780016e2 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -1,6 +1,11 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import { expect, it } from "@effect/vitest"; -import { SecretProviderId, ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"; +import { + ProviderDriverKind, + SecretProviderId, + ServerConfigStreamEvent, + UsageLimitSourceId, +} from "@t3tools/contracts"; import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -157,3 +162,57 @@ it.effect("reads a management key from its secret provider", () => { Effect.scoped, ); }); + +it("waits for the first refill only when every pooled account of the driver is spent", () => { + const now = Date.parse("2026-11-06T12:00:00.000Z"); + const claude = ProviderDriverKind.make("claudeAgent"); + const account = (id: string, windows: ReadonlyArray) => ({ + id, + driver: claude, + usageLimits: { checkedAt: "2026-11-06T12:00:00.000Z", windows }, + }); + const session = (usedPercent: number, resetsAt: string) => ({ + id: "five_hour", + kind: "session", + label: "Session", + usedPercent, + resetsAt, + }); + const hub = (accounts: ReadonlyArray>) => + [ + { + id: UsageLimitSourceId.make("hub"), + kind: "cliproxy", + label: "Hub", + checkedAt: "", + accounts, + }, + ] as never; + const spent = [ + account("a", [session(100, "2026-11-06T14:00:00.000Z")]), + account("b", [ + session(100, "2026-11-06T13:00:00.000Z"), + { + id: "seven_day_fable", + kind: "weekly", + label: "Weekly · Fable", + usedPercent: 100, + resetsAt: "2026-11-10T00:00:00.000Z", + }, + ]), + ]; + expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, now)).toBe( + "2026-11-06T14:00:00.000Z", + ); + const withRoom = [...spent, account("c", [session(40, "2026-11-06T15:00:00.000Z")])]; + expect(UsageLimitSources.hubPoolResetAt(hub(withRoom), claude, now)).toBeNull(); + const unread = { + id: "d", + driver: claude, + usageLimits: { checkedAt: "", windows: [], unavailable: { reason: "probeFailed" } }, + }; + expect(UsageLimitSources.hubPoolResetAt(hub([...spent, unread]), claude, now)).toBeNull(); + expect( + UsageLimitSources.hubPoolResetAt(hub(spent), ProviderDriverKind.make("codex"), now), + ).toBeNull(); +}); diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 7d40b0fb9ece..8f867dd29e51 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -3,6 +3,7 @@ import { UsageLimitSourceError, type UsageLimitSourceConsumeResetCreditInput, type ProviderConsumeResetCreditResult, + type ProviderDriverKind, type ServerSettings, UsageLimitSourceConfig, settingsSecretLiteral, @@ -198,3 +199,54 @@ export const make = Effect.gen(function* () { export const layer = Layer.effect(UsageLimitSources, make).pipe( Layer.provide(SecretResolverLayer.layer), ); + +/** A hub returns a usage limit only when every account it pools for the driver is spent. */ +export function hubPoolResetAt( + sources: ReadonlyArray, + driver: ProviderDriverKind, + now: number, +): string | null { + let earliest: number | null = null; + for (const account of sources.flatMap((source) => source.accounts)) { + if (account.driver !== driver) continue; + // An unread account may be the one still blocked, so its refill is unknown. + if (account.usageLimits.unavailable) return null; + const spent = account.usageLimits.windows.filter((window) => window.usedPercent >= 100); + if (spent.length === 0) return null; + const resets = spent.map((window) => Date.parse(window.resetsAt ?? "")); + if (resets.some((reset) => !Number.isFinite(reset))) return null; + const recoversAt = Math.max(...resets); + if (recoversAt <= now) return null; + earliest = earliest === null ? recoversAt : Math.min(earliest, recoversAt); + } + return earliest === null ? null : DateTime.formatIso(DateTime.makeUnsafe(earliest)); +} + +export class UsageLimitResetFallback extends Context.Reference<{ + readonly resetAt: (driver: ProviderDriverKind) => Effect.Effect; +}>("t3/usage/UsageLimitResetFallback", { + defaultValue: () => ({ resetAt: () => Effect.succeed(null) }), +}) {} + +export const layerResetFallback = Layer.effect( + UsageLimitResetFallback, + Effect.gen(function* () { + const sources = yield* UsageLimitSources; + return { + resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver) { + const servesDriver = (snapshots: ReadonlyArray) => + snapshots.filter((source) => + source.accounts.some((account) => account.driver === driver), + ); + // Two hubs for one driver leave no way to tell which one served the failed turn. + if (servesDriver(yield* sources.current).length !== 1) return null; + // The terminal waits on this, so a slow hub falls back to its last reading. + yield* sources.refresh.pipe(Effect.timeoutOption("5 seconds")); + const hubs = servesDriver(yield* sources.current); + return hubs.length === 1 + ? hubPoolResetAt(hubs, driver, DateTime.toEpochMillis(yield* DateTime.now)) + : null; + }), + }; + }), +); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts new file mode 100644 index 000000000000..c4863aad5a72 --- /dev/null +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -0,0 +1,218 @@ +import { + ProviderDriverKind, + ProviderInstanceId, + type ServerProvider, + type ServerProviderUsageLimits, + type UsageLimitSourceSnapshot, + UsageLimitSourceId, +} from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { nextPoolReset, poolSources, resolveThreadDetailsLimits } from "./ThreadDetailsUsage.logic"; + +const now = Date.parse("2026-10-30T12:00:00.000Z"); +const checkedAt = "2026-10-30T11:59:00.000Z"; +const claude = ProviderDriverKind.make("claudeAgent"); +const codex = ProviderDriverKind.make("codex"); +const instanceId = ProviderInstanceId.make("claudeAgent"); + +function limitsAt(usedPercent: number, at = checkedAt): ServerProviderUsageLimits { + return { + checkedAt: at, + windows: [ + { + id: "five_hour", + kind: "session", + label: "Session", + usedPercent, + windowDurationMins: 300, + resetsAt: "2026-10-30T13:17:00.000Z", + }, + ], + }; +} + +function limitsResettingAt(usedPercent: number, resetsAt: string): ServerProviderUsageLimits { + const [window] = limitsAt(usedPercent).windows; + return { checkedAt, windows: [{ ...window!, resetsAt }] }; +} + +const unsupported: ServerProviderUsageLimits = { + checkedAt, + windows: [], + unavailable: { reason: "unsupported" }, +}; + +function claudeInstance(usageLimits: ServerProviderUsageLimits): ServerProvider { + return { + instanceId, + driver: claude, + enabled: true, + installed: true, + version: null, + status: "ready", + auth: { status: "authenticated", type: "customEndpoint", label: "Custom API endpoint" }, + checkedAt, + models: [], + slashCommands: [], + skills: [], + usageLimits, + }; +} + +function signedIn(id: string, email: string, usageLimits: ServerProviderUsageLimits) { + const base = claudeInstance(usageLimits); + return { + ...base, + instanceId: ProviderInstanceId.make(id), + auth: { ...base.auth, email }, + } satisfies ServerProvider; +} + +interface HubAccountInput { + readonly id: string; + readonly email?: string; + readonly driver?: ProviderDriverKind; + readonly limits: ServerProviderUsageLimits; +} + +function hub( + label: string, + accounts: readonly HubAccountInput[], + error?: string, +): UsageLimitSourceSnapshot { + return { + id: UsageLimitSourceId.make(label), + kind: "cliproxy", + label, + checkedAt, + accounts: accounts.map((account) => ({ + id: account.id, + driver: account.driver ?? claude, + ...(account.email ? { email: account.email } : {}), + usageLimits: account.limits, + })), + ...(error ? { error } : {}), + }; +} + +function shown( + own: ServerProviderUsageLimits, + sources: readonly UsageLimitSourceSnapshot[], + natives: readonly ServerProvider[] = [], +) { + const resolved = resolveThreadDetailsLimits( + instanceId, + [claudeInstance(own), ...natives], + sources, + now, + ); + if (resolved?.kind !== "pool") return resolved; + return { + kind: resolved.kind, + rows: resolved.windows.map((window) => ({ + label: window.label, + left: window.remainingPercent, + accounts: window.members.length, + from: poolSources(window), + })), + }; +} + +describe("resolveThreadDetailsLimits", () => { + it("shows nothing without a selected, known instance, or before its first reading", () => { + expect(resolveThreadDetailsLimits(null, [claudeInstance(unsupported)], [], now)).toBeNull(); + expect( + resolveThreadDetailsLimits(ProviderInstanceId.make("gone"), [], [hub("Hub", [])], now), + ).toBeNull(); + const { usageLimits: _unread, ...unread } = claudeInstance(unsupported); + const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); + expect(resolveThreadDetailsLimits(instanceId, [unread], [readableHub], now)).toBeNull(); + }); + + it("keeps an instance's own readable limits over any hub", () => { + const own = limitsAt(40); + expect(shown(own, [hub("Hub", [{ id: "a.json", limits: limitsAt(90) }])])).toEqual({ + kind: "instance", + limits: own, + }); + }); + + it.each([ + { + name: "several accounts, pooled like Usage → Limits", + sources: [ + hub("Hub", [ + { id: "a.json", email: "a@example.com", limits: limitsAt(20) }, + { id: "b.json", email: "b@example.com", limits: limitsAt(60) }, + ]), + ], + rows: [{ label: "Session", left: 60, accounts: 2, from: ["Hub"] }], + }, + { + name: "accounts across hubs, skipping a hub that failed to read", + sources: [ + hub("Home", [{ id: "a.json", email: "a@example.com", limits: limitsAt(20) }]), + hub("Work", [{ id: "b.json", email: "b@example.com", limits: limitsAt(40) }]), + hub("Down", [], "The hub answered with HTTP 404."), + ], + rows: [{ label: "Session", left: 70, accounts: 2, from: ["Home", "Work"] }], + }, + { + name: "only this driver's readable accounts", + sources: [ + hub("Hub", [ + { id: "a.json", email: "a@example.com", limits: limitsAt(50) }, + { id: "c.json", email: "c@example.com", driver: codex, limits: limitsAt(0) }, + { id: "d.json", email: "d@example.com", limits: unsupported }, + ]), + ], + rows: [{ label: "Session", left: 50, accounts: 1, from: ["Hub"] }], + }, + { + name: "a login signed in natively and on a hub, counted once at its freshest reading", + sources: [hub("Hub", [{ id: "a.json", email: "a@example.com", limits: limitsAt(90) }])], + natives: [signedIn("claude-a", "a@example.com", limitsAt(30, "2026-10-30T11:59:30.000Z"))], + rows: [{ label: "Session", left: 70, accounts: 1, from: ["Signed-in accounts"] }], + }, + ])("pools $name when the instance reads no limits", ({ sources, natives, rows }) => { + expect(shown(unsupported, sources, natives)).toEqual({ kind: "pool", rows }); + }); + + it("names the next reset that refills quota, skipping an unused account's 0% reset", () => { + const soon = "2026-10-30T12:10:00.000Z"; + const later = "2026-10-30T14:00:00.000Z"; + const resolved = resolveThreadDetailsLimits( + instanceId, + [claudeInstance(unsupported)], + [ + hub("Hub", [ + { id: "a.json", email: "a@example.com", limits: limitsResettingAt(0, soon) }, + { id: "b.json", email: "b@example.com", limits: limitsResettingAt(80, later) }, + ]), + ], + now, + ); + if (resolved?.kind !== "pool") throw new Error("expected a pool"); + const [window] = resolved.windows; + expect(window!.resets.map((reset) => reset.restoresPercent)).toEqual([0, 40]); + expect(nextPoolReset(window!, now)).toMatchObject({ + at: Date.parse(later), + restoresPercent: 40, + }); + }); + + it("keeps the instance's notice when no hub has a readable account for its driver", () => { + const sources = [ + hub("Hub", [ + { id: "c.json", driver: codex, limits: limitsAt(10) }, + { id: "d.json", limits: unsupported }, + ]), + ]; + const natives = [signedIn("claude-b", "b@example.com", limitsAt(60))]; + expect(shown(unsupported, sources, natives)).toEqual({ + kind: "instance", + limits: unsupported, + }); + }); +}); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts new file mode 100644 index 000000000000..65c77743ee33 --- /dev/null +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -0,0 +1,86 @@ +import { + EnvironmentId, + type ProviderInstanceId, + type ServerProvider, + type ServerProviderUsageLimits, + type UsageLimitSourceSnapshots, +} from "@t3tools/contracts"; +import { + collectLimitAccounts, + collectLimitPools, + collectProviderUsageLimits, + displayLimitWindows, + type LimitPoolWindow, + limitsNotice, +} from "@t3tools/shared/usageLimits"; + +export type ThreadDetailsLimits = + | { readonly kind: "instance"; readonly limits: ServerProviderUsageLimits } + | { readonly kind: "pool"; readonly windows: readonly LimitPoolWindow[] }; + +// collectLimitAccounts keys its input by environment; the card reads one, so any id serves. +const CARD_ENVIRONMENT = EnvironmentId.make("thread-details"); + +export function resolveThreadDetailsLimits( + providerInstanceId: ProviderInstanceId | null, + providers: readonly ServerProvider[], + sources: UsageLimitSourceSnapshots, + now: number, +): ThreadDetailsLimits | null { + const provider = providers.find((entry) => entry.instanceId === providerInstanceId); + if (!provider) return null; + const report = collectProviderUsageLimits(provider.instanceId, providers, sources, now); + // Only this instance's data: the report keeps one instance per login, and any other entry may be + // a different login or workspace, so fall back to the instance's own snapshot. + const own = + report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ?? + provider.usageLimits; + if (!own) return null; + if (limitsNotice(own) === null) return { kind: "instance", limits: own }; + // A proxied instance reads no limits of its own; its hub pools the accounts it spends. + return driverPool(provider.driver, providers, sources, now) ?? { kind: "instance", limits: own }; +} + +export function nextPoolReset(window: LimitPoolWindow, now: number) { + const pooled = window.members.length > 1; + return ( + window.resets.find((reset) => reset.at > now && (!pooled || reset.restoresPercent > 0)) ?? null + ); +} + +function driverPool( + driver: ServerProvider["driver"], + providers: readonly ServerProvider[], + sources: UsageLimitSourceSnapshots, + now: number, +): ThreadDetailsLimits | null { + const hubReadsDriver = sources.some((source) => + source.accounts.some( + (account) => account.driver === driver && limitsNotice(account.usageLimits) === null, + ), + ); + if (!hubReadsDriver) return null; + const presentations = new Map([ + [ + CARD_ENVIRONMENT, + { + entry: { target: { label: "" } }, + serverConfig: { providers, usageLimitSources: sources }, + }, + ], + ]); + const accounts = collectLimitAccounts(presentations).filter( + (account) => account.driver === driver, + ); + const [pool] = collectLimitPools(accounts, now); + if (!pool) return null; + return { kind: "pool", windows: displayLimitWindows(pool) }; +} + +export function poolSources(window: LimitPoolWindow): string[] { + const hubs = window.members.flatMap(({ account }) => + account.environments.length === 0 && account.sourceLabel ? [account.sourceLabel] : [], + ); + const signedIn = window.members.some(({ account }) => account.environments.length > 0); + return [...new Set(hubs), ...(signedIn ? ["Signed-in accounts"] : [])]; +} diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index f3ee87e1b7f7..1a60f43f6c35 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -1,13 +1,14 @@ import type { ProviderInstanceId, ServerProvider, + ServerProviderUsageLimits, ServerProviderUsageWindow, UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { - collectProviderUsageLimits, formatDuration, type LimitPace, + type LimitPoolWindow, limitsNotice, paceOf, remainingPercent, @@ -23,6 +24,9 @@ import { formatUpcomingTimestamp } from "../../timestampFormat"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { formatContextWindowCompactionMessage } from "./ContextWindowMeter.logic"; import { ThreadDetailsSection } from "./ThreadDetailsSection"; +import { nextPoolReset, poolSources, resolveThreadDetailsLimits } from "./ThreadDetailsUsage.logic"; + +const MINUTE_MS = 60_000; const PACE_LABEL: Record = { ahead: "Ahead of pace", @@ -39,6 +43,12 @@ export interface ThreadDetailsUsageInput { type UsageTone = "error" | "warning" | null; +function limitTone(left: number): UsageTone { + if (left <= 10) return "error"; + if (left <= 20) return "warning"; + return null; +} + /** The selected provider's limit windows. Each row reads as room left. */ export function ThreadDetailsUsage({ providerInstanceId, @@ -47,17 +57,8 @@ export function ThreadDetailsUsage({ }: Omit) { const nowMinute = useNowMinute(); const now = Date.parse(`${nowMinute}:00Z`); - const report = - providerInstanceId !== null - ? collectProviderUsageLimits(providerInstanceId, providers, sources, now) - : null; - // Only this instance's data: the report keeps one instance per login, and any other entry may be - // a different login or workspace, so fall back to the instance's own snapshot. - const limits = - report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ?? - providers.find((provider) => provider.instanceId === providerInstanceId)?.usageLimits; - if (!limits) return null; - const notice = limitsNotice(limits); + const resolved = resolveThreadDetailsLimits(providerInstanceId, providers, sources, now); + if (!resolved) return null; return (
- {notice ? ( - {notice}} - /> + {resolved.kind === "pool" ? ( + resolved.windows.map((window) => ) ) : ( - limits.windows.map((window) => ) + )}
); } +function InstanceRows({ + limits, + now, +}: { + readonly limits: ServerProviderUsageLimits; + readonly now: number; +}) { + const notice = limitsNotice(limits); + if (notice) { + return ( + {notice}} + /> + ); + } + return limits.windows.map((window) => ); +} + /** The thread's context window. The bar fills as context is used; the tick marks auto-compaction. */ export function ThreadDetailsContext({ usage }: { readonly usage: ContextWindowSnapshot }) { const maxTokens = usage.maxTokens != null && usage.maxTokens > 0 ? usage.maxTokens : null; @@ -153,7 +170,7 @@ function LimitRow({ + + {window.label}: {left}% left + {accounts > 1 ? ` across ${accounts} accounts` : ""} + + + {window.pace ? ( + {PACE_LABEL[window.pace]} + ) : null} + {from.length > 0 ? ( + From {from.join(", ")} + ) : null} + + } + /> + ); +} + +function PoolResetLine({ + window, + now, +}: { + readonly window: LimitPoolWindow; + readonly now: number; +}) { + const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat); + const next = nextPoolReset(window, now); + if (!next) return null; + const resetsAt = next.member.window.resetsAt; + if (!resetsAt) return null; + const remaining = next.at - now; + const when = remaining < MINUTE_MS ? "momentarily" : `in ${formatDuration(remaining)}`; + const restores = window.members.length > 1 ? next.restoresPercent : null; + const label = restores === null ? `Resets ${when}` : `+${restores}% ${when}`; + return ( + + {label} · {formatUpcomingTimestamp(resetsAt, timestampFormat, now)} + + ); +} + /** One label, hairline, and value row; the whole row opens the details tooltip. */ function UsageRow({ label, diff --git a/docs/user/usage.md b/docs/user/usage.md index d25bdba81f00..5dadf3fde08c 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -150,9 +150,12 @@ anything. The command is offered only for providers that appear under **Usage To keep them in view on web and desktop, turn on **Usage limits** in **Settings → Appearance → Thread details**, or right-click the thread details card. The bottom of the thread details card then shows how much is left in each limit window of the current provider account. Some windows, such as a weekly Opus limit, count only one model. Hover a -row to see when it resets and your pace. Turn on **Context window** to also show the thread's -context use against the model's limit. A tick on the bar marks where the provider compacts -automatically, when it reports that point. +row to see when it resets and your pace. A provider that sends requests through a CLIProxyAPI hub +reports no limits of its own, so the card shows that provider's accounts pooled, the hub's and +any signed-in ones, from this environment only. **Usage → Limits** pools every connected +environment. Turn on **Context window** to also show the thread's context use against the +model's limit. A tick on the bar marks where the provider compacts automatically, when it reports +that point. OpenCode Go reports its session, weekly, and monthly allowance when OpenCode runs locally in the environment. T3 cannot report limits for external OpenCode servers because their credentials From 0c94651d60de0e09449d804772d53d91e1962422 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 6 Nov 2026 23:50:38 +0100 Subject: [PATCH 02/30] fix(web): a direct login's failed reading no longer shows an unrelated hub's limits --- .../chat/ThreadDetailsUsage.logic.test.ts | 14 ++++++++++++++ .../components/chat/ThreadDetailsUsage.logic.ts | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index c4863aad5a72..a1767e361b2b 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -130,6 +130,20 @@ describe("resolveThreadDetailsLimits", () => { expect(resolveThreadDetailsLimits(instanceId, [unread], [readableHub], now)).toBeNull(); }); + it("keeps a direct login's failed reading instead of an unrelated hub", () => { + const failed: ServerProviderUsageLimits = { + checkedAt, + windows: [], + unavailable: { reason: "probeFailed" }, + }; + const direct = { ...claudeInstance(failed), auth: { status: "authenticated" as const } }; + const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); + expect(resolveThreadDetailsLimits(instanceId, [direct], [readableHub], now)).toEqual({ + kind: "instance", + limits: failed, + }); + }); + it("keeps an instance's own readable limits over any hub", () => { const own = limitsAt(40); expect(shown(own, [hub("Hub", [{ id: "a.json", limits: limitsAt(90) }])])).toEqual({ diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index 65c77743ee33..aae5a9ceafb8 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -36,7 +36,9 @@ export function resolveThreadDetailsLimits( report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ?? provider.usageLimits; if (!own) return null; - if (limitsNotice(own) === null) return { kind: "instance", limits: own }; + const proxied = + provider.auth.type === "customEndpoint" || own.unavailable?.reason === "unsupported"; + if (limitsNotice(own) === null || !proxied) return { kind: "instance", limits: own }; // A proxied instance reads no limits of its own; its hub pools the accounts it spends. return driverPool(provider.driver, providers, sources, now) ?? { kind: "instance", limits: own }; } From a4073d124d077851c4bf933110603026f79ebe5e Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:00:25 +0100 Subject: [PATCH 03/30] fix(server): an empty Codex TZ reads reset times as UTC --- apps/server/src/provider/codexUsageLimits.test.ts | 1 + apps/server/src/provider/codexUsageLimits.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/server/src/provider/codexUsageLimits.test.ts b/apps/server/src/provider/codexUsageLimits.test.ts index a6cd0fa8d1a1..990bd07cec90 100644 --- a/apps/server/src/provider/codexUsageLimits.test.ts +++ b/apps/server/src/provider/codexUsageLimits.test.ts @@ -309,6 +309,7 @@ describe("codexMessageResetAt", () => { "2026-11-01T06:31:00.000Z", ); expect(codexMessageResetAt(message, fallBack, ":UTC")).toBeNull(); + expect(codexMessageResetAt(message, fallBack, "")).toBeNull(); }); it("ignores messages without a future reset", () => { diff --git a/apps/server/src/provider/codexUsageLimits.ts b/apps/server/src/provider/codexUsageLimits.ts index 7f9399e4bbe0..75101bde4d34 100644 --- a/apps/server/src/provider/codexUsageLimits.ts +++ b/apps/server/src/provider/codexUsageLimits.ts @@ -221,10 +221,14 @@ export function codexMessageResetAt( const [, month, day, year, hour, minute, meridiem] = match; const monthIndex = month ? MONTHS.indexOf(month.toLowerCase()) : 0; if (monthIndex < 0) return null; - const timeZone = Option.getOrElse( - DateTime.zoneFromString(codexTimeZone?.replace(/^:/, "") ?? ""), - DateTime.zoneMakeLocal, - ); + const timeZone = + codexTimeZone === undefined + ? DateTime.zoneMakeLocal() + : // An empty TZ means UTC on Unix. + Option.getOrElse( + DateTime.zoneFromString(codexTimeZone.replace(/^:/, "") || "UTC"), + DateTime.zoneMakeLocal, + ); const { weekDay: _, ...today } = DateTime.toParts(DateTime.makeZonedUnsafe(now, { timeZone })); const reset = DateTime.makeZonedUnsafe( { From ef61c025348ac705ca7a1745c7d111c84560ed0f Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:35:13 +0100 Subject: [PATCH 04/30] fix(server): hub limits follow the accounts that serve the thread's model A CLIProxyAPI hub routes a model to the accounts that list it, which can belong to another driver: a Claude Code instance running GPT models spends the hub's Codex accounts. The reset fallback, the thread usage card, and the /usage-limits report now read each account's routed models and match the thread's model, falling back to the driver when the hub lists none. Co-Authored-By: Claude Opus 5.5 --- .../src/features/threads/ThreadComposer.tsx | 1 + .../features/threads/ThreadDetailScreen.tsx | 1 + .../ProviderEventIngestor.test.ts | 9 ++-- .../orchestration-v2/ProviderEventIngestor.ts | 13 ++++-- .../src/usage/UsageLimitSources.test.ts | 19 +++++--- apps/server/src/usage/UsageLimitSources.ts | 31 +++++++------ apps/server/src/usage/cliproxyApi.test.ts | 22 ++++++++++ apps/server/src/usage/cliproxyApi.ts | 21 +++++++++ apps/web/src/components/ChatView.tsx | 6 +++ .../chat/ThreadDetailsUsage.logic.test.ts | 30 ++++++++++--- .../chat/ThreadDetailsUsage.logic.ts | 43 +++++++++++-------- .../components/chat/ThreadDetailsUsage.tsx | 4 +- .../components/chat/useUsageLimitsPanel.ts | 6 ++- docs/user/usage.md | 5 ++- packages/contracts/src/providerUsageLimits.ts | 2 + packages/shared/src/usageLimits.test.ts | 26 +++++++++++ packages/shared/src/usageLimits.ts | 36 ++++++++++++++-- 17 files changed, 219 insertions(+), 56 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index 57bda9aa98c9..872facb33dee 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -531,6 +531,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps props.serverConfig?.providers ?? [], props.serverConfig?.usageLimitSources ?? [], Date.now(), + currentModelSelection.model, ); onShowUsageLimits(report); if (!report) { diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 27cce9c5795b..87e8bb469db1 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -981,6 +981,7 @@ function ThreadDetailBody( props.serverConfig?.providers ?? [], props.serverConfig?.usageLimitSources ?? [], usageLimitsPanel.now, + props.selectedThread.modelSelection.model, ) : null; const showUsageLimits = (report: UsageLimitsReport | null) => diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 476070de5db4..186d2e9ce341 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -362,14 +362,17 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit ); } assert.deepEqual(lookups, Array(6).fill(instance.instanceId)); - assert.deepEqual(hubLookups, [instance.driverKind, instance.driverKind]); + assert.deepEqual( + hubLookups, + Array(2).fill(`${instance.driverKind}:${modelSelection.model}`), + ); yield* Deferred.await(refreshed); }).pipe( Effect.provide(makeLayerTest(layerTestEventSink, instances)), Effect.provideService(UsageLimitResetFallback, { - resetAt: (driver) => + resetAt: (driver, model) => Effect.sync(() => { - hubLookups.push(driver); + hubLookups.push(`${driver}:${model}`); return hubResetAt; }), }), diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 72805557f2d7..986b6a186b73 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -27,6 +27,7 @@ import * as SqlError from "effect/sql/SqlError"; import { AgentScope } from "@t3tools/shared/AgentScope"; import { isTemporaryWorktreeBranch } from "@t3tools/shared/git"; import { modelSelectionsEqual } from "@t3tools/shared/model"; +import { isProxied } from "@t3tools/shared/usageLimits"; import { ProviderTurnAnalytics, type ProviderTurnAnalyticsContext, @@ -641,11 +642,15 @@ export const layer: Layer.Layer< ) : undefined; // A direct login whose own probe failed is not served by the hub. - const proxied = - provider?.auth.type === "customEndpoint" || - provider?.usageLimits?.unavailable?.reason === "unsupported"; + const proxied = provider !== undefined && isProxied(provider); + const model = proxied + ? yield* projections.getThread(input.threadId).pipe( + Effect.map((thread) => thread.modelSelection.model), + Effect.orElseSucceed(() => null), + ) + : null; const hubResetAt = proxied - ? yield* usageLimitResets.resetAt(input.event.driver) + ? yield* usageLimitResets.resetAt(input.event.driver, model) : null; const failure = hubResetAt === null ? reported : { ...reported, resetAt: hubResetAt }; return [ diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index cda6780016e2..63fd5bd8eb90 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -166,9 +166,10 @@ it.effect("reads a management key from its secret provider", () => { it("waits for the first refill only when every pooled account of the driver is spent", () => { const now = Date.parse("2026-11-06T12:00:00.000Z"); const claude = ProviderDriverKind.make("claudeAgent"); - const account = (id: string, windows: ReadonlyArray) => ({ + const account = (id: string, windows: ReadonlyArray, models?: ReadonlyArray) => ({ id, driver: claude, + ...(models ? { models } : {}), usageLimits: { checkedAt: "2026-11-06T12:00:00.000Z", windows }, }); const session = (usedPercent: number, resetsAt: string) => ({ @@ -201,18 +202,26 @@ it("waits for the first refill only when every pooled account of the driver is s }, ]), ]; - expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, now)).toBe( + expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, null, now)).toBe( "2026-11-06T14:00:00.000Z", ); const withRoom = [...spent, account("c", [session(40, "2026-11-06T15:00:00.000Z")])]; - expect(UsageLimitSources.hubPoolResetAt(hub(withRoom), claude, now)).toBeNull(); + expect(UsageLimitSources.hubPoolResetAt(hub(withRoom), claude, null, now)).toBeNull(); const unread = { id: "d", driver: claude, usageLimits: { checkedAt: "", windows: [], unavailable: { reason: "probeFailed" } }, }; - expect(UsageLimitSources.hubPoolResetAt(hub([...spent, unread]), claude, now)).toBeNull(); + expect(UsageLimitSources.hubPoolResetAt(hub([...spent, unread]), claude, null, now)).toBeNull(); expect( - UsageLimitSources.hubPoolResetAt(hub(spent), ProviderDriverKind.make("codex"), now), + UsageLimitSources.hubPoolResetAt(hub(spent), ProviderDriverKind.make("codex"), null, now), ).toBeNull(); + const routed = [ + account("opus", [session(100, "2026-11-06T13:00:00.000Z")], ["claude-opus-5-5"]), + account("sonnet", [session(40, "2026-11-06T15:00:00.000Z")], ["claude-sonnet-5-5"]), + ]; + expect(UsageLimitSources.hubPoolResetAt(hub(routed), claude, "claude-opus-5-5[1m]", now)).toBe( + "2026-11-06T13:00:00.000Z", + ); + expect(UsageLimitSources.hubPoolResetAt(hub(routed), claude, null, now)).toBeNull(); }); diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 8f867dd29e51..55bf69eb8c56 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -11,6 +11,7 @@ import { type UsageLimitSourceSnapshot, } from "@t3tools/contracts"; import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; +import { hubRoutes } from "@t3tools/shared/usageLimits"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; @@ -200,15 +201,16 @@ export const layer = Layer.effect(UsageLimitSources, make).pipe( Layer.provide(SecretResolverLayer.layer), ); -/** A hub returns a usage limit only when every account it pools for the driver is spent. */ +/** A hub returns a usage limit only when every account it routes the model to is spent. */ export function hubPoolResetAt( sources: ReadonlyArray, driver: ProviderDriverKind, + model: string | null, now: number, ): string | null { let earliest: number | null = null; - for (const account of sources.flatMap((source) => source.accounts)) { - if (account.driver !== driver) continue; + const routes = hubRoutes(sources, driver, model); + for (const account of sources.flatMap((source) => source.accounts).filter(routes)) { // An unread account may be the one still blocked, so its refill is unknown. if (account.usageLimits.unavailable) return null; const spent = account.usageLimits.windows.filter((window) => window.usedPercent >= 100); @@ -223,7 +225,10 @@ export function hubPoolResetAt( } export class UsageLimitResetFallback extends Context.Reference<{ - readonly resetAt: (driver: ProviderDriverKind) => Effect.Effect; + readonly resetAt: ( + driver: ProviderDriverKind, + model: string | null, + ) => Effect.Effect; }>("t3/usage/UsageLimitResetFallback", { defaultValue: () => ({ resetAt: () => Effect.succeed(null) }), }) {} @@ -233,18 +238,18 @@ export const layerResetFallback = Layer.effect( Effect.gen(function* () { const sources = yield* UsageLimitSources; return { - resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver) { - const servesDriver = (snapshots: ReadonlyArray) => - snapshots.filter((source) => - source.accounts.some((account) => account.driver === driver), - ); - // Two hubs for one driver leave no way to tell which one served the failed turn. - if (servesDriver(yield* sources.current).length !== 1) return null; + resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, model) { + const serving = (snapshots: ReadonlyArray) => { + const routes = hubRoutes(snapshots, driver, model); + return snapshots.filter((source) => source.accounts.some(routes)); + }; + // Two hubs for one model leave no way to tell which one served the failed turn. + if (serving(yield* sources.current).length !== 1) return null; // The terminal waits on this, so a slow hub falls back to its last reading. yield* sources.refresh.pipe(Effect.timeoutOption("5 seconds")); - const hubs = servesDriver(yield* sources.current); + const hubs = serving(yield* sources.current); return hubs.length === 1 - ? hubPoolResetAt(hubs, driver, DateTime.toEpochMillis(yield* DateTime.now)) + ? hubPoolResetAt(hubs, driver, model, DateTime.toEpochMillis(yield* DateTime.now)) : null; }), }; diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index b9f357522bb7..efbdac652585 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -56,6 +56,7 @@ function fixture( >; upstream?: (request: RequestBody) => { status: number; body: unknown }; cooldownStatus?: number; + models?: (name: string) => Response; } = {}, ) { const requests: Array<{ path: string; body?: RequestBody }> = []; @@ -68,6 +69,12 @@ function fixture( ? decodeRequest(new TextDecoder().decode(request.body.body)) : undefined; requests.push({ path, ...(body ? { body } : {}) }); + if (path.endsWith("/auth-files/models")) + return HttpClientResponse.fromWeb( + request, + options.models?.(new URL(request.url).searchParams.get("name")!) ?? + Response.json({}, { status: 404 }), + ); if (path.endsWith("/auth-files")) return HttpClientResponse.fromWeb( request, @@ -224,6 +231,21 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect("names the models the hub routes to each account, when the hub lists them", () => + Effect.gen(function* () { + const test = fixture({ + models: (name) => + name === "first.json" + ? Response.json({ models: [{ id: "gpt-5.6-sol" }, { id: " " }] }) + : Response.json({}, { status: 404 }), + }); + const api = yield* test.api; + const result = yield* api.readAccounts(config); + expect(result.map((account) => account.models)).toEqual([["gpt-5.6-sol"], undefined]); + expect(result[1]?.usageLimits.windows).toHaveLength(1); + }), + ); + it.effect("resolves a missing usage plan from current, then legacy, token plans", () => Effect.gen(function* () { const test = fixture({ diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 87ec1e05b8f7..1a0d1f25c6ea 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -10,6 +10,7 @@ import * as Digest from "effect/crypto/Digest"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Hex from "effect/encoding/Hex"; +import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http"; @@ -39,6 +40,9 @@ const AuthFile = Schema.Struct({ ), }); const AuthFiles = Schema.Struct({ files: Schema.Array(AuthFile) }); +const AuthFileModels = Schema.Struct({ + models: Schema.Array(Schema.Struct({ id: Schema.String })), +}); const ApiResponse = Schema.Struct({ status_code: Schema.Number, body: Schema.String }); const CodexWindow = Schema.Struct({ used_percent: Schema.Number, @@ -101,6 +105,7 @@ const CreditList = Schema.Struct({ }); const decodeAuthFiles = Schema.decodeUnknownEffect(AuthFiles); +const decodeAuthFileModels = Schema.decodeUnknownEffect(AuthFileModels); const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); @@ -213,6 +218,20 @@ export const makeCliproxyApi = Effect.gen(function* () { return (yield* decodeAuthFiles(response)).files; }); + // Older hubs lack this endpoint; the account then counts for every model of its driver. + const routedModels = Effect.fn("CliproxyApi.routedModels")(function* ( + hub: CliproxyHub, + account: typeof AuthFile.Type, + ) { + const response = yield* management( + hub, + `auth-files/models?name=${encodeURIComponent(account.id)}`, + ); + return (yield* decodeAuthFileModels(response)).models.flatMap(({ id }) => + id.trim() ? [id.trim()] : [], + ); + }); + const apiCall = Effect.fn("CliproxyApi.apiCall")(function* ( hub: CliproxyHub, account: typeof AuthFile.Type, @@ -274,6 +293,7 @@ export const makeCliproxyApi = Effect.gen(function* () { account: typeof AuthFile.Type, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); + const models = yield* routedModels(hub, account).pipe(Effect.option); const base = { id: account.id, driver: ProviderDriverKind.make(account.provider === "codex" ? "codex" : "claudeAgent"), @@ -281,6 +301,7 @@ export const makeCliproxyApi = Effect.gen(function* () { ...(account.id_token?.chatgpt_account_id?.trim() ? { workspaceId: account.id_token.chatgpt_account_id.trim() } : {}), + ...(Option.isSome(models) && models.value.length > 0 ? { models: models.value } : {}), }; const read = Effect.gen(function* () { if (account.provider === "claude") { diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 76ad543630fb..fc687e638105 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1312,6 +1312,10 @@ export default function ChatView(props: ChatViewProps) { const selectedProvider = selectedProviderEntry?.driverKind ?? requestedDriverKind; const activeProviderInstanceId = selectedProviderEntry?.instanceId ?? null; const activeProviderStatus = selectedProviderEntry?.snapshot ?? null; + const activeUsageModel = + [composerSelectedModelSelection, activeThread?.modelSelection].find( + (selection) => selection?.instanceId === activeProviderInstanceId, + )?.model ?? null; const { enabled: interactionModeEnabled, interactionMode } = resolveComposerInteractionMode({ planModeEnabled: settings.planModeEnabled, provider: activeProviderStatus, @@ -1407,6 +1411,7 @@ export default function ChatView(props: ChatViewProps) { threadKey: routeThreadKey, panelKey: usageLimitsKey, providerInstanceId: activeProviderInstanceId, + model: activeUsageModel, providerDriver: activeProviderStatus?.driver ?? null, providers: providerStatuses, sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES, @@ -3950,6 +3955,7 @@ export default function ChatView(props: ChatViewProps) { onDeleteProjectScript={deleteProjectScriptStable} usage={{ providerInstanceId: activeProviderInstanceId, + model: activeUsageModel, providers: providerStatuses, sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES, contextWindow: activeContextWindow, diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index a1767e361b2b..6536c8e5a312 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -73,6 +73,7 @@ interface HubAccountInput { readonly id: string; readonly email?: string; readonly driver?: ProviderDriverKind; + readonly models?: readonly string[]; readonly limits: ServerProviderUsageLimits; } @@ -90,6 +91,7 @@ function hub( id: account.id, driver: account.driver ?? claude, ...(account.email ? { email: account.email } : {}), + ...(account.models ? { models: account.models } : {}), usageLimits: account.limits, })), ...(error ? { error } : {}), @@ -100,9 +102,11 @@ function shown( own: ServerProviderUsageLimits, sources: readonly UsageLimitSourceSnapshot[], natives: readonly ServerProvider[] = [], + model: string | null = null, ) { const resolved = resolveThreadDetailsLimits( instanceId, + model, [claudeInstance(own), ...natives], sources, now, @@ -121,13 +125,15 @@ function shown( describe("resolveThreadDetailsLimits", () => { it("shows nothing without a selected, known instance, or before its first reading", () => { - expect(resolveThreadDetailsLimits(null, [claudeInstance(unsupported)], [], now)).toBeNull(); expect( - resolveThreadDetailsLimits(ProviderInstanceId.make("gone"), [], [hub("Hub", [])], now), + resolveThreadDetailsLimits(null, null, [claudeInstance(unsupported)], [], now), + ).toBeNull(); + expect( + resolveThreadDetailsLimits(ProviderInstanceId.make("gone"), null, [], [hub("Hub", [])], now), ).toBeNull(); const { usageLimits: _unread, ...unread } = claudeInstance(unsupported); const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); - expect(resolveThreadDetailsLimits(instanceId, [unread], [readableHub], now)).toBeNull(); + expect(resolveThreadDetailsLimits(instanceId, null, [unread], [readableHub], now)).toBeNull(); }); it("keeps a direct login's failed reading instead of an unrelated hub", () => { @@ -138,7 +144,7 @@ describe("resolveThreadDetailsLimits", () => { }; const direct = { ...claudeInstance(failed), auth: { status: "authenticated" as const } }; const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); - expect(resolveThreadDetailsLimits(instanceId, [direct], [readableHub], now)).toEqual({ + expect(resolveThreadDetailsLimits(instanceId, null, [direct], [readableHub], now)).toEqual({ kind: "instance", limits: failed, }); @@ -189,8 +195,19 @@ describe("resolveThreadDetailsLimits", () => { natives: [signedIn("claude-a", "a@example.com", limitsAt(30, "2026-10-30T11:59:30.000Z"))], rows: [{ label: "Session", left: 70, accounts: 1, from: ["Signed-in accounts"] }], }, - ])("pools $name when the instance reads no limits", ({ sources, natives, rows }) => { - expect(shown(unsupported, sources, natives)).toEqual({ kind: "pool", rows }); + { + name: "only the accounts the hub routes the model to, even of another driver", + sources: [ + hub("Hub", [ + { id: "a.json", models: ["claude-opus-5-5"], limits: limitsAt(10) }, + { id: "c.json", driver: codex, models: ["gpt-5.6-sol"], limits: limitsAt(30) }, + ]), + ], + model: "gpt-5.6-sol[1m]", + rows: [{ label: "Session", left: 70, accounts: 1, from: ["Hub"] }], + }, + ])("pools $name when the instance reads no limits", ({ sources, natives, model, rows }) => { + expect(shown(unsupported, sources, natives, model)).toEqual({ kind: "pool", rows }); }); it("names the next reset that refills quota, skipping an unused account's 0% reset", () => { @@ -198,6 +215,7 @@ describe("resolveThreadDetailsLimits", () => { const later = "2026-10-30T14:00:00.000Z"; const resolved = resolveThreadDetailsLimits( instanceId, + null, [claudeInstance(unsupported)], [ hub("Hub", [ diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index aae5a9ceafb8..c44494322ecb 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -10,6 +10,8 @@ import { collectLimitPools, collectProviderUsageLimits, displayLimitWindows, + hubRoutes, + isProxied, type LimitPoolWindow, limitsNotice, } from "@t3tools/shared/usageLimits"; @@ -23,24 +25,25 @@ const CARD_ENVIRONMENT = EnvironmentId.make("thread-details"); export function resolveThreadDetailsLimits( providerInstanceId: ProviderInstanceId | null, + model: string | null, providers: readonly ServerProvider[], sources: UsageLimitSourceSnapshots, now: number, ): ThreadDetailsLimits | null { const provider = providers.find((entry) => entry.instanceId === providerInstanceId); if (!provider) return null; - const report = collectProviderUsageLimits(provider.instanceId, providers, sources, now); + const report = collectProviderUsageLimits(provider.instanceId, providers, sources, now, model); // Only this instance's data: the report keeps one instance per login, and any other entry may be // a different login or workspace, so fall back to the instance's own snapshot. const own = report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ?? provider.usageLimits; if (!own) return null; - const proxied = - provider.auth.type === "customEndpoint" || own.unavailable?.reason === "unsupported"; - if (limitsNotice(own) === null || !proxied) return { kind: "instance", limits: own }; + if (limitsNotice(own) === null || !isProxied(provider)) return { kind: "instance", limits: own }; // A proxied instance reads no limits of its own; its hub pools the accounts it spends. - return driverPool(provider.driver, providers, sources, now) ?? { kind: "instance", limits: own }; + return ( + hubPool(provider.driver, model, providers, sources, now) ?? { kind: "instance", limits: own } + ); } export function nextPoolReset(window: LimitPoolWindow, now: number) { @@ -50,33 +53,39 @@ export function nextPoolReset(window: LimitPoolWindow, now: number) { ); } -function driverPool( +function hubPool( driver: ServerProvider["driver"], + model: string | null, providers: readonly ServerProvider[], sources: UsageLimitSourceSnapshots, now: number, ): ThreadDetailsLimits | null { - const hubReadsDriver = sources.some((source) => - source.accounts.some( - (account) => account.driver === driver && limitsNotice(account.usageLimits) === null, + const routes = hubRoutes(sources, driver, model); + const routed = sources.map((source) => ({ ...source, accounts: source.accounts.filter(routes) })); + const readable = routed + .flatMap((source) => source.accounts) + .filter((account) => limitsNotice(account.usageLimits) === null); + if (readable.length === 0) return null; + // A signed-in login joins the pool only as a fresher reading of an account the hub routes to. + const signedIn = providers.filter((provider) => + readable.some( + (account) => + account.driver === provider.driver && + account.email !== undefined && + account.email.toLowerCase() === provider.auth.email?.toLowerCase(), ), ); - if (!hubReadsDriver) return null; const presentations = new Map([ [ CARD_ENVIRONMENT, { entry: { target: { label: "" } }, - serverConfig: { providers, usageLimitSources: sources }, + serverConfig: { providers: signedIn, usageLimitSources: routed }, }, ], ]); - const accounts = collectLimitAccounts(presentations).filter( - (account) => account.driver === driver, - ); - const [pool] = collectLimitPools(accounts, now); - if (!pool) return null; - return { kind: "pool", windows: displayLimitWindows(pool) }; + const [pool] = collectLimitPools(collectLimitAccounts(presentations), now); + return pool ? { kind: "pool", windows: displayLimitWindows(pool) } : null; } export function poolSources(window: LimitPoolWindow): string[] { diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index 1a60f43f6c35..3346ee02ed2d 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -36,6 +36,7 @@ const PACE_LABEL: Record = { export interface ThreadDetailsUsageInput { readonly providerInstanceId: ProviderInstanceId | null; + readonly model: string | null; readonly providers: readonly ServerProvider[]; readonly sources: UsageLimitSourceSnapshots; readonly contextWindow: ContextWindowSnapshot | null; @@ -52,12 +53,13 @@ function limitTone(left: number): UsageTone { /** The selected provider's limit windows. Each row reads as room left. */ export function ThreadDetailsUsage({ providerInstanceId, + model, providers, sources, }: Omit) { const nowMinute = useNowMinute(); const now = Date.parse(`${nowMinute}:00Z`); - const resolved = resolveThreadDetailsLimits(providerInstanceId, providers, sources, now); + const resolved = resolveThreadDetailsLimits(providerInstanceId, model, providers, sources, now); if (!resolved) return null; return ( diff --git a/apps/web/src/components/chat/useUsageLimitsPanel.ts b/apps/web/src/components/chat/useUsageLimitsPanel.ts index c0862a65bca2..f6911ab8676b 100644 --- a/apps/web/src/components/chat/useUsageLimitsPanel.ts +++ b/apps/web/src/components/chat/useUsageLimitsPanel.ts @@ -16,6 +16,7 @@ export function useUsageLimitsPanel(input: { readonly threadKey: string; readonly panelKey: string | null; readonly providerInstanceId: ProviderInstanceId | null; + readonly model: string | null; readonly providerDriver: ServerProvider["driver"] | null; readonly providers: readonly ServerProvider[]; readonly sources: UsageLimitSourceSnapshots; @@ -25,6 +26,7 @@ export function useUsageLimitsPanel(input: { threadKey, panelKey, providerInstanceId, + model, providerDriver, providers, sources, @@ -39,7 +41,7 @@ export function useUsageLimitsPanel(input: { } const report = panel !== null && panelKey !== null && panel.key === panelKey && providerInstanceId !== null - ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now) + ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now, model) : null; const banner = report !== null && panel !== null @@ -56,7 +58,7 @@ export function useUsageLimitsPanel(input: { const now = Date.now(); const nextReport = providerInstanceId !== null && panelKey !== null - ? collectProviderUsageLimits(providerInstanceId, providers, sources, now) + ? collectProviderUsageLimits(providerInstanceId, providers, sources, now, model) : null; if (nextReport && panelKey !== null) { setPanel({ key: panelKey, threadKey, now }); diff --git a/docs/user/usage.md b/docs/user/usage.md index 5dadf3fde08c..60ade0791fa7 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -151,8 +151,9 @@ To keep them in view on web and desktop, turn on **Usage limits** in **Settings The bottom of the thread details card then shows how much is left in each limit window of the current provider account. Some windows, such as a weekly Opus limit, count only one model. Hover a row to see when it resets and your pace. A provider that sends requests through a CLIProxyAPI hub -reports no limits of its own, so the card shows that provider's accounts pooled, the hub's and -any signed-in ones, from this environment only. **Usage → Limits** pools every connected +reports no limits of its own, so the card pools the hub accounts that serve the thread's model, +from this environment only. A Claude instance that runs GPT models through the hub shows the +hub's Codex accounts. **Usage → Limits** pools every connected environment. Turn on **Context window** to also show the thread's context use against the model's limit. A tick on the bar marks where the provider compacts automatically, when it reports that point. diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index 5c4927d4a164..3cb32762849b 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -61,6 +61,8 @@ export const UsageLimitSourceAccount = Schema.Struct({ email: Schema.optional(TrimmedNonEmptyString), plan: Schema.optional(TrimmedNonEmptyString), workspaceId: Schema.optional(TrimmedNonEmptyString), + /** The models the hub routes to this account. */ + models: Schema.optional(Schema.Array(TrimmedNonEmptyString)), usageLimits: ServerProviderUsageLimits, }); export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type; diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 72a17323386c..53f4f58a0769 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1252,6 +1252,32 @@ describe("/usage-limits", () => { expect(report?.notices).toEqual([]); }); + it("shows a proxied instance the hub accounts its model routes to, of any driver", () => { + const routed = [ + { + ...sources[0]!, + accounts: sources[0]!.accounts.map((account) => + account.id === "oss" ? { ...account, models: ["gpt-5.6-sol"] } : account, + ), + }, + ]; + const proxy = provider({ + instanceId: ProviderInstanceId.make("claudex"), + driver: ProviderDriverKind.make("claudeAgent"), + auth: { status: "authenticated", type: "customEndpoint" }, + }); + const ids = (instance: ServerProvider, model: string | null) => + collectProviderUsageLimits( + instance.instanceId, + [selected, proxy], + routed, + now, + model, + )?.accounts.map((account) => account.id); + expect(ids(proxy, "gpt-5.6-sol[1m]")).toEqual(["codex", "hub:oss"]); + expect(ids(selected, "gpt-5.6-sol")).toEqual(["codex", "hub:oss"]); + }); + it("keeps the freshest usable native duplicate and the freshest native credits in either order", () => { const failed = provider({ auth: selected.auth, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 869f676acae6..77b8a33a1846 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -13,6 +13,7 @@ import { type ServerProvider, type ServerProviderUsageLimits, type ServerProviderUsageWindow, + type UsageLimitSourceAccount, type UsageLimitSourceSnapshots, } from "@t3tools/contracts"; @@ -750,14 +751,43 @@ export function withUsageLimitsCommands( }); } +/** An instance that sends requests through a hub reads no limits of its own. */ +export function isProxied(provider: ServerProvider): boolean { + return ( + provider.auth.type === "customEndpoint" || + provider.usageLimits?.unavailable?.reason === "unsupported" + ); +} + +/** The hub accounts that serve a model: those that list it, else every account of the driver. */ +export function hubRoutes( + sources: UsageLimitSourceSnapshots, + driver: ServerProvider["driver"], + model: string | null, +): (account: UsageLimitSourceAccount) => boolean { + // Claude Code's 1M-context suffix is not part of the model id a hub routes. + const slug = model?.replace(/\[1m\]$/i, ""); + const listed = (account: UsageLimitSourceAccount) => + slug !== undefined && account.models?.includes(slug) === true; + return sources.some((source) => source.accounts.some(listed)) + ? listed + : (account) => account.driver === driver; +} + export function collectProviderUsageLimits( instanceId: ProviderInstanceId, providers: readonly ServerProvider[], sources: UsageLimitSourceSnapshots, now: number, + model: string | null = null, ): UsageLimitsReport | null { const selected = providers.find((provider) => provider.instanceId === instanceId); - if (!selected || !hasProviderUsageLimits(selected.driver, providers, sources)) return null; + if (!selected) return null; + // Only a proxied instance spends what its hub routes the model to, maybe another driver's accounts. + const routes = hubRoutes(sources, selected.driver, isProxied(selected) ? model : null); + const driver = + sources.flatMap((source) => source.accounts).find(routes)?.driver ?? selected.driver; + if (!hasProviderUsageLimits(driver, providers, sources)) return null; const resolveKey = accountKeyResolver([ ...providers.filter((provider) => provider.enabled).map(nativeReport), ...sources.flatMap((source) => source.accounts), @@ -768,7 +798,7 @@ export function collectProviderUsageLimits( { usage: NativeSnapshot; credits: NativeSnapshot | null } >(); for (const provider of providersWithLimits(providers)) { - if (provider.driver !== selected.driver || !provider.usageLimits) continue; + if (provider.driver !== driver || !provider.usageLimits) continue; const snapshot = { provider, limits: provider.usageLimits }; const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`; const previous = nativeByAccount.get(key); @@ -848,7 +878,7 @@ export function collectProviderUsageLimits( }); } for (const source of sources) { - const matching = source.accounts.filter((account) => account.driver === selected.driver); + const matching = source.accounts.filter(routes); for (const account of matching) { const key = resolveKey(account); if (key && nativeAccountKeys.has(key)) continue; From 8b2304f28133b37acef889937982c6e6e7a51446 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:58:05 +0100 Subject: [PATCH 05/30] fix(web): the hub pool card keeps workspaces apart and shows every routed driver Co-Authored-By: Claude Opus 5.5 --- .../chat/ThreadDetailsUsage.logic.test.ts | 31 +++++++++++++++++++ .../chat/ThreadDetailsUsage.logic.ts | 5 +-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index 6536c8e5a312..b100bf359312 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -74,6 +74,7 @@ interface HubAccountInput { readonly email?: string; readonly driver?: ProviderDriverKind; readonly models?: readonly string[]; + readonly workspaceId?: string; readonly limits: ServerProviderUsageLimits; } @@ -92,6 +93,7 @@ function hub( driver: account.driver ?? claude, ...(account.email ? { email: account.email } : {}), ...(account.models ? { models: account.models } : {}), + ...(account.workspaceId ? { workspaceId: account.workspaceId } : {}), usageLimits: account.limits, })), ...(error ? { error } : {}), @@ -206,6 +208,35 @@ describe("resolveThreadDetailsLimits", () => { model: "gpt-5.6-sol[1m]", rows: [{ label: "Session", left: 70, accounts: 1, from: ["Hub"] }], }, + { + name: "every account the hub routes the model to, across drivers", + sources: [ + hub("Hub", [ + { id: "a.json", models: ["shared"], limits: limitsAt(10) }, + { id: "c.json", driver: codex, models: ["shared"], limits: limitsAt(30) }, + ]), + ], + model: "shared", + rows: [ + { label: "Session", left: 90, accounts: 1, from: ["Hub"] }, + { label: "Session", left: 70, accounts: 1, from: ["Hub"] }, + ], + }, + { + name: "a hub workspace without the same login's other workspace", + sources: [ + hub("Hub", [ + { id: "c.json", email: "c@example.com", workspaceId: "w1", limits: limitsAt(100) }, + ]), + ], + natives: [ + { + ...signedIn("claude-c", "c@example.com", limitsAt(0)), + auth: { status: "authenticated", email: "c@example.com", workspaceId: "w2" }, + } satisfies ServerProvider, + ], + rows: [{ label: "Session", left: 0, accounts: 1, from: ["Hub"] }], + }, ])("pools $name when the instance reads no limits", ({ sources, natives, model, rows }) => { expect(shown(unsupported, sources, natives, model)).toEqual({ kind: "pool", rows }); }); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index c44494322ecb..08d64699c9af 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -71,6 +71,7 @@ function hubPool( readable.some( (account) => account.driver === provider.driver && + account.workspaceId === provider.auth.workspaceId && account.email !== undefined && account.email.toLowerCase() === provider.auth.email?.toLowerCase(), ), @@ -84,8 +85,8 @@ function hubPool( }, ], ]); - const [pool] = collectLimitPools(collectLimitAccounts(presentations), now); - return pool ? { kind: "pool", windows: displayLimitWindows(pool) } : null; + const pools = collectLimitPools(collectLimitAccounts(presentations), now); + return pools.length > 0 ? { kind: "pool", windows: pools.flatMap(displayLimitWindows) } : null; } export function poolSources(window: LimitPoolWindow): string[] { From 18f8c43227202c1492eec984a931962ebd9f4168 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:08:06 +0100 Subject: [PATCH 06/30] fix(shared): hub routing counts accounts with unread model lists and every routed driver Co-Authored-By: Claude Opus 5.5 --- .../src/usage/UsageLimitSources.test.ts | 4 ++++ packages/shared/src/usageLimits.test.ts | 22 +++++++++++++++++++ packages/shared/src/usageLimits.ts | 22 +++++++++++++------ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 63fd5bd8eb90..8707e640211d 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -224,4 +224,8 @@ it("waits for the first refill only when every pooled account of the driver is s "2026-11-06T13:00:00.000Z", ); expect(UsageLimitSources.hubPoolResetAt(hub(routed), claude, null, now)).toBeNull(); + const unlisted = account("unlisted", [session(30, "2026-11-06T15:00:00.000Z")]); + expect( + UsageLimitSources.hubPoolResetAt(hub([...routed, unlisted]), claude, "claude-opus-5-5", now), + ).toBeNull(); }); diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 53f4f58a0769..91d54098f858 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1275,6 +1275,28 @@ describe("/usage-limits", () => { model, )?.accounts.map((account) => account.id); expect(ids(proxy, "gpt-5.6-sol[1m]")).toEqual(["codex", "hub:oss"]); + const both = [ + { + ...routed[0]!, + accounts: [ + ...routed[0]!.accounts, + { + id: "claude-hub", + driver: ProviderDriverKind.make("claudeAgent"), + models: ["gpt-5.6-sol"], + usageLimits: limits, + }, + ], + }, + ]; + const reversed = [{ ...both[0]!, accounts: [...both[0]!.accounts].reverse() }]; + for (const hubs of [both, reversed]) { + expect( + collectProviderUsageLimits(proxy.instanceId, [selected, proxy], hubs, now, "gpt-5.6-sol") + ?.accounts.map((account) => account.id) + .toSorted(), + ).toEqual(["codex", "hub:claude-hub", "hub:oss"]); + } expect(ids(selected, "gpt-5.6-sol")).toEqual(["codex", "hub:oss"]); }); diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 77b8a33a1846..7117a863c77d 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -769,9 +769,13 @@ export function hubRoutes( const slug = model?.replace(/\[1m\]$/i, ""); const listed = (account: UsageLimitSourceAccount) => slug !== undefined && account.models?.includes(slug) === true; - return sources.some((source) => source.accounts.some(listed)) - ? listed - : (account) => account.driver === driver; + const serving = new Set( + sources.flatMap((source) => source.accounts.filter(listed).map((account) => account.driver)), + ); + // Model ids such as "opus" are aliases no hub lists, so no listing falls back to the driver. + if (serving.size === 0) return (account) => account.driver === driver; + // An account whose model list was unreadable may serve the model too. + return (account) => (account.models ? listed(account) : serving.has(account.driver)); } export function collectProviderUsageLimits( @@ -785,9 +789,13 @@ export function collectProviderUsageLimits( if (!selected) return null; // Only a proxied instance spends what its hub routes the model to, maybe another driver's accounts. const routes = hubRoutes(sources, selected.driver, isProxied(selected) ? model : null); - const driver = - sources.flatMap((source) => source.accounts).find(routes)?.driver ?? selected.driver; - if (!hasProviderUsageLimits(driver, providers, sources)) return null; + const drivers = new Set( + sources.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), + ); + if (drivers.size === 0) drivers.add(selected.driver); + if (![...drivers].some((driver) => hasProviderUsageLimits(driver, providers, sources))) { + return null; + } const resolveKey = accountKeyResolver([ ...providers.filter((provider) => provider.enabled).map(nativeReport), ...sources.flatMap((source) => source.accounts), @@ -798,7 +806,7 @@ export function collectProviderUsageLimits( { usage: NativeSnapshot; credits: NativeSnapshot | null } >(); for (const provider of providersWithLimits(providers)) { - if (provider.driver !== driver || !provider.usageLimits) continue; + if (!drivers.has(provider.driver) || !provider.usageLimits) continue; const snapshot = { provider, limits: provider.usageLimits }; const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`; const previous = nativeByAccount.get(key); From 8a76c9ac933e4fed1ccbb87c55c432eb43ab5ca6 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:21:53 +0100 Subject: [PATCH 07/30] fix(shared): only a custom-endpoint instance follows the hub's model routing An unsupported limits probe (a Cursor login with Keychain reads off, an external OpenCode server) does not mean the instance uses a hub. Co-Authored-By: Claude Opus 5.5 --- .../ProviderEventIngestor.test.ts | 8 ++++---- .../src/orchestration-v2/ProviderEventIngestor.ts | 15 ++++++++------- .../components/chat/ThreadDetailsUsage.logic.ts | 6 +++++- packages/shared/src/usageLimits.test.ts | 14 ++++++++++++++ packages/shared/src/usageLimits.ts | 8 ++++++-- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 186d2e9ce341..09c2bdd2820f 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -362,10 +362,10 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit ); } assert.deepEqual(lookups, Array(6).fill(instance.instanceId)); - assert.deepEqual( - hubLookups, - Array(2).fill(`${instance.driverKind}:${modelSelection.model}`), - ); + assert.deepEqual(hubLookups, [ + `${instance.driverKind}:${modelSelection.model}`, + `${instance.driverKind}:null`, + ]); yield* Deferred.await(refreshed); }).pipe( Effect.provide(makeLayerTest(layerTestEventSink, instances)), diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 986b6a186b73..a6f7bcf34e27 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -27,7 +27,7 @@ import * as SqlError from "effect/sql/SqlError"; import { AgentScope } from "@t3tools/shared/AgentScope"; import { isTemporaryWorktreeBranch } from "@t3tools/shared/git"; import { modelSelectionsEqual } from "@t3tools/shared/model"; -import { isProxied } from "@t3tools/shared/usageLimits"; +import { hubRoutedModel, isProxied } from "@t3tools/shared/usageLimits"; import { ProviderTurnAnalytics, type ProviderTurnAnalyticsContext, @@ -643,12 +643,13 @@ export const layer: Layer.Layer< : undefined; // A direct login whose own probe failed is not served by the hub. const proxied = provider !== undefined && isProxied(provider); - const model = proxied - ? yield* projections.getThread(input.threadId).pipe( - Effect.map((thread) => thread.modelSelection.model), - Effect.orElseSucceed(() => null), - ) - : null; + const model = + provider !== undefined && proxied + ? yield* projections.getThread(input.threadId).pipe( + Effect.map((thread) => hubRoutedModel(provider, thread.modelSelection.model)), + Effect.orElseSucceed(() => null), + ) + : null; const hubResetAt = proxied ? yield* usageLimitResets.resetAt(input.event.driver, model) : null; diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index 08d64699c9af..7b66a65253ce 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -10,6 +10,7 @@ import { collectLimitPools, collectProviderUsageLimits, displayLimitWindows, + hubRoutedModel, hubRoutes, isProxied, type LimitPoolWindow, @@ -42,7 +43,10 @@ export function resolveThreadDetailsLimits( if (limitsNotice(own) === null || !isProxied(provider)) return { kind: "instance", limits: own }; // A proxied instance reads no limits of its own; its hub pools the accounts it spends. return ( - hubPool(provider.driver, model, providers, sources, now) ?? { kind: "instance", limits: own } + hubPool(provider.driver, hubRoutedModel(provider, model), providers, sources, now) ?? { + kind: "instance", + limits: own, + } ); } diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 91d54098f858..7b3ab5045db8 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1275,6 +1275,20 @@ describe("/usage-limits", () => { model, )?.accounts.map((account) => account.id); expect(ids(proxy, "gpt-5.6-sol[1m]")).toEqual(["codex", "hub:oss"]); + const unread = provider({ + instanceId: ProviderInstanceId.make("cursor"), + driver: ProviderDriverKind.make("cursor"), + usageLimits: { ...limits, windows: [], unavailable: { reason: "unsupported" } }, + }); + expect( + collectProviderUsageLimits( + unread.instanceId, + [unread], + routed, + now, + "gpt-5.6-sol", + )?.accounts.map((account) => account.id) ?? [], + ).toEqual(["cursor"]); const both = [ { ...routed[0]!, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 7117a863c77d..4cf399c6b6e8 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -759,6 +759,11 @@ export function isProxied(provider: ServerProvider): boolean { ); } +/** Only an instance sent to a custom endpoint is known to reach the hub that routes its model. */ +export function hubRoutedModel(provider: ServerProvider, model: string | null): string | null { + return provider.auth.type === "customEndpoint" ? model : null; +} + /** The hub accounts that serve a model: those that list it, else every account of the driver. */ export function hubRoutes( sources: UsageLimitSourceSnapshots, @@ -787,8 +792,7 @@ export function collectProviderUsageLimits( ): UsageLimitsReport | null { const selected = providers.find((provider) => provider.instanceId === instanceId); if (!selected) return null; - // Only a proxied instance spends what its hub routes the model to, maybe another driver's accounts. - const routes = hubRoutes(sources, selected.driver, isProxied(selected) ? model : null); + const routes = hubRoutes(sources, selected.driver, hubRoutedModel(selected, model)); const drivers = new Set( sources.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), ); From e2251592387962e34c1bdcfdd87d8b62a8f539da Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:36:04 +0100 Subject: [PATCH 08/30] fix(server): a hub reset follows the failing run's model, not a later selection Co-Authored-By: Claude Opus 5.5 --- .../ProviderEventIngestor.test.ts | 37 +++++++++++++++++-- .../orchestration-v2/ProviderEventIngestor.ts | 17 +++++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 09c2bdd2820f..8a16a1f8017f 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -288,7 +288,36 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2; const idAllocator = yield* IdAllocator.IdAllocatorV2; const threadEvent = yield* threadCreatedEvent(now); - yield* eventSink.write({ events: [threadEvent] }); + // The run started on an earlier model than the thread's current selection. + const runId = RunId.make("run:usage-limit"); + yield* eventSink.write({ + events: [ + threadEvent, + { + id: yield* idAllocator.allocate.event({ threadId: threadEvent.threadId }), + type: "run.created", + threadId: threadEvent.threadId, + occurredAt: now, + payload: { + id: runId, + threadId: threadEvent.threadId, + ordinal: 1, + providerInstanceId: modelSelection.instanceId, + modelSelection: { ...modelSelection, model: "gpt-run-model" }, + providerThreadId: null, + userMessageId: MessageId.make("message:usage-limit"), + rootNodeId: null, + activeAttemptId: null, + status: "running", + requestedAt: now, + startedAt: now, + completedAt: null, + checkpointId: null, + contextHandoffId: null, + }, + }, + ], + }); const input = { providerSessionId: yield* idAllocator.allocate.providerSession({ providerInstanceId: instance.instanceId, @@ -315,6 +344,7 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit usageLimits: { ...unread, unavailable: { reason: "probeFailed" } }, }, resetAt: hubResetAt, + runId, }, { failureClass: "usage_limit", @@ -325,10 +355,11 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit resetAt: hubResetAt, }, ] as const; - for (const [index, { failureClass, provider, resetAt }] of cases.entries()) { + for (const [index, { failureClass, provider, resetAt, ...run }] of cases.entries()) { own = provider; const stored = yield* ingestor.ingestNormalized({ ...input, + ...run, event: { type: "turn.terminal", driver: instance.driverKind, @@ -363,7 +394,7 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit } assert.deepEqual(lookups, Array(6).fill(instance.instanceId)); assert.deepEqual(hubLookups, [ - `${instance.driverKind}:${modelSelection.model}`, + `${instance.driverKind}:gpt-run-model`, `${instance.driverKind}:null`, ]); yield* Deferred.await(refreshed); diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index a6f7bcf34e27..478f39ebda3c 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -643,12 +643,21 @@ export const layer: Layer.Layer< : undefined; // A direct login whose own probe failed is not served by the hub. const proxied = provider !== undefined && isProxied(provider); + // The failing run's model, which may differ from a selection changed mid-turn. const model = provider !== undefined && proxied - ? yield* projections.getThread(input.threadId).pipe( - Effect.map((thread) => hubRoutedModel(provider, thread.modelSelection.model)), - Effect.orElseSucceed(() => null), - ) + ? yield* Effect.gen(function* () { + const { runs } = + input.runId === undefined + ? { runs: [] } + : yield* projections.getThreadRecords(input.threadId, ["runs"], { + runIds: [input.runId], + }); + const selection = + runs[0]?.modelSelection ?? + (yield* projections.getThread(input.threadId)).modelSelection; + return hubRoutedModel(provider, selection.model); + }).pipe(Effect.orElseSucceed(() => null)) : null; const hubResetAt = proxied ? yield* usageLimitResets.resetAt(input.event.driver, model) From f60fc3cf8adb70b3a97d446af86cdb53b6e1a646 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 04:27:14 +0100 Subject: [PATCH 09/30] fix(web): the thread usage card shows the hub account that served the model last CLIProxyAPI reports no account per turn. T3 now dates each hub account's latest served request from the hub's standard recent_requests buckets, and the thread usage card shows only the routed accounts the hub served last. With no recent request, the card pools every routed account as before. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/cliproxyApi.test.ts | 20 ++++++++++++++++ apps/server/src/usage/cliproxyApi.ts | 20 +++++++++++++++- .../chat/ThreadDetailsUsage.logic.test.ts | 23 +++++++++++++++++++ .../chat/ThreadDetailsUsage.logic.ts | 11 ++++++++- docs/user/usage.md | 7 +++--- packages/contracts/src/providerUsageLimits.ts | 2 ++ 6 files changed, 78 insertions(+), 5 deletions(-) diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index efbdac652585..3854734b7c64 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -51,6 +51,7 @@ function fixture( accounts?: Array< (typeof accounts)[number] & { disabled?: boolean; + recent_requests?: ReadonlyArray<{ time: string; success: number; failed: number }>; id_token: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string }; } >; @@ -246,6 +247,25 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect("dates each account's last served request from the hub's request buckets", () => + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse("2026-10-30T12:05:00Z")); + const bucket = (success: number, failed = 0) => ({ time: "", success, failed }); + const test = fixture({ + accounts: [ + { ...accounts[0]!, recent_requests: [bucket(2), bucket(3), bucket(0, 4)] }, + { ...accounts[1]!, recent_requests: [bucket(0), bucket(0, 1)] }, + ], + }); + const api = yield* test.api; + const result = yield* api.readAccounts(config); + expect(result.map((account) => account.lastServedAt)).toEqual([ + "2026-10-30T11:50:00.000Z", + undefined, + ]); + }), + ); + it.effect("resolves a missing usage plan from current, then legacy, token plans", () => Effect.gen(function* () { const test = fixture({ diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 1a0d1f25c6ea..4b0aee15fe70 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -31,6 +31,7 @@ const AuthFile = Schema.Struct({ provider: Schema.String, email: Schema.optional(Schema.String), disabled: Schema.optional(Schema.Boolean), + recent_requests: Schema.optional(Schema.Array(Schema.Struct({ success: Schema.Number }))), id_token: Schema.optional( Schema.Struct({ chatgpt_account_id: Schema.optional(Schema.String), @@ -39,6 +40,18 @@ const AuthFile = Schema.Struct({ }), ), }); +// CLIProxyAPI counts each account's requests in 10-minute buckets aligned to the epoch, newest last. +const RECENT_BUCKET_MS = 10 * 60 * 1000; + +function lastServedAt(account: typeof AuthFile.Type, now: number): string | undefined { + const buckets = account.recent_requests ?? []; + const index = buckets.findLastIndex((bucket) => bucket.success > 0); + if (index === -1) return undefined; + const current = Math.floor(now / RECENT_BUCKET_MS) * RECENT_BUCKET_MS; + const start = current - (buckets.length - 1 - index) * RECENT_BUCKET_MS; + return DateTime.formatIso(DateTime.makeUnsafe(start)); +} + const AuthFiles = Schema.Struct({ files: Schema.Array(AuthFile) }); const AuthFileModels = Schema.Struct({ models: Schema.Array(Schema.Struct({ id: Schema.String })), @@ -291,8 +304,10 @@ export const makeCliproxyApi = Effect.gen(function* () { const readAccount = Effect.fn("CliproxyApi.readAccount")(function* ( hub: CliproxyHub, account: typeof AuthFile.Type, + listedAt: number, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); + const served = lastServedAt(account, listedAt); const models = yield* routedModels(hub, account).pipe(Effect.option); const base = { id: account.id, @@ -302,6 +317,7 @@ export const makeCliproxyApi = Effect.gen(function* () { ? { workspaceId: account.id_token.chatgpt_account_id.trim() } : {}), ...(Option.isSome(models) && models.value.length > 0 ? { models: models.value } : {}), + ...(served ? { lastServedAt: served } : {}), }; const read = Effect.gen(function* () { if (account.provider === "claude") { @@ -403,12 +419,14 @@ export const makeCliproxyApi = Effect.gen(function* () { : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), ), ); + // Buckets are relative to the listing, so every account shares its time. + const listedAt = DateTime.toEpochMillis(yield* DateTime.now); return yield* Effect.forEach( accounts.filter( (account) => !account.disabled && (account.provider === "codex" || account.provider === "claude"), ), - (account) => readAccount(hub, account), + (account) => readAccount(hub, account, listedAt), { concurrency: 4 }, ); }); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index b100bf359312..45227db63b48 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -75,6 +75,7 @@ interface HubAccountInput { readonly driver?: ProviderDriverKind; readonly models?: readonly string[]; readonly workspaceId?: string; + readonly lastServedAt?: string; readonly limits: ServerProviderUsageLimits; } @@ -94,6 +95,7 @@ function hub( ...(account.email ? { email: account.email } : {}), ...(account.models ? { models: account.models } : {}), ...(account.workspaceId ? { workspaceId: account.workspaceId } : {}), + ...(account.lastServedAt ? { lastServedAt: account.lastServedAt } : {}), usageLimits: account.limits, })), ...(error ? { error } : {}), @@ -222,6 +224,27 @@ describe("resolveThreadDetailsLimits", () => { { label: "Session", left: 70, accounts: 1, from: ["Hub"] }, ], }, + { + name: "only the account the hub served last", + sources: [ + hub("Hub", [ + { id: "a.json", lastServedAt: "2026-10-30T11:50:00.000Z", limits: limitsAt(20) }, + { id: "b.json", lastServedAt: "2026-10-30T10:00:00.000Z", limits: limitsAt(60) }, + { id: "c.json", limits: limitsAt(0) }, + ]), + ], + rows: [{ label: "Session", left: 80, accounts: 1, from: ["Hub"] }], + }, + { + name: "every account the hub served in the same latest window", + sources: [ + hub("Hub", [ + { id: "a.json", lastServedAt: "2026-10-30T11:50:00.000Z", limits: limitsAt(20) }, + { id: "b.json", lastServedAt: "2026-10-30T11:50:00.000Z", limits: limitsAt(60) }, + ]), + ], + rows: [{ label: "Session", left: 60, accounts: 2, from: ["Hub"] }], + }, { name: "a hub workspace without the same login's other workspace", sources: [ diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index 7b66a65253ce..24b03c3ba03d 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -3,6 +3,7 @@ import { type ProviderInstanceId, type ServerProvider, type ServerProviderUsageLimits, + type UsageLimitSourceAccount, type UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { @@ -65,7 +66,15 @@ function hubPool( now: number, ): ThreadDetailsLimits | null { const routes = hubRoutes(sources, driver, model); - const routed = sources.map((source) => ({ ...source, accounts: source.accounts.filter(routes) })); + const accounts = sources.flatMap((source) => source.accounts.filter(routes)); + // The hub names no account per turn; the latest one it served stands in for the thread's. + const lastServed = accounts + .flatMap((account) => (account.lastServedAt ? [account.lastServedAt] : [])) + .toSorted() + .at(-1); + const shown = (account: UsageLimitSourceAccount) => + routes(account) && (lastServed === undefined || account.lastServedAt === lastServed); + const routed = sources.map((source) => ({ ...source, accounts: source.accounts.filter(shown) })); const readable = routed .flatMap((source) => source.accounts) .filter((account) => limitsNotice(account.usageLimits) === null); diff --git a/docs/user/usage.md b/docs/user/usage.md index 60ade0791fa7..833d580e32a1 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -151,9 +151,10 @@ To keep them in view on web and desktop, turn on **Usage limits** in **Settings The bottom of the thread details card then shows how much is left in each limit window of the current provider account. Some windows, such as a weekly Opus limit, count only one model. Hover a row to see when it resets and your pace. A provider that sends requests through a CLIProxyAPI hub -reports no limits of its own, so the card pools the hub accounts that serve the thread's model, -from this environment only. A Claude instance that runs GPT models through the hub shows the -hub's Codex accounts. **Usage → Limits** pools every connected +reports no limits of its own, so the card shows the hub account that served the thread's model +most recently, from this environment only. If the hub has served none in the last few hours, the +card pools every hub account that serves the model. A Claude instance that runs GPT models +through the hub shows the hub's Codex accounts. **Usage → Limits** pools every connected environment. Turn on **Context window** to also show the thread's context use against the model's limit. A tick on the bar marks where the provider compacts automatically, when it reports that point. diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index 3cb32762849b..f4222a44a674 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -63,6 +63,8 @@ export const UsageLimitSourceAccount = Schema.Struct({ workspaceId: Schema.optional(TrimmedNonEmptyString), /** The models the hub routes to this account. */ models: Schema.optional(Schema.Array(TrimmedNonEmptyString)), + /** Start of the latest window in which the hub served a request with this account. */ + lastServedAt: Schema.optional(IsoDateTime), usageLimits: ServerProviderUsageLimits, }); export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type; From d18bde049d7369c30765461c712b947222406883 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 04:38:28 +0100 Subject: [PATCH 10/30] fix(server): a hub account with no routed models serves none An empty auth-files/models list means the hub routes no model to the account, so it no longer falls back to every model of its driver. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/cliproxyApi.test.ts | 7 +++++-- apps/server/src/usage/cliproxyApi.ts | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 3854734b7c64..640cabb5d934 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -235,14 +235,17 @@ describe("CLIProxyAPI built-in management API", () => { it.effect("names the models the hub routes to each account, when the hub lists them", () => Effect.gen(function* () { const test = fixture({ + accounts: [...accounts, { ...accounts[1]!, id: "third.json", auth_index: "c" }], models: (name) => name === "first.json" ? Response.json({ models: [{ id: "gpt-5.6-sol" }, { id: " " }] }) - : Response.json({}, { status: 404 }), + : name === "third.json" + ? Response.json({ models: [] }) + : Response.json({}, { status: 404 }), }); const api = yield* test.api; const result = yield* api.readAccounts(config); - expect(result.map((account) => account.models)).toEqual([["gpt-5.6-sol"], undefined]); + expect(result.map((account) => account.models)).toEqual([["gpt-5.6-sol"], undefined, []]); expect(result[1]?.usageLimits.windows).toHaveLength(1); }), ); diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 4b0aee15fe70..e43c70259692 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -316,7 +316,7 @@ export const makeCliproxyApi = Effect.gen(function* () { ...(account.id_token?.chatgpt_account_id?.trim() ? { workspaceId: account.id_token.chatgpt_account_id.trim() } : {}), - ...(Option.isSome(models) && models.value.length > 0 ? { models: models.value } : {}), + ...(Option.isSome(models) ? { models: models.value } : {}), ...(served ? { lastServedAt: served } : {}), }; const read = Effect.gen(function* () { From 8ad336bcba2732ec1889a21f333fa44442b21745 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 7 Nov 2026 19:14:45 +0100 Subject: [PATCH 11/30] docs: drop the hub account note from usage limits --- docs/user/usage.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/docs/user/usage.md b/docs/user/usage.md index 833d580e32a1..d25bdba81f00 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -150,14 +150,9 @@ anything. The command is offered only for providers that appear under **Usage To keep them in view on web and desktop, turn on **Usage limits** in **Settings → Appearance → Thread details**, or right-click the thread details card. The bottom of the thread details card then shows how much is left in each limit window of the current provider account. Some windows, such as a weekly Opus limit, count only one model. Hover a -row to see when it resets and your pace. A provider that sends requests through a CLIProxyAPI hub -reports no limits of its own, so the card shows the hub account that served the thread's model -most recently, from this environment only. If the hub has served none in the last few hours, the -card pools every hub account that serves the model. A Claude instance that runs GPT models -through the hub shows the hub's Codex accounts. **Usage → Limits** pools every connected -environment. Turn on **Context window** to also show the thread's context use against the -model's limit. A tick on the bar marks where the provider compacts automatically, when it reports -that point. +row to see when it resets and your pace. Turn on **Context window** to also show the thread's +context use against the model's limit. A tick on the bar marks where the provider compacts +automatically, when it reports that point. OpenCode Go reports its session, weekly, and monthly allowance when OpenCode runs locally in the environment. T3 cannot report limits for external OpenCode servers because their credentials From 4c07d99c5f59dbb45799ee98999def26b1c3cb17 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 03:33:49 +0000 Subject: [PATCH 12/30] test(web): drop thread usage cases covered elsewhere AI-assisted; verified with the tests and tools listed above. --- .../chat/ThreadDetailsUsage.logic.test.ts | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index 45227db63b48..bbbfc1a5a56b 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -128,18 +128,6 @@ function shown( } describe("resolveThreadDetailsLimits", () => { - it("shows nothing without a selected, known instance, or before its first reading", () => { - expect( - resolveThreadDetailsLimits(null, null, [claudeInstance(unsupported)], [], now), - ).toBeNull(); - expect( - resolveThreadDetailsLimits(ProviderInstanceId.make("gone"), null, [], [hub("Hub", [])], now), - ).toBeNull(); - const { usageLimits: _unread, ...unread } = claudeInstance(unsupported); - const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); - expect(resolveThreadDetailsLimits(instanceId, null, [unread], [readableHub], now)).toBeNull(); - }); - it("keeps a direct login's failed reading instead of an unrelated hub", () => { const failed: ServerProviderUsageLimits = { checkedAt, @@ -163,16 +151,6 @@ describe("resolveThreadDetailsLimits", () => { }); it.each([ - { - name: "several accounts, pooled like Usage → Limits", - sources: [ - hub("Hub", [ - { id: "a.json", email: "a@example.com", limits: limitsAt(20) }, - { id: "b.json", email: "b@example.com", limits: limitsAt(60) }, - ]), - ], - rows: [{ label: "Session", left: 60, accounts: 2, from: ["Hub"] }], - }, { name: "accounts across hubs, skipping a hub that failed to read", sources: [ @@ -210,20 +188,6 @@ describe("resolveThreadDetailsLimits", () => { model: "gpt-5.6-sol[1m]", rows: [{ label: "Session", left: 70, accounts: 1, from: ["Hub"] }], }, - { - name: "every account the hub routes the model to, across drivers", - sources: [ - hub("Hub", [ - { id: "a.json", models: ["shared"], limits: limitsAt(10) }, - { id: "c.json", driver: codex, models: ["shared"], limits: limitsAt(30) }, - ]), - ], - model: "shared", - rows: [ - { label: "Session", left: 90, accounts: 1, from: ["Hub"] }, - { label: "Session", left: 70, accounts: 1, from: ["Hub"] }, - ], - }, { name: "only the account the hub served last", sources: [ From 15d591b293702da69627ec4bc0d0409b86b827da Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 03:53:12 +0000 Subject: [PATCH 13/30] fix(server): a Codex custom model provider follows the hub's model routing Codex reports no account for a custom model provider, so mark that auth as a custom endpoint. The usage helpers then route the failing model to the hub accounts that serve it. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/provider/CodexProvider.ts | 3 ++- apps/server/src/provider/ProviderRegistry.test.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 6c2f464c8373..04a5eda368de 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -733,7 +733,8 @@ function accountProbeStatus({ account, rateLimits }: CodexAppServerProviderSnaps }; } - return { status: "ready", auth }; + // No OpenAI login needed means a custom model provider, such as a hub, serves the requests. + return { status: "ready", auth: { ...auth, type: "customEndpoint" } }; } export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(function* ( diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 2e138f6ea2b6..d49416cbd60a 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -478,6 +478,8 @@ it.layer( assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.status, "unknown"); + // regression: #30778 a custom model provider follows the hub's model routing + assert.strictEqual(status.auth.type, "customEndpoint"); // regression: #11873 a custom provider has no subscription to read, which is not a failure assert.deepStrictEqual(status.usageLimits?.unavailable, { reason: "unsupported" }); }), From 6f72d9c8a8d7dae9c6c7ec08af0776fafb90fa01 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:28:35 +0100 Subject: [PATCH 14/30] fix(server): a hub's limits apply only to instances sent to its address A usage limit with no reset time took the reset of whichever hub served the driver, even for an instance on another gateway or a Codex instance that only reported "unsupported". Providers now report the origin of their custom endpoint, hubs report their own, and the reset fallback, routing and thread card use only a hub at the instance's endpoint. A spent Claude model-scoped weekly window (seven_day_) also held back the pool reset for every model. It now counts only for its own model. AI-assisted; verified with the tests and tools listed above. --- .../ProviderEventIngestor.test.ts | 27 ++-- .../orchestration-v2/ProviderEventIngestor.ts | 16 +-- apps/server/src/provider/ClaudeProvider.ts | 5 +- .../server/src/provider/CodexProvider.test.ts | 9 ++ apps/server/src/provider/CodexProvider.ts | 118 +++++++++++------- .../src/provider/ProviderRegistry.test.ts | 10 +- apps/server/src/provider/claudeUsageLimits.ts | 14 ++- .../src/usage/UsageLimitSources.test.ts | 65 ++++++++++ apps/server/src/usage/UsageLimitSources.ts | 37 ++++-- .../chat/ThreadDetailsUsage.logic.test.ts | 22 +++- .../chat/ThreadDetailsUsage.logic.ts | 15 +-- docs/user/usage.md | 6 +- packages/contracts/src/providerUsageLimits.ts | 2 + packages/contracts/src/server.ts | 2 + packages/shared/src/usageLimits.test.ts | 14 ++- packages/shared/src/usageLimits.ts | 25 ++-- 16 files changed, 282 insertions(+), 105 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 1dfcfb46c018..bed7d46aabcc 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -340,7 +340,11 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit { failureClass: "usage_limit", provider: { - auth: { status: "authenticated", type: "customEndpoint" }, + auth: { + status: "authenticated", + type: "customEndpoint", + endpoint: "http://hub:8317", + }, usageLimits: { ...unread, unavailable: { reason: "probeFailed" } }, }, resetAt: hubResetAt, @@ -349,11 +353,20 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit { failureClass: "usage_limit", provider: { - auth: { status: "unknown" }, + auth: { status: "unknown", endpoint: "http://hub:8317" }, usageLimits: { ...unread, unavailable: { reason: "unsupported" } }, }, resetAt: hubResetAt, }, + // regression: #30778 an instance with no hub endpoint gets no hub reset + { + failureClass: "usage_limit", + provider: { + auth: { status: "authenticated", type: "customEndpoint" }, + usageLimits: { ...unread, unavailable: { reason: "unsupported" } }, + }, + resetAt: undefined, + }, ] as const; for (const [index, { failureClass, provider, resetAt, ...run }] of cases.entries()) { own = provider; @@ -392,18 +405,18 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit resetAt, ); } - assert.deepEqual(lookups, Array(6).fill(instance.instanceId)); + assert.deepEqual(lookups, Array(8).fill(instance.instanceId)); assert.deepEqual(hubLookups, [ - `${instance.driverKind}:gpt-run-model`, - `${instance.driverKind}:null`, + `${instance.driverKind}:http://hub:8317:gpt-run-model`, + `${instance.driverKind}:http://hub:8317:${modelSelection.model}`, ]); yield* Deferred.await(refreshed); }).pipe( Effect.provide(makeLayerTest(layerTestEventSink, instances)), Effect.provideService(UsageLimitResetFallback, { - resetAt: (driver, model) => + resetAt: (driver, endpoint, model) => Effect.sync(() => { - hubLookups.push(`${driver}:${model}`); + hubLookups.push(`${driver}:${endpoint}:${model}`); return hubResetAt; }), }), diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 8f05f4173674..4c529dd4b679 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -27,7 +27,6 @@ import * as SqlError from "effect/sql/SqlError"; import { AgentScope } from "@t3tools/shared/AgentScope"; import { isTemporaryWorktreeBranch } from "@t3tools/shared/git"; import { modelSelectionsEqual } from "@t3tools/shared/model"; -import { hubRoutedModel, isProxied } from "@t3tools/shared/usageLimits"; import { ProviderTurnAnalytics, type ProviderTurnAnalyticsContext, @@ -671,11 +670,11 @@ export const layer: Layer.Layer< (instance) => instance?.snapshot.getSnapshot ?? Effect.undefined, ) : undefined; - // A direct login whose own probe failed is not served by the hub. - const proxied = provider !== undefined && isProxied(provider); + // Only an instance sent to a hub's address is served by that hub. + const endpoint = provider?.auth.endpoint; // The failing run's model, which may differ from a selection changed mid-turn. const model = - provider !== undefined && proxied + endpoint !== undefined ? yield* Effect.gen(function* () { const { runs } = input.runId === undefined @@ -686,12 +685,13 @@ export const layer: Layer.Layer< const selection = runs[0]?.modelSelection ?? (yield* projections.getThread(input.threadId)).modelSelection; - return hubRoutedModel(provider, selection.model); + return selection.model; }).pipe(Effect.orElseSucceed(() => null)) : null; - const hubResetAt = proxied - ? yield* usageLimitResets.resetAt(input.event.driver, model) - : null; + const hubResetAt = + endpoint !== undefined + ? yield* usageLimitResets.resetAt(input.event.driver, endpoint, model) + : null; const failure = hubResetAt === null ? reported : { ...reported, resetAt: hubResetAt }; return [ ...dismissed, diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts index eafd128d610f..9772a49dbeb0 100644 --- a/apps/server/src/provider/ClaudeProvider.ts +++ b/apps/server/src/provider/ClaudeProvider.ts @@ -768,6 +768,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( (yield* readClaudeSettingsEnv(claudeSettings, cwd, resolvedEnvironment)).ANTHROPIC_BASE_URL ?? resolvedEnvironment.ANTHROPIC_BASE_URL; const customApiEndpoint = customClaudeApiEndpointHost(baseUrl) !== undefined; + const endpoint = customApiEndpoint ? URL.parse(baseUrl?.trim() ?? "")?.origin : undefined; if (!customApiEndpoint && claudeAuthStatus(capabilities) === "unauthenticated") { const path = yield* Path.Path; const configDir = (yield* makeClaudeEnvironment(claudeSettings, resolvedEnvironment)) @@ -796,7 +797,9 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( // A proxy's ANTHROPIC_AUTH_TOKEN reads as an API key; the endpoint is what the token is for. const authMetadata = apiProviderAuthMetadata(capabilities.apiProvider) ?? - (customApiEndpoint ? { type: "customEndpoint", label: "Custom API endpoint" } : undefined) ?? + (customApiEndpoint + ? { type: "customEndpoint", label: "Custom API endpoint", ...(endpoint ? { endpoint } : {}) } + : undefined) ?? claudeAuthMetadata({ subscriptionType: capabilities.subscriptionType, authMethod: capabilities.tokenSource, diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index c9a992bcf516..d6b76db51b1c 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -2,6 +2,7 @@ import { assert, it } from "@effect/vitest"; import { applyPreferredCodexDefaultModel, + codexCustomEndpoint, codexRoutesSpendNativeQuota, mapCodexModelCapabilities, } from "./CodexProvider.ts"; @@ -181,4 +182,12 @@ it("reports native quota only for an OpenAI login on a native route", () => { name, ); } + assert.strictEqual(codexCustomEndpoint(hub), "https://hub.example.com"); + assert.strictEqual( + codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1" }), + "http://127.0.0.1:8317", + ); + for (const native of [{}, { openai_base_url: "https://chatgpt.com/x" }, null]) { + assert.strictEqual(codexCustomEndpoint(native), undefined); + } }); diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 1bc21ca41caa..80ac8b43d09f 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -82,17 +82,21 @@ const CodexRoutingConfig = Schema.Struct({ }); const decodeCodexRoutingConfig = Schema.decodeUnknownOption(CodexRoutingConfig); -function routesToNativeOpenai(config: unknown): boolean { +// Null is Codex's auth-dependent OpenAI default; undefined is a route that cannot be read. +function codexRouteBaseUrl(config: unknown): string | null | undefined { const decoded = decodeCodexRoutingConfig(config); - if (Option.isNone(decoded)) return false; + if (Option.isNone(decoded)) return undefined; const routing = decoded.value; const providerId = routing.model_provider ?? "openai"; + if (providerId === "openai") return routing.openai_base_url || null; const provider = routing.model_providers?.[providerId]; - if (providerId !== "openai" && !provider) return false; - const baseUrl = - providerId === "openai" ? routing.openai_base_url || undefined : provider?.base_url; - // A provider without an override uses Codex's auth-dependent OpenAI default. - if (baseUrl == null) return true; + return provider ? (provider.base_url ?? null) : undefined; +} + +function routesToNativeOpenai(config: unknown): boolean { + const baseUrl = codexRouteBaseUrl(config); + if (baseUrl === undefined) return false; + if (baseUrl === null) return true; if (!URL.canParse(baseUrl)) return false; const url = new URL(baseUrl); return ( @@ -101,6 +105,15 @@ function routesToNativeOpenai(config: unknown): boolean { ); } +/** The origin of the custom endpoint Codex sends model requests to, if it uses one. */ +export function codexCustomEndpoint(config: unknown): string | undefined { + const baseUrl = codexRouteBaseUrl(config); + const url = baseUrl ? URL.parse(baseUrl) : null; + return url && /^https?:$/.test(url.protocol) && !routesToNativeOpenai(config) + ? url.origin + : undefined; +} + // A stored login does not mean this provider's traffic spends that login's quota. export const codexRoutesSpendNativeQuota = ( account: CodexSchema.V2GetAccountResponse, @@ -128,6 +141,7 @@ const CODEX_PRESENTATION = { export interface CodexAppServerProviderSnapshot { readonly account: CodexSchema.V2GetAccountResponse; + readonly endpoint?: string | undefined; readonly nativeUsage?: boolean; readonly rateLimits?: CodexRateLimitsProbe; readonly version: string | undefined; @@ -619,48 +633,56 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun // Optional discovery must finish before the outer timeout can discard the account result. const deadlineMs = spawnedAtMs + CODEX_AUTH_PROBE_TIMEOUT_MS - CODEX_DISCOVERY_MARGIN_MS; - const [skillsResponse, models, { serviceTier, nativeUsage, rateLimits }] = yield* Effect.all( - [ - withCodexDiscoveryDeadline(client.request("skills/list", { cwds: [input.cwd] }), deadlineMs), - accountResponse.account?.type === "chatgpt" - ? requestCodexChatGptModels({ - client, - codexHome: initialize.codexHome, - spawnedAtMs, - deadlineMs, - }) - : withCodexDiscoveryDeadline(requestAllCodexModels(client), deadlineMs), - // The shared catalog must exclude config from the server's launch project. - withCodexDiscoveryDeadline( - client.request("config/read", { includeLayers: false }), - deadlineMs, - CODEX_CONFIG_PROBE_TIMEOUT_MS, - ).pipe( - Effect.catch(() => Effect.logDebug("Codex config read failed.").pipe(Effect.as(undefined))), - Effect.flatMap((config) => { - // Only a confirmed native route may probe or publish the login's quota. - const nativeUsage = - !input.skipNativeUsage && - config !== undefined && - codexRoutesSpendNativeQuota(accountResponse, config.config); - const readUsage: Effect.Effect = nativeUsage - ? readCodexRateLimits(client, deadlineMs) - : Effect.undefined; - return readUsage.pipe( - Effect.map((rateLimits) => ({ - serviceTier: config?.config.service_tier ?? undefined, - nativeUsage, - rateLimits, - })), - ); - }), - ), - ], - { concurrency: "unbounded" }, - ); + const [skillsResponse, models, { serviceTier, endpoint, nativeUsage, rateLimits }] = + yield* Effect.all( + [ + withCodexDiscoveryDeadline( + client.request("skills/list", { cwds: [input.cwd] }), + deadlineMs, + ), + accountResponse.account?.type === "chatgpt" + ? requestCodexChatGptModels({ + client, + codexHome: initialize.codexHome, + spawnedAtMs, + deadlineMs, + }) + : withCodexDiscoveryDeadline(requestAllCodexModels(client), deadlineMs), + // The shared catalog must exclude config from the server's launch project. + withCodexDiscoveryDeadline( + client.request("config/read", { includeLayers: false }), + deadlineMs, + CODEX_CONFIG_PROBE_TIMEOUT_MS, + ).pipe( + Effect.catch(() => + Effect.logDebug("Codex config read failed.").pipe(Effect.as(undefined)), + ), + Effect.flatMap((config) => { + // Only a confirmed native route may probe or publish the login's quota. + const nativeUsage = + !input.skipNativeUsage && + config !== undefined && + codexRoutesSpendNativeQuota(accountResponse, config.config); + const readUsage: Effect.Effect = nativeUsage + ? readCodexRateLimits(client, deadlineMs) + : Effect.undefined; + return readUsage.pipe( + Effect.map((rateLimits) => ({ + serviceTier: config?.config.service_tier ?? undefined, + endpoint: config ? codexCustomEndpoint(config.config) : undefined, + nativeUsage, + rateLimits, + })), + ); + }), + ), + ], + { concurrency: "unbounded" }, + ); return { account: accountResponse, + endpoint, nativeUsage, ...(rateLimits ? { rateLimits } : {}), version, @@ -962,7 +984,9 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu installed: true, version: snapshot.version ?? null, status: snapshot.models === undefined ? "warning" : accountStatus.status, - auth: accountStatus.auth, + auth: snapshot.endpoint + ? { ...accountStatus.auth, endpoint: snapshot.endpoint } + : accountStatus.auth, ...(accountStatus.message ? { message: accountStatus.message } : snapshot.models === undefined diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index bc0be99f6bad..1449af44c607 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -494,14 +494,16 @@ it.layer( account: null, requiresOpenaiAuth: false, }, + endpoint: "http://127.0.0.1:8317", }), ), ); assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.status, "unknown"); - // regression: #30778 a custom model provider follows the hub's model routing + // regression: #30778 a custom model provider follows the routing of the hub it is sent to assert.strictEqual(status.auth.type, "customEndpoint"); + assert.strictEqual(status.auth.endpoint, "http://127.0.0.1:8317"); // regression: #11873 a custom provider has no subscription to read, which is not a failure assert.deepStrictEqual(status.usageLimits?.unavailable, { reason: "unsupported" }); }), @@ -3886,6 +3888,12 @@ it.layer( assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.type, type); assert.strictEqual(status.auth.label, label); + assert.strictEqual( + status.auth.endpoint, + type === "customEndpoint" + ? new URL(settings.env?.ANTHROPIC_BASE_URL ?? baseUrl).origin + : undefined, + ); }).pipe( Effect.provide( layerMockSpawner((args) => { diff --git a/apps/server/src/provider/claudeUsageLimits.ts b/apps/server/src/provider/claudeUsageLimits.ts index d0acf43bb496..08da2812a642 100644 --- a/apps/server/src/provider/claudeUsageLimits.ts +++ b/apps/server/src/provider/claudeUsageLimits.ts @@ -47,8 +47,20 @@ export const makeClaudeScopedLimitNames = Ref.make({ overageIncluded: undefined, }); +const SCOPED_WINDOW_PREFIX = "seven_day_"; +const scopeToken = (name: string) => name.toLowerCase().replace(/[^a-z0-9]+/g, "_"); + function scopedWindowId(displayName: string): string { - return `seven_day_${displayName.toLowerCase().replace(/[^a-z0-9]+/g, "_")}`; + return `${SCOPED_WINDOW_PREFIX}${scopeToken(displayName)}`; +} + +/** A model-scoped weekly allowance blocks only its own model; an unknown model is not blocked. */ +export function windowLimitsModel( + window: Pick, + model: string | null, +): boolean { + if (!window.id.startsWith(SCOPED_WINDOW_PREFIX)) return true; + return model !== null && scopeToken(model).includes(window.id.slice(SCOPED_WINDOW_PREFIX.length)); } function scopedWindow( diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 8707e640211d..09912b07d2a1 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -31,6 +31,13 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () => "hub.test:8317", "Usage limit source", ]); + expect(snapshots.map((source) => source.origin)).toEqual([ + undefined, + undefined, + undefined, + "https://hub.test:8317", + undefined, + ]); expect(() => encodeConfigEvent({ version: 1, @@ -202,7 +209,14 @@ it("waits for the first refill only when every pooled account of the driver is s }, ]), ]; + // regression: #30778 a spent Fable allowance does not hold back other models expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, null, now)).toBe( + "2026-11-06T13:00:00.000Z", + ); + expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, "claude-sonnet-5-5", now)).toBe( + "2026-11-06T13:00:00.000Z", + ); + expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, "claude-fable-5-2", now)).toBe( "2026-11-06T14:00:00.000Z", ); const withRoom = [...spent, account("c", [session(40, "2026-11-06T15:00:00.000Z")])]; @@ -229,3 +243,54 @@ it("waits for the first refill only when every pooled account of the driver is s UsageLimitSources.hubPoolResetAt(hub([...routed, unlisted]), claude, "claude-opus-5-5", now), ).toBeNull(); }); + +it.effect("takes a reset only from the hub at the instance's endpoint", () => { + const claude = ProviderDriverKind.make("claudeAgent"); + const spentHub = (id: string, origin: string) => ({ + id: UsageLimitSourceId.make(id), + kind: "cliproxy" as const, + label: id, + origin, + checkedAt: "2026-11-06T12:00:00.000Z", + accounts: [ + { + id: "a", + driver: claude, + usageLimits: { + checkedAt: "2026-11-06T12:00:00.000Z", + windows: [ + { + id: "five_hour", + kind: "session" as const, + label: "Session", + usedPercent: 100, + resetsAt: `2026-11-06T1${id === "home" ? 3 : 4}:00:00.000Z`, + }, + ], + }, + }, + ], + }); + return Effect.gen(function* () { + const fallback = yield* UsageLimitSources.UsageLimitResetFallback; + expect(yield* fallback.resetAt(claude, "http://home:8317", null)).toBe( + "2026-11-06T13:00:00.000Z", + ); + // regression: #30778 a gateway that is not a configured hub gets no hub's reset + expect(yield* fallback.resetAt(claude, "http://gateway:8080", null)).toBeNull(); + }).pipe( + Effect.provide( + UsageLimitSources.layerResetFallback.pipe( + Layer.provide( + Layer.mock(UsageLimitSources.UsageLimitSources)({ + current: Effect.succeed([ + spentHub("home", "http://home:8317"), + spentHub("work", "http://work:8317"), + ]), + refresh: Effect.void, + }), + ), + ), + ), + ); +}); diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 55bf69eb8c56..be67cae2120a 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -28,6 +28,7 @@ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretReso 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"; export class UsageLimitSources extends Context.Service< @@ -81,7 +82,14 @@ export const make = Effect.gen(function* () { config: UsageLimitSourceConfig, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); - const base = { id, kind: config.kind, label: sourceLabel(id, config), checkedAt } as const; + const url = URL.parse(config.url); + const base = { + id, + kind: config.kind, + label: sourceLabel(id, config), + ...(url && /^https?:$/.test(url.protocol) ? { origin: url.origin } : {}), + checkedAt, + } as const; const managementKey = yield* readManagementKey(config); if (managementKey.length === 0) { return { ...base, accounts: [], error: missingManagementKey(config) }; @@ -213,7 +221,9 @@ export function hubPoolResetAt( for (const account of sources.flatMap((source) => source.accounts).filter(routes)) { // An unread account may be the one still blocked, so its refill is unknown. if (account.usageLimits.unavailable) return null; - const spent = account.usageLimits.windows.filter((window) => window.usedPercent >= 100); + const spent = account.usageLimits.windows.filter( + (window) => window.usedPercent >= 100 && windowLimitsModel(window, model), + ); if (spent.length === 0) return null; const resets = spent.map((window) => Date.parse(window.resetsAt ?? "")); if (resets.some((reset) => !Number.isFinite(reset))) return null; @@ -227,6 +237,7 @@ export function hubPoolResetAt( export class UsageLimitResetFallback extends Context.Reference<{ readonly resetAt: ( driver: ProviderDriverKind, + endpoint: string, model: string | null, ) => Effect.Effect; }>("t3/usage/UsageLimitResetFallback", { @@ -238,19 +249,19 @@ export const layerResetFallback = Layer.effect( Effect.gen(function* () { const sources = yield* UsageLimitSources; return { - resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, model) { - const serving = (snapshots: ReadonlyArray) => { - const routes = hubRoutes(snapshots, driver, model); - return snapshots.filter((source) => source.accounts.some(routes)); - }; - // Two hubs for one model leave no way to tell which one served the failed turn. - if (serving(yield* sources.current).length !== 1) return null; + resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, endpoint, model) { + // Only the hub at the instance's endpoint serves its turns. + const atEndpoint = (snapshots: ReadonlyArray) => + snapshots.filter((source) => source.origin === endpoint); + if (atEndpoint(yield* sources.current).length === 0) return null; // The terminal waits on this, so a slow hub falls back to its last reading. yield* sources.refresh.pipe(Effect.timeoutOption("5 seconds")); - const hubs = serving(yield* sources.current); - return hubs.length === 1 - ? hubPoolResetAt(hubs, driver, model, DateTime.toEpochMillis(yield* DateTime.now)) - : null; + return hubPoolResetAt( + atEndpoint(yield* sources.current), + driver, + model, + DateTime.toEpochMillis(yield* DateTime.now), + ); }), }; }), diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index bbbfc1a5a56b..b93323555d22 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -51,7 +51,12 @@ function claudeInstance(usageLimits: ServerProviderUsageLimits): ServerProvider installed: true, version: null, status: "ready", - auth: { status: "authenticated", type: "customEndpoint", label: "Custom API endpoint" }, + auth: { + status: "authenticated", + type: "customEndpoint", + label: "Custom API endpoint", + endpoint: "http://127.0.0.1:8317", + }, checkedAt, models: [], slashCommands: [], @@ -83,11 +88,13 @@ function hub( label: string, accounts: readonly HubAccountInput[], error?: string, + origin = "http://127.0.0.1:8317", ): UsageLimitSourceSnapshot { return { id: UsageLimitSourceId.make(label), kind: "cliproxy", label, + origin, checkedAt, accounts: accounts.map((account) => ({ id: account.id, @@ -128,18 +135,29 @@ function shown( } describe("resolveThreadDetailsLimits", () => { - it("keeps a direct login's failed reading instead of an unrelated hub", () => { + it("keeps an instance's failed reading instead of a hub it is not sent to", () => { const failed: ServerProviderUsageLimits = { checkedAt, windows: [], unavailable: { reason: "probeFailed" }, }; const direct = { ...claudeInstance(failed), auth: { status: "authenticated" as const } }; + const elsewhere = claudeInstance(unsupported); const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); + const otherHub = hub( + "Other", + [{ id: "a.json", limits: limitsAt(20) }], + undefined, + "http://hub", + ); expect(resolveThreadDetailsLimits(instanceId, null, [direct], [readableHub], now)).toEqual({ kind: "instance", limits: failed, }); + expect(resolveThreadDetailsLimits(instanceId, null, [elsewhere], [otherHub], now)).toEqual({ + kind: "instance", + limits: unsupported, + }); }); it("keeps an instance's own readable limits over any hub", () => { diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index 24b03c3ba03d..6c7646d29d45 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -11,9 +11,8 @@ import { collectLimitPools, collectProviderUsageLimits, displayLimitWindows, - hubRoutedModel, hubRoutes, - isProxied, + instanceHubs, type LimitPoolWindow, limitsNotice, } from "@t3tools/shared/usageLimits"; @@ -41,14 +40,10 @@ export function resolveThreadDetailsLimits( report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ?? provider.usageLimits; if (!own) return null; - if (limitsNotice(own) === null || !isProxied(provider)) return { kind: "instance", limits: own }; - // A proxied instance reads no limits of its own; its hub pools the accounts it spends. - return ( - hubPool(provider.driver, hubRoutedModel(provider, model), providers, sources, now) ?? { - kind: "instance", - limits: own, - } - ); + const hubs = instanceHubs(provider, sources); + if (limitsNotice(own) === null || hubs.length === 0) return { kind: "instance", limits: own }; + // An instance sent to a hub reads no limits of its own; its hub pools the accounts it spends. + return hubPool(provider.driver, model, providers, hubs, now) ?? { kind: "instance", limits: own }; } export function nextPoolReset(window: LimitPoolWindow, now: number) { diff --git a/docs/user/usage.md b/docs/user/usage.md index e6b3a46d5a14..ebdc60fec27c 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -181,8 +181,10 @@ accounts for other providers are not read. Codex accounts show banked reset cred account and choose **Use reset** to redeem one. No hub plugin is required. This connection supplies usage information; configure -the provider separately to send agent requests through the hub. Remove the hub from the same -settings section when you no longer need it. +the provider separately to send agent requests through the hub. A thread uses the hub's limits and +reset times only when its provider's endpoint has the same address as the hub, for example +`http://127.0.0.1:8317` for both. Remove the hub from the same settings section when you no longer +need it. ## Subscription usage widget diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index cb2afd22e055..73187a728454 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -85,6 +85,8 @@ export const UsageLimitSourceSnapshot = Schema.Struct({ id: UsageLimitSourceId, kind: Schema.Literal("cliproxy"), label: TrimmedNonEmptyString, + /** Origin of the hub, which also serves the agents sent through it. */ + origin: Schema.optional(TrimmedNonEmptyString), checkedAt: IsoDateTime, accounts: ForwardCompatibleArray(UsageLimitSourceAccount), error: Schema.optional(TrimmedNonEmptyString), diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index 3449b01b9905..d3a34539414b 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -70,6 +70,8 @@ export const ServerProviderAuth = Schema.Struct({ subscriptionSharing: Schema.optional(Schema.Boolean), profileId: Schema.optional(TrimmedNonEmptyString), workspaceId: Schema.optional(TrimmedNonEmptyString), + /** Origin of the custom endpoint the instance sends its requests to. */ + endpoint: Schema.optional(TrimmedNonEmptyString), }); export type ServerProviderAuth = typeof ServerProviderAuth.Type; diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index b7c8c7dd1230..e0defe38dee9 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1167,6 +1167,7 @@ describe("/usage-limits", () => { id: UsageLimitSourceId.make("hub"), kind: "cliproxy" as const, label: "Accounts", + origin: "http://hub:8317", checkedAt: limits.checkedAt, accounts: [ { @@ -1347,7 +1348,7 @@ describe("/usage-limits", () => { const proxy = provider({ instanceId: ProviderInstanceId.make("claudex"), driver: ProviderDriverKind.make("claudeAgent"), - auth: { status: "authenticated", type: "customEndpoint" }, + auth: { status: "authenticated", type: "customEndpoint", endpoint: "http://hub:8317" }, }); const ids = (instance: ServerProvider, model: string | null) => collectProviderUsageLimits( @@ -1358,6 +1359,17 @@ describe("/usage-limits", () => { model, )?.accounts.map((account) => account.id); expect(ids(proxy, "gpt-5.6-sol[1m]")).toEqual(["codex", "hub:oss"]); + // regression: #30778 a custom endpoint other than the hub does not follow the hub's routing + const elsewhere = { ...proxy, auth: { ...proxy.auth, endpoint: "http://other:8317" } }; + expect( + collectProviderUsageLimits( + proxy.instanceId, + [selected, elsewhere], + routed, + now, + "gpt-5.6-sol", + ), + ).toBeNull(); const unread = provider({ instanceId: ProviderInstanceId.make("cursor"), driver: ProviderDriverKind.make("cursor"), diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index f99a282383c7..a9f4ee161470 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -841,17 +841,13 @@ export function withUsageLimitsCommands( }); } -/** An instance that sends requests through a hub reads no limits of its own. */ -export function isProxied(provider: ServerProvider): boolean { - return ( - provider.auth.type === "customEndpoint" || - provider.usageLimits?.unavailable?.reason === "unsupported" - ); -} - -/** Only an instance sent to a custom endpoint is known to reach the hub that routes its model. */ -export function hubRoutedModel(provider: ServerProvider, model: string | null): string | null { - return provider.auth.type === "customEndpoint" ? model : null; +/** The hubs an instance sends its requests through: those whose address is its endpoint. */ +export function instanceHubs( + provider: ServerProvider, + sources: UsageLimitSourceSnapshots, +): UsageLimitSourceSnapshots { + const endpoint = provider.auth.endpoint; + return endpoint === undefined ? [] : sources.filter((source) => source.origin === endpoint); } /** The hub accounts that serve a model: those that list it, else every account of the driver. */ @@ -882,7 +878,12 @@ export function collectProviderUsageLimits( ): UsageLimitsReport | null { const selected = providers.find((provider) => provider.instanceId === instanceId); if (!selected) return null; - const routes = hubRoutes(sources, selected.driver, hubRoutedModel(selected, model)); + // Only an instance sent to a hub is known to reach the accounts that hub routes its model to. + const routes = hubRoutes( + sources, + selected.driver, + instanceHubs(selected, sources).length > 0 ? model : null, + ); const drivers = new Set( sources.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), ); From efb91aa5f222b7c4919472eafd16e74ee748214d Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:50:16 +0100 Subject: [PATCH 15/30] fix(server): hub routing and resets ignore quota that cannot serve the model Another hub's model list no longer decides which of this instance's hub accounts serve the model, and the report shows only the instance's own hub. On an Antigravity account, a spent Gemini quota no longer holds back the reset for Claude or GPT models, and the reverse. AI-assisted; verified with the tests and tools listed above. --- .../src/provider/antigravityUsageLimits.ts | 8 +++++++ .../src/usage/UsageLimitSources.test.ts | 12 ++++++++++ apps/server/src/usage/UsageLimitSources.ts | 6 ++++- packages/shared/src/usageLimits.test.ts | 23 +++++++++++++++++++ packages/shared/src/usageLimits.ts | 12 ++++------ 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/apps/server/src/provider/antigravityUsageLimits.ts b/apps/server/src/provider/antigravityUsageLimits.ts index ede0e5c47d20..c46375e52ce1 100644 --- a/apps/server/src/provider/antigravityUsageLimits.ts +++ b/apps/server/src/provider/antigravityUsageLimits.ts @@ -136,6 +136,14 @@ const sha256 = Effect.fnUntraced(function* (text: string) { export const antigravityAccountFingerprint = (userInfo: unknown) => decodeUserInfo(userInfo).pipe(Effect.flatMap((info) => sha256(`google-account\0${info.id}`))); +/** Gemini quota is metered apart from the Claude and GPT quota, so each blocks only its models. */ +export function antigravityWindowLimitsModel( + window: Pick, + model: string | null, +): boolean { + return model === null || window.id.startsWith("gemini") === model.startsWith("gemini"); +} + /** Windows from a `retrieveUserQuotaSummary` answer requested at `requestedAt`. */ export const antigravitySummaryWindows = (response: unknown, requestedAt: number) => decodeSummary(response).pipe(Effect.map((summary) => summaryWindows(summary, requestedAt))); diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 09912b07d2a1..a3ab8eca5144 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -242,6 +242,18 @@ it("waits for the first refill only when every pooled account of the driver is s expect( UsageLimitSources.hubPoolResetAt(hub([...routed, unlisted]), claude, "claude-opus-5-5", now), ).toBeNull(); + // regression: #30778 a spent Gemini quota does not hold back Claude on an Antigravity account + const antigravity = { + ...account("ag", [ + { ...session(100, "2026-11-06T13:00:00.000Z"), id: "3p-5h" }, + { ...session(100, "2026-11-07T12:00:00.000Z"), id: "gemini-5h" }, + ]), + driver: ProviderDriverKind.make("antigravity"), + models: ["claude-opus-4-6-thinking", "gemini-3-pro"], + }; + expect( + UsageLimitSources.hubPoolResetAt(hub([antigravity]), claude, "claude-opus-4-6-thinking", now), + ).toBe("2026-11-06T13:00:00.000Z"); }); it.effect("takes a reset only from the hub at the instance's endpoint", () => { diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index be67cae2120a..ace15b214c86 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -28,6 +28,7 @@ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretReso import * as SecretResolverLayer from "../secrets/SecretResolver.ts"; import { resolveSettingsSecrets } from "../secrets/settingsSecretValue.ts"; import * as Settings from "../serverSettings.ts"; +import { antigravityWindowLimitsModel } from "../provider/antigravityUsageLimits.ts"; import { windowLimitsModel } from "../provider/claudeUsageLimits.ts"; import { makeCliproxyApi } from "./cliproxyApi.ts"; @@ -222,7 +223,10 @@ export function hubPoolResetAt( // An unread account may be the one still blocked, so its refill is unknown. if (account.usageLimits.unavailable) return null; const spent = account.usageLimits.windows.filter( - (window) => window.usedPercent >= 100 && windowLimitsModel(window, model), + (window) => + window.usedPercent >= 100 && + windowLimitsModel(window, model) && + (account.driver !== "antigravity" || antigravityWindowLimitsModel(window, model)), ); if (spent.length === 0) return null; const resets = spent.map((window) => Date.parse(window.resetsAt ?? "")); diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index e0defe38dee9..202a455f561b 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1359,6 +1359,29 @@ describe("/usage-limits", () => { model, )?.accounts.map((account) => account.id); expect(ids(proxy, "gpt-5.6-sol[1m]")).toEqual(["codex", "hub:oss"]); + // regression: #30778 another hub's model list does not decide this hub's routing + const other = { + ...routed[0]!, + id: UsageLimitSourceId.make("other"), + origin: "http://other:8317", + accounts: [ + { + id: "x", + driver: ProviderDriverKind.make("grok"), + models: ["gpt-5.6-sol"], + usageLimits: limits, + }, + ], + }; + expect( + collectProviderUsageLimits( + proxy.instanceId, + [selected, proxy], + [other, ...routed], + now, + "gpt-5.6-sol", + )?.accounts.map((account) => account.id), + ).toEqual(["codex", "hub:oss"]); // regression: #30778 a custom endpoint other than the hub does not follow the hub's routing const elsewhere = { ...proxy, auth: { ...proxy.auth, endpoint: "http://other:8317" } }; expect( diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index a9f4ee161470..1149331fe9cb 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -879,13 +879,11 @@ export function collectProviderUsageLimits( const selected = providers.find((provider) => provider.instanceId === instanceId); if (!selected) return null; // Only an instance sent to a hub is known to reach the accounts that hub routes its model to. - const routes = hubRoutes( - sources, - selected.driver, - instanceHubs(selected, sources).length > 0 ? model : null, - ); + const hubs = instanceHubs(selected, sources); + const routes = hubRoutes(hubs, selected.driver, hubs.length > 0 ? model : null); + const shownHubs = hubs.length > 0 ? hubs : sources; const drivers = new Set( - sources.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), + shownHubs.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), ); if (drivers.size === 0) drivers.add(selected.driver); if (![...drivers].some((driver) => hasProviderUsageLimits(driver, providers, sources))) { @@ -980,7 +978,7 @@ export function collectProviderUsageLimits( limits: shownCredits ? { ...limits, resetCredits: shownCredits } : limits, }); } - for (const source of sources) { + for (const source of shownHubs) { const matching = source.accounts.filter(routes); for (const account of matching) { const key = resolveKey(account); From c909f45f658009d074bb56a36882cb7931a630f6 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:36:34 +0100 Subject: [PATCH 16/30] fix(server): Codex custom providers keep their reported auth type --- apps/server/src/provider/CodexProvider.ts | 3 +-- apps/server/src/provider/ProviderRegistry.test.ts | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 3afa77ceed81..131a4f2e2aa1 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -925,8 +925,7 @@ function accountProbeStatus( }; } - // No OpenAI login needed means a custom model provider, such as a hub, serves the requests. - return { status: "ready", auth: { ...auth, type: "customEndpoint" } }; + return { status: "ready", auth }; } export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(function* ( diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 052f6e093489..a4a2084bb1a1 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -601,8 +601,6 @@ it.layer( assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.status, "unknown"); - // regression: #30778 a custom model provider follows the routing of the hub it is sent to - assert.strictEqual(status.auth.type, "customEndpoint"); assert.strictEqual(status.auth.endpoint, "http://127.0.0.1:8317"); // regression: #11873 a custom provider has no subscription to read, which is not a failure assert.deepStrictEqual(status.usageLimits?.unavailable, { reason: "unsupported" }); From 2a228db0e1b8d60ad9424889f53661193e4bd130 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:36:36 +0100 Subject: [PATCH 17/30] fix(server): hub limits show the routed pool, not a guessed last-served account --- apps/server/src/usage/cliproxyApi.test.ts | 20 ------------------- apps/server/src/usage/cliproxyApi.ts | 20 +------------------ .../chat/ThreadDetailsUsage.logic.ts | 11 +--------- packages/contracts/src/providerUsageLimits.ts | 2 -- 4 files changed, 2 insertions(+), 51 deletions(-) diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 9a1a8d41ddac..0396e54f5426 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -56,7 +56,6 @@ function fixture( email?: string; disabled?: boolean; project_id?: string; - recent_requests?: ReadonlyArray<{ time: string; success: number; failed: number }>; id_token?: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string }; }>; upstream?: (request: RequestBody) => { status: number; body: unknown }; @@ -254,25 +253,6 @@ describe("CLIProxyAPI built-in management API", () => { }), ); - it.effect("dates each account's last served request from the hub's request buckets", () => - Effect.gen(function* () { - yield* TestClock.setTime(Date.parse("2026-10-30T12:05:00Z")); - const bucket = (success: number, failed = 0) => ({ time: "", success, failed }); - const test = fixture({ - accounts: [ - { ...accounts[0]!, recent_requests: [bucket(2), bucket(3), bucket(0, 4)] }, - { ...accounts[1]!, recent_requests: [bucket(0), bucket(0, 1)] }, - ], - }); - const api = yield* test.api; - const result = yield* api.readAccounts(config); - expect(result.map((account) => account.lastServedAt)).toEqual([ - "2026-10-30T11:50:00.000Z", - undefined, - ]); - }), - ); - it.effect("resolves a missing usage plan from current, then legacy, token plans", () => Effect.gen(function* () { const test = fixture({ diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 54ee969a5de6..eb443280ad1e 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -46,7 +46,6 @@ const AuthFile = Schema.Struct({ email: Schema.optional(Schema.String), disabled: Schema.optional(Schema.Boolean), project_id: Schema.optional(Schema.String), - recent_requests: Schema.optional(Schema.Array(Schema.Struct({ success: Schema.Number }))), id_token: Schema.optional( Schema.Struct({ chatgpt_account_id: Schema.optional(Schema.String), @@ -55,18 +54,6 @@ const AuthFile = Schema.Struct({ }), ), }); -// CLIProxyAPI counts each account's requests in 10-minute buckets aligned to the epoch, newest last. -const RECENT_BUCKET_MS = 10 * 60 * 1000; - -function lastServedAt(account: AuthFile, now: number): string | undefined { - const buckets = account.recent_requests ?? []; - const index = buckets.findLastIndex((bucket) => bucket.success > 0); - if (index === -1) return undefined; - const current = Math.floor(now / RECENT_BUCKET_MS) * RECENT_BUCKET_MS; - const start = current - (buckets.length - 1 - index) * RECENT_BUCKET_MS; - return DateTime.formatIso(DateTime.makeUnsafe(start)); -} - const AuthFiles = Schema.Struct({ files: Schema.Array(AuthFile) }); const AuthFileModels = Schema.Struct({ models: Schema.Array(Schema.Struct({ id: Schema.String })), @@ -496,10 +483,8 @@ export const makeCliproxyApi = Effect.gen(function* () { hub: CliproxyHub, account: AuthFile, provider: HubProvider, - listedAt: number, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); - const served = lastServedAt(account, listedAt); const models = yield* routedModels(hub, account).pipe(Effect.option); const call: HubCall = (url, data) => apiCall(hub, account, provider.header(account, platform, architecture), url, data); @@ -519,7 +504,6 @@ export const makeCliproxyApi = Effect.gen(function* () { driver: provider.driver, ...reading, ...(Option.isSome(models) ? { models: models.value } : {}), - ...(served ? { lastServedAt: served } : {}), }; }); @@ -533,14 +517,12 @@ export const makeCliproxyApi = Effect.gen(function* () { : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), ), ); - // Buckets are relative to the listing, so every account shares its time. - const listedAt = DateTime.toEpochMillis(yield* DateTime.now); return yield* Effect.forEach( accounts.flatMap((account) => { const provider = HUB_PROVIDERS.get(account.provider); return provider && !account.disabled ? [{ account, provider }] : []; }), - ({ account, provider }) => readAccount(hub, account, provider, listedAt), + ({ account, provider }) => readAccount(hub, account, provider), { concurrency: 4 }, ); }); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index 6c7646d29d45..42f6d027048b 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -3,7 +3,6 @@ import { type ProviderInstanceId, type ServerProvider, type ServerProviderUsageLimits, - type UsageLimitSourceAccount, type UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { @@ -61,15 +60,7 @@ function hubPool( now: number, ): ThreadDetailsLimits | null { const routes = hubRoutes(sources, driver, model); - const accounts = sources.flatMap((source) => source.accounts.filter(routes)); - // The hub names no account per turn; the latest one it served stands in for the thread's. - const lastServed = accounts - .flatMap((account) => (account.lastServedAt ? [account.lastServedAt] : [])) - .toSorted() - .at(-1); - const shown = (account: UsageLimitSourceAccount) => - routes(account) && (lastServed === undefined || account.lastServedAt === lastServed); - const routed = sources.map((source) => ({ ...source, accounts: source.accounts.filter(shown) })); + const routed = sources.map((source) => ({ ...source, accounts: source.accounts.filter(routes) })); const readable = routed .flatMap((source) => source.accounts) .filter((account) => limitsNotice(account.usageLimits) === null); diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index 3da86925756e..c8490d165184 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -78,8 +78,6 @@ export const UsageLimitSourceAccount = Schema.Struct({ workspaceId: Schema.optional(TrimmedNonEmptyString), /** The models the hub routes to this account. */ models: Schema.optional(Schema.Array(TrimmedNonEmptyString)), - /** Start of the latest window in which the hub served a request with this account. */ - lastServedAt: Schema.optional(IsoDateTime), usageLimits: ServerProviderUsageLimits, }); export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type; From f37fb7dc47e7c37a0fd11441a942475e1ba968c3 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:36:37 +0100 Subject: [PATCH 18/30] fix(server): usage-limit failures read the hub's last reading instead of forcing a refresh --- apps/server/src/usage/UsageLimitSources.test.ts | 2 +- apps/server/src/usage/UsageLimitSources.ts | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 2be4df5ae5fe..d0a97e2adff3 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -297,7 +297,7 @@ it.effect("takes a reset only from the hub at the instance's endpoint", () => { spentHub("home", "http://home:8317"), spentHub("work", "http://work:8317"), ]), - refresh: Effect.void, + refresh: Effect.die("hub refresh"), }), ), ), diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index ace15b214c86..6542ec0e29ea 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -255,13 +255,8 @@ export const layerResetFallback = Layer.effect( return { resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, endpoint, model) { // Only the hub at the instance's endpoint serves its turns. - const atEndpoint = (snapshots: ReadonlyArray) => - snapshots.filter((source) => source.origin === endpoint); - if (atEndpoint(yield* sources.current).length === 0) return null; - // The terminal waits on this, so a slow hub falls back to its last reading. - yield* sources.refresh.pipe(Effect.timeoutOption("5 seconds")); return hubPoolResetAt( - atEndpoint(yield* sources.current), + (yield* sources.current).filter((source) => source.origin === endpoint), driver, model, DateTime.toEpochMillis(yield* DateTime.now), From 2ffd8649c01e121b15ae482d453d0e07c43b4509 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:36:38 +0100 Subject: [PATCH 19/30] test: keep only the regression cases for proxied usage limits --- .../ProviderEventIngestor.test.ts | 33 +--- .../chat/ThreadDetailsUsage.logic.test.ts | 163 ++---------------- packages/shared/src/usageLimits.test.ts | 36 ---- 3 files changed, 17 insertions(+), 215 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index f7c3bd14d047..b0af0d408af3 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -331,37 +331,17 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit { failureClass: "usage_limit", provider: { - auth: { status: "authenticated" }, - usageLimits: { ...unread, unavailable: { reason: "probeFailed" } }, - }, - resetAt: undefined, - }, - { - failureClass: "usage_limit", - provider: { - auth: { - status: "authenticated", - type: "customEndpoint", - endpoint: "http://hub:8317", - }, - usageLimits: { ...unread, unavailable: { reason: "probeFailed" } }, - }, - resetAt: hubResetAt, - runId, - }, - { - failureClass: "usage_limit", - provider: { - auth: { status: "unknown", endpoint: "http://hub:8317" }, + auth: { status: "authenticated", endpoint: "http://hub:8317" }, usageLimits: { ...unread, unavailable: { reason: "unsupported" } }, }, resetAt: hubResetAt, + runId, }, // regression: #30778 an instance with no hub endpoint gets no hub reset { failureClass: "usage_limit", provider: { - auth: { status: "authenticated", type: "customEndpoint" }, + auth: { status: "authenticated" }, usageLimits: { ...unread, unavailable: { reason: "unsupported" } }, }, resetAt: undefined, @@ -404,11 +384,8 @@ it.effect("refreshes the failing instance snapshot after a committed usage_limit resetAt, ); } - assert.deepEqual(lookups, Array(8).fill(instance.instanceId)); - assert.deepEqual(hubLookups, [ - `${instance.driverKind}:http://hub:8317:gpt-run-model`, - `${instance.driverKind}:http://hub:8317:${modelSelection.model}`, - ]); + assert.deepEqual(lookups, Array(4).fill(instance.instanceId)); + assert.deepEqual(hubLookups, [`${instance.driverKind}:http://hub:8317:gpt-run-model`]); yield* Deferred.await(refreshed); }).pipe( Effect.provide(makeLayerTest(layerTestEventSink, instances)), diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index b93323555d22..ddb3dd935ac4 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -8,7 +8,7 @@ import { } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { nextPoolReset, poolSources, resolveThreadDetailsLimits } from "./ThreadDetailsUsage.logic"; +import { poolSources, resolveThreadDetailsLimits } from "./ThreadDetailsUsage.logic"; const now = Date.parse("2026-10-30T12:00:00.000Z"); const checkedAt = "2026-10-30T11:59:00.000Z"; @@ -16,9 +16,9 @@ const claude = ProviderDriverKind.make("claudeAgent"); const codex = ProviderDriverKind.make("codex"); const instanceId = ProviderInstanceId.make("claudeAgent"); -function limitsAt(usedPercent: number, at = checkedAt): ServerProviderUsageLimits { +function limitsAt(usedPercent: number): ServerProviderUsageLimits { return { - checkedAt: at, + checkedAt, windows: [ { id: "five_hour", @@ -32,11 +32,6 @@ function limitsAt(usedPercent: number, at = checkedAt): ServerProviderUsageLimit }; } -function limitsResettingAt(usedPercent: number, resetsAt: string): ServerProviderUsageLimits { - const [window] = limitsAt(usedPercent).windows; - return { checkedAt, windows: [{ ...window!, resetsAt }] }; -} - const unsupported: ServerProviderUsageLimits = { checkedAt, windows: [], @@ -65,29 +60,16 @@ function claudeInstance(usageLimits: ServerProviderUsageLimits): ServerProvider }; } -function signedIn(id: string, email: string, usageLimits: ServerProviderUsageLimits) { - const base = claudeInstance(usageLimits); - return { - ...base, - instanceId: ProviderInstanceId.make(id), - auth: { ...base.auth, email }, - } satisfies ServerProvider; -} - interface HubAccountInput { readonly id: string; - readonly email?: string; readonly driver?: ProviderDriverKind; readonly models?: readonly string[]; - readonly workspaceId?: string; - readonly lastServedAt?: string; readonly limits: ServerProviderUsageLimits; } function hub( label: string, accounts: readonly HubAccountInput[], - error?: string, origin = "http://127.0.0.1:8317", ): UsageLimitSourceSnapshot { return { @@ -99,26 +81,21 @@ function hub( accounts: accounts.map((account) => ({ id: account.id, driver: account.driver ?? claude, - ...(account.email ? { email: account.email } : {}), ...(account.models ? { models: account.models } : {}), - ...(account.workspaceId ? { workspaceId: account.workspaceId } : {}), - ...(account.lastServedAt ? { lastServedAt: account.lastServedAt } : {}), usageLimits: account.limits, })), - ...(error ? { error } : {}), }; } function shown( own: ServerProviderUsageLimits, sources: readonly UsageLimitSourceSnapshot[], - natives: readonly ServerProvider[] = [], - model: string | null = null, + model: string | null, ) { const resolved = resolveThreadDetailsLimits( instanceId, model, - [claudeInstance(own), ...natives], + [claudeInstance(own)], sources, now, ); @@ -144,12 +121,7 @@ describe("resolveThreadDetailsLimits", () => { const direct = { ...claudeInstance(failed), auth: { status: "authenticated" as const } }; const elsewhere = claudeInstance(unsupported); const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); - const otherHub = hub( - "Other", - [{ id: "a.json", limits: limitsAt(20) }], - undefined, - "http://hub", - ); + const otherHub = hub("Other", [{ id: "a.json", limits: limitsAt(20) }], "http://hub"); expect(resolveThreadDetailsLimits(instanceId, null, [direct], [readableHub], now)).toEqual({ kind: "instance", limits: failed, @@ -160,127 +132,16 @@ describe("resolveThreadDetailsLimits", () => { }); }); - it("keeps an instance's own readable limits over any hub", () => { - const own = limitsAt(40); - expect(shown(own, [hub("Hub", [{ id: "a.json", limits: limitsAt(90) }])])).toEqual({ - kind: "instance", - limits: own, - }); - }); - - it.each([ - { - name: "accounts across hubs, skipping a hub that failed to read", - sources: [ - hub("Home", [{ id: "a.json", email: "a@example.com", limits: limitsAt(20) }]), - hub("Work", [{ id: "b.json", email: "b@example.com", limits: limitsAt(40) }]), - hub("Down", [], "The hub answered with HTTP 404."), - ], - rows: [{ label: "Session", left: 70, accounts: 2, from: ["Home", "Work"] }], - }, - { - name: "only this driver's readable accounts", - sources: [ - hub("Hub", [ - { id: "a.json", email: "a@example.com", limits: limitsAt(50) }, - { id: "c.json", email: "c@example.com", driver: codex, limits: limitsAt(0) }, - { id: "d.json", email: "d@example.com", limits: unsupported }, - ]), - ], - rows: [{ label: "Session", left: 50, accounts: 1, from: ["Hub"] }], - }, - { - name: "a login signed in natively and on a hub, counted once at its freshest reading", - sources: [hub("Hub", [{ id: "a.json", email: "a@example.com", limits: limitsAt(90) }])], - natives: [signedIn("claude-a", "a@example.com", limitsAt(30, "2026-10-30T11:59:30.000Z"))], - rows: [{ label: "Session", left: 70, accounts: 1, from: ["Signed-in accounts"] }], - }, - { - name: "only the accounts the hub routes the model to, even of another driver", - sources: [ - hub("Hub", [ - { id: "a.json", models: ["claude-opus-5-5"], limits: limitsAt(10) }, - { id: "c.json", driver: codex, models: ["gpt-5.6-sol"], limits: limitsAt(30) }, - ]), - ], - model: "gpt-5.6-sol[1m]", - rows: [{ label: "Session", left: 70, accounts: 1, from: ["Hub"] }], - }, - { - name: "only the account the hub served last", - sources: [ - hub("Hub", [ - { id: "a.json", lastServedAt: "2026-10-30T11:50:00.000Z", limits: limitsAt(20) }, - { id: "b.json", lastServedAt: "2026-10-30T10:00:00.000Z", limits: limitsAt(60) }, - { id: "c.json", limits: limitsAt(0) }, - ]), - ], - rows: [{ label: "Session", left: 80, accounts: 1, from: ["Hub"] }], - }, - { - name: "every account the hub served in the same latest window", - sources: [ - hub("Hub", [ - { id: "a.json", lastServedAt: "2026-10-30T11:50:00.000Z", limits: limitsAt(20) }, - { id: "b.json", lastServedAt: "2026-10-30T11:50:00.000Z", limits: limitsAt(60) }, - ]), - ], - rows: [{ label: "Session", left: 60, accounts: 2, from: ["Hub"] }], - }, - { - name: "a hub workspace without the same login's other workspace", - sources: [ - hub("Hub", [ - { id: "c.json", email: "c@example.com", workspaceId: "w1", limits: limitsAt(100) }, - ]), - ], - natives: [ - { - ...signedIn("claude-c", "c@example.com", limitsAt(0)), - auth: { status: "authenticated", email: "c@example.com", workspaceId: "w2" }, - } satisfies ServerProvider, - ], - rows: [{ label: "Session", left: 0, accounts: 1, from: ["Hub"] }], - }, - ])("pools $name when the instance reads no limits", ({ sources, natives, model, rows }) => { - expect(shown(unsupported, sources, natives, model)).toEqual({ kind: "pool", rows }); - }); - - it("names the next reset that refills quota, skipping an unused account's 0% reset", () => { - const soon = "2026-10-30T12:10:00.000Z"; - const later = "2026-10-30T14:00:00.000Z"; - const resolved = resolveThreadDetailsLimits( - instanceId, - null, - [claudeInstance(unsupported)], - [ - hub("Hub", [ - { id: "a.json", email: "a@example.com", limits: limitsResettingAt(0, soon) }, - { id: "b.json", email: "b@example.com", limits: limitsResettingAt(80, later) }, - ]), - ], - now, - ); - if (resolved?.kind !== "pool") throw new Error("expected a pool"); - const [window] = resolved.windows; - expect(window!.resets.map((reset) => reset.restoresPercent)).toEqual([0, 40]); - expect(nextPoolReset(window!, now)).toMatchObject({ - at: Date.parse(later), - restoresPercent: 40, - }); - }); - - it("keeps the instance's notice when no hub has a readable account for its driver", () => { + it("pools only the accounts the hub routes the model to, even of another driver", () => { const sources = [ hub("Hub", [ - { id: "c.json", driver: codex, limits: limitsAt(10) }, - { id: "d.json", limits: unsupported }, + { id: "a.json", models: ["claude-opus-5-5"], limits: limitsAt(10) }, + { id: "c.json", driver: codex, models: ["gpt-5.6-sol"], limits: limitsAt(30) }, ]), ]; - const natives = [signedIn("claude-b", "b@example.com", limitsAt(60))]; - expect(shown(unsupported, sources, natives)).toEqual({ - kind: "instance", - limits: unsupported, + expect(shown(unsupported, sources, "gpt-5.6-sol[1m]")).toEqual({ + kind: "pool", + rows: [{ label: "Session", left: 70, accounts: 1, from: ["Hub"] }], }); }); }); diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 567d470ac86f..2d6646b7f011 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1493,42 +1493,6 @@ describe("/usage-limits", () => { "gpt-5.6-sol", ), ).toBeNull(); - const unread = provider({ - instanceId: ProviderInstanceId.make("cursor"), - driver: ProviderDriverKind.make("cursor"), - usageLimits: { ...limits, windows: [], unavailable: { reason: "unsupported" } }, - }); - expect( - collectProviderUsageLimits( - unread.instanceId, - [unread], - routed, - now, - "gpt-5.6-sol", - )?.accounts.map((account) => account.id) ?? [], - ).toEqual(["cursor"]); - const both = [ - { - ...routed[0]!, - accounts: [ - ...routed[0]!.accounts, - { - id: "claude-hub", - driver: ProviderDriverKind.make("claudeAgent"), - models: ["gpt-5.6-sol"], - usageLimits: limits, - }, - ], - }, - ]; - const reversed = [{ ...both[0]!, accounts: [...both[0]!.accounts].reverse() }]; - for (const hubs of [both, reversed]) { - expect( - collectProviderUsageLimits(proxy.instanceId, [selected, proxy], hubs, now, "gpt-5.6-sol") - ?.accounts.map((account) => account.id) - .toSorted(), - ).toEqual(["codex", "hub:claude-hub", "hub:oss"]); - } expect(ids(selected, "gpt-5.6-sol")).toEqual(["codex", "hub:oss"]); }); From 84e1e4cd2b9dba58f42addeb556dda3f90e056c2 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:25:17 +0100 Subject: [PATCH 20/30] fix(shared): a hub account with no models stays out of an alias's fallback pool An alias such as opus matches no listed model, so routing falls back to every account of the driver. An account that lists no models serves none and could hide the pool's reset time. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 4 ++++ packages/shared/src/usageLimits.ts | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index d0a97e2adff3..9b832edb7582 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -217,6 +217,10 @@ it("waits for the first refill only when every pooled account of the driver is s expect(UsageLimitSources.hubPoolResetAt(hub(spent), claude, "claude-fable-5-2", now)).toBe( "2026-11-06T14:00:00.000Z", ); + const idle = account("idle", [session(20, "2026-11-06T15:00:00.000Z")], []); + expect(UsageLimitSources.hubPoolResetAt(hub([...spent, idle]), claude, "opus", now)).toBe( + "2026-11-06T13:00:00.000Z", + ); const withRoom = [...spent, account("c", [session(40, "2026-11-06T15:00:00.000Z")])]; expect(UsageLimitSources.hubPoolResetAt(hub(withRoom), claude, null, now)).toBeNull(); const unread = { diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index fa3b1c69cc35..dff8bdbc1a05 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -902,7 +902,8 @@ export function hubRoutes( sources.flatMap((source) => source.accounts.filter(listed).map((account) => account.driver)), ); // Model ids such as "opus" are aliases no hub lists, so no listing falls back to the driver. - if (serving.size === 0) return (account) => account.driver === driver; + if (serving.size === 0) + return (account) => account.driver === driver && account.models?.length !== 0; // An account whose model list was unreadable may serve the model too. return (account) => (account.models ? listed(account) : serving.has(account.driver)); } From d8e5ec88fc76e82b9ac49a11d5288f900f466bd5 Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:44:39 +0100 Subject: [PATCH 21/30] wip: replay recorders and codex usage_limit recording --- .../record-claude-agent-sdk-replay-fixture.ts | 7 ++++++ .../record-codex-app-server-replay-fixture.ts | 15 +++++++++++++ .../usage_limit/codex_transcript.ndjson | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/codex_transcript.ndjson diff --git a/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts b/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts index 49c03c88f45a..a0fcb2122f16 100644 --- a/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts +++ b/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts @@ -82,6 +82,13 @@ const CLAUDE_RECORDINGS = { queryMode: "streaming", enableTools: true, }, + // Record with ANTHROPIC_BASE_URL at a CLIProxyAPI hub whose upstream accounts answer 429. + usage_limit: { + prompts: [SIMPLE_PROMPT], + defaultTranscriptFile: "fixtures/usage_limit/claude_transcript.ndjson", + queryMode: "streaming", + enableTools: true, + }, multi_turn: { prompts: [MULTI_TURN_FIRST_PROMPT, MULTI_TURN_SECOND_PROMPT], defaultTranscriptFile: "fixtures/multi_turn/claude_transcript.ndjson", diff --git a/apps/server/scripts/record-codex-app-server-replay-fixture.ts b/apps/server/scripts/record-codex-app-server-replay-fixture.ts index 202e72de32a4..d0cd4185c600 100644 --- a/apps/server/scripts/record-codex-app-server-replay-fixture.ts +++ b/apps/server/scripts/record-codex-app-server-replay-fixture.ts @@ -71,6 +71,7 @@ const CODEX_REPLAY_MODEL_OVERRIDE = readArgValue("--model") ?? process.env.T3_CO const SCENARIO_NAMES = [ "simple", + "usage_limit", "tool_call_read_only_on_request", "tool_call_workspace_never", "tool_call_restricted_granular", @@ -355,6 +356,20 @@ function scenarios(): ReadonlyArray { }, ], }, + { + name: "usage_limit", + fileName: "usage_limit.ndjson", + description: + "One turn against a model provider that answers 429 usage_limit_reached with resets_at, as a CLIProxyAPI hub forwards it.", + runs: [ + { + name: "usage_limit", + description: "Single turn that hits the usage limit.", + prompt: SIMPLE_PROMPT, + steps: [{ type: "turn", label: "usage_limit", prompt: SIMPLE_PROMPT }], + }, + ], + }, { name: "tool_call_read_only_on_request", fileName: "tool_call_read_only_on_request.ndjson", diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/codex_transcript.ndjson b/apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/codex_transcript.ndjson new file mode 100644 index 000000000000..1b874382aaee --- /dev/null +++ b/apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/codex_transcript.ndjson @@ -0,0 +1,22 @@ +{"type":"transcript_start","provider":"codex","protocol":"codex.app-server","version":"0.169.0","scenario":"usage_limit","metadata":{"source":"record-codex-app-server-replay-fixture","fileName":"usage_limit.ndjson","description":"One turn against a model provider that answers 429 usage_limit_reached with resets_at, as a CLIProxyAPI hub forwards it.","model":"gpt-6-luna"}} +{"type":"expect_outbound","label":"initialize","frame":{"id":1,"method":"initialize","params":{"capabilities":{"experimentalApi":true,"extensions":{"io.modelcontextprotocol/ui":{"mimeTypes":["text/html;profile=mcp-app"]}},"optOutNotificationMethods":["turn/diff/updated"]},"clientInfo":{"name":"T3 Code","title":"T3 Code","version":"0.1.9"}}}} +{"type":"emit_inbound","label":"initialize","frame":{"id":1,"result":{"userAgent":"T3 Code/0.169.0 (Ubuntu 24.4.0; x86_64) unknown (T3 Code; 0.1.9)","codexHome":"/tmp/replay-rec/codexhome2","platformFamily":"unix","platformOs":"linux"}}} +{"type":"expect_outbound","label":"initialized","frame":{"method":"initialized"}} +{"type":"expect_outbound","label":"thread/start","frame":{"id":2,"method":"thread/start","params":{"config":{"tools.update_plan.enabled":true,"mcp_optional_startup_grace_ms":10000,"features.concurrent_reasoning_summaries":true},"cwd":"","model":"gpt-6-luna"}}} +{"type":"emit_inbound","label":"remoteControl/status/changed","frame":{"method":"remoteControl/status/changed","params":{"status":"disabled","serverName":"replay-host","installationId":"00000000-0000-4000-8000-000000000000","environmentId":null},"emittedAtMs":1794941012204}} +{"type":"emit_inbound","label":"thread/start","frame":{"id":2,"result":{"thread":{"id":"01a1ead2-496c-7351-94e4-0708524d5b6c","environments":[{"environmentId":"local","cwd":"/home/replay-user/t3code/apps/server","runtimeWorkspaceRoots":["/home/replay-user/t3code/apps/server"]}],"extra":null,"sessionId":"01a1ead2-496c-7351-94e4-0708524d5b6c","forkedFromId":null,"attachedToThreadId":null,"parentThreadId":null,"preview":"","ephemeral":false,"section":null,"sectionEnteredAt":null,"projectId":null,"historyMode":"paginated","modelProvider":"cliproxy","model":"gpt-6-luna","reasoningEffort":null,"createdAt":1794941013,"updatedAt":1794941013,"recencyAt":1794941013,"status":{"type":"idle"},"path":"/tmp/replay-rec/codexhome2/sessions/2026/11/17/rollout-2026-11-17T19-43-32-01a1ead2-496c-7351-94e4-0708524d5b6c.jsonl","cwd":"/home/replay-user/t3code/apps/server","cliVersion":"0.169.0","originator":"T3 Code","source":"vscode","canAcceptDirectInput":true,"threadSource":null,"agentNickname":null,"agentRole":null,"gitInfo":null,"name":null,"daybreakEnabled":null,"turns":[]},"model":"gpt-6-luna","modelProvider":"cliproxy","serviceTier":null,"disabledPluginIds":[],"cwd":"/home/replay-user/t3code/apps/server","runtimeWorkspaceRoots":["/home/replay-user/t3code/apps/server"],"instructionSources":["/home/replay-user/t3code/AGENTS.md"],"approvalPolicy":"on-request","approvalsReviewer":"user","sandbox":{"type":"readOnly","networkAccess":false},"activePermissionProfile":{"id":":read-only","extends":null},"reasoningEffort":null,"multiAgentMode":"explicitRequestOnly"}}} +{"type":"expect_outbound","label":"turn/start","frame":{"id":3,"method":"turn/start","params":{"approvalPolicy":"never","approvalsReviewer":"user","cwd":"","input":[{"text":"Respond with the following text: fixture simple ok","type":"text"}],"model":"gpt-6-luna","sandboxPolicy":{"type":"dangerFullAccess"},"summary":"detailed","threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c"}}} +{"type":"emit_inbound","label":"thread/started","frame":{"method":"thread/started","params":{"thread":{"id":"01a1ead2-496c-7351-94e4-0708524d5b6c","environments":[{"environmentId":"local","cwd":"/home/replay-user/t3code/apps/server","runtimeWorkspaceRoots":["/home/replay-user/t3code/apps/server"]}],"extra":null,"sessionId":"01a1ead2-496c-7351-94e4-0708524d5b6c","forkedFromId":null,"attachedToThreadId":null,"parentThreadId":null,"preview":"","ephemeral":false,"section":null,"sectionEnteredAt":null,"projectId":null,"historyMode":"paginated","modelProvider":"cliproxy","model":"gpt-6-luna","reasoningEffort":null,"createdAt":1794941013,"updatedAt":1794941013,"recencyAt":1794941013,"status":{"type":"idle"},"path":"/tmp/replay-rec/codexhome2/sessions/2026/11/17/rollout-2026-11-17T19-43-32-01a1ead2-496c-7351-94e4-0708524d5b6c.jsonl","cwd":"/home/replay-user/t3code/apps/server","cliVersion":"0.169.0","originator":"T3 Code","source":"vscode","canAcceptDirectInput":true,"threadSource":null,"agentNickname":null,"agentRole":null,"gitInfo":null,"name":null,"daybreakEnabled":null,"turns":[]}},"emittedAtMs":1794941013411}} +{"type":"emit_inbound","label":"warning","frame":{"method":"warning","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","message":"Under-development features enabled: concurrent_reasoning_summaries. Under-development features are incomplete and may behave unpredictably. To suppress this warning, set `suppress_unstable_features_warning = true` in /tmp/replay-rec/codexhome2/config.toml."},"emittedAtMs":1794941013460}} +{"type":"emit_inbound","label":"thread/settings/updated","frame":{"method":"thread/settings/updated","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","threadSettings":{"disabledPluginIds":[],"cwd":"/home/replay-user/t3code/apps/server","environments":[{"environmentId":"local","cwd":"/home/replay-user/t3code/apps/server","runtimeWorkspaceRoots":["/home/replay-user/t3code/apps/server"]}],"approvalPolicy":"never","approvalsReviewer":"user","sandboxPolicy":{"type":"dangerFullAccess"},"activePermissionProfile":null,"model":"gpt-6-luna","modelProvider":"cliproxy","serviceTier":null,"effort":null,"summary":"detailed","collaborationMode":{"mode":"default","settings":{"model":"gpt-6-luna","reasoning_effort":null,"developer_instructions":null}},"multiAgentMode":"explicitRequestOnly","personality":null}},"emittedAtMs":1794941014326}} +{"type":"emit_inbound","label":"turn/start","frame":{"id":3,"result":{"turn":{"id":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","rootTurnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":null,"completedAt":null,"durationMs":null}}}} +{"type":"emit_inbound","label":"thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","status":{"type":"active","activeFlags":[]}},"emittedAtMs":1794941014740}} +{"type":"emit_inbound","label":"turn/started","frame":{"method":"turn/started","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","turn":{"id":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","rootTurnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","items":[],"itemsView":"notLoaded","status":"inProgress","error":null,"startedAt":1794941014,"completedAt":null,"durationMs":null}},"emittedAtMs":1794941014740}} +{"type":"emit_inbound","label":"item/started","frame":{"method":"item/started","params":{"item":{"type":"userMessage","id":"01a1ead2-55d6-7383-b87e-19e937f58a4a","clientId":null,"content":[{"type":"text","text":"Respond with the following text: fixture simple ok","text_elements":[]}],"startedAtMs":1794941015695,"completedAtMs":null},"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","turnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","startedAtMs":1794941015695},"emittedAtMs":1794941015695}} +{"type":"emit_inbound","label":"item/completed","frame":{"method":"item/completed","params":{"item":{"type":"userMessage","id":"01a1ead2-55d6-7383-b87e-19e937f58a4a","clientId":null,"content":[{"type":"text","text":"Respond with the following text: fixture simple ok","text_elements":[]}],"startedAtMs":1794941015695,"completedAtMs":1794941015695},"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","turnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","contentTokenCount":null,"completedAtMs":1794941015695},"emittedAtMs":1794941015815}} +{"type":"emit_inbound","label":"account/rateLimits/updated","frame":{"method":"account/rateLimits/updated","params":{"rateLimits":{"limitId":"codex","limitName":null,"normalModelSlug":null,"primary":null,"secondary":null,"credits":null,"individualLimit":null,"spendControlReached":null,"planType":null,"rateLimitReachedType":null}},"emittedAtMs":1794941016145}} +{"type":"emit_inbound","label":"thread/status/changed","frame":{"method":"thread/status/changed","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","status":{"type":"systemError"}},"emittedAtMs":1794941016146}} +{"type":"emit_inbound","label":"error","frame":{"method":"error","params":{"error":{"message":"You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits or try again at Nov 21st, 2026 8:38 PM.","codexErrorInfo":"usageLimitExceeded","additionalDetails":null,"misalignment":null},"willRetry":false,"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","turnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05"},"emittedAtMs":1794941016146}} +{"type":"emit_inbound","label":"thread/readState/changed","frame":{"method":"thread/readState/changed","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","readState":{"firstUnread":{"type":"turn","turnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05"},"revision":"f6e829d3-d329-41ac-8878-7e0f444447a8"}},"emittedAtMs":1794941016250}} +{"type":"emit_inbound","label":"turn/completed","frame":{"method":"turn/completed","params":{"threadId":"01a1ead2-496c-7351-94e4-0708524d5b6c","turn":{"id":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","rootTurnId":"01a1ead2-4e5d-76b0-af6a-d8ec77e2ed05","items":[],"itemsView":"notLoaded","status":"failed","error":{"message":"You’ve hit your usage limit. Upgrade to Pro (https://chatgpt.com/explore/pro), visit https://chatgpt.com/settings/usage to purchase more credits or try again at Nov 21st, 2026 8:38 PM.","codexErrorInfo":"usageLimitExceeded","additionalDetails":null,"misalignment":null},"startedAt":1794941014,"completedAt":1794941016,"durationMs":1914}},"emittedAtMs":1794941016251}} +{"type":"runtime_exit","status":"success"} From f843f15699da498e6e37b51ba8c6f3496ef4c21a Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:44:28 +0100 Subject: [PATCH 22/30] test(server): replay a real usage-limited Claude turn through a CLIProxyAPI hub The Claude side was tested only with hand-built snapshots. A real Claude Code 2.1.326 turn through a real CLIProxyAPI 7.2.130 hub, whose upstream answered 429, is now a replay fixture: the CLI emits no rate_limit_event and no reset. Replayed alone, the failure keeps no reset. Replayed with the instance at the hub's endpoint, the ingestor takes the refill of the hub accounts that serve the model, and an unrouted account with room does not block it. Unit rows the replay covers are removed. Review fixes: - a proxied /usage-limits no longer lists local logins the hub does not route to - the thread card ignores a composer draft that no longer applies - UsageLimitSourceAccount keeps `models`, which the main merge dropped --- .../ClaudeReplayFixtures.integration.test.ts | 13 +++++++++---- apps/web/src/components/ChatView.tsx | 7 ++++--- packages/contracts/src/providerUsageLimits.ts | 2 ++ packages/shared/src/usageLimits.test.ts | 10 ++++++++-- packages/shared/src/usageLimits.ts | 9 ++++++++- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts index afd5255596fc..d8a8157a8497 100644 --- a/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts @@ -153,10 +153,17 @@ describe("Claude Agent SDK replay fixtures", () => { checkedAt: "", accounts, }) as UsageLimitSourceSnapshot; - const result = yield* runReplayFixture({ + yield* runReplayFixture({ fixtureName: fixture.name, buildInput: fixture.buildInput, - driver: { ...driver, assertOutput: () => undefined }, + driver: { + ...driver, + assertOutput: (result, transcript) => + assert.equal( + claudeUsageLimitFailure(result, transcript).resetAt, + "2033-05-19T07:20:00.000Z", + ), + }, harness: ClaudeOrchestratorReplayHarness, providerInstances: { getInstance: () => @@ -189,8 +196,6 @@ describe("Claude Agent SDK replay fixtures", () => { ), ), ); - const transcript = yield* readClaudeTranscriptFixture("usage_limit"); - assert.equal(claudeUsageLimitFailure(result, transcript).resetAt, "2033-05-19T07:20:00.000Z"); }), ); diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 8b4334734ea4..f4ee02b034fa 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1366,9 +1366,10 @@ export default function ChatView(props: ChatViewProps) { const activeProviderInstanceId = selectedProviderEntry?.instanceId ?? null; const activeProviderStatus = selectedProviderEntry?.snapshot ?? null; const activeUsageModel = - [composerSelectedModelSelection, activeThread?.modelSelection].find( - (selection) => selection?.instanceId === activeProviderInstanceId, - )?.model ?? null; + [ + composerModelSelectionApplies ? composerSelectedModelSelection : null, + activeThread?.modelSelection, + ].find((selection) => selection?.instanceId === activeProviderInstanceId)?.model ?? null; const conversationProviderStatus = providerStatuses.find((status) => status.instanceId === activeRuntime?.providerInstanceId) ?? activeProviderStatus; diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index 446d4f1f3184..4b10f3745d39 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -78,6 +78,8 @@ export const UsageLimitSourceAccount = Schema.Struct({ email: Schema.optional(TrimmedNonEmptyString), plan: Schema.optional(TrimmedNonEmptyString), workspaceId: Schema.optional(TrimmedNonEmptyString), + /** The models the hub routes to this account. */ + models: Schema.optional(Schema.Array(TrimmedNonEmptyString)), usageLimits: ServerProviderUsageLimits, }); export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type; diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 8b2b2dcd7b56..4ab8f58f0d65 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1570,10 +1570,16 @@ describe("/usage-limits", () => { driver: ProviderDriverKind.make("claudeAgent"), auth: { status: "authenticated", type: "customEndpoint", endpoint: "http://hub:8317" }, }); + // regression: #30778 a local login the hub does not route to is not this thread's account + const unrelated = provider({ + instanceId: ProviderInstanceId.make("codex-personal"), + usageLimits: limits, + auth: { status: "authenticated", email: "personal@example.com" }, + }); const ids = (instance: ServerProvider, model: string | null) => collectProviderUsageLimits( instance.instanceId, - [selected, proxy], + [selected, proxy, unrelated], routed, now, model, @@ -1613,7 +1619,7 @@ describe("/usage-limits", () => { "gpt-5.6-sol", ), ).toBeNull(); - expect(ids(selected, "gpt-5.6-sol")).toEqual(["codex", "hub:oss"]); + expect(ids(selected, "gpt-5.6-sol")).toEqual(["codex", "codex-personal", "hub:oss"]); }); it("keeps the freshest usable native duplicate and the freshest native credits in either order", () => { diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index f3e664a5bed5..5cf9ce581868 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -952,6 +952,11 @@ export function collectProviderUsageLimits( ...providers.filter((provider) => provider.enabled).map(nativeReport), ...sources.flatMap((source) => source.accounts), ]); + // Behind a hub, a local login counts only when it is one of the accounts the hub routes to. + const routedKeys = + hubs.length > 0 + ? new Set(hubs.flatMap((source) => source.accounts.filter(routes).map(resolveKey))) + : null; type NativeSnapshot = { provider: ServerProvider; limits: ServerProviderUsageLimits }; const nativeByAccount = new Map< string, @@ -959,8 +964,10 @@ export function collectProviderUsageLimits( >(); for (const provider of providersWithLimits(providers)) { if (!drivers.has(provider.driver) || !provider.usageLimits) continue; + const accountKey = resolveKey(nativeReport(provider)); + if (routedKeys && (accountKey === null || !routedKeys.has(accountKey))) continue; const snapshot = { provider, limits: provider.usageLimits }; - const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`; + const key = accountKey ?? `instance:${provider.instanceId}`; const previous = nativeByAccount.get(key); const previousUsable = previous ? limitsNotice(previous.usage.limits) === null : false; const usable = limitsNotice(snapshot.limits) === null; From f29130dd98202ceb63a3bf1cf2ba9b05102e8b8e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 16:55:48 -0800 Subject: [PATCH 23/30] fix(web): localize the hub pool rows on the thread card Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/chat/ThreadDetailsUsage.logic.ts | 2 +- apps/web/src/i18n/translations/de.json | 4 ++++ apps/web/src/i18n/translations/en.json | 4 ++++ apps/web/src/i18n/translations/es.json | 4 ++++ apps/web/src/i18n/translations/fr.json | 4 ++++ apps/web/src/i18n/translations/ja.json | 4 ++++ apps/web/src/i18n/translations/ko.json | 4 ++++ apps/web/src/i18n/translations/pt-BR.json | 4 ++++ apps/web/src/i18n/translations/ru.json | 4 ++++ apps/web/src/i18n/translations/sv.json | 4 ++++ apps/web/src/i18n/translations/zh.json | 4 ++++ 11 files changed, 41 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index e91c7e472cda..4d7131bdac3b 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -13,8 +13,8 @@ import { hubRoutes, instanceHubs, type LimitPoolWindow, - limitsNotice, } from "@t3tools/shared/usageLimits"; +import { limitsNotice } from "@t3tools/shared/usageLimitsCopy"; export type ThreadDetailsLimits = | { readonly kind: "instance"; readonly limits: ServerProviderUsageLimits } diff --git a/apps/web/src/i18n/translations/de.json b/apps/web/src/i18n/translations/de.json index 730949ecfc30..f6e630d587d6 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -4169,6 +4169,10 @@ "ebb26f93fb83a71b": "{label}: {left} % übrig", "1b16346722b65812": "{timeLeft} % des Zeitfensters übrig", "728c6cc1431e76c8": "Wird in {duration} zurückgesetzt · {time}", + "23622878aeee86d9": "Angemeldete Konten", + "d28c53157c8c0f5f": "{label}: {left} % übrig über {accounts} Konten", + "ba03873d5356238b": "+{percent} % in {duration} · {time}", + "78c1d280f021a28f": "Von {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "Die Zwischenablage-API ist beim Kopieren der Admin-Anfrage nicht verfügbar.", "aef4bec20e799aab": "Die Admin-Anfrage konnte nicht in die Zwischenablage kopiert werden.", diff --git a/apps/web/src/i18n/translations/en.json b/apps/web/src/i18n/translations/en.json index 64482e18216a..f362bb45dc10 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -4168,6 +4168,10 @@ "ebb26f93fb83a71b": "{label}: {left}% left", "1b16346722b65812": "{timeLeft}% of the window left", "728c6cc1431e76c8": "Resets in {duration} · {time}", + "23622878aeee86d9": "Signed-in accounts", + "d28c53157c8c0f5f": "{label}: {left}% left across {accounts} accounts", + "ba03873d5356238b": "+{percent}% in {duration} · {time}", + "78c1d280f021a28f": "From {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "Clipboard API is unavailable while copying admin request.", "aef4bec20e799aab": "Failed to copy admin request to the clipboard.", diff --git a/apps/web/src/i18n/translations/es.json b/apps/web/src/i18n/translations/es.json index 8c3bd00183ec..b6941561ef00 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -4166,6 +4166,10 @@ "ebb26f93fb83a71b": "{label}: {left} % restante", "1b16346722b65812": "Queda un {timeLeft}% de la ventana", "728c6cc1431e76c8": "Se restablece en {duration} · {time}", + "23622878aeee86d9": "Cuentas con sesión iniciada", + "d28c53157c8c0f5f": "{label}: queda un {left} % entre {accounts} cuentas", + "ba03873d5356238b": "+{percent} % en {duration} · {time}", + "78c1d280f021a28f": "De {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "La API del portapapeles no está disponible al copiar la solicitud de administrador.", "aef4bec20e799aab": "No se pudo copiar la solicitud de administrador al portapapeles.", diff --git a/apps/web/src/i18n/translations/fr.json b/apps/web/src/i18n/translations/fr.json index 17258a83acd7..6880ea1ad167 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -4167,6 +4167,10 @@ "ebb26f93fb83a71b": "{label} : {left} % restant", "1b16346722b65812": "{timeLeft} % de la fenêtre restant", "728c6cc1431e76c8": "Réinitialisation dans {duration} · {time}", + "23622878aeee86d9": "Comptes connectés", + "d28c53157c8c0f5f": "{label} : {left} % restants sur {accounts} comptes", + "ba03873d5356238b": "+{percent} % dans {duration} · {time}", + "78c1d280f021a28f": "Depuis {sources}", "e16b0b9b490c8bc7": "{label} : {value}", "ba67d0a9ff5dabf0": "L’API Presse-papiers n’est pas disponible pour copier la demande à l’administrateur.", "aef4bec20e799aab": "Impossible de copier la demande à l’administrateur dans le presse-papiers.", diff --git a/apps/web/src/i18n/translations/ja.json b/apps/web/src/i18n/translations/ja.json index 66eaf036ed90..07f0201541a5 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -4157,6 +4157,10 @@ "ebb26f93fb83a71b": "{label}: 残り {left}%", "1b16346722b65812": "期間の残り {timeLeft}%", "728c6cc1431e76c8": "{duration} 後にリセット · {time}", + "23622878aeee86d9": "サインイン済みのアカウント", + "d28c53157c8c0f5f": "{label}: {accounts} 個のアカウントで残り {left}%", + "ba03873d5356238b": "{duration}後に +{percent}% · {time}", + "78c1d280f021a28f": "取得元: {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "管理者へのリクエストをコピーする際にクリップボード API を利用できませんでした。", "aef4bec20e799aab": "管理者へのリクエストをクリップボードにコピーできませんでした。", diff --git a/apps/web/src/i18n/translations/ko.json b/apps/web/src/i18n/translations/ko.json index 8a36bcc967f6..d58c92c7fc0d 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -4154,6 +4154,10 @@ "ebb26f93fb83a71b": "{label}: {left}% 남음", "1b16346722b65812": "기간 {timeLeft}% 남음", "728c6cc1431e76c8": "{duration} 후 초기화 · {time}", + "23622878aeee86d9": "로그인된 계정", + "d28c53157c8c0f5f": "{label}: 계정 {accounts}개에서 {left}% 남음", + "ba03873d5356238b": "{duration} 후 +{percent}% · {time}", + "78c1d280f021a28f": "출처: {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "관리자 요청을 복사하는 중 클립보드 API를 사용할 수 없습니다.", "aef4bec20e799aab": "관리자 요청을 클립보드에 복사하지 못했습니다.", diff --git a/apps/web/src/i18n/translations/pt-BR.json b/apps/web/src/i18n/translations/pt-BR.json index 277a77ccf3d9..b34b25fb11eb 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -4166,6 +4166,10 @@ "ebb26f93fb83a71b": "{label}: {left}% restante", "1b16346722b65812": "{timeLeft}% da janela restante", "728c6cc1431e76c8": "Renova em {duration} · {time}", + "23622878aeee86d9": "Contas conectadas", + "d28c53157c8c0f5f": "{label}: {left}% restante em {accounts} contas", + "ba03873d5356238b": "+{percent}% em {duration} · {time}", + "78c1d280f021a28f": "De {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "A API da área de transferência não está disponível ao copiar o pedido para o administrador.", "aef4bec20e799aab": "Falha ao copiar o pedido para o administrador para a área de transferência.", diff --git a/apps/web/src/i18n/translations/ru.json b/apps/web/src/i18n/translations/ru.json index ac9dde8934f3..b6a8bae65d27 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -4166,6 +4166,10 @@ "ebb26f93fb83a71b": "{label}: осталось {left}%", "1b16346722b65812": "От окна осталось {timeLeft}%", "728c6cc1431e76c8": "Сброс через {duration} · {time}", + "23622878aeee86d9": "Аккаунты, в которые выполнен вход", + "d28c53157c8c0f5f": "{label}: осталось {left}% на {accounts} аккаунтах", + "ba03873d5356238b": "+{percent}% через {duration} · {time}", + "78c1d280f021a28f": "Источник: {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "API буфера обмена недоступен при копировании запроса администратору.", "aef4bec20e799aab": "Не удалось скопировать запрос администратору в буфер обмена.", diff --git a/apps/web/src/i18n/translations/sv.json b/apps/web/src/i18n/translations/sv.json index 05bbe8c0c976..2222ead5b7f6 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -4167,6 +4167,10 @@ "ebb26f93fb83a71b": "{label}: {left} % kvar", "1b16346722b65812": "{timeLeft} % av fönstret kvar", "728c6cc1431e76c8": "Återställs om {duration} · {time}", + "23622878aeee86d9": "Inloggade konton", + "d28c53157c8c0f5f": "{label}: {left} % kvar över {accounts} konton", + "ba03873d5356238b": "+{percent} % om {duration} · {time}", + "78c1d280f021a28f": "Från {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "Urklipps-API:t är inte tillgängligt vid kopiering av administratörsbegäran.", "aef4bec20e799aab": "Kunde inte kopiera administratörsbegäran till urklipp.", diff --git a/apps/web/src/i18n/translations/zh.json b/apps/web/src/i18n/translations/zh.json index 0685e33f23a8..84904cccf32c 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -4173,6 +4173,10 @@ "ebb26f93fb83a71b": "{label}:剩余 {left}%", "1b16346722b65812": "窗口剩余 {timeLeft}%", "728c6cc1431e76c8": "{duration} 后重置 · {time}", + "23622878aeee86d9": "已登录的账号", + "d28c53157c8c0f5f": "{label}:{accounts} 个账号共剩余 {left}%", + "ba03873d5356238b": "{duration}后 +{percent}% · {time}", + "78c1d280f021a28f": "来自 {sources}", "e16b0b9b490c8bc7": "{label}:{value}", "ba67d0a9ff5dabf0": "复制管理员请求时剪贴板 API 不可用。", "aef4bec20e799aab": "无法将管理员请求复制到剪贴板。", From 3bf538f4b21b311aab7b0e14ab48948a1ae2312b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:15:13 -0800 Subject: [PATCH 24/30] fix: hub card splits Antigravity quotas, direct logins keep hub accounts, models read in parallel Co-Authored-By: Claude Opus 5.5 --- .../src/provider/antigravityUsageLimits.ts | 8 ------ apps/server/src/usage/UsageLimitSources.ts | 3 +-- apps/server/src/usage/cliproxyApi.ts | 27 +++++++++++-------- .../chat/ThreadDetailsUsage.logic.ts | 18 ++++++++++++- packages/shared/src/usageLimits.ts | 13 ++++++++- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/apps/server/src/provider/antigravityUsageLimits.ts b/apps/server/src/provider/antigravityUsageLimits.ts index 8f1402478784..fadf2207c31f 100644 --- a/apps/server/src/provider/antigravityUsageLimits.ts +++ b/apps/server/src/provider/antigravityUsageLimits.ts @@ -246,14 +246,6 @@ const readCliKeyringSecret = (platform: NodeJS.Platform, architecture: NodeJS.Ar export const antigravityAccountFingerprint = (userInfo: unknown) => decodeUserInfo(userInfo).pipe(Effect.flatMap((info) => sha256(`google-account\0${info.id}`))); -/** Gemini quota is metered apart from the Claude and GPT quota, so each blocks only its models. */ -export function antigravityWindowLimitsModel( - window: Pick, - model: string | null, -): boolean { - return model === null || window.id.startsWith("gemini") === model.startsWith("gemini"); -} - /** Windows from a `retrieveUserQuotaSummary` answer requested at `requestedAt`. */ export const antigravitySummaryWindows = (response: unknown, requestedAt: number) => decodeSummary(response).pipe(Effect.map((summary) => summaryWindows(summary, requestedAt))); diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 7bb6a09cb953..f974761a6e3f 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -11,7 +11,7 @@ import { type UsageLimitSourceSnapshot, } from "@t3tools/contracts"; import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; -import { hubRoutes } from "@t3tools/shared/usageLimits"; +import { antigravityWindowLimitsModel, hubRoutes } from "@t3tools/shared/usageLimits"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; @@ -28,7 +28,6 @@ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretReso import * as SecretResolverLayer from "../secrets/SecretResolver.ts"; import { resolveSettingsSecrets } from "../secrets/settingsSecretValue.ts"; import * as Settings from "../serverSettings.ts"; -import { antigravityWindowLimitsModel } from "../provider/antigravityUsageLimits.ts"; import { windowLimitsModel } from "../provider/claudeUsageLimits.ts"; import { makeCliproxyApi } from "./cliproxyApi.ts"; diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 34589f3b9fef..1cb025a01b27 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -486,19 +486,24 @@ export const makeCliproxyApi = Effect.gen(function* () { provider: HubProvider, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); - const models = yield* routedModels(hub, account).pipe(Effect.option); const call: HubCall = (url, data) => apiCall(hub, account, provider.header(account, platform, architecture), url, data); - const reading = yield* provider.read({ account, call, checkedAt }).pipe( - Effect.provideService(Digest.Digest, digest), - Effect.orElseSucceed(() => ({ - ...listedIdentity(account), - usageLimits: makeUnavailableUsageLimits({ - checkedAt, - reason: "probeFailed", - message: "The hub could not read this account's usage.", - }), - })), + const [models, reading] = yield* Effect.all( + [ + routedModels(hub, account).pipe(Effect.option), + provider.read({ account, call, checkedAt }).pipe( + Effect.provideService(Digest.Digest, digest), + Effect.orElseSucceed(() => ({ + ...listedIdentity(account), + usageLimits: makeUnavailableUsageLimits({ + checkedAt, + reason: "probeFailed", + message: "The hub could not read this account's usage.", + }), + })), + ), + ], + { concurrency: 2 }, ); return { id: account.id, diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index 4d7131bdac3b..d3ccf8ed3a86 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -6,6 +6,7 @@ import { type UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { + antigravityWindowLimitsModel, collectLimitAccounts, collectLimitPools, collectProviderUsageLimits, @@ -60,7 +61,22 @@ function hubPool( now: number, ): ThreadDetailsLimits | null { const routes = hubRoutes(sources, driver, model); - const routed = sources.map((source) => ({ ...source, accounts: source.accounts.filter(routes) })); + const routed = sources.map((source) => ({ + ...source, + accounts: source.accounts.filter(routes).map((account) => + account.driver === "antigravity" + ? { + ...account, + usageLimits: { + ...account.usageLimits, + windows: account.usageLimits.windows.filter((window) => + antigravityWindowLimitsModel(window, model), + ), + }, + } + : account, + ), + })); const readable = routed .flatMap((source) => source.accounts) .filter((account) => limitsNotice(account.usageLimits) === null); diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 86b3d0e1b466..e67d644a6c97 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -848,6 +848,14 @@ export function instanceHubs( return endpoint === undefined ? [] : sources.filter((source) => source.origin === endpoint); } +/** Gemini quota is metered apart from the Claude and GPT quota, so each blocks only its models. */ +export function antigravityWindowLimitsModel( + window: Pick, + model: string | null, +): boolean { + return model === null || window.id.startsWith("gemini") === model.startsWith("gemini"); +} + /** The hub accounts that serve a model: those that list it, else every account of the driver. */ export function hubRoutes( sources: UsageLimitSourceSnapshots, @@ -879,7 +887,10 @@ export function collectProviderUsageLimits( if (!selected) return null; // Only an instance sent to a hub is known to reach the accounts that hub routes its model to. const hubs = instanceHubs(selected, sources); - const routes = hubRoutes(hubs, selected.driver, hubs.length > 0 ? model : null); + const routes = + hubs.length > 0 + ? hubRoutes(hubs, selected.driver, model) + : (account: UsageLimitSourceAccount) => account.driver === selected.driver; const shownHubs = hubs.length > 0 ? hubs : sources; const drivers = new Set( shownHubs.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), From 76b87272012c725ef394f33a3c6d11ea2ecd0c03 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:17:56 -0800 Subject: [PATCH 25/30] fix: hub routing follows thinking suffixes and model prefixes, lent readings keep fresh routes Co-Authored-By: Claude Opus 5.5 --- .../src/usage/UsageLimitSources.test.ts | 21 +++++++++++++++++++ apps/server/src/usage/UsageLimitSources.ts | 4 +++- packages/shared/src/usageLimits.ts | 11 ++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 702a58efffb9..dcfd5b50b0ec 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -384,6 +384,27 @@ it.each([ model: "claude-opus-4-6-thinking", expected: at(1), }, + { + name: "a hub thinking suffix routes by its base model", + accounts: [ + { driver: "codex", models: ["gpt-5.2"], windows: [spent("five_hour", 3)] }, + { windows: [] }, + ], + model: "gpt-5.2(high)", + expected: at(3), + }, + { + name: "a prefixed Gemini model is held back by the Gemini quota", + accounts: [ + { + driver: "antigravity", + models: ["ag/gemini-3.1-pro-high"], + windows: [spent("3p-5h", 1), spent("gemini-5h", 24)], + }, + ], + model: "ag/gemini-3.1-pro-high", + expected: at(24), + }, ])("hub pool reset: $name", ({ accounts, model, expected }) => { expect( UsageLimitSources.hubPoolResetAt( diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index f974761a6e3f..9e392ec11d30 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -82,7 +82,9 @@ function keepLastGoodReadings( candidate.email === account.email && candidate.workspaceId === account.workspaceId, ); - return last && !last.usageLimits.unavailable ? last : account; + if (!last || last.usageLimits.unavailable) return account; + // Only the reading is lent; a fresh model list still decides routing. + return account.models ? { ...last, models: account.models } : last; }), }; }); diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index e67d644a6c97..5c811765e3c0 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -853,7 +853,9 @@ export function antigravityWindowLimitsModel( window: Pick, model: string | null, ): boolean { - return model === null || window.id.startsWith("gemini") === model.startsWith("gemini"); + // A hub can prefix model ids, such as "ag/gemini-3.1-pro". + const base = model?.slice(model.lastIndexOf("/") + 1); + return base === undefined || window.id.startsWith("gemini") === base.startsWith("gemini"); } /** The hub accounts that serve a model: those that list it, else every account of the driver. */ @@ -863,7 +865,12 @@ export function hubRoutes( model: string | null, ): (account: UsageLimitSourceAccount) => boolean { // Claude Code's 1M-context suffix is not part of the model id a hub routes. - const slug = model?.replace(/\[1m\]$/i, ""); + const full = model?.replace(/\[1m\]$/i, ""); + // The hub routes a thinking suffix such as "gpt-5.2(high)" by its base model when one lists it. + const base = full?.replace(/\([^)]*\)$/, ""); + const lists = (id: string | undefined) => + sources.some((source) => source.accounts.some((account) => account.models?.includes(id ?? ""))); + const slug = lists(base) ? base : full; const listed = (account: UsageLimitSourceAccount) => slug !== undefined && account.models?.includes(slug) === true; const serving = new Set( From 93562f26e8992039569c4080529fdf2fbcf7d200 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:23:53 -0800 Subject: [PATCH 26/30] fix: hub reset credits stay on the thread's hub, slow model lookups don't hold quota reads, unknown Antigravity aliases count both quotas Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 6 ++++++ apps/server/src/usage/cliproxyApi.ts | 6 +++++- packages/shared/src/usageLimits.ts | 7 +++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index dcfd5b50b0ec..69ac30e4d70a 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -405,6 +405,12 @@ it.each([ model: "ag/gemini-3.1-pro-high", expected: at(24), }, + { + name: "an alias of unknown family is held back by every Antigravity quota", + accounts: [{ driver: "antigravity", models: ["fast"], windows: [spent("gemini-5h", 24)] }], + model: "fast", + expected: at(24), + }, ])("hub pool reset: $name", ({ accounts, model, expected }) => { expect( UsageLimitSources.hubPoolResetAt( diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 1cb025a01b27..f7074c726b7c 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -490,7 +490,11 @@ export const makeCliproxyApi = Effect.gen(function* () { apiCall(hub, account, provider.header(account, platform, architecture), url, data); const [models, reading] = yield* Effect.all( [ - routedModels(hub, account).pipe(Effect.option), + // Optional, so a slow lookup must not hold back the quota reading past the refresh budget. + routedModels(hub, account).pipe( + Effect.timeoutOption("2 seconds"), + Effect.orElseSucceed(() => Option.none()), + ), provider.read({ account, call, checkedAt }).pipe( Effect.provideService(Digest.Digest, digest), Effect.orElseSucceed(() => ({ diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 5c811765e3c0..deb7a951a75e 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -855,7 +855,9 @@ export function antigravityWindowLimitsModel( ): boolean { // A hub can prefix model ids, such as "ag/gemini-3.1-pro". const base = model?.slice(model.lastIndexOf("/") + 1); - return base === undefined || window.id.startsWith("gemini") === base.startsWith("gemini"); + // An alias such as "fast" may route to either quota, so both hold it back. + if (base === undefined || !/^(gemini|claude|gpt)/.test(base)) return true; + return window.id.startsWith("gemini") === base.startsWith("gemini"); } /** The hub accounts that serve a model: those that list it, else every account of the driver. */ @@ -960,7 +962,8 @@ export function collectProviderUsageLimits( credits, } of native) { const key = resolveKey(nativeReport(provider)); - const hubCredits = sources + // Only a hub this instance reaches may redeem a credit for its thread. + const hubCredits = shownHubs .flatMap((source) => source.accounts.map((account) => ({ source, account }))) .filter( ({ account }) => From 2973c1df5fc0dd4f6432cafc4d85296e48fd593f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:28:42 -0800 Subject: [PATCH 27/30] fix(server): hub model lookups don't take quota read slots, unread model lists stay unknown Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.ts | 4 +- apps/server/src/usage/cliproxyApi.ts | 70 ++++++++++++---------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index 9e392ec11d30..eba449c9a0fb 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -83,8 +83,8 @@ function keepLastGoodReadings( candidate.workspaceId === account.workspaceId, ); if (!last || last.usageLimits.unavailable) return account; - // Only the reading is lent; a fresh model list still decides routing. - return account.models ? { ...last, models: account.models } : last; + // Only the reading is lent; an unread model list stays unknown. + return { ...last, models: account.models }; }), }; }); diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index f7074c726b7c..3d6857d55116 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -488,33 +488,18 @@ export const makeCliproxyApi = Effect.gen(function* () { const checkedAt = DateTime.formatIso(yield* DateTime.now); const call: HubCall = (url, data) => apiCall(hub, account, provider.header(account, platform, architecture), url, data); - const [models, reading] = yield* Effect.all( - [ - // Optional, so a slow lookup must not hold back the quota reading past the refresh budget. - routedModels(hub, account).pipe( - Effect.timeoutOption("2 seconds"), - Effect.orElseSucceed(() => Option.none()), - ), - provider.read({ account, call, checkedAt }).pipe( - Effect.provideService(Digest.Digest, digest), - Effect.orElseSucceed(() => ({ - ...listedIdentity(account), - usageLimits: makeUnavailableUsageLimits({ - checkedAt, - reason: "probeFailed", - message: "The hub could not read this account's usage.", - }), - })), - ), - ], - { concurrency: 2 }, + const reading = yield* provider.read({ account, call, checkedAt }).pipe( + Effect.provideService(Digest.Digest, digest), + Effect.orElseSucceed(() => ({ + ...listedIdentity(account), + usageLimits: makeUnavailableUsageLimits({ + checkedAt, + reason: "probeFailed", + message: "The hub could not read this account's usage.", + }), + })), ); - return { - id: account.id, - driver: provider.driver, - ...reading, - ...(Option.isSome(models) ? { models: models.value } : {}), - }; + return { id: account.id, driver: provider.driver, ...reading }; }); const readAccounts = Effect.fn("CliproxyApi.readAccounts")(function* ( @@ -527,14 +512,33 @@ export const makeCliproxyApi = Effect.gen(function* () { : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), ), ); - return yield* Effect.forEach( - accounts.flatMap((account) => { - const provider = HUB_PROVIDERS.get(account.provider); - return provider && !account.disabled ? [{ account, provider }] : []; - }), - ({ account, provider }) => readAccount(hub, account, provider), - { concurrency: 4 }, + const readable = accounts.flatMap((account) => { + const provider = HUB_PROVIDERS.get(account.provider); + return provider && !account.disabled ? [{ account, provider }] : []; + }); + // Model lists are optional, so their lookups run apart from the quota reads and stop at one + // short deadline; a slow lookup must not push a refresh past its budget. + const [readings, models] = yield* Effect.all( + [ + Effect.forEach(readable, ({ account, provider }) => readAccount(hub, account, provider), { + concurrency: 4, + }), + Effect.forEach( + readable, + ({ account }) => + routedModels(hub, account).pipe( + Effect.timeoutOption("2 seconds"), + Effect.orElseSucceed(() => Option.none()), + ), + { concurrency: "unbounded" }, + ), + ], + { concurrency: 2 }, ); + return readings.map((reading, index) => { + const listed = models[index]; + return listed && Option.isSome(listed) ? { ...reading, models: listed.value } : reading; + }); }); const consume = Effect.fn("CliproxyApi.consume")(function* ( From ab2f5a6c8fa7388a736ed3b2f43c844c47657086 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:33:51 -0800 Subject: [PATCH 28/30] fix: Antigravity Argon and Carbon quotas only block their own models Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 12 ++++++++++++ packages/shared/src/usageLimits.ts | 10 ++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 69ac30e4d70a..a540f561ab04 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -411,6 +411,18 @@ it.each([ model: "fast", expected: at(24), }, + { + name: "an Argon model waits only for the Argon quota", + accounts: [ + { + driver: "antigravity", + models: ["gemini-4-argon-high"], + windows: [spent("gemini-argon-5h", 2), spent("gemini-carbon-weekly", 96)], + }, + ], + model: "gemini-4-argon-high", + expected: at(2), + }, ])("hub pool reset: $name", ({ accounts, model, expected }) => { expect( UsageLimitSources.hubPoolResetAt( diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index deb7a951a75e..6d3dca0ddce7 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -848,7 +848,13 @@ export function instanceHubs( return endpoint === undefined ? [] : sources.filter((source) => source.origin === endpoint); } -/** Gemini quota is metered apart from the Claude and GPT quota, so each blocks only its models. */ +// Antigravity meters Gemini Argon, Gemini Carbon, other Gemini, and Claude/GPT models apart. +function antigravityQuotaFamily(id: string) { + if (!id.startsWith("gemini")) return "shared"; + return id.includes("argon") ? "argon" : id.includes("carbon") ? "carbon" : "gemini"; +} + +/** Each Antigravity quota blocks only the models it meters. */ export function antigravityWindowLimitsModel( window: Pick, model: string | null, @@ -857,7 +863,7 @@ export function antigravityWindowLimitsModel( const base = model?.slice(model.lastIndexOf("/") + 1); // An alias such as "fast" may route to either quota, so both hold it back. if (base === undefined || !/^(gemini|claude|gpt)/.test(base)) return true; - return window.id.startsWith("gemini") === base.startsWith("gemini"); + return antigravityQuotaFamily(window.id) === antigravityQuotaFamily(base); } /** The hub accounts that serve a model: those that list it, else every account of the driver. */ From 3491e39f4d1744a2c5c4fbfed1029327496e3ade Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:38:45 -0800 Subject: [PATCH 29/30] fix(shared): an own-driver hub account with an unread model list keeps counting Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 13 +++++++++++++ packages/shared/src/usageLimits.ts | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index a540f561ab04..088450af4de7 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -411,6 +411,19 @@ it.each([ model: "fast", expected: at(24), }, + { + name: "an own-driver account whose model list was unread still counts", + accounts: [ + { windows: [spent("five_hour", 1)] }, + { + driver: "antigravity", + models: ["claude-sonnet-4-6"], + windows: [spent("3p-5h", 8)], + }, + ], + model: "claude-sonnet-4-6", + expected: at(1), + }, { name: "an Argon model waits only for the Argon quota", accounts: [ diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 6d3dca0ddce7..29c55b280b7d 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -888,7 +888,8 @@ export function hubRoutes( if (serving.size === 0) return (account) => account.driver === driver && account.models?.length !== 0; // An account whose model list was unreadable may serve the model too. - return (account) => (account.models ? listed(account) : serving.has(account.driver)); + return (account) => + account.models ? listed(account) : serving.has(account.driver) || account.driver === driver; } export function collectProviderUsageLimits( From e8eea7647eaae354613002c8f9c6fd366f233002 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 17:39:40 -0800 Subject: [PATCH 30/30] test(server): hub routing rows list the models their other accounts serve Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/UsageLimitSources.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 088450af4de7..6259c4fbe509 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -361,7 +361,7 @@ it.each([ name: "an account of another driver the hub routes the model to counts", accounts: [ { driver: "codex", models: ["gpt-5.6-sol"], windows: [spent("five_hour", 3)] }, - { windows: [] }, + { models: ["claude-sonnet-4-6"], windows: [] }, ], model: "gpt-5.6-sol", expected: at(3), @@ -388,7 +388,7 @@ it.each([ name: "a hub thinking suffix routes by its base model", accounts: [ { driver: "codex", models: ["gpt-5.2"], windows: [spent("five_hour", 3)] }, - { windows: [] }, + { models: ["claude-sonnet-4-6"], windows: [] }, ], model: "gpt-5.2(high)", expected: at(3),