Skip to content

Refuse plugin ids that name core's folders under plugins/ - #7887

Merged
ymichael merged 3 commits into
mainfrom
bb/reject-plugin-ids-that-collide-with-core-folders-thr_6nsuqsi98j
Nov 20, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/reject-plugin-ids-that-collide-with-core-folders-thr_6nsuqsi98j

Conversation

@ymichael

@ymichael ymichael commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

derivePluginId was the only check on plugin ids, and it only checked the format. Core keeps four folders under <dataDir>/plugins/ (npm, cache, runtime, snapshots), each named with a string literal in its own file, and nothing linked those names to the id rules. So a package named bb-plugin-npm installed as plugin npm, and its data.db, logs/ and secrets/ went into core's npm package store. bb-plugin-runtime likewise wrote its files next to core's server build cache in plugins/runtime/server.

What changed

  • packages/domain/src/plugin-id.ts: added CORE_PLUGIN_DIRECTORIES (npm, cache, runtime, snapshots). derivePluginId now throws for those ids: package name "bb-plugin-npm" gives plugin id "npm", which bb reserves for its own folders under plugins/; rename the package. Every install source (path, git, npm, catalog), manifest load, bb plugin new and plugin build goes through derivePluginId, so they all refuse these ids.
  • Core's folder code builds its paths from that list instead of string literals: install-sources.ts, managed-plugin-artifacts.ts, mutable-plugin-artifacts.ts, plugin-server-cache.ts, plugin-artifact-gc.ts, plugin-state-snapshot.ts.
  • A plugin already installed under one of these ids shows as error with the reason after upgrade. The server keeps running, and bb plugin remove still works.
  • Docs: the plugin-id section of the bb-plugin-authoring skill's quickstart.md lists the reserved ids.
  • pluginIdSchema is unchanged on purpose. Stored turn events (originPluginId) and queued-message wait rows are parsed with it, so tightening it could make existing rows unreadable.
  • No wire or SDK change.

Community impact: none. No package name in a 503-repo community plugin corpus (1711 names), or in the getbb.app marketplace v1 or v2 catalogs, derives one of these ids.

Server archives

  • packages/server-archive/src/server-owned-paths.ts kept its own copy of the list (npm, cache, snapshots) and was missing runtime, so archives treated plugins/runtime as a plugin and recorded plugins/runtime/server and plugins/runtime/host as separate plugin roots. It now builds the set from CORE_PLUGIN_DIRECTORIES, so plugins/runtime is one server-owned tree. The archived files are unchanged.
  • server-moved.json now records plugins/runtime as one root, which an older daemon's isServerOwnedPath rejects, so HOST_DAEMON_PROTOCOL_VERSION goes to 275. Markers an older bb wrote with the per-child roots still read (covered by a new markers.test.ts case).
  • apps/server/src/services/server-move/pending-boot.ts builds its snapshots path from the shared list too.
  • The toolchain- prefix is not reserved. Toolchain folders are named toolchain-<versions with dots>, and derivePluginId turns dots into dashes, so no plugin id can equal one.

How you verified

  • Live repro on origin/main with pnpm start:worktree and the source CLI. bb plugin install --yes path:.../bb-plugin-npm and path:.../bb-plugin-runtime both installed and ran, and they created plugins/npm/{data.db,logs/} and plugins/runtime/{data.db,logs/} next to runtime/server/<hash>.
  • Same steps on this branch:
    • Both path installs, and npm:@acme/[email protected], exit 1 with the reserved-id message.
    • bb plugin new snapshots is refused before writing anything.
    • No new plugin files are created under plugins/npm or plugins/runtime.
    • Restarting the server with the two plugins still installed shows each as error with the reason. The server stays up, and bb plugin remove removes them.
  • Tests (all 7 new server assertions fail when src is reverted to origin/main):
    • apps/server/test/helpers/core-plugin-folders.ts is a check for the whole bug class. After a real npm install (plugin-install.test.ts) and a real git update that writes a snapshot (plugin-update.test.ts), it asserts that no package name can derive any folder core created under plugins/. A future core folder added without updating the list also fails it.
    • plugin-install.test.ts: path installs of each reserved id are refused, with no registration and no logs folder. The npm source is refused before install.
    • packages/domain/test/plugin-id.test.ts: plain, scoped and uppercase names are refused for each reserved id, and near misses (npm-tools, my-cache, toolchain-0.28.1) still derive.
  • turbo run test typecheck lint for @bb/server-archive and @bb/host-daemon-contract, @bb/server test/server-move and @bb/host-daemon src/server-move suites; the new inventory assertion fails with the old archive list.
  • pnpm exec turbo run test --filter=@bb/domain --filter=@bb/cli --filter=@bb/templates --filter=@bb/plugin-build --filter=bb-plugin-bb-guide, both server test files, turbo run typecheck, lint and lint:type-aware for @bb/server, @bb/domain and bb-plugin-bb-guide, and oxfmt --check.

🤖 Generated with Claude Code

AGENT GENERATED

ymichael and others added 3 commits November 19, 2026 20:32
derivePluginId accepted "npm", "cache", "runtime" and "snapshots", so a
package such as bb-plugin-npm installed and wrote its data.db, logs/ and
secrets/ into the folders core keeps its npm package store, artifact
cache, server build cache and state snapshots in.

The names now live in one list, CORE_PLUGIN_DIRECTORIES in @bb/domain,
which core's folder code builds its paths from and derivePluginId checks,
so every install source, manifest load, scaffold and build refuses them.
An existing install with such an id loads as an error with the reason and
can still be removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…that-collide-with-core-folders-thr_6nsuqsi98j
server-archive kept its own list of core folders under plugins/ (npm,
cache, snapshots) and was missing runtime, so archives treated
plugins/runtime as a plugin and recorded its server/ and host/ children
as separate plugin roots. The list now comes from CORE_PLUGIN_DIRECTORIES,
so plugins/runtime is one server-owned tree. The archived files are the
same; server-moved.json now records plugins/runtime as one root, which an
older daemon rejects, hence the protocol bump to 275. Markers an older bb
wrote with plugins/runtime/server and plugins/runtime/host still read.

pending-boot.ts also builds its snapshots path from the shared list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit bbdce78 into main Nov 20, 2026
53 checks passed
@ymichael
ymichael deleted the bb/reject-plugin-ids-that-collide-with-core-folders-thr_6nsuqsi98j branch November 20, 2026 19:13
ymichael added a commit that referenced this pull request Nov 21, 2026
…irectory() (#7957)

## Human comments

## What was wrong

bb already gives every plugin `<dataDir>/plugins/<id>/`, but a plugin
had no way to get a path inside it for its own files. So plugins built
the path themselves from `bb.server.experimental_dataDir`, even though
that member's JSDoc said it was not a place to write. Account Pool wrote
its account credentials and machine tokens into core's `secrets/` folder
(`secrets/accounts`), next to core's secret-setting files. In a 503-repo
community corpus, 32 repos use `experimental_dataDir` and about 22 of
them write their own files under `plugins/<id>/`, two of them into
`secrets/` as well. Core built the same folder by hand in three more
places (`data.db` in `bb.storage.database()`, `logs/`, `secrets/`), plus
the update-snapshot code.

## What changed

- **New SDK member:** `bb.storage.experimental_directory(): string`
(`packages/plugin-sdk/src/backend-contract.ts`). It returns
`<dataDir>/plugins/<id>/files` and creates it with mode 0700 when it is
missing. It is a method, not a field, because it creates the folder, the
same way `database()` does.
- **One resolver:** `pluginDirectory(dataDir, id, part)`
(`apps/server/src/services/plugins/plugin-directory.ts`, part = `data.db
| logs | secrets | files`). It replaces the hand-written joins in
`plugin-api.ts` (`database()`), `plugin-log.ts`, `plugin-settings.ts`,
`plugin-service.ts` (uninstall, HTTP token) and
`plugin-state-snapshot.ts`, and the new method uses it too. Plugin ids
that would collide with core's folders are already refused by
`derivePluginId` (#7887).
- **Lifetime:** uninstall deletes `files/` along with `secrets/`, as
Michael decided. `data.db` and `logs/` keep their current lifetime and
survive uninstall. Update snapshots now copy `files/`, and rollback
restores it, the same as `data.db` and `secrets/`. Without that, Account
Pool's credentials would go from rolled-back to not rolled-back just by
moving folders. A snapshot taken before this change has no `files/`
copy, so restoring it removes the folder: the plugin goes back to the
state it had then. No schema change; the snapshot's copy sits at
`<snapshotPath>/files`.
- **Snapshot copies are clones.** `files/` and `secrets/` are cloned
into the snapshot and back on rollback (`cloneTree` in
`plugin-state-snapshot.ts`). On macOS this runs `/bin/cp -cRp`, which
uses clonefile(2) and falls back to a plain copy on other filesystems.
Elsewhere it uses `fs.cp(..., { recursive: true, mode: COPYFILE_FICLONE
})`, which reflinks on btrfs/XFS and otherwise copies. `fs.cp` with
`COPYFILE_FICLONE` can't clone on macOS: Node 22.23 / libuv 1.51 clones
only through Linux's `FICLONE` ioctl, so on APFS it quietly copies every
byte (measured below), and `COPYFILE_FICLONE_FORCE` fails with `ENOSYS`.
- **`experimental_dataDir` JSDoc** now says read-only: use it to migrate
off files bb used to manage or to compare paths, and never write under
it.
- **Account Pool** keeps credentials and machine tokens in
`files/accounts`. On load it moves `secrets/accounts` there once
(`moveAccountSecretFiles` in `store.ts`). Each file is moved with
`rename`, so modes and contents don't change. Files with the same name
are replaced by the copy from `secrets/` (only an older Account Pool
writes there). Then the empty folder is removed. A second run finds
nothing to move. Its `bbPluginSdk` range is now `>=0.6.177`.
- **Fake plugin host** (`@get-bb/plugin-sdk/testing`) implements the
method as a `files` folder in its temp storage root.
- **Uninstall wording** now says "settings, secrets, stored files, and
schedules" in the CLI `plugin remove` help and pinned-update hint, the
app's remove/uninstall confirmation, the server's removal log lines and
`bb-guide-plugins.md`.
- **Docs:** `docs/api_to_audit.md` has a new
`bb.storage.experimental_directory()` entry and a rewritten
`experimental_dataDir` entry (callers, corpus count). The Plugin Guide
storage card gets a bullet (the member is on `PluginStorage`, which is
already in its `apiSymbols`). The bb-plugin-authoring skill covers it in
`backend-foundation.md` (`bb.storage`, `bb.server`) and `quickstart.md`
(on-disk state). `docs/configuration.md` gives Account Pool's new path.
- Plugin SDK 0.6.176 → 0.6.177. No wire change, no DB migration.

Community callers that can move to `bb.storage.experimental_directory()`
(not edited here): OXI-717/bb-plugins account-pool-balanced (request
journal), nfour/bb-plugins bb-plugin-performance (`metrics/`),
architawr/bb-plugin-diagram-fences (`engines/`),
ComicBit/bb-plugin-nudger (`exports/`), sajov/bb-plugins
bb-plugin-recall (`mirror/`, hardcoded id), g4f4r0/bb-plugins
voice-tools (writes into core's `secrets/`), smsunarto/bb-plugins
agent-instructions (catalog publish), patleeman/bb-plugins bb-plugin-ds4
(guesses `homedir()/.bb`).

## How you verified

Tests (each fails on origin/main):
- `plugins/account-pool/src/server.test.ts`, "moves account and
machine-token files out of secrets/accounts once and fails over across
them". It seeds 3 accounts and a machine token in `secrets/accounts`,
loads the plugin and then reloads it. It checks that the folder moved
with contents and modes (0600, dir 0700) unchanged and didn't change on
reload, that the old machine token still authenticates, and that a 429
on the first account fails over to the second. With Account Pool's old
path code: `promise resolved "undefined" instead of rejecting`
(`secrets/accounts` still there).
- `apps/server/test/services/plugins/plugin-settings-storage.test.ts`,
"creates the plugin's files directory, keeps it across reloads, and
deletes only it on remove". It uses a real path-installed plugin through
`createPluginService`.
- `apps/server/test/services/plugins/plugin-state-snapshot.test.ts`: the
restore test now changes and adds files in `files/` after the snapshot.
Without the snapshot change: `expected 'changed-file' to be
'original-file'`.
- Existing Account Pool tests now read secrets from `files/accounts`.

Live, with `pnpm start:worktree` (clean env), the source CLI and a fake
Anthropic upstream that returns 429 for `sk-one`:
1. On origin/main: enabled Account Pool, added API-key accounts
`sk-one`, `sk-two`, `sk-three`, and minted a machine token. All 4 files
were 0600 in `secrets/accounts`. A request failed over from `sk-one` to
`sk-two` (HTTP 200).
2. Stopped the server, checked out this branch, started it again on the
same data dir. `secrets/accounts` was gone. `files/accounts` held the
same 4 files, mode 0600, in a 0700 folder. The 3 account files were
byte-identical. The token file differed only in `lastUsedAt` (the main
server wrote it at shutdown; same value and `mintedAt`). `bb pool
status` listed 3 accounts and the machine token. The old token
authenticated, and the request went to `sk-two` (sk-one was still
remembered as exhausted).
3. Restarted again: every file byte-identical with the same modes.
4. Made the upstream also reject `sk-two`: the request failed over to
`sk-three` (HTTP 200).
5. Path plugin calling `bb.storage.experimental_directory()`:
`plugins/filer/files/note.txt` was created. `bb plugin remove filer`
deleted `files/` and kept `data.db` and `logs/`. `bb plugin remove
--help` shows the new wording.

Snapshot cost (macOS 27.2, APFS, on a dedicated APFS disk image holding
only this dev data dir, so `df` sees nothing else). A `git:` plugin from
a local repo tracking `main`, with a 498 MB `files/`: 4 × 100 MB random
files plus 5,000 × 20 KB files in 50 folders. Each run is one `bb plugin
update` after a new commit. "Peak extra disk" is the lowest free space
sampled every 50 ms during the update, compared with before it.

| Copy | `files/` | Update wall time | Peak extra disk | Extra disk
after |
| --- | --- | --- | --- | --- |
| this branch, `cp -cRp` | empty | 34.6 s | — | 0 MB |
| main-style `fs.cp` | 498 MB | 35.0 s, 32.8 s | 548 MB, 548 MB | 0 MB |
| this branch, `cp -cRp` | 498 MB | 35.8 s, 33.7 s | 1 MB, 1 MB | 0 MB |
| `fs.cp` + `COPYFILE_FICLONE` (first try) | 498 MB | 45.9 s, 42.1 s |
548 MB (sampled on the second run only) | 0 MB |

The copy step on its own, warm cache, 3 runs: `fs.cp` 2.14 / 1.96 / 2.06
s and +548 MB each time; `cp -cRp` 0.91 / 0.68 / 0.64 s and +1 MB.
Update wall time hardly moves because the 30 s activation stabilization
window dominates it. A rollback restores with the same clone, so it pays
the same again. btrfs/XFS reflinks were not measured; CI runs the Linux
`fs.cp` path in the snapshot tests.

Disk use across updates: a successful update deletes its snapshot as
soon as the new version passes the stabilization window
(`plugin-activation.ts`), so nothing builds up. That is why "after" is 0
in every row; the cost is the peak during the update. A rolled-back
update keeps its snapshot (status `restored`) until it is 7 days old
(`retainedUntil`) or the server restarts (the boot GC drops snapshots
from earlier processes unless a rollback is unfinished). There is no
per-plugin count cap. So on a filesystem without clones, every
rolled-back update adds one full copy of `files/` for up to 7 days. That
is an audit question in `docs/api_to_audit.md`; this PR adds no pruning.

Commands (output in thread storage):
- `pnpm exec turbo run typecheck lint` for `@bb/server`,
`@get-bb/plugin-sdk`, `bb-plugin-account-pool`, `@bb/app`, `@bb/cli`,
`@bb/templates`, `@bb/domain`, `bb-plugin-plugin-api-docs`,
`bb-plugin-bb-guide`
- `lint:type-aware` for `@bb/server`, `bb-plugin-account-pool`,
`@get-bb/plugin-sdk`
- `turbo run test` for `@get-bb/plugin-sdk`, `bb-plugin-account-pool`
(395), `@bb/cli`, `@bb/templates`, `@bb/domain`,
`bb-plugin-plugin-api-docs`, `bb-plugin-bb-guide`
- `@bb/server` `test/services/plugins` (624), `@bb/app`
`ToolsView.plugin-detail.test.tsx`
- Re-ran typecheck/lint and the Account Pool and server plugin suites
after rebasing on 4e45c9b.

Not done:
- Downgrade: if bb is downgraded after this change, Account Pool's
credentials stay in `files/accounts`. The older Account Pool reads
`secrets/accounts`, finds nothing, and the pool has accounts without
credentials until they are added again or the folder is moved back by
hand.
- Account Pool is a builtin, and `bb plugin remove account-pool` is
refused (HTTP 409), so the uninstall rule never deletes its credentials.
Disabling it keeps them, as before.
- Automations still builds its folder from the `data.db` path, and
core's legacy-automations export writes `plugins/automations/import`.
Both were left as they are because they aren't in this gap.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 <[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