Skip to content

Keep the managed terminal's scoped server connection when the launcher delegates to BB_CLI - #8266

Merged
ymichael merged 3 commits into
get-bb:mainfrom
OXI-717:fix/launcher-scoped-env
Nov 30, 2026
Merged

ymichael merged 3 commits into
get-bb:mainfrom
OXI-717:fix/launcher-scoped-env

Conversation

@OXI-717

@OXI-717 OXI-717 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

On an enrolled machine, bb shells reach the server through the daemon's loopback machine-auth proxy: BB_SERVER_URL is http://127.0.0.1:<port> and the proxy adds the machine credential. runBbCli() resolved the installed machine configuration before handing off to BB_CLI, so it replaced that URL with the stored serverUrl and set BB_SERVER_HEADERS. The bb CLI does not send BB_SERVER_HEADERS, so the request reached bb connect without a credential and got 401. "$BB_CLI" in the same shell worked because nothing rewrote its environment. #8264

The first version of this PR kept the shell's connection only when thread context (BB_THREAD_ID, BB_ENVIRONMENT_ID, BB_THREAD_STORAGE) and a loopback URL were present. Agent shells have thread context, but terminals opened from the panel or with bb terminal, and automation scripts, do not, so they still got 401.

What changed

  • packages/bb-app/src/launcher.ts: new resolveBbCliRuntime(). When BB_CLI is set, the launcher is inside a bb terminal, agent shell, or automation script, so it hands off with the shell's own environment and does not read the stored server or headers. This is the same rule as the CLI's own BB_CLI re-exec (apps/cli/src/bb-cli-reexec.ts). Without BB_CLI, the launcher uses the installed machine configuration as before. The BB_CLI_REEXEC guard is unchanged, so a BB_CLI that points back at the launcher still ends after one hop.
  • Removed the thread-context and loopback checks and the hasThreadContextEnv export.
  • docs/configuration.md: the BB_SERVER_URL row describes the hand-off.

How you verified

  • Live repro on one Mac: a pnpm start:worktree server, a local gate in front of it that returns 401 unless x-bb-connect-machine is present (like bb connect), and a second daemon enrolled through bb-host-daemon with a config.json holding the gate URL and credential. In a real bb terminal on that machine I ran node packages/bb-app/dist/bb.js thread show <id> --json and "$BB_CLI" thread show <id> --json:

    Shell main first version of this PR this change
    Terminal (no thread context) HTTP 401 HTTP 401 works
    Agent shell shape (plus BB_THREAD_ID/BB_ENVIRONMENT_ID) HTTP 401 works works

    $BB_CLI worked in every case. The agent-shell row adds the thread variables that agent shells get to the terminal's environment; it is not a real agent run.

  • Outside bb (no BB_CLI, with or without a stray BB_SERVER_URL), the launcher still targets the configured server. A BB_CLI that points back at the launcher (the automation shape) ends after one hop and uses the shell's BB_SERVER_URL.

  • New bb-app test: with a stored serverUrl and credential, a shell without BB_CLI gets the stored server and headers, and a shell with BB_CLI hands the delegated CLI its own BB_SERVER_URL and BB_HOST_DAEMON_PORT and no headers. It fails when the BB_CLI check is removed.

  • turbo run typecheck test lint:type-aware --filter=bb-app --filter=@bb/config passes.

Separate pre-existing issue, not changed here: docs/configuration.md says the bundled bb CLI sends BB_SERVER_HEADERS, but apps/cli never reads it, so plain bb outside bb on a machine enrolled through bb connect also gets 401.

Fixes #8264

AGENT GENERATED

🤖 Generated with Claude Code

…r delegates to BB_CLI

runBbCli() applied the installed machine configuration to the delegated
CLI env: resolveServerUrl() prefers the configured serverUrl over the
inherited BB_SERVER_URL and applyManagedConfigEnv() injects configured
BB_SERVER_HEADERS. In a BB-managed terminal on an enrolled machine that
replaced the terminal's scoped loopback proxy connection, so plain bb
returned 401 while the BB_CLI executable succeeded. Preserve the
inherited scoped server connection when the terminal supplies thread
context, a safe loopback BB_SERVER_URL, and a BB_CLI override.

Fixes get-bb#8264
@OXI-717
OXI-717 marked this pull request as ready for review November 26, 2026 17:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T17:44:55.120863Z 84e20e9 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 84e20e9b7f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/configuration.md Outdated
| `BB_EXTERNAL_URL` | `bb-app env`, or environment | Startup-only | Internet-facing HTTPS base URL for generated public links; the fallback for `machineServerUrl`. |
| `BB_MARKETPLACE_URL` | `bb-app env`, or environment | Startup-only testing | Manifest URL of the reserved `bb-community` marketplace. Default `https://getbb.app/marketplace/v2/marketplace.json` (falls back to v1 on 404); a custom URL is requested without fallback. Changes only `bb-community`. |
| `BB_SERVER_URL` | `bb-app config` | Remote CLI/host use | Server targeted by the standalone `bb` CLI and `host-daemon` commands on this machine. Defaults to `http://127.0.0.1:38886`. It does not change where `npx bb-app` binds. |
| `BB_SERVER_URL` | `bb-app config` | Remote CLI/host use | Server targeted by the standalone `bb` CLI and `host-daemon` commands on this machine. Defaults to `http://127.0.0.1:38886`. It does not change where `npx bb-app` binds. Inside a BB-managed terminal that supplies a loopback machine auth proxy (thread context and `BB_CLI` set), the standalone `bb` launcher keeps the terminal's scoped `BB_SERVER_URL`/`BB_SERVER_HEADERS` instead of this stored value. |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add the precedence exception to the shipped CLI guide

When a user runs bb in a managed terminal, this introduces an exception to the stored server URL/header precedence, but the exception is documented only in docs/configuration.md. The shipped overview (packages/templates/src/templates/bb-guide-overview.md:21-29) does not describe it, so users and agents consulting the in-CLI manual cannot discover why their stored connection settings are bypassed. Root AGENTS.md requires user-facing configuration changes to update the discoverable surfaces listed in docs/cli-guide-and-skill.md; add a concise explanation to the overview template as well.

AGENTS.md reference: AGENTS.md:L27-L27

Useful? React with 👍 / 👎.

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
ymichael and others added 2 commits November 30, 2026 14:26
Inside bb terminals, agent shells, and automation scripts, BB_CLI is set
and the environment already carries the right BB_SERVER_URL. The launcher
now hands off with that environment instead of the machine's stored
server and headers, matching the CLI's own BB_CLI re-exec. This replaces
the thread-context and loopback checks, which missed ordinary terminals.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit f7232ea into get-bb:main Nov 30, 2026
51 checks passed
ymichael added a commit that referenced this pull request Dec 1, 2026
## Human comments

## What was wrong

On a machine enrolled through bb connect, running plain `bb` in an
ordinary shell (outside a bb terminal or agent shell) returned HTTP 401.
The `bb` launcher reads the machine's `config.json`, sets
`BB_SERVER_URL` to the stored server, and passes the machine credential
to the CLI in `BB_SERVER_HEADERS` (`packages/bb-app/src/launcher.ts`,
`applyManagedConfigEnv`). Nothing in `apps/cli` read
`BB_SERVER_HEADERS`, so requests reached the bb connect gate without
`x-bb-connect-machine`. `docs/configuration.md` already said the bundled
CLI sends these headers. bb terminals and agent shells were not affected
because their requests go through the daemon's loopback helper, which
adds the credential itself. Found while reviewing #8266, which fixed
only that managed path.

## What changed

- `packages/config/src/cli.ts`: new `loadCliServerHeaders()` parses
`BB_SERVER_HEADERS` with the existing `BB_SERVER_HEADERS_ENV`
definition. The default is `{}`, and a malformed value throws instead of
silently dropping the credential.
- `apps/cli/src/client.ts`: `cliFetch` is the single function every CLI
server request goes through, both SDK calls and the raw `status` and
plugin-proxy fetches. It now starts from those headers, and request
headers set by the caller take precedence.

No wire changes, so no `HOST_DAEMON_PROTOCOL_VERSION` bump. No CLI,
guide or doc changes: the existing `docs/configuration.md` text is now
accurate.

## How you verified

- **Live repro:** I built `bb-app` with `turbo run build
--filter=bb-app` and put a local gate in front of a real server. The
gate returns 401 unless `x-bb-connect-machine` matches, the way bb
connect does. I ran `node packages/bb-app/dist/bb.js thread list` under
`env -i` with `BB_DATA_DIR` pointing at a `config.json` holding
`serverUrl` (the gate) and `machineCredential`.
- Before the fix: `HTTP 401: sign in required`, and the gate logged `401
GET /api/v1/threads…`.
- After the fix: exit 0 with threads listed. `bb status`
(`/system/config`, `/plugins`, `/plugins/contributions`) and a plugin
command (`POST /plugins/bb--git/cli`) all passed the gate.
- **Tests:** in `apps/cli/src/__tests__/client.test.ts`, SDK requests
and direct `cliFetch` requests must carry `BB_SERVER_HEADERS`, and a
malformed value must throw. All three fail without the fix and pass with
it.
- `pnpm exec turbo run test typecheck --filter=@bb/cli
--filter=@bb/config` passed (CLI 1460 tests, config 101).

> AGENT GENERATED

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

Co-authored-by: Claude Opus 5.5 <[email protected]>
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…r delegates to BB_CLI (get-bb#8266)

## Human comments

## What was wrong

On an enrolled machine, bb shells reach the server through the daemon's
loopback machine-auth proxy: `BB_SERVER_URL` is
`http://127.0.0.1:<port>` and the proxy adds the machine credential.
`runBbCli()` resolved the installed machine configuration before handing
off to `BB_CLI`, so it replaced that URL with the stored `serverUrl` and
set `BB_SERVER_HEADERS`. The `bb` CLI does not send `BB_SERVER_HEADERS`,
so the request reached bb connect without a credential and got 401.
`"$BB_CLI"` in the same shell worked because nothing rewrote its
environment. get-bb#8264

The first version of this PR kept the shell's connection only when
thread context (`BB_THREAD_ID`, `BB_ENVIRONMENT_ID`,
`BB_THREAD_STORAGE`) and a loopback URL were present. Agent shells have
thread context, but terminals opened from the panel or with `bb
terminal`, and automation scripts, do not, so they still got 401.

## What changed

- `packages/bb-app/src/launcher.ts`: new `resolveBbCliRuntime()`. When
`BB_CLI` is set, the launcher is inside a bb terminal, agent shell, or
automation script, so it hands off with the shell's own environment and
does not read the stored server or headers. This is the same rule as the
CLI's own `BB_CLI` re-exec (`apps/cli/src/bb-cli-reexec.ts`). Without
`BB_CLI`, the launcher uses the installed machine configuration as
before. The `BB_CLI_REEXEC` guard is unchanged, so a `BB_CLI` that
points back at the launcher still ends after one hop.
- Removed the thread-context and loopback checks and the
`hasThreadContextEnv` export.
- `docs/configuration.md`: the `BB_SERVER_URL` row describes the
hand-off.

## How you verified

- Live repro on one Mac: a `pnpm start:worktree` server, a local gate in
front of it that returns 401 unless `x-bb-connect-machine` is present
(like bb connect), and a second daemon enrolled through `bb-host-daemon`
with a `config.json` holding the gate URL and credential. In a real `bb
terminal` on that machine I ran `node packages/bb-app/dist/bb.js thread
show <id> --json` and `"$BB_CLI" thread show <id> --json`:

  | Shell | main | first version of this PR | this change |
  |---|---|---|---|
  | Terminal (no thread context) | HTTP 401 | HTTP 401 | works |
| Agent shell shape (plus `BB_THREAD_ID`/`BB_ENVIRONMENT_ID`) | HTTP 401
| works | works |

`$BB_CLI` worked in every case. The agent-shell row adds the thread
variables that agent shells get to the terminal's environment; it is not
a real agent run.
- Outside bb (no `BB_CLI`, with or without a stray `BB_SERVER_URL`), the
launcher still targets the configured server. A `BB_CLI` that points
back at the launcher (the automation shape) ends after one hop and uses
the shell's `BB_SERVER_URL`.
- New bb-app test: with a stored `serverUrl` and credential, a shell
without `BB_CLI` gets the stored server and headers, and a shell with
`BB_CLI` hands the delegated CLI its own `BB_SERVER_URL` and
`BB_HOST_DAEMON_PORT` and no headers. It fails when the `BB_CLI` check
is removed.
- `turbo run typecheck test lint:type-aware --filter=bb-app
--filter=@bb/config` passes.

Separate pre-existing issue, not changed here: `docs/configuration.md`
says the bundled `bb` CLI sends `BB_SERVER_HEADERS`, but `apps/cli`
never reads it, so plain `bb` outside bb on a machine enrolled through
bb connect also gets 401.

Fixes get-bb#8264

> AGENT GENERATED

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

---------

Co-authored-by: Michael Yong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
## Human comments

## What was wrong

On a machine enrolled through bb connect, running plain `bb` in an
ordinary shell (outside a bb terminal or agent shell) returned HTTP 401.
The `bb` launcher reads the machine's `config.json`, sets
`BB_SERVER_URL` to the stored server, and passes the machine credential
to the CLI in `BB_SERVER_HEADERS` (`packages/bb-app/src/launcher.ts`,
`applyManagedConfigEnv`). Nothing in `apps/cli` read
`BB_SERVER_HEADERS`, so requests reached the bb connect gate without
`x-bb-connect-machine`. `docs/configuration.md` already said the bundled
CLI sends these headers. bb terminals and agent shells were not affected
because their requests go through the daemon's loopback helper, which
adds the credential itself. Found while reviewing get-bb#8266, which fixed
only that managed path.

## What changed

- `packages/config/src/cli.ts`: new `loadCliServerHeaders()` parses
`BB_SERVER_HEADERS` with the existing `BB_SERVER_HEADERS_ENV`
definition. The default is `{}`, and a malformed value throws instead of
silently dropping the credential.
- `apps/cli/src/client.ts`: `cliFetch` is the single function every CLI
server request goes through, both SDK calls and the raw `status` and
plugin-proxy fetches. It now starts from those headers, and request
headers set by the caller take precedence.

No wire changes, so no `HOST_DAEMON_PROTOCOL_VERSION` bump. No CLI,
guide or doc changes: the existing `docs/configuration.md` text is now
accurate.

## How you verified

- **Live repro:** I built `bb-app` with `turbo run build
--filter=bb-app` and put a local gate in front of a real server. The
gate returns 401 unless `x-bb-connect-machine` matches, the way bb
connect does. I ran `node packages/bb-app/dist/bb.js thread list` under
`env -i` with `BB_DATA_DIR` pointing at a `config.json` holding
`serverUrl` (the gate) and `machineCredential`.
- Before the fix: `HTTP 401: sign in required`, and the gate logged `401
GET /api/v1/threads…`.
- After the fix: exit 0 with threads listed. `bb status`
(`/system/config`, `/plugins`, `/plugins/contributions`) and a plugin
command (`POST /plugins/bb--git/cli`) all passed the gate.
- **Tests:** in `apps/cli/src/__tests__/client.test.ts`, SDK requests
and direct `cliFetch` requests must carry `BB_SERVER_HEADERS`, and a
malformed value must throw. All three fail without the fix and pass with
it.
- `pnpm exec turbo run test typecheck --filter=@bb/cli
--filter=@bb/config` passed (CLI 1460 tests, config 101).

> AGENT GENERATED

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

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.

Enrolled machine launcher overrides managed terminal CLI routing and returns 401

2 participants