refactor: run Claude and Codex on one fixed account each

The deployment has a single Claude OAuth credential and a single Codex
ChatGPT account, so the N-account rotation machinery only added state
files, cooldown bookkeeping and code paths that never ran.

Removed:
- Claude token rotation (token-rotation.ts, token-rotation-base.ts):
  rotate-on-limit, per-token cooldowns, token-rotation-state.json.
  CLAUDE_CODE_OAUTH_TOKENS is still read, but only its first entry.
- Codex account rotation (codex-token-rotation.ts): account pool,
  rotate-on-429/auth, dead-auth/cooldown tracking, round-robin advance,
  d7 auto-rotate, codex-rotation-state.json, manual account switch.
- Account index plumbing in token-refresh, claude-credentials-path
  (CLAUDE_ACCOUNTS_DIR / ~/.claude-accounts/<n>), Claude/Codex usage
  collectors, the dashboard rows and the Codex warm-up.
- provider-retry.ts rotation loops and the Codex retry path.

Kept:
- Codex exclusive auth lease (one Codex run at a time), the 15-minute
  waitForCodexAuthLease, and a usage-poll lease that skips while a run
  holds the account (codex-auth.ts).
- Session auth copy and write-back of refreshed tokens
  (syncCodexSessionAuthBack, syncClaudeSessionAuthBack).
- Codex auth path: ~/.codex-accounts/0/auth.json, else ~/.codex/auth.json
  (now resolved per run, so a re-login needs no restart).
- Claude token refresh loop, CLAUDE_CREDENTIALS_PATH and the
  CLAUDE_TOKEN_REFRESH_* / CLAUDE_USAGE_USE_HOME_CREDENTIALS flags; an
  auth-expired run still force-refreshes the token and retries once.
- Error classification (classifyClaudeTrigger, classifyCodexTrigger).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-01 20:00:46 +09:00
parent f127ac7a2b
commit a9eab5cb94
58 changed files with 1691 additions and 5547 deletions

View File

@@ -17,15 +17,11 @@ import {
} from './config.js';
import { getGlobalFailoverInfo } from './service-routing.js';
import {
fetchAllClaudeUsage,
fetchAllClaudeProfiles,
type ClaudeAccountUsage,
fetchClaudeProfile,
fetchClaudeUsage,
type ClaudeUsageSnapshot,
} from './claude-usage.js';
import {
CODEX_FULL_SCAN_INTERVAL,
refreshActiveCodexUsage,
refreshAllCodexAccountUsage,
} from './codex-usage-collector.js';
import { refreshCodexUsage } from './codex-usage-collector.js';
import {
composeDashboardContent,
formatElapsed,
@@ -40,7 +36,7 @@ import {
import {
buildClaudeUsageRows,
extractCodexUsageRows,
mergeClaudeDashboardAccounts,
mergeClaudeDashboardUsage,
type UsageRow,
} from './dashboard-usage-rows.js';
import { getAllChats, getAllTasks } from './db.js';
@@ -100,7 +96,7 @@ const DASHBOARD_DUPLICATE_CLEANUP_POLL_MS = 300_000;
let statusMessageId: string | null = null;
let cachedUsageContent = '';
let cachedClaudeAccounts: ClaudeAccountUsage[] = [];
let cachedClaudeUsage: ClaudeUsageSnapshot | null = null;
let usageUpdateInProgress = false;
let channelMetaCache = new Map<string, ChannelMeta>();
let channelMetaLastRefresh = 0;
@@ -269,13 +265,13 @@ export function formatRoomName(
export function buildWebUsageRowsForSnapshot(args: {
serviceAgentType: AgentType;
claudeAccounts: ClaudeAccountUsage[];
claudeUsage: ClaudeUsageSnapshot | null;
codexRows: UsageRow[];
}): UsageRow[] {
const rows: UsageRow[] = [];
if (args.serviceAgentType === 'claude-code') {
rows.push(...buildClaudeUsageRows(args.claudeAccounts));
rows.push(...buildClaudeUsageRows(args.claudeUsage));
}
rows.push(...args.codexRows);
@@ -288,7 +284,7 @@ function buildUsageSnapshotRows(opts: UnifiedDashboardOptions): {
} {
const rows = buildWebUsageRowsForSnapshot({
serviceAgentType: opts.serviceAgentType,
claudeAccounts: cachedClaudeAccounts,
claudeUsage: cachedClaudeUsage,
codexRows: cachedCodexUsageRows,
});
@@ -603,11 +599,11 @@ export function renderUsageTable(
async function buildUsageContent(): Promise<string> {
const shouldFetchClaudeUsage = USAGE_DASHBOARD_ENABLED;
let liveClaudeAccounts: ClaudeAccountUsage[] | null = null;
let liveClaudeUsage: ClaudeUsageSnapshot | null = null;
if (shouldFetchClaudeUsage) {
try {
liveClaudeAccounts = await fetchAllClaudeUsage();
liveClaudeUsage = await fetchClaudeUsage();
} catch (err) {
logger.warn({ err }, 'Failed to fetch Claude usage for dashboard');
}
@@ -623,11 +619,11 @@ async function buildUsageContent(): Promise<string> {
const claudeBotRows: UsageRow[] = [];
if (shouldFetchClaudeUsage) {
cachedClaudeAccounts = mergeClaudeDashboardAccounts(
liveClaudeAccounts,
cachedClaudeAccounts,
cachedClaudeUsage = mergeClaudeDashboardUsage(
liveClaudeUsage,
cachedClaudeUsage,
);
claudeBotRows.push(...buildClaudeUsageRows(cachedClaudeAccounts));
claudeBotRows.push(...buildClaudeUsageRows(cachedClaudeUsage));
}
// Group 2: Codex bot — use in-process cache (unified service)
@@ -903,7 +899,7 @@ export async function startUnifiedDashboard(
}
if (isRenderer) {
await fetchAllClaudeProfiles();
await fetchClaudeProfile();
await refreshUsageCache();
}
@@ -1058,20 +1054,13 @@ export async function startUnifiedDashboard(
cachedCodexUsageRows = result.rows;
if (result.fetchedAt) codexUsageFetchedAt = result.fetchedAt;
};
void refreshAllCodexAccountUsage().then((r) => {
applyCodexRefresh(r);
return refreshActiveCodexUsage().then(applyCodexRefresh);
});
void refreshCodexUsage().then(applyCodexRefresh);
// Each poll spawns a Codex app-server; every 5 minutes is plenty for the
// dashboard (it used to run every minute, ~1,440 spawns a day).
setInterval(
() => void refreshActiveCodexUsage().then(applyCodexRefresh),
() => void refreshCodexUsage().then(applyCodexRefresh),
Math.max(opts.usageUpdateInterval, CODEX_ACTIVE_USAGE_POLL_MS),
);
setInterval(
() => void refreshAllCodexAccountUsage().then(applyCodexRefresh),
CODEX_FULL_SCAN_INTERVAL,
);
logger.info(
{