From 90b2de595dce8ce6d618d5c4797060b18555da1a Mon Sep 17 00:00:00 2001 From: zaynram Date: Sat, 6 Jun 2026 14:44:24 -0500 Subject: [PATCH 1/2] refactor(launcher): scope SessionStart provision pre-warm to the `startup` matcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provisioning the pinned bun into CLAUDE_PLUGIN_DATA is a first-session concern, so the pre-warm hook does not need to re-fire on resume/clear/compact (the bun already exists by then). Add `matcher: "startup"` to the SessionStart hook. Why not the `Setup` event (the obvious "this is setup, not per-session" instinct): `Setup` fires only on explicit `claude --init-only` / `-p --init|--maintenance` ("one-time preparation in CI or scripts") — never on plugin install or normal startup. Moving the pre-warm there would skip it for ordinary users, so the first scholar tool call would pay the full cold bun download + (on the source-sync path) the ~67 MB import-graph auto-install — the cold-start latency already seen in the field. The Claude Code plugins reference prescribes a guarded SessionStart hook writing into CLAUDE_PLUGIN_DATA for exactly this "install a dependency once, reuse across sessions and updates" case; this change keeps that mechanism and just narrows when it fires. Changes: - .claude-plugin/plugin.json (committed source-sync manifest): matcher added. - scripts/build-plugin.ts buildManifest(): matcher added to the generated per-OS manifests + rationale comment. buildMcpbManifest emits no SessionStart hook (Desktop schema), so it is unaffected. - tests/manifests.test.ts + tests/build-plugin.test.ts: assert matcher:"startup" (committed manifest + linux/win32 generated manifests). - 2026-06-01-slim-plugin-pivot spec: forward-looking hook description updated. Verified: both targets build (linux 2.9 MB / win32 3.0 MB, 19 files each) and the generated manifests carry matcher:"startup"; suite 391 pass / 0 fail; tsc clean. Co-Authored-By: Claude Opus 4.8 (1M context) --- .claude-plugin/plugin.json | 1 + .../specs/2026-06-01-slim-plugin-pivot.md | 18 +++++++++++++++--- scripts/build-plugin.ts | 13 +++++++++++++ tests/build-plugin.test.ts | 4 ++++ tests/manifests.test.ts | 19 +++++++++++++++++-- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index b643b26..f79a23f 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -25,6 +25,7 @@ "hooks": { "SessionStart": [ { + "matcher": "startup", "hooks": [ { "type": "command", diff --git a/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md b/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md index 9a01c92..f50932d 100644 --- a/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md +++ b/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md @@ -127,9 +127,21 @@ selects target. Both packages build on the Linux host: Launch is **M2** (see `bin/`): on both OSes `bun` runs `bin/launch.mjs`, which calls the idempotent `ensure-bun` provisioner then spawns the pinned bun on the -server entry. A `SessionStart` hook pre-warms `ensure-bun` via `bun launch.mjs ---provision-only` (latency only — the launcher is the correctness gate, because -SessionStart does not block MCP spawn). **Shape change (2026-06-05):** the old +server entry. A `SessionStart` hook (scoped to the `startup` matcher) pre-warms +`ensure-bun` via `bun launch.mjs --provision-only` (latency only — the launcher is +the correctness gate, because SessionStart does not block MCP spawn). **Hook scope +(2026-06-06):** the pre-warm is `startup`-only — provisioning the pinned bun into +`CLAUDE_PLUGIN_DATA` is a first-session concern, so it need not re-fire on +resume/clear/compact. The plugins reference prescribes the *mechanism* — a +SessionStart hook that installs a dependency into `CLAUDE_PLUGIN_DATA` once and +reuses it across sessions and updates — but its example is *unscoped*, leaning on an +in-hook `diff` guard for idempotency. The `startup` narrowing is scholar's own +decision, not a doc prescription: scholar's guard-equivalent already exists +(`ensure-bun` is idempotent + flock-guarded) and `launch.mjs` (no flag) is the real +correctness gate, so re-firing on resume/clear/compact buys nothing. `Setup` is +deliberately *not* used: it fires only on explicit `claude --init-only` / +`-p --init|--maintenance` ("one-time preparation in CI or scripts"), never on +install or normal startup, so it would skip the pre-warm for ordinary users. **Shape change (2026-06-05):** the old Linux launcher `exec`ed into bun, so the PID Claude Code spawned *became* the server (no wrapper); `bun launch.mjs` instead spawns the pinned bun as a **child** on both OSes — always a parent+child pair cross-OS. Mitigated, not a regression: diff --git a/scripts/build-plugin.ts b/scripts/build-plugin.ts index e2c9bbf..42b1740 100644 --- a/scripts/build-plugin.ts +++ b/scripts/build-plugin.ts @@ -210,9 +210,22 @@ export function buildManifest( // spawn, so this is a latency optimization only — `bun launch.mjs` (no // flag) is the correctness gate. --provision-only runs the same shim, // which dispatches to the per-OS ensure-bun (idempotent + lock-guarded). + // + // Scoped to the `startup` matcher: provisioning the pinned bun into + // CLAUDE_PLUGIN_DATA is a first-session concern, so there's no point + // re-firing it on resume/clear/compact (the bun already exists by then). + // The `Setup` event is deliberately NOT used — it fires only on explicit + // `claude --init-only` / `-p --init|--maintenance` ("one-time preparation + // in CI or scripts"), never on install or normal startup, so it would skip + // the pre-warm for ordinary users. The plugins reference prescribes the + // SessionStart-into-CLAUDE_PLUGIN_DATA *mechanism* (install a dependency once, + // reuse across sessions and updates) but its example is UNSCOPED, guarding + // idempotency with an in-hook `diff`; the `startup` narrowing is scholar's own + // call, justified by ensure-bun's guard + launch.mjs being the correctness gate. hooks: { SessionStart: [ { + matcher: 'startup', hooks: [ { type: 'command', diff --git a/tests/build-plugin.test.ts b/tests/build-plugin.test.ts index dc17a93..09cceea 100644 --- a/tests/build-plugin.test.ts +++ b/tests/build-plugin.test.ts @@ -29,6 +29,8 @@ test('buildManifest(linux): bun launch.mjs + HOME runtime root + bun pre-warm', expect(s.env.SCHOLAR_RUNTIME_ROOT).toBe('${HOME}/mcp-data/scholar/runtime') expect(s.env.SCHOLAR_OLLAMA_EMBED_MODEL).toBe('nomic-embed-text:v1.5') expect(s.env.SCHOLAR_OLLAMA_CHAT_MODEL).toBe('qwen3:8b') + // Pre-warm scoped to `startup` — not Setup, not every SessionStart trigger. + expect(m.hooks.SessionStart[0].matcher).toBe('startup') const hook = m.hooks.SessionStart[0].hooks[0].command expect(hook).toContain('launch.mjs') expect(hook).toContain('--provision-only') @@ -47,6 +49,8 @@ test('buildManifest(win32): bun launch.mjs + USERPROFILE root + bun pre-warm', ( ) // Ollama defaults are platform-independent. expect(s.env.SCHOLAR_OLLAMA_EMBED_MODEL).toBe('nomic-embed-text:v1.5') + // Pre-warm scoped to `startup` — not Setup, not every SessionStart trigger. + expect(m.hooks.SessionStart[0].matcher).toBe('startup') const hook = m.hooks.SessionStart[0].hooks[0].command expect(hook).toContain('launch.mjs') expect(hook).toContain('--provision-only') diff --git a/tests/manifests.test.ts b/tests/manifests.test.ts index e820005..a734263 100644 --- a/tests/manifests.test.ts +++ b/tests/manifests.test.ts @@ -48,11 +48,26 @@ test('mcpServers.scholar uses the cross-OS bun launcher (source-sync supersedes expect(scholar.env.SCHOLAR_OLLAMA_CHAT_MODEL).toBe('qwen3:8b') }) -test('SessionStart hook pre-warms the bun provisioner via the cross-OS shim', () => { +test('SessionStart pre-warm is scoped to the startup matcher (not Setup, not every trigger)', () => { // The hook is a pre-warm only — launch.mjs (no flag) is the correctness gate, // because SessionStart does not block MCP spawn. Here it runs --provision-only // so the same shim resolves the sh-vs-powershell provisioner per-OS. - const cmd = pluginManifest.hooks?.SessionStart?.[0]?.hooks?.[0]?.command + // + // Scoped to `startup`: provisioning the pinned bun into CLAUDE_PLUGIN_DATA is a + // first-session concern, so it must NOT re-fire on resume/clear/compact (the + // bun already exists by then). The docs prescribe the SessionStart-into- + // CLAUDE_PLUGIN_DATA *mechanism* ("install a dependency once, reuse across + // sessions and updates") but their example is UNSCOPED, guarding idempotency + // with an in-hook `diff`. The `startup` narrowing is scholar's OWN call, not a + // doc prescription: ensure-bun is already idempotent + flock-guarded and + // launch.mjs (no flag) is the real correctness gate, so re-firing on + // resume/clear/compact buys nothing. The `Setup` event is deliberately NOT + // used: it fires only on explicit `claude --init-only` / `-p --init|--maintenance` + // ("for one-time preparation in CI or scripts"), never on install or normal + // startup — so it would skip the pre-warm for normal users entirely. + const group = pluginManifest.hooks?.SessionStart?.[0] + expect(group?.matcher).toBe('startup') + const cmd = group?.hooks?.[0]?.command expect(cmd).toContain('launch.mjs') expect(cmd).toContain('--provision-only') }) From 0e3bd8b2dd65696d19279a4006d2d16308358c5e Mon Sep 17 00:00:00 2001 From: zaynram Date: Sat, 6 Jun 2026 16:50:01 -0500 Subject: [PATCH 2/2] docs(launcher): self-label retired launch.{sh,cmd} records that read as current in isolation Follow-up to the matcher-scoping change (this branch / PR #5), addressing the doc-hygiene item it surfaced. The bun-launcher unification (PR #4) retired the per-OS `launch.{sh,cmd}` shell pair, and every stale reference in the docs already carries an adjacent supersede banner (repo convention: keep the dated record, add a banner). The residual problem is the grep/scroll-lander: two lines state the retired shell-pair launcher as the *current* mechanism when read without the banner in view. Self-label them inline; the historical data stays put (the banners say to keep it). - docs/.../2026-06-01-slim-plugin-pivot.md: the "Both packages build" bullet-list intro now names the 2026-06-01 sizes + `launch.{sh,cmd}` in the bullets below as the retired shell-pair record and points at the current `bun launch.mjs`. - docs/audits/2026-06-02-maintenance-readiness.md: the "Launch chain" status row is tagged as a 2026-06-02 snapshot superseded by the note directly below it. Left as-is (already adequately framed): the supersede banners themselves; the RESOLVED+moot `launch.cmd` CRLF section; HUMAN.md's "(2026-06-03)" launcher-proof paragraph (bold dated header + adjacent banner). Docs-only; no code/test change. Co-Authored-By: Claude Opus 4.8 (1M context) --- docs/audits/2026-06-02-maintenance-readiness.md | 2 +- docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/audits/2026-06-02-maintenance-readiness.md b/docs/audits/2026-06-02-maintenance-readiness.md index 606c926..24ff612 100644 --- a/docs/audits/2026-06-02-maintenance-readiness.md +++ b/docs/audits/2026-06-02-maintenance-readiness.md @@ -27,7 +27,7 @@ been run** against a live server. Everything else is tidiness. | Test suite (`bun test src tests`) | **324 pass / 4 skip / 1 todo / 0 fail** (329 across 43 files, ~13s) | | Build artifacts | `dist/server.js` 2.45 MB · `dist/pdf-server/{index.js 3.98 MB, mcp-app.html 4.42 MB}` present | | UI bundle (`measure-bundle`) | 1014 KB single-file, within budget | -| Launch chain | `plugin.json` → `/bin/sh bin/launch.sh` → `ensure-bun.sh` (pins bun 1.3.11) → `exec bun dist/server.js` — statically sound, vec0-ABI pin intact | +| Launch chain | `plugin.json` → `/bin/sh bin/launch.sh` → `ensure-bun.sh` (pins bun 1.3.11) → `exec bun dist/server.js` — statically sound, vec0-ABI pin intact. **(2026-06-02 snapshot — retired; see supersede note directly below.)** | > **Superseded 2026-06-06 (bun-launcher unification, PR #4).** The launch chain is > now `plugin.json` → `bun ${CLAUDE_PLUGIN_ROOT}/bin/launch.mjs` → diff --git a/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md b/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md index f50932d..5fac0a0 100644 --- a/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md +++ b/docs/superpowers/specs/2026-06-01-slim-plugin-pivot.md @@ -114,7 +114,10 @@ not the 6.7 MB vendored pdf — were the bloat. > history. `scripts/build-plugin.ts` rewritten to the slim model; `SCHOLAR_BUILD_WIN` -selects target. Both packages build on the Linux host: +selects target. Both packages build on the Linux host (the 2026-06-01 sizes and +`launch.{sh,cmd}` launchers named in the two bullets below are the **retired** +shell-pair record, kept per the supersede banner above; the launcher is now the +cross-OS `bun ${ROOT}/bin/launch.mjs`): - **linux** `scholar.plugin` 2.8 MB / 14 files — unzipped and launched via the generated manifest (`/bin/sh ${ROOT}/bin/launch.sh`) with a fresh