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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(chat): harden plan/question bodies against model-authored markdown
reviewc4124767e7d1
diff --git a/CLAUDE.md b/CLAUDE.mdindex 12d6d4026..872ccacc2 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -2443,8 +2443,9 @@ command-bearing opens; this does not add a human-confirm dialog or change mobile"Here is Claude's plan" — and an `AskUserQuestion` question + options unreadable in ⌘M, the oneplace meant for reading them. `core/chat-tool-body.ts` (pure) fills the tool part's optional`body` for exactly those two tools (`input.plan`; the questions rendered as markdown), capped at- 64 KB, degrading to no body (the old chip) on any other shape; ChatPanel shows a part with a- body as an expanded "Plan"/"Question" card through `MarkdownText` with its result under it, and+ 64K characters (never splitting an open code fence or a surrogate pair; model-authored labels are+ kept to one line with emphasis escaped), degrading to no body (the old chip) on any other shape;+ ChatPanel shows a part with a body as an expanded "Plan"/"Question" card through `MarkdownText` with its result under it, andthe find-bar index (`linesFrom`) indexes the body in full like assistant text.- **Subagent visualization** (agents in `SUBAGENT_CAPABLE`) — `subagent-start`/`subagent-end`normalized events (from Claude's `PreToolUse`/`PostToolUse` on tool `Agent`/`Task`, correlateddiff --git a/src/core/chat-tool-body.test.ts b/src/core/chat-tool-body.test.tsindex ba5710a06..5aae2d506 100644--- a/src/core/chat-tool-body.test.ts+++ b/src/core/chat-tool-body.test.ts@@ -60,7 +60,7 @@ describe('toolBody — AskUserQuestion', () => {)})- it('notes multiple choice and separates several questions', () => {+ it('notes a multi-select and separates several questions', () => {const input = {questions: [{ question: 'Pick features', header: 'Scope', multiSelect: true, options: [{ label: 'A' }, { label: 'B' }] },@@ -68,7 +68,7 @@ describe('toolBody — AskUserQuestion', () => {]}expect(toolBody('AskUserQuestion', input)).toBe(- '**Scope**\n\nPick features _(multiple choice)_\n\n- **A**\n- **B**\n\nShip today?\n\n- **Yes**'+ '**Scope**\n\nPick features _(select all that apply)_\n\n- **A**\n- **B**\n\nShip today?\n\n- **Yes**')})@@ -97,3 +97,42 @@ describe('toolBody — every other tool', () => {expect(toolBody('ToolSearch', { query: 'x' })).toBeUndefined()})})++describe('toolBody — hardening', () => {+ it('collapses newlines inside labels and descriptions so they cannot inject list items or headings', () => {+ const input = {+ questions: [+ {+ question: 'Pick one\n\nThe question keeps its own paragraphs.',+ options: [{ label: 'A\n# heading', description: 'first line\n- injected item\r\nmore' }]+ }+ ]+ }+ expect(toolBody('AskUserQuestion', input)).toBe(+ 'Pick one\n\nThe question keeps its own paragraphs.\n\n- **A # heading** — first line - injected item more'+ )+ })++ it('escapes emphasis characters inside the bold header and label', () => {+ const input = {+ questions: [{ header: 'use **all**_x', question: 'Q?', options: [{ label: 'snake_case *ptr', description: 'keeps *its* _style_' }] }]+ }+ expect(toolBody('AskUserQuestion', input)).toBe(+ '**use \\*\\*all\\*\\*\\_x**\n\nQ?\n\n- **snake\\_case \\*ptr** — keeps *its* _style_'+ )+ })++ it('closes an open code fence before the truncation marker', () => {+ const plan = 'intro\n```ts\n' + 'x'.repeat(TOOL_BODY_CAP) + '\n```\nafter'+ const body = toolBody('ExitPlanMode', { plan })!+ expect(body.endsWith('\n```' + TOOL_BODY_TRUNCATED)).toBe(true)+ const fences = body.split('\n').filter((l) => l.trimStart().startsWith('```')).length+ expect(fences % 2).toBe(0)+ })++ it('does not add a fence when the kept slice has balanced fences', () => {+ const plan = '```\ncode\n```\n' + 'y'.repeat(TOOL_BODY_CAP)+ const body = toolBody('ExitPlanMode', { plan })!+ expect(body.endsWith('y' + TOOL_BODY_TRUNCATED)).toBe(true)+ })+})diff --git a/src/core/chat-tool-body.ts b/src/core/chat-tool-body.tsindex 25cd6faf4..4f5ee2a97 100644--- a/src/core/chat-tool-body.ts+++ b/src/core/chat-tool-body.ts@@ -9,19 +9,34 @@// AskUserQuestion → {"questions": [{"question", "header", "multiSelect", "options": [{"label", "description"?}]}]}// Anything that does not match degrades to NO body (today's chip) — never a half-rendered guess.-/** The body crosses IPC/WS with every chat read; a plan is prose, and 64 KB is far past any real one. */+/** The body crosses IPC/WS with every chat read; a plan is prose, and 64K characters (UTF-16 code+ * units, i.e. `String.length`) is far past any real one. */export const TOOL_BODY_CAP = 64 * 1024export const TOOL_BODY_TRUNCATED = '\n\n… (truncated)'+const FENCE = /^\s*```/+function capBody(s: string): string {if (s.length <= TOOL_BODY_CAP) return slet end = TOOL_BODY_CAP// Do not leave half a surrogate pair (an emoji) dangling at the cut.const last = s.charCodeAt(end - 1)if (last >= 0xd800 && last <= 0xdbff) end -= 1- return s.slice(0, end) + TOOL_BODY_TRUNCATED+ let kept = s.slice(0, end)+ // A cut inside an open ``` block would render the marker as code (and swallow nothing after it+ // only by luck): an odd number of fence lines means one is open — close it first.+ const fences = kept.split('\n').filter((l) => FENCE.test(l)).length+ if (fences % 2 === 1) kept += '\n```'+ return kept + TOOL_BODY_TRUNCATED}+// Model-authored text dropped into a one-line markdown slot (a bold header, a list item): a newline+// there would start a new block (a heading, an injected list item), so it collapses to a space.+const oneLine = (s: string): string => s.replace(/\s*[\r\n]+\s*/g, ' ').trim()+// Inside the `**…**` wrapper, the model's own `*` / `_` / `\` would close or break the bold.+const escapeEmphasis = (s: string): string => s.replace(/[\\*_]/g, (c) => `\\${c}`)+const bold = (s: string): string => `**${escapeEmphasis(oneLine(s))}**`+const isObj = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v)const nonBlank = (v: unknown): v is string => typeof v === 'string' && v.trim() !== ''@@ -35,12 +50,13 @@ function questionsBody(input: Record<string, unknown>): string | undefined {for (const q of input.questions) {if (!isObj(q) || !nonBlank(q.question)) continueconst lines: string[] = []- if (nonBlank(q.header)) lines.push(`**${q.header}**`, '')- lines.push(q.multiSelect === true ? `${q.question} _(multiple choice)_` : q.question)+ if (nonBlank(q.header)) lines.push(bold(q.header), '')+ // The question keeps its own paragraphs: it is a block of its own, not a one-line slot.+ lines.push(q.multiSelect === true ? `${q.question} _(select all that apply)_` : q.question)const options = Array.isArray(q.options)? q.options.filter((o): o is Record<string, unknown> => isObj(o) && nonBlank(o.label))- .map((o) => `- **${o.label}**${nonBlank(o.description) ? ` — ${o.description}` : ''}`)+ .map((o) => `- ${bold(o.label as string)}${nonBlank(o.description) ? ` — ${oneLine(o.description)}` : ''}`): []if (options.length) lines.push('', ...options)blocks.push(lines.join('\n'))diff --git a/src/shared/types.ts b/src/shared/types.tsindex 377231c9f..fe24b360a 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -2777,7 +2777,7 @@ export type ChatPart =summary?: ChatToolSummary/** Markdown worth reading in full (an `ExitPlanMode` plan, an `AskUserQuestion` question and* its options — `core/chat-tool-body.ts`). Present = the renderer shows an expanded card- * instead of the collapsed chip. Capped at 64 KB. */+ * instead of the collapsed chip. Capped at 64K characters. */body?: string/** The transcript's `tool_use` id. Set only by a PAGED read (`ChatTranscriptResult.olderCursor`* present): a result carried across a page boundary (`unmatchedResults`) is attached by it. */