Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(chat): fold thinking with its duration; cap the opt-in thinking text

review55eb7a27534a
diff --git a/src/core/transcript-reader.test.ts b/src/core/transcript-reader.test.ts
index 7f4a2875a..0aa773ac0 100644
--- a/src/core/transcript-reader.test.ts
+++ b/src/core/transcript-reader.test.ts
@@ -112,7 +112,7 @@ describe('readSessionName — remote (SSH project) sessions', () => {
// ─────────────────────────────────────────────────────────────────────────────────────────────
import os from 'os'
import path from 'path'
-import { parseChatMessages, parseChatWindow, readChatWindow } from './transcript-reader'
+import { parseChatMessages, parseChatWindow, readChatWindow, SYSTEM_RESULT_MAX } from './transcript-reader'
const jl = (o: object): string => JSON.stringify(o) + '\n'
const said = (role: 'user' | 'assistant', text: string): string =>
@@ -363,6 +363,23 @@ describe('message time (`at`, from the line\'s ISO `timestamp`)', () => {
})
})
+describe('thinking parts (opt-in `thinking`)', () => {
+ const assistantThinkingLine = (thinking: string): string =>
+ jl({ type: 'assistant', message: { content: [{ type: 'thinking', thinking, signature: 'sig' }] } })
+
+ it('caps a thinking text at SYSTEM_RESULT_MAX, like a system record\'s body', () => {
+ const msgs = parseChatMessages([assistantThinkingLine('x'.repeat(SYSTEM_RESULT_MAX + 5))], { thinking: true })
+ expect(msgs).toHaveLength(1)
+ expect(msgs[0].parts).toHaveLength(1)
+ expect(msgs[0].parts[0].kind).toBe('thinking')
+ expect((msgs[0].parts[0] as { text: string }).text).toHaveLength(SYSTEM_RESULT_MAX)
+ })
+
+ it('without the option a thinking-only line is still no message (unchanged)', () => {
+ expect(parseChatMessages([assistantThinkingLine('x'.repeat(SYSTEM_RESULT_MAX + 5))])).toEqual([])
+ })
+})
+
describe('readChatWindow — byte windows read from a real file', () => {
let dir: string
afterEach(() => {
diff --git a/src/core/transcript-reader.ts b/src/core/transcript-reader.ts
index 6b9736c7a..513ba7fc2 100644
--- a/src/core/transcript-reader.ts
+++ b/src/core/transcript-reader.ts
@@ -575,7 +575,8 @@ function parseChatRecords(
if (c.type === 'text' && c.text) parts.push({ kind: 'text', text: c.text })
else if (c.type === 'thinking') {
if (opts?.thinking && typeof c.thinking === 'string' && c.thinking.trim()) {
- parts.push({ kind: 'thinking', text: c.thinking })
+ // Capped like a system record's body: one runaway block must not size the whole page.
+ parts.push({ kind: 'thinking', text: c.thinking.slice(0, SYSTEM_RESULT_MAX) })
}
}
else if (c.type === 'tool_use') {
diff --git a/src/renderer/lib/chatThinking.test.ts b/src/renderer/lib/chatThinking.test.ts
new file mode 100644
index 000000000..cd63e3ab1
--- /dev/null
+++ b/src/renderer/lib/chatThinking.test.ts
@@ -0,0 +1,60 @@
+import { describe, expect, it } from 'vitest'
+import type { ChatMessage } from '@shared/types'
+import { thinkingDuration, thinkingLabel } from './chatThinking'
+
+const think = (at?: number): ChatMessage => ({ role: 'assistant', parts: [{ kind: 'thinking', text: 'hmm' }], ...(at === undefined ? {} : { at }) })
+const say = (at?: number): ChatMessage => ({ role: 'assistant', parts: [{ kind: 'text', text: 'ok' }], ...(at === undefined ? {} : { at }) })
+
+describe('thinkingDuration', () => {
+ it('duration is the gap to the next stamped message', () => {
+ expect(thinkingDuration([think(1000), say(13_400)], 0)).toBe(12_400)
+ })
+
+ it('a zero gap is a duration, not "unknown"', () => {
+ expect(thinkingDuration([think(5000), say(5000)], 0)).toBe(0)
+ })
+
+ it('no duration for the last message, an unstamped neighbour, a negative gap, or a mixed message', () => {
+ // The last message has no next one to measure against.
+ expect(thinkingDuration([say(0), think(1000)], 1)).toBeUndefined()
+ // Either stamp missing.
+ expect(thinkingDuration([think(), say(13_400)], 0)).toBeUndefined()
+ expect(thinkingDuration([think(1000), say()], 0)).toBeUndefined()
+ // The next line is stamped EARLIER (another machine's clock): never a negative duration.
+ expect(thinkingDuration([think(13_400), say(1000)], 0)).toBeUndefined()
+ // A message that also holds text or a tool did not spend the gap thinking alone.
+ const mixed: ChatMessage = { role: 'assistant', parts: [{ kind: 'thinking', text: 'hmm' }, { kind: 'text', text: 'ok' }], at: 1000 }
+ expect(thinkingDuration([mixed, say(13_400)], 0)).toBeUndefined()
+ const withTool: ChatMessage = {
+ role: 'assistant',
+ parts: [{ kind: 'thinking', text: 'hmm' }, { kind: 'tool', name: 'Bash', arg: 'ls' }],
+ at: 1000
+ }
+ expect(thinkingDuration([withTool, say(13_400)], 0)).toBeUndefined()
+ })
+
+ it('no duration for a message that is not thinking at all, or an index out of range', () => {
+ expect(thinkingDuration([say(1000), say(13_400)], 0)).toBeUndefined()
+ expect(thinkingDuration([{ role: 'assistant', parts: [], at: 1000 }, say(13_400)], 0)).toBeUndefined()
+ expect(thinkingDuration([think(1000)], 5)).toBeUndefined()
+ expect(thinkingDuration([], 0)).toBeUndefined()
+ })
+})
+
+describe('thinkingLabel', () => {
+ it.each([
+ [undefined, 'Thinking'],
+ [0, 'Thought for 1s'],
+ [400, 'Thought for 1s'],
+ [12_400, 'Thought for 12s'],
+ [12_600, 'Thought for 13s'],
+ [59_400, 'Thought for 59s'],
+ // Rounded first, so 59.6 s is a minute, never "60s".
+ [59_600, 'Thought for 1m 0s'],
+ [60_000, 'Thought for 1m 0s'],
+ [95_000, 'Thought for 1m 35s'],
+ [3_725_000, 'Thought for 62m 5s']
+ ])('%s ms reads %j', (ms, label) => {
+ expect(thinkingLabel(ms)).toBe(label)
+ })
+})
diff --git a/src/renderer/lib/chatThinking.ts b/src/renderer/lib/chatThinking.ts
new file mode 100644
index 000000000..815fa610c
--- /dev/null
+++ b/src/renderer/lib/chatThinking.ts
@@ -0,0 +1,28 @@
+// Pure decisions behind the ⌘M thread's thinking fold (ChatPanel): how long a thinking block took,
+// and what its summary line says. No React — see ChatPanel.tsx for the glue and chatThinking.test.ts
+// for the pins.
+import type { ChatMessage } from '@shared/types'
+
+/**
+ * How long message `i` spent thinking, in ms: the gap from its `at` to the NEXT message's `at`.
+ *
+ * Only when message `i` holds ONLY thinking parts — a line that also carries text or a tool call did
+ * not spend that gap thinking — and both lines are stamped. A negative gap (the next line written by
+ * a clock behind this one) is no duration either: undefined, never a made-up or negative time.
+ */
+export function thinkingDuration(messages: readonly ChatMessage[], i: number): number | undefined {
+ const m = messages[i]
+ const next = messages[i + 1]
+ if (!m || !next || m.at === undefined || next.at === undefined) return undefined
+ if (m.parts.length === 0 || !m.parts.every((p) => p.kind === 'thinking')) return undefined
+ const gap = next.at - m.at
+ return gap >= 0 ? gap : undefined
+}
+
+/** The fold's summary: "Thinking" with no duration, else "Thought for 12s" / "Thought for 1m 35s". */
+export function thinkingLabel(ms: number | undefined): string {
+ if (ms === undefined) return 'Thinking'
+ // Rounded to whole seconds BEFORE choosing the unit, so 59.6 s reads "1m 0s", never "60s".
+ const s = Math.max(1, Math.round(ms / 1000))
+ return s < 60 ? `Thought for ${s}s` : `Thought for ${Math.floor(s / 60)}m ${s % 60}s`
+}
diff --git a/src/renderer/nodes/ChatPanel.thread.test.tsx b/src/renderer/nodes/ChatPanel.thread.test.tsx
index d241c6d14..75a75a411 100644
--- a/src/renderer/nodes/ChatPanel.thread.test.tsx
+++ b/src/renderer/nodes/ChatPanel.thread.test.tsx
@@ -207,6 +207,28 @@ describe('⌘M thread look', () => {
expect(days[1].nextElementSibling?.classList.contains('term-chat__msg--folded')).toBe(true)
})
+ it('a thinking part is a closed fold labelled with its duration', async () => {
+ const u: ChatMessage = { role: 'user', parts: [{ kind: 'text', text: 'q' }], key: 0, at: now - 30_000 }
+ await mount([
+ u,
+ { role: 'assistant', parts: [{ kind: 'thinking', text: 'hmm **deep**' }], key: 10, at: now - 20_000 },
+ { role: 'assistant', parts: [{ kind: 'text', text: 'ok' }], key: 20, at: now - 8_000 }
+ ])
+ const d = host.querySelector<HTMLDetailsElement>('details.term-chat__thinking')!
+ expect(d.open).toBe(false)
+ expect(d.querySelector('summary')?.textContent).toBe('Thought for 12s')
+ expect(d.querySelector('.term-chat__text strong')?.textContent).toBe('deep')
+ })
+
+ it('a thinking fold with no measurable duration just says Thinking', async () => {
+ await mount([
+ { role: 'user', parts: [{ kind: 'text', text: 'q' }], key: 0 },
+ { role: 'assistant', parts: [{ kind: 'thinking', text: 'hmm' }], key: 10 },
+ { role: 'assistant', parts: [{ kind: 'text', text: 'ok' }], key: 20 }
+ ])
+ expect(host.querySelector('details.term-chat__thinking > summary')?.textContent).toBe('Thinking')
+ })
+
it('a day that starts inside a folded tool run keeps its divider (drawn where the run ends)', async () => {
const midnight = new Date(now).setHours(0, 0, 0, 0)
await mount([
diff --git a/src/renderer/nodes/ChatPanel.tsx b/src/renderer/nodes/ChatPanel.tsx
index 578c65ffa..1bd312c5b 100644
--- a/src/renderer/nodes/ChatPanel.tsx
+++ b/src/renderer/nodes/ChatPanel.tsx
@@ -58,6 +58,7 @@ import { useCodeBlockCopy } from './CodeBlockCopy'
import { useCodeHighlight } from './CodeHighlight'
import { assistantTurnEnds, chatAbsoluteTime, chatClockTime, dayDividers } from '../lib/chatThread'
import { groupToolCalls, partPosition } from '../lib/chatToolGroups'
+import { thinkingDuration, thinkingLabel } from '../lib/chatThinking'
import { ChatToolGroup } from './ChatToolGroup'
import { FALLBACK_SESSION_NOTE, transcriptReadCwd } from '../lib/transcriptSession'
import { chatPageDirection, pageChatThread } from '../lib/chatPageKeys'
@@ -997,11 +998,14 @@ export function ChatPanel({
p.kind === 'text' ? (
<MarkdownText key={key} text={p.text} />
) : p.kind === 'thinking' ? (
- // Claude's thinking summary — the "thinking out loud" line its TUI prints between
- // tool calls. Shown inline like the TUI does, dimmed so it never reads as the answer.
- <div key={key} className="term-chat__thinking">
+ // Claude's thinking summary — the "thinking out loud" line its TUI prints between tool calls.
+ // A fold, closed by default and dimmed, so it never reads as the answer; its line says how long
+ // the thinking took when the stamps can tell (lib/chatThinking.ts). Native and uncontrolled,
+ // like the tool block: it keeps whatever the user opened while the thread re-renders.
+ <details key={key} className="term-chat__thinking">
+ <summary>{thinkingLabel(thinkingDuration(messages, i))}</summary>
<MarkdownText text={p.text} />
- </div>
+ </details>
) : p.body ? (
// A plan / question is the content itself, not plumbing: shown expanded, in full,
// flowing with the thread (no inner scroll box). The answer stays under it.
diff --git a/src/renderer/nodes/ChatPanel.ux.test.tsx b/src/renderer/nodes/ChatPanel.ux.test.tsx
index 62cfbd476..96c33d48e 100644
--- a/src/renderer/nodes/ChatPanel.ux.test.tsx
+++ b/src/renderer/nodes/ChatPanel.ux.test.tsx
@@ -185,14 +185,14 @@ describe('ChatPanel rendering', () => {
expect(card.querySelector('.term-chat__tool-result')).toBeNull()
})
- it('renders a thinking summary inline and dimmed, apart from the answer text', async () => {
+ it('renders a thinking summary in a closed fold, apart from the answer text', async () => {
await render()
await resolveAt(0, [{ role: 'assistant', parts: [{ kind: 'thinking', text: 'pondering' }, { kind: 'text', text: 'answer' }] }])
- const thinking = host.querySelector('.term-chat__thinking')
+ const thinking = host.querySelector<HTMLDetailsElement>('details.term-chat__thinking')
+ expect(thinking?.open).toBe(false)
expect(thinking?.textContent).toContain('pondering')
- expect(host.querySelector('details.term-chat__thinking')).toBeNull()
// The answer stays ordinary text, outside the thinking block.
const answers = [...host.querySelectorAll('.term-chat__msg > .term-chat__text')].map((e) => e.textContent?.trim())
expect(answers).toEqual(['answer'])
diff --git a/src/renderer/styles.chat-view.test.ts b/src/renderer/styles.chat-view.test.ts
index 95c190f29..501e80d9f 100644
--- a/src/renderer/styles.chat-view.test.ts
+++ b/src/renderer/styles.chat-view.test.ts
@@ -87,6 +87,16 @@ describe('⌘M thread: claude.ai look', () => {
expect(ruleBody('.term-chat__day')).toMatch(/text-align:\s*center;/)
expect(ruleBody('.term-chat__time--user')).toMatch(/font-size:\s*11px;/)
})
+
+ it('a thinking fold is a dimmed, clickable summary line with a drawn chevron, not the native marker', () => {
+ const summary = ruleBody('.term-chat__thinking > summary')
+ expect(summary).toMatch(/color:\s*var\(--muted\);/)
+ expect(summary).toMatch(/cursor:\s*pointer;/)
+ expect(summary).toMatch(/list-style:\s*none;/)
+ expect(ruleBody('.term-chat__thinking > summary::-webkit-details-marker')).toMatch(/display:\s*none;/)
+ expect(ruleBody('.term-chat__thinking > summary::before')).toMatch(/transform:\s*rotate\(-45deg\);/)
+ expect(ruleBody('.term-chat__thinking[open] > summary::before')).toMatch(/transform:\s*rotate\(45deg\);/)
+ })
})
describe('the shared .term-chat__input (plan "Revise…", question "Other") keeps a visible field', () => {
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index b05851ab7..46fc9585d 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -4167,12 +4167,43 @@ body,
.term-chat__refresh:hover {
background: rgba(var(--tint-rgb), 0.1);
}
-/* Claude's thinking summary between tool calls: inline like the TUI, dimmed so it never reads as
- the answer. */
+/* Claude's thinking summary between tool calls (a <details>, closed by default): a dimmed line —
+ chevron + "Thought for 12s" — so it never reads as the answer; open, the text stays dimmed too. */
.term-chat__thinking {
margin: 4px 0;
color: var(--muted);
}
+.term-chat__thinking > summary {
+ display: flex;
+ gap: 6px;
+ align-items: baseline;
+ font-size: 12px;
+ color: var(--muted);
+ cursor: pointer;
+ list-style: none;
+}
+.term-chat__thinking > summary::-webkit-details-marker {
+ display: none;
+}
+/* Drawn like the tool block's chevron: '▸' at this size renders as a dot. */
+.term-chat__thinking > summary::before {
+ content: '';
+ flex: 0 0 auto;
+ align-self: center;
+ width: 5px;
+ height: 5px;
+ margin: 0 2px 0 1px;
+ border-right: 1.5px solid var(--muted);
+ border-bottom: 1.5px solid var(--muted);
+ transform: rotate(-45deg);
+ transition: transform 0.12s ease;
+}
+.term-chat__thinking[open] > summary::before {
+ transform: rotate(45deg);
+}
+.term-chat__thinking > .term-chat__text {
+ margin-top: 4px;
+}
/* Transparent overlay that lets you pan across a terminal until you dwell/click. */
.term-hover-guard {