refactor: replace the Claude→Codex failover with a usage-limit notice
When Claude hit a limit the run was handed to a Codex service (codex-main/codex-review) through service_handoffs and a global owner failover. That service does not exist in this deployment, so handoffs stayed pending forever (2 rows since May) and the user got no answer. Removed: - handoff creation (message-agent-executor-handoff, paired-turn-fallback) and the delegated paired-execution path - the pending-handoff poll/claim worker (message-runtime-handoffs) and the service_handoffs CRUD module (db/service-handoffs, db exports) - global owner failover in service-routing, its recovery timer in index, the dashboard failover line, the codex-review failover prompt pack and prompts/codex-review-failover-platform.md - failoverOwner in RoomRoleContext and the `failover=` ROOM_ROLE field - OWNER_/REVIEWER_/ARBITER_FALLBACK_ENABLED and FAILOVER_MIN_DURATION_MS - tests for the removed behaviour Now, when a Claude run fails with a usage/rate limit (trigger reason 429 or usage-exhausted) the executor closes the open paired task as completed/claude_usage_limit and sends the room one final notice: "Claude 사용 한도에 도달했습니다. 21:40에 재개할 수 있습니다." The reset time comes from the error text (CLI banner "resets 9:40pm (Asia/Seoul)", dated banners, "try again at ...", ISO timestamps), else from the cached usage API window at 100%, else "잠시 후 다시 시도해 주세요". Nothing retries silently; the next user message starts a new task. Other Claude failures (session failure, org access denied) now end as ordinary run errors. The service_handoffs table, its columns on paired_turn_attempts and the migrations stay untouched for the later migration squash; the runtime no longer reads or writes them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,7 +25,6 @@ vi.mock('./config.js', () => ({
|
||||
getRoleModelConfig: vi.fn(() => ({
|
||||
model: undefined,
|
||||
effort: undefined,
|
||||
fallbackEnabled: true,
|
||||
})),
|
||||
TIMEZONE: 'Asia/Seoul',
|
||||
}));
|
||||
@@ -51,13 +50,13 @@ vi.mock('./db.js', () => {
|
||||
|
||||
return {
|
||||
completePairedTurn: vi.fn(),
|
||||
createServiceHandoff: vi.fn(),
|
||||
failPairedTurn: vi.fn(),
|
||||
getAllTasks: vi.fn(() => []),
|
||||
getLastHumanMessageSender: vi.fn(() => null),
|
||||
getLatestOpenPairedTaskForChat: vi.fn(() => undefined),
|
||||
getLatestTurnNumber: vi.fn(() => 0),
|
||||
getPairedTaskById: vi.fn(() => undefined),
|
||||
updatePairedTaskIfUnchanged: vi.fn(() => true),
|
||||
getPairedTurnAttempts: vi.fn(() => []),
|
||||
getPairedTurnOutputs: vi.fn(() => []),
|
||||
insertPairedTurnOutput: vi.fn(),
|
||||
@@ -96,8 +95,6 @@ vi.mock('./db.js', () => {
|
||||
});
|
||||
|
||||
vi.mock('./service-routing.js', () => ({
|
||||
activateCodexFailover: vi.fn(),
|
||||
clearGlobalFailover: vi.fn(),
|
||||
getEffectiveChannelLease: vi.fn(() => ({
|
||||
chat_jid: 'group@test',
|
||||
owner_agent_type: 'claude-code',
|
||||
@@ -119,16 +116,11 @@ vi.mock('./service-routing.js', () => ({
|
||||
owner_agent_type?: 'claude-code' | 'codex';
|
||||
reviewer_agent_type?: 'claude-code' | 'codex' | null;
|
||||
arbiter_agent_type?: 'claude-code' | 'codex' | null;
|
||||
owner_failover_active?: boolean;
|
||||
},
|
||||
role: 'owner' | 'reviewer' | 'arbiter',
|
||||
) => {
|
||||
if (role === 'owner') {
|
||||
return lease.owner_failover_active
|
||||
? lease.owner_service_id
|
||||
: lease.owner_agent_type === 'codex'
|
||||
? 'codex-main'
|
||||
: 'claude';
|
||||
return lease.owner_agent_type === 'codex' ? 'codex-main' : 'claude';
|
||||
}
|
||||
if (role === 'reviewer') {
|
||||
if (lease.reviewer_agent_type === 'codex') {
|
||||
@@ -381,7 +373,6 @@ describe('runAgentForGroup room memory', () => {
|
||||
role: 'owner',
|
||||
ownerServiceId: 'claude',
|
||||
reviewerServiceId: 'codex-review',
|
||||
failoverOwner: false,
|
||||
}),
|
||||
}),
|
||||
expect.any(Function),
|
||||
@@ -471,7 +462,6 @@ describe('runAgentForGroup room memory', () => {
|
||||
role: 'reviewer',
|
||||
ownerServiceId: 'claude',
|
||||
reviewerServiceId: 'claude',
|
||||
failoverOwner: false,
|
||||
}),
|
||||
}),
|
||||
expect.any(Function),
|
||||
@@ -529,7 +519,6 @@ describe('runAgentForGroup room memory', () => {
|
||||
role: 'reviewer',
|
||||
ownerServiceId: 'codex-main',
|
||||
reviewerServiceId: 'claude',
|
||||
failoverOwner: false,
|
||||
}),
|
||||
}),
|
||||
expect.any(Function),
|
||||
@@ -538,7 +527,7 @@ describe('runAgentForGroup room memory', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('honors a forced agent type for reviewer failover handoffs', async () => {
|
||||
it('honors a forced agent type for reviewer turns', async () => {
|
||||
const group: RegisteredGroup = {
|
||||
...makeGroup(),
|
||||
agentType: 'codex',
|
||||
@@ -637,7 +626,6 @@ describe('runAgentForGroup room memory', () => {
|
||||
vi.mocked(config.getRoleModelConfig).mockReturnValue({
|
||||
model: 'claude-opus-4-8',
|
||||
effort: 'high',
|
||||
fallbackEnabled: true,
|
||||
});
|
||||
|
||||
await runAgentForGroup(makeDeps(), {
|
||||
@@ -2934,7 +2922,6 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
expect(result).toBe('error');
|
||||
expect(agentRunner.runAgentProcess).toHaveBeenCalledTimes(1);
|
||||
expect(outputs).toEqual([]);
|
||||
expect(db.createServiceHandoff).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('clears the Claude session and retries fresh when a retryable thinking 400 is surfaced as text', async () => {
|
||||
@@ -2989,7 +2976,7 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
expect(outputs).toEqual(['fresh Claude retry success']);
|
||||
});
|
||||
|
||||
it('hands off to codex when the fresh Claude retry also hits the same retryable thinking 400', async () => {
|
||||
it('returns error when the fresh Claude retry also hits the same retryable thinking 400', async () => {
|
||||
const outputs: string[] = [];
|
||||
const deps = makeDeps();
|
||||
|
||||
@@ -3033,28 +3020,13 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toBe('success');
|
||||
expect(result).toBe('error');
|
||||
expect(agentRunner.runAgentProcess).toHaveBeenCalledTimes(2);
|
||||
expect(deps.clearSession).toHaveBeenCalledTimes(2);
|
||||
expect(outputs).toEqual([]);
|
||||
expect(serviceRouting.activateCodexFailover).toHaveBeenCalledWith(
|
||||
'group@test',
|
||||
'claude-session-failure',
|
||||
);
|
||||
expect(db.createServiceHandoff).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
chat_jid: 'group@test',
|
||||
source_role: 'owner',
|
||||
target_role: 'owner',
|
||||
source_agent_type: 'claude-code',
|
||||
target_agent_type: 'codex',
|
||||
reason: 'claude-session-failure',
|
||||
intended_role: 'owner',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not retry when Claude is usage-exhausted', async () => {
|
||||
it('tells the room when Claude resets instead of retrying on usage exhaustion', async () => {
|
||||
const outputs: string[] = [];
|
||||
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementationOnce(
|
||||
@@ -3080,33 +3052,100 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
startSeq: 10,
|
||||
endSeq: 12,
|
||||
onOutput: async (output) => {
|
||||
if (typeof output.result === 'string') outputs.push(output.result);
|
||||
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(agentRunner.runAgentProcess).toHaveBeenCalledTimes(1);
|
||||
expect(outputs).toEqual([]);
|
||||
expect(serviceRouting.activateCodexFailover).toHaveBeenCalledWith(
|
||||
'group@test',
|
||||
'claude-usage-exhausted',
|
||||
);
|
||||
expect(db.createServiceHandoff).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
chat_jid: 'group@test',
|
||||
source_role: 'owner',
|
||||
source_agent_type: 'claude-code',
|
||||
target_role: 'owner',
|
||||
target_agent_type: 'codex',
|
||||
start_seq: 10,
|
||||
end_seq: 12,
|
||||
reason: 'claude-usage-exhausted',
|
||||
intended_role: 'owner',
|
||||
}),
|
||||
expect(outputs).toHaveLength(1);
|
||||
expect(outputs[0]).toMatch(
|
||||
/^Claude 사용 한도에 도달했습니다\. (\d+월 \d+일 )?04:00에 재개할 수 있습니다\.$/,
|
||||
);
|
||||
});
|
||||
|
||||
it('hands off to codex after repeated retryable Claude session failures', async () => {
|
||||
it('closes the paired task when the Claude owner hits its usage limit', async () => {
|
||||
const outputs: string[] = [];
|
||||
const pairedTask = {
|
||||
id: 'paired-task-usage-limit',
|
||||
chat_jid: 'group@test',
|
||||
group_folder: 'test-claude',
|
||||
owner_service_id: 'claude',
|
||||
reviewer_service_id: 'codex-review',
|
||||
title: null,
|
||||
source_ref: 'HEAD',
|
||||
plan_notes: null,
|
||||
round_trip_count: 0,
|
||||
review_requested_at: null,
|
||||
status: 'active' as const,
|
||||
arbiter_verdict: null,
|
||||
arbiter_requested_at: null,
|
||||
completion_reason: null,
|
||||
created_at: '2026-04-06T00:00:00.000Z',
|
||||
updated_at: '2026-04-06T00:00:00.000Z',
|
||||
};
|
||||
vi.mocked(
|
||||
pairedExecutionContext.preparePairedExecutionContext,
|
||||
).mockReturnValue({ task: pairedTask, workspace: null, envOverrides: {} });
|
||||
vi.mocked(db.getPairedTaskById).mockReturnValue(pairedTask);
|
||||
vi.mocked(db.updatePairedTaskIfUnchanged).mockReturnValue(true);
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementationOnce(
|
||||
async (_group, _input, _onProcess, onOutput) => {
|
||||
await onOutput?.({
|
||||
status: 'error',
|
||||
result: null,
|
||||
error: 'API Error: 429 rate_limit_error',
|
||||
});
|
||||
return {
|
||||
status: 'error',
|
||||
result: null,
|
||||
error: 'API Error: 429 rate_limit_error',
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
const result = await runAgentForGroup(makeDeps(), {
|
||||
group: makeGroup(),
|
||||
prompt: 'hello',
|
||||
chatJid: 'group@test',
|
||||
runId: 'run-paired-usage-limit',
|
||||
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(agentRunner.runAgentProcess).toHaveBeenCalledTimes(1);
|
||||
expect(db.updatePairedTaskIfUnchanged).toHaveBeenCalledWith(
|
||||
'paired-task-usage-limit',
|
||||
'2026-04-06T00:00:00.000Z',
|
||||
expect.objectContaining({
|
||||
status: 'completed',
|
||||
completion_reason: 'claude_usage_limit',
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
pairedExecutionContext.completePairedExecutionContext,
|
||||
).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
taskId: 'paired-task-usage-limit',
|
||||
status: 'failed',
|
||||
}),
|
||||
);
|
||||
expect(outputs).toEqual([
|
||||
expect.stringMatching(/^Claude 사용 한도에 도달했습니다\. /),
|
||||
]);
|
||||
});
|
||||
|
||||
it('returns error after repeated retryable Claude session failures', async () => {
|
||||
vi.mocked(
|
||||
sessionRecovery.shouldRetryFreshSessionOnAgentFailure,
|
||||
).mockReturnValue(true);
|
||||
@@ -3135,129 +3174,9 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
onOutput: async () => {},
|
||||
});
|
||||
|
||||
expect(result).toBe('success');
|
||||
expect(result).toBe('error');
|
||||
expect(agentRunner.runAgentProcess).toHaveBeenCalledTimes(2);
|
||||
expect(deps.clearSession).toHaveBeenCalledTimes(2);
|
||||
expect(serviceRouting.activateCodexFailover).toHaveBeenCalledWith(
|
||||
'group@test',
|
||||
'claude-session-failure',
|
||||
);
|
||||
expect(db.createServiceHandoff).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
chat_jid: 'group@test',
|
||||
source_role: 'owner',
|
||||
target_role: 'owner',
|
||||
source_agent_type: 'claude-code',
|
||||
target_agent_type: 'codex',
|
||||
start_seq: 3,
|
||||
end_seq: 7,
|
||||
reason: 'claude-session-failure',
|
||||
intended_role: 'owner',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not enqueue generic paired reviewer recovery after delegating to a fallback handoff', async () => {
|
||||
vi.mocked(
|
||||
sessionRecovery.shouldRetryFreshSessionOnAgentFailure,
|
||||
).mockReturnValue(true);
|
||||
vi.mocked(serviceRouting.getEffectiveChannelLease).mockReturnValue({
|
||||
chat_jid: 'group@test',
|
||||
owner_agent_type: 'claude-code',
|
||||
reviewer_agent_type: 'claude-code',
|
||||
arbiter_agent_type: null,
|
||||
owner_service_id: 'claude',
|
||||
reviewer_service_id: 'claude',
|
||||
arbiter_service_id: null,
|
||||
activated_at: null,
|
||||
reason: null,
|
||||
explicit: false,
|
||||
});
|
||||
vi.mocked(
|
||||
pairedExecutionContext.preparePairedExecutionContext,
|
||||
).mockReturnValue({
|
||||
task: {
|
||||
id: 'paired-task-reviewer-handoff-delegated',
|
||||
chat_jid: 'group@test',
|
||||
group_folder: 'test-group',
|
||||
owner_service_id: 'claude',
|
||||
reviewer_service_id: 'claude',
|
||||
title: null,
|
||||
source_ref: 'HEAD',
|
||||
plan_notes: null,
|
||||
round_trip_count: 1,
|
||||
review_requested_at: '2026-03-31T00:00:00.000Z',
|
||||
status: 'in_review',
|
||||
arbiter_verdict: null,
|
||||
arbiter_requested_at: null,
|
||||
completion_reason: null,
|
||||
created_at: '2026-03-31T00:00:00.000Z',
|
||||
updated_at: '2026-03-31T00:00:00.000Z',
|
||||
},
|
||||
workspace: null,
|
||||
envOverrides: {},
|
||||
});
|
||||
vi.mocked(db.getPairedTaskById).mockReturnValue({
|
||||
id: 'paired-task-reviewer-handoff-delegated',
|
||||
chat_jid: 'group@test',
|
||||
group_folder: 'test-group',
|
||||
owner_service_id: 'claude',
|
||||
reviewer_service_id: 'claude',
|
||||
title: null,
|
||||
source_ref: 'HEAD',
|
||||
plan_notes: null,
|
||||
round_trip_count: 1,
|
||||
review_requested_at: '2026-03-31T00:00:00.000Z',
|
||||
status: 'review_ready',
|
||||
arbiter_verdict: null,
|
||||
arbiter_requested_at: null,
|
||||
completion_reason: null,
|
||||
created_at: '2026-03-31T00:00:00.000Z',
|
||||
updated_at: '2026-03-31T00:00:00.000Z',
|
||||
});
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
|
||||
async (_group, _input, _onProcess, onOutput) => {
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'final',
|
||||
result: null,
|
||||
});
|
||||
return {
|
||||
status: 'success',
|
||||
result: null,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
const deps = makeDeps();
|
||||
const result = await runAgentForGroup(deps, {
|
||||
group: makeGroup(),
|
||||
prompt: 'please review',
|
||||
chatJid: 'group@test',
|
||||
runId: 'run-reviewer-delegated-handoff',
|
||||
forcedRole: 'reviewer',
|
||||
onOutput: async () => {},
|
||||
});
|
||||
|
||||
expect(result).toBe('success');
|
||||
expect(db.createServiceHandoff).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
chat_jid: 'group@test',
|
||||
paired_task_id: 'paired-task-reviewer-handoff-delegated',
|
||||
paired_task_updated_at: '2026-03-31T00:00:00.000Z',
|
||||
turn_id:
|
||||
'paired-task-reviewer-handoff-delegated:2026-03-31T00:00:00.000Z:reviewer-turn',
|
||||
turn_intent_kind: 'reviewer-turn',
|
||||
turn_role: 'reviewer',
|
||||
source_role: 'reviewer',
|
||||
target_role: 'reviewer',
|
||||
intended_role: 'reviewer',
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
pairedExecutionContext.completePairedExecutionContext,
|
||||
).not.toHaveBeenCalled();
|
||||
expect(deps.queue.enqueueMessageCheck).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('drops a stale Claude session id before retrying a fresh session', async () => {
|
||||
@@ -3367,7 +3286,7 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
expect(deps.clearSession).toHaveBeenCalledWith('test-codex');
|
||||
});
|
||||
|
||||
it('suppresses a usage-exhausted banner even when Claude already emitted progress text', async () => {
|
||||
it('replaces a usage-exhausted banner with the reset notice even after progress text', async () => {
|
||||
const outputs: string[] = [];
|
||||
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementationOnce(
|
||||
@@ -3395,13 +3314,21 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
chatJid: 'group@test',
|
||||
runId: 'run-progress-before-usage-banner',
|
||||
onOutput: async (output) => {
|
||||
if (typeof output.result === 'string') outputs.push(output.result);
|
||||
const text =
|
||||
output.output && 'text' in output.output
|
||||
? output.output.text
|
||||
: output.result;
|
||||
if (typeof text === 'string') outputs.push(text);
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toBe('error');
|
||||
expect(outputs).toEqual(['대화 요약 중...']);
|
||||
expect(db.createServiceHandoff).not.toHaveBeenCalled();
|
||||
// The banner itself stays hidden; the room gets the reset notice instead
|
||||
// of a generic failure.
|
||||
expect(result).toBe('success');
|
||||
expect(outputs).toEqual([
|
||||
'대화 요약 중...',
|
||||
expect.stringMatching(/^Claude 사용 한도에 도달했습니다\. .*02:00에/),
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not retry when Claude org access is denied', async () => {
|
||||
@@ -3432,24 +3359,9 @@ describe('runAgentForGroup Claude failure recovery', () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toBe('success');
|
||||
expect(result).toBe('error');
|
||||
expect(agentRunner.runAgentProcess).toHaveBeenCalledTimes(1);
|
||||
expect(outputs).toEqual([]);
|
||||
expect(serviceRouting.activateCodexFailover).toHaveBeenCalledWith(
|
||||
'group@test',
|
||||
'claude-org-access-denied',
|
||||
);
|
||||
expect(db.createServiceHandoff).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
chat_jid: 'group@test',
|
||||
source_role: 'owner',
|
||||
source_agent_type: 'claude-code',
|
||||
target_role: 'owner',
|
||||
target_agent_type: 'codex',
|
||||
reason: 'claude-org-access-denied',
|
||||
intended_role: 'owner',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not mistake a normal response quoting the banner text for a usage error', async () => {
|
||||
|
||||
Reference in New Issue
Block a user