Skip to content

Stop overriding Codex's shell_environment_policy with the thread env - #8075

Merged
ymichael merged 3 commits into
get-bb:mainfrom
lanjak:fix-8008-codex-sandbox-env
Nov 24, 2026
Merged

ymichael merged 3 commits into
get-bb:mainfrom
lanjak:fix-8008-codex-sandbox-env

Conversation

@lanjak

@lanjak lanjak commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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 (Let nested bb servers use the parent's Account Pooler deliberately #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

The Codex bridge copied every thread env var, including plugin
contributions such as the Account Pooler token, into
shell_environment_policy.set.*. Codex applies set after inherit and
exclude, so an admin's policy could not keep those variables out of
sandboxed commands. Each bb thread has its own app-server process whose
environment is already the thread env, so Codex's own policy now decides
what commands get. bb still sets BB_THREAD_ID.
…e test

CI sets BB_TEST_DURATIONS_DIR, so the fake app-server logged it with the
test's BB_TEST_ variables and the launch env assertion failed.
Removing the thread-env copy from shell_environment_policy.set left only
BB_THREAD_ID forced, so inherit = "core" or an exclude matching BB_* dropped
BB_SERVER_URL and BB_CLI and broke the bb CLI inside Codex commands. Force
bb's non-secret context variables (BB_PROJECT_ID, BB_ENVIRONMENT_ID,
BB_THREAD_STORAGE, BB_CLI, BB_SERVER_URL, BB_HOST_DAEMON_PORT, BB_THREAD_ID)
and leave every other variable, including tokens and project variables, to
Codex's policy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 511e1ed into get-bb:main Nov 24, 2026
64 of 65 checks passed
ymichael added a commit that referenced this pull request Nov 25, 2026
…ess start speed (#8147)

## Human comments

## What was wrong

Two tests in
`packages/agent-runtime/src/runtime.process-lifecycle.test.ts` fail at
random in `Tests (packages-host)` on loaded runners. Both depend on how
fast processes start under CPU load.

**`drains provider stderr before reporting process exit`** fails with
`expected null to be 'stderr-after-exit'`, for example on #8075's merge
run
([job](https://github.com/get-bb/bb/actions/runs/45507748534/job/138847431154),
2286 ms). The fake provider spawned a grandchild that inherits stderr,
then exited at once. The grandchild had to boot Node and wait 50 ms
before writing. After a provider exits, the runtime waits at most
`PROVIDER_PROCESS_CLOSE_GRACE_MS` (1 s) for stderr to close. On a loaded
runner, Node startup alone can take longer than that.

**`does not wait for stalled replacement initialization during full
shutdown`** times out at 15 s, followed by an unhandled `Provider "fake"
failed to start: spawn .../node ENOENT`
([job](https://github.com/get-bb/bb/actions/runs/45506198560/job/138847994142);
it failed on all three attempts of that branch, and took 12.1 s on
#8076's passing run). Before reaching its stalled replacement, the test
started and retired a full scripted-echo bridge. #8141 measured a full
start at 3.7–4.6 s under load, and stopping one can take up to 5 s plus
kill and stderr grace periods. On other PRs' loaded runs the test took
3–12 s. When it overran, Vitest timed the test out but its body kept
running. `afterEach` removed `tmpDir`, and the replacement then spawned
into the missing cwd, which Node reports as `spawn <node> ENOENT`. The
ENOENT is a consequence of the timeout, not its cause. #8136 and #8141
fixed the sibling retirement tests the same way; this one was left.

## What changed

`runtime.process-lifecycle.test.ts` only. No runtime code changes.

- **stderr drain:** the grandchild reports ready over IPC, the provider
exits only after that, and the grandchild writes to stderr 50 ms after
the IPC channel disconnects, which happens when the provider exits. The
write still lands after the exit, but Node startup no longer counts
toward the 1 s grace period.
- **stalled shutdown:** the first process is the idle child
(`process.stdin.resume()`, no post-initialize requests) that the sibling
tests use. The adapter it uses is pulled out into a `createIdleAdapter`
helper that `createIdleProviderManager` now shares. The replacement
still uses the real fake adapter on the raw stalled script, so it
receives `initialize` and holds its reply for 3 s, which is the behavior
under test.

## How you verified

CPU starvation locally: the test runs at background QoS (`taskpolicy
-b`) next to 32 busy-loop processes on a 16-core Mac. This reproduces
both CI failures.

| Test | Before | After |
|---|---|---|
| stderr drain | 2 CI-assertion failures before I stopped it | 6 of 6
pass |
| stalled shutdown | 2 of 4 time out (18.9 s, 21.4 s); passes took 13.1
s and 2.3 s | 4 of 4 pass in 198–358 ms |

Both tests still catch the bugs they guard:

- stderr drain: with `PROVIDER_PROCESS_CLOSE_GRACE_MS` temporarily set
to 0, the new test fails 3 of 3.
- stalled shutdown: with `shutdown()` temporarily changed to await
in-flight provider starts, the new test fails.

`pnpm exec turbo run typecheck lint test --filter=@bb/agent-runtime`:
pass (316 tests, 1 skipped).

> AGENT GENERATED

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
alexburlis added a commit to alexburlis/omnyo that referenced this pull request Nov 26, 2026
Record the upstream PRs, revisions, contributors, and Omnyo integration
commits for merge d2a24ac in docs/upstream-credits.md.

Original-PR: get-bb/bb#7663
Original-Commit: get-bb/bb@1737a03
Original-Author: rufi
Original-PR: get-bb/bb#8055
Original-Commit: get-bb/bb@651ae20
Original-Author: Jake Lancaster
Original-PR: get-bb/bb#8075
Original-Commit: get-bb/bb@511e1ed
Original-Author: Jake Lancaster
Original-Co-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8096
Original-Commit: get-bb/bb@775090b
Original-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8145
Original-Commit: get-bb/bb@8de6711
Original-Commit: get-bb/bb@0d3db49
Original-Commit: get-bb/bb@92d3a76
Original-Commit: get-bb/bb@6705422
Original-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8171
Original-Commit: get-bb/bb@b56ed04
Original-Author: Jake Lancaster
Original-PR: get-bb/bb#8238
Original-Commit: get-bb/bb@2a9a142
Original-Author: SawyerHood
Original-PR: get-bb/bb#8247
Original-Commit: get-bb/bb@ba4ae22
Original-Author: Salem
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8259
Original-Commit: get-bb/bb@96f40a9
Original-Author: SawyerHood
Original-PR: get-bb/bb#8263
Original-Commit: get-bb/bb@141e40a
Original-Author: mk
Original-PR: get-bb/bb#8266
Original-Commit: get-bb/bb@84e20e9
Original-Author: ILIA GOLUBEV
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.

2 participants