Repository navigation
Conversation
…experiment The isolatedPluginServers experiment moves the server entry of git and npm plugins into a worker thread per plugin. Core keeps building the plugin API handle; the worker gets a mirror whose calls cross synchronously through Atomics, and its callbacks, RPC methods and HTTP routes are proxied back into the same records core already dispatches. storage.database() opens in the worker. Plugins that use APIs a worker cannot host load in-process. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…plugin workers Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/run-marketplace-plugin-server-code-off-core-s-ev-thr_jbvai7bpud
branch
from
November 9, 2026 21:22
c196e7d to
aac8a7c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Every plugin's server code runs on the bb server's event loop. In production, the third-party usage plugin (
github.com/MayankBansal12/bb-plugin-usage) accounted for 15.7% of all sampled event-loop stall time (24.9 s of 158.5 s, up to 361 ms in one stall). 76% of that was its own SQLite work on its plugindata.db, and itsdashboardRPC runs a 335 ms query. No change in core can make that work cheaper, because it is the plugin's own synchronous code. It can only stop stalling core if it runs somewhere else. (Investigation: O2 audit report, "Proposal: isolate plugin server work".)What changed
A new
isolatedPluginServersexperiment, off by default and not on for early adopters, runs the server entry of every plugin installed from git or npm in its ownworker_thread.Design. Core still builds the plugin's real API handle with
createPluginApi. Status, stats,invokeWrapped,wireLookup, services, schedules, events and dispose work exactly as before. The plugin's factory runs in the worker against a mirror ofBbPluginApi:bb.*blocks only the worker (Atomics.wait+receiveMessageOnPort). Core runs the realhandle.apimember and returns the value or the error, so registration errors still throw synchronously. A member that returns a promise resolves later.start, event and settings listeners, schedules, CLIrun,onDispose/onInstall,whileWatched) become proxies in core's handle records and run in the worker. Abort signals, errors (includingPluginContextStaleErrorand RPC boundary errors), streams andResponses cross the boundary.runPluginRpcMethod, which validates input, runs the handler, validates and normalizes the output, and computes tags. It runs wherever the plugin's code runs. Core's in-process records and worker records both exposeinvoke(), andinvokeRpcHandlercalls it.Responsebody are transferred as streams, and the request's abort signal is mirrored into the worker. The worker's HonoContextgets core'sc.req.path,c.req.routePath,c.req.param()and context variables (c.var/c.get).c.env(the raw Node request and response) isn't available in a worker.storage.database()stays synchronous inside the worker (sharedopenPluginDatabase). The hourly incremental-vacuum sweep runs inside each worker (maintainIsolatedPluginDatabases).bb.sdkis built in the worker with the same caller token and wrapper (plugin-sdk-wrapping.ts, moved out ofplugin-api.ts).onInstall,onDispose,whileWatched) except settingsonChangelisteners. For the awaited kinds, a worker callback that throws rejects the core-side proxy, which lands in the same catch and log line as in-process. Settings listeners are called synchronously and are fire-and-forget, so the worker wraps them: a synchronous throw comes back to core, which logs it through the same call-site message (callSettingsListeneris shared by both call sites). An async rejection stays unhandled in the worker and is reported as[plugin:<id>] uncaught error, as in-process. One deliberate difference: in-process, an uncaught error whose stack isn't the plugin's exits the server; in a worker, every uncaught error is contained.process.exit, OOM), core keeps running: the plugin is disposed and set toerror, then restarted with the background-service backoff.PUT /settings/experiments) reloads the affected plugins live.Why threads rather than processes:
forkThreads are also what makes synchronous
bb.*calls possible, because they share memory.What stays in-process. Before evaluating the plugin, the worker scans the server entry and the files it imports relatively for the APIs it can't host yet (
plugin-server-worker-preflight.ts). If it finds one, the plugin loads in-process and its setup runs only once, there. bb logs which API kept it in-process. That covers agent tools, configuration and instructions, providers, mention providers,experimental_hooks.on, environment, machine, server-access and AI-service providers, WebSocket routes,hosts.experimental_client, tunnel APIs, and settings withexperimental_schema. These all need core to call synchronously into the plugin or to hold the plugin's schemas. Bundled first-party plugins are never isolated, because they rely on those in-process core objects. The scan matches member names, so it also catches destructured and aliased access. It is conservative: a match keeps the plugin in-process, as today. Only a computed member name the scan can't see, such asbb.agents["register" + "Tool"], reaches the runtime backstop. In that case the worker's partial setup is thrown away and setup runs again in-process. That second run isn't side-effect free (writes the partial setup already made, such as database migrations or KV writes, stay), which is why the scan runs first. A plugin that hits one of these APIs only after it loads is reloaded in-process.Sandboxing. Worker isolation doesn't sandbox the filesystem, network or child processes; a worker plugin has the same access as an in-process one today. The usage plugin, for example, writes a scan cache under the host's
~/.cache/bb-plugin-usage/. All benchmark and end-to-end runs used a host daemon withHOMEpointed at APFS clones inside the dev data dir.Other changes:
@bb/db/sqlite-maintenanceis a new drizzle-free module holding the raw SQLite vacuum helpers, so the worker bundle doesn't load the schema.dist/plugin-server-worker.js.docs/configuration.md.bb guide customizationandbb guide plugins.There is no daemon wire change.
How you verified
New tests in
apps/server/test/services/plugins/plugin-server-isolation.test.ts(11, passing), all using real git-sourced installs on a real HTTP server:threadId.process.exitin a worker: the plugin restarts on a new thread.routePath, variables, query, headers, body) has the same shape in a worker as in-process.onInstall,onDispose, service crash, CLI, RPC.Existing plugin, route, internal, public and catalog suites (155 files):
globalThisorvi.stubGlobalfrom the test thread, which a worker can't share by design:prebuilt-server-loading×2,plugin-native-server-loading, and theplugin-updatestabilization-crash test. The fifth,builtin-pluginshot-reload, involves a builtin (never isolated) and also flaked with the experiment off.Checks:
turbo run typecheck lintfor server, db, domain, app and templates, plus the db and domain test suites.End-to-end with the real usage plugin (v0.3.27, installed from its git URL into a
start:worktreeinstance on an empty data dir): the host daemon ran withHOMEset to APFS clones of the session history inside that data dir. Core main-thread event loop and CPU were measured with an injected probe (5 ms timer lateness,process.threadCpuUsage()). Runs were interleaved A/B on a machine with load average 5–12.dashboardRPC: stall timedashboard: max delaydashboard: core CPUdashboardlatency p50Official plugins with the experiment on. connect-next, github-pr-status, prompt-library and storage-retention are always installed as
builtin(bb-official lists them as bundled), so isolation never applies to them. A git install with the same id is refused while the builtin exists, and a git install from source fails on theirworkspace:*dependencies. To exercise their real server code anyway, I installed their prebuilt bundles as standalone git repos under renamed ids in the dev instance:bb.hosts.experimental_client) and ran with their services up.bb.server.internal_DO_NOT_USE_openTunnelIngress) and ran with itstunnelservice up.star,search(with prompt history throughbb.sdk) and the CLIlist --jsonall worked.Scan wall time was unchanged (58–62 s in both modes). Bundle cost per worker, measured separately: 66 ms and ~23 MB heap, versus 12 ms and 6 MB for an empty worker. Most of that is the plugin SDK client and its schemas.
🤖 Generated with Claude Code