diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index 50e67bcbedac..a28c03843b16 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -401,6 +401,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps props.serverConfig?.providers ?? [], props.serverConfig?.usageLimitSources ?? [], Date.now(), + currentModelSelection.model, ); onShowUsageLimits(report); if (!report) { diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 7df9f595d855..fb0e4f46a1fb 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -1017,6 +1017,7 @@ function ThreadDetailBody( props.serverConfig?.providers ?? [], props.serverConfig?.usageLimitSources ?? [], usageLimitsPanel.now, + props.selectedThread.modelSelection.model, ) : null; const showUsageLimits = (report: UsageLimitsReport | null) => diff --git a/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts b/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts index 49c03c88f45a..a0fcb2122f16 100644 --- a/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts +++ b/apps/server/scripts/record-claude-agent-sdk-replay-fixture.ts @@ -82,6 +82,13 @@ const CLAUDE_RECORDINGS = { queryMode: "streaming", enableTools: true, }, + // Record with ANTHROPIC_BASE_URL at a CLIProxyAPI hub whose upstream accounts answer 429. + usage_limit: { + prompts: [SIMPLE_PROMPT], + defaultTranscriptFile: "fixtures/usage_limit/claude_transcript.ndjson", + queryMode: "streaming", + enableTools: true, + }, multi_turn: { prompts: [MULTI_TURN_FIRST_PROMPT, MULTI_TURN_SECOND_PROMPT], defaultTranscriptFile: "fixtures/multi_turn/claude_transcript.ndjson", diff --git a/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts index 1085fa04f756..d8a8157a8497 100644 --- a/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ClaudeReplayFixtures.integration.test.ts @@ -6,13 +6,19 @@ import { MessageId, ProviderDriverKind, type ProviderReplayEntry, + type ServerProvider, + UsageLimitSourceId, + type UsageLimitSourceSnapshot, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; import { ClaudeOrchestratorReplayHarness } from "../Adapters/ClaudeAdapterV2.testkit.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as Orchestrator from "../Orchestrator.ts"; import { userFacingDispatchErrorMessage } from "../UserFacingErrors.ts"; +import * as UsageLimitSources from "../../usage/UsageLimitSources.ts"; +import { claudeUsageLimitFailure } from "./fixtures/usage_limit/claude_output.ts"; import { provideDeterministicTestRuntime } from "./DeterministicRuntime.ts"; import { subagentInput } from "./fixtures/subagent/input.ts"; import { runOrchestratorV2Scenario } from "./OrchestratorScenario.ts"; @@ -122,6 +128,77 @@ describe("Claude Agent SDK replay fixtures", () => { }), ); + it.effect("takes the reset of the hub accounts that serve the model the hub refused", () => + Effect.gen(function* () { + const fixture = findReplayFixture("usage_limit")!; + const driver = fixture.providers[0]!; + const endpoint = "http://127.0.0.1:18319"; + const account = (id: string, model: string, usedPercent: number, resetsAt: string) => ({ + id, + driver: ProviderDriverKind.make("claudeAgent"), + models: [model], + usageLimits: { + checkedAt: resetsAt, + windows: [ + { id: "five_hour", kind: "session" as const, label: "Session", usedPercent, resetsAt }, + ], + }, + }); + const hub = (id: string, origin: string, accounts: ReadonlyArray) => + ({ + id: UsageLimitSourceId.make(id), + kind: "cliproxy", + label: id, + origin, + checkedAt: "", + accounts, + }) as UsageLimitSourceSnapshot; + yield* runReplayFixture({ + fixtureName: fixture.name, + buildInput: fixture.buildInput, + driver: { + ...driver, + assertOutput: (result, transcript) => + assert.equal( + claudeUsageLimitFailure(result, transcript).resetAt, + "2033-05-19T07:20:00.000Z", + ), + }, + harness: ClaudeOrchestratorReplayHarness, + providerInstances: { + getInstance: () => + Effect.succeed({ + snapshot: { + getSnapshot: Effect.succeed({ + auth: { status: "unknown", type: "customEndpoint", endpoint }, + } as ServerProvider), + }, + } as never), + }, + }).pipe( + Effect.provide( + UsageLimitSources.layerResetFallback.pipe( + Layer.provide( + Layer.mock(UsageLimitSources.UsageLimitSources)({ + current: Effect.succeed([ + hub("home", endpoint, [ + account("sonnet", driver.modelSelection.model, 100, "2033-05-19T07:20:00.000Z"), + // regression: #30778 an account the hub does not route the model to has no say + account("opus", "claude-opus-4-8", 10, "2033-05-19T05:00:00.000Z"), + ]), + hub("elsewhere", "http://127.0.0.1:9999", [ + account("other", driver.modelSelection.model, 100, "2033-05-18T00:00:00.000Z"), + ]), + ]), + refresh: Effect.void, + }), + ), + ), + ), + ); + }), + ); + const afterRootFixture = findReplayFixture("claude_background_subagent_after_root"); const afterRootProvider = afterRootFixture?.providers[0]; if (afterRootFixture !== undefined && afterRootProvider !== undefined) { diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayFixtures.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayFixtures.ts index 1683439c9641..769f891fc79a 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayFixtures.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayFixtures.ts @@ -8,6 +8,7 @@ import * as Path from "effect/Path"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; import { readProviderReplayTranscript } from "@t3tools/provider-testing/replayTranscript"; +import type { ProviderInstanceRegistry } from "../../provider/ProviderInstanceRegistry.ts"; import { provideDeterministicTestRuntime } from "./DeterministicRuntime.ts"; import { ORCHESTRATOR_REPLAY_FIXTURES } from "./fixtures/index.ts"; import { @@ -94,6 +95,8 @@ export interface ReplayFixtureCase { readonly buildInput: () => OrchestratorFixtureInput; readonly driver: ProviderOrchestratorReplayVariant; readonly transformTranscript?: (transcript: ProviderReplayTranscript) => ProviderReplayTranscript; + /** The instances the ingestor reads snapshots from, such as an endpoint behind a hub. */ + readonly providerInstances?: Partial; } export interface ReplayFixtureRun< @@ -145,11 +148,12 @@ const runFixtureProvider = Effect.fn("runOrchestratorReplayFixture")(function* < }, }; - const result = yield* runOrchestratorV2ProviderReplayScenario( - scenario, - input.harness, - input.driver.runContinuationWorker === true ? { runContinuationWorker: true } : {}, - ).pipe(provideDeterministicTestRuntime); + const result = yield* runOrchestratorV2ProviderReplayScenario(scenario, input.harness, { + ...(input.driver.runContinuationWorker === true ? { runContinuationWorker: true } : {}), + ...(input.providerInstances === undefined + ? {} + : { providerInstances: input.providerInstances }), + }).pipe(provideDeterministicTestRuntime); input.driver.assertOutput(result, transcript); assertProviderNativeSubagentRootTurns(result); const expectedAbsentWorkspacePaths = input.driver.expectedAbsentWorkspacePaths; diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts index 2c977140a5a2..6b51a0d90f88 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts @@ -208,6 +208,9 @@ export function runOrchestratorV2ProviderReplayScenario< readonly recoverOnStartup?: boolean; readonly continueThreadsAfterServerUpdate?: boolean; readonly managedPolicy?: ManagedPolicyDocument; + readonly providerInstances?: Partial< + ProviderInstanceRegistry.ProviderInstanceRegistry["Service"] + >; } = {}, ): Effect.Effect< OrchestratorV2ScenarioResult, @@ -252,6 +255,9 @@ export function layerProviderReplay; } = {}, ): Layer.Layer< | Orchestrator.OrchestratorV2 @@ -289,6 +295,10 @@ export function layerWithRegistry( store: ProjectionStore.ProjectionStoreV2Shape, ) => ProjectionStore.ProjectionStoreV2Shape; readonly managedPolicy?: ManagedPolicyDocument; + /** The instances the ingestor reads snapshots from, such as an endpoint behind a hub. */ + readonly providerInstances?: Partial< + ProviderInstanceRegistry.ProviderInstanceRegistry["Service"] + >; } = {}, ): Layer.Layer< | Orchestrator.OrchestratorV2 @@ -361,9 +371,9 @@ export function layerWithRegistry( const layerProviderEventIngestorProvided = ProviderEventIngestor.layer.pipe( Layer.provide( Layer.mergeAll( - Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ - getInstance: () => Effect.undefined, - }), + Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)( + options.providerInstances ?? { getInstance: () => Effect.undefined }, + ), layerStores, layerEventSinkProvided, IdAllocator.layer, diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/index.ts b/apps/server/src/orchestration-v2/testkit/fixtures/index.ts index 1a0b1dbea1f3..fe96b9368ae6 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/index.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/index.ts @@ -36,6 +36,7 @@ import { claudeMcpToolPresentationInput } from "./claude_mcp_tool_presentation/i import { assertClaudeMcpToolPresentationOutput } from "./claude_mcp_tool_presentation/output.ts"; import { claudeResultIsErrorInput } from "./claude_result_is_error/input.ts"; import { assertClaudeResultIsErrorOutput } from "./claude_result_is_error/output.ts"; +import { assertClaudeUsageLimitOutput } from "./usage_limit/claude_output.ts"; import { grokAutoBlockedCommandInput } from "./grok_auto_blocked_command/input.ts"; import { kiroCompactionInput } from "./kiro_compaction/input.ts"; import { kiroForkFromTurnInput } from "./kiro_fork_from_turn/input.ts"; @@ -513,6 +514,18 @@ export const ORCHESTRATOR_REPLAY_FIXTURES: ReadonlyArray item.type === "error", + ); + if (error?.type !== "error") throw new Error("expected an error turn item"); + assert.equal(error.failure.class, "usage_limit"); + assert.equal(error.failure.code, "api_error_429"); + return error.failure; +} + +export function assertClaudeUsageLimitOutput( + result: OrchestratorV2ScenarioResult, + transcript: ProviderReplayTranscript, +) { + // The hub drops the upstream rate-limit headers, so the CLI reports no reset time. + assert.isNull(claudeUsageLimitFailure(result, transcript).resetAt); +} diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/claude_transcript.ndjson b/apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/claude_transcript.ndjson new file mode 100644 index 000000000000..8ec4e28a1f28 --- /dev/null +++ b/apps/server/src/orchestration-v2/testkit/fixtures/usage_limit/claude_transcript.ndjson @@ -0,0 +1,20 @@ +{"type":"transcript_start","provider":"claudeAgent","protocol":"claude-agent-sdk.query","version":"0.2.111","scenario":"usage_limit","metadata":{"prompts":["Respond with the following text: fixture simple ok"],"model":"claude-sonnet-4-6","nativeSessionId":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","queryMode":"streaming","tools":"claude_code","permissionMode":"bypassPermissions","generatedBy":"recordClaudeAgentSdkReplayTranscript","recordedThrough":"CLIProxyAPI 7.2.130 at ANTHROPIC_BASE_URL with ANTHROPIC_AUTH_TOKEN; its one Claude API key upstream answered 429 rate_limit_error with anthropic-ratelimit-unified-reset, which the hub does not forward. Claude Code 2.1.326."}} +{"type":"expect_outbound","label":"query.open","frame":{"type":"query.open","options":{"model":"claude-sonnet-4-6","tools":{"type":"preset","preset":"claude_code"},"permissionMode":"bypassPermissions","settings":{"showThinkingSummaries":true},"allowDangerouslySkipPermissions":true,"sessionId":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117"}}} +{"type":"expect_outbound","label":"prompt.offer:1","frame":{"type":"prompt.offer","message":{"type":"user","message":{"role":"user","content":"Respond with the following text: fixture simple ok"},"parent_tool_use_id":null,"uuid":"301cffa7-d83e-49db-bbd8-1312e0e79c4e"}}} +{"type":"emit_inbound","label":"command_lifecycle","frame":{"type":"command_lifecycle","command_uuid":"301cffa7-d83e-49db-bbd8-1312e0e79c4e","state":"queued","uuid":"683f753d-e44d-418b-81be-fe1635105ee0","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117"}} +{"type":"emit_inbound","label":"command_lifecycle","frame":{"type":"command_lifecycle","command_uuid":"301cffa7-d83e-49db-bbd8-1312e0e79c4e","state":"started","uuid":"b00be524-44da-49bf-8d43-a4e060e6ed2f","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"init","agents":[],"apiKeySource":"none","claude_code_version":"2.1.326","cwd":"/tmp/claude-replay-usage_limit","tools":[],"mcp_servers":[],"model":"claude-sonnet-4-6","permissionMode":"bypassPermissions","slash_commands":[],"output_style":"default","skills":[],"plugins":[],"fast_mode_state":"off","uuid":"ad82e0c8-db2d-4309-af62-5e1072c2d771","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"status","status":"requesting","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"7d799f81-c12e-4f47-8cd9-273fef1b30a3","user_message_uuids":["301cffa7-d83e-49db-bbd8-1312e0e79c4e"]}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":1,"max_retries":10,"retry_delay_ms":526,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"593bcdde-73d4-42c5-857a-49bc8054f108"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":2,"max_retries":10,"retry_delay_ms":1084,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"78b8707b-2945-4a47-a417-865211f63165"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":3,"max_retries":10,"retry_delay_ms":2101,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"e5a2272c-ac69-40dc-afc1-74b8664dd394"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":4,"max_retries":10,"retry_delay_ms":4924,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"e88d160e-9ed6-4075-91a8-c46c5df070fc"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":5,"max_retries":10,"retry_delay_ms":8012,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"67cb3f4c-35dd-47b8-a484-fb4341403e3e"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":6,"max_retries":10,"retry_delay_ms":19519,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"8d70cb4a-6c32-4956-ac87-37cc1af2757a"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":7,"max_retries":10,"retry_delay_ms":39008,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"62a2c80e-71dd-433d-be92-9cbc4a122dc1"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":8,"max_retries":10,"retry_delay_ms":39524,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"6c828da7-7f7d-492b-82f5-394b3eaab75b"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":9,"max_retries":10,"retry_delay_ms":33452,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"293b9b2f-4c97-4790-b28f-9c8c0d80827a"}} +{"type":"emit_inbound","label":"system","frame":{"type":"system","subtype":"api_retry","attempt":10,"max_retries":10,"retry_delay_ms":35131,"error_status":429,"error":"rate_limit","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"7d3b9d7a-587b-4a07-a4fe-d67b5dbfa7e9"}} +{"type":"emit_inbound","label":"assistant","frame":{"type":"assistant","message":{"diagnostics":null,"id":"815983ba-9182-4ae4-a656-8158541615e4","container":null,"model":"","role":"assistant","stop_details":null,"stop_reason":"stop_sequence","stop_sequence":"","type":"message","usage":{"output_tokens_details":null,"input_tokens":0,"output_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":null,"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":null,"iterations":null,"speed":null,"fallback_credit":null},"content":[{"type":"text","text":"API Error: Request rejected (429) · All credentials for model claude-sonnet-4-6 are cooling down via provider claude"}],"context_management":null},"parent_tool_use_id":null,"session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","uuid":"c56a86ab-b7cf-4ca5-b9fe-2a44ff5f7adc","timestamp":"2026-11-17T18:47:23.739Z","error":"rate_limit","is_api_error_message":true,"api_error_status":429,"api_error_type":"rate_limit_error","user_message_uuid":"301cffa7-d83e-49db-bbd8-1312e0e79c4e","user_message_uuids":["301cffa7-d83e-49db-bbd8-1312e0e79c4e"]}} +{"type":"emit_inbound","label":"result","frame":{"duration_api_ms":0,"stop_reason":"stop_sequence","session_id":"b8eed9d2-ed85-48fb-a4f2-316f6a0b1117","total_cost_usd":0,"usage":{"output_tokens_details":{"thinking_tokens":0},"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0,"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"inference_geo":"","iterations":[],"speed":"standard","fallback_credit":null},"modelUsage":{},"permission_denials":[],"terminal_reason":"api_error","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","subagent_stats":{"spawned":0,"requested":{"background":0,"foreground":0,"unset":0},"started_in_background":0,"max_depth":0,"spawned_by_subagents":0,"completed":0,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{}},"safety_stops":0,"is_error":true,"num_turns":1,"subtype":"success","api_error_status":429,"api_error_type":"rate_limit_error","result":"API Error: Request rejected (429) · All credentials for model claude-sonnet-4-6 are cooling down via provider claude","type":"result","duration_ms":185616,"uuid":"ec685404-a730-46d8-a867-3b40e87cdc94","user_message_uuid":"301cffa7-d83e-49db-bbd8-1312e0e79c4e","user_message_uuids":["301cffa7-d83e-49db-bbd8-1312e0e79c4e"],"queued_turn_count":0,"result_index":0,"model_usage_since_last_result":{}}} +{"type":"runtime_exit","status":"success"} diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts index 4a71640aa262..6259c4fbe509 100644 --- a/apps/server/src/usage/UsageLimitSources.test.ts +++ b/apps/server/src/usage/UsageLimitSources.test.ts @@ -288,6 +288,7 @@ const spent = (id: string, hours: number): ServerProviderUsageWindow => ({ const hub = ( accounts: ReadonlyArray<{ driver?: string; + models?: ReadonlyArray; windows?: ReadonlyArray; unavailable?: true; }>, @@ -299,6 +300,7 @@ const hub = ( accounts: accounts.map((account, index) => ({ id: `account-${index}`, driver: ProviderDriverKind.make(account.driver ?? "claudeAgent"), + ...(account.models ? { models: account.models } : {}), usageLimits: { checkedAt: at(0), windows: account.windows ?? [], @@ -354,6 +356,86 @@ it.each([ model: "claude-haiku-5-5", expected: null, }, + // regression: #30778 only the accounts the hub routes the model to can refill it + { + name: "an account of another driver the hub routes the model to counts", + accounts: [ + { driver: "codex", models: ["gpt-5.6-sol"], windows: [spent("five_hour", 3)] }, + { models: ["claude-sonnet-4-6"], windows: [] }, + ], + model: "gpt-5.6-sol", + expected: at(3), + }, + { + name: "an account that lists no models is left out of an alias", + accounts: [{ windows: [spent("five_hour", 1)] }, { models: [], windows: [] }], + model: "opus", + expected: at(1), + }, + { + name: "a spent Gemini quota does not hold back Claude on Antigravity", + accounts: [ + { + driver: "antigravity", + models: ["claude-opus-4-6-thinking"], + windows: [spent("3p-5h", 1), spent("gemini-5h", 24)], + }, + ], + model: "claude-opus-4-6-thinking", + expected: at(1), + }, + { + name: "a hub thinking suffix routes by its base model", + accounts: [ + { driver: "codex", models: ["gpt-5.2"], windows: [spent("five_hour", 3)] }, + { models: ["claude-sonnet-4-6"], windows: [] }, + ], + model: "gpt-5.2(high)", + expected: at(3), + }, + { + name: "a prefixed Gemini model is held back by the Gemini quota", + accounts: [ + { + driver: "antigravity", + models: ["ag/gemini-3.1-pro-high"], + windows: [spent("3p-5h", 1), spent("gemini-5h", 24)], + }, + ], + model: "ag/gemini-3.1-pro-high", + expected: at(24), + }, + { + name: "an alias of unknown family is held back by every Antigravity quota", + accounts: [{ driver: "antigravity", models: ["fast"], windows: [spent("gemini-5h", 24)] }], + model: "fast", + expected: at(24), + }, + { + name: "an own-driver account whose model list was unread still counts", + accounts: [ + { windows: [spent("five_hour", 1)] }, + { + driver: "antigravity", + models: ["claude-sonnet-4-6"], + windows: [spent("3p-5h", 8)], + }, + ], + model: "claude-sonnet-4-6", + expected: at(1), + }, + { + name: "an Argon model waits only for the Argon quota", + accounts: [ + { + driver: "antigravity", + models: ["gemini-4-argon-high"], + windows: [spent("gemini-argon-5h", 2), spent("gemini-carbon-weekly", 96)], + }, + ], + model: "gemini-4-argon-high", + expected: at(2), + }, ])("hub pool reset: $name", ({ accounts, model, expected }) => { expect( UsageLimitSources.hubPoolResetAt( diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index ff54bb02781a..eba449c9a0fb 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -11,6 +11,7 @@ import { type UsageLimitSourceSnapshot, } from "@t3tools/contracts"; import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; +import { antigravityWindowLimitsModel, hubRoutes } from "@t3tools/shared/usageLimits"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; @@ -81,7 +82,9 @@ function keepLastGoodReadings( candidate.email === account.email && candidate.workspaceId === account.workspaceId, ); - return last && !last.usageLimits.unavailable ? last : account; + if (!last || last.usageLimits.unavailable) return account; + // Only the reading is lent; an unread model list stays unknown. + return { ...last, models: account.models }; }), }; }); @@ -249,7 +252,7 @@ export const layer = Layer.effect(UsageLimitSources, make).pipe( Layer.provide(SecretResolverLayer.layer), ); -/** A hub answers with a usage limit only when every account it routes the driver to is spent. */ +/** A hub returns a usage limit only when every account it routes the model to is spent. */ export function hubPoolResetAt( sources: ReadonlyArray, driver: ProviderDriverKind, @@ -257,21 +260,22 @@ export function hubPoolResetAt( now: number, ): string | null { let earliest: number | null = null; - for (const source of sources) { - for (const account of source.accounts) { - if (account.driver !== driver) continue; - // An unread account may be the one still blocked, so its refill is unknown. - if (account.usageLimits.unavailable) return null; - const spent = account.usageLimits.windows.filter( - (window) => window.usedPercent >= 100 && windowLimitsModel(window, model), - ); - if (spent.length === 0) return null; - const resets = spent.map((window) => Date.parse(window.resetsAt ?? "")); - if (resets.some((reset) => !Number.isFinite(reset))) return null; - const recoversAt = Math.max(...resets); - if (recoversAt <= now) return null; - earliest = earliest === null ? recoversAt : Math.min(earliest, recoversAt); - } + const routes = hubRoutes(sources, driver, model); + for (const account of sources.flatMap((source) => source.accounts).filter(routes)) { + // An unread account may be the one still blocked, so its refill is unknown. + if (account.usageLimits.unavailable) return null; + const spent = account.usageLimits.windows.filter( + (window) => + window.usedPercent >= 100 && + windowLimitsModel(window, model) && + (account.driver !== "antigravity" || antigravityWindowLimitsModel(window, model)), + ); + if (spent.length === 0) return null; + const resets = spent.map((window) => Date.parse(window.resetsAt ?? "")); + if (resets.some((reset) => !Number.isFinite(reset))) return null; + const recoversAt = Math.max(...resets); + if (recoversAt <= now) return null; + earliest = earliest === null ? recoversAt : Math.min(earliest, recoversAt); } return earliest === null ? null : DateTime.formatIso(DateTime.makeUnsafe(earliest)); } diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 5002d2223d1b..e0b98beb7363 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -60,6 +60,7 @@ function fixture( }>; upstream?: (request: RequestBody) => { status: number; body: unknown }; cooldownStatus?: number; + models?: (name: string) => Response; } = {}, ) { const requests: Array<{ path: string; body?: RequestBody }> = []; @@ -72,6 +73,12 @@ function fixture( ? decodeRequest(new TextDecoder().decode(request.body.body)) : undefined; requests.push({ path, ...(body ? { body } : {}) }); + if (path.endsWith("/auth-files/models")) + return HttpClientResponse.fromWeb( + request, + options.models?.(new URL(request.url).searchParams.get("name")!) ?? + Response.json({}, { status: 404 }), + ); if (path.endsWith("/auth-files")) return HttpClientResponse.fromWeb( request, @@ -228,6 +235,24 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect("names the models the hub routes to each account, when the hub lists them", () => + Effect.gen(function* () { + const test = fixture({ + accounts: [...accounts, { ...accounts[1]!, id: "third.json", auth_index: "c" }], + models: (name) => + name === "first.json" + ? Response.json({ models: [{ id: "gpt-5.6-sol" }, { id: " " }] }) + : name === "third.json" + ? Response.json({ models: [] }) + : Response.json({}, { status: 404 }), + }); + const api = yield* test.api; + const result = yield* api.readAccounts(config); + expect(result.map((account) => account.models)).toEqual([["gpt-5.6-sol"], undefined, []]); + expect(result[1]?.usageLimits.windows).toHaveLength(1); + }), + ); + it.effect("resolves a missing usage plan from current, then legacy, token plans", () => Effect.gen(function* () { const test = fixture({ @@ -620,7 +645,9 @@ describe("CLIProxyAPI built-in management API", () => { }); const [account] = yield* (yield* test.api).readAccounts(config); expect(account?.usageLimits.unavailable?.reason).toBe("probeFailed"); - expect(test.requests.map((request) => request.path)).toEqual(["/v0/management/auth-files"]); + expect(test.requests.map((request) => request.path)).not.toContain( + "/v0/management/api-call", + ); }), ); diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 537a2b436f4a..3d6857d55116 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -55,6 +55,9 @@ const AuthFile = Schema.Struct({ ), }); const AuthFiles = Schema.Struct({ files: Schema.Array(AuthFile) }); +const AuthFileModels = Schema.Struct({ + models: Schema.Array(Schema.Struct({ id: Schema.String })), +}); const ApiResponse = Schema.Struct({ status_code: Schema.Number, body: Schema.String }); const CodexWindow = Schema.Struct({ used_percent: Schema.Number, @@ -118,6 +121,7 @@ const CreditList = Schema.Struct({ }); const decodeAuthFiles = Schema.decodeUnknownEffect(AuthFiles); +const decodeAuthFileModels = Schema.decodeUnknownEffect(AuthFileModels); const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); @@ -439,6 +443,20 @@ export const makeCliproxyApi = Effect.gen(function* () { return (yield* decodeAuthFiles(response)).files; }); + // Older hubs lack this endpoint; the account then counts for every model of its driver. + const routedModels = Effect.fn("CliproxyApi.routedModels")(function* ( + hub: CliproxyHub, + account: AuthFile, + ) { + const response = yield* management( + hub, + `auth-files/models?name=${encodeURIComponent(account.id)}`, + ); + return (yield* decodeAuthFileModels(response)).models.flatMap(({ id }) => + id.trim() ? [id.trim()] : [], + ); + }); + const apiCall = Effect.fn("CliproxyApi.apiCall")(function* ( hub: CliproxyHub, account: AuthFile, @@ -494,14 +512,33 @@ export const makeCliproxyApi = Effect.gen(function* () { : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), ), ); - return yield* Effect.forEach( - accounts.flatMap((account) => { - const provider = HUB_PROVIDERS.get(account.provider); - return provider && !account.disabled ? [{ account, provider }] : []; - }), - ({ account, provider }) => readAccount(hub, account, provider), - { concurrency: 4 }, + const readable = accounts.flatMap((account) => { + const provider = HUB_PROVIDERS.get(account.provider); + return provider && !account.disabled ? [{ account, provider }] : []; + }); + // Model lists are optional, so their lookups run apart from the quota reads and stop at one + // short deadline; a slow lookup must not push a refresh past its budget. + const [readings, models] = yield* Effect.all( + [ + Effect.forEach(readable, ({ account, provider }) => readAccount(hub, account, provider), { + concurrency: 4, + }), + Effect.forEach( + readable, + ({ account }) => + routedModels(hub, account).pipe( + Effect.timeoutOption("2 seconds"), + Effect.orElseSucceed(() => Option.none()), + ), + { concurrency: "unbounded" }, + ), + ], + { concurrency: 2 }, ); + return readings.map((reading, index) => { + const listed = models[index]; + return listed && Option.isSome(listed) ? { ...reading, models: listed.value } : reading; + }); }); const consume = Effect.fn("CliproxyApi.consume")(function* ( diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 834584ba7a7f..169ee531148e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1401,6 +1401,11 @@ export default function ChatView(props: ChatViewProps) { const selectedProvider = selectedProviderEntry?.driverKind ?? requestedDriverKind; const activeProviderInstanceId = selectedProviderEntry?.instanceId ?? null; const activeProviderStatus = selectedProviderEntry?.snapshot ?? null; + const activeUsageModel = + [ + composerModelSelectionApplies ? composerSelectedModelSelection : null, + activeThread?.modelSelection, + ].find((selection) => selection?.instanceId === activeProviderInstanceId)?.model ?? null; const conversationProviderStatus = providerStatuses.find((status) => status.instanceId === activeRuntime?.providerInstanceId) ?? activeProviderStatus; @@ -1473,6 +1478,7 @@ export default function ChatView(props: ChatViewProps) { threadKey: routeThreadKey, panelKey: usageLimitsKey, providerInstanceId: activeProviderInstanceId, + model: activeUsageModel, providerDriver: activeProviderStatus?.driver ?? null, providers: providerStatuses, sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES, @@ -4040,6 +4046,7 @@ export default function ChatView(props: ChatViewProps) { onDeleteProjectScript={deleteProjectScriptStable} usage={{ providerInstanceId: activeProviderInstanceId, + model: activeUsageModel, providers: providerStatuses, sources: serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES, contextWindow: activeContextWindow, diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts new file mode 100644 index 000000000000..5a024cf4e558 --- /dev/null +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -0,0 +1,147 @@ +import { + ProviderDriverKind, + ProviderInstanceId, + type ServerProvider, + type ServerProviderUsageLimits, + type UsageLimitSourceSnapshot, + UsageLimitSourceId, +} from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { poolSources, resolveThreadDetailsLimits } from "./ThreadDetailsUsage.logic"; + +const now = Date.parse("2026-10-30T12:00:00.000Z"); +const checkedAt = "2026-10-30T11:59:00.000Z"; +const claude = ProviderDriverKind.make("claudeAgent"); +const codex = ProviderDriverKind.make("codex"); +const instanceId = ProviderInstanceId.make("claudeAgent"); + +function limitsAt(usedPercent: number): ServerProviderUsageLimits { + return { + checkedAt, + windows: [ + { + id: "five_hour", + kind: "session", + label: "Session", + usedPercent, + windowDurationMins: 300, + resetsAt: "2026-10-30T13:17:00.000Z", + }, + ], + }; +} + +const unsupported: ServerProviderUsageLimits = { + checkedAt, + windows: [], + unavailable: { reason: "unsupported" }, +}; + +function claudeInstance(usageLimits: ServerProviderUsageLimits): ServerProvider { + return { + instanceId, + driver: claude, + enabled: true, + installed: true, + version: null, + status: "ready", + auth: { + status: "authenticated", + type: "customEndpoint", + label: "Custom API endpoint", + endpoint: "http://127.0.0.1:8317", + }, + checkedAt, + models: [], + slashCommands: [], + skills: [], + usageLimits, + }; +} + +interface HubAccountInput { + readonly id: string; + readonly driver?: ProviderDriverKind; + readonly models?: readonly string[]; + readonly limits: ServerProviderUsageLimits; +} + +function hub( + label: string, + accounts: readonly HubAccountInput[], + origin = "http://127.0.0.1:8317", +): UsageLimitSourceSnapshot { + return { + id: UsageLimitSourceId.make(label), + kind: "cliproxy", + label, + origin, + checkedAt, + accounts: accounts.map((account) => ({ + id: account.id, + driver: account.driver ?? claude, + ...(account.models ? { models: account.models } : {}), + usageLimits: account.limits, + })), + }; +} + +function shown( + own: ServerProviderUsageLimits, + sources: readonly UsageLimitSourceSnapshot[], + model: string | null, +) { + const resolved = resolveThreadDetailsLimits( + instanceId, + model, + [claudeInstance(own)], + sources, + now, + ); + if (resolved?.kind !== "pool") return resolved; + return { + kind: resolved.kind, + rows: resolved.windows.map((window) => ({ + label: window.label, + left: window.remainingPercent, + accounts: window.members.length, + from: poolSources(window), + })), + }; +} + +describe("resolveThreadDetailsLimits", () => { + it("keeps an instance's failed reading instead of a hub it is not sent to", () => { + const failed: ServerProviderUsageLimits = { + checkedAt, + windows: [], + unavailable: { reason: "probeFailed" }, + }; + const direct = { ...claudeInstance(failed), auth: { status: "authenticated" as const } }; + const elsewhere = claudeInstance(unsupported); + const readableHub = hub("Hub", [{ id: "a.json", limits: limitsAt(20) }]); + const otherHub = hub("Other", [{ id: "a.json", limits: limitsAt(20) }], "http://hub"); + expect(resolveThreadDetailsLimits(instanceId, null, [direct], [readableHub], now)).toEqual({ + kind: "instance", + limits: failed, + }); + expect(resolveThreadDetailsLimits(instanceId, null, [elsewhere], [otherHub], now)).toEqual({ + kind: "instance", + limits: unsupported, + }); + }); + + it("pools only the accounts the hub routes the model to, even of another driver", () => { + const sources = [ + hub("Hub", [ + { id: "a.json", models: ["claude-opus-5-5"], limits: limitsAt(10) }, + { id: "c.json", driver: codex, models: ["gpt-5.6-sol"], limits: limitsAt(30) }, + ]), + ]; + expect(shown(unsupported, sources, "gpt-5.6-sol[1m]")).toEqual({ + kind: "pool", + rows: [{ label: "Session", left: 70, accounts: 1, from: { hubs: ["Hub"], signedIn: false } }], + }); + }); +}); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts new file mode 100644 index 000000000000..d3ccf8ed3a86 --- /dev/null +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -0,0 +1,113 @@ +import { + EnvironmentId, + type ProviderInstanceId, + type ServerProvider, + type ServerProviderUsageLimits, + type UsageLimitSourceSnapshots, +} from "@t3tools/contracts"; +import { + antigravityWindowLimitsModel, + collectLimitAccounts, + collectLimitPools, + collectProviderUsageLimits, + displayLimitWindows, + hubRoutes, + instanceHubs, + type LimitPoolWindow, +} from "@t3tools/shared/usageLimits"; +import { limitsNotice } from "@t3tools/shared/usageLimitsCopy"; + +export type ThreadDetailsLimits = + | { readonly kind: "instance"; readonly limits: ServerProviderUsageLimits } + | { readonly kind: "pool"; readonly windows: readonly LimitPoolWindow[] }; + +// collectLimitAccounts keys its input by environment; the card reads one, so any id serves. +const CARD_ENVIRONMENT = EnvironmentId.make("thread-details"); + +export function resolveThreadDetailsLimits( + providerInstanceId: ProviderInstanceId | null, + model: string | null, + providers: readonly ServerProvider[], + sources: UsageLimitSourceSnapshots, + now: number, +): ThreadDetailsLimits | null { + const provider = providers.find((entry) => entry.instanceId === providerInstanceId); + if (!provider) return null; + const report = collectProviderUsageLimits(provider.instanceId, providers, sources, now, model); + // Only this instance's data: the report keeps one instance per login, and any other entry may be + // a different login or workspace, so fall back to the instance's own snapshot. + const own = + report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ?? + provider.usageLimits; + if (!own) return null; + const hubs = instanceHubs(provider, sources); + if (limitsNotice(own) === null || hubs.length === 0) return { kind: "instance", limits: own }; + // An instance sent to a hub reads no limits of its own; its hub pools the accounts it spends. + return hubPool(provider.driver, model, providers, hubs, now) ?? { kind: "instance", limits: own }; +} + +export function nextPoolReset(window: LimitPoolWindow, now: number) { + const pooled = window.members.length > 1; + return ( + window.resets.find((reset) => reset.at > now && (!pooled || reset.restoresPercent > 0)) ?? null + ); +} + +function hubPool( + driver: ServerProvider["driver"], + model: string | null, + providers: readonly ServerProvider[], + sources: UsageLimitSourceSnapshots, + now: number, +): ThreadDetailsLimits | null { + const routes = hubRoutes(sources, driver, model); + const routed = sources.map((source) => ({ + ...source, + accounts: source.accounts.filter(routes).map((account) => + account.driver === "antigravity" + ? { + ...account, + usageLimits: { + ...account.usageLimits, + windows: account.usageLimits.windows.filter((window) => + antigravityWindowLimitsModel(window, model), + ), + }, + } + : account, + ), + })); + const readable = routed + .flatMap((source) => source.accounts) + .filter((account) => limitsNotice(account.usageLimits) === null); + if (readable.length === 0) return null; + // A signed-in login joins the pool only as a fresher reading of an account the hub routes to. + const signedIn = providers.filter((provider) => + readable.some( + (account) => + account.driver === provider.driver && + account.workspaceId === provider.auth.workspaceId && + account.email !== undefined && + account.email.toLowerCase() === provider.auth.email?.toLowerCase(), + ), + ); + const presentations = new Map([ + [ + CARD_ENVIRONMENT, + { + entry: { target: { label: "" } }, + serverConfig: { providers: signedIn, usageLimitSources: routed }, + }, + ], + ]); + const pools = collectLimitPools(collectLimitAccounts(presentations), now); + return pools.length > 0 ? { kind: "pool", windows: pools.flatMap(displayLimitWindows) } : null; +} + +export function poolSources(window: LimitPoolWindow) { + const hubs = window.members.flatMap(({ account }) => + account.environments.length === 0 && account.sourceLabel ? [account.sourceLabel] : [], + ); + const signedIn = window.members.some(({ account }) => account.environments.length > 0); + return { hubs: [...new Set(hubs)], signedIn }; +} diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index b2e09a35c526..d1975ef80136 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -1,12 +1,13 @@ import type { ProviderInstanceId, ServerProvider, + ServerProviderUsageLimits, ServerProviderUsageWindow, UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { - collectProviderUsageLimits, type LimitPace, + type LimitPoolWindow, paceOf, paceRemainingPercent, remainingPercent, @@ -25,6 +26,7 @@ import { formatUpcomingTimestamp } from "../../timestampFormat"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { formatContextWindowCompactionMessage } from "./compaction.logic"; import { ThreadDetailsSection } from "./ThreadDetailsSection"; +import { nextPoolReset, poolSources, resolveThreadDetailsLimits } from "./ThreadDetailsUsage.logic"; const PACE_LABEL: Record = { ahead: msg("Ahead of pace"), @@ -34,6 +36,7 @@ const PACE_LABEL: Record = { export interface ThreadDetailsUsageInput { readonly providerInstanceId: ProviderInstanceId | null; + readonly model: string | null; readonly providers: readonly ServerProvider[]; readonly sources: UsageLimitSourceSnapshots; readonly contextWindow: ContextWindowSnapshot | null; @@ -41,27 +44,24 @@ export interface ThreadDetailsUsageInput { type UsageTone = "error" | "warning" | null; +function limitTone(left: number): UsageTone { + if (left <= 10) return "error"; + if (left <= 20) return "warning"; + return null; +} + /** The selected provider's limit windows. Each row reads as room left and opens the usage page. */ export function ThreadDetailsUsage({ providerInstanceId, + model, providers, sources, }: Omit) { const gt = useGT(); - 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; - if (!limits) return null; - const notice = limitsNotice(limits, m); + const resolved = resolveThreadDetailsLimits(providerInstanceId, model, providers, sources, now); + if (!resolved) return null; return (
- {notice ? ( - {notice}} - opensUsage - /> + {resolved.kind === "pool" ? ( + resolved.windows.map((window) => ) ) : ( - limits.windows.map((window) => ) + )}
); } +function InstanceRows({ + limits, + now, +}: { + readonly limits: ServerProviderUsageLimits; + readonly now: number; +}) { + const gt = useGT(); + const m = useMessages(); + const notice = limitsNotice(limits, m); + if (notice) { + return ( + {notice}} + opensUsage + /> + ); + } + return limits.windows.map((window) => ); +} + /** The thread's context window. The bar fills as context is used; the tick marks auto-compaction. */ export function ThreadDetailsContext({ usage }: { readonly usage: ContextWindowSnapshot }) { const maxTokens = usage.maxTokens != null && usage.maxTokens > 0 ? usage.maxTokens : null; @@ -172,7 +190,7 @@ function LimitRow({ settings.timestampFormat); + const left = window.remainingPercent; + const accounts = window.members.length; + const from = poolSources(window); + const sourceLabels = [...from.hubs, ...(from.signedIn ? [gt("Signed-in accounts")] : [])]; + const next = nextPoolReset(window, now); + const resetsAt = next?.member.window.resetsAt; + const reset = + next && resetsAt + ? { + duration: formatDuration(next.at - now, m), + time: formatUpcomingTimestamp(resetsAt, timestampFormat, now, undefined, m), + } + : null; + return ( + + + {accounts > 1 + ? gt("{label}: {left}% left across {accounts} accounts", { + label: window.label, + left, + accounts, + }) + : gt("{label}: {left}% left", { label: window.label, left })} + + {window.pace ? ( + {m(PACE_LABEL[window.pace])} + ) : null} + {next && reset ? ( + + {accounts > 1 + ? gt("+{percent}% in {duration} · {time}", { + percent: next.restoresPercent, + ...reset, + }) + : gt("Resets in {duration} · {time}", reset)} + + ) : null} + {sourceLabels.length > 0 ? ( + + {gt("From {sources}", { sources: sourceLabels.join(", ") })} + + ) : null} + + } + /> + ); +} + /** One label, hairline, and value row; the whole row opens the details tooltip. With `opensUsage` it links to the usage page. */ function UsageRow({ label, diff --git a/apps/web/src/components/chat/useUsageLimitsPanel.ts b/apps/web/src/components/chat/useUsageLimitsPanel.ts index 09ebc64f2612..7024699f4deb 100644 --- a/apps/web/src/components/chat/useUsageLimitsPanel.ts +++ b/apps/web/src/components/chat/useUsageLimitsPanel.ts @@ -18,6 +18,7 @@ export function useUsageLimitsPanel(input: { readonly threadKey: string; readonly panelKey: string | null; readonly providerInstanceId: ProviderInstanceId | null; + readonly model: string | null; readonly providerDriver: ServerProvider["driver"] | null; readonly providers: readonly ServerProvider[]; readonly sources: UsageLimitSourceSnapshots; @@ -27,6 +28,7 @@ export function useUsageLimitsPanel(input: { threadKey, panelKey, providerInstanceId, + model, providerDriver, providers, sources, @@ -42,7 +44,7 @@ export function useUsageLimitsPanel(input: { } const report = panel !== null && panelKey !== null && panel.key === panelKey && providerInstanceId !== null - ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now) + ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now, model) : null; const banner = report !== null && panel !== null @@ -63,7 +65,7 @@ export function useUsageLimitsPanel(input: { const now = Date.now(); const nextReport = providerInstanceId !== null && panelKey !== null - ? collectProviderUsageLimits(providerInstanceId, providers, sources, now) + ? collectProviderUsageLimits(providerInstanceId, providers, sources, now, model) : null; if (nextReport && panelKey !== null) { setPanel({ key: panelKey, threadKey, now }); diff --git a/apps/web/src/i18n/translations/de.json b/apps/web/src/i18n/translations/de.json index 730949ecfc30..f6e630d587d6 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -4169,6 +4169,10 @@ "ebb26f93fb83a71b": "{label}: {left} % übrig", "1b16346722b65812": "{timeLeft} % des Zeitfensters übrig", "728c6cc1431e76c8": "Wird in {duration} zurückgesetzt · {time}", + "23622878aeee86d9": "Angemeldete Konten", + "d28c53157c8c0f5f": "{label}: {left} % übrig über {accounts} Konten", + "ba03873d5356238b": "+{percent} % in {duration} · {time}", + "78c1d280f021a28f": "Von {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "Die Zwischenablage-API ist beim Kopieren der Admin-Anfrage nicht verfügbar.", "aef4bec20e799aab": "Die Admin-Anfrage konnte nicht in die Zwischenablage kopiert werden.", diff --git a/apps/web/src/i18n/translations/en.json b/apps/web/src/i18n/translations/en.json index 64482e18216a..f362bb45dc10 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -4168,6 +4168,10 @@ "ebb26f93fb83a71b": "{label}: {left}% left", "1b16346722b65812": "{timeLeft}% of the window left", "728c6cc1431e76c8": "Resets in {duration} · {time}", + "23622878aeee86d9": "Signed-in accounts", + "d28c53157c8c0f5f": "{label}: {left}% left across {accounts} accounts", + "ba03873d5356238b": "+{percent}% in {duration} · {time}", + "78c1d280f021a28f": "From {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "Clipboard API is unavailable while copying admin request.", "aef4bec20e799aab": "Failed to copy admin request to the clipboard.", diff --git a/apps/web/src/i18n/translations/es.json b/apps/web/src/i18n/translations/es.json index 8c3bd00183ec..b6941561ef00 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -4166,6 +4166,10 @@ "ebb26f93fb83a71b": "{label}: {left} % restante", "1b16346722b65812": "Queda un {timeLeft}% de la ventana", "728c6cc1431e76c8": "Se restablece en {duration} · {time}", + "23622878aeee86d9": "Cuentas con sesión iniciada", + "d28c53157c8c0f5f": "{label}: queda un {left} % entre {accounts} cuentas", + "ba03873d5356238b": "+{percent} % en {duration} · {time}", + "78c1d280f021a28f": "De {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "La API del portapapeles no está disponible al copiar la solicitud de administrador.", "aef4bec20e799aab": "No se pudo copiar la solicitud de administrador al portapapeles.", diff --git a/apps/web/src/i18n/translations/fr.json b/apps/web/src/i18n/translations/fr.json index 17258a83acd7..6880ea1ad167 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -4167,6 +4167,10 @@ "ebb26f93fb83a71b": "{label} : {left} % restant", "1b16346722b65812": "{timeLeft} % de la fenêtre restant", "728c6cc1431e76c8": "Réinitialisation dans {duration} · {time}", + "23622878aeee86d9": "Comptes connectés", + "d28c53157c8c0f5f": "{label} : {left} % restants sur {accounts} comptes", + "ba03873d5356238b": "+{percent} % dans {duration} · {time}", + "78c1d280f021a28f": "Depuis {sources}", "e16b0b9b490c8bc7": "{label} : {value}", "ba67d0a9ff5dabf0": "L’API Presse-papiers n’est pas disponible pour copier la demande à l’administrateur.", "aef4bec20e799aab": "Impossible de copier la demande à l’administrateur dans le presse-papiers.", diff --git a/apps/web/src/i18n/translations/ja.json b/apps/web/src/i18n/translations/ja.json index 66eaf036ed90..07f0201541a5 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -4157,6 +4157,10 @@ "ebb26f93fb83a71b": "{label}: 残り {left}%", "1b16346722b65812": "期間の残り {timeLeft}%", "728c6cc1431e76c8": "{duration} 後にリセット · {time}", + "23622878aeee86d9": "サインイン済みのアカウント", + "d28c53157c8c0f5f": "{label}: {accounts} 個のアカウントで残り {left}%", + "ba03873d5356238b": "{duration}後に +{percent}% · {time}", + "78c1d280f021a28f": "取得元: {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "管理者へのリクエストをコピーする際にクリップボード API を利用できませんでした。", "aef4bec20e799aab": "管理者へのリクエストをクリップボードにコピーできませんでした。", diff --git a/apps/web/src/i18n/translations/ko.json b/apps/web/src/i18n/translations/ko.json index 8a36bcc967f6..d58c92c7fc0d 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -4154,6 +4154,10 @@ "ebb26f93fb83a71b": "{label}: {left}% 남음", "1b16346722b65812": "기간 {timeLeft}% 남음", "728c6cc1431e76c8": "{duration} 후 초기화 · {time}", + "23622878aeee86d9": "로그인된 계정", + "d28c53157c8c0f5f": "{label}: 계정 {accounts}개에서 {left}% 남음", + "ba03873d5356238b": "{duration} 후 +{percent}% · {time}", + "78c1d280f021a28f": "출처: {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "관리자 요청을 복사하는 중 클립보드 API를 사용할 수 없습니다.", "aef4bec20e799aab": "관리자 요청을 클립보드에 복사하지 못했습니다.", diff --git a/apps/web/src/i18n/translations/pt-BR.json b/apps/web/src/i18n/translations/pt-BR.json index 277a77ccf3d9..b34b25fb11eb 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -4166,6 +4166,10 @@ "ebb26f93fb83a71b": "{label}: {left}% restante", "1b16346722b65812": "{timeLeft}% da janela restante", "728c6cc1431e76c8": "Renova em {duration} · {time}", + "23622878aeee86d9": "Contas conectadas", + "d28c53157c8c0f5f": "{label}: {left}% restante em {accounts} contas", + "ba03873d5356238b": "+{percent}% em {duration} · {time}", + "78c1d280f021a28f": "De {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "A API da área de transferência não está disponível ao copiar o pedido para o administrador.", "aef4bec20e799aab": "Falha ao copiar o pedido para o administrador para a área de transferência.", diff --git a/apps/web/src/i18n/translations/ru.json b/apps/web/src/i18n/translations/ru.json index ac9dde8934f3..b6a8bae65d27 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -4166,6 +4166,10 @@ "ebb26f93fb83a71b": "{label}: осталось {left}%", "1b16346722b65812": "От окна осталось {timeLeft}%", "728c6cc1431e76c8": "Сброс через {duration} · {time}", + "23622878aeee86d9": "Аккаунты, в которые выполнен вход", + "d28c53157c8c0f5f": "{label}: осталось {left}% на {accounts} аккаунтах", + "ba03873d5356238b": "+{percent}% через {duration} · {time}", + "78c1d280f021a28f": "Источник: {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "API буфера обмена недоступен при копировании запроса администратору.", "aef4bec20e799aab": "Не удалось скопировать запрос администратору в буфер обмена.", diff --git a/apps/web/src/i18n/translations/sv.json b/apps/web/src/i18n/translations/sv.json index 05bbe8c0c976..2222ead5b7f6 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -4167,6 +4167,10 @@ "ebb26f93fb83a71b": "{label}: {left} % kvar", "1b16346722b65812": "{timeLeft} % av fönstret kvar", "728c6cc1431e76c8": "Återställs om {duration} · {time}", + "23622878aeee86d9": "Inloggade konton", + "d28c53157c8c0f5f": "{label}: {left} % kvar över {accounts} konton", + "ba03873d5356238b": "+{percent} % om {duration} · {time}", + "78c1d280f021a28f": "Från {sources}", "e16b0b9b490c8bc7": "{label}: {value}", "ba67d0a9ff5dabf0": "Urklipps-API:t är inte tillgängligt vid kopiering av administratörsbegäran.", "aef4bec20e799aab": "Kunde inte kopiera administratörsbegäran till urklipp.", diff --git a/apps/web/src/i18n/translations/zh.json b/apps/web/src/i18n/translations/zh.json index 0685e33f23a8..84904cccf32c 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -4173,6 +4173,10 @@ "ebb26f93fb83a71b": "{label}:剩余 {left}%", "1b16346722b65812": "窗口剩余 {timeLeft}%", "728c6cc1431e76c8": "{duration} 后重置 · {time}", + "23622878aeee86d9": "已登录的账号", + "d28c53157c8c0f5f": "{label}:{accounts} 个账号共剩余 {left}%", + "ba03873d5356238b": "{duration}后 +{percent}% · {time}", + "78c1d280f021a28f": "来自 {sources}", "e16b0b9b490c8bc7": "{label}:{value}", "ba67d0a9ff5dabf0": "复制管理员请求时剪贴板 API 不可用。", "aef4bec20e799aab": "无法将管理员请求复制到剪贴板。", diff --git a/docs/user/usage.md b/docs/user/usage.md index 5160becc2156..43f4c94a1932 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -188,8 +188,10 @@ accounts for other providers are not read. Codex accounts show banked reset cred account and choose **Use reset** to redeem one. No hub plugin is required. This connection supplies usage information; configure -the provider separately to send agent requests through the hub. Remove the hub from the same -settings section when you no longer need it. +the provider separately to send agent requests through the hub. A thread uses the hub's limits and +reset times only when its provider's endpoint has the same address as the hub, for example +`http://127.0.0.1:8317` for both. Remove the hub from the same settings section when you no longer +need it. ## Subscription usage widget diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index 446d4f1f3184..4b10f3745d39 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -78,6 +78,8 @@ export const UsageLimitSourceAccount = Schema.Struct({ email: Schema.optional(TrimmedNonEmptyString), plan: Schema.optional(TrimmedNonEmptyString), workspaceId: Schema.optional(TrimmedNonEmptyString), + /** The models the hub routes to this account. */ + models: Schema.optional(Schema.Array(TrimmedNonEmptyString)), usageLimits: ServerProviderUsageLimits, }); export type UsageLimitSourceAccount = typeof UsageLimitSourceAccount.Type; diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 0078c21b566b..eacb390a1bea 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1387,6 +1387,7 @@ describe("/usage-limits", () => { id: UsageLimitSourceId.make("hub"), kind: "cliproxy" as const, label: "Accounts", + origin: "http://hub:8317", checkedAt: limits.checkedAt, accounts: [ { @@ -1555,6 +1556,72 @@ describe("/usage-limits", () => { expect(report?.notices).toEqual([]); }); + it("shows a proxied instance the hub accounts its model routes to, of any driver", () => { + const routed = [ + { + ...sources[0]!, + accounts: sources[0]!.accounts.map((account) => + account.id === "oss" ? { ...account, models: ["gpt-5.6-sol"] } : account, + ), + }, + ]; + const proxy = provider({ + instanceId: ProviderInstanceId.make("claudex"), + driver: ProviderDriverKind.make("claudeAgent"), + auth: { status: "authenticated", type: "customEndpoint", endpoint: "http://hub:8317" }, + }); + // regression: #30778 a local login the hub does not route to is not this thread's account + const unrelated = provider({ + instanceId: ProviderInstanceId.make("codex-personal"), + usageLimits: limits, + auth: { status: "authenticated", email: "personal@example.com" }, + }); + const ids = (instance: ServerProvider, model: string | null) => + collectProviderUsageLimits( + instance.instanceId, + [selected, proxy, unrelated], + routed, + now, + model, + )?.accounts.map((account) => account.id); + expect(ids(proxy, "gpt-5.6-sol[1m]")).toEqual(["codex", "hub:oss"]); + // regression: #30778 another hub's model list does not decide this hub's routing + const other = { + ...routed[0]!, + id: UsageLimitSourceId.make("other"), + origin: "http://other:8317", + accounts: [ + { + id: "x", + driver: ProviderDriverKind.make("grok"), + models: ["gpt-5.6-sol"], + usageLimits: limits, + }, + ], + }; + expect( + collectProviderUsageLimits( + proxy.instanceId, + [selected, proxy], + [other, ...routed], + now, + "gpt-5.6-sol", + )?.accounts.map((account) => account.id), + ).toEqual(["codex", "hub:oss"]); + // regression: #30778 a custom endpoint other than the hub does not follow the hub's routing + const elsewhere = { ...proxy, auth: { ...proxy.auth, endpoint: "http://other:8317" } }; + expect( + collectProviderUsageLimits( + proxy.instanceId, + [selected, elsewhere], + routed, + now, + "gpt-5.6-sol", + ), + ).toBeNull(); + expect(ids(selected, "gpt-5.6-sol")).toEqual(["codex", "codex-personal", "hub:oss"]); + }); + it("keeps the freshest usable native duplicate and the freshest native credits in either order", () => { const failed = provider({ auth: selected.auth, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 693d82b95697..29c55b280b7d 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -13,6 +13,7 @@ import { type ServerProvider, type ServerProviderUsageLimits, type ServerProviderUsageWindow, + type UsageLimitSourceAccount, type UsageLimitSourceSnapshot, type UsageLimitSourceSnapshots, } from "@t3tools/contracts"; @@ -838,27 +839,102 @@ export function withUsageLimitsCommands( }); } +/** The hubs an instance sends its requests through: those whose address is its endpoint. */ +export function instanceHubs( + provider: ServerProvider, + sources: UsageLimitSourceSnapshots, +): UsageLimitSourceSnapshots { + const endpoint = provider.auth.endpoint; + return endpoint === undefined ? [] : sources.filter((source) => source.origin === endpoint); +} + +// Antigravity meters Gemini Argon, Gemini Carbon, other Gemini, and Claude/GPT models apart. +function antigravityQuotaFamily(id: string) { + if (!id.startsWith("gemini")) return "shared"; + return id.includes("argon") ? "argon" : id.includes("carbon") ? "carbon" : "gemini"; +} + +/** Each Antigravity quota blocks only the models it meters. */ +export function antigravityWindowLimitsModel( + window: Pick, + model: string | null, +): boolean { + // A hub can prefix model ids, such as "ag/gemini-3.1-pro". + const base = model?.slice(model.lastIndexOf("/") + 1); + // An alias such as "fast" may route to either quota, so both hold it back. + if (base === undefined || !/^(gemini|claude|gpt)/.test(base)) return true; + return antigravityQuotaFamily(window.id) === antigravityQuotaFamily(base); +} + +/** The hub accounts that serve a model: those that list it, else every account of the driver. */ +export function hubRoutes( + sources: UsageLimitSourceSnapshots, + driver: ServerProvider["driver"], + model: string | null, +): (account: UsageLimitSourceAccount) => boolean { + // Claude Code's 1M-context suffix is not part of the model id a hub routes. + const full = model?.replace(/\[1m\]$/i, ""); + // The hub routes a thinking suffix such as "gpt-5.2(high)" by its base model when one lists it. + const base = full?.replace(/\([^)]*\)$/, ""); + const lists = (id: string | undefined) => + sources.some((source) => source.accounts.some((account) => account.models?.includes(id ?? ""))); + const slug = lists(base) ? base : full; + const listed = (account: UsageLimitSourceAccount) => + slug !== undefined && account.models?.includes(slug) === true; + const serving = new Set( + sources.flatMap((source) => source.accounts.filter(listed).map((account) => account.driver)), + ); + // Model ids such as "opus" are aliases no hub lists, so no listing falls back to the driver. + if (serving.size === 0) + return (account) => account.driver === driver && account.models?.length !== 0; + // An account whose model list was unreadable may serve the model too. + return (account) => + account.models ? listed(account) : serving.has(account.driver) || account.driver === driver; +} + export function collectProviderUsageLimits( instanceId: ProviderInstanceId, providers: readonly ServerProvider[], sources: UsageLimitSourceSnapshots, now: number, + model: string | null = null, ): UsageLimitsReport | null { const selected = providers.find((provider) => provider.instanceId === instanceId); - if (!selected || !hasProviderUsageLimits(selected.driver, providers, sources)) return null; + if (!selected) return null; + // Only an instance sent to a hub is known to reach the accounts that hub routes its model to. + const hubs = instanceHubs(selected, sources); + const routes = + hubs.length > 0 + ? hubRoutes(hubs, selected.driver, model) + : (account: UsageLimitSourceAccount) => account.driver === selected.driver; + const shownHubs = hubs.length > 0 ? hubs : sources; + const drivers = new Set( + shownHubs.flatMap((source) => source.accounts.filter(routes).map((account) => account.driver)), + ); + if (drivers.size === 0) drivers.add(selected.driver); + if (![...drivers].some((driver) => hasProviderUsageLimits(driver, providers, sources))) { + return null; + } const resolveKey = accountKeyResolver([ ...providers.filter((provider) => provider.enabled).map(nativeReport), ...sources.flatMap((source) => source.accounts), ]); + // Behind a hub, a local login counts only when it is one of the accounts the hub routes to. + const routedKeys = + hubs.length > 0 + ? new Set(hubs.flatMap((source) => source.accounts.filter(routes).map(resolveKey))) + : null; type NativeSnapshot = { provider: ServerProvider; limits: ServerProviderUsageLimits }; const nativeByAccount = new Map< string, { usage: NativeSnapshot; credits: NativeSnapshot | null } >(); for (const provider of providersWithLimits(providers)) { - if (provider.driver !== selected.driver || !provider.usageLimits) continue; + if (!drivers.has(provider.driver) || !provider.usageLimits) continue; + const accountKey = resolveKey(nativeReport(provider)); + if (routedKeys && (accountKey === null || !routedKeys.has(accountKey))) continue; const snapshot = { provider, limits: provider.usageLimits }; - const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`; + const key = accountKey ?? `instance:${provider.instanceId}`; const previous = nativeByAccount.get(key); const previousUsable = previous ? limitsNotice(previous.usage.limits) === null : false; const usable = limitsNotice(snapshot.limits) === null; @@ -893,7 +969,8 @@ export function collectProviderUsageLimits( credits, } of native) { const key = resolveKey(nativeReport(provider)); - const hubCredits = sources + // Only a hub this instance reaches may redeem a credit for its thread. + const hubCredits = shownHubs .flatMap((source) => source.accounts.map((account) => ({ source, account }))) .filter( ({ account }) => @@ -935,8 +1012,8 @@ export function collectProviderUsageLimits( limits: shownCredits ? { ...limits, resetCredits: shownCredits } : limits, }); } - for (const source of sources) { - const matching = source.accounts.filter((account) => account.driver === selected.driver); + for (const source of shownHubs) { + const matching = source.accounts.filter(routes); for (const account of matching) { const key = resolveKey(account); if (key && nativeAccountKeys.has(key)) continue;