perf: mount only the tail of a chat transcript, older messages on scroll (#12469)

* perf: mount only the tail of a chat transcript, older messages on scroll

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep revealed transcript items across prepended pages and follow-mode exits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: say what the transcript tail clamp guarantees

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem
2026-11-17 12:11:31 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 364e729a08
commit 17a56db9ea
@@ -4,7 +4,7 @@
import AppAvailableContextList from './AppAvailableContextList.svelte'
import ChatContextPicker from './ChatContextPicker.svelte'
import WorkspaceMentionPicker from './WorkspaceMentionPicker.svelte'
import { type Snippet } from 'svelte'
import { flushSync, untrack, type Snippet } from 'svelte'
import {
AlertTriangle,
ArrowDown,
@@ -196,6 +196,50 @@
const groupChatItems = createChatItemGrouper()
const chatItems = $derived(groupChatItems(messages))
// Mounting a message parses its markdown, so a long transcript mounted at once blocks the
// page for every message in it. Only the tail is mounted when a transcript arrives; older
// items mount in batches as the reader scrolls toward them, and stay mounted.
const TAIL_ITEMS = 30
const REVEAL_BATCH = 30
const REVEAL_MARGIN_PX = 600
const transcriptKey = $derived(messages[0]?.content)
// How many items are hidden, counted from the item holding message `first`.
let revealed = $state<{ first: string | undefined; hidden: number }>()
const hiddenHead = $derived.by(() => {
const first = transcriptKey
const r = revealed
return untrack(() => {
if (r && r.first === first) return r.hidden
// The message it was counted from moved down: a host prepended a page of older
// messages (the flow chat does), which stays hidden unless everything above was shown.
const moved = r ? messages.findIndex((m) => m.content === r.first) : -1
if (r && moved > 0) {
if (r.hidden === 0) return 0
return r.hidden + chatItems.findIndex((item) => chatItemStart(item) >= moved)
}
// Another transcript: the reload of the same chat keeps its first message.
return Math.max(0, chatItems.length - TAIL_ITEMS)
})
})
// A swap the key misses (two chats opening with the same message) keeps the previous chat's
// count: clamped so that count can never hide the tail, though a low one mounts more than needed.
const shownFrom = $derived(Math.min(hiddenHead, Math.max(0, chatItems.length - TAIL_ITEMS)))
const shownItems = $derived(shownFrom > 0 ? chatItems.slice(shownFrom) : chatItems)
function chatItemStart(item: (typeof chatItems)[number]): number {
return item.kind === 'group' ? (item.entries[0]?.index ?? 0) : item.index
}
function revealOlder() {
if (!scrollElement || shownFrom === 0 || scrollElement.scrollTop >= REVEAL_MARGIN_PX) return
// Assigned from the distance to the bottom rather than added to, so it holds whether or
// not the browser's own scroll anchoring already moved the position.
const fromBottom = scrollElement.scrollHeight - scrollElement.scrollTop
revealed = { first: transcriptKey, hidden: Math.max(0, shownFrom - REVEAL_BATCH) }
flushSync()
scrollElement.scrollTop = scrollElement.scrollHeight - fromBottom
}
// Escape stops the generation when focus is on the chat (or parked on
// body), but stays with other widgets (e.g. the session's Monaco editor).
// Capture phase is required: Monaco and mounted-but-closed Modal2
@@ -282,6 +326,9 @@
const distance =
scrollElement.scrollHeight - scrollElement.scrollTop - scrollElement.clientHeight
showScrollToLatest = distance > SCROLL_TO_LATEST_THRESHOLD_PX
// A tail too short to scroll fires no scroll event, so nothing else would mount the rest.
// Untracked: a reveal re-running this effect would scroll a following reader back down.
if (untrack(() => shownFrom) > 0) requestAnimationFrame(revealOlder)
}
})
@@ -307,6 +354,9 @@
} else {
chatHost.disableAutomaticScroll()
}
// After follow mode is settled, so the reveal cannot scroll a reader leaving the end back
// down; before the host's own paging, which waits until nothing here is left to mount.
revealOlder()
onTranscriptScroll?.()
}
@@ -990,7 +1040,7 @@ the panel, or the Escape-to-stop focus check would wrongly reject them. -->
{#snippet groupRow(message: DisplayMessage, messageIndex: number)}
{@render messageRow(message, messageIndex, false, true)}
{/snippet}
{#each chatItems as item (chatItemKey(item))}
{#each shownItems as item (chatItemKey(item))}
{#if item.kind === 'group'}
<div
class={twMerge(