From e9a9ffc9aba97bd8679861cb2b98a8fd02dc5496 Mon Sep 17 00:00:00 2001 From: Hikari Date: Fri, 6 Mar 2026 21:06:14 -0800 Subject: [PATCH] fix: prevent stale windowStart when switching conversations --- src/lib/components/Terminal.svelte | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/lib/components/Terminal.svelte b/src/lib/components/Terminal.svelte index a0bc24b..d559ce4 100644 --- a/src/lib/components/Terminal.svelte +++ b/src/lib/components/Terminal.svelte @@ -59,20 +59,27 @@ currentConversationId = newId; + // Peek at the saved position to set windowStart before the first tick, + // preventing a stale windowStart from a previous conversation leaving + // visibleLines empty (windowStart >= lines.length). + const savedPosition = claudeStore.getScrollPosition(newId); + if (savedPosition === -1) { + // Will auto-scroll: pin the window to the tail of the new conversation + shouldAutoScroll = true; + windowStart = Math.max(0, lines.length - WINDOW_SIZE); + } else { + // Will restore a specific position: always start from the top of history + shouldAutoScroll = false; + windowStart = 0; + } + // Restore scroll position for the new conversation after DOM updates await tick(); if (terminalElement) { - const savedPosition = claudeStore.getScrollPosition(newId); isRestoringScroll = true; if (savedPosition === -1) { - // Auto-scroll to bottom — window reactive statement will advance windowStart - shouldAutoScroll = true; terminalElement.scrollTop = terminalElement.scrollHeight; } else { - // Restore to saved position — show from the beginning of history - windowStart = 0; - shouldAutoScroll = false; - await tick(); terminalElement.scrollTop = savedPosition; } // Small delay to prevent the scroll handler from overriding our restore