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> {