Skip to content

Let nested bb servers use the parent's Account Pooler deliberately - #3654

Merged
ymichael merged 1 commit into
mainfrom
bb/trace-environment-variables-to-agents-thr_uazquq3zvk
Sep 14, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/trace-environment-variables-to-agents-thr_uazquq3zvk

Conversation

@ymichael

@ymichael ymichael commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A bb server started inside another bb server's thread — the normal way we dev on bb itself — already inherited the parent's Account Pooler routing, but only by accident of process inheritance. Contributed env lands in the agent's shell, toDevProcessEnv strips only three BB_ thread keys, turbo passes everything through (@bb/server#dev and @bb/host-daemon#dev both declare passThroughEnv: ["*"]), and the daemon's bridge spawn strips only BB_* and NODE_ENV — so the provider credentials survive into the nested instance.

That accident is provider-locked. experimental_contributeEnv is registered per provider and the server resolves only the thread's own provider, so a Codex thread propagates only CODEX_* and a Claude thread only ANTHROPIC_*; which half of the nested server works is decided by whichever provider happened to run pnpm dev. Verified live on a running nested instance: its host-daemon and its provider-claude-code bridge worker both carried the parent's CODEX_OPENAI_BASE_URL and CODEX_POOL_AUTH_TOKEN and no ANTHROPIC_*, because a Codex thread had launched it. The accident also hands the parent's unscoped hub token to every agent in the nested instance, and leaves nested threads invisible to recordRouted and unreachable by bypass.

What changed

Marker (plugins/account-pool/src/server.ts). The parent contributes a provider-neutral pair to both providers: BB_ACCOUNT_POOL_PARENT_URL (as a { serverPath }, so it stays correct on remote machines where agents reach the server through the daemon's local auth proxy) and BB_ACCOUNT_POOL_PARENT_TOKEN. The BB_ prefix is deliberate — contributed env is an explicit session override so it reaches the nested server, but sanitizeInheritedChildProcessEnv strips it at the bridge spawn, so it cannot propagate past one level without a deliberate re-contribution.

Proxy mode (src/hub.ts, src/parent-pool.ts). A nested instance reads the marker and defaults to proxying: it keeps its own hub and mints its own machine tokens, and Hub.forwardToParent forwards upstream with the parent's token, skipping account selection, quota, pacing and failover entirely. The parent's token never reaches nested agents, and the nested instance's own RoutingStore records and bypasses its own threads. The abort/release plumbing is shared with fetchUpstream via trackRequest rather than duplicated.

Availability route. New GET …/http/availability, authenticated by the hub token, returning { claude, codex } booleans from the same canServe gate the env contribution uses — that gate is now one function instead of the three inline copies it was before. A proxying child queries it and contributes routing only for providers the parent can serve, failing closed when the parent is unreachable. Booleans only: a hub token is unscoped, so every holder can read this.

Isolate mode. bb pool parent isolate contributes empty routing values, which override the inherited ones. Verified empty string is sufficient at both consumers, so this needs no unset capability: no HOST_DAEMON_PROTOCOL_VERSION bump and no new public plugin API. ENABLE_TOOL_SEARCH is deliberately left alone — it only matters behind a custom base URL, so blanking the base URL is enough and empty-boolean semantics stay out of it.

Default-enable (apps/server/src/services/plugins/builtin-registry.ts). account-pool is defaultEnabled: false, so without this a nested instance never loads the plugin and falls through to the silent inheritance. defaultEnabled is now marker-aware. It is a first-run seed (enabled: existing?.enabled ?? bundled.defaultEnabled), so fresh nested data dirs pick it up and already-booted instances need one manual flip.

Settings UI (app.tsx). The parent is a banner above everything else, since it decides whether the rest of the page applies at all. While proxying, the remainder is dimmed and inert — accurate, because Hub.handle forwards every request to the parent in that mode, so local accounts go unused regardless of provider. The error alert and dialogs stay outside the inert region. Five states, all screenshotted against the running app:

State Heading Body
No parent (no banner) page unchanged from main
Isolate Parent Account Pooler available This server was started from a thread on <host>. Turn this on to send Claude and Codex requests to its pool instead of using the accounts below.
Proxy, both Using the parent Account Pooler Claude and Codex requests are sent to the pool on <host>. Accounts on this server are not used while this is on.
Proxy, one Using the parent Account Pooler Claude requests are sent to the pool on <host>. Codex has no accounts there, so those requests fall back to their own credentials.
Proxy, none Using the parent Account Pooler <host> has no accounts available right now, so nothing is being sent there. Requests fall back to each provider's own credentials.

Parent-unreachable renders as the last row, since it fails closed to the same value. The banner shows the host, not the full /api/v1/plugins/account-pool/http path.

Surfaces. bb pool parent [proxy|isolate] plus parentMode in bb pool config; PLUGIN_OVERVIEW.md, the account-pool skill reference, and the two new env vars in docs/configuration.md.

Drive-by. httpUrlSchema in contracts.ts had a latent bug the new marker schema would have copied — .refine runs even when .url() fails, so new URL() threw a raw TypeError on malformed CLI or RPC input instead of returning a validation error. My own new test caught it. Both schemas now catch and return false. Happy to split this out if you'd rather.

How you verified

  • New src/parent-pool.test.ts: marker parsing rejects a malformed URL or token rather than half-configuring proxying; header rewriting drops inbound credentials; availability caches, and fails closed on unreachable / 401 / unexpected body.
  • New nested-proxy integration tests in src/server.test.ts: isolate, can't-serve and unreachable all neutralise; proxy contributes self-pointing routing plus the marker with a token that is not the parent's; forwarding presents the parent token and drops the inbound one; the child rejects traffic bearing the parent's token; the availability route is 401 without a hub token.
  • New banner tests in app.test.tsx covering all four copy branches plus the no-parent case, and asserting the API path is not leaked into the UI.
  • Regression pins for the inheritance chain, which nothing previously covered: sanitizeInheritedChildProcessEnv keeps the provider vars and strips the marker (@bb/process-utils); toDevProcessEnv passes the marker through (@bb/scripts); resolveAppServerLaunch drops pool args when an inherited value is neutralised with "" (bb-plugin-provider-codex). These exist because a plausible-sounding *_TOKEN hardening would otherwise silently break every nested instance.
  • turbo run typecheck lint --filter='...[origin/main]' — 78/78 green.
  • turbo run test — bb-plugin-account-pool 310, bb-plugin-provider-codex 287, @bb/scripts 133, @bb/process-utils 21, @bb/server 2677.
  • Manually driven in a real app: parent half confirmed end to end — a pnpm start:worktree server running this branch contributed the marker into a nested instance's environment, and its /availability answered that token with {"claude":true,"codex":true}. The five UI states above were captured from the running settings page.
  • Not verified: proxied request traffic actually traversing child → parent → upstream in a live pair. The forwarding path is covered by integration tests only.

Fixes #

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael force-pushed the bb/trace-environment-variables-to-agents-thr_uazquq3zvk branch from b736145 to f6f004e Compare September 14, 2026 20:29
A bb server started inside another bb server's thread already inherited
the parent's pooler routing, but only by accident of process
inheritance. Contributed env reaches the agent's shell, toDevProcessEnv
strips only three BB_ thread keys, turbo passes everything through, and
the daemon's bridge spawn strips only BB_* and NODE_ENV, so the
provider credentials survive into the nested instance.

That accident is provider-locked: contributeEnv is registered per
provider and the server resolves only the thread's own provider, so a
Codex thread propagates only CODEX_*, a Claude thread only ANTHROPIC_*.
Which half of the nested server works is decided by whichever provider
happened to launch it. It also hands the parent's unscoped hub token to
every agent in the nested instance, and leaves nested threads invisible
to recordRouted and unreachable by bypass.

The parent now contributes a provider-neutral marker
(BB_ACCOUNT_POOL_PARENT_URL as a serverPath, plus the machine token) to
both providers. A nested instance reads it, enables the pooler on first
run, and defaults to proxying: it runs its own hub with its own machine
tokens and forwards upstream with the parent's token, so that token
never reaches nested agents. It reads a new hub-token-authenticated
/availability route to learn which providers the parent can serve, and
contributes routing only for those, failing closed when the parent is
unreachable. bb pool parent isolate switches to contributing empty
routing values, which overrides the inherited ones.

The marker is BB_-prefixed deliberately: contributed env is an explicit
session override so it reaches the nested server, but
sanitizeInheritedChildProcessEnv strips it at the bridge spawn, so it
cannot propagate further without a deliberate re-contribution.

In settings the parent appears as a banner above everything else, since
it decides whether the rest of the page applies at all. While proxying,
the remainder of the page is dimmed and inert, which is accurate:
Hub.handle forwards every request to the parent in that mode, so local
accounts go unused regardless of provider. The banner names the
providers the parent can actually serve and says what falls back.

Also fixes a latent bug in httpUrlSchema that the new marker schema
would have copied: .refine runs even when .url() fails, so malformed
CLI or RPC input threw a raw TypeError instead of a validation error.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ymichael
ymichael force-pushed the bb/trace-environment-variables-to-agents-thr_uazquq3zvk branch from f6f004e to eb62b37 Compare September 14, 2026 21:44
@ymichael
ymichael merged commit 7dcefea into main Sep 14, 2026
16 checks passed
@ymichael
ymichael deleted the bb/trace-environment-variables-to-agents-thr_uazquq3zvk branch September 14, 2026 21:52
OXI-717 added a commit to OXI-717/bb that referenced this pull request Sep 17, 2026
Twenty commits from the 0.43.1 line: three opencode subscriptions and Cursor pooled
behind the hub, real quota for all of them, and an exhausted pool that refuses instead of
handing back a multi-day retry-after a coding agent obeys in silence.

Every conflict was inside plugins/account-pool, so the merge takes that plugin whole from
the branch that is actually running rather than reconciling two designs. Upstream's own
post-0.43.1 work on this plugin — the parent-pool layer from get-bb#3654 and the usage-source
split from get-bb#3449 — is therefore not carried over; adopting it belongs with the re-port
onto whatever app version supersedes 0.43.1, where the result can be exercised.

Verified: turbo typecheck and the plugin suite (14 files) on the merge result, whose
plugin tree is identical to the one serving six pooled subscriptions today.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
ymichael added a commit that referenced this pull request Nov 24, 2026
…8075)

## Human comments

## What was wrong

The Codex bridge copied every thread environment variable into
`shell_environment_policy.set.*` in the `thread/start` and
`thread/resume` config (`buildShellEnvironmentPolicyConfig` in
`plugins/provider-codex/src/session-params.ts`). This included plugin
contributions such as the Account Pooler's `CODEX_POOL_AUTH_TOKEN` and
`BB_ACCOUNT_POOL_PARENT_TOKEN`, and `GH_TOKEN`. Codex applies `set`
after `inherit` and `exclude`, so an admin's
`shell_environment_policy.exclude = ["*TOKEN*"]` could not keep these
values out of sandboxed commands (#8008, part 2). The `set` copy dates
from when one app-server served several bb threads (901aebd). Now
each bb thread starts its own app-server, and its process environment is
the thread environment (`spawnChildConnection`, also on every rebuild
from #6939). The copy therefore only overrode the user's or admin's
Codex policy.

## What changed

- `plugins/provider-codex/src/session-params.ts`: `buildCodexConfig` no
longer copies the thread environment into
`shell_environment_policy.set`. Codex's own `shell_environment_policy`
decides which variables from the app-server environment reach commands.
bb still forces its non-secret context variables through `set`
(`BB_THREAD_ID`, `BB_PROJECT_ID`, `BB_ENVIRONMENT_ID`,
`BB_THREAD_STORAGE`, `BB_CLI`, `BB_SERVER_URL`, `BB_HOST_DAEMON_PORT`),
so the `bb` CLI works under any policy. With Codex's default policy
(inherit all), commands get the same variables as before.
- `plugins/provider-codex/skills/codex-provider/SKILL.md`: documents how
the thread environment reaches Codex commands and how an `exclude`
filters it.
- Tests: `session-params.test.ts` asserts that only bb's context
variables appear in `shell_environment_policy`, and no tokens or project
variables. `bridge.session-signature.test.ts` now asserts that a rotated
or removed variable reaches the relaunched app-server's process
environment and not the session config. `fake-codex-app-server.mjs` logs
`CODEX_SIGNATURE_TEST_*` variables at launch for that assertion.
- Behavior change for strict Codex policies: with `inherit = "core"` or
`"none"`, or a matching `exclude`, commands no longer get `GH_TOKEN`,
plugin contributions or project variables, because bb no longer forces
them through `set`. bb's context variables above are still forced, so
`bb` CLI calls keep working. Codex's default policy is not affected.
- No wire change, no `HOST_DAEMON_PROTOCOL_VERSION` bump, no plugin API
change.
- Not changed, left for a maintainer decision:
- Part 1, `networkAccess: true` in `toWorkspaceWriteCodexSandboxPolicy`:
this has been there since the first Codex adapter (c7a5bd2), next to
`readOnly` with `networkAccess: false`. Codex's Linux sandbox with
network off also blocks loopback, so `bb` CLI calls from sandboxed
commands would fail (checked with `codex sandbox` and `curl` to
127.0.0.1: `000` with network off, `200` with network on). The
`workspaceWrite` schema requires `networkAccess`, so the only way to
defer to Codex is to stop sending a per-turn `sandboxPolicy`. Options:
(a) keep it and document that the workspace modes allow network; (b) add
a Codex provider setting, like Claude Code's `sandboxEnabled`, that
leaves the sandbox to Codex's config and `requirements.toml`; (c) turn
network off and route it through approval, like Claude's sandbox, which
allows loopback only.
- With Codex's default policy, sandboxed commands still see the hub
token. `BB_ACCOUNT_POOL_PARENT_TOKEN` reaches agent commands on purpose
so that a nested bb server can use the parent's pool (#3654). Claude
Code threads get it the same way, and `CODEX_POOL_AUTH_TOKEN` has the
same value. Whether routed threads should pass that token by default is
a product decision.
  - The login-shell PATH order note.

## How you verified

- The new `session-params.test.ts` and
`bridge.session-signature.test.ts` assertions fail on origin/main (2
failed, 80 passed) and pass with the change.
- End to end with Codex CLI 0.171.0: a real `codex app-server`, started
with bb's `resolveAppServerLaunch`, `buildCodexConfig` and
`toCodexPermissionSettings`, a scratch `CODEX_HOME`, a fake Responses
server as the hub, and a fake pool token. An accept-edits workspace turn
ran `env` in the workspace-write sandbox, and a write outside the
workspace was denied.
- With `exclude = ["*TOKEN*"]` in config.toml: origin/main showed
`BB_ACCOUNT_POOL_PARENT_TOKEN` and `CODEX_POOL_AUTH_TOKEN` in the
command environment. With this change the command environment has no
TOKEN variables.
- With Codex's default policy, both builds pass both variables (Codex
inherits all).
- In every run the hub received `x-bb-account-pool-token` on 2/2 model
requests, and `BB_THREAD_ID` and `PLUGIN_API_URL` reached the command.
- Live check on a `pnpm start:worktree` instance with Codex 0.171,
reading raw command output from the event log:
- Project `.codex/config.toml` with `exclude = ["*TOKEN*"]` and a
project variable `QA_FAKE_TOKEN`: main passes `QA_FAKE_TOKEN` to the
command; this branch does not, and `$BB_CLI status` works.
- `inherit = "core"`: commands get only the bb context variables (no
project variables or tokens), and `$BB_CLI status` works.
- `pnpm exec turbo run typecheck test lint
--filter=bb-plugin-provider-codex --force`: pass (625 tests).
- `pnpm exec turbo run test --filter=@bb/provider-parity --force`: pass
(80 tests).
- `apps/server/test/skills/shipped-skills-quality.test.ts`: pass.

Part of #8008. The network-access half and Codex permission profiles are
in #8145.

> AGENT GENERATED

---------

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