Files
EJClaw/src/message-agent-executor-lifecycle.ts
Codex a9eab5cb94 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>
2026-10-01 20:14:38 +09:00

413 lines
12 KiB
TypeScript

import { getAgentOutputText } from './agent-output.js';
import {
retryClaudeAttemptOnTrigger,
runClaudeAttemptWithAuthRefresh,
type ClaudeTrigger,
} from './agent-attempt-orchestration.js';
import { isRetryableClaudeSessionFailureAttempt } from './agent-attempt-retry.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 {
shouldResetCodexSessionOnAgentFailure,
shouldResetSessionOnAgentFailure,
shouldRetryFreshCodexSessionOnAgentFailure,
} from './session-recovery.js';
import { getErrorMessage } from './utils.js';
type AttemptResult = 'success' | 'error';
function isRetryableCodexSessionFailureAttempt(args: {
provider: 'claude' | 'codex';
attempt: MessageAgentAttempt;
}): boolean {
const { provider, attempt } = args;
if (provider !== 'codex' || attempt.sawOutput) return false;
if (attempt.retryableSessionFailureDetected === true) return true;
if (attempt.output != null)
return shouldRetryFreshCodexSessionOnAgentFailure(attempt.output);
return attempt.error == null
? false
: shouldRetryFreshCodexSessionOnAgentFailure({
result: null,
error: getErrorMessage(attempt.error),
});
}
interface ExecuteMessageAgentAttemptLifecycleArgs {
provider: 'claude' | 'codex';
runAttempt: (provider: 'claude' | 'codex') => Promise<MessageAgentAttempt>;
isClaudeCodeAgent: boolean;
canRetryClaudeCredentials: boolean;
clearStoredSession: () => void;
clearRoleSdkSessions: () => void;
sessionFolder: string;
maybeHandoffToCodex: (
reason: AgentTriggerReason,
sawVisibleOutput: boolean,
) => boolean;
hasDirectTerminalDelivery: () => boolean;
pairedExecutionLifecycle: Pick<
PairedExecutionLifecycle,
'markStatus' | 'markSawOutput' | 'updateSummary' | 'getSummary'
>;
shouldRetryFreshSessionOnAgentFailure: (args: {
result: null;
error: string;
}) => boolean;
retryLogContext: {
chatJid: string;
group: string;
groupFolder: string;
runId: string;
};
log: {
warn: (obj: Record<string, unknown> | string, msg?: string) => void;
error: (obj: Record<string, unknown> | string, msg?: string) => void;
};
}
interface RecoveryResult {
attempt: MessageAgentAttempt;
resolved: AttemptResult | null;
}
class MessageAgentAttemptLifecycleRunner {
private resetSessionRequested = false;
constructor(private readonly args: ExecuteMessageAgentAttemptLifecycleArgs) {}
async execute(): Promise<AttemptResult> {
let primaryAttempt = await this.runTrackedAttempt(this.args.provider);
const recoveredSessionAttempt =
await this.recoverRetryableClaudeSessionFailure(primaryAttempt);
if (recoveredSessionAttempt.resolved) {
return recoveredSessionAttempt.resolved;
}
primaryAttempt = recoveredSessionAttempt.attempt;
const recoveredCodexSessionAttempt =
await this.recoverRetryableCodexSessionFailure(primaryAttempt);
if (recoveredCodexSessionAttempt.resolved) {
return recoveredCodexSessionAttempt.resolved;
}
primaryAttempt = recoveredCodexSessionAttempt.attempt;
if (primaryAttempt.error) {
return this.handlePrimaryAttemptFailure(
primaryAttempt,
getErrorMessage(primaryAttempt.error),
);
}
return this.finalizePrimaryAttempt(primaryAttempt);
}
private rememberAttempt(attempt: MessageAgentAttempt): MessageAgentAttempt {
if (attempt.resetSessionRequested) {
this.resetSessionRequested = true;
}
return attempt;
}
private async runTrackedAttempt(
currentProvider: 'claude' | 'codex',
): Promise<MessageAgentAttempt> {
return this.rememberAttempt(await this.args.runAttempt(currentProvider));
}
private async retryClaudeWithAuthRefresh(
initialTrigger: ClaudeTrigger,
): Promise<AttemptResult> {
return runClaudeAttemptWithAuthRefresh({
initialTrigger,
runAttempt: () => this.runTrackedAttempt('claude'),
logContext: this.args.retryLogContext,
onSuccess: ({ sawOutput }) => {
this.args.pairedExecutionLifecycle.markSawOutput(sawOutput);
},
});
}
private maybeHandoffAfterError(
reason: AgentTriggerReason,
attempt: MessageAgentAttempt,
): AttemptResult {
if (this.args.maybeHandoffToCodex(reason, attempt.sawVisibleOutput)) {
return 'success';
}
return 'error';
}
private async retryClaudeAttemptIfNeeded(
attempt: MessageAgentAttempt,
errorMessage?: string | null,
): Promise<AttemptResult | null> {
const retry = await retryClaudeAttemptOnTrigger({
provider: this.args.provider,
canRetryClaudeCredentials: this.args.canRetryClaudeCredentials,
attempt,
errorMessage,
runClaude: (trigger) => this.retryClaudeWithAuthRefresh(trigger),
});
if (retry.kind !== 'claude') {
return null;
}
if (retry.result === 'error') {
return this.maybeHandoffAfterError(retry.trigger.reason, attempt);
}
this.args.pairedExecutionLifecycle.markStatus('succeeded');
return retry.result;
}
private isRetryableClaudeSessionFailure(
attempt: MessageAgentAttempt,
): boolean {
return isRetryableClaudeSessionFailureAttempt({
attempt,
isClaudeCodeAgent: this.args.isClaudeCodeAgent,
provider: this.args.provider,
shouldRetryFreshSessionOnAgentFailure:
this.args.shouldRetryFreshSessionOnAgentFailure,
});
}
private async recoverRetryableClaudeSessionFailure(
attempt: MessageAgentAttempt,
): Promise<RecoveryResult> {
const { clearRoleSdkSessions, clearStoredSession, log } = this.args;
if (!this.isRetryableClaudeSessionFailure(attempt)) {
return { attempt, resolved: null };
}
clearStoredSession();
clearRoleSdkSessions();
log.warn(
'Cleared poisoned Claude session before visible output, retrying fresh session',
);
const freshAttempt = await this.runTrackedAttempt('claude');
if (!this.isRetryableClaudeSessionFailure(freshAttempt)) {
return { attempt: freshAttempt, resolved: null };
}
clearStoredSession();
log.warn('Fresh Claude retry also hit a retryable session failure');
log.error('Retryable Claude session failure persisted after fresh retry');
return {
attempt: freshAttempt,
resolved: this.maybeHandoffAfterError('session-failure', freshAttempt),
};
}
private async recoverRetryableCodexSessionFailure(
attempt: MessageAgentAttempt,
): Promise<RecoveryResult> {
const { clearRoleSdkSessions, clearStoredSession, log, provider } =
this.args;
if (!isRetryableCodexSessionFailureAttempt({ provider, attempt })) {
return { attempt, resolved: null };
}
clearStoredSession();
clearRoleSdkSessions();
log.warn(
'Cleared poisoned Codex session before visible output, retrying fresh session',
);
const freshAttempt = await this.runTrackedAttempt('codex');
if (
!isRetryableCodexSessionFailureAttempt({
provider,
attempt: freshAttempt,
})
) {
return { attempt: freshAttempt, resolved: null };
}
clearStoredSession();
log.warn('Fresh Codex retry also hit a retryable session failure');
log.error('Retryable Codex session failure persisted after fresh retry');
return {
attempt: freshAttempt,
resolved: 'error',
};
}
private async handlePrimaryAttemptFailure(
attempt: MessageAgentAttempt,
errorMessage: string,
): Promise<AttemptResult> {
const { log, provider } = this.args;
const claudeRetryResult = await this.retryClaudeAttemptIfNeeded(
attempt,
errorMessage,
);
if (claudeRetryResult) {
return claudeRetryResult;
}
if (attempt.error) {
log.error(
{
provider,
err: attempt.error,
},
'Agent error',
);
return 'error';
}
log.error(
{
provider,
error: attempt.output?.error,
},
'Agent process error',
);
return 'error';
}
private async finalizePrimaryAttempt(
attempt: MessageAgentAttempt,
): Promise<AttemptResult> {
const { log, provider } = this.args;
const output = attempt.output;
if (!output) {
log.error({ provider }, 'Agent produced no output object');
return 'error';
}
this.updateSummaryFromOutputIfNeeded(output);
if (
!attempt.sawOutput &&
!this.args.hasDirectTerminalDelivery() &&
output.status !== 'error'
) {
const claudeRetryResult = await this.retryClaudeAttemptIfNeeded(attempt);
if (claudeRetryResult) {
return claudeRetryResult;
}
}
this.clearPoisonedSessionAfterFailure(output);
if (output.status === 'error') {
return this.handlePrimaryAttemptFailure(
attempt,
output.error ?? 'Agent process error',
);
}
if (attempt.streamedTriggerReason) {
if (this.resolveStreamedTriggerHandoff(attempt)) {
return 'success';
}
log.error(
{
reason: attempt.streamedTriggerReason.reason,
},
'Agent trigger detected but could not be resolved',
);
return 'error';
}
if (this.isSuccessWithoutVisibleOutput(attempt)) {
log.error(
'Agent returned success with null result and no visible output',
);
return 'error';
}
this.args.pairedExecutionLifecycle.markStatus('succeeded');
this.args.pairedExecutionLifecycle.markSawOutput(
attempt.sawOutput || this.args.hasDirectTerminalDelivery(),
);
return 'success';
}
private updateSummaryFromOutputIfNeeded(
output: NonNullable<MessageAgentAttempt['output']>,
): void {
const { pairedExecutionLifecycle } = this.args;
if (pairedExecutionLifecycle.getSummary()) {
return;
}
const finalOutputText = getAgentOutputText(output);
pairedExecutionLifecycle.updateSummary({
outputText:
typeof finalOutputText === 'string' && finalOutputText.length > 0
? finalOutputText
: null,
errorText:
typeof output.error === 'string' && output.error.length > 0
? output.error
: null,
});
}
private clearPoisonedSessionAfterFailure(
output: NonNullable<MessageAgentAttempt['output']>,
): void {
const {
clearRoleSdkSessions,
clearStoredSession,
isClaudeCodeAgent,
log,
provider,
sessionFolder,
} = this.args;
if (
isClaudeCodeAgent &&
(this.resetSessionRequested || shouldResetSessionOnAgentFailure(output))
) {
clearStoredSession();
log.warn(
{ sessionFolder },
'Cleared poisoned agent session after unrecoverable error',
);
return;
}
if (
!isClaudeCodeAgent &&
provider === 'codex' &&
(this.resetSessionRequested ||
shouldResetCodexSessionOnAgentFailure(output))
) {
clearStoredSession();
clearRoleSdkSessions();
log.warn(
{ sessionFolder },
'Cleared poisoned Codex session after unrecoverable error',
);
}
}
private resolveStreamedTriggerHandoff(attempt: MessageAgentAttempt): boolean {
if (!attempt.streamedTriggerReason || !this.args.isClaudeCodeAgent) {
return false;
}
return this.args.maybeHandoffToCodex(
attempt.streamedTriggerReason.reason,
attempt.sawVisibleOutput,
);
}
private isSuccessWithoutVisibleOutput(attempt: MessageAgentAttempt): boolean {
return (
attempt.sawSuccessNullResultWithoutOutput &&
!attempt.sawOutput &&
!this.args.hasDirectTerminalDelivery()
);
}
}
export async function executeMessageAgentAttemptLifecycle(
args: ExecuteMessageAgentAttemptLifecycleArgs,
): Promise<AttemptResult> {
return new MessageAgentAttemptLifecycleRunner(args).execute();
}