From 0f0f698fb457904329aa2520e5377009fe7f5024 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 11 Nov 2026 15:45:50 +0900 Subject: [PATCH] fix(paired): deliver escalation notices that were silently dropped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a paired task escalated to the user after a reviewer final, the "⚠️ 자동 해결 불가" mention was emitted after the turn had already published its final and released its attempt. MessageTurnController dropped it ("Discarding late agent output after terminal final") and the ownership guard would have suppressed it anyway, so rooms looked stalled right after the reviewer's reply (javis_bot 2026-11-11 14:11, 15:28). - Mark host post-completion notices (escalation, reviewer-unavailable answer) and send them as a fresh message past both guards and the reviewer direct-terminal skip. - Reviewer prompt: NEEDS_CONTEXT is only for questions the user must answer; ask the owner for evidence with DONE_WITH_CONCERNS. - run_verification: load the helper and verifier from the EJClaw install instead of the repo under review (was "Module not found" outside EJClaw), and run the helper outside the repo so its .env is not read by EJClaw's config loader. Co-Authored-By: Claude Opus 5.5 --- prompts/claude-paired-room.md | 5 +- runners/agent-runner/src/verification.ts | 14 +++-- shared/verification-request-runner.js | 10 ++-- src/agent-runner.ts | 6 ++ src/message-agent-executor-paired.test.ts | 70 ++++++++++++++++++++++ src/message-agent-executor-paired.ts | 15 ++++- src/message-runtime-final-delivery.test.ts | 26 ++++++++ src/message-runtime-final-delivery.ts | 3 + src/message-runtime-turns.ts | 2 + src/message-runtime.ts | 2 + src/message-turn-controller.test.ts | 69 +++++++++++++++++++++ src/message-turn-controller.ts | 29 +++++++++ 12 files changed, 235 insertions(+), 16 deletions(-) diff --git a/prompts/claude-paired-room.md b/prompts/claude-paired-room.md index 6ea6929..d243520 100644 --- a/prompts/claude-paired-room.md +++ b/prompts/claude-paired-room.md @@ -74,7 +74,8 @@ If the first visible line is not one of these statuses, the output is invalid; d - **DONE** — Legacy alias for **TASK_DONE**. Prefer **TASK_DONE** for new turns - **DONE_WITH_CONCERNS** — Changes required: at least one unresolved P0-P2 finding. List each finding with its severity and the exact fix. This is not an approval - **BLOCKED** — Cannot proceed without user decision -- **NEEDS_CONTEXT** — Missing information from user +- **NEEDS_CONTEXT** — Missing information that only the user can provide. This ends the task and hands it to the user, so never use it to ask the owner for anything +- To get test output, evidence, or a fix from the owner, use **DONE_WITH_CONCERNS** and name exactly what the owner must provide; that sends the task back to the owner Do not start with non-status review labels like **APPROVE** or **REVISE**. Use **TASK_DONE** for approval and **DONE_WITH_CONCERNS** for change requests. @@ -87,7 +88,7 @@ Do not start with non-status review labels like **APPROVE** or **REVISE**. Use * - Treat `EJCLAW_WORK_DIR` as the canonical verification root for this turn. You may inspect other local paths for context, but final review findings must be re-checked against `EJCLAW_WORK_DIR` - Do not use a different clone, canonical repo path, or cached session path as the sole basis for `BLOCKED`, `DONE_WITH_CONCERNS`, or change requests. If another path disagrees with `EJCLAW_WORK_DIR`, prefer `EJCLAW_WORK_DIR` and explicitly call out the mismatch - When test/typecheck/build/lint evidence is needed, prefer the dedicated verification path (`run_verification`) over assuming the reviewer workspace should execute the full project locally -- If direct execution is unavailable, request `run_verification` or owner-provided evidence. Do not present static analysis as completed verification +- If direct execution is unavailable, request `run_verification` or owner-provided evidence (via DONE_WITH_CONCERNS). Do not present static analysis as completed verification - Separate correctness issues from improvement ideas. If something is only a better alternative, label it P3 instead of blocking the owner unnecessarily - Stagnation: **Spinning** (same error 3+), **Oscillation** (alternating approaches), **Diminishing returns** (shrinking improvement), **No progress** (discussion without change) — name the pattern and report: **Status**, **Attempted**, **Recommendation** - Implementation, commits, and pushes require agreement from both sides. Either can veto diff --git a/runners/agent-runner/src/verification.ts b/runners/agent-runner/src/verification.ts index a5c6165..a588052 100644 --- a/runners/agent-runner/src/verification.ts +++ b/runners/agent-runner/src/verification.ts @@ -1,6 +1,6 @@ import { execFile } from 'child_process'; import path from 'path'; -import { pathToFileURL } from 'url'; +import { fileURLToPath, pathToFileURL } from 'url'; export { computeVerificationSnapshotId } from '../../../shared/verification-snapshot.js'; export const VERIFICATION_PROFILES = [ @@ -130,17 +130,19 @@ export async function runVerificationRequestDirect( repoRoot: string, request: VerificationRequest, ): Promise { - const helperPath = path.join( - repoRoot, - 'shared', - 'verification-request-runner.js', + // The helper ships with the EJClaw install, not with the repo under review + // (src/ and dist/ both sit three levels below the install root). + const helperPath = fileURLToPath( + new URL('../../../shared/verification-request-runner.js', import.meta.url), ); const helperEnv = buildVerificationHelperEnv(); const { stdout, stderr } = await execFileCapture( 'bun', [helperPath, repoRoot, JSON.stringify(request)], { - cwd: repoRoot, + // Not repoRoot: EJClaw's config loader reads `.env` from the cwd, and the + // repo under review may have its own `.env`. Commands still run in repoRoot. + cwd: path.dirname(helperPath), env: helperEnv, }, ); diff --git a/shared/verification-request-runner.js b/shared/verification-request-runner.js index c6e34d0..7056e96 100644 --- a/shared/verification-request-runner.js +++ b/shared/verification-request-runner.js @@ -1,6 +1,3 @@ -import path from 'path'; -import { pathToFileURL } from 'url'; - async function main() { const [repoDir, requestJson] = process.argv.slice(2); @@ -10,8 +7,11 @@ async function main() { ); } - const verificationModuleUrl = pathToFileURL( - path.join(repoDir, 'src', 'verification.ts'), + // Load the verifier from the EJClaw install this helper ships with; repoDir + // is only the workspace being verified and need not be an EJClaw checkout. + const verificationModuleUrl = new URL( + '../src/verification.ts', + import.meta.url, ).href; const verificationModule = await import(verificationModuleUrl); const request = JSON.parse(requestJson); diff --git a/src/agent-runner.ts b/src/agent-runner.ts index a0251d6..7a34d83 100644 --- a/src/agent-runner.ts +++ b/src/agent-runner.ts @@ -62,6 +62,12 @@ export interface AgentOutput { }; /** Tool calls of the run, reported with the terminal output (Claude runner). */ toolUsage?: import('./review-skip.js').AgentToolUsage; + /** + * Host notice emitted after the paired task state changed (e.g. escalated to + * the user). Sent as a separate message even after the turn's final, which + * would otherwise drop it. + */ + postCompletionNotice?: boolean; } function readRoomSkillOverridesForRunner( diff --git a/src/message-agent-executor-paired.test.ts b/src/message-agent-executor-paired.test.ts index b823051..2b53b28 100644 --- a/src/message-agent-executor-paired.test.ts +++ b/src/message-agent-executor-paired.test.ts @@ -328,6 +328,76 @@ describe('createPairedExecutionLifecycle completion handling', () => { '원하시던 .env 위치는 여기에 있습니다.', 'ℹ️ 리뷰어 일시 사용 불가 — 리뷰 없이 owner 답변으로 진행했습니다.', ]); + expect(outputs.every((o) => o.postCompletionNotice === true)).toBe(true); + }); + + it('flags the escalation notice so it is sent after the reviewer final', async () => { + const outputs: AgentOutput[] = []; + const base = { + id: 'paired-task-reviewer-escalated', + chat_jid: 'group@test', + group_folder: 'test-group', + owner_service_id: 'claude', + reviewer_service_id: 'codex-main', + title: null, + source_ref: 'HEAD', + plan_notes: null, + round_trip_count: 1, + review_requested_at: '2026-04-09T00:00:00.000Z', + arbiter_requested_at: null, + created_at: '2026-04-09T00:00:00.000Z', + }; + vi.mocked(db.getPairedTaskById).mockReturnValue({ + ...base, + status: 'completed', + arbiter_verdict: 'escalate', + completion_reason: 'escalated', + updated_at: '2026-04-09T00:00:01.000Z', + }); + + const lifecycle = createPairedExecutionLifecycle({ + pairedExecutionContext: { + task: { + ...base, + status: 'in_review', + arbiter_verdict: null, + completion_reason: null, + updated_at: '2026-04-09T00:00:00.000Z', + }, + workspace: null, + envOverrides: {}, + }, + pairedTurnIdentity: { + turnId: + 'paired-task-reviewer-escalated:2026-04-09T00:00:00.000Z:reviewer-turn', + taskId: 'paired-task-reviewer-escalated', + taskUpdatedAt: '2026-04-09T00:00:00.000Z', + intentKind: 'reviewer-turn', + role: 'reviewer', + }, + completedRole: 'reviewer', + chatJid: 'group@test', + runId: 'run-reviewer-escalated', + enqueueMessageCheck: vi.fn(), + onOutput: async (output) => { + outputs.push(output); + }, + log, + }); + + lifecycle.recordFinalOutputBeforeDelivery( + 'NEEDS_CONTEXT\n배포 대상을 알려 주세요.', + ); + lifecycle.markStatus('succeeded'); + lifecycle.markSawOutput(true); + await lifecycle.asyncFinalize(); + + expect(outputs).toEqual([ + expect.objectContaining({ + result: '<@216851709744513024> ⚠️ 자동 해결 불가 — 확인이 필요합니다.', + postCompletionNotice: true, + }), + ]); }); it('releases an owner turn interrupted by a human message without counting an owner failure', async () => { diff --git a/src/message-agent-executor-paired.ts b/src/message-agent-executor-paired.ts index 8303f5d..2bffb41 100644 --- a/src/message-agent-executor-paired.ts +++ b/src/message-agent-executor-paired.ts @@ -125,19 +125,27 @@ async function notifyPairedCompletionIfNeeded(args: { const task = args.task; if (!task) return; - const emit = (text: string): Promise | undefined => + const emit = ( + text: string, + options?: { postCompletionNotice?: boolean }, + ): Promise | undefined => args.onOutput?.({ status: 'success', result: text, output: { visibility: 'public', text }, phase: 'final', + ...(options?.postCompletionNotice ? { postCompletionNotice: true } : {}), }); // Escalation notice fires for any role's completion that escalated to the user. if (task.status === 'completed' && task.completion_reason === 'escalated') { const sender = getLastHumanMessageSender(args.chatJid); const mention = sender ? `<@${sender}> ` : ''; - await emit(`${mention}⚠️ 자동 해결 불가 — 확인이 필요합니다.`); + // The turn already published its final and released its attempt, so this + // must bypass those guards or the user is left with a silent stop. + await emit(`${mention}⚠️ 자동 해결 불가 — 확인이 필요합니다.`, { + postCompletionNotice: true, + }); return; } @@ -151,10 +159,11 @@ async function notifyPairedCompletionIfNeeded(args: { ) { const ownerAnswer = getLatestOwnerAnswer(task.id); if (ownerAnswer) { - await emit(ownerAnswer); + await emit(ownerAnswer, { postCompletionNotice: true }); } await emit( 'ℹ️ 리뷰어 일시 사용 불가 — 리뷰 없이 owner 답변으로 진행했습니다.', + { postCompletionNotice: true }, ); return; } diff --git a/src/message-runtime-final-delivery.test.ts b/src/message-runtime-final-delivery.test.ts index 0934f16..f6434f9 100644 --- a/src/message-runtime-final-delivery.test.ts +++ b/src/message-runtime-final-delivery.test.ts @@ -105,6 +105,32 @@ describe('message-runtime-final-delivery', () => { }, ); + it('still delivers a post-completion notice after direct terminal IPC delivery', async () => { + const result = await deliverMessageRuntimeFinalText({ + text: '⚠️ 자동 해결 불가 — 확인이 필요합니다.', + chatJid: 'chat-1', + runId: 'run-1', + channel: makeChannel(), + group: makeGroup(), + startSeq: 10, + endSeq: 11, + deliveryRole: 'reviewer', + deliveryServiceId: 'codex-main', + notice: true, + hasDirectTerminalDeliveryForRun: vi.fn(() => true), + isDuplicateOfLastBotFinal: vi.fn(() => false), + openContinuation: vi.fn(), + }); + + expect(result).toBe(true); + expect(createProducedWorkItem).toHaveBeenCalledWith( + expect.objectContaining({ + result_payload: '⚠️ 자동 해결 불가 — 확인이 필요합니다.', + }), + ); + expect(deliverOpenWorkItem).toHaveBeenCalledTimes(1); + }); + it('creates and delivers a produced work item with default agent type and attachment roots', async () => { const channel = makeChannel(); const isDuplicateOfLastBotFinal = vi.fn(() => false); diff --git a/src/message-runtime-final-delivery.ts b/src/message-runtime-final-delivery.ts index f5c15bb..1ba4b94 100644 --- a/src/message-runtime-final-delivery.ts +++ b/src/message-runtime-final-delivery.ts @@ -23,6 +23,8 @@ export async function deliverMessageRuntimeFinalText(args: { deliveryRole: PairedRoomRole | null; deliveryServiceId: string | null; replaceMessageId?: string | null; + /** Post-completion host notice: sent even when the run's terminal went via IPC. */ + notice?: boolean; hasDirectTerminalDeliveryForRun?: ( chatJid: string, runId: string, @@ -32,6 +34,7 @@ export async function deliverMessageRuntimeFinalText(args: { openContinuation: (chatJid: string) => void; }): Promise { if ( + !args.notice && (args.deliveryRole === 'reviewer' || args.deliveryRole === 'arbiter') && args.hasDirectTerminalDeliveryForRun?.( args.chatJid, diff --git a/src/message-runtime-turns.ts b/src/message-runtime-turns.ts index 6d602c6..1a345d5 100644 --- a/src/message-runtime-turns.ts +++ b/src/message-runtime-turns.ts @@ -121,6 +121,7 @@ interface CreateExecuteTurnDeps { deliveryRole: PairedRoomRole | null; deliveryServiceId: string | null; replaceMessageId?: string | null; + notice?: boolean; }) => Promise; afterDeliverySuccess?: (args: { chatJid: string; @@ -247,6 +248,7 @@ export function createExecuteTurn(deps: CreateExecuteTurnDeps): ExecuteTurnFn { deliveryRole: resolvedDeliveryRole, deliveryServiceId: resolvedDeliveryServiceId, replaceMessageId: options?.replaceMessageId ?? null, + ...(options?.notice ? { notice: true } : {}), }); } catch (err) { logger.warn( diff --git a/src/message-runtime.ts b/src/message-runtime.ts index e2bb368..4f3ce40 100644 --- a/src/message-runtime.ts +++ b/src/message-runtime.ts @@ -150,6 +150,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): { deliveryRole, deliveryServiceId, replaceMessageId, + notice, }) => { return deliverMessageRuntimeFinalText({ text, @@ -164,6 +165,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): { deliveryRole, deliveryServiceId, replaceMessageId, + notice, hasDirectTerminalDeliveryForRun: deps.queue.hasDirectTerminalDeliveryForRun?.bind(deps.queue), isDuplicateOfLastBotFinal: checkDuplicateOfLastBotFinal, diff --git a/src/message-turn-controller.test.ts b/src/message-turn-controller.test.ts index 8bc7270..bf30e30 100644 --- a/src/message-turn-controller.test.ts +++ b/src/message-turn-controller.test.ts @@ -1017,3 +1017,72 @@ describe('MessageTurnController human interruptions', () => { expect(deliverFinalText).not.toHaveBeenCalled(); }); }); + +describe('MessageTurnController post-completion notices', () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + it('delivers an escalation notice after the reviewer final even though the attempt was released', async () => { + const channel = makeChannel(); + const deliverFinalText = vi.fn().mockResolvedValue(true); + let ownsAttempt = true; + const controller = new MessageTurnController({ + chatJid: 'dc:test-room', + group: makeGroup(), + runId: 'run-reviewer-escalated', + channel, + idleTimeout: 1_000, + failureFinalText: '실패', + isClaudeCodeAgent: false, + clearSession: vi.fn(), + requestClose: vi.fn(), + deliverFinalText, + canDeliverFinalText: () => ownsAttempt, + allowProgressReplayWithoutFinal: false, + deliveryRole: 'reviewer', + pairedTurnIdentity: makeTurnIdentity(), + }); + + await controller.start(); + await controller.handleOutput({ + status: 'success', + phase: 'final', + result: 'NEEDS_CONTEXT\n검증 출력을 주세요.', + output: { + visibility: 'public', + text: 'NEEDS_CONTEXT\n검증 출력을 주세요.', + }, + }); + // Completing the paired execution escalates the task and releases the + // attempt before the host emits its notice. + ownsAttempt = false; + // A late runner output must still be dropped. + await controller.handleOutput({ + status: 'success', + phase: 'final', + result: '늦은 출력', + output: { visibility: 'public', text: '늦은 출력' }, + }); + await controller.handleOutput({ + status: 'success', + phase: 'final', + result: '<@user-1> ⚠️ 자동 해결 불가 — 확인이 필요합니다.', + output: { + visibility: 'public', + text: '<@user-1> ⚠️ 자동 해결 불가 — 확인이 필요합니다.', + }, + postCompletionNotice: true, + }); + await controller.finish('success'); + + expect(deliverFinalText.mock.calls.map(([text]) => text)).toEqual([ + 'NEEDS_CONTEXT\n검증 출력을 주세요.', + '<@user-1> ⚠️ 자동 해결 불가 — 확인이 필요합니다.', + ]); + expect(deliverFinalText).toHaveBeenLastCalledWith( + '<@user-1> ⚠️ 자동 해결 불가 — 확인이 필요합니다.', + { replaceMessageId: null, notice: true }, + ); + }); +}); diff --git a/src/message-turn-controller.ts b/src/message-turn-controller.ts index 46d8488..ecdce4c 100644 --- a/src/message-turn-controller.ts +++ b/src/message-turn-controller.ts @@ -66,6 +66,8 @@ interface MessageTurnControllerOptions { options?: { attachments?: OutboundAttachment[]; replaceMessageId?: string | null; + /** A post-completion host notice, not the run's own terminal output. */ + notice?: boolean; }, ) => Promise; canDeliverFinalText?: () => boolean; @@ -173,6 +175,11 @@ export class MessageTurnController { } async handleOutput(result: AgentOutput): Promise { + if (result.postCompletionNotice) { + await this.deliverPostCompletionNotice(result); + return; + } + if (this.terminalObserved()) { this.log.info( { @@ -871,6 +878,28 @@ export class MessageTurnController { this.latestProgressTextForFinal = null; } + /** + * Host notices about the paired task's next state (e.g. escalated to the + * user) are produced after this run already completed its paired turn + * attempt, so the terminal-final and turn-ownership guards would always drop + * them. Send them as a fresh message; the progress state is left untouched. + */ + private async deliverPostCompletionNotice( + result: AgentOutput, + ): Promise { + const raw = getAgentOutputText(result); + const text = raw ? sanitizeForOutbound(raw) : null; + if (!text) return; + const delivered = await this.options.deliverFinalText(text, { + replaceMessageId: null, + notice: true, + }); + this.logOutboundAudit('post-completion-notice', { + textLength: text.length, + delivered, + }); + } + private async publishFailureFinal(): Promise { if (this.terminalObserved()) { return;