@hncode/hncode 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,235 +1,242 @@
1
- // A tool result row: the `↳` block under the tool line.
2
- //
3
- // The shape is the same for every tool, and the parts differ:
4
- //
5
- // ● Used Edit (a.js) +1 -1 <- the tool line, counts only
6
- // ↳ 179 - old line <- ONE `↳` on the FIRST body row
7
- // 179 + new line
8
- // Edited a.js: replaced 1 occurrence(s). <- receipt LAST
9
- //
10
- // An Edit's DIFF is the body and carries the marker, with the receipt moved to the bottom; every other tool's output
11
- // IS the body and carries the marker on its first line.
12
-
13
- import { C } from './colors.js';
14
- import { col, expandTabs, fitAnsi, sanitizeText, visualCol, wrapWords } from './text.js';
15
- import { uiText } from './i18n.js';
16
- import { langForPath } from './highlight.js';
17
- import { paintCode } from './diff.js';
18
-
19
- export function toolResultRows(msg, width, expanded) {
20
- // The marker sits in the SAME COLUMN as the tool bullet `●` (columns 0-1), so `↳ ` starts at column 0 and the output
21
- // text is indented to the tool text column. ONE `↳` per result, however many lines it has.
22
- const pre = '↳ ';
23
- const cont = ' ';
24
- const bodyW = Math.max(1, width - visualCol(cont));
25
- // Strip escapes BEFORE splitting: a file or a command can emit raw cursor codes, and letting them through made the
26
- // terminal OBEY them — recolouring the transcript, resetting styles mid-row, clearing the screen over the composer.
27
- const rawLines = sanitizeText(msg.text).replace(/\r\n/g, '\n').split('\n');
28
- // A TAB counts as ZERO columns in our width maths but advances to the next tab stop in a real terminal. A row
29
- // containing one was therefore padded short and the terminal kept the old frame's glyphs in the gap — the "residue"
30
- // under tool output. Read rows are the exception: their first tab is the line-number separator and is handled in the
31
- // Read branch below, where the CONTENT half is expanded too.
32
- if (!msg.readPath) for (let k = 0; k < rawLines.length; k++) rawLines[k] = expandTabs(rawLines[k]);
33
- // Trim blank lines at BOTH ends: commands typically emit a trailing newline, and some tools emit a leading one. A
34
- // stray blank line would render as a lone "↳ " with no content.
35
- while (rawLines.length && rawLines[0].trim() === '') rawLines.shift();
36
- while (rawLines.length && rawLines[rawLines.length - 1].trim() === '') rawLines.pop();
37
- // COLLAPSED IS A GLANCE; EXPANDED IS A READ, NOT A DUMP.
38
- //
39
- // Expanded used to be `Infinity`, so Ctrl+O on a command that printed 3000 lines put all 3000 into the transcript: the
40
- // conversation above it scrolled away, the scrollbar became meaningless, and every frame afterwards paid to lay out
41
- // rows nobody would read. The cap here is what a person can actually scan; past that the output belongs in a file,
42
- // which is what the hint says.
43
- const MAX_RESULT_LINES = expanded ? 400 : 12;
44
- // The exit-code line is bookkeeping for the AGENT and is NEVER shown to the user — success or failure. A failure is
45
- // already legible from the RED result text below. `[error: …]` is kept: it carries the message, not just a code.
46
- const visible = rawLines.filter((ln) => !/^\[exit code:/.test(ln.trim()));
47
- const failed = msg.failed === true;
48
- // A silent Bash keeps the ONE empty output row it had while running. Only Bash has this area: an empty Read/Grep
49
- // result must not grow a phantom line merely because the command itself printed nothing.
50
- const shown = visible.length ? visible.slice(0, MAX_RESULT_LINES) : (msg.toolName === 'Bash' ? [''] : []);
51
-
52
- const diffRows = [];
53
- const receiptRows = [];
54
- const hasDiff = Array.isArray(msg.diff); // empty array => receipt-only (Write)
55
-
56
- if (hasDiff) {
57
- // Same collapse rule as elsewhere: a short preview by default, everything once Ctrl+O expands. 16 rows (not 12)
58
- // because a change renders as a PAIR: `- old` then `+ new`.
59
- const MAX = expanded ? 400 : 16;
60
- // Show the unified diff WITH context rows (ctx = dim) so a change is not two orphaned blocks; `gap` rows mark where
61
- // unchanged lines were elided.
62
- const total = msg.diff.length;
63
- const wNo = Math.max(1, ...msg.diff.map((d) => String(d.no || 0).length));
64
- // THE COMMENT STATE TRAVELS BETWEEN THE ROWS of one diff, so a block comment that opens on a context row and closes
65
- // on a changed one stays grey across both.
66
- let inComment = false;
67
- let emitted = 0;
68
- for (const d of msg.diff) {
69
- if (emitted >= MAX) break;
70
- if (d.type === 'gap') {
71
- diffRows.push({ text: col(' ⋮', C.gray), ind: cont, raw: true });
72
- emitted++;
73
- continue;
74
- }
75
- const no = String(d.no || 0).padStart(wNo);
76
- const mark = d.type === 'add' ? '+' : d.type === 'del' ? '-' : ' ';
77
- // TABS ARE EXPANDED FIRST: `visualCol` counts one as zero columns, so an indented row measured short and its
78
- // background stopped before the right edge — the strip of page colour seen on Go diffs.
79
- const text = expandTabs(sanitizeText(d.text));
80
-
81
- // The sign, line number and content share the line's addition/deletion background; syntax highlighting changes
82
- // only the foreground. The wash runs to the right edge so adjacent changed rows read as one block.
83
- const isAdd = d.type === 'add';
84
- const isDel = d.type === 'del';
85
- const lineBg = isAdd ? C.diffAddBg : isDel ? C.diffDelBg : '';
86
- const fg = isAdd ? C.diffAddFg : isDel ? C.diffDelFg : C.white;
87
- const lead = col(`${no} ${mark} `, lineBg ? fg + lineBg : fg);
88
-
89
- if (!lineBg) {
90
- // CONTEXT LINES ARE CODE TOO. They are most of a diff and the part a reader scans to place the change, so they
91
- // get the same highlighter, on the CODE SURFACE rather than the raw page — a changed block sits on one recessed
92
- // field. The +/- rows around it keep their green/red wash.
93
- const ctxPainted = paintCode(text, d.lang, '', inComment);
94
- inComment = ctxPainted.inComment;
95
- const ctxRow = (lead + ctxPainted.text).replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
96
- diffRows.push({ text: C.codeBg + fitAnsi(ctxRow, bodyW) + C.bgReset, ind: cont, raw: true });
97
- emitted++;
98
- continue;
99
- }
100
-
101
- // Keep only the line-level wash and syntax foreground. Positional word pairing can highlight the wrong token when
102
- // lines are reordered or punctuation changes, so no word tint is applied.
103
- const painted = paintCode(text, d.lang, lineBg, inComment);
104
- inComment = painted.inComment;
105
- const used = visualCol(`${no} ${mark} `) + visualCol(painted.text);
106
- const pad = ' '.repeat(Math.max(0, bodyW - used));
107
- // The padding is INSIDE the wash, so it must not end with a bare reset: `col()` closes with ESC[0m, which would
108
- // drop the background on the last column and leave a strip of page colour at the right edge.
109
- diffRows.push({ text: lead + painted.text + lineBg + pad + '\x1b[39m', ind: cont, raw: true });
110
- emitted++;
111
- }
112
- if (total > emitted) {
113
- diffRows.push({
114
- // A DEAD-END Ctrl+O IS WORSE THAN NO HINT. Collapsed it expands; already expanded there is nothing left to
115
- // reveal, so the line says how much was withheld instead of inviting a key that would do nothing.
116
- text: col(expanded
117
- ? uiText(`… (${total - emitted} more lines not shown; press ctrl+e for the whole thing)`)
118
- : uiText(`… (${total - emitted} more lines, ctrl+o to expand)`), C.gray),
119
- ind: cont, raw: true,
120
- });
121
- }
122
- if (total === 0) diffRows.push({ text: col('(no changes)', C.gray), ind: cont, raw: true });
123
- // The receipt: one line per source line of the tool's own message.
124
- for (const ln of shown) {
125
- const color = failed ? C.red : C.gray;
126
- for (const t of (ln === '' ? [''] : wrapWords(ln, bodyW))) {
127
- receiptRows.push({ text: t, ind: cont, color });
128
- }
129
- }
130
- } else {
131
- // No diff: the tool's own output is the body.
132
- //
133
- // A READ RESULT IS SOURCE CODE, so its numbered lines are syntax-coloured — the file's own language, carried on the
134
- // message by the tool_result handler, which is the last place that still knows the path. The header and any error
135
- // keep the plain grey: those are the tool's own words, not the file's.
136
- const readLang = msg.readPath ? langForPath(msg.readPath) : null;
137
- // THE GUTTER IS ONE WIDTH FOR THE WHOLE BLOCK. The tool sends `N<TAB>text`, and the tab is what used to place the
138
- // code — so `1` and `10` started their text at different columns once the tab was expanded. Padding the NUMBER to
139
- // the widest one in this block keeps the digits left-aligned and the code aligned under itself.
140
- const numW = shown.reduce((w, ln) => {
141
- const m = /^(\d+)\t/.exec(ln);
142
- return m ? Math.max(w, m[1].length) : w;
143
- }, 0);
144
- let inComment = false;
145
- shown.forEach((ln, idx) => {
146
- const isErr = /^\[error:/.test(ln.trim());
147
- // A failed run is red throughout so it cannot be skimmed past.
148
- const color = isErr ? C.red : (failed ? C.red : C.gray);
149
- // `N<TAB>text` is the Read tool's own line format; only those rows are file content.
150
- const numbered = readLang && !isErr ? /^(\d+)\t([\s\S]*)$/.exec(ln) : null;
151
- if (numbered) {
152
- // A READ CAN START INSIDE A BLOCK COMMENT. The `/**` may sit above the first visible row, so no cross-row state
153
- // can tell the highlighter the first row is a comment — it coloured `of`, `switch`, `from` in a JSDoc paragraph
154
- // as keywords. A row that is plainly part of a block comment (` * …`, an opening `/**`, or the closing `*/`) is
155
- // painted as a comment on its own merits; the opener also seeds the rows that follow.
156
- const content = expandTabs(numbered[2]);
157
- const isBlockCommentRow = /^\s*(?:\/\*|\*|\*\/|\*\*)/.test(content);
158
- const painted = paintCode(content, readLang, '', inComment || isBlockCommentRow);
159
- if (/\/\*/.test(content) && !/\*\//.test(content)) inComment = true;
160
- else if (/\*\//.test(content)) inComment = false;
161
- // The separator is a SPACE: the number is padded to the block's widest, so every row's code starts in the same
162
- // column.
163
- const text = `${col(numbered[1].padStart(numW), C.white)} ${painted.text}`;
164
- // A Read row is TRUNCATED, not wrapped. Wrapping folded an overlong line onto a second row without a line
165
- // number, and that second row was exactly what a differential painter could not reconcile with the previous
166
- // frame: the leftover glyphs stayed under it as residue. One source line is one display row.
167
- //
168
- // The background has to survive the row's own resets: every syntax token closes with `ESC[0m`, and the padding
169
- // `fitAnsi` adds sits AFTER the last one. Re-asserting after each reset — the same trick `paintCode` uses for a
170
- // diff wash — keeps the surface on the padding too, so the block reads solid.
171
- const surfaced = text.replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
172
- diffRows.push({ text: C.codeBg + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 ? pre : cont, raw: true });
173
- return;
174
- }
175
- const wrapped = ln === '' ? [''] : wrapWords(ln, bodyW);
176
- // BASH OUTPUT SITS ON THE TERMINAL SURFACE, like text inside a real terminal emulator: the darkest recess on
177
- // screen. Other tools keep the plain page background, so a command's output reads as "a terminal ran this" rather
178
- // than as an ordinary result.
179
- const termSurface = msg.toolName === 'Bash' || msg.toolName === 'Shell';
180
- wrapped.forEach((t, i) => {
181
- if (termSurface) {
182
- const surfaced = t.replace(/\x1b\[0m/g, `\x1b[0m${C.termBg}${color}`);
183
- // A FOREGROUND IS NOT OPTIONAL HERE.
184
- //
185
- // This set only the background, so the text fell back to the terminal's own default colour. Against the very
186
- // dark `termBg` that is readable on a light default and invisible on a dark one — which is exactly what a
187
- // dark-background terminal showed: a solid dark block with no words in it.
188
- //
189
- // The `\x1b[0m` inside the row closes EVERYTHING, background included, so both have to be re-asserted after
190
- // each one: the wash and the colour.
191
- diffRows.push({ text: C.termBg + color + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 && i === 0 ? pre : cont, raw: true });
192
- } else {
193
- diffRows.push({ text: t, ind: idx === 0 && i === 0 ? pre : cont, color });
194
- }
195
- });
196
- });
197
- if (visible.length > MAX_RESULT_LINES) {
198
- // THE HINT SAYS HOW TO SEE THE REST, and what "the rest" is depends on where we are.
199
- //
200
- // Collapsed: Ctrl+O expands — that is the whole point of the line.
201
- // Already expanded: there is no more to reveal here, so inviting another Ctrl+O would be a dead end. Say how much
202
- // was withheld and where it does live, which is the tool result the model was given (and, when the result was
203
- // spilled, the file it was written to).
204
- const rest = visible.length - MAX_RESULT_LINES;
205
- const text = expanded
206
- ? uiText(`… ${rest} more line${rest === 1 ? '' : 's'} not shown; ctrl+o collapses, ctrl+e shows all of it`)
207
- : uiText(`… ${rest} more lines, ctrl+o to expand`);
208
- diffRows.push({
209
- text,
210
- ind: cont,
211
- color: failed ? C.red : C.gray,
212
- });
213
- }
214
- }
215
-
216
- const out = [];
217
- for (const r of diffRows) out.push(r);
218
- for (const r of receiptRows) out.push(r);
219
- // Put the `↳` on the FIRST rendered row (diff or output) — never on the receipt, which sits last.
220
- if (out.length && out[0].ind === cont && Array.isArray(msg.diff) && msg.diff.length > 0) {
221
- out[0] = { ...out[0], ind: pre };
222
- }
223
- // The TOOL ROW may already have drawn this result's body — a Write streams the file's content up there — in which
224
- // case the receipt below is the whole result and must NOT open a second `↳` block:
225
- // ● Used Write (a.txt) 1.3KB
226
- // ↳ 1 <content> <- the body, marked on the tool row
227
- // ↳ File written: a.txt (1372 bytes) <- the stray second `↳`, now plain
228
- // One body, one marker. `bodyOnToolRow` is set by the tool_result handler, the only place that knows whether the tool
229
- // row had content to show.
230
- if (msg.bodyOnToolRow && out.length && out[0].ind === pre) {
231
- out[0] = { ...out[0], ind: cont };
232
- }
233
- // Nothing to show (empty output) -> render no row at all rather than a dangling "↳ ".
234
- return out;
235
- }
1
+ // A tool result row: the `↳` block under the tool line.
2
+ //
3
+ // The shape is the same for every tool, and the parts differ:
4
+ //
5
+ // ● Used Edit (a.js) +1 -1 <- the tool line, counts only
6
+ // ↳ 179 - old line <- ONE `↳` on the FIRST body row
7
+ // 179 + new line
8
+ // Edited a.js: replaced 1 occurrence(s). <- receipt LAST
9
+ //
10
+ // An Edit's DIFF is the body and carries the marker, with the receipt moved to the bottom; every other tool's output
11
+ // IS the body and carries the marker on its first line.
12
+
13
+ import { C } from './colors.js';
14
+ import { col, expandTabs, fitAnsi, sanitizeText, visualCol, wrapWords } from './text.js';
15
+ import { uiText } from './i18n.js';
16
+ import { langForPath } from './highlight.js';
17
+ import { paintCode } from './diff.js';
18
+
19
+ export function toolResultRows(msg, width, expanded) {
20
+ // The marker sits in the SAME COLUMN as the tool bullet `●` (columns 0-1), so `↳ ` starts at column 0 and the output
21
+ // text is indented to the tool text column. ONE `↳` per result, however many lines it has.
22
+ //
23
+ // The marker carries the STRUCTURAL colour, not the body's. A `↳` is a piece of chrome —
24
+ // it says "what follows belongs to the line above" — and it was inheriting the result's
25
+ // grey, which on a dark page made it a smudge in the margin rather than a marker. The
26
+ // plain text is kept for the comparisons below and for the width maths; the coloured one
27
+ // is what is drawn.
28
+ const PRE = '↳ ';
29
+ const pre = C.marker + '↳ ' + C.reset;
30
+ const cont = ' ';
31
+ const bodyW = Math.max(1, width - visualCol(cont));
32
+ // Strip escapes BEFORE splitting: a file or a command can emit raw cursor codes, and letting them through made the
33
+ // terminal OBEY them — recolouring the transcript, resetting styles mid-row, clearing the screen over the composer.
34
+ const rawLines = sanitizeText(msg.text).replace(/\r\n/g, '\n').split('\n');
35
+ // A TAB counts as ZERO columns in our width maths but advances to the next tab stop in a real terminal. A row
36
+ // containing one was therefore padded short and the terminal kept the old frame's glyphs in the gap — the "residue"
37
+ // under tool output. Read rows are the exception: their first tab is the line-number separator and is handled in the
38
+ // Read branch below, where the CONTENT half is expanded too.
39
+ if (!msg.readPath) for (let k = 0; k < rawLines.length; k++) rawLines[k] = expandTabs(rawLines[k]);
40
+ // Trim blank lines at BOTH ends: commands typically emit a trailing newline, and some tools emit a leading one. A
41
+ // stray blank line would render as a lone "↳ " with no content.
42
+ while (rawLines.length && rawLines[0].trim() === '') rawLines.shift();
43
+ while (rawLines.length && rawLines[rawLines.length - 1].trim() === '') rawLines.pop();
44
+ // COLLAPSED IS A GLANCE; EXPANDED IS A READ, NOT A DUMP.
45
+ //
46
+ // Expanded used to be `Infinity`, so Ctrl+O on a command that printed 3000 lines put all 3000 into the transcript: the
47
+ // conversation above it scrolled away, the scrollbar became meaningless, and every frame afterwards paid to lay out
48
+ // rows nobody would read. The cap here is what a person can actually scan; past that the output belongs in a file,
49
+ // which is what the hint says.
50
+ const MAX_RESULT_LINES = expanded ? 400 : 12;
51
+ // The exit-code line is bookkeeping for the AGENT and is NEVER shown to the user — success or failure. A failure is
52
+ // already legible from the RED result text below. `[error: …]` is kept: it carries the message, not just a code.
53
+ const visible = rawLines.filter((ln) => !/^\[exit code:/.test(ln.trim()));
54
+ const failed = msg.failed === true;
55
+ // A silent Bash keeps the ONE empty output row it had while running. Only Bash has this area: an empty Read/Grep
56
+ // result must not grow a phantom line merely because the command itself printed nothing.
57
+ const shown = visible.length ? visible.slice(0, MAX_RESULT_LINES) : (msg.toolName === 'Bash' ? [''] : []);
58
+
59
+ const diffRows = [];
60
+ const receiptRows = [];
61
+ const hasDiff = Array.isArray(msg.diff); // empty array => receipt-only (Write)
62
+
63
+ if (hasDiff) {
64
+ // Same collapse rule as elsewhere: a short preview by default, everything once Ctrl+O expands. 16 rows (not 12)
65
+ // because a change renders as a PAIR: `- old` then `+ new`.
66
+ const MAX = expanded ? 400 : 16;
67
+ // Show the unified diff WITH context rows (ctx = dim) so a change is not two orphaned blocks; `gap` rows mark where
68
+ // unchanged lines were elided.
69
+ const total = msg.diff.length;
70
+ const wNo = Math.max(1, ...msg.diff.map((d) => String(d.no || 0).length));
71
+ // THE COMMENT STATE TRAVELS BETWEEN THE ROWS of one diff, so a block comment that opens on a context row and closes
72
+ // on a changed one stays grey across both.
73
+ let inComment = false;
74
+ let emitted = 0;
75
+ for (const d of msg.diff) {
76
+ if (emitted >= MAX) break;
77
+ if (d.type === 'gap') {
78
+ diffRows.push({ text: col(' ⋮', C.gray), ind: cont, raw: true });
79
+ emitted++;
80
+ continue;
81
+ }
82
+ const no = String(d.no || 0).padStart(wNo);
83
+ const mark = d.type === 'add' ? '+' : d.type === 'del' ? '-' : ' ';
84
+ // TABS ARE EXPANDED FIRST: `visualCol` counts one as zero columns, so an indented row measured short and its
85
+ // background stopped before the right edge — the strip of page colour seen on Go diffs.
86
+ const text = expandTabs(sanitizeText(d.text));
87
+
88
+ // The sign, line number and content share the line's addition/deletion background; syntax highlighting changes
89
+ // only the foreground. The wash runs to the right edge so adjacent changed rows read as one block.
90
+ const isAdd = d.type === 'add';
91
+ const isDel = d.type === 'del';
92
+ const lineBg = isAdd ? C.diffAddBg : isDel ? C.diffDelBg : '';
93
+ const fg = isAdd ? C.diffAddFg : isDel ? C.diffDelFg : C.white;
94
+ const lead = col(`${no} ${mark} `, lineBg ? fg + lineBg : fg);
95
+
96
+ if (!lineBg) {
97
+ // CONTEXT LINES ARE CODE TOO. They are most of a diff and the part a reader scans to place the change, so they
98
+ // get the same highlighter, on the CODE SURFACE rather than the raw page — a changed block sits on one recessed
99
+ // field. The +/- rows around it keep their green/red wash.
100
+ const ctxPainted = paintCode(text, d.lang, '', inComment);
101
+ inComment = ctxPainted.inComment;
102
+ const ctxRow = (lead + ctxPainted.text).replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
103
+ diffRows.push({ text: C.codeBg + fitAnsi(ctxRow, bodyW) + C.bgReset, ind: cont, raw: true });
104
+ emitted++;
105
+ continue;
106
+ }
107
+
108
+ // Keep only the line-level wash and syntax foreground. Positional word pairing can highlight the wrong token when
109
+ // lines are reordered or punctuation changes, so no word tint is applied.
110
+ const painted = paintCode(text, d.lang, lineBg, inComment);
111
+ inComment = painted.inComment;
112
+ const used = visualCol(`${no} ${mark} `) + visualCol(painted.text);
113
+ const pad = ' '.repeat(Math.max(0, bodyW - used));
114
+ // The padding is INSIDE the wash, so it must not end with a bare reset: `col()` closes with ESC[0m, which would
115
+ // drop the background on the last column and leave a strip of page colour at the right edge.
116
+ diffRows.push({ text: lead + painted.text + lineBg + pad + '\x1b[39m', ind: cont, raw: true });
117
+ emitted++;
118
+ }
119
+ if (total > emitted) {
120
+ diffRows.push({
121
+ // A DEAD-END Ctrl+O IS WORSE THAN NO HINT. Collapsed it expands; already expanded there is nothing left to
122
+ // reveal, so the line says how much was withheld instead of inviting a key that would do nothing.
123
+ text: col(expanded
124
+ ? uiText(`… (${total - emitted} more lines not shown; press ctrl+e for the whole thing)`)
125
+ : uiText(`… (${total - emitted} more lines, ctrl+o to expand)`), C.gray),
126
+ ind: cont, raw: true,
127
+ });
128
+ }
129
+ if (total === 0) diffRows.push({ text: col('(no changes)', C.gray), ind: cont, raw: true });
130
+ // The receipt: one line per source line of the tool's own message.
131
+ for (const ln of shown) {
132
+ const color = failed ? C.red : C.gray;
133
+ for (const t of (ln === '' ? [''] : wrapWords(ln, bodyW))) {
134
+ receiptRows.push({ text: t, ind: cont, color });
135
+ }
136
+ }
137
+ } else {
138
+ // No diff: the tool's own output is the body.
139
+ //
140
+ // A READ RESULT IS SOURCE CODE, so its numbered lines are syntax-coloured — the file's own language, carried on the
141
+ // message by the tool_result handler, which is the last place that still knows the path. The header and any error
142
+ // keep the plain grey: those are the tool's own words, not the file's.
143
+ const readLang = msg.readPath ? langForPath(msg.readPath) : null;
144
+ // THE GUTTER IS ONE WIDTH FOR THE WHOLE BLOCK. The tool sends `N<TAB>text`, and the tab is what used to place the
145
+ // code — so `1` and `10` started their text at different columns once the tab was expanded. Padding the NUMBER to
146
+ // the widest one in this block keeps the digits left-aligned and the code aligned under itself.
147
+ const numW = shown.reduce((w, ln) => {
148
+ const m = /^(\d+)\t/.exec(ln);
149
+ return m ? Math.max(w, m[1].length) : w;
150
+ }, 0);
151
+ let inComment = false;
152
+ shown.forEach((ln, idx) => {
153
+ const isErr = /^\[error:/.test(ln.trim());
154
+ // A failed run is red throughout so it cannot be skimmed past.
155
+ const color = isErr ? C.red : (failed ? C.red : C.gray);
156
+ // `N<TAB>text` is the Read tool's own line format; only those rows are file content.
157
+ const numbered = readLang && !isErr ? /^(\d+)\t([\s\S]*)$/.exec(ln) : null;
158
+ if (numbered) {
159
+ // A READ CAN START INSIDE A BLOCK COMMENT. The `/**` may sit above the first visible row, so no cross-row state
160
+ // can tell the highlighter the first row is a comment — it coloured `of`, `switch`, `from` in a JSDoc paragraph
161
+ // as keywords. A row that is plainly part of a block comment (` * …`, an opening `/**`, or the closing `*/`) is
162
+ // painted as a comment on its own merits; the opener also seeds the rows that follow.
163
+ const content = expandTabs(numbered[2]);
164
+ const isBlockCommentRow = /^\s*(?:\/\*|\*|\*\/|\*\*)/.test(content);
165
+ const painted = paintCode(content, readLang, '', inComment || isBlockCommentRow);
166
+ if (/\/\*/.test(content) && !/\*\//.test(content)) inComment = true;
167
+ else if (/\*\//.test(content)) inComment = false;
168
+ // The separator is a SPACE: the number is padded to the block's widest, so every row's code starts in the same
169
+ // column.
170
+ const text = `${col(numbered[1].padStart(numW), C.white)} ${painted.text}`;
171
+ // A Read row is TRUNCATED, not wrapped. Wrapping folded an overlong line onto a second row without a line
172
+ // number, and that second row was exactly what a differential painter could not reconcile with the previous
173
+ // frame: the leftover glyphs stayed under it as residue. One source line is one display row.
174
+ //
175
+ // The background has to survive the row's own resets: every syntax token closes with `ESC[0m`, and the padding
176
+ // `fitAnsi` adds sits AFTER the last one. Re-asserting after each reset — the same trick `paintCode` uses for a
177
+ // diff wash — keeps the surface on the padding too, so the block reads solid.
178
+ const surfaced = text.replace(/\x1b\[0m/g, `\x1b[0m${C.codeBg}`);
179
+ diffRows.push({ text: C.codeBg + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 ? pre : cont, raw: true });
180
+ return;
181
+ }
182
+ const wrapped = ln === '' ? [''] : wrapWords(ln, bodyW);
183
+ // BASH OUTPUT SITS ON THE TERMINAL SURFACE, like text inside a real terminal emulator: the darkest recess on
184
+ // screen. Other tools keep the plain page background, so a command's output reads as "a terminal ran this" rather
185
+ // than as an ordinary result.
186
+ const termSurface = msg.toolName === 'Bash' || msg.toolName === 'Shell';
187
+ wrapped.forEach((t, i) => {
188
+ if (termSurface) {
189
+ const surfaced = t.replace(/\x1b\[0m/g, `\x1b[0m${C.termBg}${color}`);
190
+ // A FOREGROUND IS NOT OPTIONAL HERE.
191
+ //
192
+ // This set only the background, so the text fell back to the terminal's own default colour. Against the very
193
+ // dark `termBg` that is readable on a light default and invisible on a dark one — which is exactly what a
194
+ // dark-background terminal showed: a solid dark block with no words in it.
195
+ //
196
+ // The `\x1b[0m` inside the row closes EVERYTHING, background included, so both have to be re-asserted after
197
+ // each one: the wash and the colour.
198
+ diffRows.push({ text: C.termBg + color + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 && i === 0 ? pre : cont, raw: true });
199
+ } else {
200
+ diffRows.push({ text: t, ind: idx === 0 && i === 0 ? pre : cont, color });
201
+ }
202
+ });
203
+ });
204
+ if (visible.length > MAX_RESULT_LINES) {
205
+ // THE HINT SAYS HOW TO SEE THE REST, and what "the rest" is depends on where we are.
206
+ //
207
+ // Collapsed: Ctrl+O expands — that is the whole point of the line.
208
+ // Already expanded: there is no more to reveal here, so inviting another Ctrl+O would be a dead end. Say how much
209
+ // was withheld and where it does live, which is the tool result the model was given (and, when the result was
210
+ // spilled, the file it was written to).
211
+ const rest = visible.length - MAX_RESULT_LINES;
212
+ const text = expanded
213
+ ? uiText(`… ${rest} more line${rest === 1 ? '' : 's'} not shown; ctrl+o collapses, ctrl+e shows all of it`)
214
+ : uiText(`… ${rest} more lines, ctrl+o to expand`);
215
+ diffRows.push({
216
+ text,
217
+ ind: cont,
218
+ color: failed ? C.red : C.gray,
219
+ });
220
+ }
221
+ }
222
+
223
+ const out = [];
224
+ for (const r of diffRows) out.push(r);
225
+ for (const r of receiptRows) out.push(r);
226
+ // Put the `↳` on the FIRST rendered row (diff or output) — never on the receipt, which sits last.
227
+ if (out.length && out[0].ind === cont && Array.isArray(msg.diff) && msg.diff.length > 0) {
228
+ out[0] = { ...out[0], ind: pre };
229
+ }
230
+ // The TOOL ROW may already have drawn this result's body — a Write streams the file's content up there — in which
231
+ // case the receipt below is the whole result and must NOT open a second `↳` block:
232
+ // ● Used Write (a.txt) 1.3KB
233
+ // ↳ 1 <content> <- the body, marked on the tool row
234
+ // ↳ File written: a.txt (1372 bytes) <- the stray second `↳`, now plain
235
+ // One body, one marker. `bodyOnToolRow` is set by the tool_result handler, the only place that knows whether the tool
236
+ // row had content to show.
237
+ if (msg.bodyOnToolRow && out.length && out[0].ind === pre) {
238
+ out[0] = { ...out[0], ind: cont };
239
+ }
240
+ // Nothing to show (empty output) -> render no row at all rather than a dangling "↳ ".
241
+ return out;
242
+ }
@@ -75,6 +75,10 @@ export const SETTINGS_SCHEMA = [
75
75
  // forgetting it left /settings offering fewer themes than /theme did.
76
76
  { key: 'theme', ...en('dark', THEME_NAMES),
77
77
  ui: { tab: 'appearance', group: 'Display', label: 'Theme', hint: 'Colour scheme. `auto` follows the terminal background' } },
78
+ // The language of the TERMINAL UI. Only what a person reads is translated; the system
79
+ // prompt and everything the model reads stay English.
80
+ { key: 'lang', ...en('en', ['en', 'zh']),
81
+ ui: { tab: 'appearance', group: 'Display', label: 'Interface language', hint: 'The language the terminal UI speaks. The model still reads English' } },
78
82
  { key: 'calm_mode', ...bool(false),
79
83
  ui: { tab: 'appearance', group: 'Display', label: 'Calm mode', hint: 'Fewer decorative flourishes; keeps the information, drops the animation' } },
80
84
  { key: 'reduced_motion', ...bool(false),
@@ -219,6 +223,11 @@ export const SETTINGS_SCHEMA = [
219
223
  ui: { tab: 'model', group: 'Thinking', label: 'Max output size', hint: 'Startup default for the reply length; 0 means fall back' } },
220
224
  { key: 'reasoning', ...bool(false),
221
225
  ui: { tab: 'model', group: 'Thinking', label: 'Reasoning by default', hint: 'Whether new sessions start with thinking on' } },
226
+ // Whether the model is told to wrap its reasoning in <|thinking|> tags. THREE states,
227
+ // not two: `auto` follows the model (a model with a reasoning channel does not need
228
+ // the tag, one without it does), and the other two decide by hand.
229
+ { key: 'thinking_tags', ...en('auto', ['auto', 'on', 'off']),
230
+ ui: { tab: 'model', group: 'Thinking', label: 'Inline reasoning tags', hint: 'Ask the model to wrap its reasoning in <|thinking|> tags. Auto: only for a model with no reasoning channel of its own' } },
222
231
  { key: 'plugins_dir', ...str(''),
223
232
  ui: { tab: 'advanced', group: 'MCP', label: 'Plugins directory', hint: 'Where plugins load from; defaults to ~/.hncode/plugins' } },
224
233
  { key: 'web_host', ...str('127.0.0.1'),
@@ -241,7 +241,7 @@ export function renderSwarmProgress(snapshot, width, opts = {}) {
241
241
  const lines = [];
242
242
 
243
243
  // ---- header: `─ Agent Swarm ─ desc ─ model ─────` ----
244
- const title = col('Agent Swarm', C.spring + C.bold);
244
+ const title = col(uiText('Agent Swarm'), C.spring + C.bold);
245
245
  const suffix = [];
246
246
  if (desc) suffix.push(col(' \u2500 ', C.spring) + col(desc, C.white));
247
247
  if (model) suffix.push(col(' \u2500 ', C.spring) + col(model, C.gray));