Files
EJClaw/src/paired-finalize.test.ts
Codex 0283abbb56 feat: skip or slim down the owner finalize turn
(a) When the reviewer approves the owner's first TASK_DONE answer on the
first review (one review round, outputs are exactly owner -> reviewer, no
revision or arbiter turns) and the owner workspace still matches the
approved tree with a clean working tree, no owner run happens: the task is
completed as done and the reviewed answer (status line stripped) is posted
as the final message. If the review was requested with a bare "검토해줘"
after a review-skipped answer, a one-line confirmation is posted instead of
repeating the answer.

(b) Every other finalize turn runs in a fresh, non-persisted session (no
resume of the task's long working session, no room memory briefing) with a
self-contained prompt: this task's user messages, the latest six turn
outputs and the finalize instructions/format. All three finalize entry
points (pending turn, inline bot-only follow-up, queued turn) use it.

The owner prompt now states that the first TASK_DONE answer must already
follow the final-message format because it may be posted as the final
message unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 20:42:12 +09:00

210 lines
6.2 KiB
TypeScript

import { execFileSync } from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('./db.js', () => ({
getMessagesSince: vi.fn(() => []),
getRecentChatMessages: vi.fn(() => []),
getPairedTurnOutputs: vi.fn(() => []),
getPairedWorkspace: vi.fn(),
getPairedTaskById: vi.fn(),
updatePairedTaskIfUnchanged: vi.fn(() => true),
}));
vi.mock('./logger.js', () => ({
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
import * as db from './db.js';
import {
completeTaskWithoutFinalizeRun,
REVIEW_REQUEST_APPROVED_NOTE,
resolveFastFinalizeText,
} from './paired-finalize.js';
import type { NewMessage, PairedTask, PairedTurnOutput } from './types.js';
function git(repoDir: string, ...args: string[]): string {
return execFileSync('git', args, {
cwd: repoDir,
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim();
}
function createRepo(): string {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-finalize-'));
git(repoDir, 'init');
git(repoDir, 'config', 'user.name', 'Test');
git(repoDir, 'config', 'user.email', 'test@example.com');
fs.writeFileSync(path.join(repoDir, 'a.txt'), 'a\n');
git(repoDir, 'add', 'a.txt');
git(repoDir, 'commit', '-m', 'init');
return repoDir;
}
function output(
role: PairedTurnOutput['role'],
text: string,
turn: number,
): PairedTurnOutput {
return {
id: turn,
task_id: 'task-1',
turn_number: turn,
role,
output_text: text,
created_at: `2026-03-28T00:0${turn}:00.000Z`,
};
}
function human(content: string, timestamp: string): NewMessage {
return {
id: `m-${timestamp}`,
chat_jid: 'dc:test',
sender: 'user',
sender_name: 'User',
content,
timestamp,
is_bot_message: false,
is_from_me: false,
};
}
describe('resolveFastFinalizeText', () => {
let repoDir: string;
let task: PairedTask;
beforeEach(() => {
vi.resetAllMocks();
repoDir = createRepo();
task = {
id: 'task-1',
chat_jid: 'dc:test',
group_folder: 'room',
owner_service_id: 'claude',
reviewer_service_id: 'codex-review',
title: null,
source_ref: git(repoDir, 'rev-parse', 'HEAD^{tree}'),
plan_notes: null,
review_requested_at: null,
round_trip_count: 1,
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',
};
vi.mocked(db.getPairedWorkspace).mockReturnValue({
id: 'task-1:owner',
task_id: 'task-1',
role: 'owner',
workspace_dir: repoDir,
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.mocked(db.getMessagesSince).mockReturnValue([
human('설정 확인해줘', '2026-03-28T00:00:01.000Z'),
]);
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
output(
'owner',
'[Carried forward context from the previous task: latest owner final]\nold',
0,
),
output(
'owner',
'TASK_DONE\n\n결론입니다.\n\n사용자 액션 아이템: 없음',
1,
),
output('reviewer', 'TASK_DONE\n\n확인했습니다.', 2),
]);
});
it('returns the reviewed first answer without its status line', () => {
expect(resolveFastFinalizeText(task)).toBe(
'결론입니다.\n\n사용자 액션 아이템: 없음',
);
});
it('accepts a PROCEED approval from the reviewer', () => {
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
output('owner', 'TASK_DONE\n\n결론', 1),
output('reviewer', 'PROCEED\n좋습니다', 2),
]);
expect(resolveFastFinalizeText(task)).toBe('결론');
});
it('needs a finalize run after more than one review round', () => {
expect(resolveFastFinalizeText({ ...task, round_trip_count: 2 })).toBe(
null,
);
});
it('needs a finalize run when the owner revised the answer', () => {
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
output('owner', 'TASK_DONE\n\n초안', 1),
output('reviewer', 'DONE_WITH_CONCERNS\n고칠 점', 2),
output('owner', 'TASK_DONE\n\n수정본', 3),
output('reviewer', 'TASK_DONE\n좋음', 4),
]);
expect(resolveFastFinalizeText(task)).toBeNull();
});
it('needs a finalize run when the first answer was only a STEP_DONE', () => {
vi.mocked(db.getPairedTurnOutputs).mockReturnValue([
output('owner', 'STEP_DONE\n\n1단계', 1),
output('reviewer', 'TASK_DONE\n좋음', 2),
]);
expect(resolveFastFinalizeText(task)).toBeNull();
});
it('needs a finalize run when the workspace changed after approval', () => {
fs.writeFileSync(path.join(repoDir, 'a.txt'), 'changed\n');
expect(resolveFastFinalizeText(task)).toBeNull();
git(repoDir, 'commit', '-am', 'after approval');
expect(resolveFastFinalizeText(task)).toBeNull();
});
it('needs a finalize run when the task is not merge_ready', () => {
expect(resolveFastFinalizeText({ ...task, status: 'in_review' })).toBe(
null,
);
});
it('confirms instead of repeating the answer when the user asked for this review', () => {
vi.mocked(db.getMessagesSince).mockReturnValue([
human('설정 확인해줘', '2026-03-28T00:00:01.000Z'),
human('검토해줘', '2026-03-28T00:01:30.000Z'),
]);
expect(resolveFastFinalizeText(task)).toBe(REVIEW_REQUEST_APPROVED_NOTE);
});
});
describe('completeTaskWithoutFinalizeRun', () => {
it('closes the merge_ready task as done', () => {
vi.mocked(db.updatePairedTaskIfUnchanged).mockReturnValue(true);
const task = {
id: 'task-1',
status: 'merge_ready',
updated_at: '2026-03-28T00:05:00.000Z',
} as PairedTask;
expect(completeTaskWithoutFinalizeRun(task)).toBe(true);
expect(db.updatePairedTaskIfUnchanged).toHaveBeenCalledWith(
'task-1',
'2026-03-28T00:05:00.000Z',
expect.objectContaining({
status: 'completed',
completion_reason: 'done',
}),
);
});
});