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,1411 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The DESIGN bus (S1–S9): designs per tool, joined into LINES, one process-global slot.
|
|
3
|
+
*
|
|
4
|
+
* ## Why the bus, and not each plugin
|
|
5
|
+
*
|
|
6
|
+
* Pi has exactly one sanctioned way to choose how a tool's card is drawn:
|
|
7
|
+
* `pi.registerToolRenderer(resolver)` (`docs/extensions.md:2123`;
|
|
8
|
+
* `dist/core/extensions/types.d.ts:546`). Resolvers COMPOSE — Pi chains every extension's resolver
|
|
9
|
+
* (`dist/core/extensions/runner.js:550-553`) — so two plugins that each called it would each wrap the
|
|
10
|
+
* other's component, and neither could see the other's choice.
|
|
11
|
+
*
|
|
12
|
+
* So the BRIDGE registers the resolver (`./tool-design-wiring.ts`), once, and fans it out to the
|
|
13
|
+
* designs plugins contributed here. Two plugins contributing a design for the same line do not
|
|
14
|
+
* collide: they are two VALUES of that line, and they only conflict when they claim the same identity
|
|
15
|
+
* (`line` + `id`) or the same visible label — which is reported, never silently replaced.
|
|
16
|
+
*
|
|
17
|
+
* ## Two registration shapes, one line each
|
|
18
|
+
*
|
|
19
|
+
* - `{ tool, id, label, apply }` is UNGROUPED. It gets an implicit line keyed by the tool name
|
|
20
|
+
* (`Card bash`), exactly as it did before lines existed (S4).
|
|
21
|
+
* - `{ line, tools, id, label, apply }` is GROUPED. It joins the line named `line`; several
|
|
22
|
+
* registrations naming the same line produce ONE customize line (`Card tools`) and that line's
|
|
23
|
+
* tool set is the union of their `tools` (S2, S3).
|
|
24
|
+
*
|
|
25
|
+
* A tool belongs to at most ONE line (S5/D1) — unless a line narrows its claim on that tool with a
|
|
26
|
+
* SCOPE (S7–S10, S19). A claim is a (line, tool) pair: a registration WITHOUT a scope for a tool
|
|
27
|
+
* claims every call of it, so a second UNSCOPED claim of that tool — or a legacy `Card <tool>`
|
|
28
|
+
* registration on a tool that already has one — is refused whole and recorded. A SCOPED claim owns
|
|
29
|
+
* only the calls its scope's `matches` answers true for: any number of scoped claims may name one
|
|
30
|
+
* tool beside the one unscoped claim, and the resolver decides per call which of them paints it. A
|
|
31
|
+
* scope goes on the GROUPED shape only: an ungrouped registration IS the tool's own `Card <tool>`
|
|
32
|
+
* line, which means every call of that tool, so `{ tool, scope, … }` is refused whole (L6/S7).
|
|
33
|
+
*
|
|
34
|
+
* The scope belongs to the CLAIM, not to the line (S19): one line may own some of its tools
|
|
35
|
+
* unconditionally and narrow others (`Card tools`: bash/grep/find/ls/read for every call, write/edit
|
|
36
|
+
* only under `skills/` or `odd/`), while another line carries the generic claim on the same tools.
|
|
37
|
+
* `scopes` maps a tool to its scope, and `scope` is the shorthand for the same object on every tool.
|
|
38
|
+
* Nothing here is bash-specific: a design is declared for a tool NAME, and the registry is keyed by
|
|
39
|
+
* that name.
|
|
40
|
+
*
|
|
41
|
+
* ## The selection
|
|
42
|
+
*
|
|
43
|
+
* One persisted value per line in the bridge's own store file (`bridge-tool-designs`, never the
|
|
44
|
+
* host's config): grouped lines live under `lines` (`{ "tools": "<id>" }`), ungrouped ones keep the
|
|
45
|
+
* historical per-tool `selections` map (`{ "bash": "<id>" }`), untouched. The stored value is a design
|
|
46
|
+
* ID — identity, so renaming a label never loses a user's choice — and `native` means "the host's own
|
|
47
|
+
* card", the documented degrade when a selected design is disposed (`missing`) or does not cover a
|
|
48
|
+
* tool of the line (`not-covered`, S6/D4).
|
|
49
|
+
*
|
|
50
|
+
* Migration (D3) happens ONCE, at REGISTRATION time, never in the render path: a tool joining a line
|
|
51
|
+
* that has no stored value lends that line its legacy per-tool value when that value names a design of
|
|
52
|
+
* the line, and the line value is written then.
|
|
53
|
+
*
|
|
54
|
+
* This module is internal: `src/index.ts` re-exports `registerToolDesign`,
|
|
55
|
+
* `resolveToolDesignSelection` and the public types those signatures carry, never this file's
|
|
56
|
+
* diagnostics (`toolDesignClaims`, the index) or its `reset…ForTests` seam.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
import { assertRegistrationId } from "./bus.ts";
|
|
60
|
+
import { attributeRegistration } from "./plugin-lines.ts";
|
|
61
|
+
import { isRecord } from "./rows.ts";
|
|
62
|
+
import { createStore } from "./store.ts";
|
|
63
|
+
import type { ToolDesignContext, ToolDesignRegistration, ToolDesignScope } from "./types.ts";
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The STORED value meaning "no design: the host draws the card", shared by every line. It is
|
|
67
|
+
* identity, so it never changes: a user's saved choice keeps working whatever the line shows.
|
|
68
|
+
*/
|
|
69
|
+
export const TOOL_DESIGN_NATIVE = "native";
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* What a line SHOWS for the host's own card. Display only: the stored value stays
|
|
73
|
+
* `TOOL_DESIGN_NATIVE`. A design may not take this word (or `native`) as its label.
|
|
74
|
+
*/
|
|
75
|
+
export const TOOL_DESIGN_DEFAULT_LABEL = "default";
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The bridge's own plugin id for the selection file. One namespaced file holds every line's value,
|
|
79
|
+
* so a selection never lands in the host's `visual-customization.json`.
|
|
80
|
+
*/
|
|
81
|
+
export const TOOL_DESIGN_STORE_ID = "bridge-tool-designs";
|
|
82
|
+
|
|
83
|
+
/** The historical per-tool key: `{ "bash": "loon.bash-heading" }`. Untouched by lines (D3). */
|
|
84
|
+
export const TOOL_DESIGN_STORE_KEY = "selections";
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The reserved key INSIDE the `selections` value that holds the per-line map, so one tool's selection
|
|
88
|
+
* and one line's selection are the same document and one `get` serves both (S9/D6: one store read per
|
|
89
|
+
* card per frame). A tool name can never be `$lines`, so it cannot shadow a line.
|
|
90
|
+
*/
|
|
91
|
+
export const TOOL_DESIGN_LINE_STORE_KEY = "$lines";
|
|
92
|
+
|
|
93
|
+
/** Why a registration did not become the live one: it replaced one, or it was refused. */
|
|
94
|
+
export type ToolDesignConflictKind = "replaced" | "label" | "line" | "scope";
|
|
95
|
+
|
|
96
|
+
/** One registration that replaced another, or was refused, kept for the diagnostics record. */
|
|
97
|
+
export interface ToolDesignConflict {
|
|
98
|
+
/** The tool of an ungrouped design, or the line a grouped one belongs to. */
|
|
99
|
+
tool: string;
|
|
100
|
+
id: string;
|
|
101
|
+
kind: ToolDesignConflictKind;
|
|
102
|
+
/** What happened, as a person reads it. */
|
|
103
|
+
message: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** One design that failed while the host's card was drawn; deduplicated by (tool, id, message). */
|
|
107
|
+
export interface ToolDesignFailure {
|
|
108
|
+
tool: string;
|
|
109
|
+
designId: string;
|
|
110
|
+
message: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** How many failures are kept for the diagnostics record; a design that throws every frame is one fact. */
|
|
114
|
+
const MAX_FAILURES = 8;
|
|
115
|
+
|
|
116
|
+
/** How far `drainThenable` follows the values `then` returns before it stops (S18). */
|
|
117
|
+
const THENABLE_DRAIN_DEPTH = 4;
|
|
118
|
+
|
|
119
|
+
/** One live registration, normalized: its line, the tools it paints, and the plugin's design. */
|
|
120
|
+
interface DesignEntry {
|
|
121
|
+
/** `tool` for an ungrouped implicit line, `line` for a grouped one. */
|
|
122
|
+
scope: "tool" | "line";
|
|
123
|
+
/** The selection key within its scope: the tool name or the registered line id. */
|
|
124
|
+
line: string;
|
|
125
|
+
/** The tools THIS design paints, in registration order, deduplicated. */
|
|
126
|
+
tools: string[];
|
|
127
|
+
design: ToolDesignRegistration;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One customize line, as the resolver, the rows and the diagnostics record read it. */
|
|
131
|
+
export interface ToolDesignLine {
|
|
132
|
+
scope: "tool" | "line";
|
|
133
|
+
/** The store key within its scope: the tool name or the registered line id. */
|
|
134
|
+
line: string;
|
|
135
|
+
/** Every tool this line paints, in first-registration order. */
|
|
136
|
+
tools: string[];
|
|
137
|
+
/** Every design of the line, in registration order. */
|
|
138
|
+
designs: ToolDesignRegistration[];
|
|
139
|
+
/**
|
|
140
|
+
* The scope of each TOOL this line claims that has one (S19), one entry per DISTINCT scope object
|
|
141
|
+
* in tool order: the menu shows it inside the same preview row and the diagnostics record reads it per
|
|
142
|
+
* claim. Empty when every claim of the line is unscoped, which is the historical meaning.
|
|
143
|
+
*/
|
|
144
|
+
scopedClaims: Array<{ tools: string[]; scope: ToolDesignScope }>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface IndexedLine {
|
|
148
|
+
scope: "tool" | "line";
|
|
149
|
+
line: string;
|
|
150
|
+
tools: string[];
|
|
151
|
+
entries: DesignEntry[];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* One (line, tool) CLAIM: the line that makes it, the scope of THAT tool (S19) and the live entry
|
|
156
|
+
* that declares the scope, so a throwing or non-boolean answer can be reported against its design.
|
|
157
|
+
* The unit of `claimsByTool`, precedence and resolution — never a whole line.
|
|
158
|
+
*/
|
|
159
|
+
interface IndexedClaim {
|
|
160
|
+
line: IndexedLine;
|
|
161
|
+
/** `design.scopes[tool]`, else the `scope` shorthand, else `undefined` (EVERY call of the tool). */
|
|
162
|
+
scope?: ToolDesignScope;
|
|
163
|
+
/** The live entry that declares the scope, for the failure record (a scope belongs to a design). */
|
|
164
|
+
entry: DesignEntry;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
interface ToolDesignIndex {
|
|
168
|
+
/**
|
|
169
|
+
* The SHAPE this index was built with (`TOOL_DESIGN_INDEX_SHAPE`). Two copies of the bridge can
|
|
170
|
+
* share this process-global slot — a plugin pinned to an older release and the host on this one —
|
|
171
|
+
* and the older copy leaves an index built from fields this one does not read. The stamp is what
|
|
172
|
+
* lets a cached index be DROPPED instead of trusted (S18).
|
|
173
|
+
*/
|
|
174
|
+
shape: number;
|
|
175
|
+
revision: number;
|
|
176
|
+
/** tool → the one line that owns it (S5/D1), by the first entry that names the tool. */
|
|
177
|
+
owner: Map<string, { scope: "tool" | "line"; line: string }>;
|
|
178
|
+
/**
|
|
179
|
+
* Lines in DISPLAY order: the first LIVE entry of a line fixes its place, exactly as the Cards rows
|
|
180
|
+
* and the diagnostics record block ordered themselves before claim precedence existed (S2/S13/S14).
|
|
181
|
+
* PRECEDENCE is a different order and lives only in `claimsByTool` (S10).
|
|
182
|
+
*/
|
|
183
|
+
lines: IndexedLine[];
|
|
184
|
+
byKey: Map<string, IndexedLine>;
|
|
185
|
+
/** tool → its designs, in registration order: the ONE lookup every render path uses (T1). */
|
|
186
|
+
designsByTool: Map<string, ToolDesignRegistration[]>;
|
|
187
|
+
/**
|
|
188
|
+
* tool → every (line, tool) CLAIM on it, in PRECEDENCE order (`ToolDesignState.lineOrder`, the line
|
|
189
|
+
* creation order that survives the disposal of one of its values — S10): the scoped claims and the
|
|
190
|
+
* ONE unscoped claim (S9/S19), each with the scope of THAT tool. The resolver walks this list per
|
|
191
|
+
* call; the diagnostics record lists it for a tool with scoped claims. Never a display order: see
|
|
192
|
+
* `lines`.
|
|
193
|
+
*/
|
|
194
|
+
claimsByTool: Map<string, IndexedClaim[]>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
interface ToolDesignState {
|
|
198
|
+
/** Every live registration, in registration order; one entry per (`scope`, `line`, `id`). */
|
|
199
|
+
entries: DesignEntry[];
|
|
200
|
+
/**
|
|
201
|
+
* The creation order of every live line, keyed by `lineKey`: the order a line keeps for as long as
|
|
202
|
+
* any value of it is live (S10). Disposing ONE value of a line must not move the line, because a
|
|
203
|
+
* line never stopped claiming its tools; precedence between two claims therefore never depends on
|
|
204
|
+
* which registrations happen to survive. It lives in the STATE and not in the derived index, so it
|
|
205
|
+
* is stable across index rebuilds and identical for every copy sharing this slot.
|
|
206
|
+
*/
|
|
207
|
+
lineOrder: Map<string, number>;
|
|
208
|
+
/** The order the NEXT line to appear receives. Monotonic: an order is never reused. */
|
|
209
|
+
lineOrderSeq: number;
|
|
210
|
+
conflicts: ToolDesignConflict[];
|
|
211
|
+
failures: ToolDesignFailure[];
|
|
212
|
+
/**
|
|
213
|
+
* How many times the persisted selections were READ since the last reset. One read is exactly one
|
|
214
|
+
* logical read of the bridge's store, so this counts the file work a render pass performs.
|
|
215
|
+
*/
|
|
216
|
+
selectionReads: number;
|
|
217
|
+
/** Bumped by every registry mutation; the in-memory index is rebuilt only when it changes (D6). */
|
|
218
|
+
revision: number;
|
|
219
|
+
/** The derived maps, valid only for `revision`. Never persisted. */
|
|
220
|
+
index?: ToolDesignIndex;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Stable process-global slot, like the bus and the store: two copies must agree. */
|
|
224
|
+
const DESIGNS_KEY = Symbol.for("gentle-shell-bridge.tool-designs.v1");
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The shape of the derived index: bumped whenever `ToolDesignIndex` gains, loses or renames a field
|
|
228
|
+
* OR the meaning of one of them changes. The index is DERIVED and never persisted, so an index of
|
|
229
|
+
* another shape is always safe to drop and rebuild — which is exactly what a slot warmed by another
|
|
230
|
+
* copy of the bridge requires (S18). Shape 4 split the two orders shape 3 had collapsed: `lines` is
|
|
231
|
+
* DISPLAY order (the first live entry, S2) and only `claimsByTool` is claim PRECEDENCE (`lineOrder`,
|
|
232
|
+
* S10). An index of shape 3 ordered `lines` by precedence too, so it must be dropped, not read.
|
|
233
|
+
*
|
|
234
|
+
* Shape 5 is the per-(line, tool) claim (S19): `claimsByTool` maps a tool to CLAIMS carrying that
|
|
235
|
+
* tool's own scope, where shape 4 mapped it to LINES carrying one scope for the whole line. An index
|
|
236
|
+
* of shape 4 would make a mixed line (some tools scoped, others not) answer with the wrong scope for
|
|
237
|
+
* some of its tools, so it is dropped and rebuilt — which keeps a slot warmed by the previous copy
|
|
238
|
+
* readable (S18), including the state that copy left behind.
|
|
239
|
+
*/
|
|
240
|
+
const TOOL_DESIGN_INDEX_SHAPE = 5;
|
|
241
|
+
|
|
242
|
+
/** Is this a cached index THIS copy can read? A slot written by an older copy answers no (S18). */
|
|
243
|
+
function isCurrentIndexShape(index: unknown): index is ToolDesignIndex {
|
|
244
|
+
return isRecord(index) && index.shape === TOOL_DESIGN_INDEX_SHAPE;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function designsHost(): Record<symbol, ToolDesignState | undefined> {
|
|
248
|
+
// SAFETY: only `designState()` writes this key, and it re-checks every list before trusting a
|
|
249
|
+
// value left behind by another copy or an older load.
|
|
250
|
+
return globalThis as unknown as Record<symbol, ToolDesignState | undefined>;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function designState(): ToolDesignState {
|
|
254
|
+
const host = designsHost();
|
|
255
|
+
const existing = host[DESIGNS_KEY];
|
|
256
|
+
if (existing !== undefined && Array.isArray(existing.entries)) {
|
|
257
|
+
// A slot created by a copy that predates the diagnostics keeps its live registrations and gains
|
|
258
|
+
// the lists in place, so two copies always see one registry.
|
|
259
|
+
if (!Array.isArray(existing.conflicts)) existing.conflicts = [];
|
|
260
|
+
if (!Array.isArray(existing.failures)) existing.failures = [];
|
|
261
|
+
if (typeof existing.selectionReads !== "number") existing.selectionReads = 0;
|
|
262
|
+
if (typeof existing.revision !== "number") {
|
|
263
|
+
existing.revision = 0;
|
|
264
|
+
delete existing.index;
|
|
265
|
+
}
|
|
266
|
+
// The derived maps are valid only for THIS copy's shape and revision. An index left by a copy
|
|
267
|
+
// that predates the claim map (S18) is dropped here, so no read site below can reach a field
|
|
268
|
+
// that copy never wrote; the next read rebuilds it from the entries that survived.
|
|
269
|
+
if (!isCurrentIndexShape(existing.index)) delete existing.index;
|
|
270
|
+
// A slot created by a copy that predates the line order gains it here, exactly like the lists
|
|
271
|
+
// above: the entries it already holds are the only evidence of which line appeared first, so the
|
|
272
|
+
// next rebuild assigns each of them an order in that order.
|
|
273
|
+
if (!(existing.lineOrder instanceof Map)) existing.lineOrder = new Map<string, number>();
|
|
274
|
+
if (typeof existing.lineOrderSeq !== "number") existing.lineOrderSeq = existing.lineOrder.size;
|
|
275
|
+
// A slot created before grouped lines has `{ tool, design }` entries: normalize them in place.
|
|
276
|
+
for (const raw of existing.entries as unknown[]) {
|
|
277
|
+
if (!isRecord(raw) || !isRecord(raw.design)) continue;
|
|
278
|
+
if (typeof raw.scope === "string" && typeof raw.line === "string" && Array.isArray(raw.tools)) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
const tool = typeof raw.tool === "string" ? raw.tool : raw.design.tool;
|
|
282
|
+
if (typeof tool !== "string") continue;
|
|
283
|
+
raw.scope = "tool";
|
|
284
|
+
raw.line = tool;
|
|
285
|
+
raw.tools = [tool];
|
|
286
|
+
}
|
|
287
|
+
return existing;
|
|
288
|
+
}
|
|
289
|
+
const created: ToolDesignState = {
|
|
290
|
+
entries: [],
|
|
291
|
+
lineOrder: new Map<string, number>(),
|
|
292
|
+
lineOrderSeq: 0,
|
|
293
|
+
conflicts: [],
|
|
294
|
+
failures: [],
|
|
295
|
+
selectionReads: 0,
|
|
296
|
+
revision: 0,
|
|
297
|
+
};
|
|
298
|
+
host[DESIGNS_KEY] = created;
|
|
299
|
+
return created;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** What the diagnostics record says about a thrown value that cannot be read as text at all (S18). */
|
|
303
|
+
const UNPRINTABLE_THROWN_VALUE = "a value that cannot be read as text";
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Text for a thrown value, whatever a plugin threw at us. It NEVER throws (S18): a paint is not a
|
|
307
|
+
* place to fail, and the failure record is built from this string, so `String(...)` on a value whose
|
|
308
|
+
* prototype is null (or whose `toString` explodes) would escape as `Cannot convert object to
|
|
309
|
+
* primitive value` out of the published resolver. Every reading is isolated, and a fixed text stands
|
|
310
|
+
* in when even the object tag cannot be read. Shared with `applyToolDesign`, so a design's throw takes
|
|
311
|
+
* the same path.
|
|
312
|
+
*/
|
|
313
|
+
function errorMessage(error: unknown): string {
|
|
314
|
+
if (typeof error === "string") return error;
|
|
315
|
+
try {
|
|
316
|
+
if (error instanceof Error && typeof error.message === "string") return error.message;
|
|
317
|
+
} catch {
|
|
318
|
+
// A hostile prototype chain (`instanceof` walks it) or a throwing `message` getter: try the
|
|
319
|
+
// generic readings below instead of giving up on the report.
|
|
320
|
+
}
|
|
321
|
+
try {
|
|
322
|
+
return String(error);
|
|
323
|
+
} catch {
|
|
324
|
+
// `String` needs a primitive out of the value: a null prototype, a throwing `toString`/`valueOf`
|
|
325
|
+
// or an object-returning pair all end here.
|
|
326
|
+
}
|
|
327
|
+
try {
|
|
328
|
+
return Object.prototype.toString.call(error);
|
|
329
|
+
} catch {
|
|
330
|
+
// Even the tag is unreadable (a Proxy whose trap throws). The caller still gets text.
|
|
331
|
+
}
|
|
332
|
+
return UNPRINTABLE_THROWN_VALUE;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function lineKey(scope: "tool" | "line", line: string): string {
|
|
336
|
+
return `${scope}\u0000${line}`;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Every registry mutation bumps this and drops the derived maps; the next read rebuilds once. */
|
|
340
|
+
function bumpRevision(state: ToolDesignState): void {
|
|
341
|
+
state.revision += 1;
|
|
342
|
+
delete state.index;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function toolDesignIndex(state: ToolDesignState): ToolDesignIndex {
|
|
346
|
+
const cached = state.index;
|
|
347
|
+
if (cached !== undefined) {
|
|
348
|
+
// A cached index is trusted only when it was built by THIS copy, for THIS revision. Another copy
|
|
349
|
+
// of the bridge can write the same slot between two calls, so the shape is checked HERE and not
|
|
350
|
+
// only in the repair: dropping a derived value is always safe (S18).
|
|
351
|
+
if (isCurrentIndexShape(cached) && cached.revision === state.revision) return cached;
|
|
352
|
+
delete state.index;
|
|
353
|
+
}
|
|
354
|
+
const byKey = new Map<string, IndexedLine>();
|
|
355
|
+
/**
|
|
356
|
+
* DISPLAY order, built by walking the ENTRIES: the first LIVE entry of a line fixes its place
|
|
357
|
+
* (S2/S13/S14). Disposing one value of a line, while another survives, therefore leaves the Cards
|
|
358
|
+
* rows and the diagnostics record block in the order the previous release drew them.
|
|
359
|
+
*/
|
|
360
|
+
const lines: IndexedLine[] = [];
|
|
361
|
+
const designsByTool = new Map<string, ToolDesignRegistration[]>();
|
|
362
|
+
const owner = new Map<string, { scope: "tool" | "line"; line: string }>();
|
|
363
|
+
for (const entry of state.entries) {
|
|
364
|
+
const key = lineKey(entry.scope, entry.line);
|
|
365
|
+
let line = byKey.get(key);
|
|
366
|
+
if (line === undefined) {
|
|
367
|
+
line = { scope: entry.scope, line: entry.line, tools: [], entries: [] };
|
|
368
|
+
byKey.set(key, line);
|
|
369
|
+
lines.push(line);
|
|
370
|
+
// The order a line receives when it FIRST appears, kept until its last value is gone (S10).
|
|
371
|
+
if (!state.lineOrder.has(key)) {
|
|
372
|
+
state.lineOrder.set(key, state.lineOrderSeq);
|
|
373
|
+
state.lineOrderSeq += 1;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
line.entries.push(entry);
|
|
377
|
+
for (const tool of entry.tools) {
|
|
378
|
+
if (!line.tools.includes(tool)) line.tools.push(tool);
|
|
379
|
+
if (!owner.has(tool)) owner.set(tool, { scope: entry.scope, line: entry.line });
|
|
380
|
+
const designs = designsByTool.get(tool);
|
|
381
|
+
if (designs === undefined) designsByTool.set(tool, [entry.design]);
|
|
382
|
+
else designs.push(entry.design);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// A line with NO live entry left is gone: its order is dropped, so a line that comes back is a new
|
|
386
|
+
// line that takes its place at the end instead of inheriting a claim it stopped making.
|
|
387
|
+
for (const key of [...state.lineOrder.keys()]) if (!byKey.has(key)) state.lineOrder.delete(key);
|
|
388
|
+
const orderOf = (line: IndexedLine): number =>
|
|
389
|
+
state.lineOrder.get(lineKey(line.scope, line.line)) ?? Number.MAX_SAFE_INTEGER;
|
|
390
|
+
// CLAIM PRECEDENCE, a DIFFERENT order from the display one above (S10): line creation order, which
|
|
391
|
+
// survives the disposal of a value the line owns. `claimsByTool` is the only list built from it, so
|
|
392
|
+
// collapsing the two orders — which is what the previous fix did — cannot move a row or a status
|
|
393
|
+
// entry again.
|
|
394
|
+
const claimsByTool = new Map<string, IndexedClaim[]>();
|
|
395
|
+
const byPrecedence = [...lines].sort((left, right) => orderOf(left) - orderOf(right));
|
|
396
|
+
for (const line of byPrecedence) {
|
|
397
|
+
for (const tool of line.tools) {
|
|
398
|
+
// One claim per (line, tool): the scope of THAT tool (S19), never the line's first scope.
|
|
399
|
+
const claim: IndexedClaim = {
|
|
400
|
+
line,
|
|
401
|
+
...claimOfLine(line, tool),
|
|
402
|
+
};
|
|
403
|
+
const claims = claimsByTool.get(tool);
|
|
404
|
+
if (claims === undefined) claimsByTool.set(tool, [claim]);
|
|
405
|
+
else claims.push(claim);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const built: ToolDesignIndex = {
|
|
409
|
+
shape: TOOL_DESIGN_INDEX_SHAPE,
|
|
410
|
+
revision: state.revision,
|
|
411
|
+
owner,
|
|
412
|
+
lines,
|
|
413
|
+
byKey,
|
|
414
|
+
designsByTool,
|
|
415
|
+
claimsByTool,
|
|
416
|
+
};
|
|
417
|
+
state.index = built;
|
|
418
|
+
return built;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Every design of one tool, or every design when `tool` is omitted, in registration order. */
|
|
422
|
+
export function designsOfTool(tool: string): ToolDesignRegistration[] {
|
|
423
|
+
return [...(toolDesignIndex(designState()).designsByTool.get(tool) ?? [])];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Every design of one tool, or of every tool when `tool` is omitted, in registration order. The array
|
|
428
|
+
* is a fresh copy, so a fan-out that iterates it sees a stable list even if a design registers or
|
|
429
|
+
* disposes mid-iteration. It is the ONE lookup the resolver, adoption, the rows and status share (T1).
|
|
430
|
+
*/
|
|
431
|
+
export function snapshotToolDesigns(tool?: string): ToolDesignRegistration[] {
|
|
432
|
+
if (tool !== undefined) return designsOfTool(tool);
|
|
433
|
+
return designState().entries.map((entry) => entry.design);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* The scope of ONE tool's claim on one line (S19): `scopes[tool]`, else the `scope` shorthand, else
|
|
438
|
+
* `undefined` — the claim every call of that tool, which is the historical meaning.
|
|
439
|
+
*/
|
|
440
|
+
function scopeOfClaim(design: ToolDesignRegistration, tool: string): ToolDesignScope | undefined {
|
|
441
|
+
// SAFETY: the member is typed, but a plugin is plain JavaScript and another copy of the bridge may
|
|
442
|
+
// share this slot, so every reading is re-checked instead of trusted.
|
|
443
|
+
//
|
|
444
|
+
// This is the ONLY read of the claim, and it sees exactly the own keys `scopesProblem` validates
|
|
445
|
+
// (`Reflect.ownKeys`, S19/L14): a key no validation saw can never be read, so a key that is not
|
|
446
|
+
// enumerable is validated before it can reach the registry.
|
|
447
|
+
const scopes: unknown = design.scopes;
|
|
448
|
+
if (isRecord(scopes) && Object.hasOwn(scopes, tool)) {
|
|
449
|
+
const scope = scopes[tool];
|
|
450
|
+
// SAFETY: `isRecord` proved a non-null, non-array object; the members themselves were validated
|
|
451
|
+
// when the registration entered THIS copy, and a value a foreign copy left is re-checked here by
|
|
452
|
+
// the consumers (`scopeProblem` on registration, the isolated `matches` call on resolution).
|
|
453
|
+
return isRecord(scope) ? (scope as unknown as ToolDesignScope) : undefined;
|
|
454
|
+
}
|
|
455
|
+
return design.scope;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** The live entry of one line that claims one tool, and the scope of ITS claim on it (S19/S8). */
|
|
459
|
+
function claimOfLine(line: IndexedLine, tool: string): { scope?: ToolDesignScope; entry: DesignEntry } {
|
|
460
|
+
const entry = line.entries.find((candidate) => candidate.tools.includes(tool));
|
|
461
|
+
if (entry === undefined) {
|
|
462
|
+
// SAFETY: every tool of `line.tools` came from an entry that names it, so the search above always
|
|
463
|
+
// finds one; this branch only keeps the compiler honest about the empty case.
|
|
464
|
+
return { entry: line.entries[0] as DesignEntry };
|
|
465
|
+
}
|
|
466
|
+
return { scope: scopeOfClaim(entry.design, tool), entry };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** The scope of one LINE's claim on one tool (S19), or `undefined` when that claim is unscoped. */
|
|
470
|
+
function scopeOf(line: IndexedLine, tool: string): ToolDesignScope | undefined {
|
|
471
|
+
return claimOfLine(line, tool).scope;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* One line's SCOPED claims, grouped by scope OBJECT in tool order (S19): the menu prints one
|
|
476
|
+
* ` — scope: <tool[, tool]>: <description>` per group, and a single group prints today's text.
|
|
477
|
+
*/
|
|
478
|
+
function scopedClaimsOf(line: IndexedLine): Array<{ tools: string[]; scope: ToolDesignScope }> {
|
|
479
|
+
const groups: Array<{ tools: string[]; scope: ToolDesignScope }> = [];
|
|
480
|
+
for (const tool of line.tools) {
|
|
481
|
+
const scope = scopeOf(line, tool);
|
|
482
|
+
if (scope === undefined) continue;
|
|
483
|
+
const group = groups.find((candidate) => candidate.scope === scope);
|
|
484
|
+
if (group === undefined) groups.push({ tools: [tool], scope });
|
|
485
|
+
else group.tools.push(tool);
|
|
486
|
+
}
|
|
487
|
+
return groups;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* The live lines in DISPLAY order — the first LIVE entry of a line orders it, as the Cards rows and
|
|
492
|
+
* the diagnostics record always ordered themselves (S2/S13) — each with its tools and designs in
|
|
493
|
+
* registration order. Claim precedence is a separate order and is read through `toolDesignClaims`.
|
|
494
|
+
*/
|
|
495
|
+
export function toolDesignLines(): ToolDesignLine[] {
|
|
496
|
+
return toolDesignIndex(designState()).lines.map((line) => ({
|
|
497
|
+
scope: line.scope,
|
|
498
|
+
line: line.line,
|
|
499
|
+
tools: [...line.tools],
|
|
500
|
+
designs: line.entries.map((entry) => entry.design),
|
|
501
|
+
scopedClaims: scopedClaimsOf(line),
|
|
502
|
+
}));
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** The line that owns one tool, or `undefined` when no design covers it. */
|
|
506
|
+
export function lineOfTool(tool: string): { scope: "tool" | "line"; line: string } | undefined {
|
|
507
|
+
const owner = toolDesignIndex(designState()).owner.get(tool);
|
|
508
|
+
return owner === undefined ? undefined : { ...owner };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** The tool names that have at least one design, in line DISPLAY order and tool-registration order. */
|
|
512
|
+
export function toolNamesWithDesigns(): string[] {
|
|
513
|
+
const names: string[] = [];
|
|
514
|
+
for (const line of toolDesignIndex(designState()).lines) {
|
|
515
|
+
for (const tool of line.tools) if (!names.includes(tool)) names.push(tool);
|
|
516
|
+
}
|
|
517
|
+
return names;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function assertLabel(subject: string, id: string, label: unknown): void {
|
|
521
|
+
if (typeof label !== "string" || label.trim() === "") {
|
|
522
|
+
throw new Error(
|
|
523
|
+
`gentle-shell-bridge: invalid tool design ${JSON.stringify(id)} for ${subject}: label must be ` +
|
|
524
|
+
"a non-empty string. It is the value the user reads on the customize line, so keep it short.",
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
if (label === TOOL_DESIGN_NATIVE || label === TOOL_DESIGN_DEFAULT_LABEL) {
|
|
528
|
+
throw new Error(
|
|
529
|
+
`gentle-shell-bridge: invalid tool design label ${JSON.stringify(label)} for ` +
|
|
530
|
+
`${JSON.stringify(id)}: it is the reserved value meaning "the host's own card", so the line ` +
|
|
531
|
+
`would show ${JSON.stringify(label)} twice. Pick a distinct label.`,
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function assertApplyFunction(subject: string, id: string, apply: unknown): void {
|
|
537
|
+
if (typeof apply !== "function") {
|
|
538
|
+
throw new Error(
|
|
539
|
+
`gentle-shell-bridge: invalid tool design ${JSON.stringify(id)} for ${subject}: apply must be ` +
|
|
540
|
+
"a function of (lines, ctx) that returns the lines to draw. A design rewrites the rows the " +
|
|
541
|
+
"host's card produced; the shell API hands a copy, isolates its throw and records it as a " +
|
|
542
|
+
"failure (S5).",
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** One registration's normalized line, tools and identity. */
|
|
548
|
+
interface NormalizedRegistration {
|
|
549
|
+
scope: "tool" | "line";
|
|
550
|
+
line: string;
|
|
551
|
+
tools: string[];
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Exactly one of `tool` / (`line` + `tools`) is required (S2). Mixing them, an empty tool set, a
|
|
556
|
+
* duplicate tool or an invalid name is refused before anything is stored.
|
|
557
|
+
*/
|
|
558
|
+
function normalizeRegistration(design: ToolDesignRegistration): NormalizedRegistration {
|
|
559
|
+
if (!isRecord(design)) {
|
|
560
|
+
throw new Error(
|
|
561
|
+
"gentle-shell-bridge: invalid tool design: a registration must be an object with exactly one " +
|
|
562
|
+
"of `tool` / (`line` + `tools`), plus `id`, `label` and `apply`.",
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
const hasTool = design.tool !== undefined;
|
|
566
|
+
const hasLine = design.line !== undefined;
|
|
567
|
+
const hasTools = design.tools !== undefined;
|
|
568
|
+
const tool = design.tool;
|
|
569
|
+
if (hasTool && (hasLine || hasTools)) {
|
|
570
|
+
throw new Error(
|
|
571
|
+
"gentle-shell-bridge: invalid tool design: `tool` cannot be combined with `line` or `tools`. " +
|
|
572
|
+
"Use `{ tool, id, label, apply }` for one tool, or `{ line, tools, id, label, apply }` for " +
|
|
573
|
+
"one line over several tools.",
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
if (!hasTool && !(hasLine && hasTools)) {
|
|
577
|
+
throw new Error(
|
|
578
|
+
"gentle-shell-bridge: invalid tool design: exactly one of `tool` / (`line` + `tools`) is " +
|
|
579
|
+
"required. `{ tool, … }` gives the tool its own line; `{ line, tools, … }` paints several " +
|
|
580
|
+
"tools from one line.",
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
if (hasTool) {
|
|
584
|
+
assertRegistrationId("tool name", tool);
|
|
585
|
+
if (typeof tool !== "string") {
|
|
586
|
+
// `assertRegistrationId` narrows by throwing; this keeps the compiler honest.
|
|
587
|
+
throw new Error("gentle-shell-bridge: invalid tool name");
|
|
588
|
+
}
|
|
589
|
+
return { scope: "tool", line: tool, tools: [tool] };
|
|
590
|
+
}
|
|
591
|
+
const line = design.line;
|
|
592
|
+
assertRegistrationId("tool design line", line);
|
|
593
|
+
if (typeof line !== "string") {
|
|
594
|
+
throw new Error("gentle-shell-bridge: invalid tool design line");
|
|
595
|
+
}
|
|
596
|
+
if (!Array.isArray(design.tools) || design.tools.length === 0) {
|
|
597
|
+
throw new Error(
|
|
598
|
+
`gentle-shell-bridge: invalid tool design for line ${JSON.stringify(line)}: tools must be a ` +
|
|
599
|
+
"non-empty array of tool names.",
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
const tools: string[] = [];
|
|
603
|
+
for (const value of design.tools) {
|
|
604
|
+
assertRegistrationId("tool name", value);
|
|
605
|
+
if (typeof value === "string") tools.push(value);
|
|
606
|
+
}
|
|
607
|
+
if (new Set(tools).size !== tools.length) {
|
|
608
|
+
throw new Error(
|
|
609
|
+
`gentle-shell-bridge: invalid tool design for line ${JSON.stringify(line)}: tools must be ` +
|
|
610
|
+
"deduplicated. List each tool once; the line's tool set is the union of its designs.",
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
return { scope: "line", line, tools };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function subjectOf(registration: NormalizedRegistration): string {
|
|
617
|
+
return registration.scope === "tool"
|
|
618
|
+
? `tool ${JSON.stringify(registration.line)}`
|
|
619
|
+
: `line ${JSON.stringify(registration.line)}`;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* The label is what the user sees, so two designs of one line showing the same text cannot be told
|
|
624
|
+
* apart (the same rule the card-style line applies). Rejected BEFORE anything is stored, with the
|
|
625
|
+
* clash recorded so the diagnostics record explains why the second design is not in the menu.
|
|
626
|
+
*/
|
|
627
|
+
function assertLabelIsDistinct(registration: NormalizedRegistration, design: ToolDesignRegistration): void {
|
|
628
|
+
const line = toolDesignIndex(designState()).byKey.get(lineKey(registration.scope, registration.line));
|
|
629
|
+
const clash = line?.entries.find(
|
|
630
|
+
(candidate) => candidate.design.id !== design.id && candidate.design.label === design.label,
|
|
631
|
+
);
|
|
632
|
+
if (clash === undefined) return;
|
|
633
|
+
const subject = subjectOf(registration);
|
|
634
|
+
const message =
|
|
635
|
+
`gentle-shell-bridge: invalid tool design label ${JSON.stringify(design.label)} for ` +
|
|
636
|
+
`${JSON.stringify(design.id)}: it is already used by ${JSON.stringify(clash.design.id)} on ` +
|
|
637
|
+
`${subject}. Two values reading the same cannot be told apart on the line. Pick a distinct label.`;
|
|
638
|
+
recordConflict({
|
|
639
|
+
tool: registration.line,
|
|
640
|
+
id: design.id,
|
|
641
|
+
kind: "label",
|
|
642
|
+
message,
|
|
643
|
+
});
|
|
644
|
+
throw new Error(message);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* A tool has at most ONE unscoped claim (S9/D1), and the claim table applies PER TOOL of the
|
|
649
|
+
* registration (S19). Joining the same line again is fine (S3: union); a second UNSCOPED claim of a
|
|
650
|
+
* tool — another grouped line, or the tool's own legacy `Card <tool>` line — refuses the WHOLE
|
|
651
|
+
* registration and is recorded under the `line` kind, with today's message. A SCOPED claim narrows
|
|
652
|
+
* the calls it owns, so it never conflicts: any number of them live beside the one unscoped claim,
|
|
653
|
+
* and the resolver decides per call which of them paints it.
|
|
654
|
+
*
|
|
655
|
+
* The unscoped check is INDEPENDENT of "this registration joins the line that already claims the
|
|
656
|
+
* tool" (L6): re-registering its own (line, id) is exempt from ITS OWN claim, never from the claim
|
|
657
|
+
* of another line. Without that independence a scoped line could re-register itself unscoped over a
|
|
658
|
+
* second unscoped line and leave two unscoped claims of one tool alive.
|
|
659
|
+
*/
|
|
660
|
+
function assertToolsAreFree(registration: NormalizedRegistration, design: ToolDesignRegistration): void {
|
|
661
|
+
const index = toolDesignIndex(designState());
|
|
662
|
+
const self = (line: IndexedLine): boolean =>
|
|
663
|
+
line.scope === registration.scope && line.line === registration.line;
|
|
664
|
+
for (const tool of registration.tools) {
|
|
665
|
+
// S9 per claim: a SCOPED claim of THIS tool narrows the calls it owns, so it never conflicts —
|
|
666
|
+
// not with the unscoped claim of that tool, not with another scoped one.
|
|
667
|
+
if (scopeOfClaim(design, tool) !== undefined) continue;
|
|
668
|
+
const claims = index.claimsByTool.get(tool) ?? [];
|
|
669
|
+
// The one UNSCOPED claim of this tool that is not this registration's own line.
|
|
670
|
+
const live = claims.find((claim) => claim.scope === undefined && !self(claim.line));
|
|
671
|
+
if (live === undefined) continue;
|
|
672
|
+
const current =
|
|
673
|
+
live.line.scope === "tool"
|
|
674
|
+
? `its own Card ${JSON.stringify(tool)} line`
|
|
675
|
+
: `the line ${JSON.stringify(live.line.line)}`;
|
|
676
|
+
const message =
|
|
677
|
+
`gentle-shell-bridge: tool ${JSON.stringify(tool)} already belongs to ${current}, so design ` +
|
|
678
|
+
`${JSON.stringify(design.id)} for ${subjectOf(registration)} was refused whole (S5/D1). A tool ` +
|
|
679
|
+
"belongs to at most one line; register it on that line or remove the other registration.";
|
|
680
|
+
recordConflict({ tool, id: design.id, kind: "line", message });
|
|
681
|
+
throw new Error(message);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* S7/S19: a scope is a predicate plus the one line that explains it. An absent scope is never an
|
|
687
|
+
* error; a present one that is not an object, whose `description` is not a non-empty string after
|
|
688
|
+
* trim, or whose `matches` is not a function refuses the WHOLE registration before anything is
|
|
689
|
+
* stored, and the refusal is recorded under the `scope` kind.
|
|
690
|
+
*
|
|
691
|
+
* The scope is declared PER (line, tool) claim: `scopes` maps each of this registration's tools to
|
|
692
|
+
* its scope, and `scope` is the shorthand for the SAME object on every tool (S19). Refused whole:
|
|
693
|
+
* both members together, and a `scopes` map that is not an object, is empty, names a tool this
|
|
694
|
+
* registration does not paint, or carries a value that is not a scope.
|
|
695
|
+
*
|
|
696
|
+
* A scope is declared on a GROUPED registration ONLY (L6): `{ tool, scope, … }` and `{ tool, scopes,
|
|
697
|
+
* … }` are refused whole. An ungrouped registration IS the tool's own `Card <tool>` line, and that
|
|
698
|
+
* line means every call of that tool by definition; "only some calls" is a line that shares the tool
|
|
699
|
+
* with another claim, which is the grouped shape. Keeping it apart also keeps the two key spaces
|
|
700
|
+
* apart: an implicit line is keyed by the tool name, and so is the historical per-tool selection.
|
|
701
|
+
*/
|
|
702
|
+
function assertScope(registration: NormalizedRegistration, design: ToolDesignRegistration): void {
|
|
703
|
+
// SAFETY: the members are typed, but a plugin is plain JavaScript: every member is re-checked here.
|
|
704
|
+
const scope: unknown = design.scope;
|
|
705
|
+
const scopes: unknown = design.scopes;
|
|
706
|
+
if (scope === undefined && scopes === undefined) return;
|
|
707
|
+
if (registration.scope === "tool") {
|
|
708
|
+
// The `scope` SHORTHAND keeps the refusal text it had before `scopes` existed, byte for byte
|
|
709
|
+
// (S19/L14/S2): the shorthand IS `scopes` with one object, so only a registration that SPELLS
|
|
710
|
+
// `scopes` carries the per-tool wording — and that wording names `scopes`, the member it used.
|
|
711
|
+
const message =
|
|
712
|
+
scopes === undefined
|
|
713
|
+
? `gentle-shell-bridge: invalid tool design ${JSON.stringify(design.id)} for tool ` +
|
|
714
|
+
`${JSON.stringify(registration.line)}: a scope is declared on a GROUPED registration only, and ` +
|
|
715
|
+
`\`{ tool, id, label, apply }\` is that tool's own Card ${JSON.stringify(registration.line)} ` +
|
|
716
|
+
"line, which means every call of the tool. To claim only SOME calls, register a grouped line " +
|
|
717
|
+
"`{ line, tools, scope, id, label, apply }` for the tool beside the line that already claims " +
|
|
718
|
+
"it. The registration was refused whole."
|
|
719
|
+
: `gentle-shell-bridge: invalid tool design ${JSON.stringify(design.id)} for tool ` +
|
|
720
|
+
`${JSON.stringify(registration.line)}: a scope is declared on a GROUPED registration only, and ` +
|
|
721
|
+
`\`{ tool, id, label, apply }\` is that tool's own Card ${JSON.stringify(registration.line)} ` +
|
|
722
|
+
"line, which means every call of the tool. To claim only SOME calls, register a grouped line " +
|
|
723
|
+
"`{ line, tools, scopes, id, label, apply }` for the tool beside the line that already " +
|
|
724
|
+
"claims it (`scopes` on an ungrouped registration was refused). The registration was refused whole.";
|
|
725
|
+
recordConflict({ tool: registration.line, id: design.id, kind: "scope", message });
|
|
726
|
+
throw new Error(message);
|
|
727
|
+
}
|
|
728
|
+
// The two spellings of one claim would disagree, so they cannot be combined.
|
|
729
|
+
if (scope !== undefined && scopes !== undefined) {
|
|
730
|
+
const message =
|
|
731
|
+
`gentle-shell-bridge: invalid tool design ${JSON.stringify(design.id)} for ` +
|
|
732
|
+
`${subjectOf(registration)}: \`scope\` and \`scopes\` cannot be combined. \`scope\` is the ` +
|
|
733
|
+
"shorthand for the SAME scope on every tool of `tools`; use `scopes` keyed by tool to give each " +
|
|
734
|
+
"tool its own scope. The registration was refused whole.";
|
|
735
|
+
recordConflict({ tool: registration.line, id: design.id, kind: "scope", message });
|
|
736
|
+
throw new Error(message);
|
|
737
|
+
}
|
|
738
|
+
if (scopes !== undefined) {
|
|
739
|
+
const problem = scopesProblem(scopes, registration.tools);
|
|
740
|
+
if (problem !== undefined) {
|
|
741
|
+
const message =
|
|
742
|
+
`gentle-shell-bridge: invalid tool design ${JSON.stringify(design.id)} for ` +
|
|
743
|
+
`${subjectOf(registration)}: ${problem} A scope is { description, matches }: ` +
|
|
744
|
+
"`description` is the one line the menu shows, and `matches(call)` answers " +
|
|
745
|
+
"whether the line owns that call (the call is { tool, args }). The registration was refused whole.";
|
|
746
|
+
recordConflict({ tool: registration.line, id: design.id, kind: "scope", message });
|
|
747
|
+
throw new Error(message);
|
|
748
|
+
}
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
const problem = scopeProblem(scope);
|
|
752
|
+
if (problem === undefined) return;
|
|
753
|
+
const message =
|
|
754
|
+
`gentle-shell-bridge: invalid tool design ${JSON.stringify(design.id)} for ` +
|
|
755
|
+
`${subjectOf(registration)}: scope ${problem}. A scope is { description, matches }: ` +
|
|
756
|
+
"`description` is the one line the menu shows, and `matches(call)` answers " +
|
|
757
|
+
"whether this line owns that call (the call is { tool, args }). The registration was refused whole.";
|
|
758
|
+
recordConflict({ tool: registration.line, id: design.id, kind: "scope", message });
|
|
759
|
+
throw new Error(message);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** What is wrong with one `ToolDesignScope` value, or `undefined` when it is valid (S7/S18). */
|
|
763
|
+
function scopeProblem(scope: unknown): string | undefined {
|
|
764
|
+
if (!isRecord(scope)) return "must be an object carrying both members";
|
|
765
|
+
if (typeof scope.description !== "string" || scope.description.trim() === "") {
|
|
766
|
+
return "needs a non-empty string as its description member";
|
|
767
|
+
}
|
|
768
|
+
if (/[\r\n]/u.test(scope.description)) {
|
|
769
|
+
return (
|
|
770
|
+
"description must be ONE line, and it carries a line break (CR or LF). It is rendered " +
|
|
771
|
+
"inside an existing menu row, so a break would add a row there; remove the break"
|
|
772
|
+
);
|
|
773
|
+
}
|
|
774
|
+
if (typeof scope.matches !== "function") return "needs a function as its matches member";
|
|
775
|
+
return undefined;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* What is wrong with a `scopes` map, or `undefined` when it is valid (S19). The map is keyed by the
|
|
780
|
+
* tools THIS registration paints: a key it does not paint claims calls it could never own, so it is a
|
|
781
|
+
* contradiction rather than a harmless extra. The message names the offending tool key when one is
|
|
782
|
+
* the problem, because that is the entry the author has to find.
|
|
783
|
+
*
|
|
784
|
+
* EVERY OWN key is validated, enumerable or not (S19/L14), and a symbol key is refused: `scopeOfClaim`
|
|
785
|
+
* reads the claim through `Object.hasOwn`, which sees the same own keys, so this list and that read
|
|
786
|
+
* are ONE key set. With `Object.keys` a non-enumerable `read: null` or a hidden foreign `find` passed
|
|
787
|
+
* validation and then reached the live registry — and, through the legacy-selection migration, the
|
|
788
|
+
* persisted selection.
|
|
789
|
+
*/
|
|
790
|
+
function scopesProblem(scopes: unknown, tools: readonly string[]): string | undefined {
|
|
791
|
+
if (!isRecord(scopes)) {
|
|
792
|
+
return (
|
|
793
|
+
"scopes must be an object keyed by tool name, each key one of the registration's `tools` and " +
|
|
794
|
+
"each value a { description, matches } scope."
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
const keys = Reflect.ownKeys(scopes);
|
|
798
|
+
if (keys.length === 0) {
|
|
799
|
+
return (
|
|
800
|
+
"scopes is empty: it maps each tool to the scope of its calls, so leave it out entirely to " +
|
|
801
|
+
"claim every call of every tool."
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
for (const key of keys) {
|
|
805
|
+
if (typeof key === "symbol") {
|
|
806
|
+
return (
|
|
807
|
+
`scopes carries the symbol key ${String(key)}, which cannot name one of this registration's ` +
|
|
808
|
+
`tools (${tools.join(", ")}). scopes is keyed by tool names, so every key is one of the ` +
|
|
809
|
+
"tools the design paints."
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
const tool = key;
|
|
813
|
+
if (!tools.includes(tool)) {
|
|
814
|
+
return (
|
|
815
|
+
`scopes names tool ${JSON.stringify(tool)}, which is not one of this registration's tools ` +
|
|
816
|
+
`(${tools.join(", ")}). scopes is keyed by the tools the design paints.`
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
const problem = scopeProblem(scopes[tool]);
|
|
820
|
+
if (problem !== undefined) return `scopes for tool ${JSON.stringify(tool)} ${problem}.`;
|
|
821
|
+
}
|
|
822
|
+
return undefined;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* S8 per (line, tool) (S19): every LIVE registration of one line that names a tool must carry the
|
|
827
|
+
* SAME scope object for that tool (`===`; both absent counts as equal). A registration that does not
|
|
828
|
+
* name the tool does not constrain it, so one line may own some tools unconditionally and narrow
|
|
829
|
+
* others. A registration that replaces the live entry with the same (`line`, `id`) is exempt, so a
|
|
830
|
+
* plugin may re-register its own design with a fresh scope object; any other disagreement refuses the
|
|
831
|
+
* whole registration and is recorded under the `scope` kind.
|
|
832
|
+
*/
|
|
833
|
+
function assertOneScopePerClaim(
|
|
834
|
+
registration: NormalizedRegistration,
|
|
835
|
+
design: ToolDesignRegistration,
|
|
836
|
+
): void {
|
|
837
|
+
const line = toolDesignIndex(designState()).byKey.get(
|
|
838
|
+
lineKey(registration.scope, registration.line),
|
|
839
|
+
);
|
|
840
|
+
if (line === undefined) return;
|
|
841
|
+
for (const entry of line.entries) {
|
|
842
|
+
// Its own entry is the one this registration replaces: that is the documented exemption.
|
|
843
|
+
if (entry.design.id === design.id) continue;
|
|
844
|
+
for (const tool of registration.tools) {
|
|
845
|
+
if (!entry.tools.includes(tool)) continue;
|
|
846
|
+
if (scopeOfClaim(entry.design, tool) === scopeOfClaim(design, tool)) continue;
|
|
847
|
+
// The `scope` SHORTHAND keeps the diagnostics it had before `scopes` existed, byte for byte
|
|
848
|
+
// (S19/L14/S2): it IS `scopes` with one object, so its refusal must not change shape. The
|
|
849
|
+
// per-tool wording belongs to a registration that SPELLS `scopes`.
|
|
850
|
+
const message =
|
|
851
|
+
design.scopes === undefined
|
|
852
|
+
? `gentle-shell-bridge: design ${JSON.stringify(design.id)} for ${subjectOf(registration)} was ` +
|
|
853
|
+
`refused whole: the line already carries a different scope on design ` +
|
|
854
|
+
`${JSON.stringify(entry.design.id)}. One line is one switch, so every live registration of a ` +
|
|
855
|
+
"line must share the SAME scope object (S8); re-register the same (line, id) to change it."
|
|
856
|
+
: `gentle-shell-bridge: design ${JSON.stringify(design.id)} for ${subjectOf(registration)} ` +
|
|
857
|
+
`was refused whole: the line already carries a different scope on tool ` +
|
|
858
|
+
`${JSON.stringify(tool)} (design ${JSON.stringify(entry.design.id)}). One line is one switch ` +
|
|
859
|
+
`per call of a tool, so every live registration of a line that names tool ` +
|
|
860
|
+
`${JSON.stringify(tool)} must share the SAME scope object for it (S8); a registration that ` +
|
|
861
|
+
"does not name the tool does not constrain it, and re-registering the same (line, id) is " +
|
|
862
|
+
"exempt. Re-register the same (line, id) to change a scope.";
|
|
863
|
+
recordConflict({ tool: registration.line, id: design.id, kind: "scope", message });
|
|
864
|
+
throw new Error(message);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function recordConflict(conflict: ToolDesignConflict): void {
|
|
870
|
+
const state = designState();
|
|
871
|
+
const index = state.conflicts.findIndex(
|
|
872
|
+
(candidate) =>
|
|
873
|
+
candidate.tool === conflict.tool &&
|
|
874
|
+
candidate.id === conflict.id &&
|
|
875
|
+
candidate.kind === conflict.kind &&
|
|
876
|
+
candidate.message === conflict.message,
|
|
877
|
+
);
|
|
878
|
+
if (index === -1) state.conflicts.push(conflict);
|
|
879
|
+
else state.conflicts[index] = conflict;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Register a design for one tool (ungrouped), for one line over several tools (grouped), or an array
|
|
884
|
+
* of registrations of either shape, and return a disposer that removes exactly what this call
|
|
885
|
+
* registered. Two designs of one line from one call are the same as two calls: they join that line.
|
|
886
|
+
*
|
|
887
|
+
* Same entry discipline as every other bridge surface, because a plugin author should not have to
|
|
888
|
+
* learn two:
|
|
889
|
+
*
|
|
890
|
+
* - a second registration with the same (`line`, `id`) replaces the previous one IN PLACE, so the
|
|
891
|
+
* value keeps the position its registration earned — and the replacement is RECORDED, because a
|
|
892
|
+
* silent takeover is exactly what two plugins must never experience (`getToolDesignConflicts`);
|
|
893
|
+
* - the returned disposer removes only the registration it created: after a replacement the replaced
|
|
894
|
+
* design's disposer is a no-op instead of removing the replacement, and calling it twice does
|
|
895
|
+
* nothing the second time;
|
|
896
|
+
* - validation happens BEFORE anything is stored (shape, tool, line, id, label, distinctness,
|
|
897
|
+
* `apply`, tool ownership), so a refused design never half-registers and never reaches a menu;
|
|
898
|
+
* - the registration is ATTRIBUTED like every other surface (`./plugin-lines.ts`): a plugin that
|
|
899
|
+
* contributes only designs still gets its one INTEGRATIONS line, its `name` is honoured once for
|
|
900
|
+
* the identity, and a refused design shows up as a rejection instead of disappearing.
|
|
901
|
+
*/
|
|
902
|
+
export function registerToolDesign(
|
|
903
|
+
design: ToolDesignRegistration | readonly ToolDesignRegistration[],
|
|
904
|
+
): () => void {
|
|
905
|
+
if (!Array.isArray(design)) return registerOneToolDesign(design as ToolDesignRegistration);
|
|
906
|
+
// A batch is all-or-nothing: a refused design never leaves the ones before it registered, and the
|
|
907
|
+
// single disposer removes exactly the designs this call stored.
|
|
908
|
+
const state = designState();
|
|
909
|
+
const before = [...state.entries];
|
|
910
|
+
const disposers: Array<() => void> = [];
|
|
911
|
+
try {
|
|
912
|
+
for (const one of design as readonly ToolDesignRegistration[]) {
|
|
913
|
+
disposers.push(registerOneToolDesign(one));
|
|
914
|
+
}
|
|
915
|
+
} catch (error) {
|
|
916
|
+
for (const dispose of disposers.reverse()) dispose();
|
|
917
|
+
// A batch that failed also RESTORES a grouped design it replaced on its way, so "all or nothing"
|
|
918
|
+
// covers the entries it overwrote and not only the ones it appended.
|
|
919
|
+
state.entries.length = 0;
|
|
920
|
+
state.entries.push(...before);
|
|
921
|
+
bumpRevision(state);
|
|
922
|
+
throw error;
|
|
923
|
+
}
|
|
924
|
+
return () => {
|
|
925
|
+
for (const dispose of disposers) dispose();
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
function registerOneToolDesign(design: ToolDesignRegistration): () => void {
|
|
930
|
+
return attributeRegistration("tool design", design, undefined, () =>
|
|
931
|
+
registerToolDesignStored(design),
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/** The store half: validate, then replace in place or append. Called through attribution above. */
|
|
936
|
+
function registerToolDesignStored(design: ToolDesignRegistration): () => void {
|
|
937
|
+
const registration = normalizeRegistration(design);
|
|
938
|
+
// Reused from the bus: identity is a plain-object key and the store's key set. The message reads
|
|
939
|
+
// "invalid tool name" / "invalid tool design id", both of which name the field that was wrong.
|
|
940
|
+
assertRegistrationId("tool design", design.id);
|
|
941
|
+
assertScope(registration, design);
|
|
942
|
+
assertLabel(subjectOf(registration), design.id, design.label);
|
|
943
|
+
assertApplyFunction(subjectOf(registration), design.id, design.apply);
|
|
944
|
+
assertOneScopePerClaim(registration, design);
|
|
945
|
+
assertToolsAreFree(registration, design);
|
|
946
|
+
assertLabelIsDistinct(registration, design);
|
|
947
|
+
|
|
948
|
+
const state = designState();
|
|
949
|
+
const entry: DesignEntry = {
|
|
950
|
+
scope: registration.scope,
|
|
951
|
+
line: registration.line,
|
|
952
|
+
tools: [...registration.tools],
|
|
953
|
+
design,
|
|
954
|
+
};
|
|
955
|
+
const index = state.entries.findIndex(
|
|
956
|
+
(candidate) =>
|
|
957
|
+
candidate.scope === registration.scope &&
|
|
958
|
+
candidate.line === registration.line &&
|
|
959
|
+
candidate.design.id === design.id,
|
|
960
|
+
);
|
|
961
|
+
if (index === -1) {
|
|
962
|
+
state.entries.push(entry);
|
|
963
|
+
} else {
|
|
964
|
+
const previous = state.entries[index].design;
|
|
965
|
+
if (previous !== design) {
|
|
966
|
+
const ownership = registration.scope === "tool" ? "tool" : "line";
|
|
967
|
+
recordConflict({
|
|
968
|
+
tool: registration.line,
|
|
969
|
+
id: design.id,
|
|
970
|
+
kind: "replaced",
|
|
971
|
+
message:
|
|
972
|
+
`gentle-shell-bridge: design ${JSON.stringify(design.id)} for ${ownership} ` +
|
|
973
|
+
`${JSON.stringify(registration.line)} was registered again and the later registration is ` +
|
|
974
|
+
`the live one. Only one design can own a (${ownership}, id); the replacement is reported ` +
|
|
975
|
+
"here instead of happening silently.",
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
state.entries[index] = entry;
|
|
979
|
+
}
|
|
980
|
+
bumpRevision(state);
|
|
981
|
+
// Best-effort and never fatal: the registration is already live, so a store that cannot be read or
|
|
982
|
+
// written must not leave a live design with no disposer (the legacy value is simply not migrated).
|
|
983
|
+
try {
|
|
984
|
+
migrateLegacySelection(registration);
|
|
985
|
+
} catch (error) {
|
|
986
|
+
recordToolDesignFailure(
|
|
987
|
+
registration.line,
|
|
988
|
+
design.id,
|
|
989
|
+
`the legacy per-tool selection could not be migrated to ` +
|
|
990
|
+
`${JSON.stringify(registration.line)} (${errorMessage(error)}); the line keeps its own value.`,
|
|
991
|
+
);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
let disposed = false;
|
|
995
|
+
return () => {
|
|
996
|
+
if (disposed) return;
|
|
997
|
+
disposed = true;
|
|
998
|
+
const current = state.entries.indexOf(entry);
|
|
999
|
+
if (current !== -1) {
|
|
1000
|
+
state.entries.splice(current, 1);
|
|
1001
|
+
bumpRevision(state);
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* D3: a tool joining a line that has NO stored value lends that line its legacy per-tool value when
|
|
1008
|
+
* that value names a design of the line, and the line value is written then. Registration time only —
|
|
1009
|
+
* never the render path. A line that already has a value is never migrated again, and the legacy
|
|
1010
|
+
* `selections` map is left exactly as it was.
|
|
1011
|
+
*/
|
|
1012
|
+
function migrateLegacySelection(registration: NormalizedRegistration): void {
|
|
1013
|
+
if (registration.scope !== "line") return;
|
|
1014
|
+
const selections = readSelections();
|
|
1015
|
+
if (Object.hasOwn(selections.lines, registration.line)) return;
|
|
1016
|
+
const line = toolDesignIndex(designState()).byKey.get(
|
|
1017
|
+
lineKey(registration.scope, registration.line),
|
|
1018
|
+
);
|
|
1019
|
+
if (line === undefined) return;
|
|
1020
|
+
for (const tool of registration.tools) {
|
|
1021
|
+
const legacy = storedId(selections.tools, tool);
|
|
1022
|
+
if (legacy === TOOL_DESIGN_NATIVE) continue;
|
|
1023
|
+
if (!line.entries.some((candidate) => candidate.design.id === legacy)) continue;
|
|
1024
|
+
writeSelections({
|
|
1025
|
+
tools: selections.tools,
|
|
1026
|
+
lines: { ...selections.lines, [registration.line]: legacy },
|
|
1027
|
+
});
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/** Conflicts recorded by registration, newest per (tool/line, id, kind, message), for the diagnostics record. */
|
|
1033
|
+
export function getToolDesignConflicts(): ToolDesignConflict[] {
|
|
1034
|
+
return designState().conflicts.map((conflict) => ({ ...conflict }));
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Record one design failure, ONCE per (tool, id, message): a design that throws every frame is one
|
|
1039
|
+
* fact, not one line per frame. Bounded, so a plugin cannot grow the report without limit.
|
|
1040
|
+
*/
|
|
1041
|
+
export function recordToolDesignFailure(tool: string, designId: string, message: string): void {
|
|
1042
|
+
const state = designState();
|
|
1043
|
+
const duplicate = state.failures.some(
|
|
1044
|
+
(failure) =>
|
|
1045
|
+
failure.tool === tool && failure.designId === designId && failure.message === message,
|
|
1046
|
+
);
|
|
1047
|
+
if (duplicate) return;
|
|
1048
|
+
state.failures.push({ tool, designId, message });
|
|
1049
|
+
if (state.failures.length > MAX_FAILURES) {
|
|
1050
|
+
state.failures.splice(0, state.failures.length - MAX_FAILURES);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
/** The failures the last frames recorded, oldest first. Read by the diagnostics record. */
|
|
1055
|
+
export function getToolDesignFailures(): ToolDesignFailure[] {
|
|
1056
|
+
return designState().failures.map((failure) => ({ ...failure }));
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* Hand the host's own rows to the selected design (`./tool-design-wiring.ts` calls this on every
|
|
1061
|
+
* render pass).
|
|
1062
|
+
*
|
|
1063
|
+
* Returns `undefined` when the host's rows must stand — a throw, or a return value that is not an
|
|
1064
|
+
* array of strings — so the caller draws the host's card unchanged. The design receives a COPY: a
|
|
1065
|
+
* design that mutates its input cannot corrupt the array the host's component still owns.
|
|
1066
|
+
*/
|
|
1067
|
+
export function applyToolDesign(
|
|
1068
|
+
design: ToolDesignRegistration,
|
|
1069
|
+
lines: readonly string[],
|
|
1070
|
+
ctx: ToolDesignContext,
|
|
1071
|
+
): string[] | undefined {
|
|
1072
|
+
let applied: unknown;
|
|
1073
|
+
try {
|
|
1074
|
+
applied = design.apply([...lines], ctx);
|
|
1075
|
+
} catch (error) {
|
|
1076
|
+
recordToolDesignFailure(ctx.tool, design.id, errorMessage(error));
|
|
1077
|
+
return undefined;
|
|
1078
|
+
}
|
|
1079
|
+
if (!Array.isArray(applied) || !applied.every((line) => typeof line === "string")) {
|
|
1080
|
+
recordToolDesignFailure(
|
|
1081
|
+
ctx.tool,
|
|
1082
|
+
design.id,
|
|
1083
|
+
"apply returned something that is not an array of strings; the host's own rows are drawn " +
|
|
1084
|
+
"instead. Return the rows to draw, e.g. `return lines`.",
|
|
1085
|
+
);
|
|
1086
|
+
return undefined;
|
|
1087
|
+
}
|
|
1088
|
+
// SAFETY: the element check above proves every member is a string.
|
|
1089
|
+
return applied as string[];
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
// ---------------------------------------------------------------------------------------------
|
|
1093
|
+
// The selection: one persisted value per line
|
|
1094
|
+
// ---------------------------------------------------------------------------------------------
|
|
1095
|
+
|
|
1096
|
+
interface SelectionState {
|
|
1097
|
+
/** The unchanged per-tool map (`selections`). */
|
|
1098
|
+
tools: Record<string, unknown>;
|
|
1099
|
+
/** The per-line map (`lines`), beside it in the same file. */
|
|
1100
|
+
lines: Record<string, unknown>;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
function asRecord(value: unknown): Record<string, unknown> {
|
|
1104
|
+
return isRecord(value) ? value : {};
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* The persisted selections, read live. An unreadable or malformed file reads as empty, so a
|
|
1109
|
+
* hand-edited file can never select a design the user did not choose. ONE logical read of the
|
|
1110
|
+
* bridge's store per call, which is what the cost bound counts (S9/D6).
|
|
1111
|
+
*/
|
|
1112
|
+
function readSelections(): SelectionState {
|
|
1113
|
+
designState().selectionReads += 1;
|
|
1114
|
+
// ONE `get`, therefore one file read: the line map travels inside the same value under a reserved
|
|
1115
|
+
// key (S9/D6). A file written before grouped lines is a flat `{ <tool>: <id> }` and reads as-is.
|
|
1116
|
+
const stored = asRecord(createStore(TOOL_DESIGN_STORE_ID).get<unknown>(TOOL_DESIGN_STORE_KEY, {}));
|
|
1117
|
+
const { [TOOL_DESIGN_LINE_STORE_KEY]: lines, ...tools } = stored;
|
|
1118
|
+
return { tools, lines: asRecord(lines) };
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* The one value to persist: the tool selections flat, the line selections under the reserved key — and
|
|
1123
|
+
* the reserved key is written ONLY when a line has a value, so a session with no grouped line keeps the
|
|
1124
|
+
* document it has always had, byte for byte.
|
|
1125
|
+
*/
|
|
1126
|
+
function writeSelections(state: SelectionState): void {
|
|
1127
|
+
const document: Record<string, unknown> = { ...state.tools };
|
|
1128
|
+
if (Object.keys(state.lines).length > 0) {
|
|
1129
|
+
document[TOOL_DESIGN_LINE_STORE_KEY] = state.lines;
|
|
1130
|
+
}
|
|
1131
|
+
createStore(TOOL_DESIGN_STORE_ID).set(TOOL_DESIGN_STORE_KEY, document);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function storedId(map: Record<string, unknown>, key: string): string {
|
|
1135
|
+
if (!Object.hasOwn(map, key)) return TOOL_DESIGN_NATIVE;
|
|
1136
|
+
const stored = map[key];
|
|
1137
|
+
return typeof stored === "string" && stored.trim() !== "" ? stored : TOOL_DESIGN_NATIVE;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* How many persisted-selection reads happened since the last reset. Each one is a single logical read
|
|
1142
|
+
* of the bridge's tool-design store, so a caller can bound the file work a switch or a frame performs
|
|
1143
|
+
* instead of timing it (S13/T23). Read by the cost test; not part of the public API.
|
|
1144
|
+
*/
|
|
1145
|
+
export function getToolDesignSelectionReads(): number {
|
|
1146
|
+
return designState().selectionReads;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/** Test-only seam for the read counter; deliberately not re-exported from `src/index.ts`. */
|
|
1150
|
+
export function resetToolDesignSelectionReads(): void {
|
|
1151
|
+
designState().selectionReads = 0;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/** The design id stored for one LEGACY per-tool value, or `native` when it has none. */
|
|
1155
|
+
export function getToolDesignSelection(tool: string): string {
|
|
1156
|
+
return storedId(readSelections().tools, tool);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/** The design id stored for one line, or `native` when it has none. */
|
|
1160
|
+
export function getToolDesignLineSelection(scope: "tool" | "line", line: string): string {
|
|
1161
|
+
const selections = readSelections();
|
|
1162
|
+
return storedId(scope === "tool" ? selections.tools : selections.lines, line);
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* Persist one line's selection, FIRST and synchronously: the repaint reads the stored value, so the
|
|
1167
|
+
* bullet cannot land on a value that was never written. Ungrouped lines write the historical
|
|
1168
|
+
* `selections` map; grouped ones write `lines` beside it (D3).
|
|
1169
|
+
*/
|
|
1170
|
+
export function setToolDesignLineSelection(
|
|
1171
|
+
scope: "tool" | "line",
|
|
1172
|
+
line: string,
|
|
1173
|
+
id: string,
|
|
1174
|
+
): void {
|
|
1175
|
+
const selections = readSelections();
|
|
1176
|
+
if (scope === "tool") writeSelections({ tools: { ...selections.tools, [line]: id }, lines: selections.lines });
|
|
1177
|
+
else writeSelections({ tools: selections.tools, lines: { ...selections.lines, [line]: id } });
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/** Persist one tool's (ungrouped) selection. Kept as the historical name. */
|
|
1181
|
+
export function setToolDesignSelection(tool: string, id: string): void {
|
|
1182
|
+
setToolDesignLineSelection("tool", tool, id);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* One live line's claim on one tool, with the value stored for that line: the answer the diagnostics record
|
|
1187
|
+
* gives for a tool that has SCOPED claims (S14). `scopeDescription` is the scope of THIS tool's claim
|
|
1188
|
+
* (S19), so a line that narrows only some of its tools reports the scope of the tool being listed.
|
|
1189
|
+
*/
|
|
1190
|
+
export interface ToolDesignClaim {
|
|
1191
|
+
/** `tool` for an ungrouped design's implicit line, `line` for a registered line. */
|
|
1192
|
+
scope: "tool" | "line";
|
|
1193
|
+
/** The selection key: the tool name, or the registered line id. */
|
|
1194
|
+
line: string;
|
|
1195
|
+
/** The design id stored for the line, or `native`. */
|
|
1196
|
+
stored: string;
|
|
1197
|
+
/** The scope's description when this (line, tool) claim is scoped (S7/S8/S19). */
|
|
1198
|
+
scopeDescription?: string;
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
/**
|
|
1202
|
+
* Every line that CLAIMS one tool, in creation order — the one unscoped claim and the scoped ones
|
|
1203
|
+
* (S9) — each with its stored value and the scope of THAT tool, read from ONE snapshot of the
|
|
1204
|
+
* persisted selections.
|
|
1205
|
+
*/
|
|
1206
|
+
export function toolDesignClaims(tool: string): ToolDesignClaim[] {
|
|
1207
|
+
const claims = toolDesignIndex(designState()).claimsByTool.get(tool) ?? [];
|
|
1208
|
+
if (claims.length === 0) return [];
|
|
1209
|
+
const selections = readSelections();
|
|
1210
|
+
return claims.map((claim) => ({
|
|
1211
|
+
scope: claim.line.scope,
|
|
1212
|
+
line: claim.line.line,
|
|
1213
|
+
stored: storedId(
|
|
1214
|
+
claim.line.scope === "tool" ? selections.tools : selections.lines,
|
|
1215
|
+
claim.line.line,
|
|
1216
|
+
),
|
|
1217
|
+
...(claim.scope === undefined ? {} : { scopeDescription: claim.scope.description }),
|
|
1218
|
+
}));
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* What the stored value means right now for one tool: `native` (the host's own card), the live
|
|
1223
|
+
* `design`, `missing` when the design it names left the line, or `not-covered` when the selected
|
|
1224
|
+
* value is a design of the line that does not name this tool (S6/D4). Both degrade to the host's
|
|
1225
|
+
* card; the difference is what the diagnostics record reports.
|
|
1226
|
+
*/
|
|
1227
|
+
export type ToolDesignSelection =
|
|
1228
|
+
| { kind: "native" }
|
|
1229
|
+
| { kind: "design"; design: ToolDesignRegistration }
|
|
1230
|
+
| { kind: "missing"; id: string }
|
|
1231
|
+
| { kind: "not-covered"; id: string };
|
|
1232
|
+
|
|
1233
|
+
/** The stored value of one line inside one snapshot of the persisted selections. */
|
|
1234
|
+
function storedFor(selections: SelectionState, line: IndexedLine): string {
|
|
1235
|
+
return storedId(line.scope === "tool" ? selections.tools : selections.lines, line.line);
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/**
|
|
1239
|
+
* The answer one line's stored value gives for one tool. `native`, `missing` and `not-covered`
|
|
1240
|
+
* DECLINE a call: the next candidate answers (S10).
|
|
1241
|
+
*/
|
|
1242
|
+
function selectionOfLine(
|
|
1243
|
+
index: ToolDesignIndex,
|
|
1244
|
+
scope: "tool" | "line",
|
|
1245
|
+
line: string,
|
|
1246
|
+
stored: string,
|
|
1247
|
+
tool: string,
|
|
1248
|
+
): ToolDesignSelection {
|
|
1249
|
+
if (stored === TOOL_DESIGN_NATIVE) return { kind: "native" };
|
|
1250
|
+
const indexed = index.byKey.get(lineKey(scope, line));
|
|
1251
|
+
const entry = indexed?.entries.find((candidate) => candidate.design.id === stored);
|
|
1252
|
+
if (entry === undefined) return { kind: "missing", id: stored };
|
|
1253
|
+
return entry.tools.includes(tool)
|
|
1254
|
+
? { kind: "design", design: entry.design }
|
|
1255
|
+
: { kind: "not-covered", id: stored };
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* Does this line own THIS call (S10)? The scope sees `{ tool, args }` and nothing else — never the card's
|
|
1260
|
+
* `part` — so both halves are decided by this same procedure. It is not promised the same input: the
|
|
1261
|
+
* arguments are the host's object, read again on every pass (see `ToolDesignCall`), and the selection is
|
|
1262
|
+
* read on every pass too, so a switch or a mutated argument can legitimately change the answer between the
|
|
1263
|
+
* two halves. A scope that throws, or that answers with anything but a boolean, counts as
|
|
1264
|
+
* "does not match" and is recorded once per (tool, design id, message); resolution never throws.
|
|
1265
|
+
*/
|
|
1266
|
+
/**
|
|
1267
|
+
* Swallow a rejected thenable without ever throwing (S18). Both halves reach PLUGIN code: reading
|
|
1268
|
+
* `then` can run a getter, and calling it can run anything, so both happen inside a try. A value that
|
|
1269
|
+
* cannot even be drained is still only "not an answer", and the failure the caller already recorded
|
|
1270
|
+
* is the whole report. A native rejected promise IS drained here, so it never reaches the process as
|
|
1271
|
+
* an unhandled rejection.
|
|
1272
|
+
*
|
|
1273
|
+
* The value the `then` CALL RETURNS is drained too, because it can itself be rejecting: an `async
|
|
1274
|
+
* then` cannot throw to its caller — it rejects the promise it returns — and dropping that promise
|
|
1275
|
+
* lets `--unhandled-rejections=strict` fail the host on a paint already declined. The recursion is
|
|
1276
|
+
* bounded, so a thenable chain cannot loop forever and a promise that keeps handing back promises
|
|
1277
|
+
* costs at most `THENABLE_DRAIN_DEPTH` steps.
|
|
1278
|
+
*/
|
|
1279
|
+
function drainThenable(value: unknown, depth = THENABLE_DRAIN_DEPTH): void {
|
|
1280
|
+
if (depth <= 0) return;
|
|
1281
|
+
let returned: unknown;
|
|
1282
|
+
try {
|
|
1283
|
+
// SAFETY: a plugin is plain JavaScript, so `then` is read as `unknown` and re-checked.
|
|
1284
|
+
const then = (value as { then?: unknown }).then;
|
|
1285
|
+
if (typeof then !== "function") return;
|
|
1286
|
+
returned = (
|
|
1287
|
+
then as (this: unknown, onFulfilled: undefined, onRejected: () => void) => unknown
|
|
1288
|
+
).call(value, undefined, () => {});
|
|
1289
|
+
} catch {
|
|
1290
|
+
// A `then` getter or a `then` body that throws synchronously is not a rejection: the decline
|
|
1291
|
+
// stands as recorded, and there is no value to drain.
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
drainThenable(returned, depth - 1);
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
function scopeMatches(claim: IndexedClaim, tool: string, args: unknown): boolean {
|
|
1298
|
+
const scope = claim.scope;
|
|
1299
|
+
if (scope === undefined) return false;
|
|
1300
|
+
const designId = claim.entry.design.id;
|
|
1301
|
+
let answer: unknown;
|
|
1302
|
+
try {
|
|
1303
|
+
answer = scope.matches({ tool, args });
|
|
1304
|
+
} catch (error) {
|
|
1305
|
+
recordToolDesignFailure(
|
|
1306
|
+
tool,
|
|
1307
|
+
designId,
|
|
1308
|
+
`the scope of design ${JSON.stringify(designId)} threw (${errorMessage(error)}); ` +
|
|
1309
|
+
"the call counts as one the line does not own, so the line declined it.",
|
|
1310
|
+
);
|
|
1311
|
+
return false;
|
|
1312
|
+
}
|
|
1313
|
+
// A paint cannot await (S18/S10), so only `true` and `false` ARE answers: a Promise, a truthy
|
|
1314
|
+
// string, `undefined` or anything else is a scope that has not answered NOW, and the call counts as
|
|
1315
|
+
// one the line does not own. Recorded once per (tool, design id, message), like a throw.
|
|
1316
|
+
if (typeof answer !== "boolean") {
|
|
1317
|
+
recordToolDesignFailure(
|
|
1318
|
+
tool,
|
|
1319
|
+
designId,
|
|
1320
|
+
`the scope of design ${JSON.stringify(designId)} answered with ${typeof answer} ` +
|
|
1321
|
+
"instead of a boolean, and a paint cannot await; the call counts as one the line does not " +
|
|
1322
|
+
"own, so the line declined it. Return true or false from matches.",
|
|
1323
|
+
);
|
|
1324
|
+
// A scope that returned a Promise is a plugin bug the bridge must not turn into a host crash: an
|
|
1325
|
+
// unhandled rejection can take the process down on a host that treats it as fatal, and this paint
|
|
1326
|
+
// declines the call anyway. Inspection itself is isolated, so a thenable that throws while it is
|
|
1327
|
+
// read or drained leaves the failure recorded above as the whole report.
|
|
1328
|
+
drainThenable(answer);
|
|
1329
|
+
return false;
|
|
1330
|
+
}
|
|
1331
|
+
return answer;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
/**
|
|
1335
|
+
* Resolve one tool's card for ONE call against the live registry. Never throws.
|
|
1336
|
+
*
|
|
1337
|
+
* The candidates, in order (S10): the scoped claims of the tool whose scope matches `{ tool, args }`,
|
|
1338
|
+
* in registration order, then the unscoped claim of the tool, if any. The first candidate whose
|
|
1339
|
+
* line's stored value is a live design covering the tool answers; otherwise the LAST candidate's
|
|
1340
|
+
* answer stands — today's answer, byte for byte, when the only candidate is the unscoped claim.
|
|
1341
|
+
*
|
|
1342
|
+
* With no `args` argument no scoped claim is a candidate, so a consumer that only knows a tool name
|
|
1343
|
+
* keeps answering exactly as before. The persisted selections are read ONCE per call (S11), never
|
|
1344
|
+
* once per candidate.
|
|
1345
|
+
*
|
|
1346
|
+
* The HISTORICAL per-tool value answers only for a tool with NO live claim at all (S10/L6): that is
|
|
1347
|
+
* what keeps a stale legacy selection resolving to `missing` for a disposed design (S2). When the
|
|
1348
|
+
* tool HAS live claims and every one of them declined the call — each scoped claim's predicate
|
|
1349
|
+
* missed, `args` were omitted, or a value was `native` — the answer is `{ kind: "native" }`: never a
|
|
1350
|
+
* value re-entered from `selections.tools[tool]`, which for an implicit line is that line's own
|
|
1351
|
+
* stored value.
|
|
1352
|
+
*/
|
|
1353
|
+
export function resolveToolDesignSelection(tool: string, args?: unknown): ToolDesignSelection {
|
|
1354
|
+
const state = designState();
|
|
1355
|
+
const index = toolDesignIndex(state);
|
|
1356
|
+
// ONE snapshot serves every candidate: one logical store read per resolve (S11).
|
|
1357
|
+
const selections = readSelections();
|
|
1358
|
+
const claims = index.claimsByTool.get(tool) ?? [];
|
|
1359
|
+
const scoped: IndexedClaim[] = [];
|
|
1360
|
+
let unscoped: IndexedClaim | undefined;
|
|
1361
|
+
for (const claim of claims) {
|
|
1362
|
+
if (claim.scope === undefined) {
|
|
1363
|
+
if (unscoped === undefined) unscoped = claim;
|
|
1364
|
+
continue;
|
|
1365
|
+
}
|
|
1366
|
+
// No arguments: the call is unknown, so no scoped claim applies (S10).
|
|
1367
|
+
if (args === undefined) continue;
|
|
1368
|
+
if (!scopeMatches(claim, tool, args)) continue;
|
|
1369
|
+
scoped.push(claim);
|
|
1370
|
+
}
|
|
1371
|
+
const candidates = unscoped === undefined ? scoped : [...scoped, unscoped];
|
|
1372
|
+
if (candidates.length === 0) {
|
|
1373
|
+
// Live claims exist and every one declined this call: the host's own card, never the stored value
|
|
1374
|
+
// of a line that just declined it.
|
|
1375
|
+
if (claims.length > 0) return { kind: "native" };
|
|
1376
|
+
// No live claim at all: the historical per-tool value still answers, so a design that was
|
|
1377
|
+
// disposed keeps resolving to `missing` instead of silently to `default`.
|
|
1378
|
+
return selectionOfLine(index, "tool", tool, storedId(selections.tools, tool), tool);
|
|
1379
|
+
}
|
|
1380
|
+
for (const candidate of candidates) {
|
|
1381
|
+
const selection = selectionOfLine(
|
|
1382
|
+
index,
|
|
1383
|
+
candidate.line.scope,
|
|
1384
|
+
candidate.line.line,
|
|
1385
|
+
storedFor(selections, candidate.line),
|
|
1386
|
+
tool,
|
|
1387
|
+
);
|
|
1388
|
+
if (selection.kind === "design") return selection;
|
|
1389
|
+
}
|
|
1390
|
+
const last = candidates[candidates.length - 1];
|
|
1391
|
+
return selectionOfLine(
|
|
1392
|
+
index,
|
|
1393
|
+
last.line.scope,
|
|
1394
|
+
last.line.line,
|
|
1395
|
+
storedFor(selections, last.line),
|
|
1396
|
+
tool,
|
|
1397
|
+
);
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
/** Test-only seam: empties the registry, the conflict records and the failure records in place. */
|
|
1401
|
+
export function resetToolDesignsForTests(): void {
|
|
1402
|
+
const state = designState();
|
|
1403
|
+
state.entries.length = 0;
|
|
1404
|
+
state.conflicts.length = 0;
|
|
1405
|
+
state.failures.length = 0;
|
|
1406
|
+
state.selectionReads = 0;
|
|
1407
|
+
state.lineOrder.clear();
|
|
1408
|
+
state.lineOrderSeq = 0;
|
|
1409
|
+
state.revision += 1;
|
|
1410
|
+
delete state.index;
|
|
1411
|
+
}
|