From 54d006d522183ebcb5bfc9bcdb4485598689cc33 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Sat, 8 Aug 2026 19:18:30 -0700 Subject: [PATCH] fix: post-rebase review findings - Call the SDK usage method through the query object; an extracted reference loses its receiver and the catch made that failure silent. - Treat a backwards wall-clock step as an expired throttle. - Pick the codex transcript snapshot by its own timestamp, not file mtime. - Per-provider loading state: a provider without a snapshot shows Loading while any environment is still answering, not 'No limit data yet'. - The usage page refresh button now refreshes the limits cache too. Co-Authored-By: Claude Fable 5 --- apps/server/src/provider/Layers/ClaudeAdapter.ts | 12 ++++++++---- apps/server/src/usage/accountLimitsTranscripts.ts | 11 +++++++++-- apps/web/src/components/usage/AccountLimits.tsx | 10 ++++++---- apps/web/src/components/usage/UsagePage.tsx | 12 ++++++++++-- apps/web/src/state/accountLimits.ts | 7 +++++++ 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/apps/server/src/provider/Layers/ClaudeAdapter.ts b/apps/server/src/provider/Layers/ClaudeAdapter.ts index 34c43fe51b..bb3e0cfa0e 100644 --- a/apps/server/src/provider/Layers/ClaudeAdapter.ts +++ b/apps/server/src/provider/Layers/ClaudeAdapter.ts @@ -3434,15 +3434,19 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* ( const emitAccountUsageSnapshot = Effect.fn("emitAccountUsageSnapshot")(function* ( context: ClaudeSessionContext, ) { - const fetchUsage = context.query.usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET; - if (!fetchUsage) return; + if (!context.query.usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET) return; const now = yield* Clock.currentTimeMillis; - if (now - lastAccountUsageFetchAtMs < ACCOUNT_USAGE_MIN_INTERVAL_MS) return; + const elapsed = now - lastAccountUsageFetchAtMs; + // A negative elapsed means the wall clock stepped backwards; treat it as + // expired rather than freezing refreshes until the clock catches up. + if (elapsed >= 0 && elapsed < ACCOUNT_USAGE_MIN_INTERVAL_MS) return; lastAccountUsageFetchAtMs = now; const usage = yield* Effect.promise(async () => { try { - return await fetchUsage(); + // Called through the query object so the SDK method keeps its + // receiver; an extracted reference loses `this` and throws. + return await context.query.usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET?.(); } catch { return undefined; } diff --git a/apps/server/src/usage/accountLimitsTranscripts.ts b/apps/server/src/usage/accountLimitsTranscripts.ts index 07683fa6f1..59024cedbe 100644 --- a/apps/server/src/usage/accountLimitsTranscripts.ts +++ b/apps/server/src/usage/accountLimitsTranscripts.ts @@ -48,11 +48,18 @@ export async function readLatestCodexRateLimits( return null; } const newestFirst = [...files].sort((a, b) => b.mtimeMs - a.mtimeMs).slice(0, MAX_FILES); + // The newest-mtime file does not necessarily hold the newest snapshot: a + // session can keep appending non-token events after its last rate-limit + // line. A file's snapshot can never be newer than the file's mtime, so + // keep scanning only while a remaining file's mtime could still beat the + // best snapshot found - which usually stops after one or two files. + let best: CodexTranscriptRateLimits | null = null; for (const file of newestFirst) { + if (best !== null && file.mtimeMs <= best.asOfMs) break; const found = await readTailRateLimits(file.path, file.mtimeMs); - if (found) return found; + if (found !== null && (best === null || found.asOfMs > best.asOfMs)) best = found; } - return null; + return best; } function isRecord(value: unknown): value is Record { diff --git a/apps/web/src/components/usage/AccountLimits.tsx b/apps/web/src/components/usage/AccountLimits.tsx index 31d78b85ef..ca1113af96 100644 --- a/apps/web/src/components/usage/AccountLimits.tsx +++ b/apps/web/src/components/usage/AccountLimits.tsx @@ -69,7 +69,7 @@ function SnapshotAge({ snapshot, nowMs }: { snapshot: AccountLimitsSnapshot; now /** Compact per-provider availability, shown on hovering the Usage button. */ export function AccountLimitsHoverCard() { - const { snapshots, isPending } = useAccountLimits(); + const { snapshots, isPending, isSettling } = useAccountLimits(); const nowMs = useNowMs(); if (isPending && snapshots.size === 0) { @@ -93,7 +93,9 @@ export function AccountLimitsHoverCard() { {snapshot === undefined || snapshot.windows.length === 0 ? ( -

No limit data yet

+

+ {snapshot === undefined && isSettling ? "Loading…" : "No limit data yet"} +

) : ( snapshot.windows.map((window) => (
@@ -128,7 +130,7 @@ export function AccountLimitsHoverCard() { /** The "Limits" strip above the analytics: one column per provider. */ export function AccountLimitsSection() { - const { snapshots, isPending } = useAccountLimits(); + const { snapshots, isSettling } = useAccountLimits(); const nowMs = useNowMs(); return ( @@ -153,7 +155,7 @@ export function AccountLimitsSection() {
{snapshot === undefined || snapshot.windows.length === 0 ? (

- {isPending && snapshot === undefined ? "Loading…" : "No limit data yet"} + {snapshot === undefined && isSettling ? "Loading…" : "No limit data yet"}

) : ( snapshot.windows.map((window) => { diff --git a/apps/web/src/components/usage/UsagePage.tsx b/apps/web/src/components/usage/UsagePage.tsx index 35e27b5f52..7d65587f4e 100644 --- a/apps/web/src/components/usage/UsagePage.tsx +++ b/apps/web/src/components/usage/UsagePage.tsx @@ -1,9 +1,10 @@ import type { UsageProviderKind } from "@t3tools/contracts"; import { useCanGoBack, useNavigate, useRouter } from "@tanstack/react-router"; import { ArrowLeftIcon, CheckIcon, RefreshCwIcon, XIcon } from "lucide-react"; -import { useMemo, useState } from "react"; +import { useCallback, useMemo, useState } from "react"; import { cn } from "../../lib/utils"; +import { useAccountLimits } from "../../state/accountLimits"; import { useUsage, type EnvironmentUsageStatus } from "../../state/usage"; import { enumerateDays, @@ -36,7 +37,14 @@ export function UsagePage() { // Recomputed only when the window length changes, so a re-render does not // shift the range and refetch every environment. const window = useMemo(() => makeWindow(windowDays), [windowDays]); - const { merged, environments, isPending, isPartial, refresh } = useUsage(window); + const { merged, environments, isPending, isPartial, refresh: refreshUsage } = useUsage(window); + const { refresh: refreshLimits } = useAccountLimits(); + // One refresh button, two caches: the transcript scan and the limits + // snapshot both re-read, or the Limits strip lags the rest of the page. + const refresh = useCallback(() => { + refreshUsage(); + refreshLimits(); + }, [refreshUsage, refreshLimits]); // Hold the content until every environment is terminal. Rendering merged // totals while devices are still answering makes every number on the page diff --git a/apps/web/src/state/accountLimits.ts b/apps/web/src/state/accountLimits.ts index 6e0d4d9cd0..134e06253b 100644 --- a/apps/web/src/state/accountLimits.ts +++ b/apps/web/src/state/accountLimits.ts @@ -51,6 +51,12 @@ export interface AccountLimitsView { readonly snapshots: ReadonlyMap; /** True until at least one environment has answered. */ readonly isPending: boolean; + /** + * True while any environment is still answering. A provider with no + * snapshot is "loading" while this holds and "no data" once it clears - + * the first environment to answer must not decide that for the rest. + */ + readonly isSettling: boolean; readonly refresh: () => void; } @@ -87,6 +93,7 @@ export function useAccountLimits(): AccountLimitsView { return { snapshots, isPending: answered === 0 && stillReporting > 0, + isSettling: stillReporting > 0, refresh, }; }