From 2cb2885195760732379cbf97b89c4446a79cfee2 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:39:31 +0100 Subject: [PATCH 1/6] test(shared): instances routed through a CLIProxyAPI hub read the hub's limits Co-authored-by: Claude Opus 5.5 --- packages/shared/src/usageLimits.test.ts | 124 ++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 0078c21b566b7..79e6f4f7522ba 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -20,6 +20,7 @@ import { collectLimitPools, elapsedShare, exhaustedUsageWindow, + instanceUsageLimits, paceOf, paceRemainingPercent, usesChatGptSharing, @@ -1729,6 +1730,129 @@ describe("/usage-limits", () => { }); }); +describe("instance usage limits", () => { + const hubOrigin = "http://127.0.0.1:8317"; + const checkedAt = "2026-09-03T11:00:00.000Z"; + const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } }; + const routed = provider({ + usageLimits: unsupported, + auth: { status: "authenticated", type: "customEndpoint", endpoint: hubOrigin }, + }); + const weekly = { + id: "weekly", + kind: "weekly", + label: "Weekly", + usedPercent: 10, + windowDurationMins: 10_080, + resetsAt: "2026-09-08T12:00:00.000Z", + } as const; + const hubAccount = (id: string, usedPercent: number, extra: object = {}) => ({ + id, + driver: routed.driver, + email: `${id}@example.com`, + usageLimits: { checkedAt, windows: [{ ...window, usedPercent }, weekly] }, + ...extra, + }); + const hub = (accounts: ReadonlyArray>, extra: object = {}) => ({ + id: UsageLimitSourceId.make("hub"), + kind: "cliproxy" as const, + label: "Hub", + origin: hubOrigin, + endpoint: hubOrigin, + checkedAt, + accounts, + ...extra, + }); + const limitsOf = (selected: ServerProvider, sources: ReadonlyArray>) => + instanceUsageLimits(selected.instanceId, [selected], sources, now); + + it.each([ + ["one hub account", [hub([hubAccount("a", 30)])], [30, 10]], + ["the pool of hub accounts", [hub([hubAccount("a", 30), hubAccount("b", 70)])], [50, 10]], + [ + "each account once across hubs on one origin", + [ + hub([hubAccount("a", 30)]), + hub( + [ + hubAccount("a", 90, { + usageLimits: { + checkedAt: "2026-09-03T11:30:00.000Z", + windows: [{ ...window, usedPercent: 90 }, weekly], + }, + }), + hubAccount("b", 70), + ], + { + id: UsageLimitSourceId.make("hub-2"), + endpoint: `${hubOrigin}/second`, + }, + ), + ], + [80, 10], + ], + [ + "the readable hub accounts when others cannot be read", + [ + hub([ + hubAccount("a", 30), + hubAccount("b", 0, { + usageLimits: { ...unsupported, unavailable: { reason: "probeFailed" as const } }, + }), + ]), + ], + [30, 10], + ], + ])("shows %s for an instance routed through its hub", (_, sources, usedPercents) => { + const limits = limitsOf(routed, sources); + expect(limits?.unavailable).toBeUndefined(); + expect(limits?.windows.map((entry) => entry.usedPercent)).toEqual(usedPercents); + }); + + it("keeps a lone hub account's reset clock", () => { + expect(limitsOf(routed, [hub([hubAccount("a", 30)])])?.windows[0]).toMatchObject({ + resetsAt: window.resetsAt, + windowDurationMins: window.windowDurationMins, + }); + }); + + it.each([ + [ + "an instance not routed through a hub", + provider({ usageLimits: unsupported }), + [hub([hubAccount("a", 30)])], + ], + [ + "a hub on another origin", + routed, + [hub([hubAccount("a", 30)], { origin: "http://127.0.0.1:9999" })], + ], + [ + "a hub without accounts for the driver", + routed, + [hub([hubAccount("a", 30, { driver: ProviderDriverKind.make("claudeAgent") })])], + ], + ])("keeps the instance's own state for %s", (_, selected, sources) => { + expect(limitsOf(selected, sources)).toEqual(unsupported); + }); + + it("reports an unreadable hub as unreadable, not as an account without limits", () => { + expect(limitsOf(routed, [hub([], { error: "token expired" })])?.unavailable).toEqual({ + reason: "probeFailed", + message: "token expired", + }); + expect( + limitsOf(routed, [ + hub([ + hubAccount("a", 0, { + usageLimits: { ...unsupported, unavailable: { reason: "probeFailed" as const } }, + }), + ]), + ])?.unavailable?.reason, + ).toBe("probeFailed"); + }); +}); + describe("sameUsageLimitCommandCoverage", () => { const codexAccount = { id: "a", From 5b74880bb675e380ab122db56f98dfb07f8fa3d3 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:40:16 +0100 Subject: [PATCH 2/6] fix(web): thread details show the CLIProxyAPI hub's limits for instances routed through it Co-authored-by: Claude Opus 5.5 --- .../components/chat/ThreadDetailsUsage.tsx | 13 +--- docs/user/usage.md | 5 +- packages/shared/src/usageLimits.ts | 75 +++++++++++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index b2e09a35c5264..10b945c86f566 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -5,7 +5,7 @@ import type { UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { - collectProviderUsageLimits, + instanceUsageLimits, type LimitPace, paceOf, paceRemainingPercent, @@ -51,15 +51,10 @@ export function ThreadDetailsUsage({ const m = useMessages(); 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; + providerInstanceId !== null + ? instanceUsageLimits(providerInstanceId, providers, sources, now) + : undefined; if (!limits) return null; const notice = limitsNotice(limits, m); diff --git a/docs/user/usage.md b/docs/user/usage.md index 5160becc21567..bc09e7bfca256 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -188,8 +188,9 @@ 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. When a Codex or Claude instance +sends its requests to the hub's origin, the thread details card shows the pooled limits of the hub's +accounts for that provider. Remove the hub from the same settings section when you no longer need it. ## Subscription usage widget diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 693d82b956971..7cb6d7873540e 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -965,3 +965,78 @@ export function collectProviderUsageLimits( } return { createdAt: DateTime.formatIso(DateTime.makeUnsafe(now)), accounts, notices }; } + +/** + * The limits an instance's turns spend: the pool of the CLIProxyAPI hub it sends requests to, + * otherwise its own account's. Shown for the selected instance in the thread details card. + */ +export function instanceUsageLimits( + instanceId: ProviderInstanceId, + providers: readonly ServerProvider[], + sources: UsageLimitSourceSnapshots, + now: number, +): ServerProviderUsageLimits | undefined { + const selected = providers.find((provider) => provider.instanceId === instanceId); + if (!selected) return undefined; + const { endpoint } = selected.auth; + // Hubs on one origin under different base paths all serve requests sent to that origin. + const hubs = endpoint ? sources.filter((source) => source.origin === endpoint) : []; + const hubAccounts = new Map(); + for (const source of hubs) { + for (const account of source.accounts) { + if (account.driver !== selected.driver) continue; + const key = + accountKey(account.driver, account.email, account.usageLimits) ?? + `${source.id}:${account.id}`; + const previous = hubAccounts.get(key); + if ( + !previous || + Date.parse(account.usageLimits.checkedAt) > Date.parse(previous.usageLimits.checkedAt) + ) { + hubAccounts.set(key, account); + } + } + } + const readable = [...hubAccounts].filter(([, account]) => !limitsNotice(account.usageLimits)); + const [pool] = collectLimitPools( + readable.map(([key, account]) => ({ + key, + driver: account.driver, + displayName: null, + email: account.email, + plan: account.plan, + accentColor: undefined, + environments: [], + sourceLabel: null, + redeem: null, + limits: account.usageLimits, + })), + now, + ); + if (pool) { + return { + checkedAt: readable + .map(([, account]) => account.usageLimits.checkedAt) + .reduce((oldest, next) => (Date.parse(next) < Date.parse(oldest) ? next : oldest)), + // A pool refills account by account, so only a lone account's window has one reset clock. + windows: displayLimitWindows(pool).map(({ id, kind, label, usedPercent, members }) => + members.length === 1 ? members[0]!.window : { id, kind, label, usedPercent }, + ), + }; + } + const hubError = hubs.find((source) => source.error)?.error; + if (hubAccounts.size > 0 || hubError) { + return { + checkedAt: DateTime.formatIso(DateTime.makeUnsafe(now)), + windows: [], + unavailable: { reason: "probeFailed", ...(hubError ? { message: hubError } : {}) }, + }; + } + // 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. + return ( + collectProviderUsageLimits(instanceId, providers, sources, now)?.accounts.find( + (entry) => entry.instanceId === instanceId, + )?.limits ?? selected.usageLimits + ); +} From 44c6ef65aef03240ffec952566d28001edddb777 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:55:12 +0100 Subject: [PATCH 3/6] fix(usage): match CLIProxyAPI limits to the provider route Preserve provider API paths for hub selection and reset lookup. Pool distinct workspaces, keep readable snapshots, and retain the precision needed to round remaining quota. Co-authored-by: GPT 6.2 Sol --- .../orchestration-v2/ProviderEventIngestor.ts | 12 +++- apps/server/src/provider/ClaudeProvider.ts | 14 +++-- .../server/src/provider/CodexProvider.test.ts | 15 +++-- apps/server/src/provider/CodexProvider.ts | 14 ++--- .../src/provider/ProviderRegistry.test.ts | 12 +++- .../src/usage/UsageLimitSources.test.ts | 17 +++++- apps/server/src/usage/UsageLimitSources.ts | 34 ++++++----- docs/user/usage.md | 9 +-- packages/contracts/src/server.ts | 2 + packages/shared/src/usageLimits.test.ts | 59 ++++++++++++++++++ packages/shared/src/usageLimits.ts | 61 +++++++++++++------ 11 files changed, 184 insertions(+), 65 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index e036bbe8994c1..49884b49120bb 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -671,13 +671,14 @@ export const layer: Layer.Layer< ? { ...input.event.failure, message: "Killed: out of memory.", code: "oom_kill" } : input.event.failure; // A proxied CLI can drop the reset time; the hub still knows when its pool refills. - const endpoint = + const auth = reported.class === "usage_limit" && reported.resetAt == null ? (yield* Effect.flatMap( instances.getInstance(input.providerInstanceId), (instance) => instance?.snapshot.getSnapshot ?? Effect.undefined, - ))?.auth.endpoint + ))?.auth : undefined; + const endpoint = auth?.endpoint; const hubResetAt = endpoint === undefined ? null @@ -696,7 +697,12 @@ export const layer: Layer.Layer< }).pipe( Effect.orElseSucceed(() => null), Effect.flatMap((model) => - usageLimitResets.resetAt(input.event.driver, endpoint, model), + usageLimitResets.resetAt( + input.event.driver, + endpoint, + model, + auth?.endpointPath, + ), ), ); const failure = hubResetAt === null ? reported : { ...reported, resetAt: hubResetAt }; diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts index b657a2b813feb..04a44d6b3d660 100644 --- a/apps/server/src/provider/ClaudeProvider.ts +++ b/apps/server/src/provider/ClaudeProvider.ts @@ -829,9 +829,8 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( const baseUrl = (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 endpoint = customClaudeApiEndpointHost(baseUrl) ? URL.parse(baseUrl?.trim() ?? "") : null; + if (!endpoint && claudeAuthStatus(capabilities) === "unauthenticated") { const path = yield* Path.Path; const configDir = (yield* makeClaudeEnvironment(claudeSettings, resolvedEnvironment)) .CLAUDE_CONFIG_DIR; @@ -859,8 +858,13 @@ 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", ...(endpoint ? { endpoint } : {}) } + (endpoint + ? { + type: "customEndpoint", + label: "Custom API endpoint", + endpoint: endpoint.origin, + endpointPath: endpoint.pathname, + } : undefined) ?? claudeAuthMetadata({ subscriptionType: capabilities.subscriptionType, diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index a70ea15a73570..65b9f9698e0a0 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -312,7 +312,7 @@ it("reports native quota only for an OpenAI login on a native route", () => { const chatgpt = { type: "chatgpt", email: "a@example.com", planType: "pro" } as const; const hub = { model_provider: "hub", - model_providers: { hub: { base_url: "https://hub.example.com/v1" } }, + model_providers: { hub: { base_url: "https://hub.example.com/team/v1" } }, }; const cases = [ ["default route", chatgpt, true, {}, true], @@ -332,11 +332,14 @@ it("reports native quota only for an OpenAI login on a native route", () => { ); } // The custom endpoint is what a usage-limit hub is matched against. - 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", - ); + assert.deepStrictEqual(codexCustomEndpoint(hub), { + endpoint: "https://hub.example.com", + endpointPath: "/team/v1", + }); + assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1" }), { + endpoint: "http://127.0.0.1:8317", + endpointPath: "/v1", + }); assert.strictEqual(codexCustomEndpoint({ openai_base_url: "https://chatgpt.com/x" }), undefined); assert.strictEqual(codexCustomEndpoint({}), undefined); }); diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 63154f4e99499..65109e62c4246 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -120,12 +120,12 @@ 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 { +/** The custom endpoint Codex sends model requests to, without URL credentials. */ +export function codexCustomEndpoint(config: unknown) { const baseUrl = codexRouteBaseUrl(config); const url = baseUrl ? URL.parse(baseUrl) : null; return url && /^https?:$/.test(url.protocol) && !routesToNativeOpenai(config) - ? url.origin + ? { endpoint: url.origin, endpointPath: url.pathname } : undefined; } @@ -156,7 +156,7 @@ const CODEX_PRESENTATION = { export interface CodexAppServerProviderSnapshot { readonly account: CodexSchema.V2GetAccountResponse; - readonly endpoint?: string | undefined; + readonly customEndpoint?: ReturnType; readonly nativeUsage?: boolean; readonly rateLimits?: CodexRateLimitsProbe; readonly version: string | undefined; @@ -811,7 +811,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun const cyberAccess = Option.getOrUndefined(decodeCodexCyberAccessConfig(config)); return { account: accountResponse, - endpoint: config ? codexCustomEndpoint(config) : undefined, + customEndpoint: config ? codexCustomEndpoint(config) : undefined, nativeUsage, ...(rateLimits ? { rateLimits } : {}), version, @@ -1176,9 +1176,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu installed: true, version: snapshot.version ?? null, status: snapshot.models === undefined ? "warning" : accountStatus.status, - auth: snapshot.endpoint - ? { ...accountStatus.auth, endpoint: snapshot.endpoint } - : accountStatus.auth, + auth: { ...accountStatus.auth, ...snapshot.customEndpoint }, ...(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 e4ec16b77dde0..2298da46bc599 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -4679,7 +4679,9 @@ it.layer( it.effect.each([ { - settings: { env: { ANTHROPIC_BASE_URL: "https://hub.example.ts.net:8317" } }, + settings: { env: { ANTHROPIC_BASE_URL: "https://hub.example.ts.net:8317/team" } }, + endpoint: "https://hub.example.ts.net:8317", + endpointPath: "/team", baseUrl: "https://api.anthropic.com", type: "customEndpoint", label: "Custom API endpoint", @@ -4689,16 +4691,20 @@ it.layer( baseUrl: "https://proxy.example", type: "apiKey", label: "Claude API Key", + endpoint: undefined, + endpointPath: undefined, }, { settings: {}, baseUrl: "https://proxy.example", + endpoint: "https://proxy.example", + endpointPath: "/", type: "customEndpoint", label: "Custom API endpoint", }, ])( "uses the settings endpoint before the process endpoint: $type / $baseUrl", - ({ settings, baseUrl, type, label }) => + ({ settings, baseUrl, type, label, endpoint, endpointPath }) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -4728,6 +4734,8 @@ it.layer( assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.type, type); assert.strictEqual(status.auth.label, label); + assert.strictEqual(status.auth.endpoint, endpoint); + assert.strictEqual(status.auth.endpointPath, endpointPath); }).pipe( Effect.provide( layerMockSpawner((args) => { diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 4a71640aa2626..d1d884721b239 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -368,7 +368,18 @@ it.each([ it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold the failure", () => Effect.gen(function* () { yield* TestClock.setTime(NOW); - const roomLeft = { ...hub([{ windows: [spent("five_hour", 2)] }]), origin: "http://hub.test" }; + const origin = "http://hub.test"; + const roomLeft = { + ...hub([{ windows: [spent("five_hour", 2)] }]), + origin, + endpoint: `${origin}/team`, + }; + const otherRoute = { + ...hub([{ windows: [] }]), + id: UsageLimitSourceId.make("other"), + origin, + endpoint: `${origin}/other`, + }; const cached = { ...roomLeft, accounts: roomLeft.accounts.map((account) => ({ @@ -381,11 +392,11 @@ it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold t }; let current: ReadonlyArray = [cached]; let refresh: Effect.Effect = Effect.sync(() => { - current = [roomLeft]; + current = [roomLeft, otherRoute]; }); const lookup = UsageLimitSources.UsageLimitResetFallback.pipe( Effect.flatMap((fallback) => - fallback.resetAt(ProviderDriverKind.make("claudeAgent"), "http://hub.test", null), + fallback.resetAt(ProviderDriverKind.make("claudeAgent"), origin, null, "/team"), ), Effect.provide( UsageLimitSources.layerResetFallback.pipe( diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index ff54bb02781a9..eb3842ac93186 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -10,6 +10,7 @@ import { type UsageLimitSourceId, type UsageLimitSourceSnapshot, } from "@t3tools/contracts"; +import { hubUsageSources } from "@t3tools/shared/usageLimits"; import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; @@ -281,6 +282,7 @@ export class UsageLimitResetFallback extends Context.Reference<{ driver: ProviderDriverKind, endpoint: string, model: string | null, + endpointPath?: string, ) => Effect.Effect; }>("t3/usage/UsageLimitResetFallback", { defaultValue: () => ({ resetAt: () => Effect.succeed(null) }), @@ -291,21 +293,23 @@ export const layerResetFallback = Layer.effect( Effect.gen(function* () { const sources = yield* UsageLimitSources; return { - resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, endpoint, model) { - // Only the hub at the instance's endpoint serves its turns. - const atEndpoint = Effect.map(sources.current, (current) => - current.filter((source) => source.origin === endpoint), - ); - if ((yield* atEndpoint).length === 0) return null; - // The pool usually ran out after the last poll, so the cached accounts still show room. - yield* sources.refresh.pipe(Effect.timeout("5 seconds"), Effect.ignore); - return hubPoolResetAt( - yield* atEndpoint, - driver, - model, - DateTime.toEpochMillis(yield* DateTime.now), - ); - }), + resetAt: Effect.fn("UsageLimitResetFallback.resetAt")( + function* (driver, endpoint, model, endpointPath) { + // Only the hub at the instance's endpoint serves its turns. + const atEndpoint = Effect.map(sources.current, (current) => + hubUsageSources(current, driver, endpoint, endpointPath), + ); + if ((yield* atEndpoint).length === 0) return null; + // The pool usually ran out after the last poll, so the cached accounts still show room. + yield* sources.refresh.pipe(Effect.timeout("5 seconds"), Effect.ignore); + return hubPoolResetAt( + yield* atEndpoint, + driver, + model, + DateTime.toEpochMillis(yield* DateTime.now), + ); + }, + ), }; }), ); diff --git a/docs/user/usage.md b/docs/user/usage.md index bc09e7bfca256..3320542e6bdda 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -187,10 +187,11 @@ The hub's Codex, Claude, xAI (shown as Grok) and Antigravity accounts appear und accounts for other providers are not read. Codex accounts show banked reset credits; select an 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. When a Codex or Claude instance -sends its requests to the hub's origin, the thread details card shows the pooled limits of the hub's -accounts for that provider. Remove the hub from the same settings section when you no longer need it. +Configure the provider separately to send agent requests through the hub. When the Codex or Claude +instance's API base URL points to the hub, the thread details card shows the hub's pooled limits for +that provider. Use the same hostname, port, and reverse proxy path in both settings. A Codex API +base URL can include the OpenAI `/v1` suffix. Remove the hub from the same settings section when you +no longer need it. ## Subscription usage widget diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index f074b8f6a3e46..0362f6263e066 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -72,6 +72,8 @@ export const ServerProviderAuth = Schema.Struct({ workspaceId: Schema.optional(TrimmedNonEmptyString), /** Origin of the custom endpoint the instance sends its requests to. */ endpoint: Schema.optional(TrimmedNonEmptyString), + /** Path of the custom API base URL. A slash denotes the root path. */ + endpointPath: 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 79e6f4f7522ba..223d9c28ae60c 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -23,6 +23,7 @@ import { instanceUsageLimits, paceOf, paceRemainingPercent, + remainingPercent, usesChatGptSharing, resolveNewThreadModelSelection, } from "./usageLimits.ts"; @@ -1809,6 +1810,64 @@ describe("instance usage limits", () => { expect(limits?.windows.map((entry) => entry.usedPercent)).toEqual(usedPercents); }); + it.each([ + ["root", "/v1", hubOrigin], + ["a reverse proxy path", "/second/v1/", `${hubOrigin}/second`], + ])( + "selects the hub for %s even when another hub shares its origin", + (_, endpointPath, endpoint) => { + const selected = { ...routed, auth: { ...routed.auth, endpointPath } }; + const limits = limitsOf(selected, [ + hub([hubAccount("a", endpoint === hubOrigin ? 100 : 0)]), + hub([hubAccount("b", endpoint === hubOrigin ? 0 : 100)], { + id: UsageLimitSourceId.make("hub-2"), + endpoint: `${hubOrigin}/second`, + }), + ]); + expect(limits?.windows[0]?.usedPercent).toBe(100); + }, + ); + + it("pools separate workspaces for the same Codex email", () => { + const email = "same@example.com"; + const limits = limitsOf(routed, [ + hub([ + hubAccount("personal.json", 100, { email, workspaceId: "personal" }), + hubAccount("team.json", 0, { email, workspaceId: "team" }), + ]), + ]); + expect(limits?.windows[0]?.usedPercent).toBe(50); + }); + + it("keeps a readable snapshot when a newer duplicate failed", () => { + const limits = limitsOf(routed, [ + hub([hubAccount("a", 30)]), + hub( + [ + hubAccount("a", 0, { + usageLimits: { + checkedAt: "2026-09-03T11:30:00.000Z", + windows: [], + unavailable: { reason: "probeFailed" }, + }, + }), + ], + { id: UsageLimitSourceId.make("hub-2") }, + ), + ]); + expect(limits?.windows[0]?.usedPercent).toBe(30); + }); + + it("rounds remaining quota for a pool of accounts the same way as Usage", () => { + const limits = limitsOf(routed, [hub([hubAccount("a", 99), hubAccount("b", 100)])]); + expect(limits?.windows.map(remainingPercent)).toEqual([1, 90]); + }); + + it("preserves a successful snapshot with no windows", () => { + const empty = { checkedAt, windows: [] }; + expect(limitsOf(routed, [hub([hubAccount("a", 0, { usageLimits: empty })])])).toEqual(empty); + }); + it("keeps a lone hub account's reset clock", () => { expect(limitsOf(routed, [hub([hubAccount("a", 30)])])?.windows[0]).toMatchObject({ resetsAt: window.resetsAt, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 7cb6d7873540e..a061a7a316627 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -260,6 +260,23 @@ function accountKeyResolver(reports: Iterable) { const hubAccountName = (account: { readonly id: string }) => account.id.replace(/\.json$/i, ""); +export function hubUsageSources( + sources: UsageLimitSourceSnapshots, + driver: ServerProvider["driver"], + endpoint: string | undefined, + endpointPath: string | undefined, +): UsageLimitSourceSnapshots { + if (!endpoint) return []; + const path = endpointPath?.replace(/\/+$/, ""); + // Codex includes the OpenAI API's /v1 prefix in its base URL. + const base = driver === "codex" ? path?.replace(/\/v1$/, "") : path; + return sources.filter( + (source) => + source.origin === endpoint && + (base === undefined || (source.endpoint ?? source.origin) === `${endpoint}${base}`), + ); +} + // Source ids are environment-local, so only a remote endpoint names the same hub across environments. function hubIdentity(environmentId: EnvironmentId, source: UsageLimitSourceSnapshot): string { // The server supplies canonical endpoints; native URL polyfills can misparse IPv6 hosts. @@ -678,7 +695,7 @@ function poolWindows(accounts: readonly LimitAccount[], now: number): readonly L columns: accounts.map( (account) => memberByAccount.get(account.key) ?? { account, window: null }, ), - usedPercent: Math.round(usedPercent), + usedPercent, remainingPercent: Math.round(100 - usedPercent), pace: meanElapsed === null ? null : paceOfShares(timedUsed, meanElapsed), resets, @@ -978,26 +995,27 @@ export function instanceUsageLimits( ): ServerProviderUsageLimits | undefined { const selected = providers.find((provider) => provider.instanceId === instanceId); if (!selected) return undefined; - const { endpoint } = selected.auth; - // Hubs on one origin under different base paths all serve requests sent to that origin. - const hubs = endpoint ? sources.filter((source) => source.origin === endpoint) : []; + const { endpoint, endpointPath } = selected.auth; + const hubs = hubUsageSources(sources, selected.driver, endpoint, endpointPath); + const reports = hubs.flatMap((source) => + source.accounts + .filter((account) => account.driver === selected.driver) + .map((account) => ({ source, account })), + ); + const resolveKey = accountKeyResolver(reports.map(({ account }) => account)); const hubAccounts = new Map(); - for (const source of hubs) { - for (const account of source.accounts) { - if (account.driver !== selected.driver) continue; - const key = - accountKey(account.driver, account.email, account.usageLimits) ?? - `${source.id}:${account.id}`; - const previous = hubAccounts.get(key); - if ( - !previous || - Date.parse(account.usageLimits.checkedAt) > Date.parse(previous.usageLimits.checkedAt) - ) { - hubAccounts.set(key, account); - } + for (const { source, account } of reports) { + if (limitsNotice(account.usageLimits) !== null) continue; + const key = resolveKey(account) ?? `${source.id}:${account.id}:${account.workspaceId ?? ""}`; + const previous = hubAccounts.get(key); + if ( + !previous || + Date.parse(account.usageLimits.checkedAt) > Date.parse(previous.usageLimits.checkedAt) + ) { + hubAccounts.set(key, account); } } - const readable = [...hubAccounts].filter(([, account]) => !limitsNotice(account.usageLimits)); + const readable = [...hubAccounts]; const [pool] = collectLimitPools( readable.map(([key, account]) => ({ key, @@ -1025,13 +1043,18 @@ export function instanceUsageLimits( }; } const hubError = hubs.find((source) => source.error)?.error; - if (hubAccounts.size > 0 || hubError) { + const failed = reports.find( + ({ account }) => account.usageLimits.unavailable?.reason === "probeFailed", + ); + if (hubError || failed) { return { checkedAt: DateTime.formatIso(DateTime.makeUnsafe(now)), windows: [], unavailable: { reason: "probeFailed", ...(hubError ? { message: hubError } : {}) }, }; } + const empty = reports.find(({ account }) => !account.usageLimits.unavailable) ?? reports[0]; + if (empty) return empty.account.usageLimits; // 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. return ( From 13f18fac536a5dc621723a41feb8bf8c0a7b9ebb Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:09:48 +0100 Subject: [PATCH 4/6] fix(usage): avoid ambiguous CLIProxyAPI quota attribution Resolve the Codex base URL against a single hub mount for quota display and reset timing. Share Usage's auth-file identity in the sidebar. Co-authored-by: GPT 6.2 Sol --- .../ProviderEventIngestor.test.ts | 6 +- .../orchestration-v2/ProviderEventIngestor.ts | 10 +-- .../server/src/provider/CodexProvider.test.ts | 10 ++- apps/server/src/provider/CodexProvider.ts | 12 +++- .../src/usage/UsageLimitSources.test.ts | 6 +- apps/server/src/usage/UsageLimitSources.ts | 34 +++++----- docs/user/usage.md | 5 +- packages/contracts/src/server.ts | 2 + packages/shared/src/usageLimits.test.ts | 66 +++++++++++++++++-- packages/shared/src/usageLimits.ts | 50 +++++++++----- 10 files changed, 139 insertions(+), 62 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 938ad7e81eec5..5dab46fa61483 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -276,7 +276,7 @@ it.effect("takes a hub's reset for an unreset usage_limit, then refreshes the sn }, }; const lookups: ProviderInstanceId[] = []; - const hubLookups: Array<[string, string, string | null]> = []; + const hubLookups: Array<[string, string | undefined, string | null]> = []; const instances = Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ getInstance: (id) => Effect.sync(() => { @@ -341,9 +341,9 @@ it.effect("takes a hub's reset for an unreset usage_limit, then refreshes the sn }).pipe( Effect.provide(makeLayerTest(layerTestEventSink, instances)), Effect.provideService(UsageLimitResetFallback, { - resetAt: (driver, endpoint, model) => + resetAt: (driver, auth, model) => Effect.sync(() => { - hubLookups.push([driver, endpoint, model]); + hubLookups.push([driver, auth.endpoint, model]); return "2026-11-13T18:00:00.000Z"; }), }), diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 49884b49120bb..c66b02fd564df 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -678,9 +678,8 @@ export const layer: Layer.Layer< (instance) => instance?.snapshot.getSnapshot ?? Effect.undefined, ))?.auth : undefined; - const endpoint = auth?.endpoint; const hubResetAt = - endpoint === undefined + auth?.endpoint === undefined ? null : yield* Effect.gen(function* () { // The failing run's model, which may differ from a selection changed mid-turn. @@ -697,12 +696,7 @@ export const layer: Layer.Layer< }).pipe( Effect.orElseSucceed(() => null), Effect.flatMap((model) => - usageLimitResets.resetAt( - input.event.driver, - endpoint, - model, - auth?.endpointPath, - ), + usageLimitResets.resetAt(input.event.driver, auth, model), ), ); const failure = hubResetAt === null ? reported : { ...reported, resetAt: hubResetAt }; diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index 65b9f9698e0a0..b2a0fa7d37ed2 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -335,10 +335,16 @@ it("reports native quota only for an OpenAI login on a native route", () => { assert.deepStrictEqual(codexCustomEndpoint(hub), { endpoint: "https://hub.example.com", endpointPath: "/team/v1", + endpointPathWithoutApiVersion: "/team", }); - assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1" }), { + assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1/" }), { endpoint: "http://127.0.0.1:8317", - endpointPath: "/v1", + endpointPath: "/v1/", + endpointPathWithoutApiVersion: "/", + }); + assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "https://hub.example.com/team" }), { + endpoint: "https://hub.example.com", + endpointPath: "/team", }); assert.strictEqual(codexCustomEndpoint({ openai_base_url: "https://chatgpt.com/x" }), undefined); assert.strictEqual(codexCustomEndpoint({}), undefined); diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 65109e62c4246..7450c721765b6 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -124,9 +124,15 @@ function routesToNativeOpenai(config: unknown): boolean { export function codexCustomEndpoint(config: unknown) { const baseUrl = codexRouteBaseUrl(config); const url = baseUrl ? URL.parse(baseUrl) : null; - return url && /^https?:$/.test(url.protocol) && !routesToNativeOpenai(config) - ? { endpoint: url.origin, endpointPath: url.pathname } - : undefined; + if (!url || !/^https?:$/.test(url.protocol) || routesToNativeOpenai(config)) return undefined; + const pathWithoutVersion = url.pathname.replace(/\/v1\/*$/, ""); + return { + endpoint: url.origin, + endpointPath: url.pathname, + ...(pathWithoutVersion !== url.pathname + ? { endpointPathWithoutApiVersion: pathWithoutVersion || "/" } + : {}), + }; } // A stored login does not mean this provider's traffic spends that login's quota. diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index d1d884721b239..3a2f4a5260e69 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -396,7 +396,11 @@ it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold t }); const lookup = UsageLimitSources.UsageLimitResetFallback.pipe( Effect.flatMap((fallback) => - fallback.resetAt(ProviderDriverKind.make("claudeAgent"), origin, null, "/team"), + fallback.resetAt( + ProviderDriverKind.make("claudeAgent"), + { status: "authenticated", endpoint: origin, endpointPath: "/team" }, + null, + ), ), Effect.provide( UsageLimitSources.layerResetFallback.pipe( diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index eb3842ac93186..12428efbd9630 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -4,6 +4,7 @@ import { type UsageLimitSourceConsumeResetCreditInput, type ProviderConsumeResetCreditResult, type ProviderDriverKind, + type ServerProviderAuth, type ServerSettings, UsageLimitSourceConfig, settingsSecretLiteral, @@ -280,9 +281,8 @@ export function hubPoolResetAt( export class UsageLimitResetFallback extends Context.Reference<{ readonly resetAt: ( driver: ProviderDriverKind, - endpoint: string, + auth: ServerProviderAuth, model: string | null, - endpointPath?: string, ) => Effect.Effect; }>("t3/usage/UsageLimitResetFallback", { defaultValue: () => ({ resetAt: () => Effect.succeed(null) }), @@ -293,23 +293,19 @@ export const layerResetFallback = Layer.effect( Effect.gen(function* () { const sources = yield* UsageLimitSources; return { - resetAt: Effect.fn("UsageLimitResetFallback.resetAt")( - function* (driver, endpoint, model, endpointPath) { - // Only the hub at the instance's endpoint serves its turns. - const atEndpoint = Effect.map(sources.current, (current) => - hubUsageSources(current, driver, endpoint, endpointPath), - ); - if ((yield* atEndpoint).length === 0) return null; - // The pool usually ran out after the last poll, so the cached accounts still show room. - yield* sources.refresh.pipe(Effect.timeout("5 seconds"), Effect.ignore); - return hubPoolResetAt( - yield* atEndpoint, - driver, - model, - DateTime.toEpochMillis(yield* DateTime.now), - ); - }, - ), + resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, auth, model) { + // Only the hub at the instance's endpoint serves its turns. + const atEndpoint = Effect.map(sources.current, (current) => hubUsageSources(current, auth)); + if ((yield* atEndpoint).length === 0) return null; + // The pool usually ran out after the last poll, so the cached accounts still show room. + yield* sources.refresh.pipe(Effect.timeout("5 seconds"), Effect.ignore); + return hubPoolResetAt( + yield* atEndpoint, + driver, + model, + DateTime.toEpochMillis(yield* DateTime.now), + ); + }), }; }), ); diff --git a/docs/user/usage.md b/docs/user/usage.md index 3320542e6bdda..e46ed176229df 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -190,8 +190,9 @@ account and choose **Use reset** to redeem one. No hub plugin is required. Configure the provider separately to send agent requests through the hub. When the Codex or Claude instance's API base URL points to the hub, the thread details card shows the hub's pooled limits for that provider. Use the same hostname, port, and reverse proxy path in both settings. A Codex API -base URL can include the OpenAI `/v1` suffix. Remove the hub from the same settings section when you -no longer need it. +base URL can include the OpenAI `/v1` suffix. When that URL matches both a hub with the suffix and +a hub without it, the card uses the instance's own limits. Remove the hub from the same settings +section when you no longer need it. ## Subscription usage widget diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index 0362f6263e066..3dec390353984 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -74,6 +74,8 @@ export const ServerProviderAuth = Schema.Struct({ endpoint: Schema.optional(TrimmedNonEmptyString), /** Path of the custom API base URL. A slash denotes the root path. */ endpointPath: Schema.optional(TrimmedNonEmptyString), + /** Alternative hub mount when the API base URL ends in a provider API version. */ + endpointPathWithoutApiVersion: 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 223d9c28ae60c..a78434bcbd96b 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1771,7 +1771,7 @@ describe("instance usage limits", () => { ["one hub account", [hub([hubAccount("a", 30)])], [30, 10]], ["the pool of hub accounts", [hub([hubAccount("a", 30), hubAccount("b", 70)])], [50, 10]], [ - "each account once across hubs on one origin", + "each account once across registrations of one hub", [ hub([hubAccount("a", 30)]), hub( @@ -1786,7 +1786,6 @@ describe("instance usage limits", () => { ], { id: UsageLimitSourceId.make("hub-2"), - endpoint: `${hubOrigin}/second`, }, ), ], @@ -1811,12 +1810,15 @@ describe("instance usage limits", () => { }); it.each([ - ["root", "/v1", hubOrigin], - ["a reverse proxy path", "/second/v1/", `${hubOrigin}/second`], + ["root", "/v1", "/", hubOrigin], + ["a reverse proxy path", "/second/v1/", "/second", `${hubOrigin}/second`], ])( "selects the hub for %s even when another hub shares its origin", - (_, endpointPath, endpoint) => { - const selected = { ...routed, auth: { ...routed.auth, endpointPath } }; + (_, endpointPath, endpointPathWithoutApiVersion, endpoint) => { + const selected = { + ...routed, + auth: { ...routed.auth, endpointPath, endpointPathWithoutApiVersion }, + }; const limits = limitsOf(selected, [ hub([hubAccount("a", endpoint === hubOrigin ? 100 : 0)]), hub([hubAccount("b", endpoint === hubOrigin ? 0 : 100)], { @@ -1828,6 +1830,58 @@ describe("instance usage limits", () => { }, ); + it("keeps the instance's own state when cached Claude auth matches multiple hub mounts", () => { + const claude = { ...routed, driver: ProviderDriverKind.make("claudeAgent") }; + expect( + limitsOf(claude, [ + hub([hubAccount("exhausted", 100, { driver: claude.driver })]), + hub([hubAccount("unused", 0, { driver: claude.driver })], { + id: UsageLimitSourceId.make("hub-2"), + endpoint: `${hubOrigin}/second`, + }), + ]), + ).toEqual(unsupported); + }); + + it("uses a literal Codex /v1 mount only when the same URL cannot also name a root hub", () => { + const selected = { + ...routed, + auth: { ...routed.auth, endpointPath: "/v1", endpointPathWithoutApiVersion: "/" }, + }; + const versionMount = hub([hubAccount("exhausted", 100)], { + endpoint: `${hubOrigin}/v1`, + }); + const rootMount = hub([hubAccount("unused", 0)], { + id: UsageLimitSourceId.make("hub-2"), + }); + expect(limitsOf(selected, [versionMount, rootMount])).toEqual(unsupported); + expect(limitsOf(selected, [versionMount])?.windows[0]?.usedPercent).toBe(100); + }); + + it.each([ + ["http://127.0.0.1:8317", 33], + ["https://hub.example.com", 50], + ])("uses the same auth file identity as Usage at %s", (origin, remaining) => { + const selected = { ...routed, auth: { ...routed.auth, endpoint: origin } }; + const authFile = hubAccount("spent.json", 100, { email: undefined }); + const sources = [ + { ...hub([authFile, hubAccount("unused", 0)]), origin, endpoint: origin }, + { ...hub([authFile]), id: UsageLimitSourceId.make("hub-2"), origin, endpoint: origin }, + ]; + const presentations = new Map([ + [ + EnvironmentId.make("local"), + { + entry: { target: { label: "Local" } }, + serverConfig: { providers: [], usageLimitSources: sources }, + }, + ], + ]); + const [pool] = collectLimitPools(collectLimitAccounts(presentations), now); + expect(displayLimitWindows(pool!).map(remainingPercent)).toEqual([remaining, 90]); + expect(limitsOf(selected, sources)?.windows.map(remainingPercent)).toEqual([remaining, 90]); + }); + it("pools separate workspaces for the same Codex email", () => { const email = "same@example.com"; const limits = limitsOf(routed, [ diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index a061a7a316627..25f6b1b858e29 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -11,6 +11,7 @@ import { isProviderAvailable, PROVIDER_DISPLAY_NAMES, type ServerProvider, + type ServerProviderAuth, type ServerProviderUsageLimits, type ServerProviderUsageWindow, type UsageLimitSourceSnapshot, @@ -262,23 +263,32 @@ const hubAccountName = (account: { readonly id: string }) => account.id.replace( export function hubUsageSources( sources: UsageLimitSourceSnapshots, - driver: ServerProvider["driver"], - endpoint: string | undefined, - endpointPath: string | undefined, + { + endpoint, + endpointPath, + endpointPathWithoutApiVersion, + }: Pick, ): UsageLimitSourceSnapshots { if (!endpoint) return []; - const path = endpointPath?.replace(/\/+$/, ""); - // Codex includes the OpenAI API's /v1 prefix in its base URL. - const base = driver === "codex" ? path?.replace(/\/v1$/, "") : path; - return sources.filter( - (source) => - source.origin === endpoint && - (base === undefined || (source.endpoint ?? source.origin) === `${endpoint}${base}`), - ); + const exact = + endpointPath === undefined ? undefined : endpoint + endpointPath.replace(/\/+$/, ""); + const alternate = + endpointPathWithoutApiVersion === undefined + ? undefined + : endpoint + endpointPathWithoutApiVersion.replace(/\/+$/, ""); + const matches = sources.filter((source) => { + if (source.origin !== endpoint) return false; + const mount = source.endpoint ?? source.origin; + return exact === undefined || mount === exact || mount === alternate; + }); + // Cached auth can lack a path, and a version suffix can itself be a literal mount. + return new Set(matches.map((source) => source.endpoint ?? source.origin)).size === 1 + ? matches + : []; } // Source ids are environment-local, so only a remote endpoint names the same hub across environments. -function hubIdentity(environmentId: EnvironmentId, source: UsageLimitSourceSnapshot): string { +function hubIdentity(source: UsageLimitSourceSnapshot, environmentId?: EnvironmentId): string { // The server supplies canonical endpoints; native URL polyfills can misparse IPv6 hosts. const endpoint = source.endpoint; const host = endpoint?.match(/^https?:\/\/(\[[\da-f:.]+\]|[^:/?#@]+)(?::\d+)?(?:\/|$)/)?.[1]; @@ -288,7 +298,12 @@ function hubIdentity(environmentId: EnvironmentId, source: UsageLimitSourceSnaps isWildcardHost(host) || // Each machine resolves these to itself too; shared host classification leaves them out. /\.localhost\.*$|^\[::ffff:(?:127\.|7f[\da-f]{2}:)/i.test(host); - return endpoint && !local ? endpoint : `${environmentId}:${source.id}`; + if (endpoint && !local) return endpoint; + return environmentId === undefined ? source.id : `${environmentId}:${source.id}`; +} + +function hubAccountFileKey(hub: string, account: UsageLimitSourceSnapshot["accounts"][number]) { + return `${hub}:${account.id}${account.workspaceId ? `:${account.workspaceId}` : ""}`; } export interface LimitAccount { @@ -419,8 +434,8 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl if (limitsNotice(account.usageLimits) !== null) continue; const resolved = resolveKey(account); // A resolved account already has an identity; its public key must stay distinct from others'. - const hub = resolved ? `${environmentId}:${source.id}` : hubIdentity(environmentId, source); - const key = `${hub}:${account.id}${account.workspaceId ? `:${account.workspaceId}` : ""}`; + const hub = resolved ? `${environmentId}:${source.id}` : hubIdentity(source, environmentId); + const key = hubAccountFileKey(hub, account); merge(resolved ?? key, { key, driver: account.driver, @@ -995,8 +1010,7 @@ export function instanceUsageLimits( ): ServerProviderUsageLimits | undefined { const selected = providers.find((provider) => provider.instanceId === instanceId); if (!selected) return undefined; - const { endpoint, endpointPath } = selected.auth; - const hubs = hubUsageSources(sources, selected.driver, endpoint, endpointPath); + const hubs = hubUsageSources(sources, selected.auth); const reports = hubs.flatMap((source) => source.accounts .filter((account) => account.driver === selected.driver) @@ -1006,7 +1020,7 @@ export function instanceUsageLimits( const hubAccounts = new Map(); for (const { source, account } of reports) { if (limitsNotice(account.usageLimits) !== null) continue; - const key = resolveKey(account) ?? `${source.id}:${account.id}:${account.workspaceId ?? ""}`; + const key = resolveKey(account) ?? hubAccountFileKey(hubIdentity(source), account); const previous = hubAccounts.get(key); if ( !previous || From 26b174c094843aeab49696e41f54cabf8d1bf37e Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:10:53 +0100 Subject: [PATCH 5/6] test(usage): cover distinct mounts and duplicate hub registrations Co-authored-by: GPT 6.2 Sol --- packages/shared/src/usageLimits.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index a78434bcbd96b..f1e745523d3cb 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1771,7 +1771,7 @@ describe("instance usage limits", () => { ["one hub account", [hub([hubAccount("a", 30)])], [30, 10]], ["the pool of hub accounts", [hub([hubAccount("a", 30), hubAccount("b", 70)])], [50, 10]], [ - "each account once across registrations of one hub", + "each account once across hubs on one origin", [ hub([hubAccount("a", 30)]), hub( @@ -1786,6 +1786,7 @@ describe("instance usage limits", () => { ], { id: UsageLimitSourceId.make("hub-2"), + endpoint: `${hubOrigin}/second`, }, ), ], @@ -1804,6 +1805,11 @@ describe("instance usage limits", () => { [30, 10], ], ])("shows %s for an instance routed through its hub", (_, sources, usedPercents) => { + if (sources.some((source) => source.endpoint !== hubOrigin)) { + expect(limitsOf(routed, sources)).toEqual(unsupported); + // Two registrations of the same mount can report overlapping accounts. + sources = sources.map((source) => ({ ...source, endpoint: hubOrigin })); + } const limits = limitsOf(routed, sources); expect(limits?.unavailable).toBeUndefined(); expect(limits?.windows.map((entry) => entry.usedPercent)).toEqual(usedPercents); From 4b7909b128e31c2e96e3b5f92ed4945077a83a21 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 14:27:27 +0000 Subject: [PATCH 6/6] test(shared): drop instance usage cases that existing coverage already guards Co-Authored-By: Claude Opus 5.5 --- packages/shared/src/usageLimits.test.ts | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index f1e745523d3cb..edaff5470e99d 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1888,17 +1888,6 @@ describe("instance usage limits", () => { expect(limitsOf(selected, sources)?.windows.map(remainingPercent)).toEqual([remaining, 90]); }); - it("pools separate workspaces for the same Codex email", () => { - const email = "same@example.com"; - const limits = limitsOf(routed, [ - hub([ - hubAccount("personal.json", 100, { email, workspaceId: "personal" }), - hubAccount("team.json", 0, { email, workspaceId: "team" }), - ]), - ]); - expect(limits?.windows[0]?.usedPercent).toBe(50); - }); - it("keeps a readable snapshot when a newer duplicate failed", () => { const limits = limitsOf(routed, [ hub([hubAccount("a", 30)]), @@ -1923,11 +1912,6 @@ describe("instance usage limits", () => { expect(limits?.windows.map(remainingPercent)).toEqual([1, 90]); }); - it("preserves a successful snapshot with no windows", () => { - const empty = { checkedAt, windows: [] }; - expect(limitsOf(routed, [hub([hubAccount("a", 0, { usageLimits: empty })])])).toEqual(empty); - }); - it("keeps a lone hub account's reset clock", () => { expect(limitsOf(routed, [hub([hubAccount("a", 30)])])?.windows[0]).toMatchObject({ resetsAt: window.resetsAt,