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 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-08-08 19:18:30 -07:00
1 parent a3823306b7
commit 54d006d522
5 files changed
+40 -12

No files matched your search

@@ -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;
}
@@ -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<string, unknown> {
@@ -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() {
</span>
</div>
{snapshot === undefined || snapshot.windows.length === 0 ? (
<p className="text-[11px] text-muted-foreground">No limit data yet</p>
<p className="text-[11px] text-muted-foreground">
{snapshot === undefined && isSettling ? "Loading…" : "No limit data yet"}
</p>
) : (
snapshot.windows.map((window) => (
<div key={window.id} className="flex items-center gap-2">
@@ -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() {
</div>
{snapshot === undefined || snapshot.windows.length === 0 ? (
<p className="text-xs text-muted-foreground">
{isPending && snapshot === undefined ? "Loading…" : "No limit data yet"}
{snapshot === undefined && isSettling ? "Loading…" : "No limit data yet"}
</p>
) : (
snapshot.windows.map((window) => {
+10 -2
View File
@@ -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
+7
View File
@@ -51,6 +51,12 @@ export interface AccountLimitsView {
readonly snapshots: ReadonlyMap<UsageProviderKind, AccountLimitsSnapshot>;
/** 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,
};
}