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 e036bbe8994c1..c66b02fd564df 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -671,15 +671,15 @@ 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 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. @@ -696,7 +696,7 @@ export const layer: Layer.Layer< }).pipe( Effect.orElseSucceed(() => null), Effect.flatMap((model) => - usageLimitResets.resetAt(input.event.driver, endpoint, model), + usageLimitResets.resetAt(input.event.driver, auth, model), ), ); 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..b2a0fa7d37ed2 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,20 @@ 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", + endpointPathWithoutApiVersion: "/team", + }); + assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1/" }), { + endpoint: "http://127.0.0.1:8317", + 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 63154f4e99499..7450c721765b6 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -120,13 +120,19 @@ 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 - : 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. @@ -156,7 +162,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 +817,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 +1182,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..3a2f4a5260e69 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,15 @@ 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"), + { 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 ff54bb02781a9..12428efbd9630 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -4,12 +4,14 @@ import { type UsageLimitSourceConsumeResetCreditInput, type ProviderConsumeResetCreditResult, type ProviderDriverKind, + type ServerProviderAuth, type ServerSettings, UsageLimitSourceConfig, settingsSecretLiteral, 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"; @@ -279,7 +281,7 @@ export function hubPoolResetAt( export class UsageLimitResetFallback extends Context.Reference<{ readonly resetAt: ( driver: ProviderDriverKind, - endpoint: string, + auth: ServerProviderAuth, model: string | null, ) => Effect.Effect; }>("t3/usage/UsageLimitResetFallback", { @@ -291,11 +293,9 @@ export const layerResetFallback = Layer.effect( Effect.gen(function* () { const sources = yield* UsageLimitSources; return { - resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, endpoint, model) { + 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) => - current.filter((source) => source.origin === endpoint), - ); + 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); 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..e46ed176229df 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -187,9 +187,12 @@ 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. 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. 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 f074b8f6a3e46..3dec390353984 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -72,6 +72,10 @@ 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), + /** 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 0078c21b566b7..edaff5470e99d 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -20,8 +20,10 @@ import { collectLimitPools, elapsedShare, exhaustedUsageWindow, + instanceUsageLimits, paceOf, paceRemainingPercent, + remainingPercent, usesChatGptSharing, resolveNewThreadModelSelection, } from "./usageLimits.ts"; @@ -1729,6 +1731,231 @@ 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) => { + 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); + }); + + it.each([ + ["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, 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)], { + id: UsageLimitSourceId.make("hub-2"), + endpoint: `${hubOrigin}/second`, + }), + ]); + expect(limits?.windows[0]?.usedPercent).toBe(100); + }, + ); + + 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("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("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", diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 693d82b956971..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, @@ -260,8 +261,34 @@ function accountKeyResolver(reports: Iterable) { const hubAccountName = (account: { readonly id: string }) => account.id.replace(/\.json$/i, ""); +export function hubUsageSources( + sources: UsageLimitSourceSnapshots, + { + endpoint, + endpointPath, + endpointPathWithoutApiVersion, + }: Pick, +): UsageLimitSourceSnapshots { + if (!endpoint) return []; + 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]; @@ -271,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 { @@ -402,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, @@ -678,7 +710,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, @@ -965,3 +997,83 @@ 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 hubs = hubUsageSources(sources, selected.auth); + 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, account } of reports) { + if (limitsNotice(account.usageLimits) !== null) continue; + const key = resolveKey(account) ?? hubAccountFileKey(hubIdentity(source), account); + const previous = hubAccounts.get(key); + if ( + !previous || + Date.parse(account.usageLimits.checkedAt) > Date.parse(previous.usageLimits.checkedAt) + ) { + hubAccounts.set(key, account); + } + } + const readable = [...hubAccounts]; + 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; + 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 ( + collectProviderUsageLimits(instanceId, providers, sources, now)?.accounts.find( + (entry) => entry.instanceId === instanceId, + )?.limits ?? selected.usageLimits + ); +}