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>
311 lines
9.1 KiB
TypeScript
311 lines
9.1 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
vi.mock('./config.js', async () => {
|
|
const actual =
|
|
await vi.importActual<typeof import('./config.js')>('./config.js');
|
|
return {
|
|
...actual,
|
|
PAIRED_CARRY_FORWARD_LATEST_OWNER_FINAL: true,
|
|
};
|
|
});
|
|
|
|
vi.mock('./db.js', () => {
|
|
const updatePairedTask = vi.fn();
|
|
return {
|
|
cancelPairedTurn: vi.fn(),
|
|
createPairedTask: vi.fn(),
|
|
getLatestPairedTaskForChat: vi.fn(),
|
|
getLatestOpenPairedTaskForChat: vi.fn(),
|
|
getPairedTaskById: vi.fn(),
|
|
getPairedTurnById: vi.fn(),
|
|
getPairedTurnOutputs: vi.fn(() => []),
|
|
getPairedWorkspace: vi.fn(),
|
|
insertPairedTurnOutput: vi.fn(),
|
|
updatePairedTask,
|
|
updatePairedTaskIfUnchanged: vi.fn((id, _expectedUpdatedAt, updates) => {
|
|
updatePairedTask(id, updates);
|
|
return true;
|
|
}),
|
|
upsertPairedProject: vi.fn(),
|
|
hasActiveCiWatcherForChat: vi.fn(() => false),
|
|
releasePairedTaskExecutionLease: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock('./paired-workspace-manager.js', () => ({
|
|
isOwnerWorkspaceRepairNeededError: vi.fn(() => false),
|
|
markPairedTaskReviewReady: vi.fn(),
|
|
prepareReviewerWorkspaceForExecution: vi.fn(() => ({
|
|
workspace: null,
|
|
autoRefreshed: false,
|
|
})),
|
|
provisionOwnerWorkspaceForPairedTask: vi.fn(() => ({
|
|
id: 'task-new:owner',
|
|
task_id: 'task-new',
|
|
role: 'owner',
|
|
workspace_dir: '/tmp/paired/task-new/owner',
|
|
snapshot_source_dir: null,
|
|
snapshot_ref: null,
|
|
status: 'ready',
|
|
snapshot_refreshed_at: null,
|
|
created_at: '2026-03-28T00:00:00.000Z',
|
|
updated_at: '2026-03-28T00:00:00.000Z',
|
|
})),
|
|
}));
|
|
|
|
vi.mock('./logger.js', () => ({
|
|
logger: {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
import * as db from './db.js';
|
|
import { resolveOwnerTaskForHumanMessage } from './paired-execution-context.js';
|
|
import type { PairedTask, RegisteredGroup, RoomRoleContext } from './types.js';
|
|
|
|
const group: RegisteredGroup = {
|
|
name: 'Paired Room',
|
|
folder: 'paired-room',
|
|
trigger: '@codex',
|
|
added_at: '2026-03-28T00:00:00.000Z',
|
|
agentType: 'codex',
|
|
workDir: '/repo/canonical',
|
|
};
|
|
|
|
const ownerContext: RoomRoleContext = {
|
|
serviceId: 'codex-main',
|
|
role: 'owner',
|
|
ownerServiceId: 'codex-main',
|
|
reviewerServiceId: 'claude-review',
|
|
};
|
|
|
|
function buildTask(overrides: Partial<PairedTask> = {}): PairedTask {
|
|
return {
|
|
id: 'task-superseded',
|
|
chat_jid: 'dc:test',
|
|
group_folder: group.folder,
|
|
owner_service_id: 'codex-main',
|
|
reviewer_service_id: 'claude-review',
|
|
owner_agent_type: 'codex',
|
|
reviewer_agent_type: 'claude-code',
|
|
arbiter_agent_type: 'codex',
|
|
title: null,
|
|
source_ref: 'HEAD',
|
|
plan_notes: null,
|
|
review_requested_at: null,
|
|
round_trip_count: 0,
|
|
owner_failure_count: 0,
|
|
owner_step_done_streak: 0,
|
|
finalize_step_done_count: 0,
|
|
task_done_then_user_reopen_count: 0,
|
|
empty_step_done_streak: 0,
|
|
status: 'merge_ready',
|
|
arbiter_verdict: null,
|
|
arbiter_requested_at: null,
|
|
completion_reason: null,
|
|
created_at: '2026-03-28T00:00:00.000Z',
|
|
updated_at: '2026-03-28T00:05:00.000Z',
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('paired execution carry-forward attachments', () => {
|
|
it('preserves latest owner final attachments when carrying context into a superseding task', () => {
|
|
const supersededTask = buildTask();
|
|
const attachments = [
|
|
{
|
|
path: '/data/attachments/paired-turn-outputs/task/1/owner/result.png',
|
|
name: 'result.png',
|
|
mime: 'image/png',
|
|
},
|
|
];
|
|
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
|
|
{
|
|
id: 1,
|
|
task_id: supersededTask.id,
|
|
turn_number: 1,
|
|
role: 'owner',
|
|
output_text: 'TASK_DONE\n새 렌더 이미지입니다.',
|
|
attachments,
|
|
created_at: '2026-03-28T00:01:00.000Z',
|
|
},
|
|
{
|
|
id: 2,
|
|
task_id: supersededTask.id,
|
|
turn_number: 2,
|
|
role: 'reviewer',
|
|
output_text: 'TASK_DONE\nreview approved',
|
|
created_at: '2026-03-28T00:02:00.000Z',
|
|
},
|
|
]);
|
|
|
|
resolveOwnerTaskForHumanMessage({
|
|
group,
|
|
chatJid: 'dc:test',
|
|
roomRoleContext: ownerContext,
|
|
existingTask: supersededTask,
|
|
});
|
|
|
|
expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
0,
|
|
'owner',
|
|
expect.stringContaining('TASK_DONE\n새 렌더 이미지입니다.'),
|
|
{
|
|
createdAt: '2026-03-28T00:01:00.000Z',
|
|
attachments,
|
|
},
|
|
);
|
|
});
|
|
|
|
it('carries context forward on a cold start after the previous task already closed', () => {
|
|
vi.clearAllMocks();
|
|
const previousTask = buildTask({
|
|
id: 'task-previous',
|
|
status: 'completed',
|
|
completion_reason: 'done',
|
|
});
|
|
// No open task exists -> cold start path.
|
|
vi.mocked(db.getLatestOpenPairedTaskForChat).mockReturnValue(undefined);
|
|
vi.mocked(db.getLatestPairedTaskForChat).mockReturnValue(previousTask);
|
|
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
|
|
{
|
|
id: 1,
|
|
task_id: previousTask.id,
|
|
turn_number: 1,
|
|
role: 'owner',
|
|
output_text:
|
|
'TASK_DONE\n사용자 액션 아이템: 1. 재배포 2. 서버 업데이트',
|
|
created_at: '2026-03-28T00:01:00.000Z',
|
|
},
|
|
]);
|
|
|
|
const resolved = resolveOwnerTaskForHumanMessage({
|
|
group,
|
|
chatJid: 'dc:test',
|
|
roomRoleContext: ownerContext,
|
|
// no existingTask -> resolves via getLatestOpenPairedTaskForChat (none)
|
|
});
|
|
|
|
expect(resolved.supersededTask).toBeNull();
|
|
expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
0,
|
|
'owner',
|
|
expect.stringContaining(
|
|
'TASK_DONE\n사용자 액션 아이템: 1. 재배포 2. 서버 업데이트',
|
|
),
|
|
expect.objectContaining({ createdAt: '2026-03-28T00:01:00.000Z' }),
|
|
);
|
|
});
|
|
|
|
it('carries the last final owner output, skipping an intermediate STEP_DONE', () => {
|
|
vi.clearAllMocks();
|
|
const previousTask = buildTask({
|
|
id: 'task-arbiter',
|
|
status: 'completed',
|
|
completion_reason: 'arbiter_escalated',
|
|
});
|
|
vi.mocked(db.getLatestOpenPairedTaskForChat).mockReturnValue(undefined);
|
|
vi.mocked(db.getLatestPairedTaskForChat).mockReturnValue(previousTask);
|
|
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
|
|
{
|
|
id: 1,
|
|
task_id: previousTask.id,
|
|
turn_number: 1,
|
|
role: 'owner',
|
|
output_text:
|
|
'TASK_DONE\n사용자 액션 아이템: 1. 재배포 2. 서버 업데이트',
|
|
created_at: '2026-03-28T00:01:00.000Z',
|
|
},
|
|
{
|
|
id: 2,
|
|
task_id: previousTask.id,
|
|
turn_number: 3,
|
|
role: 'owner',
|
|
output_text: 'STEP_DONE\nexception 경로 버그를 고쳤습니다.',
|
|
created_at: '2026-03-28T00:03:00.000Z',
|
|
},
|
|
]);
|
|
|
|
resolveOwnerTaskForHumanMessage({
|
|
group,
|
|
chatJid: 'dc:test',
|
|
roomRoleContext: ownerContext,
|
|
});
|
|
|
|
expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
0,
|
|
'owner',
|
|
expect.stringContaining('사용자 액션 아이템'),
|
|
expect.objectContaining({ createdAt: '2026-03-28T00:01:00.000Z' }),
|
|
);
|
|
});
|
|
|
|
it('carries the arbiter verdict forward when the previous task ended on an arbiter escalation', () => {
|
|
vi.clearAllMocks();
|
|
const previousTask = buildTask({
|
|
id: 'task-escalated',
|
|
status: 'completed',
|
|
completion_reason: 'arbiter_escalated',
|
|
});
|
|
vi.mocked(db.getLatestOpenPairedTaskForChat).mockReturnValue(undefined);
|
|
vi.mocked(db.getLatestPairedTaskForChat).mockReturnValue(previousTask);
|
|
// The arbiter's ESCALATE is the last thing the user saw — they are now
|
|
// replying to it, so it (not the earlier owner turn) must carry forward.
|
|
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
|
|
{
|
|
id: 1,
|
|
task_id: previousTask.id,
|
|
turn_number: 1,
|
|
role: 'owner',
|
|
output_text: 'STEP_DONE\n방향 A로 진행했습니다.',
|
|
created_at: '2026-03-28T00:01:00.000Z',
|
|
},
|
|
{
|
|
id: 2,
|
|
task_id: previousTask.id,
|
|
turn_number: 2,
|
|
role: 'reviewer',
|
|
output_text: 'REVISE\n방향 B가 맞습니다.',
|
|
created_at: '2026-03-28T00:02:00.000Z',
|
|
},
|
|
{
|
|
id: 3,
|
|
task_id: previousTask.id,
|
|
turn_number: 3,
|
|
role: 'arbiter',
|
|
output_text:
|
|
'ESCALATE\n두 방향(A/B) 중 사용자가 선택해야 합니다. 1) A 유지 2) B 전환',
|
|
created_at: '2026-03-28T00:03:00.000Z',
|
|
},
|
|
]);
|
|
|
|
resolveOwnerTaskForHumanMessage({
|
|
group,
|
|
chatJid: 'dc:test',
|
|
roomRoleContext: ownerContext,
|
|
});
|
|
|
|
expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
0,
|
|
'owner',
|
|
expect.stringContaining('latest arbiter verdict'),
|
|
expect.objectContaining({ createdAt: '2026-03-28T00:03:00.000Z' }),
|
|
);
|
|
// And it must carry the arbiter's escalation text, not the earlier owner turn.
|
|
expect(db.insertPairedTurnOutput).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
0,
|
|
'owner',
|
|
expect.stringContaining('두 방향(A/B) 중 사용자가 선택'),
|
|
expect.anything(),
|
|
);
|
|
});
|
|
});
|