pi-odd-worktree 3.15.0 → 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/lib/user-prompt-card.js +48 -2
- 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 +80 -10
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolution of gentle-shell's config home, mirroring how the host resolves its own files
|
|
3
|
+
* (`lib/gentle-shell-bundled.ts:231-239`): `GENTLE_PI_CONFIG_HOME` when set and non-empty,
|
|
4
|
+
* else `<home>/.pi/gentle-ai`.
|
|
5
|
+
*
|
|
6
|
+
* The core resolver is pure: it takes the environment, the home directory and the working
|
|
7
|
+
* directory as parameters, so it is testable without touching `process.env`. `getConfigHome`
|
|
8
|
+
* is the thin wrapper that reads the live process.
|
|
9
|
+
*
|
|
10
|
+
* Path rules, settled here because the host leaves them to `path` semantics:
|
|
11
|
+
* - Only the empty string counts as "unset". Values are not trimmed; a whitespace-only value
|
|
12
|
+
* is a non-empty path and is used as given.
|
|
13
|
+
* - `~` and a leading `~/` are expanded against the supplied home directory. Any other
|
|
14
|
+
* leading `~` (another user's home) is left literal, since resolving another user's home
|
|
15
|
+
* is not something the bridge can do without inventing a lookup.
|
|
16
|
+
* - After expansion a relative value is resolved against the supplied working directory, so
|
|
17
|
+
* the result is always absolute and normalized (no `.`/`..` segments, no doubled or
|
|
18
|
+
* trailing separators).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { homedir } from "node:os";
|
|
22
|
+
import { join, resolve as resolvePath } from "node:path";
|
|
23
|
+
|
|
24
|
+
/** Name of the directory the bridge owns inside the gentle-shell config home. */
|
|
25
|
+
export const BRIDGE_DIR_NAME = "gentle-shell-bridge";
|
|
26
|
+
|
|
27
|
+
/** Environment shape the resolver needs; `process.env` satisfies it structurally. */
|
|
28
|
+
export type ConfigEnv = Readonly<Record<string, string | undefined>>;
|
|
29
|
+
|
|
30
|
+
export interface ResolveConfigHomeInput {
|
|
31
|
+
/** Environment to read `GENTLE_PI_CONFIG_HOME` from. */
|
|
32
|
+
env: ConfigEnv;
|
|
33
|
+
/** Home directory used for `~` expansion and for the default value. */
|
|
34
|
+
homeDir: string;
|
|
35
|
+
/** Working directory used to make a relative override absolute. Defaults to `process.cwd()`. */
|
|
36
|
+
cwd?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `GENTLE_PI_CONFIG_HOME` when set and non-empty, else `<homeDir>/.pi/gentle-ai`.
|
|
41
|
+
* Always absolute and normalized.
|
|
42
|
+
*/
|
|
43
|
+
export function resolveConfigHome(input: ResolveConfigHomeInput): string {
|
|
44
|
+
const configured = input.env.GENTLE_PI_CONFIG_HOME;
|
|
45
|
+
const cwd = input.cwd ?? process.cwd();
|
|
46
|
+
if (configured === undefined || configured === "") {
|
|
47
|
+
return resolvePath(input.homeDir, ".pi", "gentle-ai");
|
|
48
|
+
}
|
|
49
|
+
return resolvePath(cwd, expandTilde(configured, input.homeDir));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The bridge's own directory inside a resolved config home. */
|
|
53
|
+
export function resolveBridgeConfigHome(configHome: string): string {
|
|
54
|
+
return join(configHome, BRIDGE_DIR_NAME);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Reads the live process environment and home directory. */
|
|
58
|
+
export function getConfigHome(): string {
|
|
59
|
+
return resolveConfigHome({ env: process.env, homeDir: homedir() });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `<config home>/gentle-shell-bridge` for the live process. */
|
|
63
|
+
export function getBridgeConfigHome(): string {
|
|
64
|
+
return resolveBridgeConfigHome(getConfigHome());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function expandTilde(value: string, homeDir: string): string {
|
|
68
|
+
if (value === "~") return homeDir;
|
|
69
|
+
if (value.startsWith("~/")) return join(homeDir, value.slice(2));
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shell API's runtime: the shell surfaces odd-worktree installs in this process.
|
|
3
|
+
*
|
|
4
|
+
* odd-worktree's own extension entry (`extensions/odd-worktree-tools.ts`) imports this module and
|
|
5
|
+
* calls its factory first, so everything described here is odd-worktree's own code, not a second
|
|
6
|
+
* plugin loaded beside it. Three responsibilities, no more:
|
|
7
|
+
* 1. publish the runtime handshake and announce it. Importing `./api.ts` publishes the frozen API
|
|
8
|
+
* object at `Symbol.for("gentle-shell-bridge/api/v1")` at module load, before any
|
|
9
|
+
* `session_start`, so a plugin that registers from its own `session_start` handler finds the
|
|
10
|
+
* object or learns the API is absent. When Pi loads the plugin BEFORE this package — the
|
|
11
|
+
* real TUI order — an absent symbol is only "not yet", so the factory also announces the
|
|
12
|
+
* publication on `pi.events` (`gentle-shell-bridge:ready`), once per process and never for an
|
|
13
|
+
* incompatible slot (`announceBridgeReady`). A plugin therefore needs no path, no environment
|
|
14
|
+
* variable and no import.
|
|
15
|
+
* 2. arm the row surfaces on `session_start`, only when the context has a UI AND the required host
|
|
16
|
+
* package `gentle-pi` is present at RUNTIME (`pi.getCommands()` names `gentle:customize`, see
|
|
17
|
+
* `./presence.ts`). Arming is already idempotent (the capture target and the hook registry carry
|
|
18
|
+
* non-enumerable markers), so this module adds no second guard layer: a double load, a `/reload`
|
|
19
|
+
* or a rebind all resolve to "already armed". The same event flushes the notices buffered before
|
|
20
|
+
* any context existed through `ctx.ui.notify` — the in-TUI channel — exactly once per message.
|
|
21
|
+
* Without `gentle-pi` nothing is armed, one warning names the package and later events stay
|
|
22
|
+
* quiet while the absence lasts (S9, T12): the check runs before any arming.
|
|
23
|
+
* 3. install the sidebar surface: the `Sections` rows provider (needs no `tui`) and a
|
|
24
|
+
* `ui.setWidget` factory whose only job is to be handed the real `tui` (see `armSidebar`).
|
|
25
|
+
*
|
|
26
|
+
* The API surface used here was confirmed against `docs/extensions.md`: `pi.on("session_start",
|
|
27
|
+
* async (_event, ctx) => …)` and `ctx.ui.setWidget(key, (tui, theme) => Component)` — the only
|
|
28
|
+
* documented way an extension receives a `tui`.
|
|
29
|
+
*
|
|
30
|
+
* At import time the only global write is the API publication above: no arming and no
|
|
31
|
+
* registration.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { armBridge } from "./arm.ts";
|
|
35
|
+
import { announceBridgeReady, publishBridgeApi, type BridgeEventBus } from "./api.ts";
|
|
36
|
+
import { flushNotices } from "./notices.ts";
|
|
37
|
+
import { checkGentlePi, warnMissingGentlePi } from "./presence.ts";
|
|
38
|
+
import { isRecord } from "./rows.ts";
|
|
39
|
+
import { installCardStyleRows } from "./card-style-rows.ts";
|
|
40
|
+
import { installCardStyleTransform, installRailCardStyleTransform } from "./card-style-wiring.ts";
|
|
41
|
+
import { installSectionRows } from "./sections-rows.ts";
|
|
42
|
+
import { setSectionTheme } from "./section-panel.ts";
|
|
43
|
+
import { installSidebarBackend, noteSidebarUnavailable, type SidebarTuiLike } from "./sidebar.ts";
|
|
44
|
+
import { setRepaintHost } from "./repaint.ts";
|
|
45
|
+
import { clearPluginStatus, installPluginStatus } from "./plugin-lines.ts";
|
|
46
|
+
import { installSectionRail } from "./sidebar-render.ts";
|
|
47
|
+
import { installMessageDesignRows } from "./message-design-rows.ts";
|
|
48
|
+
import { installToolDesignRows } from "./tool-design-rows.ts";
|
|
49
|
+
import { installToolDesignResolver, type ToolDesignResolver } from "./tool-design-wiring.ts";
|
|
50
|
+
|
|
51
|
+
/** The part of the session context this extension reads. */
|
|
52
|
+
export interface BridgeSessionContext {
|
|
53
|
+
hasUI?: boolean;
|
|
54
|
+
ui?: unknown;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type BridgeSessionHandler = (event: unknown, ctx: BridgeSessionContext) => unknown;
|
|
58
|
+
|
|
59
|
+
/** Key of the widget whose only purpose is to receive the host's `tui`. */
|
|
60
|
+
export const SIDEBAR_WIDGET_KEY = "gentle-shell-bridge.sidebar";
|
|
61
|
+
|
|
62
|
+
/** What one sidebar wiring attempt achieved. */
|
|
63
|
+
export type SidebarArmOutcome =
|
|
64
|
+
| "widget-installed"
|
|
65
|
+
| "awaiting-factory"
|
|
66
|
+
| "no-set-widget"
|
|
67
|
+
| "set-widget-threw";
|
|
68
|
+
|
|
69
|
+
export interface SidebarArmResult {
|
|
70
|
+
outcome: SidebarArmOutcome;
|
|
71
|
+
/** Present when nothing could be installed. */
|
|
72
|
+
reason?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The `ctx.ui` members this module reads. */
|
|
76
|
+
interface BridgeUiSurface {
|
|
77
|
+
setWidget?: (key: string, factory: (tui: unknown, theme: unknown) => unknown) => void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Reach the sidebar rail through Pi's one documented hand-out of a `tui`.
|
|
82
|
+
*
|
|
83
|
+
* Pi has no `tui` getter (`ExtensionUIContext` in `dist/core/extensions/types.d.ts` exposes `TUI` only
|
|
84
|
+
* as a factory parameter), and gentle-shell itself obtains one the same way
|
|
85
|
+
* (`extensions/gentle-agents.ts:2165-2175`). So: register a widget factory, keep the `tui` it
|
|
86
|
+
* receives, and return a component that renders NO rows.
|
|
87
|
+
*
|
|
88
|
+
* UNCONFIRMED: whether a widget whose component renders zero rows leaves the editor area visually
|
|
89
|
+
* untouched has not been observed in a real Pi TUI. It is the reason this component paints nothing
|
|
90
|
+
* at all rather than a small marker.
|
|
91
|
+
*
|
|
92
|
+
* Failure paths are recorded, never thrown: a missing `setWidget` or a factory the host never runs
|
|
93
|
+
* records why the rail could not be reached, and the row side keeps working.
|
|
94
|
+
*/
|
|
95
|
+
export function armSidebar(ui: unknown): SidebarArmResult {
|
|
96
|
+
let captured: SidebarTuiLike | undefined;
|
|
97
|
+
try {
|
|
98
|
+
// Reading `setWidget` can itself throw (a getter on the host's UI object), so the whole
|
|
99
|
+
// acquisition happens inside this guard: a broken accessor must never escape the session
|
|
100
|
+
// handler and take the row side with it.
|
|
101
|
+
const surface = isRecord(ui) ? (ui as BridgeUiSurface) : undefined;
|
|
102
|
+
const setWidget = surface?.setWidget;
|
|
103
|
+
if (surface === undefined || typeof setWidget !== "function") {
|
|
104
|
+
noteSidebarUnavailable("ctx.ui has no setWidget, so a tui cannot be acquired");
|
|
105
|
+
return { outcome: "no-set-widget" };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// `this` matters: Pi's setWidget is a method on the UI context.
|
|
109
|
+
//
|
|
110
|
+
// The factory's SECOND argument is Pi's theme (`dist/core/extensions/types.d.ts:113`: `(tui:
|
|
111
|
+
// theme) => Component`), the same documented hand-out gentle-shell's own parts take their theme
|
|
112
|
+
// from. The bridge keeps it so a section header and its rows paint in the host's roles instead
|
|
113
|
+
// of the terminal's defaults (N11); without it the drawing is uncoloured and the recorded
|
|
114
|
+
// outcome says so.
|
|
115
|
+
setWidget.call(surface, SIDEBAR_WIDGET_KEY, (tui: unknown, theme?: unknown) => {
|
|
116
|
+
captured = isRecord(tui) ? (tui as SidebarTuiLike) : undefined;
|
|
117
|
+
setSectionTheme(theme);
|
|
118
|
+
// The renderer and the interception are installed from inside the factory: that is the first
|
|
119
|
+
// moment a `tui` exists, and the resolver keeps working for a later re-install.
|
|
120
|
+
installSectionRail();
|
|
121
|
+
installSidebarBackend(() => captured);
|
|
122
|
+
// The same hand-out is the only way to the conversation component (S4) and to the rail
|
|
123
|
+
// component (R1), so both card-style wrappers arm here too — never at module load, never twice
|
|
124
|
+
// (their marker decides), and every absence is a reported outcome instead of a throw inside
|
|
125
|
+
// the host's factory. The captured `tui` is also what a bridge-owned write asks for a frame
|
|
126
|
+
// through (R3), so it is remembered first: a later rail attach (the fullscreen layout may not
|
|
127
|
+
// be mounted yet) reads it from there.
|
|
128
|
+
setRepaintHost(captured);
|
|
129
|
+
installCardStyleTransform(captured);
|
|
130
|
+
installRailCardStyleTransform(captured);
|
|
131
|
+
return { render: () => [], invalidate: () => {} };
|
|
132
|
+
});
|
|
133
|
+
} catch (error) {
|
|
134
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
135
|
+
noteSidebarUnavailable(`setWidget threw: ${reason}`);
|
|
136
|
+
return { outcome: "set-widget-threw", reason };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (captured === undefined) {
|
|
140
|
+
// The host accepted the factory but has not run it yet: there is nothing to intercept until it
|
|
141
|
+
// does, and the reason is recorded instead of guessed.
|
|
142
|
+
noteSidebarUnavailable("the setWidget factory has not run yet, so no tui is available");
|
|
143
|
+
return { outcome: "awaiting-factory" };
|
|
144
|
+
}
|
|
145
|
+
return { outcome: "widget-installed" };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** One entry of Pi's command list, as this module reads it: only the name is ever inspected. */
|
|
149
|
+
export interface BridgeCommandEntry {
|
|
150
|
+
readonly name?: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Pi's command list (`pi.getCommands()`). The host is a TRUST BOUNDARY: `./presence.ts` re-validates
|
|
155
|
+
* every shape (a non-array answer, an entry without a name, a throwing accessor), so nothing here
|
|
156
|
+
* relies on the declared type being honoured.
|
|
157
|
+
*/
|
|
158
|
+
export type BridgeCommandList = readonly BridgeCommandEntry[];
|
|
159
|
+
|
|
160
|
+
/** The part of Pi's `ExtensionAPI` this extension uses. */
|
|
161
|
+
export interface BridgeExtensionApi {
|
|
162
|
+
on(event: "session_start" | "session_shutdown", handler: BridgeSessionHandler): void;
|
|
163
|
+
/**
|
|
164
|
+
* Pi's command list (`docs/extensions.md`): the runtime evidence of which extensions are loaded.
|
|
165
|
+
* The bridge reads it for the REQUIRED host package (S9): an entry named `gentle:customize` means
|
|
166
|
+
* `gentle-pi` is there. Optional, because a bare harness may expose no command surface at all —
|
|
167
|
+
* which is MISSING, not an error.
|
|
168
|
+
*/
|
|
169
|
+
getCommands?(): BridgeCommandList;
|
|
170
|
+
/** Pi's cross-extension bus (`docs/extensions.md:1965-1972`); absent only in a bare harness. */
|
|
171
|
+
events?: BridgeEventBus;
|
|
172
|
+
/**
|
|
173
|
+
* Pi's tool-renderer hook (`docs/extensions.md:2123`;
|
|
174
|
+
* `dist/core/extensions/types.d.ts:546`): the sanctioned way to choose how a tool's card is drawn.
|
|
175
|
+
* Optional, so a bare harness stays a valid host — a missing hook is a reported outcome, never a
|
|
176
|
+
* throw inside `session_start`.
|
|
177
|
+
*/
|
|
178
|
+
registerToolRenderer?(resolver: ToolDesignResolver): void;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export default function shellApiExtension(pi: BridgeExtensionApi): void {
|
|
182
|
+
// The module load above already published the object; this factory is the first moment `pi` —
|
|
183
|
+
// and therefore `pi.events` — exists, so it is where the process is told. Both calls are
|
|
184
|
+
// idempotent: publishing again hands back the slot's object, and the announcement happens once
|
|
185
|
+
// per process even when two copies of this package are loaded.
|
|
186
|
+
publishBridgeApi();
|
|
187
|
+
announceBridgeReady(pi.events);
|
|
188
|
+
|
|
189
|
+
pi.on("session_start", (_event, ctx) => {
|
|
190
|
+
// Without a UI there is no customize menu to reach, so there is nothing to arm. There is also
|
|
191
|
+
// no `notify` to show a buffered notice with: those stay pending for the next UI session.
|
|
192
|
+
if (ctx.hasUI !== true) return;
|
|
193
|
+
|
|
194
|
+
// S9/T12 — the REQUIRED host package, checked at RUNTIME and BEFORE anything is armed. Every
|
|
195
|
+
// extension is loaded before `session_start`, so this answer is real; the check runs again on
|
|
196
|
+
// every later event, which is how a host that appears late is picked up (and how a host that
|
|
197
|
+
// disappears again is reported). `checkGentlePi` records the answer and decides the warning
|
|
198
|
+
// budget; a missing host warns once per absence and then stays silent.
|
|
199
|
+
const presence = checkGentlePi(pi);
|
|
200
|
+
// The in-TUI diagnostics channel. A notice produced at module load (an incompatible API slot)
|
|
201
|
+
// has been waiting for this moment; delivered notices stay in the buffer afterwards.
|
|
202
|
+
flushNotices(ctx.ui);
|
|
203
|
+
if (!presence.present) {
|
|
204
|
+
// Nothing below this line runs: no capture, no rows, no resolver, no widget, no footer line
|
|
205
|
+
// (S9). The one warning names the missing package; the reason rides with it.
|
|
206
|
+
if (presence.warnNow) {
|
|
207
|
+
warnMissingGentlePi(ctx.ui, presence.reason ?? "the host does not advertise gentle-pi");
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
armBridge(ctx.ui);
|
|
212
|
+
// The `Sections` provider needs no `tui`, so it is installed even when the rail cannot be
|
|
213
|
+
// reached: the user can still see and toggle a section. Re-installing is idempotent — the bus
|
|
214
|
+
// replaces a provider with the same id in place.
|
|
215
|
+
installSectionRows();
|
|
216
|
+
// The card-style fallback line needs no `tui`: it is the bridge's own `Bridge card style` line,
|
|
217
|
+
// registered only while the host's own line cannot be extended (the capture decides that, and
|
|
218
|
+
// disposes this provider the moment it extends the host's line instead).
|
|
219
|
+
installCardStyleRows();
|
|
220
|
+
// The per-tool DESIGN bus (S4, S5, S7), at this same arming point:
|
|
221
|
+
//
|
|
222
|
+
// - the customize rows need no `tui`, so they are installed here like every other row surface;
|
|
223
|
+
// - the ONE `pi.registerToolRenderer` resolver is installed here too, and nowhere else. Pi
|
|
224
|
+
// resolves renderers when it CONSTRUCTS a card, so this is early enough for every card of the
|
|
225
|
+
// session; returning `next()` composes with gentle-shell instead of replacing it. Both calls
|
|
226
|
+
// are idempotent through process-global markers, so a second load or a rebind registers
|
|
227
|
+
// neither twice.
|
|
228
|
+
installToolDesignRows();
|
|
229
|
+
installToolDesignResolver(pi);
|
|
230
|
+
// The MESSAGE-design lines, at this same arming point and for the same reason: the rows need
|
|
231
|
+
// no `tui`, and a design registered later shows up on the next menu build. Installing twice is
|
|
232
|
+
// a no-op through a process-global marker.
|
|
233
|
+
installMessageDesignRows();
|
|
234
|
+
armSidebar(ctx.ui);
|
|
235
|
+
// The ONE INTEGRATIONS line each plugin gets (I1–I6). A plugin that registered before any `ui`
|
|
236
|
+
// existed is painted here; a plugin that registers later repaints its own key in place. A host
|
|
237
|
+
// whose `ctx.ui` has no `setStatus` is recorded as unavailable instead of throwing (I6).
|
|
238
|
+
installPluginStatus(ctx.ui);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// The footer row is session state, like everything else these surfaces paint: the host is told
|
|
242
|
+
// to drop every key this module set (`undefined`) before the next bind, and the records survive
|
|
243
|
+
// so the next `session_start` paints the same keys again (I5).
|
|
244
|
+
pi.on("session_shutdown", () => {
|
|
245
|
+
clearPluginStatus();
|
|
246
|
+
});
|
|
247
|
+
}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook backend (S4): prefer a real host registry over patching anything.
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ UNVERIFIED BRANCH. No host has been observed exposing
|
|
5
|
+
* `globalThis[Symbol.for("gentle-pi.customize-row-providers")]`; the key arrives from the
|
|
6
|
+
* verified probe notes, but its value shape does not. The bridge therefore accepts exactly two
|
|
7
|
+
* shapes and reports anything else:
|
|
8
|
+
*
|
|
9
|
+
* - a plain `Array` — the fan-out provider is appended with `push`;
|
|
10
|
+
* - an object with a callable `register` method — the provider is handed to `register(provider)`.
|
|
11
|
+
*
|
|
12
|
+
* Anything else (primitive, function, `null`, object without a callable `register`, or a
|
|
13
|
+
* `register` getter that throws) is `unusable-hook`: the caller falls back to the capture backend
|
|
14
|
+
* and diagnostics records why. It is never treated as "hooked", so a wrong guess degrades to the
|
|
15
|
+
* proven path instead of silently doing nothing.
|
|
16
|
+
*
|
|
17
|
+
* ## Honest installation
|
|
18
|
+
*
|
|
19
|
+
* Success is claimed only when the provider was actually accepted: an array `push` must have added
|
|
20
|
+
* it (a no-op `push` is a failure), a `register` that returns `false` is a rejection, and a throw
|
|
21
|
+
* from `push`, from `register` or from the `register` getter is an `install-failed` with the
|
|
22
|
+
* message recorded. Every failure falls back to capture. `acceptedProviders` counts what was
|
|
23
|
+
* really accepted, so diagnostics can tell "installed" from "asked".
|
|
24
|
+
*
|
|
25
|
+
* ## Dedupe does not mutate the host registry
|
|
26
|
+
*
|
|
27
|
+
* The registry is keyed by identity in a `WeakSet` held in the process-global state, so a frozen
|
|
28
|
+
* registry still receives exactly one fan-out provider across arms and package copies. A
|
|
29
|
+
* non-enumerable marker is written as a best-effort second signal, never as the authority.
|
|
30
|
+
*
|
|
31
|
+
* When the host removes the slot, the recorded state stops counting as installed: diagnostics
|
|
32
|
+
* follow the live registry rather than a stale memory of it.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import type { CaptureOutcome } from "./capture.ts";
|
|
36
|
+
import type { CustomizeRow } from "./types.ts";
|
|
37
|
+
import { recordObservedHostCategories } from "./bus.ts";
|
|
38
|
+
import {
|
|
39
|
+
buildRows,
|
|
40
|
+
contextFromHost,
|
|
41
|
+
isRecord,
|
|
42
|
+
planRowPlacement,
|
|
43
|
+
type RowBuildFailure,
|
|
44
|
+
} from "./rows.ts";
|
|
45
|
+
|
|
46
|
+
/** The host registry key from S4. Read only; the bridge never creates it. */
|
|
47
|
+
const HOOK_REGISTRY = Symbol.for("gentle-pi.customize-row-providers");
|
|
48
|
+
/** Best-effort non-enumerable marker on the registry: this package pushed its fan-out provider. */
|
|
49
|
+
const HOOK_ARMED = Symbol.for("gentle-shell-bridge.hook.armed.v1");
|
|
50
|
+
/** Process-global state for diagnostics, shared across package copies. */
|
|
51
|
+
const HOOK_STATE = Symbol.for("gentle-shell-bridge.hook.state.v1");
|
|
52
|
+
|
|
53
|
+
/** Stable id of the single provider the bridge pushes into a host registry. */
|
|
54
|
+
export const FAN_OUT_PROVIDER_ID = "gentle-shell-bridge";
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The provider pushed into the host registry. Its shape is a guess constrained by the unproven
|
|
58
|
+
* registry: the fan-out already knows every row's category, so it carries no category of its own.
|
|
59
|
+
*/
|
|
60
|
+
export interface HookFanOutProvider {
|
|
61
|
+
readonly id: string;
|
|
62
|
+
rows(ctx?: unknown): CustomizeRow[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type HookShape = "array" | "register" | "unusable";
|
|
66
|
+
|
|
67
|
+
export type HookOutcome = "hooked" | "no-hook" | "unusable-hook" | "install-failed";
|
|
68
|
+
|
|
69
|
+
export interface InstallHookResult {
|
|
70
|
+
/** True when the registry now holds — or already held — the bridge's fan-out provider. */
|
|
71
|
+
installed: boolean;
|
|
72
|
+
outcome: HookOutcome;
|
|
73
|
+
shape?: HookShape;
|
|
74
|
+
/** Why a present registry was not usable, or why installation failed. */
|
|
75
|
+
reason?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface HookDiagnostics {
|
|
79
|
+
/** True only while the registry this process installed into is still the live registry. */
|
|
80
|
+
installed: boolean;
|
|
81
|
+
shape?: HookShape;
|
|
82
|
+
lastOutcome?: HookOutcome;
|
|
83
|
+
/** Why the last attempt did not succeed. */
|
|
84
|
+
reason?: string;
|
|
85
|
+
/** How many fan-out providers this process has actually got accepted. */
|
|
86
|
+
acceptedProviders: number;
|
|
87
|
+
/** Outcome of the most recent fan-out build, in the same shape the capture backend records. */
|
|
88
|
+
lastCapture?: CaptureOutcome;
|
|
89
|
+
/** Failures recorded during the most recent fan-out build. */
|
|
90
|
+
lastFailures: RowBuildFailure[];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface HookState {
|
|
94
|
+
installed: boolean;
|
|
95
|
+
/** Identity of the registry this process installed into. */
|
|
96
|
+
installedRegistry?: object;
|
|
97
|
+
/** Identity dedupe for registries that cannot be marked (frozen objects included). */
|
|
98
|
+
installedRegistries: WeakSet<object>;
|
|
99
|
+
shape?: HookShape;
|
|
100
|
+
lastOutcome?: HookOutcome;
|
|
101
|
+
reason?: string;
|
|
102
|
+
acceptedProviders: number;
|
|
103
|
+
lastCapture?: CaptureOutcome;
|
|
104
|
+
lastFailures: RowBuildFailure[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function hookHost(): Record<symbol, unknown> {
|
|
108
|
+
// SAFETY: `globalThis` accepts symbol keys, and every value stored under HOOK_STATE is written
|
|
109
|
+
// by `hookState()` below; a foreign value is rejected there before use.
|
|
110
|
+
return globalThis as unknown as Record<symbol, unknown>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function hookState(): HookState {
|
|
114
|
+
const host = hookHost();
|
|
115
|
+
const existing = host[HOOK_STATE];
|
|
116
|
+
if (isHookState(existing)) return existing;
|
|
117
|
+
const created: HookState = {
|
|
118
|
+
installed: false,
|
|
119
|
+
installedRegistries: new WeakSet<object>(),
|
|
120
|
+
acceptedProviders: 0,
|
|
121
|
+
lastFailures: [],
|
|
122
|
+
};
|
|
123
|
+
host[HOOK_STATE] = created;
|
|
124
|
+
return created;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function isHookState(value: unknown): value is HookState {
|
|
128
|
+
return (
|
|
129
|
+
isRecord(value) &&
|
|
130
|
+
typeof value.installed === "boolean" &&
|
|
131
|
+
value.installedRegistries instanceof WeakSet &&
|
|
132
|
+
Array.isArray(value.lastFailures)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Read the host slot without letting a hostile global getter escape. */
|
|
137
|
+
function readRegistry(): { registry: unknown; error?: string } {
|
|
138
|
+
try {
|
|
139
|
+
return { registry: hookHost()[HOOK_REGISTRY] };
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return { registry: undefined, error: errorMessage(error) };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Diagnostics snapshot for `status` (S6) and for this backend's tests. */
|
|
146
|
+
export function getHookDiagnostics(): HookDiagnostics {
|
|
147
|
+
const state = hookState();
|
|
148
|
+
const { registry, error } = readRegistry();
|
|
149
|
+
const installed =
|
|
150
|
+
state.installed && state.installedRegistry !== undefined && registry === state.installedRegistry;
|
|
151
|
+
const diagnostics: HookDiagnostics = {
|
|
152
|
+
installed,
|
|
153
|
+
shape: state.shape,
|
|
154
|
+
lastOutcome: error === undefined ? state.lastOutcome : "unusable-hook",
|
|
155
|
+
reason: error === undefined ? state.reason : error,
|
|
156
|
+
acceptedProviders: state.acceptedProviders,
|
|
157
|
+
lastFailures: [...state.lastFailures],
|
|
158
|
+
};
|
|
159
|
+
if (state.lastCapture !== undefined) diagnostics.lastCapture = state.lastCapture;
|
|
160
|
+
return diagnostics;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Test-only seam; deliberately not re-exported from `src/index.ts`. The registry marker stays. */
|
|
164
|
+
export function resetHookStateForTests(): void {
|
|
165
|
+
const state = hookState();
|
|
166
|
+
state.installed = false;
|
|
167
|
+
state.installedRegistry = undefined;
|
|
168
|
+
state.installedRegistries = new WeakSet<object>();
|
|
169
|
+
state.shape = undefined;
|
|
170
|
+
state.lastOutcome = undefined;
|
|
171
|
+
state.reason = undefined;
|
|
172
|
+
state.acceptedProviders = 0;
|
|
173
|
+
state.lastCapture = undefined;
|
|
174
|
+
state.lastFailures = [];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Install the fan-out provider into the host registry, if the host exposes a usable one.
|
|
179
|
+
* Idempotent per registry identity: a second arm — or a second copy of this package — never pushes
|
|
180
|
+
* a second provider, even when the registry cannot be marked because it is frozen.
|
|
181
|
+
*/
|
|
182
|
+
export function installHookBackend(): InstallHookResult {
|
|
183
|
+
const state = hookState();
|
|
184
|
+
const { registry, error } = readRegistry();
|
|
185
|
+
|
|
186
|
+
if (error !== undefined) {
|
|
187
|
+
forgetInstalled(state);
|
|
188
|
+
state.lastOutcome = "unusable-hook";
|
|
189
|
+
state.reason = `reading the registry slot threw: ${error}`;
|
|
190
|
+
return { installed: false, outcome: "unusable-hook", reason: state.reason };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (registry === undefined) {
|
|
194
|
+
// The host removed the slot (or never had one): stop reporting the previous hook as live.
|
|
195
|
+
forgetInstalled(state);
|
|
196
|
+
state.shape = undefined;
|
|
197
|
+
state.lastOutcome = "no-hook";
|
|
198
|
+
state.reason = undefined;
|
|
199
|
+
return { installed: false, outcome: "no-hook" };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const shape = classifyRegistry(registry);
|
|
203
|
+
state.shape = shape;
|
|
204
|
+
|
|
205
|
+
if (shape === "unusable") {
|
|
206
|
+
forgetInstalled(state);
|
|
207
|
+
state.lastOutcome = "unusable-hook";
|
|
208
|
+
state.reason = describeUnusable(registry);
|
|
209
|
+
return { installed: false, outcome: "unusable-hook", shape, reason: state.reason };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const target = registry as object;
|
|
213
|
+
if (state.installedRegistries.has(target)) {
|
|
214
|
+
state.installed = true;
|
|
215
|
+
state.installedRegistry = target;
|
|
216
|
+
state.lastOutcome = "hooked";
|
|
217
|
+
state.reason = undefined;
|
|
218
|
+
return { installed: true, outcome: "hooked", shape };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const provider = createFanOutProvider(state);
|
|
222
|
+
let accepted = false;
|
|
223
|
+
let reason: string | undefined;
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
if (shape === "array") {
|
|
227
|
+
const array = registry as unknown[];
|
|
228
|
+
(array as { push: (provider: unknown) => unknown }).push(provider);
|
|
229
|
+
// A no-op or lying `push` must not be mistaken for acceptance.
|
|
230
|
+
accepted = Array.prototype.includes.call(registry, provider);
|
|
231
|
+
if (!accepted) reason = "the registry's push did not add the provider";
|
|
232
|
+
} else {
|
|
233
|
+
const registered = (
|
|
234
|
+
registry as { register: (provider: unknown) => unknown }
|
|
235
|
+
).register.call(registry, provider);
|
|
236
|
+
accepted = registered !== false;
|
|
237
|
+
if (!accepted) reason = "the registry's register returned false";
|
|
238
|
+
}
|
|
239
|
+
} catch (installError) {
|
|
240
|
+
accepted = false;
|
|
241
|
+
reason = `installing the provider threw: ${errorMessage(installError)}`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!accepted) {
|
|
245
|
+
forgetInstalled(state);
|
|
246
|
+
state.lastOutcome = "install-failed";
|
|
247
|
+
state.reason = reason ?? "the registry did not accept the provider";
|
|
248
|
+
return { installed: false, outcome: "install-failed", shape, reason: state.reason };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Identity dedupe first: a frozen registry refuses the marker, and the WeakSet is authoritative.
|
|
252
|
+
state.installedRegistries.add(target);
|
|
253
|
+
try {
|
|
254
|
+
Object.defineProperty(target, HOOK_ARMED, {
|
|
255
|
+
value: true,
|
|
256
|
+
enumerable: false,
|
|
257
|
+
configurable: true,
|
|
258
|
+
});
|
|
259
|
+
} catch {
|
|
260
|
+
// Unmarkable host object: the WeakSet above still prevents a second provider.
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
state.installed = true;
|
|
264
|
+
state.installedRegistry = target;
|
|
265
|
+
state.acceptedProviders += 1;
|
|
266
|
+
state.lastOutcome = "hooked";
|
|
267
|
+
state.reason = undefined;
|
|
268
|
+
return { installed: true, outcome: "hooked", shape };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function forgetInstalled(state: HookState): void {
|
|
272
|
+
state.installed = false;
|
|
273
|
+
state.installedRegistry = undefined;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function classifyRegistry(value: unknown): HookShape {
|
|
277
|
+
if (Array.isArray(value)) return "array";
|
|
278
|
+
if (!isRecord(value)) return "unusable";
|
|
279
|
+
try {
|
|
280
|
+
return typeof value.register === "function" ? "register" : "unusable";
|
|
281
|
+
} catch {
|
|
282
|
+
// A throwing getter is an unusable registry, not a crash.
|
|
283
|
+
return "unusable";
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function describeUnusable(value: unknown): string {
|
|
288
|
+
if (typeof value !== "object" || value === null) {
|
|
289
|
+
return `registry is a ${value === null ? "null" : typeof value}, not an array or a registry object`;
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
const register = (value as Record<string, unknown>).register;
|
|
293
|
+
return typeof register === "function"
|
|
294
|
+
? "registry rejected the provider"
|
|
295
|
+
: "registry object has no callable register";
|
|
296
|
+
} catch (error) {
|
|
297
|
+
return `reading the registry's register property threw: ${errorMessage(error)}`;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function createFanOutProvider(state: HookState): HookFanOutProvider {
|
|
302
|
+
return {
|
|
303
|
+
id: FAN_OUT_PROVIDER_ID,
|
|
304
|
+
rows(hostCtx?: unknown): CustomizeRow[] {
|
|
305
|
+
// Same ordered snapshot and same builder as the capture backend, so `registerRows`
|
|
306
|
+
// behaves identically on either backend.
|
|
307
|
+
const built = buildRows(contextFromHost(hostCtx));
|
|
308
|
+
// This backend never sees the host array, so it has no host categories to honour. The resolver
|
|
309
|
+
// still orders each category by S2, and every `start`/`after` falls back to the end with the
|
|
310
|
+
// reason the diagnostics record reports: this backend must not claim host-relative placement (S6).
|
|
311
|
+
const plan = planRowPlacement([], built.providers, built.blocks);
|
|
312
|
+
recordObservedHostCategories(plan.hostCategories);
|
|
313
|
+
state.lastFailures = built.failures;
|
|
314
|
+
state.lastCapture = { kind: "injected", injectedRows: plan.rows.length };
|
|
315
|
+
return plan.rows;
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function errorMessage(error: unknown): string {
|
|
321
|
+
return error instanceof Error ? error.message : String(error);
|
|
322
|
+
}
|