sumo-agents 0.6.1 → 0.7.1

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.
package/src/text.mjs CHANGED
@@ -25,6 +25,16 @@ export function ftsQuery(text) {
25
25
  /** Han, Hiragana, Katakana and Hangul: scripts whose words run on into the next, or into what is joined to them. */
26
26
  const CJK = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
27
27
 
28
+ /**
29
+ * The same words as an expression that wants every one of them (AND): what a search tries first, `ftsQuery` being what
30
+ * it falls back on. Words in Chinese, Japanese or Korean are left out — the index keeps a run of them as one word, so
31
+ * the caller looks for those in the text itself (`cjkTerms`). Empty when no other word is left.
32
+ */
33
+ export function ftsAllQuery(text) {
34
+ const unique = [...new Set(words(text))].filter((w) => !CJK.test(w));
35
+ return unique.map((w) => `"${w.replaceAll('"', '""')}"`).join(' AND ');
36
+ }
37
+
28
38
  /**
29
39
  * The words of a search in Chinese, Japanese or Korean. The index keeps a run of such characters as one word —
30
40
  * "日本語のテスト" is a single token — so a word inside it is never matched there; these are looked for in the text
@@ -59,6 +69,19 @@ export function overlap(a, b) {
59
69
  return shared / Math.min(sa.size, sb.size);
60
70
  }
61
71
 
72
+ /**
73
+ * Share of `text`'s own stems also found in `source`: 0..1. Not `overlap`, which takes the smaller text's share: a
74
+ * long sentence is not about a three-word one because those three words are in it.
75
+ */
76
+ export function coverage(text, source) {
77
+ const its = new Set(words(text).map(stem));
78
+ const theirs = new Set(words(source).map(stem));
79
+ if (its.size === 0) return 0;
80
+ let shared = 0;
81
+ for (const s of its) if (theirs.has(s)) shared++;
82
+ return shared / its.size;
83
+ }
84
+
62
85
  /** How much of what a memory says must be in the user's own words for it to be theirs. */
63
86
  const SAID_SHARE = 0.8;
64
87
  /**
@@ -68,8 +91,6 @@ const SAID_SHARE = 0.8;
68
91
  const NEGATION = /\b(?:never|not|no|none|avoid|without|cannot|dont|doesnt|didnt|wont|cant|isnt|arent|shouldnt|mustnt)\b|\b\w+n['’]t\b/i;
69
92
  /** A trailing "…, not B" or "…, never B", B a word or three: the alternative a statement is chosen over. */
70
93
  const ALTERNATIVE = /[,;]\s*(?:but\s+)?(?:not|never)\s+([\w.@/+-]+(?:\s+[\w.@/+-]+){0,2})\s*\.?\s*$/i;
71
- /** Whether a text is nothing but such an alternative — "never npm" — with no statement of its own to turn round. */
72
- export const onlyAlternative = (text) => /^\s*(?:but\s+)?(?:not|never)\s+[\w.@/+-]+(?:\s+[\w.@/+-]+){0,2}\s*\.?\s*$/i.test(text);
73
94
  /** Whether a text sets `alt` aside itself: moved "from" it, chose "instead of", "rather than" or "over" it, or said "not" it. */
74
95
  const setsAside = (text, alt) => {
75
96
  const said = alt.replace(/[.*+?^${}()|[\]\\]/g, (c) => `\\${c}`);
@@ -80,12 +101,69 @@ const withoutAlternative = (text, other) => {
80
101
  const m = ALTERNATIVE.exec(text);
81
102
  return m && setsAside(other, m[1].replace(/\.+$/, '')) ? text.slice(0, m.index) : text;
82
103
  };
104
+ /** "on" or "off" as a switch's state — turned on, kept off, "is off" ending its clause — never the "on" of "on fridays". */
105
+ const switched = (state) =>
106
+ new RegExp(`\\b(?:turn(?:ed|s|ing)?|switch(?:ed|es|ing)?)\\s+(?:[\\w-]+\\s+){0,3}?${state}\\b|\\b(?:is|are|be|stays?|left|leave|keeps?|kept)\\s+(?:[\\w-]+\\s+){0,3}?${state}\\b(?=\\s+(?:in|by|for|at|when|while|during|everywhere)\\b|\\s*[,.;!?]|\\s*$)`, 'i');
107
+ /**
108
+ * Words that turn a rule round with no negation between them: "disabled" filed from "enabled" is as much the opposite
109
+ * as "never" from "always" (an independent review, Nov 18 2026, filed each of these from the user's own quote).
110
+ */
111
+ const OPPOSITES = [
112
+ [/\benabl(?:e|es|ed|ing)\b/i, /\bdisabl(?:e|es|ed|ing)\b/i],
113
+ [/\b(?:allow(?:s|ed|ing)?|permit(?:s|ted|ting)?)\b/i, /\b(?:forbid(?:s|den|ding)?|prohibit(?:s|ed|ing)?|disallow(?:s|ed|ing)?|block(?:s|ed|ing)?)\b/i],
114
+ [/\bbefore\b/i, /\bafter\b/i],
115
+ [/\badd(?:s|ed|ing)?\b/i, /\bremov(?:e|es|ed|ing)\b/i],
116
+ [switched('on'), switched('off')],
117
+ ];
118
+ /** Whether one text holds one side of a pair and the other text only the other side: both sides in one text is no turn. */
119
+ const opposite = (a, b) =>
120
+ OPPOSITES.some(([one, other]) => {
121
+ const [a1, a2, b1, b2] = [one.test(a), other.test(a), one.test(b), other.test(b)];
122
+ return (a1 && !a2 && b2 && !b1) || (a2 && !a1 && b1 && !b2);
123
+ });
124
+
125
+ /** A text's words with what a name or a version is written with kept in them: `3.12`, `feature/login`, `node-22`. */
126
+ const named = (text) => (text.toLowerCase().match(/[\p{L}\p{N}_][\p{L}\p{N}_./+-]*/gu) ?? []).map((w) => w.replace(/[./+-]+$/, ''));
127
+ const DETERMINERS = new Set(['the', 'a', 'an', 'our', 'my', 'your', 'their']);
128
+ /**
129
+ * The values a text names where one can be told from prose: the word before "branch", a word with a digit (with the
130
+ * word before it — "node 22"), and what is used ("uses pnpm"). Narrow on purpose: a synonym elsewhere is a paraphrase.
131
+ */
132
+ function valuesNamed(text) {
133
+ const all = named(text);
134
+ const out = { branch: [], version: [], tool: [] };
135
+ all.forEach((w, i) => {
136
+ if (w === 'branch' && i > 0 && !STOPWORDS.has(all[i - 1])) out.branch.push({ value: all[i - 1], after: '' });
137
+ if (/\d/.test(w) && i > 0) out.version.push({ value: w, after: stem(all[i - 1]) });
138
+ if (/^us(?:e|es|ed|ing)$/.test(w)) {
139
+ let next = i + 1;
140
+ while (DETERMINERS.has(all[next])) next++;
141
+ if (all[next] && !STOPWORDS.has(all[next])) out.tool.push({ value: all[next], after: '' });
142
+ }
143
+ });
144
+ return { all: new Set(all.map(stem)), ...out };
145
+ }
146
+ /**
147
+ * Whether `a` names a branch, a version or a tool that `b` nowhere holds, where `b` names another: "the release
148
+ * branch" from "the main branch", "node 20" from "node 22", "uses npm" from "uses pnpm". A value `b` holds anywhere is
149
+ * the user's own — "uses bun" from "we moved from pnpm to bun".
150
+ */
151
+ function namesAnother(a, b) {
152
+ const [its, theirs] = [valuesNamed(a), valuesNamed(b)];
153
+ return ['branch', 'version', 'tool'].some((kind) => its[kind].some((v) => !theirs.all.has(stem(v.value)) && theirs[kind].some((t) => t.after === v.after)));
154
+ }
155
+
83
156
  /**
84
157
  * Whether two texts say a thing the same way round: both negate it, or neither does. "slate uses bun, not pnpm" from "we
85
158
  * moved from pnpm to bun" is not a turn — the "not" sets aside what they moved from; "never force-push" is, from "force-push
86
- * freely", which set nothing aside. `aside` is where `a`'s alternative is looked for, when `b` is only part of what was said.
159
+ * freely", which set nothing aside. Nor is it the same way round when one says the opposite in a word (`OPPOSITES`) or
160
+ * names another value (`namesAnother`). `aside` is the whole of what was said, when `b` is only part of it: where
161
+ * `a`'s alternative and the values it names are looked for.
87
162
  */
88
- export const sameWayRound = (a, b, aside = b) => NEGATION.test(withoutAlternative(a, aside)) === NEGATION.test(withoutAlternative(b, a));
163
+ export const sameWayRound = (a, b, aside = b) => {
164
+ const [said, typed] = [withoutAlternative(a, aside), withoutAlternative(b, a)];
165
+ return NEGATION.test(said) === NEGATION.test(typed) && !opposite(said, typed) && !namesAnother(a, aside);
166
+ };
89
167
 
90
168
  /**
91
169
  * Whether what the user typed says what a memory says: nearly all of the memory's own words are in theirs — not a
@@ -105,6 +183,16 @@ export function sharedStems(a, b) {
105
183
  return [...new Set(words(a).map(stem))].filter((s) => sb.has(s)).length;
106
184
  }
107
185
 
186
+ /**
187
+ * How many of a search's distinct words a text holds: by stem, and for a Chinese, Japanese or Korean word by its
188
+ * characters anywhere in the text, as it is searched.
189
+ */
190
+ export function termsHeld(query, text) {
191
+ const lower = text.toLowerCase();
192
+ const stems = new Set(words(text).map(stem));
193
+ return [...new Set(words(query))].filter((w) => (CJK.test(w) ? lower.includes(w) : stems.has(stem(w)))).length;
194
+ }
195
+
108
196
  /**
109
197
  * Deliberately a heuristic: an exact count needs a tokenizer, and these numbers
110
198
  * only have to keep a generated block under its budget.
@@ -130,10 +218,10 @@ export function clip(text, max) {
130
218
  return flat.length <= max ? flat : `${head(flat, max - 1)}…`;
131
219
  }
132
220
 
133
- /** Words that mark a sentence as a standing instruction, which is worth filing right away rather than in the next batch. */
221
+ /** Words that mark a sentence as a standing instruction: whatever else it does, it is not only a request for work. */
134
222
  const DURABLE = /\b(always|never|from now on|going forward|remember|don'?t ever|i (prefer|like|want(?! to\b)|hate|use)|we (use|moved|switched|decided)|make sure|by default|every time|stop (doing|using))\b/i;
135
223
 
136
- export const looksDurable = (text) => DURABLE.test(text);
224
+ const looksDurable = (text) => DURABLE.test(text);
137
225
 
138
226
  /**
139
227
  * A clause that asks for work or an answer — describe this, fix that, delegate, a question — rather than stating
@@ -177,6 +265,30 @@ export const asksForWork = (text) => {
177
265
  return REQUEST.test(said) || sentences.some((x) => oneChange(x) || ANSWER_OR_SWITCH.test(x.replace(LEADING, '')));
178
266
  };
179
267
 
268
+ /** Words that agree, thank or wave on, and nouns that only stand for something said before: none of them is what a sentence is about. */
269
+ const EMPTY_WORDS = new Set(
270
+ `ok okay yes yeah yep yup sure please pls thanks thank good great fine nice cool perfect right agreed sounds sound looks look seems
271
+ works go ahead just well plan idea approach option suggestion proposal thing way one same lets let`.split(/\s+/),
272
+ );
273
+ /** Words that point at something said elsewhere. */
274
+ const POINTS = /\b(?:it|this|that|these|those|them|so|same)\b/i;
275
+ /** A sentence whose whole statement is approval. */
276
+ const APPROVES = /\b(?:sounds?|looks?|seems?)\s+(?:good|great|fine|right|ok|okay|perfect|nice)\b|\bworks\s+for\s+me\b|\b(?:that|it|this)(?:'s|\s+is)\s+(?:good|great|fine|right|ok|okay|perfect)\b/i;
277
+
278
+ /**
279
+ * Whether a sentence says something by itself, so it can be a memory when stored as typed. "ok that plan sounds
280
+ * good", "yes do that always" and "use it" are the user's words and say nothing: what they agree to or point at was
281
+ * said by someone else. Not so when nothing is left once the words of agreement are taken out, when what is left is
282
+ * under three words beside a pointing word, or when the sentence only approves and gives no rule of its own. A short
283
+ * rule stands: "no emojis", "never push to main".
284
+ */
285
+ export function standsAlone(sentence) {
286
+ const content = new Set(words(sentence).filter((w) => !STOPWORDS.has(w) && !EMPTY_WORDS.has(w)).map(stem));
287
+ if (content.size === 0) return false;
288
+ if (content.size < 3 && POINTS.test(sentence)) return false;
289
+ return !(APPROVES.test(sentence) && !looksDurable(sentence));
290
+ }
291
+
180
292
  /**
181
293
  * The known name a mistyped one was most likely meant to be: two edits away at most, nearer than any other, and with
182
294
  * at least half of what was typed kept — `m` is no guess at `mcp`, nor `xml` at `md`; `serch` is at `search`.
package/src/tools.mjs CHANGED
@@ -5,6 +5,7 @@ import { delimiter, dirname, extname, join, relative, resolve, sep } from 'node:
5
5
  import { CREDENTIAL_NAMES } from './auth.mjs';
6
6
  import { guardCommand, guardPath } from './guard.mjs';
7
7
  import { SELF } from './paths.mjs';
8
+ import { readPlainFile } from './scan.mjs';
8
9
  import { redact } from './redact.mjs';
9
10
  import { blockedWrite, guardedPlaces, guardedWhy, readOnlyPlaces, sandboxWrap, unsandboxable, writablePlaces } from './sandbox.mjs';
10
11
 
@@ -710,8 +711,13 @@ async function blocked(command, shown, ctx, jail, options) {
710
711
  const BINARY_PROBE_BYTES = 8192;
711
712
 
712
713
  function view(path, range) {
713
- if (statSync(path).isDirectory()) return result(cap(readdirSync(path).join('\n')));
714
- const bytes = readFileSync(path);
714
+ let bytes;
715
+ try {
716
+ bytes = readFileSync(path);
717
+ } catch (cause) {
718
+ if (cause.code === 'EISDIR') return result(cap(readdirSync(path).join('\n')));
719
+ throw cause;
720
+ }
715
721
  // A binary shown as text is pages of nonsense, every page billed: say what it is instead.
716
722
  if (bytes.subarray(0, BINARY_PROBE_BYTES).includes(0)) return result(`${path} is a binary file (${bytes.length} bytes) — not shown; use a tool that reads its kind, or \`file\` and \`xxd | head\` for a look`, true);
717
723
  const lines = bytes.toString('utf8').split('\n');
@@ -948,9 +954,9 @@ function change(path, ctx, write, { created = false } = {}) {
948
954
  }
949
955
 
950
956
  /** A whole line that is one comment — `//`, `#`, `/* *\/`, `<!-- -->`, `--`, JSX's `{/* *\/}` — and what it says. */
951
- const COMMENT_LINE = /^(?:\/\/+|#+|\/\*+|<!--|--|\{\/\*+)\s*(.*?)\s*(?:\*+\/\}?|-->)?$/;
952
- /** The block comments that close on their line: only dots in one is a gap left in the code, where `// ...` alone may be a deliberate stub. */
953
- const CLOSED_COMMENT = /(?:\*\/\}?|-->)$/;
957
+ const COMMENT_LINE = /^(?:\/\/+|#+|\/\*+|<!--|--|\{\/\*+)\s*(.*?)\s*(?:\*+\/\}?|--!?>)?$/;
958
+ /** The block comments that close on their line (HTML's closes on `--!>` too): only dots in one is a gap left in the code, where `// ...` alone may be a deliberate stub. */
959
+ const CLOSED_COMMENT = /(?:\*\/\}?|--!?>)$/;
954
960
  const DOTS = /\.{3,}|…/g;
955
961
  /** That what was left out stays as it was: optional after `rest of the code`; needed after `other methods` with no dots around it, which could be a heading. */
956
962
  const STAYS = String.raw`(?:\s+(?:is\s+|are\s+|stays\s+|remains\s+|goes\s+)?(unchanged|the same|as before|as is|omitted|here|goes here|follows|not shown))?`;
@@ -1143,8 +1149,13 @@ export function runEditor(input, ctx) {
1143
1149
  const gap = placeholderIn(path, 'file_text', input.file_text);
1144
1150
  if (gap) return result(gap, true);
1145
1151
  // The write below refuses a file that is there anyway; said here in words that tell the model what to do instead.
1146
- if (existsSync(path) && statSync(path).isFile()) {
1147
- const held = readFileSync(path, 'utf8');
1152
+ let held = null;
1153
+ try {
1154
+ held = readPlainFile(path);
1155
+ } catch {
1156
+ // Not there: made below.
1157
+ }
1158
+ if (held !== null) {
1148
1159
  const lines = held.split('\n').length - (held.endsWith('\n') ? 1 : 0);
1149
1160
  return result(`${path} already exists (${lines} line${lines === 1 ? '' : 's'}): change the lines that change with str_replace or insert — a file is never rewritten whole`, true);
1150
1161
  }
@@ -1,10 +1,10 @@
1
1
  import { closeSync, fstatSync, openSync, readSync } from 'node:fs';
2
- import { redact } from './redact.mjs';
3
2
  import { head } from './text.mjs';
4
3
 
5
4
  /**
6
- * Generous on purpose: the closing reply of a piece of work is where the traps it hit are mentioned,
7
- * usually mid-paragraph. At 400 characters a real "tests only pass with SIMBA_TZ set" was cut out.
5
+ * How much of a reply the memory pass is shown. Generous on purpose: the closing reply of a piece of work is where
6
+ * the traps it hit are mentioned, usually mid-paragraph. At 400 characters a real "tests only pass with SIMBA_TZ set"
7
+ * was cut out.
8
8
  */
9
9
  const REPLY_MAX = 1200;
10
10
 
@@ -60,47 +60,3 @@ export function readBackwards(file, enough) {
60
60
  return null;
61
61
  }
62
62
  }
63
-
64
- /**
65
- * The assistant's own words from a Claude Code transcript, since a given time.
66
- *
67
- * Only `text` blocks of main-thread assistant messages are read. Tool calls,
68
- * tool results, thinking and sub-agent traffic are skipped on purpose: they are
69
- * large, and tool output is exactly where instructions injected by a web page
70
- * or a file would arrive. Nothing from there may ever reach the memory writer.
71
- *
72
- * The format is not a stable contract, so anything unexpected is ignored rather
73
- * than fatal — without this file the writer still has the user's own turns.
74
- * A secret the assistant repeated is scrubbed like one the user typed, and before
75
- * the reply is cut: half a token left at the edge of the cut would still be a leak.
76
- */
77
- export function assistantReplies(file, sinceIso, wanted = Infinity) {
78
- // Read from the end: the last `wanted` replies since `sinceIso` are all that is ever asked for.
79
- return (
80
- readBackwards(file, (lines, whole) => {
81
- const replies = [];
82
- let reachedSince = false;
83
- for (const line of lines) {
84
- if (!line.includes('"assistant"')) continue;
85
- let entry;
86
- try {
87
- entry = JSON.parse(line);
88
- } catch {
89
- continue;
90
- }
91
- if (entry.type !== 'assistant' || entry.isSidechain || !Array.isArray(entry.message?.content)) continue;
92
- if (sinceIso && entry.timestamp && entry.timestamp < sinceIso) {
93
- reachedSince = true;
94
- continue;
95
- }
96
- const text = entry.message.content
97
- .filter((block) => block?.type === 'text' && typeof block.text === 'string')
98
- .map((block) => block.text)
99
- .join('\n')
100
- .trim();
101
- if (text) replies.push({ ts: entry.timestamp ?? '', text: abridge(redact(text).text) });
102
- }
103
- return whole || reachedSince || replies.length >= wanted ? replies : undefined;
104
- }) ?? []
105
- );
106
- }
package/src/tty.mjs CHANGED
@@ -23,11 +23,11 @@ const CODES = { bold: ['1', '22'], dim: ['2', '22'], italic: ['3', '23'], underl
23
23
  * reader says as itself: no box drawing, no symbol spelled out by its Unicode name.
24
24
  */
25
25
  const GLYPHS = {
26
- tool: '⏺', fail: '✗', pick: '❯', under: '⎿', bar: '┃', quote: '│', rule: '─', fenceOpen: '┌─', fenceClose: '└─', todo: '☐', done: '☑', bullet: '•', warn: '⚠', thought: '✻',
26
+ tool: '⏺', fail: '✗', pick: '❯', under: '⎿', bar: '┃', quote: '│', rule: '─', todo: '☐', done: '☑', bullet: '•', warn: '⚠', thought: '✻',
27
27
  box: { h: '─', v: '│', tl: '┌', tm: '┬', tr: '┐', ml: '├', mm: '┼', mr: '┤', bl: '└', bm: '┴', br: '┘' },
28
28
  };
29
29
  const PLAIN_GLYPHS = {
30
- tool: '*', fail: 'x', pick: '>', under: '|', bar: '>', quote: '|', rule: '-', fenceOpen: '+-', fenceClose: '+-', todo: '[ ]', done: '[x]', bullet: '-', warn: '!', thought: '*',
30
+ tool: '*', fail: 'x', pick: '>', under: '|', bar: '>', quote: '|', rule: '-', todo: '[ ]', done: '[x]', bullet: '-', warn: '!', thought: '*',
31
31
  box: { h: '-', v: '|', tl: '+', tm: '+', tr: '+', ml: '+', mm: '+', mr: '+', bl: '+', bm: '+', br: '+' },
32
32
  };
33
33
 
@@ -72,7 +72,7 @@ const WIDE = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF
72
72
  const columnsOf = (ch) => (WIDE.test(ch) ? 2 : 1);
73
73
 
74
74
  /** The columns text takes on a terminal, colour codes not counted: a wide character is two, so a table around it lines up. */
75
- const visible = (t) => {
75
+ export const visible = (t) => {
76
76
  const plain = t.replace(/\x1b\[[0-9;]*m|\x1b\]8;[^\x07]*\x07/g, '');
77
77
  if (!/[^\x00-\x7e]/.test(plain)) return plain.length;
78
78
  let width = 0;
@@ -195,7 +195,7 @@ export function highlight(line, lang, s, code = null) {
195
195
  state.comment = false;
196
196
  }
197
197
  const quotes = [...language.quotes].map((q) => `${q === '`' ? '`' : `\\${q}`}(?:[^${q === '`' ? '`' : `\\${q}`}\\\\]|\\\\.)*${q === '`' ? '`' : `\\${q}`}`).join('|');
198
- const comment = language.line === '#' ? '(?:^|(?<=\\s))#.*' : language.line ? `${language.line.replace(/\//g, '\\/')}.*` : '(?!)';
198
+ const comment = language.line === '#' ? '(?:^|(?<=\\s))#.*' : language.line ? `${language.line.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')}.*` : '(?!)';
199
199
  const pattern = new RegExp(`(${comment})|(${language.block ? '\\/\\*' : '(?!)'})|(${quotes || '(?!)'})|(\\b0x[0-9a-fA-F]+\\b|\\b\\d[\\d_]*(?:\\.\\d+)?(?:[eE][+-]?\\d+)?\\b)|(\\b[A-Za-z_][\\w]*\\b)`, 'g');
200
200
  pattern.lastIndex = at;
201
201
  for (let m; (m = pattern.exec(line)); ) {
@@ -223,10 +223,11 @@ export function highlight(line, lang, s, code = null) {
223
223
  * One line of a reply: block-level marks replaced by weight and indent, then wrapped with a hanging indent.
224
224
  * `hang` is the indent of the list item a line may go on with: an indented line that is not an item of its own is drawn there.
225
225
  */
226
- export function renderLine(line, s, { fence = false, width = MAX_WIDTH, hang = null, lang = '', code = null } = {}) {
227
- // Code is what is read most closely: the frame around it is faint, the code is not.
228
- // A line too long for the window goes on under itself, at its own indent, so the code keeps its shape.
229
- if (fence) return wrap(` ${highlight(line, lang, s, code)}`, width, ` ${/^\s*/.exec(line)[0]}`);
226
+ export function renderLine(line, s, { fence = false, width = MAX_WIDTH, hang = null, lang = '', code = null, whole = false } = {}) {
227
+ // Code is drawn as it was written, from the first column, with nothing around it: what is selected on the screen is the
228
+ // code. `whole`, a line too long for the window is left for the terminal to wrap, and a copy joins it again; else it
229
+ // goes on under itself, at its own indent, so the code keeps its shape.
230
+ if (fence) return whole ? highlight(line, lang, s, code) : wrap(highlight(line, lang, s, code), width, /^\s*/.exec(line)[0]);
230
231
  const heading = /^(#{1,6})\s+(.*)$/.exec(line);
231
232
  if (heading) return wrap(heading[1].length === 1 ? s.bold(s.underline(inline(heading[2], s))) : s.bold(inline(heading[2], s)), width);
232
233
  const item = LIST_ITEM.exec(line);
@@ -382,7 +383,14 @@ function closedForNow(line) {
382
383
  return shown.slice(at + 2).trim() ? `${words}**${shown.slice(words.length)}` : shown.slice(0, at);
383
384
  }
384
385
 
385
- export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
386
+ const FENCE = /^(\s*)```\s*(\S*)/;
387
+
388
+ /**
389
+ * A reply rendered as it streams, each finished line handed to `write` with `{ code }`: whether it is a line of a code
390
+ * block. `whole` leaves a code line unbroken for the terminal to wrap (see renderLine); the line still arriving is
391
+ * always wrapped, for it is drawn and drawn again inside the window.
392
+ */
393
+ export function createRenderer(write, s, { width = MAX_WIDTH, whole = false } = {}) {
386
394
  let tail = '';
387
395
  let fence = false;
388
396
  let rows = [];
@@ -391,19 +399,30 @@ export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
391
399
  /** The language the code block in hand is in, and where its colouring has got to: a comment can run over lines. */
392
400
  let lang = '';
393
401
  let code = { comment: false };
402
+ /** A code line loses the indent its fence has, as Markdown reads it: a block in a list item is not drawn, or copied, at the item's indent. */
403
+ let indent = '';
404
+ const unindented = (raw) => (raw.startsWith(indent) ? raw.slice(indent.length) : raw.trimStart());
405
+ /** A line rendered, or null for a fence: outside plain mode it draws no row of its own. */
394
406
  const line = (raw) => {
395
- const marks = /^\s*```\s*(\S*)/.exec(raw);
407
+ const marks = FENCE.exec(raw);
396
408
  if (marks) {
397
409
  fence = !fence;
398
410
  hang = null;
399
- lang = fence ? marks[1].toLowerCase() : '';
411
+ lang = fence ? marks[2].toLowerCase() : '';
412
+ indent = fence ? marks[1] : '';
400
413
  code = { comment: false };
401
- // Plain mode names the block in words, for a reader: no frame corner to spell out.
402
- if (s.plain) return s.dim(fence ? ` code${marks[1] ? ` (${marks[1]})` : ''}:` : ' end of code');
403
- return s.dim(fence ? ` ${s.g.fenceOpen}${marks[1] ? ` ${marks[1]}` : ''}` : ` ${s.g.fenceClose}`);
414
+ // Plain mode names the block in words, for a reader.
415
+ if (s.plain) return s.dim(fence ? `code${marks[2] ? ` (${marks[2]})` : ''}:` : 'end of code');
416
+ return null;
404
417
  }
405
- if (!fence) hang = hangOf(raw) ?? (/^\s+\S/.test(raw) || raw.trim() === '' ? hang : null);
406
- return renderLine(raw, s, { fence, width, hang: hangOf(raw) === null ? hang : null, lang, code });
418
+ if (fence) return renderLine(unindented(raw), s, { fence, width, lang, code, whole });
419
+ hang = hangOf(raw) ?? (/^\s+\S/.test(raw) || raw.trim() === '' ? hang : null);
420
+ return renderLine(raw, s, { width, hang: hangOf(raw) === null ? hang : null });
421
+ };
422
+ const put = (raw, end) => {
423
+ const isCode = fence && !FENCE.test(raw);
424
+ const shown = line(raw);
425
+ if (shown !== null) write(`${shown}${end}`, { code: isCode });
407
426
  };
408
427
  /** The rows held so far: a table once its rule has come, the lines they were otherwise. */
409
428
  const held = () => (rows.length >= 2 && TABLE_RULE.test(rows[1]) ? renderTable(rows, s, width) : rows.map((r) => renderLine(r, s, { width })).join('\n'));
@@ -418,9 +437,9 @@ export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
418
437
  rows.push(raw);
419
438
  continue;
420
439
  }
421
- if (rows.length > 0) write(`${held()}\n`);
440
+ if (rows.length > 0) write(`${held()}\n`, { code: false });
422
441
  rows = [];
423
- write(`${line(raw)}\n`);
442
+ put(raw, '\n');
424
443
  }
425
444
  },
426
445
  flush() {
@@ -428,19 +447,24 @@ export function createRenderer(write, s, { width = MAX_WIDTH } = {}) {
428
447
  rows.push(tail);
429
448
  tail = '';
430
449
  }
431
- if (rows.length > 0) write(`${held()}${tail ? '\n' : ''}`);
432
- if (tail) write(line(tail));
450
+ if (rows.length > 0) write(`${held()}${tail ? '\n' : ''}`, { code: false });
451
+ if (tail) put(tail, '');
433
452
  tail = '';
434
453
  rows = [];
435
454
  fence = false;
436
455
  hang = null;
437
456
  lang = '';
457
+ indent = '';
438
458
  code = { comment: false };
439
459
  },
460
+ /** Whether the line still arriving is a line of code. */
461
+ get fenced() {
462
+ return fence;
463
+ },
440
464
  /** What has not been written yet, rendered as far as it has got: the rows of a table, and the line still arriving. */
441
465
  get tail() {
442
466
  const going = /^\s+\S/.test(tail) && hangOf(tail) === null ? hang : null;
443
- return [rows.length > 0 ? held() : '', tail && !/^\s*```/.test(tail) ? renderLine(fence ? tail : closedForNow(tail), s, { fence, width, hang: going, lang, code: { ...code } }) : ''].filter(Boolean).join('\n');
467
+ return [rows.length > 0 ? held() : '', tail && !FENCE.test(tail) ? renderLine(fence ? unindented(tail) : closedForNow(tail), s, { fence, width, hang: going, lang, code: { ...code } }) : ''].filter(Boolean).join('\n');
444
468
  },
445
469
  };
446
470
  }
@@ -847,10 +871,11 @@ export function transcript(log, s, { width = MAX_WIDTH, cwd = null } = {}) {
847
871
  if (entry.kind === 'user') out.push('', ...untab(entry.text).split('\n').map((line) => `${s.yellow(g.bar)} ${line}`));
848
872
  else if (entry.kind === 'reply') {
849
873
  const drawn = [];
850
- const reply = createRenderer((text) => drawn.push(text), s, { width: Math.max(MIN_WIDTH, width - 3) });
874
+ const reply = createRenderer((text, { code }) => drawn.push(...text.replace(/\n$/, '').split('\n').map((row) => ({ row, code }))), s, { width: Math.max(MIN_WIDTH, width - 3) });
851
875
  reply.write(entry.raw);
852
876
  reply.flush();
853
- out.push('', ...drawn.join('').replace(/\n$/, '').split('\n').map((line, i) => `${i === 0 ? `${g.tool} ` : ' '}${line}`));
877
+ // A line of code stands at the first column, as on the chat screen: what is selected is the code.
878
+ out.push('', ...drawn.map(({ row, code }, i) => (code ? row : `${i === 0 ? `${g.tool} ` : ' '}${row}`)));
854
879
  } else if (entry.kind === 'tool') {
855
880
  const view = toolView(entry.call, entry.result, { full: true, cwd });
856
881
  const stat = statOf(view.stat, s);