refactor: remove unwired auto-paired-mode and usage exhaustion gate
src/auto-paired-mode.ts had no production importer. Remove it together with the helpers only it used: getClaudeUsageExhaustion / isClaudeUsageExhausted in claude-usage.ts and getCodexUsageExhaustion / isCodexUsageExhausted in codex-usage-collector.ts, plus their tests. auto-paired-mode-state.json stays in the data-state allowlist: the live data dir still has that file and an unknown state file is a startup precondition failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -637,103 +637,3 @@ export async function fetchAllClaudeUsage(): Promise<ClaudeAccountUsage[]> {
|
||||
|
||||
// Legacy alias
|
||||
export const fetchClaudeUsageViaCli = fetchClaudeUsage;
|
||||
|
||||
// ── Exhaustion gate ───────────────────────────────────────────────────────
|
||||
//
|
||||
// Threshold above which a Claude account is considered "out of budget" for
|
||||
// the purpose of admitting new turns. A turn is blocked only when *every*
|
||||
// Claude account is either at/over this threshold on either window or
|
||||
// already in a rate-limit cooldown.
|
||||
|
||||
export const CLAUDE_EXHAUSTION_THRESHOLD_PCT = 95;
|
||||
|
||||
export interface UsageExhaustionInfo {
|
||||
exhausted: boolean;
|
||||
/** ISO timestamp of the soonest reset that would relieve exhaustion. */
|
||||
nextResetAt: string | null;
|
||||
}
|
||||
|
||||
/** Normalise the API's utilization field (which can be fractional or percent). */
|
||||
function normaliseUtilization(u: number | undefined): number {
|
||||
if (u == null || !Number.isFinite(u)) return 0;
|
||||
return u >= 0 && u < 1 ? u * 100 : u;
|
||||
}
|
||||
|
||||
function parseResetMs(s: string | undefined | null): number | null {
|
||||
if (!s) return null;
|
||||
const t = Date.parse(s);
|
||||
return Number.isFinite(t) ? t : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports whether every configured Claude account is either ≥ 95% on a
|
||||
* window or in a rate-limit cooldown, and — when so — the soonest moment at
|
||||
* which any one account is expected to recover.
|
||||
*
|
||||
* Reads the on-disk usage cache (refreshed every minute by the dashboard
|
||||
* renderer); performs no network I/O. Returns `exhausted: false` when there
|
||||
* are no configured tokens, or when usage data is missing for any account
|
||||
* (err on letting work through).
|
||||
*/
|
||||
export function getClaudeUsageExhaustion(): UsageExhaustionInfo {
|
||||
loadUsageDiskCache();
|
||||
const allTokens = getAllTokens();
|
||||
if (allTokens.length === 0) return { exhausted: false, nextResetAt: null };
|
||||
|
||||
let earliestRecoveryMs = Infinity;
|
||||
|
||||
for (const t of allTokens) {
|
||||
if (t.isRateLimited) {
|
||||
// Rate-limit recovery time is not exposed on the token snapshot here;
|
||||
// we treat it as "unknown but eventually". Skip from the min calc.
|
||||
continue;
|
||||
}
|
||||
const credsToken = readCredentialsAccessToken(t.index);
|
||||
const readKeys = getUsageCacheReadKeys(t.token, t.index, credsToken);
|
||||
let entry: UsageCacheEntry | undefined;
|
||||
for (const key of readKeys) {
|
||||
if (usageDiskCache[key]) {
|
||||
entry = usageDiskCache[key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!entry) return { exhausted: false, nextResetAt: null };
|
||||
|
||||
const h5 = normaliseUtilization(entry.usage.five_hour?.utilization);
|
||||
const d7 = normaliseUtilization(entry.usage.seven_day?.utilization);
|
||||
if (Math.max(h5, d7) < CLAUDE_EXHAUSTION_THRESHOLD_PCT) {
|
||||
return { exhausted: false, nextResetAt: null };
|
||||
}
|
||||
|
||||
// This account is exhausted. It becomes available again when whichever
|
||||
// window(s) are over the threshold drop back below it. We approximate
|
||||
// that as max(reset of each window currently ≥ threshold).
|
||||
const h5ResetMs =
|
||||
h5 >= CLAUDE_EXHAUSTION_THRESHOLD_PCT
|
||||
? parseResetMs(entry.usage.five_hour?.resets_at)
|
||||
: null;
|
||||
const d7ResetMs =
|
||||
d7 >= CLAUDE_EXHAUSTION_THRESHOLD_PCT
|
||||
? parseResetMs(entry.usage.seven_day?.resets_at)
|
||||
: null;
|
||||
const candidates = [h5ResetMs, d7ResetMs].filter(
|
||||
(v): v is number => v != null,
|
||||
);
|
||||
if (candidates.length > 0) {
|
||||
const accountRecovery = Math.max(...candidates);
|
||||
if (accountRecovery < earliestRecoveryMs) {
|
||||
earliestRecoveryMs = accountRecovery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const nextResetAt = Number.isFinite(earliestRecoveryMs)
|
||||
? new Date(earliestRecoveryMs).toISOString()
|
||||
: null;
|
||||
return { exhausted: true, nextResetAt };
|
||||
}
|
||||
|
||||
/** Backwards-compat boolean wrapper. */
|
||||
export function isClaudeUsageExhausted(): boolean {
|
||||
return getClaudeUsageExhaustion().exhausted;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user