pi-odd-worktree 2.1.0 → 3.0.0

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.
Files changed (160) hide show
  1. package/README.md +54 -319
  2. package/extensions/odd-worktree-tools.ts +4145 -2529
  3. package/lib/bash-activity.js +1164 -0
  4. package/lib/bash-design.js +684 -0
  5. package/lib/bash-destructive.js +559 -0
  6. package/lib/bash-frame.js +205 -0
  7. package/lib/card-content.js +23 -0
  8. package/lib/card-style.js +175 -0
  9. package/lib/checkout-facts.js +30 -70
  10. package/lib/codegraph-card-preview.js +628 -0
  11. package/lib/codegraph-card.js +515 -0
  12. package/lib/codegraph-compaction.js +76 -0
  13. package/lib/codegraph-daemon.js +246 -0
  14. package/lib/codegraph-edit-sync.js +182 -0
  15. package/lib/codegraph-guard-target.js +337 -0
  16. package/lib/codegraph-guard.js +211 -0
  17. package/lib/codegraph-index.js +1020 -0
  18. package/lib/codegraph-lock.js +607 -0
  19. package/lib/codegraph-mcp-config.js +134 -0
  20. package/lib/codegraph-mcp.js +123 -0
  21. package/lib/codegraph-paths.js +69 -0
  22. package/lib/codegraph-prompt/explore.md +11 -0
  23. package/lib/codegraph-prompt/fix.md +6 -0
  24. package/lib/codegraph-prompt/generic.md +8 -0
  25. package/lib/codegraph-prompt/judge.md +9 -0
  26. package/lib/codegraph-prompt/orchestrator.md +17 -0
  27. package/lib/codegraph-prompt/verify.md +10 -0
  28. package/lib/codegraph-prompt/worker.md +7 -0
  29. package/lib/codegraph-role.js +55 -0
  30. package/lib/codegraph-rules.js +146 -0
  31. package/lib/codegraph-session.js +303 -383
  32. package/lib/codegraph-warmup.js +139 -0
  33. package/lib/file-card-markdown.js +141 -0
  34. package/lib/file-card-resolver.js +245 -0
  35. package/lib/file-card.js +659 -0
  36. package/lib/files-index.js +387 -0
  37. package/lib/files-insert.js +81 -0
  38. package/lib/files-orders.js +326 -0
  39. package/lib/files-picker-view.js +1416 -0
  40. package/lib/files-preview.js +338 -0
  41. package/lib/files-scope-detail.js +171 -0
  42. package/lib/files-scope-view.js +507 -0
  43. package/lib/gh-issues.js +308 -14
  44. package/lib/issue-preview.js +26 -7
  45. package/lib/issues-bash-mutation.js +206 -0
  46. package/lib/issues-cache-store.js +261 -0
  47. package/lib/issues-section.js +710 -0
  48. package/lib/issues-view.js +649 -52
  49. package/lib/odd-prompt/odd-worktrees.md +21 -0
  50. package/lib/odd-worktrees-prompt.js +10 -28
  51. package/lib/related-issues-cache.js +210 -0
  52. package/lib/shell-card.js +825 -0
  53. package/lib/skill-odd-card.js +183 -0
  54. package/lib/tui-paint.js +4 -5
  55. package/lib/warp-create-panel.js +538 -250
  56. package/lib/warp-create-picker.js +56 -9
  57. package/lib/warp-delete-confirm.js +19 -28
  58. package/lib/warp-selector-view.js +160 -82
  59. package/lib/which-command.js +90 -0
  60. package/lib/worktree-card.js +267 -897
  61. package/lib/worktree-cards.js +383 -0
  62. package/lib/worktree-create.js +287 -0
  63. package/lib/worktree-git.js +3 -3
  64. package/lib/worktree-guard.js +548 -0
  65. package/lib/worktree-hooks.js +421 -712
  66. package/lib/worktree-list.js +120 -0
  67. package/lib/worktree-registry.js +162 -879
  68. package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
  69. package/lib/worktree-setup-runner.js +376 -0
  70. package/lib/worktree-setup.js +140 -0
  71. package/lib/worktree-warp-busy.js +462 -85
  72. package/lib/worktree-warp-sessions.js +1 -2
  73. package/package.json +2 -2
  74. package/tools/bash-activity.test.js +1419 -0
  75. package/tools/bash-design.test.js +1091 -0
  76. package/tools/bash-destructive.test.js +139 -0
  77. package/tools/card-content.test.js +44 -0
  78. package/tools/card-style.test.js +298 -0
  79. package/tools/codegraph-card-preview.test.js +254 -0
  80. package/tools/codegraph-card.test.js +1080 -0
  81. package/tools/codegraph-compaction.test.js +378 -0
  82. package/tools/codegraph-daemon.test.js +248 -0
  83. package/tools/codegraph-edit-sync.test.js +295 -0
  84. package/tools/codegraph-guard-target.test.js +174 -0
  85. package/tools/codegraph-guard.test.js +238 -0
  86. package/tools/codegraph-index.test.js +1948 -0
  87. package/tools/codegraph-lock.test.js +932 -0
  88. package/tools/codegraph-mcp-config.test.js +181 -0
  89. package/tools/codegraph-mcp.test.js +194 -0
  90. package/tools/codegraph-paths.test.js +44 -0
  91. package/tools/codegraph-role.test.js +30 -0
  92. package/tools/codegraph-rules.test.js +250 -0
  93. package/tools/codegraph-session.test.js +660 -391
  94. package/tools/codegraph-warmup.test.js +316 -0
  95. package/tools/commands.test.js +510 -31
  96. package/tools/file-card-markdown.test.js +188 -0
  97. package/tools/file-card.test.js +1289 -0
  98. package/tools/files-bridge.test.js +575 -0
  99. package/tools/files-command.test.js +610 -0
  100. package/tools/files-index.test.js +473 -0
  101. package/tools/files-insert.test.js +139 -0
  102. package/tools/files-orders.test.js +597 -0
  103. package/tools/files-picker-view.test.js +1871 -0
  104. package/tools/files-preview.test.js +452 -0
  105. package/tools/files-scope-detail.test.js +264 -0
  106. package/tools/files-scope-view.test.js +614 -0
  107. package/tools/fixtures/card-codegraph-outputs.js +110 -0
  108. package/tools/fixtures/codegraph-cli-stub.js +16 -0
  109. package/tools/fixtures/codegraph-init-process.js +28 -0
  110. package/tools/fixtures/codegraph-lock-worker.js +117 -0
  111. package/tools/fixtures/daemon-fake.js +98 -0
  112. package/tools/fixtures/daemon-mcp-server.js +39 -0
  113. package/tools/fixtures/daemon-warmup-worker.js +12 -0
  114. package/tools/fixtures/load-extension.js +136 -3
  115. package/tools/gh-issues-feedback.test.js +31 -7
  116. package/tools/gh-issues.test.js +391 -1
  117. package/tools/issue-preview.test.js +44 -7
  118. package/tools/issues-bash-mutation.test.js +101 -0
  119. package/tools/issues-cache-store.test.js +222 -0
  120. package/tools/issues-command.test.js +1512 -66
  121. package/tools/issues-open-guard.test.js +1096 -0
  122. package/tools/issues-section.test.js +4195 -0
  123. package/tools/issues-seed-query-regression.test.js +67 -0
  124. package/tools/issues-view.test.js +1489 -60
  125. package/tools/odd-worktrees-prompt.test.js +98 -65
  126. package/tools/package-surface.test.js +431 -36
  127. package/tools/related-issues-cache.test.js +324 -0
  128. package/tools/shell-card.test.js +834 -0
  129. package/tools/skill-odd-card.test.js +200 -0
  130. package/tools/t5-session-identity.test.js +44 -22
  131. package/tools/warp-busy-guard.test.js +2565 -309
  132. package/tools/warp-create-panel.test.js +499 -74
  133. package/tools/warp-create-picker.test.js +101 -5
  134. package/tools/warp-delete-confirm.test.js +49 -15
  135. package/tools/warp-empty-session.test.js +0 -1
  136. package/tools/warp-fallback-regression.test.js +533 -0
  137. package/tools/warp-selector-view.test.js +460 -196
  138. package/tools/which-command.test.js +126 -0
  139. package/tools/worktree-card.test.js +551 -0
  140. package/tools/worktree-cards.test.js +845 -0
  141. package/tools/worktree-changes.test.js +4 -5
  142. package/tools/worktree-create.test.js +489 -0
  143. package/tools/worktree-guard.test.js +823 -0
  144. package/tools/worktree-hooks.test.js +905 -0
  145. package/tools/worktree-list.test.js +170 -0
  146. package/tools/worktree-registry.test.js +89 -230
  147. package/tools/worktree-remove-async.test.js +318 -0
  148. package/tools/worktree-setup-runner.test.js +276 -0
  149. package/tools/worktree-setup.test.js +140 -0
  150. package/tools/worktree-tools.test.js +419 -199
  151. package/lib/codegraph-commands.js +0 -165
  152. package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
  153. package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
  154. package/lib/worktree-model-result.js +0 -291
  155. package/tools/codegraph-commands.test.js +0 -371
  156. package/tools/herdr-free-create.test.js +0 -161
  157. package/tools/worktree-close-herdr-async.test.js +0 -204
  158. package/tools/worktree-odd-migration.test.js +0 -224
  159. package/tools/worktree-registry-cache.test.js +0 -163
  160. package/tools/worktree-registry-odd.test.js +0 -231
@@ -0,0 +1,533 @@
1
+ 'use strict';
2
+ // D-A: a card's text is measured, wrapped and clipped BEFORE it is painted.
3
+ //
4
+ // The two card families (`lib/worktree-card.js`'s `/warp` message card and
5
+ // `lib/worktree-cards.js`'s tool cards) draw on the one substrate of
6
+ // `lib/shell-card.js`. That substrate hands a body row to the injected text seam
7
+ // and then paints it; a row that is painted FIRST is measured and clipped a
8
+ // second time as if every escape character were a column. With the host's
9
+ // ANSI-aware helpers that only costs a redundant pass. With the package's own
10
+ // fallback (`createWarpMessageRenderer()` with no helpers: an ANSI-aware
11
+ // `visibleWidth` beside an ANSI-UNAWARE `truncateToWidth`) it clips INTO the
12
+ // escape sequence: text is lost and a partial SGR is emitted onto the terminal.
13
+ //
14
+ // This suite pins the difference the way an independent verifier measured it:
15
+ // * every fixture is rendered through the ANSI-aware helpers and through the
16
+ // fallback, and the two must show the SAME text at the same width;
17
+ // * no row may contain an escape character that is not part of a complete SGR
18
+ // pair, on either path;
19
+ // * the reported width-40 row is pinned literally, as the baseline drew it;
20
+ // * the tool cards get the mirror: an ANSI-unaware clipping helper substituted
21
+ // for the real one (the same latent order, only hidden today by the host's
22
+ // ANSI-aware metrics) must not change the text either.
23
+ //
24
+ // The strongest case is structural, and it is the one the fix is named after: no
25
+ // helper is ever handed a painted string. The recording seam below fails the
26
+ // suite the moment one is.
27
+
28
+ const { test, before, after } = require('node:test');
29
+ const assert = require('node:assert/strict');
30
+ const { mkdtempSync, rmSync } = require('node:fs');
31
+ const { tmpdir } = require('node:os');
32
+ const { join } = require('node:path');
33
+
34
+ const { createWarpMessageRenderer, localTruncate } = require('../lib/worktree-card.js');
35
+ const { createWorktreeCards } = require('../lib/worktree-cards.js');
36
+
37
+ // ---- the style seam: the process-wide slot, and a scratch preference home ----
38
+
39
+ const CARD_STYLE_SLOT = Symbol.for('gentle-pi.card-style');
40
+
41
+ function useStyle(style) {
42
+ if (style === undefined) delete globalThis[CARD_STYLE_SLOT];
43
+ else globalThis[CARD_STYLE_SLOT] = style;
44
+ }
45
+
46
+ function withStyle(style, run) {
47
+ useStyle(style);
48
+ try {
49
+ return run();
50
+ } finally {
51
+ useStyle(undefined);
52
+ }
53
+ }
54
+
55
+ let configHome;
56
+ const savedHome = process.env.GENTLE_PI_CONFIG_HOME;
57
+
58
+ before(() => {
59
+ // The renderer reads the shell's slot first and this document second; an empty
60
+ // scratch home keeps the real user's preference out of the suite.
61
+ configHome = mkdtempSync(join(tmpdir(), 'odd-warp-fallback-'));
62
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
63
+ useStyle(undefined);
64
+ });
65
+
66
+ after(() => {
67
+ useStyle(undefined);
68
+ if (savedHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
69
+ else process.env.GENTLE_PI_CONFIG_HOME = savedHome;
70
+ rmSync(configHome, { recursive: true, force: true });
71
+ });
72
+
73
+ // ---- the text seam, in its two honest shapes ----
74
+
75
+ const ANSI = /\x1b\[[0-9;]*m/g;
76
+ const stripAnsi = (value) => String(value).replace(ANSI, '');
77
+ // The fixtures are ASCII, so one code point is one column.
78
+ const columns = (value) => [...stripAnsi(value)].length;
79
+
80
+ /**
81
+ * The host's helpers, ANSI aware: an escape never counts as a column, and
82
+ * clipping measures what is left after the escapes are ignored. Colour is
83
+ * dropped by the clip (the host re-applies it); the TEXT is what this suite
84
+ * compares.
85
+ */
86
+ const ansiAware = {
87
+ visibleWidth: (value) => columns(value),
88
+ truncateToWidth(value, width, ellipsis = '') {
89
+ const plain = [...stripAnsi(value)];
90
+ const tail = String(ellipsis ?? '');
91
+ if (plain.length <= width) return value;
92
+ const keep = Math.max(0, width - [...tail].length);
93
+ return plain.slice(0, keep).join('') + tail;
94
+ },
95
+ wrapTextWithAnsi(value, width) {
96
+ const budget = Math.max(1, Math.floor(Number(width) || 0));
97
+ const plain = [...stripAnsi(value)];
98
+ if (plain.length <= budget) return [stripAnsi(value)];
99
+ const rows = [];
100
+ for (let index = 0; index < plain.length; index += budget) rows.push(plain.slice(index, index + budget).join(''));
101
+ return rows;
102
+ },
103
+ };
104
+
105
+ /**
106
+ * The same seam with ANSI-UNWARE clipping, which is exactly the pairing the
107
+ * package's own fallback uses (`lib/worktree-card.js`'s local `visibleWidth`
108
+ * beside its local `truncateToWidth`): the measurement ignores escapes, the
109
+ * clip counts every character of them as a column.
110
+ */
111
+ const countsEscapes = {
112
+ visibleWidth: (value) => columns(value),
113
+ truncateToWidth(value, width, ellipsis = '') {
114
+ if (columns(value) <= width) return value;
115
+ if (width <= 0) return '';
116
+ const tail = String(ellipsis ?? '');
117
+ const mark = [...tail].length >= width ? '' : tail;
118
+ const budget = width - [...mark].length;
119
+ let used = 0;
120
+ let out = '';
121
+ for (const char of String(value)) {
122
+ if (used + 1 > budget) break;
123
+ out += char;
124
+ used += 1;
125
+ }
126
+ return out + mark;
127
+ },
128
+ wrapTextWithAnsi(value, width) {
129
+ const budget = Math.max(1, Math.floor(Number(width) || 0));
130
+ const all = [...String(value)];
131
+ if (all.length <= budget) return [String(value)];
132
+ const rows = [];
133
+ for (let index = 0; index < all.length; index += budget) rows.push(all.slice(index, index + budget).join(''));
134
+ return rows;
135
+ },
136
+ };
137
+
138
+ // ---- one theme that really paints, so a clipped escape is observable ----
139
+
140
+ const ROLE_COLOUR = {
141
+ text: 250,
142
+ warning: 214,
143
+ success: 40,
144
+ error: 196,
145
+ border: 244,
146
+ accent: 213,
147
+ dim: 240,
148
+ muted: 245,
149
+ };
150
+ const BG_COLOUR = { toolSuccessBg: 22, toolPendingBg: 58, toolErrorBg: 52 };
151
+ const BG_RESET = '\x1b[49m';
152
+
153
+ function ansiTheme() {
154
+ return {
155
+ fg(role, value) {
156
+ const colour = ROLE_COLOUR[role] ?? 250;
157
+ return `\x1b[38;5;${colour}m${value}\x1b[0m`;
158
+ },
159
+ bg(role, value) {
160
+ const colour = BG_COLOUR[role] ?? 236;
161
+ return `\x1b[48;5;${colour}m${value}${BG_RESET}`;
162
+ },
163
+ };
164
+ }
165
+
166
+ /** Rows as the reader sees them: complete SGR pairs removed, nothing else. */
167
+ const textRows = (rows) => rows.map((row) => stripAnsi(row));
168
+
169
+ /** Every row is inside the width, and no incomplete escape sequence survives. */
170
+ function assertClean(rows, width, label) {
171
+ for (const row of rows) {
172
+ // A complete SGR pair is the theme's own paint, and expected. What must never
173
+ // reach the terminal is the LEFTOVER of a sequence a clip cut in half, so the
174
+ // check is made after the complete pairs are removed.
175
+ const leftover = row.replace(ANSI, '');
176
+ assert.equal(leftover.includes('\x1b'), false, `${label}: a partial escape sequence reached the terminal in ${JSON.stringify(row)}`);
177
+ assert.ok(columns(row) <= width, `${label}: ${JSON.stringify(stripAnsi(row))} is wider than ${width}`);
178
+ }
179
+ }
180
+
181
+ // ---- the fixtures: names and paths as long as a real ODD makes them ----
182
+
183
+ const WARP_FIXTURES = [
184
+ {
185
+ label: 'a realistic long ODD name',
186
+ details: {
187
+ fromName: 'feat-one',
188
+ name: 'fix-long-and-realistic-feature-name',
189
+ to: '/home/jona/dev/projects/odd-worktree-worktrees/fix-long-and-realistic-feature-name',
190
+ branch: 'fix/long-and-realistic-feature-name',
191
+ },
192
+ },
193
+ {
194
+ label: 'two long names',
195
+ details: {
196
+ fromName: 'warp-queue-boundary-with-a-long-name',
197
+ name: 'warp-fallback-regression-with-a-long-name',
198
+ to: '/clone-worktrees/warp-fallback-regression-with-a-long-name',
199
+ branch: 'feat/warp-fallback-regression-with-a-long-name',
200
+ },
201
+ },
202
+ {
203
+ label: 'the prose fallback',
204
+ details: {},
205
+ content: 'session warped to a checkout whose name is long enough to need clipping',
206
+ },
207
+ {
208
+ label: 'a short name that fits every width',
209
+ details: { fromName: 'feat-one', name: 'feat-two', to: '/clone-worktrees/feat-two', branch: 'feat/two' },
210
+ },
211
+ ];
212
+
213
+ const TOOL_FIXTURES = [
214
+ {
215
+ label: 'a created checkout on a long path',
216
+ text: [
217
+ 'worktree created: /home/jona/dev/projects/odd-worktree-worktrees/fix-long-and-realistic-feature-name',
218
+ 'branch: fix/long-and-realistic-feature-name (created)',
219
+ 'setup: pnpm install · log: /home/jona/dev/projects/odd-worktree-worktrees/fix-long-and-realistic-feature-name/.worktree-setup.log',
220
+ ].join('\n'),
221
+ },
222
+ {
223
+ label: 'a refusal that names the branch and the worktree',
224
+ text: 'branch fix/long-and-realistic-feature-name is already checked out in /home/jona/dev/projects/odd-worktree-worktrees/fix-long-and-realistic-feature-name\nUse a different branch, or work in that worktree.',
225
+ },
226
+ {
227
+ label: 'a list row wider than the card',
228
+ text: 'worktree list: 2 checkouts\n1 fix-long-and-realistic-feature-name fix/long-and-realistic-feature-name',
229
+ },
230
+ {
231
+ label: 'a short result',
232
+ text: 'branch kept',
233
+ },
234
+ ];
235
+
236
+ const WIDTHS = [40, 60, 30, 24, 16];
237
+ const STYLES = ['neon', 'float'];
238
+
239
+ // ---- the `/warp` message card ----
240
+
241
+ function warpMessage(fixture, expanded) {
242
+ return {
243
+ customType: 'worktree-warp',
244
+ content: fixture.content ?? 'x',
245
+ details: { ...fixture.details, durationMs: 1200 },
246
+ _expanded: expanded,
247
+ };
248
+ }
249
+
250
+ function renderWarp(fixture, { helpers, width, expanded, style }) {
251
+ const message = warpMessage(fixture, expanded);
252
+ const theme = ansiTheme();
253
+ const options = { expanded };
254
+ return withStyle(style, () => createWarpMessageRenderer(helpers)(message, options, theme).render(width));
255
+ }
256
+
257
+ test('the fallback warp card shows exactly the text the ANSI-aware helpers show', () => {
258
+ let pairs = 0;
259
+ for (const fixture of WARP_FIXTURES) {
260
+ for (const width of WIDTHS) {
261
+ for (const expanded of [false, true]) {
262
+ for (const style of STYLES) {
263
+ const label = `${fixture.label} @${width} ${style} expanded=${expanded}`;
264
+ const injected = renderWarp(fixture, { helpers: ansiAware, width, expanded, style });
265
+ const fallback = renderWarp(fixture, { helpers: undefined, width, expanded, style });
266
+ pairs += 1;
267
+ assert.deepEqual(textRows(fallback), textRows(injected), `${label}: the fallback lost or gained text`);
268
+ assertClean(fallback, width, `${label} (fallback)`);
269
+ assertClean(injected, width, `${label} (injected)`);
270
+ }
271
+ }
272
+ }
273
+ }
274
+ assert.ok(pairs > 0, 'the matrix rendered');
275
+ });
276
+
277
+ test('the reported width-40 row reads as the baseline drew it', () => {
278
+ const fixture = WARP_FIXTURES[0];
279
+ for (const helpers of [ansiAware, undefined]) {
280
+ const rows = renderWarp(fixture, { helpers, width: 40, expanded: false, style: 'neon' });
281
+ const row = rows.find((value) => stripAnsi(value).includes('to: '));
282
+ assert.equal(row === undefined, false, 'the target row is drawn');
283
+ // The baseline (HEAD, no body role) clipped the plain row to the inner width;
284
+ // the fallback must reproduce that clip exactly, not shorten it.
285
+ assert.equal(stripAnsi(row), '│ to: fix-long-and-realistic-feature-… │');
286
+ }
287
+ });
288
+
289
+ // ---- the tool cards, on the same substrate ----
290
+
291
+ const cards = createWorktreeCards({ metrics: ansiAware });
292
+ const fallbackCards = createWorktreeCards({ metrics: countsEscapes });
293
+
294
+ function renderResult(factory, fixture, { width, expanded, style }) {
295
+ const theme = ansiTheme();
296
+ const result = { content: [{ type: 'text', text: fixture.text }], details: {} };
297
+ return withStyle(style, () => factory
298
+ .renderer('worktree_list')
299
+ .renderResult(result, { expanded, isPartial: false }, theme, { durationMs: 1200 })
300
+ .render(width));
301
+ }
302
+
303
+ test('a tool result body clipped on the ANSI-unaware helper shows the same text', () => {
304
+ let pairs = 0;
305
+ for (const fixture of TOOL_FIXTURES) {
306
+ for (const width of WIDTHS) {
307
+ for (const expanded of [false, true]) {
308
+ for (const style of STYLES) {
309
+ const label = `${fixture.label} @${width} ${style} expanded=${expanded}`;
310
+ const injected = renderResult(cards, fixture, { width, expanded, style });
311
+ const fallback = renderResult(fallbackCards, fixture, { width, expanded, style });
312
+ pairs += 1;
313
+ assert.deepEqual(textRows(fallback), textRows(injected), `${label}: the body changed with the helper`);
314
+ assertClean(fallback, width, `${label} (unaware clip)`);
315
+ assertClean(injected, width, `${label} (ANSI-aware clip)`);
316
+ }
317
+ }
318
+ }
319
+ }
320
+ assert.ok(pairs > 0, 'the matrix rendered');
321
+ });
322
+
323
+ test('a clipped tool body row ends on the same column, with the same mark', () => {
324
+ const fixture = TOOL_FIXTURES[1];
325
+ for (const width of [40, 24]) {
326
+ const injected = renderResult(cards, fixture, { width, expanded: false, style: 'neon' });
327
+ const fallback = renderResult(fallbackCards, fixture, { width, expanded: false, style: 'neon' });
328
+ assert.deepEqual(textRows(fallback), textRows(injected));
329
+ for (const row of fallback.slice(0, -1)) assert.equal(columns(row), width, 'a body row fills the card');
330
+ }
331
+ });
332
+
333
+ // ---- the host's truncation rule, at the widths where the ellipsis bites ----
334
+ //
335
+ // THE RULE IS THE HOST'S, not this package's: `truncateToWidth` in the installed
336
+ // `@earendil-works/pi-tui/dist/utils.js` returns the empty string for a width of
337
+ // zero or less, leaves text that fits UNCHANGED, clips and emits the ELLIPSIS
338
+ // ITSELF when the ellipsis is at least as wide as the budget (so width 1 of an
339
+ // overflowing string is `…`, not its first column), and otherwise keeps
340
+ // `width - ellipsisWidth` columns and appends the ellipsis. The table below is
341
+ // that rule written down, not a report of what this package happens to draw.
342
+ const HOST_RULE = [
343
+ { width: 0, shown: '' },
344
+ { width: 1, shown: '…' },
345
+ { width: 2, shown: 'a…' },
346
+ { width: 3, shown: 'ab…' },
347
+ { width: 4, shown: 'abc…' },
348
+ ];
349
+
350
+ // `lib/worktree-card.js` owns the only clip this package implements (the
351
+ // fallback seam of the `/warp` card), so its rule is stated directly.
352
+ test("the local fallback truncator implements the host's rule at every width", () => {
353
+ for (const { width, shown } of HOST_RULE) {
354
+ assert.equal(localTruncate('abcdef', width, '…'), shown, `an overflowing row at width ${width}`);
355
+ }
356
+ assert.equal(localTruncate('abcdef', 6, '…'), 'abcdef', 'text that exactly fits is unchanged');
357
+ assert.equal(localTruncate('abcdef', 40, '…'), 'abcdef', 'and so is text that fits with room to spare');
358
+ assert.equal(localTruncate('', 3, '…'), '', 'empty text stays empty');
359
+ // The rule holds for any ellipsis width, not just one column: an ellipsis at
360
+ // least as wide as the budget is itself clipped and emitted.
361
+ assert.equal(localTruncate('abcdef', 1, '...'), '.', 'a three-column ellipsis keeps one of its columns at width 1');
362
+ assert.equal(localTruncate('abcdef', 2, '...'), '..', 'and two at width 2');
363
+ assert.equal(localTruncate('abcdef', 3, '...'), '...', 'it fills the budget exactly at its own width');
364
+ assert.equal(localTruncate('abcdef', 4, '...'), 'a...', 'one column past it, the text keeps a column first');
365
+ // Whole cells only: a wide character is never cut in half.
366
+ assert.equal(localTruncate('日本語', 2, '…'), '…', 'no half cell at width 2');
367
+ assert.equal(localTruncate('日本語', 4, '…'), '日…', 'and the columns that fit are kept whole');
368
+ });
369
+
370
+ // The same rule as the cards draw it: card width = inner width + the four chrome
371
+ // columns (`│ … │`). The row is `moved: main`, which overflows every width here.
372
+ const BOUNDARY_ROWS = [
373
+ { inner: 0, shown: '' },
374
+ { inner: 1, shown: '…' },
375
+ { inner: 2, shown: 'm…' },
376
+ { inner: 3, shown: 'mo…' },
377
+ { inner: 4, shown: 'mov…' },
378
+ ];
379
+
380
+ /** The one body row of a two-row compact card, as `rows.slice(1, -1)`.
381
+ * Its content cell is the columns between the frame's `│ ` and its ` │`. */
382
+ function bodyCells(rows, inner) {
383
+ return rows.slice(1, -1).map((row) => stripAnsi(row).slice(2, 2 + inner));
384
+ }
385
+
386
+ function shortWarpFixture() {
387
+ return { label: 'two short names', details: { fromName: 'main', name: 'feat-two', to: '/clone-worktrees/feat-two', branch: 'feat/two' } };
388
+ }
389
+
390
+ test('the /warp card clips an overflowing row the way the host clips it', () => {
391
+ const fixture = { label: 'a row wider than every boundary width', details: { fromName: 'main', name: 'a-long-enough-name' } };
392
+ for (const { inner, shown } of BOUNDARY_ROWS) {
393
+ for (const style of STYLES) {
394
+ const label = `inner ${inner} ${style}`;
395
+ const injected = renderWarp(fixture, { helpers: ansiAware, width: inner + 4, expanded: false, style });
396
+ const fallback = renderWarp(fixture, { helpers: undefined, width: inner + 4, expanded: false, style });
397
+ // The first body row shows the rule's own answer on BOTH seams...
398
+ assert.equal(bodyCells(fallback, inner)[0], shown, `${label}: the fallback clip is the host's rule`);
399
+ assert.equal(bodyCells(injected, inner)[0], shown, `${label}: the host's own helper agrees`);
400
+ // ...and the whole card agrees, so the clip alone moved.
401
+ assert.deepEqual(textRows(fallback), textRows(injected), `${label}: the fallback lost or gained text`);
402
+ assertClean(fallback, inner + 4, `${label} (fallback)`);
403
+ }
404
+ }
405
+ });
406
+
407
+ test('a /warp row that fits is drawn unchanged at the boundary widths', () => {
408
+ const fixture = shortWarpFixture();
409
+ for (const inner of [0, 1, 2, 3, 4, 20]) {
410
+ const injected = renderWarp(fixture, { helpers: ansiAware, width: inner + 4, expanded: false, style: 'neon' });
411
+ const fallback = renderWarp(fixture, { helpers: undefined, width: inner + 4, expanded: false, style: 'neon' });
412
+ assert.deepEqual(textRows(fallback), textRows(injected), `inner ${inner}: the fallback lost or gained text`);
413
+ if (inner >= 'moved: main'.length) {
414
+ assert.equal(bodyCells(fallback, inner)[0], 'moved: main'.padEnd(inner), 'a row that fits is padded, never clipped');
415
+ }
416
+ }
417
+ });
418
+
419
+ // The verifier's own independent boundary case, folded in faithfully: it is NOT a
420
+ // byte-for-byte copy of the verifier's file — this suite adapts it to its own
421
+ // fixtures (`content: 'x'` instead of an empty string, the style slot reset through
422
+ // `useStyle` instead of a local wrapper, no expansion). What is identical is the
423
+ // part that matters: a renderer built with no dependencies, a theme that paints
424
+ // every role `\x1b[37m…\x1b[39m`, the same message details, and the exact two
425
+ // widths with the exact expected rows the verifier reproduced the defect at.
426
+ // Do not describe this as verbatim; it is equivalent, and both widths must pass.
427
+ const VERIFIER_THEME = { fg: (_role, value) => `\x1b[37m${value}\x1b[39m` };
428
+ const VERIFIER_MESSAGE = { customType: 'worktree-warp', content: 'x', details: { fromName: 'main', name: 'long-name' } };
429
+
430
+ /** The verifier's render: no injected helpers, no style slot, no expansion. */
431
+ function verifierRows(width) {
432
+ useStyle(undefined);
433
+ return createWarpMessageRenderer()(VERIFIER_MESSAGE, {}, VERIFIER_THEME).render(width);
434
+ }
435
+
436
+ test("the verifier's independent boundary case renders the host's rows", () => {
437
+ const cases = [
438
+ [5, ['│ … │', '│ … │']],
439
+ [6, ['│ m… │', '│ t… │']],
440
+ ];
441
+ for (const [width, expected] of cases) {
442
+ const rows = verifierRows(width).map((row) => stripAnsi(row)).slice(1, -1);
443
+ assert.deepEqual(rows, expected, `width ${width}`);
444
+ }
445
+ });
446
+
447
+ // The tool cards clip through the same substrate step, so the boundary widths are
448
+ // pinned there too, with the package's own fallback truncator in the clip seat:
449
+ // the two seams must agree at every width (nothing in this family needs the
450
+ // ellipsis rule yet, and nothing may start to).
451
+ const localSeam = {
452
+ visibleWidth: (value) => columns(value),
453
+ truncateToWidth: (value, width, ellipsis = '') => localTruncate(value, width, ellipsis),
454
+ wrapTextWithAnsi: ansiAware.wrapTextWithAnsi,
455
+ };
456
+ const localCards = createWorktreeCards({ metrics: localSeam });
457
+
458
+ test('the tool cards keep the host rule at the boundary widths', () => {
459
+ for (const fixture of TOOL_FIXTURES) {
460
+ for (const inner of [0, 1, 2, 3, 4]) {
461
+ for (const expanded of [false, true]) {
462
+ for (const style of STYLES) {
463
+ const label = `${fixture.label} inner ${inner} ${style} expanded=${expanded}`;
464
+ const injected = renderResult(cards, fixture, { width: inner + 4, expanded, style });
465
+ const fallback = renderResult(localCards, fixture, { width: inner + 4, expanded, style });
466
+ assert.deepEqual(textRows(fallback), textRows(injected), `${label}: the fallback clip changed the text`);
467
+ assertClean(fallback, inner + 4, `${label} (fallback)`);
468
+ }
469
+ }
470
+ }
471
+ }
472
+ });
473
+
474
+ // ---- the invariant itself: the BODY path never measures a painted string ----
475
+ //
476
+ // Scope, deliberately: this guard renders result bodies, never a call heading.
477
+ // The heading carries the host's `keyHint`, which the host paints `dim`/`muted`
478
+ // BEFORE handing it over, and `cardTop` legitimately measures that painted string
479
+ // with `visibleWidth`. So the truthful claim is "no string THIS REPO painted is
480
+ // measured", not "no helper ever sees paint" — an absolute assertion would fail on
481
+ // the host's own hint and would tempt a future reader to "fix" working code.
482
+
483
+ /** A seam that records every string the substrate asks it to measure or clip. */
484
+ function recordingSeam() {
485
+ const seen = [];
486
+ const record = (value) => {
487
+ seen.push(String(value));
488
+ return value;
489
+ };
490
+ return {
491
+ seen,
492
+ metrics: {
493
+ visibleWidth(value) {
494
+ record(value);
495
+ return columns(value);
496
+ },
497
+ truncateToWidth(value, width, ellipsis) {
498
+ record(value);
499
+ return ansiAware.truncateToWidth(value, width, ellipsis);
500
+ },
501
+ wrapTextWithAnsi(value, width) {
502
+ record(value);
503
+ return ansiAware.wrapTextWithAnsi(value, width);
504
+ },
505
+ },
506
+ };
507
+ }
508
+
509
+ test('the body path never measures or clips a painted string, in either card family', () => {
510
+ const seam = recordingSeam();
511
+ const painted = createWorktreeCards({ metrics: seam.metrics });
512
+ for (const fixture of TOOL_FIXTURES) {
513
+ for (const width of [60, 40, 24]) {
514
+ for (const expanded of [false, true]) {
515
+ renderResult(painted, fixture, { width, expanded, style: 'neon' });
516
+ }
517
+ }
518
+ }
519
+ // The warp card resolves its own fallback seam, so its painted strings are
520
+ // observed through the same guard: a clipped row that still holds an escape
521
+ // character proves the paint happened before the measurement.
522
+ for (const fixture of WARP_FIXTURES) {
523
+ for (const width of [60, 40, 24, 16]) {
524
+ for (const expanded of [false, true]) {
525
+ renderWarp(fixture, { helpers: undefined, width, expanded, style: 'neon' });
526
+ }
527
+ }
528
+ }
529
+ assert.ok(seam.seen.length > 0, 'the seam was exercised');
530
+ for (const value of seam.seen) {
531
+ assert.equal(value.includes('\x1b'), false, `a painted string reached the text seam: ${JSON.stringify(value)}`);
532
+ }
533
+ });