Skip to content

Accept tunnel tickets behind a flag, and document deploying the gate by hand - #4251

Closed
SawyerHood wants to merge 2 commits into
bb/account-stack-1-web-thr_5npmg24byifrom
bb/account-stack-2-gate-thr_5npmg24byi
Closed

SawyerHood wants to merge 2 commits into
bb/account-stack-1-web-thr_5npmg24byifrom
bb/account-stack-2-gate-thr_5npmg24byi

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Closed on 2026-09-24: the re-land dropped tunnel tickets, so the gate needs no change at all. The gate deploy doc (docs/connect-gate-rollout.md) and the Deploy Connect run summary moved into #4250. The staging reproduction below stays here as the record for a Cloudflare support ticket.

The 2026-09-24 outage was caused by the gate deployment itself, not by the new gate's code. On staging, redeploying the unchanged pre-stack gate (aea2d767, built from main) while tunnels were connected reproduced the production failure every time:

  • Any new deployment of bb-connect orphans the hibernated tunnel WebSockets. That includes a 0% split and a redeploy of the same version. The bb keeps an open socket that nothing answers, so heartbeats go unanswered. The Durable Object answers visitors with 503 offline because it no longer has a tunnel. After 60 s the bb gives up and redials 1 s later.
  • While the object keeps receiving requests (the bb redialing, visitors retrying), every new dial into it is orphaned the same way. A few objects instead fail every request with Network connection lost. (HTTP 500).
  • An object that gets no requests for about 45 seconds recovers. Busy servers never go quiet, so production stayed down until the rollback.
  • wrangler rollback is the exception. Every rollback we saw closed connected tunnels at once, and they redialed cleanly: production at 01:43, and three staging rollbacks, including one to the pre-fix gate under load and one to a never-deployed version. Every wrangler deploy / versions deploy orphaned them.

What the platform does here isn't visible to us. The trigger, the persistence and the recovery are. The staging record is below; it's the evidence for a Cloudflare support ticket.

The fix for that, the TunnelDO self-restart, was split out as #4268 and has run in production since 16:53 UTC; this PR no longer contains it. What's left here is the gate half of #4227 (tunnel tickets, behind a flag) and the manual deploy procedure. An earlier revision also had a canary-driven gradual rollout workflow. It was dropped: every step of a gradual rollout is a deployment that reconnects every tunnel, and with tickets off this gate behaves like production's, so one deploy to 100% by hand is the least disruptive path.

What changed

  • Tunnel tickets behind a flag (the gate half of Add bb account endpoints, tunnel tickets, and the AI gateway #4227). apps/connect/src/worker.ts accepts a bbtkt_ ticket only when TUNNEL_TICKETS is "on". Otherwise a ticket gets 401 invalid ticket, as it does from today's gate, and new clients answer that by dialing with their raw credential (Add the bb account plugin and move connect onto it, safe to roll back #4253). wrangler.jsonc ships production "off" and staging "on"; pnpm cloud:dev runs "on". Raw credentials work in every mode.
  • verifyServerCredential (apps/connect/src/servers.ts) uses Add bb account endpoints to bb-web #4250's shared resolveServerCredential: the same lookup, and it also trims whitespace.
  • Deploy Connect's run summary gives the commands to deploy the uploaded version at 100% and to roll it back.
  • docs/connect-gate-rollout.md covers:
    • why every deployment disconnects tunnels, and why a version without the restart (anything before Restart a tunnel Durable Object whose tunnel socket vanished #4268) must never be deployed with versions deploy again;
    • how Cloudflare assigns versions for a Worker with Durable Objects: the entrypoint per request; each object pinned to one version per deployment, never moved back when the new version's share grows in the same order; Cloudflare-Workers-Version-Overrides reaches a 0% version;
    • the manual steps: one wrangler versions deploy <new>@100%, or a 5% split first for a change that could break dials;
    • what to watch, and rollback with wrangler rollback.

How you verified

  • tunnel-ticket-dial.test.ts (fails before, passes after): tickets refused with 401 when TUNNEL_TICKETS is off or unset; raw credentials accepted.
  • pnpm exec turbo run typecheck test --filter=@bb/connect on this layer: pass.

Staging reproduction and fix (vibecodethis.site, 2026-09-24 UTC)

# Time Deployment Connected Result
X0 03:08:56 new gate at 0%, pre-stack aea2d767 at 100% a pre-stack bb on sawyer Visitors 503 offline from 03:09:12; bb tunnel heartbeat missed 03:10:07; its redial orphaned too; from 03:11:10, 500 Network connection lost. on every request
X2 03:16:02 aea2d767 again (same version, no code change) sawyer + 20 load tunnels 17/20 tunnels orphaned: redial every 60–100 s, orphaned again, visitors 503 throughout. 3/20 and sawyer: 500 on every request
X3/X6 03:22–03:33 none load paused 45 s with no requests healed an object; 12 s healed half; tunnels stopped but visitors still hitting every 5 s: never healed
X7 03:33:15 aea2d767 again 8 healthy + 2 broken tunnels all 10 broken afterwards
B1 03:56:51 the self-restart (now #4268), at 100%, from the pre-stack gate sawyer + 10 load tunnels restart loop: the close record written before abort() was thrown away, 586 aborts. Fixed with storage.sync() (the test for it fails without it)
B2 04:00:51 the self-restart with the fix, at 100% same every tunnel back within about 40 s; DO internalError 205 → 0
B3 04:03:05 this version again same each tunnel back within about 5–10 s: one 500 per visitor, a clean close, redial
E2 05:16:22 this gate with TUNNEL_TICKETS off new-client bb + 10 load tunnels + a test tunnel connected beforehand orphaned, then restarted; the test tunnel was back in 3.6 s
E1 05:26:28 wrangler rollback aea2d767 (pre-stack, no restart) same every tunnel closed at once and redialed cleanly; 0 visitor failures
X10b 05:53:00 versions deploy of this version again same all 10 load tunnels orphaned, then restarted in seconds
X11 05:56:53 wrangler rollback to a never-deployed upload of this change same prompt close, clean redial (one extra dial from the restart check)

Example cf-rays for Network connection lost. on the stuck object (X0): a3fe99ac1ab1eb30, a3fe99d1493ae71e, a3fe9a00ba26f9c5.

Upgrade test step B. A pre-stack bb (main at 9bfe862b35) stayed paired while the new web (03:08:47) and this gate (B2/B3) were deployed. From 04:03:10 to 04:33:14 it held one tunnel with 0 heartbeat misses. Visitors to sawyer.vibecodethis.site/install/version got 200 on 347/347 probes. Staging DO outcomes were 0 internalError and 3–9 clientDisconnected per 5 minutes. Ten load tunnels with visitors ran alongside, with no errors.

Rollout

Current production gate: 1f42b2f5-ee85-4447-a43e-061acc517ea1 (#4268, with the self-restart).

  1. Merge after Add bb account endpoints to bb-web #4250. Deploy Connect (upload only) uploads the version; note its ID from the run summary. Nothing serves it yet.

  2. Deploy it in one step, from apps/connect:

    pnpm exec wrangler versions deploy <new>@100% --yes --message "tunnel tickets behind a flag (off)"

    Every connected tunnel reconnects once, and Restart a tunnel Durable Object whose tunnel socket vanished #4268 brings each back within seconds. A 5%/25% split would cost every tunnel one reconnect per step without protecting against anything: apart from the ticket check, which is off, this version behaves like production's gate.

  3. Watch for 15 minutes:

    • grep 'plugin:connect\] tunnel' ~/.bb/logs/server-stdio.log on bee: one reconnect within seconds, then no tunnel heartbeat missed;
    • 503s per host on *.getbb.app: one reconnect burst that settles within 5 minutes;
    • Durable Object internalError spikes once as objects restart, then returns to 0.

    Queries are in docs/connect-gate-rollout.md.

  4. Leave TUNNEL_TICKETS off until a bb release with Add the bb account plugin and move connect onto it, safe to roll back #4253 is out and stable. Then change it to "on" in one line and deploy that version the same way; bb builds with bb account then log tunnel connected with a ticket.

Rollback

pnpm --filter @bb/connect exec wrangler rollback 1f42b2f5-ee85-4447-a43e-061acc517ea1 -m "<reason>" returns to #4268's gate. Every wrangler rollback we saw closed tunnels cleanly. Never roll back with wrangler versions deploy <old>@100%, and never deploy a gate from before #4268 that way: its objects can't restart orphaned tunnels.

Stack: 2 of 5. Base: #4250.

AGENT GENERATED

🤖 Generated with Claude Code

@bb-slop-cop

bb-slop-cop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

This PR aims to reconnect hosted tunnels after gate deployments and introduce a gradual rollout with canary checks. I’m the Slop Cop, reviewing security, code quality, performance, and opportunities to reuse existing code, with runtime checks where the local setup permits.

AGENT GENERATED

WRANGLER_ENV_ARGS: ${{ inputs.environment == 'staging' && '--env staging' || '' }}
WORKER_NAME: ${{ inputs.environment == 'staging' && 'bb-connect-staging' || 'bb-connect' }}
CANARY_SERVER_URL: ${{ inputs.environment == 'staging' && vars.CONNECT_CANARY_STAGING_SERVER_URL || vars.CONNECT_CANARY_SERVER_URL }}
CONNECT_CANARY_CREDENTIAL: ${{ inputs.environment == 'staging' && secrets.CONNECT_CANARY_STAGING_CREDENTIAL || secrets.CONNECT_CANARY_CREDENTIAL }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — [P2] Do not fall back to production canary settings for staging

When staging is selected but its URL or secret is unset, these && ... || ... expressions fall back to the production values. With both staging values missing, the configuration check passes and the staging rollout dials the production canary, potentially replacing its active tunnel, while never probing the deployed staging gate. Select each environment explicitly and fail if that environment’s settings are missing.

AGENT GENERATED

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in ddf26f5. The job now reads the production and staging canary settings into separate variables. A "Select the canary for this environment" step picks one set by inputs.environment and fails, naming the missing variable or secret, if that environment's pair isn't set. There is no fallback to production.

AGENT GENERATED


- name: Canary with the raw credential
run: |
pnpm --silent --filter @bb/connect-rollout run rollout canary \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — [P2] Keep a canary connected across the deployment

The canary only opens its tunnel after the deployment and the 90-second wait, and runCanary terminates that tunnel at the end of each run. It therefore never exercises the failure condition documented in this PR: a tunnel already connected when the gate deploys. A fresh or idle canary object can pass while busy existing objects remain orphaned. Establish a canary before deploying, keep traffic flowing, and verify that it reconnects and resumes proxying within a bounded recovery window.

AGENT GENERATED

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in ddf26f5. The canary now connects before wrangler versions deploy and stays connected through it. The workflow refuses to deploy if it can't connect. rollout canary --recover-within 120 treats an unanswered heartbeat, a 503, or a dropped tunnel as a disconnect and redials. It fails unless the tunnel answers heartbeats and serves its own nonce through the hostname again within 120 s, and it then holds for canary_minutes. Tests cover recovering after a drop and failing when the gate stays broken after the first drop. This is exactly the staging failure: on the pre-fix gate a connected tunnel never came back, and with this PR it came back in 5–40 s.

AGENT GENERATED

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 SLOP COP 🚨 · review

This PR helps remote connections recover after a gate deployment, adds a switch for accepting short-lived tunnel tickets, and introduces gradual releases with automated health checks and rollback.

I found two rollout issues worth fixing, with details inline:

  • P2 — Staging can silently use the production canary. Missing staging settings fall back to production instead of stopping the run, so a staging rollout can test the wrong environment and replace its canary connection.
  • P2 — The canary misses the deployment transition. It connects only after deployment and a 90-second wait. That cannot verify recovery of a tunnel that was connected during deployment—the failure this change addresses.

The parallel security and performance reviews found no additional concrete regressions. The architecture scan found useful reuse of the shared tunnel client and URL helpers. One follow-up refactor: TunnelSession.start() and the canary both send heartbeats, but their replies are indistinguishable. A shared configurable heartbeat observer would avoid skewed acknowledgment and latency measurements.

Validation at a44a47be: the focused Turbo run passed both typechecks and all 159 tests (145 Connect, 14 rollout), including local runtime integration and fake-gate canary tests. A separate local Wrangler gate started and returned the expected unknown-host 404. The doobie CLI is unavailable here, and I did not reproduce a real Cloudflare deployment transition; the local checks do not establish that recovery behavior.

AGENT GENERATED

@SawyerHood
SawyerHood force-pushed the bb/account-stack-1-web-thr_5npmg24byi branch from 5edf39f to e66b41f Compare September 24, 2026 04:49
@SawyerHood
SawyerHood force-pushed the bb/account-stack-2-gate-thr_5npmg24byi branch 2 times, most recently from ddf26f5 to 626efe6 Compare September 24, 2026 05:59
SawyerHood added a commit that referenced this pull request Sep 24, 2026
## Human comments

## What was wrong

When the gate's tunnel Durable Objects restart, hibernated tunnel
WebSockets can be orphaned. The bb keeps an open socket that nothing
answers, so its heartbeats go unanswered. The object has no tunnel
socket, so visitors get `503 offline`. After 60 s the bb redials, and
while the object keeps getting requests, every new dial is orphaned the
same way. An object heals only after about 45 s with no requests, which
busy servers never get. #4251 reproduced this on staging: any deployment
of `bb-connect`, even an unchanged version, triggers it (runs X0–X7
there).

This caused the 2026-09-24 01:07 outage, and it's happening again in
prod **without any gate deploy**. Prod `bb-connect` has stayed on
`6f79c289` since the 01:43 rollback. Since about 07:00 UTC, Durable
Object outcomes show recurring bursts at 08:45–10:30, 12:30, 13:30,
14:30 and 16:15, each with `responseStreamDisconnected` up 5–10× and
`scriptThrewException` up 10–20×. Each burst leaves more servers stuck:

- the 503 share on `*.getbb.app` climbed from 9–10% overnight to 16–30%
per hour;
- 112 hosts had 100 or more extra 503s from 14:30 to 16:30;
- bee's log shows `tunnel heartbeat missed` every minute again.

The current gate has no way to recover.

## What changed

This is the `TunnelDO` self-restart from #4251, split out so it can ship
ahead of the account stack. It contains no ticket code and no workflow
changes.

- `apps/connect/src/tunnel-do.ts`:
- It records `tunnelOpenedAt` when it accepts a tunnel and
`tunnelClosedAt` when the last tunnel socket closes or the owner revokes
it.
- When a visitor request, a tunnel dial, or the 50 s presence alarm
finds no live tunnel socket although the last one never closed (after a
5 s grace), it writes the close, calls `storage.sync()`, then
`ctx.abort()`. The restarted object drops the orphaned socket and the bb
redials into a working object.
- `sync()` is required. Without it, `abort()` discards the write and the
object restarts forever (586 aborts in staging run B1 of #4251).
- Objects written by older code only have `serverId`/`machineId`; they
restart once the same way, so the first deploy of this code heals
existing stuck objects.
  - The alarm path covers servers nobody is visiting.
- `apps/connect/src/worker.test.ts`: a `TunnelDO restarts after its
tunnel socket vanishes` suite. It restarts on a visitor request, a dial
and the alarm. It doesn't restart after a recorded close, within the
grace period, or with a live socket. Legacy objects restart exactly
once, and the close record survives the restart.

## How you verified

- On `origin/main`'s `tunnel-do.ts` the new suite fails 5 of 9; with
this change 9 of 9 pass.
- `pnpm exec turbo run typecheck lint test --filter=@bb/connect`: pass
(130 tests).
- Staging evidence from #4251, using the same `tunnel-do.ts` logic:
- B2: the first deploy of this code over the old gate brought every
tunnel back within about 40 s, and DO `internalError` went 205 → 0.
  - B3 and X10b: later deploys brought each tunnel back within 5–10 s.
- Upgrade step B: a pre-stack bb held one tunnel for 30 minutes with 0
heartbeat misses, and visitors got 347/347 200s.

## Rollout

**Deploy Connect is enabled, so merging deploys this with `wrangler
deploy` at 100%.** That's acceptable: the deploy orphans the tunnels,
and this code restarts them. On staging (#4251 B2), all tunnels were
back within about 40 s. The gentler path, recommended in #4251, is:

1. Disable Deploy Connect.
2. Merge.
3. `pnpm --filter @bb/connect exec wrangler versions upload`.
4. `pnpm --filter @bb/connect exec wrangler rollback <new-version-id> -m
"gate: tunnel self-restart"`. A rollback to a never-deployed version
closed tunnels cleanly on staging (#4251 X11).
5. Re-enable Deploy Connect.

Either way, go straight to 100%: objects still on old code can't
restart.

For 30 minutes afterwards, watch:

- the 503 share and 503s per host on `*.getbb.app`;
- Durable Object `internalError` (one spike as stuck objects restart,
then 0) and `clientDisconnected`;
- `grep 'plugin:connect\] tunnel' ~/.bb/logs/server-stdio.log` on bee.

## Rollback

`pnpm --filter @bb/connect exec wrangler rollback
6f79c289-9e43-4697-b056-78e31bc3e6ad -m "<reason>"`. Use `wrangler
rollback`, never `versions deploy <old>@100%`: rollbacks closed tunnels
cleanly every time we saw one, and a deployment orphans them again.

> AGENT GENERATED

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

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
@SawyerHood
SawyerHood force-pushed the bb/account-stack-1-web-thr_5npmg24byi branch from e66b41f to c304459 Compare September 24, 2026 19:18
@SawyerHood
SawyerHood force-pushed the bb/account-stack-2-gate-thr_5npmg24byi branch from 626efe6 to 3ec8797 Compare September 24, 2026 19:18
@SawyerHood SawyerHood changed the title Heal tunnels after gate deploys, accept tunnel tickets behind a flag, and roll the gate out gradually Accept tunnel tickets behind a flag, and roll the gate out gradually with a canary Sep 24, 2026
@SawyerHood
SawyerHood force-pushed the bb/account-stack-2-gate-thr_5npmg24byi branch from 3ec8797 to 5a40030 Compare September 24, 2026 19:28
@SawyerHood
SawyerHood force-pushed the bb/account-stack-1-web-thr_5npmg24byi branch 2 times, most recently from 1e37077 to 77ce343 Compare September 24, 2026 21:04
@SawyerHood
SawyerHood force-pushed the bb/account-stack-2-gate-thr_5npmg24byi branch from 5a40030 to 4af389e Compare September 24, 2026 21:04
SawyerHood and others added 2 commits September 24, 2026 21:47
…rsions instead of deploying them

Re-lands the web half of #4227: /api/account/* (me, link start and
poll), the /link approval page, /api/connect/tunnel-ticket,
/api/connect/machine-code-lookup, and the connect-db helpers they use
(resolveServerCredential, tunnel ticket mint and verify). Everything is
additive; released bbs don't call any of it.

An approved link code can no longer replace a newer sign-in. Delivering
a link credential, re-pairing from the dashboard, and disconnecting a
server deny every other approved, undelivered link for that server, and
an approved code is delivered at most two minutes past its expiry.

This is also the first change in the stack that touches
packages/connect-db, which triggers Deploy Connect. That workflow
deployed every gate straight to 100% on merge, which is how the
2026-09-24 outage shipped. It now applies migrations and runs
`wrangler versions upload`, so a merge never moves gate traffic.

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

The gate half of #4227. The tunnel self-restart that fixed the
2026-09-24 outage landed separately in #4268.

- The gate accepts a `bbtkt_` tunnel ticket in place of the raw
  credential when TUNNEL_TICKETS is "on". Production ships "off"; a
  refused ticket gets 401, which bb answers by dialing with its raw
  credential. Staging and local cloud dev run "on".
- Deploy Connect's summary gives the commands to deploy the uploaded
  version and to roll it back.
- docs/connect-gate-rollout.md covers why every deployment disconnects
  tunnels, how Cloudflare assigns versions for a Worker with Durable
  Objects (entrypoint per request, each object pinned per deployment),
  the manual steps (one `wrangler versions deploy` to 100%, or a 5%
  split first for a change that could break dials), what to watch, and
  rollback (`wrangler rollback`, never `versions deploy <old>@100%`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@SawyerHood
SawyerHood force-pushed the bb/account-stack-1-web-thr_5npmg24byi branch from 77ce343 to ec5e1a3 Compare September 24, 2026 21:52
@SawyerHood
SawyerHood force-pushed the bb/account-stack-2-gate-thr_5npmg24byi branch from 4af389e to 34a21f2 Compare September 24, 2026 21:52
@SawyerHood SawyerHood changed the title Accept tunnel tickets behind a flag, and roll the gate out gradually with a canary Accept tunnel tickets behind a flag, and document deploying the gate by hand Sep 24, 2026
@SawyerHood
SawyerHood force-pushed the bb/account-stack-1-web-thr_5npmg24byi branch from ec5e1a3 to f10b906 Compare September 24, 2026 22:49
@SawyerHood
SawyerHood removed this pull request from stack #4255 September 24, 2026 22:49
@SawyerHood

Copy link
Copy Markdown
Collaborator Author

Closing: the re-land no longer uses tunnel tickets, so the gate needs no change. The gate deploy doc and the Deploy Connect summary moved into #4250, and the stack is now #4250 → #4252 → #4253 → #4254. The staging reproduction in the description stays as the record for the Cloudflare support ticket.

AGENT GENERATED

@SawyerHood SawyerHood closed this Sep 24, 2026
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