From fd31315582a662ac1923b0f480948292a4c702d6 Mon Sep 17 00:00:00 2001 From: Hadrien Kerlero <199662351+HadrienKerlero@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:56:45 +0100 Subject: [PATCH] fix(web): keep fetch-time output dirty and clamp replay travel per move (#814) Review follow-up. The early re-pull sampled the live-output generation after its fetch, but nothing gates live output while the capture downloads, so output newer than the capture was overwritten and the session marked clean. Sample before the fetch. Travel made during the replay was one signed sum clamped once, so scrolling to the bottom and back up cancelled out. Record moves in order and clamp after each, as live scrolling does. --- src/web/public/app.js | 17 ++++++++++----- src/web/public/terminal-ui.js | 4 ++-- test/shell-scroll-history-pull.test.ts | 30 ++++++++++++++++++++------ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/web/public/app.js b/src/web/public/app.js index 95302d6e..1f166e87 100644 --- a/src/web/public/app.js +++ b/src/web/public/app.js @@ -806,7 +806,7 @@ class CodemanApp { this._fullHistoryRepullInFlight = false; this._liveOutputSinceFullPull = new Set(); // Set this._liveOutputGen = new Map(); // Map - this._earlyReplayTravel = null; + this._earlyReplayTravel = null; // number[] while an early (#814) replay runs // Sessions whose last re-pull came back THINNER than the live buffer (a // repaint-mode CLI pane, where tmux keeps no history of its own). The pull is // refused for those and retried far more slowly — see _maybeRefetchFullHistory. @@ -9658,6 +9658,10 @@ class CodemanApp { // replaying its own capture, and the reset below would land in the middle // of that write. The select generation moves on every switch, so it can. const selectGen = this._selectGeneration; + // Sampled BEFORE the fetch: this path does not gate live output while the + // capture downloads, so output flushed after tmux captured would otherwise + // be overwritten by the replay and still count as covered by it (#814). + const liveOutputGen = this._liveOutputGen?.get(sessionId) || 0; try { const requestStartedAt = performance.now(); const capture = await this._fetchTerminalCapture( @@ -9754,8 +9758,9 @@ class CodemanApp { const rowsBefore = this.terminal.buffer.active.length; const replayStartedAt = performance.now(); const rowsFromBottom = this.terminal.buffer.active.baseY - this.terminal.buffer.active.viewportY; - const liveOutputGen = this._liveOutputGen?.get(sessionId) || 0; - if (anchor === 'bottom') this._earlyReplayTravel = 0; + // Ordered, not summed: each move clamps at the buffer edge as it would + // have live, so a reversal after hitting the bottom is not cancelled out. + if (anchor === 'bottom') this._earlyReplayTravel = []; this._resetTerminalForReplay(); const { parsedAt, @@ -9787,8 +9792,10 @@ class CodemanApp { if (anchor === 'bottom') { // Recover the tmux repaint gap above the live screen without a jump (#814). const baseY = this.terminal.buffer.active.baseY; - const distance = rowsFromBottom - this._earlyReplayTravel; - this.terminal.scrollToLine(Math.max(0, Math.min(baseY, baseY - distance))); + const clamp = (row) => Math.max(0, Math.min(baseY, row)); + let row = clamp(baseY - rowsFromBottom); + for (const move of this._earlyReplayTravel) row = clamp(row + move); + this.terminal.scrollToLine(row); this._earlyReplayTravel = null; } else { const delta = parsedBufferLength - rowsBefore; diff --git a/src/web/public/terminal-ui.js b/src/web/public/terminal-ui.js index 369e2b07..17c17403 100644 --- a/src/web/public/terminal-ui.js +++ b/src/web/public/terminal-ui.js @@ -4068,8 +4068,8 @@ Object.assign(CodemanApp.prototype, { * what the pull left rather than flashing the state it is about to replace. */ _maybeLoadMoreHistoryOnScroll(lines) { - if (typeof this._earlyReplayTravel === 'number') { - this._earlyReplayTravel += lines; + if (Array.isArray(this._earlyReplayTravel)) { + this._earlyReplayTravel.push(lines); return; } if (lines > 0) { diff --git a/test/shell-scroll-history-pull.test.ts b/test/shell-scroll-history-pull.test.ts index e80345d5..19c5d1f8 100644 --- a/test/shell-scroll-history-pull.test.ts +++ b/test/shell-scroll-history-pull.test.ts @@ -262,7 +262,7 @@ describe('early full-history recovery above live output (#814)', () => { const app = Object.assign(base, { _liveOutputSinceFullPull: new Set(), _liveOutputGen: new Map(), - _earlyReplayTravel: null as number | null, + _earlyReplayTravel: null as number[] | null, pendingWrites: [] as string[], _wasAtBottomBeforeWrite: false, _scheduleTerminalWriteFlush: vi.fn(), @@ -338,18 +338,20 @@ describe('early full-history recovery above live output (#814)', () => { }); it.each([ - { travel: -12, distance: 17 }, - { travel: 20, distance: 0 }, - { travel: -1000, distance: 370 }, - ])('preserves travel $travel during an unfinished replay, clamped to the buffer', async ({ travel, distance }) => { + { moves: [-12], distance: 17 }, + { moves: [20], distance: 0 }, + { moves: [-1000], distance: 370 }, + // Reversal after an edge: each move clamps as it would have live. + { moves: [20, -12], distance: 12 }, + { moves: [-1000, 30], distance: 340 }, + ])('replays travel $moves made during an unfinished replay, clamped per move', async ({ moves, distance }) => { const { app, active, liveOutput, scroll, pauseReplay } = makeScrollApp(); liveOutput(); const paused = pauseReplay(); const pull = scroll(-5); await paused.ready; // The temporary reset buffer cannot scroll, but the gesture still counts. - active.viewportY = Math.max(0, Math.min(active.baseY, active.viewportY + travel)); - (mixin._maybeLoadMoreHistoryOnScroll as Function).call(app, travel); + for (const move of moves) (mixin._maybeLoadMoreHistoryOnScroll as Function).call(app, move); expect(app._maybeRefetchFullHistory).toHaveBeenCalledTimes(1); paused.release(); await pull; @@ -357,6 +359,20 @@ describe('early full-history recovery above live output (#814)', () => { expect(app._earlyReplayTravel).toBeNull(); }); + it('keeps a session dirty when live output lands while the capture is still downloading', async () => { + const { app, urls, liveOutput, scroll } = makeScrollApp(); + liveOutput(); + const fetchCapture = app._fetchTerminalCapture.getMockImplementation()!; + app._fetchTerminalCapture.mockImplementationOnce(async (url: string) => { + const capture = await fetchCapture(url); + // tmux has captured; this output is newer than the capture the replay writes. + liveOutput(); + return capture; + }); + await scroll(-5); + expect(urls).toHaveLength(1); + expect(app._liveOutputSinceFullPull.has('s1')).toBe(true); + }); it('keeps live output flushed before the final restore dirty for the next early pull', async () => { const { app, active, urls, liveOutput, scroll, pauseReplay } = makeScrollApp(); liveOutput();