pi-odd-worktree 3.6.0 → 3.6.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/README.md CHANGED
@@ -73,7 +73,7 @@ The full rules of each step, including every refusal, are in [Create, list, move
73
73
  - **The issues rail is checkout-scoped and optional.** When a Gentle Shell has `gentle-shell-bridge` installed and draws its rail, a `GITHUB` rail section lists the open issues of this checkout's repository assigned to you — that one repository, not every issue across every repository you touch — seven at a time, with `and N more issues` for the rest and a pull-request mark on the issues that already have one. A click opens that issue in the `/issues` panel on `Assigned`, not in a browser: the click first proves the number belongs to this checkout, so a row left over from another checkout — or a checkout whose repository cannot be proven — opens nothing instead of guessing, and `/issues`' own `o` key is still the browser. The overflow row opens `/issues` with no number. While the rail's cache holds this repository, `/issues` paints its first frame from it and reads one tab at mount instead of three, the other two reading the first time you open them; the cache outlives both the session and the process — one document under `$XDG_CACHE_HOME/odd-worktree/` — so a warm checkout's first read is already a delta. A frame never waits on `gh`, and neither does a session start: the `gh` gate is dispatched, never awaited, so no child process and no network call sits on opening or switching a session. While the section is drawn it re-reads on its own 20 s timer, keeps no timer while hidden and stops the moment it is no longer drawn, and a `bash` command that mutates an issue or a pull request forces one read behind a 5 s cap; a failed refresh keeps the rows and marks them `stale` rather than emptying the list. Without `gh` on `PATH` neither the section nor `/issues` exists at all — the command is never registered and the section is never added to the rail — and every other surface keeps working. With no bridge the section simply never appears; the one thing you get is a single warning naming the install command, in a primary session only. See [the rail section](docs/reference.md#the-github-rail-section).
74
74
  - **Cards follow your shell.** Tool and warp cards use the card style Gentle Shell publishes, and a click expands that card only. See [card style](docs/reference.md#card-style-follows-your-shell).
75
75
  - **Tool headings can name the activity.** With `activity` chosen on the `Card tags` line, a bash card whose heading shows the whole command is headed with the family its command names (mark and word) instead of `$`, and a `grep`, `find` or `ls` card is headed `⌕ exploring` instead of its own glyph and name, while a `read` — which runs no shell command — keeps the host's own card, colour and width; a bash command the classifier cannot name, or a heading that hides part of it, keeps the host's `$`. See [the cards the `activity` design paints](docs/reference.md#the-cards-the-activity-design-paints).
76
- - **File calls on a skill or an ODD are cards too.** While `Card tags` answers with `activity` for the tool being painted, a `read`, `edit` or `write` call whose path names a skill (anything below a `skills` folder) or an ODD (anything below an `odd` folder) heads as that file, with the tool, the path and a read's line range; its Markdown body — the whole document when expanded, the first rows when collapsed — is drawn by Pi's own renderer in the theme's Markdown roles, and an edit always keeps its diff. Any other file call, and every call while that line answers anything else, is the host's own row. See [the file cards](docs/reference.md#the-file-cards).
76
+ - **File calls on a skill or an ODD are cards too.** While `Card tags` answers with `activity` for the tool being painted, a `read`, `edit` or `write` call whose path names a skill (anything below a `skills` folder) or an ODD (anything below an `odd` folder) heads as that file, with the tool, the path and a read's line range; its Markdown body — the whole document when expanded, the first rows when collapsed — is drawn by Pi's own renderer in the theme's Markdown roles — for an `edit`, the fragment it leaves behind, never its diff, and a call that leaves no fragment falls back to its own rows. Any other file call, and every call while that line answers anything else, is the host's own row. See [the file cards](docs/reference.md#the-file-cards).
77
77
  - **Background-job tools are cards too.** While `Card tags` answers with `activity` for the tool being painted, `bash_background`, `monitor`, `job_stop` and `job_list` are drawn in the shell's own card style instead of Pi's plain box. Any other answer leaves Pi's box untouched. See [the jobs card](docs/reference.md#the-jobs-card).
78
78
  - **`edit` and `write` cannot land in the principal checkout's default branch.** A write whose path is inside the principal checkout of the session's repository, while that checkout is on the default branch, is refused with one line pointing at `worktree_list` and `worktree_create`. Paths outside the repository, anything under the OS temp directories, other repositories and linked worktrees are never blocked. See [the guard](docs/reference.md#the-edit-and-write-guard).
79
79
  - **`bash` cannot leave the principal checkout's default branch.** A `git worktree add` or `git worktree list` is refused in any cwd and pointed at `worktree_create` and `worktree_list`; in the principal checkout, a switch that would leave the branch it is on is refused while that branch is the repository's default, and creating a branch is not blocked. See [the guards](docs/reference.md#the-bash-guards).
package/lib/file-card.js CHANGED
@@ -9,7 +9,7 @@
9
9
  // `read`/`edit`/`write` heading in `extensions/quiet-tools.ts`, read read-only, L3),
10
10
  // split so classification and drawing never share a decision: `lib/skill-odd-card.js`
11
11
  // says WHICH card a path wears and which call shape it states (the file's name, the
12
- // range only a read asked for, the Markdown document an expanded read or write draws);
12
+ // range only a read asked for, the Markdown document a read, a write or an edit draws);
13
13
  // this module only dresses that answer.
14
14
  //
15
15
  // What the card never decides:
@@ -206,9 +206,24 @@ function resultText(result) {
206
206
  }
207
207
 
208
208
  /**
209
- * What one result states as rows: an edit is its DIFF (the change the host carries),
210
- * never the file read back as a document; every other call is the text the result
211
- * reported — and a write's document comes from its call, not from here.
209
+ * What a result states as its body. A write states the document its call carried, because
210
+ * its result only reports how many bytes landed. An edit states the fragment its call left
211
+ * behind, which is the same source its Markdown document is drawn from: a host that cannot
212
+ * draw the document falls back to that fragment rather than to the diff, so the card answers
213
+ * one question either way. Every other call states what its result reported, and a FAILED
214
+ * call of any of them still states its error.
215
+ */
216
+ function resultBody(tool, args, result, failed, document) {
217
+ if (failed) return fileResultText(tool, result);
218
+ if (tool === WRITE_TOOL) return writtenContent(args);
219
+ if (tool === EDIT_TOOL && document !== undefined) return document;
220
+ return fileResultText(tool, result);
221
+ }
222
+
223
+ /**
224
+ * What one result states as rows: an edit's DIFF (the change the host carries) when the
225
+ * call leaves no Markdown fragment to draw; every other call is the text the result
226
+ * reported — and a write's or an edit's document comes from its call, not from here.
212
227
  */
213
228
  function fileResultText(tool, result) {
214
229
  const value = asObject(result);
@@ -599,9 +614,10 @@ function fileResultSlot(tool, inherited, seams, shell) {
599
614
  const expanded = optionFlag(options, ctx, 'expanded');
600
615
  const tone = cardTone(failed, partial);
601
616
  // A write states the document its call carried; its result says how many bytes
602
- // landed, which is no body at all. Every other tool states what its result
603
- // reported, and a FAILED write still states its error.
604
- const text = tool === WRITE_TOOL && !failed ? writtenContent(args) : fileResultText(tool, result);
617
+ // landed, which is no body at all. An edit states the fragment its call left, and
618
+ // its diff is the body only when that fragment is nothing.
619
+ const document = markdownDocumentSource(tool, args, resultText(result), { expanded: true, isError: failed });
620
+ const text = resultBody(tool, args, result, failed, document);
605
621
  // One frame: from here on the call above is a heading and this card closes it.
606
622
  markCardResult(ctx.state);
607
623
  return floatRows(tone, paint, width, (inner) => {
@@ -609,11 +625,12 @@ function fileResultSlot(tool, inherited, seams, shell) {
609
625
  // repaints the rows it decides.
610
626
  const rows = fileResultRows({
611
627
  text,
612
- // A read draws the text it read; a write the content its call carries. An
613
- // edit is declined by that rule, because an edit's rows are its diff. The
614
- // document is asked for whatever the expansion — the collapsed card shows
615
- // its first rows (S4), the expanded one the whole of it.
616
- markdown: markdownDocumentSource(tool, args, resultText(result), { expanded: true, isError: failed }),
628
+ // A read draws the text it read, a write the content its call carries and an
629
+ // edit the fragment it leaves; the rows of the three are the diff or the
630
+ // result only when there is no document. The document is asked for whatever
631
+ // the expansion — the collapsed card shows its first rows (S4), the expanded
632
+ // one the whole of it.
633
+ markdown: document,
617
634
  expanded,
618
635
  failed,
619
636
  content: seams.content(),
@@ -2,7 +2,7 @@
2
2
  // T1 (S1): the file a `read`/`edit`/`write` call names — a skill or an ODD artifact —
3
3
  // as the card identity that call heads with. Pure classification and the two call
4
4
  // shapes a file card states: the line range only a read asked for, and the Markdown
5
- // document an expanded read or write draws.
5
+ // document a read, a write or an edit draws.
6
6
  //
7
7
  // The classification is about path components, never substrings: `skills` and `odd`
8
8
  // are folder names, so `lib/skillset/notes.md` and `docs/oddity.md` are ordinary
@@ -147,24 +147,48 @@ function fileCardLineRange(tool, args) {
147
147
  return limit === undefined ? `:${start}` : `:${start}-${start + limit - 1}`;
148
148
  }
149
149
 
150
+ /**
151
+ * The Markdown fragment an `edit` call leaves behind, or undefined when it leaves no text.
152
+ *
153
+ * An edit states its answer as the text it produced, never as the change it made: the
154
+ * harness hands one `edits` array over (one entry per replacement, a legacy top-level
155
+ * pair folded into it), so those entries are read in their OWN order and joined with one
156
+ * newline — two replacements read as the two paragraphs they leave. A call that states
157
+ * no array is the legacy flat pair, whose one `newText` is the fragment. `oldText` is
158
+ * never read, because it names exactly what this document must not show.
159
+ *
160
+ * An entry that states no string is skipped rather than stringified, and a fragment that
161
+ * holds no non-whitespace text is no fragment at all: a pure deletion leaves nothing to
162
+ * draw, and the caller keeps the rows it already has instead of an invented placeholder.
163
+ */
164
+ function editedFragment(args) {
165
+ const call = args !== null && typeof args === 'object' ? args : undefined;
166
+ const entries = Array.isArray(call?.edits) ? call.edits : undefined;
167
+ const stated = entries === undefined ? [call?.newText] : entries.map((entry) => entry?.newText);
168
+ const fragment = stated.filter((text) => typeof text === 'string').join('\n');
169
+ return fragment.trim().length > 0 ? fragment : undefined;
170
+ }
171
+
150
172
  /**
151
173
  * The Markdown document a call draws instead of its result rows, or undefined when the
152
174
  * call draws rows.
153
175
  *
154
176
  * Markdown is the shape a document is written in, not a property of where it lives: an
155
- * expanded read of any `.md` file, and an expanded write to one, render as a document
156
- * whatever folder holds them. Everything else keeps its rows — a collapsed card,
157
- * because the preview is the first rows of the file; a failed call, because its rows
158
- * are the error; an edit, because its rows are the diff; and every non-Markdown file.
159
- * A write draws the content it wrote, which the call carries and the result does not
160
- * echo.
177
+ * expanded read of any `.md` file, an expanded write to one, and an expanded edit of one,
178
+ * render as a document whatever folder holds them. Everything else keeps its rows — a
179
+ * collapsed card, because the preview is the first rows of the file; a failed call,
180
+ * because its rows are the error; a pure deletion, whose `editedFragment` is undefined
181
+ * and whose rows are therefore the diff; and every non-Markdown file. A write draws the
182
+ * content it wrote and an edit the fragment it left, both carried by the call, which the
183
+ * result neither echoes nor states.
161
184
  */
162
185
  function markdownDocumentSource(tool, args, resultText, options) {
163
186
  const settings = options !== null && typeof options === 'object' ? options : {};
164
187
  if (settings.expanded !== true || settings.isError === true) return undefined;
165
- if (tool !== 'read' && tool !== 'write') return undefined;
188
+ if (tool !== 'read' && tool !== 'write' && tool !== 'edit') return undefined;
166
189
  const call = args !== null && typeof args === 'object' ? args : undefined;
167
190
  if (!isMarkdownFile(call?.path)) return undefined;
191
+ if (tool === 'edit') return editedFragment(call);
168
192
  const source = tool === 'write' ? call?.content : resultText;
169
193
  return typeof source === 'string' && source.trim().length > 0 ? source : undefined;
170
194
  }
@@ -179,5 +203,6 @@ module.exports = {
179
203
  fileCardSkillPath,
180
204
  isMarkdownFile,
181
205
  fileCardLineRange,
206
+ editedFragment,
182
207
  markdownDocumentSource,
183
208
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-odd-worktree",
3
- "version": "3.6.0",
3
+ "version": "3.6.1",
4
4
  "description": "Pi package for odd-worktree: the Git worktree tools a Pi session uses (create and list the checkouts the agent works in), the /warp worktree panel, the /odd ODD viewer, and the /issues GitHub issue reader. Needs only Pi and git: no companion app, no extra service.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -1146,21 +1146,178 @@ test('a failed call and a non-Markdown file keep their plain rows (S8)', () => {
1146
1146
  assert.deepEqual(json.slice(0, -1).map(bodyText), ['{"a":1}'], 'a non-Markdown file is drawn as it is');
1147
1147
  });
1148
1148
 
1149
- test('an edit draws its diff and is never rendered as a document', () => {
1150
- const resolver = fileResolver();
1151
- const diff = ['@@ -1,2 +1,2 @@', '-**old**', '+**new**'].join('\n');
1152
- const args = { path: ODD, old_string: '**old**', new_string: '**new**' };
1153
- const collapsed = resultRows(resolver, 'edit', args, textResult('Edited', { details: { diff } }), { isPartial: false }, plainTheme, 60);
1154
- assert.deepEqual(collapsed.slice(0, -1).map(bodyText), ['@@ -1,2 +1,2 @@', '-**old**', '+**new**'], 'the diff is the edit\'s rows');
1155
-
1156
- const expanded = resultRows(resolver, 'edit', args, textResult('Edited', { details: { diff } }), { expanded: true, isPartial: false }, plainTheme, 60);
1157
- const body = expanded.slice(0, -1).map(bodyText).join('\n');
1158
- assert.equal(body, diff, 'expanding an edit shows the whole diff, not a document');
1159
- assert.doesNotMatch(body, /▌/u, 'a Markdown path does not turn an edit into a document');
1160
-
1161
- // A result that carries no diff is its own text.
1162
- const plain = resultRows(resolver, 'edit', args, textResult('Edited 2 lines'), { isPartial: false }, plainTheme, 60);
1163
- assert.deepEqual(plain.slice(0, -1).map(bodyText), ['Edited 2 lines']);
1149
+ // ---- S1/S2/S3/S4/S5/S6: an edit states the fragment it leaves, drawn as a document ----
1150
+
1151
+ // The two shapes an `edit` call arrives in: the `edits` array the harness folds a legacy
1152
+ // pair into (one entry per replacement), and the flat top-level pair itself.
1153
+ const EDITS = [{ oldText: '# Old heading', newText: '# Handoff\n\nBody text' }, { oldText: 'x', newText: '```\nconst a = 1;\n```' }];
1154
+ const FRAGMENT = ['# Handoff', '', 'Body text', '```', 'const a = 1;', '```'].join('\n');
1155
+ // The host's own rows for that call: a hunk header, the removed lines and the added ones.
1156
+ const EDIT_DIFF = ['@@ -2,3 +2,3 @@', '-# Old heading', '-removed line', '+# Handoff', '+Body text'].join('\n');
1157
+ const editResult = () => textResult('Edited', { details: { diff: EDIT_DIFF } });
1158
+
1159
+ test('an edit of a Markdown skill or ODD artifact draws the fragment it leaves (S1)', () => {
1160
+ const resolver = documentResolver();
1161
+ const rows = resultRows(resolver, 'edit', { path: ODD, edits: EDITS }, editResult(), { expanded: true, isPartial: false }, taggedTheme, 60);
1162
+ const body = rows.slice(0, -1).join('\n');
1163
+ // The fragment is a document, drawn in the renderer's own roles.
1164
+ assert.equal(body.includes('<mdHeading>Handoff</mdHeading>'), true, `the fragment's heading is drawn as a heading: ${body}`);
1165
+ assert.equal(body.includes('<mdCodeBlock>const a = 1;</mdCodeBlock>'), true, 'so is its fenced block');
1166
+ // None of the host's diff rows are left: no hunk header, no gutter, no `+`/`-` marker,
1167
+ // and no line the edit removed.
1168
+ const text = unpaint(body);
1169
+ assert.equal(text.includes('@@'), false, 'no hunk header');
1170
+ assert.equal(text.includes('removed line'), false, 'a line the edit removed is never shown');
1171
+ assert.equal(text.includes('-# Old heading'), false, 'no removed marker');
1172
+ assert.equal(text.includes('+# Handoff'), false, 'no added marker');
1173
+ assert.doesNotMatch(text, /^\s*\d+ │/mu, 'no line-number gutter');
1174
+ // A host diff that carries a gutter row of its own reaches the body no more than the
1175
+ // rest of it does: no digit ever stands beside the diff's own bar.
1176
+ const gutted = resultRows(
1177
+ resolver,
1178
+ 'edit',
1179
+ { path: ODD, edits: EDITS },
1180
+ textResult('Edited', { details: { diff: ['@@ -1,1 +1,1 @@', ' 12 │ -old line', ' 12 │ +new line'].join('\n') } }),
1181
+ { expanded: true, isPartial: false },
1182
+ taggedTheme,
1183
+ 60,
1184
+ );
1185
+ assert.doesNotMatch(unpaint(gutted.slice(0, -1).join('\n')), /\d+\s*│|│\s*\d+/u, 'a gutter row is never drawn');
1186
+ // The same rows a read of the resulting text draws, exactly.
1187
+ const read = resultRows(resolver, 'read', { path: ODD }, textResult(FRAGMENT), { expanded: true, isPartial: false }, taggedTheme, 60);
1188
+ assert.deepEqual(rows, read, 'the fragment is drawn exactly as a read of it is');
1189
+
1190
+ // The legacy flat shape is the same call, so it draws the same fragment.
1191
+ const flat = resultRows(
1192
+ resolver,
1193
+ 'edit',
1194
+ { path: ODD, oldText: '# Old heading', newText: FRAGMENT },
1195
+ editResult(),
1196
+ { expanded: true, isPartial: false },
1197
+ taggedTheme,
1198
+ 60,
1199
+ );
1200
+ assert.deepEqual(flat, rows, 'either call shape states the same fragment');
1201
+
1202
+ // A skill's Markdown file is the same card: the claim is the identity, not the tool. The
1203
+ // rows must be the ones a read of that same fragment on that same skill path draws, so a
1204
+ // skill card that fell back to its raw source, or to the diff, cannot pass.
1205
+ const skillEdit = resultRows(resolver, 'edit', { path: SKILL, edits: EDITS }, editResult(), { expanded: true, isPartial: false }, taggedTheme, 60);
1206
+ const skillRead = resultRows(resolver, 'read', { path: SKILL }, textResult(FRAGMENT), { expanded: true, isPartial: false }, taggedTheme, 60);
1207
+ assert.deepEqual(skillEdit, skillRead, 'a skill edit draws exactly a read of its fragment');
1208
+ const skillBody = skillEdit.slice(0, -1).join('\n');
1209
+ const skillText = unpaint(skillBody);
1210
+ assert.equal(skillBody.includes('<mdHeading>Handoff</mdHeading>'), true, 'in the renderer\'s own roles');
1211
+ assert.equal(skillBody.includes('<mdCodeBlock>const a = 1;</mdCodeBlock>'), true, 'fenced block included');
1212
+ assert.equal(skillText.includes('@@'), false, 'and never the diff');
1213
+ assert.equal(skillText.includes('-# Old heading'), false, 'no removed marker');
1214
+ assert.equal(skillText.includes('+# Handoff'), false, 'no added marker');
1215
+ assert.doesNotMatch(skillText, /\d+\s*│|│\s*\d+/u, 'no line-number gutter');
1216
+ });
1217
+
1218
+ // The array is the whole source when it is present: only an ABSENT or non-array `edits`
1219
+ // falls back to the legacy pair, so a pair beside an empty array states nothing.
1220
+ test('an `edits` array, even an empty one, is the whole source (S2)', () => {
1221
+ const resolver = documentResolver();
1222
+ const args = { path: ODD, edits: [], newText: '# Competing legacy text' };
1223
+ const rows = resultRows(resolver, 'edit', args, editResult(), { expanded: true, isPartial: false }, taggedTheme, 60);
1224
+ assert.deepEqual(rows.slice(0, -1).map(bodyText), EDIT_DIFF.split('\n'), 'the diff rows, not the legacy fragment beside the array');
1225
+ assert.equal(rows.slice(0, -1).join('\n').includes('Competing legacy text'), false, 'the pair is not drawn at all');
1226
+ });
1227
+
1228
+ test('a pure deletion keeps the edit\'s rows, with no invented placeholder (S3)', () => {
1229
+ const resolver = documentResolver();
1230
+ // Longer than the preview budget, so the collapsed and the expanded case draw different
1231
+ // amounts of the same diff.
1232
+ const lines = ['@@ -1,4 +1,0 @@', '-# Design', '-Body text', '-More body', '-Last body'];
1233
+ const result = textResult('Edited', { details: { diff: lines.join('\n') } });
1234
+ for (const edits of [[{ oldText: '# Design\nBody text', newText: '' }], [{ oldText: 'a', newText: ' \n ' }]]) {
1235
+ const collapsed = resultRows(resolver, 'edit', { path: ODD, edits }, result, { isPartial: false }, taggedTheme, 60);
1236
+ assert.deepEqual(collapsed.slice(0, -1).map(bodyText), lines.slice(0, PREVIEW_ROWS), 'collapsed: the first rows of the diff, capped as any body is');
1237
+ const expanded = resultRows(resolver, 'edit', { path: ODD, edits }, result, { expanded: true, isPartial: false }, taggedTheme, 60);
1238
+ assert.deepEqual(expanded.slice(0, -1).map(bodyText), lines, 'expanded: the whole diff, row for row, never a document');
1239
+ assert.equal(bodyPaint(expanded.slice(0, -1).join('\n')).includes('<mdHeading>'), false, 'and none of it is drawn as Markdown');
1240
+ }
1241
+ // A result that carries no diff is its own text, exactly as before.
1242
+ const plain = resultRows(resolver, 'edit', { path: ODD, edits: [{ newText: '' }] }, textResult('Edited 2 lines'), { isPartial: false }, plainTheme, 60);
1243
+ assert.deepEqual(plain.slice(0, -1).map(bodyText), ['Edited 2 lines'], 'no wording is invented for the deleted fragment');
1244
+ });
1245
+
1246
+ test('a failed edit keeps its error rows, and no other path becomes a document (S4)', () => {
1247
+ const resolver = documentResolver();
1248
+ const failed = resultRows(resolver, 'edit', { path: ODD, edits: EDITS }, textResult('ENOENT: no such file', { isError: true }), { expanded: true, isPartial: false }, taggedTheme, 60);
1249
+ assert.deepEqual(failed.slice(0, -1).map(bodyText), ['ENOENT: no such file'], 'exactly the error, with no body row of its own');
1250
+ const failedBody = failed.slice(0, -1).join('\n');
1251
+ assert.equal(failedBody.includes('<mdHeading>'), false, 'a failed edit is never a document');
1252
+ assert.match(failed[0], /^<error>│/u, 'and it wears the error role');
1253
+
1254
+ // A non-Markdown ODD artifact keeps its diff.
1255
+ const json = resultRows(resolver, 'edit', { path: 'odd/tasks/data.json', edits: [{ newText: '## Handoff' }] }, editResult(), { isPartial: false }, plainTheme, 60);
1256
+ assert.deepEqual(json.slice(0, -1).map(bodyText), ['@@ -2,3 +2,3 @@', '-# Old heading', '-removed line'], 'a non-Markdown path keeps its diff rows');
1257
+
1258
+ // A path with no skill or ODD identity is no row of this card at all: the chain keeps it.
1259
+ const forwarded = resolver('edit', () => HOST_EDIT)
1260
+ .renderResult(editResult(), { isPartial: false }, plainTheme, { args: { path: 'docs/notes.md', edits: EDITS } })
1261
+ .render(60);
1262
+ assert.deepEqual(forwarded, ['host edit result'], 'an ordinary Markdown file is the chain\'s row');
1263
+ });
1264
+
1265
+ test('without a Markdown renderer the edit body is the same fragment, never the diff (S6)', () => {
1266
+ const drawsNothing = class { render() { return []; } invalidate() {} };
1267
+ const throws = class { render() { throw new Error('renderer exploded'); } invalidate() {} };
1268
+ const cases = [['no renderer', null], ['a renderer that throws', throws], ['a renderer that draws nothing', drawsNothing]];
1269
+ // The fragment's own lines are all such a host can draw: its first PREVIEW_ROWS collapsed,
1270
+ // every one of them expanded. Asserted exactly, so a dropped heading or fence row fails.
1271
+ const lines = FRAGMENT.split('\n');
1272
+ for (const [label, markdownClass] of cases) {
1273
+ const resolver = fileResolver({ markdownClass });
1274
+ const args = { path: ODD, edits: EDITS };
1275
+ for (const options of [{ isPartial: false }, { expanded: true, isPartial: false }]) {
1276
+ const rows = resultRows(resolver, 'edit', args, editResult(), options, taggedTheme, 60);
1277
+ const expected = options.expanded === true ? lines : lines.slice(0, PREVIEW_ROWS);
1278
+ assert.deepEqual(rows.slice(0, -1).map(bodyText), expected, `${label}: every fragment row it draws, in order`);
1279
+ assert.match(unpaint(rows.at(-1)), /^╰.*╯$/u, `${label}: and its own closing rule`);
1280
+ assert.equal(bodyPaint(rows.slice(0, -1).join('\n')).includes('<mdHeading>'), false, `${label}: as plain source, in no Markdown role`);
1281
+ }
1282
+ }
1283
+ });
1284
+
1285
+ test('a collapsed edit previews the fragment exactly like a write of it (S5)', () => {
1286
+ const resolver = documentResolver();
1287
+ const args = { path: ODD, edits: [{ newText: DOCUMENT }] };
1288
+ const collapsed = resultRows(resolver, 'edit', args, editResult(), { isPartial: false }, taggedTheme, 60);
1289
+ const write = resultRows(resolver, 'write', { path: ODD, content: DOCUMENT }, textResult('Wrote 42 bytes'), { isPartial: false }, taggedTheme, 60);
1290
+ assert.equal(collapsed.length, PREVIEW_ROWS + 1, 'three preview rows and the closing rule');
1291
+ assert.deepEqual(collapsed.slice(0, PREVIEW_ROWS), write.slice(0, PREVIEW_ROWS), 'the same preview a write of the document draws');
1292
+ const preview = collapsed.slice(0, PREVIEW_ROWS).join('\n');
1293
+ assert.equal(preview.includes('<mdHeading>Design</mdHeading>'), true, `the preview starts at the fragment's top: ${preview}`);
1294
+ assert.equal(preview.includes('<mdCodeBlock>const a = 1;</mdCodeBlock>'), false, 'the preview stops at its budget');
1295
+
1296
+ // Expanded, all of it: exactly the rows a write of the same document draws, so a row
1297
+ // dropped from the middle cannot pass, and every drawn row of the document is there.
1298
+ const expanded = resultRows(resolver, 'edit', args, editResult(), { expanded: true, isPartial: false }, taggedTheme, 60);
1299
+ const expandedWrite = resultRows(resolver, 'write', { path: ODD, content: DOCUMENT }, textResult('Wrote 42 bytes'), { expanded: true, isPartial: false }, taggedTheme, 60);
1300
+ assert.deepEqual(expanded, expandedWrite, 'expanding an edit is the whole document, row for row');
1301
+ assert.ok(expanded.length > PREVIEW_ROWS + 1, 'and it is more than the collapsed preview draws');
1302
+ assert.equal(unpaint(expanded.slice(0, -1).join('\n')).includes('@@'), false, 'and no diff row anywhere');
1303
+
1304
+ // `minimal` draws the call alone: the heading above it is still the call's, and the
1305
+ // result is its closing rule with no body under it.
1306
+ globalThis[CARD_CONTENT_SLOT] = 'minimal';
1307
+ try {
1308
+ const call = callRows(resolver, 'edit', args, taggedTheme, 60);
1309
+ assert.equal(call.length, 1, 'the call is its one heading row at either level');
1310
+ assert.match(unpaint(call[0]), /📑 odd/u, 'and it still heads as the ODD artifact');
1311
+ assert.match(unpaint(call[0]), /edit/u, 'naming the tool');
1312
+ assert.match(unpaint(call[0]), /cards-skill-odd\.md/u, 'and the file');
1313
+ const minimal = resultRows(resolver, 'edit', args, editResult(), { isPartial: false }, taggedTheme, 60);
1314
+ assert.equal(minimal.length, 1, 'the closing rule alone');
1315
+ assert.match(unpaint(minimal[0]), /^╰.*╯$/u, 'with no body above it');
1316
+ assert.doesNotMatch(unpaint(minimal[0]), /Design|quoted|```/u, 'and nothing of the document on it');
1317
+ assertFramed(minimal, 60);
1318
+ } finally {
1319
+ delete globalThis[CARD_CONTENT_SLOT];
1320
+ }
1164
1321
  });
1165
1322
 
1166
1323
  test('a failed result keeps its error rows and the error tone', () => {
@@ -14,6 +14,7 @@ const {
14
14
  FILE_CARD_IDENTITY,
15
15
  ODD_FILE_CARD,
16
16
  SKILL_FILE_CARD,
17
+ editedFragment,
17
18
  fileCardIdentity,
18
19
  fileCardLineRange,
19
20
  fileCardName,
@@ -175,19 +176,31 @@ test('a read names the line range it asked for, and no other tool states one', (
175
176
  assert.equal(fileCardLineRange('read', undefined), undefined);
176
177
  });
177
178
 
179
+ // The fragment source itself, as the one answer both the document and its plain fallback
180
+ // are drawn from.
181
+ test('the fragment an edit leaves is every `newText` it states, and nothing else', () => {
182
+ assert.equal(editedFragment({ edits: [{ newText: 'a' }, { newText: 'b' }] }), 'a\nb');
183
+ assert.equal(editedFragment({ newText: 'a' }), 'a');
184
+ assert.equal(editedFragment({ edits: [{ oldText: 'gone', newText: '' }] }), undefined);
185
+ assert.equal(editedFragment(undefined), undefined);
186
+ });
187
+
178
188
  // A Markdown document is drawn as a document only when expanded and successful; a read
179
- // draws what it returned, a write what its call carried, and nothing to draw keeps the rows.
180
- test('only an expanded, successful read or write of a Markdown file draws a document', () => {
189
+ // draws what it returned, a write what its call carried, an edit the fragment it leaves
190
+ // behind, and nothing to draw keeps the rows.
191
+ test('an expanded, successful read, write or edit of a Markdown file draws a document', () => {
181
192
  const markdown = '## Handoff\n\nBody.';
182
193
  const draw = (tool, args, result, options = {}) =>
183
194
  markdownDocumentSource(tool, args, result, { expanded: true, ...options });
184
195
  assert.equal(draw('read', { path: 'docs/a.md' }, markdown), markdown);
185
196
  assert.equal(draw('write', { path: 'docs/a.md', content: markdown }, 'Successfully wrote 20 bytes'), markdown);
197
+ // An edit's document is the text it leaves behind, never what it removed or its diff.
198
+ assert.equal(draw('edit', { path: 'docs/a.md', oldText: 'a', newText: markdown }, 'Applied'), markdown);
186
199
  // Collapsed, failed, non-Markdown and every other tool keep their rows.
187
200
  assert.equal(draw('read', { path: 'docs/a.md' }, markdown, { expanded: false }), undefined);
188
201
  assert.equal(draw('read', { path: 'docs/a.md' }, markdown, { isError: true }), undefined);
189
202
  assert.equal(draw('write', { path: 'docs/a.md', content: markdown }, 'ok', { isError: true }), undefined);
190
- assert.equal(draw('edit', { path: 'docs/a.md', oldText: 'a', newText: markdown }, 'Applied'), undefined);
203
+ assert.equal(draw('edit', { path: 'docs/a.md', oldText: 'a', newText: markdown }, 'Applied', { isError: true }), undefined);
191
204
  assert.equal(draw('grep', { path: 'docs/a.md' }, markdown), undefined);
192
205
  assert.equal(draw('read', { path: 'docs/a.txt' }, markdown), undefined);
193
206
  assert.equal(draw('write', { path: 'lib/x.ts', content: markdown }, 'ok'), undefined);
@@ -198,3 +211,32 @@ test('only an expanded, successful read or write of a Markdown file draws a docu
198
211
  assert.equal(draw('write', { path: 'docs/a.md' }, 'Successfully wrote 0 bytes'), undefined);
199
212
  assert.equal(draw('read', undefined, markdown), undefined);
200
213
  });
214
+
215
+ // An edit's document is the fragment its call leaves behind: every `newText` it states, in
216
+ // the call's own order, and nothing it removed.
217
+ test('an edit draws the fragment it leaves behind, in either call shape', () => {
218
+ const draw = (args, options = {}) => markdownDocumentSource('edit', args, 'Applied', { expanded: true, ...options });
219
+ // The shape the harness hands over: one `edits` array, joined in its own order.
220
+ assert.equal(
221
+ draw({ path: 'odd/a.md', edits: [{ oldText: 'one', newText: '# One' }, { oldText: 'two', newText: '## Two' }] }),
222
+ '# One\n## Two',
223
+ );
224
+ assert.equal(draw({ path: 'odd/a.md', edits: [{ newText: 'b' }, { newText: 'a' }] }), 'b\na', 'the array order, never a sort');
225
+ // The legacy flat shape: the one pair at the top level.
226
+ assert.equal(draw({ path: 'odd/a.md', oldText: 'one', newText: '# One' }), '# One');
227
+ assert.equal(draw({ path: 'odd/a.md', edits: 'not an array', newText: '# One' }), '# One', 'an `edits` that is not an array is the flat shape');
228
+ // An entry that states no string is skipped, never stringified: the fragment is text.
229
+ assert.equal(
230
+ draw({ path: 'odd/a.md', edits: [{ newText: 7 }, null, 'text', { newText: '# One' }, {}, { newText: ['x'] }] }),
231
+ '# One',
232
+ );
233
+ // A pure deletion leaves no fragment, so there is no document and the rows stay.
234
+ assert.equal(draw({ path: 'odd/a.md', edits: [{ oldText: '# Gone', newText: '' }] }), undefined);
235
+ assert.equal(draw({ path: 'odd/a.md', edits: [{ oldText: '# Gone', newText: ' \n ' }] }), undefined);
236
+ assert.equal(draw({ path: 'odd/a.md', oldText: '# Gone' }), undefined, 'no newText states nothing');
237
+ // A collapsed or failed edit, and a path that is no Markdown, keep their rows too.
238
+ assert.equal(draw({ path: 'odd/a.md', edits: [{ newText: '# One' }] }, { expanded: false }), undefined);
239
+ assert.equal(draw({ path: 'odd/a.json', edits: [{ newText: '# One' }] }), undefined);
240
+ // Nothing to read is no document, and no throw either.
241
+ assert.equal(draw(undefined), undefined);
242
+ });