Skip to content

Defer plugin frontend boot past first route paint with bounded, ordered loads (A3, B23) - #1896

Merged
SawyerHood merged 4 commits into
bb/mobile-perf/plugin-build-minifyfrom
bb/mobile-perf/plugin-boot-defer
Aug 19, 2026
Merged

SawyerHood merged 4 commits into
bb/mobile-perf/plugin-build-minifyfrom
bb/mobile-perf/plugin-boot-defer

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 19, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

  • Every running plugin's app bundle (about 3 MB unminified JS plus about 780 KB CSS across the default set) started importing as soon as /system/config resolved, all at once via Promise.all, each injecting its own <link rel=stylesheet> on arrival. On a phone this parse/eval and the dozen style invalidations landed in the same window in which the route chunk was still downloading, so first route content painted late. (Audit finding A3.)
  • The workflows composer banner called workflowActiveRuns once per second on every open thread for as long as it was mounted, whether the thread had a run and whether the tab was visible. (Audit finding B23.)

What changed

  • usePluginFrontendBoot waits for system config, then for the first route content to commit (RouteContentPaintSignal, a sibling of the lazy routes inside their Suspense boundary) plus an idle slot (requestIdleCallback, or two animation frames on WebKit), bounded by a 1.5 s timeout. A /plugins/:pluginId/... route boots as soon as config resolves because the plugin is the page there.
  • reconcilePluginFrontends imports at most three bundles at a time. The plugin that owns the current panel route goes first, then ascending bundle size. The server now reports jsBytes per app bundle (pluginAppStateSchema, GET /api/v1/plugins).
  • Plugin stylesheet insertions coalesce into one animation frame (createBatchedPluginCssApplier); removals stay synchronous and cancel a pending insertion. The pagehide teardown wiring is untouched.
  • Workflows: the service publishes a workflow-runs realtime signal for the origin thread on run create, claim, settle, and cancel. The banner subscribes and refreshes on a matching signal. Banner and preview directive poll once per second only while a run is active (or the last fetch errored) and document.visibilityState is not hidden, with one catch-up refresh when the tab or the realtime connection comes back.

How you verified

  • New tests (fail before, pass after): apps/app/src/lib/plugin-frontend-boot-schedule.test.ts (paint+idle vs timeout vs cancel), apps/app/src/hooks/usePluginFrontendBoot.test.tsx (no boot on config alone; boot after paint+idle; boot at 1.5 s; immediate boot on plugin panel route), apps/app/src/lib/plugin-frontend-load-order.test.ts (ordering, max-3 concurrency and lane hand-off, failure isolation, CSS batching/removal), plugins/workflows/src/app.test.tsx (idle thread never polls, realtime signal triggers one refresh and polling starts/stops with active runs; hidden document pauses polling and refreshes once on return), plugins/workflows/src/service-policy.test.ts (signal published on start/claim/cancel, not on a no-op stop), apps/server/test/services/plugins/plugin-app-bundle.test.ts (jsBytes equals dist/app.js size).
  • pnpm exec turbo run typecheck for @bb/app, @bb/server, @bb/server-contract, @bb/templates, @bb/cli, @bb/sdk, bb-plugin-workflows: pass. pnpm exec turbo run lint --filter=@bb/app: 0 errors, no warnings in touched files. pnpm exec turbo run build --filter=@bb/app + node scripts/check-bundle-budget.mjs: bundle budget OK (449.2 KB br boot). Targeted vitest runs listed above: all pass.

Fixes: part of the mobile / iOS Safari performance program (verified sweep report in the bb thread; no single issue).

Stack context

Layer 17 of 22 in the bb/mobile-perf/* stack (bottom → top: quick wins first, big rocks last).

  • Prerequisite (layer below): bb/mobile-perf/plugin-build-minify (Minify plugin app bundles, optimize their CSS, drop provider plugin app bundles (A4, H7) #1895).
  • Next layer: bb/mobile-perf/plugin-host-slots (Shim clsx, tailwind-merge, cva and the shared icon onto plugin host slots (A5, partial) #1897).
  • Audit findings addressed: see title IDs. Review: approved-with-fixes; 1 review fix commit(s).
  • Reviewer notes / follow-ups: The package title mentions a 'pageshow reboot' (audit J5: tear down plugin frontends only when !event.persisted, and re-boot on a persisted pageshow) but the branch does not implem | Design trade-off to be aware of (not a bug): reconcile now runs 3 lanes over import + setup + content-script mount. A third-party plugin whose bundle import stalls on a bad network | Contract note: /api/v1/plugins app.bundle.jsBytes is required and the browser-side isFrontendBundle guard drops bundles without it. App, SDK/CLI, and server ship together so this i
  • Wire/contract: GET /api/v1/plugins (and plugin reload/list responses) app.bundle gains a required jsBytes: number field (packages/server-contract pluginAppStateSchema, apps/server app-bundle.ts, apps/app isFrontendBundle guard). This is server<->browser only; nothing on the server<->host-daemon wire changed, so HOST_DAEMON_PROTOCOL_VERSION was not bumped. Regenerated tracked artifacts: packages/plugin-sdk/bund

AGENT GENERATED: by Claude Opus 5

@SawyerHood SawyerHood changed the title bb/mobile perf/plugin boot defer Defer plugin frontend boot past first route paint with bounded, ordered loads (A3, B23) Aug 19, 2026
@SawyerHood
SawyerHood force-pushed the bb/mobile-perf/plugin-boot-defer branch 2 times, most recently from f2cf113 to b76b34b Compare August 19, 2026 07:50
@SawyerHood
SawyerHood marked this pull request as ready for review August 19, 2026 08:04
@SawyerHood
SawyerHood force-pushed the bb/mobile-perf/plugin-boot-defer branch from b76b34b to 19e75e1 Compare August 19, 2026 15:59
SawyerHood and others added 4 commits August 19, 2026 16:10
…ed loads

Every running plugin's app bundle (about 3 MB unminified JS plus about
780 KB CSS across the default set) started importing the moment
/system/config resolved, all at once via Promise.all, each injecting its
own stylesheet on arrival. On a phone that parse/eval and the dozen
style invalidations landed in the same window in which the route chunk
was still downloading, so first route content painted late.

- Boot now waits for system config AND the first route content to
  commit (a RouteContentPaintSignal sibling inside the route Suspense
  boundary) plus an idle slot (requestIdleCallback, or two animation
  frames on WebKit), bounded by a 1.5 s timeout so plugins never wait on
  a route that does not resolve. A /plugins/:pluginId panel route still
  boots as soon as config resolves: the plugin is the page there.
- reconcilePluginFrontends imports at most three bundles at a time. The
  plugin owning the current panel route goes first, then ascending
  bundle size, so several light plugins register before one heavy one.
  The server now reports jsBytes for each app bundle (server-contract
  pluginAppStateSchema, GET /api/v1/plugins) to drive that order.
- Stylesheet insertions coalesce into one animation frame; removals stay
  synchronous and cancel a pending insertion (teardown must not leave a
  sheet behind).

The pagehide teardown wiring in plugin-frontend.ts is untouched.

Co-Authored-By: Claude <[email protected]>
… over realtime

The workflows composer banner called workflowActiveRuns every second on
every open thread for as long as it was mounted, whether or not the
thread had a run and whether or not the tab was visible. On a phone that
kept the radio and main thread busy on idle threads.

- The service publishes a `workflow-runs` realtime signal `{ threadId }`
  for the origin thread when a run is created, claimed, settled, or
  cancelled. The banner subscribes and refreshes on a matching signal,
  so an idle thread needs no standing poll to show a new run.
- Both the banner and the workflow-preview directive poll once per
  second only while a run is active (or the last fetch errored) and
  document.visibilityState is not hidden. Coming back to the tab, or the
  realtime connection returning to `connected`, triggers one catch-up
  refresh before the cadence resumes.

Co-Authored-By: Claude <[email protected]>
…s field

packages/templates embeds @get-bb/plugin-sdk/bundled-types as strings and
its typecheck/test scripts assert the embed is current. The previous
commit added jsBytes to the plugin app bundle schema, which the bundled
declarations reflect.

Co-Authored-By: Claude <[email protected]>
…export

lib.dom already declares requestIdleCallback/cancelIdleCallback, so the
feature check works on window directly (AGENTS.md: no internal casts).
isRouteContentPainted had no callers.

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood force-pushed the bb/mobile-perf/plugin-boot-defer branch from 19e75e1 to 0be12a3 Compare August 19, 2026 16:13
@SawyerHood
SawyerHood merged commit 9ea34d3 into main Aug 19, 2026
12 checks passed
@SawyerHood
SawyerHood deleted the bb/mobile-perf/plugin-boot-defer branch August 19, 2026 16:23
SawyerHood added a commit that referenced this pull request Aug 19, 2026
…pp bundles (A4, H7) (#1895)

## What was wrong

Plugin frontend bundles were built without minification, and every
plugin's `app.css` carried the Tailwind utilities of all of
`@bb/shared-ui`: `bb.pluginTailwindContent: ["src/**/*"]` fed the whole
package glob to the scanner whether or not the plugin imported those
components (~110 KB / ~1200 rules per sheet, plus a `:root` block that
re-declared the host's semantic token bridge). On phones the raw bytes
drive parse time, each stylesheet insertion is a document-wide restyle,
and ~17k scoped rules stay resident. (A4)

Four first-party provider plugins (claude-code, codex, pi, acp) shipped
an app bundle whose only job was to register, through
`experimental_providerIcon`, an SVG byte-identical to the one the host
already vendors in `BUILT_IN_BRAND_ICONS`: four JS fetches, four CSS
fetches, four stylesheet inserts and four icon remounts per page load
for no visible change. (H7)

## What changed

- `buildPluginApp` builds with `minify: true`, `legalComments: "none"`,
and takes `{ minify }`; `bb plugin dev` passes `{ minify: false }` so
stack traces and CSS stay readable while iterating. Server installs and
`bb plugin build` minify.
- The Tailwind output plus authored CSS run through Tailwind's own
optimizer (`optimize` from `@tailwindcss/node`, the lightningcss pass
the host's Vite build uses); minified in production, pretty in dev, same
browser-target lowering in both.
- `bb.pluginTailwindContent` globs now only select which dependency
files may be scanned; the scan reads their intersection with esbuild's
`metafile.inputs`, so a plugin pays only for the shared components it
bundles. Scan bases are realpath'd because esbuild reports real paths
for workspace links.
- The generated plugin theme marks the host's `@theme inline` bridge as
`inline reference`: inline tokens are substituted into utilities at
compile time, so the `:root { --color-*: var(--*) }` block was never
read by plugin rules. Non-inline tokens still emit.
- Removed `app.tsx` / `bb.app` / react devDeps from the four provider
plugins; updated the provider-icon comment, `docs/api_to_audit.md`, and
the plugin-authoring skill example that pointed at the deleted file.

Built-in bundle bytes (raw / gzip), before -> after:

| plugin | app.js | app.css |
| --- | --- | --- |
| tasks | 2,383,490 / 503,399 -> 1,257,491 / 360,279 | 127,234 / 15,866
-> 51,898 / 8,763 |
| docs | 1,641,918 / 374,940 -> 839,167 / 262,909 | 112,434 / 14,127 ->
30,250 / 5,502 |
| automations | 1,064,406 / 185,248 -> 587,528 / 141,850 | 112,712 /
14,112 -> 51,952 / 8,449 |
| secrets | 644,324 / 100,199 -> 352,915 / 76,467 | 110,067 / 13,891 ->
17,288 / 3,321 |
| provider-* (x4) | ~2-3.6 KB each -> none | ~3-4.3 KB each -> none |

## How you verified

- New tests in `packages/plugin-build/src/build-plugin-app.test.ts`:
minified vs readable artifacts (identifier mangling, license comment
dropped, CSS minified/pretty for both utilities and authored CSS);
dependency scanning through a symlinked package with a negated pattern
(bundled file scanned, glob-only file not, negated file not, plugin's
own dir still scanned whole, `:root` has the default token and not the
bridge). Both fail before the change.
- Updated dependent assertions for minified CSS in `apps/cli`
plugin-build test, `apps/server` plugin-app-bundle test, and
`packages/plugin-registry` vendor test.
- `turbo run typecheck` for @bb/plugin-build, @bb/cli, @bb/server,
@bb/app, @bb/plugin-registry and the four provider plugins: pass. `turbo
run lint --filter=@bb/app`: pass.
- `turbo run test`: @bb/plugin-build full suite (22 passed, 1 skipped);
@bb/cli plugin-build, docs/github official bundle,
packaged-plugin-build, plugin-dev-loop (21 passed); @bb/server
`test/services/plugins` (34 files, 380 passed); @bb/plugin-registry
vendor-all-items; @bb/app provider-icon test.
- Rebuilt all builtins with
`apps/server/scripts/copy-builtin-plugins.ts` before and after for the
byte table above.

Fixes: part of the mobile / iOS Safari performance program (verified
sweep report in the bb thread; no single issue).


## Stack context

Layer 16 of 22 in the `bb/mobile-perf/*` stack (bottom → top: quick wins
first, big rocks last).
- Prerequisite (layer below):
`bb/mobile-perf/pierre-pool-and-route-closure` (#1894).
- Next layer: `bb/mobile-perf/plugin-boot-defer` (#1896).
- Audit findings addressed: see title IDs. Review: approved-with-fixes;
1 review fix commit(s).
- Reviewer notes / follow-ups: Minified production plugin bundles ship
without source maps (implementer-acknowledged; dev loop is readable).
Not a blocker. | Authored plugin CSS now passes through lightningcss
(errorRecovery on, warnings printed); invalid authored CSS is dropped at
build time rather than by the browser. Behavioural but | Four touched
files (bb-plugin-authoring SKILL.md, generate-plugin-theme.mjs,
vendor-all-items.test.ts, provider-pi/tsconfig.json) are not
prettier-clean, but they were already not
- Wire/contract: None. No server<->daemon wire, RPC, or session payload
changed; HOST_DAEMON_PROTOCOL_VERSION untouched. Artifact format
(dist/app.js, app.css, app.meta.json, artifactFormatVersion 1)
unchanged; only content minification. Public API:
`buildPluginApp(rootDir, bbVersion, toolchain, options?:
PluginAppBuildOptions)` gained an optional 4th arg (exported type
PluginAppBuildOptions from @bb/plugin-build)

> AGENT GENERATED: by Claude Code (claude-mangosteen-eap)

---------

Co-authored-by: Claude <[email protected]>
SawyerHood added a commit that referenced this pull request Aug 19, 2026
…lots (A5, partial) (#1897)

## What was wrong

Every plugin app bundle carried its own copy of libraries the host
already ships (audit A5). zod (~516 KB raw / ~75 KB gzip) was bundled by
four builtins, tailwind-merge + clsx by thirteen,
class-variance-authority by most, and the shared-ui `Icon` with its ~110
KB hugeicons map by twelve. Phones parsed and evaluated all of it on
every page load. `RUNTIME_SLOT_BY_SPECIFIER` in packages/plugin-build
only shimmed react, the SDK, pierre, the portal radix families, sonner
and vaul.

## What changed

- `bb plugin build` routes `zod`, `clsx`, `tailwind-merge`,
`class-variance-authority` and `@bb/shared-ui/icon` to new
`globalThis.__bbPluginRuntime` slots (`zod`, `clsx`, `tailwindMerge`,
`classVarianceAuthority`, `sharedUiIcon`). shared-ui's own components
import the icon module as `./icon`; the esbuild plugin routes that
import to the same slot when the importer lives in shared-ui's source
tree. A plugin's vendored `icon.tsx` still bundles.
- Shims forward a real default export (`mod.default` when the host
namespace has one), so `import clsx from "clsx"` receives the callable,
not the namespace.
- Host (`apps/app/src/lib/plugin-frontend.ts`) installs the five slots.
Export manifest regenerated (`@bb/shared-ui/icon` exports come from
esbuild metadata like the SDK facade).
- `PLUGIN_SDK_VERSION` 0.4.8 -> 0.4.9. Old bundles keep working (they
carry their own copies); new bundles need the new host slots, so
path/builtin plugins rebuild on the sdkVersion-differs trigger and
scaffolds pin the floor.
- Scaffold generator moves clsx/tailwind-merge/cva to type-only deps
(zod stays a dependency: server.ts bundles it). Guide and
bb-plugin-authoring skill updated. CLI scaffold-dependency test now
applies the shim exemption only to app-side files.
- `apps/app/bundle-budget.json` raised: exposing the whole zod namespace
stops the boot chunk from tree-shaking zod exports the app never calls:
boot payload 1655.7 KB / 449.1 KB brotli -> 1848.4 KB / 481.7 KB (+193
KB raw, +33 KB brotli). The other four slots add under 2 KB. Removing
`zod` from RUNTIME_SLOT_BY_SPECIFIER reverts both sides of the trade.

Builtin app.js after rebuild: 8.87 MB -> 4.01 MB raw (-55%), 1.76 MB ->
0.93 MB gzip (-47%). Per plugin (raw): ask-user-question 784 KB -> 38
KB, automations 1063 -> 315, connect 376 -> 145, custom-instructions 99
-> 9, docs 1640 -> 1419, github 416 -> 185, inline-vis 239 -> 9,
keep-awake 308 -> 79, memory 122 -> 30, provider-retry 249 -> 18,
secrets 644 -> 37, side-chat 241 -> 11, tasks 2380 -> 1638, workflows
300 -> 69.

## How you verified

- New tests in `packages/plugin-build/src/build-plugin-app.test.ts`:
every non-SDK slot has a manifest entry; a bundle importing the five
libraries compiles with no node_modules and, executed against a fake
runtime, yields the callable clsx default, `z`, `twMerge`, `cva`,
`Icon`; shared-ui's relative `./icon` import is shimmed while a plugin's
own `components/ui/icon.tsx` still bundles;
`isSharedUiIconRelativeImport` unit cases.
`apps/app/src/lib/plugin-frontend.test.ts` asserts the new slots and
their identity with the host modules.
- `pnpm exec turbo run typecheck lint` for @bb/plugin-build,
@bb/templates, @get-bb/plugin-sdk, @bb/domain, @bb/app, @bb/cli,
@bb/server: green. `pnpm exec turbo run test` for @bb/plugin-build (24
passed), @bb/templates (44), @bb/plugin-registry (1), plugin-sdk version
test, CLI
plugin-build/plugin-new/packaged-plugin-build/plugin-guide-docs/plugin-dev-loop/docs-
and github-official-plugin-bundle/plugin-scaffold-dependencies tests,
server plugin-app-bundle + plugin-sdk tests, app plugin-frontend test:
all green.
- `pnpm exec turbo run build --filter=@bb/app` + `node
scripts/check-bundle-budget.mjs`: passes with the ratchet.
- Rebuilt all 18 plugin app bundles through buildPluginApp (sizes
above).

Fixes: part of the mobile / iOS Safari performance program (verified
sweep report in the bb thread; no single issue).


## Stack context

Layer 18 of 22 in the `bb/mobile-perf/*` stack (bottom → top: quick wins
first, big rocks last).
- Prerequisite (layer below): `bb/mobile-perf/plugin-boot-defer`
(#1896).
- Next layer: `bb/mobile-perf/rerender-amplifiers` (#1898).
- Audit findings addressed: see title IDs. Review: approved-with-fixes;
1 review fix commit(s).
- Partial: A5 (bundle splitting): not implemented (outdir + esbuild
splitting + served chunks). Slots shipped; splitting plan described in
the report.
- Reviewer notes / follow-ups: A5 'splitting' (outdir + splitting, lazy
definePluginApp components) is not implemented; the branch only covers
the host-slot half of A5, as the implementer reported. | Design
trade-off to be aware of, not a defect: exposing the full zod namespace
on __bbPluginRuntime stops the host boot chunk from tree-shaking zod
(+193 KB raw / +33 KB br on boot | Compatibility note (pre-existing
pattern, same as the earlier @pierre/diffs slot): a plugin app built
with the new bb plugin build throws 'Cannot load "zod"' if loaded by an
older
- Wire/contract: No server<->host-daemon wire change;
HOST_DAEMON_PROTOCOL_VERSION untouched. Plugin runtime contract: five
new slots on globalThis.__bbPluginRuntime (zod, clsx, tailwindMerge,
classVarianceAuthority, sharedUiIcon); RUNTIME_SLOT_BY_SPECIFIER gains
zod, clsx, tailwind-merge, class-variance-authority, @bb/shared-ui/icon;
runtime shims now export `("default" in mod ? mod.default : mod)` as
default. PL

> AGENT GENERATED: by Claude Code (claude-mangosteen-eap)

---------

Co-authored-by: Claude <[email protected]>
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