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.

feat(agent-chat): the model's thinking reads as a scratchpad where it happened

rubenluetke10-beepd8e5133d6b41
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.test.tsx b/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.test.tsx
new file mode 100644
index 0000000000..2c5f5fc39a
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.test.tsx
@@ -0,0 +1,226 @@
+import { act, cleanup, fireEvent, render, screen, within } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import {
+ AgentTimeline,
+ arrangeThinking,
+ thoughtGist,
+ thoughtNeedsFold,
+} from "@/components/agentchat/AgentTimeline";
+import { EMPTY_TIMELINE, reduceEvents, type TurnBlock } from "@/components/agentchat/reduce";
+import type { AgentChatEvent } from "@/lib/agentChatApi";
+
+/**
+ * The scratchpad — how a turn's thinking is shown.
+ *
+ * The complaint this file guards (maintainer, 2026-08-25): the model's
+ * intermediate steps had stopped showing. Every stretch of thought was
+ * merged into one folded "Thought for Ns" row at the top of the turn, and
+ * the running one drew nothing at all, so the reasoning was neither
+ * watchable while it happened nor readable where it happened.
+ */
+
+let seq = 0;
+function ev(kind: string, payload: Record<string, unknown>, tsMs = 1_000): AgentChatEvent {
+ seq += 1;
+ return { seq, ts_ms: tsMs, kind, payload } as AgentChatEvent;
+}
+
+const TURN = { provider: "openai-codex", model: "gpt-5.4", effort: "high", runner: "codex-cli" };
+
+function draw(events: AgentChatEvent[]) {
+ const { items } = reduceEvents(EMPTY_TIMELINE, events);
+ return render(
+ <AgentTimeline
+ items={items}
+ assistantName="Jarvis"
+ providerLabel={(id) => id}
+ onDecide={() => undefined}
+ />,
+ );
+}
+
+const LONG =
+ "First I need to find where the port is configured. The launcher reads it from the " +
+ "instance config, so the dev instance and the live one cannot collide.\n\n" +
+ "Then I will check whether anything else already listens there.";
+
+afterEach(() => {
+ cleanup();
+ seq = 0;
+});
+
+describe("arrangeThinking", () => {
+ const spoken = (id: string, text: string, live = false): TurnBlock => ({
+ kind: "reasoning",
+ id,
+ text,
+ durationMs: live ? null : 1000,
+ live,
+ startedMs: 0,
+ });
+ const silent = (id: string, durationMs: number, live = false): TurnBlock => ({
+ kind: "reasoning",
+ id,
+ text: "",
+ durationMs: live ? null : durationMs,
+ live,
+ startedMs: 0,
+ });
+ const tool: TurnBlock = {
+ kind: "tool",
+ callId: "c1",
+ name: "Bash",
+ input: {},
+ output: "ok",
+ isError: false,
+ durationMs: 10,
+ approval: null,
+ startedMs: 0,
+ };
+
+ it("keeps a thought with words where it happened, live or finished", () => {
+ const out = arrangeThinking([spoken("a", "Port first."), tool, spoken("b", "Now the log.", true)]);
+ expect(out.map((b) => (b.kind === "reasoning" ? b.id : b.kind))).toEqual(["a", "tool", "b"]);
+ });
+
+ it("folds the wordless thoughts into one timing row and drops a running one", () => {
+ const out = arrangeThinking([silent("a", 1000), tool, silent("b", 4000), silent("c", 0, true)]);
+ expect(out).toHaveLength(2);
+ const [row] = out;
+ expect(row.kind).toBe("reasoning");
+ if (row.kind === "reasoning") expect(row.durationMs).toBe(5000);
+ });
+});
+
+describe("thought helpers", () => {
+ it("folds only what a preview cannot hold", () => {
+ expect(thoughtNeedsFold("Looking in the wiki for the holiday dates.")).toBe(false);
+ expect(thoughtNeedsFold(LONG)).toBe(true);
+ expect(thoughtNeedsFold("x".repeat(201))).toBe(true);
+ });
+
+ it("flattens Markdown into one readable line", () => {
+ expect(thoughtGist("**Checking** the `config`\n\n- first\n- second")).toBe(
+ "Checking the config - first - second",
+ );
+ });
+});
+
+describe("the scratchpad", () => {
+ it("streams the thought as it is written, under the turn's one live line", () => {
+ vi.useFakeTimers();
+ try {
+ const started = Date.now();
+ draw([
+ ev("user_message", { text: "look into it" }, started),
+ ev("turn_started", { turn_id: "t1", ...TURN }, started),
+ ev("reasoning_started", { turn_id: "t1", message_id: "r1" }, started),
+ ev("usage_delta", { turn_id: "t1", usage: { input_tokens: 12, output_tokens: 300 } }, started),
+ ev("reasoning_delta", { turn_id: "t1", text: "The port is read from " }, started),
+ ev("reasoning_delta", { turn_id: "t1", text: "the instance config." }, started),
+ ]);
+
+ const pad = screen.getByTestId("agent-reasoning");
+ expect(pad.getAttribute("data-state")).toBe("live");
+ // The words are on screen while they arrive.
+ expect(pad.textContent).toContain("The port is read from the instance config.");
+ // …and the live line is the scratchpad's own header: the core, the
+ // running time, the tokens so far — once, not once here and once below.
+ const live = screen.getAllByTestId("agent-turn-live");
+ expect(live).toHaveLength(1);
+ expect(pad.contains(live[0])).toBe(true);
+ expect(within(live[0]).getByTestId("live-core")).toBeTruthy();
+ expect(live[0].textContent).toMatch(/300/);
+ act(() => {
+ vi.advanceTimersByTime(8_000);
+ });
+ expect(live[0].textContent).toMatch(/8s/);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("reads every stretch of thinking where it happened, open while the turn still works", () => {
+ draw([
+ ev("user_message", { text: "look into it" }),
+ ev("turn_started", { turn_id: "t2", ...TURN }),
+ ev("reasoning", { turn_id: "t2", text: LONG, duration_ms: 3000 }),
+ ev("tool_call", { turn_id: "t2", call_id: "c1", name: "Bash", input: { command: "ss -ltnp" } }),
+ ev("tool_result", { turn_id: "t2", call_id: "c1", output: "ok", is_error: false }),
+ ev("reasoning", { turn_id: "t2", text: "It is listening.", duration_ms: 1000 }),
+ ]);
+
+ const rows = screen.getAllByTestId("agent-reasoning");
+ expect(rows).toHaveLength(2);
+ // Thought, command, thought — in that order, not one merged row on top.
+ const order = Array.from(
+ document.querySelectorAll("[data-testid='agent-reasoning'],[data-testid='agent-tool']"),
+ ).map((el) => el.getAttribute("data-testid"));
+ expect(order).toEqual(["agent-reasoning", "agent-tool", "agent-reasoning"]);
+
+ // A long thought is open in full while the turn runs; the short one is
+ // whole either way, with nothing left to unfold.
+ expect(rows[0].getAttribute("data-state")).toBe("open");
+ expect(rows[0].textContent).toContain("3s");
+ expect(rows[0].textContent).toContain("already listens there");
+ expect(rows[1].getAttribute("data-state")).toBe("whole");
+ expect(rows[1].textContent).toContain("It is listening.");
+ expect(within(rows[1]).getByRole("button").hasAttribute("disabled")).toBe(true);
+
+ // The turn's own live line sits below, because no thought is streaming.
+ expect(screen.getAllByTestId("agent-turn-live")).toHaveLength(1);
+ expect(rows[0].contains(screen.getByTestId("agent-turn-live"))).toBe(false);
+ });
+
+ it("folds a long thought to a two-line preview once the turn is done, and opens on a click", () => {
+ draw([
+ ev("user_message", { text: "look into it" }),
+ ev("turn_started", { turn_id: "t3", ...TURN }),
+ ev("reasoning", { turn_id: "t3", text: LONG, duration_ms: 3000 }),
+ ev("assistant_text", { turn_id: "t3", message_id: "m1", text: "Running." }),
+ ev("turn_finished", { turn_id: "t3", status: "done", duration_ms: 4000, usage: {} }),
+ ]);
+
+ const pad = screen.getByTestId("agent-reasoning");
+ expect(pad.getAttribute("data-state")).toBe("folded");
+ // The preview still shows the thought's first words without a click.
+ const preview = within(pad).getByTestId("agent-reasoning-preview");
+ expect(preview.textContent).toContain("First I need to find where the port is configured.");
+ expect(preview.className).toContain("line-clamp-2");
+
+ fireEvent.click(within(pad).getByRole("button"));
+ expect(pad.getAttribute("data-state")).toBe("open");
+ expect(pad.textContent).toContain("already listens there");
+ fireEvent.click(within(pad).getByRole("button"));
+ expect(pad.getAttribute("data-state")).toBe("folded");
+ });
+
+ it("reduces wordless thinking to its time and never opens it", () => {
+ draw([
+ ev("turn_started", { turn_id: "t4", ...TURN }),
+ ev("reasoning", { turn_id: "t4", text: "", duration_ms: 2000 }),
+ ev("tool_call", { turn_id: "t4", call_id: "c1", name: "Read", input: { path: "a.py" } }),
+ ev("tool_result", { turn_id: "t4", call_id: "c1", output: "ok", is_error: false }),
+ ev("reasoning", { turn_id: "t4", text: "", duration_ms: 6000 }),
+ ev("assistant_text", { turn_id: "t4", message_id: "m1", text: "Done." }),
+ ev("turn_finished", { turn_id: "t4", status: "done", duration_ms: 9000, usage: {} }),
+ ]);
+
+ const rows = screen.getAllByTestId("agent-reasoning");
+ expect(rows).toHaveLength(1);
+ expect(rows[0].getAttribute("data-state")).toBe("silent");
+ expect(rows[0].textContent).toMatch(/8s/);
+ expect(within(rows[0]).getByRole("button").hasAttribute("disabled")).toBe(true);
+ });
+
+ it("draws nothing for a thought that has started but has no words yet", () => {
+ draw([
+ ev("turn_started", { turn_id: "t5", ...TURN }),
+ ev("reasoning_started", { turn_id: "t5", message_id: "r1" }),
+ ]);
+ expect(screen.queryByTestId("agent-reasoning")).toBeNull();
+ // The live line below is what says the turn is thinking.
+ expect(screen.getAllByTestId("agent-turn-live")).toHaveLength(1);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.tsx b/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.tsx
index 6dffe910ed..c4529f4e15 100644
--- a/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentchat/AgentTimeline.tsx
@@ -1,4 +1,12 @@
-import { memo, useEffect, useMemo, useState, type ReactNode } from "react";
+import {
+ memo,
+ useEffect,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+ type ReactNode,
+} from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import {
@@ -35,9 +43,9 @@ import { cn } from "@/lib/utils";
* right, each assistant turn flush-left under a byline that says who
* answered (the provider's mark, the model, the effort).
*
- * Under the byline, in the order they happened: ONE row for everything the
- * model thought (folded to "Thought for Ns"; a vendor that redacts its
- * thinking still gets the row, because the time is the fact), the tool calls
+ * Under the byline, in the order they happened: the model's thinking as a
+ * readable scratchpad wherever it thought (a vendor that redacts its
+ * thinking still gets a row, because the time is the fact), the tool calls
* under the names the agent's own log uses, approval cards where the runner
* asked, and the answer as Markdown.
*
@@ -51,10 +59,17 @@ import { cn } from "@/lib/utils";
* The turn itself is held together by its byline and the spacing; an
* earlier version hung it off a glowing hairline rail, which read as a
* stray stripe running down the transcript.
- * 2. The turn says it is thinking exactly ONCE. Every stretch of thought
- * used to get its own row, stacked above a live line saying the same
- * thing again; the finished thoughts now fold into a single row and the
- * running one is left to the live line alone.
+ * 2. The thinking is READ, not counted (maintainer, 2026-08-25: the
+ * intermediate steps had stopped showing). A stretch of thought that has
+ * words is a scratchpad in the place it happened: its text streams while
+ * the model writes it, stays open while the turn still works, and folds
+ * to a two-line preview once the turn is done. Only a thought with no
+ * words to show — a vendor that redacts (Claude Code) — is reduced to its
+ * time, and a running one of those draws nothing, because the live line
+ * already says the turn is thinking and two lines saying it at once is
+ * the duplication the maintainer objected to. A streaming scratchpad
+ * carries the live line in its own header for the same reason: the turn
+ * says it is working exactly ONCE.
* 3. The turn ALWAYS says which state it is in. While it runs: the live
* core, a word that changes as the minutes pass, the elapsed time and
* the tokens spent — the Claude CLI's "Composing… (2m 35s · ↓ 3.7k
@@ -169,7 +184,10 @@ const Turn = memo(function Turn({
const t = useT();
const live = turn.status === "running";
const elapsed = useElapsedMs(live ? turn.startedMs : null);
- const blocks = useMemo(() => foldThinking(turn.blocks), [turn.blocks]);
+ const blocks = useMemo(() => arrangeThinking(turn.blocks), [turn.blocks]);
+ // A scratchpad still receiving words carries the turn's live line in its
+ // own header; the line below would only say "thinking" a second time.
+ const thinkingAloud = blocks.some((b) => b.kind === "reasoning" && b.live);
return (
<div
@@ -199,11 +217,15 @@ const Turn = memo(function Turn({
<div className="flex flex-col gap-1.5 px-1">
{blocks.map((block) => {
if (block.kind === "text") return <Prose key={block.id} block={block} />;
- if (block.kind === "reasoning") return <Reasoning key={block.id} block={block} />;
+ if (block.kind === "reasoning") {
+ return (
+ <Thought key={block.id} block={block} turnLive={live} usage={turn.liveUsage} />
+ );
+ }
return <ToolRow key={block.callId} block={block} onDecide={onDecide} />;
})}
- {live && <LiveStatus elapsed={elapsed} usage={turn.liveUsage} />}
+ {live && !thinkingAloud && <LiveStatus elapsed={elapsed} usage={turn.liveUsage} />}
{turn.status === "error" && turn.error && (
<div
@@ -223,47 +245,50 @@ const Turn = memo(function Turn({
});
/**
- * All of a turn's thinking, folded into one row.
+ * Where each stretch of a turn's thinking goes.
*
- * A turn thinks, calls a tool, thinks again — and every stretch used to draw
- * its own "Thought for Ns" row, so an ordinary turn stacked two or three of
- * them above a live line that said the same thing once more (maintainer,
- * 2026-08-25: the thinking is shown twice). The finished
- * thoughts now merge into one row where the first of them happened, their
- * segments in order inside it and their seconds added up.
+ * A thought WITH words is a scratchpad and stays exactly where it happened —
+ * ahead of the tool call it explains, between two of them, before the
+ * answer. Reading the reasoning in the order it was written is the whole
+ * point of showing it; the earlier design merged every stretch into one row
+ * at the top of the turn, which hid the streaming text altogether and put
+ * the finished thoughts behind a single click (maintainer, 2026-08-25: the
+ * intermediate steps are no longer shown properly).
*
- * A thought that is still running gets NO row at all: the live line below
- * already says the turn is thinking, and saying it twice in a row IS the
- * duplication. It joins the folded row the moment it closes.
+ * A thought WITHOUT words is only a duration: a vendor that redacts its
+ * thinking (Claude Code), or a runner that announced the thought before any
+ * text arrived. Those stay quiet. The finished ones fold into ONE "Thought
+ * for Ns" row where the first of them happened, seconds added up — a stack
+ * of timing rows said nothing a sum does not. A running one draws nothing at
+ * all: the live line below already says the turn is thinking, and saying it
+ * twice in a row is the duplication the maintainer objected to.
*/
-export function foldThinking(blocks: TurnBlock[]): TurnBlock[] {
- const thoughts = blocks.filter(
- (b): b is ReasoningBlock => b.kind === "reasoning" && !b.live,
+export function arrangeThinking(blocks: TurnBlock[]): TurnBlock[] {
+ const silent = blocks.filter(
+ (b): b is ReasoningBlock => b.kind === "reasoning" && !b.live && !b.text.trim(),
);
- const merged: ReasoningBlock | null =
- thoughts.length === 0
+ const folded: ReasoningBlock | null =
+ silent.length === 0
? null
: {
kind: "reasoning",
- id: thoughts[0].id,
- text: thoughts
- .map((th) => th.text.trim())
- .filter(Boolean)
- .join("\n\n···\n\n"),
- durationMs: thoughts.reduce((sum, th) => sum + (th.durationMs ?? 0), 0),
+ id: silent[0].id,
+ text: "",
+ durationMs: silent.reduce((sum, th) => sum + (th.durationMs ?? 0), 0),
live: false,
- startedMs: thoughts[0].startedMs,
+ startedMs: silent[0].startedMs,
};
const out: TurnBlock[] = [];
let placed = false;
for (const block of blocks) {
- if (block.kind !== "reasoning") {
+ if (block.kind !== "reasoning" || block.text.trim()) {
out.push(block);
continue;
}
- if (merged && !placed) {
- out.push(merged);
+ if (block.live) continue;
+ if (folded && !placed) {
+ out.push(folded);
placed = true;
}
}
@@ -412,66 +437,234 @@ function Prose({ block }: { block: TextBlock }) {
);
}
+/** One stretch of thinking: a scratchpad when it has words, a timing row when it has none. */
+function Thought({
+ block,
+ turnLive,
+ usage,
+}: {
+ block: ReasoningBlock;
+ turnLive: boolean;
+ usage: Record<string, number> | null;
+}) {
+ if (!block.text.trim()) return <SilentThought block={block} />;
+ return <Scratchpad block={block} turnLive={turnLive} usage={usage} />;
+}
+
+/**
+ * Past this many characters (or at the first blank line) a finished thought
+ * is more than a preview can hold and earns a fold.
+ */
+const PREVIEW_CHARS = 200;
+
+/** Does a thought need a fold, or does its preview already show all of it? */
+export function thoughtNeedsFold(text: string): boolean {
+ const trimmed = text.trim();
+ return trimmed.length > PREVIEW_CHARS || /\n\s*\n/.test(trimmed);
+}
+
/**
- * Everything the turn thought, in one collapsed row.
+ * A thought as one flat line for the folded preview — no Markdown marks, no
+ * code blocks, no line breaks — so two lines of it read as a sentence.
+ */
+export function thoughtGist(text: string): string {
+ return text
+ .replace(/```[\s\S]*?```/g, " ")
+ .replace(/[`*_#>~]+/g, "")
+ .replace(/\s+/g, " ")
+ .trim();
+}
+
+/**
+ * The model's thinking, readable — the scratchpad.
*
- * Always a FINISHED thought — `foldThinking` keeps the running one off the
- * screen so this row never competes with the live line. "Thought for 8s"
- * opens to the text when there IS text; when the vendor redacted it (Claude
- * Code does) the row still states the time, because the time is the fact,
- * and stays shut rather than opening onto emptiness.
+ * WHILE THE WORDS ARRIVE it is open. Its header is the turn's live line for
+ * as long as the thought runs — the core, "Thinking for 8s", the tokens so
+ * far, the interrupt hint — and under a hairline sits the text itself,
+ * anchored to its newest lines so the reader watches the thought unfold
+ * instead of scrolling after it (the earlier lines fade out at the top once
+ * there are more than fit).
+ *
+ * WHEN THE THOUGHT CLOSES it keeps its place. It stays open while the turn
+ * still works, because watching the work is the point of a running turn,
+ * and folds to "Thought for 8s" over a two-line preview the moment the turn
+ * is done, so the answer is what a finished conversation reads as. A click
+ * opens it again; a thought short enough to fit its own preview never needs
+ * one and shows whole.
*
* The thought reads as prose, not as a log dump: models write their
* reasoning in Markdown, and a raw `**like this**` on screen is the same
* defect the local-models panel was fixed for.
*/
-function Reasoning({ block }: { block: ReasoningBlock }) {
+function Scratchpad({
+ block,
+ turnLive,
+ usage,
+}: {
+ block: ReasoningBlock;
+ turnLive: boolean;
+ usage: Record<string, number> | null;
+}) {
const t = useT();
- const [open, setOpen] = useState(false);
- const hasText = Boolean(block.text.trim());
+ // `null` until the person decides; until then the turn's state decides.
+ const [manual, setManual] = useState<boolean | null>(null);
+ const live = block.live;
+ const thinking = useElapsedMs(live ? block.startedMs : null);
+ const text = block.text.trim();
+ const foldable = thoughtNeedsFold(text);
+ const open = live || (foldable && (manual ?? turnLive));
const duration = block.durationMs ?? 0;
+ const out = outputTokens(usage);
const header =
duration > 0
? fill(t("agent_chat.thought_for"), { duration: formatThoughtDuration(duration) })
: t("agent_chat.thought");
return (
- <div data-testid="agent-reasoning">
- <button
- type="button"
- onClick={() => setOpen((v) => !v)}
- aria-expanded={open}
- disabled={!hasText}
- className={cn(
- "-ml-1 inline-flex items-center gap-1.5 rounded-md px-1 py-0.5 text-xs text-muted-foreground transition-colors",
- hasText ? "hover:bg-secondary/50 hover:text-foreground" : "cursor-default",
- )}
- >
- <ChevronRight
+ <div
+ data-testid="agent-reasoning"
+ data-state={live ? "live" : open ? "open" : foldable ? "folded" : "whole"}
+ >
+ {live ? (
+ <div
+ className="flex flex-wrap items-center gap-x-2 gap-y-0.5 py-0.5 text-xs"
+ data-testid="agent-turn-live"
+ role="status"
+ aria-live="polite"
+ >
+ <LiveCore />
+ <span className="thinking-shimmer font-medium">
+ {fill(t("agent_chat.thinking_for"), { duration: formatThoughtDuration(thinking) })}
+ </span>
+ {out !== null && out > 0 && (
+ <span className="inline-flex items-center gap-1.5 font-mono text-[11px] tabular-nums text-muted-foreground/80">
+ <span aria-hidden>·</span>
+ <OutTokens n={out} />
+ </span>
+ )}
+ <span className="text-muted-foreground/60">{t("agent_chat.interrupt_hint")}</span>
+ </div>
+ ) : (
+ <button
+ type="button"
+ onClick={() => setManual(!open)}
+ aria-expanded={open}
+ disabled={!foldable}
className={cn(
- "h-3.5 w-3.5 shrink-0 transition-transform",
- open && "rotate-90",
- !hasText && "opacity-30",
+ "-ml-1 inline-flex items-center gap-1.5 rounded-md px-1 py-0.5 text-xs text-muted-foreground transition-colors",
+ foldable ? "hover:bg-secondary/50 hover:text-foreground" : "cursor-default",
)}
- aria-hidden
- />
- <span>{header}</span>
- </button>
- {open && hasText && (
- <Trace>
- <div
+ >
+ <ChevronRight
className={cn(
- "prose prose-neutral max-w-none text-xs leading-relaxed text-muted-foreground dark:prose-invert [overflow-wrap:anywhere]",
- "prose-p:my-1.5 prose-headings:my-1.5 prose-headings:text-xs prose-headings:font-semibold",
- "prose-strong:text-foreground/80 prose-li:my-0.5 prose-ul:my-1.5 prose-ol:my-1.5",
- "prose-code:font-mono prose-code:text-[0.9em] prose-code:before:hidden prose-code:after:hidden",
- "prose-pre:my-1.5 prose-pre:bg-transparent prose-pre:p-0 prose-pre:text-[11px]",
+ "h-3.5 w-3.5 shrink-0 transition-transform",
+ open && "rotate-90",
+ !foldable && "opacity-30",
)}
- >
- <ReactMarkdown remarkPlugins={[remarkGfm]}>{block.text}</ReactMarkdown>
- </div>
+ aria-hidden
+ />
+ <span>{header}</span>
+ </button>
+ )}
+
+ {live ? (
+ <Trace>
+ <ThoughtTail text={text} />
</Trace>
+ ) : open ? (
+ <Trace>
+ <ThoughtProse text={text} />
+ </Trace>
+ ) : (
+ <div
+ className={cn(
+ "ml-[1.25rem] text-[13px] leading-relaxed text-muted-foreground/80 [overflow-wrap:anywhere]",
+ foldable && "line-clamp-2",
+ )}
+ data-testid="agent-reasoning-preview"
+ >
+ {thoughtGist(text)}
+ </div>
+ )}
+ </div>
+ );
+}
+
+/**
+ * The streaming thought, anchored to its newest lines.
+ *
+ * The window is a fixed height with the text pinned to its bottom edge, so
+ * new words push the old ones up and out of view — the reader always sees
+ * what the model is writing NOW. Once more has been written than fits, the
+ * top fades out to say so; a thought that fits is shown whole, unfaded.
+ */
+function ThoughtTail({ text }: { text: string }) {
+ const outer = useRef<HTMLDivElement>(null);
+ const inner = useRef<HTMLDivElement>(null);
+ const [clipped, setClipped] = useState(false);
+ useLayoutEffect(() => {
+ const box = outer.current;
+ const body = inner.current;
+ if (!box || !body) return;
+ setClipped(body.offsetHeight > box.clientHeight + 1);
+ }, [text]);
+ return (
+ <div
+ ref={outer}
+ className={cn(
+ "flex max-h-44 flex-col justify-end overflow-hidden",
+ clipped && "[mask-image:linear-gradient(to_bottom,transparent,black_2.75rem)]",
)}
+ data-testid="agent-reasoning-tail"
+ data-clipped={clipped ? "true" : undefined}
+ >
+ <div ref={inner}>
+ <ThoughtProse text={text} />
+ </div>
+ </div>
+ );
+}
+
+function ThoughtProse({ text }: { text: string }) {
+ return (
+ <div
+ className={cn(
+ "prose prose-neutral max-w-none text-[13px] leading-relaxed text-muted-foreground dark:prose-invert [overflow-wrap:anywhere]",
+ "prose-p:my-1.5 prose-headings:my-1.5 prose-headings:text-[13px] prose-headings:font-semibold prose-headings:text-foreground/80",
+ "prose-strong:text-foreground/80 prose-li:my-0.5 prose-ul:my-1.5 prose-ol:my-1.5",
+ "prose-code:font-mono prose-code:text-[0.9em] prose-code:before:hidden prose-code:after:hidden",
+ "prose-pre:my-1.5 prose-pre:bg-transparent prose-pre:p-0 prose-pre:text-[11px]",
+ )}
+ >
+ <ReactMarkdown remarkPlugins={[remarkGfm]}>{text}</ReactMarkdown>
+ </div>
+ );
+}
+
+/**
+ * Thinking with no words to show — the vendor redacted it (Claude Code
+ * does). The row still states the time, because the time is the fact, and
+ * stays shut rather than opening onto emptiness.
+ */
+function SilentThought({ block }: { block: ReasoningBlock }) {
+ const t = useT();
+ const duration = block.durationMs ?? 0;
+ const header =
+ duration > 0
+ ? fill(t("agent_chat.thought_for"), { duration: formatThoughtDuration(duration) })
+ : t("agent_chat.thought");
+
+ return (
+ <div data-testid="agent-reasoning" data-state="silent">
+ <button
+ type="button"
+ aria-expanded={false}
+ disabled
+ className="-ml-1 inline-flex cursor-default items-center gap-1.5 rounded-md px-1 py-0.5 text-xs text-muted-foreground"
+ >
+ <ChevronRight className="h-3.5 w-3.5 shrink-0 opacity-30" aria-hidden />
+ <span>{header}</span>
+ </button>
</div>
);
}