pi-odd-worktree 3.15.1 → 3.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -25
- package/extensions/odd-worktree-tools.ts +64 -211
- 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/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,877 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The INTEGRATIONS line (I1–I7): exactly one footer row per plugin that registers with this bridge.
|
|
3
|
+
*
|
|
4
|
+
* Pi renders an extension's own `ctx.ui.setStatus(key, text)` map: a row is the WHOLE opaque status
|
|
5
|
+
* string the extension set (`extensions/gentle-shell.ts:357-385`, `lib/shell-bar.ts:289-298`). So a
|
|
6
|
+
* plugin that contributes ten rows, sections, styles and transforms still gets ONE line here — this
|
|
7
|
+
* module owns that line, and the plugin never writes one.
|
|
8
|
+
*
|
|
9
|
+
* ## Attribution without configuration (I2, I8, I11)
|
|
10
|
+
*
|
|
11
|
+
* A registration arrives through `./bus.ts`, which captures the calling module from the stack ONCE
|
|
12
|
+
* per registration: the first frame outside this package's `src/`. That module is then resolved to
|
|
13
|
+
* its owning package by walking up from its directory to the nearest `package.json` with a non-empty
|
|
14
|
+
* `name`, and BOTH that answer and the plugin IDENTITY it implies are cached per module — a
|
|
15
|
+
* plugin's second registration costs one map lookup, never another stack walk or disk read. The
|
|
16
|
+
* identity is the package root, or the registering module when no package owns it (I8); the record,
|
|
17
|
+
* and the footer key, are keyed from it, never from a display name (I10).
|
|
18
|
+
*
|
|
19
|
+
* Four outcomes:
|
|
20
|
+
*
|
|
21
|
+
* - **a package**: a plugin, and this package's own package root is no exception — odd-worktree is
|
|
22
|
+
* an ordinary plugin whose registrations keep their footer line. Only the shell API's own
|
|
23
|
+
* directory (`lib/shell-api/`, the module that captured the caller) is plumbing: its providers,
|
|
24
|
+
* sections and fallbacks never claim a footer row, because they are odd-worktree's own code
|
|
25
|
+
* registered on a plugin's behalf.
|
|
26
|
+
* - **the Pi distribution** (I11): the package that owns the running `pi` entry point, or one whose
|
|
27
|
+
* manifest declares the `pi` executable, is never a plugin identity. A shared extensions
|
|
28
|
+
* directory inside the Pi installation therefore fails closed exactly like an unattributable
|
|
29
|
+
* module, and only an explicit name gives it a row.
|
|
30
|
+
* - **a module this package cannot attribute** (a single copied file, e.g.
|
|
31
|
+
* `~/.pi/agent/extensions/foo.ts`, or a registration with no caller frame outside the shell API):
|
|
32
|
+
* FAIL CLOSED. No line is set, and the report says why. The only way such a module gets a line is
|
|
33
|
+
* the author naming it explicitly, which is not an invented name.
|
|
34
|
+
*
|
|
35
|
+
* ## The name (I3, I9)
|
|
36
|
+
*
|
|
37
|
+
* The name obeys the same shape rule as every other row name (`./row-names.ts`): one to three plain
|
|
38
|
+
* words with no arrow, colon, dot, pipe, slash or bracket. A derived name is cleaned from the
|
|
39
|
+
* package name — scope dropped, separators and marks become word breaks, the first word capitalised,
|
|
40
|
+
* clamped to three words — so a derived name can never break the rule; when nothing usable is left,
|
|
41
|
+
* the plugin gets no line. An EXPLICIT name is validated, never rewritten: a rejected name produces
|
|
42
|
+
* no line at all, never a rejected name in the footer.
|
|
43
|
+
*
|
|
44
|
+
* An explicit name is STICKY for the identity: the first registration that declares one fixes the
|
|
45
|
+
* name for the identity's single row, and later registrations inherit it whether or not they repeat
|
|
46
|
+
* it. A later DIFFERENT name never creates a second row and never renames: the first name stands and
|
|
47
|
+
* the conflict is reported in the plugin report. That is deliberate — a name is attribution, not
|
|
48
|
+
* mutable state, and a rename ships as an edit plus `/reload`, a new registration lifetime.
|
|
49
|
+
*
|
|
50
|
+
* ## The state (I4)
|
|
51
|
+
*
|
|
52
|
+
* `ready` is derived, never promised: one rejected registration — an invalid id, a row label that
|
|
53
|
+
* broke the shape rule — and the same key is repainted as `degraded`. The report names the
|
|
54
|
+
* rejected items.
|
|
55
|
+
*
|
|
56
|
+
* ## The lifecycle (I5, I6)
|
|
57
|
+
*
|
|
58
|
+
* The line is painted when the plugin first registers, or on `session_start` when the registration
|
|
59
|
+
* happened before any `ctx.ui` existed; every later change repaints the SAME key, so the host updates
|
|
60
|
+
* one row in place. `session_shutdown` clears each painted key with `undefined`. A `ctx.ui` without
|
|
61
|
+
* `setStatus` (a fake, a different host) makes this whole feature a reported no-op: nothing here may
|
|
62
|
+
* throw into a plugin's registration path.
|
|
63
|
+
*
|
|
64
|
+
* ## What the row is, and is not
|
|
65
|
+
*
|
|
66
|
+
* The text is OPAQUE to the host: it is rendered as one string, its ANSI sequences are stripped
|
|
67
|
+
* (`lib/shell-bar.ts:151-155`) and it is painted with the host's own status role. So the bridge puts
|
|
68
|
+
* any glyph INSIDE the text (`Name · ready`), never in colour: colour belongs to the host.
|
|
69
|
+
*
|
|
70
|
+
* This module is internal: `src/index.ts` does not re-export it.
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
import { createHash } from "node:crypto";
|
|
74
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
75
|
+
import { basename, dirname, extname, isAbsolute, join, sep } from "node:path";
|
|
76
|
+
import { fileURLToPath } from "node:url";
|
|
77
|
+
|
|
78
|
+
import {
|
|
79
|
+
MAX_ROW_NAME_WORDS,
|
|
80
|
+
getRowNameRejections,
|
|
81
|
+
rowNameProblem,
|
|
82
|
+
type RowNameRejection,
|
|
83
|
+
type RowNameSurface,
|
|
84
|
+
} from "./row-names.ts";
|
|
85
|
+
|
|
86
|
+
/** Prefix every key this module writes carries; the host's own keys can never match it. */
|
|
87
|
+
export const PLUGIN_LINE_PREFIX = "gentle-shell-bridge.plugin.";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Keys gentle-shell paints itself and removes from INTEGRATIONS (`extensions/gentle-shell.ts:357-385`):
|
|
91
|
+
* `gentle-fast` and `gentle-jobs` are filtered out of the list, `gentle:mode` and `gentle:yolo` are
|
|
92
|
+
* shown as the permissions row instead. Reusing one would make a plugin's line disappear or be
|
|
93
|
+
* re-read as a permission mode, so the bridge's prefix is deliberately disjoint from all four.
|
|
94
|
+
*/
|
|
95
|
+
export const HOST_OWN_STATUS_KEYS: readonly string[] = [
|
|
96
|
+
"gentle-fast",
|
|
97
|
+
"gentle-jobs",
|
|
98
|
+
"gentle:mode",
|
|
99
|
+
"gentle:yolo",
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
/** Which surface a plugin registered. Diagnostics only: the footer never shows it. */
|
|
103
|
+
export type PluginSurface =
|
|
104
|
+
| "rows"
|
|
105
|
+
| "section"
|
|
106
|
+
| "card style"
|
|
107
|
+
| "transform"
|
|
108
|
+
| "tool design"
|
|
109
|
+
| "message design";
|
|
110
|
+
|
|
111
|
+
/** One registration the bridge refused, reported by the plugin report (I4). */
|
|
112
|
+
export interface PluginRejection {
|
|
113
|
+
surface: PluginSurface;
|
|
114
|
+
/** The registration's id as the author wrote it, for finding the call. */
|
|
115
|
+
id: string;
|
|
116
|
+
reason: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A later registration that declared a different name for an identity whose name was fixed (I9). */
|
|
120
|
+
export interface PluginNameConflict {
|
|
121
|
+
surface: PluginSurface;
|
|
122
|
+
id: string;
|
|
123
|
+
/** The later declaration; reported, never applied. */
|
|
124
|
+
name: string;
|
|
125
|
+
/** The name the identity already had, kept for the whole registration lifetime. */
|
|
126
|
+
kept: string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One plugin, as the report lists it and as the footer line holds it. */
|
|
130
|
+
export interface PluginLineRecord {
|
|
131
|
+
/** The footer key: stable for the plugin's lifetime, never shown to the user. */
|
|
132
|
+
key: string;
|
|
133
|
+
/** The row text's name, when one could be derived or was given explicitly. */
|
|
134
|
+
name?: string;
|
|
135
|
+
nameSource?: "derived" | "explicit";
|
|
136
|
+
/** The owning package's `name`, when there is one. Diagnostics. */
|
|
137
|
+
packageName?: string;
|
|
138
|
+
/** The module that registered. Diagnostics — never a row name (I2). */
|
|
139
|
+
modulePath: string;
|
|
140
|
+
status: "ready" | "degraded";
|
|
141
|
+
accepted: number;
|
|
142
|
+
rejected: PluginRejection[];
|
|
143
|
+
/** Later registrations that declared a different name; the first name was kept (I9). */
|
|
144
|
+
nameConflicts: PluginNameConflict[];
|
|
145
|
+
/** What the bridge last did with the key: painted, cleared with `undefined`, or never set. */
|
|
146
|
+
line: "set" | "not-set" | "cleared";
|
|
147
|
+
/** Only when `line` is not `set`: why the row is absent. */
|
|
148
|
+
reason?: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface PluginLineReport {
|
|
152
|
+
availability: "installed" | "not-armed" | "unavailable";
|
|
153
|
+
/** Only when the feature could not be installed: the host shape that was missing. */
|
|
154
|
+
reason?: string;
|
|
155
|
+
plugins: PluginLineRecord[];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
type Owner =
|
|
159
|
+
| { kind: "package"; root: string; name: string; modulePath: string }
|
|
160
|
+
| { kind: "pi"; root: string; name: string; modulePath: string }
|
|
161
|
+
| { kind: "module"; modulePath: string }
|
|
162
|
+
// The literal is `bridge` on purpose: the cache this value lands in is a shared `Symbol.for` slot, and an
|
|
163
|
+
// older copy of this API reads and writes the same kind, so renaming it would make a reader throw.
|
|
164
|
+
| { kind: "bridge" };
|
|
165
|
+
|
|
166
|
+
type Attribution =
|
|
167
|
+
| (Extract<Owner, { kind: "package" | "pi" | "module" }> & { identity: PluginIdentity })
|
|
168
|
+
| Extract<Owner, { kind: "bridge" }>;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* What a plugin IS (I8, I10): its owning package root, or the registering module when no package
|
|
172
|
+
* owns it. Resolved once per module and cached with the attribution; the row key hashes THIS, never
|
|
173
|
+
* the display name, so renaming never produces a second key.
|
|
174
|
+
*/
|
|
175
|
+
interface PluginIdentity {
|
|
176
|
+
/** `pkg:<root>` or `mod:<module>`: the record a registration joins. */
|
|
177
|
+
id: string;
|
|
178
|
+
/** The raw seed the key is hashed from: the package root or the module path. */
|
|
179
|
+
seed: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The parts of a `package.json` attribution needs. */
|
|
183
|
+
interface PackageManifest {
|
|
184
|
+
name: string;
|
|
185
|
+
/** `bin`, as declared; a `pi` executable means this package provides Pi (I11). */
|
|
186
|
+
bin?: unknown;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
interface PluginRecordInternal extends PluginLineRecord {
|
|
190
|
+
/** Package root or module path: two registrations of one plugin share it. */
|
|
191
|
+
identity: string;
|
|
192
|
+
/** A name is sticky: the first usable explicit declaration fixes it for the identity (I9). */
|
|
193
|
+
seenRegistration: boolean;
|
|
194
|
+
/** Why no usable name exists; formatted for the report, never used as a name. */
|
|
195
|
+
nameProblem?: string;
|
|
196
|
+
/** Set when the host's `setStatus` threw for this key (I6). */
|
|
197
|
+
paintError?: string;
|
|
198
|
+
/** The text last handed to the host for this key, so an unchanged state is not repainted (I5). */
|
|
199
|
+
paintedText?: string;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface PluginLinesState {
|
|
203
|
+
plugins: PluginRecordInternal[];
|
|
204
|
+
/** module path → owning package (or the fail-closed answer), so it is resolved once. */
|
|
205
|
+
modules: Map<string, Attribution>;
|
|
206
|
+
/** The host's `setStatus`, bound to the object it came from, when it is available. */
|
|
207
|
+
ui?: (key: string, text?: string) => void;
|
|
208
|
+
/** Why `ui` is absent: the feature is skipped and this is what the report says. */
|
|
209
|
+
unavailableReason?: string;
|
|
210
|
+
/** Keys this module painted, so shutdown clears exactly them. */
|
|
211
|
+
painted: Set<string>;
|
|
212
|
+
/** The package that owns the running `pi` entry point, resolved once (`root` undefined = none). */
|
|
213
|
+
entryPackage?: { root?: string };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Stable process-global slot, like the bus: two copies of this package must agree on the one line a
|
|
218
|
+
* plugin gets, and the state has to survive jiti loading each copy separately.
|
|
219
|
+
*/
|
|
220
|
+
const PLUGIN_LINES_KEY = Symbol.for("gentle-shell-bridge.plugin-lines.v1");
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The shell API's own directory: its frames are skipped when the caller is captured, so a
|
|
224
|
+
* registration the shell API makes on a plugin's behalf is never attributed to a frame of plumbing.
|
|
225
|
+
*/
|
|
226
|
+
const BRIDGE_SRC_DIR = dirname(fileURLToPath(import.meta.url));
|
|
227
|
+
|
|
228
|
+
function pluginLinesHost(): Record<symbol, PluginLinesState | undefined> {
|
|
229
|
+
// SAFETY: only this module writes the key, and `pluginLinesState()` re-checks the shapes before
|
|
230
|
+
// trusting a value left by another copy or an earlier load.
|
|
231
|
+
return globalThis as unknown as Record<symbol, PluginLinesState | undefined>;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function pluginLinesState(): PluginLinesState {
|
|
235
|
+
const host = pluginLinesHost();
|
|
236
|
+
const existing = host[PLUGIN_LINES_KEY];
|
|
237
|
+
if (
|
|
238
|
+
existing !== undefined &&
|
|
239
|
+
Array.isArray(existing.plugins) &&
|
|
240
|
+
existing.modules instanceof Map &&
|
|
241
|
+
existing.painted instanceof Set
|
|
242
|
+
) {
|
|
243
|
+
return existing;
|
|
244
|
+
}
|
|
245
|
+
const created: PluginLinesState = { plugins: [], modules: new Map(), painted: new Set() };
|
|
246
|
+
host[PLUGIN_LINES_KEY] = created;
|
|
247
|
+
return created;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** A plain, lower-case fragment usable inside a footer key. */
|
|
251
|
+
function slug(text: string): string {
|
|
252
|
+
const normalized = text
|
|
253
|
+
.toLowerCase()
|
|
254
|
+
.replace(/[^a-z0-9]+/gu, "-")
|
|
255
|
+
.replace(/^-+|-+$/gu, "");
|
|
256
|
+
return normalized === "" ? "plugin" : normalized;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The derived display name (I3): package scope dropped, every separator and mark a word break,
|
|
261
|
+
* first word capitalised, clamped to the row-name word limit. `undefined` means nothing usable was
|
|
262
|
+
* left, which is a no line — never an invented one.
|
|
263
|
+
*/
|
|
264
|
+
export function derivePluginName(packageName: string): string | undefined {
|
|
265
|
+
const unscoped = packageName.replace(/^@[^/]*\//u, "");
|
|
266
|
+
const words = unscoped
|
|
267
|
+
.split(/[^A-Za-z0-9]+/u)
|
|
268
|
+
.filter((word) => word !== "")
|
|
269
|
+
.slice(0, MAX_ROW_NAME_WORDS);
|
|
270
|
+
if (words.length === 0) return undefined;
|
|
271
|
+
const candidate = words
|
|
272
|
+
.map((word, index) =>
|
|
273
|
+
index === 0 ? `${word.slice(0, 1).toUpperCase()}${word.slice(1)}` : word.toLowerCase(),
|
|
274
|
+
)
|
|
275
|
+
.join(" ");
|
|
276
|
+
return rowNameProblem(candidate) === undefined ? candidate : undefined;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The footer key for an identity: the package root (or module path) is hashed in, so two packages
|
|
281
|
+
* with the same derived name still get two rows and one plugin keeps the same key forever — a
|
|
282
|
+
* rename, of the package or of an explicit name, repaints THIS key (I10).
|
|
283
|
+
*/
|
|
284
|
+
function pluginLineKey(identity: PluginIdentity): string {
|
|
285
|
+
const stem = slug(basename(identity.seed, extname(identity.seed)));
|
|
286
|
+
const digest = createHash("sha1").update(identity.id).digest("hex").slice(0, 8);
|
|
287
|
+
return `${PLUGIN_LINE_PREFIX}${stem}.${digest}`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function packageIdentity(root: string): PluginIdentity {
|
|
291
|
+
return { id: `pkg:${root}`, seed: root };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function moduleIdentity(modulePath: string): PluginIdentity {
|
|
295
|
+
return { id: `mod:${modulePath}`, seed: modulePath };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** `package.json`'s name and bin in one directory, or `undefined` when there is none to read. */
|
|
299
|
+
function readPackageManifest(dir: string): PackageManifest | undefined {
|
|
300
|
+
let raw: string;
|
|
301
|
+
try {
|
|
302
|
+
raw = readFileSync(join(dir, "package.json"), "utf8");
|
|
303
|
+
} catch {
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
const parsed: unknown = JSON.parse(raw);
|
|
308
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
309
|
+
const name = (parsed as { name?: unknown }).name;
|
|
310
|
+
if (typeof name !== "string") return undefined;
|
|
311
|
+
const trimmed = name.trim();
|
|
312
|
+
if (trimmed === "") return undefined;
|
|
313
|
+
return { name: trimmed, bin: (parsed as { bin?: unknown }).bin };
|
|
314
|
+
} catch {
|
|
315
|
+
return undefined;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** The nearest directory above `from` (inclusive of its directory) that holds a named manifest. */
|
|
320
|
+
function nearestPackageRoot(from: string): string | undefined {
|
|
321
|
+
let dir = dirname(from);
|
|
322
|
+
for (;;) {
|
|
323
|
+
if (readPackageManifest(dir) !== undefined) return dir;
|
|
324
|
+
const parent = dirname(dir);
|
|
325
|
+
if (parent === dir) return undefined;
|
|
326
|
+
dir = parent;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Whether two paths name the same directory; symlinks are compared by their target. */
|
|
331
|
+
function sameDirectory(a: string, b: string): boolean {
|
|
332
|
+
if (a === b) return true;
|
|
333
|
+
try {
|
|
334
|
+
return realpathSync(a) === realpathSync(b);
|
|
335
|
+
} catch {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Whether a `bin` declaration provides the `pi` executable (I11) — a declaration, not a name guess. */
|
|
341
|
+
function declaresPiBinary(bin: unknown): boolean {
|
|
342
|
+
if (typeof bin === "string") {
|
|
343
|
+
const file = basename(bin);
|
|
344
|
+
const ext = extname(file);
|
|
345
|
+
return (ext === "" ? file : basename(file, ext)) === "pi";
|
|
346
|
+
}
|
|
347
|
+
if (typeof bin !== "object" || bin === null) return false;
|
|
348
|
+
const entries = bin as Record<string, unknown>;
|
|
349
|
+
return Object.prototype.hasOwnProperty.call(entries, "pi") && typeof entries.pi === "string";
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* The package that provides the RUNNING `pi`: the one that owns `process.argv[1]` (the CLI entry),
|
|
354
|
+
* resolved through symlinks and remembered once. A process with no such entry has none.
|
|
355
|
+
*/
|
|
356
|
+
function piEntryPackageRoot(): string | undefined {
|
|
357
|
+
const state = pluginLinesState();
|
|
358
|
+
if (state.entryPackage !== undefined) return state.entryPackage.root;
|
|
359
|
+
let root: string | undefined;
|
|
360
|
+
const entry = process.argv[1];
|
|
361
|
+
if (typeof entry === "string" && entry !== "" && isAbsolute(entry)) {
|
|
362
|
+
try {
|
|
363
|
+
root = nearestPackageRoot(realpathSync(entry));
|
|
364
|
+
} catch {
|
|
365
|
+
root = undefined;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
state.entryPackage = { root };
|
|
369
|
+
return root;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The Pi distribution is the package that owns the running `pi` entry point, or one whose manifest
|
|
374
|
+
* declares the `pi` executable. Recognised from those declarations, never from a package NAME: a
|
|
375
|
+
* plugin may legitimately be called anything.
|
|
376
|
+
*/
|
|
377
|
+
function isPiDistribution(root: string, manifest: PackageManifest): boolean {
|
|
378
|
+
const entryRoot = piEntryPackageRoot();
|
|
379
|
+
if (entryRoot !== undefined && sameDirectory(entryRoot, root)) return true;
|
|
380
|
+
return declaresPiBinary(manifest.bin);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Walk up from the module to its owning package (I8). No manifest means no owner: fail closed. */
|
|
384
|
+
function resolveOwner(modulePath: string): Owner {
|
|
385
|
+
let dir = dirname(modulePath);
|
|
386
|
+
for (;;) {
|
|
387
|
+
const manifest = readPackageManifest(dir);
|
|
388
|
+
if (manifest !== undefined) {
|
|
389
|
+
if (isPiDistribution(dir, manifest)) {
|
|
390
|
+
return { kind: "pi", root: dir, name: manifest.name, modulePath };
|
|
391
|
+
}
|
|
392
|
+
return { kind: "package", root: dir, name: manifest.name, modulePath };
|
|
393
|
+
}
|
|
394
|
+
const parent = dirname(dir);
|
|
395
|
+
if (parent === dir) return { kind: "module", modulePath };
|
|
396
|
+
dir = parent;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Resolve the whole answer ONCE per module: owner, identity and (for a plugin) its record seed. */
|
|
401
|
+
function resolveAttribution(modulePath: string): Attribution {
|
|
402
|
+
const owner = resolveOwner(modulePath);
|
|
403
|
+
if (owner.kind === "bridge") return owner;
|
|
404
|
+
if (owner.kind === "package") return { ...owner, identity: packageIdentity(owner.root) };
|
|
405
|
+
// A Pi-owned module fails closed exactly like an unattributable one (I11), so its plugin is the
|
|
406
|
+
// module itself, never the Pi distribution's package root.
|
|
407
|
+
return { ...owner, identity: moduleIdentity(modulePath) };
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** The absolute file path in one stack frame, or `undefined` for an internal/native frame. */
|
|
411
|
+
function framePath(text: string): string | undefined {
|
|
412
|
+
const open = text.lastIndexOf("(");
|
|
413
|
+
const location = (open === -1 ? text : text.slice(open + 1, text.endsWith(")") ? -1 : undefined))
|
|
414
|
+
.trim()
|
|
415
|
+
.replace(/^(?:async|new|eval) /u, "");
|
|
416
|
+
const match = /^(.+):(\d+):(\d+)$/u.exec(location);
|
|
417
|
+
if (match === null) return undefined;
|
|
418
|
+
let file = match[1];
|
|
419
|
+
if (file.startsWith("node:") || file === "<anonymous>") return undefined;
|
|
420
|
+
if (file.startsWith("file://")) {
|
|
421
|
+
try {
|
|
422
|
+
file = fileURLToPath(file);
|
|
423
|
+
} catch {
|
|
424
|
+
// A URL this Node cannot convert is still a location; the walk above may resolve it.
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (!isAbsolute(file)) return undefined;
|
|
428
|
+
return file;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* The first stack frame outside this package: the plugin module that called the registration. One
|
|
433
|
+
* capture per registration, then the answer is cached per module.
|
|
434
|
+
*/
|
|
435
|
+
function callerModule(): string | undefined {
|
|
436
|
+
const stack = new Error().stack;
|
|
437
|
+
if (stack === undefined) return undefined;
|
|
438
|
+
for (const raw of stack.split("\n")) {
|
|
439
|
+
const frame = raw.trim();
|
|
440
|
+
if (!frame.startsWith("at ")) continue;
|
|
441
|
+
const path = framePath(frame.slice(3));
|
|
442
|
+
if (path === undefined) continue;
|
|
443
|
+
if (path === BRIDGE_SRC_DIR || path.startsWith(`${BRIDGE_SRC_DIR}${sep}`)) continue;
|
|
444
|
+
return path;
|
|
445
|
+
}
|
|
446
|
+
return undefined;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function attributionOfCaller(): Attribution {
|
|
450
|
+
let modulePath: string | undefined;
|
|
451
|
+
try {
|
|
452
|
+
modulePath = callerModule();
|
|
453
|
+
} catch {
|
|
454
|
+
modulePath = undefined;
|
|
455
|
+
}
|
|
456
|
+
// No caller frame means the shell API itself (an internal helper): never a plugin line.
|
|
457
|
+
if (modulePath === undefined) return { kind: "bridge" };
|
|
458
|
+
|
|
459
|
+
const state = pluginLinesState();
|
|
460
|
+
const cached = state.modules.get(modulePath);
|
|
461
|
+
if (cached !== undefined) return cached;
|
|
462
|
+
|
|
463
|
+
let resolved: Attribution;
|
|
464
|
+
try {
|
|
465
|
+
resolved = resolveAttribution(modulePath);
|
|
466
|
+
} catch {
|
|
467
|
+
resolved = { kind: "module", modulePath, identity: moduleIdentity(modulePath) };
|
|
468
|
+
}
|
|
469
|
+
state.modules.set(modulePath, resolved);
|
|
470
|
+
return resolved;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function messageOf(error: unknown): string {
|
|
474
|
+
return error instanceof Error ? error.message : String(error);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** A value as diagnostics text: strings verbatim, everything else in a stable printable form. */
|
|
478
|
+
function describeValue(value: unknown): string {
|
|
479
|
+
if (typeof value === "string") return value;
|
|
480
|
+
try {
|
|
481
|
+
const json = JSON.stringify(value);
|
|
482
|
+
return json === undefined ? String(value) : json;
|
|
483
|
+
} catch {
|
|
484
|
+
return String(value);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** What a registration's optional `name` is: absent, a usable string, or a present non-string. */
|
|
489
|
+
type ExplicitName =
|
|
490
|
+
| { kind: "absent" }
|
|
491
|
+
| { kind: "text"; value: string }
|
|
492
|
+
| { kind: "invalid" };
|
|
493
|
+
|
|
494
|
+
/** The registration's `id` as diagnostics text; a non-string (or unreadable) id is named as such. */
|
|
495
|
+
function registrationId(registration: unknown): string {
|
|
496
|
+
if (typeof registration !== "object" || registration === null) return "undefined";
|
|
497
|
+
try {
|
|
498
|
+
const id = (registration as { id?: unknown }).id;
|
|
499
|
+
if (typeof id === "string") return id;
|
|
500
|
+
return id === undefined ? "undefined" : describeValue(id);
|
|
501
|
+
} catch {
|
|
502
|
+
return "unreadable id";
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Read the optional `name` without trusting the object the plugin handed us. */
|
|
507
|
+
function explicitNameOf(registration: unknown): ExplicitName {
|
|
508
|
+
if (typeof registration !== "object" || registration === null) return { kind: "absent" };
|
|
509
|
+
let candidate: unknown;
|
|
510
|
+
try {
|
|
511
|
+
candidate = (registration as { name?: unknown }).name;
|
|
512
|
+
} catch {
|
|
513
|
+
return { kind: "absent" };
|
|
514
|
+
}
|
|
515
|
+
if (candidate === undefined || candidate === null) return { kind: "absent" };
|
|
516
|
+
return typeof candidate === "string" ? { kind: "text", value: candidate } : { kind: "invalid" };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** The row-name rejection this registration just produced, if any (I4). */
|
|
520
|
+
function rowNameRejectionFor(
|
|
521
|
+
surface: RowNameSurface,
|
|
522
|
+
id: string,
|
|
523
|
+
): RowNameRejection | undefined {
|
|
524
|
+
return getRowNameRejections().find(
|
|
525
|
+
(rejection) => rejection.surface === surface && rejection.id === id,
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function ensureRecord(attribution: Exclude<Attribution, { kind: "bridge" }>): PluginRecordInternal {
|
|
530
|
+
const state = pluginLinesState();
|
|
531
|
+
const identity = attribution.identity;
|
|
532
|
+
const existing = state.plugins.find((record) => record.identity === identity.id);
|
|
533
|
+
if (existing !== undefined) return existing;
|
|
534
|
+
|
|
535
|
+
const record: PluginRecordInternal = {
|
|
536
|
+
identity: identity.id,
|
|
537
|
+
key: pluginLineKey(identity),
|
|
538
|
+
modulePath: attribution.modulePath,
|
|
539
|
+
accepted: 0,
|
|
540
|
+
rejected: [],
|
|
541
|
+
nameConflicts: [],
|
|
542
|
+
status: "ready",
|
|
543
|
+
line: "not-set",
|
|
544
|
+
seenRegistration: false,
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
if (attribution.kind === "package") {
|
|
548
|
+
record.packageName = attribution.name;
|
|
549
|
+
const derived = derivePluginName(attribution.name);
|
|
550
|
+
if (derived === undefined) {
|
|
551
|
+
record.nameProblem =
|
|
552
|
+
`the package name ${JSON.stringify(attribution.name)} has no usable words to derive a ` +
|
|
553
|
+
"row name from";
|
|
554
|
+
} else {
|
|
555
|
+
record.name = derived;
|
|
556
|
+
record.nameSource = "derived";
|
|
557
|
+
}
|
|
558
|
+
} else if (attribution.kind === "pi") {
|
|
559
|
+
// I11: the Pi installation is not a plugin. Its name is not a row name and nothing is derived
|
|
560
|
+
// from it; only an explicit `name` from the module itself can give the row one.
|
|
561
|
+
record.packageName = attribution.name;
|
|
562
|
+
record.nameProblem =
|
|
563
|
+
`the module is attributed to the Pi installation ${JSON.stringify(attribution.name)}, the ` +
|
|
564
|
+
"package that provides `pi`; the Pi distribution is never a plugin identity, so no row is " +
|
|
565
|
+
"derived from it. Pass `name` on a registration to name the plugin explicitly";
|
|
566
|
+
} else {
|
|
567
|
+
// Fail closed (I2): a copied single file has no owning package, so there is no name to derive
|
|
568
|
+
// and nothing is invented. Only an explicit `name` can name it.
|
|
569
|
+
record.nameProblem =
|
|
570
|
+
"no package.json was found above the module, so there is no owning package name; pass " +
|
|
571
|
+
"`name` on a registration to name it";
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
state.plugins.push(record);
|
|
575
|
+
return record;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Honour an explicit `name` for the identity, whichever registration declares it first (I9). The
|
|
580
|
+
* first usable declaration fixes the name for the identity's single row; a repeat inherits it, and
|
|
581
|
+
* a DIFFERENT later name never renames — it is reported as a conflict and the first name stands.
|
|
582
|
+
*/
|
|
583
|
+
function applyExplicitName(
|
|
584
|
+
record: PluginRecordInternal,
|
|
585
|
+
surface: PluginSurface,
|
|
586
|
+
id: string,
|
|
587
|
+
name: ExplicitName,
|
|
588
|
+
): void {
|
|
589
|
+
if (name.kind === "absent") return;
|
|
590
|
+
|
|
591
|
+
if (name.kind === "invalid") {
|
|
592
|
+
// The first registration's invalid name withholds the line (existing behaviour); a later one is
|
|
593
|
+
// reported without discarding a name the identity already has.
|
|
594
|
+
if (record.seenRegistration) {
|
|
595
|
+
record.rejected.push({
|
|
596
|
+
surface,
|
|
597
|
+
id,
|
|
598
|
+
reason: "the explicit name was rejected: it is not a string",
|
|
599
|
+
});
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
rejectExplicitName(record, surface, id, "not a string", "it is not a string");
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (record.nameSource === "explicit") {
|
|
607
|
+
if (record.name === name.value) return;
|
|
608
|
+
recordConflict(record, surface, id, name.value);
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const problem = rowNameProblem(name.value);
|
|
613
|
+
if (problem === undefined) {
|
|
614
|
+
record.name = name.value;
|
|
615
|
+
record.nameSource = "explicit";
|
|
616
|
+
record.nameProblem = undefined;
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
if (!record.seenRegistration) {
|
|
620
|
+
rejectExplicitName(record, surface, id, name.value, problem.reason);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
record.rejected.push({
|
|
624
|
+
surface,
|
|
625
|
+
id,
|
|
626
|
+
reason: `the explicit name ${JSON.stringify(name.value)} was rejected: ${problem.reason}`,
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** Report a name that was declared too late to apply, once per (surface, id, name) (I9). */
|
|
631
|
+
function recordConflict(
|
|
632
|
+
record: PluginRecordInternal,
|
|
633
|
+
surface: PluginSurface,
|
|
634
|
+
id: string,
|
|
635
|
+
name: string,
|
|
636
|
+
): void {
|
|
637
|
+
const kept = record.name ?? "";
|
|
638
|
+
const duplicate = record.nameConflicts.some(
|
|
639
|
+
(conflict) =>
|
|
640
|
+
conflict.surface === surface && conflict.id === id && conflict.name === name,
|
|
641
|
+
);
|
|
642
|
+
if (duplicate) return;
|
|
643
|
+
record.nameConflicts.push({ surface, id, name, kept });
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/** A rejected explicit name is reported, and produces NO line — never a rejected name (I3). */
|
|
647
|
+
function rejectExplicitName(
|
|
648
|
+
record: PluginRecordInternal,
|
|
649
|
+
surface: PluginSurface,
|
|
650
|
+
id: string,
|
|
651
|
+
value: string,
|
|
652
|
+
reason: string,
|
|
653
|
+
): void {
|
|
654
|
+
record.name = undefined;
|
|
655
|
+
record.nameSource = undefined;
|
|
656
|
+
record.nameProblem =
|
|
657
|
+
`the explicit name ${JSON.stringify(value)} breaks the row-name shape (${reason}), so no ` +
|
|
658
|
+
"line is set: a rejected name is never shown";
|
|
659
|
+
record.rejected.push({
|
|
660
|
+
surface,
|
|
661
|
+
id,
|
|
662
|
+
reason: `the explicit name ${JSON.stringify(value)} was rejected: ${reason}`,
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** The row text: the name and the derived state, both INSIDE the opaque string (I7). */
|
|
667
|
+
function lineText(record: PluginRecordInternal): string {
|
|
668
|
+
return `${record.name ?? "plugin"} · ${record.status}`;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Paint one plugin's line, or record why it cannot be painted. The derived status is recomputed
|
|
673
|
+
* here so every repaint uses the same key with the current answer (I4, I5), and every host call is
|
|
674
|
+
* contained: a footer that throws must cost the row, never the plugin's registration (I6).
|
|
675
|
+
*/
|
|
676
|
+
function paint(record: PluginRecordInternal): void {
|
|
677
|
+
const state = pluginLinesState();
|
|
678
|
+
record.status = record.rejected.length === 0 ? "ready" : "degraded";
|
|
679
|
+
record.paintError = undefined;
|
|
680
|
+
if (record.name === undefined) {
|
|
681
|
+
record.line = "not-set";
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
const setStatus = state.ui;
|
|
685
|
+
if (setStatus === undefined) {
|
|
686
|
+
record.line = "not-set";
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
const text = lineText(record);
|
|
690
|
+
// I5: repaint only when the derived status actually changed. The host's `setStatus` asks for a
|
|
691
|
+
// frame, so a no-op registration must not cost one; the row is already right.
|
|
692
|
+
if (record.line === "set" && record.paintedText === text) return;
|
|
693
|
+
try {
|
|
694
|
+
setStatus(record.key, text);
|
|
695
|
+
record.line = "set";
|
|
696
|
+
record.paintedText = text;
|
|
697
|
+
state.painted.add(record.key);
|
|
698
|
+
} catch (error) {
|
|
699
|
+
record.line = "not-set";
|
|
700
|
+
record.paintError = `setStatus threw: ${messageOf(error)}`;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
type SetStatusRead =
|
|
705
|
+
| { kind: "available"; set: (key: string, text?: string) => void }
|
|
706
|
+
| { kind: "unavailable"; reason: string };
|
|
707
|
+
|
|
708
|
+
function readSetStatus(ui: unknown): SetStatusRead {
|
|
709
|
+
if (ui === null || typeof ui !== "object") {
|
|
710
|
+
return { kind: "unavailable", reason: "ctx.ui is missing, so setStatus cannot be called" };
|
|
711
|
+
}
|
|
712
|
+
let member: unknown;
|
|
713
|
+
try {
|
|
714
|
+
member = (ui as { setStatus?: unknown }).setStatus;
|
|
715
|
+
} catch (error) {
|
|
716
|
+
return { kind: "unavailable", reason: `reading ctx.ui.setStatus threw: ${messageOf(error)}` };
|
|
717
|
+
}
|
|
718
|
+
if (typeof member !== "function") {
|
|
719
|
+
return { kind: "unavailable", reason: "ctx.ui has no setStatus" };
|
|
720
|
+
}
|
|
721
|
+
const bound = member as (key: string, text?: string) => void;
|
|
722
|
+
return {
|
|
723
|
+
kind: "available",
|
|
724
|
+
set: (key, text) => {
|
|
725
|
+
bound.call(ui, key, text);
|
|
726
|
+
},
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Take the session's `ctx.ui` and paint every plugin already registered (I5): a plugin that
|
|
732
|
+
* registered before any context existed gets its line now, and a UI without `setStatus` is recorded
|
|
733
|
+
* as the feature's unavailability instead of throwing (I6). Returns whether the host surface exists.
|
|
734
|
+
*/
|
|
735
|
+
export function installPluginStatus(ui: unknown): boolean {
|
|
736
|
+
const state = pluginLinesState();
|
|
737
|
+
const read = readSetStatus(ui);
|
|
738
|
+
if (read.kind === "available") {
|
|
739
|
+
state.ui = read.set;
|
|
740
|
+
state.unavailableReason = undefined;
|
|
741
|
+
} else {
|
|
742
|
+
state.ui = undefined;
|
|
743
|
+
state.unavailableReason = read.reason;
|
|
744
|
+
}
|
|
745
|
+
for (const record of state.plugins) {
|
|
746
|
+
// A rebind hands a new `ctx.ui` (and with it a fresh footer map), so what the old one was told
|
|
747
|
+
// does not carry over: every key is painted again on the new object.
|
|
748
|
+
record.paintedText = undefined;
|
|
749
|
+
paint(record);
|
|
750
|
+
}
|
|
751
|
+
return read.kind === "available";
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/** Clear every key this module painted with `undefined`, and forget the session's UI (I5). */
|
|
755
|
+
export function clearPluginStatus(): void {
|
|
756
|
+
const state = pluginLinesState();
|
|
757
|
+
const setStatus = state.ui;
|
|
758
|
+
if (setStatus !== undefined) {
|
|
759
|
+
for (const key of [...state.painted]) {
|
|
760
|
+
try {
|
|
761
|
+
setStatus(key, undefined);
|
|
762
|
+
} catch {
|
|
763
|
+
// The footer is already gone; there is nothing left to clear and nothing to report.
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
state.painted.clear();
|
|
768
|
+
state.ui = undefined;
|
|
769
|
+
for (const record of state.plugins) {
|
|
770
|
+
if (record.line === "set") record.line = "cleared";
|
|
771
|
+
record.paintedText = undefined;
|
|
772
|
+
record.paintError = undefined;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/** Why this record has no line right now, or `undefined` when it does. */
|
|
777
|
+
function lineReason(
|
|
778
|
+
record: PluginRecordInternal,
|
|
779
|
+
state: PluginLinesState,
|
|
780
|
+
): string | undefined {
|
|
781
|
+
if (record.name === undefined) return record.nameProblem;
|
|
782
|
+
if (record.line === "set") return undefined;
|
|
783
|
+
if (record.paintError !== undefined) return record.paintError;
|
|
784
|
+
if (state.ui === undefined) {
|
|
785
|
+
return state.unavailableReason ?? "no ui yet: the line is painted on session_start";
|
|
786
|
+
}
|
|
787
|
+
return undefined;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/** A fresh copy of everything the plugin report says, so a caller cannot mutate the registry. */
|
|
791
|
+
export function getPluginLineReport(): PluginLineReport {
|
|
792
|
+
const state = pluginLinesState();
|
|
793
|
+
const availability: PluginLineReport["availability"] =
|
|
794
|
+
state.ui !== undefined ? "installed" : state.unavailableReason === undefined ? "not-armed" : "unavailable";
|
|
795
|
+
const reason = state.ui !== undefined ? undefined : state.unavailableReason;
|
|
796
|
+
return {
|
|
797
|
+
availability,
|
|
798
|
+
...(reason === undefined ? {} : { reason }),
|
|
799
|
+
plugins: state.plugins.map((record) => {
|
|
800
|
+
const reasonNow = lineReason(record, state);
|
|
801
|
+
return {
|
|
802
|
+
key: record.key,
|
|
803
|
+
...(record.name === undefined ? {} : { name: record.name }),
|
|
804
|
+
...(record.nameSource === undefined ? {} : { nameSource: record.nameSource }),
|
|
805
|
+
...(record.packageName === undefined ? {} : { packageName: record.packageName }),
|
|
806
|
+
modulePath: record.modulePath,
|
|
807
|
+
status: record.rejected.length === 0 ? "ready" : "degraded",
|
|
808
|
+
accepted: record.accepted,
|
|
809
|
+
rejected: record.rejected.map((rejection) => ({ ...rejection })),
|
|
810
|
+
nameConflicts: record.nameConflicts.map((conflict) => ({ ...conflict })),
|
|
811
|
+
line: record.line,
|
|
812
|
+
...(reasonNow === undefined ? {} : { reason: reasonNow }),
|
|
813
|
+
};
|
|
814
|
+
}),
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* The attribution boundary every registration goes through (`./bus.ts`, I2–I4).
|
|
820
|
+
*
|
|
821
|
+
* `store` is the real registration: this function wraps it so a failure still lands in the plugin's
|
|
822
|
+
* record before it propagates (a plugin's invalid id must keep throwing exactly as before), and a
|
|
823
|
+
* success is counted as accepted — or as rejected when the bus just recorded a row-name rejection for
|
|
824
|
+
* it. The disposer is returned untouched: attribution never changes what a registration does.
|
|
825
|
+
*/
|
|
826
|
+
export function attributeRegistration(
|
|
827
|
+
surface: PluginSurface,
|
|
828
|
+
registration: unknown,
|
|
829
|
+
rowNameSurface: RowNameSurface | undefined,
|
|
830
|
+
store: () => () => void,
|
|
831
|
+
): () => void {
|
|
832
|
+
const attribution = attributionOfCaller();
|
|
833
|
+
// The shell API's own plumbing claims nothing; an unattributable internal helper claims nothing.
|
|
834
|
+
if (attribution.kind === "bridge") return store();
|
|
835
|
+
|
|
836
|
+
const record = ensureRecord(attribution);
|
|
837
|
+
const id = registrationId(registration);
|
|
838
|
+
applyExplicitName(record, surface, id, explicitNameOf(registration));
|
|
839
|
+
|
|
840
|
+
let disposer: () => void;
|
|
841
|
+
try {
|
|
842
|
+
disposer = store();
|
|
843
|
+
} catch (error) {
|
|
844
|
+
record.rejected.push({ surface, id, reason: messageOf(error) });
|
|
845
|
+
record.seenRegistration = true;
|
|
846
|
+
paint(record);
|
|
847
|
+
throw error;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
const rowRejection =
|
|
851
|
+
rowNameSurface === undefined ? undefined : rowNameRejectionFor(rowNameSurface, id);
|
|
852
|
+
if (rowRejection === undefined) {
|
|
853
|
+
record.accepted += 1;
|
|
854
|
+
} else {
|
|
855
|
+
record.rejected.push({
|
|
856
|
+
surface,
|
|
857
|
+
id,
|
|
858
|
+
reason:
|
|
859
|
+
`${rowRejection.field} ${JSON.stringify(rowRejection.value)} rejected: ` +
|
|
860
|
+
rowRejection.reason,
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
record.seenRegistration = true;
|
|
864
|
+
paint(record);
|
|
865
|
+
return disposer;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/** Test-only seam; deliberately not re-exported from `src/index.ts`. */
|
|
869
|
+
export function resetPluginLinesForTests(): void {
|
|
870
|
+
const state = pluginLinesState();
|
|
871
|
+
state.plugins.length = 0;
|
|
872
|
+
state.modules.clear();
|
|
873
|
+
state.painted.clear();
|
|
874
|
+
state.ui = undefined;
|
|
875
|
+
state.unavailableReason = undefined;
|
|
876
|
+
state.entryPackage = undefined;
|
|
877
|
+
}
|