fix(web): thread details card shows a proxied instance's limits from its usage hub #27740
ClosedPedroNavHur(GitHub profile, opens in a new tab)wants to merge 19 commits into
main fromrefs/pull/27740/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:M
- apps/web/src/components/chat/ThreadDetailsUsage.logic.ts lines 40 to 42OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +22,21 @@22+ providerInstanceId: ProviderInstanceId | null,23+ providers: readonly ServerProvider[],24+ sources: UsageLimitSourceSnapshots,25+ now: number,26+): ThreadDetailsLimits | null {27+ if (providerInstanceId === null) return null;28+ const provider = providers.find((entry) => entry.instanceId === providerInstanceId);29+ const report = collectProviderUsageLimits(providerInstanceId, providers, sources, now);30+ // Only this instance's data: the report keeps one instance per login, and any other entry may be31+ // a different login or workspace, so fall back to the instance's own snapshot.32+ const own =33+ report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ??34+ provider?.usageLimits;35+ if (own && limitsNotice(own) === null) return { limits: own, sourceLabel: null };36+37+ const hubAccounts = provider38+ ? sources.flatMap((source) =>39+ source.accounts40+ .filter(41+ (account) =>42+ account.driver === provider.driver && limitsNotice(account.usageLimits) === null,
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/chat/ThreadDetailsUsage.logic.ts lines 65 to 89OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +55,21 @@55+ * since the hub may route the thread through them. An account is a login (driver + email, else56+ * credential) plus workspace: one login from two hubs is one account, its freshest snapshot wins.57+ */58+function onlyHubAccount(59+ provider: ServerProvider,60+ sources: UsageLimitSourceSnapshots,61+): HubAccount | null {62+ // A hub that failed to read may hold accounts for this driver too.63+ if (sources.some((source) => source.error !== undefined)) return null;64+ const candidates = sources.flatMap((source) =>65+ source.accounts66+ .filter((account) => canServeInstance(provider, account))67+ .map((account) => ({ source, account })),68+ );69+ const resolveKey = accountKeyResolver(candidates.map(({ account }) => account));70+ const freshestByAccount = new Map<string, HubAccount>();71+ for (const candidate of candidates) {72+ const key = resolveKey(candidate.account) ?? `${candidate.source.id}:${candidate.account.id}`;73+ const known = freshestByAccount.get(key);74+ if (!known || checkedAtMs(candidate) > checkedAtMs(known)) {75+ freshestByAccount.set(key, candidate);apps/web/src/components/chat/ThreadDetailsUsage.logic.ts line 92OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +72,21 @@72+ const key = resolveKey(candidate.account) ?? `${candidate.source.id}:${candidate.account.id}`;73+ const known = freshestByAccount.get(key);74+ if (!known || checkedAtMs(candidate) > checkedAtMs(known)) {75+ freshestByAccount.set(key, candidate);76+ }77+ }78+ const [only, ...rest] = freshestByAccount.values();79+ if (!only || rest.length > 0) return null;80+ return limitsNotice(only.account.usageLimits) === null ? only : null;81+}82+83+const checkedAtMs = ({ account }: HubAccount) => Date.parse(account.usageLimits.checkedAt);84+85+const normalizeEmail = (email: string | undefined) => email?.trim().toLowerCase() || undefined;86+87+/** A hub account for the instance's driver, unless both sides name different logins. */88+function canServeInstance(provider: ServerProvider, account: UsageLimitSourceAccount): boolean {89+ if (account.driver !== provider.driver) return false;90+ const instanceEmail = normalizeEmail(provider.auth.email);91+ const hubEmail = normalizeEmail(account.email);92+ return !instanceEmail || !hubEmail || instanceEmail === hubEmail;
- apps/web/src/components/chat/ThreadDetailsUsage.logic.ts line 71OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +51,21 @@51+}52+53+/**54+ * The one hub account serving a driver, if its limits are usable. Unreadable accounts still count,55+ * since the hub may route the thread through them. An account is a login (driver + email, else56+ * credential) plus workspace: one login from two hubs is one account, its freshest snapshot wins.57+ */58+function onlyHubAccount(59+ provider: ServerProvider,60+ providers: readonly ServerProvider[],61+ sources: UsageLimitSourceSnapshots,62+): HubAccount | null {63+ // A hub that failed to read may hold accounts for this driver too.64+ if (sources.some((source) => source.error !== undefined)) return null;65+ const candidates = sources.flatMap((source) =>66+ source.accounts67+ .filter((account) => canServeInstance(provider, account))68+ .map((account) => ({ source, account })),69+ );70+ // Learn workspaces from every report, so an account without one never merges into one of several.71+ const resolveKey = environmentAccountKeyResolver(providers, sources);
juliusmarminge added labelsbugfix
juliusmarminge added labelspr:needs-julius
tanstack-source added labelsbug
juliusmarminge added labelstakeover:in-progress
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
github-actions[bot](GitHub profile, opens in a new tab) added labelsneeds-rebase
github-actions[bot](GitHub profile, opens in a new tab) removed labelsneeds-rebase
Sign in to join this conversation.
After: 
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
Walkthrough
Thread details now use limits from the selected provider instance or, when those limits have a notice, from one eligible usage-hub account. Limit rows identify the source in a tooltip when available.
Changes: Thread usage limits
apps/web/src/components/chat/ThreadDetailsUsage.logic.ts,apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts,packages/shared/src/usageLimits.tsenvironmentAccountKeyResolveris exported and used bycollectProviderUsageLimits.apps/web/src/components/chat/ThreadDetailsUsage.tsx,docs/user/usage.mdEstimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix · Severity of issue fixed: Medium
Sequence Diagram(s)#
Possibly related issues
Suggested reviewers:
t3dotgg,maria-rcks,macroscopeapp,juliusmarmingeMerge Risk:
Low · previous review
c0df4The thread card can show another workspace’s limits. The change is mergeable with awareness of this display risk.
Pre-merge checks:
3 · 
1
Full details: Description check
Explanation
The description includes the required Problem, Change, Scope and approval, and Verification sections. It provides reproduction details, implementation scope, test results, screenshots, and limitations. However, the Scope and approval section explicitly states that maintainer approval is not yet available, although the template requires explicit approval or a valid exemption.
Resolution
Obtain explicit maintainer approval in the linked discussion and add the approval comment to the Scope and approval section. If the change qualifies as an exemption, explain why it is a focused obvious fix or established-capability configuration and document that rationale instead.
Finishing Touches
Fix failing CI checks
🧪 Generate unit tests (beta)
791020df-17d7-4406-a4d2-3d32a4af03b4Reviewing files that changed from the base of the PR and between 6c9850faec2003c59e574b16205f90c67909d86f and c0df4f4ee46128c34976a228f7e53022e8a1702a.
apps/web/src/components/chat/ThreadDetailsUsage.logic.test.tsapps/web/src/components/chat/ThreadDetailsUsage.logic.tsdocs/user/usage.mdpackages/shared/src/usageLimits.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.