diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 5002d2223d1ba..0f1570f182bc6 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -58,13 +58,13 @@ function fixture( project_id?: string; id_token?: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string }; }>; - upstream?: (request: RequestBody) => { status: number; body: unknown }; + upstream?: (request: RequestBody) => { status: number; body: unknown } | "stall"; cooldownStatus?: number; } = {}, ) { const requests: Array<{ path: string; body?: RequestBody }> = []; const http = HttpClient.make((request) => - Effect.sync(() => { + Effect.suspend(() => { expect(request.headers.authorization).toBe("Bearer management-secret"); const path = new URL(request.url).pathname; const body = @@ -73,14 +73,18 @@ function fixture( : undefined; requests.push({ path, ...(body ? { body } : {}) }); if (path.endsWith("/auth-files")) - return HttpClientResponse.fromWeb( - request, - Response.json({ files: options.accounts ?? accounts }), + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({ files: options.accounts ?? accounts }), + ), ); if (path.endsWith("/reset-quota")) - return HttpClientResponse.fromWeb( - request, - Response.json({}, { status: options.cooldownStatus ?? 200 }), + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({}, { status: options.cooldownStatus ?? 200 }), + ), ); expect(path).toMatch(/\/v0\/management\/api-call$/); expect(body?.header?.Authorization).toBe("Bearer $TOKEN$"); @@ -108,9 +112,12 @@ function fixture( }, }, }; - return HttpClientResponse.fromWeb( - request, - Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }), + if (upstream === "stall") return Effect.never; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }), + ), ); }), ); @@ -373,6 +380,65 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect.each([ + { + profile: { + status: 200, + body: { + organization: { + organization_type: "claude_max", + rate_limit_tier: "default_claude_max_20x", + }, + }, + }, + plan: "Claude Ultra Subscription", + }, + { + profile: { status: 200, body: { organization: { organization_type: "claude_pro" } } }, + plan: "Claude Pro Subscription", + }, + { profile: { status: 500, body: {} }, plan: "Claude Subscription" }, + ])("labels a hub Claude account $plan from its profile", ({ profile, plan }) => + Effect.gen(function* () { + const test = fixture({ + accounts: [{ ...accounts[0]!, provider: "claude" }], + upstream: (request) => + request.url?.endsWith("/api/oauth/profile") + ? profile + : { status: 200, body: { seven_day: { utilization: 50, resets_at: null } } }, + }); + const result = yield* (yield* test.api).readAccounts(config); + expect(result[0]?.plan).toBe(plan); + expect(result[0]?.usageLimits.windows.map((window) => window.id)).toEqual(["seven_day"]); + }), + ); + + it.effect("bounds stalled Claude profile reads across the whole refresh", () => + Effect.gen(function* () { + const claude = Array.from({ length: 5 }, (_, index) => ({ + ...accounts[0]!, + id: `claude-${index}.json`, + auth_index: `c${index}`, + provider: "claude", + })); + const test = fixture({ + accounts: claude, + upstream: (request) => + request.url?.endsWith("/api/oauth/profile") + ? "stall" + : { status: 200, body: { seven_day: { utilization: 100, resets_at: null } } }, + }); + const api = yield* test.api; + const reading = yield* Effect.forkChild(api.readAccounts(config)); + yield* TestClock.adjust("3 seconds"); + const exit = reading.pollUnsafe(); + expect(exit?._tag).toBe("Success"); + expect( + exit?._tag === "Success" ? exit.value.map((account) => account.plan) : undefined, + ).toEqual(Array.from({ length: 5 }, () => "Claude Subscription")); + }), + ); + it.effect.each([ { name: "spend budget", diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 537a2b436f4a4..f827d4261e82c 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -106,6 +106,16 @@ const ClaudeUsage = Schema.Struct({ ), ), }); +const ClaudeProfile = Schema.Struct({ + organization: Schema.optional( + Schema.NullOr( + Schema.Struct({ + organization_type: Schema.optional(Schema.NullOr(Schema.String)), + rate_limit_tier: Schema.optional(Schema.NullOr(Schema.String)), + }), + ), + ), +}); const CreditList = Schema.Struct({ credits: Schema.Array( Schema.Struct({ @@ -123,10 +133,29 @@ const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage)); const decodeCodexUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(CodexUsage)); +const decodeClaudeProfile = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeProfile)); const decodeGrokUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(GrokUsageResponse)); const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); +/** Names the tier the way the native Claude provider does, so pools weight both alike. */ +function claudeHubPlanLabel(profile: typeof ClaudeProfile.Type | undefined): string { + const organization = profile?.organization; + if (organization?.rate_limit_tier?.includes("max_20x")) return "Claude Ultra Subscription"; + switch (organization?.organization_type) { + case "claude_max": + return "Claude Max Subscription"; + case "claude_pro": + return "Claude Pro Subscription"; + case "claude_team": + return "Claude Team Subscription"; + case "claude_enterprise": + return "Claude Enterprise Subscription"; + default: + return "Claude Subscription"; + } +} + // Extra CAs can fix untrusted chains; expiry/hostname failures report codes only. const UNTRUSTED_CERTIFICATE_CODES = new Set([ "DEPTH_ZERO_SELF_SIGNED_CERT", @@ -199,6 +228,8 @@ type HubCall = ( url: string, data?: unknown, ) => Effect.Effect; +/** Runs an optional request, or yields undefined once the refresh's shared budget is spent. */ +type HubOptional = (effect: Effect.Effect) => Effect.Effect; type HubReading = Omit; interface HubProvider { readonly driver: ProviderDriverKind; @@ -210,6 +241,7 @@ interface HubProvider { readonly read: (input: { readonly account: AuthFile; readonly call: HubCall; + readonly optional: HubOptional; readonly checkedAt: string; }) => Effect.Effect; } @@ -308,10 +340,20 @@ const HUB_PROVIDERS = new Map([ // The OAuth usage endpoint can return 429 without a Claude Code user agent. "User-Agent": "claude-code/2.1.273", }), - read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { - const usage = yield* decodeClaudeUsage( - yield* call("https://api.anthropic.com/api/oauth/usage"), + read: Effect.fnUntraced(function* ({ account, call, optional, checkedAt }) { + const [body, profile] = yield* Effect.all( + [ + call("https://api.anthropic.com/api/oauth/usage"), + // The tier only weights pools, so a failed read keeps the usage. + optional( + call("https://api.anthropic.com/api/oauth/profile").pipe( + Effect.flatMap(decodeClaudeProfile), + ), + ), + ], + { concurrency: 2 }, ); + const usage = yield* decodeClaudeUsage(body); const model_scoped = (usage.limits ?? []).flatMap((limit) => limit.kind === "weekly_scoped" && limit.scope?.model && typeof limit.percent === "number" ? [ @@ -332,7 +374,7 @@ const HUB_PROVIDERS = new Map([ }; return { ...listedIdentity(account), - plan: "Claude Subscription", + plan: claudeHubPlanLabel(profile), usageLimits: claudeUsageResponseToLimits({ checkedAt, response: { rate_limits_available: true, rate_limits: rateLimits }, @@ -466,11 +508,22 @@ export const makeCliproxyApi = Effect.gen(function* () { hub: CliproxyHub, account: AuthFile, provider: HubProvider, + optionalUntil: number, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); const call: HubCall = (url, data) => apiCall(hub, account, provider.header(account, platform, architecture), url, data); - const reading = yield* provider.read({ account, call, checkedAt }).pipe( + const optional: HubOptional = Effect.fn("CliproxyApi.optional")(function* ( + effect: Effect.Effect, + ) { + const left = optionalUntil - DateTime.toEpochMillis(yield* DateTime.now); + if (left <= 0) return undefined; + return yield* effect.pipe( + Effect.timeout(left), + Effect.orElseSucceed(() => undefined), + ); + }); + const reading = yield* provider.read({ account, call, optional, checkedAt }).pipe( Effect.provideService(Digest.Digest, digest), Effect.orElseSucceed(() => ({ ...listedIdentity(account), @@ -494,12 +547,15 @@ export const makeCliproxyApi = Effect.gen(function* () { : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), ), ); + // One budget for the whole refresh: per-account timeouts would stack across batches + // and outlast the usage-limit reset fallback's deadline. + const optionalUntil = DateTime.toEpochMillis(yield* DateTime.now) + 3_000; 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), + ({ account, provider }) => readAccount(hub, account, provider, optionalUntil), { concurrency: 4 }, ); }); diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 0078c21b566b7..895621a514536 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1018,6 +1018,45 @@ describe("pools", () => { ).toContainEqual(["pro@example.com", 95]); }); + it("weights pooled Claude weekly quota by the plan labels the provider reports", () => { + const [pool] = collectLimitPools( + collectLimitAccounts( + new Map([ + [ + EnvironmentId.make("env-a"), + { + ...laptop, + serverConfig: { + providers: [], + usageLimitSources: [ + { + ...source, + accounts: [ + ["ultra", "Claude Ultra Subscription", 100], + ["pro", "Claude Pro Subscription", 0], + ].map(([id, plan, usedPercent]) => ({ + id: String(id), + email: `${id}@example.com`, + driver: claude, + plan: String(plan), + usageLimits: { + checkedAt, + windows: [{ ...weekly, usedPercent: Number(usedPercent) }], + }, + })), + }, + ], + }, + }, + ], + ]), + ), + now, + ); + // Ultra holds 10 of the pool's 11 capacity parts. + expect(pool?.windows[0]?.remainingPercent).toBe(9); + }); + it("pools windows by id across accounts and orders resets by when they land", () => { const input = new Map([ [ diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 693d82b956971..b91d1abc15d0c 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -550,16 +550,21 @@ const CODEX_PLAN_CAPACITY: Record = { "ChatGPT Pro 100 Subscription": 5, "ChatGPT Pro 200 Subscription": 20, }; -// Claude's published plan multipliers describe the session allowance only. -const CLAUDE_SESSION_CAPACITY: Record = { Pro: 1, Max: 5, Ultra: 20 }; +// Claude's pricing page: Max is 5x Pro every 5 hours and week; Ultra is 10x Pro weekly with no 5-hour limit. +// Keyed by the auth label the Claude provider and hubs report. +const CLAUDE_PLAN_CAPACITY: Record = { + "Claude Pro Subscription": 1, + "Claude Max Subscription": 5, + "Claude Ultra Subscription": 10, +}; function planCapacity(account: LimitAccount, kind: ServerProviderUsageWindow["kind"]) { if (!account.plan) return undefined; if (account.driver === "codex" && (kind === "session" || kind === "weekly")) { return CODEX_PLAN_CAPACITY[account.plan]; } - if (account.driver === "claudeAgent" && kind === "session") { - return CLAUDE_SESSION_CAPACITY[account.plan]; + if (account.driver === "claudeAgent" && (kind === "session" || kind === "weekly")) { + return CLAUDE_PLAN_CAPACITY[account.plan]; } return undefined; }