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.
This commit is contained in:
Hadrien Kerlero committed 2026-11-22 02:43:30 +01:00
1 parent e91c62885a
commit fd31315582
3 files changed
+37 -14

No files matched your search

+12 -5
View File
@@ -806,7 +806,7 @@ class CodemanApp {
this._fullHistoryRepullInFlight = false;
this._liveOutputSinceFullPull = new Set(); // Set<sessionId>
this._liveOutputGen = new Map(); // Map<sessionId, number>
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;
+2 -2
View File
@@ -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) {
+23 -7
View File
@@ -262,7 +262,7 @@ describe('early full-history recovery above live output (#814)', () => {
const app = Object.assign(base, {
_liveOutputSinceFullPull: new Set<string>(),
_liveOutputGen: new Map<string, number>(),
_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();