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

@@ -1,15 +1,11 @@
import { getAgentOutputText } from './agent-output.js';
import {
executeAttemptRetryAction,
runClaudeAttemptWithRotation,
runCodexAttemptWithRotation,
retryClaudeAttemptOnTrigger,
runClaudeAttemptWithAuthRefresh,
type ClaudeTrigger,
} from './agent-attempt-orchestration.js';
import { isRetryableClaudeSessionFailureAttempt } from './agent-attempt-retry.js';
import { getCodexAccountCount } from './codex-token-rotation.js';
import type {
AgentTriggerReason,
CodexRotationReason,
} from './agent-error-detection.js';
import type { AgentTriggerReason } from './agent-error-detection.js';
import type { PairedExecutionLifecycle } from './message-agent-executor-paired.js';
import type { MessageAgentAttempt } from './message-agent-executor-attempt-runner.js';
import {
@@ -59,7 +55,7 @@ interface ExecuteMessageAgentAttemptLifecycleArgs {
result: null;
error: string;
}) => boolean;
rotationLogContext: {
retryLogContext: {
chatJid: string;
group: string;
groupFolder: string;
@@ -120,30 +116,13 @@ class MessageAgentAttemptLifecycleRunner {
return this.rememberAttempt(await this.args.runAttempt(currentProvider));
}
private async retryCodexWithRotation(
initialTrigger: { reason: CodexRotationReason },
rotationMessage?: string,
private async retryClaudeWithAuthRefresh(
initialTrigger: ClaudeTrigger,
): Promise<AttemptResult> {
return runCodexAttemptWithRotation({
initialTrigger,
runAttempt: () => this.runTrackedAttempt('codex'),
logContext: this.args.rotationLogContext,
rotationMessage,
});
}
private async retryClaudeWithRotation(
initialTrigger: {
reason: AgentTriggerReason;
retryAfterMs?: number;
},
rotationMessage?: string,
): Promise<AttemptResult> {
return runClaudeAttemptWithRotation({
return runClaudeAttemptWithAuthRefresh({
initialTrigger,
runAttempt: () => this.runTrackedAttempt('claude'),
logContext: this.args.rotationLogContext,
rotationMessage,
logContext: this.args.retryLogContext,
onSuccess: ({ sawOutput }) => {
this.args.pairedExecutionLifecycle.markSawOutput(sawOutput);
},
@@ -162,54 +141,25 @@ class MessageAgentAttemptLifecycleRunner {
private async retryClaudeAttemptIfNeeded(
attempt: MessageAgentAttempt,
rotationMessage?: string | null,
errorMessage?: string | null,
): Promise<AttemptResult | null> {
const retryAction = await executeAttemptRetryAction({
const retry = await retryClaudeAttemptOnTrigger({
provider: this.args.provider,
canRetryClaudeCredentials: this.args.canRetryClaudeCredentials,
canRetryCodex: false,
attempt,
rotationMessage,
runClaude: (trigger, message) =>
this.retryClaudeWithRotation(trigger, message),
runCodex: (trigger, message) =>
this.retryCodexWithRotation(trigger, message),
errorMessage,
runClaude: (trigger) => this.retryClaudeWithAuthRefresh(trigger),
});
if (retryAction.kind !== 'claude') {
if (retry.kind !== 'claude') {
return null;
}
if (retryAction.result === 'error') {
return this.maybeHandoffAfterError(retryAction.trigger.reason, attempt);
if (retry.result === 'error') {
return this.maybeHandoffAfterError(retry.trigger.reason, attempt);
}
this.args.pairedExecutionLifecycle.markStatus('succeeded');
return retryAction.result;
}
private async retryCodexAttemptIfNeeded(
attempt: MessageAgentAttempt,
rotationMessage?: string | null,
): Promise<AttemptResult | null> {
const retryAction = await executeAttemptRetryAction({
provider: this.args.provider,
canRetryClaudeCredentials: false,
canRetryCodex: !this.args.isClaudeCodeAgent && getCodexAccountCount() > 1,
attempt,
rotationMessage,
runClaude: (trigger, message) =>
this.retryClaudeWithRotation(trigger, message),
runCodex: (trigger, message) =>
this.retryCodexWithRotation(trigger, message),
});
if (retryAction.kind !== 'codex') {
return null;
}
if (retryAction.result === 'success') {
this.args.pairedExecutionLifecycle.markStatus('succeeded');
}
return retryAction.result;
return retry.result;
}
private isRetryableClaudeSessionFailure(
@@ -288,25 +238,17 @@ class MessageAgentAttemptLifecycleRunner {
private async handlePrimaryAttemptFailure(
attempt: MessageAgentAttempt,
rotationMessage: string,
errorMessage: string,
): Promise<AttemptResult> {
const { log, provider } = this.args;
const claudeRetryResult = await this.retryClaudeAttemptIfNeeded(
attempt,
rotationMessage,
errorMessage,
);
if (claudeRetryResult) {
return claudeRetryResult;
}
const codexRetryResult = await this.retryCodexAttemptIfNeeded(
attempt,
rotationMessage,
);
if (codexRetryResult) {
return codexRetryResult;
}
if (attempt.error) {
log.error(
{
@@ -360,14 +302,6 @@ class MessageAgentAttemptLifecycleRunner {
);
}
const codexRetryResult = await this.retryCodexAttemptIfNeeded(
attempt,
output.error ?? output.result,
);
if (codexRetryResult) {
return codexRetryResult;
}
if (attempt.streamedTriggerReason) {
if (this.resolveStreamedTriggerHandoff(attempt)) {
return 'success';