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

This commit is contained in:
Neil
2026-11-11 09:24:57 -08:00
parent 685d83a9f7
commit 66546e7cd7
4 changed files with 41 additions and 6 deletions
@@ -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)
@@ -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
})
}
@@ -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<void> {
@@ -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$ ')
@@ -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<readonly string[] | null>
/** 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)