From 02ea212be0a4797ae9c38d2394e35067d81ffa40 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 1 Oct 2026 18:50:40 +0900 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + src/auto-paired-mode.ts | 179 ------------------- src/claude-usage.ts | 100 ----------- src/codex-usage-collector.ts | 73 -------- src/data-state-files.ts | 2 + src/usage-exhaustion.test.ts | 336 ----------------------------------- 6 files changed, 3 insertions(+), 688 deletions(-) delete mode 100644 src/auto-paired-mode.ts delete mode 100644 src/usage-exhaustion.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d47dec..8f35c79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - 웹 대시보드(`src/web-dashboard-*`, `apps/dashboard`), Android 앱(`apps/android`), Glasses UI와 이를 위해서만 있던 설정 저장소(`settings-store`, `settings-store-moa`, `room-skill-settings`, `runtime-inventory`, `codex-live-status`, `settings-effort`), `WEB_DASHBOARD_*` 설정, `dashboard:*`/`android:*` 스크립트, react·vite·playwright 의존성. 잘못된 계정(`~/.codex`)을 갱신하던 Codex 계정 새로고침 루프도 함께 제거. 호스트 증거 도구의 `dashboard_dist`/`android_debug_apk` 항목 제거. Discord 상태 대시보드는 그대로 유지 - Mixture of Agents(MoA) arbiter 프롬프트 보강, Kimi 사용량 조회/대시보드 행, `MOA_*` 설정, 코드에서 읽지 않던 `FALLBACK_*` provider 설정 예시 +- 런타임에 연결되지 않았던 auto-paired-mode(`src/auto-paired-mode.ts`)와 그것만 쓰던 Claude/Codex 사용량 소진 판정 함수 ### Fixed diff --git a/src/auto-paired-mode.ts b/src/auto-paired-mode.ts deleted file mode 100644 index 5ecada4..0000000 --- a/src/auto-paired-mode.ts +++ /dev/null @@ -1,179 +0,0 @@ -/** - * Auto paired-mode degrade/restore. - * - * When all configured Claude accounts are at/over the 95% exhaustion threshold, - * paired (tribunal) rooms cannot meaningfully run their reviewer/arbiter - * because the gate blocks Claude turns. To keep the user-facing rooms - * responsive, we temporarily flip selected rooms to `single` mode and notify - * each affected channel. When usage recovers we flip them back to `tribunal`. - * - * Selection (Option B agreed with the user): - * - The most-recently-active room (per chats.last_message_time) - * - All rooms that are currently in-flight (queue status === 'processing') - * - Deduplicated; rooms whose owner is `claude-code` are skipped because - * degrading them does not help — the 95% gate blocks them either way. - * - * Restore behavior: - * - Only restore a room if it is still in 'single' (don't override a manual - * user change made during the degrade window). - */ -import fs from 'fs'; -import path from 'path'; - -import { getClaudeUsageExhaustion } from './claude-usage.js'; -import { DATA_DIR } from './config.js'; -import { - getAllChats, - getAllRoomBindings, - getEffectiveRoomMode, - getStoredRoomSettings, - setExplicitRoomMode, -} from './db.js'; -import { logger } from './logger.js'; -import { readJsonFile, writeJsonFile } from './utils.js'; - -const STATE_PATH = path.join(DATA_DIR, 'auto-paired-mode-state.json'); - -const DEGRADE_NOTICE = - '클로드 사용량이 95%를 넘어 잠시 single 모드로 전환했어요. 사용량이 회복되면 paired 모드로 다시 켤게요.'; -const RESTORE_NOTICE = '클로드 사용량이 회복돼서 paired 모드로 다시 켰어요.'; - -interface DowngradeRecord { - chatJid: string; - atIso: string; -} - -interface AutoPairedModeState { - exhausted: boolean; - downgraded: DowngradeRecord[]; -} - -export interface AutoPairedModeDeps { - queue?: { - getStatuses(jids: string[]): Array<{ jid: string; status: string }>; - }; - sendNotice: (chatJid: string, text: string) => Promise; -} - -function loadState(): AutoPairedModeState { - const raw = readJsonFile(STATE_PATH); - if (!raw || typeof raw !== 'object') { - return { exhausted: false, downgraded: [] }; - } - return { - exhausted: Boolean(raw.exhausted), - downgraded: Array.isArray(raw.downgraded) ? raw.downgraded : [], - }; -} - -function saveState(state: AutoPairedModeState): void { - try { - fs.mkdirSync(path.dirname(STATE_PATH), { recursive: true }); - writeJsonFile(STATE_PATH, state); - } catch (err) { - logger.warn({ err }, 'auto-paired-mode: failed to persist state'); - } -} - -function pickCandidates(deps: AutoPairedModeDeps): string[] { - const bindings = getAllRoomBindings(); - const allJids = Object.keys(bindings); - if (allJids.length === 0) return []; - - const candidates = new Set(); - - // Most-recently-active room first (chats ordered DESC by last_message_time). - const chats = getAllChats(); - const recent = chats.find((c) => allJids.includes(c.jid)); - if (recent) candidates.add(recent.jid); - - // Currently in-flight rooms. - if (deps.queue) { - for (const status of deps.queue.getStatuses(allJids)) { - if (status.status === 'processing') candidates.add(status.jid); - } - } - - // Skip claude-owned rooms (Option B). - const result: string[] = []; - for (const jid of candidates) { - const settings = getStoredRoomSettings(jid); - if (settings?.ownerAgentType === 'claude-code') continue; - if (getEffectiveRoomMode(jid) !== 'tribunal') continue; - result.push(jid); - } - return result; -} - -export async function evaluateAndApplyAutoPairedMode( - deps: AutoPairedModeDeps, -): Promise { - const exhaustion = getClaudeUsageExhaustion(); - const state = loadState(); - - if (exhaustion.exhausted && !state.exhausted) { - // Transition: healthy → exhausted. Downgrade selected rooms. - const targets = pickCandidates(deps); - const downgraded: DowngradeRecord[] = []; - for (const jid of targets) { - try { - setExplicitRoomMode(jid, 'single'); - downgraded.push({ chatJid: jid, atIso: new Date().toISOString() }); - logger.info( - { jid }, - 'auto-paired-mode: downgraded room to single (claude exhausted)', - ); - try { - await deps.sendNotice(jid, DEGRADE_NOTICE); - } catch (err) { - logger.warn( - { err, jid }, - 'auto-paired-mode: failed to send degrade notice', - ); - } - } catch (err) { - logger.warn({ err, jid }, 'auto-paired-mode: failed to downgrade room'); - } - } - saveState({ exhausted: true, downgraded }); - return; - } - - if (!exhaustion.exhausted && state.exhausted) { - // Transition: exhausted → healthy. Restore previously-downgraded rooms. - for (const record of state.downgraded) { - try { - if (getEffectiveRoomMode(record.chatJid) !== 'single') { - // Respect manual user changes during the degrade window. - logger.info( - { jid: record.chatJid }, - 'auto-paired-mode: skip restore (room is no longer single)', - ); - continue; - } - setExplicitRoomMode(record.chatJid, 'tribunal'); - logger.info( - { jid: record.chatJid }, - 'auto-paired-mode: restored room to tribunal', - ); - try { - await deps.sendNotice(record.chatJid, RESTORE_NOTICE); - } catch (err) { - logger.warn( - { err, jid: record.chatJid }, - 'auto-paired-mode: failed to send restore notice', - ); - } - } catch (err) { - logger.warn( - { err, jid: record.chatJid }, - 'auto-paired-mode: failed to restore room', - ); - } - } - saveState({ exhausted: false, downgraded: [] }); - return; - } - - // Steady state — nothing to do. -} diff --git a/src/claude-usage.ts b/src/claude-usage.ts index 5844c2f..b7ab1eb 100644 --- a/src/claude-usage.ts +++ b/src/claude-usage.ts @@ -637,103 +637,3 @@ export async function fetchAllClaudeUsage(): Promise { // 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; -} diff --git a/src/codex-usage-collector.ts b/src/codex-usage-collector.ts index da2646c..0032204 100644 --- a/src/codex-usage-collector.ts +++ b/src/codex-usage-collector.ts @@ -40,26 +40,6 @@ export interface CodexUsageRefreshResult { /** Full scan interval — exported so the orchestrator can schedule it. */ export const CODEX_FULL_SCAN_INTERVAL = 3_600_000; // 1 hour -/** - * Threshold above which a Codex account is considered "out of budget" for - * the purpose of admitting new turns. A turn is blocked only when *every* - * Codex account is either at/over this threshold on either window or - * already in a rate-limit cooldown. - */ -export const CODEX_EXHAUSTION_THRESHOLD_PCT = 95; - -export interface UsageExhaustionInfo { - exhausted: boolean; - /** ISO timestamp of the soonest reset that would relieve exhaustion. */ - nextResetAt: string | null; -} - -function parseResetMs(s: string | undefined | null): number | null { - if (!s) return null; - const t = Date.parse(s); - return Number.isFinite(t) ? t : null; -} - /** Coerce a string-or-number resetsAt (numeric epoch in seconds) to ISO. */ function toIsoMaybe(value: string | number | undefined): string | undefined { if (value == null) return undefined; @@ -71,59 +51,6 @@ function toIsoMaybe(value: string | number | undefined): string | undefined { return Number.isFinite(ms) ? new Date(ms).toISOString() : undefined; } -/** - * Reports whether every configured Codex 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 in-process rotation snapshot; performs no I/O. Returns - * `exhausted: false` when there are no configured accounts, or when usage - * data is missing for any account (err on letting work through). - */ -export function getCodexUsageExhaustion(): UsageExhaustionInfo { - const accounts = getAllCodexAccounts(); - if (accounts.length === 0) return { exhausted: false, nextResetAt: null }; - - let earliestRecoveryMs = Infinity; - - for (const a of accounts) { - if (a.isRateLimited) continue; // counts as exhausted, recovery unknown here - const h5 = a.cachedUsagePct; - const d7 = a.cachedUsageD7Pct; - // -1 / undefined means "unknown" → treat as headroom - if (h5 == null || h5 < 0 || d7 == null || d7 < 0) { - return { exhausted: false, nextResetAt: null }; - } - if (Math.max(h5, d7) < CODEX_EXHAUSTION_THRESHOLD_PCT) { - return { exhausted: false, nextResetAt: null }; - } - - const h5ResetMs = - h5 >= CODEX_EXHAUSTION_THRESHOLD_PCT ? parseResetMs(a.resetAt) : null; - const d7ResetMs = - d7 >= CODEX_EXHAUSTION_THRESHOLD_PCT ? parseResetMs(a.resetD7At) : 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 isCodexUsageExhausted(): boolean { - return getCodexUsageExhaustion().exhausted; -} - function getFnmCodexBinDirs(): string[] { const fnmRoot = path.join(os.homedir(), '.local', 'share', 'fnm'); const dirs: string[] = []; diff --git a/src/data-state-files.ts b/src/data-state-files.ts index 8a3f9f3..6c0130d 100644 --- a/src/data-state-files.ts +++ b/src/data-state-files.ts @@ -7,6 +7,8 @@ const ALLOWED_DATA_JSON_PATTERNS = [ /^codex-warmup-state\.json$/, /^claude-usage-cache\.json$/, /^restart-context\..+\.json$/, + // Leftover from the removed auto-paired-mode feature. Kept allowed so an + // existing file does not block startup; drop once deployments delete it. /^auto-paired-mode-state\.json$/, ]; diff --git a/src/usage-exhaustion.test.ts b/src/usage-exhaustion.test.ts deleted file mode 100644 index 55b148e..0000000 --- a/src/usage-exhaustion.test.ts +++ /dev/null @@ -1,336 +0,0 @@ -/** - * Unit tests for the 95% usage-exhaustion helpers (Claude + Codex). - * - * These do not exercise the real disk cache or rotation state; they mock - * just enough to verify the gate's truth-table: - * - * - no accounts → false (don't block) - * - missing data on any → false (don't block) - * - rate-limited account → counts as exhausted - * - any account < 95% → false (route there instead) - * - all accounts ≥ 95% → true, with earliest expected recovery time - */ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; - -vi.mock('./logger.js', () => ({ - logger: { - info: vi.fn(), - warn: vi.fn(), - error: vi.fn(), - debug: vi.fn(), - }, -})); - -vi.mock('./config.js', () => ({ - DATA_DIR: '/tmp/ejclaw-usage-exhaustion-data', -})); - -const { getAllTokensMock } = vi.hoisted(() => ({ - getAllTokensMock: vi.fn(), -})); -vi.mock('./token-rotation.js', () => ({ - getAllTokens: getAllTokensMock, - getCurrentToken: () => null, - getConfiguredClaudeTokens: () => [], -})); - -const { getAllCodexAccountsMock } = vi.hoisted(() => ({ - getAllCodexAccountsMock: vi.fn(), -})); -vi.mock('./codex-token-rotation.js', () => ({ - getAllCodexAccounts: getAllCodexAccountsMock, - getCodexAuthPath: () => null, - updateCodexAccountUsage: vi.fn(), -})); - -const { readJsonFileMock } = vi.hoisted(() => ({ - readJsonFileMock: vi.fn(), -})); -vi.mock('./utils.js', async () => { - const actual = - await vi.importActual('./utils.js'); - return { - ...actual, - readJsonFile: readJsonFileMock, - writeJsonFile: vi.fn(), - }; -}); - -afterEach(() => { - vi.resetModules(); - vi.clearAllMocks(); -}); - -describe('getClaudeUsageExhaustion', () => { - beforeEach(() => { - readJsonFileMock.mockReturnValue({}); - }); - - it('returns exhausted=false when no tokens are configured', async () => { - getAllTokensMock.mockReturnValue([]); - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - expect(getClaudeUsageExhaustion()).toEqual({ - exhausted: false, - nextResetAt: null, - }); - }); - - it('returns exhausted=false when an account has no cached usage', async () => { - getAllTokensMock.mockReturnValue([ - { - token: 'sk-ant-oat01-aaaaaaaa', - index: 0, - masked: 'aaa', - isActive: true, - isRateLimited: false, - }, - ]); - readJsonFileMock.mockReturnValue({}); // empty cache - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - expect(getClaudeUsageExhaustion().exhausted).toBe(false); - }); - - it('returns exhausted=true with the earliest recovery when all accounts are exhausted', async () => { - const reset0 = new Date('2026-04-28T01:00:00Z').toISOString(); - const reset1 = new Date('2026-04-28T03:00:00Z').toISOString(); - getAllTokensMock.mockReturnValue([ - { - token: 'sk-ant-oat01-aaaaaaaa', - index: 0, - masked: 'aaa', - isActive: true, - isRateLimited: false, - }, - { - token: 'sk-ant-oat01-bbbbbbbb', - index: 1, - masked: 'bbb', - isActive: false, - isRateLimited: false, - }, - ]); - readJsonFileMock.mockReturnValue({ - 'account-0': { - usage: { - five_hour: { utilization: 96, resets_at: reset0 }, - seven_day: { utilization: 50, resets_at: '' }, - }, - fetchedAt: Date.now(), - }, - 'account-1': { - usage: { - five_hour: { utilization: 30, resets_at: '' }, - seven_day: { utilization: 99, resets_at: reset1 }, - }, - fetchedAt: Date.now(), - }, - }); - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - const info = getClaudeUsageExhaustion(); - expect(info.exhausted).toBe(true); - // Earliest recovery is account-0 (5h reset at reset0). - expect(info.nextResetAt).toBe(reset0); - }); - - it('returns exhausted=false when at least one account still has headroom', async () => { - getAllTokensMock.mockReturnValue([ - { - token: 'sk-ant-oat01-aaaaaaaa', - index: 0, - masked: 'aaa', - isActive: true, - isRateLimited: false, - }, - { - token: 'sk-ant-oat01-bbbbbbbb', - index: 1, - masked: 'bbb', - isActive: false, - isRateLimited: false, - }, - ]); - readJsonFileMock.mockReturnValue({ - 'account-0': { - usage: { - five_hour: { utilization: 99 }, - seven_day: { utilization: 99 }, - }, - fetchedAt: Date.now(), - }, - 'account-1': { - usage: { - five_hour: { utilization: 80 }, - seven_day: { utilization: 70 }, - }, - fetchedAt: Date.now(), - }, - }); - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - expect(getClaudeUsageExhaustion().exhausted).toBe(false); - }); - - it('treats rate-limited accounts as exhausted', async () => { - getAllTokensMock.mockReturnValue([ - { - token: 'sk-ant-oat01-aaaaaaaa', - index: 0, - masked: 'aaa', - isActive: false, - isRateLimited: true, - }, - ]); - readJsonFileMock.mockReturnValue({}); - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - expect(getClaudeUsageExhaustion().exhausted).toBe(true); - }); - - it('handles utilization on the 0–1 scale (legacy fractional form)', async () => { - getAllTokensMock.mockReturnValue([ - { - token: 'sk-ant-oat01-aaaaaaaa', - index: 0, - masked: 'aaa', - isActive: true, - isRateLimited: false, - }, - ]); - readJsonFileMock.mockReturnValue({ - 'account-0': { - usage: { - five_hour: { utilization: 0.97 }, - seven_day: { utilization: 0.4 }, - }, - fetchedAt: Date.now(), - }, - }); - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - expect(getClaudeUsageExhaustion().exhausted).toBe(true); - }); - - it('treats percent-scale utilization=1 as 1%, not 100%', async () => { - getAllTokensMock.mockReturnValue([ - { - token: 'sk-ant-oat01-aaaaaaaa', - index: 0, - masked: 'aaa', - isActive: true, - isRateLimited: false, - }, - ]); - readJsonFileMock.mockReturnValue({ - 'account-0': { - usage: { - five_hour: { utilization: 1 }, - seven_day: { utilization: 40 }, - }, - fetchedAt: Date.now(), - }, - }); - const { getClaudeUsageExhaustion } = await import('./claude-usage.js'); - expect(getClaudeUsageExhaustion().exhausted).toBe(false); - }); -}); - -describe('getCodexUsageExhaustion', () => { - it('returns exhausted=false when no Codex accounts are configured', async () => { - getAllCodexAccountsMock.mockReturnValue([]); - const { getCodexUsageExhaustion } = - await import('./codex-usage-collector.js'); - expect(getCodexUsageExhaustion()).toEqual({ - exhausted: false, - nextResetAt: null, - }); - }); - - it('returns exhausted=false when an account has unknown usage (-1)', async () => { - getAllCodexAccountsMock.mockReturnValue([ - { - index: 0, - accountId: 'a', - planType: 'pro', - isActive: true, - isRateLimited: false, - cachedUsagePct: -1, - cachedUsageD7Pct: -1, - }, - ]); - const { getCodexUsageExhaustion } = - await import('./codex-usage-collector.js'); - expect(getCodexUsageExhaustion().exhausted).toBe(false); - }); - - it('returns exhausted=true with earliest recovery when all accounts are exhausted', async () => { - const reset0 = new Date('2026-04-28T02:00:00Z').toISOString(); - const reset1 = new Date('2026-04-28T05:00:00Z').toISOString(); - getAllCodexAccountsMock.mockReturnValue([ - { - index: 0, - accountId: 'a', - planType: 'pro', - isActive: true, - isRateLimited: false, - cachedUsagePct: 96, - cachedUsageD7Pct: 40, - resetAt: reset0, - }, - { - index: 1, - accountId: 'b', - planType: 'pro', - isActive: false, - isRateLimited: false, - cachedUsagePct: 30, - cachedUsageD7Pct: 99, - resetD7At: reset1, - }, - ]); - const { getCodexUsageExhaustion } = - await import('./codex-usage-collector.js'); - const info = getCodexUsageExhaustion(); - expect(info.exhausted).toBe(true); - expect(info.nextResetAt).toBe(reset0); - }); - - it('returns exhausted=false when at least one Codex account has headroom', async () => { - getAllCodexAccountsMock.mockReturnValue([ - { - index: 0, - accountId: 'a', - planType: 'pro', - isActive: true, - isRateLimited: false, - cachedUsagePct: 99, - cachedUsageD7Pct: 99, - }, - { - index: 1, - accountId: 'b', - planType: 'pro', - isActive: false, - isRateLimited: false, - cachedUsagePct: 50, - cachedUsageD7Pct: 80, - }, - ]); - const { getCodexUsageExhaustion } = - await import('./codex-usage-collector.js'); - expect(getCodexUsageExhaustion().exhausted).toBe(false); - }); - - it('treats rate-limited Codex accounts as exhausted', async () => { - getAllCodexAccountsMock.mockReturnValue([ - { - index: 0, - accountId: 'a', - planType: 'pro', - isActive: false, - isRateLimited: true, - cachedUsagePct: 10, - cachedUsageD7Pct: 10, - }, - ]); - const { getCodexUsageExhaustion } = - await import('./codex-usage-collector.js'); - expect(getCodexUsageExhaustion().exhausted).toBe(true); - }); -});