Skip to content

fix(threads): keep live watches recovering after transport drops - #748

Merged
nixuuu merged 1 commit into
devfrom
fix/thread-view-event-loss
Nov 14, 2026
Merged

nixuuu merged 1 commit into
devfrom
fix/thread-view-event-loss

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 14, 2026

Copy link
Copy Markdown
Member

Remote prompts and even a desktop send could disappear from the desktop timeline while the overview kept updating. Three short native EventSource transport failures consumed the thread repair budget introduced in #673 and permanently stopped that watch; later online/focus events could not reopen it.

Transport loss now stays in the existing connection backoff (capped at 30 seconds), without exhausting the projection repair budget. Scoped gateway errors, invalid batches and premature stream completion retain their bounded repairs. Mobile applies the same distinction.

The visible thread is always included in overview head selection. Desktop and mobile compare that head with the view's own applied cursor, independently of the warmer's cache. After 10 seconds without progress, they resume incrementally from that cursor, preserving loaded history. Repairs are spaced by at least 30 seconds, capped at three per unchanged cursor, and recorded in Timeline recoveries with redacted numeric logs. No full snapshot is requested by this self-check.

Validation:

  • The real gateway + mobile Connect client + desktop SSE/reconnect/feed-state integration reproduces the pre-fix failure at the third transport loss. It covers remote sends, a local send during a drop, retained-view reopening, background warming, and a silent but open SSE connection. All reconnects preserve the cursor and add no snapshots.
  • 28 focused tests passed, 321 assertions; check:tmp left no test directories. Includes warming, input ordering, scoped failures, recovery limits and the head divergence guard.
  • bun install, gen:check, full workspace typecheck, lint, format:check and the production web build passed on the updated dev base. Checks used nice and a private temporary directory, removed afterward.
  • No full-suite run or physical Electron/iOS/Android sleep test. No UI layout or text changes; copy audit §8: no changed copy. Mobile changes are JavaScript only, with no native dependency/configuration or protocol change; no OTA publication, build or release was run.

Fixes: #673

Keep transport retry backoff separate from the thread projection repair budget. Detect a stalled applied cursor against overview heads and resume incrementally on desktop and mobile.

Fixes: #673
@nixuuu
nixuuu merged commit 995b944 into dev Nov 14, 2026
11 checks passed
@nixuuu
nixuuu deleted the fix/thread-view-event-loss branch November 14, 2026 19:23
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