pi-odd-worktree 3.15.1 → 3.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -25
- package/extensions/odd-worktree-tools.ts +70 -212
- package/lib/shell-api/api.ts +523 -0
- package/lib/shell-api/arm.ts +83 -0
- package/lib/shell-api/bus.ts +1389 -0
- package/lib/shell-api/capture.ts +1340 -0
- package/lib/shell-api/card-style-line.ts +112 -0
- package/lib/shell-api/card-style-rows.ts +390 -0
- package/lib/shell-api/card-style-transform.ts +862 -0
- package/lib/shell-api/card-style-wiring.ts +477 -0
- package/lib/shell-api/card-styles.ts +153 -0
- package/lib/shell-api/config.ts +71 -0
- package/lib/shell-api/extension.ts +247 -0
- package/lib/shell-api/hook.ts +322 -0
- package/lib/shell-api/index.ts +88 -0
- package/lib/shell-api/message-design-adoption.ts +549 -0
- package/lib/shell-api/message-design-rows.ts +313 -0
- package/lib/shell-api/message-design-wiring.ts +369 -0
- package/lib/shell-api/message-designs.ts +746 -0
- package/lib/shell-api/notices.ts +137 -0
- package/lib/shell-api/plugin-lines.ts +877 -0
- package/lib/shell-api/presence.ts +258 -0
- package/lib/shell-api/repaint.ts +351 -0
- package/lib/shell-api/row-labels.ts +146 -0
- package/lib/shell-api/row-names.ts +273 -0
- package/lib/shell-api/row-origin.ts +144 -0
- package/lib/shell-api/rows.ts +591 -0
- package/lib/shell-api/section-panel.ts +378 -0
- package/lib/shell-api/section-pointer.ts +164 -0
- package/lib/shell-api/sections-preview.ts +275 -0
- package/lib/shell-api/sections-rows.ts +133 -0
- package/lib/shell-api/sidebar-render.ts +705 -0
- package/lib/shell-api/sidebar.ts +1126 -0
- package/lib/shell-api/store.ts +279 -0
- package/lib/shell-api/tool-design-adoption.ts +263 -0
- package/lib/shell-api/tool-design-rows.ts +307 -0
- package/lib/shell-api/tool-design-wiring.ts +481 -0
- package/lib/shell-api/tool-designs.ts +1411 -0
- package/lib/shell-api/transform-pipeline.ts +351 -0
- package/lib/shell-api/types.ts +803 -0
- package/lib/shell-api/visibility.ts +154 -0
- package/package.json +3 -2
- package/tools/files-bridge.test.js +81 -66
- package/tools/fixtures/load-extension.js +40 -18
- package/tools/issues-command.test.js +4 -7
- package/tools/issues-section.test.js +71 -81
- package/tools/odd-rail-wiring.test.js +16 -10
- package/tools/package-surface.test.js +42 -13
- package/tools/prs-command.test.js +11 -6
- package/tools/session-auto-name-wiring.test.js +39 -0
- package/tools/shell-api/api.test.ts +484 -0
- package/tools/shell-api/apply-on-move.test.ts +529 -0
- package/tools/shell-api/arm.test.ts +220 -0
- package/tools/shell-api/bridge-ready.test.ts +309 -0
- package/tools/shell-api/bus.test.ts +194 -0
- package/tools/shell-api/button-rows.test.ts +435 -0
- package/tools/shell-api/capture.test.ts +583 -0
- package/tools/shell-api/card-style-rows.test.ts +493 -0
- package/tools/shell-api/card-style-transform.test.ts +379 -0
- package/tools/shell-api/card-style-wiring.test.ts +258 -0
- package/tools/shell-api/card-styles-e2e.test.ts +502 -0
- package/tools/shell-api/card-styles.test.ts +324 -0
- package/tools/shell-api/card-transcript-path.test.ts +178 -0
- package/tools/shell-api/compatibility-regression.test.mjs +64 -0
- package/tools/shell-api/config.test.ts +93 -0
- package/tools/shell-api/docs-coverage.test.ts +82 -0
- package/tools/shell-api/e2e.test.ts +562 -0
- package/tools/shell-api/example-bash-identity.test.ts +372 -0
- package/tools/shell-api/example-bash-matrix.test.ts +856 -0
- package/tools/shell-api/example-designs.test.ts +737 -0
- package/tools/shell-api/example-neon-transform.test.ts +283 -0
- package/tools/shell-api/example-plain-style.test.ts +1225 -0
- package/tools/shell-api/extension.test.ts +157 -0
- package/tools/shell-api/fake-events.ts +58 -0
- package/tools/shell-api/fake-host.ts +211 -0
- package/tools/shell-api/fake-sidebar.ts +516 -0
- package/tools/shell-api/fake-transcript.ts +486 -0
- package/tools/shell-api/frame-telemetry.test.ts +317 -0
- package/tools/shell-api/hook.test.ts +196 -0
- package/tools/shell-api/manifest.test.ts +47 -0
- package/tools/shell-api/message-design-adoption.test.ts +611 -0
- package/tools/shell-api/message-design-follows.test.ts +361 -0
- package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
- package/tools/shell-api/message-design-regressions.test.mjs +162 -0
- package/tools/shell-api/message-design-rows.test.ts +266 -0
- package/tools/shell-api/message-design-wiring.test.ts +362 -0
- package/tools/shell-api/message-designs.test.ts +339 -0
- package/tools/shell-api/new-category.test.ts +1101 -0
- package/tools/shell-api/notices.test.ts +292 -0
- package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
- package/tools/shell-api/presence.test.ts +396 -0
- package/tools/shell-api/rail-part-hits.test.ts +281 -0
- package/tools/shell-api/rail-surfaces.test.ts +402 -0
- package/tools/shell-api/rail-width.test.ts +406 -0
- package/tools/shell-api/regression.test.ts +434 -0
- package/tools/shell-api/repaint-api.test.ts +200 -0
- package/tools/shell-api/reserved-categories.test.ts +250 -0
- package/tools/shell-api/row-labels.test.ts +261 -0
- package/tools/shell-api/row-names.test.ts +407 -0
- package/tools/shell-api/row-origin.test.ts +395 -0
- package/tools/shell-api/row-placement-regression.test.ts +797 -0
- package/tools/shell-api/row-placement.test.ts +410 -0
- package/tools/shell-api/section-interaction.test.ts +364 -0
- package/tools/shell-api/section-panel.test.ts +430 -0
- package/tools/shell-api/section-placement-rail.test.ts +390 -0
- package/tools/shell-api/section-placement.test.ts +386 -0
- package/tools/shell-api/sections-preview.test.ts +312 -0
- package/tools/shell-api/sections-rows.test.ts +293 -0
- package/tools/shell-api/sidebar-backend.test.ts +477 -0
- package/tools/shell-api/sidebar-e2e.test.ts +317 -0
- package/tools/shell-api/sidebar-hover.test.ts +276 -0
- package/tools/shell-api/sidebar-independent.test.ts +291 -0
- package/tools/shell-api/sidebar-render.test.ts +281 -0
- package/tools/shell-api/sidebar-sections.test.ts +247 -0
- package/tools/shell-api/store.test.ts +192 -0
- package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
- package/tools/shell-api/tool-design-card-style.test.ts +308 -0
- package/tools/shell-api/tool-design-chain.test.ts +471 -0
- package/tools/shell-api/tool-design-cost.test.ts +591 -0
- package/tools/shell-api/tool-design-rows.test.ts +627 -0
- package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
- package/tools/shell-api/tool-designs.test.ts +2412 -0
- package/tools/shell-api/transform-pipeline.test.ts +376 -0
- package/tools/shell-api/transforms-bus.test.ts +233 -0
- package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
- package/tools/shell-api-rpc.test.js +189 -0
- package/tools/user-prompt-card.test.js +8 -9
|
@@ -0,0 +1,1126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sidebar rail interception (S4, S9): the backend that lets the bridge contribute to
|
|
3
|
+
* gentle-shell's fullscreen rail.
|
|
4
|
+
*
|
|
5
|
+
* ## Why the rail map, and not an API
|
|
6
|
+
*
|
|
7
|
+
* The rail is painted from ONE process-global map: `Symbol.for("gentle-pi.experimental-sidebar.state")`
|
|
8
|
+
* on the terminal object → `state.parts`, a `Map<string, SidebarRail>` (`lib/shell-sidebar.ts:6,78-90`).
|
|
9
|
+
* The layout paints only the hard-coded keys `["todo","jobs","agents","footer"]`
|
|
10
|
+
* (`lib/shell-sidebar-layout.ts:344`), so a new key never renders: contributing rows to one of those
|
|
11
|
+
* parts is the only way in. The bridge wraps ALL FOUR, because a section may ask to sit before the
|
|
12
|
+
* first one, after one of the middle ones, or after the last (`SIDEBAR_RAIL_KEY`); the status
|
|
13
|
+
* sections themselves live in a closure (`lib/shell-bar.ts:247,251-316`) we cannot reach — the
|
|
14
|
+
* documented limit, and the reason "after a status section" is not offered.
|
|
15
|
+
*
|
|
16
|
+
* ## Order independence (S4)
|
|
17
|
+
*
|
|
18
|
+
* The host registers its parts itself (`extensions/gentle-shell.ts:2827` for `footer`, `:2895` for
|
|
19
|
+
* `jobs`, `extensions/gentle-todo.ts:292` for `todo`, `extensions/gentle-agents.ts:2500` for
|
|
20
|
+
* `agents`), so our arming may run before OR after any of them. The rail itself is therefore not the
|
|
21
|
+
* seam; the MAP is. On install we patch the map instance's own `set` and `get`:
|
|
22
|
+
*
|
|
23
|
+
* - `set` wraps the value when the key is one of the four painted parts, so a registration that
|
|
24
|
+
* happens after our arm is captured with no polling;
|
|
25
|
+
* - `get` wraps and stores on read, so a value written by a path that skipped our `set` (foreign
|
|
26
|
+
* code calling `Map.prototype.set`, a copy loaded before us) still arrives wrapped, and every
|
|
27
|
+
* later read returns the SAME wrapper object — stable identity matters, because the layout's memo
|
|
28
|
+
* compares the component and foreign readers compare `parts.get(key).spend` results.
|
|
29
|
+
*
|
|
30
|
+
* **Why patch the instance instead of subclassing or replacing the map:** `sidebarPart` and every
|
|
31
|
+
* other reader re-reads `state.parts` from the state object, but nothing in the host forbids a
|
|
32
|
+
* third-party extension from holding its own reference to the map it got earlier. A replacement
|
|
33
|
+
* `Map` would leave those holders writing into a copy nobody intercepts, and a `Map` subclass cannot
|
|
34
|
+
* be created from an existing map without changing its identity. Patching the instance's own
|
|
35
|
+
* `set`/`get` (non-enumerable, configurable) keeps the identity every holder already has, keeps the
|
|
36
|
+
* prototype's methods intact, and — unlike a prototype patch — is scoped to the one host map.
|
|
37
|
+
* Restoring is exact: the own property is deleted, or the own descriptor that was there before is
|
|
38
|
+
* put back.
|
|
39
|
+
*
|
|
40
|
+
* ## Wrapping a rail
|
|
41
|
+
*
|
|
42
|
+
* The wrapper spreads the host rail (`{ ...rail, render, cardGroups, handleMouse }`) so `digest`,
|
|
43
|
+
* `spend`, `surface`, `summary`, `open`, `alert`, `invalidate` and every other field keep reaching
|
|
44
|
+
* their readers with their identity untouched. `digest` is specifically NOT wrapped: the layout memo
|
|
45
|
+
* keys on its value, and a wrapper that recomputed it would defeat the memo. The host's rails are
|
|
46
|
+
* object literals (`extensions/gentle-shell.ts:2827`, `extensions/gentle-agents.ts:2500`), so an
|
|
47
|
+
* own-property spread carries every field; a class instance whose methods live on the prototype
|
|
48
|
+
* would need a prototype-preserving wrapper, which no host rail requires today.
|
|
49
|
+
*
|
|
50
|
+
* `render`/`cardGroups` delegate first and only then add — our rows around the host's own, never
|
|
51
|
+
* instead of them. `handleMouse` delegates every event to the host's own handler with the host's own
|
|
52
|
+
* `y` (its frame starts after our BEFORE rows), and answers an event that lands on a row this
|
|
53
|
+
* contributor rendered itself — because the host's part handler never declines a move (see
|
|
54
|
+
* `mouseThrough`). Every other event reaches the contributor only where the host declined, so the
|
|
55
|
+
* host's own targets always win.
|
|
56
|
+
*
|
|
57
|
+
* ## Markers and idempotency
|
|
58
|
+
*
|
|
59
|
+
* Same discipline as the row capture backend (`./capture.ts`): a marker certifies the EXACT function
|
|
60
|
+
* or object it names, is read as an OWN property, and never through the prototype chain.
|
|
61
|
+
*
|
|
62
|
+
* - `PARTS_INTERCEPTED` (own property on the map) → the exact `set` function we installed. A second
|
|
63
|
+
* install, a rebind or a second copy of this package sees it and reports `already-intercepted`
|
|
64
|
+
* instead of stacking a second layer.
|
|
65
|
+
* - `RAIL_WRAPPED` (own property on the wrapper) → the exact host rail object it wraps. That makes
|
|
66
|
+
* wrapping idempotent (re-wrapping our own wrapper would double every contributed group) and
|
|
67
|
+
* leaves a FOREIGN wrapper in place: we wrap around it, never replace it.
|
|
68
|
+
*
|
|
69
|
+
* ## Disable and foreign references
|
|
70
|
+
*
|
|
71
|
+
* `disableSidebarBackend()` puts the host's rail back and unpatches the map, then bumps the
|
|
72
|
+
* generation. Every method the wrapper exposes captures the generation it was built in and degrades
|
|
73
|
+
* to a plain pass-through afterwards, so a wrapper that foreign code kept a reference to cannot
|
|
74
|
+
* contribute rows, groups or mouse targets once the backend is off.
|
|
75
|
+
*
|
|
76
|
+
* ## Getting a real `tui` (open wiring question)
|
|
77
|
+
*
|
|
78
|
+
* A rail state is reached through a `tui`: `sidebarState(tui)` reads `tui.terminal`
|
|
79
|
+
* (`lib/shell-sidebar.ts:78-80`). Pi does not expose a `tui` getter: `ExtensionUIContext` has no such
|
|
80
|
+
* member (verified in `dist/core/extensions/types.d.ts` — `TUI` appears only as a factory parameter).
|
|
81
|
+
* The only surfaces that hand an extension's factory the real TUI are
|
|
82
|
+
* `ctx.ui.setWidget(key, factory)` (`interactive-mode.js:1998`, `content(this.ui, theme)`),
|
|
83
|
+
* `setFooter` (`:2084`), `setHeader`, `custom` and `setEditorComponent`, and each installs a rendered
|
|
84
|
+
* component. So this backend takes the rail state from a RESOLVER the caller supplies
|
|
85
|
+
* (`installSidebarBackend(() => tuiLike)`), which keeps the eventual acquisition path — a widget
|
|
86
|
+
* factory that stores the `tui` it receives and then yields a component rendering no rows — a wiring
|
|
87
|
+
* detail rather than a rewrite. Whether such an empty component leaves the editor area untouched is
|
|
88
|
+
* UNCONFIRMED: it has not been observed in a running Pi TUI.
|
|
89
|
+
*
|
|
90
|
+
* This module is internal: `src/index.ts` does not re-export it.
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
import { isRecord } from "./rows.ts";
|
|
94
|
+
import { notePanelCandidate } from "./section-panel.ts";
|
|
95
|
+
import { RAIL_PARTS } from "./types.ts";
|
|
96
|
+
import type { RailPart } from "./types.ts";
|
|
97
|
+
|
|
98
|
+
/** The host's own state key: the seam this backend intercepts. Never our own state. */
|
|
99
|
+
const HOST_STATE = Symbol.for("gentle-pi.experimental-sidebar.state");
|
|
100
|
+
|
|
101
|
+
/** Process-global backend state, shared across copies like the bus (jiti's `moduleCache: false`). */
|
|
102
|
+
const BACKEND_STATE = Symbol.for("gentle-shell-bridge.sidebar.state.v1");
|
|
103
|
+
/** Own property on an intercepted map → the exact `set` function this module installed. */
|
|
104
|
+
const PARTS_INTERCEPTED = Symbol.for("gentle-shell-bridge.sidebar.parts.v1");
|
|
105
|
+
/** Own property on a wrapper rail → the exact host rail object it wraps. */
|
|
106
|
+
const RAIL_WRAPPED = Symbol.for("gentle-shell-bridge.sidebar.rail.v1");
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The part that ends the rail, and the one `"end"` means. The layout paints
|
|
110
|
+
* `todo`, `jobs`, `agents`, `footer` in that order (`lib/shell-sidebar-layout.ts:344`), so the footer
|
|
111
|
+
* is after every native section. Every painted part is wrapped (a section may ask for `"start"` or
|
|
112
|
+
* `after` any of them); this key stays the one the diagnostics call "the rail".
|
|
113
|
+
*/
|
|
114
|
+
export const SIDEBAR_RAIL_KEY = "footer";
|
|
115
|
+
|
|
116
|
+
/** Whether a map key is a part the layout paints, and therefore one the bridge wraps. */
|
|
117
|
+
function isPaintedRailKey(key: string): key is RailPart {
|
|
118
|
+
return (RAIL_PARTS as readonly string[]).includes(key);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Row ranges of the section cards a part draws, as the host's `cardGroups` returns them. */
|
|
122
|
+
export interface RailGroupLayout {
|
|
123
|
+
groups: Array<{ start: number; end: number }>;
|
|
124
|
+
loose?: number[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** What a pointer handler answers: the host's `{ handled, render }`, or our target's own result. */
|
|
128
|
+
export interface RailMouseResult {
|
|
129
|
+
handled?: boolean;
|
|
130
|
+
render?: boolean;
|
|
131
|
+
[key: string]: unknown;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The part of a rail this backend depends on. Every OTHER own enumerable string-keyed property of
|
|
136
|
+
* the host's rail is carried through the wrapper's spread — in practice `digest`, `spend`,
|
|
137
|
+
* `surface`, `summary`, `open`, `alert` and `invalidate` — but "every field" would be false: a
|
|
138
|
+
* non-enumerable property, a method that lives on the prototype, and a getter (snapshotted once at
|
|
139
|
+
* wrap time) do not survive the spread. Every host rail observed so far is an object literal
|
|
140
|
+
* Every host rail observed so far is an object literal
|
|
141
|
+
* (`extensions/gentle-shell.ts:2827`, `extensions/gentle-agents.ts:2500`), so nothing it carries is
|
|
142
|
+
* lost; `test/sidebar-independent.test.ts` pins the limit rather than leaving it to prose.
|
|
143
|
+
*/
|
|
144
|
+
export interface SidebarRailLike {
|
|
145
|
+
render(width: number): string[];
|
|
146
|
+
cardGroups?(width: number): RailGroupLayout | undefined;
|
|
147
|
+
/** The `Component`'s normalized mouse handler (`pi-tui` `Component.handleMouse`). */
|
|
148
|
+
handleMouse?(event: unknown): RailMouseResult | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Cheap live-state digest; the layout memo skips re-renders while it holds (`digest !== undefined
|
|
151
|
+
* ? existing.digest === digest : … cache.revision`, `lib/shell-sidebar-layout.ts:114-119`).
|
|
152
|
+
*
|
|
153
|
+
* A WRAPPER may answer `undefined` when neither the host nor a contributor has a signature: the
|
|
154
|
+
* layout's reader (`railDigest`) treats that as "no digest" and falls back to the revision, which
|
|
155
|
+
* is exactly how a rail without this member behaves. That case must stay distinguishable, so the
|
|
156
|
+
* option is typed here rather than hidden behind a cast.
|
|
157
|
+
*/
|
|
158
|
+
digest?(): string | undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A value in the host's rail map. The host types it as a map of rails; the bridge hands anything it
|
|
163
|
+
* cannot wrap back unchanged instead of claiming every value is a rail, so a foreign write cannot
|
|
164
|
+
* crash a read.
|
|
165
|
+
*/
|
|
166
|
+
export type RailMapValue = SidebarRailLike | Record<string, unknown> | undefined;
|
|
167
|
+
|
|
168
|
+
/** What a contributor adds to ONE wrapped part: rows around the host's own. */
|
|
169
|
+
export interface RailContribution {
|
|
170
|
+
lines: string[];
|
|
171
|
+
/** Card groups for `lines`, as row ranges RELATIVE to `lines[0]`; the wrapper offsets them. */
|
|
172
|
+
groups: Array<{ start: number; end: number }>;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** One part's contribution: a block drawn above the host's rows and/or a block drawn below them. */
|
|
176
|
+
export interface RailPartContribution {
|
|
177
|
+
/** Drawn BEFORE the host's own rows. Absent means nothing above them. */
|
|
178
|
+
before?: RailContribution;
|
|
179
|
+
/** Drawn AFTER the host's own rows. Absent means nothing below them. */
|
|
180
|
+
after?: RailContribution;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** What a contributor is told about the frame it is being asked to contribute to. */
|
|
184
|
+
export interface RailContributorContext {
|
|
185
|
+
/** The rail part currently rendering (`todo`, `jobs`, `agents`, `footer`). */
|
|
186
|
+
part: RailPart;
|
|
187
|
+
/**
|
|
188
|
+
* The parts the host paints, in paint order — what a placement resolves against (S5). A part that
|
|
189
|
+
* is absent from the map, hidden by `state.visibility.todo === false`, or that rendered no rows in
|
|
190
|
+
* the previous frame is not painted, and an anchor on it falls forward to the next painted one.
|
|
191
|
+
*/
|
|
192
|
+
painted: readonly RailPart[];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The extension point the section renderer plugs into (T4). It knows nothing about sections: it
|
|
197
|
+
* contributes rows around one part's own, their card groups, and may answer mouse events over them.
|
|
198
|
+
*/
|
|
199
|
+
export interface RailContributor {
|
|
200
|
+
render(width: number, context: RailContributorContext): RailPartContribution | undefined;
|
|
201
|
+
/**
|
|
202
|
+
* A cheap signature of the live state the contributed rows paint (N14). The layout re-renders a
|
|
203
|
+
* part only when its digest changes, so a row whose `meta` moved between frames must move this
|
|
204
|
+
* value — otherwise the host would keep the cached rows and the field would look frozen.
|
|
205
|
+
*/
|
|
206
|
+
digest?(context: RailContributorContext): string;
|
|
207
|
+
/**
|
|
208
|
+
* Our own mouse targets. An event over a row this contributor rendered is offered here FIRST (the
|
|
209
|
+
* host's own part handler answers every move inside its frame, so waiting for it to decline would
|
|
210
|
+
* never reach us); every other event is offered here only after the host's own handler declined,
|
|
211
|
+
* so the host's own targets win wherever it has one. The event's `y` is in the PART's frame, so
|
|
212
|
+
* `position` says which of those rows are this contributor's.
|
|
213
|
+
*/
|
|
214
|
+
handleMouse?(event: unknown, position?: RailContributionPosition): RailMouseResult | undefined;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A half-open range of rows in one part's rendered frame: `[start, end)`. */
|
|
218
|
+
export interface RailRowRange {
|
|
219
|
+
start: number;
|
|
220
|
+
end: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Where a contributor's rows sit inside the part's rendered frame (S7). */
|
|
224
|
+
export interface RailContributionPosition {
|
|
225
|
+
/** The part whose frame the event's `y` counts. */
|
|
226
|
+
part: string;
|
|
227
|
+
/**
|
|
228
|
+
* The width the event was mapped at for this part — the width whose memo entry produced the ranges
|
|
229
|
+
* below. A contributor records its rows per part AND per width, so an event that does not match
|
|
230
|
+
* the width its rows were drawn at is declined instead of mapped onto a stale offset. Optional: a
|
|
231
|
+
* position produced by an older copy carries no width, and a contributor that checks it declines
|
|
232
|
+
* exactly as it does for its own parts it never rendered.
|
|
233
|
+
*/
|
|
234
|
+
width?: number;
|
|
235
|
+
/** The host's own rows in that frame. */
|
|
236
|
+
native?: RailRowRange;
|
|
237
|
+
/**
|
|
238
|
+
* The rows this contributor drew ABOVE the host's own. A row range, not an offset: contributed
|
|
239
|
+
* rows can now sit on either side of the host's, so "everything after the host's rows" is no
|
|
240
|
+
* longer the shape of a contribution.
|
|
241
|
+
*/
|
|
242
|
+
before?: RailRowRange;
|
|
243
|
+
/** The rows this contributor drew BELOW the host's own. */
|
|
244
|
+
after?: RailRowRange;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* A `tui`-like object: all this backend reads is `terminal`, which carries the host's rail state. The
|
|
249
|
+
* index signature keeps the host's other members irrelevant instead of demanding an exact shape.
|
|
250
|
+
*/
|
|
251
|
+
export interface SidebarTuiLike {
|
|
252
|
+
terminal?: unknown;
|
|
253
|
+
[key: string]: unknown;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Supplies the `tui`-like object whose `terminal` carries the host's rail state. Deliberately a
|
|
258
|
+
* function: the real acquisition path may only have a `tui` later (see the module comment).
|
|
259
|
+
*/
|
|
260
|
+
export type SidebarTuiResolver = () => SidebarTuiLike | undefined;
|
|
261
|
+
|
|
262
|
+
export type SidebarInstallOutcome =
|
|
263
|
+
| "intercepted"
|
|
264
|
+
| "already-intercepted"
|
|
265
|
+
| "no-sidebar-state"
|
|
266
|
+
| "resolver-threw";
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* What the wiring last recorded: an install attempt's outcome, or a problem that prevented one (no
|
|
270
|
+
* `setWidget` surface, or a factory that has not run yet). the diagnostics record reports the difference
|
|
271
|
+
* between "we could not reach a rail" and "we never had a way to try".
|
|
272
|
+
*/
|
|
273
|
+
export type SidebarRecordedOutcome = SidebarInstallOutcome | "no-widget-surface";
|
|
274
|
+
|
|
275
|
+
/** What one install attempt did, so the caller and the diagnostics record can report it. */
|
|
276
|
+
export interface InstallSidebarResult {
|
|
277
|
+
installed: boolean;
|
|
278
|
+
outcome: SidebarInstallOutcome;
|
|
279
|
+
/** Whether the part `"end"` draws in (the host's `footer`) was already in the map when we looked. */
|
|
280
|
+
rail: "wrapped" | "pending";
|
|
281
|
+
/** Only for `resolver-threw`: the resolver's message. */
|
|
282
|
+
reason?: string;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export type SidebarFailurePhase =
|
|
286
|
+
| "render"
|
|
287
|
+
| "cardGroups"
|
|
288
|
+
| "contribute"
|
|
289
|
+
| "handleMouse"
|
|
290
|
+
| "digest";
|
|
291
|
+
|
|
292
|
+
/** A delegated host call or a contributor call that threw; recorded, never swallowed silently. */
|
|
293
|
+
export interface SidebarFailure {
|
|
294
|
+
key: string;
|
|
295
|
+
phase: SidebarFailurePhase;
|
|
296
|
+
message: string;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export interface SidebarDiagnostics {
|
|
300
|
+
/** Host rail maps currently intercepted in this process. */
|
|
301
|
+
interceptedMaps: number;
|
|
302
|
+
/** Painted parts currently wrapped, across every intercepted map (four per complete map). */
|
|
303
|
+
wrappedRails: number;
|
|
304
|
+
/**
|
|
305
|
+
* Whether the part `"end"` draws in (the host's `footer`) is wrapped in the first intercepted map.
|
|
306
|
+
* Reported separately from `wrappedRails` because the four parts are wrapped independently: a rail
|
|
307
|
+
* whose sections all land before the footer must not look "wrapped" while the part they land in is
|
|
308
|
+
* missing.
|
|
309
|
+
*/
|
|
310
|
+
rail?: "wrapped" | "pending";
|
|
311
|
+
lastFailures: SidebarFailure[];
|
|
312
|
+
/** The most recent install attempt's outcome; absent until the backend is installed once. */
|
|
313
|
+
lastOutcome?: SidebarRecordedOutcome;
|
|
314
|
+
/** Only for `resolver-threw` and `no-widget-surface`: the reason, verbatim. */
|
|
315
|
+
lastReason?: string;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
type Rail = SidebarRailLike & Record<string, unknown>;
|
|
319
|
+
type RailMap = Map<string, RailMapValue>;
|
|
320
|
+
type MapSet = RailMap["set"];
|
|
321
|
+
type MapGet = RailMap["get"];
|
|
322
|
+
type MapDelete = RailMap["delete"];
|
|
323
|
+
type MapClear = RailMap["clear"];
|
|
324
|
+
|
|
325
|
+
/** One patched map, with everything a restore needs. */
|
|
326
|
+
interface Interception {
|
|
327
|
+
parts: RailMap;
|
|
328
|
+
/**
|
|
329
|
+
* The host's sidebar state object the map came from. Kept for one field only: `visibility.todo`,
|
|
330
|
+
* which the layout applies BEFORE asking a part to render, so a part the host hides must not be
|
|
331
|
+
* treated as a placement anchor. Reading it is optional-chained and a missing field means painted.
|
|
332
|
+
*/
|
|
333
|
+
hostState?: Record<string, unknown>;
|
|
334
|
+
/** The own descriptor the map had before us, if any, so a restore is exact. */
|
|
335
|
+
ownSet?: PropertyDescriptor;
|
|
336
|
+
ownGet?: PropertyDescriptor;
|
|
337
|
+
ownDelete?: PropertyDescriptor;
|
|
338
|
+
ownClear?: PropertyDescriptor;
|
|
339
|
+
/** The methods that were in force before us; a foreign patch stays reachable through them. */
|
|
340
|
+
originalSet: MapSet;
|
|
341
|
+
originalGet: MapGet;
|
|
342
|
+
originalDelete: MapDelete;
|
|
343
|
+
originalClear: MapClear;
|
|
344
|
+
patchedSet: MapSet;
|
|
345
|
+
patchedGet: MapGet;
|
|
346
|
+
patchedDelete: MapDelete;
|
|
347
|
+
patchedClear: MapClear;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** One wrapped part, per (map, key), so a disable can put the host's own rail back. */
|
|
351
|
+
interface WrappedRailRecord {
|
|
352
|
+
parts: RailMap;
|
|
353
|
+
key: string;
|
|
354
|
+
original: RailMapValue;
|
|
355
|
+
wrapper: RailMapValue;
|
|
356
|
+
/**
|
|
357
|
+
* How many rows the host's own `render` produced the last time it was asked — the render-side
|
|
358
|
+
* answer to "is this part painted?". Absent before the first frame: an unrendered part is assumed
|
|
359
|
+
* painted, which is what makes the very first frame resolve the way the rail looks.
|
|
360
|
+
*/
|
|
361
|
+
nativeRows?: number;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** One render's worth of state, so `cardGroups` need not re-run the host or the contributor. */
|
|
365
|
+
interface RailMemoEntry {
|
|
366
|
+
nativeLength: number;
|
|
367
|
+
contribution?: RailPartContribution;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
interface SidebarBackendState {
|
|
371
|
+
/** Bumped by a disable/reset; a wrapper from an older generation is a pass-through. */
|
|
372
|
+
generation: number;
|
|
373
|
+
intercepted: Interception[];
|
|
374
|
+
wrapped: WrappedRailRecord[];
|
|
375
|
+
contributor?: RailContributor;
|
|
376
|
+
lastFailures: SidebarFailure[];
|
|
377
|
+
/** Remembered so the diagnostics record can answer "was the rail ever reached?" (T6). */
|
|
378
|
+
lastOutcome?: SidebarRecordedOutcome;
|
|
379
|
+
lastReason?: string;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function backendHost(): Record<symbol, SidebarBackendState | undefined> {
|
|
383
|
+
// SAFETY: only `sidebarState()` writes this key, and it validates every field before trusting a
|
|
384
|
+
// value it finds, so a foreign or partial slot is replaced rather than used.
|
|
385
|
+
return globalThis as unknown as Record<symbol, SidebarBackendState | undefined>;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function sidebarState(): SidebarBackendState {
|
|
389
|
+
const host = backendHost();
|
|
390
|
+
const existing = host[BACKEND_STATE];
|
|
391
|
+
if (
|
|
392
|
+
existing !== undefined &&
|
|
393
|
+
typeof existing.generation === "number" &&
|
|
394
|
+
Array.isArray(existing.intercepted) &&
|
|
395
|
+
Array.isArray(existing.wrapped) &&
|
|
396
|
+
Array.isArray(existing.lastFailures)
|
|
397
|
+
) {
|
|
398
|
+
return existing;
|
|
399
|
+
}
|
|
400
|
+
const created: SidebarBackendState = {
|
|
401
|
+
generation: 0,
|
|
402
|
+
intercepted: [],
|
|
403
|
+
wrapped: [],
|
|
404
|
+
lastFailures: [],
|
|
405
|
+
};
|
|
406
|
+
host[BACKEND_STATE] = created;
|
|
407
|
+
return created;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** Diagnostics snapshot for `status` (T6) and for this backend's own tests. */
|
|
411
|
+
export function getSidebarDiagnostics(): SidebarDiagnostics {
|
|
412
|
+
const state = sidebarState();
|
|
413
|
+
const interception = state.intercepted[0];
|
|
414
|
+
return {
|
|
415
|
+
interceptedMaps: state.intercepted.length,
|
|
416
|
+
wrappedRails: state.wrapped.length,
|
|
417
|
+
...(interception === undefined ? {} : { rail: railStatus(interception.parts) }),
|
|
418
|
+
lastFailures: [...state.lastFailures],
|
|
419
|
+
...(state.lastOutcome === undefined ? {} : { lastOutcome: state.lastOutcome }),
|
|
420
|
+
...(state.lastReason === undefined ? {} : { lastReason: state.lastReason }),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Test-only seam: forget every record and failure, so one test cannot leak into another. */
|
|
425
|
+
export function resetSidebarStateForTests(): void {
|
|
426
|
+
const state = sidebarState();
|
|
427
|
+
state.intercepted.length = 0;
|
|
428
|
+
state.wrapped.length = 0;
|
|
429
|
+
state.contributor = undefined;
|
|
430
|
+
state.lastFailures.length = 0;
|
|
431
|
+
delete state.lastOutcome;
|
|
432
|
+
delete state.lastReason;
|
|
433
|
+
// Bump so a wrapper a previous test retained becomes a pass-through instead of contributing.
|
|
434
|
+
state.generation += 1;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** Install (or clear) the renderer that contributes rows and groups to the wrapped part. */
|
|
438
|
+
export function setRailContributor(contributor: RailContributor | undefined): void {
|
|
439
|
+
sidebarState().contributor = contributor;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Record that the wiring could not even attempt an install — Pi's UI has no `setWidget`, or the
|
|
444
|
+
* factory it accepted has not run yet. Reported by the diagnostics record as unarmed WITH the reason, so
|
|
445
|
+
* a missing section is explainable instead of silent. Never throws: reporting must not break a
|
|
446
|
+
* session start.
|
|
447
|
+
*/
|
|
448
|
+
export function noteSidebarUnavailable(reason: string): void {
|
|
449
|
+
const state = sidebarState();
|
|
450
|
+
state.lastOutcome = "no-widget-surface";
|
|
451
|
+
state.lastReason = reason;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Intercept the host's rail map so our contribution reaches the part the layout paints, whether the
|
|
456
|
+
* host registers that part before or after this call. Safe to call repeatedly: a second arm, a
|
|
457
|
+
* rebind or a second copy of the package reports `already-intercepted` and installs nothing new.
|
|
458
|
+
*/
|
|
459
|
+
export function installSidebarBackend(resolveTui: SidebarTuiResolver): InstallSidebarResult {
|
|
460
|
+
const result = attemptInstall(resolveTui);
|
|
461
|
+
// Remembered for the diagnostics record: "the rail was never reached" must be answerable (T6).
|
|
462
|
+
const state = sidebarState();
|
|
463
|
+
state.lastOutcome = result.outcome;
|
|
464
|
+
if (result.reason === undefined) delete state.lastReason;
|
|
465
|
+
else state.lastReason = result.reason;
|
|
466
|
+
return result;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function attemptInstall(resolveTui: SidebarTuiResolver): InstallSidebarResult {
|
|
470
|
+
let resolved: SidebarTuiLike | undefined;
|
|
471
|
+
try {
|
|
472
|
+
resolved = resolveTui();
|
|
473
|
+
} catch (error) {
|
|
474
|
+
// The wiring may not have a `tui` yet; that is a reported outcome, never a crash.
|
|
475
|
+
return {
|
|
476
|
+
installed: false,
|
|
477
|
+
outcome: "resolver-threw",
|
|
478
|
+
rail: "pending",
|
|
479
|
+
reason: errorMessage(error),
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const parts = resolveParts(resolved);
|
|
484
|
+
if (parts === undefined) return { installed: false, outcome: "no-sidebar-state", rail: "pending" };
|
|
485
|
+
// One of the objects the bridge is handed: if a host ever publishes its panel builders on the
|
|
486
|
+
// sidebar state, the section renderer uses them and the diagnostics record says so (N11, N15).
|
|
487
|
+
const hostState = hostSidebarStateOf(resolved);
|
|
488
|
+
notePanelCandidate("the terminal sidebar state", hostState);
|
|
489
|
+
return intercept(parts, hostState);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Read the host's rail map from a `tui`-like object, exactly as `sidebarState(tui)` does: the state
|
|
494
|
+
* hangs on `tui.terminal`, never on the `tui` itself. Absent state is never created here — inventing
|
|
495
|
+
* an empty map would fabricate host state and mask a wiring mistake.
|
|
496
|
+
*/
|
|
497
|
+
function resolveParts(tui: SidebarTuiLike | undefined): RailMap | undefined {
|
|
498
|
+
const state = hostSidebarStateOf(tui);
|
|
499
|
+
if (state === undefined) return undefined;
|
|
500
|
+
const parts = state.parts;
|
|
501
|
+
if (!(parts instanceof Map)) return undefined;
|
|
502
|
+
// SAFETY: the host declares `parts` as `Map<string, SidebarRail>`, and `SidebarRailLike` is the
|
|
503
|
+
// part of a rail this backend relies on; anything else a foreign writer puts there is handled
|
|
504
|
+
// defensively by `isRailLike` before it is wrapped.
|
|
505
|
+
return parts as RailMap;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* The host's sidebar state object, read from the `tui` exactly as `sidebarState(tui)` writes it
|
|
510
|
+
* (`lib/shell-sidebar.ts:78-81`): the state hangs on `tui.terminal`, never on the `tui` itself.
|
|
511
|
+
*
|
|
512
|
+
* Exported for the pointer claim (`./section-pointer.ts`), which performs the host's own
|
|
513
|
+
* `setSidebarHover` steps against the SAME published slots — `parts` here, `hover` there. One reader
|
|
514
|
+
* for one seam: a second copy of this lookup would be a second place to get the symbol wrong.
|
|
515
|
+
*/
|
|
516
|
+
export function hostSidebarStateOf(tui: unknown): Record<string, unknown> | undefined {
|
|
517
|
+
if (!isRecord(tui)) return undefined;
|
|
518
|
+
const terminal = tui.terminal;
|
|
519
|
+
if (!isRecord(terminal)) return undefined;
|
|
520
|
+
// SAFETY: the host stores its state under a symbol key; `isRecord` already proved the terminal is
|
|
521
|
+
// an object, and everything read below is re-validated before use.
|
|
522
|
+
const state = (terminal as Record<symbol, unknown>)[HOST_STATE];
|
|
523
|
+
return isRecord(state) ? state : undefined;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function intercept(parts: RailMap, hostState?: Record<string, unknown>): InstallSidebarResult {
|
|
527
|
+
const state = sidebarState();
|
|
528
|
+
const existing = state.intercepted.find((record) => record.parts === parts);
|
|
529
|
+
|
|
530
|
+
// The marker certifies the EXACT function we installed, read as an own property: a marker found
|
|
531
|
+
// through the prototype chain describes some other object, and a marker whose value is not the
|
|
532
|
+
// live `set` means foreign code replaced ours since (so we re-patch around it below).
|
|
533
|
+
if (existing !== undefined && parts.set === existing.patchedSet) {
|
|
534
|
+
// Already intercepted. Still make sure every painted part currently in the map is wrapped: a
|
|
535
|
+
// value that arrived through a path which skipped our `set` is wrapped by our `get`.
|
|
536
|
+
primePaintedParts(parts);
|
|
537
|
+
return { installed: true, outcome: "already-intercepted", rail: railStatus(parts) };
|
|
538
|
+
}
|
|
539
|
+
if (existing !== undefined) state.intercepted.splice(state.intercepted.indexOf(existing), 1);
|
|
540
|
+
|
|
541
|
+
const ownSet = Object.getOwnPropertyDescriptor(parts, "set");
|
|
542
|
+
const ownGet = Object.getOwnPropertyDescriptor(parts, "get");
|
|
543
|
+
const ownDelete = Object.getOwnPropertyDescriptor(parts, "delete");
|
|
544
|
+
const ownClear = Object.getOwnPropertyDescriptor(parts, "clear");
|
|
545
|
+
// Whatever is in force now stays reachable, so a foreign patch already on the map keeps running.
|
|
546
|
+
const originalSet = parts.set;
|
|
547
|
+
const originalGet = parts.get;
|
|
548
|
+
const originalDelete = parts.delete;
|
|
549
|
+
const originalClear = parts.clear;
|
|
550
|
+
|
|
551
|
+
const patchedSet = function (this: RailMap, key: string, value: RailMapValue) {
|
|
552
|
+
const stored = isPaintedRailKey(key) ? wrapRail(parts, key, value) : value;
|
|
553
|
+
return originalSet.call(this, key, stored);
|
|
554
|
+
} as MapSet;
|
|
555
|
+
|
|
556
|
+
const patchedGet = function (this: RailMap, key: string) {
|
|
557
|
+
const value = originalGet.call(this, key);
|
|
558
|
+
if (!isPaintedRailKey(key) || value === undefined) return value;
|
|
559
|
+
const wrapped = wrapRail(parts, key, value);
|
|
560
|
+
if (wrapped === value) return value;
|
|
561
|
+
// Store through the ORIGINAL set: re-entering our own patch would wrap twice for nothing.
|
|
562
|
+
originalSet.call(this, key, wrapped);
|
|
563
|
+
return wrapped;
|
|
564
|
+
} as MapGet;
|
|
565
|
+
|
|
566
|
+
// Deletion prunes the wrapped record, so diagnostics cannot claim a part is wrapped after the host
|
|
567
|
+
// dropped it.
|
|
568
|
+
const patchedDelete = function (this: RailMap, key: string): boolean {
|
|
569
|
+
const deleted = originalDelete.call(this, key);
|
|
570
|
+
if (deleted) forgetWrapped(parts, key);
|
|
571
|
+
return deleted;
|
|
572
|
+
} as MapDelete;
|
|
573
|
+
|
|
574
|
+
const patchedClear = function (this: RailMap): void {
|
|
575
|
+
originalClear.call(this);
|
|
576
|
+
forgetWrapped(parts);
|
|
577
|
+
} as MapClear;
|
|
578
|
+
|
|
579
|
+
defineHidden(parts, "set", patchedSet);
|
|
580
|
+
defineHidden(parts, "get", patchedGet);
|
|
581
|
+
defineHidden(parts, "delete", patchedDelete);
|
|
582
|
+
defineHidden(parts, "clear", patchedClear);
|
|
583
|
+
defineHidden(parts, PARTS_INTERCEPTED, patchedSet);
|
|
584
|
+
|
|
585
|
+
state.intercepted.push({
|
|
586
|
+
parts,
|
|
587
|
+
...(hostState === undefined ? {} : { hostState }),
|
|
588
|
+
ownSet,
|
|
589
|
+
ownGet,
|
|
590
|
+
ownDelete,
|
|
591
|
+
ownClear,
|
|
592
|
+
originalSet,
|
|
593
|
+
originalGet,
|
|
594
|
+
originalDelete,
|
|
595
|
+
originalClear,
|
|
596
|
+
patchedSet,
|
|
597
|
+
patchedGet,
|
|
598
|
+
patchedDelete,
|
|
599
|
+
patchedClear,
|
|
600
|
+
});
|
|
601
|
+
primePaintedParts(parts);
|
|
602
|
+
return { installed: true, outcome: "intercepted", rail: railStatus(parts) };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Read every painted key once, so a part registered before our arm arrives wrapped (see `patchedGet`). */
|
|
606
|
+
function primePaintedParts(parts: RailMap): void {
|
|
607
|
+
for (const key of RAIL_PARTS) void parts.get(key);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** Drop the wrapped-rail records for a part, so a deleted or cleared key is not reported as wrapped. */
|
|
611
|
+
function forgetWrapped(parts: RailMap, key?: string): void {
|
|
612
|
+
const state = sidebarState();
|
|
613
|
+
for (let index = state.wrapped.length - 1; index >= 0; index -= 1) {
|
|
614
|
+
const record = state.wrapped[index];
|
|
615
|
+
if (record.parts !== parts) continue;
|
|
616
|
+
if (key !== undefined && record.key !== key) continue;
|
|
617
|
+
state.wrapped.splice(index, 1);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function railStatus(parts: RailMap): "wrapped" | "pending" {
|
|
622
|
+
return parts.get(SIDEBAR_RAIL_KEY) === undefined ? "pending" : "wrapped";
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** Put the host's own rail back and unpatch every map we intercepted. Returns rails restored. */
|
|
626
|
+
export function disableSidebarBackend(): number {
|
|
627
|
+
const state = sidebarState();
|
|
628
|
+
const rails = state.wrapped.splice(0);
|
|
629
|
+
let restored = 0;
|
|
630
|
+
|
|
631
|
+
for (const interception of state.intercepted.splice(0)) {
|
|
632
|
+
for (const rail of rails) {
|
|
633
|
+
if (rail.parts !== interception.parts) continue;
|
|
634
|
+
// Write through the ORIGINAL methods: our own `set` would wrap the host's rail again.
|
|
635
|
+
const current = interception.originalGet.call(interception.parts, rail.key);
|
|
636
|
+
if (current !== rail.wrapper) continue; // foreign code replaced it since: leave its value
|
|
637
|
+
interception.originalSet.call(interception.parts, rail.key, rail.original);
|
|
638
|
+
restored += 1;
|
|
639
|
+
}
|
|
640
|
+
restoreOwnMethod(interception.parts, "set", interception.patchedSet, interception.ownSet);
|
|
641
|
+
restoreOwnMethod(interception.parts, "get", interception.patchedGet, interception.ownGet);
|
|
642
|
+
restoreOwnMethod(
|
|
643
|
+
interception.parts,
|
|
644
|
+
"delete",
|
|
645
|
+
interception.patchedDelete,
|
|
646
|
+
interception.ownDelete,
|
|
647
|
+
);
|
|
648
|
+
restoreOwnMethod(
|
|
649
|
+
interception.parts,
|
|
650
|
+
"clear",
|
|
651
|
+
interception.patchedClear,
|
|
652
|
+
interception.ownClear,
|
|
653
|
+
);
|
|
654
|
+
if (Object.hasOwn(interception.parts, PARTS_INTERCEPTED)) {
|
|
655
|
+
// SAFETY: the marker is the only symbol-keyed property this module writes on the map, and a
|
|
656
|
+
// `Map` is an object, so it is reachable through a widened index type; this only removes it.
|
|
657
|
+
delete (interception.parts as unknown as Record<PropertyKey, unknown>)[PARTS_INTERCEPTED];
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// A wrapper retained by foreign code becomes a pass-through through this bump. The remembered
|
|
662
|
+
// outcome stays: "installed once, now off" is what lets the diagnostics record answer "unarmed" rather
|
|
663
|
+
// than "never attempted".
|
|
664
|
+
state.generation += 1;
|
|
665
|
+
return restored;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function restoreOwnMethod(
|
|
669
|
+
parts: RailMap,
|
|
670
|
+
name: "set" | "get" | "delete" | "clear",
|
|
671
|
+
patched: unknown,
|
|
672
|
+
own: PropertyDescriptor | undefined,
|
|
673
|
+
): void {
|
|
674
|
+
// SAFETY: `parts` is a real `Map`, i.e. an object; `set`/`get` are ordinary string-keyed
|
|
675
|
+
// properties of it, so reading and deleting them through a widened index type is exact.
|
|
676
|
+
const target = parts as unknown as Record<string, unknown>;
|
|
677
|
+
if (target[name] !== patched) return; // not ours any more: never clobber a foreign patch
|
|
678
|
+
if (own === undefined) delete target[name];
|
|
679
|
+
else Object.defineProperty(parts, name, own);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Wrap one host rail, once. A value that is already our wrapper is returned unchanged, which is what
|
|
684
|
+
* keeps a second install, a rebind and a second package copy from stacking wrappers or doubling the
|
|
685
|
+
* contributed card groups.
|
|
686
|
+
*/
|
|
687
|
+
function wrapRail(parts: RailMap, key: string, rail: RailMapValue): RailMapValue {
|
|
688
|
+
if (!isRailLike(rail)) return rail;
|
|
689
|
+
if (isOurWrapper(rail)) return rail;
|
|
690
|
+
|
|
691
|
+
const state = sidebarState();
|
|
692
|
+
const original = rail;
|
|
693
|
+
const generation = state.generation;
|
|
694
|
+
const memo = new Map<number, RailMemoEntry>();
|
|
695
|
+
// Only the four painted keys are ever wrapped (`isPaintedRailKey`), so this cast names the part
|
|
696
|
+
// the contributor is told about, and nothing else is inferred from it.
|
|
697
|
+
const part = key as RailPart;
|
|
698
|
+
|
|
699
|
+
const wrapper: Rail = {
|
|
700
|
+
...original,
|
|
701
|
+
render: (width: number) => renderThrough(parts, part, original, generation, memo, width),
|
|
702
|
+
cardGroups: (width: number) => groupsThrough(parts, part, original, generation, memo, width),
|
|
703
|
+
handleMouse: (event: unknown) => mouseThrough(part, original, generation, memo, event),
|
|
704
|
+
};
|
|
705
|
+
|
|
706
|
+
// The host's panel builders are looked for on the part itself (N11, N15): if a host ever publishes
|
|
707
|
+
// `renderPanelHeader`/`renderPanelRow` here, the section renderer uses them instead of its local
|
|
708
|
+
// port, and the diagnostics record says so. Looking on every object the bridge is handed is what makes
|
|
709
|
+
// "the host's builders are unreachable" a measurement rather than a claim.
|
|
710
|
+
notePanelCandidate("the rail part", original);
|
|
711
|
+
|
|
712
|
+
// The digest is the one field that is not a pass-through (N14): the layout memo re-renders a part
|
|
713
|
+
// only when its digest VALUE moves, so a live row field (`meta`) would otherwise never repaint.
|
|
714
|
+
// The field therefore stays a function whose value is the host's own digest, unchanged, whenever
|
|
715
|
+
// nothing contributes one — the passthrough tests pin the VALUE, because the memo compares values
|
|
716
|
+
// and never function identity. When a contributor has a signature, it is appended, so a change in
|
|
717
|
+
// the built rows reaches the memo as a new key.
|
|
718
|
+
wrapper.digest = () => digestValue(part, original, generation, paintedParts(parts));
|
|
719
|
+
|
|
720
|
+
Object.defineProperty(wrapper, RAIL_WRAPPED, {
|
|
721
|
+
value: original,
|
|
722
|
+
enumerable: false,
|
|
723
|
+
configurable: true,
|
|
724
|
+
});
|
|
725
|
+
Object.defineProperty(wrapper, "railMemo", {
|
|
726
|
+
value: memo,
|
|
727
|
+
enumerable: false,
|
|
728
|
+
configurable: true,
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
const record: WrappedRailRecord = { parts, key, original, wrapper };
|
|
732
|
+
const index = state.wrapped.findIndex((entry) => entry.parts === parts && entry.key === key);
|
|
733
|
+
if (index === -1) state.wrapped.push(record);
|
|
734
|
+
else state.wrapped[index] = record;
|
|
735
|
+
return wrapper;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function isRailLike(value: RailMapValue): value is Rail {
|
|
739
|
+
return isRecord(value) && typeof value.render === "function";
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The parts the host actually paints, in paint order — what a placement resolves against (S5).
|
|
744
|
+
*
|
|
745
|
+
* Three facts make a part painted: it is in the map (the host registered it and did not dispose it),
|
|
746
|
+
* the host's own visibility switch does not hide it (`state.visibility.todo === false`, the filter
|
|
747
|
+
* the layout applies at `lib/shell-sidebar-layout.ts:344`), and its own `render` did not return an
|
|
748
|
+
* empty frame the last time it ran (a part in compact-work or status-hidden mode paints nothing).
|
|
749
|
+
* A part that has not rendered yet counts as painted: the first frame must resolve the way the rail
|
|
750
|
+
* looks, not the way an empty observation would.
|
|
751
|
+
*/
|
|
752
|
+
function paintedParts(parts: RailMap): RailPart[] {
|
|
753
|
+
const state = sidebarState();
|
|
754
|
+
const visibility = hostVisibility(parts);
|
|
755
|
+
return RAIL_PARTS.filter((key) => {
|
|
756
|
+
if (key === "todo" && visibility?.todo === false) return false;
|
|
757
|
+
const record = state.wrapped.find((entry) => entry.parts === parts && entry.key === key);
|
|
758
|
+
return record !== undefined && record.nativeRows !== 0;
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** The host's own `state.visibility`, when the object the map came from carries one. */
|
|
763
|
+
function hostVisibility(parts: RailMap): { todo?: boolean } | undefined {
|
|
764
|
+
const hostState = sidebarState().intercepted.find((entry) => entry.parts === parts)?.hostState;
|
|
765
|
+
const visibility = hostState?.visibility;
|
|
766
|
+
return isRecord(visibility) ? (visibility as { todo?: boolean }) : undefined;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** Remember how many rows one part's own render produced, so "painted" is a measurement. */
|
|
770
|
+
function recordNativeRows(parts: RailMap, key: RailPart, rows: number): void {
|
|
771
|
+
const record = sidebarState().wrapped.find(
|
|
772
|
+
(entry) => entry.parts === parts && entry.key === key,
|
|
773
|
+
);
|
|
774
|
+
if (record !== undefined) record.nativeRows = rows;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* The parts the host paints right now, for a caller that has no frame of its own (the diagnostics record).
|
|
779
|
+
* Before the first interception — or before any frame — this is every painted key, which is the
|
|
780
|
+
* rail's normal shape: `todo`, `jobs`, `agents`, `footer`.
|
|
781
|
+
*/
|
|
782
|
+
export function observedPaintedParts(): RailPart[] {
|
|
783
|
+
const interception = sidebarState().intercepted[0];
|
|
784
|
+
return interception === undefined ? [...RAIL_PARTS] : paintedParts(interception.parts);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/** Own-property identity check: the marker's value is the exact rail this object wraps. */
|
|
788
|
+
function isOurWrapper(value: RailMapValue): boolean {
|
|
789
|
+
if (!isRecord(value) || !Object.hasOwn(value, RAIL_WRAPPED)) return false;
|
|
790
|
+
// SAFETY: the only writer of this symbol key is `wrapRail` above, whose value is a rail object.
|
|
791
|
+
return isRecord((value as Record<symbol, unknown>)[RAIL_WRAPPED]);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/** Delegate to the host's `render`, then wrap our own rows around it — if the host painted anything. */
|
|
795
|
+
function renderThrough(
|
|
796
|
+
parts: RailMap,
|
|
797
|
+
key: RailPart,
|
|
798
|
+
original: Rail,
|
|
799
|
+
generation: number,
|
|
800
|
+
memo: Map<number, RailMemoEntry>,
|
|
801
|
+
width: number,
|
|
802
|
+
): string[] {
|
|
803
|
+
if (sidebarState().generation !== generation) return [...(original.render(width) ?? [])];
|
|
804
|
+
|
|
805
|
+
let native: string[];
|
|
806
|
+
try {
|
|
807
|
+
native = [...(original.render(width) ?? [])];
|
|
808
|
+
} catch (error) {
|
|
809
|
+
recordFailure(key, "render", error);
|
|
810
|
+
// Never swallowed: the host's own failure path decides what a throwing part means.
|
|
811
|
+
throw error;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// What the host's own frame is, remembered BEFORE the contribution resolves: whether this part is
|
|
815
|
+
// painted is part of the answer to "where does a block anchored on it go" (S5).
|
|
816
|
+
recordNativeRows(parts, key, native.length);
|
|
817
|
+
|
|
818
|
+
if (native.length === 0) {
|
|
819
|
+
// The host painted no frame (rail inactive, compact work row, another surface owns it). A frame
|
|
820
|
+
// of ours here would appear exactly where the host suppressed its own.
|
|
821
|
+
remember(memo, width, { nativeLength: 0 });
|
|
822
|
+
return native;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
const contribution = contributionFor(key, width, paintedParts(parts));
|
|
826
|
+
remember(memo, width, { nativeLength: native.length, contribution });
|
|
827
|
+
const before = contribution?.before?.lines ?? [];
|
|
828
|
+
const after = contribution?.after?.lines ?? [];
|
|
829
|
+
if (before.length === 0 && after.length === 0) return native;
|
|
830
|
+
return [...before, ...native, ...after];
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/** Delegate to the host's `cardGroups`, then wrap our groups around the host's, offset by ours. */
|
|
834
|
+
function groupsThrough(
|
|
835
|
+
parts: RailMap,
|
|
836
|
+
key: RailPart,
|
|
837
|
+
original: Rail,
|
|
838
|
+
generation: number,
|
|
839
|
+
memo: Map<number, RailMemoEntry>,
|
|
840
|
+
width: number,
|
|
841
|
+
): RailGroupLayout | undefined {
|
|
842
|
+
if (sidebarState().generation !== generation) return original.cardGroups?.(width);
|
|
843
|
+
|
|
844
|
+
const remembered = memo.get(width);
|
|
845
|
+
let nativeLength = remembered?.nativeLength;
|
|
846
|
+
if (nativeLength === undefined) {
|
|
847
|
+
// `cardGroups` without a preceding `render`. The host always renders first (it needs the rows
|
|
848
|
+
// before it asks for their ranges), so this is a fallback: measure instead of dropping our
|
|
849
|
+
// groups silently, and let a throw surface exactly like it does through `render`.
|
|
850
|
+
try {
|
|
851
|
+
nativeLength = [...(original.render(width) ?? [])].length;
|
|
852
|
+
} catch (error) {
|
|
853
|
+
recordFailure(key, "render", error);
|
|
854
|
+
throw error;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
const contribution =
|
|
858
|
+
remembered !== undefined
|
|
859
|
+
? remembered.contribution
|
|
860
|
+
: contributionFor(key, width, paintedParts(parts));
|
|
861
|
+
if (remembered === undefined) remember(memo, width, { nativeLength, contribution });
|
|
862
|
+
|
|
863
|
+
let base: RailGroupLayout | undefined;
|
|
864
|
+
try {
|
|
865
|
+
base = original.cardGroups?.(width);
|
|
866
|
+
} catch (error) {
|
|
867
|
+
recordFailure(key, "cardGroups", error);
|
|
868
|
+
throw error;
|
|
869
|
+
}
|
|
870
|
+
// Same default the layout applies: without `cardGroups` a part is one section headed by its first
|
|
871
|
+
// row, and that one section must cover the rows we wrap around it too.
|
|
872
|
+
const hostLayout: RailGroupLayout = base ?? { groups: [{ start: 0, end: nativeLength }] };
|
|
873
|
+
const beforeLength = contribution?.before?.lines.length ?? 0;
|
|
874
|
+
const afterLength = contribution?.after?.lines.length ?? 0;
|
|
875
|
+
|
|
876
|
+
if (nativeLength === 0 || (beforeLength === 0 && afterLength === 0)) {
|
|
877
|
+
// Nothing of ours: the host's own layout object, untouched.
|
|
878
|
+
return hostLayout;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
const before = contribution?.before;
|
|
882
|
+
const after = contribution?.after;
|
|
883
|
+
return {
|
|
884
|
+
groups: [
|
|
885
|
+
...(before?.groups ?? []),
|
|
886
|
+
...hostLayout.groups.map((group) => ({
|
|
887
|
+
start: group.start + beforeLength,
|
|
888
|
+
end: group.end + beforeLength,
|
|
889
|
+
})),
|
|
890
|
+
...(after?.groups ?? []).map((group) => ({
|
|
891
|
+
start: group.start + beforeLength + nativeLength,
|
|
892
|
+
end: group.end + beforeLength + nativeLength,
|
|
893
|
+
})),
|
|
894
|
+
],
|
|
895
|
+
// The host's loose rows (key hints) are indices into the HOST's frame, so they move with it.
|
|
896
|
+
...(hostLayout.loose === undefined
|
|
897
|
+
? {}
|
|
898
|
+
: { loose: hostLayout.loose.map((row) => row + beforeLength) }),
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* Delegate mouse dispatch to the host's own handler, and answer the rows we drew.
|
|
904
|
+
*
|
|
905
|
+
* A MOVE over our own rows goes to the contributor FIRST. The host's part handler is reachable
|
|
906
|
+
* proof of why: the real `footer` part is a `NativePointerRegion`
|
|
907
|
+
* (`extensions/gentle-shell.ts:2766`) whose `onHover`
|
|
908
|
+
* (`extensions/gentle-shell.ts:2732-2741`, reached from `lib/native-pointer-region.ts:80-88`) claims
|
|
909
|
+
* or clears the shared pointer and always returns `{ handled: true }` — the layout hands the part
|
|
910
|
+
* every inflated row of its frame (`lib/shell-sidebar-layout.ts:245-259`) and the part answers all of
|
|
911
|
+
* them. So "consult the host first and only then our targets" meant our appended rows never saw a
|
|
912
|
+
* move at all. Our rows are exactly `[startRow, endRow)`, which the host's own targets never cover
|
|
913
|
+
* (they live inside the host's own lines), so asking the contributor first for those rows cannot
|
|
914
|
+
* take the pointer from a host card.
|
|
915
|
+
*
|
|
916
|
+
* Every other event keeps the host first: its targets win wherever it has one, and it DECLINES a
|
|
917
|
+
* click or a press on a row it does not own (`extensions/gentle-shell.ts:2747-2757`), which is how a
|
|
918
|
+
* contributed row's click reaches us today. When the contributor declines a move over our rows the
|
|
919
|
+
* host still runs, so its own hover is cleared exactly as it would be for any row it does not own.
|
|
920
|
+
*
|
|
921
|
+
* The event's `y` counts the PART's rows. Since a part can now carry our rows ABOVE the host's as
|
|
922
|
+
* well as below, the event is TRANSLATED for the host: `y` minus our before-block, so a host row
|
|
923
|
+
* still reaches the host's handler with the host's own coordinate (S7). An event over one of our own
|
|
924
|
+
* rows is offered to the host at `nativeLength` — one past its last row — which is exactly where an
|
|
925
|
+
* appended row already sat before this feature, so the host still never claims one of our rows.
|
|
926
|
+
*/
|
|
927
|
+
function mouseThrough(
|
|
928
|
+
key: RailPart,
|
|
929
|
+
original: Rail,
|
|
930
|
+
generation: number,
|
|
931
|
+
memo: Map<number, RailMemoEntry>,
|
|
932
|
+
event: unknown,
|
|
933
|
+
): RailMouseResult | undefined {
|
|
934
|
+
const hostHandler = original.handleMouse;
|
|
935
|
+
if (sidebarState().generation !== generation) {
|
|
936
|
+
return typeof hostHandler === "function" ? hostHandler.call(original, event) : undefined;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
const position = contributionPosition(key, memo, event);
|
|
940
|
+
const region = regionOf(event, position);
|
|
941
|
+
let consulted = false;
|
|
942
|
+
if (region === "before" || region === "after") {
|
|
943
|
+
// Our own rows: the contributor goes first, over the host's region, which would answer a move
|
|
944
|
+
// here with "not mine" and a click with nothing.
|
|
945
|
+
consulted = true;
|
|
946
|
+
const own = contributorMouse(key, event, position);
|
|
947
|
+
if (own !== undefined) return own;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
if (typeof hostHandler === "function") {
|
|
951
|
+
let result: RailMouseResult | undefined;
|
|
952
|
+
const hostY = hostYFor(event, position, region);
|
|
953
|
+
const hostEvent =
|
|
954
|
+
hostY === undefined || !isRecord(event) ? event : { ...event, y: hostY };
|
|
955
|
+
try {
|
|
956
|
+
result = hostHandler.call(original, hostEvent);
|
|
957
|
+
} catch (error) {
|
|
958
|
+
recordFailure(key, "handleMouse", error);
|
|
959
|
+
throw error;
|
|
960
|
+
}
|
|
961
|
+
// The host's targets win whenever it handled the event: we add targets, never override.
|
|
962
|
+
if (result !== undefined) return result;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// Offered once: a second call would run a plugin's handler twice for one event.
|
|
966
|
+
return consulted ? undefined : contributorMouse(key, event, position);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/** Which rows of the part frame the event landed on. */
|
|
970
|
+
type PointerRegion = "before" | "native" | "after" | undefined;
|
|
971
|
+
|
|
972
|
+
function within(y: number, range: RailRowRange | undefined): boolean {
|
|
973
|
+
return range !== undefined && y >= range.start && y < range.end;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
function regionOf(event: unknown, position: RailContributionPosition | undefined): PointerRegion {
|
|
977
|
+
if (position === undefined || !isRecord(event)) return undefined;
|
|
978
|
+
const y = event.y;
|
|
979
|
+
if (typeof y !== "number" || !Number.isFinite(y)) return undefined;
|
|
980
|
+
if (within(y, position.before)) return "before";
|
|
981
|
+
if (within(y, position.after)) return "after";
|
|
982
|
+
if (within(y, position.native)) return "native";
|
|
983
|
+
return undefined;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
* The same pointer event's `y` in the HOST's own coordinates: its frame starts after our before-block,
|
|
988
|
+
* and a row it did not draw is reported one past its last row (where an appended row already sat
|
|
989
|
+
* before a before-block existed). `undefined` when there is nothing to translate.
|
|
990
|
+
*/
|
|
991
|
+
function hostYFor(
|
|
992
|
+
event: unknown,
|
|
993
|
+
position: RailContributionPosition | undefined,
|
|
994
|
+
region: PointerRegion,
|
|
995
|
+
): number | undefined {
|
|
996
|
+
if (position === undefined || !isRecord(event)) return undefined;
|
|
997
|
+
const y = event.y;
|
|
998
|
+
if (typeof y !== "number" || !Number.isFinite(y)) return undefined;
|
|
999
|
+
const beforeLength = position.before?.end ?? 0;
|
|
1000
|
+
const nativeLength = position.native === undefined ? 0 : position.native.end - position.native.start;
|
|
1001
|
+
return region === "before" ? nativeLength : y - beforeLength;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/** Ask the contributor, recording a throw instead of letting it reach the host's dispatch. */
|
|
1005
|
+
function contributorMouse(
|
|
1006
|
+
key: string,
|
|
1007
|
+
event: unknown,
|
|
1008
|
+
position: RailContributionPosition | undefined,
|
|
1009
|
+
): RailMouseResult | undefined {
|
|
1010
|
+
const contributor = sidebarState().contributor;
|
|
1011
|
+
if (contributor?.handleMouse === undefined) return undefined;
|
|
1012
|
+
try {
|
|
1013
|
+
return contributor.handleMouse(event, position);
|
|
1014
|
+
} catch (error) {
|
|
1015
|
+
recordFailure(key, "handleMouse", error);
|
|
1016
|
+
return undefined; // a broken target must not break the host's dispatch
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* Where the contributor's rows sit in the frame this event was mapped into.
|
|
1022
|
+
*
|
|
1023
|
+
* The host maps a hit to the part's own coordinates before calling its `handleMouse`
|
|
1024
|
+
* (`lib/shell-sidebar-layout.ts:245-259`), so `y` counts the part's rows, our before-block included;
|
|
1025
|
+
* the render that preceded the event recorded how many rows each block has at that width. Without
|
|
1026
|
+
* such a frame there is nothing to map, and the contributor declines instead of guessing an offset.
|
|
1027
|
+
* The width comes along, because the contributor's own ranges belong to the width they were drawn at.
|
|
1028
|
+
*/
|
|
1029
|
+
function contributionPosition(
|
|
1030
|
+
key: RailPart,
|
|
1031
|
+
memo: Map<number, RailMemoEntry>,
|
|
1032
|
+
event: unknown,
|
|
1033
|
+
): RailContributionPosition | undefined {
|
|
1034
|
+
if (!isRecord(event)) return undefined;
|
|
1035
|
+
const width = event.width;
|
|
1036
|
+
if (typeof width !== "number") return undefined;
|
|
1037
|
+
const entry = memo.get(width);
|
|
1038
|
+
if (entry === undefined) return undefined;
|
|
1039
|
+
const before = entry.contribution?.before?.lines.length ?? 0;
|
|
1040
|
+
const after = entry.contribution?.after?.lines.length ?? 0;
|
|
1041
|
+
return {
|
|
1042
|
+
part: key,
|
|
1043
|
+
width,
|
|
1044
|
+
native: { start: before, end: before + entry.nativeLength },
|
|
1045
|
+
...(before === 0 ? {} : { before: { start: 0, end: before } }),
|
|
1046
|
+
...(after === 0
|
|
1047
|
+
? {}
|
|
1048
|
+
: { after: { start: before + entry.nativeLength, end: before + entry.nativeLength + after } }),
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/**
|
|
1053
|
+
* The part's digest: the host's own value when nothing contributes one, otherwise the host's value
|
|
1054
|
+
* with the contributor's signature appended (N14). The signature receives the same context the
|
|
1055
|
+
* render did, so a placement change moves it and the host re-frames the part.
|
|
1056
|
+
*/
|
|
1057
|
+
function digestValue(
|
|
1058
|
+
key: RailPart,
|
|
1059
|
+
original: Rail,
|
|
1060
|
+
generation: number,
|
|
1061
|
+
painted: readonly RailPart[],
|
|
1062
|
+
): string | undefined {
|
|
1063
|
+
const hostDigest = original.digest;
|
|
1064
|
+
const raw = typeof hostDigest === "function" ? hostDigest.call(original) : undefined;
|
|
1065
|
+
// A foreign rail can return anything; only a string is a digest the layout can compare.
|
|
1066
|
+
const hostValue = typeof raw === "string" ? raw : undefined;
|
|
1067
|
+
if (sidebarState().generation !== generation) return hostValue;
|
|
1068
|
+
const contributor = sidebarState().contributor;
|
|
1069
|
+
if (contributor?.digest === undefined) return hostValue;
|
|
1070
|
+
let contribution: string;
|
|
1071
|
+
try {
|
|
1072
|
+
contribution = contributor.digest({ part: key, painted });
|
|
1073
|
+
} catch (error) {
|
|
1074
|
+
// A broken signature must not take the host's memo down with it: record it and stay on the
|
|
1075
|
+
// host's own digest, which degrades to invalidation-only, exactly as the layout does.
|
|
1076
|
+
recordFailure(key, "digest", error);
|
|
1077
|
+
return hostValue;
|
|
1078
|
+
}
|
|
1079
|
+
return `${hostValue ?? ""}|${contribution}`;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
function contributionFor(
|
|
1083
|
+
key: RailPart,
|
|
1084
|
+
width: number,
|
|
1085
|
+
painted: readonly RailPart[],
|
|
1086
|
+
): RailPartContribution | undefined {
|
|
1087
|
+
const contributor = sidebarState().contributor;
|
|
1088
|
+
if (contributor === undefined) return undefined;
|
|
1089
|
+
let contribution: RailPartContribution | undefined;
|
|
1090
|
+
try {
|
|
1091
|
+
contribution = contributor.render(width, { part: key, painted });
|
|
1092
|
+
} catch (error) {
|
|
1093
|
+
// A broken renderer loses its own rows and nothing else (S8): record it, draw the host's rows.
|
|
1094
|
+
recordFailure(key, "contribute", error);
|
|
1095
|
+
return undefined;
|
|
1096
|
+
}
|
|
1097
|
+
if (contribution === undefined) return undefined;
|
|
1098
|
+
const before = contribution.before?.lines.length ?? 0;
|
|
1099
|
+
const after = contribution.after?.lines.length ?? 0;
|
|
1100
|
+
if (before === 0 && after === 0) return undefined;
|
|
1101
|
+
return contribution;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
function remember(memo: Map<number, RailMemoEntry>, width: number, entry: RailMemoEntry): void {
|
|
1105
|
+
if (memo.size > 32) memo.clear();
|
|
1106
|
+
memo.set(width, entry);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
function recordFailure(key: string, phase: SidebarFailurePhase, error: unknown): void {
|
|
1110
|
+
const state = sidebarState();
|
|
1111
|
+
state.lastFailures.push({ key, phase, message: errorMessage(error) });
|
|
1112
|
+
if (state.lastFailures.length > 32) state.lastFailures.splice(0, state.lastFailures.length - 32);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
function defineHidden<T extends object>(target: T, key: PropertyKey, value: unknown): void {
|
|
1116
|
+
Object.defineProperty(target, key, {
|
|
1117
|
+
value,
|
|
1118
|
+
enumerable: false,
|
|
1119
|
+
configurable: true,
|
|
1120
|
+
writable: true,
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
function errorMessage(error: unknown): string {
|
|
1125
|
+
return error instanceof Error ? error.message : String(error);
|
|
1126
|
+
}
|