From 424189ea329496b82291595e06dfe42005ad1a96 Mon Sep 17 00:00:00 2001 From: ScriptedAlchemy Date: Tue, 17 Nov 2026 11:15:16 -0800 Subject: [PATCH 1/5] fix(inference): show hosted account availability Read CLIProxyAPI account health when refreshing provider routes and show safe per-account status in settings. Reject new routes when no hosted account is eligible. --- .../src/inference/InferenceProxy.test.ts | 88 ++++++++++++++----- apps/server/src/inference/InferenceProxy.ts | 63 +++++++++++-- ...InferenceProviderAccounts.browser.test.tsx | 10 ++- .../settings/InferenceProviderAccounts.tsx | 16 +++- .../components/settings/InferenceSettings.tsx | 11 ++- packages/contracts/src/inferenceProxy.ts | 30 ++++++- 6 files changed, 184 insertions(+), 34 deletions(-) diff --git a/apps/server/src/inference/InferenceProxy.test.ts b/apps/server/src/inference/InferenceProxy.test.ts index 069700b6aeacd..2c72516ab7420 100644 --- a/apps/server/src/inference/InferenceProxy.test.ts +++ b/apps/server/src/inference/InferenceProxy.test.ts @@ -93,6 +93,10 @@ it.effect( }; let serving = false; let poolUnavailable = false; + let keepRuntimeAvailable = false; + let listedAccountRuntimeOnly = false; + let poolStatusMessage: string | undefined; + let poolRetryAfter: string | undefined; let lan = false; let rejectNextLaunch = false; let currentExit: Deferred.Deferred | undefined; @@ -228,7 +232,8 @@ it.effect( HttpClientResponse.fromWeb(request, Response.json({ environmentId: peerId })), ); } - remoteCatalogKeys.push(request.headers.authorization); + if (!request.url.endsWith("/health")) + remoteCatalogKeys.push(request.headers.authorization); if (staleLinkedCatalog && request.url.startsWith("http://127.0.0.1:34567/")) return Effect.succeed( HttpClientResponse.fromWeb(request, new Response(null, { status: 502 })), @@ -242,26 +247,34 @@ it.effect( HttpClientResponse.fromWeb( request, Response.json( - request.url.endsWith("auth-files") + request.url.endsWith("/health") ? { - files: [ - { - name: "codex:api-key:fixture", - provider: "codex", - status: "active", - unavailable: poolUnavailable, - runtime_only: true, - }, - { - name: "one.json", - provider: "codex", - status: "active", - unavailable: poolUnavailable, - refresh_token: "must stay local", - }, - ], + availableDrivers: poolUnavailable ? [] : ["codex", "opencode", "pi"], + models: catalog.map((model) => model.id), } - : { data: catalog }, + : request.url.endsWith("auth-files") + ? { + files: [ + { + name: "codex:api-key:fixture", + provider: "codex", + status: "active", + unavailable: poolUnavailable && !keepRuntimeAvailable, + runtime_only: true, + }, + { + name: "one.json", + provider: "codex", + status: "active", + unavailable: poolUnavailable, + runtime_only: listedAccountRuntimeOnly, + status_message: poolStatusMessage, + next_retry_after: poolRetryAfter, + refresh_token: "must stay local", + }, + ], + } + : { data: catalog }, ), ), ); @@ -652,7 +665,34 @@ it.effect( serving = true; yield* startSharing; const { accounts } = yield* proxy.status; - expect(accounts).toEqual([{ name: "one.json", provider: "codex", status: "active" }]); + expect(accounts).toEqual([ + { name: "one.json", provider: "codex", status: "active", availability: "available" }, + ]); + listedAccountRuntimeOnly = true; + expect((yield* proxy.status).accounts).toEqual([]); + expect((yield* proxy.status).availableDrivers).toContain("codex"); + listedAccountRuntimeOnly = false; + poolUnavailable = true; + poolStatusMessage = "HTTP 429 quota exhausted"; + poolRetryAfter = "2026-11-17T20:00:00Z"; + expect((yield* proxy.status).accounts[0]).toMatchObject({ + availability: "rateLimited", + retryAfter: poolRetryAfter, + }); + poolStatusMessage = "Failed to authenticate. API Error: 403 status code (no body)"; + poolRetryAfter = undefined; + expect((yield* proxy.status).accounts[0]).toMatchObject({ + availability: "authenticationError", + }); + poolStatusMessage = undefined; + poolRetryAfter = "1960-01-01T00:00:00Z"; + expect((yield* proxy.status).accounts[0]).toMatchObject({ + availability: "unavailable", + }); + expect((yield* proxy.status).accounts[0]).not.toHaveProperty("retryAfter"); + poolUnavailable = false; + poolStatusMessage = undefined; + poolRetryAfter = undefined; yield* proxy.manage({ operation: "setRouting", strategy: "fill-first" }); yield* proxy.manage({ operation: "setRouting", sessionAffinity: false }); const readConfig = fs @@ -675,6 +715,9 @@ it.effect( ]); poolUnavailable = true; expect((yield* proxy.health(issued.key))?.availableDrivers).toEqual([]); + keepRuntimeAvailable = true; + expect((yield* proxy.health(issued.key))?.availableDrivers).toContain("codex"); + keepRuntimeAvailable = false; poolUnavailable = false; expect((yield* proxy.health(issued.key))?.availableDrivers).toContain("codex"); const connect = { grant: local, driver: "codex" as const, hostLabel: "Host" }; @@ -690,6 +733,11 @@ it.effect( { id: "codex-pool-work", owned_by: "openai" }, { id: "claude-pool-sonnet", owned_by: "anthropic" }, ]; + poolUnavailable = true; + expect((yield* proxy.connect(connect).pipe(Effect.flip)).message).toContain( + "no available accounts", + ); + poolUnavailable = false; expect( (yield* proxy .connect({ ...connect, grant: { ...local, key: "expired-local-key" } }) diff --git a/apps/server/src/inference/InferenceProxy.ts b/apps/server/src/inference/InferenceProxy.ts index aa9c10156a8c4..5205dccb51fc3 100644 --- a/apps/server/src/inference/InferenceProxy.ts +++ b/apps/server/src/inference/InferenceProxy.ts @@ -87,6 +87,8 @@ const Files = Schema.Struct({ status: Schema.String, disabled: Schema.optionalKey(Schema.Boolean), unavailable: Schema.optionalKey(Schema.Boolean), + status_message: Schema.optionalKey(Schema.NullOr(Schema.String)), + next_retry_after: Schema.optionalKey(Schema.NullOr(Schema.String)), runtime_only: Schema.optionalKey(Schema.Boolean), }), ), @@ -156,6 +158,35 @@ const isProxyClaudeAuth = Schema.is(ProxyClaudeAuth); const isRecord = Schema.is(Schema.Record(Schema.String, Schema.Unknown)); const decodeUnknownJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown)); +function accountAvailability(account: (typeof Files.Type)["files"][number], now: number) { + if (account.disabled) return "disabled" as const; + if (account.status === "active" && !account.unavailable) return "available" as const; + const reason = account.status_message ?? ""; + if ( + /\b(?:401|403)\b|auth(?:entication)?[_ -]?fail|invalid[_ -]?grant|invalid[_ -]?credential/i.test( + reason, + ) + ) + return "authenticationError" as const; + if ( + /\b429\b|rate[_ -]?limit|quota|cooldown|exhausted/i.test(reason) || + (account.next_retry_after && Date.parse(account.next_retry_after) > now) + ) + return "rateLimited" as const; + return "unavailable" as const; +} + +function availableDrivers(files: (typeof Files.Type)["files"]) { + const available = files.filter( + (account) => account.status === "active" && !account.disabled && !account.unavailable, + ); + return INFERENCE_DRIVERS.filter((driver) => + available.some( + (account) => INFERENCE_HARNESSES[driver].accounts?.includes(account.provider) ?? true, + ), + ); +} + function jwtClaims(token: string): Record | null { const payload = token.split(".")[1]; if (!payload) return null; @@ -816,6 +847,7 @@ const make = Effect.gen(function* () { ]; }), routing: state.routing ?? defaultInferenceRouting, + availableDrivers: [], version: state.release?.version ?? proxyVersion, enabled: state.enabled, sharing: state.sharing, @@ -831,12 +863,27 @@ const make = Effect.gen(function* () { runtime.management, Files, ); + const now = yield* Clock.currentTimeMillis; return { ...summary, running: true, + availableDrivers: availableDrivers(files), accounts: files .filter((file) => !file.runtime_only) - .map(({ name, provider, status }) => ({ name, provider, status })), + .map((file) => { + const availability = accountAvailability(file, now); + return { + name: file.name, + provider: file.provider, + status: file.status, + availability, + ...(availability === "rateLimited" && + file.next_retry_after && + Date.parse(file.next_retry_after) > now + ? { retryAfter: file.next_retry_after } + : {}), + }; + }), problem: routeProblem ?? sync.problem, }; }).pipe( @@ -1238,15 +1285,8 @@ const make = Effect.gen(function* () { ], { concurrency: "unbounded" }, ); - const available = files.filter( - (account) => account.status === "active" && !account.disabled && !account.unavailable, - ); return { - availableDrivers: INFERENCE_DRIVERS.filter((driver) => - available.some( - (account) => INFERENCE_HARNESSES[driver].accounts?.includes(account.provider) ?? true, - ), - ), + availableDrivers: availableDrivers(files), models: data.map((model) => model.id), }; }); @@ -1333,6 +1373,11 @@ const make = Effect.gen(function* () { }), ), ); + const health = local + ? yield* mutex.withPermit(localHealth()) + : yield* jsonRequest(baseUrl + "/health", grant.key, InferenceRouteHealth); + if (!health.availableDrivers.includes(driver)) + return yield* failure("The inference host has no available accounts for this provider."); const models = modelsForDriver(data, driver); if (!models.length) return yield* failure("Sign in to an account for this provider on the host, then retry."); diff --git a/apps/web/src/components/settings/InferenceProviderAccounts.browser.test.tsx b/apps/web/src/components/settings/InferenceProviderAccounts.browser.test.tsx index 5108b64b0dfaa..a32d52be55d43 100644 --- a/apps/web/src/components/settings/InferenceProviderAccounts.browser.test.tsx +++ b/apps/web/src/components/settings/InferenceProviderAccounts.browser.test.tsx @@ -22,7 +22,13 @@ function setup() { data: { enabled: true, accounts: [ - { name: "work.json", provider: "codex", status: "active" }, + { + name: "work.json", + provider: "codex", + status: "error", + availability: "rateLimited", + retryAfter: "2099-01-01T00:00:00Z", + }, { name: "personal.json", provider: "claude", status: "active" }, ], }, @@ -44,6 +50,8 @@ describe("inference provider accounts", () => { , ); await expect.element(screen.getByText("work.json")).toBeVisible(); + await expect.element(screen.getByText("codex · Rate limited")).toBeVisible(); + await expect.element(screen.getByText(/Retry after/)).toBeVisible(); await expect.element(screen.getByText("personal.json")).not.toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: "Codex", exact: true })); expect(state.run).toHaveBeenLastCalledWith({ diff --git a/apps/web/src/components/settings/InferenceProviderAccounts.tsx b/apps/web/src/components/settings/InferenceProviderAccounts.tsx index 86d5d1c0854e6..c074effd2e2d2 100644 --- a/apps/web/src/components/settings/InferenceProviderAccounts.tsx +++ b/apps/web/src/components/settings/InferenceProviderAccounts.tsx @@ -19,6 +19,13 @@ import { SettingsRow } from "./settingsLayout"; const { inferenceProxy } = serverEnvironment; const everyUpstream = Object.keys(INFERENCE_UPSTREAMS) as ReadonlyArray; +const accountStatus = { + available: "Active", + authenticationError: "Authentication or access rejected", + rateLimited: "Rate limited", + unavailable: "Unavailable", + disabled: "Disabled", +} as const; export function InferenceProviderAccounts({ environmentId, @@ -107,7 +114,14 @@ export function InferenceProviderAccounts({ + Retry after {new Date(account.retryAfter).toLocaleString()} + + ) : undefined + } control={ - } - /> - ))} + .map((account) => { + const retryAfter = inferenceAccountRetryAfter(account); + return ( + + Retry after {new Date(retryAfter).toLocaleString()} + + ) : undefined + } + control={ + + } + /> + ); + })} {login ? ( <> - status.data?.availableDrivers?.includes(driver) ?? - status.data?.accounts.some( - (account) => - (INFERENCE_HARNESSES[driver].accounts?.includes(account.provider) ?? true) && - (account.availability === "available" || - (account.availability === undefined && account.status === "active")), - ) ?? - false; + status.data?.availableDrivers.includes(driver) ?? false; const route = useAtomCommand(inferenceProxy.routeInference, { reportFailure: false }); const [busy, setBusy] = useState(false); const [notice, setNotice] = useState(""); diff --git a/docs/user/providers-claude.md b/docs/user/providers-claude.md index 192be85b73c61..244228628a719 100644 --- a/docs/user/providers-claude.md +++ b/docs/user/providers-claude.md @@ -64,6 +64,10 @@ T3 keeps a thread on the host that successfully handles its request after a swit host selects accounts independently, so a switch between hosts with multiple eligible accounts may use a different account even when the same login is present on both. +In **Inference accounts**, a rate-limited Claude account shows its retry time when known. +If two hosts share that login, its subscription limit applies on both. Another host can +serve the request if it has an eligible account for the model. + Configure account pooling on the host. Turn on round-robin routing to distribute requests across eligible accounts, or leave it off to use the first available account. Session affinity keeps a conversation on its credential while available. The model diff --git a/packages/client-runtime/src/state/inferenceProxy.ts b/packages/client-runtime/src/state/inferenceProxy.ts index ec1c2798defa0..18f1df840b5c8 100644 --- a/packages/client-runtime/src/state/inferenceProxy.ts +++ b/packages/client-runtime/src/state/inferenceProxy.ts @@ -31,6 +31,24 @@ export interface InferenceFleetEnvironment extends InferenceConsumer { readonly serverConfig: EnvironmentServerConfig | null; } +type InferenceAccount = InferenceProxyStatus["accounts"][number]; + +const accountAvailabilityLabels = { + available: "Active", + authenticationError: "Authentication or access rejected", + rateLimited: "Rate limited", + unavailable: "Unavailable", + disabled: "Disabled", +} as const; + +export function inferenceAccountStatus(account: InferenceAccount): string { + return accountAvailabilityLabels[account.availability]; +} + +export function inferenceAccountRetryAfter(account: InferenceAccount): string | null { + return account.availability === "rateLimited" ? (account.retryAfter ?? null) : null; +} + export function planInferenceFallbacks( host: InferenceFleetEnvironment, status: InferenceProxyStatus, @@ -47,7 +65,7 @@ export function planInferenceFallbacks( if (consumer.connection.phase !== "connected" || !config) return []; if (host.environmentId !== consumer.environmentId && !status.sharing) return []; return INFERENCE_DRIVERS.flatMap((driver) => { - if (!status.availableDrivers?.includes(driver)) return []; + if (!status.availableDrivers.includes(driver)) return []; const instance = config.settings.providerInstances[ defaultInstanceIdForDriver(ProviderDriverKind.make(driver)) diff --git a/packages/contracts/src/inferenceProxy.ts b/packages/contracts/src/inferenceProxy.ts index 19d4223293c9a..30db397365c52 100644 --- a/packages/contracts/src/inferenceProxy.ts +++ b/packages/contracts/src/inferenceProxy.ts @@ -94,19 +94,17 @@ export const InferenceProxyStatus = Schema.Struct({ name: Schema.String, provider: Schema.String, status: Schema.String, - availability: Schema.optionalKey( - Schema.Literals([ - "available", - "authenticationError", - "rateLimited", - "unavailable", - "disabled", - ]), - ), + availability: Schema.Literals([ + "available", + "authenticationError", + "rateLimited", + "unavailable", + "disabled", + ]), retryAfter: Schema.optionalKey(Schema.String), }), ), - availableDrivers: Schema.optionalKey(Schema.Array(InferenceDriver)), + availableDrivers: Schema.Array(InferenceDriver), consumers: Schema.Array(InferenceConsumer), routes: Schema.Array(Schema.Struct({ instanceId: ProviderInstanceId, label: Schema.String })), }); @@ -117,18 +115,9 @@ export function inferenceRouteIssue(host: InferenceProxyStatus | null, local: bo if (!host.enabled) return "Enable inference hosting on this environment first."; if (!host.running) return host.problem ?? "The inference host is stopped."; if (!local && !host.sharing) return "Enable sharing on the host first."; - if (!host.accounts.length && !host.availableDrivers?.length) + if (!host.accounts.length && !host.availableDrivers.length) return "Add a provider account on the host first."; - if (host.availableDrivers?.length === 0) return "All hosted accounts are unavailable."; - if ( - host.availableDrivers === undefined && - !host.accounts.some( - (account) => - account.availability === "available" || - (account.availability === undefined && account.status === "active"), - ) - ) - return "All hosted accounts are unavailable."; + if (host.availableDrivers.length === 0) return "All hosted accounts are unavailable."; return null; }