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

@@ -15,11 +15,8 @@ vi.mock('./logger.js', () => ({
vi.mock('./config.js', () => ({ DATA_DIR: '/tmp/ejclaw-usage-backoff-data' }));
const { getAllTokensMock } = vi.hoisted(() => ({ getAllTokensMock: vi.fn() }));
vi.mock('./token-rotation.js', () => ({
getAllTokens: getAllTokensMock,
getCurrentToken: () => null,
getConfiguredClaudeTokens: () => [],
vi.mock('./claude-oauth-token.js', () => ({
getCurrentClaudeToken: () => 'sk-ant-oat01-testTokenAAAAAAAA',
}));
const { readJsonFileMock } = vi.hoisted(() => ({ readJsonFileMock: vi.fn() }));
@@ -72,15 +69,6 @@ describe('parseRetryAfterMs', () => {
describe('Claude usage 429 Retry-After backoff', () => {
beforeEach(() => {
readJsonFileMock.mockReturnValue({}); // start from an empty disk cache
getAllTokensMock.mockReturnValue([
{
index: 0,
token: 'sk-ant-oat01-testTokenAAAAAAAA',
masked: 'sk-ant-oat01-test...AAAA',
isActive: true,
isRateLimited: false,
},
]);
});
it('records a cooldown on 429 and skips the next fetch within the window', async () => {
@@ -89,17 +77,17 @@ describe('Claude usage 429 Retry-After backoff', () => {
);
vi.stubGlobal('fetch', fetchMock);
const { fetchAllClaudeUsage } = await import('./claude-usage.js');
const { fetchClaudeUsage } = await import('./claude-usage.js');
const first = await fetchAllClaudeUsage();
const first = await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(first[0].usageRateLimited).toBe(true);
expect(first[0].usage).toBeNull();
expect(first!.usageRateLimited).toBe(true);
expect(first!.usage).toBeNull();
// Immediate re-call: cooldown gate must hold, no second network call.
const second = await fetchAllClaudeUsage();
const second = await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(second[0].usageRateLimited).toBe(true);
expect(second!.usageRateLimited).toBe(true);
});
it('falls back to the default cooldown when Retry-After is 0 (does not disable backoff)', async () => {
@@ -115,17 +103,17 @@ describe('Claude usage 429 Retry-After backoff', () => {
);
vi.stubGlobal('fetch', fetchMock);
const { fetchAllClaudeUsage } = await import('./claude-usage.js');
const { fetchClaudeUsage } = await import('./claude-usage.js');
await fetchAllClaudeUsage();
await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
// +70s: past the 60s throttle. With the bug (0 cooldown) this would refetch;
// with the default 5-min cooldown it must still be held.
vi.setSystemTime(Date.now() + 70_000);
const held = await fetchAllClaudeUsage();
const held = await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(held[0].usageRateLimited).toBe(true);
expect(held!.usageRateLimited).toBe(true);
});
it('keeps backing off after the 5-minute throttle elapses but before the cooldown closes', async () => {
@@ -142,21 +130,21 @@ describe('Claude usage 429 Retry-After backoff', () => {
.mockResolvedValueOnce(makeResponse(200));
vi.stubGlobal('fetch', fetchMock);
const { fetchAllClaudeUsage } = await import('./claude-usage.js');
const { fetchClaudeUsage } = await import('./claude-usage.js');
await fetchAllClaudeUsage();
await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
// +370s: past the 300s throttle, still inside the ~398s cooldown → no refetch.
vi.setSystemTime(Date.now() + 370_000);
const held = await fetchAllClaudeUsage();
const held = await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(held[0].usageRateLimited).toBe(true);
expect(held!.usageRateLimited).toBe(true);
// +400s total: cooldown window closed → poller is allowed to fetch again.
vi.setSystemTime(Date.now() + 30_000);
const resumed = await fetchAllClaudeUsage();
const resumed = await fetchClaudeUsage();
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(resumed[0].usageRateLimited).toBe(false);
expect(resumed!.usageRateLimited).toBe(false);
});
});