Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(agentic-ide): anchor a full-screen pane's scrollbar to what its screen admits

Ruben Lütke17b73b570b48
diff --git a/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.test.tsx b/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.test.tsx
index eb8edbec53..001dcbf181 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.test.tsx
@@ -20,6 +20,8 @@ interface TermState {
viewportY: number;
baseY: number;
mouseTrackingMode: string;
+ /** What the screen shows, for the overlay anchor; row index → text. */
+ lines: string[];
}
function fakeTerminal(overrides: Partial<TermState> = {}) {
@@ -30,6 +32,7 @@ function fakeTerminal(overrides: Partial<TermState> = {}) {
viewportY: 976,
baseY: 976,
mouseTrackingMode: "none",
+ lines: [],
...overrides,
};
const listeners: Record<string, (() => void)[]> = {};
@@ -60,6 +63,9 @@ function fakeTerminal(overrides: Partial<TermState> = {}) {
get baseY() {
return state.baseY;
},
+ getLine(y: number) {
+ return { translateToString: () => state.lines[y] ?? "" };
+ },
},
onBufferChange: on("bufferChange"),
},
@@ -319,6 +325,47 @@ describe("PaneScrollbar", () => {
await waitFor(() => expect(thumbTop() + thumbHeight()).toBe(TRACK_PX));
});
+ /*
+ * The reported bug, at the level the user met it: the pane stood at its
+ * newest output while the thumb hung mid-track, because notches the CLI had
+ * silently ignored at the transcript's top stayed counted. The CLI's own
+ * scrolled-back overlay is the anchor: the moment it leaves the screen, the
+ * thumb comes home — no wheel traffic required.
+ */
+ it("snaps home the moment the CLI's scrolled-back overlay disappears", async () => {
+ const { state, term, fire } = appTerminal(24);
+ state.lines = Array.from({ length: 24 }, (_, i) => `transcript row ${i}`);
+ state.lines[20] = " 86 Jump to bottom (ctrl+End) ↓";
+ render(<Harness term={term} />);
+
+ // A pile of wheel-ups, some of which the CLI ignored at its top.
+ for (let i = 0; i < 40; i += 1) fireEvent.wheel(host(), { deltaY: -1 });
+ reachThePane();
+ expect(thumbTop() + thumbHeight()).toBeLessThan(TRACK_PX);
+
+ // The CLI returns to the live end and erases its overlay.
+ state.lines[20] = "";
+ fire("render");
+
+ await waitFor(() => expect(thumbTop() + thumbHeight()).toBe(TRACK_PX));
+ });
+
+ it("keeps standing away from the live end while the overlay is up", async () => {
+ const { state, term } = appTerminal(24);
+ state.lines = Array.from({ length: 24 }, (_, i) => `transcript row ${i}`);
+ state.lines[20] = " 12 Jump to bottom (ctrl+End) ↓";
+ render(<Harness term={term} />);
+
+ // Enough wheel-downs to clamp any count to zero — but the CLI's screen
+ // still says the view is away, and the overlay outranks the count.
+ for (let i = 0; i < 10; i += 1) fireEvent.wheel(host(), { deltaY: 1 });
+ reachThePane();
+
+ await waitFor(() =>
+ expect(thumbTop() + thumbHeight()).toBeLessThan(TRACK_PX),
+ );
+ });
+
it("starts a fresh terminal's count from zero", async () => {
const first = appTerminal(24).term;
const { rerender } = render(<Harness term={first} epoch={1} />);
diff --git a/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.tsx b/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.tsx
index 149f4a43d2..7c30784931 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/PaneScrollbar.tsx
@@ -44,13 +44,19 @@ import { cn } from "@/lib/utils";
import type { TerminalAppearance } from "./terminalThemes";
import {
backAtThumbTop,
- countWheel,
+ exactView,
+ freshTravel,
hasScroll,
notchesFor,
+ screenTravel,
+ SCROLLED_BACK_MARKERS,
+ SETTLE_MS,
thumbBox,
- type ScrollView,
- exactView,
travelView,
+ wheelTravel,
+ type ScreenGlance,
+ type ScrollView,
+ type Travel,
} from "./scrollbarModel";
/** How long the bar lingers after the pointer leaves, so it cannot flicker. */
@@ -75,12 +81,14 @@ function appOwnsScreen(term: Terminal | null): boolean {
*/
export function readScrollView(
term: Terminal | null,
- travelled: number,
+ travel: Travel,
): ScrollView | null {
const buffer = term?.buffer?.active;
const rows = term?.rows ?? 0;
if (!term || !buffer || rows < 1) return null;
- if (appOwnsScreen(term)) return travelView(travelled, rows);
+ if (appOwnsScreen(term)) {
+ return travelView(travel.travelled, rows, travel.ceiling);
+ }
const view = exactView(
buffer.length ?? rows,
buffer.baseY ?? 0,
@@ -90,6 +98,39 @@ export function readScrollView(
return hasScroll(view) ? view : null;
}
+/**
+ * One look at an application-held pane's screen: is the scrolled-back
+ * overlay up, and what does the transcript region above it say?
+ *
+ * The overlay's row splits the screen — transcript above, the CLI's animated
+ * chrome (spinner, input box, status bars) below — so the fingerprint stops
+ * there: a spinner repainting every frame must not read as "the transcript
+ * moved". Returns null when the buffer cannot be read; the caller keeps its
+ * estimate rather than inventing one.
+ */
+export function readScreen(term: Terminal | null): ScreenGlance | null {
+ const buffer = term?.buffer?.active;
+ const rows = term?.rows ?? 0;
+ if (!term || !buffer || typeof buffer.getLine !== "function" || rows < 1) {
+ return null;
+ }
+ try {
+ const text: string[] = [];
+ for (let y = 0; y < rows; y += 1) {
+ text.push(buffer.getLine(y)?.translateToString(true) ?? "");
+ }
+ const markerRow = text.findIndex((row) =>
+ SCROLLED_BACK_MARKERS.some((marker) => marker.test(row)),
+ );
+ return {
+ markerRow,
+ fingerprint: markerRow > 0 ? text.slice(0, markerRow).join("\n") : "",
+ };
+ } catch {
+ return null;
+ }
+}
+
/**
* Hand `count` wheel notches to whatever runs in `host` (direction -1 is
* towards older output, matching a wheel turned away from the hand).
@@ -154,10 +195,11 @@ export function PaneScrollbar({
// would trail the pointer that is holding it.
const [heldTopPx, setHeldTopPx] = useState<number | null>(null);
- // Lines a full-screen CLI has been scrolled away from its live end, counted
- // off the wheel traffic — the user's own turns and this bar's relays alike,
- // since both pass the same observer below.
- const travelledRef = useRef(0);
+ // Where a full-screen CLI's pane stands, counted off the wheel traffic —
+ // the user's own turns and this bar's relays alike, since both pass the
+ // same observer below — and reconciled against the screen's own anchors
+ // (see ./scrollbarModel, "the three anchors").
+ const travelRef = useRef<Travel>(freshTravel());
// Which kind of pane the last reading saw. A CLI that enters or leaves the
// alternate screen mid-session (a shell running `less`) starts a different
// history, and a count carried across that line would describe the old one.
@@ -172,7 +214,7 @@ export function PaneScrollbar({
// A replaced terminal is a fresh transcript.
useEffect(() => {
- travelledRef.current = 0;
+ travelRef.current = freshTravel();
lastKindRef.current = null;
setView(null);
}, [epoch]);
@@ -214,7 +256,11 @@ export function PaneScrollbar({
const onWheel = (event: WheelEvent) => {
if (event.deltaY === 0) return;
if (appOwnsScreen(getTerminal())) {
- travelledRef.current = countWheel(travelledRef.current, event.deltaY);
+ travelRef.current = wheelTravel(
+ travelRef.current,
+ event.deltaY,
+ Date.now(),
+ );
}
scheduleRef.current?.();
setFlashing(true);
@@ -239,21 +285,35 @@ export function PaneScrollbar({
const term = getTerminal();
if (!term) return;
let frame: number | undefined;
+ let settleTimer: number | undefined;
const sync = () => {
frame = undefined;
- const next = readScrollView(term, travelledRef.current);
- // Crossing between a terminal-held and an application-held screen resets
- // the count: it described a history that is no longer on stage.
- if (next && lastKindRef.current && next.kind !== lastKindRef.current) {
- travelledRef.current = 0;
+ const appHeld = appOwnsScreen(term);
+ // Crossing between a terminal-held and an application-held screen
+ // resets the count: it described a history that is no longer on stage.
+ const kind: ScrollView["kind"] = appHeld ? "travel" : "exact";
+ if (lastKindRef.current && kind !== lastKindRef.current) {
+ travelRef.current = freshTravel();
+ }
+ lastKindRef.current = kind;
+ if (appHeld) {
+ // Reconcile the count with the screen's own anchors — the overlay
+ // and the saturation brake (see ./scrollbarModel).
+ travelRef.current = screenTravel(
+ travelRef.current,
+ readScreen(term),
+ Date.now(),
+ );
+ // Unconfirmed ups need a second look once the pty has had its say,
+ // even if no event arrives to prompt one.
+ if (settleTimer !== undefined) window.clearTimeout(settleTimer);
+ if (travelRef.current.pendingUp > 0) {
+ settleTimer = window.setTimeout(schedule, SETTLE_MS + 50);
+ }
}
- if (next) lastKindRef.current = next.kind;
- const settled =
- next && next.kind === "travel"
- ? readScrollView(term, travelledRef.current)
- : next;
- setView((current) => (sameView(current, settled) ? current : settled));
+ const next = readScrollView(term, travelRef.current);
+ setView((current) => (sameView(current, next) ? current : next));
};
const schedule = () => {
if (frame === undefined) frame = requestAnimationFrame(sync);
@@ -270,6 +330,7 @@ export function PaneScrollbar({
return () => {
scheduleRef.current = null;
if (frame !== undefined) cancelAnimationFrame(frame);
+ if (settleTimer !== undefined) window.clearTimeout(settleTimer);
for (const subscription of subscriptions) subscription?.dispose();
};
}, [shown, getTerminal, epoch]);
@@ -298,6 +359,9 @@ export function PaneScrollbar({
const term = getTerminal();
if (!term || lines === 0) return;
if (appOwnsScreen(term)) {
+ // Nothing above the top: while the brake holds, an up-notch would be
+ // ignored by the application and only pollute its pty.
+ if (lines > 0 && travelRef.current.saturated) return;
const notches = notchesFor(lines);
relayNotches(
hostRef.current,
@@ -319,7 +383,7 @@ export function PaneScrollbar({
if (appOwnsScreen(term)) {
// Asked in notches, against what has actually been relayed so far —
// the observer above moves the count as each notch goes past.
- scrollBy(wanted - travelledRef.current);
+ scrollBy(wanted - travelRef.current.travelled);
return;
}
// A terminal that owns its scrollback is told the line outright, so a
diff --git a/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.test.ts b/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.test.ts
index 00eb6548c5..d81cde3ff6 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.test.ts
+++ b/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.test.ts
@@ -1,15 +1,19 @@
import { describe, expect, it } from "vitest";
import {
backAtThumbTop,
- countWheel,
exactView,
+ freshTravel,
hasScroll,
LINES_PER_NOTCH,
MAX_NOTCHES_PER_STEP,
MIN_THUMB_PX,
notchesFor,
+ screenTravel,
+ SETTLE_MS,
thumbBox,
travelView,
+ wheelTravel,
+ type Travel,
} from "./scrollbarModel";
describe("exactView", () => {
@@ -42,6 +46,13 @@ describe("travelView", () => {
it("never stands behind the live end", () => {
expect(travelView(-5, 24).back).toBe(0);
});
+
+ it("maps the real span once the top has been measured", () => {
+ const view = travelView(90, 24, 90);
+ // At the measured ceiling the thumb genuinely reaches the top.
+ expect(view.above).toBe(90);
+ expect(thumbBox(view, 300)!.topPx).toBe(0);
+ });
});
describe("hasScroll", () => {
@@ -105,20 +116,113 @@ describe("notchesFor", () => {
});
});
-describe("countWheel", () => {
+describe("wheelTravel", () => {
it("counts a turn away from the hand as travelling back", () => {
- expect(countWheel(0, -1)).toBe(LINES_PER_NOTCH);
- expect(countWheel(LINES_PER_NOTCH, 1)).toBe(0);
+ const t = wheelTravel(freshTravel(), -1, 1000);
+ expect(t.travelled).toBe(LINES_PER_NOTCH);
+ expect(t.pendingUp).toBe(LINES_PER_NOTCH);
+ expect(wheelTravel(t, 1, 1001).travelled).toBe(0);
+ });
+
+ it("clamps at zero however far past the live end the wheel turns", () => {
+ let t = wheelTravel(freshTravel(), -1, 0);
+ for (let i = 0; i < 10; i += 1) t = wheelTravel(t, 1, i);
+ expect(t.travelled).toBe(0);
});
- it("re-anchors at zero however far past the live end the wheel turns", () => {
- let travelled = countWheel(0, -1);
- for (let i = 0; i < 10; i += 1) travelled = countWheel(travelled, 1);
- expect(travelled).toBe(0);
+ it("stops counting ups while the top is saturated", () => {
+ const saturated: Travel = { ...freshTravel(), travelled: 30, saturated: true };
+ expect(wheelTravel(saturated, -1, 0).travelled).toBe(30);
+ });
+
+ it("a turn towards the live end releases the brake", () => {
+ const saturated: Travel = { ...freshTravel(), travelled: 30, saturated: true };
+ const t = wheelTravel(saturated, 1, 0);
+ expect(t.saturated).toBe(false);
+ expect(t.travelled).toBe(27);
});
it("ignores a turn that moved nothing", () => {
- expect(countWheel(6, 0)).toBe(6);
- expect(countWheel(6, Number.NaN)).toBe(6);
+ const t: Travel = { ...freshTravel(), travelled: 6 };
+ expect(wheelTravel(t, 0, 0)).toBe(t);
+ expect(wheelTravel(t, Number.NaN, 0)).toBe(t);
+ });
+});
+
+describe("screenTravel", () => {
+ const seen = (travelled: number): Travel => ({
+ ...freshTravel(),
+ travelled,
+ markerSeen: true,
+ fingerprint: "same",
+ });
+
+ it("learns that this application paints a scrolled-back overlay", () => {
+ const t = screenTravel(
+ freshTravel(),
+ { markerRow: 22, fingerprint: "a" },
+ 0,
+ );
+ expect(t.markerSeen).toBe(true);
+ });
+
+ it("snaps to the live end the moment the overlay disappears", () => {
+ // The user's reported bug: at the newest output with the thumb mid-track.
+ const t = screenTravel(seen(300), { markerRow: -1, fingerprint: "" }, 0);
+ expect(t.travelled).toBe(0);
+ expect(t.saturated).toBe(false);
+ });
+
+ it("does not treat a missing overlay as an anchor before one was ever seen", () => {
+ const t: Travel = { ...freshTravel(), travelled: 60 };
+ expect(
+ screenTravel(t, { markerRow: -1, fingerprint: "" }, 0).travelled,
+ ).toBe(60);
+ });
+
+ it("keeps the estimate when the screen cannot be read", () => {
+ const t = seen(60);
+ expect(screenTravel(t, null, 0)).toBe(t);
+ });
+
+ it("confirms pending ups when the transcript moved", () => {
+ const t: Travel = { ...seen(60), pendingUp: 30, lastUpAt: 0 };
+ const next = screenTravel(t, { markerRow: 22, fingerprint: "moved" }, 1);
+ expect(next.travelled).toBe(60);
+ expect(next.pendingUp).toBe(0);
+ expect(next.saturated).toBe(false);
+ });
+
+ it("un-counts ups the application ignored at the top, and measures it", () => {
+ // The user's other reported bug: scrolling past a top nobody can see.
+ const t: Travel = { ...seen(90), pendingUp: 30, lastUpAt: 0 };
+ const next = screenTravel(
+ t,
+ { markerRow: 22, fingerprint: "same" },
+ SETTLE_MS,
+ );
+ expect(next.travelled).toBe(60);
+ expect(next.saturated).toBe(true);
+ expect(next.ceiling).toBe(60);
+ });
+
+ it("waits for the pty before judging an unmoved screen", () => {
+ const t: Travel = { ...seen(90), pendingUp: 30, lastUpAt: 0 };
+ const next = screenTravel(
+ t,
+ { markerRow: 22, fingerprint: "same" },
+ SETTLE_MS - 1,
+ );
+ expect(next.travelled).toBe(90);
+ expect(next.saturated).toBe(false);
+ });
+
+ it("floors the count while the overlay says the view is away", () => {
+ const t = screenTravel(
+ { ...seen(0), fingerprint: "a" },
+ { markerRow: 22, fingerprint: "a" },
+ 0,
+ );
+ expect(t.travelled).toBe(LINES_PER_NOTCH);
});
});
diff --git a/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.ts b/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.ts
index d984e4f3dc..5b668c1c93 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.ts
+++ b/jarvis/ui/web/frontend/src/components/agentic/scrollbarModel.ts
@@ -18,9 +18,34 @@
* Whenever the user returns to the live end the count re-anchors at zero,
* so the one position that matters is always right.
*
+ * ## The three anchors that keep the count honest
+ *
+ * Pure counting drifts, and the drift was visible in the product within a day:
+ * notches relayed at the transcript's TOP are silently ignored by the CLI but
+ * were still counted, so the claimed position inflated — and after scrolling
+ * back down the thumb hung mid-track at the live end. Probed against the real
+ * CLI (2026-07-28, hidden pty): an ignored notch emits ZERO bytes, so the
+ * stream cannot tell. What CAN tell, and what the count is anchored to:
+ *
+ * 1. **The scrolled-back overlay.** Claude Code paints "Jump to bottom
+ * (ctrl+End)" onto the screen whenever the view left the live end, and
+ * erases it there. Once a pane has seen that overlay ONCE (capability
+ * learned, not provider-gated), its absence is proof of "at the newest
+ * output" and snaps the count to zero — the anchor that ends thumb-stuck-
+ * mid-track. Its presence floors the count at one notch.
+ * 2. **The saturation brake.** Up-notches whose repaint never arrives (the
+ * transcript region above the overlay unchanged once the pty had time to
+ * answer) were ignored by the CLI: they are un-counted, and further ups are
+ * neither counted nor relayed until the screen moves again. The top stops
+ * being scrollable-forever.
+ * 3. **The measured ceiling.** The moment the brake engages, the total travel
+ * to the top is KNOWN, and the thumb maps the real span — top means top.
+ *
+ * An application that paints no overlay keeps the plain counted behaviour.
* An earlier generation of this feature tried to MEASURE the second kind by
- * nudging the application and comparing screens, and every unreadable screen
- * came out as "nothing to scroll". Nothing in this module measures anything.
+ * nudging the application and comparing screens as its ONLY source of truth,
+ * and every unreadable screen came out as "nothing to scroll". The brake
+ * fails the other way: judging nothing, it merely keeps the old estimate.
*/
/** Lines one relayed wheel notch moves a full-screen CLI (measured: ~3). */
@@ -37,9 +62,10 @@ export const MIN_THUMB_PX = 24;
* without a ceiling floods the pty with mouse reports it will spend seconds
* working through — long after the hand has stopped. The next move sends the
* remainder anyway, because each step is computed against what has actually
- * been relayed so far.
+ * been relayed so far. Kept small also to bound how far a burst can overrun
+ * the transcript's top before the saturation brake can see it.
*/
-export const MAX_NOTCHES_PER_STEP = 90;
+export const MAX_NOTCHES_PER_STEP = 30;
/** What a pane can scroll right now, in lines. */
export interface ScrollView {
@@ -91,12 +117,166 @@ export function exactView(
}
/**
- * The view of a pane whose application owns the screen: the distance
- * travelled from the live end, plus one screenful of assumed headroom.
+ * The view of a pane whose application owns the screen.
+ *
+ * With the top never reached, the span is the distance travelled plus one
+ * screenful of assumed headroom, so there is always somewhere left to go.
+ * Once the top HAS been reached the total is known ({@link Travel.ceiling})
+ * and the span is that measurement — the thumb can then genuinely say "top".
*/
-export function travelView(travelled: number, rows: number): ScrollView {
+export function travelView(
+ travelled: number,
+ rows: number,
+ ceiling: number | null = null,
+): ScrollView {
const back = Math.max(0, Math.round(travelled));
- return { kind: "travel", above: back + rows, back, rows };
+ const above = ceiling === null ? back + rows : Math.max(back, ceiling);
+ return { kind: "travel", above, back, rows };
+}
+
+// ---------------------------------------------------------------- travel
+
+/**
+ * How long the pty gets to answer an up-notch before an unchanged screen
+ * counts as proof the notch fell off the top. The CLI answers a notch in
+ * 1–30 ms (measured); the margin covers the socket and a busy render loop.
+ */
+export const SETTLE_MS = 350;
+
+/**
+ * Overlays a full-screen CLI paints while its view is away from the live
+ * end. Matched against the visible screen; the row that matches also splits
+ * the transcript (above it) from the animated chrome (below it), which is
+ * why the fingerprint in {@link screenTravel} covers only the rows above.
+ */
+export const SCROLLED_BACK_MARKERS = [/Jump to bottom/i];
+
+/** What a full-screen CLI's pane knows about where it stands. */
+export interface Travel {
+ /** Best estimate of lines back from the live end. */
+ travelled: number;
+ /** This application paints a scrolled-back overlay — learned, never assumed. */
+ markerSeen: boolean;
+ /** Total travel measured at the transcript's top, once reached. */
+ ceiling: number | null;
+ /** Up-lines counted but not yet confirmed by a repaint. */
+ pendingUp: number;
+ /** When the newest unconfirmed up was counted. */
+ lastUpAt: number;
+ /** The transcript region's last known content, for the brake. */
+ fingerprint: string | null;
+ /** Ups are known to be falling off the top right now. */
+ saturated: boolean;
+}
+
+export function freshTravel(): Travel {
+ return {
+ travelled: 0,
+ markerSeen: false,
+ ceiling: null,
+ pendingUp: 0,
+ lastUpAt: 0,
+ fingerprint: null,
+ saturated: false,
+ };
+}
+
+/** What one look at the screen saw — see {@link readScreen} in the component. */
+export interface ScreenGlance {
+ /** Index of the scrolled-back overlay's row, or -1 when absent. */
+ markerRow: number;
+ /** Content of the transcript region above the overlay ("" without one). */
+ fingerprint: string;
+}
+
+/** The count after one observed wheel event (real or relayed alike). */
+export function wheelTravel(travel: Travel, deltaY: number, now: number): Travel {
+ if (!Number.isFinite(deltaY) || deltaY === 0) return travel;
+ if (deltaY > 0) {
+ // Towards the live end: a down always has somewhere to go while the
+ // count is positive, and leaving the top ends the saturation.
+ return {
+ ...travel,
+ travelled: Math.max(0, travel.travelled - LINES_PER_NOTCH),
+ pendingUp: 0,
+ saturated: false,
+ };
+ }
+ // Away from the live end. While saturated these are known to be ignored by
+ // the application — counting them is exactly the inflation this fixes.
+ if (travel.saturated) return travel;
+ return {
+ ...travel,
+ travelled: travel.travelled + LINES_PER_NOTCH,
+ pendingUp: travel.pendingUp + LINES_PER_NOTCH,
+ lastUpAt: now,
+ };
+}
+
+/**
+ * Reconcile the count with what the screen actually shows.
+ *
+ * `glance` is null when the screen could not be read — the estimate then
+ * simply stands, which is the fail-open this module owes its history.
+ */
+export function screenTravel(
+ travel: Travel,
+ glance: ScreenGlance | null,
+ now: number,
+): Travel {
+ if (!glance) return travel;
+
+ if (glance.markerRow < 0) {
+ // No overlay. From an application known to paint one, that is proof of
+ // the live end — the anchor that ends "thumb stuck mid-track".
+ if (!travel.markerSeen) return travel;
+ if (
+ travel.travelled === 0 &&
+ travel.pendingUp === 0 &&
+ !travel.saturated
+ ) {
+ return travel;
+ }
+ return {
+ ...travel,
+ travelled: 0,
+ pendingUp: 0,
+ saturated: false,
+ fingerprint: null,
+ };
+ }
+
+ let next: Travel = travel.markerSeen
+ ? travel
+ : { ...travel, markerSeen: true };
+
+ if (next.fingerprint !== glance.fingerprint) {
+ // The transcript moved: whatever was pending has been answered.
+ next = {
+ ...next,
+ fingerprint: glance.fingerprint,
+ pendingUp: 0,
+ saturated: false,
+ };
+ } else if (next.pendingUp > 0 && now - next.lastUpAt >= SETTLE_MS) {
+ // Counted ups, an answered pty, an unmoved screen: those notches fell
+ // off the top. Un-count them and remember where the top IS.
+ const travelled = Math.max(0, next.travelled - next.pendingUp);
+ next = {
+ ...next,
+ travelled,
+ pendingUp: 0,
+ saturated: true,
+ ceiling: travelled,
+ };
+ }
+
+ // The overlay itself says the view is away from the live end — a count of
+ // zero would draw the thumb at a bottom the application denies.
+ if (next.travelled === 0) {
+ next = { ...next, travelled: LINES_PER_NOTCH };
+ }
+ return next;
}
/** Is there anywhere for this pane to go? */
@@ -144,17 +324,3 @@ export function notchesFor(lines: number): number {
return clamp(whole, -MAX_NOTCHES_PER_STEP, MAX_NOTCHES_PER_STEP);
}
-/**
- * The travel count after one observed wheel event.
- *
- * Counted per event rather than per delta pixel: the application scrolls per
- * received notch, and both a gentle and a violent turn of a real wheel arrive
- * as one event per notch. The count can only be an estimate either way — what
- * keeps it honest is the clamp at zero, where it re-anchors against the one
- * position the application also stops at.
- */
-export function countWheel(travelled: number, deltaY: number): number {
- if (!Number.isFinite(deltaY) || deltaY === 0) return travelled;
- const step = deltaY < 0 ? LINES_PER_NOTCH : -LINES_PER_NOTCH;
- return Math.max(0, travelled + step);
-}