mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-30 08:39:32 +01:00
fix(web): recover the tmux repaint gap above the live screen on the first scroll-up (#814)
A tmux repaint burst overwrites rows that never reach xterm scrollback, leaving a hole just above the live screen. The #205 re-pull only fired at viewportY 0, so the user had to scroll past the hole first. Mark a non-shell session dirty on each live write; the first upward scroll from the bottom of a dirty session runs the existing full-history re-pull anchored to the bottom, keeping the viewport's distance from live output and replaying travel made during the replay. Cooldown, downgrade guard and the top-of-buffer path are unchanged.
This commit is contained in:
1 parent
5548c17c4a
commit
e91c62885a
3 files changed
+250
-8
No files matched your search
+31
-5
@@ -804,6 +804,9 @@ class CodemanApp {
|
||||
// Cooldown per session for the scroll-to-top "load more history" re-pull.
|
||||
this._fullHistoryRepullAt = new Map(); // Map<sessionId, timestamp>
|
||||
this._fullHistoryRepullInFlight = false;
|
||||
this._liveOutputSinceFullPull = new Set(); // Set<sessionId>
|
||||
this._liveOutputGen = new Map(); // Map<sessionId, number>
|
||||
this._earlyReplayTravel = null;
|
||||
// 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.
|
||||
@@ -9616,7 +9619,7 @@ class CodemanApp {
|
||||
* gets a much longer cooldown so a hollow pane stops re-fetching megabytes on
|
||||
* every scroll-up (issue #205, round 2).
|
||||
*/
|
||||
async _maybeRefetchFullHistory({ force = false } = {}) {
|
||||
async _maybeRefetchFullHistory({ force = false, anchor = 'top' } = {}) {
|
||||
const sessionId = this.activeSessionId;
|
||||
if (!sessionId || this._fullHistoryRepullInFlight || this._isLoadingBuffer) return;
|
||||
if (this.detachedSessions?.has(sessionId)) return;
|
||||
@@ -9750,6 +9753,9 @@ class CodemanApp {
|
||||
this._fullHistoryRepullUseless?.delete(sessionId);
|
||||
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;
|
||||
this._resetTerminalForReplay();
|
||||
const {
|
||||
parsedAt,
|
||||
@@ -9763,6 +9769,9 @@ class CodemanApp {
|
||||
);
|
||||
timing.resetAndParseMs = parsedAt - replayStartedAt;
|
||||
if (!completed || this.activeSessionId !== sessionId || selectGen !== this._selectGeneration) return;
|
||||
if ((this._liveOutputGen?.get(sessionId) || 0) === liveOutputGen) {
|
||||
this._liveOutputSinceFullPull?.delete(sessionId);
|
||||
}
|
||||
// Keep shell tab restores bounded too. A user-triggered full-history pull
|
||||
// may be tens of MB; caching it would replay that whole payload again on
|
||||
// the next tab switch before the normal 1MB tail fetch replaces it.
|
||||
@@ -9775,9 +9784,17 @@ class CodemanApp {
|
||||
// UPWARD, so what used to be row 0 (what they were looking at) is now `delta`
|
||||
// rows down; scrolling there reveals the recovered history above it instead
|
||||
// of teleporting them to the bottom the way a normal buffer load does.
|
||||
const delta = parsedBufferLength - rowsBefore;
|
||||
if (delta > 0) this.terminal.scrollToLine(delta);
|
||||
else this.terminal.scrollToTop();
|
||||
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)));
|
||||
this._earlyReplayTravel = null;
|
||||
} else {
|
||||
const delta = parsedBufferLength - rowsBefore;
|
||||
if (delta > 0) this.terminal.scrollToLine(delta);
|
||||
else this.terminal.scrollToTop();
|
||||
}
|
||||
// The load's own replay sampled the sticky-scroll baseline while the
|
||||
// terminal sat at the bottom of a just-rewritten buffer, so the next
|
||||
// flush would scroll back down and undo the restore above. This path is
|
||||
@@ -9793,6 +9810,7 @@ class CodemanApp {
|
||||
} catch {
|
||||
// Transient (offline, 5xx) — the next scroll-up past the cooldown retries.
|
||||
} finally {
|
||||
if (anchor === 'bottom') this._earlyReplayTravel = null;
|
||||
this._fullHistoryRepullInFlight = false;
|
||||
}
|
||||
}
|
||||
@@ -10355,6 +10373,7 @@ class CodemanApp {
|
||||
const fetchStartedAt = performance.now();
|
||||
const tailUrl = `/api/sessions/${sessionId}/terminal?tail=${TERMINAL_TAIL_SIZE}`;
|
||||
let capture;
|
||||
let fullHistoryReplay = useFullHistory;
|
||||
try {
|
||||
capture = await this._fetchTerminalCapture(
|
||||
useFullHistory
|
||||
@@ -10375,6 +10394,7 @@ class CodemanApp {
|
||||
_crashDiag.log('FULL CAPTURE ABORTED → tail');
|
||||
// It never loaded, so the next select must be allowed to try again.
|
||||
this._fullHistoryLoaded.delete(sessionId);
|
||||
fullHistoryReplay = false;
|
||||
capture = await this._fetchTerminalCapture(tailUrl);
|
||||
}
|
||||
const headersReceivedAt = capture.headersAt;
|
||||
@@ -10413,6 +10433,7 @@ class CodemanApp {
|
||||
_crashDiag.log(`REWRITE: ${(data.terminalBuffer.length/1024).toFixed(0)}KB`);
|
||||
this._setTerminalLoadState(sessionId, selectGen, 'replaying');
|
||||
const replayStartedAt = performance.now();
|
||||
const liveOutputGen = this._liveOutputGen?.get(sessionId) || 0;
|
||||
this._resetTerminalForReplay();
|
||||
// Truncation is reported OUT OF BAND (#258). This used to write a grey
|
||||
// "... earlier output truncated ..." line into the
|
||||
@@ -10420,7 +10441,7 @@ class CodemanApp {
|
||||
// cannot be actioned, and is indistinguishable from real CLI output.
|
||||
this._setHistoryTruncation(sessionId, data);
|
||||
// Use chunked write for large buffers to avoid UI jank
|
||||
const { parsedAt: freshParsedAt } = await this.chunkedTerminalWrite(
|
||||
const { parsedAt: freshParsedAt, completed } = await this.chunkedTerminalWrite(
|
||||
data.terminalBuffer,
|
||||
TERMINAL_CHUNK_SIZE,
|
||||
bufferLoadOwner,
|
||||
@@ -10431,6 +10452,9 @@ class CodemanApp {
|
||||
this._clearTerminalLoadState(sessionId, selectGen);
|
||||
return;
|
||||
}
|
||||
if (fullHistoryReplay && completed && (this._liveOutputGen?.get(sessionId) || 0) === liveOutputGen) {
|
||||
this._liveOutputSinceFullPull?.delete(sessionId);
|
||||
}
|
||||
// Ensure terminal is scrolled to bottom after buffer load
|
||||
this.terminal.scrollToBottom();
|
||||
}
|
||||
@@ -10838,6 +10862,8 @@ class CodemanApp {
|
||||
}
|
||||
this.terminalBuffers.delete(sessionId);
|
||||
this.terminalBufferCache.delete(sessionId);
|
||||
this._liveOutputSinceFullPull?.delete(sessionId);
|
||||
this._liveOutputGen?.delete(sessionId);
|
||||
this._clearHistoryTruncation(sessionId);
|
||||
this._xtermSnapshots?.delete(sessionId);
|
||||
try { localStorage.removeItem(`codeman-xs-${sessionId}`); } catch {}
|
||||
|
||||
@@ -4068,11 +4068,41 @@ 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;
|
||||
return;
|
||||
}
|
||||
if (lines > 0) {
|
||||
if (this.isTerminalAtBottom()) this._setHistoryNoticeRevealed?.(null);
|
||||
return;
|
||||
}
|
||||
if (lines === 0 || this.terminal?.buffer?.active?.viewportY !== 0) return;
|
||||
if (lines === 0) return;
|
||||
const buffer = this.terminal?.buffer?.active;
|
||||
if (buffer?.viewportY !== 0) {
|
||||
const sessionId = this.activeSessionId;
|
||||
// tmux repaint bursts can leave a gap just above live output (#814).
|
||||
// Called after scrollLines: include this gesture's travel in the bottom check.
|
||||
if (
|
||||
lines < 0 &&
|
||||
sessionId &&
|
||||
this._liveOutputSinceFullPull?.has(sessionId) &&
|
||||
this.sessions.get(sessionId)?.mode !== 'shell' &&
|
||||
buffer?.viewportY >= buffer.baseY - 2 - Math.abs(lines) &&
|
||||
!this._fullHistoryRepullInFlight &&
|
||||
!this._isLoadingBuffer
|
||||
) {
|
||||
Promise.resolve(this._maybeRefetchFullHistory?.({ anchor: 'bottom' }))
|
||||
.catch(() => {})
|
||||
.then(() => {
|
||||
// Travel during the replay is swallowed above, so a gesture that
|
||||
// reached the top meanwhile still owes the notice its reveal.
|
||||
if (this.activeSessionId === sessionId && this.terminal?.buffer?.active?.viewportY === 0) {
|
||||
this._setHistoryNoticeRevealed?.(sessionId);
|
||||
}
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
// An earlier frame of this gesture already started the pull; its own
|
||||
// completion reveals the notice, so do not flash the pre-pull state now.
|
||||
if (this._fullHistoryRepullInFlight || this._isLoadingBuffer) return;
|
||||
@@ -4703,6 +4733,12 @@ Object.assign(CodemanApp.prototype, {
|
||||
this._scheduleTerminalWriteFlush();
|
||||
this._noteMobileImeAuthoritativeOutput(flushedOutputSeq, flushSessionId);
|
||||
});
|
||||
// Only live writes can open a new tmux repaint gap; replays cannot (#814).
|
||||
if (writeChunk.length && flushSessionId && !this._isLoadingBuffer) {
|
||||
(this._liveOutputSinceFullPull ||= new Set()).add(flushSessionId);
|
||||
const generations = (this._liveOutputGen ||= new Map());
|
||||
generations.set(flushSessionId, (generations.get(flushSessionId) || 0) + 1);
|
||||
}
|
||||
} catch (err) {
|
||||
this._terminalWriteInFlight = false;
|
||||
this._terminalWriteInFlightBytes = 0;
|
||||
|
||||
@@ -68,7 +68,7 @@ function loadTerminalMixin(): Record<string, unknown> {
|
||||
return FakeCodemanApp.prototype;
|
||||
}
|
||||
|
||||
function loadRefetch(): (this: unknown, opts?: { force?: boolean }) => Promise<void> {
|
||||
function loadRefetch(): (this: unknown, opts?: { force?: boolean; anchor?: string }) => Promise<void> {
|
||||
const app = readFileSync(resolve(PUBLIC, 'app.js'), 'utf8');
|
||||
const body = methodSource(app, '_maybeRefetchFullHistory');
|
||||
const context = vm.createContext({ performance, TERMINAL_TAIL_SIZE, TERMINAL_CHUNK_SIZE: 32 * 1024 });
|
||||
@@ -134,7 +134,7 @@ function makeApp(
|
||||
rows: 30,
|
||||
// xterm's scrollback option; 0 leaves the browser-cap check out of a test.
|
||||
options: { scrollback },
|
||||
buffer: { active: { length: bufferRows, baseY: 0 } },
|
||||
buffer: { active: { length: bufferRows, baseY: 0, viewportY: 0 } },
|
||||
scrollToLine: vi.fn(),
|
||||
scrollToTop: vi.fn(),
|
||||
},
|
||||
@@ -256,6 +256,186 @@ describe('shell scroll-up pulls a bounded window of tmux history', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('early full-history recovery above live output (#814)', () => {
|
||||
function makeScrollApp(mode = 'omp') {
|
||||
const { app: base, urls } = makeApp(mode, { bufferRows: 130, capture: lines(400) });
|
||||
const app = Object.assign(base, {
|
||||
_liveOutputSinceFullPull: new Set<string>(),
|
||||
_liveOutputGen: new Map<string, number>(),
|
||||
_earlyReplayTravel: null as number | null,
|
||||
pendingWrites: [] as string[],
|
||||
_wasAtBottomBeforeWrite: false,
|
||||
_scheduleTerminalWriteFlush: vi.fn(),
|
||||
_restoreTerminalViewport: vi.fn(),
|
||||
_noteMobileImeAuthoritativeOutput: vi.fn(),
|
||||
_hasRecentUserScrollUp: () => false,
|
||||
isTerminalAtBottom: () => app.terminal.buffer.active.viewportY >= app.terminal.buffer.active.baseY - 2,
|
||||
_setHistoryNoticeRevealed: vi.fn(),
|
||||
_maybeRefetchFullHistory: vi.fn((opts?: { anchor?: string }) => refetch.call(app, opts)),
|
||||
});
|
||||
Object.assign(app.terminal.buffer.active, { baseY: 100, viewportY: 100 });
|
||||
const active = app.terminal.buffer.active as typeof app.terminal.buffer.active & { viewportY: number };
|
||||
Object.assign(app.terminal, {
|
||||
write: vi.fn((_text: string, done: () => void) => done()),
|
||||
scrollToBottom: vi.fn(),
|
||||
scrollLines: (n: number) => {
|
||||
active.viewportY = Math.max(0, Math.min(active.baseY, active.viewportY + n));
|
||||
},
|
||||
});
|
||||
app.terminal.scrollToLine.mockImplementation((row: number) => {
|
||||
active.viewportY = row;
|
||||
});
|
||||
app.chunkedTerminalWrite.mockImplementation(async () => {
|
||||
active.baseY = 370;
|
||||
active.viewportY = 370;
|
||||
active.length = 400;
|
||||
return { parsedAt: performance.now(), bufferLength: 400, completed: true };
|
||||
});
|
||||
const liveOutput = () => {
|
||||
(mixin.batchTerminalWrite as Function).call(app, 'live repaint');
|
||||
(mixin.flushPendingWrites as Function).call(app);
|
||||
};
|
||||
const scroll = async (n: number) => {
|
||||
active.viewportY = Math.max(0, Math.min(active.baseY, active.viewportY + n));
|
||||
(mixin._maybeLoadMoreHistoryOnScroll as Function).call(app, n);
|
||||
const pull = app._maybeRefetchFullHistory.mock.results.at(-1);
|
||||
if (pull) await pull.value;
|
||||
};
|
||||
function pauseReplay(beforeComplete = () => {}) {
|
||||
const replay = app.chunkedTerminalWrite.getMockImplementation()!;
|
||||
const { promise: gate, resolve: release } = Promise.withResolvers<void>();
|
||||
const { promise: ready, resolve: started } = Promise.withResolvers<void>();
|
||||
app.chunkedTerminalWrite.mockImplementationOnce(async () => {
|
||||
app._isLoadingBuffer = true;
|
||||
active.baseY = 0;
|
||||
active.viewportY = 0;
|
||||
started();
|
||||
await gate;
|
||||
const result = await replay();
|
||||
app._isLoadingBuffer = false;
|
||||
beforeComplete();
|
||||
return result;
|
||||
});
|
||||
return { ready, release };
|
||||
}
|
||||
return { app, active, urls, liveOutput, scroll, pauseReplay };
|
||||
}
|
||||
|
||||
it('pulls on the first upward scroll after live output and preserves distance from the new bottom', async () => {
|
||||
const { app, active, urls, liveOutput, scroll } = makeScrollApp();
|
||||
liveOutput();
|
||||
await scroll(-5);
|
||||
expect(urls).toEqual(['/api/sessions/s1/terminal?full=1']);
|
||||
expect(active.viewportY).toBe(365);
|
||||
expect(active.baseY - active.viewportY).toBe(5);
|
||||
expect(app._liveOutputSinceFullPull.has('s1')).toBe(false);
|
||||
expect(app._syncStickyScrollBaseline).toHaveBeenCalledOnce();
|
||||
// No second early pull until another live write dirties this session.
|
||||
active.viewportY = active.baseY;
|
||||
app._fullHistoryRepullAt.clear();
|
||||
await scroll(-3);
|
||||
expect(urls).toHaveLength(1);
|
||||
});
|
||||
|
||||
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 }) => {
|
||||
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);
|
||||
expect(app._maybeRefetchFullHistory).toHaveBeenCalledTimes(1);
|
||||
paused.release();
|
||||
await pull;
|
||||
expect(active.baseY - active.viewportY).toBe(distance);
|
||||
expect(app._earlyReplayTravel).toBeNull();
|
||||
});
|
||||
|
||||
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();
|
||||
const generation = app._liveOutputGen.get('s1');
|
||||
const paused = pauseReplay(liveOutput);
|
||||
const pull = scroll(-5);
|
||||
await paused.ready;
|
||||
paused.release();
|
||||
await pull;
|
||||
expect(app._liveOutputGen.get('s1')).toBe(generation! + 1);
|
||||
expect(app._liveOutputSinceFullPull.has('s1')).toBe(true);
|
||||
app._fullHistoryRepullAt.clear();
|
||||
active.viewportY = active.baseY;
|
||||
await scroll(-3);
|
||||
expect(urls).toEqual(['/api/sessions/s1/terminal?full=1', '/api/sessions/s1/terminal?full=1']);
|
||||
expect(app._liveOutputSinceFullPull.has('s1')).toBe(false);
|
||||
});
|
||||
|
||||
it('keeps a clean non-shell session local until the existing top path is reached', async () => {
|
||||
const { app, active, urls, scroll } = makeScrollApp();
|
||||
await scroll(-5);
|
||||
expect(urls).toEqual([]);
|
||||
await scroll(-active.viewportY);
|
||||
expect(urls).toEqual(['/api/sessions/s1/terminal?full=1']);
|
||||
expect(app._maybeRefetchFullHistory).toHaveBeenCalledWith();
|
||||
expect(app.terminal.scrollToLine).toHaveBeenCalledWith(270);
|
||||
});
|
||||
|
||||
it('never pulls early for a shell, even after live output', async () => {
|
||||
const { urls, liveOutput, scroll } = makeScrollApp('shell');
|
||||
liveOutput();
|
||||
await scroll(-5);
|
||||
expect(urls).toEqual([]);
|
||||
await scroll(-95);
|
||||
expect(urls).toEqual([`/api/sessions/s1/terminal?full=1&tail=${TERMINAL_TAIL_SIZE}`]);
|
||||
});
|
||||
|
||||
it('does not dirty a session while replay loading queues its output', () => {
|
||||
const { app, liveOutput } = makeScrollApp();
|
||||
app._isLoadingBuffer = true;
|
||||
liveOutput();
|
||||
expect(app._liveOutputSinceFullPull.size).toBe(0);
|
||||
});
|
||||
|
||||
it('does not pull early for downward travel or while already reading older rows', async () => {
|
||||
const { active, urls, liveOutput, scroll } = makeScrollApp();
|
||||
liveOutput();
|
||||
active.viewportY = 50;
|
||||
await scroll(-5);
|
||||
active.viewportY = 95;
|
||||
await scroll(3);
|
||||
expect(urls).toEqual([]);
|
||||
});
|
||||
|
||||
it.each(['_isLoadingBuffer', '_fullHistoryRepullInFlight'] as const)('respects the %s guard', async (guard) => {
|
||||
const { app, urls, liveOutput, scroll } = makeScrollApp();
|
||||
liveOutput();
|
||||
app[guard] = true;
|
||||
await scroll(-5);
|
||||
expect(urls).toEqual([]);
|
||||
});
|
||||
|
||||
it('keeps the cooldown and downgrade guard on an early pull', async () => {
|
||||
const { app, active, urls, liveOutput, scroll } = makeScrollApp();
|
||||
liveOutput();
|
||||
app._fullHistoryRepullAt.set('s1', Date.now());
|
||||
await scroll(-3);
|
||||
expect(urls).toEqual([]);
|
||||
app._fullHistoryRepullAt.clear();
|
||||
active.viewportY = active.baseY;
|
||||
active.length = 1000;
|
||||
await scroll(-3);
|
||||
expect(urls).toHaveLength(1);
|
||||
expect(app._resetTerminalForReplay).not.toHaveBeenCalled();
|
||||
expect(app._liveOutputSinceFullPull.has('s1')).toBe(true);
|
||||
expect(app._fullHistoryRepullUseless.has('s1')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('a skipped bounded window never damages the Load full history banner', () => {
|
||||
it('a tail-cut window smaller than the browser is not replayed and never marks the session exhausted', async () => {
|
||||
// The browser holds far more rows than a 1 MiB window carries, and tmux holds
|
||||
|
||||
Reference in new issue
Block a user