Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(chat): keep session opens at the latest turn
vastsad68ec68329b4
diff --git a/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts b/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.tsindex ca9f4ebca..14a24d04b 100644--- a/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts+++ b/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts@@ -31,10 +31,13 @@ import {TRANSCRIPT_WINDOW_MIN,} from "../../../../lib/transcript-window";import {+ HISTORY_REVEAL_THRESHOLD_PX,+ isHistoryRevealPosition,isRecentScrollGesture,isScrollGestureInput,reduceTranscriptScroll,SCROLL_OWNER_ATTRIBUTE,+ transcriptHasLayout,TRANSCRIPT_SCROLL_ROUNDING_TOLERANCE_PX,type ScrollInputType,} from "../../../../lib/transcript-scroll";@@ -45,8 +48,6 @@ import { useTranscriptSearchFocus } from "../../../../hooks/use-transcript-searcimport { useAppStore } from "../../../../stores/app-store";-const HISTORY_REVEAL_THRESHOLD_PX = 120;-type UseTranscriptScrollOptions = {sessionId: string | undefined;messages: UiMessage[];@@ -84,6 +85,10 @@ export function useTranscriptScroll({const historyBoundaryRef = useRef<HTMLDivElement>(null);const pinnedRef = useRef(true);const lastScrollTopRef = useRef(0);+ // Last offset sampled while the scroller had a real layout box. A hidden+ // pane's `content-visibility: hidden` box reports `scrollTop === 0`, so the+ // hide transition must restore this rather than the collapsed value.+ const lastLaidOutScrollTopRef = useRef(0);const lastScrollGestureAtRef = useRef(-Infinity);const wasRunningRef = useRef(isRunning);const followFrameRef = useRef(0);@@ -102,7 +107,7 @@ export function useTranscriptScroll({const scrollToBottom = useCallback((behavior: ScrollBehavior = "auto") => {const el = scrollRef.current;- if (!el) return;+ if (!el || !transcriptHasLayout(el)) return;const targetTop = Math.max(0, el.scrollHeight - el.clientHeight);el.scrollTo({ top: targetTop, behavior });// `scrollTo({ behavior: "auto" })` is synchronous. Record the position the@@ -110,7 +115,10 @@ export function useTranscriptScroll({// fractional device pixel ratio the browser lands a fraction of a pixel// away (asked 841, got 840.909), and the intended value would make the// following native scroll event read as the user scrolling up.- if (behavior === "auto") lastScrollTopRef.current = el.scrollTop;+ if (behavior === "auto") {+ lastScrollTopRef.current = el.scrollTop;+ lastLaidOutScrollTopRef.current = el.scrollTop;+ }}, []);const cancelFollowScroll = useCallback(() => {@@ -130,6 +138,7 @@ export function useTranscriptScroll({}, [cancelFollowScroll]);const recordScrollPosition = useCallback((top: number) => {lastScrollTopRef.current = top;+ lastLaidOutScrollTopRef.current = top;}, []);const {notifier: disclosureAnchorNotifier,@@ -212,7 +221,9 @@ export function useTranscriptScroll({if (becameHidden) {releaseDisclosureAnchor();cancelFollowScroll();- retainedScrollTopRef.current = el.scrollTop;+ // Do not read `el.scrollTop` here: the hide CSS has already skipped+ // rendering, so the box reports 0. Restore the last laid-out offset.+ retainedScrollTopRef.current = lastLaidOutScrollTopRef.current;return;}if (!becameVisible) return;@@ -224,6 +235,7 @@ export function useTranscriptScroll({if (retained === null) return;el.scrollTop = retained;lastScrollTopRef.current = retained;+ lastLaidOutScrollTopRef.current = retained;}, [cancelFollowScroll, paneVisible, releaseDisclosureAnchor, scrollToBottom]);// A hidden pane must not chase its stream: its scroller has no visible@@ -311,6 +323,7 @@ export function useTranscriptScroll({if (delta <= 0) return;el.scrollTop += delta;lastScrollTopRef.current = el.scrollTop;+ if (transcriptHasLayout(el)) lastLaidOutScrollTopRef.current = el.scrollTop;}, [messages.length, windowSize]);@@ -320,7 +333,15 @@ export function useTranscriptScroll({const handleScroll = useCallback(() => {const el = scrollRef.current;if (!el) return;- if (el.scrollTop <= HISTORY_REVEAL_THRESHOLD_PX) reachTop();+ if (paneVisibleRef.current && transcriptHasLayout(el)) {+ lastLaidOutScrollTopRef.current = el.scrollTop;+ }+ if (+ paneVisibleRef.current &&+ isHistoryRevealPosition(el, pinnedRef.current)+ ) {+ reachTop();+ }if (readingWindow) {pinnedRef.current = false;lastScrollTopRef.current = el.scrollTop;@@ -490,7 +511,9 @@ export function useTranscriptScroll({const boundedFirstCommitRef = useRef(false);useEffect(() => {if (!hydrationBounded) {- firstCommitRef.current = false;+ // An empty first paint must not spend this gate: revalidation can still+ // land a long transcript that needs the bounded expand and re-bottom.+ if (allHistoryEntries.length > 0) firstCommitRef.current = false;return;}boundedFirstCommitRef.current = true;@@ -501,7 +524,7 @@ export function useTranscriptScroll({return () => cancelAnimationFrame(frame);// `hydrationTick` is a dependency so a pane whose expansion is still queued// re-evaluates instead of holding a stale frame.- }, [hydrationBounded, hydrationTick]);+ }, [allHistoryEntries.length, hydrationBounded, hydrationTick]);// Settle veil (D287). A bounded first commit means the transcript is long// enough for its geometry to keep moving for several frames after mount: the@@ -620,7 +643,7 @@ export function useTranscriptScroll({const revealEarlierHistory = useCallback(() => {const el = scrollRef.current;if (!el) return;- if (el.scrollTop <= HISTORY_REVEAL_THRESHOLD_PX) {+ if (isHistoryRevealPosition(el)) {reachTop();return;}@@ -670,7 +693,7 @@ export function useTranscriptScroll({frame = requestAnimationFrame(() => {if (scrollRef.current !== root ||- root.scrollTop > HISTORY_REVEAL_THRESHOLD_PX+ !isHistoryRevealPosition(root, pinnedRef.current)) {return;}diff --git a/apps/desktop/src/lib/transcript-scroll.ts b/apps/desktop/src/lib/transcript-scroll.tsindex af85e7a5d..2aa5f82d1 100644--- a/apps/desktop/src/lib/transcript-scroll.ts+++ b/apps/desktop/src/lib/transcript-scroll.ts@@ -12,6 +12,45 @@ export const TRANSCRIPT_REPIN_THRESHOLD_PX = 48;*/export const TRANSCRIPT_SCROLL_ROUNDING_TOLERANCE_PX = 1;+/**+ * Near-top band that grows the mounted window or fetches an older page (D269).+ * Shared by the scroll handler and the history-boundary observer so the two+ * triggers cannot drift apart.+ */+export const HISTORY_REVEAL_THRESHOLD_PX = 120;++export type TranscriptScrollerGeometry = {+ scrollTop: number;+ scrollHeight: number;+ clientHeight: number;+};++/** A scroller that has not been measured yet, or is skipped by `content-visibility`. */+export function transcriptHasLayout(+ geometry: Pick<TranscriptScrollerGeometry, "scrollHeight" | "clientHeight">,+): boolean {+ return geometry.clientHeight > 0 && geometry.scrollHeight > 0;+}++/**+ * Whether the visible top should grow or page earlier history (D269).+ *+ * A collapsed box reports `scrollTop === 0` and must not be treated as "the+ * user is reading the top". A pinned overflowing transcript is at (or about+ * to be restored to) the bottom; a stale zero offset there must not page the+ * conversation back to its start. An underfilled pinned tail still advances:+ * it does not overflow, so the top boundary is genuinely in view.+ */+export function isHistoryRevealPosition(+ geometry: TranscriptScrollerGeometry,+ pinned = false,+): boolean {+ if (!transcriptHasLayout(geometry)) return false;+ const overflows = geometry.scrollHeight > geometry.clientHeight + 1;+ if (pinned && overflows) return false;+ return geometry.scrollTop <= HISTORY_REVEAL_THRESHOLD_PX;+}+export type TranscriptScrollInput = {previousScrollTop: number;scrollTop: number;diff --git a/apps/desktop/test/interaction-performance.test.mjs b/apps/desktop/test/interaction-performance.test.mjsindex 46199825d..daaaadea4 100644--- a/apps/desktop/test/interaction-performance.test.mjs+++ b/apps/desktop/test/interaction-performance.test.mjs@@ -202,7 +202,7 @@ test("a revealed pane restores its own scroll position in the layout phase", ()/useLayoutEffect\(\(\) => \{([\s\S]*?)\n \}, \[cancelFollowScroll, paneVisible, releaseDisclosureAnchor, scrollToBottom\]\);/,)?.[1];assert.ok(revealEffect, "the reveal must restore position in a layout effect");- assert.match(revealEffect, /retainedScrollTopRef\.current = el\.scrollTop/);+ assert.match(revealEffect, /retainedScrollTopRef\.current = lastLaidOutScrollTopRef\.current/);assert.match(revealEffect, /if \(pinnedRef\.current\) \{\s*scrollToBottom\(\);/);assert.match(revealEffect, /el\.scrollTop = retained/);assert.match(revealEffect, /lastScrollTopRef\.current = retained/);@@ -295,6 +295,11 @@ test("first-commit hydration expands without moving the transcript", () => {/boundedFirstCommitRef\.current = true;\n\s*const frame = requestAnimationFrame/,"the flag is armed in the same effect that queues the expansion",);+ assert.match(+ transcript,+ /if \(allHistoryEntries\.length > 0\) firstCommitRef\.current = false/,+ "an empty first paint must not spend the first-commit gate",+ );});test("minimap separates resize checks from message-position measurement", () => {diff --git a/apps/desktop/test/session-switch-performance.test.mjs b/apps/desktop/test/session-switch-performance.test.mjsindex b559e0b4e..f42298c39 100644--- a/apps/desktop/test/session-switch-performance.test.mjs+++ b/apps/desktop/test/session-switch-performance.test.mjs@@ -47,11 +47,8 @@ test("session reads use a bounded tail and load older pages on demand", () => {assert.match(transcript, /onLoadOlder\?: \(\) => Promise<void>/);// The near-top band is one named constant shared by the scroll check and the// D269 boundary observer, so the two triggers cannot drift apart.- assert.match(transcript, /HISTORY_REVEAL_THRESHOLD_PX = 120/);- assert.match(- transcript,- /el\.scrollTop <= HISTORY_REVEAL_THRESHOLD_PX/,- );+ assert.match(transcript, /HISTORY_REVEAL_THRESHOLD_PX/);+ assert.match(transcript, /isHistoryRevealPosition\(el, pinnedRef\.current\)/);// Paging is wired per retained pane (ADR 0137), so each pane requests its own// older pages rather than the surface requesting them for whichever session// happens to be active.diff --git a/apps/desktop/test/transcript-scroll.test.mjs b/apps/desktop/test/transcript-scroll.test.mjsindex 397aaad25..3bf6ff6c2 100644--- a/apps/desktop/test/transcript-scroll.test.mjs+++ b/apps/desktop/test/transcript-scroll.test.mjs@@ -1,10 +1,13 @@import assert from "node:assert/strict";import test from "node:test";import {+ HISTORY_REVEAL_THRESHOLD_PX,TRANSCRIPT_REPIN_THRESHOLD_PX,TRANSCRIPT_SCROLL_GESTURE_WINDOW_MS,+ isHistoryRevealPosition,isRecentScrollGesture,reduceTranscriptScroll,+ transcriptHasLayout,} from "../src/lib/transcript-scroll.ts";function update(overrides = {}) {@@ -108,3 +111,53 @@ test("a scroll event without recent user input is not a gesture", () => {);assert.equal(isRecentScrollGesture(100, -Infinity), false);});++test("a collapsed scroller has no layout", () => {+ assert.equal(transcriptHasLayout({ scrollHeight: 0, clientHeight: 0 }), false);+ assert.equal(transcriptHasLayout({ scrollHeight: 800, clientHeight: 0 }), false);+ assert.equal(transcriptHasLayout({ scrollHeight: 800, clientHeight: 400 }), true);+});++test("a collapsed scroller is not a history-reveal position", () => {+ assert.equal(+ isHistoryRevealPosition({ scrollTop: 0, scrollHeight: 0, clientHeight: 0 }),+ false,+ );+});++test("a pinned overflowing transcript does not page from a stale zero offset", () => {+ assert.equal(+ isHistoryRevealPosition(+ { scrollTop: 0, scrollHeight: 4_000, clientHeight: 800 },+ true,+ ),+ false,+ );+});++test("an underfilled pinned tail still reveals earlier history", () => {+ assert.equal(+ isHistoryRevealPosition(+ { scrollTop: 0, scrollHeight: 400, clientHeight: 800 },+ true,+ ),+ true,+ );+});++test("an unpinned transcript at the top reveals earlier history", () => {+ assert.equal(+ isHistoryRevealPosition(+ { scrollTop: HISTORY_REVEAL_THRESHOLD_PX, scrollHeight: 4_000, clientHeight: 800 },+ false,+ ),+ true,+ );+ assert.equal(+ isHistoryRevealPosition(+ { scrollTop: HISTORY_REVEAL_THRESHOLD_PX + 1, scrollHeight: 4_000, clientHeight: 800 },+ false,+ ),+ false,+ );+});diff --git a/apps/desktop/test/transcript-window.test.mjs b/apps/desktop/test/transcript-window.test.mjsindex c46b03d85..c37b5e810 100644--- a/apps/desktop/test/transcript-window.test.mjs+++ b/apps/desktop/test/transcript-window.test.mjs@@ -210,7 +210,7 @@ test("the transcript bounds mounted history and escalates at the top", () => {// older page once the window already covers it.assert.match(transcript,- /if \(el\.scrollTop <= HISTORY_REVEAL_THRESHOLD_PX\) reachTop\(\)/,+ /isHistoryRevealPosition\(el, pinnedRef\.current\)/,);const reachTop = transcript.match(/const reachTop = useCallback\(\(\) => \{([\s\S]*?)\n \}, \[loadOlder, windowSize\]\);/,@@ -276,7 +276,7 @@ test("a visible history boundary advances without waiting for a scroll event", ();// Escalation is bounded per run: one growth step or one page request, and the// effect only re-runs when that step actually changed the projection.- assert.match(transcript, /root\.scrollTop > HISTORY_REVEAL_THRESHOLD_PX/);+ assert.match(transcript, /!isHistoryRevealPosition\(root, pinnedRef\.current\)/);assert.match(transcript, /observer\.disconnect\(\)/);});diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex 59d6bbf60..337936e85 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -1876,7 +1876,9 @@ Renderer: `apps/desktop/src/components/Markdown.tsx` + `apps/desktop/src/lib/shiobserved inside the scroller with the same near-top threshold the scroll handleruses. An underfilled tail page, a page whose fetched rows all land outside themounted window, and a window transition can leave `scrollTop` untouched, so a- scroll-only trigger could never fire again.+ scroll-only trigger could never fire again. A collapsed scroller and a pinned+ overflowing transcript whose `scrollTop` has been reset to 0 are not treated as+ "at the top", so opening or revealing a session cannot page back to the start.- **Minimap hover cost**: dash magnification is applied by writing a customproperty per dash, and dash centers are measured in a separate read-only pass.Reading a dash's geometry inside the same loop that writes to it forces onediff --git a/docs/spec/04-ux/09-interaction-patterns.md b/docs/spec/04-ux/09-interaction-patterns.mdindex 862da2631..7bf5aeb57 100644--- a/docs/spec/04-ux/09-interaction-patterns.md+++ b/docs/spec/04-ux/09-interaction-patterns.md@@ -302,6 +302,10 @@ may be retained while exactly one workspace supplies the visible shell context.- A first-opened session settles at its newest turn. A revisited pane returns tothe offset the user left, and a pane still pinned re-anchors to the bottom;activation no longer resets manual-scroll state for a revisit (ADR 0137).+ History continuation (D269) does not page earlier rows from a collapsed+ scroller or from a pinned overflowing transcript whose `scrollTop` has been+ reset to 0; an empty first paint does not spend the first-commit hydration+ gate, so a later long page still re-bottoms behind the settle veil.- Selecting a project-scoped conversation activates its project as part of thestore-owned selection transaction. Selecting a Temporary conversation clearsthe visible workspace. Project-scoped new-session actions pass their targetdiff --git a/docs/zh-CN/spec/04-ux/09-interaction-patterns.md b/docs/zh-CN/spec/04-ux/09-interaction-patterns.mdindex 453e551d7..1ee8a1119 100644--- a/docs/zh-CN/spec/04-ux/09-interaction-patterns.md+++ b/docs/zh-CN/spec/04-ux/09-interaction-patterns.md@@ -244,7 +244,10 @@摘要;发送和粘贴会等待这次进行中的创建,而不是再开一个槽位(ADR 0154)。- 首次打开的会话在其最新回合处落定。重新访问的面板回到用户离开的偏移,而仍然固定在底部的面板重新锚定到底部;对于重新访问,激活- 不再重置手动滚动状态(ADR 0137)。+ 不再重置手动滚动状态(ADR 0137)。历史续接(D269)不会把塌缩的+ 滚动容器、或 `scrollTop` 被重置为 0 的已钉住溢出记录当成「在顶部」+ 去翻更早的页;空的首帧不会消耗首次提交的水合门闩,因此随后到达的+ 长记录仍会在骨架帷幕后面重新吸底。- 选择项目范围的对话会激活其项目作为商店自有精选交易。选择临时对话将清除可见的工作空间。项目范围内的新会话操作通过了目标