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,378 @@
1
+ /**
2
+ * The section's panel drawing (N11): the host's header and row builders, reached when they are
3
+ * reachable and reproduced byte for byte when they are not.
4
+ *
5
+ * ## What the host draws, and why we must draw it too
6
+ *
7
+ * A section must look like `AGENTS` / `CHANGES` / `STATS`. Those are drawn by
8
+ * gentle-shell's `lib/shell-panel.ts`:
9
+ *
10
+ * ```ts
11
+ * renderPanelHeader(theme, width, { title, count, countRole, open, hovered })
12
+ * → fitPanelLine(`${paintHoverable(theme, open ? "▾" : "▸", hovered, "muted")} ${theme.fg(hovered ? HOVER_ROLE : "text", bold(title.toUpperCase()))}`, count && theme.fg(countRole ?? "muted", count), width)
13
+ * renderPanelRow(width, left, right) → fitPanelLine(` ${left}`, right, width)
14
+ * paintHoverable(theme, text, hovered, idleRole) → hovered ? theme.fg(HOVER_ROLE, text) : theme.fg(idleRole, text)
15
+ * ```
16
+ *
17
+ * The theme comes from Pi: `ctx.ui.setWidget(key, (tui, theme) => Component)` is the documented
18
+ * hand-out (`docs/extensions.md:2922`), and gentle-shell obtains its own theme the same way. So the
19
+ * bridge captures that theme (`setSectionTheme`) instead of inventing a palette.
20
+ *
21
+ * ## Why the fallback exists
22
+ *
23
+ * The host's builders are NOT reachable: gentle-shell is not a dependency of this package, and Pi
24
+ * loads each extension through its own jiti loader with `moduleCache: false`, so
25
+ * `lib/shell-panel.ts` is not importable and publishes no seam on any object the bridge is handed.
26
+ * Rather than claim otherwise, this module:
27
+ *
28
+ * 1. LOOKS, on every object the bridge is actually given — the rail part, the Pi theme, the
29
+ * terminal's sidebar state — for the host's two builder functions (`notePanelCandidate`);
30
+ * 2. uses them when they are found, and reports `host` in the diagnostics record;
31
+ * 3. otherwise draws with the local port of the same algorithm (`renderSectionHeader` /
32
+ * `renderSectionRow`), which `test/section-panel.test.ts` compares byte for byte against the
33
+ * host's code transcribed into the test, and reports `fallback` with what was looked for and what
34
+ * was found (N15).
35
+ *
36
+ * Width is not a difference any more (N17): both sides measure the way pi-tui's `visibleWidth`
37
+ * measures, because the host's card frame and its rail gate read the row with that model —
38
+ * `hostColumns` ports it (a TAB is three columns, a grapheme cluster is charged as a whole), so a
39
+ * row fitted to `width` here is never wider than `width` to the host.
40
+ *
41
+ * This module is internal: `src/index.ts` does not re-export it.
42
+ */
43
+
44
+ import { hostColumns } from "./card-style-transform.ts";
45
+ import { isRecord } from "./rows.ts";
46
+ import { TONES, type SectionRow, type Tone } from "./types.ts";
47
+
48
+ /** Columns a panel row is indented under its header (`lib/shell-panel.ts:26`). */
49
+ export const PANEL_ROW_INDENT = 2;
50
+
51
+ /** The single role every clickable host surface paints its own hover with (`lib/shell-hover.ts:33`). */
52
+ export const HOVER_ROLE: Tone = "warning";
53
+
54
+ /** The role a header's glyph and count use when they are not hovered. */
55
+ const IDLE_META_ROLE: Tone = "muted";
56
+
57
+ /** The role a row's label uses when its `tone` is absent. */
58
+ const IDLE_TEXT_ROLE: Tone = "text";
59
+
60
+ /**
61
+ * The theme the panel draws with: Pi's own theme, as far as this module reads it. Reached through
62
+ * Pi's documented `setWidget` factory (`docs/extensions.md:2922`) — the same hand-out
63
+ * gentle-shell's own parts take their theme from.
64
+ */
65
+ export interface PanelTheme {
66
+ fg(role: string, text: string): string;
67
+ bold?(text: string): string;
68
+ }
69
+
70
+ /** One header, in the shape the host's `renderPanelHeader` takes (`lib/shell-panel.ts:31-42`). */
71
+ export interface PanelHeader {
72
+ title: string;
73
+ count?: string;
74
+ countRole?: string;
75
+ open: boolean;
76
+ hovered?: boolean;
77
+ }
78
+
79
+ /**
80
+ * The host's own builders, when a host publishes them. Only these two: a row is composed by this
81
+ * package (glyph, label, meta with their roles), exactly as `lib/agents-widget.ts:400-407`
82
+ * composes one before handing it to `renderPanelRow`.
83
+ */
84
+ export interface HostPanelBuilders {
85
+ renderPanelHeader(theme: PanelTheme, width: number, header: PanelHeader): string;
86
+ renderPanelRow(width: number, left: string, right?: string, indent?: number): string;
87
+ }
88
+
89
+ export type PanelSource = "host" | "fallback";
90
+
91
+ /** What the diagnostics record says about the frame: which drawing was used, and what it looked for. */
92
+ export interface PanelReport {
93
+ source: PanelSource;
94
+ /** The candidate objects that were inspected, in the order they were offered. */
95
+ lookedAt: string[];
96
+ /** What was missing, as a person reads it. Empty when the host's builders were found. */
97
+ missing: string[];
98
+ theme: "captured" | "missing";
99
+ }
100
+
101
+ interface PanelState {
102
+ theme?: PanelTheme;
103
+ builders?: HostPanelBuilders;
104
+ lookedAt: string[];
105
+ missing: string[];
106
+ }
107
+
108
+ /** Process-global, like every other piece of bridge state (`./bus.ts` explains why). */
109
+ const PANEL_STATE = Symbol.for("gentle-shell-bridge.section-panel.state.v1");
110
+
111
+ function panelHost(): Record<symbol, PanelState | undefined> {
112
+ // SAFETY: only `panelState()` writes this key, and it re-checks the shape before trusting it.
113
+ return globalThis as unknown as Record<symbol, PanelState | undefined>;
114
+ }
115
+
116
+ function isPanelStateLike(value: unknown): value is PanelState {
117
+ return isRecord(value) && Array.isArray(value.lookedAt) && Array.isArray(value.missing);
118
+ }
119
+
120
+ function panelState(): PanelState {
121
+ const host = panelHost();
122
+ const existing = host[PANEL_STATE];
123
+ if (isPanelStateLike(existing)) return existing;
124
+ const created: PanelState = { lookedAt: [], missing: [] };
125
+ host[PANEL_STATE] = created;
126
+ return created;
127
+ }
128
+
129
+ /** Test-only seam; deliberately not re-exported from `src/index.ts`. */
130
+ export function resetSectionPanelForTests(): void {
131
+ const state = panelState();
132
+ delete state.theme;
133
+ delete state.builders;
134
+ state.lookedAt = [];
135
+ state.missing = [];
136
+ }
137
+
138
+ /**
139
+ * Remember the theme Pi handed the widget factory. Anything without a callable `fg` is not a theme
140
+ * and is ignored, so a foreign or partial value can never make a row throw.
141
+ */
142
+ export function setSectionTheme(theme: unknown): void {
143
+ const state = panelState();
144
+ notePanelCandidate("the Pi theme", theme);
145
+ if (!isPanelTheme(theme)) {
146
+ delete state.theme;
147
+ return;
148
+ }
149
+ state.theme = theme;
150
+ }
151
+
152
+ function isPanelTheme(value: unknown): value is PanelTheme {
153
+ return isRecord(value) && typeof value.fg === "function";
154
+ }
155
+
156
+ /** The captured theme, or `undefined` before Pi has run the factory. */
157
+ export function getSectionTheme(): PanelTheme | undefined {
158
+ return panelState().theme;
159
+ }
160
+
161
+ /**
162
+ * Look for the host's panel builders on one object the bridge was handed (N11, N15). Called with the
163
+ * rail part, the Pi theme and the terminal's sidebar state — every object in this package's reach —
164
+ * so the answer in the diagnostics record is a measurement of this run, not a claim in a comment.
165
+ */
166
+ export function notePanelCandidate(label: string, value: unknown): void {
167
+ const state = panelState();
168
+ if (!state.lookedAt.includes(label)) state.lookedAt.push(label);
169
+ if (state.builders !== undefined) return;
170
+ const builders = asHostPanel(value);
171
+ if (builders !== undefined) {
172
+ state.builders = builders;
173
+ state.missing = [];
174
+ return;
175
+ }
176
+ if (!state.missing.includes(label)) state.missing.push(label);
177
+ }
178
+
179
+ function asHostPanel(value: unknown): HostPanelBuilders | undefined {
180
+ if (!isRecord(value)) return undefined;
181
+ const header = value.renderPanelHeader;
182
+ const row = value.renderPanelRow;
183
+ if (typeof header !== "function" || typeof row !== "function") return undefined;
184
+ // SAFETY: both members were just proved callable; the host's own signatures are the contract this
185
+ // adapter mirrors, and a member that misbehaves takes the same failure path as a local throw.
186
+ return value as unknown as HostPanelBuilders;
187
+ }
188
+
189
+ /** The frame's report: which drawing was used, what was looked for and what was found. */
190
+ export function getPanelReport(): PanelReport {
191
+ const state = panelState();
192
+ return {
193
+ source: state.builders === undefined ? "fallback" : "host",
194
+ lookedAt: [...state.lookedAt],
195
+ missing: state.builders === undefined ? [...state.missing] : [],
196
+ theme: state.theme === undefined ? "missing" : "captured",
197
+ };
198
+ }
199
+
200
+ // ---------------------------------------------------------------------------------------------
201
+ // The drawing
202
+ // ---------------------------------------------------------------------------------------------
203
+
204
+ /** Clips plain-or-ANSI `text` to `width` host columns with an ellipsis; SGR codes are kept. */
205
+ function clipPanelText(text: string, width: number): string {
206
+ if (width <= 0) return "";
207
+ if (hostColumns(text) <= width) return text;
208
+ let clipped = "";
209
+ for (const unit of text.match(/\u001b\[[0-9;]*m|[\s\S]/gu) ?? []) {
210
+ if (unit.startsWith("\u001b[")) {
211
+ clipped += unit;
212
+ continue;
213
+ }
214
+ if (hostColumns(clipped + unit) > width - 1) break;
215
+ clipped += unit;
216
+ }
217
+ return `${clipped}…`;
218
+ }
219
+
220
+ /**
221
+ * One author-written row, fitted to `width` HOST columns (N17): a row that already fits keeps its
222
+ * exact bytes (N10), a wider one is cut with an ellipsis, and nothing is ever appended — padding is
223
+ * the panel's business, and the panel pads to the same number the host measures.
224
+ */
225
+ export function clipSectionRow(row: string, width: number): string {
226
+ return clipPanelText(row, width);
227
+ }
228
+
229
+ /** `left` and `right` on one row exactly `width` host columns wide; `left` gives way first. */
230
+ function fitPanelLine(left: string, right: string, width: number): string {
231
+ const target = Math.max(0, Math.floor(width));
232
+ const rightWidth = hostColumns(right);
233
+ const shownRight = rightWidth + 1 <= target ? right : "";
234
+ const room = target - hostColumns(shownRight) - (shownRight ? 1 : 0);
235
+ const shownLeft = clipPanelText(left, room);
236
+ const pad = Math.max(0, target - hostColumns(shownLeft) - hostColumns(shownRight));
237
+ return `${shownLeft}${" ".repeat(pad)}${shownRight}`;
238
+ }
239
+
240
+ /** Paint `text` in the shared hover role when hovered, unchanged otherwise (host's own helper). */
241
+ export function paintHoverable(
242
+ theme: PanelTheme | undefined,
243
+ text: string,
244
+ hovered: boolean,
245
+ idleRole?: string,
246
+ ): string {
247
+ if (theme === undefined) return text;
248
+ if (hovered) return theme.fg(HOVER_ROLE, text);
249
+ return idleRole === undefined ? text : theme.fg(idleRole, text);
250
+ }
251
+
252
+ function paint(theme: PanelTheme | undefined, role: string, text: string): string {
253
+ return theme === undefined ? text : theme.fg(role, text);
254
+ }
255
+
256
+ /** The host's `renderPanelHeader`, or the host's own function when one was found. */
257
+ export function renderSectionHeader(
258
+ theme: PanelTheme | undefined,
259
+ width: number,
260
+ header: PanelHeader,
261
+ ): string {
262
+ const host = panelState().builders;
263
+ if (host !== undefined && theme !== undefined) return host.renderPanelHeader(theme, width, header);
264
+ const glyph = paintHoverable(theme, header.open ? "▾" : "▸", header.hovered ?? false, IDLE_META_ROLE);
265
+ const name = header.title.toUpperCase();
266
+ const bold = theme?.bold;
267
+ const painted = bold === undefined ? name : bold(name);
268
+ const title = paint(theme, header.hovered === true ? HOVER_ROLE : IDLE_TEXT_ROLE, painted);
269
+ const count = header.count === undefined ? "" : paint(theme, header.countRole ?? IDLE_META_ROLE, header.count);
270
+ return fitPanelLine(`${glyph} ${title}`, count, width);
271
+ }
272
+
273
+ /**
274
+ * One structured body row, composed the way the host's own `AGENTS` rows are
275
+ * (`lib/agents-widget.ts:400-407`): glyph and label in the row's `tone` (or the idle roles), the
276
+ * right-aligned `meta` in the dimmer `muted` role, and every part swapped to the hover role while
277
+ * the pointer is on it.
278
+ */
279
+ export function renderSectionRow(
280
+ theme: PanelTheme | undefined,
281
+ width: number,
282
+ row: SectionRow,
283
+ hovered: boolean,
284
+ ): string {
285
+ const tone = row.tone ?? IDLE_TEXT_ROLE;
286
+ const glyphRole = row.tone ?? IDLE_META_ROLE;
287
+ const glyph = row.glyph === undefined ? "" : `${paintHoverable(theme, row.glyph, hovered, glyphRole)} `;
288
+ const label = paintHoverable(theme, row.label, hovered, tone);
289
+ const meta = row.meta === undefined ? "" : paintHoverable(theme, row.meta, hovered, IDLE_META_ROLE);
290
+ const left = `${glyph}${label}`;
291
+ const host = panelState().builders;
292
+ if (host !== undefined && theme !== undefined) {
293
+ return host.renderPanelRow(width, left, meta, PANEL_ROW_INDENT);
294
+ }
295
+ return fitPanelLine(`${" ".repeat(PANEL_ROW_INDENT)}${left}`, meta, width);
296
+ }
297
+
298
+ // ---------------------------------------------------------------------------------------------
299
+ // Row validation (N10)
300
+ // ---------------------------------------------------------------------------------------------
301
+
302
+ /** One accepted body row: a plain string, or a validated structured row. */
303
+ export type AcceptedRow = string | SectionRow;
304
+
305
+ export type RowCheck = { ok: true; rows: AcceptedRow[] } | { ok: false; message: string };
306
+
307
+ /**
308
+ * Validate what a plugin's `lines()` returned, entry by entry. Nothing is coerced and nothing is
309
+ * dropped silently: the first bad entry fails the whole body with a message naming the rule, exactly
310
+ * as a non-string row did before structured rows existed.
311
+ */
312
+ export function checkSectionRows(value: unknown): RowCheck {
313
+ if (!Array.isArray(value)) {
314
+ return { ok: false, message: "lines() must return an array of rows (a string or a { label } object)" };
315
+ }
316
+ // Index by index, NOT `every`/`map`: those skip array holes, and a hole reaches the clipping code
317
+ // as `undefined` (B1).
318
+ const rows: unknown[] = value;
319
+ const accepted: AcceptedRow[] = [];
320
+ for (let index = 0; index < rows.length; index += 1) {
321
+ if (!Object.hasOwn(rows, index)) {
322
+ return { ok: false, message: "lines() must not return a sparse array" };
323
+ }
324
+ const row: unknown = rows[index];
325
+ if (typeof row === "string") {
326
+ accepted.push(row);
327
+ continue;
328
+ }
329
+ const checked = checkSectionRow(row);
330
+ if (!checked.ok) return checked;
331
+ accepted.push(checked.row);
332
+ }
333
+ return { ok: true, rows: accepted };
334
+ }
335
+
336
+ type SingleRowCheck = { ok: true; row: SectionRow } | { ok: false; message: string };
337
+
338
+ function checkSectionRow(row: unknown): SingleRowCheck {
339
+ if (!isRecord(row)) {
340
+ return {
341
+ ok: false,
342
+ message: "lines() must return an array of rows (a string or a { label } object)",
343
+ };
344
+ }
345
+ if (typeof row.label !== "string") {
346
+ return { ok: false, message: "a section row's label must be a string" };
347
+ }
348
+ if (row.meta !== undefined && typeof row.meta !== "string") {
349
+ return { ok: false, message: "a section row's meta must be a string" };
350
+ }
351
+ if (row.glyph !== undefined && typeof row.glyph !== "string") {
352
+ return { ok: false, message: "a section row's glyph must be a string" };
353
+ }
354
+ const onClick = row.onClick;
355
+ if (onClick !== undefined && typeof onClick !== "function") {
356
+ return { ok: false, message: "a section row's onClick must be a function" };
357
+ }
358
+ if (row.tone !== undefined && !isTone(row.tone)) {
359
+ return {
360
+ ok: false,
361
+ message: `a section row's tone must be one of ${TONES.join(", ")} (a semantic role, never a colour)`,
362
+ };
363
+ }
364
+ return {
365
+ ok: true,
366
+ row: {
367
+ label: row.label,
368
+ ...(row.meta === undefined ? {} : { meta: row.meta }),
369
+ ...(row.glyph === undefined ? {} : { glyph: row.glyph }),
370
+ ...(row.tone === undefined ? {} : { tone: row.tone }),
371
+ ...(onClick === undefined ? {} : { onClick: onClick as () => void }),
372
+ },
373
+ };
374
+ }
375
+
376
+ function isTone(value: unknown): value is Tone {
377
+ return TONES.includes(value as Tone);
378
+ }
@@ -0,0 +1,164 @@
1
+ /**
2
+ * The pointer claim (N12): one hovered surface at a time, the way the host does it.
3
+ *
4
+ * ## What the host does, and what of it is reachable
5
+ *
6
+ * gentle-shell's parts wrap themselves in `NativePointerRegion` and claim the shared pointer with
7
+ * `setSidebarHover(tui, owner, clear)` (`lib/shell-sidebar.ts:100-108`), which stores
8
+ * `{ owner, clear }` on the terminal's sidebar state and calls the PREVIOUS owner's `clear()` when
9
+ * the owner changes. Moving the pointer off the rail calls `setSidebarHover(tui)` with no owner,
10
+ * which clears the current one.
11
+ *
12
+ * The function itself is not reachable — gentle-shell is not a dependency and Pi loads it in its own
13
+ * jiti module graph — but the SLOT it writes is: `SidebarState.hover` is declared on the
14
+ * process-global state the bridge already intercepts, and its own comment says it is "shared across
15
+ * separately loaded extensions" (`lib/shell-sidebar.ts:37`). So this module performs the same two
16
+ * steps against the same published state, exactly as `./repaint.ts` bumps the host's cache revision
17
+ * rather than importing `invalidateSidebar`:
18
+ *
19
+ * 1. read the terminal's sidebar state (`./sidebar.ts`'s `hostSidebarStateOf`, the seam the backend
20
+ * already reads for `parts`);
21
+ * 2. store `{ owner, clear }`, clearing the previous owner first — the host's own semantics.
22
+ *
23
+ * What is NOT reachable is `NativePointerRegion`, and it is not needed — but its behaviour is what
24
+ * decides who sees an event first. The host dispatches a hit on the part to the part's own
25
+ * `handleMouse` with the coordinates already mapped into the part's frame
26
+ * (`lib/shell-sidebar-layout.ts:245-259`), which is where the section pointer handler reads them.
27
+ * That part handler is a `NativePointerRegion` around the host's own card
28
+ * (`extensions/gentle-shell.ts:2766`), whose `onHover` claims or clears the pointer and always
29
+ * returns `{ handled: true }` (`extensions/gentle-shell.ts:2732-2741`). So a MOVE over a row the
30
+ * bridge appended is consumed by the host unless the rail wrapper offers it to the contributor
31
+ * first; `./sidebar.ts`'s `mouseThrough` does exactly that for the rows the contributor rendered,
32
+ * and keeps the host first for every other event.
33
+ *
34
+ * This module is internal: `src/index.ts` does not re-export it.
35
+ */
36
+
37
+ import { getRepaintHost } from "./repaint.ts";
38
+ import { isRecord } from "./rows.ts";
39
+ import { hostSidebarStateOf } from "./sidebar.ts";
40
+
41
+ /** How a hover claim is carried, as the diagnostics record reports it. */
42
+ export type HoverMechanism = "host-state" | "unavailable";
43
+
44
+ /** The published field's shape (`SidebarState.hover`, `lib/shell-sidebar.ts:37`). */
45
+ interface HoverSlot {
46
+ owner: object;
47
+ clear: () => void;
48
+ }
49
+
50
+ interface PointerState {
51
+ /** The token this package claims the pointer with; stable, so a second claim is a no-op. */
52
+ owner: object;
53
+ lastMechanism?: HoverMechanism;
54
+ lastReason?: string;
55
+ }
56
+
57
+ const POINTER_STATE = Symbol.for("gentle-shell-bridge.section-pointer.state.v1");
58
+
59
+ function pointerHost(): Record<symbol, PointerState | undefined> {
60
+ // SAFETY: only `pointerState()` writes this key, and it re-checks the shape before trusting it.
61
+ return globalThis as unknown as Record<symbol, PointerState | undefined>;
62
+ }
63
+
64
+ function pointerState(): PointerState {
65
+ const host = pointerHost();
66
+ const existing = host[POINTER_STATE];
67
+ if (existing !== undefined && typeof existing.owner === "object") return existing;
68
+ const created: PointerState = { owner: {} };
69
+ host[POINTER_STATE] = created;
70
+ return created;
71
+ }
72
+
73
+ /** Test-only seam; deliberately not re-exported from `src/index.ts`. */
74
+ export function resetSectionPointerForTests(): void {
75
+ const state = pointerState();
76
+ delete state.lastMechanism;
77
+ delete state.lastReason;
78
+ }
79
+
80
+ interface HostHoverSlot {
81
+ state: Record<string, unknown>;
82
+ }
83
+
84
+ /** The terminal's sidebar state, or `undefined` when no `tui` has been captured yet. */
85
+ function hostHoverSlot(): HostHoverSlot | undefined {
86
+ const tui = getRepaintHost();
87
+ if (tui === undefined) return undefined;
88
+ const state = hostSidebarStateOf(tui);
89
+ return state === undefined ? undefined : { state };
90
+ }
91
+
92
+ function readHover(state: Record<string, unknown>): HoverSlot | undefined {
93
+ const hover = state.hover;
94
+ if (!isRecord(hover)) return undefined;
95
+ const owner = hover.owner;
96
+ const clear = hover.clear;
97
+ if (typeof owner !== "object" || owner === null) return undefined;
98
+ if (typeof clear !== "function") return undefined;
99
+ return { owner, clear: clear as () => void };
100
+ }
101
+
102
+ /**
103
+ * Claim the shared pointer for the section rows. `clear` runs when another surface takes the pointer
104
+ * or the host moves it off the rail, which is how a hovered row stops being hovered without the
105
+ * host knowing anything about section rows.
106
+ *
107
+ * Never throws: a terminal that cannot carry the slot is reported, and the section still paints — it
108
+ * only loses the "someone else took the pointer" notification.
109
+ */
110
+ export function claimSectionHover(clear: () => void): HoverMechanism {
111
+ const state = pointerState();
112
+ const slot = hostHoverSlot();
113
+ if (slot === undefined) {
114
+ state.lastMechanism = "unavailable";
115
+ state.lastReason =
116
+ "no terminal sidebar state is reachable yet, so the shared pointer slot cannot be claimed";
117
+ return "unavailable";
118
+ }
119
+ try {
120
+ const current = readHover(slot.state);
121
+ // The host's own rule: the same owner keeps the claim without clearing itself.
122
+ if (current?.owner === state.owner) {
123
+ state.lastMechanism = "host-state";
124
+ delete state.lastReason;
125
+ return "host-state";
126
+ }
127
+ slot.state.hover = { owner: state.owner, clear };
128
+ current?.clear();
129
+ state.lastMechanism = "host-state";
130
+ delete state.lastReason;
131
+ return "host-state";
132
+ } catch (error) {
133
+ state.lastMechanism = "unavailable";
134
+ state.lastReason = `the host's pointer slot rejected the claim: ${errorMessage(error)}`;
135
+ return "unavailable";
136
+ }
137
+ }
138
+
139
+ /** Release the claim if it is still ours; another surface's claim is left alone. */
140
+ export function releaseSectionHover(): void {
141
+ const slot = hostHoverSlot();
142
+ if (slot === undefined) return;
143
+ try {
144
+ const current = readHover(slot.state);
145
+ if (current === undefined || current.owner !== pointerState().owner) return;
146
+ delete slot.state.hover;
147
+ } catch {
148
+ // A terminal that cannot be read cannot hold our claim either; nothing to release.
149
+ }
150
+ }
151
+
152
+ /** The mechanism the last claim used, as the diagnostics record reports it. */
153
+ export function getSectionHoverReport(): { mechanism: HoverMechanism | "not used"; reason?: string } {
154
+ const state = pointerState();
155
+ if (state.lastMechanism === undefined) return { mechanism: "not used" };
156
+ return {
157
+ mechanism: state.lastMechanism,
158
+ ...(state.lastReason === undefined ? {} : { reason: state.lastReason }),
159
+ };
160
+ }
161
+
162
+ function errorMessage(error: unknown): string {
163
+ return error instanceof Error ? error.message : String(error);
164
+ }