Skip to content

fix(transport): detect dead pinned gateway sessions - #777

Merged
nixuuu merged 1 commit into
devfrom
fix/pinned-transport-keepalive
Nov 15, 2026
Merged

nixuuu merged 1 commit into
devfrom
fix/pinned-transport-keepalive

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

A desktop app paired with a remote gateway sometimes stopped showing thread history updates (often while a phone was also on the thread). The sidebar and thread summary stayed current; Cmd+R brought back the full history.

Pinned gateway connections (routedPinnedTransport: remote desktop BFF, desktop control and its worker) sent no HTTP/2 PINGs. connect-node's default ping interval is infinite, so it never verified a session. A session left half-open by sleep, a Tailscale route change or a network switch looked alive forever. Its streams stopped silently, and new streams on that session (including the thread watch's own stall repair) queued without an error. Meanwhile the BFF kept the browser's event stream alive with its own SSE heartbeats, so the UI stayed "connected". The overview stream could stay healthy on another route's session, which is why the sidebar kept updating.

These transports now ping every 10 s, like the local BFF (apps/web/src/lib/server/gateway.ts). A failed PING (connect-node's 15 s timeout) ends the session. Its streams fail, and the existing watch recovery reconnects on a fresh session.

The gateway logs from the incident show the server ending several network access sessions with session_error while that thread was running. I did not find a direct path from the phone's separate session to the desktop's streams.

Not linked to an earlier PR: no recent change introduced this. It goes back to when pinned transports were added.

Validation: bun install, bun run gen:check, bun run typecheck, bun run lint, bun run format:check, transport tests. Full TMPDIR=/var/tmp bun run check:tmp: 3166 passed, 175 skipped, 2 failed, nothing left in the temp dir. Both failures (worker-profile-env.test.ts "adding variables replaces only the instance process", codex/connections.test.ts "keeps full history when resuming…") fail the same way on a clean origin/dev checkout. The locally installed Codex CLI rejects a provider change on resume. Both pass in CI. No automated test of a half-open session: it needs a traffic-dropping proxy and runs ≥25 s.

🤖 Generated with Claude Code

Pinned gateway connections (remote desktop BFF, desktop control and its
worker) sent no HTTP/2 PINGs. A session left half-open by sleep or a
route change looked alive forever: its streams stopped silently and new
streams queued on it, while the BFF kept the browser's event stream
alive with its own heartbeats. Ping every 10 s, like the local BFF.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nixuuu
nixuuu marked this pull request as ready for review November 15, 2026 01:55
@nixuuu
nixuuu merged commit 61058b0 into dev Nov 15, 2026
13 checks passed
@nixuuu
nixuuu deleted the fix/pinned-transport-keepalive branch November 15, 2026 01:55
nixuuu added a commit that referenced this pull request Nov 15, 2026
)

## Problem

In the desktop app the open thread view could keep showing old state (a
just-sent prompt missing, a finished turn still shown as working) while
the sidebar and thread summary kept updating. Only a reload (Cmd+R)
recovered it. The view stayed "live": no sync indicator, no error,
composer enabled. Mobile never had this. The renderer console showed
hundreds of `derived_inert` warnings.

## Root cause

Retained thread views (`threadViews`, up to 12 threads) are created
inside the thread screen's effect (`openThreadMemory` in a `$derived` on
the page). `ThreadState`'s `$derived` fields (`running`, `ownTurns`,
`ownEntries`, `queued`, `pending`, `steering`, …) belong to that effect.
When the screen is destroyed (leaving for settings or the overview),
Svelte 5 keeps returning their last value (`derived_inert`,
`execute_derived` in svelte 5.57). After a return, the stream still
applied updates and advanced the cursor, so no recovery fired, but
everything read through those fields was frozen. Reproduced in
isolation: a class `$derived` created in an effect stays at its old
value after the effect is destroyed and the `$state` changes, while a
getter returns the new value.

**Fix:** the fields are now getters over `$state`, memoized on input
identity (`lib/shared/last-result.ts`). They stay live in any reader
regardless of where the view was created, and keep a stable result for
unchanged inputs. `docs/client-performance.md` records the rule:
retained state must not hold `$derived`.

## Also in this PR (recovery and transport hardening)

- **Liveness never gives up**
(`packages/domain/src/thread-watch-liveness.ts`). The overview-head
self-check stopped for good after three repairs without progress, and
never reloaded. Now it resumes from the applied cursor twice, then
reloads a fresh window as a page reload would, backing off from 30 s up
to 5 min while the evidence lasts.
- **Second witness: overview thread status** (`history.svelte.ts`,
thread page). If, for 10 s, the live overview reports `running` while
the view has no running turn, or `idle`/`interrupted`/`failed` while the
view still runs one, the same repair starts even when the cursor looks
current. This is the signal the user saw.
- **Pinned remote transport reuses the verified route**
(`packages/transport/src/routed-pinned.ts`). Every RPC used to race
fresh TLS probes to every address first: tens of thousands of aborted
handshakes a day on the gateway, extra latency, and failures on a slow
link while the session was fine. Now a verified route is reused (each
new connection still checks the pin in `client.ts`). It is probed again
after `Unavailable`, after a session closes under a call the caller did
not abort, when routes change, and in the background at most every 30 s.
Concurrent probes are shared.
- **SSE transport loss is not a thread failure** (`sse.ts`,
`gateway-errors.ts`). When the gateway connection breaks without
`canopy-error-scope` (`canceled`, `unavailable`, `internal`, e.g. a
gateway restart), the BFF closes the response, so the browser reconnects
with backoff. Before, it sent `failure`, and three of those stopped the
watch.
- Docs: `ui-api.md`, `client-performance.md`, `mobile.md`,
`desktop-remote-frontend.md`. Mobile shares the liveness class: it keeps
resuming incrementally, with backoff and no cap.

Related: #748, #777

## Verification

Ran locally:
- `check:tmp` on `packages/domain/src/thread-watch-liveness.test.ts`.
- `check:tmp` on the integration tests `desktop-clients` (new: route
reuse without per-call probes, then replacement after the route dies),
`thread-watch-failures` (new: SSE closes without `failure` when the
gateway session ends), `thread-client-continuity`, `remote-threads`,
`gateway-chatter`, `desktop-version-skew`, `mobile-network-port`,
`desktop-updates`, `paired-app-updates`.
- Typecheck of `packages/transport`, `packages/domain` and `apps/web`
(svelte-check).
- oxlint and oxfmt on the changed files.

- After the root-cause fix: `apps/web` typecheck (svelte-check), oxlint
and oxfmt on the changed files. There is no Svelte test harness for
`.svelte.ts` state and web UI tests are out of policy; the mechanism was
confirmed with a standalone compile of a minimal module against the
repo's Svelte. No visual change.

Not run: the full CI gate (left to CI) and a physical remote desktop
session. The repair path will log `Thread watch resumed from applied
cursor` / `Thread watch reloaded` in the renderer console.

## Copy audit (docs/copy.md §8)

No UI text changes: only console diagnostics were added. Nothing to
audit.

🤖 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