From 66546e7cd7228a66ed8052bc72d4460bebd4ebd5 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 11 Nov 2026 09:24:57 -0800 Subject: [PATCH] fix(cloud): WAKE-SCROLLBACK the woken terminal's first snapshot takes the restore (new PTY id), no race with React arming; test times relative to now --- .../output/replay-data-drain.ts | 7 +++++- .../lib/orca-cloud/cloud-wake-pane-capture.ts | 3 ++- .../orca-cloud/cloud-wake-scrollback.test.ts | 23 +++++++++++++++++-- .../lib/orca-cloud/cloud-wake-scrollback.ts | 14 +++++++++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/components/terminal-pane/pty-connection/output/replay-data-drain.ts b/src/renderer/src/components/terminal-pane/pty-connection/output/replay-data-drain.ts index 10e08d112a2..8c47b08efc5 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection/output/replay-data-drain.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection/output/replay-data-drain.ts @@ -119,7 +119,12 @@ export function bindReplayDataDrain(session: ConnectPanePtySession): void { let preSleep: WakeScrollbackRestore | null = null if (clearBeforeReplay) { // WAKE-SCROLLBACK: a woken cloud pane's pre-sleep output, put back once this snapshot painted. - preSleep = beginWakeScrollbackRestore(session.pane.leafId, session.pane.terminal) + preSleep = beginWakeScrollbackRestore( + session.pane.leafId, + session.pane.terminal, + Date.now(), + payload.ptyId + ) if (carriesNormalBuffer) { // Wait for queued alt-screen transitions before choosing replay’s target buffer. await waitForTerminalOutputParsed(session.pane.terminal) diff --git a/src/renderer/src/lib/orca-cloud/cloud-wake-pane-capture.ts b/src/renderer/src/lib/orca-cloud/cloud-wake-pane-capture.ts index d8ec1b46f18..7298e8e5068 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-wake-pane-capture.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-wake-pane-capture.ts @@ -50,6 +50,7 @@ export function captureCloudPaneBeforeWake(args: { sleptAt: args.asleepSince, agentPane, maxRows: normalizeDesktopTerminalScrollbackRows(state.settings?.terminalScrollbackRows), - readOnlyCopy: () => readOnlyCopyLines(worktreeId, ptyId) + readOnlyCopy: () => readOnlyCopyLines(worktreeId, ptyId), + ptyId }) } diff --git a/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.test.ts b/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.test.ts index 8d05bbb3777..dba35089c30 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.test.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.test.ts @@ -16,7 +16,7 @@ vi.mock('@/i18n/i18n', () => ({ afterEach(() => resetWakeScrollbackForTests()) -const SLEPT_AT = new Date(2026, 10, 11, 14, 2).getTime() +const SLEPT_AT = Date.now() - 29 * 60_000 const DIVIDER = /^── Slept .+–.+ · processes stopped ──$/ function write(terminal: Terminal, data: string): Promise { @@ -126,7 +126,7 @@ describe('WAKE-SCROLLBACK', () => { await wake(terminal, 'leaf-1', '$ ') await write(terminal, 'echo two\r\ntwo\r\n$ ') captureWakeScrollback('leaf-1', terminal, { - sleptAt: SLEPT_AT + 3_600_000, + sleptAt: SLEPT_AT + 10 * 60_000, agentPane: false, maxRows: 5000 }) @@ -140,6 +140,25 @@ describe('WAKE-SCROLLBACK', () => { expect(lines.indexOf('two')).toBeLessThan(dividers[1]!) }) + it('a snapshot for the new (woken) terminal takes it at once, before React arms it', async () => { + const terminal = pane(4) + await write(terminal, '$ ls\r\nREADME\r\n$ ') + captureWakeScrollback('leaf-1', terminal, { + sleptAt: SLEPT_AT, + agentPane: false, + maxRows: 5000, + ptyId: 'remote:env::term-old' + }) + // A cold restore of the old terminal while still asleep: not taken. + expect( + beginWakeScrollbackRestore('leaf-1', terminal, Date.now(), 'remote:env::term-old') + ).toBeNull() + // The woken terminal's snapshot: taken, even though nothing armed it yet. + expect( + beginWakeScrollbackRestore('leaf-1', terminal, Date.now(), 'remote:env::term-new')?.block + ).toContain('README') + }) + it('a replay before the workspace leaves "asleep" (a cold restore) does not take it', async () => { const terminal = pane(4) await write(terminal, '$ ls\r\nREADME\r\n$ ') diff --git a/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.ts b/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.ts index acad98c8e31..0859a2e48c8 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-wake-scrollback.ts @@ -20,6 +20,8 @@ type PendingHistory = { * replay (a cold restore while still asleep) must not take it. */ armed: boolean + /** The pre-sleep terminal: a snapshot for a different one belongs to the woken terminal. */ + ptyId: string | null } // Keyed by the pane's durable leaf id; cleared once restored, or when the pane goes away. @@ -60,6 +62,8 @@ export function captureWakeScrollback( agentPane: boolean maxRows: number readOnlyCopy?: () => Promise + /** The pane's terminal before the sleep; a replay for any other one is the woken terminal's. */ + ptyId?: string | null } ): void { const existing = pending.get(leafId) @@ -72,6 +76,7 @@ export function captureWakeScrollback( const entry: PendingHistory = { sleptAt: options.sleptAt ?? Date.now(), armed: false, + ptyId: options.ptyId ?? null, text: text.replace(SGR, '').trim() === '' ? null : text } pending.set(leafId, entry) @@ -149,10 +154,15 @@ export type WakeScrollbackRestore = { export function beginWakeScrollbackRestore( leafId: string, terminal: WokenTerminal, - wokeAt = Date.now() + wokeAt = Date.now(), + /** The terminal this snapshot is for. */ + ptyId: string | null = null ): WakeScrollbackRestore | null { const entry = pending.get(leafId) - if (!entry?.armed) { + // Armed by the workspace leaving "asleep", or (sooner, with no race against React) by the + // snapshot being for a new terminal: a cold restore while asleep is for the old one. + const newTerminal = entry?.ptyId != null && ptyId !== null && ptyId !== entry.ptyId + if (!entry || !(entry.armed || newTerminal)) { return null } pending.delete(leafId)