fix(orchestration): close a chat approval Orca answered just before a restart

This commit is contained in:
Jinwoo-H
2026-11-12 01:32:35 -05:00
parent bda1ad0d55
commit e7a129ffd5
3 changed files with 112 additions and 4 deletions
@@ -31,16 +31,22 @@ export const readChatWorkerPrompt: ChatWorkerPromptReader = async (sessionId, it
return item?.body.kind === 'approval' ? { revision: item.revision, body: item.body } : null
}
/** Settled by Orca's own answer, for a coordinator or a person. */
export function answeredByOrca(prompt: ChatWorkerPrompt): boolean {
const { resolution } = prompt.body
return (
resolution.state !== 'pending' &&
resolution.resolvedBy?.startsWith(ORCHESTRATION_APPROVAL_CALLER_PREFIX) === true
)
}
/** How a reported chat approval closed, from its journal item; null while it is still open, or when
* Orca answered it, since that answer writes its own closing line with the reason. */
export function chatApprovalCloseOutcome(
prompt: ChatWorkerPrompt | null
): WorkerApprovalOutcome | null {
const resolution = prompt?.body.resolution
if (
resolution?.state === 'pending' ||
resolution?.resolvedBy?.startsWith(ORCHESTRATION_APPROVAL_CALLER_PREFIX)
) {
if (resolution?.state === 'pending' || (prompt && answeredByOrca(prompt))) {
return null
}
return {
@@ -0,0 +1,77 @@
import { expect, it, vi } from 'vitest'
import { makePaneKey } from '../../../../../shared/workspace-layout/stable-pane-id'
import { OrchestrationDb } from '../../db/orchestration-db'
import '../../db/contract-constants'
import '../../db/messages/mailbox-routing-page'
import '../../db/dispatch-row-writer'
import '../../db/attempt-outcome-projection'
import { createRootDispatch } from '../../db/root-dispatch-test-fixture'
import { approvalReportId } from './worker-approval-report'
vi.mock('../../mailbox/routing/structured-session-mail-target', () => ({
structuredMailboxCustody: () => ({ sessionId: 'session-1', dispatchId: null })
}))
const { ChatWorkerApprovalWatch } = await import('./chat-worker-approval-watch')
it('closes, after a restart, a chat approval Orca answered before its closing line was stored', async () => {
const db = new OrchestrationDb(':memory:')
const run = db.createRun({
objective: 'approvals',
coordinatorHandle: 'term_coordinator',
coordinatorPaneKey: makePaneKey('tab-coordinator', '22222222-2222-4222-8222-222222222222')
})
const task = db.createTask({ spec: 'do the work', runId: run.id })
const dispatch = createRootDispatch(db, task.id, 'term_worker')
db.insertMessage({
id: approvalReportId(dispatch.id, 'item_1', 'open'),
from: 'term_worker',
to: `run:${run.id}`,
subject: 'Worker is waiting for permission',
type: 'escalation',
payload: JSON.stringify({
approval: {
v: 1,
approvalId: 'item_1',
state: 'open',
worker: { handle: 'term_worker', dispatchId: dispatch.id, taskId: dispatch.task_id },
summary: { title: 'Allow Bash?', detail: 'rm -rf build', reason: null },
allowedActions: ['allow', 'deny']
}
}),
runId: run.id
})
const watch = new ChatWorkerApprovalWatch({
getDb: () => db,
notify: vi.fn(),
signal: new AbortController().signal,
watchJournal: () => () => {},
// The worker's chat recorded Orca's Allow; Orca quit before writing the closing line.
readPrompt: async () => ({
revision: 2,
body: {
kind: 'approval',
title: 'Allow Bash?',
detail: 'rm -rf build',
options: [
{ id: 'allow', label: 'Allow', intent: 'allow_once' },
{ id: 'deny', label: 'Deny', intent: 'reject_once' }
],
resolution: {
state: 'resolved',
selectedOptionId: 'allow',
resolvedBy: `trusted-local:orchestration:approval:${dispatch.id}`,
resolvedAt: 2
}
}
})
})
await watch.adopt()
const closed = db.getMessageById(approvalReportId(dispatch.id, 'item_1', 'closed'))
expect(JSON.parse(closed?.payload ?? '{}').approval).toMatchObject({
state: 'answered',
outcome: { decision: 'allow' }
})
})
@@ -6,6 +6,7 @@ import { subscribeForRedrive } from '../../mailbox/structured-pointer/structured
import { structuredMailboxCustody } from '../../mailbox/routing/structured-session-mail-target'
import type { WorkerTurnStopReportDeps } from '../turn-stops/worker-turn-stop-report'
import {
answeredByOrca,
chatApprovalCloseOutcome,
readChatWorkerPrompt,
type ChatWorkerPromptReader
@@ -16,6 +17,7 @@ import {
closeReportedApproval,
recentOpenApprovalReports
} from './worker-approval-report'
import { workerApprovalDecisionOf } from './worker-approval'
export type ChatJournalWatch = (
sessionId: string,
@@ -163,6 +165,29 @@ export class ChatWorkerApprovalWatch {
)
continue
}
const prompt = await (this.deps.readPrompt ?? readChatWorkerPrompt)(
sessionId,
approvalId
).catch(() => undefined)
if (this.deps.signal.aborted) {
return
}
if (prompt && answeredByOrca(prompt)) {
// Orca stopped after answering it but before storing that answer's closing line.
closeReportedApproval(
db,
dispatch,
approvalId,
{
by: 'unknown',
decision: workerApprovalDecisionOf(prompt.body),
reason: null,
reasonDelivered: false
},
this.deps
)
continue
}
this.track(sessionId, approvalId, dispatch.id)
sessions.add(sessionId)
}