feat(usage): thread details card shows the hub account each thread was routed to #29978
ClosedEhsanulHaqueSiam(GitHub profile, opens in a new tab)wants to merge 29 commits into
main fromrefs/pull/29978/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/chat/ThreadDetailsUsage.logic.ts lines 70 to 75OutdatedResolvedby EhsanulHaqueSiam(GitHub profile, opens in a new tab)@@ -0,0 +55,21 @@55+interface HubAccount {56+ readonly source: UsageLimitSourceSnapshot;57+ readonly account: UsageLimitSourceAccount;58+}59+60+/**61+ * The hub account a hub last routed this provider session to, when it shares that routing.62+ * Only the driver must match: the hub served the session with this account, whatever login the63+ * instance itself is signed in to.64+ */65+function routedHubAccount(66+ provider: ServerProvider,67+ sources: UsageLimitSourceSnapshots,68+ providerSessionId: string,69+): HubAccount | undefined {70+ return sources71+ .flatMap((source) => source.accounts.map((account) => ({ source, account })))72+ .find(73+ ({ account }) =>74+ account.driver === provider.driver && account.sessionIds?.includes(providerSessionId),75+ );apps/server/src/usage/UsageLimitSources.ts line 111OutdatedResolvedby EhsanulHaqueSiam(GitHub profile, opens in a new tab)@@ -75,5 +105,7 @@75105 });7610677107 const publish = Effect.fn("UsageLimitSources.publish")(function* (78- next: ReadonlyArray<UsageLimitSourceSnapshot>,108+ reads?: ReadonlyArray<UsageLimitSourceSnapshot>,79109 ) {110+ if (reads) yield* Ref.set(readsRef, reads);111+ const next = withSessionIds(yield* Ref.get(readsRef), yield* Ref.get(routesRef));apps/server/src/usage/cliproxyApi.ts line 392OutdatedResolvedby EhsanulHaqueSiam(GitHub profile, opens in a new tab)@@ -365,3 +374,9 @@365374 );366375 });367376377+ // Pops the hub's usage queue: records read here are gone for every other reader.378+ const readSessionAccounts = Effect.fn("CliproxyApi.readSessionAccounts")(function* (379+ config: UsageLimitSourceConfig,380+ ) {381+ // Records without a session or account, or not JSON objects, carry nothing to match.382+ const records = (yield* decodeUsageQueue(
- apps/server/src/usage/UsageLimitSources.ts line 140OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -74,6 +104,17 @@74104 return { ...base, accounts: accounts.success };75105 });76106107+ // Its own lock, so a route read never waits on a slow quota refresh yet never publishes stale reads.108+ const publishLock = yield* Semaphore.make(1);77109 const publish = Effect.fn("UsageLimitSources.publish")(function* (78- next: ReadonlyArray<UsageLimitSourceSnapshot>,110+ reads: ReadonlyArray<UsageLimitSourceSnapshot> | undefined,79111 ) {112+ if (reads) yield* Ref.set(readsRef, reads);113+ // Current settings, so no publish names routes for a hub switched out of Thread accounts,114+ // even when a queue read started before the switch lands after it.115+ const settings = yield* settingsService.getSettings.pipe(116+ Effect.orElseSucceed((): ServerSettings | null => null),117+ );118+ const routes = new Map(119+ [...(yield* Ref.get(routesRef))].filter(120+ ([id]) => settings?.usageLimitSources[id]?.threadAccounts,
tanstack-source added labelsfeature
- @@ -159,0 +288,21 @@288+ Effect.provideService(HttpClient.HttpClient, http),289+ );290+ yield* sources.refresh;291+ // The 30-second queue read starts and waits on the hub.292+ yield* TestClock.adjust("30 seconds");293+294+ const settings = yield* ServerSettings.ServerSettingsService;295+ yield* settings.updateSettings({296+ usageLimitSources: {297+ [UsageLimitSourceId.make("routed")]: {298+ kind: "cliproxy",299+ url: "https://routed.test",300+ managementKey: "key",301+ enabled: true,302+ threadAccounts: false,303+ },304+ },305+ });306+ yield* sources.refresh;307+ yield* Deferred.succeed(release, undefined);308+ yield* TestClock.adjust("1 second");
github-actions[bot](GitHub profile, opens in a new tab) added labelsneeds-rebase
- @@ -484,3 +492,14 @@484492 return { id: account.id, driver: provider.driver, ...reading };485493 });486494495+ // Pops the hub's usage queue: records read here are gone for every other reader.496+ const readSessionAccounts = Effect.fn("CliproxyApi.readSessionAccounts")(function* (497+ config: CliproxyHub,498+ ) {499+ // Accounts first: a failed lookup after the pop would lose those records for good.500+ const accountIds = new Map(501+ (yield* authFiles(config)).map((account) => [account.auth_index, account.id]),502+ );503+ // Records without a session or account, or not JSON objects, carry nothing to match.504+ const records = (yield* decodeUsageQueue(505+ yield* management(config, "usage-queue?count=1000"),@@ -409,0 +545,21 @@545+ return Deferred.succeed(started, undefined).pipe(546+ Effect.andThen(Deferred.await(release)),547+ Effect.tap(() =>548+ Effect.sync(() => {549+ returned = true;550+ }),551+ ),552+ Effect.as(553+ HttpClientResponse.fromWeb(554+ request,555+ Response.json([{ session_id: "thread-session", auth_index: "a" }]),556+ ),557+ ),558+ );559+ }560+ return Effect.succeed(561+ HttpClientResponse.fromWeb(562+ request,563+ url.pathname.endsWith("/auth-files")564+ ? Response.json({565+ files: [{ id: "claude-a.json", auth_index: "a", provider: "claude" }],@@ -200,0 +283,21 @@283+ ([id, config]) =>284+ readManagementKey(config).pipe(285+ Effect.flatMap((managementKey) =>286+ managementKey.length > 0287+ ? api.readSessionAccounts({ url: config.url, managementKey })288+ : Effect.succeed([]),289+ ),290+ Effect.orElseSucceed(() => []),291+ Effect.map((routes) => [id as UsageLimitSourceId, routes] as const),292+ ),293+ { concurrency: 4 },294+ );295+ yield* Ref.update(routesRef, (previous) => {296+ const next = new Map<UsageLimitSourceId, ReadonlyMap<string, SessionRoute>>();297+ for (const [id, routes] of reads) {298+ const routed = new Map(previous.get(id));299+ const source = snapshots.find((source) => source.id === id);300+ for (const { sessionId, accountId } of routes) {301+ const account = source?.accounts.find((account) => account.id === accountId);302+ if (!source || !account) continue;303+ routed.delete(sessionId);@@ -74,0 +72,21 @@72+ !readOnly ? (73+ <RemoveUsageProviderButton74+ label={label}75+ onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })}76+ />77+ ) : null78+ }79+ />80+ <SettingsRow81+ title={gt("Thread accounts")}82+ description={gt(83+ "Show the limits of the hub account each thread uses. T3 reads the hub's usage queue, which removes those records for anything else reading it. The hub needs usage statistics and session affinity on.",84+ )}85+ control={86+ <Switch87+ aria-label={gt("Thread accounts for {label}", { label })}88+ checked={source.threadAccounts === true}89+ disabled={readOnly}90+ onCheckedChange={(threadAccounts) =>91+ updateSettings({92+ usageLimitSources: { [id]: { ...source, threadAccounts } },@@ -187,3 +187,12 @@187187 accounts for other providers are not read. Codex accounts show banked reset credits; select an188188 account and choose **Use reset** to redeem one. No hub plugin is required.189189190+When an instance cannot read its own limits and your connected hubs have exactly one account for191+that provider between them, the thread details card shows that account's limits, and hovering a row192+shows which hub they come from. A hub account signed in with a different email or workspace than the193+instance is skipped. With several hub accounts, or while a hub cannot be read, the card keeps its notice, so194+check **Usage → Limits**.195+196+To show the right account when a hub has several, turn on **Thread accounts** for that hub. T3 then197+reads which account served each thread from the hub's usage queue and shows that account's limits,198+even when the CLI itself is signed in to another account, as Codex can be while it sends through the hub. The
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
EhsanulHaqueSiam(GitHub profile, opens in a new tab) reopened this
Sign in to join this conversation.

















macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed