mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-01 08:59:32 +01:00
After a server restart the byte recording a tab switch replays (`?tail=`) holds only the tmux client's attach repaint, so an inline Claude session came back with an empty local buffer while tmux still held its scrollback. The hollow-buffer fallback then sent PageUp/PageDown to Claude, which moved the composer cursor instead of the transcript, until a page reload. Pre-existing on an unmodified build; every restart of the server showed it. The paging fallback now stands down when the last capture reported tmux scrollback (`paneHistoryLines` > 0), dropping the gesture debt and any page keys already queued, so the gesture falls through to the local path, whose scroll-to-top pull fetches `full=1` and the gesture scrolls on from there. Zero and unknown counts keep paging (fullscreen Claude, codex, opencode, remote and Docker panes). The pull no longer inherits a downgrade refusal's 60 s latch for such a buffer, drops a completion that outlived a tab switch back to the same session (select generation, not only the session id), records no count from an error envelope (a JSON 5xx would otherwise forget it and hand the next gesture back to paging), and re-positions the local-echo overlay after its rewrite. The refresh path records its count only once it still owns the terminal, for the same reason. The `[scroll]` line prints `paneHistoryLines` so an unloaded buffer reads apart from a hollow one, and the invariant docs record the exception. Measured with phone emulation after a restart: before, the second visit landed at baseY 0 and every swipe sent PageUp; after, the first swipe fetched the history once with nothing sent to Claude and the gesture scrolled 21 rows, with an unsent draft intact on the overlay.
95 lines
4.3 KiB
TypeScript
95 lines
4.3 KiB
TypeScript
/**
|
|
* @fileoverview What the client remembers about a pane's tmux scrollback.
|
|
*
|
|
* `selectSession` serves a tab switch from the small `full=1` capture when the
|
|
* session's last terminal response reported `paneHistoryLines: 0` (a fullscreen
|
|
* CLI in the alternate screen), instead of the 1 MiB byte tail. The memory that
|
|
* decides it must treat ONLY a reported 0 as empty: a missing field (an older
|
|
* server, a byte-history fallback) has to forget, so the tab switch goes back to
|
|
* the bounded tail rather than trusting a stale answer.
|
|
*
|
|
* Runs the real `_notePaneHistory` / `_clearHistoryTruncation` from app.js in a
|
|
* `vm`. The switch itself is driven end to end in
|
|
* fullscreen-tab-switch-capture.browser.test.ts (browser suite).
|
|
*/
|
|
import { readFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
import vm from 'node:vm';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
const APP = readFileSync(resolve(import.meta.dirname, '../src/web/public/app.js'), 'utf8');
|
|
|
|
function methodSource(source: string, method: string): string {
|
|
const start = source.search(new RegExp(`^ {2}(?:async )?${method}\\(`, 'm'));
|
|
expect(start, `${method} not found`).toBeGreaterThan(-1);
|
|
const next = /^ {2}(?:async )?[A-Za-z_$][\w$]*\(/m.exec(source.slice(start + 1));
|
|
return next ? source.slice(start, start + 1 + next.index) : source.slice(start);
|
|
}
|
|
|
|
function makeApp() {
|
|
const methods = vm.runInContext(
|
|
`({ ${methodSource(APP, '_notePaneHistory')}, ${methodSource(APP, '_clearHistoryTruncation')} })`,
|
|
vm.createContext({})
|
|
) as Record<string, (...args: unknown[]) => void>;
|
|
return {
|
|
activeSessionId: null,
|
|
_paneHistoryLines: new Map<string, number>(),
|
|
_renderHistoryTruncationBanner() {},
|
|
...methods,
|
|
} as Record<string, any>;
|
|
}
|
|
|
|
describe('_notePaneHistory', () => {
|
|
it('records the count a capture reported, 0 included', () => {
|
|
const app = makeApp();
|
|
app._notePaneHistory('s1', { paneHistoryLines: 0 });
|
|
app._notePaneHistory('s2', { paneHistoryLines: 40000 });
|
|
expect(app._paneHistoryLines.get('s1')).toBe(0);
|
|
expect(app._paneHistoryLines.get('s2')).toBe(40000);
|
|
});
|
|
|
|
it('forgets on a response without a usable count, so unknown never reads as empty', () => {
|
|
const app = makeApp();
|
|
for (const payload of [{}, { paneHistoryLines: null }, { paneHistoryLines: NaN }, { paneHistoryLines: -1 }, null]) {
|
|
app._notePaneHistory('s1', { paneHistoryLines: 0 });
|
|
app._notePaneHistory('s1', payload);
|
|
expect(app._paneHistoryLines.has('s1')).toBe(false);
|
|
}
|
|
});
|
|
|
|
it('ignores a call without a session', () => {
|
|
const app = makeApp();
|
|
app._notePaneHistory(null, { paneHistoryLines: 0 });
|
|
expect(app._paneHistoryLines.size).toBe(0);
|
|
});
|
|
|
|
it('is dropped with the session', () => {
|
|
const app = makeApp();
|
|
app._notePaneHistory('s1', { paneHistoryLines: 0 });
|
|
app._clearHistoryTruncation('s1');
|
|
expect(app._paneHistoryLines.has('s1')).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('every terminal response feeds it (static guard)', () => {
|
|
it('the tab switch, the refresh and the history re-pull all record the pane', () => {
|
|
expect(methodSource(APP, 'selectSession')).toContain('this._notePaneHistory?.(sessionId, data);');
|
|
expect(methodSource(APP, '_onSessionNeedsRefresh')).toContain('this._notePaneHistory?.(sessionId, data);');
|
|
expect(methodSource(APP, '_maybeRefetchFullHistory')).toContain('this._notePaneHistory?.(sessionId, payload);');
|
|
});
|
|
|
|
it('the refresh records the pane only once it still owns the terminal, and neither it nor the pull records an error envelope', () => {
|
|
// A refresh that lost its owner is older than what the switch back read; an
|
|
// error envelope has no count, and forgetting on it would hand a scroll back
|
|
// to the paging fallback (see _maybePageCliTranscript).
|
|
const refresh = methodSource(APP, '_onSessionNeedsRefresh');
|
|
const ownerCheck = refresh.indexOf('this._terminalRefreshOwner !== refreshOwner) return false;');
|
|
expect(ownerCheck).toBeGreaterThan(-1);
|
|
expect(refresh.indexOf('this._notePaneHistory?.(sessionId, data);')).toBeGreaterThan(ownerCheck);
|
|
expect(refresh).toContain('capture.json?.success !== false) this._notePaneHistory?.(sessionId, data);');
|
|
expect(methodSource(APP, '_maybeRefetchFullHistory')).toContain(
|
|
'capture.json?.success !== false) this._notePaneHistory?.(sessionId, payload);'
|
|
);
|
|
});
|
|
});
|