diff --git a/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-02.spec.ts b/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-02.spec.ts index 856d8fdda90..aac1c106b4f 100644 --- a/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-02.spec.ts +++ b/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-02.spec.ts @@ -47,9 +47,10 @@ describe('OrcaRuntimeService', () => { expect(write).not.toHaveBeenCalled() runtime.onPtyData('pty-1', '\x1b]0;Codex done\x07', 101) + await vi.advanceTimersByTimeAsync(0) expect(write).toHaveBeenCalledWith( 'pty-1', - '\nYou have 1 orchestration message. Run `orca-dev orchestration check --run run_mailbox`.\n' + '\nYou have 1 orchestration message. Run orca-dev orchestration check --run run_mailbox.\n' ) expect(write).not.toHaveBeenCalledWith( 'pty-1', @@ -248,7 +249,7 @@ describe('OrcaRuntimeService', () => { await vi.waitFor(() => { expect(write).toHaveBeenCalledWith( 'pty-1', - '\nYou have 1 orchestration message. Run `orca-dev orchestration check --run run_codex_native_title`.\n' + '\nYou have 1 orchestration message. Run orca-dev orchestration check --run run_codex_native_title.\n' ) }) db.close() @@ -315,6 +316,7 @@ describe('OrcaRuntimeService', () => { // Resumed agents can report idle first; seeded idle has no transition, so liveness alone must release mail (#12536). runtime.onPtyData('pty-1', '\x1b]0;Codex done\x07', 100) + await vi.advanceTimersByTimeAsync(0) expect(write).toHaveBeenCalledWith( 'pty-1', @@ -363,6 +365,7 @@ describe('OrcaRuntimeService', () => { runtime.onPtyData('pty-1', '\x1b]0;Codex working\x07', 100) runtime.onPtyData('pty-1', '\x1b]0;Codex done\x07', 101) + await vi.advanceTimersByTimeAsync(0) expect(write).toHaveBeenCalledWith( 'pty-1', expect.stringContaining('You have 1 orchestration message') diff --git a/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-03.spec.ts b/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-03.spec.ts index e9f6d2b5863..9fe65b16874 100644 --- a/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-03.spec.ts +++ b/src/main/runtime/orca-runtime-tests/mobile-creation-and-orchestration-part-03.spec.ts @@ -61,8 +61,9 @@ describe('OrcaRuntimeService', () => { const payloads = write.mock.calls .map(([, data]) => data) .filter((data): data is string => typeof data === 'string') - expect(payloads).toContain( - '\nYou have 1 orchestration message. Run `orca-dev orchestration check --run run_test`.\n' + expect(write).toHaveBeenCalledWith( + 'pty-1', + '\nYou have 1 orchestration message. Run orca-dev orchestration check --run run_test.\n' ) expect(payloads.some((data) => data.includes('reserved completion'))).toBe(false) expect(status.delivered_at).toEqual(expect.any(String)) @@ -240,7 +241,7 @@ describe('OrcaRuntimeService', () => { }) runtime.notifyMessageArrived(republished.handle, 'status') - await Promise.resolve() + await vi.advanceTimersByTimeAsync(0) expect(write).toHaveBeenCalledWith( 'pty-1', @@ -294,6 +295,7 @@ describe('OrcaRuntimeService', () => { // Replacement can start idle with no transition; its first live idle evidence must reauthorize delivery. runtime.onPtyData('pty-1', '\x1b]0;Codex done\x07', 200) + await vi.advanceTimersByTimeAsync(0) expect(write).toHaveBeenCalledWith( 'pty-1', expect.stringContaining('You have 1 orchestration message') @@ -329,9 +331,9 @@ describe('OrcaRuntimeService', () => { db.insertMessage({ from: 'sender', to: terminal.handle, subject: 'once only' }) runtime.notifyMessageArrived(terminal.handle, 'status') - await Promise.resolve() + await vi.advanceTimersByTimeAsync(0) runtime.notifyMessageArrived(terminal.handle, 'status') - await Promise.resolve() + await vi.advanceTimersByTimeAsync(0) const pointerWrites = write.mock.calls.filter( ([, payload]) => typeof payload === 'string' && payload.includes('orchestration check') @@ -377,12 +379,12 @@ describe('OrcaRuntimeService', () => { const first = db.insertMessage({ from: 'sender', to: terminal.handle, subject: 'first' }) runtime.notifyMessageArrived(terminal.handle, 'status') // Arm the first flight before the next message, or this tests ordinary batching. - await Promise.resolve() + await vi.advanceTimersByTimeAsync(0) // Mid-flight notify must retry after settlement without losing or duplicating the second row. const second = db.insertMessage({ from: 'sender', to: terminal.handle, subject: 'second' }) runtime.notifyMessageArrived(terminal.handle, 'status') - await Promise.resolve() + await vi.advanceTimersByTimeAsync(0) expect( write.mock.calls.filter( ([, payload]) => typeof payload === 'string' && payload.includes('orchestration check') diff --git a/src/main/runtime/orchestration/mailbox/delivery-hold/mailbox-delivery-hold.ts b/src/main/runtime/orchestration/mailbox/delivery-hold/mailbox-delivery-hold.ts index 9cd7fc5eae1..06e21a62589 100644 --- a/src/main/runtime/orchestration/mailbox/delivery-hold/mailbox-delivery-hold.ts +++ b/src/main/runtime/orchestration/mailbox/delivery-hold/mailbox-delivery-hold.ts @@ -92,7 +92,7 @@ export function describeMailboxDeliveryHold(recipient: string, hold: MailboxDeli case 'not-foreground': return `Queued. ${hold.agentName} isn't in the foreground of that terminal; the message will be delivered when it's back.` case 'not-live': - return `Queued. The message will arrive once ${hold.agentName} is back.` + return `Queued. The message will arrive once ${hold.agentName} is ready.` case 'prompt': return `Queued. ${recipient} is at a prompt that needs a person; delivery is held while it stays open.` case 'draft': diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-delivery.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-delivery.ts index 7260e9c3835..5610f9f7051 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-delivery.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-delivery.ts @@ -152,13 +152,14 @@ export class OrchestrationMailboxPointerDelivery { this.foregroundDeferredMailboxes.delete(ptyId) const current = this.deps.resolveSubmitTarget(ptyId) - if (current?.processIncarnation === target.processIncarnation && foreground === 'shell') { - this.holdForeground(ptyId, target.processIncarnation) - } + const retryProbe = + current?.processIncarnation === target.processIncarnation && + foreground.foreground === 'shell' && + !this.holdForeground(ptyId, foreground) for (const [probedMailbox, reservedTypes] of mailboxes) { const terminal = this.deps.getTerminal(ptyId) if (terminal) { @@ -166,7 +167,7 @@ export class OrchestrationMailboxPointerDelivery { - return this.state.probeForeground(ptyId, () => this.deps.readForeground(ptyId)) - } + readonly probeForeground = (ptyId: string, processIncarnation: string) => + this.state.probeForeground(ptyId, processIncarnation, () => this.deps.readForeground(ptyId)) - holdForeground( - ptyId: string, - processIncarnation = this.deps.resolveSubmitTarget(ptyId)?.processIncarnation - ): void { - if (processIncarnation) { - this.state.holdForeground(ptyId, processIncarnation) - } + readonly holdForeground = this.state.holdForeground.bind(this.state) + + captureForegroundObservation(ptyId: string) { + const processIncarnation = this.deps.resolveSubmitTarget(ptyId)?.processIncarnation + return processIncarnation + ? this.state.captureForegroundObservation(ptyId, processIncarnation) + : undefined } isForegroundHeld(ptyId: string): boolean { const hold = this.state.foregroundHold(ptyId) - if (!hold) { + if (hold && hold !== this.deps.resolveSubmitTarget(ptyId)?.processIncarnation) { + this.observeTerminal(ptyId) return false } - if (hold === this.deps.resolveSubmitTarget(ptyId)?.processIncarnation) { - return true - } - this.observeTerminal(ptyId) - return false + return !!hold } observeTerminal(ptyId: string): void { - if (this.state.clearForegroundHold(ptyId)) { + if (this.state.observeTerminal(ptyId)) { queueMicrotask(() => this.deps.redriveTerminal(ptyId)) } } diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-stage.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-stage.ts index 90cd072c84b..e1d4b81890e 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-stage.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-stage.ts @@ -143,8 +143,7 @@ function finishPointerWriteAndStageEnter - args.state.probeForeground(ptyId, () => args.deps.readForeground(ptyId)), + readForeground: args.deps.readForeground, isPtyAwaitingUserInput: args.deps.isPtyAwaitingUserInput, writePty: args.deps.writePty, settle: args.settle, diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-state.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-state.ts index a4c9c9651b5..02d8afbeede 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-state.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-state.ts @@ -11,6 +11,21 @@ export type OrchestrationMailboxDeliveryFlight = { /** A mailbox waiting behind a terminal's in-flight pointer, with the types its waiters reserve. */ type ParkedOrchestrationMailboxDeliveries = Map | undefined> +type ForegroundObservation = { + processIncarnation: string + generation: number + held: boolean +} + +export type OrchestrationMailboxForegroundObservation = { + observation: Readonly> + generation: number +} + +export type OrchestrationMailboxForegroundProbe = OrchestrationMailboxForegroundObservation & { + foreground: 'agent' | 'shell' | 'unknown' +} + export class OrchestrationMailboxPointerState { private readonly flightsByPtyId = new Map() private readonly parkedDeliveriesByPtyId = new Map() @@ -21,13 +36,18 @@ export class OrchestrationMailboxPointerState { private readonly watermarkMailboxesByPtyId = new Map>() private readonly parkedTypesByMailbox = new Map | null>() - private readonly foregroundHolds = new Map() - private readonly foregroundProbes = new Map>() + private readonly foregroundObservations = new Map() + private readonly foregroundProbes = new Map< + string, + Promise + >() probeForeground( ptyId: string, + processIncarnation: string, read: () => Promise<'agent' | 'shell' | 'unknown'> - ): Promise<'agent' | 'shell' | 'unknown'> { + ): Promise { + const observation = this.captureForegroundObservation(ptyId, processIncarnation) const pending = this.foregroundProbes.get(ptyId) if (pending) { return pending @@ -35,6 +55,7 @@ export class OrchestrationMailboxPointerState { const probe = Promise.resolve() .then(read) .catch(() => 'unknown' as const) + .then((foreground) => ({ foreground, ...observation })) .finally(() => { if (this.foregroundProbes.get(ptyId) === probe) { this.foregroundProbes.delete(ptyId) @@ -44,16 +65,54 @@ export class OrchestrationMailboxPointerState { return probe } - holdForeground(ptyId: string, processIncarnation: string): void { - this.foregroundHolds.set(ptyId, processIncarnation) + captureForegroundObservation( + ptyId: string, + processIncarnation: string + ): OrchestrationMailboxForegroundObservation { + const observation = this.foregroundObservation(ptyId, processIncarnation) + return { observation, generation: observation.generation } + } + + holdForeground( + ptyId: string, + proof: OrchestrationMailboxForegroundObservation | undefined + ): boolean { + const observation = this.foregroundObservations.get(ptyId) + if ( + !proof || + !observation || + observation !== proof.observation || + observation.generation !== proof.generation + ) { + return false + } + observation.held = true + return true } foregroundHold(ptyId: string): string | undefined { - return this.foregroundHolds.get(ptyId) + const observation = this.foregroundObservations.get(ptyId) + return observation?.held ? observation.processIncarnation : undefined } - clearForegroundHold(ptyId: string): boolean { - return this.foregroundHolds.delete(ptyId) + observeTerminal(ptyId: string): boolean { + const observation = this.foregroundObservations.get(ptyId) + if (!observation) { + return false + } + observation.generation++ + const held = observation.held + observation.held = false + return held + } + + private foregroundObservation(ptyId: string, processIncarnation: string): ForegroundObservation { + let observation = this.foregroundObservations.get(ptyId) + if (!observation || observation.processIncarnation !== processIncarnation) { + observation = { processIncarnation, generation: 0, held: false } + this.foregroundObservations.set(ptyId, observation) + } + return observation } hasFlight(ptyId: string): boolean { @@ -213,6 +272,7 @@ export class OrchestrationMailboxPointerState { flight: OrchestrationMailboxDeliveryFlight | undefined releasedMailboxes: string[] } { + this.foregroundObservations.delete(ptyId) const flight = this.flightsByPtyId.get(ptyId) this.flightsByPtyId.delete(ptyId) this.parkedDeliveriesByPtyId.delete(ptyId) diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-submit.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-submit.ts index 88cc299df85..50ff7cf970d 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-submit.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/mailbox-pointer-submit.ts @@ -49,7 +49,7 @@ export function submitOrchestrationMailboxPointer( let releaseWithoutRedrive = false let finalizeReservation = true let preserveAmbiguousDelivery = false - let deferredUntilIdle = false + let preserveFlight = false let expectedPhase = MAILBOX_POINTER_WRITE_ATTEMPTED const messageIds = input.messages.map((message) => message.id) const reservationTarget = { @@ -63,7 +63,11 @@ export function submitOrchestrationMailboxPointer( clearAndRedrive = true return } - const foreground = await deps.readForeground(input.ptyId) + const foreground = await deps.state.probeForeground( + input.ptyId, + input.expectedTarget.processIncarnation, + () => deps.readForeground(input.ptyId) + ) if (!deps.state.isCurrentFlight(input.ptyId, input.flight)) { finalizeReservation = false return @@ -79,8 +83,12 @@ export function submitOrchestrationMailboxPointer( const queueSafe = !!terminal && isMailboxTerminalTakingInput(terminal) if (!terminal || !sameMailbox) { clearAndRedrive = true - } else if (foreground === 'shell') { - deps.state.holdForeground(input.ptyId, input.expectedTarget.processIncarnation) + } else if (foreground.foreground === 'shell') { + if (!deps.state.holdForeground(input.ptyId, foreground)) { + submitOrchestrationMailboxPointer(deps, input) + preserveFlight = true + return + } clearAndRedrive = true redriveClearedPointer = false } else if ( @@ -91,7 +99,7 @@ export function submitOrchestrationMailboxPointer( // Enter as its answer; no Enter has been attempted yet. deps.state.deferFlightUntilIdle(input.ptyId) input.flight.submitEnter = () => submitOrchestrationMailboxPointer(deps, input) - deferredUntilIdle = true + preserveFlight = true } else if (!queueSafe) { releaseWithoutRedrive = true } else { @@ -122,7 +130,7 @@ export function submitOrchestrationMailboxPointer( } }) .finally(() => { - if (deferredUntilIdle) { + if (preserveFlight) { return } let released = false diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-mailbox-typed-pointer-submit.spec.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-mailbox-typed-pointer-submit.spec.ts index a5cecde1e93..f95d544fdc0 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-mailbox-typed-pointer-submit.spec.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-mailbox-typed-pointer-submit.spec.ts @@ -37,6 +37,7 @@ describe('orchestration typed pointer submission', () => { harness.write.mock.calls.filter(([, payload]) => payload === '\r').length await driveToLiveIdle(harness.runtime) + await vi.advanceTimersByTimeAsync(0) expect(pointerCount(harness.write)).toBe(1) const checked = await checkBoundMailbox(harness.runtime) expect(checked).toMatchObject({ runId: run.id, count: 1 }) diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-multi-run-coordinator-wake.spec.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-multi-run-coordinator-wake.spec.ts index 81e352ab9eb..2f718324cc6 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-multi-run-coordinator-wake.spec.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-multi-run-coordinator-wake.spec.ts @@ -55,6 +55,7 @@ describe('a coordinator of two Runs', () => { insertRunReport(db, runA.id) await driveToLiveIdle(harness.runtime) + await vi.advanceTimersByTimeAsync(0) expect(harness.write).toHaveBeenCalledWith( PTY_ID, diff --git a/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-open-dialog-delivery-hold.test.ts b/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-open-dialog-delivery-hold.test.ts index 07838a32896..f1be4707a03 100644 --- a/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-open-dialog-delivery-hold.test.ts +++ b/src/main/runtime/orchestration/mailbox/terminal-pointer/orchestration-open-dialog-delivery-hold.test.ts @@ -54,29 +54,54 @@ describe('mailbox delivery into a pane awaiting the user', () => { const { runtime } = createRuntime(db, { launchAgent: null }) const write = vi.fn((_ptyId: string, _data: string) => true) let foreground: 'codex' | 'zsh' = 'codex' + let nextForegroundRead: Promise | undefined + let nextExitNameRead: Promise | undefined + const delayNextForegroundRead = (): (() => void) => { + let release = (): void => {} + nextForegroundRead = new Promise((resolve) => { + release = resolve + }) + return release + } + const sampleForeground = async (): Promise => { + const sampled = foreground + const delayed = nextForegroundRead + nextForegroundRead = undefined + await delayed + return sampled + } runtime.setPtyController({ write, writeWithSettlement: settledWriteStub(write), kill: vi.fn(), getForegroundProcess: async () => 'codex', - confirmForegroundProcess: async () => foreground, - confirmShellForeground: async () => foreground === 'zsh', + confirmForegroundProcess: async () => { + const sampled = foreground + const delayed = nextExitNameRead + nextExitNameRead = undefined + await delayed + return sampled + }, + confirmShellForeground: async () => (await sampleForeground()) === 'zsh', supportsForegroundObservation: () => true, - inspectProcess: async () => ({ - foregroundProcess: foreground, - hasChildProcesses: foreground !== 'zsh', - foregroundObservation: { - ptyId: PTY_ID, - ptyIncarnationId: 'mailbox-incarnation', - authorityGeneration: 'mailbox-test', - observationEpoch: 1, - capturedAgeMs: 0, - verdict: 'observed', - source: 'pty-master', - foregroundKind: foreground === 'zsh' ? 'shell' : 'non-shell', - processName: foreground + inspectProcess: async () => { + const sampled = await sampleForeground() + return { + foregroundProcess: sampled, + hasChildProcesses: sampled !== 'zsh', + foregroundObservation: { + ptyId: PTY_ID, + ptyIncarnationId: 'mailbox-incarnation', + authorityGeneration: 'mailbox-test', + observationEpoch: 1, + capturedAgeMs: 0, + verdict: 'observed', + source: 'pty-master', + foregroundKind: sampled === 'zsh' ? 'shell' : 'non-shell', + processName: sampled + } } - }) + } }) const run = createBoundRun(db, 'Foreground delivery') await runtime.listTerminals() @@ -94,8 +119,17 @@ describe('mailbox delivery into a pane awaiting the user', () => { const receipt = await sendDeliveryHoldWarning(runtime, db, `run:${run.id}`) expect(receipt?.code).toBe('recipient_delivery_held') + runtime.onPtyData(PTY_ID, '\x1b]7501;state=idle:app=codex\x07', 3) + await vi.advanceTimersByTimeAsync(1000) + foreground = 'codex' + runtime.onPtyData(PTY_ID, '\x1b]0;another workspace shell title\x07', 4) + await vi.advanceTimersByTimeAsync(1000) + expect(write).not.toHaveBeenCalled() + expect(db.getMessageById(message.id)).toMatchObject({ read: 0, delivered_at: null }) + foreground = 'zsh' + // A shell can reuse the last agent's title without giving it the terminal's input. - runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 3) + runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 5) await vi.advanceTimersByTimeAsync(1000) expect(write).not.toHaveBeenCalled() expect(db.getMessageById(message.id)).toMatchObject({ @@ -107,10 +141,18 @@ describe('mailbox delivery into a pane awaiting the user', () => { 'recipient_delivery_held' ) + const releaseShell = delayNextForegroundRead() + runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 6) + await vi.advanceTimersByTimeAsync(300) + const delayedReceipt = sendDeliveryHoldWarning(runtime, db, `run:${run.id}`) + await vi.advanceTimersByTimeAsync(0) foreground = 'codex' - // The unchanged title must still clear the mail hold and retry delivery. - runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 4) + // The return edge must survive an older shell sample that arrives afterward. + runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 7) + await vi.advanceTimersByTimeAsync(300) + releaseShell() await vi.advanceTimersByTimeAsync(2000) + expect(await delayedReceipt).toBeNull() expect(write.mock.calls.map(([, data]) => data)).toEqual([ expect.stringContaining('You have 1 orchestration message'), '\r' @@ -140,7 +182,7 @@ describe('mailbox delivery into a pane awaiting the user', () => { pointer_enter_pending: 0 }) foreground = 'codex' - runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 5) + runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 8) await vi.advanceTimersByTimeAsync(2000) expect(write.mock.calls.map(([, data]) => data)).toEqual([ expect.stringContaining('You have 1 orchestration message'), @@ -152,6 +194,60 @@ describe('mailbox delivery into a pane awaiting the user', () => { delivered_at: expect.any(String), pointer_enter_pending: 0 }) + + write.mockClear() + const delayedEnterMessage = insertDirectRunMessage(db, run.id, 'Delayed foreground at Enter') + runtime.notifyMessageArrived(`run:${run.id}`, 'status') + await vi.advanceTimersByTimeAsync(100) + foreground = 'zsh' + const releaseEnterShell = delayNextForegroundRead() + await vi.advanceTimersByTimeAsync(500) + foreground = 'codex' + runtime.onPtyData(PTY_ID, '\x1b]0;Codex done\x07', 9) + await vi.advanceTimersByTimeAsync(300) + releaseEnterShell() + await vi.advanceTimersByTimeAsync(2000) + expect(write.mock.calls.map(([, data]) => data)).toEqual([ + expect.stringContaining('You have 1 orchestration message'), + '\r' + ]) + expect(db.getMessageById(delayedEnterMessage.id)).toMatchObject({ + read: 0, + delivered_at: expect.any(String), + pointer_enter_pending: 0 + }) + + write.mockClear() + runtime.onPtyData(PTY_ID, '\x1b]7501;state=clear\x07', 10) + await vi.advanceTimersByTimeAsync(0) + foreground = 'zsh' + let releaseExitName = (): void => {} + nextExitNameRead = new Promise((resolve) => { + releaseExitName = resolve + }) + runtime.onPtyData(PTY_ID, '\x1b]0;workspace shell after exit\x07', 11) + await vi.advanceTimersByTimeAsync(300) + const returnedMessage = insertDirectRunMessage(db, run.id, 'Mail during exit confirmation') + runtime.notifyMessageArrived(`run:${run.id}`, 'status') + foreground = 'codex' + // An OSC-only return must invalidate the older exit sample without another title. + const releaseReturnForeground = delayNextForegroundRead() + runtime.onPtyData(PTY_ID, '\x1b]7501;state=idle:app=codex\x07', 12) + await vi.advanceTimersByTimeAsync(0) + releaseExitName() + await vi.advanceTimersByTimeAsync(0) + releaseReturnForeground() + await vi.advanceTimersByTimeAsync(2000) + expect(write.mock.calls.map(([, data]) => data)).toEqual([ + expect.stringContaining('You have 1 orchestration message'), + '\r' + ]) + expect(db.getMessageById(returnedMessage.id)).toMatchObject({ + read: 0, + delivered_at: expect.any(String), + pointer_enter_pending: 0 + }) + expect(await sendDeliveryHoldWarning(runtime, db, `run:${run.id}`)).toBeNull() db.close() }) @@ -173,6 +269,7 @@ describe('mailbox delivery into a pane awaiting the user', () => { const run = createBoundRun(db, 'Claude Run') await runtime.listTerminals() + expect(await runtime.readTerminalMailHold(`run:${run.id}`)).toEqual({ kind: 'prompt' }) runtime.onPtyData(PTY_ID, QUESTION_SCREEN, 1) await vi.advanceTimersByTimeAsync(200) insertDirectRunMessage(db, run.id, 'Worker heartbeat') diff --git a/src/main/runtime/terminal/input/terminal-agent-status-and-submission.ts b/src/main/runtime/terminal/input/terminal-agent-status-and-submission.ts index 90fa3c3aac1..ffc35998f42 100644 --- a/src/main/runtime/terminal/input/terminal-agent-status-and-submission.ts +++ b/src/main/runtime/terminal/input/terminal-agent-status-and-submission.ts @@ -101,15 +101,24 @@ export abstract class OrcaRuntimeWithSerializeAgentPromptSubmission extends Orca protected confirmPtyAgentExit(ptyId: string, recoverCompletedHook = false): void { const current = this.ptysById.get(ptyId) const incarnation = current?.incarnationId + const foregroundObservation = + this.orchestrationMailboxPointerDelivery.captureForegroundObservation(ptyId) + const recordExit = (): void => + this.recordTerminalSideEffectFact(ptyId, { kind: 'agent-exited' }, foregroundObservation) void this.recheckHookAgentPresenceForPty(ptyId).then((verdict) => { if (this.ptysById.get(ptyId) !== current || current?.incarnationId !== incarnation) { return } // No identified owner keeps legacy rules; an unverifiable owned process cannot be declared exited from silence. if (verdict === null || verdict === 'unverifiable') { - this.confirmLegacyPtyAgentExit(ptyId, recoverCompletedHook, verdict === 'unverifiable') + this.confirmLegacyPtyAgentExit( + ptyId, + recoverCompletedHook, + verdict === 'unverifiable', + recordExit + ) } else if (verdict === 'exited' && !recoverCompletedHook) { - this.recordTerminalSideEffectFact(ptyId, { kind: 'agent-exited' }) + recordExit() } else if (verdict === 'live') { this.restoreDisprovedAgentExit(ptyId) } else { @@ -143,7 +152,8 @@ export abstract class OrcaRuntimeWithSerializeAgentPromptSubmission extends Orca private confirmLegacyPtyAgentExit( ptyId: string, recoverCompletedHook: boolean, - keepOnSilence: boolean + keepOnSilence: boolean, + recordExit: () => void ): void { const pty = this.ptysById.get(ptyId) const handle = this.handleByPtyId.get(ptyId) @@ -161,7 +171,7 @@ export abstract class OrcaRuntimeWithSerializeAgentPromptSubmission extends Orca if (keepOnSilence) { this.ptyTitleTrackersByPtyId.get(ptyId)?.tracker.restoreLastAgentExit() } else if (!recoverCompletedHook) { - this.recordTerminalSideEffectFact(ptyId, { kind: 'agent-exited' }) + recordExit() } return } @@ -207,12 +217,10 @@ export abstract class OrcaRuntimeWithSerializeAgentPromptSubmission extends Orca result.controller === this.ptyController && result.available && typeof result.process === 'string' - if (!keepOnSilence || answered) { - if (!recoverCompletedHook) { - this.recordTerminalSideEffectFact(ptyId, { kind: 'agent-exited' }) - } - } else { + if (keepOnSilence && !answered) { this.ptyTitleTrackersByPtyId.get(ptyId)?.tracker.restoreLastAgentExit() + } else if (!recoverCompletedHook) { + recordExit() } }) } diff --git a/src/main/runtime/terminal/output/terminal-title-observation.ts b/src/main/runtime/terminal/output/terminal-title-observation.ts index 5ee0f166ecd..ea99e38d156 100644 --- a/src/main/runtime/terminal/output/terminal-title-observation.ts +++ b/src/main/runtime/terminal/output/terminal-title-observation.ts @@ -53,10 +53,14 @@ export abstract class OrcaRuntimeWithApplyTrackedPtyTitle extends OrcaRuntimeWit /** The PTY's agent status under this title: the program's own OSC 7501 activity while it * reports one, else what the title glyph says. */ - protected classifyPtyTitleStatus(ptyId: string, rawTitle: string): AgentStatus | null { + protected classifyPtyTitleStatus( + ptyId: string, + rawTitle: string, + { includeProgramActivity = true } = {} + ): AgentStatus | null { const agent = this.getPaneTerminalAgent(ptyId) return ( - this.ptyTitleTrackersByPtyId.get(ptyId)?.programActivity ?? + (includeProgramActivity ? this.ptyTitleTrackersByPtyId.get(ptyId)?.programActivity : null) ?? hermesNativeTitleStatus(rawTitle, agent) ?? jcodePaneTitleStatus(rawTitle, agent) ?? detectAgentStatusFromTitle(rawTitle) @@ -101,9 +105,6 @@ export abstract class OrcaRuntimeWithApplyTrackedPtyTitle extends OrcaRuntimeWit * `status`: the agent reported its status some other way, so the title is not classified. */ { statusOnly = false, status }: { statusOnly?: boolean; status?: AgentStatus } = {} ): boolean { - if (!meta?.staleWorkingTitleClear) { - this.orchestrationMailboxPointerDelivery.observeTerminal(ptyId) - } if (meta?.staleWorkingTitleClear) { return this.applyStaleWorkingTitleClear(ptyId, rawTitle, normalizedTitle) } @@ -119,6 +120,14 @@ export abstract class OrcaRuntimeWithApplyTrackedPtyTitle extends OrcaRuntimeWit : normalizedTitle const agentStatus = status ?? (identityOnlyTitle ? null : this.classifyPtyTitleStatus(ptyId, rawTitle)) + if ( + status !== undefined || + (!statusOnly && + !identityOnlyTitle && + this.classifyPtyTitleStatus(ptyId, rawTitle, { includeProgramActivity: false }) !== null) + ) { + this.orchestrationMailboxPointerDelivery.observeTerminal(ptyId) + } // Why before retiring the clear: the lifecycle counts a turn from the status the clear showed. this.recordAgentPromptLifecycleState(ptyId, agentStatus) let ptyRecordChanged = false diff --git a/src/main/runtime/terminal/output/terminal-transient-facts.ts b/src/main/runtime/terminal/output/terminal-transient-facts.ts index 0e0544c6ba3..9748b132230 100644 --- a/src/main/runtime/terminal/output/terminal-transient-facts.ts +++ b/src/main/runtime/terminal/output/terminal-transient-facts.ts @@ -1,3 +1,4 @@ +import type { OrchestrationMailboxForegroundObservation } from '../../orchestration/mailbox/terminal-pointer/mailbox-pointer-state' import { OrcaRuntimeWithScheduleWaitBlockedCheck } from '../wait/terminal-wait-blocked-checks' import type { PtyTransientFact } from '../../../providers/pty-provider-events' import type { @@ -75,9 +76,15 @@ export abstract class OrcaRuntimeWithEmitDaemonPtyTransientFact extends OrcaRunt /** Record one derived side-effect fact: batched per chunk while applying * bytes, emitted immediately for between-chunk facts (stale-title timer). */ - protected recordTerminalSideEffectFact(ptyId: string, fact: TerminalSideEffectFact): void { - if (fact.kind === 'agent-exited') { - this.orchestrationMailboxPointerDelivery.holdForeground(ptyId) + protected recordTerminalSideEffectFact( + ptyId: string, + fact: TerminalSideEffectFact, + foregroundObservation?: OrchestrationMailboxForegroundObservation + ): void { + if ( + fact.kind === 'agent-exited' && + this.orchestrationMailboxPointerDelivery.holdForeground(ptyId, foregroundObservation) + ) { this.clearDeliveryRecheck(ptyId) } if (!this.terminalSideEffectConsumerAvailable) { diff --git a/src/main/runtime/terminal/wait/terminal-wait-settlements.ts b/src/main/runtime/terminal/wait/terminal-wait-settlements.ts index 9074148c679..aae6ab91016 100644 --- a/src/main/runtime/terminal/wait/terminal-wait-settlements.ts +++ b/src/main/runtime/terminal/wait/terminal-wait-settlements.ts @@ -136,16 +136,20 @@ export abstract class OrcaRuntimeWithResolveExitWaiters extends OrcaRuntimeWithB if (!ptyId) { return null } - const incarnation = this.ptysById.get(ptyId)?.incarnationId - const foreground = await this.orchestrationMailboxPointerDelivery.probeForeground(ptyId) + const processIncarnation = this.getTerminalProcessIncarnation(terminal.handle) + const foreground = processIncarnation + ? await this.orchestrationMailboxPointerDelivery.probeForeground(ptyId, processIncarnation) + : null if ( - this.ptysById.get(ptyId)?.incarnationId !== incarnation || + this.getTerminalProcessIncarnation(terminal.handle) !== processIncarnation || this.orchestrationMailboxDeliveryTarget.resolveTerminal(terminalHandle)?.ptyId !== ptyId ) { return null } - if (foreground === 'shell' && incarnation) { - this.orchestrationMailboxPointerDelivery.holdForeground(ptyId) + if (foreground?.foreground === 'shell' && processIncarnation) { + if (!this.orchestrationMailboxPointerDelivery.holdForeground(ptyId, foreground)) { + return this.readTerminalMailHold(terminalHandle) + } this.clearDeliveryRecheck(ptyId) } const agent = this.getPaneTerminalAgent(ptyId) @@ -153,11 +157,14 @@ export abstract class OrcaRuntimeWithResolveExitWaiters extends OrcaRuntimeWithB if (this.orchestrationMailboxPointerDelivery.isForegroundHeld(ptyId)) { return { kind: 'not-foreground', agentName } } - if (!terminal.lastAgentStatusObservedLive) { - return { kind: 'not-live', agentName } - } const refusal = this.deliveryRefusal(ptyId) - return refusal === 'no-terminal' ? null : refusal + if (refusal === 'no-terminal') { + return null + } + if (refusal && ['prompt', 'draft', 'restoring'].includes(refusal.kind)) { + return refusal + } + return !terminal.lastAgentStatusObservedLive ? { kind: 'not-live', agentName } : refusal } private deliveryRefusal(ptyId: string): MailboxDeliveryHold | 'no-terminal' | null {