diff --git a/src/agent-error-detection.test.ts b/src/agent-error-detection.test.ts index b01939c..37a0c46 100644 --- a/src/agent-error-detection.test.ts +++ b/src/agent-error-detection.test.ts @@ -7,6 +7,7 @@ import { detectClaudeProviderFailureMessage, isClaudeOrgAccessDeniedMessage, isClaudeSafetyRefusalMessage, + isClaudeUsageExhaustedMessage, } from './agent-error-detection.js'; describe('agent-error-detection', () => { @@ -126,4 +127,34 @@ describe('agent-error-detection', () => { retryAfterMs: undefined, }); }); + + it('detects Claude usage-limit banners, including ones that name the limit', () => { + for (const banner of [ + "You've hit your limit · resets 9:40pm (Asia/Seoul)", + "You've hit your session limit · resets 3:20am (Asia/Seoul)", + 'You\u2019ve hit your weekly limit · resets Oct 20, 9am (Asia/Seoul)', + 'You have hit your Opus limit · resets 5pm', + ]) { + expect(isClaudeUsageExhaustedMessage(banner)).toBe(true); + expect(classifyAgentError(banner)).toMatchObject({ reason: '429' }); + } + expect( + isClaudeUsageExhaustedMessage( + "You're out of extra usage · resets 5pm (Asia/Seoul)", + ), + ).toBe(true); + }); + + it('does not treat an answer that quotes the usage-limit banner as one', () => { + expect( + isClaudeUsageExhaustedMessage( + `TASK_DONE\n원인은 "You've hit your session limit · resets 3:20am" 입니다.`, + ), + ).toBe(false); + expect( + isClaudeUsageExhaustedMessage( + "You've hit your limit with this approach.", + ), + ).toBe(false); + }); }); diff --git a/src/agent-error-detection.ts b/src/agent-error-detection.ts index fb6ca1c..0f4bf53 100644 --- a/src/agent-error-detection.ts +++ b/src/agent-error-detection.ts @@ -7,6 +7,15 @@ // ── Banner / text detection ───────────────────────────────────── +/** + * "hit your limit" and the newer variants that name the limit, e.g. + * "You've hit your session limit" / "You've hit your weekly limit". + */ +const CLAUDE_HIT_LIMIT_PATTERN = /hit your (?:[a-z0-9-]+ ){0,2}limit\b/; +const CLAUDE_HIT_LIMIT_BANNER = new RegExp( + `^you(?:'ve| have) ${CLAUDE_HIT_LIMIT_PATTERN.source}`, +); + export function isClaudeAuthError(text: string): boolean { const lower = text.toLowerCase(); return ( @@ -25,8 +34,7 @@ export function isClaudeUsageExhaustedMessage(text: string): boolean { const looksLikeBanner = normalized.startsWith("you're out of extra usage") || normalized.startsWith('you are out of extra usage') || - normalized.startsWith("you've hit your limit") || - normalized.startsWith('you have hit your limit'); + CLAUDE_HIT_LIMIT_BANNER.test(normalized); const hasResetHint = normalized.includes('resets ') || normalized.includes('reset at ') || @@ -297,7 +305,7 @@ export function classifyAgentError( lower.includes('usage limit') || lower.includes('out of credits') || lower.includes('workspace out of credits') || - lower.includes('hit your limit') || + CLAUDE_HIT_LIMIT_PATTERN.test(lower) || lower.includes('too many requests') || lower.includes('rate_limit') ) { diff --git a/src/message-agent-executor.test.ts b/src/message-agent-executor.test.ts index 4ba8b64..3c890fe 100644 --- a/src/message-agent-executor.test.ts +++ b/src/message-agent-executor.test.ts @@ -3510,6 +3510,45 @@ describe('runAgentForGroup Claude failure recovery', () => { ]); }); + it('pauses on the newer "session limit" banner instead of delivering it as an answer', async () => { + const outputs: string[] = []; + + vi.mocked(agentRunner.runAgentProcess).mockImplementationOnce( + async (_group, _input, _onProcess, onOutput) => { + await onOutput?.({ + status: 'success', + phase: 'final', + result: "You've hit your session limit · resets 3:20am (Asia/Seoul)", + }); + return { + status: 'success', + result: null, + }; + }, + ); + + const result = await runAgentForGroup(makeDeps(), { + group: makeGroup(), + prompt: 'hello', + chatJid: 'group@test', + runId: 'run-session-limit-banner', + onOutput: async (output) => { + const text = + output.output && 'text' in output.output + ? output.output.text + : output.result; + if (typeof text === 'string') outputs.push(text); + }, + }); + + expect(result).toBe('success'); + expect(outputs).toEqual([ + expect.stringMatching( + /^Claude 사용 한도에 도달해서 이 방의 작업을 일시정지했습니다\. .*03:20에/, + ), + ]); + }); + it('does not retry when Claude org access is denied', async () => { const outputs: string[] = []; diff --git a/src/paired-execution-context-reviewer.ts b/src/paired-execution-context-reviewer.ts index 7548184..c6641c5 100644 --- a/src/paired-execution-context-reviewer.ts +++ b/src/paired-execution-context-reviewer.ts @@ -133,6 +133,7 @@ export function handleFailedReviewerExecution(args: { 'Reviewer failed repeatedly without a visible verdict — requesting arbiter', escalateLogMessage: 'Reviewer failed repeatedly without a visible verdict — escalating to user', + arbiterInterventionCount: task.arbiter_intervention_count ?? 0, logContext: { taskId, role: 'reviewer', diff --git a/src/paired-execution-context.test.ts b/src/paired-execution-context.test.ts index 104602b..be2c9c5 100644 --- a/src/paired-execution-context.test.ts +++ b/src/paired-execution-context.test.ts @@ -1556,6 +1556,38 @@ describe('paired execution context', () => { ); }); + it('escalates instead of re-requesting the arbiter after repeated reviewer failures once the arbiter budget is spent', () => { + vi.spyOn(config, 'isArbiterEnabled').mockReturnValue(true); + vi.mocked(db.getPairedTaskById).mockReturnValue( + buildPairedTask({ + status: 'review_ready', + reviewer_failure_count: 1, + arbiter_intervention_count: config.ARBITER_MAX_INTERVENTIONS, + }), + ); + + completePairedExecutionContext({ + taskId: 'task-1', + role: 'reviewer', + status: 'failed', + summary: 'runtime exploded before verdict', + }); + + expect(db.updatePairedTask).toHaveBeenCalledWith( + 'task-1', + expect.objectContaining({ + status: 'completed', + reviewer_failure_count: 2, + arbiter_verdict: 'escalate', + completion_reason: 'escalated', + }), + ); + expect(db.updatePairedTask).not.toHaveBeenCalledWith( + 'task-1', + expect.objectContaining({ status: 'arbiter_requested' }), + ); + }); + it('keeps arbiter tasks arbiter_requested when arbiter execution fails without a terminal verdict', () => { vi.mocked(db.getPairedTaskById).mockReturnValue( buildPairedTask({ diff --git a/src/paired-verdict.test.ts b/src/paired-verdict.test.ts index 715d660..fceb471 100644 --- a/src/paired-verdict.test.ts +++ b/src/paired-verdict.test.ts @@ -146,4 +146,16 @@ describe('paired verdict parser', () => { expect(classifyArbiterVerdict('CONTINUE\nsame as proceed')).toBe('proceed'); expect(classifyArbiterVerdict('no verdict here')).toBe('unknown'); }); + + it('only reads a verdict keyword that leads its line', () => { + expect( + classifyArbiterVerdict( + "You've hit your session limit · resets 3:20am (Asia/Seoul)", + ), + ).toBe('unknown'); + expect(classifyArbiterVerdict('The owner should proceed.')).toBe('unknown'); + expect(classifyArbiterVerdict('RESETS are pending')).toBe('unknown'); + expect(classifyArbiterVerdict('**PROCEED**\n근거')).toBe('proceed'); + expect(classifyArbiterVerdict('VERDICT: ESCALATE')).toBe('escalate'); + }); }); diff --git a/src/paired-verdict.ts b/src/paired-verdict.ts index 3d4e17b..fd59618 100644 --- a/src/paired-verdict.ts +++ b/src/paired-verdict.ts @@ -137,8 +137,11 @@ export function classifyArbiterVerdict( cleaned, ARBITER_VERDICT_SCAN_LINE_LIMIT, )) { + // The keyword must lead the line as a whole word: an unanchored, + // case-insensitive match read "resets 3:20am" in a Claude usage-limit + // banner as RESET. const verdictMatch = line.match( - /\*{0,2}(?:VERDICT\s*[:—-]\s*)?(PROCEED|REVISE|RESET|ESCALATE|CONTINUE)\*{0,2}/i, + /^\*{0,2}(?:VERDICT\s*[:—-]\s*)?(PROCEED|REVISE|RESET|ESCALATE|CONTINUE)\b/, ); if (verdictMatch) { const normalized = verdictMatch[1].toLowerCase();