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(chat): follow reasoning traces as they grow past the fold

rubenluetke10-beep1e15ad99e79e
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx b/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx
index 5743f3fb76..b9544a0d8c 100644
--- a/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx
@@ -1,4 +1,4 @@
-import { memo, useEffect, useId, useMemo, useRef, useState, type ReactNode } from "react";
+import { memo, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { Brain, Check, ChevronRight, CircleAlert, CircleDashed, FilePenLine, FileText, FolderSearch, ShieldQuestion, Terminal } from "lucide-react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
@@ -127,6 +127,24 @@ function Disclosure({ label, children, forced = false, initiallyOpen = false, ic
);
}
+function ReasoningBody({ text, live }: { text: string; live: boolean }) {
+ const ref = useRef<HTMLDivElement>(null);
+ useLayoutEffect(() => {
+ const el = ref.current;
+ if (!el || !live) return;
+ el.scrollTop = el.scrollHeight;
+ }, [text, live]);
+ return (
+ <div
+ ref={ref}
+ data-testid="reasoning-body"
+ className="prose prose-sm max-h-64 max-w-none overflow-auto text-xs leading-6 text-muted-foreground dark:prose-invert [overflow-wrap:anywhere] prose-p:my-1 prose-pre:overflow-auto"
+ >
+ <ReactMarkdown remarkPlugins={[remarkGfm]}>{text}</ReactMarkdown>
+ </div>
+ );
+}
+
export function ReasoningTrace({ block, turnLive, compact = false }: { block: ReasoningBlock; turnLive: boolean; compact?: boolean }) {
const t = useT();
const live = turnLive && block.live;
@@ -140,9 +158,7 @@ export function ReasoningTrace({ block, turnLive, compact = false }: { block: Re
forced={live} initiallyOpen={compact ? live : turnLive}
resetKey={compact ? String(turnLive) : ""}
summary={!compact && !turnLive && gist ? <p className="mb-2 ml-6 line-clamp-2 text-xs leading-5">{gist.slice(0, 240)}</p> : undefined}>
- {text ? <div className="prose prose-sm max-h-64 max-w-none overflow-auto text-xs leading-6 text-muted-foreground dark:prose-invert [overflow-wrap:anywhere] prose-p:my-1 prose-pre:overflow-auto">
- <ReactMarkdown remarkPlugins={[remarkGfm]}>{text}</ReactMarkdown>
- </div> : undefined}
+ {text ? <ReasoningBody text={text} live={live} /> : undefined}
</Disclosure>;
}
diff --git a/jarvis/ui/web/frontend/src/components/home/ChatStage.tsx b/jarvis/ui/web/frontend/src/components/home/ChatStage.tsx
index 475ed0b4de..4966b04e62 100644
--- a/jarvis/ui/web/frontend/src/components/home/ChatStage.tsx
+++ b/jarvis/ui/web/frontend/src/components/home/ChatStage.tsx
@@ -156,15 +156,17 @@ function ChatStageContent() {
scrollMessageToTop(viewport, lastItemId);
return;
}
- // A new item under a turn that has outgrown its window — or an opened
- // conversation landing at its end, or a coding agent's next tool call
- // arriving under a pane that was already full: the end, if the view was
- // there. A reader who scrolled up is left where they are.
+ // A turn that has outgrown its window — a live reasoning trace, a
+ // coding agent's next tool call, an opened conversation landing at
+ // its end: follow if the view was already there. Growth in place
+ // (same item, more words) used to wait for ResizeObserver and lose
+ // the reader for that frame. A reader who scrolled up is left where
+ // they are (`follow` no-ops).
const roomLeft = applySpacer();
- if (isNew && !roomLeft) follow();
+ if (!roomLeft) follow();
// `applySpacer` reads refs only and is recreated per render by design.
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [lastItemId, lastItemIsUser, activeSessionId, items.length, follow]);
+ }, [lastItemId, lastItemIsUser, activeSessionId, items, follow]);
// The live blocks grow without a new item — a reasoning trace, a streamed
// answer — so no render-driven effect fires for it; the column's own size
diff --git a/jarvis/ui/web/frontend/src/components/society/chat/AgentChatPanel.tsx b/jarvis/ui/web/frontend/src/components/society/chat/AgentChatPanel.tsx
index bd2fe427ee..b4067afca9 100644
--- a/jarvis/ui/web/frontend/src/components/society/chat/AgentChatPanel.tsx
+++ b/jarvis/ui/web/frontend/src/components/society/chat/AgentChatPanel.tsx
@@ -617,7 +617,11 @@ export function Transcript({
// watches the content's own size too, so growth follows; scrolled up, the
// reader keeps their place and gets a button back.
const { rootRef, contentRef, atEnd, jumpToEnd, follow } = useStickToBottom();
- useLayoutEffect(follow, [follow, items.length]);
+ // `items` itself, not its length: a reasoning trace or tool row grows
+ // the same turn in place, so the length does not change. Pin in this
+ // layout pass — waiting for ResizeObserver is one frame too late, and
+ // that frame is when overflow anchoring would unstick the view.
+ useLayoutEffect(follow, [follow, items]);
if (items.length === 0) {
return (
diff --git a/jarvis/ui/web/frontend/src/components/society/chat/ReasoningTrace.test.tsx b/jarvis/ui/web/frontend/src/components/society/chat/ReasoningTrace.test.tsx
index f807878ba3..fb36c58dfd 100644
--- a/jarvis/ui/web/frontend/src/components/society/chat/ReasoningTrace.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/society/chat/ReasoningTrace.test.tsx
@@ -17,3 +17,12 @@ it("does not offer an empty disclosure for redacted thought", () => {
render(<ReasoningTrace block={{...block, text: ""}} turnLive={false} />);
expect((screen.getByRole("button") as HTMLButtonElement).disabled).toBe(true);
});
+it("keeps the live thought pinned to its newest line", () => {
+ const live: ReasoningBlock = { ...block, live: true, durationMs: null, text: "First line.\n" };
+ const { rerender } = render(<ReasoningTrace block={live} turnLive />);
+ const body = screen.getByTestId("reasoning-body");
+ Object.defineProperty(body, "scrollHeight", { value: 400, configurable: true });
+ body.scrollTop = 0;
+ rerender(<ReasoningTrace block={{ ...live, text: "First line.\nSecond line that wraps past the box." }} turnLive />);
+ expect(body.scrollTop).toBe(400);
+});
diff --git a/jarvis/ui/web/frontend/src/components/society/chat/TranscriptFollow.test.tsx b/jarvis/ui/web/frontend/src/components/society/chat/TranscriptFollow.test.tsx
index d80d3da2b8..69c132f23c 100644
--- a/jarvis/ui/web/frontend/src/components/society/chat/TranscriptFollow.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/society/chat/TranscriptFollow.test.tsx
@@ -96,6 +96,28 @@ describe("society transcript follow", () => {
expect(el.scrollTop).toBe(2000);
});
+ it("follows in the same layout pass when the turn grows without a new item", () => {
+ const events = runningTurn("First I will check the inbox");
+ const { rerender } = draw(events);
+ const el = scroller();
+ measure(el, 950, 1000, 50);
+ act(() => {
+ fireEvent.scroll(el);
+ });
+
+ const grown = reduceEvents(EMPTY_TIMELINE, [
+ ...events,
+ ev("reasoning_delta", { turn_id: "t1", message_id: "r1", text: ", then the calendar, then the drive." }),
+ ]).items;
+ // Layout already happened: the column is taller, the view still sits
+ // where it was. The layout effect must pin before paint — not wait
+ // for a resize observer that jsdom (and one browser frame) do not have.
+ measure(el, 950, 2000, 50);
+ rerender(<Transcript items={grown} agent={agent} roster={[]} onDecide={async () => {}} />);
+ expect(el.scrollTop).toBe(2000);
+ expect(screen.queryByTestId("society-scroll-end")).toBeNull();
+ });
+
it("pulls new turns along while the view sits at the end", () => {
const events = runningTurn("First I will check the inbox");
const { rerender } = draw(events);
diff --git a/jarvis/ui/web/frontend/src/hooks/useStickToBottom.test.tsx b/jarvis/ui/web/frontend/src/hooks/useStickToBottom.test.tsx
index ef10726bb1..f224e4716e 100644
--- a/jarvis/ui/web/frontend/src/hooks/useStickToBottom.test.tsx
+++ b/jarvis/ui/web/frontend/src/hooks/useStickToBottom.test.tsx
@@ -118,4 +118,40 @@ describe("useStickToBottom", () => {
});
expect(scroller.scrollTop).toBe(2000);
});
+
+ it("stays at the end when a thought grows past the near-end slack before the observer runs", () => {
+ render(<Thread />);
+ const scroller = screen.getByTestId("scroller");
+ measure(scroller, 950, 1000, 50);
+ act(() => {
+ fireEvent.scroll(scroller);
+ });
+
+ // A live reasoning block is taller than NEAR_END_PX in one frame.
+ // Overflow anchoring fires `scroll` with the old top; that must not
+ // read as "the reader moved away".
+ measure(scroller, 950, 2000, 50);
+ act(() => {
+ fireEvent.scroll(scroller);
+ });
+ expect(scroller.scrollTop).toBe(2000);
+ expect(screen.queryByTestId("back")).toBeNull();
+ });
+
+ it("does not pin a reader who scrolled up when the answer then grows", () => {
+ render(<Thread />);
+ const scroller = screen.getByTestId("scroller");
+ measure(scroller, 100, 1000, 50);
+ act(() => {
+ fireEvent.scroll(scroller);
+ });
+ expect(screen.getByTestId("back")).toBeTruthy();
+
+ measure(scroller, 100, 2000, 50);
+ act(() => {
+ fireEvent.scroll(scroller);
+ });
+ expect(scroller.scrollTop).toBe(100);
+ expect(screen.getByTestId("back")).toBeTruthy();
+ });
});
diff --git a/jarvis/ui/web/frontend/src/hooks/useStickToBottom.ts b/jarvis/ui/web/frontend/src/hooks/useStickToBottom.ts
index b393a23d10..b186205ab6 100644
--- a/jarvis/ui/web/frontend/src/hooks/useStickToBottom.ts
+++ b/jarvis/ui/web/frontend/src/hooks/useStickToBottom.ts
@@ -38,6 +38,18 @@ export function useStickToBottom() {
const [contentEl, setContentEl] = useState<HTMLElement | null>(null);
const stickRef = useRef(true);
const [atEnd, setAtEnd] = useState(true);
+ // Layout growth (a live thought, a tool row) is taller than NEAR_END_PX in
+ // one frame. Overflow anchoring then fires `scroll` with the OLD top —
+ // before any resize observer can pin — and `isNearEnd` would lie that the
+ // reader moved away. Remembering the last size lets that event re-pin
+ // instead of unsticking; a real scroll-up still unsticks.
+ const lastHeightRef = useRef(0);
+ const lastTopRef = useRef(0);
+ // Smooth "jump to end" emits many `scroll` events that are not near the
+ // end yet. Those must not clear the stick, or the next reasoning token
+ // leaves the reader stranded mid-animation.
+ const jumpingRef = useRef(false);
+ const jumpTimerRef = useRef(0);
const rootRef = useCallback((node: HTMLElement | null) => {
rootElRef.current = node;
@@ -48,11 +60,17 @@ export function useStickToBottom() {
setContentEl(node);
}, []);
+ const pinToEnd = (viewport: HTMLElement) => {
+ viewport.scrollTop = viewport.scrollHeight;
+ lastHeightRef.current = viewport.scrollHeight;
+ lastTopRef.current = viewport.scrollTop;
+ };
+
/** Pull the view to the end — but only if it was already there. */
const follow = useCallback(() => {
const viewport = scrollViewportOf(rootElRef.current);
if (!viewport || !stickRef.current) return;
- viewport.scrollTop = viewport.scrollHeight;
+ pinToEnd(viewport);
}, []);
/** Take the reader back to the end, and follow again from there. */
@@ -62,9 +80,17 @@ export function useStickToBottom() {
stickRef.current = true;
setAtEnd(true);
if (!prefersReducedMotion() && typeof viewport.scrollTo === "function") {
+ jumpingRef.current = true;
viewport.scrollTo({ top: viewport.scrollHeight, behavior: "smooth" });
+ window.clearTimeout(jumpTimerRef.current);
+ jumpTimerRef.current = window.setTimeout(() => {
+ if (!jumpingRef.current) return;
+ jumpingRef.current = false;
+ pinToEnd(viewport);
+ setAtEnd(true);
+ }, 400);
} else {
- viewport.scrollTop = viewport.scrollHeight;
+ pinToEnd(viewport);
}
}, []);
@@ -73,14 +99,54 @@ export function useStickToBottom() {
useEffect(() => {
const viewport = scrollViewportOf(rootEl);
if (!viewport) return;
+ const previousAnchor = viewport.style.overflowAnchor;
+ viewport.style.overflowAnchor = "none";
const read = () => {
- const near = isNearEnd(viewport.scrollTop, viewport.scrollHeight, viewport.clientHeight);
+ if (jumpingRef.current) {
+ stickRef.current = true;
+ if (isNearEnd(viewport.scrollTop, viewport.scrollHeight, viewport.clientHeight)) {
+ jumpingRef.current = false;
+ pinToEnd(viewport);
+ setAtEnd(true);
+ }
+ return;
+ }
+ const height = viewport.scrollHeight;
+ const top = viewport.scrollTop;
+ // A first observation (height still 0) is not growth — jsdom and a
+ // just-mounted pane both start there. Treating it as growth would pin
+ // a reader who opened the thread mid-way.
+ const hadLayout = lastHeightRef.current > 0;
+ const grew = hadLayout && height > lastHeightRef.current;
+ const scrolledUp = hadLayout && top + 1 < lastTopRef.current;
+ // Content grew while we were following, and the reader did not scroll
+ // up: this `scroll` is layout, not a choice. Stay stuck and pin.
+ if (stickRef.current && grew && !scrolledUp) {
+ pinToEnd(viewport);
+ setAtEnd(true);
+ return;
+ }
+ lastHeightRef.current = height;
+ lastTopRef.current = top;
+ const near = isNearEnd(top, height, viewport.clientHeight);
stickRef.current = near;
setAtEnd(near);
};
read();
viewport.addEventListener("scroll", read, { passive: true });
- return () => viewport.removeEventListener("scroll", read);
+ const onScrollEnd = () => {
+ if (!jumpingRef.current) return;
+ jumpingRef.current = false;
+ if (stickRef.current) pinToEnd(viewport);
+ setAtEnd(true);
+ };
+ viewport.addEventListener("scrollend", onScrollEnd);
+ return () => {
+ viewport.removeEventListener("scroll", read);
+ viewport.removeEventListener("scrollend", onScrollEnd);
+ viewport.style.overflowAnchor = previousAnchor;
+ window.clearTimeout(jumpTimerRef.current);
+ };
}, [rootEl]);
// An answer grows WITHOUT a new item arriving, so no render-driven effect
@@ -89,7 +155,9 @@ export function useStickToBottom() {
const viewport = scrollViewportOf(rootEl);
if (!viewport || !contentEl || typeof ResizeObserver === "undefined") return;
const ro = new ResizeObserver(() => {
- if (stickRef.current) viewport.scrollTop = viewport.scrollHeight;
+ if (!stickRef.current) return;
+ pinToEnd(viewport);
+ setAtEnd(true);
});
ro.observe(contentEl);
return () => ro.disconnect();