Skip to content

fix(chat): page chat history and restore the reading position exactly - #581

Merged
ArgaFairuz merged 1 commit into
mainfrom
fix/chat-history-paging-and-scroll-restore
Nov 17, 2026
Merged

ArgaFairuz merged 1 commit into
mainfrom
fix/chat-history-paging-and-scroll-restore

Conversation

@ArgaFairuz

Copy link
Copy Markdown
Collaborator

Summary

Load chat history a page at a time instead of the whole branch, and make returning to a chat (after a project switch or a browser refresh) land exactly on the last reading position.

Why

Opening a chat sent every message on its active branch. On a long chat that meant tens of megabytes and about a second of server work, every time the chat was opened. Separately, the saved reading position was restored inaccurately or not at all after switching projects or refreshing.

Changes

  • backend/chat/message-pages.ts (new): page the active branch backwards from the head (default 80 messages). A page boundary never separates a tool call from its result, a sub-agent from its parent call, or a compaction/skill message from its source. Also serves a prompt index for the navigation rail and the task-tool history older than the loaded page.
  • backend/ws/messages/crud.ts: messages:list-page, messages:list-prompts, messages:list-task-history (session access checked).
  • sessions.svelte.ts: load the latest page and fetch/prepend older pages, with a generation guard against session switches. Holds the prompt index and task history.
  • virtual-scroll.svelte.ts: prepend() shifts the window so older items enter without moving what is on screen.
  • ChatMessages.svelte: page in on scroll-up, when a short first page doesn't fill the viewport, for nav-rail jumps, for a restored edit target, and for a saved anchor that is not loaded. Single render path (the two-branch render remounted every message on restore). Holds the restored position against late layout. Flushes the position on pagehide/hidden. Loading indicator is now an overlay instead of an in-flow row.
  • TaskProgress.svelte: replays the task history plus the loaded messages.
  • project-workspace.svelte.ts: saves requested while docks are settling are deferred, not dropped. Adds flushWorkspaceSave() and flushes on pagehide/hidden.

Notes

  • Measured on a real 7,084-message chat: first page 81 messages / 124 KB / 28 ms (was 41.8 MB / ~1 s). Paging back covers the branch exactly once.
  • Known limitation: a TaskOutput whose background Bash call sits on an unloaded page shows unlinked until that page loads.
  • The fade-in when the whole list is revealed is gone with the single render path.
  • Two renderScopeConfig tests fail locally on main too (they read the machine's git identity); unrelated.

Opening a chat sent its whole active branch over the socket - on a
long chat tens of megabytes and about a second of server work, every
time the chat was opened. Returning to a chat after switching projects
or refreshing also rarely landed where the user had left it.

The chat now loads its latest page (about 80 messages) and fetches
older pages on scroll-up or when a jump targets an unloaded message.
Page boundaries never separate a tool call from its result, a
sub-agent from its parent call, or a compaction/skill message from its
source. The navigation rail and the task widget, which need the whole
branch, get a light prompt index and task history instead of message
bodies. A 7,084-message chat now opens with 124 KB instead of 41.8 MB.

The reading position was off for four reasons, each addressed:
- the list rendered through two branches, so every message remounted
  just as a restore finished, dropping the heights being aligned to;
  it now has one render path
- heights kept settling after the one-time alignment, and Safari has
  no scroll anchoring; the restored position is now held for two
  seconds or until the user scrolls, clicks or types
- workspace saves requested while docks were still loading after a
  switch were dropped; they are now deferred until the docks settle
- the last scroll before a refresh sat in two debounces; it is now
  flushed on pagehide and when the tab is hidden
@ArgaFairuz
ArgaFairuz merged commit a1c1ba7 into main Nov 17, 2026
1 check passed
@ArgaFairuz
ArgaFairuz deleted the fix/chat-history-paging-and-scroll-restore branch November 17, 2026 03:12
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