Skip to content

terminal: the live pane keeps its history and its place across a refresh (#152) - #159

Merged
Lexus2016 merged 1 commit into
mainfrom
fix/152-pane-history-and-scroll
Nov 26, 2026
Merged

Lexus2016 merged 1 commit into
mainfrom
fix/152-pane-history-and-scroll

Conversation

@Lexus2016

Copy link
Copy Markdown
Owner

Fixes #152.

What was wrong

A re-attach bootstrapped the browser from capture-pane -p -e — the visible screen only. terminal-bridge.js already had -S - for the reaper's saved scrollback, so the history existed on the server side and was simply never sent to a viewer. A refresh therefore came back with the current screen and nothing above it, and xterm's own scrollback: 10000 started empty. The pane's horizontal offset was lost with the same reload.

What changed

Server

  • captureScreen(name, historyLines) — adds -S -N and keeps the old argv byte-identical when historyLines is 0, so the compositor's repaint and every existing caller are untouched.
  • attach({ …, historyLines }) forwards it to the bootstrap only; BOOTSTRAP_HISTORY_LINES = 2000 (tmux's default history-limit, under xterm's 10000-line scrollback).
  • /ws/terminal reads hist, clamps it to 0..10000 — it is client input — and passes it to both attach paths (the engine viewer and an ordinary terminal).

Client

  • Only a NEW xterm asks for the history (first open, page load, reload). A reconnect of a live one sends nothing: its buffer already holds that history, and replaying it again would append a duplicate of every line the reader scrolls back through.
  • entry.booted flips on the first parsed frame, not on open, so a connect that dies before painting asks again on its retry instead of leaving an empty buffer with no history either.
  • The reader's place is saved as lines above the bottom — an absolute viewportY would land elsewhere in a replayed buffer of a different length — plus the pane's scrollLeft (engine panes wider than the window scroll sideways), and restored from the first frame's term.write callback, the only moment the buffer exists to scroll. localStorage carries it across a reload; the in-memory Map avoids a storage write per scroll frame.

Verification

  • test/terminal-history.test.js (new): the argv shape (including the unchanged visible-only form), the server clamp and both attach call sites, the client's ask-once wiring, and the save/restore pair run against a fake xterm — including a different-length buffer and a storage that refuses writes.
  • test/terminal-bridge.integration.test.js gains a real-tmux case: a 120-line pane, where the visible-only capture has lost the first line and a bootstrap with history has it. tmux is not installed on this host, so that one runs in CI.
  • Locally green: terminal-history (21), composer-terminal (71), terminal-session (52), terminal-schema (9), tmux-composite (26), engine-pane (10), engine-idle-reaper (24), engine-prompts (17), terminal-pane-guard (5), i18n-completeness (139), running-sessions (17), session-liveness (15), frontend-recovery (10), frontend-refresh-races (8), script-scope, pane-font, engine-default; node -c server.js and node -c terminal-bridge.js.

The full npm test cannot complete on this host: task-backlog.test.js spawns a server with HOST=localhost, which binds ::1 here, while its fetch('http://localhost:…') probe reaches 127.0.0.1 first and never connects. That is a local resolver/binding mismatch, unrelated to this change, and the same suite is green in this PR's CI on Node 20 and 24.

…esh (#152)

A re-attach bootstrapped the browser from `capture-pane -p -e` — the visible screen
only. A page refresh therefore threw away everything above it, and the xterm's own
scrollback started empty: the pane came back with no way to read the turn that had
just scrolled off. The horizontal offset was lost with it.

Server: `captureScreen(name, historyLines)` adds `-S -N` and `attach()` forwards a
`historyLines` option, bounded by BOOTSTRAP_HISTORY_LINES (2000 — tmux's default
history-limit, and under xterm's 10000-line scrollback). The compositor's repaint and
every existing caller keep the old visible-only shape. `/ws/terminal` reads `hist`,
clamps it to 0..10000 (client input) and passes it to BOTH attach paths.

Client: only a NEW xterm asks for the history (first open, page load, reload).
A reconnect of a live one sends nothing — its buffer already holds it, and a second
replay would duplicate every line the reader scrolls back through. `booted` flips on
the first PARSED frame, not on open, so a connect that dies before painting asks again
on its retry. The reader's place is stored as lines ABOVE the bottom (a replayed
buffer of a different length lands in the same place) plus the pane's scrollLeft, and
restored from the first frame's write callback, which is the only moment the buffer
exists to scroll.

Verified locally: test/terminal-history.test.js (new, 21 checks, the save/restore pair
RUN against a fake xterm), composer-terminal, terminal-session, terminal-schema,
tmux-composite, engine-pane, engine-idle-reaper, engine-prompts, engine-prompt-replay,
terminal-keys, terminal-clipboard, terminal-addons, terminal-pane-guard, responsive-ui,
i18n-completeness, running-sessions, session-liveness, frontend-recovery,
frontend-refresh-races, script-scope/pane-font/engine-default — all green. tmux is not
installed on this host, so the new integration check (a 120-line pane: visible-only
capture loses line 1, a bootstrap with history has it) runs in CI, where tmux is.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T18:15:53.448610Z c266cb5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c266cb52f4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread terminal-bridge.js
return;
}
const screen = captureScreen(name); // synchronous — see the bootstrap note above
const screen = captureScreen(name, historyLines); // synchronous — see the bootstrap note above

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve trailing screen rows when replaying history

When a pane has scrollback and blank rows below its current content (for example, after clearing the screen and returning to a prompt), the history bootstrap misaligns the visible screen. captureScreen() removes all trailing newlines, including those representing the blank screen rows. Replaying history then leaves the last populated row at the bottom of xterm, while cursorSeq() positions the cursor at its original screen row. For example, replaying 30 history lines plus a prompt originally on row 0 into a 24-row xterm places the prompt on row 23 but the cursor on row 0, so subsequent output overwrites unrelated text. Preserve the trailing screen rows for history captures, removing only the capture's final output delimiter.

Useful? React with 👍 / 👎.

@Lexus2016
Lexus2016 merged commit a1eb0e4 into main Nov 26, 2026
3 checks passed
@Lexus2016
Lexus2016 deleted the fix/152-pane-history-and-scroll branch November 26, 2026 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Live pane: a refresh loses the horizontal scroll and the history

1 participant