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>
This commit is contained in:
Codex
2026-10-12 13:17:22 +09:00
parent e22feb0215
commit f579b4f406
11 changed files with 240 additions and 13 deletions

View File

@@ -33,6 +33,12 @@ import {
/** completion_reason of a paired task closed because Claude ran out of usage. */
export const CLAUDE_USAGE_LIMIT_COMPLETION_REASON = 'claude_usage_limit';
/** completion_reason of a paired task closed because Claude's safeguards refused it. */
export const CLAUDE_SAFETY_REFUSAL_COMPLETION_REASON = 'claude_safety_refusal';
export const CLAUDE_SAFETY_REFUSAL_NOTICE =
'Claude 안전 필터가 이 요청을 차단해서 처리하지 못했습니다. 이 방의 Claude 세션은 초기화했습니다. 요청을 조금 바꿔서 다시 보내 주세요. 에러 원문은 붙이지 말아 주세요.';
function buildPromptWithCompactRefresh(args: {
prompt: string;
sessionFolder: string;
@@ -69,9 +75,10 @@ interface ClaudeUsageLimitFailure {
}
/**
* When a Claude run failed because the account is out of usage, close the
* paired task (nothing should retry against the exhausted account; the next
* user message starts a new task) and tell the room when Claude resets.
* When a Claude run failed because the account is out of usage, or because
* the model's safeguards refused the request, close the paired task (retrying
* cannot help; the next user message starts a new task) and tell the room.
* The refusal is never handed to the reviewer/arbiter as if it were an answer.
* Returns false for any other failure.
*/
async function reportClaudeUsageLimitToRoom(
@@ -87,23 +94,34 @@ async function reportClaudeUsageLimitToRoom(
log: { warn: (obj: Record<string, unknown>, msg: string) => void };
},
): Promise<boolean> {
if (!args.isClaudeCodeAgent || !isClaudeUsageLimitReason(args.reason)) {
if (!args.isClaudeCodeAgent) return false;
const isSafetyRefusal = args.reason === 'safety-refusal';
if (!isSafetyRefusal && !isClaudeUsageLimitReason(args.reason)) {
return false;
}
const notice = buildClaudeUsageLimitMessage(args.errorText);
const notice = isSafetyRefusal
? CLAUDE_SAFETY_REFUSAL_NOTICE
: buildClaudeUsageLimitMessage(args.errorText);
const task = args.pairedTaskId
? getPairedTaskById(args.pairedTaskId)
: undefined;
if (task) {
completePairedTaskWithReason(task, CLAUDE_USAGE_LIMIT_COMPLETION_REASON);
completePairedTaskWithReason(
task,
isSafetyRefusal
? CLAUDE_SAFETY_REFUSAL_COMPLETION_REASON
: CLAUDE_USAGE_LIMIT_COMPLETION_REASON,
);
}
args.log.warn(
{ reason: args.reason, role: args.activeRole, notice },
'Claude usage limit reached; told the room when it resets',
{ reason: args.reason, role: args.activeRole, errorText: args.errorText },
isSafetyRefusal
? 'Claude safeguards refused the request; closed the task and told the room'
: 'Claude usage limit reached; told the room when it resets',
);
await deliverNoticeAndFailExecution({
notice,
summary: args.errorText ?? notice,
summary: isSafetyRefusal ? notice : (args.errorText ?? notice),
pairedExecutionLifecycle: args.pairedExecutionLifecycle,
onOutput: args.onOutput,
});