fix: tell transient Claude refresh failures apart from a lost login

A 401-rejected token whose refresh failed with 429/5xx/timeout was marked
re-login-required at once, closing tasks and asking for a needless re-login.

- refreshToken throws TokenRefreshError; only 400/401/invalid_grant is
  definitive. A forced refresh of a rejected token retries transient
  failures twice right away; the periodic loop then keeps retrying the
  known-rejected token every check, and only a failure lasting 30 min turns
  into re-login-required.
- While recovering, the room is told once per incident and the paired task
  is kept, so the normal retry path can resume it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-11-02 10:55:31 +09:00
parent 3f2e364c73
commit ff76d041d3
4 changed files with 296 additions and 11 deletions

View File

@@ -36,6 +36,7 @@ import {
import { completePairedTaskWithReason } from './paired-task-status.js';
import {
forceRefreshToken,
getClaudeAuthRecoveryIncident,
isReloginRequired,
RELOGIN_REQUIRED_MESSAGE,
} from './token-refresh.js';
@@ -62,6 +63,14 @@ export const CLAUDE_SAFETY_REFUSAL_COMPLETION_REASON = 'claude_safety_refusal';
export const CLAUDE_RELOGIN_REQUIRED_COMPLETION_REASON =
'claude_relogin_required';
export const CLAUDE_AUTH_RECOVERING_NOTICE =
'⚠️ 클로드 인증 서버 오류로 토큰 갱신이 잠시 실패해서 이번 작업을 멈췄습니다. ' +
'봇이 5분마다 자동으로 다시 시도하고 있습니다. 잠시 후 이 방에 "계속"이라고 보내 주시면 이어서 진행합니다. ' +
'30분 넘게 복구되지 않으면 재로그인 안내를 드리겠습니다.';
// chatJid -> auth recovery incident already announced in that room.
const announcedAuthRecovery = new Map<string, number>();
export const CLAUDE_SAFETY_REFUSAL_NOTICE =
'Claude 안전 필터가 이 요청을 차단해서 처리하지 못했습니다. 이 방의 Claude 세션은 초기화했습니다. 요청을 조금 바꿔서 다시 보내 주세요. 에러 원문은 붙이지 말아 주세요.';
@@ -286,6 +295,7 @@ async function reportClaudeUsageLimitToRoom(
* owner/arbiter retries fail silently until someone asks why the bot stopped.
*/
async function reportClaudeReloginRequired(args: {
chatJid: string;
activeRole: PairedRoomRole;
pairedTaskId?: string;
errorText?: string | null;
@@ -297,7 +307,9 @@ async function reportClaudeReloginRequired(args: {
log: { warn: (obj: Record<string, unknown>, msg: string) => void };
}): Promise<boolean> {
if (!isReloginRequired()) await forceRefreshToken();
if (!isReloginRequired()) return false;
if (!isReloginRequired()) {
return reportClaudeAuthRecovering(args);
}
const task = args.pairedTaskId
? getPairedTaskById(args.pairedTaskId)
: undefined;
@@ -320,6 +332,40 @@ async function reportClaudeReloginRequired(args: {
return true;
}
/**
* The token was rejected but refreshing failed only transiently (429/5xx/
* timeout): the login may still be fine, so keep the task and let the
* automatic retries run. Tell the room once per incident why the work stopped;
* later failures in the same incident take the normal retry path.
*/
async function reportClaudeAuthRecovering(args: {
chatJid: string;
activeRole: PairedRoomRole;
errorText?: string | null;
pairedExecutionLifecycle: Pick<
PairedExecutionLifecycle,
'updateSummary' | 'completeImmediately'
>;
onOutput?: (output: AgentOutput) => Promise<void>;
log: { warn: (obj: Record<string, unknown>, msg: string) => void };
}): Promise<boolean> {
const incident = getClaudeAuthRecoveryIncident();
if (incident === null) return false;
if (announcedAuthRecovery.get(args.chatJid) === incident) return false;
announcedAuthRecovery.set(args.chatJid, incident);
args.log.warn(
{ role: args.activeRole, incident, errorText: args.errorText },
'Claude token refresh failing transiently; told the room and kept the task',
);
await deliverNoticeAndFailExecution({
notice: CLAUDE_AUTH_RECOVERING_NOTICE,
summary: CLAUDE_AUTH_RECOVERING_NOTICE,
pairedExecutionLifecycle: args.pairedExecutionLifecycle,
onOutput: args.onOutput,
});
return true;
}
/**
* Owner finalize without an agent run (see paired-finalize.ts): close the task
* and deliver the already-reviewed answer as the final message. Returns false