Skip to content

Reach machines over every enabled server route, behind the serverRoutes experiment - #7864

Merged
ymichael merged 21 commits into
mainfrom
bb/6748-ship-pr2-server-routes-daemon-failover-thr_8fyb466bfh
Nov 23, 2026
Merged

ymichael merged 21 commits into
mainfrom
bb/6748-ship-pr2-server-routes-daemon-failover-thr_8fyb466bfh

Conversation

@ymichael

@ymichael ymichael commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Each machine knows its bb server by one URL, the one it was installed with. When that route stops working (for example the home internet drops for a bb connect-enrolled machine sitting on the server's own LAN, or a tailnet goes down), the machine disconnects even though another route to the same server would work. The server also had no stable identity a client could check a route against, and server access was one "connection method" per server (defaultMachineAccess) rather than a set of routes.

What changed

Server (behind the new early adopter experiment serverRoutes; while it is off, the server advertises routes: [], the Local network route does not exist, and daemons behave exactly as on main, including the Tailscale plugin's address being admitted by the Host/Origin guards):

  • Stable serverId (srv_<hex>, stored in bb.db so it survives restarts and moves with bb server move), returned by /health, system config, daemon session open, bb status --json and bb server access list --json.
  • Every set-up server access provider is a route; there is no per-route on/off. serverAccessOrder replaces defaultMachineAccess, and localNetworkEnabled (default off) adds the Local network route. Plugin routes are routes while their plugin reports them available and are turned off in the plugin's settings; Manual is a route while machineServerUrl is set. Providers declare routeKind (local-network | private-overlay | relay | custom) for default ranking; bb connect declares relay and the Tailscale plugin (Add an official Tailscale plugin that serves bb over Tailscale Serve #7862) declares private-overlay. Kind ranking applies only with serverRoutes on; while it is off, providers rank by the user's order, then Add an official Tailscale plugin that serves bb over Tailscale Serve #7862's earliest-installed-plugin order, then Manual, as the single default route did on main. Providers of one kind keep Add an official Tailscale plugin that serves bb over Tailscale Serve #7862's earliest-installed-plugin order, not load order. Plugin SDK 0.6.188. routeKind is required in the SDK types; registration still accepts a declaration without it, from a plugin built against an older SDK, and treats it as custom (marketplace installs bundle with esbuild without type-checking, so such plugins still install); documented under the existing bb.experimental_serverAccess.register entry in docs/api_to_audit.md and in the Plugin Guide surface.
  • Per-host grants (host_server_access_grants); the install route stays the host's anchor. Enrollment falls back to the next enabled route when one needs setup.
  • New machines install on the first working route in that order, with Local network last (a laptop's install route should not be a LAN address). A migrated Tailscale default keeps installing on Tailscale even with bb connect on, and ordering alone puts Tailscale first with the experiment on too.
  • Core Local network provider: extra in-process listeners on the machine's private LAN IPv4s (no restart). Removing it closes the listeners and drops connections already upgraded over them.
  • Routes appear in system config and the session-open response, and are pushed live with a new server-routes.replace WS message. The daemon reports its route with a new session.route message; each daemon session stores it.
  • The Host/Origin guards admit enabled routes' addresses. The admitted set is persisted in app_settings_values (serverAccessOrigins) and restored in createApp before the listener, so route hostnames work from the first request after a restart. An address survives a failed or timed-out availability check and is dropped on a definite answer, when the route is turned off, or when its plugin is disabled or removed. This replaces Add an official Tailscale plugin that serves bb over Tailscale Serve #7862's copy of the same mechanism. Plugin providers' addresses (Tailscale, bb connect) are admitted whether or not serverRoutes is on, as on main. Core's Local network and Manual addresses are admitted only while it is on. experimental_serverAccess.recheck() still refreshes the set directly, as Add an official Tailscale plugin that serves bb over Tailscale Serve #7862 does.
  • UI and CLI: Settings → Machine access becomes "How devices reach this server" (the routes that are set up, in order; Local network and Manual have a menu with Remove, plugin routes link to their settings; Add a route offers the rest), Settings → Machines shows "via ", bb server access list|add|remove|order (add/remove cover local-network and direct), and a Route column in bb machine list.
  • Migration 0167 copies existing grants and defaultMachineAccess into the new model but keeps hosts.server_access_grant_id and the old setting row for one release, so going back a version works; the anchor grant id is mirrored into the old column.

Host daemon:

  • Candidates are the learned routes in server order plus the install URL. Every dial probes /health plus a serverId check on all of them in parallel (2 s timeout; route i may win after i × 500 ms), sending each route only its own headers (the bb connect credential goes only to bb connect).
  • No mid-session switching: it stays on a working route until the connection drops, then picks again. With resumeDaemonSessions it keeps its session across the switch.
  • Learned routes persist in <data dir>/server-routes.json, not config.json, whose strict schema would make an older launcher refuse the file after a rollback.
  • Attachment and self-update downloads prefer an encrypted route; the bb connect gate comes from the relay route; machine-auth-proxy follows the active route.

Wire changes: HOST_DAEMON_PROTOCOL_VERSION 278 → 279 (serverRoutes in session open, route in the session-open request, server-routes.replace, session.route).

Docs: docs/multiple-devices.md, docs/configuration.md, docs/api_to_audit.md, bb guide machines/customization, the bb-cli command index, the plugin-authoring machines reference, and the Tailscale plugin's skill and overview. These no longer say "choose Tailscale as the connection method" or defaultMachineAccess tailscale.

Not in this PR: desktop and mobile route selection (later PRs), the prototype's x-bb-gate-* peer-address strip (replaced by a separate auth change), and the better-route check, flap holds and backoff (a later shared-policy PR).

How you verified

Tests added, each failing before its fix:

  • apps/host-daemon/src/server-routes.test.ts: ranking with head start, serverId mismatch rejected, per-route headers, failover and return on redial, current route kept when nothing answers, no probing or saving while the server advertises no routes, persistence outside config.json.
  • apps/host-daemon/src/server-connection.test.ts: stays on its working route after learning better ones, then fails over and reports session.route on each redial.
  • apps/server/test/services/machines/server-routes.test.ts: experiment off advertises nothing and acquires no grants; LAN advertised as soon as it is added; LAN connections dropped when it is removed; new machines install on Local network only when nothing else works; route addresses admitted from the first request after a restart (fresh modules); address kept through a failed check and dropped on a definite answer; Manual's address refused once its URL is cleared; with the experiment off a plugin route's address stays admitted and Manual's is dropped.
  • apps/server/test/services/machines/server-access.test.ts: a declaration without routeKind (an older plugin) registers on the real and fake hosts and ranks as custom (fails without the registration default); install route prefers relay/custom over private-overlay/local-network with the experiment on; with it off, the user's first route and then the first registered plugin win over the relay (fails without the gate: a Tailscale-first order installed on the relay). Add an official Tailscale plugin that serves bb over Tailscale Serve #7862's tests stay as they were (address admitted until lost, kept across a restart and a failed check, dropped on plugin disable). Its "defaults to the earliest-installed plugin" test now checks route order and the install route instead of the removed defaultProviderId.
  • packages/db/test/migrate.test.ts: migration copies grants and the default without dropping the old column or row.

Commands (all green): pnpm exec turbo run typecheck lint --filter='...[origin/main]'; pnpm exec turbo run test for every changed package (app, cli, demo-server, host-daemon, server, db, domain, host-daemon-contract, plugin-sdk, sdk, server-contract, templates, test-helpers, bb-guide, connect, environment-modal-sandbox, plugin-api-docs); pnpm exec turbo run test --filter=@bb/integration-tests --force. Counts below.

After rebasing onto main at 76d4358:

  • typecheck + lint: 167/167 turbo tasks.
  • Package tests: 33/33 turbo tasks across the changed packages, which now include the Tailscale plugin (server 4,432). Three server tests failed once under full parallel load and passed when rerun: two in install-machine-script.test.ts and plugin-install.test.ts's git fallback. db, server and host-daemon were rerun after the final rebase onto Time server and plugin SQL in the query cost check, and fail every case on its ceiling #7934, which moves server SQL into @bb/db data modules (13/13 tasks). Following Time server and plugin SQL in the query cost check, and fail every case on its ceiling #7934, server-access.ts writes host columns through setHostColumns, and sql-in-data-modules.test.ts passes.
  • After the final rebase onto 76d4358: 21 test tasks for db, server, host-daemon, host-daemon-contract, plugin-sdk, cli, app, Plugin Guide, bb connect, Tailscale and bb-guide. All passed except one plugin-sdk process-kill test that timed out under load and passes alone.
  • Integration (--force, not a cache replay): 31/31 files, 80/80 tests.
  • pnpm exec turbo run check:query-costs --filter=@bb/db: passes under Fail the query cost check on undeclared growth, and bound nine history lookups #7923's stricter growth check; cases added for every new query function (getOrCreateServerId, get/setServerAccessOrigins, the grants table functions, recordSessionRoute); no growth warnings from them.
  • plugin-instance-lifecycle.test.ts "runs a provider callback only on the instance that registered it" now tolerates the background availability refresh on plugin change; it still asserts the stale record never reaches the old instance (same adjustment as the Tailscale plugin PR).

Live, on an isolated pnpm start:worktree instance with a second enrolled daemon ("Client A") and a TCP relay standing in for a breakable Manual route ordered first, Local network second:

  • Experiment off: Client A connected on its install URL, learned no routes, wrote no server-routes.json; Local network was not offered.
  • Experiment on: Client A learned all three routes live through server-routes.replace and stayed on its working route. After a restart it dialed the saved top route (Manual) in 15 ms; bb machine list showed Route "Manual".
  • Relay down at 19:47:26.094, connected via Local network at 19:47:27.605 (about 1.5 s). Settings showed "Client A · via Local network". After the relay came back it stayed on Local network for 75 s+.
  • Turning Local network off with the Settings toggle moved Client A back to Manual; with resumeDaemonSessions on, session hses_5zcdm8va5c resumed across the switch. Turning Local network on from Settings reached the connected daemon at once.
  • Turning the experiment off: the daemon learned routes: [] and deleted its routes file, Local network disappeared, and the LAN listeners closed.
  • Restart window (fix for persisted route addresses): with a Manual route on a hostname (http://127.0.0.1.sslip.io:25900), the first requests after a server restart returned 403 forbidden_host on /health and /api/v1/system/config before the fix, and 200 on the very first request after it.
  • Install route: with Local network ordered first and Manual enabled, a new manual machine was installed on Manual (direct), not Local network.

Re-run after the rebase (it changed the daemon dial path: per-route headers now go through main's compressed WebSocket constructor and send queue), on the same setup:

  • Client A enrolled on Manual (http://127.0.0.1.sslip.io:25900) and learned Manual + 2 Local network routes.
  • Relay down at 23:29:48.528; connected via Local network at 23:29:50.354 (about 1.8 s). It stayed on Local network for 60 s after the relay came back.
  • With resumeDaemonSessions on, session hses_bmjc944x9h resumed across a Manual → Local network switch, and its row recorded local-network. Turning Local network off moved it back to Manual. Turning it on reached the connected daemon live.
  • Experiment off while connected on Manual: the daemon learned routes: [] and deleted server-routes.json, Local network disappeared, and the LAN listeners refused connections.
  • Restart window: the first request after a server restart to the Manual hostname returned 200 for /health and /api/v1/system/config.

Known limit, unchanged by the rebase: if a daemon was installed on a Manual hostname while the experiment was on, it cannot reconnect after the experiment is turned off. The hostname gets 403 forbidden_host again, which is main's behaviour for a hostname without BB_APP_URL. It recovered as soon as the experiment was turned back on.

Not exercised live: a real bb connect route (the dev server was unpaired) or a real Tailscale route (unit tests cover its admission with the experiment off).

Part of #6748

🤖 Generated with Claude Code

AGENT GENERATED

@ymichael
ymichael force-pushed the bb/6748-ship-pr2-server-routes-daemon-failover-thr_8fyb466bfh branch 10 times, most recently from f671ad2 to f2e2fce Compare November 23, 2026 21:16
ymichael and others added 20 commits November 23, 2026 13:54
…es experiment (#6748)

Server:
- Stable serverId (srv_<hex>, stored in app settings) in /health, system
  config, daemon session open, bb status --json and bb server access list.
- Server access providers become ordered routes: serverAccessOrder and
  disabledServerAccessProviderIds replace defaultMachineAccess. Providers
  declare routeKind (bb connect: relay, Tailscale: private-overlay) for
  default ranking; providers of one kind follow the earliest-installed
  plugin. Plugin SDK 0.6.177.
- Per-host grants table (host_server_access_grants); the install route stays
  the anchor. New machines install on a relay or custom route before
  local-network and private-overlay, and enrollment falls back to the next
  enabled route.
- Core Local network provider: extra in-process listeners on private LAN
  IPv4s, no restart; turning it off closes the listeners and drops their
  connections.
- Routes in system config and the session-open response, pushed live with
  server-routes.replace; the daemon reports its route with session.route and
  each session stores it. Settings -> "How devices reach this server", via
  <route> on each machine, bb server access list|enable|disable|order, and a
  Route column in bb machine list.
- Admitted route addresses persist in app_settings_values
  (serverAccessOrigins) and are restored before the listener, so route
  hostnames pass the Host/Origin guards from the first request after a
  restart; an address survives a failed check and is dropped on a definite
  answer, when turned off, or when its plugin is disabled or removed.
  Plugin providers' addresses (Tailscale) are admitted with the experiment
  off too, as on main; core Local network and Manual only with it on.
- Early adopter experiment serverRoutes: off advertises routes: [] and hides
  Local network, so daemons behave as before.
- Migration 0165 copies grants and defaultMachineAccess without dropping
  hosts.server_access_grant_id or the old setting, so a downgrade still works;
  the anchor grant id is mirrored into the old column.

Daemon:
- Candidates are the learned routes in server order plus the install URL.
  Every dial probes /health + serverId in parallel (2 s timeout, rank i may
  win after i x 500 ms) with per-route headers. No mid-session switching: it
  stays on a working route until the connection drops.
- Learned routes persist in <data dir>/server-routes.json (not the strict
  config.json, which older launchers would reject).
- Guarded downloads prefer an encrypted route; the bb connect gate comes from
  the relay route; machine-auth-proxy follows the active route.

HOST_DAEMON_PROTOCOL_VERSION 276 -> 277.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…plugins at registration

- ServerAccessProviderDeclaration.routeKind is required, so new and rebuilt
  plugins must choose a kind instead of silently ranking as custom.
- Registration still accepts a declaration without it (a plugin built against
  an SDK before 0.6.177) and fills in "custom" once there, in both the real and
  fake hosts; core's own fallback in server-access.ts is gone.
- Marketplace installs bundle with esbuild and never type-check, so old
  plugins still install.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Settings → Machines shows the routes that are on, broken ones included with
their error and a Fix action, and offers the others from an "Add a route"
dropdown in the section header. Reordering appears only with two or more
routes. The Add Machine dialog lists the choices inline. Settings → Mobile
summarizes ready routes and links to Machines to change them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the experiment off, new machines picked the anchor relay-first and
providers ranked by route kind, so a user whose migrated default was Tailscale
got bb connect for new machines. Off now ranks by the user's order, then the
earliest-installed plugin, then Manual, and installs on the first working one,
as the single default route did on main. Route-kind ranking and the
relay-first install rule apply only with serverRoutes on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ebasing onto main

Main shipped its own 277 and SDK 0.6.179 since this branch bumped them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The settings section is now "How devices reach this server" on the Machines
page. The experiment description, the Host guard message, the no-route
enrollment error, the docs, the Plugin Guide surface and the Tailscale
overview still sent people to "Settings → Machine access". The plugin
authoring reference and the Tailscale overview also now state the install rule
for the serverRoutes experiment both off and on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bb connect's phone pairing section renders nothing until the account is signed
in, but its wrapper still took the card's space-y-3 gap, leaving 12px of extra
space under the text. The plugin section brings its own top margin and border,
as it did on main, so render it without a wrapper or gap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
"Turn off" on a plugin route (bb connect, Tailscale) only hid it from this
list and dropped its hostname from the Host and Origin guards, so bb connect
stayed signed in and connected while every request through it got 403
forbidden_host. Each route already has a real switch elsewhere, so the second
one goes:

- disabledServerAccessProviderIds is replaced by localNetworkEnabled (default
  false). Plugin routes are routes while their plugin reports them available
  and are turned off in the plugin's own settings (Manage). Manual is a route
  while machineServerUrl is set.
- Local network and Manual get a menu with Remove (and Edit address for
  Manual) instead of Turn off; Add a route adds them back.
- New machines install on the first working route in the user's order, with
  Local network last, whether or not serverRoutes is on. The relay-first
  install rule is gone, so ordering alone puts Tailscale ahead of bb connect.
- bb server access enable|disable become add <id> [url] and remove <id>, for
  local-network and direct; plugin routes are refused with a pointer to the
  plugin's settings.
- The ServerAccessStatus provider entries lose their enabled field. No daemon
  wire change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the Manual editor on save

- Local network listed every private IPv4 on any interface, including
  virtual bridges such as OrbStack/UTM's bridge100 that only VMs on this
  computer can reach. It now offers and listens on the address this computer
  routes outbound traffic through (found by connecting a UDP socket without
  sending), and falls back to every private address when that is unknown.
- Every route row has a menu: plugin routes carry Manage there (a broken one
  still shows Fix beside it), Manual has Edit address and Remove, Local
  network has Remove.
- Saving a new Manual address left the add editor open, hidden only while
  Manual was set, so removing Manual brought the editor back. Saving now
  closes the add and edit editors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Dropping a route started dnd-kit's settle animation before the new order
reached the list: the general settings mutation applies its optimistic patch
asynchronously, so the displaced row's slot moved a frame later while its
transform was still animating, and it shot above the list before sliding back.
Apply the dropped order synchronously, as the providers list does, until the
write settles. The lifted row in the routes, projects and providers lists is
now fully opaque, so the row sliding under it no longer shows through.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each address in Settings → Machines → How devices reach this server gets a
copy button after it, shown on hover and always on touch screens.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Plugin routes get a settings icon that opens the plugin's settings (a broken
one keeps its Fix button instead), Manual gets edit and remove icons, and
Local network gets a remove icon. Each icon has a tooltip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The route rows and the phone card's ready routes share one address list with
a copy button after each address.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
It makes Local network available to add rather than adding it, and machines
prefer the highest route in the user's order that answers, not simply the
first to answer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/6748-ship-pr2-server-routes-daemon-failover-thr_8fyb466bfh branch from b9014f4 to 9c2f94d Compare November 23, 2026 21:55
@ymichael
ymichael merged commit 02239e9 into main Nov 23, 2026
55 checks passed
@ymichael
ymichael deleted the bb/6748-ship-pr2-server-routes-daemon-failover-thr_8fyb466bfh branch November 23, 2026 22:04
ymichael added a commit that referenced this pull request Nov 25, 2026
…igation

Behind the serverRoutes experiment (#7864), the desktop app knows a remote
bb server by its serverId instead of one URL.

- One entry per server. When a saved custom or bb connect entry's server
  advertises routes, it becomes a routed server in server-target.json
  (routedServers, activeRoutedServerId, target "server"). Saved entries for
  the same server fold in (other custom URLs are probed for the same
  serverId), keeping the user's name. Adding any address of a known server
  lands in that entry. The address the user added stays a route when the
  server stops advertising it. Account servers already reached as a route
  are hidden from the list. With the experiment off the server advertises no
  routes and nothing changes.
- Pick on open with the daemon's rule, now shared in @bb/server-routes:
  ordered routes, parallel /health + serverId probes, 2 s timeout, rank i may
  win after i x 500 ms. The daemon resolver uses the same package. The race
  now also settles as soon as every better-ranked route has failed. The bb
  connect session is set up and sent only for the bb connect route.
- Fail over by navigation, no loopback proxy: the active route is checked
  every 10 s and right away on resume, app activation and a failed main-frame
  load. Two misses in a row pick again; every window loads the same path,
  query and hash on the new route's origin. No switching up while the route
  in use works; if no route answers the page stays and checks continue.
- Indicator: optional getServerRoute/onServerRouteChange/setServerRoute on
  the desktop contract; the native Server menu shows "<server> · via
  <route>" and a Route submenu (Automatic + each route); a toast on each
  switch (the preload holds the notice for the page that loads after the
  switch); command-palette actions to pin or unpin a route.
- Docs: multiple-devices.md (one entry per server, what a switch keeps),
  configuration.md and the customization guide (server-target.json fields and
  rollback).

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