Skip to content

feat(tui): page session history instead of loading it whole - #803

Merged
huichen merged 4 commits into
mainfrom
feat/tui-history-paging
Sep 23, 2026
Merged

huichen merged 4 commits into
mainfrom
feat/tui-history-paging

Conversation

@huichen

@huichen huichen commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Paged session history in the TUI

Why

Switching to a session read its entire transcript in one response:

Switched to session: Jev 技能推荐 — its transcript could not be loaded
(Error, decoded message length too large: found 10342115 bytes, the limit is: 4194304 bytes).

#800 fixed the symptom (tonic's 4 MiB decoding default was never raised on the TUI's client — the agent already encoded up to 32 MiB) but left the unbounded read in place: a 10 MB session still crosses the wire in one message, and a session past 32 MiB still cannot be opened.

The agent has had an indexed history pager all along — get_session_entries with an exclusive backward cursor (before), a limit counted in user exchanges, and hasMore/nextOffset for the continuation. The desktop (local GUI and the mobile bridge) already reads display history that way; the TUI was the client still using get_messages, which has no cursor at all.

What changed

  • tui/src/rpc/types.rs — SessionEntriesPage parses a page and carries the cursor. It also accepts the legacy whole-history messages shape, so one mapper renders both.
  • tui/src/rpc/grpc_client.rs — session_tail_page / session_older_page / session_entries_page build the request (before, limit, explicit session id) and decode it. The page size (10 exchanges) matches the desktop's remote history page.
  • tui/src/app.rs
    • load_history_tail reads the tail page and falls back to get_messages when the pager yields nothing or errors. Two real cases need that: an agent that predates backward paging, and a session the agent never persisted (an ephemeral one — the pager rejects it) that still has a live in-memory context. "No page" must not read as "no conversation"; the fallback is also what keeps version skew from blanking a transcript, and the pager's error is the one reported when both fail.
    • HistoryPaging holds the cursor keyed by session id: a page whose request was in flight when the user switched away is dropped rather than prepended into another transcript, and a page that claims more history without advancing the cursor ends the walk (the guard the desktop's page loop applies to a non-advancing nextOffset).
    • The trigger is the upward-scroll key, not the scroll result: a tail page shorter than the viewport cannot scroll at all (scroll_up reports false), and that is exactly when the rest of the history would be unreachable.
    • clear_transcript drops the chat, the cursor and the scrollback alignment together (switch, /new, a fresh load) so a stale cursor can never be applied to the next session.
  • tui/src/components/chat_area.rs — prepend_messages inserts above the transcript and shifts viewport_top by the height it measured from the fresh layout, so the reader keeps their place even if they scrolled while the page was in flight. When they are still at the top (the normal case), the view is moved to show the loaded page instead of anchoring to rows that would stay invisible, and auto_scroll is switched off so the next streamed row cannot drag them away from the page they asked for.
  • tui/src/insert_history.rs — HistoryWriter::shift/rebase keep the scrollback watermark aligned. The terminal scrollback is append-only, so prepended rows can never be written there; without the shift the watermark would stop matching, and the next flush would mistake the whole transcript for new content and append a second copy of it.
  • Compaction checkpoints now actually render: the mapper's kind == "compaction" branch was unreachable through get_messages (whose LLM wire shape has no kind/checkpoint); the journal rows carry them.
  • Docs: docs/guide/tui.md / tui.zh-CN.md describe the paging behaviour under the keyboard table.

Behaviour notes

  • /transcript shows the pages loaded so far; /export still writes the whole session (the agent reads it from its own store).
  • A failed initial load keeps the previous behaviour: the switch reports it and clears the transcript.
  • Rows loaded above the scrollback are not written to the terminal scrollback (append-only), so a terminal-side scroll-back starts at the tail page.

Tests

  • tui/src/rpc/grpc_client.rs — the request shape (before/limit/session_id, no forward offset), the cursor surviving the typed payload decode (the shape the agent actually sends, where the cursor is omitted on a final page), and the legacy/empty shapes parsing to "paging off".
  • tui/src/app.rs — the tail load on --session (and that get_messages is not asked for when a page arrives), the older page fetched on PageUp with the right cursor, the loaded page being revealed, a short transcript still paging from the top, no page in the middle of a transcript, the empty/error fallbacks, both reads failing, a non-advancing cursor, a page for a left-behind session, and no scrollback duplication after a prepend.
  • tui/src/components/chat_area.rs / tui/src/insert_history.rs — the anchoring math and the watermark alignment.
  • make test-tui-tmux — new history-tail / history-older scenarios switch to a session the mock planted 14 exchanges in, assert the oldest row is not loaded, press PageUp and assert the older page arrives. Every pre-existing golden still byte-matches, which is the end-to-end check that no other screen changed.

Two of the golden files this PR touches are not related to the feature: help-*.txt were stale on main since #798 (which added the /skill-recommend row without re-recording), so the harness was red before this work. They are a separate commit (test(tui): re-record the help goldens stale since /skill-recommend) whose only diff is that row and the counts it shifts.

Verification against the real agent

Isolated home + real agent (no model configured), with a legacy JSONL session imported first (agent/src/session/legacy_import.rs), so the journal is real:

before=i64::MAX, limit=10 -> 20 rows e2e-05..reply-14, hasMore=true, nextOffset=8
before=8,        limit=10 ->  8 rows e2e-01..reply-04, no cursor (final page)

Then the real future-tui in a tmux pane with --session paged-e2e: the newest exchange is on screen, e2e-01 is not; PageUp at the top loads the older page and e2e-01 appears. A second run against a fresh one-message session loads it with no error notice and PageUp is a no-op.

Local checks

cargo test -p future-tui (2069 tests), cargo fmt --check, cargo clippy -p future-tui --all-targets --no-deps -D warnings, tui/tests/tmux-diff.sh. No other crate is touched.

`make test-tui-tmux` was already red on main: #798 added the
`/skill-recommend` row to the help card but did not re-record the three
goldens that show it (they still read "↓ 26 more" / "↑ 26 above"). The
Rust pane was byte-compared against them and lost.

Recorded with `tui/tests/tmux-diff.sh --record`; the only changes are the
new row and the two counts it shifts.
Switching to a long session read its entire transcript in one response
(`get_messages`), which is why a 10 MB session stalled and why a session
past the 32 MiB wire cap could not be opened at all (#800 raised the
client cap; this removes the unbounded read).

The TUI now reads display history through the agent's indexed pager:
`get_session_entries` with the exclusive backward cursor `before`
(`i64::MAX` = the tail) and `limit` counted in user exchanges. A switch
loads the newest ten exchanges; scrolling up past the top of the
transcript fetches the ten before those, until the session begins. Each
page is bounded, and the rows are the same display projection the
desktop renders.

- `SessionEntriesPage` (tui/src/rpc/types.rs) parses a page and keeps the
  continuation cursor. It also accepts the legacy whole-history
  `messages` shape, so one mapper renders both.
- `load_history_tail` reads the tail page and falls back to
  `get_messages` when the pager answers with nothing *or* errors: an
  agent that predates backward paging, and a session with no persisted
  history (e.g. an ephemeral one, which the pager rejects), still have a
  live context — "no page" must not read as "no conversation".
- `App::history_paging` holds the cursor, keyed by session id: a page
  that was in flight when the user switched away is dropped instead of
  prepended into another transcript, and a page that claims more history
  without advancing its cursor ends the walk (the guard the desktop's
  page loop already applies).
- `ChatArea::prepend_messages` inserts above the transcript and shifts
  `viewport_top` by the height it measured, so the reader keeps their
  place. When the reader is still at the top (the normal case — that is
  what triggered the load), the view is moved to show the loaded page
  instead of anchoring to invisible rows.
- `HistoryWriter::shift`/`rebase` keep the scrollback watermark aligned:
  the terminal scrollback is append-only, so prepended rows are never
  written there, and the next flush must not mistake the shifted
  transcript for new content and write a second copy.
- History rows now render the compaction checkpoint the mapper already
  handled (it was unreachable through `get_messages`, whose LLM wire shape
  has no `kind`/`checkpoint`).

Fallback: an empty/failed pager read still ends at `get_messages`, which
is the previous behaviour and now only runs when paging cannot answer.

Verified against the real agent (isolated home, real journal): with 14
planted exchanges, `before=i64::MAX, limit=10` returns e2e-05..reply-14
with `hasMore`/`nextOffset=8`, the next page returns the rest with no
cursor, and the real TUI in tmux loads the tail on switch and the older
page on PageUp.
@huichen
huichen enabled auto-merge (squash) September 23, 2026 10:51
@huichen
huichen merged commit a6b8b60 into main Sep 23, 2026
12 checks passed
@huichen
huichen deleted the feat/tui-history-paging branch September 23, 2026 11:02
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.

1 participant