diff --git a/docs/api-reference.md b/docs/api-reference.md index 9a53134e..c0376960 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -551,7 +551,7 @@ geometry was read. The capture runs synchronous tmux calls on the server; the |---|---| | `full=1` | tmux's scrollback, not only the visible frame (`source: 'mux-full-history'`), ending with a relative cursor move back to the pane's caret. | | `tail=` | Keep the newest `` of the result (`truncationReason: 'tail'` when it cut). | -| `lines=` | With `full=1` only: read at most `` lines of tmux history above the visible frame. An integer of at least 1, clamped to the configured history limit; absent or malformed, the whole limit (100,000 lines by default), as before. `truncated` and `truncationReason` describe byte cuts only, not this bound. Without it a full capture reads all of that history before `tail` cuts it, so a client that keeps a fixed number of lines (the tile grid sends its xterm's scrollback plus its rows) should send it. | +| `lines=` | With `full=1` only: read at most `` lines of tmux history above the visible frame. An integer of at least 1, clamped to the configured history limit; absent or malformed, the whole limit (100,000 lines by default), as before. When the bound left older lines in tmux (its `history_size` is deeper), `truncated` is true with `truncationReason: 'tail'`; a byte cap still reports `'capped'`. Without it a full capture reads all of that history before `tail` cuts it, so a client that keeps a fixed number of lines (the tile grid sends its xterm's scrollback plus its rows) should send it. | ## The `codeman agent` CLI (client over these endpoints) diff --git a/docs/architecture-invariants.md b/docs/architecture-invariants.md index ce51923b..2a10199f 100644 --- a/docs/architecture-invariants.md +++ b/docs/architecture-invariants.md @@ -823,7 +823,7 @@ Tests: `test/tab-rail-search.test.ts` (gate) and `test/tab-rail-search.browser.t ⚠️ **The main terminal is parked while the grid is open.** Opening runs `_cleanupPreviousSession()` ONCE (its snapshot is right at that moment, and it closes the main socket), and `activeSessionId` is always the FOCUSED tile's session, so everything keyed on it (files panel, git status, respawn and Ralph panels, subagent windows, voice, image paste, the tab highlight) follows focus. With the main socket closed `_wsReady` is false, so every SSE terminal handler would write the focused tile's output into the hidden xterm: `_tilesOwnTerminal()` turns `_onSessionTerminal`, `_onSessionClearTerminal`, `_onSessionNeedsRefresh` (returns `false`, which the drop recovery reads), `_scheduleDroppedOutputRecovery`, the `writeln` in `_onSessionCompletion`/`_onSessionError`, `sendResize`, `throttledResize`'s fit and `_maybeRefetchFullHistory` into no-ops; `retryConnection` and `handleInit` re-arm the TILES' sockets instead of the main one (`handleInit` keeps live tiles, drops dead ones through `_reconcileTileGrid`, and re-selects only when the focused tile is gone, so an SSE blip never hides an open web tab). ⚠️ The page's SSE filter names `TILE_GRID_SSE_FILTER` (constants.js, an id no session takes) instead of the focused session while tiles own the terminal: the server's filter gates only `session:terminal` batches, which the parked terminal could only parse and drop. Both places that set the filter ask `_sseFilterSessionId()`: the live re-subscribe every tile focus runs (`_updateSseSubscription`) and the connect URL an SSE reconnect rebuilds (`connectSSE`); leaving the grid re-subscribes the shown session through `selectSession`. `test/sse-tile-grid-filter.test.ts` pins the server's side in multi-user mode (the id is accepted on connect and on re-subscribe, and session/hook events still reach their owner through it). ⚠️ The WebGL long-task observer watches the WHOLE page: it counts nothing while tiles own the terminal, or tile renders would write the sticky 7-day WebGL disable. The header connection dot reads the tile sockets (`_tileGridSocketState`; a tile stopped for good does not count). `_focusedPane()` answers with the focused tile even when DOM focus left every terminal, and `_forEachTile` reaches every grid tile (`{ grid: false }` skips them where tiles keep their own font size). Leaving the grid destroys every tile, resets `_lastResizeDims`, invalidates the main terminal's cached content for EVERY tiled id (`_xtermSnapshots`, `codeman-xs-`, `terminalBufferCache`: written before the grid opened, and selectSession paints a snapshot as its first frame) and replays the focused session through `selectSession(id, { forceReload: true, auto: true })`. -⚠️ **One load queue.** Every capture a tile fetches (initial load, refresh after a reconnect, server `{t:'r'}` refresh, server `{t:'c'}` refresh, dropped-output recovery refresh, shell history pull) goes through the grid's ONE `TileLoadQueue` (terminal-tile.js, the tile's `scheduleLoad` option): concurrency 1, a history pull first, then the focused tile, then reading order; a destroyed tile's waiting loads are dropped unrun and destroy() aborts its running fetch. A load holds the slot only while xterm parses its replay: `writeChunked` queues every 32 KB slice at once (one 1 MiB window at a time, since xterm's write queue throws past 50 MB) and settles on the callback of a write queued behind them, never one slice per animation frame (that pacing held the slot about a second per 1 MiB tile). ⚠️ destroy() settles a replay in progress (`_cancelReplay`): a disposed xterm never runs that callback, and an unsettled replay would hold the tile's flag and the grid's queue forever. Each `GET /api/sessions/:id/terminal` runs synchronous tmux calls, so N tiles loading at once (after a deploy restart all N reopen within a second) would stall every WebSocket and SSE stream on the server back to back. Grid tiles load the BOUNDED window (`full=1&tail=TERMINAL_TAIL_SIZE` for a TUI, `tail=` for a shell; the `boundedLoad` option), and every full capture of theirs (a TUI load, a shell history pull) also sends `lines=`, so tmux reads no more history than the tile keeps instead of the whole history limit (the route's optional `lines` bound, docs/api-reference.md; the split's Pane B sends none), keep `TILE_SCROLLBACK` lines and their own per-device font (`codeman-tile-font-size`, Ctrl +/- while the grid is open). A refresh fetches at its turn, not when asked, and resets the screen with the queued in-stream `\x1bc` only once its capture is in hand, right before the replay (never xterm's `clear()` before the fetch), so a tile keeps its last frame through its wait and its own round trip; a failed, aborted or empty fetch writes nothing and resets nothing, and the tile keeps its last frame and every held live frame. +⚠️ **One load queue.** Every capture a tile fetches (initial load, refresh after a reconnect, server `{t:'r'}` refresh, server `{t:'c'}` refresh, dropped-output recovery refresh, shell history pull) goes through the grid's ONE `TileLoadQueue` (terminal-tile.js, the tile's `scheduleLoad` option): concurrency 1, a history pull first, then the focused tile, then reading order; a destroyed tile's waiting loads are dropped unrun and destroy() aborts its running fetch. A load holds the slot only while xterm parses its replay: `writeChunked` queues every 32 KB slice at once (one 1 MiB window at a time, since xterm's write queue throws past 50 MB) and settles on the callback of a write queued behind them, never one slice per animation frame (that pacing held the slot about a second per 1 MiB tile). ⚠️ destroy() settles a replay in progress (`_cancelReplay`): a disposed xterm never runs that callback, and an unsettled replay would hold the tile's flag and the grid's queue forever. Each `GET /api/sessions/:id/terminal` runs synchronous tmux calls, so N tiles loading at once (after a deploy restart all N reopen within a second) would stall every WebSocket and SSE stream on the server back to back. Grid tiles load the BOUNDED window (`full=1&tail=TERMINAL_TAIL_SIZE` for a TUI, `SHELL_LOAD_QUERY` for a shell, as every shell load is; the `boundedLoad` option), and every full capture of theirs (a TUI load, a shell history pull) also sends `lines=`, so tmux reads no more history than the tile keeps instead of the whole history limit (the route's optional `lines` bound, docs/api-reference.md; the split's Pane B sends none), keep `TILE_SCROLLBACK` lines and their own per-device font (`codeman-tile-font-size`, Ctrl +/- while the grid is open). A refresh fetches at its turn, not when asked, and resets the screen with the queued in-stream `\x1bc` only once its capture is in hand, right before the replay (never xterm's `clear()` before the fetch), so a tile keeps its last frame through its wait and its own round trip; a failed, aborted or empty fetch writes nothing and resets nothing, and the tile keeps its last frame and every held live frame. ⚠️ **Selections.** The tile branch of `selectSession` sits right after its "already active" early return: a tiled id is FOCUSED (`_selectTiledSession`: no cleanup, replay, resize or main socket; the shared `_refreshSessionPanels`; only a user-initiated pick acknowledges the idle alert). A USER-initiated pick of a session that is not tiled leaves the grid for the single view, remembered (decision 1), and so does `leaveTiles` (a followed `#session=` link); an `auto` pick never collapses it. Whoever calls `closeTileGrid({ reselect: false })` and then selects must null `activeSessionId` first, or `_cleanupPreviousSession` saves the parked terminal's stale content as a snapshot. App-driven fallbacks pick a tile: `closeSession` captures the neighbouring tile BEFORE it tears the tile down (like `wasActive`) and focuses it with `auto` before the delete request goes out; the `_onSessionDeleted` wrapper does the same for a delete from elsewhere, and leaves a close from this tab (`_closingSessions`) to `closeSession`. Ctrl+Tab and Alt+[ ] cycle the tiles. A popped-out (detached) session leaves the grid. Moving focus off a zoomed tile restores the grid (tmux `select-pane`); an automatic zoom (window too small for the minimum tile) follows focus instead. diff --git a/src/mux-interface.ts b/src/mux-interface.ts index 56214633..ab96ef19 100644 --- a/src/mux-interface.ts +++ b/src/mux-interface.ts @@ -204,6 +204,13 @@ export interface PaneCaptureOptions { * rendering it needs the real height to know the frame fits. */ capturedGeometry?: { cols: number; rows: number }; + /** + * Filled in by the implementation with the pane's scrollback depth (lines + * above the screen) at capture time, when it can tell. A full-history + * capture bounded by `historyLimitLines` below this depth left older lines + * behind in the multiplexer, which the caller must report as recoverable. + */ + capturedHistorySize?: number; } /** diff --git a/src/tmux-manager.ts b/src/tmux-manager.ts index d7974a75..f572156a 100644 --- a/src/tmux-manager.ts +++ b/src/tmux-manager.ts @@ -700,13 +700,17 @@ interface PaneCursorGeometry { rows: number; cursorX: number; cursorY: number; + /** Lines of scrollback above the screen (`#{history_size}`), when tmux reported it. */ + historySize?: number; } /** * Read the pane's cursor and size, or null when tmux cannot say. * - * Every field is validated together: a caller that gets a value back can place - * a caret with it, and one that gets null must not try. + * The four geometry fields are validated together: a caller that gets a value + * back can place a caret with it, and one that gets null must not try. The + * optional fifth field, the history depth, is only informational, so a bad one + * is dropped rather than discarding a good caret. */ export function queryPaneCursor(run: () => string): PaneCursorGeometry | null { let raw: string; @@ -716,7 +720,7 @@ export function queryPaneCursor(run: () => string): PaneCursorGeometry | null { console.error('[TmuxManager] Failed to query pane cursor after capture:', cursorErr); return null; } - const [cursorX, cursorY, cols, rows] = raw.split(/\s+/).map((value) => parseInt(value, 10)); + const [cursorX, cursorY, cols, rows, historySize] = raw.split(/\s+/).map((value) => parseInt(value, 10)); if ( !Number.isFinite(cursorX) || !Number.isFinite(cursorY) || @@ -729,7 +733,9 @@ export function queryPaneCursor(run: () => string): PaneCursorGeometry | null { ) { return null; } - return { cols, rows, cursorX, cursorY }; + const geometry: PaneCursorGeometry = { cols, rows, cursorX, cursorY }; + if (Number.isFinite(historySize) && historySize >= 0) geometry.historySize = historySize; + return geometry; } /** SGR attributes, which is all `capture-pane -e` emits. */ @@ -3890,7 +3896,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer { // to keep when a move follows to put the caret back above them. const geometry = queryPaneCursor(() => execSync( - `${this.tmux()} display-message -p -t ${shellescape(target)} '#{cursor_x} #{cursor_y} #{pane_width} #{pane_height}'`, + `${this.tmux()} display-message -p -t ${shellescape(target)} '#{cursor_x} #{cursor_y} #{pane_width} #{pane_height} #{history_size}'`, { encoding: 'utf-8', timeout: EXEC_TIMEOUT_MS } ) ); @@ -3902,6 +3908,9 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer { // geometry is reported there for diagnosis rather than for repair. Only // the caller can see both sizes, so hand it this one. if (opts && geometry) opts.capturedGeometry = { cols: geometry.cols, rows: geometry.rows }; + // How deep tmux's history really is, so a caller that bounded the capture + // with `historyLimitLines` can tell whether the bound cut anything. + if (opts && geometry?.historySize !== undefined) opts.capturedHistorySize = geometry.historySize; if (fullHistory) { // Without geometry there is no cursor move, so fall back to the old trim. diff --git a/src/web/public/app.js b/src/web/public/app.js index dda35644..42747eb8 100644 --- a/src/web/public/app.js +++ b/src/web/public/app.js @@ -3203,15 +3203,16 @@ class CodemanApp { this._terminalRefreshOwner = refreshOwner; try { // A shell can retain a multi-megabyte/100k-line tmux history. Automatic - // recovery stays bounded just like normal shell selection; only the - // explicit "Load full history" action is allowed to pay for a full replay. + // recovery stays bounded just like normal shell selection (tmux's rendered + // history, SHELL_LOAD_QUERY); only the explicit "Load full history" action + // is allowed to pay for a full replay. // TUI modes still recover the whole picture, with the downgrade guard for // repaint-mode panes whose tmux capture can be smaller than xterm's buffer. const useFullHistory = this.sessions.get(sessionId)?.mode !== 'shell'; let capture = await this._fetchTerminalCapture( useFullHistory ? `/api/sessions/${sessionId}/terminal?full=1` - : `/api/sessions/${sessionId}/terminal?tail=${TERMINAL_TAIL_SIZE}`, + : `/api/sessions/${sessionId}/terminal?${SHELL_LOAD_QUERY}`, { full: useFullHistory } ); let headersReceivedAt = capture.headersAt; @@ -3308,8 +3309,13 @@ class CodemanApp { try { // No-param capture: `terminalBufferMaxBytes` (32MB) is its only ceiling, // so it needs the full-history budget. Defaulting to the tail budget - // gave the largest payload the smallest deadline. - const capture = await this._fetchTerminalCapture(`/api/sessions/${data.id}/terminal`, { full: true }); + // gave the largest payload the smallest deadline. A shell reloads tmux's + // rendered history instead (SHELL_LOAD_QUERY), never the byte recording. + const shellSession = this.sessions.get(data.id)?.mode === 'shell'; + const capture = await this._fetchTerminalCapture( + shellSession ? `/api/sessions/${data.id}/terminal?${SHELL_LOAD_QUERY}` : `/api/sessions/${data.id}/terminal`, + { full: !shellSession } + ); const headersReceivedAt = capture.headersAt; const termData = capture.json?.data ?? {}; @@ -9317,16 +9323,23 @@ class CodemanApp { // TUI sessions still get one canonical full replay per page (COD-47/#205). // A shell can retain hundreds of thousands of plain scrollback lines, so // automatically replaying all of them makes tab selection scale with the - // entire session. Load its bounded 1MB tail first; the existing truncation - // banner action fetches ?full=1 when the user explicitly asks for it. - const useFullHistory = session?.mode !== 'shell' && !this._fullHistoryLoaded.has(sessionId); + // entire session. Load a bounded window of tmux's RENDERED history first + // (SHELL_LOAD_QUERY: never the raw byte recording, whose repaints replay + // wrong at another pane height); the existing truncation banner action + // fetches ?full=1 when the user explicitly asks for it. + const shellSession = session?.mode === 'shell'; + const useFullHistory = !shellSession && !this._fullHistoryLoaded.has(sessionId); if (useFullHistory) this._fullHistoryLoaded.add(sessionId); const fetchStartedAt = performance.now(); const tailUrl = `/api/sessions/${sessionId}/terminal?tail=${TERMINAL_TAIL_SIZE}`; let capture; try { capture = await this._fetchTerminalCapture( - useFullHistory ? `/api/sessions/${sessionId}/terminal?full=1` : tailUrl, + useFullHistory + ? `/api/sessions/${sessionId}/terminal?full=1` + : shellSession + ? `/api/sessions/${sessionId}/terminal?${SHELL_LOAD_QUERY}` + : tailUrl, { full: useFullHistory } ); } catch (err) { diff --git a/src/web/public/constants.js b/src/web/public/constants.js index 8a1d500f..c0f678ca 100644 --- a/src/web/public/constants.js +++ b/src/web/public/constants.js @@ -116,6 +116,16 @@ const MAX_NOTIFICATION_DURATION_MS = 300000; // Longest configurable toast / bro const THROTTLE_DELAY_MS = 100; // General UI throttle delay const TERMINAL_CHUNK_SIZE = 32 * 1024; // 32KB chunks for terminal buffer loading const TERMINAL_TAIL_SIZE = 1024 * 1024; // 1MB tail for initial load (more scrollback on tab switch) +// A shell's automatic loads (tab select, drop recovery, a tile's load) read +// tmux's RENDERED history, never the raw byte recording: that recording holds +// full-screen repaints (an ssh'd tmux, less, a TUI) drawn for the pane size of +// the moment, and replayed into a terminal of another height each repaint +// scrolls its top row into scrollback, so one line shows up dozens of times. +// `lines=` bounds the synchronous capture-pane (~8 ms per 1000 lines measured, +// so ~0.1 s here); the server reports a cut as 'tail', and scroll-to-top pulls +// the older lines as before. +const SHELL_LOAD_HISTORY_LINES = 10000; +const SHELL_LOAD_QUERY = `full=1&tail=${TERMINAL_TAIL_SIZE}&lines=${SHELL_LOAD_HISTORY_LINES}`; const SYNC_WAIT_TIMEOUT_MS = 50; // Wait timeout for terminal sync const STATS_POLLING_INTERVAL_MS = 2000; // System stats polling const TUI_REDRAW_SETTLE_MS = 400; // Grace for a TUI to redraw after a real resize, before fetching its buffer @@ -1902,9 +1912,13 @@ function computeHistoryTruncationNotice(state = {}) { canLoadMore: false, }; } + // A `lines=`-bounded capture is cut by tmux before any bytes are counted, so + // its fullSize is the window itself and the remainder's size is unknown: + // say that more exists without inventing an amount. + const remainder = dropped > 0 ? `${formatHistoryBytes(dropped)} more` : 'Earlier output'; return { visible: true, - message: `Showing the most recent ${shown} of this session. ${formatHistoryBytes(dropped)} more may still be retained.`, + message: `Showing the most recent ${shown} of this session. ${remainder} may still be retained.`, canLoadMore: true, }; } diff --git a/src/web/public/terminal-tile.js b/src/web/public/terminal-tile.js index 5ac18139..3a8fa755 100644 --- a/src/web/public/terminal-tile.js +++ b/src/web/public/terminal-tile.js @@ -48,7 +48,7 @@ * Android tablet, or a large foldable unfolded in landscape, reaches. * * @dependency vendor/xterm.js, vendor/xterm-addon-fit.js - * @dependency constants.js (window.CodemanTerminalFont, window.CodemanFetchDeadline, DEFAULT_SCROLLBACK, TERMINAL_TAIL_SIZE, TERMINAL_CHUNK_SIZE) + * @dependency constants.js (window.CodemanTerminalFont, window.CodemanFetchDeadline, DEFAULT_SCROLLBACK, TERMINAL_TAIL_SIZE, SHELL_LOAD_QUERY, TERMINAL_CHUNK_SIZE) * @dependency terminal-ui.js (codemanCurrentXtermTheme, codemanCurrentSkinIsLight, CodemanTerminalInput.shouldSuppressTerminalQueryResponse/isTerminalFocusOrMouseReport/wheelDeltaLines/wheelDeltaWholeLines/sgrWheelReports/pageKeysForTravel, app._shouldForwardWheelToApp/_localScrollbackIsHollow/_terminalViewportAtBottom/_clientPointToCell/_handleDesktopTerminalClick) * @dependency terminal-keycode229-recovery.js (window.CodemanKeyCode229Recovery, optional: absent, xterm's own textarea handling stands) * @loadorder 7.4 of 16, loaded after terminal-ui.js and before terminal-split.js @@ -770,7 +770,9 @@ try { if (this._destroyed) return; const shell = this.sessionMode === 'shell'; - let query = shell ? `tail=${TERMINAL_TAIL_SIZE}` : 'full=1'; + // A shell loads tmux's rendered history, bounded (SHELL_LOAD_QUERY, + // constants.js), never the raw byte recording. + let query = shell ? SHELL_LOAD_QUERY : 'full=1'; if (this.boundedLoad && !shell) query = `full=1&tail=${TERMINAL_TAIL_SIZE}${this._historyLinesQuery()}`; this._loadAbort = controller; armDeadline(global.CodemanFetchDeadline?.terminalFetchDeadlineMs?.({ full: !shell }) ?? 45000); diff --git a/src/web/routes/session-routes.ts b/src/web/routes/session-routes.ts index 5960d9e6..5b0ba30f 100644 --- a/src/web/routes/session-routes.ts +++ b/src/web/routes/session-routes.ts @@ -3160,6 +3160,21 @@ export function registerSessionRoutes( let truncationReason: 'capped' | 'tail' | null = null; let cleanBuffer: string; + // A `lines=` bound shallower than tmux's history left the older lines in + // tmux: an intentional partial read, so 'tail', and a later pull with a + // deeper (or no) bound recovers them. Without this a bounded window looks + // like the whole history to the client. The byte cap below still wins. + const capturedHistorySize = captureOpts.capturedHistorySize; + if ( + isFullCapture && + captureOpts.historyLimitLines !== undefined && + capturedHistorySize !== undefined && + capturedHistorySize > captureOpts.historyLimitLines + ) { + truncated = true; + truncationReason = 'tail'; + } + // Cap the payload EARLY — before the regex normalization passes below run // over it. A full-history tmux capture can be tens of MB of scrollback; // normalizing all of it would stall the event loop only to discard most diff --git a/test/history-truncation-notice.test.ts b/test/history-truncation-notice.test.ts index 50eb5735..782c720a 100644 --- a/test/history-truncation-notice.test.ts +++ b/test/history-truncation-notice.test.ts @@ -75,6 +75,21 @@ describe('computeHistoryTruncationNotice (issue #258)', () => { expect(notice.message).toContain('more may still be retained'); }); + it('names no amount when tmux cut the window by lines before any bytes were counted', () => { + // A shell's `lines=`-bounded load: the server knows tmux holds more, but the + // window IS the whole payload, so fullSize equals what was kept. + const notice = computeHistoryTruncationNotice({ + truncated: true, + reason: 'tail', + source: 'mux-full-history', + fullSize: 800 * 1024, + retainedBytes: 800 * 1024, + }); + expect(notice.canLoadMore).toBe(true); + expect(notice.message).toContain('Earlier output may still be retained'); + expect(notice.message).not.toMatch(/\d+ (B|KB) more/); + }); + it('promises nothing more once the FULL capture itself hit the ceiling', () => { // This is the case the old boolean could not express: a full-history pull // that was still capped means tmux has already given everything it has. @@ -124,7 +139,10 @@ describe('the in-terminal truncation line is gone (static guard)', () => { it('loads a bounded shell tail first and keeps unbounded full history user-triggered', () => { const app = readFileSync(resolve(PUBLIC, 'app.js'), 'utf8'); - expect(app).toContain("session?.mode !== 'shell' && !this._fullHistoryLoaded.has(sessionId)"); + expect(app).toContain("const shellSession = session?.mode === 'shell';"); + expect(app).toContain('const useFullHistory = !shellSession && !this._fullHistoryLoaded.has(sessionId);'); + // The shell's bounded load is tmux's rendered history, never the byte recording. + expect(app).toContain('`/api/sessions/${sessionId}/terminal?${SHELL_LOAD_QUERY}`'); expect(app).toContain("!restoredSnapshot && session?.mode !== 'shell'"); expect(app).toContain('`/api/sessions/${sessionId}/terminal?tail=${TERMINAL_TAIL_SIZE}`'); // Every terminal capture now goes through _fetchTerminalCapture, which adds diff --git a/test/routes/session-routes.test.ts b/test/routes/session-routes.test.ts index 7dc6c4cf..41b779b7 100644 --- a/test/routes/session-routes.test.ts +++ b/test/routes/session-routes.test.ts @@ -1154,6 +1154,52 @@ describe('session-routes', () => { expect(captureSpy).toHaveBeenCalledWith(harness.ctx._session.muxName, {}); }); + // A shell's tab-select load reads a `lines=` window SMALLER than xterm's + // scrollback, so a cut there is real: the client must hear that tmux holds + // more, or it treats the window as the whole history and scroll-to-top + // stops offering the rest. tmux reports its history depth on the capture. + it('reports a `lines=` cut of tmux history as a recoverable tail truncation', async () => { + harness.ctx._session.mode = 'shell'; + const lineCountFor = async (lines: number, historySize: number | undefined) => { + (harness.ctx.mux as { captureActivePaneBuffer?: unknown }).captureActivePaneBuffer = vi.fn( + (_name: string, opts?: { capturedHistorySize?: number }) => { + if (opts && historySize !== undefined) opts.capturedHistorySize = historySize; + return 'captured rows\r\nlast row'; + } + ); + const res = await harness.app.inject({ + method: 'GET', + url: `/api/sessions/${harness.ctx._sessionId}/terminal?full=1&tail=${1024 * 1024}&lines=${lines}`, + }); + const data = JSON.parse(res.body).data; + return { truncated: data.truncated, reason: data.truncationReason }; + }; + + expect(await lineCountFor(10000, 25000)).toEqual({ truncated: true, reason: 'tail' }); + expect(await lineCountFor(10000, 10000)).toEqual({ truncated: false, reason: null }); + expect(await lineCountFor(10000, 400)).toEqual({ truncated: false, reason: null }); + // No depth reported (an older tmux, a failed query): nothing is claimed. + expect(await lineCountFor(10000, undefined)).toEqual({ truncated: false, reason: null }); + }); + + it('lets the byte cap outrank a `lines=` cut: the oldest bytes of the window are gone for good', async () => { + harness.ctx._session.mode = 'shell'; + const { terminalBufferMaxBytes } = await harness.ctx.getTerminalHistoryConfig(); + (harness.ctx.mux as { captureActivePaneBuffer?: unknown }).captureActivePaneBuffer = vi.fn( + (_name: string, opts?: { capturedHistorySize?: number }) => { + if (opts) opts.capturedHistorySize = 25000; + return `${'x'.repeat(100)}\r\n`.repeat(Math.ceil(terminalBufferMaxBytes / 102) + 10); + } + ); + const res = await harness.app.inject({ + method: 'GET', + url: `/api/sessions/${harness.ctx._sessionId}/terminal?full=1&lines=10000`, + }); + const data = JSON.parse(res.body).data; + expect(data.truncated).toBe(true); + expect(data.truncationReason).toBe('capped'); + }); + it('a capture bounded by `lines=` is still a full capture: rows kept, cursor restore last', async () => { // Only how much history tmux reads changes. The row-preserving skips key // on isFullCapture, and the relative cursor move must still end it. diff --git a/test/terminal-flush-budget.test.ts b/test/terminal-flush-budget.test.ts index e5120ca6..e398c6bc 100644 --- a/test/terminal-flush-budget.test.ts +++ b/test/terminal-flush-budget.test.ts @@ -159,13 +159,36 @@ describe('terminal flush budget', () => { const first = app._onSessionNeedsRefresh({ id: 'session-1' }); const duplicate = app._onSessionNeedsRefresh({ id: 'session-1' }); expect(fetchMock).toHaveBeenCalledOnce(); - expect(fetchMock).toHaveBeenCalledWith('/api/sessions/session-1/terminal?tail=1048576'); + expect(fetchMock).toHaveBeenCalledWith('/api/sessions/session-1/terminal?full=1&tail=1048576&lines=10000'); releaseFetch(); await Promise.all([first, duplicate]); expect(app._terminalRefreshOwner).toBe(null); }); + it("reloads a cleared shell from tmux's rendered history and an agent from the byte recording", async () => { + const { CodemanApp, fetchMock } = loadAppHarness(); + const app = Object.create(CodemanApp.prototype) as any; + app.sessions = new Map([ + ['shell-1', { mode: 'shell' }], + ['agent-1', { mode: 'claude' }], + ]); + app._isLoadingBuffer = false; + app._resetTerminalForReplay = vi.fn(); + app.sendResize = vi.fn(); + fetchMock.mockResolvedValue({ json: async () => ({ data: { terminalBuffer: '' } }) }); + + app.activeSessionId = 'shell-1'; + await app._onSessionClearTerminal({ id: 'shell-1' }); + app.activeSessionId = 'agent-1'; + await app._onSessionClearTerminal({ id: 'agent-1' }); + + expect(fetchMock.mock.calls.map((call) => call[0])).toEqual([ + '/api/sessions/shell-1/terminal?full=1&tail=1048576&lines=10000', + '/api/sessions/agent-1/terminal', + ]); + }); + it('drains a large final batch without waiting for unrelated terminal output', () => { const { app, writes } = loadTerminalUiHarness('codex'); const scheduled: Array<() => void> = []; diff --git a/test/terminal-tile-unit.test.ts b/test/terminal-tile-unit.test.ts index e24f6680..daf645f1 100644 --- a/test/terminal-tile-unit.test.ts +++ b/test/terminal-tile-unit.test.ts @@ -24,6 +24,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; const TERMINAL_CHUNK_SIZE = 32 * 1024; const TERMINAL_TAIL_SIZE = 1024 * 1024; +// constants.js: a shell loads tmux's rendered history, bounded, never the raw byte recording. +const SHELL_LOAD_QUERY = `full=1&tail=${TERMINAL_TAIL_SIZE}&lines=10000`; /** The pane's `performance.now()`, so frame arrival vs. capture time is set by hand, not raced. */ let clock = 0; @@ -108,6 +110,7 @@ function loadTerminalTile() { // The constants.js globals the module reads at call time. TERMINAL_CHUNK_SIZE, TERMINAL_TAIL_SIZE, + SHELL_LOAD_QUERY, }); // The module's tail patches CodemanApp.prototype; nothing on it runs here. vm.runInContext(`class CodemanApp { _onSessionDeleted() {} selectSession() {} }\n${SOURCE}`, context); @@ -266,14 +269,17 @@ describe('TerminalTile server-refresh single-flight', () => { expect(pane._bufferLoading).toBe(false); }); - it('a shell pane asks for the bounded tail, matching connect()', async () => { + it("a shell pane asks for a bounded window of tmux's rendered history, matching connect()", async () => { const pane = makePane('shell'); fetchMock.mockResolvedValueOnce(jsonResponse('tail')); pane._refreshBuffer(); await settle(); - expect(fetchMock).toHaveBeenCalledWith(`/api/sessions/s1/terminal?tail=${1024 * 1024}`, expect.anything()); + expect(fetchMock).toHaveBeenCalledWith( + `/api/sessions/s1/terminal?full=1&tail=${1024 * 1024}&lines=10000`, + expect.anything() + ); }); it('refreshes arriving mid-fetch neither reset nor fetch again, and run ONCE after the replay lands', async () => { @@ -865,10 +871,7 @@ describe('TerminalTile scroll-to-top history pull', () => { // current screen and replays it last. expect(order.slice(0, 2)).toEqual(['write:before', 'write:after']); expect(fetchMock).toHaveBeenCalledTimes(2); - expect(fetchMock).toHaveBeenLastCalledWith( - `/api/sessions/s1/terminal?tail=${TERMINAL_TAIL_SIZE}`, - expect.anything() - ); + expect(fetchMock).toHaveBeenLastCalledWith(`/api/sessions/s1/terminal?${SHELL_LOAD_QUERY}`, expect.anything()); expect(order.at(-1)).toBe('write:after startup'); expect(pane._liveQueue).toBeNull(); expect(pane._bufferLoading).toBe(false); diff --git a/test/tile-grid-load-queue.test.ts b/test/tile-grid-load-queue.test.ts index 53b60143..41a3d92c 100644 --- a/test/tile-grid-load-queue.test.ts +++ b/test/tile-grid-load-queue.test.ts @@ -216,14 +216,14 @@ describe('initial loads', () => { expect(states.filter(([id]) => id === 'b').map(([, s]) => s)).toEqual(['queued', 'running', 'idle']); }); - it('loads a bounded window: full=1&tail= for a TUI, tail= for a shell', async () => { + it("loads a bounded window of tmux's rendered history: the tile's lines for a TUI, the shell bound for a shell", async () => { const { tiles } = makeGrid(['tui', 'sh'], { modes: { sh: 'shell' } }); const connecting = tiles.map((t) => t.connect()); await drain(); await Promise.all(connecting); expect(captures.map((c) => c.url)).toEqual([ `/api/sessions/tui/terminal?full=1&tail=${TAIL}${LINES}`, - `/api/sessions/sh/terminal?tail=${TAIL}`, + `/api/sessions/sh/terminal?full=1&tail=${TAIL}&lines=10000`, ]); }); diff --git a/test/tmux-capture-full-history.test.ts b/test/tmux-capture-full-history.test.ts index 4460f75a..771d8957 100644 --- a/test/tmux-capture-full-history.test.ts +++ b/test/tmux-capture-full-history.test.ts @@ -11,7 +11,7 @@ import { readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import { describe, expect, it } from 'vitest'; -import { formatCursorRestore, formatPaneSnapshot, hasVisibleContent } from '../src/tmux-manager.js'; +import { formatCursorRestore, formatPaneSnapshot, hasVisibleContent, queryPaneCursor } from '../src/tmux-manager.js'; describe('tmux full-history pane capture (COD-47)', () => { const source = readFileSync(resolve(import.meta.dirname, '../src/tmux-manager.ts'), 'utf8'); @@ -171,3 +171,27 @@ describe('why a capture has to report its height', () => { expect(addressed.filter((row) => row > 30)).toHaveLength(20); }); }); + +describe('queryPaneCursor history depth', () => { + it('reads tmux history_size as an optional fifth field', () => { + expect(queryPaneCursor(() => '4 22 133 24 25000\n')).toEqual({ + cols: 133, + rows: 24, + cursorX: 4, + cursorY: 22, + historySize: 25000, + }); + }); + + it('keeps the geometry and omits the depth when the fifth field is missing or invalid', () => { + for (const raw of ['4 22 133 24', '4 22 133 24 abc', '4 22 133 24 -1']) { + const geometry = queryPaneCursor(() => raw); + expect(geometry, raw).toEqual({ cols: 133, rows: 24, cursorX: 4, cursorY: 22 }); + } + }); + + it('asks tmux for the history depth in the same display-message as the cursor', () => { + const source = readFileSync(resolve(import.meta.dirname, '../src/tmux-manager.ts'), 'utf8'); + expect(source).toContain("'#{cursor_x} #{cursor_y} #{pane_width} #{pane_height} #{history_size}'"); + }); +});