From 9dae06fd9fb807c7c57f1ca4b841227c2e267fb0 Mon Sep 17 00:00:00 2001 From: K1erans <178052660+K1erans@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:18:11 +0000 Subject: [PATCH 1/4] test(usage): add failing tests for cross-environment thread details limits Co-Authored-By: Claude Sonnet 5.5 --- .../chat/ThreadDetailsUsage.browser.test.tsx | 88 +++++++++++++++ .../chat/ThreadDetailsUsage.logic.test.ts | 57 ++++++++++ packages/shared/src/usageLimits.test.ts | 101 ++++++++++++++++++ 3 files changed, 246 insertions(+) create mode 100644 apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx new file mode 100644 index 0000000000000..4bcab8195175b --- /dev/null +++ b/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx @@ -0,0 +1,88 @@ +import { + ProviderDriverKind, + ProviderInstanceId, + type ServerProvider, + type ServerProviderUsageLimits, +} from "@t3tools/contracts"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { render } from "../../../test/render"; + +const peers = vi.hoisted(() => ({ providers: [] as unknown[] })); + +vi.mock(import("~/state/presentation"), async (original) => { + const { Atom } = await import("effect/reactivity"); + const actual = await original(); + return { + ...actual, + environmentPresentations: { + ...actual.environmentPresentations, + presentationsWithProvidersAtom: Atom.make( + () => + new Map([ + [ + "linked", + { + entry: { target: { label: "Linked" } }, + serverConfig: { providers: peers.providers }, + }, + ], + ]), + ) as never, + }, + }; +}); + +import { ThreadDetailsUsage } from "./ThreadDetailsUsage"; + +const checkedAt = "2026-10-30T11:59:00.000Z"; +const instanceId = ProviderInstanceId.make("claudeAgent"); + +function limits(usedPercent: number, at: string): ServerProviderUsageLimits { + return { + checkedAt: at, + credentialFingerprint: "fp-1", + windows: [ + { + id: "five_hour", + kind: "session", + label: "Session", + usedPercent, + windowDurationMins: 300, + resetsAt: "2026-10-30T13:17:00.000Z", + }, + ], + }; +} + +function claude(usageLimits: ServerProviderUsageLimits): ServerProvider { + return { + instanceId, + driver: ProviderDriverKind.make("claudeAgent"), + enabled: true, + installed: true, + version: null, + status: "ready", + auth: { status: "authenticated" }, + checkedAt, + models: [], + slashCommands: [], + skills: [], + usageLimits, + }; +} + +describe("ThreadDetailsUsage", () => { + it("shows a linked environment's fresher reading of the same account", async () => { + peers.providers = [claude(limits(25, "2026-10-30T11:58:00.000Z"))]; + await using screen = await render( + , + ); + await expect.element(screen.getByRole("link", { name: "Session: 75% left" })).toBeVisible(); + }); +}); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index 5a024cf4e558b..c6be94a1d6c64 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -145,3 +145,60 @@ describe("resolveThreadDetailsLimits", () => { }); }); }); + +describe("resolveThreadDetailsLimits across environments", () => { + const stale = "2026-10-30T11:00:00.000Z"; + const fresh = "2026-10-30T11:58:00.000Z"; + const identified = ( + usageLimits: ServerProviderUsageLimits, + auth: ServerProvider["auth"] = { status: "authenticated" }, + ): ServerProvider => ({ ...claudeInstance(usageLimits), auth }); + const reading = (usedPercent: number, at: string, credentialFingerprint?: string) => ({ + ...limitsAt(usedPercent), + checkedAt: at, + ...(credentialFingerprint ? { credentialFingerprint } : {}), + }); + const failed: ServerProviderUsageLimits = { + checkedAt, + windows: [], + unavailable: { reason: "probeFailed" }, + }; + + it("shows a linked environment's fresher reading of the same credential", () => { + const own = identified(reading(80, stale, "fp-1")); + const peer = identified(reading(25, fresh, "fp-1")); + expect(resolveThreadDetailsLimits(instanceId, null, [own], [], now, [peer])).toEqual({ + kind: "instance", + limits: peer.usageLimits, + }); + }); + + it("shows a linked environment's readable windows over a failed local probe", () => { + const auth = { status: "authenticated" as const, email: "dev@example.com" }; + const own = identified(failed, auth); + const peer = identified(reading(25, fresh), auth); + expect(resolveThreadDetailsLimits(instanceId, null, [own], [], now, [peer])).toEqual({ + kind: "instance", + limits: peer.usageLimits, + }); + }); + + it("keeps a hub-routed instance's own reading instead of a non-hub peer's", () => { + const own = identified(unsupported, { + status: "authenticated", + type: "customEndpoint", + label: "Custom API endpoint", + endpoint: "http://127.0.0.1:8317", + email: "dev@example.com", + }); + const peer = identified(reading(25, fresh), { + status: "authenticated", + email: "dev@example.com", + }); + const unreadable = hub("Hub", [{ id: "a.json", limits: failed }]); + expect(resolveThreadDetailsLimits(instanceId, null, [own], [unreadable], now, [peer])).toEqual({ + kind: "instance", + limits: unsupported, + }); + }); +}); diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index 0019c74e1d5cd..d3ecc9c96f7fb 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -13,6 +13,7 @@ import { type LimitAccount, displayLimitWindows, cursorUsageWindowDetails, + accountUsageLimits, collectProviderUsageLimits, sameUsageLimitCommandCoverage, withUsageLimitsCommands, @@ -2011,3 +2012,103 @@ describe("ChatGPT sharing presentation", () => { ).toBe(false); }); }); + +describe("accountUsageLimits", () => { + const older = "2026-09-03T11:00:00.000Z"; + const newer = "2026-09-03T11:30:00.000Z"; + const readings = (usedPercent: number, at: string, credentialFingerprint = "fp-1") => ({ + checkedAt: at, + credentialFingerprint, + windows: [{ ...window, usedPercent }], + }); + const failed = (at: string) => ({ + checkedAt: at, + windows: [], + unavailable: { reason: "probeFailed" as const }, + }); + const login = (overrides: Partial = {}) => + provider({ instanceId: ProviderInstanceId.make("login"), ...overrides }); + + it.each(["codex", "claudeAgent", "antigravity"])( + "takes a fresher %s reading that shares a credential fingerprint", + (driver) => { + const own = login({ + driver: ProviderDriverKind.make(driver), + usageLimits: readings(80, older), + }); + const peer = login({ driver: own.driver, usageLimits: readings(20, newer) }); + expect(accountUsageLimits(own, own.usageLimits!, [peer])).toBe(peer.usageLimits); + }, + ); + + it("takes a fresher reading that shares an email and workspace", () => { + const auth = { status: "authenticated" as const, email: "Dev@Example.com", workspaceId: "w1" }; + const own = login({ auth, usageLimits: readings(80, older, "fp-own") }); + const peer = login({ + auth: { ...auth, email: "dev@example.com" }, + usageLimits: readings(20, newer, "fp-peer"), + }); + expect(accountUsageLimits(own, own.usageLimits!, [peer])).toBe(peer.usageLimits); + }); + + it("prefers a readable peer over a failed local reading of the same account", () => { + const own = login({ usageLimits: { ...failed(newer), credentialFingerprint: "fp-1" } }); + const peer = login({ usageLimits: readings(20, older) }); + expect(accountUsageLimits(own, own.usageLimits!, [peer])).toBe(peer.usageLimits); + }); + + it.each([ + ["newer own reading", readings(20, newer), readings(80, older), {}], + ["tied own reading", readings(20, newer), readings(80, newer), {}], + [ + "other driver", + readings(80, older), + readings(20, newer), + { driver: ProviderDriverKind.make("claudeAgent") }, + ], + ["disabled peer", readings(80, older), readings(20, newer), { enabled: false }], + ["failed peer", readings(80, older), failed(newer), {}], + [ + "other workspace", + readings(80, older), + readings(20, newer), + { auth: { status: "authenticated" as const, email: "a@x.io", workspaceId: "w2" } }, + ], + [ + "other account", + readings(80, older), + readings(20, newer), + { auth: { status: "authenticated" as const, email: "b@x.io", workspaceId: "w1" } }, + ], + ])("keeps its own reading against a %s", (_name, ownLimits, peerLimits, peerOverrides) => { + const own = login({ + auth: { status: "authenticated", email: "a@x.io", workspaceId: "w1" }, + usageLimits: ownLimits, + }); + const peer = login({ + auth: { status: "authenticated", email: "a@x.io", workspaceId: "w1" }, + usageLimits: peerLimits, + ...peerOverrides, + }); + expect(accountUsageLimits(own, ownLimits, [peer])).toBe(ownLimits); + }); + + it("keeps an unsupported own reading", () => { + const unsupported = { + checkedAt: older, + credentialFingerprint: "fp-1", + windows: [], + unavailable: { reason: "unsupported" as const }, + }; + const own = login({ usageLimits: unsupported }); + const peer = login({ usageLimits: readings(20, newer) }); + expect(accountUsageLimits(own, unsupported, [peer])).toBe(unsupported); + }); + + it("keeps its own reading when the account has no email or credential fingerprint", () => { + const ownLimits = { checkedAt: older, windows: [{ ...window, usedPercent: 80 }] }; + const own = login({ usageLimits: ownLimits }); + const peer = login({ usageLimits: { checkedAt: newer, windows: [window] } }); + expect(accountUsageLimits(own, ownLimits, [peer])).toBe(ownLimits); + }); +}); From 28cd17d0354954846f7cb53bda903929d955d54c Mon Sep 17 00:00:00 2001 From: K1erans <178052660+K1erans@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:19:21 +0000 Subject: [PATCH 2/4] fix(web): show freshest cross-environment usage limits in thread details Co-Authored-By: Claude Sonnet 5.5 --- .../chat/ThreadDetailsUsage.logic.ts | 7 +++- .../components/chat/ThreadDetailsUsage.tsx | 15 ++++++- packages/shared/src/usageLimits.ts | 39 +++++++++++++++---- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts index d3ccf8ed3a865..f45dd77ab4572 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.ts @@ -6,6 +6,7 @@ import { type UsageLimitSourceSnapshots, } from "@t3tools/contracts"; import { + accountUsageLimits, antigravityWindowLimitsModel, collectLimitAccounts, collectLimitPools, @@ -30,6 +31,7 @@ export function resolveThreadDetailsLimits( providers: readonly ServerProvider[], sources: UsageLimitSourceSnapshots, now: number, + peers: Iterable = [], ): ThreadDetailsLimits | null { const provider = providers.find((entry) => entry.instanceId === providerInstanceId); if (!provider) return null; @@ -41,7 +43,10 @@ export function resolveThreadDetailsLimits( provider.usageLimits; if (!own) return null; const hubs = instanceHubs(provider, sources); - if (limitsNotice(own) === null || hubs.length === 0) return { kind: "instance", limits: own }; + // Linked environments can hold a fresher reading of the same account; a hub-routed instance never takes theirs. + if (limitsNotice(own) === null || hubs.length === 0) { + return { kind: "instance", limits: accountUsageLimits(provider, own, peers) }; + } // 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 }; } diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.tsx index d1975ef801364..6331fb45811e5 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.tsx @@ -13,12 +13,14 @@ import { remainingPercent, } from "@t3tools/shared/usageLimits"; import { formatDuration, limitsNotice } from "@t3tools/shared/usageLimitsCopy"; +import { useAtomValue } from "@effect/atom-react"; import { Link } from "@tanstack/react-router"; import { msg, useGT, useLocale, useMessages } from "gt-react"; import type { ReactNode } from "react"; import { usePrimarySettings } from "../../hooks/useSettings"; import { useNowMinute } from "../../hooks/useNowMinute"; +import { environmentPresentations } from "../../state/presentation"; import type { ContextWindowSnapshot } from "@t3tools/client-runtime/state/context-window"; import { formatContextWindowTokens } from "../../lib/contextWindow"; import { cn } from "../../lib/utils"; @@ -60,7 +62,18 @@ export function ThreadDetailsUsage({ const gt = useGT(); const nowMinute = useNowMinute(); const now = Date.parse(`${nowMinute}:00Z`); - const resolved = resolveThreadDetailsLimits(providerInstanceId, model, providers, sources, now); + const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom); + const peers = [...presentations.values()].flatMap( + (presentation) => presentation.serverConfig?.providers ?? [], + ); + const resolved = resolveThreadDetailsLimits( + providerInstanceId, + model, + providers, + sources, + now, + peers, + ); if (!resolved) return null; return ( diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 711f3637d0a9f..941d61b7d5c57 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -944,6 +944,37 @@ export function hubRoutes( account.models ? listed(account) : serving.has(account.driver) || account.driver === driver; } +// A readable reading beats an unreadable one; between equals the later check wins. +function fresherLimits(next: ServerProviderUsageLimits, previous: ServerProviderUsageLimits) { + const previousUsable = limitsNotice(previous) === null; + const usable = limitsNotice(next) === null; + return ( + (usable && !previousUsable) || + (usable === previousUsable && Date.parse(next.checkedAt) > Date.parse(previous.checkedAt)) + ); +} + +/** The freshest readable limits any enabled login of this provider's account reports across environments. */ +export function accountUsageLimits( + provider: ServerProvider, + own: ServerProviderUsageLimits, + peers: Iterable, +): ServerProviderUsageLimits { + if (!provider.enabled || own.unavailable?.reason === "unsupported") return own; + const enabledPeers = [...peers].filter((peer) => peer.enabled); + const resolveKey = accountKeyResolver([provider, ...enabledPeers].map(nativeReport)); + const key = resolveKey(nativeReport(provider)); + if (key === null) return own; + let best = own; + for (const peer of enabledPeers) { + if (peer.driver !== provider.driver || !peer.usageLimits) continue; + if (limitsNotice(peer.usageLimits) !== null) continue; + if (resolveKey(nativeReport(peer)) !== key) continue; + if (fresherLimits(peer.usageLimits, best)) best = peer.usageLimits; + } + return best; +} + export function collectProviderUsageLimits( instanceId: ProviderInstanceId, providers: readonly ServerProvider[], @@ -988,13 +1019,7 @@ export function collectProviderUsageLimits( const snapshot = { provider, limits: provider.usageLimits }; 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; - const providerWins = - !previous || - (usable && !previousUsable) || - (usable === previousUsable && - Date.parse(snapshot.limits.checkedAt) > Date.parse(previous.usage.limits.checkedAt)); + const providerWins = !previous || fresherLimits(snapshot.limits, previous.usage.limits); const previousCredit = previous?.credits; const credits = snapshot.limits.resetCredits && From b4cb3d2b2a67eba36c05818e56e42be71e382a04 Mon Sep 17 00:00:00 2001 From: K1erans <178052660+K1erans@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:29:58 +0000 Subject: [PATCH 3/4] fix(usage): guard hub-routed instances, filter unavailable peers, and isolate browser test --- .../chat/ThreadDetailsUsage.browser.test.tsx | 63 +++++++++++------- .../chat/ThreadDetailsUsage.logic.test.ts | 60 ++++++----------- packages/shared/src/usageLimits.test.ts | 66 +++++++++++++++---- packages/shared/src/usageLimits.ts | 13 ++-- 4 files changed, 121 insertions(+), 81 deletions(-) diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx index 4bcab8195175b..9a3e3f735c25e 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx +++ b/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx @@ -4,33 +4,32 @@ import { type ServerProvider, type ServerProviderUsageLimits, } from "@t3tools/contracts"; +import { RegistryContext } from "@effect/atom-react"; +import { type Atom, AtomRegistry } from "effect/reactivity"; import { describe, expect, it, vi } from "vite-plus/test"; import { render } from "../../../test/render"; -const peers = vi.hoisted(() => ({ providers: [] as unknown[] })); +type PresentationMap = ReadonlyMap< + string, + { readonly serverConfig: { readonly providers: readonly ServerProvider[] } | null } +>; -vi.mock(import("~/state/presentation"), async (original) => { +const presentationsAtom = vi.hoisted(() => ({ + current: null as Atom.Writable | null, +})); + +vi.mock(import("~/state/presentation"), async () => { const { Atom } = await import("effect/reactivity"); - const actual = await original(); + const atom = Atom.make(new Map()); + presentationsAtom.current = atom; return { - ...actual, environmentPresentations: { - ...actual.environmentPresentations, - presentationsWithProvidersAtom: Atom.make( - () => - new Map([ - [ - "linked", - { - entry: { target: { label: "Linked" } }, - serverConfig: { providers: peers.providers }, - }, - ], - ]), - ) as never, + presentationsWithProvidersAtom: atom as never, }, - }; + environmentSummaries: {} as never, + useEnvironmentPresentation: () => ({ isReady: true, presentation: null }), + } as never; }); import { ThreadDetailsUsage } from "./ThreadDetailsUsage"; @@ -72,16 +71,30 @@ function claude(usageLimits: ServerProviderUsageLimits): ServerProvider { }; } +function registryWithPeers(peers: readonly ServerProvider[]) { + const registry = AtomRegistry.make(); + registry.set( + presentationsAtom.current!, + new Map([ + ["connecting", { serverConfig: null }], + ["linked", { serverConfig: { providers: peers } }], + ]), + ); + return registry; +} + describe("ThreadDetailsUsage", () => { it("shows a linked environment's fresher reading of the same account", async () => { - peers.providers = [claude(limits(25, "2026-10-30T11:58:00.000Z"))]; + const registry = registryWithPeers([claude(limits(25, "2026-10-30T11:58:00.000Z"))]); await using screen = await render( - , + + + , ); await expect.element(screen.getByRole("link", { name: "Session: 75% left" })).toBeVisible(); }); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index c6be94a1d6c64..ea6aea5d480e5 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -147,58 +147,36 @@ describe("resolveThreadDetailsLimits", () => { }); describe("resolveThreadDetailsLimits across environments", () => { - const stale = "2026-10-30T11:00:00.000Z"; const fresh = "2026-10-30T11:58:00.000Z"; - const identified = ( - usageLimits: ServerProviderUsageLimits, - auth: ServerProvider["auth"] = { status: "authenticated" }, - ): ServerProvider => ({ ...claudeInstance(usageLimits), auth }); - const reading = (usedPercent: number, at: string, credentialFingerprint?: string) => ({ - ...limitsAt(usedPercent), - checkedAt: at, - ...(credentialFingerprint ? { credentialFingerprint } : {}), - }); const failed: ServerProviderUsageLimits = { checkedAt, windows: [], unavailable: { reason: "probeFailed" }, }; - it("shows a linked environment's fresher reading of the same credential", () => { - const own = identified(reading(80, stale, "fp-1")); - const peer = identified(reading(25, fresh, "fp-1")); - expect(resolveThreadDetailsLimits(instanceId, null, [own], [], now, [peer])).toEqual({ + it("keeps a hub-routed instance's failed reading instead of a non-hub peer's", () => { + const own: ServerProvider = { + ...claudeInstance(failed), + auth: { + status: "authenticated", + type: "customEndpoint", + label: "Custom API endpoint", + endpoint: "http://127.0.0.1:8317", + email: "dev@example.com", + }, + }; + const peer: ServerProvider = { + ...claudeInstance({ ...limitsAt(25), checkedAt: fresh }), + auth: { status: "authenticated", email: "dev@example.com" }, + }; + const unreadable = hub("Hub", [{ id: "a.json", limits: failed }]); + expect(resolveThreadDetailsLimits(instanceId, null, [own], [unreadable], now, [peer])).toEqual({ kind: "instance", - limits: peer.usageLimits, + limits: failed, }); - }); - - it("shows a linked environment's readable windows over a failed local probe", () => { - const auth = { status: "authenticated" as const, email: "dev@example.com" }; - const own = identified(failed, auth); - const peer = identified(reading(25, fresh), auth); expect(resolveThreadDetailsLimits(instanceId, null, [own], [], now, [peer])).toEqual({ kind: "instance", - limits: peer.usageLimits, - }); - }); - - it("keeps a hub-routed instance's own reading instead of a non-hub peer's", () => { - const own = identified(unsupported, { - status: "authenticated", - type: "customEndpoint", - label: "Custom API endpoint", - endpoint: "http://127.0.0.1:8317", - email: "dev@example.com", - }); - const peer = identified(reading(25, fresh), { - status: "authenticated", - email: "dev@example.com", - }); - const unreadable = hub("Hub", [{ id: "a.json", limits: failed }]); - expect(resolveThreadDetailsLimits(instanceId, null, [own], [unreadable], now, [peer])).toEqual({ - kind: "instance", - limits: unsupported, + limits: failed, }); }); }); diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index d3ecc9c96f7fb..7a37a483adedf 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -2029,17 +2029,11 @@ describe("accountUsageLimits", () => { const login = (overrides: Partial = {}) => provider({ instanceId: ProviderInstanceId.make("login"), ...overrides }); - it.each(["codex", "claudeAgent", "antigravity"])( - "takes a fresher %s reading that shares a credential fingerprint", - (driver) => { - const own = login({ - driver: ProviderDriverKind.make(driver), - usageLimits: readings(80, older), - }); - const peer = login({ driver: own.driver, usageLimits: readings(20, newer) }); - expect(accountUsageLimits(own, own.usageLimits!, [peer])).toBe(peer.usageLimits); - }, - ); + it("takes a fresher reading that shares a credential fingerprint", () => { + const own = login({ usageLimits: readings(80, older) }); + const peer = login({ usageLimits: readings(20, newer) }); + expect(accountUsageLimits(own, own.usageLimits!, [peer])).toBe(peer.usageLimits); + }); it("takes a fresher reading that shares an email and workspace", () => { const auth = { status: "authenticated" as const, email: "Dev@Example.com", workspaceId: "w1" }; @@ -2067,7 +2061,27 @@ describe("accountUsageLimits", () => { { driver: ProviderDriverKind.make("claudeAgent") }, ], ["disabled peer", readings(80, older), readings(20, newer), { enabled: false }], + ["uninstalled peer", readings(80, older), readings(20, newer), { installed: false }], + [ + "unavailable peer", + readings(80, older), + readings(20, newer), + { availability: "unavailable" as const }, + ], ["failed peer", readings(80, older), failed(newer), {}], + [ + "hub-routed peer", + readings(80, older), + readings(20, newer), + { + auth: { + status: "authenticated" as const, + email: "a@x.io", + workspaceId: "w1", + endpoint: "http://127.0.0.1:8317", + }, + }, + ], [ "other workspace", readings(80, older), @@ -2093,6 +2107,36 @@ describe("accountUsageLimits", () => { expect(accountUsageLimits(own, ownLimits, [peer])).toBe(ownLimits); }); + it("keeps a hub-routed login's own reading instead of a direct peer's", () => { + const ownLimits = failed(older); + const own = login({ + auth: { status: "authenticated", email: "a@x.io", endpoint: "http://127.0.0.1:8317" }, + usageLimits: ownLimits, + }); + const peer = login({ + auth: { status: "authenticated", email: "a@x.io" }, + usageLimits: readings(20, newer), + }); + expect(accountUsageLimits(own, ownLimits, [peer])).toBe(ownLimits); + }); + + it("keeps its own reading when multiple workspaces make the account ambiguous", () => { + const ownLimits = readings(80, older); + const own = login({ + auth: { status: "authenticated", email: "a@x.io" }, + usageLimits: ownLimits, + }); + const peerA = login({ + auth: { status: "authenticated", email: "a@x.io", workspaceId: "w1" }, + usageLimits: readings(20, newer), + }); + const peerB = login({ + auth: { status: "authenticated", email: "a@x.io", workspaceId: "w2" }, + usageLimits: readings(10, newer), + }); + expect(accountUsageLimits(own, ownLimits, [peerA, peerB])).toBe(ownLimits); + }); + it("keeps an unsupported own reading", () => { const unsupported = { checkedAt: older, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 941d61b7d5c57..6788b2c6a3fa6 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -944,7 +944,6 @@ export function hubRoutes( account.models ? listed(account) : serving.has(account.driver) || account.driver === driver; } -// A readable reading beats an unreadable one; between equals the later check wins. function fresherLimits(next: ServerProviderUsageLimits, previous: ServerProviderUsageLimits) { const previousUsable = limitsNotice(previous) === null; const usable = limitsNotice(next) === null; @@ -960,15 +959,21 @@ export function accountUsageLimits( own: ServerProviderUsageLimits, peers: Iterable, ): ServerProviderUsageLimits { - if (!provider.enabled || own.unavailable?.reason === "unsupported") return own; + if ( + !provider.enabled || + provider.auth.endpoint !== undefined || + own.unavailable?.reason === "unsupported" + ) { + return own; + } const enabledPeers = [...peers].filter((peer) => peer.enabled); const resolveKey = accountKeyResolver([provider, ...enabledPeers].map(nativeReport)); const key = resolveKey(nativeReport(provider)); if (key === null) return own; let best = own; - for (const peer of enabledPeers) { + for (const peer of providersWithLimits(enabledPeers)) { if (peer.driver !== provider.driver || !peer.usageLimits) continue; - if (limitsNotice(peer.usageLimits) !== null) continue; + if (peer.auth.endpoint !== undefined || limitsNotice(peer.usageLimits) !== null) continue; if (resolveKey(nativeReport(peer)) !== key) continue; if (fresherLimits(peer.usageLimits, best)) best = peer.usageLimits; } From 8c375ca5d2dd6b326a61c9d7ce3870f412492438 Mon Sep 17 00:00:00 2001 From: K1erans <178052660+K1erans@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:54:24 +0000 Subject: [PATCH 4/4] test(web): drop thread details tests the shared unit tests already cover The browser test mocked the presentations atom and the logic test passed on main; accountUsageLimits in usageLimits.test.ts covers both cases. Co-Authored-By: Claude Opus 5.5 --- .../chat/ThreadDetailsUsage.browser.test.tsx | 101 ------------------ .../chat/ThreadDetailsUsage.logic.test.ts | 35 ------ 2 files changed, 136 deletions(-) delete mode 100644 apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx b/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx deleted file mode 100644 index 9a3e3f735c25e..0000000000000 --- a/apps/web/src/components/chat/ThreadDetailsUsage.browser.test.tsx +++ /dev/null @@ -1,101 +0,0 @@ -import { - ProviderDriverKind, - ProviderInstanceId, - type ServerProvider, - type ServerProviderUsageLimits, -} from "@t3tools/contracts"; -import { RegistryContext } from "@effect/atom-react"; -import { type Atom, AtomRegistry } from "effect/reactivity"; -import { describe, expect, it, vi } from "vite-plus/test"; - -import { render } from "../../../test/render"; - -type PresentationMap = ReadonlyMap< - string, - { readonly serverConfig: { readonly providers: readonly ServerProvider[] } | null } ->; - -const presentationsAtom = vi.hoisted(() => ({ - current: null as Atom.Writable | null, -})); - -vi.mock(import("~/state/presentation"), async () => { - const { Atom } = await import("effect/reactivity"); - const atom = Atom.make(new Map()); - presentationsAtom.current = atom; - return { - environmentPresentations: { - presentationsWithProvidersAtom: atom as never, - }, - environmentSummaries: {} as never, - useEnvironmentPresentation: () => ({ isReady: true, presentation: null }), - } as never; -}); - -import { ThreadDetailsUsage } from "./ThreadDetailsUsage"; - -const checkedAt = "2026-10-30T11:59:00.000Z"; -const instanceId = ProviderInstanceId.make("claudeAgent"); - -function limits(usedPercent: number, at: string): ServerProviderUsageLimits { - return { - checkedAt: at, - credentialFingerprint: "fp-1", - windows: [ - { - id: "five_hour", - kind: "session", - label: "Session", - usedPercent, - windowDurationMins: 300, - resetsAt: "2026-10-30T13:17:00.000Z", - }, - ], - }; -} - -function claude(usageLimits: ServerProviderUsageLimits): ServerProvider { - return { - instanceId, - driver: ProviderDriverKind.make("claudeAgent"), - enabled: true, - installed: true, - version: null, - status: "ready", - auth: { status: "authenticated" }, - checkedAt, - models: [], - slashCommands: [], - skills: [], - usageLimits, - }; -} - -function registryWithPeers(peers: readonly ServerProvider[]) { - const registry = AtomRegistry.make(); - registry.set( - presentationsAtom.current!, - new Map([ - ["connecting", { serverConfig: null }], - ["linked", { serverConfig: { providers: peers } }], - ]), - ); - return registry; -} - -describe("ThreadDetailsUsage", () => { - it("shows a linked environment's fresher reading of the same account", async () => { - const registry = registryWithPeers([claude(limits(25, "2026-10-30T11:58:00.000Z"))]); - await using screen = await render( - - - , - ); - await expect.element(screen.getByRole("link", { name: "Session: 75% left" })).toBeVisible(); - }); -}); diff --git a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts index ea6aea5d480e5..5a024cf4e558b 100644 --- a/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts +++ b/apps/web/src/components/chat/ThreadDetailsUsage.logic.test.ts @@ -145,38 +145,3 @@ describe("resolveThreadDetailsLimits", () => { }); }); }); - -describe("resolveThreadDetailsLimits across environments", () => { - const fresh = "2026-10-30T11:58:00.000Z"; - const failed: ServerProviderUsageLimits = { - checkedAt, - windows: [], - unavailable: { reason: "probeFailed" }, - }; - - it("keeps a hub-routed instance's failed reading instead of a non-hub peer's", () => { - const own: ServerProvider = { - ...claudeInstance(failed), - auth: { - status: "authenticated", - type: "customEndpoint", - label: "Custom API endpoint", - endpoint: "http://127.0.0.1:8317", - email: "dev@example.com", - }, - }; - const peer: ServerProvider = { - ...claudeInstance({ ...limitsAt(25), checkedAt: fresh }), - auth: { status: "authenticated", email: "dev@example.com" }, - }; - const unreadable = hub("Hub", [{ id: "a.json", limits: failed }]); - expect(resolveThreadDetailsLimits(instanceId, null, [own], [unreadable], now, [peer])).toEqual({ - kind: "instance", - limits: failed, - }); - expect(resolveThreadDetailsLimits(instanceId, null, [own], [], now, [peer])).toEqual({ - kind: "instance", - limits: failed, - }); - }); -});