Skip to content

Share one lazily loaded zod across plugin frontends - #6959

Merged
ymichael merged 2 commits into
mainfrom
bb/plugin-shared-zod
Nov 3, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/plugin-shared-zod

Conversation

@ymichael

@ymichael ymichael commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Twelve bundled plugin frontends each bundled their own copy of zod: 187 KB raw, about 45 KB brotli each, and about 540 KB brotli on every cold load. That was a fifth of everything a cold load transferred, measured in the cold-load investigation (thr_epbrhxq39i). zod had been kept out of the shared plugin runtime on purpose. runtime-shims.mjs records that exposing its namespace from the host stopped the boot chunk from tree-shaking zod and grew boot by 33 KB brotli.

What changed

  • Plugin builds (packages/plugin-build): VERSIONED_RUNTIME_SLOT_BY_SPECIFIER adds a zod slot, behind a new buildPluginApp option, hostProvidedZod. Builds the bb server makes itself pass true: bundled, git and local plugins, and source rebuilds. The CLI's bb plugin build and bb plugin dev keep zod bundled. A published npm artifact therefore still runs on older bb versions, which would not provide the slot and check only the SDK major. When enabled, a build shims zod to the slot only when the zod installed for that import has the host's major version and is no newer than the host's copy. The host's version is emitted as RUNTIME_PACKAGE_VERSIONS by the export-manifest generator. Otherwise zod still bundles from the plugin's node_modules, so a plugin pinned to zod 3 or a newer zod 4 keeps working. zod/mini and server builds are unchanged. The slot is kept out of RUNTIME_SLOT_BY_SPECIFIER, so it doesn't become a scaffold type-only devDependency, and the server's default slot list for bundles without metadata doesn't load zod for old bundles.
  • Host (apps/app): the zod slot is a lazy runtime group, like Pierre. It is loaded in parallel with the first plugin bundle that declares it. vite-plugin-zod-runtime.ts resolves virtual:bb-plugin-zod to a copy of zod under separate module ids, so the slot's full namespace never pins the boot chunk's tree-shaken zod. It stubs zod's locale barrel to en, as plugin builds already do. In dev and tests the id resolves to the normal zod.
  • Docs: docs/ui-code-splitting.md (lazy runtime groups) and the plugin-authoring reference (frontend-hooks-and-ui.md) describe when zod is shared.

There are no server/daemon wire changes and no Plugin SDK surface change.

How you verified

Bytes, from production builds of this branch and its base 7fa7f46fad:

before after
All 42 bundled plugin app.js files 7729 KB raw, 1975 KB br 4596 KB raw, 1254 KB br
push-notifications / thread-list / account-pool app.js 209 / 480 / 336 KB raw 20 / 291 / 147 KB raw
Shared zod chunk (loaded once) none 194 KB raw, 44 KB br
App boot payload 1579.7 KB raw 1580.1 KB raw (bundle budget OK)

Cold loads in Chromium on a scrubbed subset of production data (4,387 threads): production build, fresh context per load, 100 ms latency. The two builds were benchmarked in alternating server restarts. Medians of 5 runs per side at 20 Mbps and 3 at 5 Mbps:

before after
/ transfer 2706 KB 2287 KB
/ settled, HTTP/2, 5 Mbps 5492 ms 4851 ms
mid thread settled, HTTP/2, 5 Mbps 5324 ms 4712 ms
small thread settled, HTTP/2, 5 Mbps 5384 ms 4748 ms
/ sidebar, HTTP/2, 5 Mbps 2577 ms 2590 ms
/ sidebar, HTTP/2, 20 Mbps 1135 ms 1114 ms
mid thread content, HTTP/2, 20 Mbps 1288 ms 1246 ms
/ sidebar, HTTP/1.1, 20 Mbps 2489 ms 2383 ms

First-content times are within noise at both bandwidths. The win is bytes and settle time on slow links. One request is added (plugin-zod-runtime-*.js). Loading /, a thread and /settings/plugins gave no plugin load warnings or new console errors. Ten enabled plugins report the zod slot, and the shared chunk is requested once.

  • packages/plugin-build/src/build-plugin-app.test.ts: the version gate (same, older, newer, other major, prerelease), and builds against a fake installed zod. A compatible version is shimmed only when hostProvidedZod is set; a newer minor, the next major, or a CLI build bundles the plugin's own copy.
  • apps/app/src/vite-plugin-zod-runtime.test.ts: builds a fixture whose entry imports one zod export and lazily imports the slot. The lazy chunk holds only the separate copy, the entry holds no zod code outside its import, and locales are stubbed. Resolving the copy's imports to the shared ids makes it fail.
  • Locally: pnpm exec turbo run typecheck lint --filter=@bb/app --filter=@bb/plugin-build, the two test files, the bundle budget check after pnpm exec turbo run build --filter=@bb/app --filter=@bb/bundled-plugins. CI runs the full suites.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 3, 2026 15:33
Twelve bundled plugin frontends each carried their own 187 KB raw copy of
zod, about 540 KB brotli per cold load. zod was kept out of the shared
plugin runtime because exposing its namespace from the boot chunk stopped
tree-shaking and grew boot by 33 KB brotli.

Plugin app builds now shim `zod` to a `zod` runtime slot when the
plugin's installed zod has the host's major version and is no newer than
it. Other plugins keep bundling their own copy. The host provides the
slot as a lazy runtime group, like Pierre. vite-plugin-zod-runtime.ts
builds that group from separate module ids, so the slot's full namespace
never touches the boot chunk's zod. It also stubs locales the way plugin
builds do.

Cold load of `/` on a scrubbed production subset, 100 ms latency:
transfer 2706 -> 2287 KB. At 5 Mbps over HTTP/2 the load settles
5492 -> 4851 ms. First content is unchanged, and boot grows 0.4 KB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A bundle from `bb plugin build` (npm plugins must ship one) expected the
host to provide the shared zod. Older bb versions don't, and their only
artifact check is the SDK major, so that frontend would fail to load
there. buildPluginApp now takes hostProvidedZod. Builds the bb server
makes itself (bundled, git and local plugins, and source rebuilds) pass
true. The CLI's `bb plugin build` and `bb plugin dev` keep zod bundled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/plugin-shared-zod branch from 0cb2368 to c4273ba Compare November 3, 2026 23:35
@ymichael
ymichael merged commit bcf958d into main Nov 3, 2026
51 checks passed
@ymichael
ymichael deleted the bb/plugin-shared-zod branch November 3, 2026 23:41
ymichael pushed a commit that referenced this pull request Nov 24, 2026
## Human comments

## What was wrong

Five Ladle stories called `installTestPluginRuntime()` at module load
(`.ladle/workflows.stories.tsx` through `loadPluginApp`, which calls
it). That call replaces the plugin runtime that Ladle's global provider
installs (`.ladle/plugin-runtime.ts`) with the fake test SDK. The
stories mount plugins through core hosts, not through `renderSlot`, so
the fake `useBbContext()` and `useBbNavigate()` throw. The call had no
effect until #6942 removed Ladle's static `@get-bb/plugin-sdk/app` shim.
Since then, plugins read the global runtime. Because Ladle changes
stories without a reload, one of these stories also broke every later
story in the same tab. In the same pass, `ladle build` failed on
`virtual:bb-plugin-zod`, because #6959 added `pluginZodRuntime()` to the
app and Vitest configs but not to `.ladle/vite.config.ts`. See #8060.

## What changed

- `PluginThreadList.stories.tsx`, `AskUserQuestionPlugin.stories.tsx`,
`CodexMcpElicitation.stories.tsx`, `InteractionStates.stories.tsx`:
remove `installTestPluginRuntime()`.
- `.ladle/workflows.stories.tsx`: collect registrations with core's
`collectPluginAppRegistrations` instead of `loadPluginApp`.
- New `PluginThreadList.stories.test.tsx`: renders the Action alignment
story with Ladle's runtime and waits for a thread row. It also fails if
any `*.stories.tsx` imports `@get-bb/plugin-sdk/testing/app`.
- `.ladle/vite.config.ts`: add `pluginZodRuntime()` (separate commit).

## How you verified

- The new tests fail on the original stories (the render test fails with
`useBbContext() needs the test slot environment`, and the guard lists
all five files). They pass with this change.
- `apps/app/.ladle/tests/sidebar-actions.mjs` (Chromium, Playwright
1.58.2): on main it times out waiting for `thr_actions_leaf_short`. With
this change it prints `PASS: parent and leaf actions align at 220, 288
and 360px; disclosure precedes quick actions and overflow` on `ladle
serve` and on a served `ladle build`.
- Nine affected stories (thread list x2, question form, Codex form and
computer use, Additional States x3, Workflows panel) render with no page
errors.
- `npx ladle build` in `apps/app`: fails on main with `Rollup failed to
resolve import "virtual:bb-plugin-zod"`, succeeds with this change.
- `pnpm exec turbo run typecheck lint lint:type-aware test
--filter=@bb/app`: pass. One test, `UpdatePluginDialog.test.tsx`, fails
only under an `en_AU` host locale and passes with `en_US`. It is not
related to this change.

Fixes #8060

> AGENT GENERATED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant