From 0a47d250dc054855762f0013a4d15209365880be Mon Sep 17 00:00:00 2001 From: Bil0000 <62337003+Bil0000@users.noreply.github.com> Date: Sat, 21 Nov 2026 13:45:42 +0100 Subject: [PATCH] feat(web): thread card shows the hub account serving the thread --- apps/server/src/auth/RpcAuthorization.ts | 1 + .../src/observability/RpcInstrumentation.ts | 1 + apps/server/src/usage/UsageLimitSources.ts | 42 +++++++++++++++---- apps/server/src/usage/cliproxyApi.test.ts | 18 ++++++++ apps/server/src/usage/cliproxyApi.ts | 16 ++++++- apps/server/src/ws.ts | 2 + apps/web/src/components/ChatView.tsx | 5 +++ .../chat/ThreadDetailsUsage.logic.test.ts | 17 ++++++++ .../chat/ThreadDetailsUsage.logic.ts | 13 +++++- .../components/chat/ThreadDetailsUsage.tsx | 37 +++++++++++++++- docs/user/usage.md | 5 ++- packages/client-runtime/src/state/server.ts | 7 ++++ packages/contracts/src/providerUsageLimits.ts | 10 +++++ packages/contracts/src/rpc.ts | 12 ++++++ 14 files changed, 174 insertions(+), 12 deletions(-) diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.ts index ffe84c428a67a..e238a3c841ac4 100644 --- a/apps/server/src/auth/RpcAuthorization.ts +++ b/apps/server/src/auth/RpcAuthorization.ts @@ -69,6 +69,7 @@ export const RPC_REQUIRED_SCOPES = { [WS_METHODS.projectsMutate]: AuthOrchestrationOperateScope, [WS_METHODS.serverProbe]: AuthOrchestrationReadScope, [WS_METHODS.serverGetConfig]: AuthOrchestrationReadScope, + [WS_METHODS.providerUsageLimitSourceServingAccount]: AuthOrchestrationReadScope, [WS_METHODS.serverGetNpmRelease]: AuthOrchestrationReadScope, [WS_METHODS.serverRefreshProviders]: AuthOrchestrationReadScope, [WS_METHODS.serverUpdateProvider]: AuthProvidersManageScope, diff --git a/apps/server/src/observability/RpcInstrumentation.ts b/apps/server/src/observability/RpcInstrumentation.ts index f02f32d67e764..a36d478b7ada0 100644 --- a/apps/server/src/observability/RpcInstrumentation.ts +++ b/apps/server/src/observability/RpcInstrumentation.ts @@ -38,6 +38,7 @@ const RPC_AGGREGATES = { [WS_METHODS.projectsMutate]: "orchestration", [WS_METHODS.serverProbe]: "server", [WS_METHODS.serverGetConfig]: "server", + [WS_METHODS.providerUsageLimitSourceServingAccount]: "provider", [WS_METHODS.serverGetNpmRelease]: "server", [WS_METHODS.serverRefreshProviders]: "server", [WS_METHODS.serverUpdateProvider]: "server", diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts index eba449c9a0fb3..dceba4aeb9c2d 100644 --- a/apps/server/src/usage/UsageLimitSources.ts +++ b/apps/server/src/usage/UsageLimitSources.ts @@ -2,6 +2,7 @@ import { DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL, UsageLimitSourceError, type UsageLimitSourceConsumeResetCreditInput, + type UsageLimitSourceServingAccountInput, type ProviderConsumeResetCreditResult, type ProviderDriverKind, type ServerSettings, @@ -40,9 +41,25 @@ export class UsageLimitSources extends Context.Service< readonly consumeResetCredit: ( input: UsageLimitSourceConsumeResetCreditInput, ) => Effect.Effect; + readonly servingAccount: ( + input: UsageLimitSourceServingAccountInput, + ) => Effect.Effect; } >()("t3/usage/UsageLimitSources") {} +// Hubs route sessions under their own provider names. +const SESSION_ROUTED_PROVIDERS: Partial> = { + claudeAgent: "claude", + codex: "codex", +}; + +function hubEndpoint(value: string) { + const url = URL.parse(value); + return url && /^https?:$/.test(url.protocol) + ? `${url.origin}${url.pathname.replace(/(\/management\.html)?\/*$/i, "")}` + : undefined; +} + function sourceLabel(id: string, config: UsageLimitSourceConfig): string { if (config.label) return config.label; const fallback = id.trim() || "Usage limit source"; @@ -119,17 +136,12 @@ export const make = Effect.gen(function* () { config: UsageLimitSourceConfig, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); - const url = URL.parse(config.url); + const endpoint = hubEndpoint(config.url); const base = { id, kind: config.kind, label: sourceLabel(id, config), - ...(url && /^https?:$/.test(url.protocol) - ? { - origin: url.origin, - endpoint: `${url.origin}${url.pathname.replace(/(\/management\.html)?\/*$/i, "")}`, - } - : {}), + ...(endpoint ? { origin: new URL(endpoint).origin, endpoint } : {}), checkedAt, } as const; const managementKey = yield* readManagementKey(config); @@ -201,6 +213,21 @@ export const make = Effect.gen(function* () { return result; }, refreshLock.withPermits(1)); + // Any miss, such as a hub without session routing, answers null so the card shows the pool. + const servingAccount = Effect.fn("UsageLimitSources.servingAccount")( + function* (input: UsageLimitSourceServingAccountInput) { + const provider = SESSION_ROUTED_PROVIDERS[input.driver]; + const settings = yield* settingsService.getSettings; + const config = settings.usageLimitSources[input.sourceId]; + if (!provider || !config?.enabled || hubEndpoint(config.url) !== input.endpoint) return null; + const managementKey = yield* readManagementKey(config); + if (managementKey.length === 0) return null; + return yield* api.servingAccount({ url: config.url, managementKey }, provider, input.session); + }, + Effect.timeout("5 seconds"), + Effect.orElseSucceed(() => null), + ); + yield* settingsService.streamChanges.pipe( Stream.map((settings) => settings.usageLimitSources), Stream.changes, @@ -233,6 +260,7 @@ export const make = Effect.gen(function* () { return { current: Ref.get(stateRef), consumeResetCredit, + servingAccount, refresh, get streamChanges() { return Stream.unwrap( diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 56983c0684685..1231cab363d13 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -73,6 +73,15 @@ function fixture( ? decodeRequest(new TextDecoder().decode(request.body.body)) : undefined; requests.push({ path, ...(body ? { body } : {}) }); + if (path.endsWith("/routing/session-affinity")) { + const query = new URL(request.url).searchParams; + return HttpClientResponse.fromWeb( + request, + query.get("provider") === "claude" && query.get("session") === "session-a" + ? Response.json({ provider: "claude", id: "first.json", auth_index: "a" }) + : Response.json({ error: "session is not bound to an account" }, { status: 404 }), + ); + } if (path.endsWith("/auth-files/models")) return HttpClientResponse.fromWeb( request, @@ -741,6 +750,15 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect("names the account a hub with session routing serves a session from", () => + Effect.gen(function* () { + const api = yield* fixture().api; + expect(yield* api.servingAccount(config, "claude", "session-a")).toBe("first.json"); + const unbound = yield* api.servingAccount(config, "claude", "other").pipe(Effect.result); + expect(unbound._tag).toBe("Failure"); + }), + ); + it.effect("rejects unknown accounts without forwarding a redemption", () => Effect.gen(function* () { const test = fixture(); diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index af80091c3eb41..93ac321f85a61 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -1,5 +1,6 @@ import { ProviderDriverKind, + TrimmedNonEmptyString, UsageLimitSourceError, type ProviderConsumeResetCreditResult, type UsageLimitSourceAccount, @@ -122,6 +123,9 @@ const CreditList = Schema.Struct({ const decodeAuthFiles = Schema.decodeUnknownEffect(AuthFiles); const decodeAuthFileModels = Schema.decodeUnknownEffect(AuthFileModels); +const decodeSessionAccount = Schema.decodeUnknownEffect( + Schema.Struct({ id: TrimmedNonEmptyString }), +); const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); @@ -604,5 +608,15 @@ export const makeCliproxyApi = Effect.gen(function* () { ), ); }); - return { readAccounts, consume }; + // Hubs without session routing reject this route; the caller then shows the pool. + const servingAccount = Effect.fn("CliproxyApi.servingAccount")(function* ( + hub: CliproxyHub, + provider: "claude" | "codex", + session: string, + ) { + const query = new URLSearchParams({ provider, session }); + const response = yield* management(hub, `routing/session-affinity?${query}`); + return (yield* decodeSessionAccount(response)).id; + }); + return { readAccounts, consume, servingAccount }; }); diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index d96b91f30d62a..16ba2e11d7cf2 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -2647,6 +2647,8 @@ export const layerWsRpc = ( ), ), ), + [WS_METHODS.providerUsageLimitSourceServingAccount]: (input) => + usageLimitSources.servingAccount(input), [WS_METHODS.serverUpdateProvider]: (input) => providerMaintenanceRunner.updateProvider(input), [WS_METHODS.providerConsumeResetCredit]: diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 77c47d668daf4..26146cdc405f7 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -4099,6 +4099,11 @@ export default function ChatView(props: ChatViewProps) { onUpdateProjectScript={updateProjectScriptStable} onDeleteProjectScript={deleteProjectScriptStable} usage={{ + environmentId: activeThread.environmentId, + nativeSessionId: + activeProviderThread?.providerInstanceId === activeProviderInstanceId + ? (activeProviderThread.nativeThreadRef?.nativeId ?? null) + : null, providerInstanceId: activeProviderInstanceId, model: activeUsageModel, providers: providerStatuses, diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index 5a024cf4e558b..9a800a6f22a8b 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -91,6 +91,7 @@ function shown( own: ServerProviderUsageLimits, sources: readonly UsageLimitSourceSnapshot[], model: string | null, + servingAccount: string | null = null, ) { const resolved = resolveThreadDetailsLimits( instanceId, @@ -98,6 +99,7 @@ function shown( [claudeInstance(own)], sources, now, + servingAccount === null ? null : { sourceId: "Hub", accountId: servingAccount }, ); if (resolved?.kind !== "pool") return resolved; return { @@ -144,4 +146,19 @@ describe("resolveThreadDetailsLimits", () => { rows: [{ label: "Session", left: 70, accounts: 1, from: { hubs: ["Hub"], signedIn: false } }], }); }); + + it("shows the account the hub serves the thread from, else the pool", () => { + const sources = [ + hub("Hub", [ + { id: "a.json", limits: limitsAt(10) }, + { id: "b.json", models: [], limits: limitsAt(50) }, + ]), + ]; + const pool = shown(unsupported, sources, null); + expect(shown(unsupported, sources, null, "b.json")).toMatchObject({ + rows: [{ left: 50, accounts: 1 }], + }); + expect(pool).toMatchObject({ rows: [{ left: 90, accounts: 1 }] }); + expect(shown(unsupported, sources, null, "gone.json")).toEqual(pool); + }); }); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index d3ccf8ed3a865..1bafbbfdb042c 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -30,6 +30,7 @@ export function resolveThreadDetailsLimits( providers: readonly ServerProvider[], sources: UsageLimitSourceSnapshots, now: number, + serving: { readonly sourceId: string; readonly accountId: string } | null = null, ): ThreadDetailsLimits | null { const provider = providers.find((entry) => entry.instanceId === providerInstanceId); if (!provider) return null; @@ -43,7 +44,17 @@ export function resolveThreadDetailsLimits( 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 }; + // The account the hub names as serving this thread, even if its cached model list is stale. + const servingHubs = hubs.map((hub) => ({ + ...hub, + accounts: hub.accounts.flatMap(({ models: _, ...account }) => + hub.id === serving?.sourceId && account.id === serving.accountId ? [account] : [], + ), + })); + return ( + hubPool(provider.driver, model, providers, servingHubs, now) ?? + hubPool(provider.driver, model, providers, hubs, now) ?? { kind: "instance", limits: own } + ); } export function nextPoolReset(window: LimitPoolWindow, now: number) { diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index d1975ef801364..e269800b0cf93 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -1,11 +1,14 @@ import type { + EnvironmentId, ProviderInstanceId, ServerProvider, ServerProviderUsageLimits, ServerProviderUsageWindow, UsageLimitSourceSnapshots, } from "@t3tools/contracts"; +import { selectEnvironmentQueryData } from "@t3tools/client-runtime/state/runtime"; import { + instanceHubs, type LimitPace, type LimitPoolWindow, paceOf, @@ -18,6 +21,8 @@ import { msg, useGT, useLocale, useMessages } from "gt-react"; import type { ReactNode } from "react"; import { usePrimarySettings } from "../../hooks/useSettings"; +import { serverEnvironment } from "../../state/server"; +import { useEnvironmentQuerySelector } from "../../state/query"; import { useNowMinute } from "../../hooks/useNowMinute"; import type { ContextWindowSnapshot } from "@t3tools/client-runtime/state/context-window"; import { formatContextWindowTokens } from "../../lib/contextWindow"; @@ -35,6 +40,9 @@ const PACE_LABEL: Record = { }; export interface ThreadDetailsUsageInput { + readonly environmentId: EnvironmentId; + /** The provider's own session id, which a hub with session routing maps to an account. */ + readonly nativeSessionId: string | null; readonly providerInstanceId: ProviderInstanceId | null; readonly model: string | null; readonly providers: readonly ServerProvider[]; @@ -52,6 +60,8 @@ function limitTone(left: number): UsageTone { /** The selected provider's limit windows. Each row reads as room left and opens the usage page. */ export function ThreadDetailsUsage({ + environmentId, + nativeSessionId, providerInstanceId, model, providers, @@ -60,7 +70,32 @@ export function ThreadDetailsUsage({ const gt = useGT(); const nowMinute = useNowMinute(); const now = Date.parse(`${nowMinute}:00Z`); - const resolved = resolveThreadDetailsLimits(providerInstanceId, model, providers, sources, now); + const provider = providers.find((entry) => entry.instanceId === providerInstanceId); + // Endpoints carry only an origin, so hubs that share one cannot say which serves the thread. + const hubs = provider ? instanceHubs(provider, sources) : []; + const hub = hubs.length === 1 ? hubs[0] : undefined; + const servingAccount = useEnvironmentQuerySelector( + provider && hub?.endpoint && nativeSessionId + ? serverEnvironment.usageLimitSourceServingAccount({ + environmentId, + input: { + sourceId: hub.id, + endpoint: hub.endpoint, + driver: provider.driver, + session: nativeSessionId, + }, + }) + : null, + selectEnvironmentQueryData, + ); + const resolved = resolveThreadDetailsLimits( + providerInstanceId, + model, + providers, + sources, + now, + hub && servingAccount ? { sourceId: hub.id, accountId: servingAccount } : null, + ); if (!resolved) return null; return ( diff --git a/docs/user/usage.md b/docs/user/usage.md index 43f4c94a19328..e1ec1f2782872 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -190,8 +190,9 @@ account and choose **Use reset** to redeem one. No hub plugin is required. This connection supplies usage information; configure 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. +`http://127.0.0.1:8317` for both. The thread details card shows the account the hub serves the thread +from when the hub reports it, and the hub's pooled accounts otherwise. Remove the hub from the same +settings section when you no longer need it. ## Subscription usage widget diff --git a/packages/client-runtime/src/state/server.ts b/packages/client-runtime/src/state/server.ts index 3179f0c149e4b..813dbba471b31 100644 --- a/packages/client-runtime/src/state/server.ts +++ b/packages/client-runtime/src/state/server.ts @@ -1590,6 +1590,13 @@ export function createServerEnvironmentAtoms( refreshIntervalMs: 5_000, idleTtlMs: 0, }), + // A hub can move a session to another account mid-turn, so re-read it on the hub's cadence. + usageLimitSourceServingAccount: createEnvironmentRpcQueryAtomFamily(runtime, { + label: "environment-data:server:usage-limit-source-serving-account", + tag: WS_METHODS.providerUsageLimitSourceServingAccount, + staleTimeMs: 30_000, + refreshIntervalMs: 60_000, + }), scheduledTaskWebhookDeliveries: createEnvironmentRpcQueryAtomFamily(runtime, { label: "environment-data:server:scheduled-task:webhook-deliveries", tag: WS_METHODS.scheduledTasksListWebhookDeliveries, diff --git a/packages/contracts/src/providerUsageLimits.ts b/packages/contracts/src/providerUsageLimits.ts index 4b10f3745d392..0adca7cee3f1b 100644 --- a/packages/contracts/src/providerUsageLimits.ts +++ b/packages/contracts/src/providerUsageLimits.ts @@ -101,6 +101,16 @@ export type UsageLimitSourceSnapshot = typeof UsageLimitSourceSnapshot.Type; export const UsageLimitSourceSnapshots = ForwardCompatibleArray(UsageLimitSourceSnapshot); export type UsageLimitSourceSnapshots = typeof UsageLimitSourceSnapshots.Type; +/** A native session id, which a hub with session routing maps to the account serving it. */ +export const UsageLimitSourceServingAccountInput = Schema.Struct({ + sourceId: UsageLimitSourceId, + /** The hub's endpoint as the client saw it, so a hub replaced under the same id answers nothing. */ + endpoint: TrimmedNonEmptyString, + driver: ProviderDriverKind, + session: TrimmedNonEmptyString, +}); +export type UsageLimitSourceServingAccountInput = typeof UsageLimitSourceServingAccountInput.Type; + export const UsageLimitSourceConsumeResetCreditInput = Schema.Struct({ sourceId: UsageLimitSourceId, accountId: TrimmedNonEmptyString, diff --git a/packages/contracts/src/rpc.ts b/packages/contracts/src/rpc.ts index b0892de7b13d9..9b206feb90478 100644 --- a/packages/contracts/src/rpc.ts +++ b/packages/contracts/src/rpc.ts @@ -407,6 +407,7 @@ import { UsageLimitSourceError, ProviderConsumeResetCreditInput, ProviderConsumeResetCreditResult, + UsageLimitSourceServingAccountInput, } from "./providerUsageLimits.ts"; import { UsagePricing, UsageReadError, UsageSummary, UsageSummaryInput } from "./usage.ts"; import { @@ -503,6 +504,7 @@ export const WS_METHODS = { providerMcpSignInComplete: "provider.mcpSignIn.complete", providerAuthStart: "provider.auth.start", providerConsumeResetCredit: "provider.consumeResetCredit", + providerUsageLimitSourceServingAccount: "provider.usageLimitSourceServingAccount", providerAuthComplete: "provider.auth.complete", chatGptReconnectProfile: "provider.chatgpt.reconnect-profile", chatGptImportProfile: "provider.chatgpt.import-profile", @@ -788,6 +790,15 @@ const WsProviderConsumeResetCreditRpc = Rpc.make(WS_METHODS.providerConsumeReset error: Schema.Union([ProviderSetupError, UsageLimitSourceError, EnvironmentAuthorizationError]), }); +const WsProviderUsageLimitSourceServingAccountRpc = Rpc.make( + WS_METHODS.providerUsageLimitSourceServingAccount, + { + payload: UsageLimitSourceServingAccountInput, + success: Schema.NullOr(TrimmedNonEmptyString), + error: EnvironmentAuthorizationError, + }, +); + const WsProviderAuthStartRpc = Rpc.make(WS_METHODS.providerAuthStart, { payload: ProviderAuthStartInput, success: ProviderAuthState, @@ -2391,6 +2402,7 @@ export const WsRpcGroup = RpcGroup.make( WsServerRefreshProvidersRpc, WsServerUpdateProviderRpc, WsProviderConsumeResetCreditRpc, + WsProviderUsageLimitSourceServingAccountRpc, WsProviderAuthStartRpc, WsProviderAuthCompleteRpc, WsChatGptReconnectProfileRpc,