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.

fix(chat): cap an Edit/Write diff side as plain text, without fence repair

review4ab59e17d442
diff --git a/src/core/chat-tool-body.test.ts b/src/core/chat-tool-body.test.ts
index 9002a570f..79ee90133 100644
--- a/src/core/chat-tool-body.test.ts
+++ b/src/core/chat-tool-body.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { TOOL_BODY_CAP, TOOL_BODY_TRUNCATED, toolBody } from './chat-tool-body'
+import { TOOL_BODY_CAP, TOOL_BODY_TRUNCATED, capText, toolBody } from './chat-tool-body'
// Input shapes measured on this server's transcripts (2026-09-26):
// ExitPlanMode → {"plan": "<full markdown plan>"}
@@ -40,6 +40,25 @@ describe('toolBody — ExitPlanMode', () => {
})
})
+// A side of an Edit/Write diff is file content, not markdown: the cap keeps it as the file had it.
+describe('capText', () => {
+ it('leaves a text at the cap untouched', () => {
+ const s = 'z'.repeat(TOOL_BODY_CAP)
+ expect(capText(s)).toBe(s)
+ })
+
+ it('cuts an over-cap text and appends the marker — no fence a file never had', () => {
+ // A balanced markdown file whose cut lands inside a code block: `capBody` would close the fence.
+ const s = '# Doc\n```ts\n' + 'x'.repeat(TOOL_BODY_CAP) + '\n```\n'
+ expect(capText(s)).toBe(s.slice(0, TOOL_BODY_CAP) + TOOL_BODY_TRUNCATED)
+ })
+
+ it('never splits a surrogate pair at the cap', () => {
+ const s = 'a'.repeat(TOOL_BODY_CAP - 1) + '😀' + 'tail'
+ expect(capText(s)).toBe('a'.repeat(TOOL_BODY_CAP - 1) + TOOL_BODY_TRUNCATED)
+ })
+})
+
describe('toolBody — AskUserQuestion', () => {
it('renders header, question and options with descriptions', () => {
const input = {
diff --git a/src/core/chat-tool-body.ts b/src/core/chat-tool-body.ts
index 1870b3485..e7bd79d84 100644
--- a/src/core/chat-tool-body.ts
+++ b/src/core/chat-tool-body.ts
@@ -23,15 +23,28 @@ export const TOOL_BODY_TRUNCATED = '\n\n… (truncated)'
const FENCE = /^\s*```/
-/** Caps a long text at `TOOL_BODY_CAP`, then appends `TOOL_BODY_TRUNCATED` (a tool body, and each
- * side of an Edit/Write diff in the chat parse). */
-export function capBody(s: string): string {
- if (s.length <= TOOL_BODY_CAP) return s
+/** The first `TOOL_BODY_CAP` code units of `s`, never leaving half a surrogate pair (an emoji)
+ * dangling at the cut. */
+function cutAtCap(s: string): string {
let 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
- let kept = s.slice(0, end)
+ return s.slice(0, end)
+}
+
+/** Caps a long text at `TOOL_BODY_CAP`, then appends `TOOL_BODY_TRUNCATED` — and nothing else: for
+ * text that is NOT rendered as markdown, each side of an Edit/Write diff in the chat parse. A diff
+ * side is file content, so `capBody`'s fence repair would add a line the file never had (a balanced
+ * markdown file cut inside a code block showed `+```` before the marker). */
+export function capText(s: string): string {
+ return s.length <= TOOL_BODY_CAP ? s : cutAtCap(s) + TOOL_BODY_TRUNCATED
+}
+
+/** Caps a long markdown tool body at `TOOL_BODY_CAP`, then appends `TOOL_BODY_TRUNCATED` — closing
+ * a code fence the cut left open first. For a text that is not markdown, `capText`. */
+export function capBody(s: string): string {
+ if (s.length <= TOOL_BODY_CAP) return s
+ let kept = cutAtCap(s)
// 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
diff --git a/src/core/transcript-reader.test.ts b/src/core/transcript-reader.test.ts
index ffe0af4af..c96d7b381 100644
--- a/src/core/transcript-reader.test.ts
+++ b/src/core/transcript-reader.test.ts
@@ -581,6 +581,19 @@ describe('Edit / Write diffs (opt-in `rich`)', () => {
])
})
+ it('rich: a capped side is the file as written — no fence repair for a cut inside a code block', () => {
+ // A balanced markdown Write whose cut lands inside its ```ts block. A tool BODY closes the fence
+ // there (it is rendered as markdown); a diff side is file content, so a line the file never had
+ // would show as `+```` in the diff.
+ const content = '# Doc\n```ts\n' + 'x'.repeat(TOOL_BODY_CAP) + '\n```\n'
+ const [part] = toolPart(call('w1', 'Write', { file_path: '/d.md', content }), { rich: true })
+ expect(part.kind === 'tool' ? part.diff : undefined).toEqual({
+ filePath: '/d.md',
+ oldText: '',
+ newText: content.slice(0, TOOL_BODY_CAP) + TOOL_BODY_TRUNCATED
+ })
+ })
+
it('without rich an Edit is the same chip as before', () => {
const raw =
call('e1', 'Edit', { file_path: '/a.ts', old_string: 'a', new_string: 'b' }) +
diff --git a/src/core/transcript-reader.ts b/src/core/transcript-reader.ts
index 857d90295..acd0087b5 100644
--- a/src/core/transcript-reader.ts
+++ b/src/core/transcript-reader.ts
@@ -8,7 +8,7 @@ import type { TranscriptLine, ChatMessage, ChatPart, ChatCarriedToolResult, Chat
import { transcriptRootFor } from './claude-accounts-core'
import { linkedClaudeConfigDirFor } from './claude-config-dir'
import { platform } from './platform'
-import { capBody, toolBody } from './chat-tool-body'
+import { capText, toolBody } from './chat-tool-body'
import { isRecord } from '../shared/is-record'
import { ASK_USER_QUESTION_TOOL, readQuestions } from '../shared/agents/permission-answer'
import { BASH_COMMAND_TOOL, CHAT_TOOL_ARG_MAX, COMMAND_OUTPUT_TOOL } from '../shared/chat-command'
@@ -404,7 +404,8 @@ function imagePart(source: unknown): Extract<ChatPart, { kind: 'image' }> | null
* The change an `Edit` / `Write` call asked for (`ChatParseOptions.rich`), from its INPUT — measured
* (CLI 2.1.314) as `Edit → {file_path, old_string, new_string, replace_all?}` and
* `Write → {file_path, content}`. Undefined for any other tool or shape: the plain chip, as before.
- * An empty `new_string` is a deletion and still a diff. Each text is capped like a tool body.
+ * An empty `new_string` is a deletion and still a diff. Each text is capped at the tool-body length
+ * (`capText`) with no markdown repair: it is file content, and is shown as it was written.
*/
function toolDiff(name: string, input: unknown): ChatToolDiff | undefined {
if (!isRecord(input)) return undefined
@@ -413,10 +414,10 @@ function toolDiff(name: string, input: unknown): ChatToolDiff | undefined {
if (name === 'Edit') {
const { old_string: oldText, new_string: newText } = input
if (typeof oldText !== 'string' || typeof newText !== 'string') return undefined
- return { filePath, oldText: capBody(oldText), newText: capBody(newText) }
+ return { filePath, oldText: capText(oldText), newText: capText(newText) }
}
if (name === 'Write') {
- return typeof input.content === 'string' ? { filePath, oldText: '', newText: capBody(input.content) } : undefined
+ return typeof input.content === 'string' ? { filePath, oldText: '', newText: capText(input.content) } : undefined
}
return undefined
}
diff --git a/src/renderer/lib/chatDiff.ts b/src/renderer/lib/chatDiff.ts
index 7c6882f43..bc011b883 100644
--- a/src/renderer/lib/chatDiff.ts
+++ b/src/renderer/lib/chatDiff.ts
@@ -6,7 +6,7 @@ export type DiffLine = { kind: 'ctx' | 'add' | 'del'; text: string }
/**
* The largest old × new line product diffed properly. The table is O(n·m) in time and memory, and
- * each side can be a 64K-character text (`capBody`), so past this an edit is shown as every old line
+ * each side can be a 64K-character text (`capText`), so past this an edit is shown as every old line
* removed, then every new line added — still the whole change, just without the pairing.
*/
export const LINE_DIFF_MAX_CELLS = 250_000
diff --git a/src/shared/chat-fixtures/README.md b/src/shared/chat-fixtures/README.md
index 2b11564f5..9b3689624 100644
--- a/src/shared/chat-fixtures/README.md
+++ b/src/shared/chat-fixtures/README.md
@@ -103,8 +103,11 @@ nothing above changes. What it adds:
- A `diff: {filePath, oldText, newText}` on an `Edit` tool part (`{file_path, old_string,
new_string}`) or a `Write` one (`{file_path, content}`, `oldText: ""`), read from the call's input
when `file_path` is a non-empty string and the texts are strings (an empty `new_string` is a
- deletion and still a diff). Each text is capped like a tool `body` (65536 UTF-16 units, then
- `TOOL_BODY_TRUNCATED`). Any other shape yields no `diff`; `arg` and `result` are unchanged.
+ deletion and still a diff). Each text over 65536 UTF-16 units is cut there (one unit earlier when
+ the cut would split a surrogate pair) and `TOOL_BODY_TRUNCATED` appended — and, unlike a tool
+ `body`, no closing fence line is added for a code block the cut left open: the text is file
+ content, not markdown (`capText`). Any other shape yields no `diff`; `arg` and `result` are
+ unchanged.
- A `{type:"system", subtype:"compact_boundary", compactMetadata}` record becomes ONE assistant
message with ONE tool part `{name:"Compaction", arg, result?}`: `arg` is `compactMetadata.trigger`
(`""` when not a string; its first 200 UTF-16 units, `CHAT_TOOL_ARG_MAX`), and `result` is
diff --git a/src/shared/chat-page.ts b/src/shared/chat-page.ts
index ab990a40a..20ab3ffa5 100644
--- a/src/shared/chat-page.ts
+++ b/src/shared/chat-page.ts
@@ -27,8 +27,10 @@ export interface ChatTranscriptPageRequest {
* `chat.page`, and the Swift ports locked to the shared fixtures — gets the same parts as before.
* Only claude's reader honours it. */
thinking?: boolean
- /** Also emit the parts only the desktop ⌘M panel draws: an `image` part for each image the user
- * attached to a prompt. Opt-in for the same reason as `thinking` (the phone and the Swift ports
+ /** Also emit the parts only the desktop ⌘M panel draws (`ChatParseOptions.rich`): an `image` part
+ * for each image the user attached to a prompt, a `diff` on each `Edit` / `Write` call, a
+ * `Compaction` chip in place of a context compaction's summary record, and a checklist `body` on
+ * each `TodoWrite` call. Opt-in for the same reason as `thinking` (the phone and the Swift ports
* locked to the shared fixtures never set it). Only claude's reader honours it. */
rich?: boolean
}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 27cff76ce..226d298a5 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -3646,8 +3646,8 @@ export type ChatPart =
* The change an `Edit` / `Write` tool call asked for (`ChatPart` tool `diff`). Edit's
* `{file_path, old_string, new_string}` maps to `{filePath, oldText, newText}`; Write's
* `{file_path, content}` to `{filePath, oldText: '', newText: content}`. An empty `newText` on an
- * Edit is a deletion. Each text is capped like a tool body (`capBody`, core/chat-tool-body.ts: 64K
- * characters, then `TOOL_BODY_TRUNCATED`).
+ * Edit is a deletion. Each text is capped at a tool body's length (`capText`, core/chat-tool-body.ts:
+ * 64K characters, then `TOOL_BODY_TRUNCATED`, with no markdown fence repair — it is file content).
*/
export interface ChatToolDiff {
filePath: string