Skip to content

Recover from an unreachable saved remote server target - #2161

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1494-remote-target-dead-end
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1494-remote-target-dead-end

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

applyServerTarget() in apps/desktop/src/main.ts awaited loadWindowUrl() for custom and connect targets with no handler. BrowserWindow.loadURL rejects for every network-level failure (ERR_CONNECTION_REFUSED, ERR_CONNECTION_TIMED_OUT, ERR_FAILED, ...). At launch that rejection unwound to runDesktopApp().catch(...), which rendered error.stack into the static, script-less error view: "Could not open bb" plus Electron internals and nothing to click. From Window > Server the same rejection was unhandled (void setActiveServerTarget(...)), so the window went blank and bbAppLoaded stayed true. bbAppLoaded was also set before the load was known to have succeeded.

Issue: #1494. Report: https://get-bb.github.io/reports/issues/1494.html

What changed

  • apps/desktop/src/remote-server-load.ts (new): loadRemoteServerPage() loads the remote URL and, on rejection, logs only the Electron error code plus the server origin and renders the shared startup error view: "Could not reach this bb server. The bb server at <origin> did not answer. Check that the machine is awake and reachable, then choose Window > Server to retry this server or switch to This Mac." describeServerUrl() prints only the origin, because a saved custom URL can carry a credential or a query token. A load the user already superseded (generation check) shows nothing.
  • apps/desktop/src/main.ts: both remote branches of applyServerTarget() go through a small loadRemoteServerTarget() wrapper around that helper. bbAppLoaded flips and the remote system-config sync starts only after the page loaded. A failed connect load stops the session renewal it had just started. refreshApplicationMenu() now runs on the failure path too.

Deviation from the issue's proposal: this PR does not add Retry / Use This Mac buttons. The data: error view runs under default-src 'none' with no preload, so buttons need a new main-process IPC surface (that is what the closed #1519 built, +768 lines). This PR fixes the root cause (an unhandled rejection) and gives the user the server name and the existing menu route; the error view now has parity with the builtin and Connect-authentication failures. Actionable buttons can follow as a separate UI change. No wire, CLI, or doc changes.

How you verified

Live repro on origin/main with the built desktop shell under Xvfb (Electron, Linux), saved custom target http://127.0.0.1:47771 with nothing listening:

BODY TEXT:
Could not open bb
Error: ERR_CONNECTION_REFUSED (-102) loading 'http://127.0.0.1:47771/' at rejectAndCleanup (node:electron/js2c/browser_init:2:89743) ...
interactive controls on page: 0

Re-selecting the same target from Window > Server on main: URL: chrome-error://chromewebdata/, empty body, 2x UnhandledPromiseRejectionWarning on stderr.

Same two steps on this branch:

BODY TEXT:
Could not reach this bb server
The bb server at http://127.0.0.1:47771 did not answer. Check that the machine is awake and reachable, then choose Window ▸ Server to retry this server or switch to This Mac. Logs are under .../logs/.

stderr: [desktop] could not load the bb server at http://127.0.0.1:47771: ERR_CONNECTION_REFUSED (-102), 0 unhandled rejections. Menu re-select shows the same named error instead of a blank window. Starting a server on 47771 and re-selecting the target from the menu then loads the page (URL: http://127.0.0.1:47771/).

Tests: apps/desktop/test/remote-server-load.test.ts (6 tests) covers the error view copy and title, that the stack never reaches the screen, credential/query redaction on screen and in the log, the superseded-load path, and the success path. With origin/main's main.ts and without the new module the file fails with Error: Cannot find module '../src/remote-server-load.js' (the code path under test did not exist; main.ts runs on import and is not unit-testable). apps/desktop/src/main.ts is exercised by the live repro above.

pnpm exec turbo run typecheck test --filter=@bb/desktop --force on the committed tree: Tasks: 5 successful, 5 total, Test Files 35 passed (35), Tests 228 passed (228).

Fixes #1494

AGENT GENERATED: by Claude Opus 5

Independent verification

Verified by a second agent on a fresh worktree (git fetch origin bb/fix-1494-remote-target-dead-end && git checkout -b verify-1494-r1 FETCH_HEAD, HEAD 2bb375108, origin/main is an ancestor, PR reports MERGEABLE).

Commands run:

  • pnpm install --frozen-lockfile --prefer-offline && pnpm exec turbo run build
  • Fail-before: git checkout origin/main -- apps/desktop/src/main.ts && rm apps/desktop/src/remote-server-load.ts, then pnpm exec vitest run test/remote-server-load.test.ts from apps/desktop -> FAIL ... Error: Cannot find module '../src/remote-server-load.js' (1 failed suite). The helper under test does not exist on main; main.ts runs on import, so the call site itself has no unit seam.
  • Pass-after: git checkout HEAD -- <those files>, same command -> Test Files 1 passed (1), Tests 6 passed (6).
  • pnpm exec turbo run typecheck test --filter=@bb/desktop --force -> Tasks: 5 successful, 5 total, Test Files 35 passed (35), Tests 228 passed (228).

Live repro (built Electron shell under xvfb-run, own --user-data-dir and BB_DATA_DIR under ~/.bb-dev/scratch/1494-verify/, saved custom target http://127.0.0.1:47771 with nothing listening, CDP on 49333, main-process inspector on 49334; the report's run-desktop-unreachable.sh, cdp-screenshot.mjs, and main-click-server-menu.mjs):

  • Built from origin/main's main.ts (bug confirmed): startup body Could not open bb / Error: ERR_CONNECTION_REFUSED (-102) loading 'http://127.0.0.1:47771/' at rejectAndCleanup (node:electron/js2c/browser_init...), 0 interactive controls. Window > Server > re-select same target: URL http://127.0.0.1:47771/, empty body, 2x UnhandledPromiseRejectionWarning on stderr.
  • Built from this branch: startup body Could not reach this bb server / The bb server at http://127.0.0.1:47771 did not answer. Check that the machine is awake and reachable, then choose Window ▸ Server to retry this server or switch to This Mac.; stderr [desktop] could not load the bb server at http://127.0.0.1:47771: ERR_CONNECTION_REFUSED (-102); 0 unhandled rejections. Menu re-select shows the same named error (no blank window). After starting an HTTP server on 47771, menu re-select loads http://127.0.0.1:47771/ (body fake bb server up), so the menu route recovers.

CI at verification time: 11 checks pass, 2 skipped (Node Compatibility Smoke, iOS simulator flows), none failing or pending.

Residual risks (none blocking): the unit test proves the helper's contract, not the applyServerTarget() call site (covered by the live repro only); the error view still has no clickable Retry / Use This Mac control; a half-open host still waits for Chromium's connect timeout before the named error appears (#1524); the loading view copy still says "Starting local services" for remote targets; the connect failure branch was checked by code reading only (no bb Connect server here); verified on Linux/Xvfb, not macOS (no platform branch in the changed code).

AGENT GENERATED: by Claude Opus 5

applyServerTarget() awaited BrowserWindow.loadURL for custom and connect
targets with no handler. When the host was down the rejection unwound to
the top-level startup handler, which printed the Electron stack on a
screen with no way out, and from the Server menu it was an unhandled
rejection that left a blank window.

Route both remote loads through loadRemoteServerPage(), which renders
the shared startup error view naming the server origin and the
Window > Server route to retry or switch to This Mac, logs only the
Electron error code (a saved URL can carry a credential), and shows
nothing for a load the user has already superseded. bbAppLoaded now
flips only after the page is up, and a failed connect load stops the
session renewal it had just started.

Fixes #1494

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 06:27
@SawyerHood
SawyerHood merged commit ad7edf8 into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1494-remote-target-dead-end branch August 21, 2026 14:39
SawyerHood added a commit that referenced this pull request Aug 21, 2026
## What was wrong

`createConnectServerSync().runSync()` in
`apps/desktop/src/connect-server-sync.ts` treated every empty outcome of
`fetchServers()` as `null`: no local runtime and no cached credential,
connect plugin disabled (HTTP 503), connect plugin unpaired (`{ code:
"handler_error", message: "not_paired" }`), server or gate unreachable,
and a refused credential. It logged one line and returned, so `main.ts`
was never told and never rebuilt the menu. `buildMenuServerItems()`
mapped an empty `connectAccountServers` and `createServerMenuItems()` in
`menu.ts` has no empty-state row, so Window > Server looked exactly like
an account with no servers. `fetchConnectAccountServers()` even parsed
`rpcFailureSchema` and discarded the result, although the server says
which case it is on the wire.

Issue: #1753. Report: https://get-bb.github.io/reports/issues/1753.html

## What changed

- `apps/desktop/src/connect-server-sync.ts`:
`fetchConnectAccountServers()` returns `{ ok: true, result } | { ok:
false, reason }` instead of `T | null`. The reason is derived from the
response: 503 is `plugin-disabled`, `handler_error`/`not_paired` is
`not-paired`, anything else (fetch threw, non-JSON, unknown shape) is
`unavailable`. The gate path returns `no-credential` when the app has
none, `unauthorized` when the gate refuses the credential (still calls
`onUnauthorized`), `unavailable` otherwise. New required callback
`onSkipped(reason)` fires for every skipped sync next to `onServers`.
The log line now names the reason and is written once per failure streak
plus once when the reason changes.
- `apps/desktop/src/main.ts`: stores the reason in
`connectServerSyncSkipReason`, clears it in `onServers`, and calls
`refreshApplicationMenu()` from `onSkipped` because Electron menus are
immutable once built. The menu gets the reason only when
`listMenuConnectServers()` is empty, so a persisted Connect selection
that is still listed gets no note under it.
- `apps/desktop/src/menu.ts`:
`InstallApplicationMenuArgs.connectServersSkipReason:
ConnectServerSyncSkipReason | null` (required, not optional). A non-null
reason renders one disabled row after the server radio items: `No
Connect servers — sign in to bb Connect` / `… Connect not paired on This
Mac` / `… Connect plugin disabled` / `… sign in to bb Connect again` /
`… could not reach bb Connect`.

No wire change between server and host daemon; the desktop to
local-server RPC request is unchanged, only the parse of its failure
body. No CLI or doc surfaces.

Deviation from the reporter's prototype
(`Joesirven/bb@prototype/connect-server-list-skip-reason`, no PR): the
reason is pushed through a callback that rebuilds the menu, instead of a
`getSkipReason()` polled at build time, so the row appears after the
sync that produced it rather than after the next unrelated rebuild.
Plugin-disabled and not-paired are told apart. The reason is set from
the outcome, never before the fetch resolves. No clickable "Sign in to
bb Connect…" item: that is a product decision and the issue asked for
the disabled row as the minimum.

Relation to #1494 / #2161: in the reporter's exact startup flow (saved
custom target that no longer answers) `applyServerTarget()` rejects on
`loadURL` before the startup `syncNow()` runs, so the first sync fires
from `menu-will-show` and the row shows from the second open of the
menu. With #2161 the startup sync is reached and the row is there on the
first open. This PR does not touch that flow.

## How you verified

Tests (`apps/desktop/test/connect-server-sync.test.ts`,
`apps/desktop/test/menu.test.ts`). With `git checkout origin/main --
apps/desktop/src/connect-server-sync.ts apps/desktop/src/menu.ts
apps/desktop/src/main.ts` and the new tests: `Tests 7 failed | 8 passed
(15)`, including

```
× explains an empty Connect list with a disabled row when the sync was skipped
  TypeError: Cannot read properties of undefined (reading 'no-credential')
× reports no-credential without calling the gate when the app has none
  AssertionError: expected [] to deeply equal [ 'no-credential' ]
× names the reason on network failure, plugin disabled, or not paired
  AssertionError: expected null to deeply equal { ok: false, reason: 'unavailable' }
× reports a refused credential so the caller drops it
  AssertionError: expected [] to deeply equal [ 'unauthorized' ]
```

On this branch the same files pass (`Tests 15 passed (15)`).

`pnpm exec turbo run typecheck test --filter=@bb/desktop --force` on the
committed tree (`git status --porcelain` empty): `Tasks: 5 successful, 5
total`, `Test Files 34 passed (34)`, `Tests 224 passed (224)`.

Manual: rendered the real `menu.ts` inside Electron 41 under Xvfb with
the report's harness, using the `servers` array `main.ts` produces for
the reporter's state (saved custom target, no local runtime, no
credential). Before: `This Mac | old-host.tailnet.ts.net:38886 |
<separator> | Set Server URL…`. After, with `connectServersSkipReason:
"no-credential"` as `main.ts` now passes it after the skipped sync:
`This Mac | old-host.tailnet.ts.net:38886 | No Connect servers — sign in
to bb Connect | <separator> | Set Server URL…`, the new row greyed out
and not clickable.

The server-side shapes the parser matches were checked against
`apps/server/src/routes/plugins.ts` (503 + string `error` for a plugin
that is not running) and
`apps/server/src/services/plugins/plugin-service.ts` +
`plugins/connect/src/rpc.ts` (`{ code: "handler_error", message:
error.message }` where the handler rethrows `ConnectListError.code`),
and match the report's live probe.

Fixes #1753

> AGENT GENERATED: by Claude Opus 5


## Independent verification

Checked out `d4d3412ef` (`origin/main` is an ancestor; `mergeable:
MERGEABLE`) in a fresh worktree, read the issue and the report first,
and verified the wire shapes the new parser matches against
`apps/server/src/routes/plugins.ts` (503 + string `error` for
`not-running`), `plugin-service.ts` (`{ code: "handler_error", message
}` with the raw thrown message) and `plugins/connect/src/rpc.ts` (`throw
new Error(error.code)` for `ConnectListError`).

Fail-before / pass-after:

```
git checkout origin/main -- apps/desktop/src/connect-server-sync.ts apps/desktop/src/main.ts apps/desktop/src/menu.ts
cd apps/desktop && pnpm exec vitest run test/connect-server-sync.test.ts test/menu.test.ts
  Tests  7 failed | 8 passed (15)
  AssertionError: expected null to deeply equal { ok: false, reason: 'unavailable' }
  AssertionError: expected [] to deeply equal [ 'no-credential' ]
  AssertionError: expected [] to deeply equal [ 'unauthorized' ]
  TypeError: Cannot read properties of undefined (reading 'no-credential')
git checkout HEAD -- <same files>
  Tests  15 passed (15)
```

`pnpm exec turbo run typecheck test --filter=@bb/desktop --force` on the
clean committed tree: `Tasks: 5 successful, 5 total`, `Test Files 34
passed (34)`, `Tests 224 passed (224)`.

Live wire check (own dev instance, not mocks): ran the branch's
`fetchConnectAccountServers` + `createConnectServerSync` with `tsx`
against the real local server. Connect enabled but unpaired (HTTP 500
`{"code":"handler_error","message":"not_paired"}`) ->
`onSkipped(["not-paired"])`; `bb plugin disable connect` (HTTP 503) ->
`onSkipped(["plugin-disabled"])`; closed port ->
`onSkipped(["unavailable"])`. One log line each.

Repro on the fixed branch: bundled the branch's real
`connect-server-sync.ts` + `menu.ts` into Electron 41 under Xvfb, wired
`onSkipped`/`onServers` to rebuild the application menu the way
`main.ts` does, and fired `onListRequested()` (the `menu-will-show`
trigger, the only one reached in the reporter's flow). Before the sync:
`This Mac | old-host.tailnet.ts.net:38886 | <separator> | Set Server
URL…`. After: `This Mac | old-host.tailnet.ts.net:38886 | No Connect
servers — sign in to bb Connect | <separator> | Set Server URL…`, the
new row greyed (`enabled: false`), 2 menu builds. Same harness with the
live dev server as the local runtime and the plugin disabled: `This Mac
| No Connect servers — Connect plugin disabled | <separator> | Set
Server URL…`. No longer reproduces.

CI: all required checks pass (Checks, Tests x6, Package Smoke ubuntu +
macos-15, version check); iOS/Node-compat smoke skipped by path filter.

Residual risks (none blocking): every desktop user whose bundled server
is not paired to Connect now sees the greyed `Connect not paired on This
Mac` row in Window > Server, which is the behaviour the issue asks for
but is visible beyond the reporter's case; `onSkipped` rebuilds the menu
on every skipped sync (every 10 min and on throttled menu open),
mirroring the existing `onServers` path; after switching targets the
previous reason can show for the sub-second window until the new sync
completes; `main.ts` wiring is verified by reading plus the harness
above, not by a unit test. The startup sync is still not reached when
the saved custom target is dead (#1494 / #2161), so in that exact flow
the row appears from the second open of the menu.

> AGENT GENERATED: by Claude Opus 5

Co-authored-by: Claude <[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.

Desktop: an unreachable saved remote server target leaves the app on a dead-end startup error

1 participant