Skip to content

Add the bb account plugin and move connect onto it - #4229

Merged
SawyerHood merged 8 commits into
bb/account-stack-2-ai-servicesfrom
bb/account-stack-3-bb-account
Sep 24, 2026
Merged

SawyerHood merged 8 commits into
bb/account-stack-2-ai-servicesfrom
bb/account-stack-3-bb-account

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

getbb.app sign-in lived inside the Connect plugin. The pairing code, the stored credential, and the tunnel dial were all Connect's, so no other plugin could use the bb account. A hosted service such as bb cloud AI (the top PR) would have had to duplicate or reach into Connect's credential.

Stacked on #4228 (AI services). It needs the hosted endpoints from #4227.

What changed

  • New built-in bb-account plugin. It owns the server's getbb.app credential and never returns it to callers.
    • Its discoverable RPC contract, bb-account.v1, has four methods:
      • status;
      • waitForStatusChange, a long poll keyed by a revision;
      • fetch, which sends the credential to the fixed getbb.app origin, only for /api/ paths, with 1 MB caps and no redirects;
      • adoptConnectCredential.
    • Sign-in:
      • a browser device-code link through /link;
      • a pasted dashboard pairing code;
      • an upstream 401 signs the server out everywhere.
    • CLI: bb account status | login [--wait] [--code <code>] [--base-url <url>] | logout.
    • A Settings page with the account, sign-in dialog, "Have a pairing code?", and sign-out.
  • Connect moves onto bb account.
    • It dials the gate with a fresh five-minute ticket from /api/connect/tunnel-ticket for every connection.
    • It tears the tunnel down on sign-out.
    • It adopts an existing Connect credential on upgrade and deletes Connect's copy, so paired servers stay paired.
    • bb connect off and bb connect on stop and restart the tunnel while the server stays signed in.
    • bb connect --code still works, as an alias of bb account login --code.
    • Signed out, the Remote access page shows an inline "Sign in to your bb account" card and the pairing-code form.
    • Machine access and shares use bb account's fetch. The machine-code lookup moves to /api/connect/machine-code-lookup.
  • Server import hold. The hold after bb server import now covers bb account as well as Connect, so a copied server can't use the original's account or tunnel. bb server allow-connect lifts both.
  • pnpm dev --staging. A source bb can sign in, open tunnels, and call the gateway on the staging Cloud (https://vibecodethis.site). bb account and Connect accept that exact origin, or a bb.localhost one, only in development builds. As with local Cloud, the tunnel serves the Vite dev app.
  • Docs, the guide, the CLI skill, and verify-bb feature maps are updated.

How you verified

  • turbo run typecheck lint test for bb-plugin-bb-account, bb-plugin-connect, @bb/server, @bb/cli, and @bb/scripts.
    • The new bb-account tests cover code and link sign-in, denial, expiry, slow-down, cancellation, the fetch allowlist and caps, sign-out on upstream 401, revision waits, legacy adoption, and the CLI and settings UI.
    • The Connect integration tests cover ticket dials, a fresh ticket per reconnect, legacy adoption, sign-out teardown, connect off and on, and connect --code.
    • Trunk's "reacquires fresh access for the same host after release" test is ported to the new harness.
  • Old-client compatibility, end to end against the new hosted code:
    • An unmodified server from main: it paired with the old redeem, tunneled with its raw credential (101), listed servers, issued and redeemed machine codes, dialed a machine credential, shared a port, and loaded its app through the tunnel.
    • Upgrade: copying that server's data to this build adopted the credential, removed Connect's copy, kept the shares, and reconnected the tunnel with tickets.
  • New-client flow against local Cloud: link sign-in with a handle claim, the tunnel, sign-out, and code sign-in.

AGENT GENERATED

🤖 Generated with Claude Code

@SawyerHood
SawyerHood added this pull request to stack #4231 September 23, 2026 22:32
@SawyerHood
SawyerHood force-pushed the bb/account-stack-3-bb-account branch from fdb87d6 to 3874e0f Compare September 23, 2026 22:58
@SawyerHood
SawyerHood force-pushed the bb/account-stack-3-bb-account branch from 3874e0f to 46b75a7 Compare September 23, 2026 23:04
@SawyerHood
SawyerHood force-pushed the bb/account-stack-3-bb-account branch 2 times, most recently from 0e5d1c9 to cf39093 Compare September 24, 2026 00:22
SawyerHood and others added 8 commits September 23, 2026 17:32
A new built-in bb-account plugin owns the server's getbb.app sign-in
(browser link or pairing code) and exposes status, a status long poll,
and a credential-holding fetch to other plugins over RPC. Connect dials
with short-lived tunnel tickets through it, adopts an existing Connect
credential on upgrade, and gains `bb connect off` and `on`. The server
import hold covers bb account, and `pnpm dev --staging` points a source
bb at the staging Cloud.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`pnpm dev --staging` pairs with a https://*.vibecodethis.site server URL,
so the Connect tunnel sent visitors to the API server, which only answers
"bb server" at /. Route that pairing to the Vite dev app the way a
local-Cloud pairing already is; BB_DEV_APP_PORT is set only by pnpm dev.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Plugin-to-plugin rpc goes through the same loopback route as the app and
the CLI, so a handler could not tell connect from an agent running
`bb plugin rpc call`. bb account needs that to keep connect's endpoints
and credential adoption away from other callers.

Each plugin load now gets an unguessable caller token that only server
memory holds. The plugin's bb.sdk.plugins.callRpc attaches it, the rpc
route verifies it (403 for any other token), and handlers receive
`context.experimental_caller` as their second argument: the calling
plugin, or `client` for everyone else. The token is revoked when the
load is disposed or replaced. `register` takes the new
ExperimentalPluginRpcHandlersWithContext map; PluginRpcHandlers keeps its
one-argument signature so plugins that call their own handlers still
compile. The fake plugin host can set the caller. SDK 0.5.24.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Review of the bb account stack found that the credential could be used,
dropped, or overwritten in ways the user never asked for:

- bb-account.v1.fetch reached any /api/ path for any caller, including
  agents through `bb plugin rpc call`. It now serves /api/ai/ to anyone
  and /api/connect/ only to the connect plugin (by rpc caller identity),
  adoption is connect-only, and callers can set timeoutMs (1-15 s).
- Any 401 through fetch signed the server out, so a typo'd path did
  too. A 401 is now confirmed with /api/account/me first.
- Sign-in races: cancelling or signing out while a sign-in finished
  still signed in, a stale completion could overwrite a newer one, and
  concurrent starts orphaned a poll loop. Every sign-in carries an
  attempt checked inside the serialized write; starts are serialized,
  a new start or sign-out cancels the pending link, and a credential
  from a sign-in that lost is revoked.
- A stored credential without a loaded profile read as signed out. It
  is now `profile-pending`, with its own CLI and settings copy.
- Sign-out now reports a failed revocation, signing in to a different
  server revokes the previous one, and a legacy connect pairing that
  isn't adopted is revoked before connect forgets it.
- --base-url/baseUrl (and so connect's --server) accept only getbb.app
  and vibecodethis.site, plus bb.localhost in development builds.
- adoptConnectCredential no longer takes the serverUrl/handle it ignored.
- Connect retries a tunnel ticket with backoff when bb account is
  briefly unavailable instead of staying "reconnecting" forever, and
  its --code surfaces explain profile and pairing errors.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
docs/configuration.md still said connect stores its own credential and
that `bb connect off` clears the pairing. Rewrite the section around bb
account, tunnel tickets, `bb connect on|off`, and the remoteAccess
setting, and document the new base URL allowlist, the pending-profile
state, and the sign-out revocation result in the guide and QA maps.
`bb connect --code` is no longer called a plain alias because it also
turns remote access back on, and run-dev's argument error now mentions
--staging.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e network

A 429 from link/start and any other getbb.app error response both surfaced
as "couldn't reach getbb.app". They now map to rate_limited and unavailable
in the rpc error codes, the CLI, the bb account settings, and Remote access.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
backend-events.md grew to 503 lines, past the shipped-skills limit of 500.
Say the same thing about experimental_caller in fewer lines.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@SawyerHood
SawyerHood force-pushed the bb/account-stack-3-bb-account branch from cf39093 to add0ebf Compare September 24, 2026 00:33
@SawyerHood
SawyerHood marked this pull request as ready for review September 24, 2026 00:55
@SawyerHood
SawyerHood merged commit f706e54 into main Sep 24, 2026
23 of 34 checks passed
@SawyerHood
SawyerHood deleted the bb/account-stack-3-bb-account branch September 24, 2026 01:06
SawyerHood added a commit that referenced this pull request Sep 24, 2026
…unts (#4230)

## Human comments

## What was wrong

Thread titles, branch names, and commit messages only came from a model
when the user had the Codex CLI signed in. Everyone else got the start
of the prompt as a title and `bb: automated commit` as the commit
message.

Stacked on #4229 (bb account). It uses the gateway from #4227 and the AI
services API from #4228.

## What changed

- **New built-in `bb-ai` plugin ("bb cloud AI").**
- It registers the AI service `{ id: "bb", displayName: "bb cloud",
complete, status }`.
- `complete` posts to `/api/ai/v1/complete` through bb account's
`fetch`, so the plugin never holds a credential.
- `status` is ready while the server is signed in and the day's budget
remains. A 402 `budget_exhausted` marks it not ready until the next UTC
midnight, and Automatic moves on.
- **Automatic.** bb cloud joins the Automatic chain after Codex, so a
signed-in bb gets generated titles and commit messages with nothing else
set up.
- **Surfaces.**
  - CLI: `bb ai status` and `bb ai usage`.
- A settings section with readiness and today's usage against the 50¢
daily allowance.
- A skill, and the Plugin Guide's AI services card lists it as a
first-party example.
  - Voice stays with Codex for now.

## How you verified

- `turbo run typecheck lint test` for `bb-plugin-bb-ai` (10 tests),
`@bb/server` (builtin reconciliation and icons), and `@bb/scripts` (the
Plugin Guide first-party table).
- End to end against local Cloud with a real OpenRouter key and
`inception/mercury-2.5`: a thread title in 428 ms and a commit message
in 498 ms. A managed worktree got the branch
`bb/add-user-preference-dark-mode-toggle-…` from its generated title.
- Budget exhaustion: with the day's spend set to the limit in local D1,
an explicit bb cloud pick failed with "daily limit reached" and did not
fall through. `bb ai status` reported not ready, and `bb ai usage`
showed the day's spend at the limit.

> AGENT GENERATED

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 24, 2026
## Human comments

## What was wrong

The bb-connect gate deployed from #4227 (version `985dc695`, 2026-09-24
01:07 UTC) broke tunnels for every server, old releases included.
Tunnels opened, but the gate never answered their heartbeats: servers
logged `tunnel heartbeat missed` 60–80 s after every dial and redialed
forever. Across `*.getbb.app`, 503s went from a steady 9–10%
(00:00–01:05) to 25–32% (01:10–01:25) and peaked at 55–56%
(01:41–01:42); WebSocket upgrades roughly tripled from constant redials.
Rolling the gate back to `6f79c289` at 01:43 brought 503s back to 12–14%
within a minute. The root cause is not identified yet: the gate diff
only adds the ticket check before the tunnel is handed to the Durable
Object and does not touch heartbeats or routing.

`main` still carries that gate, so the next merge touching
`apps/connect/**`, `packages/connect-db/**`, or
`packages/tunnel-contract/**` would redeploy it. `main`'s bb clients
also dial with tunnel tickets that the rolled-back gate refuses, so
`main` can't be released as is.

## What changed

- Reverts #4230 (bb cloud AI), #4229 (bb account plugin, Connect on bb
account), and #4227 (account endpoints, tunnel tickets, AI gateway).
- **Keeps** `packages/connect-db` migrations 0006/0007, their snapshots
and journal entries, and `schema.ts`. Production D1 already has them,
and dropping them from the schema would make the next `db:generate` emit
a destructive migration.
- Keeps #4227's null-owner guards in `apps/web/src/server/api.ts`:
`connect_code.user_id` stays nullable, and production already has
ownerless `server-link` rows. Redeeming one as a pairing code or machine
code returns `invalid-code`.
- Keeps #4228 (AI services). `main` returns to its state right after
#4228 merged.

Rollout notes:
- Merging redeploys the web worker without `/api/account/*`, `/link`,
and `/api/connect/tunnel-ticket`. No released bb uses them.
- If `Deploy Connect` is enabled, merging redeploys a gate equivalent to
the live `6f79c289`.
- The deployed `bb-ai-gateway` worker is not removed by this PR; delete
it in Cloudflare if wanted.
- Any bb that ran `main` after #4229 had its Connect credential moved
into the bb-account plugin. On this code it starts signed out; pair it
again with `bb connect --code <code>`.

## How you verified

- `pnpm install --frozen-lockfile` passes with the reverted lockfile.
- `turbo run typecheck lint test` for `@bb/connect-db`, `@bb/web`,
`@bb/connect`, `bb-plugin-connect`, `@bb/bundled-plugins`,
`bb-plugin-plugin-api-docs`: pass.
- `turbo run typecheck lint --filter=@bb/server`: pass. Server
`test/services/plugins/builtin-plugins.test.ts` and `test/ai/*`: 95
tests pass.
- Production evidence for the incident and the recovery: Cloudflare zone
analytics (per-minute `edgeResponseStatus` for `*.getbb.app`), `wrangler
tail bb-connect`, and bee's server log. The bee log shows the pre-stack
build dropping every minute from 01:08:24.

> AGENT GENERATED

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

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 24, 2026
Re-lands #4229: the bb-account plugin owns this server's getbb.app
credential and sign-in, and connect gets tunnel tickets and hosted
requests through it. Changes from #4229, after the 2026-09-24 outage:

- Ticket fallback. When getbb.app can't mint a ticket, or the gate
  answers a ticket dial with 401 (the pre-ticket gate, or TUNNEL_TICKETS
  off), connect dials with the raw server credential and keeps doing so
  for 30 minutes. bb account hands the credential only to the connect
  plugin, through the private bb-account.v1.connectCredential RPC. A 403
  (server not paired) never falls back.
- Copy, don't move. Connect keeps the pairing under its old `credential`
  KV key, where builds from before bb account read it, and keeps it in
  step with bb account: it writes bb account's credential there, hands
  bb account a pairing an older build made after a downgrade, and
  removes its copy only after bb account signs out of that same pairing.
  Adoption never revokes or deletes a pairing. adoptConnectCredential
  now takes `replaces` and stores the offered credential only while bb
  account still holds what connect saw.
- The sign-in polls in the connect and bb account settings back off to
  30 s while getbb.app or the tunnel is unreachable, instead of polling
  every 2 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 24, 2026
Re-lands #4229: the bb-account plugin owns this server's getbb.app
credential and sign-in, and connect gets tunnel tickets and hosted
requests through it. Changes from #4229, after the 2026-09-24 outage:

- Ticket fallback. When getbb.app can't mint a ticket, or the gate
  answers a ticket dial with 401 (the pre-ticket gate, or TUNNEL_TICKETS
  off), connect dials with the raw server credential and keeps doing so
  for 30 minutes. bb account hands the credential only to the connect
  plugin, through the private bb-account.v1.connectCredential RPC. A 403
  (server not paired) never falls back.
- Copy, don't move. Connect keeps the pairing under its old `credential`
  KV key, where builds from before bb account read it, and keeps it in
  step with bb account: it writes bb account's credential there, hands
  bb account a pairing an older build made after a downgrade, and
  removes its copy only after bb account signs out of that same pairing.
  Adoption never revokes or deletes a pairing. adoptConnectCredential
  now takes `replaces` and stores the offered credential only while bb
  account still holds what connect saw.
- A 401 from the ticket endpoint no longer counts as signed out: only
  bb account's confirmed signed-out answer stops the tunnel, and any
  other failure to mint falls back to the credential.
- Account status and credential sync failures back off exponentially
  (the count used to reset before the sync ran).
- The sign-in polls in the connect and bb account settings back off to
  30 s while getbb.app or the tunnel is unreachable, instead of polling
  every 2 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 24, 2026
Re-lands #4229: the bb-account plugin owns this server's getbb.app
credential and sign-in, and connect gets tunnel tickets and hosted
requests through it. Changes from #4229, after the 2026-09-24 outage:

- Ticket fallback. When getbb.app can't mint a ticket, or the gate
  answers a ticket dial with 401 (the pre-ticket gate, or TUNNEL_TICKETS
  off), connect dials with the raw server credential and keeps doing so
  for 30 minutes. bb account hands the credential only to the connect
  plugin, through the private bb-account.v1.connectCredential RPC. A 403
  (server not paired) never falls back.
- Copy, don't move. Connect keeps the pairing under its old `credential`
  KV key, where builds from before bb account read it, and keeps it in
  step with bb account: it writes bb account's credential there, hands
  bb account a pairing an older build made after a downgrade, and
  removes its copy only after bb account signs out of that same pairing.
  Adoption never revokes or deletes a pairing. adoptConnectCredential
  now takes `replaces` and stores the offered credential only while bb
  account still holds what connect saw.
- A 401 from the ticket endpoint no longer counts as signed out: only
  bb account's confirmed signed-out answer stops the tunnel, and any
  other failure to mint falls back to the credential.
- Account status and credential sync failures back off exponentially
  (the count used to reset before the sync ran).
- The sign-in polls in the connect and bb account settings back off to
  30 s while getbb.app or the tunnel is unreachable, instead of polling
  every 2 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 24, 2026
Re-lands #4229: the bb-account plugin owns this server's getbb.app
credential and sign-in, and connect gets tunnel tickets and hosted
requests through it. Changes from #4229, after the 2026-09-24 outage:

- Ticket fallback. When getbb.app can't mint a ticket, or the gate
  answers a ticket dial with 401 (the pre-ticket gate, or TUNNEL_TICKETS
  off), connect dials with the raw server credential and keeps doing so
  for 30 minutes. bb account hands the credential only to the connect
  plugin, through the private bb-account.v1.connectCredential RPC. A 403
  (server not paired) never falls back.
- Copy, don't move. Connect keeps the pairing under its old `credential`
  KV key, where builds from before bb account read it, and keeps it in
  step with bb account: it writes bb account's credential there, hands
  bb account a pairing an older build made after a downgrade, and
  removes its copy only after bb account signs out of that same pairing.
  Adoption never revokes or deletes a pairing. adoptConnectCredential
  now takes `replaces` and stores the offered credential only while bb
  account still holds what connect saw.
- A 401 from the ticket endpoint no longer counts as signed out: only
  bb account's confirmed signed-out answer stops the tunnel, and any
  other failure to mint falls back to the credential.
- Account status and credential sync failures back off exponentially
  (the count used to reset before the sync ran).
- The sign-in polls in the connect and bb account settings back off to
  30 s while getbb.app or the tunnel is unreachable, instead of polling
  every 2 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ymichael added a commit that referenced this pull request Sep 24, 2026
## Human comments

## What was wrong

When bb stops bundling a built-in plugin, its installed row stays.
Startup (`reconcileBundled`) only walks plugins that are currently
bundled: it installs new ones and never looks at rows whose built-in is
gone. After #4232 reverted #4229/#4230, data dirs that had run those
builds kept `bb-ai` and `bb-account` forever, showing as "Missing:
Plugin files are missing. Update or reinstall bb." The delete route
refuses built-ins, so users couldn't remove them either. #4240 made the
manual uninstall possible; this makes cleaning them up by hand
unnecessary.

## What changed

`apps/server/src/services/plugins/plugin-service.ts`:
- After bundled reconciliation and before `loadAll()`, startup deletes
every installed built-in whose name is no longer in this build's
registry. It uses the existing orphan check behind `isOrphanedBuiltin`,
now a shared `isOrphanedBuiltinRow` helper.
- It also deletes the plugin's settings, secrets and schedules through
the same helper manual removal uses (`deleteRemovedPluginData`,
extracted from `remove()`). That includes `onPluginUnregistered`, which
re-dispatches queued messages the plugin was holding.
- The row is deleted outright rather than tombstoned, so a plugin that
is bundled again later installs fresh.
- Startup deletes the row only after settings, schedules, and secret
cleanup succeeds. If secret removal fails, the row remains so the next
startup retries cleanup.
- Rows a user already uninstalled (tombstoned) are left alone, because
`listInstalledPlugins` skips them.
- Each removal is logged: `plugin <id> removed because bb no longer
bundles builtin:<name>; its settings, secrets, and schedules were
deleted`.
- The check compares against the static registry, not what loaded, so a
broken or partial build can't delete bundled plugins.

No wire or daemon protocol changes. No CLI or UI changes.

## How you verified

- `builtin-plugins.test.ts`: the old "marks a persisted builtin as
orphaned" test is replaced by "removes a builtin and its data once bb no
longer bundles it". It seeds a setting, a schedule and a secret file,
restarts without the built-in, and asserts the row, settings, schedule
and secrets directory are gone and the list is empty. It then re-bundles
and asserts a fresh, enabled install. It fails with the startup call
removed and passes with it.
- `builtin-plugins.test.ts` also simulates one secret removal failure,
confirms the registration remains, then confirms a second startup
removes the secret and row.
- `pnpm exec turbo run test --filter=@bb/server -- --run
test/services/plugins/builtin-plugins.test.ts`: 35/35.
- `pnpm exec turbo run typecheck --filter=@bb/server`: clean.
- `pnpm exec turbo run typecheck lint --filter=@bb/server`: clean.
- `turbo run test --filter=@bb/server -- test/services/plugins`:
521/521.
- Live, in a disposable `pnpm start:worktree` instance whose data dir
really went through #4230 then the revert, so it had `bb-ai` and
`bb-account` rows with their folders missing, plus one seeded `bb-ai`
setting. After restarting on this branch, both rows and the setting were
gone, the API listed no orphans, `bb-guide` was unchanged, and the
server log had both removal lines.

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

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 29, 2026
## Human comments

## What was wrong

#4232 reverted the bb account stack after the 2026-09-24 connect gate
outage. This PR brings back the web half of #4227: the endpoints a bb
account client needs. It is the first layer of a four-PR stack that
re-lands #4227, #4229 and #4230 so each part can be deployed and rolled
back on its own. The re-land drops #4227's tunnel tickets, so the gate
itself doesn't change.

## What changed

- **bb-web** (from #4227, without the tunnel ticket endpoint):
- `/api/account/me`, `/api/account/link/start`, `/api/account/link/poll`
and the `/link` approval page;
  - `POST /api/connect/machine-code-lookup`;
- the dashboard's connect UI moved into `components/connect-ui.tsx`. The
pairing command it shows (`bb connect --code … --server …`) works for
released bbs and new ones.
- **Link approvals can't replace a newer sign-in** (SlopCop P1).
Delivering a link credential, re-pairing from the dashboard, and
disconnecting a server now deny every other approved, undelivered link
for that server. An approved code is delivered at most two minutes past
its expiry; before, it could be delivered at any time.
- **connect-db:** `resolveServerCredential`,
`serverCredentialFromHeaders`, `write-result`, and a `./testing` export.
Migrations 0006/0007 are already on `main`; this adds none.
- Nothing changes under `apps/connect`, the tunnel packages, or the
deploy workflows.

## How you verified

- `pnpm exec turbo run typecheck --filter="...[HEAD^1]"` and `test
--filter="[HEAD^1]"` on this layer alone: pass (connect-db, web,
connect).
- New `account.test.ts` cases, all four failing on the previous code:
- an approved, unpolled code is denied after a disconnect plus a newer
link;
  - it is denied after another link replaces the server;
  - it is denied after a dashboard re-pair;
  - it isn't delivered past the grace.
- Not changed, answered on the review: atomic delivery writes and an
index for the expired-link prune. Both are follow-ups.
- Staging (`vibecodethis.site`), last run 2026-09-25 02:40–03:07 UTC on
`main` with #4313: this web (`630823a5`) with `main`'s gate. A
released-style bb paired through it with a dashboard-style pairing code
(`Paired as sawyer — reachable at https://sawyer.vibecodethis.site`),
then upgraded, downgraded and signed in again on the new client (#4253).
`POST /api/connect/tunnel-ticket` answers 404. Rolling this web back to
the pre-stack version under the new client kept its tunnel and sign-in
(details in #4253). No `wrangler tail` was used.

## Rollout

1. Merge. **Deploy Web** ships bb-web to 100%. It only adds routes. This
PR touches `packages/connect-db`, so **Deploy Connect** also runs: it
applies no migrations (none are pending, checked against production D1
on 2026-09-25) and `wrangler deploy`s the gate. The gate's own code is
unchanged from production's (#4313, `000248de`); the connect-db
additions it bundles are unused by it. So connected bbs just reconnect
once; with #4313's visitor hold that is a short delay for visitors
(staging: 75/75 one-second probes returned 200, one held 3.1 s).
2. Watch for 10 minutes:
- bb-web 5xx on `getbb.app`, especially `/api/account/*` and
`/api/connect/*`;
- a dashboard pairing code still pairs a released bb (`npx -p
bb-app@latest bb connect --code <code> --server
https://<handle>.getbb.app`).

## Rollback

- Web: `pnpm --filter @bb/web exec wrangler rollback <previous> --name
bb-web -m "<reason>"`, where `<previous>` is the version `wrangler
deployments status --name bb-web` shows before the merge deploys
(`d929bb65-81be-4090-ad8e-70a6e9d46fd7` on 2026-09-29 16:54 UTC, `main`
at #4454). Once #4253 has shipped, a web rollback keeps those bbs
connected and signed in; only new sign-ins stop working until it rolls
forward.
- Gate: `pnpm --filter @bb/connect exec wrangler rollback
000248de-6a8b-4ca0-94f9-a7a27fe80b8f -m "<reason>"` (production's gate
before the merge).

Stack: 1 of 4. Base: `main`. Next: #4252.

> AGENT GENERATED

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

Co-authored-by: Claude Opus 5.5 <[email protected]>
SawyerHood added a commit that referenced this pull request Sep 29, 2026
…#4253)

## Human comments

## What was wrong

#4229 moved Connect onto the new bb-account plugin. Two choices in it
made the 2026-09-24 gate outage harder to recover from:

- **Connect could only dial with tunnel tickets.** Rolling the gate back
to a version that doesn't know tickets returned 401 to every bb running
`main`, so the rollback locked them out.
- **Adoption moved the credential.** bb account adopted Connect's legacy
pairing and deleted Connect's copy. When it held a different server's
credential, it revoked the legacy one. A bb downgraded to an older build
started signed out, as bee did after the revert.

Separately, the sign-in dialogs in both plugins polled every 2 s whether
or not the last poll failed.

## What changed

This is #4229 plus these changes:

- **No tunnel tickets** (`plugins/connect/src/tunnel.ts`, `hosted.ts`).
Connect dials `wss://<handle>.getbb.app/__tunnel` with the raw server
credential, as released builds do, so it works with every gate,
including one that has been rolled back. Before each dial it reads the
credential from `bb-account.v1.connectCredential` (private contract;
only the connect plugin may call it, and other plugins and the CLI get
"only the connect plugin"). Log lines are `tunnel connecting to …` and
`tunnel connected`, as in released builds.
- **A credential the gate refuses signs bb account out**
(`bb-account.v1.confirmRefusedCredential`, connect-only). With tickets,
a replaced or revoked credential showed up as a 401 from the ticket
endpoint, which bb account checked and signed out on. Now the gate's 401
or 403 is the only signal. Connect passes the refused credential to bb
account, which checks it with `GET /api/account/me` and signs out if
getbb.app answers 401. It ignores a credential that is no longer
current, and a check that fails changes nothing. Connect keeps retrying
with backoff meanwhile. Without this, a bb whose server was replaced or
removed would dial forever.
- **#4313's client changes, carried into this tunnel.** #4313 changed
`main`'s old `tunnel.ts`, which this PR replaces, so its client behavior
is ported:
- stopping closes the tunnel with 1000, so the gate shows the server
offline at once instead of holding visitors;
  - the handshake timeout is 10 s;
- a close with the gate's `replaced by a new tunnel connection` reason
(another bb took the tunnel over) backs off 5 minutes and says why.
- **Copy, don't move** (`plugins/connect/src/credential-copy.ts`).
Connect keeps the pairing under its KV key `credential`, in the
`{serverUrl, handle, credential}` shape builds from before bb account
read. `credentialCopyOf` records the SHA-256 of the credential it last
shared with bb account. On each bb account change:
- Signed in: connect writes bb account's credential to its copy. If the
copy isn't the one it last shared (an older build re-paired after a
downgrade, or a bb that ran `main` during the outage window, like bee),
it first offers it to bb account.
- Signed out: a copy it shared is removed, so older builds see the
sign-out. A copy it never shared (the normal upgrade) is offered.
- **`adoptConnectCredential`** takes `replaces`: the credential connect
saw in bb account, or null for signed out. It answers:
  - `changed` if bb account has moved on since;
  - `rejected` if `/api/account/me` refuses the offered credential;
  - `adopted` once stored.

It never revokes or deletes a pairing. Keep Connect's copy for at least
two releases after the first release with bb account; the Connect README
says so.
- **`AccountLink` backoff:** status and credential-sync failures back
off exponentially up to 30 s. The failure count used to reset before the
sync ran, so a failing adoption retried every second.
- **Sign-in poll backoff** in `plugins/connect/app.tsx` and
`plugins/bb-account/app.tsx`: 2 s while polls succeed, then doubling to
30 s while they fail, instead of a fixed `setInterval`.
- Plugin SDK 0.6.3 (the `experimental_caller` surface; `main` is at
0.6.2).

## How you verified

- Tests that fail before and pass after:
- `account-integration.test.ts` (real bb-account plugin plus the stub
getbb):
- `signs out everywhere when getbb.app rejects the credential`: the stub
gate refuses a revoked credential on the redial, bb account signs out,
and connect tears the tunnel down. Without `confirmRefusedCredential` it
fails: connect retried forever;
- copies the legacy credential, keeps Connect's copy, revokes nothing,
and dials with it;
- prefers a pairing an older build made over a stale bb account sign-in;
- retries when bb account is briefly unavailable while handing over the
credential.
- `account.test.ts`: `confirmRefusedCredential` checks only the current
credential, keeps a valid one, signs out when getbb.app answers 401, and
serves only the connect plugin; `adoptConnectCredential`
compare-and-swap and no revocation; `connectCredential` only for the
connect plugin.
- `tunnel-lifecycle.test.ts`: dials the account's tunnel URL with the
credential from bb account; a gate 401 is reported to bb account once
per refusal and backs off; a 500 isn't reported; a stalled handshake
reads the credential again; bb account being unavailable backs off, then
dials.
- `tunnel-lifecycle.test.ts`, #4313's tests ported to this tunnel: clean
close on stop, the 10 s handshake deadline, the 5-minute backoff after a
takeover, a normal redial after 1006, and a late "replaced" close from a
stale socket. The first three fail on this PR's tunnel without the port.
- `connect.test.ts`: mirrors bb account's pairing for older builds and
removes it after sign-out; hands over a pairing an older build made
after a downgrade; offers a rejected legacy credential once; backs off
while adoption fails; remote access off and on redials.
- `app.test.tsx` in both plugins: a failing sign-in poll isn't repeated
within 2.5 s. The old code fails this; checked by running it against the
previous `app.tsx`.
- `pnpm exec turbo run typecheck --filter="...[HEAD^1]"` (101 tasks) and
`test --filter="[HEAD^1]"` on this layer. The only failures were outside
this change, and each passes when rerun alone: host-daemon command
discovery and provider-acp native roots (a `TMPDIR` under `$HOME` on the
test machine), and one `apps/server` machine-environment test (a load
flake).
- Both plugin SDK version guards pass with 0.6.3.

### Staging end to end (`vibecodethis.site`, 2026-09-25 UTC, on `main`
with #4313)

- **Setup:** gate = `main`'s with #4313 (`2e6e20c7`, deployed with
#4250's manual `versions deploy`); web = #4250 (`630823a5`); AI gateway
= #4252 (`c0cb8b9a`). No `wrangler tail` at any point.
- **Test bb:** one fresh data dir throughout, label `sawyer`. Its
hostname was probed every 5 s at `/install/version`.

| Step | Time | What happened | Result |
|---|---|---|---|
| R1 | 02:40 | A released-style bb (`main` `9781a2126a`, no bb account)
paired with a dashboard-style code through #4250's web | `tunnel
connected` at 02:40:18 |
| R2 | 02:40 | Upgraded in place to this stack (`152c87ff0c`, the #4254
head) | Copied the pairing and `tunnel connected` at 02:40:48.
bb-account and Connect hold the same credential plus `credentialCopyOf`
|
| R3 | 02:41 | bb cloud AI through #4252's gateway | See #4254 |
| R4 | 02:41 | Downgraded to `main` | #4289 removed the bb-account and
bb-ai installs at startup and kept their KV. Connected at 02:41:15 from
Connect's copy |
| R5 | 02:41 | Upgraded again | bb account signed in from its KV,
`tunnel connected` at 02:41:24 |
| R6 | 02:41–02:42 | bb-web rolled back to the pre-stack version, and
this client restarted under it | Logged `could not refresh the bb
account profile: … HTTP 500`; stayed signed in, `tunnel connected`, bb
cloud worked. Web rolled forward |
| R7 | 02:42 | Gate redeployed with this client connected | `tunnel
closed (code 1006)` at 02:42:12, `tunnel connected` at 02:42:14. A
one-second probe got 75/75 200s; one request was held 3.1 s |
| R8 | 02:43–02:49 | Another bb re-paired the server and dialed with the
new credential | Logs below: this client backed off 5 minutes, then its
redial got a 401 and bb account signed out |
| R9 | 02:49 | `bb account login --code` with a new code | Connect saved
its copy, `tunnel connected` at 02:49:19, bb cloud worked |
| R10 | 02:49 | Server revoked on getbb.app, then a redial | gate 403,
signed out at 02:49:24; restored and signed in again |
| R11 | 02:49 | After the fresh sign-in, downgraded to `main` | `tunnel
connected` at 02:49:53 from Connect's copy of the new pairing |
| R12 | 02:51–03:06 | Upgraded again and held for 15 minutes | 174/174
probes 200; no Connect or bb account log lines; 0 heartbeat misses or
stalls |
| R13 | 02:51 | `bb connect off` | Clean close: the next visitor got 503
in 0.07 s, with no hold |

R8:
```
[02:43:58] [plugin:connect] another bb connected with this server's identity and took over bb connect; reconnecting in 300000ms
[02:48:58] [plugin:connect] tunnel connecting to wss://sawyer.vibecodethis.site/__tunnel?v=1 (origin http://127.0.0.1:19316)
[02:48:58] [plugin:connect] the gate refused this bb's server credential (HTTP 401); reconnecting in 2000ms
[02:48:58] [plugin:bb-account] getbb.app rejected this bb's credential (HTTP 401 on GET /api/account/me after the connect gate refused a tunnel); signed out
[02:48:58] [plugin:connect] bb account signed out, so connect removed its copy of that pairing
```

Cloudflare GraphQL for `sawyer.vibecodethis.site`, 02:39–03:06:
- 503s only in the takeover window (02:44–02:49) and the two stop checks
(02:50–02:51);
- one 401 (R8) and one 403 (R10);
- staging Durable Object `internalError`: 2 around the first deploy,
then 0; `clientDisconnected` 0–9 per 5 minutes.

**Unchanged by this PR:** stopping the dev launcher's whole process
group (SIGTERM) exits before the tunnel's clean close reaches the gate.
With `main`'s client too, visitors saw one 502 and a 14 s hold before
503. `bb connect off` closes cleanly (R13).

Earlier runs are in the PR history: round 1 on 2026-09-24 before #4313,
and the ticket version's steps A–E in closed #4251.

## Rollout

1. Merge after #4250 and #4252 are deployed. The gate needs no change:
this client dials exactly as released builds do, and production's gate
already has #4313. Run it on dev servers first (bee), then ship a
release.
2. On bee, expect `copied connect's pairing into bb account`, `tunnel
connecting to …` and `tunnel connected`.
3. Watch:
- `grep 'plugin:connect\]\|plugin:bb-account\]'
~/.bb/logs/server-stdio.log` on bee;
   - `/api/account/*` status codes on bb-web;
- bb account `signed out` lines, which should only follow a server being
removed or replaced on getbb.app.

## Rollback

Revert this PR on `main` (and ship a patch release if it was released).
A bb that ran it keeps its pairing when it goes back: Connect's copy is
still in its KV (R4, R11). A bb that signed in fresh on this build also
keeps it, because the copy is kept in step with bb account (R11).

Since #4289, a build without bb account deletes that plugin's install,
settings, secrets and schedules at startup. It keeps plugin KV, which is
where bb account keeps its sign-in and bb-ai keeps its opt-in, and a
built-in reinstalls under the same id. Upgrading again after a downgrade
therefore comes back signed in, as R4 and R5 showed.

Stack: 3 of 4. Base: #4252.

> 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.

1 participant