From 21b30bc51360ac0ff2544b07bc30e6b0d54421e7 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 23 May 2026 13:02:30 -0700 Subject: [PATCH] fix: avoid stale headless reveal snapshots (#2702) --- src/main/runtime/orca-runtime.test.ts | 34 +++++++++++++++++++++++++ src/main/runtime/orca-runtime.ts | 36 ++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/main/runtime/orca-runtime.test.ts b/src/main/runtime/orca-runtime.test.ts index 50404c8af..3743f8d59 100644 --- a/src/main/runtime/orca-runtime.test.ts +++ b/src/main/runtime/orca-runtime.test.ts @@ -2194,6 +2194,40 @@ describe('OrcaRuntimeService', () => { expect(writes).toEqual(['continue', '\r']) }) + it('includes headless writes appended while a renderer snapshot is awaiting prior writes', async () => { + const runtime = new OrcaRuntimeService(store) + const firstWrite = deferred() + let appendedWriteParsed = false + const state = { + emulator: { + isAlternateScreen: false, + getSnapshot: vi.fn(() => ({ + rehydrateSequences: '', + snapshotAnsi: appendedWriteParsed ? 'before-after' : 'before', + cols: 80, + rows: 24 + })) + }, + writeChain: firstWrite.promise + } + const runtimeInternals = runtime as unknown as { + headlessTerminals: Map + } + runtimeInternals.headlessTerminals.set('pty-1', state) + + const snapshotPromise = runtime.serializeHeadlessTerminalBufferForRenderer('pty-1') + state.writeChain = state.writeChain.then(() => { + appendedWriteParsed = true + }) + firstWrite.resolve() + + await expect(snapshotPromise).resolves.toEqual({ + data: 'before-after', + cols: 80, + rows: 24 + }) + }) + it('creates visible terminal sessions without asking the renderer to focus a tab', async () => { const spawn = vi.fn().mockResolvedValue({ id: 'pty-bg' }) const createTerminal = vi.fn() diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index eeb71ad09..79d2b7f9b 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -578,6 +578,7 @@ const BRACKETED_PASTE_BEGIN = '\x1b[200~' const BRACKETED_PASTE_END = '\x1b[201~' const BRACKETED_PASTE_QUIET_MS = 1500 const DRAFT_PASTE_READY_TIMEOUT_MS = 8000 +const HEADLESS_RENDERER_SNAPSHOT_STABLE_WRITE_ATTEMPTS = 8 const RECENT_PTY_OUTPUT_LIMIT = 4096 type RuntimeNotifier = { @@ -2319,7 +2320,10 @@ export class OrcaRuntimeService { ptyId: string, opts: { scrollbackRows?: number } = {} ): Promise<{ data: string; cols: number; rows: number } | null> { - return this.serializeHeadlessTerminalBuffer(ptyId, opts) + return this.serializeHeadlessTerminalBuffer(ptyId, { + ...opts, + requireStableWrites: true + }) } async clearTerminalBuffer(handle: string): Promise<{ handle: string; cleared: boolean }> { @@ -2543,13 +2547,15 @@ export class OrcaRuntimeService { private async serializeHeadlessTerminalBuffer( ptyId: string, - opts: { scrollbackRows?: number } = {} + opts: { scrollbackRows?: number; requireStableWrites?: boolean } = {} ): Promise<{ data: string; cols: number; rows: number } | null> { const state = this.headlessTerminals.get(ptyId) if (!state) { return null } - await state.writeChain + if (!(await this.waitForHeadlessWritesBeforeSnapshot(state, opts.requireStableWrites))) { + return null + } // Why: when an alternate-screen TUI (Claude Code, vim, etc.) is currently // active, the visible content is the alt-screen snapshot — replaying any // normal-buffer scrollback before it can duplicate shell prompts and @@ -2564,6 +2570,30 @@ export class OrcaRuntimeService { return data.length > 0 ? { data, cols: snapshot.cols, rows: snapshot.rows } : null } + private async waitForHeadlessWritesBeforeSnapshot( + state: RuntimeHeadlessTerminal, + requireStableWrites = false + ): Promise { + let observed = state.writeChain + await observed + if (!requireStableWrites || state.writeChain === observed) { + return true + } + + for (let attempt = 0; attempt < HEADLESS_RENDERER_SNAPSHOT_STABLE_WRITE_ATTEMPTS; attempt++) { + observed = state.writeChain + await observed + if (state.writeChain === observed) { + return true + } + } + + // Why: renderer reveal drops its fallback queue on a successful snapshot. + // If output keeps appending while we wait, prefer the renderer fallback + // replay over returning a stale "authoritative" headless snapshot. + return false + } + private disposeHeadlessTerminal(ptyId: string): void { this.headlessHydrationState.delete(ptyId) const state = this.headlessTerminals.get(ptyId)