Files
EJClaw/src/streamed-output-evaluator.ts
Codex f579b4f406 fix: treat Claude safeguards refusals as failures, reset the session
Claude Code returns "API Error: <model>'s safeguards flagged this message"
as a success-typed result. EJClaw delivered it as the owner's answer,
handed it to reviewer/arbiter, resumed the refused session (refused again)
and carried the banner forward into the next task.

Now it is a `safety-refusal` trigger: the raw banner is suppressed, the
Claude session is cleared, the paired task closes as
`claude_safety_refusal`, the room gets a short Korean notice, and refusal
banners are never carried forward. Global memory gains a rule not to read
thinking blocks from session transcripts (the reasoning_extraction trigger
of the 2026-10-12 incident).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 13:17:22 +09:00

231 lines
6.4 KiB
TypeScript

import { getAgentOutputText, hasAgentOutputPayload } from './agent-output.js';
import {
classifyClaudeAuthError,
classifyCodexTrigger,
classifyClaudeTrigger,
detectClaudeProviderFailureMessage,
isClaudeAuthError,
isClaudeAuthExpiredMessage,
isClaudeOrgAccessDeniedMessage,
isClaudeSafetyRefusalMessage,
isClaudeUsageExhaustedMessage,
type AgentTriggerReason,
} from './agent-error-detection.js';
import type { AgentOutput } from './agent-runner.js';
import {
shouldRetryFreshCodexSessionOnAgentFailure,
shouldRetryFreshSessionOnAgentFailure,
} from './session-recovery.js';
export interface StreamedTriggerReason {
reason: AgentTriggerReason;
retryAfterMs?: number;
message?: string;
}
export interface StreamedOutputState {
sawOutput: boolean;
sawVisibleOutput: boolean;
sawSuccessNullResultWithoutOutput: boolean;
streamedTriggerReason?: StreamedTriggerReason;
retryableSessionFailureDetected?: boolean;
}
export interface EvaluateStreamedOutputOptions {
agentType: 'claude-code' | 'codex';
provider: string;
suppressClaudeAuthErrorOutput?: boolean;
trackSuccessNullResult?: boolean;
shortCircuitTriggeredErrors?: boolean;
}
export interface EvaluateStreamedOutputResult {
state: StreamedOutputState;
shouldForwardOutput: boolean;
newTrigger?: StreamedTriggerReason;
suppressedAuthError?: boolean;
suppressedRetryableSessionFailure?: boolean;
}
export function evaluateStreamedOutput(
output: AgentOutput,
state: StreamedOutputState,
options: EvaluateStreamedOutputOptions,
): EvaluateStreamedOutputResult {
const nextState: StreamedOutputState = { ...state };
const primaryAgent = primaryAgentFor(options);
const countsAsFinalOutput =
output.phase === undefined || output.phase === 'final';
const outputText = getAgentOutputText(output);
const retryableSessionFailure = suppressRetryableSessionFailure(
output,
state,
nextState,
primaryAgent,
);
if (retryableSessionFailure) return retryableSessionFailure;
if (
primaryAgent === 'claude' &&
output.status === 'success' &&
typeof outputText === 'string' &&
// A safeguards refusal is never a real answer, even after earlier output.
(!state.sawOutput || isClaudeSafetyRefusalMessage(outputText))
) {
const triggerReason = classifyClaudeSuccessTrigger(outputText);
if (triggerReason) {
const newTrigger = nextState.streamedTriggerReason
? undefined
: { reason: triggerReason, message: outputText };
nextState.streamedTriggerReason =
nextState.streamedTriggerReason ?? newTrigger;
return {
state: nextState,
shouldForwardOutput: false,
newTrigger,
};
}
if (
options.suppressClaudeAuthErrorOutput &&
isClaudeAuthError(outputText)
) {
return {
state: nextState,
shouldForwardOutput: false,
suppressedAuthError: true,
};
}
}
if (countsAsFinalOutput && hasAgentOutputPayload(output)) {
nextState.sawOutput = true;
} else if (
options.trackSuccessNullResult &&
primaryAgent === 'claude' &&
output.status === 'success' &&
!state.sawOutput
) {
nextState.sawSuccessNullResultWithoutOutput = true;
}
if (
primaryAgent === 'codex' &&
typeof output.error === 'string' &&
output.error.length > 0 &&
!nextState.sawOutput &&
!nextState.streamedTriggerReason
) {
const trigger = classifyCodexTrigger(output.error);
if (trigger.triggered) {
const newTrigger: StreamedTriggerReason = {
reason: trigger.reason,
message: output.error,
};
nextState.streamedTriggerReason = newTrigger;
return {
state: nextState,
shouldForwardOutput: !options.shortCircuitTriggeredErrors,
newTrigger,
};
}
}
if (
output.status === 'error' &&
!nextState.sawOutput &&
!nextState.streamedTriggerReason
) {
let newTrigger: StreamedTriggerReason | undefined;
if (primaryAgent === 'claude') {
const trigger = classifyClaudeTrigger(output.error);
if (trigger.shouldRetry) {
newTrigger = {
reason: trigger.reason,
retryAfterMs: trigger.retryAfterMs,
...(output.error ? { message: output.error } : {}),
};
}
} else if (primaryAgent === 'codex') {
const trigger = classifyCodexTrigger(output.error);
if (trigger.triggered) {
newTrigger = { reason: trigger.reason, message: output.error };
}
}
if (newTrigger) {
nextState.streamedTriggerReason = newTrigger;
return {
state: nextState,
shouldForwardOutput: !options.shortCircuitTriggeredErrors,
newTrigger,
};
}
}
return {
state: nextState,
shouldForwardOutput: true,
};
}
type PrimaryAgent = 'claude' | 'codex' | undefined;
function primaryAgentFor(options: EvaluateStreamedOutputOptions): PrimaryAgent {
if (options.agentType === 'claude-code' && options.provider === 'claude') {
return 'claude';
}
if (options.agentType === 'codex' && options.provider === 'codex') {
return 'codex';
}
return undefined;
}
function suppressRetryableSessionFailure(
output: AgentOutput,
state: StreamedOutputState,
nextState: StreamedOutputState,
primaryAgent: PrimaryAgent,
): EvaluateStreamedOutputResult | undefined {
if (state.sawOutput) return undefined;
const shouldSuppress =
primaryAgent === 'claude'
? shouldRetryFreshSessionOnAgentFailure(output)
: primaryAgent === 'codex'
? shouldRetryFreshCodexSessionOnAgentFailure(output)
: false;
if (!shouldSuppress) return undefined;
nextState.retryableSessionFailureDetected = true;
return {
state: nextState,
shouldForwardOutput: false,
suppressedRetryableSessionFailure: true,
};
}
function classifyClaudeSuccessTrigger(
outputText: string,
): AgentTriggerReason | undefined {
if (isClaudeSafetyRefusalMessage(outputText)) return 'safety-refusal';
if (isClaudeUsageExhaustedMessage(outputText)) return 'usage-exhausted';
const authClassification = classifyClaudeAuthError(outputText);
if (
isClaudeOrgAccessDeniedMessage(outputText) ||
authClassification.category === 'org-access-denied'
) {
return 'org-access-denied';
}
if (
isClaudeAuthExpiredMessage(outputText) ||
authClassification.category === 'auth-expired'
) {
return 'auth-expired';
}
return detectClaudeProviderFailureMessage(outputText) || undefined;
}