Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(transcript): let an unpinned bookmark own the pane reveal

zszz3549b67b990cf
diff --git a/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts b/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts
index 32a3f525f..4d9d838ca 100644
--- a/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts
+++ b/apps/desktop/src/features/chat/transcript/hooks/useTranscriptScroll.ts
@@ -40,7 +40,10 @@ import { readScrollInputContext } from "../../../../lib/scroll-input";
import { useDisclosureAnchor } from "../../../../hooks/use-disclosure-anchor";
import type { TranscriptSearchTarget } from "../../../../lib/transcript-reading";
import { useTranscriptSearchFocus } from "../../../../hooks/use-transcript-search-focus";
-import { getTranscriptReadingPosition } from "../../../../lib/transcript-reading-position";
+import {
+ getTranscriptReadingPosition,
+ transcriptRevealOwnership,
+} from "../../../../lib/transcript-reading-position";
import { useTranscriptReadingPosition, type RestoreTranscriptReading } from "../../../../hooks/use-transcript-reading-position";
type UseTranscriptScrollOptions = {
@@ -247,8 +250,23 @@ export function useTranscriptScroll({
retainedScrollTopRef.current = lastLaidOutScrollTopRef.current;
return;
}
- if (!becameVisible || readingPositionPendingRef.current) return;
- if (pinnedRef.current) {
+ if (!becameVisible) return;
+ const bookmark = getTranscriptReadingPosition(sessionId);
+ const ownership = transcriptRevealOwnership(
+ bookmark,
+ Boolean(readingPositionPendingRef.current),
+ pinnedRef.current,
+ );
+ if (ownership === "reading-position") {
+ // The reading-position hook restores this bookmark (or reports it
+ // missing). Bottoming here would race it and land on the newest turn.
+ if (bookmark?.pinned === false) {
+ pinnedRef.current = false;
+ setShowJump(true);
+ }
+ return;
+ }
+ if (ownership === "pinned") {
scrollToBottom();
return;
}
@@ -257,7 +275,13 @@ export function useTranscriptScroll({
el.scrollTop = retained;
lastScrollTopRef.current = retained;
lastLaidOutScrollTopRef.current = retained;
- }, [cancelFollowScroll, paneVisible, releaseDisclosureAnchor, scrollToBottom]);
+ }, [
+ cancelFollowScroll,
+ paneVisible,
+ releaseDisclosureAnchor,
+ scrollToBottom,
+ sessionId,
+ ]);
// A hidden pane must not chase its stream: its scroller has no visible
// viewport, and the measurements a follow scroll depends on are unreliable
diff --git a/apps/desktop/src/hooks/use-transcript-reading-position.ts b/apps/desktop/src/hooks/use-transcript-reading-position.ts
index 0c4ad057e..e2ab87c91 100644
--- a/apps/desktop/src/hooks/use-transcript-reading-position.ts
+++ b/apps/desktop/src/hooks/use-transcript-reading-position.ts
@@ -34,8 +34,17 @@ export function useTranscriptReadingPosition({
const alignmentUntilRef = useRef(0);
const wasVisibleRef = useRef(visible);
useLayoutEffect(() => {
- if (visible && !wasVisibleRef.current && !pinnedRef.current) {
- pendingRef.current = getTranscriptReadingPosition(sessionId);
+ if (visible && !wasVisibleRef.current) {
+ const bookmark = getTranscriptReadingPosition(sessionId);
+ if (bookmark?.pinned === false) {
+ // The follow flag can still read `true` when a send or retry hid the
+ // pane; the recorded bookmark is the authoritative state for this
+ // reveal, so claim the edge instead of letting follow bottom it.
+ pendingRef.current = bookmark;
+ pinnedRef.current = false;
+ } else if (!pinnedRef.current) {
+ pendingRef.current = bookmark;
+ }
alignmentUntilRef.current = 0;
}
wasVisibleRef.current = visible;
diff --git a/apps/desktop/src/lib/transcript-reading-position.ts b/apps/desktop/src/lib/transcript-reading-position.ts
index b4a5b8911..1090b8bdd 100644
--- a/apps/desktop/src/lib/transcript-reading-position.ts
+++ b/apps/desktop/src/lib/transcript-reading-position.ts
@@ -27,6 +27,23 @@ export function sessionReadingState(sessionId: string): ReadingState {
export function getTranscriptReadingPosition(sessionId: string | undefined) {
return sessionId ? readingStates.get(sessionId)?.position : undefined;
}
+
+export type TranscriptRevealOwnership = "reading-position" | "pinned" | "retained";
+
+/**
+ * Which effect owns a pane reveal. A recorded unpinned bookmark wins over a
+ * stale follow flag: follow mode can still read `true` when a send or retry hid
+ * the pane, and bottoming there would undo the position the reader is meant to
+ * return to (issue #1771).
+ */
+export function transcriptRevealOwnership(
+ bookmark: TranscriptReadingPosition | undefined,
+ pending: boolean,
+ pinned: boolean,
+): TranscriptRevealOwnership {
+ if (bookmark?.pinned === false || pending) return "reading-position";
+ return pinned ? "pinned" : "retained";
+}
export function forgetTranscriptReadingState(sessionId: string) {
readingStates.delete(sessionId);
}
diff --git a/apps/desktop/test/interaction-performance.test.mjs b/apps/desktop/test/interaction-performance.test.mjs
index 4517668c7..37bbce3a8 100644
--- a/apps/desktop/test/interaction-performance.test.mjs
+++ b/apps/desktop/test/interaction-performance.test.mjs
@@ -191,11 +191,15 @@ test("a revealed pane restores its own scroll position in the layout phase", ()
// scroller can be clamped while its content grows off screen, and a passive
// effect would leave one visible frame at the wrong offset (ADR 0137).
const revealEffect = transcript.match(
- /useLayoutEffect\(\(\) => \{([\s\S]*?)\n \}, \[cancelFollowScroll, paneVisible, releaseDisclosureAnchor, scrollToBottom\]\);/,
+ /useLayoutEffect\(\(\) => \{([\s\S]*?)\n \}, \[\n cancelFollowScroll,\n paneVisible,\n releaseDisclosureAnchor,\n scrollToBottom,\n sessionId,\n \]\);/,
)?.[1];
assert.ok(revealEffect, "the reveal must restore position in a layout effect");
assert.match(revealEffect, /retainedScrollTopRef\.current = lastLaidOutScrollTopRef\.current/);
- assert.match(revealEffect, /if \(pinnedRef\.current\) \{\s*scrollToBottom\(\);/);
+ // A recorded unpinned bookmark owns the edge; only a genuinely pinned pane
+ // bottoms. A stale follow flag must not override the bookmark (#1771).
+ assert.match(revealEffect, /transcriptRevealOwnership\(/);
+ assert.match(revealEffect, /ownership === "reading-position"/);
+ assert.match(revealEffect, /ownership === "pinned"\) \{\s*scrollToBottom\(\);/);
assert.match(revealEffect, /el\.scrollTop = retained/);
assert.match(revealEffect, /lastScrollTopRef\.current = retained/);
});
diff --git a/apps/desktop/test/transcript-reveal-ownership.test.mjs b/apps/desktop/test/transcript-reveal-ownership.test.mjs
new file mode 100644
index 000000000..5a0690b3a
--- /dev/null
+++ b/apps/desktop/test/transcript-reveal-ownership.test.mjs
@@ -0,0 +1,60 @@
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+import { register } from "node:module";
+import test from "node:test";
+
+register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));
+const { transcriptRevealOwnership } = await import(
+ "../src/lib/transcript-reading-position.ts"
+);
+
+const bookmark = (pinned) => ({
+ pinned,
+ messageId: "m1",
+ offset: 24,
+ scrollTop: 480,
+ windowSize: 20,
+});
+const read = (path) => readFile(new URL(path, import.meta.url), "utf8");
+
+test("an unpinned bookmark owns a reveal even when follow still reads pinned", () => {
+ assert.equal(
+ transcriptRevealOwnership(bookmark(false), false, true),
+ "reading-position",
+ );
+});
+
+test("a pending restore owns the reveal", () => {
+ assert.equal(transcriptRevealOwnership(undefined, true, true), "reading-position");
+});
+
+test("a pinned pane without a bookmark bottoms", () => {
+ assert.equal(transcriptRevealOwnership(undefined, false, true), "pinned");
+});
+
+test("an unpinned pane without a bookmark reapplies its retained offset", () => {
+ assert.equal(transcriptRevealOwnership(undefined, false, false), "retained");
+ assert.equal(transcriptRevealOwnership(bookmark(true), false, false), "retained");
+});
+
+test("both transcript hooks route the reveal edge through the shared rule", async () => {
+ const [readingHook, scrollHook] = await Promise.all([
+ read("../src/hooks/use-transcript-reading-position.ts"),
+ read("../src/features/chat/transcript/hooks/useTranscriptScroll.ts"),
+ ]);
+ // The bookmark, not the possibly stale follow flag, claims an unpinned
+ // reveal; the old `!pinnedRef.current` gate let follow bottom it (#1771).
+ assert.match(
+ readingHook,
+ /bookmark\?\.pinned === false[\s\S]*?pendingRef\.current = bookmark[\s\S]*?pinnedRef\.current = false/,
+ );
+ assert.doesNotMatch(
+ readingHook,
+ /!wasVisibleRef\.current && !pinnedRef\.current/,
+ );
+ assert.match(scrollHook, /transcriptRevealOwnership\(/);
+ assert.match(
+ scrollHook,
+ /ownership === "reading-position"[\s\S]*?pinnedRef\.current = false/,
+ );
+});