Skip to content

Look up cached plugin server builds before building - #6467

Merged
ymichael merged 2 commits into
mainfrom
bb/5729-community-plugins-rebuild-on-every-bb-launc-thr_8entu4ue7q
Oct 28, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/5729-community-plugins-rebuild-on-every-bb-launc-thr_8entu4ue7q

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Community plugins whose prebuilt server bundle targets another plugin SDK version fall back to a source build, which is intentional while the SDK is pre-1.0. The cache for those source builds was looked up by a hash of the finished bundle (apps/server/src/services/plugins/plugin-server-cache.ts), so bb ran the full esbuild build on every launch before it could find the cached copy. A cache hit saved nothing (#5729).

What changed

The cache is now checked before building, with nothing skipped that could make the reused build stale.

  • packages/plugin-build/src/build-plugin-server.ts: buildPluginServer also returns the bundled input files from the esbuild metafile.
  • packages/plugin-build/src/code-digest.ts (new): pluginBuildCodeDigest() hashes the code files next to plugin-build's module. That is plugin-build's source in a source checkout and the server bundle in built and packaged bb, so a dev checkout that changes the build code doesn't serve builds made by the old code. It runs once per process, about 15 ms on the 9.6 MB server bundle.
  • apps/server/src/services/plugins/plugin-server-cache.ts (rewritten, same export and signature): each build stores a build.json that a later load checks before building.
    • Config key: plugin id, root, entry, package name and version, SDK version, bb version, the Node version, toolchain pins, runtime imports and the build-code digest.
    • Tracked paths:
      • every bundled input;
      • package.json, tsconfig.json and jsconfig.json in every ancestor directory, with missing ones recorded so that creating one invalidates;
      • files that a tsconfig extends;
      • directory listings of each input's directory and its ancestors inside the plugin root, so a new foo.ts beside foo.js invalidates.
    • Mid-build saves: the build start time is the staging directory's mtime. If any tracked path's mtime or ctime is at or after it, the record isn't written; whole-second filesystems get 2 s of slack. The build still loads; it just isn't reused.
    • Fast checks: a path whose size, mtime, ctime and inode all match counts as unchanged without being read; otherwise bb falls back to its content or listing. Up to 64 checks run at once and the check stops at the first mismatch.
    • Failure handling: any error while recording inputs skips the record instead of failing the load. A missing or corrupt record means a rebuild.
    • Retired-SDK warnings are stored with the build, so a reused build still reports them. The newest 4 builds per plugin are kept.

There are no wire, database, CLI or public SDK changes. This deviates from the issue's proposed fix only in going further: it tracks resolution inputs beyond the plugin source so that a cache hit can't be stale.

How you verified

  • New and updated tests in apps/server/test/services/plugins/plugin-server-cache.test.ts and prebuilt-server-loading.test.ts. I confirmed each fails when its fix is removed:
    • An unchanged plugin is built once and keeps its warnings; an SDK version change rebuilds (fails on main's cache).
    • A source file saved during the build isn't reused (fails without the build-start check).
    • A tsconfig paths change, an extended tsconfig change, a dependency's exports change and a new foo.ts beside foo.js each rebuild (fail when only bundled files are tracked; the extended-tsconfig case also fails without following extends).
    • A hit reads no files; a touched but unchanged file still hits; new content with its old mtime restored rebuilds (fail without the stat fast path or the ctime comparison).
    • A change in bb's plugin build code rebuilds (fails without the digest in the key).
    • A successful build whose inputs can't be recorded still loads (fails without the catch).
    • The issue's done-when: a community plugin with an older-SDK prebuild, loaded by two service instances on one data directory, is built once (fails on main).
  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/plugin-build passes. The cache and prebuilt-loading tests pass (17), and the @bb/plugin-build suite passes.
  • Benchmark on a plugin bundling lodash-es and date-fns (945 inputs): a cold build takes ~500 ms, which main paid on every launch; a cache hit takes 6–12 ms.
  • Manually on a pnpm start:worktree server: I installed the community plugin Agent Graph and changed the stamp in its dist/server.meta.json to SDK 0.6.93 to simulate a bb update.
    • The first reload built from source in 5.6 s.
    • Later reloads and a full server restart reused the same build in ~1.1 s, mostly CLI startup.
    • Editing a local plugin's server.ts rebuilt it, and reverting the edit reused the original build.
  • In a full parallel run of apps/server/test/services/plugins, two builtin-plugin watcher tests flaked under heavy load. Main fails one of them in the same run, and both pass when their file runs alone.

Fixes #5729

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits October 28, 2026 12:26
Community plugins whose prebuilt dist targets another SDK version fall
back to a source build, and the cache for those builds was keyed by a
hash of the finished bundle, so every bb launch ran the full esbuild
build before it could find the cached copy.

Each build now records the state of everything that decided its output,
and the next load checks that record before building:

- every bundled file, by content;
- every package.json, tsconfig.json and jsconfig.json from each bundled
  file's directory up to the filesystem root (absent ones included, so
  creating one invalidates), plus the files those tsconfigs extend;
- the directory listing of each bundled file's directory and its
  ancestors inside the plugin root, so a new file that changes
  resolution (foo.ts beside foo.js, a new node_modules package)
  invalidates.

The record is written only when no tracked path's mtime or ctime is at
or after the build started (read from the stage directory's timestamp,
with slack for whole-second filesystems). A file saved mid-build
therefore never gets cached as current. Because every recorded stat
predates the build, a later load can trust a matching size, mtime,
ctime and inode without reading the file. It hashes only paths whose
stat changed, with 64 checks in flight at once. For a plugin bundling
945 node_modules files, a cache hit takes ~6-12 ms against a ~500 ms
build (serially hashing every input took ~50 ms).

buildPluginServer now returns the bundled input paths from esbuild's
metafile.

Fixes #5729

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The cache key named the bb version but not the code that bundles
plugins, so a dev checkout that changed @bb/plugin-build kept serving
plugin builds made by the old code. The key now includes a digest of
the code files beside @bb/plugin-build's module: its source in a source
checkout, and the server bundle in built and packaged bb.

A failure while recording a finished build's inputs now skips the
record instead of failing a load whose build succeeded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit d28e001 into main Oct 28, 2026
93 of 95 checks passed
@ymichael
ymichael deleted the bb/5729-community-plugins-rebuild-on-every-bb-launc-thr_8entu4ue7q branch October 28, 2026 19:41
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.

Community plugins rebuild on every bb launch instead of once per update

1 participant