pi-odd-worktree 3.15.1 → 3.16.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 (126) hide show
  1. package/README.md +48 -25
  2. package/extensions/odd-worktree-tools.ts +64 -211
  3. package/lib/shell-api/api.ts +523 -0
  4. package/lib/shell-api/arm.ts +83 -0
  5. package/lib/shell-api/bus.ts +1389 -0
  6. package/lib/shell-api/capture.ts +1340 -0
  7. package/lib/shell-api/card-style-line.ts +112 -0
  8. package/lib/shell-api/card-style-rows.ts +390 -0
  9. package/lib/shell-api/card-style-transform.ts +862 -0
  10. package/lib/shell-api/card-style-wiring.ts +477 -0
  11. package/lib/shell-api/card-styles.ts +153 -0
  12. package/lib/shell-api/config.ts +71 -0
  13. package/lib/shell-api/extension.ts +247 -0
  14. package/lib/shell-api/hook.ts +322 -0
  15. package/lib/shell-api/index.ts +88 -0
  16. package/lib/shell-api/message-design-adoption.ts +549 -0
  17. package/lib/shell-api/message-design-rows.ts +313 -0
  18. package/lib/shell-api/message-design-wiring.ts +369 -0
  19. package/lib/shell-api/message-designs.ts +746 -0
  20. package/lib/shell-api/notices.ts +137 -0
  21. package/lib/shell-api/plugin-lines.ts +877 -0
  22. package/lib/shell-api/presence.ts +258 -0
  23. package/lib/shell-api/repaint.ts +351 -0
  24. package/lib/shell-api/row-labels.ts +146 -0
  25. package/lib/shell-api/row-names.ts +273 -0
  26. package/lib/shell-api/row-origin.ts +144 -0
  27. package/lib/shell-api/rows.ts +591 -0
  28. package/lib/shell-api/section-panel.ts +378 -0
  29. package/lib/shell-api/section-pointer.ts +164 -0
  30. package/lib/shell-api/sections-preview.ts +275 -0
  31. package/lib/shell-api/sections-rows.ts +133 -0
  32. package/lib/shell-api/sidebar-render.ts +705 -0
  33. package/lib/shell-api/sidebar.ts +1126 -0
  34. package/lib/shell-api/store.ts +279 -0
  35. package/lib/shell-api/tool-design-adoption.ts +263 -0
  36. package/lib/shell-api/tool-design-rows.ts +307 -0
  37. package/lib/shell-api/tool-design-wiring.ts +481 -0
  38. package/lib/shell-api/tool-designs.ts +1411 -0
  39. package/lib/shell-api/transform-pipeline.ts +351 -0
  40. package/lib/shell-api/types.ts +803 -0
  41. package/lib/shell-api/visibility.ts +154 -0
  42. package/package.json +3 -2
  43. package/tools/files-bridge.test.js +81 -66
  44. package/tools/fixtures/load-extension.js +40 -18
  45. package/tools/issues-command.test.js +4 -7
  46. package/tools/issues-section.test.js +71 -81
  47. package/tools/odd-rail-wiring.test.js +16 -10
  48. package/tools/package-surface.test.js +42 -13
  49. package/tools/prs-command.test.js +11 -6
  50. package/tools/shell-api/api.test.ts +484 -0
  51. package/tools/shell-api/apply-on-move.test.ts +529 -0
  52. package/tools/shell-api/arm.test.ts +220 -0
  53. package/tools/shell-api/bridge-ready.test.ts +309 -0
  54. package/tools/shell-api/bus.test.ts +194 -0
  55. package/tools/shell-api/button-rows.test.ts +435 -0
  56. package/tools/shell-api/capture.test.ts +583 -0
  57. package/tools/shell-api/card-style-rows.test.ts +493 -0
  58. package/tools/shell-api/card-style-transform.test.ts +379 -0
  59. package/tools/shell-api/card-style-wiring.test.ts +258 -0
  60. package/tools/shell-api/card-styles-e2e.test.ts +502 -0
  61. package/tools/shell-api/card-styles.test.ts +324 -0
  62. package/tools/shell-api/card-transcript-path.test.ts +178 -0
  63. package/tools/shell-api/compatibility-regression.test.mjs +64 -0
  64. package/tools/shell-api/config.test.ts +93 -0
  65. package/tools/shell-api/docs-coverage.test.ts +82 -0
  66. package/tools/shell-api/e2e.test.ts +562 -0
  67. package/tools/shell-api/example-bash-identity.test.ts +372 -0
  68. package/tools/shell-api/example-bash-matrix.test.ts +856 -0
  69. package/tools/shell-api/example-designs.test.ts +737 -0
  70. package/tools/shell-api/example-neon-transform.test.ts +283 -0
  71. package/tools/shell-api/example-plain-style.test.ts +1225 -0
  72. package/tools/shell-api/extension.test.ts +157 -0
  73. package/tools/shell-api/fake-events.ts +58 -0
  74. package/tools/shell-api/fake-host.ts +211 -0
  75. package/tools/shell-api/fake-sidebar.ts +516 -0
  76. package/tools/shell-api/fake-transcript.ts +486 -0
  77. package/tools/shell-api/frame-telemetry.test.ts +317 -0
  78. package/tools/shell-api/hook.test.ts +196 -0
  79. package/tools/shell-api/manifest.test.ts +47 -0
  80. package/tools/shell-api/message-design-adoption.test.ts +611 -0
  81. package/tools/shell-api/message-design-follows.test.ts +361 -0
  82. package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
  83. package/tools/shell-api/message-design-regressions.test.mjs +162 -0
  84. package/tools/shell-api/message-design-rows.test.ts +266 -0
  85. package/tools/shell-api/message-design-wiring.test.ts +362 -0
  86. package/tools/shell-api/message-designs.test.ts +339 -0
  87. package/tools/shell-api/new-category.test.ts +1101 -0
  88. package/tools/shell-api/notices.test.ts +292 -0
  89. package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
  90. package/tools/shell-api/presence.test.ts +396 -0
  91. package/tools/shell-api/rail-part-hits.test.ts +281 -0
  92. package/tools/shell-api/rail-surfaces.test.ts +402 -0
  93. package/tools/shell-api/rail-width.test.ts +406 -0
  94. package/tools/shell-api/regression.test.ts +434 -0
  95. package/tools/shell-api/repaint-api.test.ts +200 -0
  96. package/tools/shell-api/reserved-categories.test.ts +250 -0
  97. package/tools/shell-api/row-labels.test.ts +261 -0
  98. package/tools/shell-api/row-names.test.ts +407 -0
  99. package/tools/shell-api/row-origin.test.ts +395 -0
  100. package/tools/shell-api/row-placement-regression.test.ts +797 -0
  101. package/tools/shell-api/row-placement.test.ts +410 -0
  102. package/tools/shell-api/section-interaction.test.ts +364 -0
  103. package/tools/shell-api/section-panel.test.ts +430 -0
  104. package/tools/shell-api/section-placement-rail.test.ts +390 -0
  105. package/tools/shell-api/section-placement.test.ts +386 -0
  106. package/tools/shell-api/sections-preview.test.ts +312 -0
  107. package/tools/shell-api/sections-rows.test.ts +293 -0
  108. package/tools/shell-api/sidebar-backend.test.ts +477 -0
  109. package/tools/shell-api/sidebar-e2e.test.ts +317 -0
  110. package/tools/shell-api/sidebar-hover.test.ts +276 -0
  111. package/tools/shell-api/sidebar-independent.test.ts +291 -0
  112. package/tools/shell-api/sidebar-render.test.ts +281 -0
  113. package/tools/shell-api/sidebar-sections.test.ts +247 -0
  114. package/tools/shell-api/store.test.ts +192 -0
  115. package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
  116. package/tools/shell-api/tool-design-card-style.test.ts +308 -0
  117. package/tools/shell-api/tool-design-chain.test.ts +471 -0
  118. package/tools/shell-api/tool-design-cost.test.ts +591 -0
  119. package/tools/shell-api/tool-design-rows.test.ts +627 -0
  120. package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
  121. package/tools/shell-api/tool-designs.test.ts +2412 -0
  122. package/tools/shell-api/transform-pipeline.test.ts +376 -0
  123. package/tools/shell-api/transforms-bus.test.ts +233 -0
  124. package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
  125. package/tools/shell-api-rpc.test.js +189 -0
  126. package/tools/user-prompt-card.test.js +8 -9
@@ -0,0 +1,611 @@
1
+ /**
2
+ * T3 — the adoption pass: the message Pi already mounted, reached where Pi keeps it.
3
+ *
4
+ * A `CustomMessageComponent` holds the renderer Pi resolved for that message in `customRenderer` and
5
+ * calls it again on every `rebuild()`. Nothing else reaches it: `registerMessageRenderer` writes into
6
+ * the registering extension's own map and the runner answers the first one in load order, so the
7
+ * component is the only capture point there is.
8
+ *
9
+ * This file drives the pass with a component that rebuilds the way Pi's does, so the observable
10
+ * contract is pinned: an unclaimed type is never touched (S4), a host-rendered type keeps its frame
11
+ * and only its rows change (S3), and the switch back to `default` restores the host's own output.
12
+ */
13
+
14
+ import assert from "node:assert/strict";
15
+ import { mkdtempSync, rmSync } from "node:fs";
16
+ import { tmpdir } from "node:os";
17
+ import { join } from "node:path";
18
+ import test from "node:test";
19
+
20
+ import { registerCardStyle, resetBusForTests } from "../../lib/shell-api/bus.ts";
21
+ import { setCardStyleSelection } from "../../lib/shell-api/card-styles.ts";
22
+ import {
23
+ adoptCustomMessages,
24
+ resetMessageDesignAdoptionForTests,
25
+ } from "../../lib/shell-api/message-design-adoption.ts";
26
+ import {
27
+ getMessageDesignFailures,
28
+ getMessageDesignSelectionReads,
29
+ registerMessageDesign,
30
+ resetMessageDesignSelectionReads,
31
+ resetMessageDesignsForTests,
32
+ setMessageDesignSelection,
33
+ } from "../../lib/shell-api/message-designs.ts";
34
+ import { resetStoreStateForTests } from "../../lib/shell-api/store.ts";
35
+ import type { MessageDesignRegistration } from "../../lib/shell-api/types.ts";
36
+
37
+ let configHome: string;
38
+ let previousConfigHome: string | undefined;
39
+
40
+ test.before(() => {
41
+ configHome = mkdtempSync(join(tmpdir(), "gsb-message-adoption-"));
42
+ previousConfigHome = process.env.GENTLE_PI_CONFIG_HOME;
43
+ process.env.GENTLE_PI_CONFIG_HOME = configHome;
44
+ });
45
+
46
+ test.after(() => {
47
+ if (previousConfigHome === undefined) delete process.env.GENTLE_PI_CONFIG_HOME;
48
+ else process.env.GENTLE_PI_CONFIG_HOME = previousConfigHome;
49
+ rmSync(configHome, { recursive: true, force: true });
50
+ });
51
+
52
+ test.beforeEach(() => {
53
+ rmSync(join(configHome, "gentle-shell-bridge"), { recursive: true, force: true });
54
+ resetMessageDesignsForTests();
55
+ resetMessageDesignAdoptionForTests();
56
+ resetBusForTests();
57
+ resetStoreStateForTests();
58
+ });
59
+
60
+ const TYPE = "gentle-pi.exploration-budget";
61
+ const REMINDER = "gentle-pi.verifier-settle-reminder";
62
+
63
+ /**
64
+ * The smallest faithful stand-in for Pi's `CustomMessageComponent`: it keeps the renderer it was
65
+ * built with, calls it again on `rebuild()`, and falls back to its own box when the renderer answers
66
+ * `undefined`. `rendererCalls` and `invalidations` are the observable effects of an adoption pass.
67
+ */
68
+ interface FakeMessage {
69
+ message: { customType: string; content: string };
70
+ customRenderer: unknown;
71
+ /** What `rebuild()` stored: Pi renders THIS on every frame, it does not re-call the renderer. */
72
+ component: unknown;
73
+ expanded: boolean;
74
+ /** Pi's own default box, present on a real component from construction. */
75
+ box?: { padding?: number; render(width: number): readonly string[]; setPaddingX?(padding: number): void };
76
+ outputPad?: number;
77
+ rendererCalls: number;
78
+ invalidations: number;
79
+ rebuilds: number;
80
+ rebuild(): void;
81
+ invalidate(): void;
82
+ setExpanded(expanded: boolean): void;
83
+ render(width: number): readonly string[];
84
+ }
85
+
86
+ function fakeCustomMessage(
87
+ customType: string,
88
+ hostRows?: (message: unknown, options: unknown) => readonly string[],
89
+ ): FakeMessage {
90
+ const child: FakeMessage = {
91
+ message: { customType, content: "hello" },
92
+ customRenderer:
93
+ hostRows === undefined
94
+ ? undefined
95
+ : (message: unknown, options: unknown) => ({
96
+ render: (width: number): readonly string[] =>
97
+ hostRows(message, options).map((row) => `${row}@${width}`),
98
+ }),
99
+ component: undefined,
100
+ expanded: false,
101
+ rendererCalls: 0,
102
+ invalidations: 0,
103
+ rebuilds: 0,
104
+ // Exactly Pi: the renderer is invoked as a MEMBER, once per rebuild.
105
+ rebuild(): void {
106
+ child.rebuilds += 1;
107
+ child.rendererCalls += 1;
108
+ child.component =
109
+ typeof child.customRenderer === "function"
110
+ ? (child.customRenderer as (m: unknown, o: unknown, t: unknown) => unknown).call(
111
+ child,
112
+ child.message,
113
+ { expanded: child.expanded, outputPad: 1 },
114
+ {},
115
+ )
116
+ : undefined;
117
+ },
118
+ invalidate(): void {
119
+ child.invalidations += 1;
120
+ child.rebuild();
121
+ },
122
+ setExpanded(expanded: boolean): void {
123
+ if (child.expanded === expanded) return;
124
+ child.expanded = expanded;
125
+ child.rebuild();
126
+ },
127
+ render(width: number): readonly string[] {
128
+ const component = child.component;
129
+ if (component !== undefined) {
130
+ return (component as { render(width: number): readonly string[] }).render(width);
131
+ }
132
+ return [`[${customType}] pi's own box@${width}`];
133
+ },
134
+ };
135
+ child.rebuild();
136
+ return child;
137
+ }
138
+
139
+ const container = (children: unknown[]): { children: unknown[] } => ({ children });
140
+
141
+ const design = (
142
+ extra: Partial<MessageDesignRegistration> = {},
143
+ ): MessageDesignRegistration => ({
144
+ customType: TYPE,
145
+ id: "loon.budget",
146
+ label: "budget",
147
+ apply: (lines: readonly string[]) => [...lines, "design row"],
148
+ ...extra,
149
+ });
150
+
151
+ // ---------------------------------------------------------------------------------------------
152
+ // S4 — nobody claimed it: the pass is invisible
153
+ // ---------------------------------------------------------------------------------------------
154
+
155
+ test("S4: a message of a type nobody claimed is left exactly as it was", () => {
156
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
157
+ const before = child.customRenderer;
158
+
159
+ const result = adoptCustomMessages(container([child]));
160
+
161
+ assert.equal(result.scanned, 0, "no claim anywhere: not one child is looked at");
162
+ assert.equal(result.adopted, 0);
163
+ assert.equal(child.customRenderer, before, "the host's renderer, untouched");
164
+ assert.equal(child.invalidations, 0, "and nothing rebuilt: no work, no bytes changed");
165
+ });
166
+
167
+ test("S4: a child that is not a custom message is not ours to touch", () => {
168
+ // Something IS claimed, so the pass really walks the children; the strangers stay strangers.
169
+ registerMessageDesign(design());
170
+ const stranger = { render: () => ["rows"], toolName: "bash" };
171
+ const result = adoptCustomMessages(container([stranger, 42, null]));
172
+
173
+ assert.equal(result.scanned, 3);
174
+ assert.equal(result.ignored, 3);
175
+ assert.equal(result.adopted, 0);
176
+ });
177
+
178
+ test("S4: a container that is not a container is answered, never thrown at", () => {
179
+ for (const notAContainer of [undefined, null, 42, {}, { children: "nope" }]) {
180
+ assert.doesNotThrow(() => adoptCustomMessages(notAContainer));
181
+ assert.equal(adoptCustomMessages(notAContainer).scanned, 0);
182
+ }
183
+ });
184
+
185
+ // ---------------------------------------------------------------------------------------------
186
+ // S3 — a host-rendered type: the frame stays, the rows change
187
+ // ---------------------------------------------------------------------------------------------
188
+
189
+ test("S3: the first pass installs the design renderer and rebuilds the message ONCE", () => {
190
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
191
+ registerMessageDesign(design());
192
+
193
+ const result = adoptCustomMessages(container([child]));
194
+
195
+ assert.equal(result.adopted, 1);
196
+ assert.equal(child.invalidations, 1, "one rebuild, so the stored renderer is re-resolved");
197
+ assert.notEqual(child.customRenderer, undefined, "the design renderer is in place");
198
+ });
199
+
200
+ test("S3: the second pass adopts nothing and rebuilds nothing", () => {
201
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
202
+ registerMessageDesign(design());
203
+
204
+ adoptCustomMessages(container([child]));
205
+ const afterFirst = child.customRenderer;
206
+ const result = adoptCustomMessages(container([child]));
207
+
208
+ assert.equal(result.already, 1);
209
+ assert.equal(result.adopted, 0);
210
+ assert.equal(child.customRenderer, afterFirst, "the same renderer, not a second wrapper");
211
+ assert.equal(child.invalidations, 1, "and no second rebuild");
212
+ });
213
+
214
+ test("S3: with `default` selected the host's own frame is drawn, byte for byte", () => {
215
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
216
+ registerMessageDesign(design());
217
+ adoptCustomMessages(container([child]));
218
+
219
+ assert.deepEqual(child.render(40), ["host frame@40"], "exactly what Pi drew before the bridge");
220
+ });
221
+
222
+ test("S3: a selected design receives the host's rows at the message's width and returns the rest", () => {
223
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
224
+ registerMessageDesign(design());
225
+ setMessageDesignSelection(TYPE, "loon.budget");
226
+ adoptCustomMessages(container([child]));
227
+
228
+ assert.deepEqual(child.render(40), ["host frame@40", "design row"]);
229
+ assert.deepEqual(child.render(72), ["host frame@72", "design row"], "the width is read per pass");
230
+ });
231
+
232
+ test("S3: choosing a design AFTER the message was drawn reaches it on the next pass", () => {
233
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
234
+ registerMessageDesign(design());
235
+ adoptCustomMessages(container([child]));
236
+ assert.deepEqual(child.render(40), ["host frame@40"]);
237
+
238
+ // The user picks the design: the action repaints, the pass sees the new selection, and the message
239
+ // is rebuilt onto the design renderer. The reused raw host component could never draw it.
240
+ setMessageDesignSelection(TYPE, "loon.budget");
241
+ adoptCustomMessages(container([child]));
242
+ assert.deepEqual(child.render(40), ["host frame@40", "design row"]);
243
+ });
244
+
245
+ test("S3: going back to `default` restores Pi's own output on the next rebuild", () => {
246
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
247
+ registerMessageDesign(design());
248
+ setMessageDesignSelection(TYPE, "loon.budget");
249
+ adoptCustomMessages(container([child]));
250
+ assert.deepEqual(child.render(40), ["host frame@40", "design row"]);
251
+
252
+ // `default` with a host renderer needs no rebuild: the component reads the live selection.
253
+ setMessageDesignSelection(TYPE, "native");
254
+ adoptCustomMessages(container([child]));
255
+ assert.deepEqual(child.render(40), ["host frame@40"]);
256
+ });
257
+
258
+ // ---------------------------------------------------------------------------------------------
259
+ // A child the pass cannot adopt is reported, never thrown at
260
+ // ---------------------------------------------------------------------------------------------
261
+
262
+ test("S3: a message whose renderer cannot be replaced is reported and left alone", () => {
263
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
264
+ registerMessageDesign(design());
265
+ Object.defineProperty(child, "customRenderer", {
266
+ get: () => undefined,
267
+ set: () => {
268
+ // A host that refuses the assignment without throwing.
269
+ },
270
+ configurable: true,
271
+ });
272
+
273
+ const result = adoptCustomMessages(container([child]));
274
+
275
+ assert.equal(result.failed, 1);
276
+ assert.equal(
277
+ getMessageDesignFailures().some((failure) => failure.customType === TYPE),
278
+ true,
279
+ "reported through the design bus's one failure channel",
280
+ );
281
+ });
282
+
283
+ test("S3: `render` on an adopted message answers rows for a host renderer that returns nothing", () => {
284
+ const child = fakeCustomMessage(TYPE);
285
+ registerMessageDesign(design({ apply: () => ["own box"] }));
286
+ setMessageDesignSelection(TYPE, "loon.budget");
287
+ adoptCustomMessages(container([child]));
288
+
289
+ assert.deepEqual(child.render(30), ["own box"], "a design without host rows still draws");
290
+ });
291
+
292
+ test("S2: the two motivating types are adopted only when a design claims them", () => {
293
+ const budget = fakeCustomMessage(TYPE);
294
+ const reminder = fakeCustomMessage(REMINDER);
295
+ registerMessageDesign({
296
+ line: "gentle pi",
297
+ types: [TYPE, REMINDER],
298
+ id: "loon.panel",
299
+ label: "panel",
300
+ apply: () => ["panel"],
301
+ });
302
+
303
+ const result = adoptCustomMessages(container([budget, reminder]));
304
+
305
+ assert.equal(result.adopted, 2, "one line, two messages, one pass");
306
+ });
307
+
308
+ // ---------------------------------------------------------------------------------------------
309
+ // S2 — a type with NO host renderer: the design owns the box
310
+ // ---------------------------------------------------------------------------------------------
311
+
312
+ test("S2: the design draws the whole box, and `default` gives Pi's own box back after ONE rebuild", () => {
313
+ // A customType written by an extension that renders nothing: `customRenderer` is undefined, so
314
+ // without a design Pi draws its default `[type]` box. This is the motivating case (S2).
315
+ const child = fakeCustomMessage(REMINDER);
316
+ registerMessageDesign({
317
+ customType: REMINDER,
318
+ id: "loon.reminder",
319
+ label: "reminder",
320
+ apply: () => ["╭─ reminder ─╮", "│ settle │", "╰───────────╯"],
321
+ });
322
+ setMessageDesignSelection(REMINDER, "loon.reminder");
323
+
324
+ adoptCustomMessages(container([child]));
325
+ assert.equal(child.invalidations, 1, "the first adoption rebuilds the message");
326
+ assert.deepEqual(child.render(40), ["╭─ reminder ─╮", "│ settle │", "╰───────────╯"], "the design owns the box");
327
+
328
+ // The user goes back to `default`: the renderer must answer `undefined` again so Pi draws its box,
329
+ // which is a different ANSWER from the same renderer, so exactly one rebuild happens.
330
+ setMessageDesignSelection(REMINDER, "native");
331
+ const pass = adoptCustomMessages(container([child]));
332
+ assert.equal(pass.already, 1);
333
+ assert.equal(child.invalidations, 2, "one more rebuild, never two");
334
+ assert.deepEqual(child.render(40), [`[${REMINDER}] pi's own box@40`], "Pi's box, byte for byte");
335
+
336
+ // And back again, still one rebuild per switch.
337
+ setMessageDesignSelection(REMINDER, "loon.reminder");
338
+ adoptCustomMessages(container([child]));
339
+ assert.equal(child.invalidations, 3);
340
+ assert.deepEqual(child.render(40)[0], "╭─ reminder ─╮");
341
+ });
342
+
343
+ test("S2: the design reads `ctx.cardStyle`, so the consumer's painter knows which frame to draw", () => {
344
+ registerCardStyle({
345
+ id: "loon.lunar",
346
+ label: "lunar",
347
+ frame: {
348
+ top: () => "top",
349
+ line: (line) => line.text,
350
+ bottom: () => "bottom",
351
+ },
352
+ });
353
+ setCardStyleSelection("loon.lunar");
354
+
355
+ let seen: unknown;
356
+ const child = fakeCustomMessage(REMINDER);
357
+ registerMessageDesign({
358
+ customType: REMINDER,
359
+ id: "loon.reminder",
360
+ label: "reminder",
361
+ apply: (lines: readonly string[], ctx) => {
362
+ seen = ctx.cardStyle;
363
+ return [`painted ${lines.length}`];
364
+ },
365
+ });
366
+ setMessageDesignSelection(REMINDER, "loon.reminder");
367
+
368
+ adoptCustomMessages(container([child]));
369
+ assert.deepEqual(
370
+ child.render(40),
371
+ ["painted 3"],
372
+ "the message body's own rows: label, spacer, content",
373
+ );
374
+ const style = seen as { kind: string; style?: { id: string } };
375
+ assert.equal(style.kind, "style", "the bridge's own Card style selection, the one a tool design reads");
376
+ assert.equal(style.style?.id, "loon.lunar", "and the exact style the user chose");
377
+ });
378
+
379
+ test("S2: own-the-box works for ANY custom type, not just the two Pi ships today", () => {
380
+ // Nothing in the bridge knows this type: no host renderer, no Pi component, no list. A design still
381
+ // claims it and owns the box, which is the capability a consumer registers for its own messages.
382
+ const TYPE = "loon.custom-note";
383
+ const child = fakeCustomMessage(TYPE);
384
+ registerMessageDesign({
385
+ customType: TYPE,
386
+ id: "loon.note",
387
+ label: "note",
388
+ apply: (_lines: readonly string[], ctx) => [
389
+ `╭─ ${ctx.customType} @${ctx.width} ─╮`,
390
+ `│ ${String((ctx.render.message as { content: string }).content)} │`,
391
+ "╰─────────╯",
392
+ ],
393
+ });
394
+ setMessageDesignSelection(TYPE, "loon.note");
395
+
396
+ adoptCustomMessages(container([child]));
397
+
398
+ assert.deepEqual(child.render(30), [
399
+ "╭─ loon.custom-note @30 ─╮",
400
+ "│ hello │",
401
+ "╰─────────╯",
402
+ ]);
403
+ });
404
+
405
+ // ---------------------------------------------------------------------------------------------
406
+ // The verifier's reproduced defects (T6). Each test below FAILED against the slice before the fix.
407
+ // ---------------------------------------------------------------------------------------------
408
+
409
+ test("S4: with no claim anywhere, a pass reads no child at all", () => {
410
+ // S4 is a cost bound too, not only an output one: the pass must be able to answer before it
411
+ // touches a single message. A session with no message design pays nothing per conversation frame.
412
+ let reads = 0;
413
+ const child = fakeCustomMessage(TYPE);
414
+ Object.defineProperty(child, "message", {
415
+ get: () => {
416
+ reads += 1;
417
+ return { customType: TYPE, content: "hello" };
418
+ },
419
+ configurable: true,
420
+ });
421
+
422
+ const result = adoptCustomMessages(container([child, fakeCustomMessage(REMINDER)]));
423
+
424
+ assert.equal(reads, 0, "no claim, no read");
425
+ assert.equal(result.adopted, 0);
426
+ });
427
+
428
+ test("S3: a host renderer that is a METHOD keeps its `this`, as Pi calls it", () => {
429
+ const child = fakeCustomMessage(TYPE);
430
+ let seenThis: unknown;
431
+ child.customRenderer = function (this: unknown): unknown {
432
+ seenThis = this;
433
+ return { render: (): readonly string[] => ["host frame"] };
434
+ };
435
+ registerMessageDesign(design({ apply: (lines: readonly string[]) => [...lines] }));
436
+ setMessageDesignSelection(TYPE, "loon.budget");
437
+
438
+ adoptCustomMessages(container([child]));
439
+ child.render(40);
440
+
441
+ assert.equal(seenThis, child, "the component Pi would have passed as `this`");
442
+ });
443
+
444
+ test("S3: with a host renderer, switching to `default` rebuilds nothing", () => {
445
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
446
+ registerMessageDesign(design());
447
+ setMessageDesignSelection(TYPE, "loon.budget");
448
+ adoptCustomMessages(container([child]));
449
+ assert.equal(child.invalidations, 1, "the adoption itself");
450
+
451
+ setMessageDesignSelection(TYPE, "native");
452
+ const pass = adoptCustomMessages(container([child]));
453
+
454
+ assert.equal(pass.already, 1);
455
+ assert.equal(
456
+ child.invalidations,
457
+ 1,
458
+ "the renderer reads the live selection, so a host-backed switch needs no rebuild",
459
+ );
460
+ assert.deepEqual(child.render(40), ["host frame@40"]);
461
+ });
462
+
463
+ test("S3: a component that cannot be marked is never half-adopted, so passes cannot stack", () => {
464
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
465
+ const original = child.customRenderer;
466
+ registerMessageDesign(design());
467
+ // A real Pi component this pass may not add an own symbol to: sealing it must be discovered
468
+ // BEFORE the renderer is replaced, or every pass wraps the same message again.
469
+ Object.preventExtensions(child);
470
+
471
+ const first = adoptCustomMessages(container([child]));
472
+ const second = adoptCustomMessages(container([child]));
473
+
474
+ assert.equal(first.failed, 1);
475
+ assert.equal(second.failed, 1, "still refused, never silently adopted twice");
476
+ assert.equal(child.customRenderer, original, "the host's renderer was never replaced");
477
+ });
478
+
479
+ test("S4: disposing the last design restores the host's renderer and stops paying per pass", () => {
480
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
481
+ const original = child.customRenderer;
482
+ const dispose = registerMessageDesign(design());
483
+ setMessageDesignSelection(TYPE, "loon.budget");
484
+ adoptCustomMessages(container([child]));
485
+ assert.notEqual(child.customRenderer, original, "adopted");
486
+
487
+ dispose();
488
+ adoptCustomMessages(container([child]));
489
+
490
+ assert.equal(child.customRenderer, original, "the host's renderer, back in place");
491
+ assert.deepEqual(child.render(40), ["host frame@40"]);
492
+
493
+ resetMessageDesignSelectionReads();
494
+ child.render(40);
495
+ assert.equal(
496
+ getMessageDesignSelectionReads(),
497
+ 0,
498
+ "no live claim: a render must not read the store any more",
499
+ );
500
+ });
501
+
502
+ test("S3: a failing design on a HOST-rendered message keeps the host's own frame", () => {
503
+ // The design was only ever allowed to rewrite the host's rows, so its failure must leave exactly
504
+ // those rows — not Pi's box, which this message never drew.
505
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
506
+ registerMessageDesign(
507
+ design({
508
+ apply: () => {
509
+ throw new Error("boom");
510
+ },
511
+ }),
512
+ );
513
+ setMessageDesignSelection(TYPE, "loon.budget");
514
+ adoptCustomMessages(container([child]));
515
+
516
+ assert.deepEqual(child.render(40), ["host frame@40"], "the host's frame, untouched");
517
+ });
518
+
519
+ test("S3: an external rebuild at `default` does not strand the design", () => {
520
+ // Pi rebuilds on its own (`setExpanded`, a session switch, a reload) and calls the renderer with
521
+ // whatever is selected then. A record that still believed a design was mounted would leave the
522
+ // message on the raw host component and no design could ever reach it again.
523
+ const child = fakeCustomMessage(TYPE, () => ["host frame"]);
524
+ registerMessageDesign(design());
525
+ setMessageDesignSelection(TYPE, "loon.budget");
526
+ adoptCustomMessages(container([child]));
527
+ assert.deepEqual(child.render(40), ["host frame@40", "design row"]);
528
+
529
+ setMessageDesignSelection(TYPE, "native");
530
+ adoptCustomMessages(container([child]));
531
+ child.setExpanded(true); // Pi's own rebuild, at `default`
532
+ assert.deepEqual(child.render(40), ["host frame@40"]);
533
+
534
+ setMessageDesignSelection(TYPE, "loon.budget");
535
+ adoptCustomMessages(container([child]));
536
+ assert.deepEqual(child.render(40), ["host frame@40", "design row"], "the design draws again");
537
+ });
538
+
539
+ test("S4: a message the host removed stops closing the fast path", () => {
540
+ const child = fakeCustomMessage(TYPE);
541
+ const frame = container([child]);
542
+ const drop = registerMessageDesign(design());
543
+ setMessageDesignSelection(TYPE, "loon.budget");
544
+ adoptCustomMessages(frame);
545
+
546
+ // The host drops the message before anybody disposes the design: the container the message was
547
+ // adopted in is the only one that may conclude it is gone.
548
+ frame.children.length = 0;
549
+ adoptCustomMessages(frame);
550
+ drop();
551
+
552
+ let reads = 0;
553
+ const other = fakeCustomMessage(REMINDER);
554
+ Object.defineProperty(other, "message", {
555
+ get: () => {
556
+ reads += 1;
557
+ return { customType: REMINDER, content: "hello" };
558
+ },
559
+ configurable: true,
560
+ });
561
+ adoptCustomMessages(container([other]));
562
+
563
+ assert.equal(reads, 0, "nothing claimed and nothing mounted: no child is read again");
564
+ });
565
+
566
+ test("S2: the fallback box carries the message's CURRENT output padding", () => {
567
+ // Pi applies padding to its default box only on its own default path, which a design bypasses. After
568
+ // `setOutputPad(4)` the box still holds the padding it was born with, so a failing design that
569
+ // rendered it as-is would answer a frame indented to a setting nobody has any more.
570
+ const child = fakeCustomMessage(REMINDER);
571
+ child.outputPad = 4;
572
+ child.box = {
573
+ padding: 2,
574
+ setPaddingX(this: { padding: number }, padding: number): void {
575
+ this.padding = padding;
576
+ },
577
+ render(this: { padding: number }): readonly string[] {
578
+ return [`pad ${this.padding}`];
579
+ },
580
+ };
581
+ registerMessageDesign({
582
+ customType: REMINDER,
583
+ id: "loon.reminder",
584
+ label: "reminder",
585
+ apply: () => {
586
+ throw new Error("boom");
587
+ },
588
+ });
589
+ setMessageDesignSelection(REMINDER, "loon.reminder");
590
+ adoptCustomMessages(container([child]));
591
+
592
+ assert.deepEqual(child.render(40), ["pad 4"], "the frame that failed shows the box at the padding NOW");
593
+ // And then Pi draws its own box: the failed design is not retried while it is the selected one, which
594
+ // is what lets Pi run the default path that POPULATES the box for the rest of the session.
595
+ assert.deepEqual(child.render(40), [`[${REMINDER}] pi's own box@40`], "Pi's own next frame");
596
+ });
597
+
598
+ // Coexistence: the adoption state lives on a process-global slot that an older copy of this API (a
599
+ // standalone `gentle-shell-bridge` still installed) reads too, and that copy only trusts the slot
600
+ // while `adoptedMessages` is an array — otherwise it overwrites the whole object and drops the live
601
+ // adoption count and the per-container record. So the fields stay, even though nothing here reads
602
+ // `adoptedMessages` for a report any more.
603
+ test("the shared adoption state keeps the shape an older copy validates before trusting it", () => {
604
+ const slot = (globalThis as unknown as Record<symbol, Record<string, unknown> | undefined>)[
605
+ Symbol.for("gentle-shell-bridge.message-designs.adoption.v1")
606
+ ];
607
+ assert.ok(slot !== undefined, "resetting the adoption state publishes the slot");
608
+ assert.ok(Array.isArray(slot.adoptedMessages), "adoptedMessages stays an array, or an older copy resets the slot");
609
+ assert.equal(typeof slot.liveAdoptions, "number");
610
+ assert.ok(slot.adoptedByContainer instanceof WeakMap);
611
+ });