Skip to content

perf(remote): stop re-sending an unchanged catalog on a timer - #841

Merged
huichen merged 2 commits into
mainfrom
perf/lean-remote-lane
Sep 25, 2026
Merged

huichen merged 2 commits into
mainfrom
perf/lean-remote-lane

Conversation

@huichen

@huichen huichen commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Problem

An idle foreground phone received the full session list (~15 KB) and workspace list (~2 KB) every 20 seconds even when the directory had not changed, on top of a 1 Hz presence heartbeat:

Lane Before Per day
presence 232 B @ 1s 20.0 MB
state.sessions 14.9 KB @ 20s 64.5 MB
state.workspaces 2.1 KB @ 20s 9.0 MB
Idle total 93.5 MB

That is idle traffic — more than a heavy run's entire coalesced live lane. The 20s re-send existed as the only self-heal for a snapshot dropped on NATS core (at-most-once), because the phone does not poll the catalogue on a healthy link.

Approach

Replace the timer with a revision advertised on the presence heartbeat.

  • The heartbeat carries catalogVersion {epoch, sessions, workspaces}, read O(1) from the revision cache without touching the store.
  • A client whose applied revision is older pulls the catalogue itself (list_sessions / list_workspaces).
  • Presence cadence 1s → 3s. Liveness only has to beat inside the client's 15s receipt window (5x margin).
  • The catalog change-detection tick stays at 1s, so a session created by the TUI, the future CLI or loop still reaches the phone as fast as before.
Before After
Idle total (1 phone, foreground) 93.5 MB/day ~7.0 MB/day (−92.5%)
Worst-case recovery from a dropped push 20s ≤3s (one heartbeat)
Unchanged snapshot re-sends 4,320/day 0

The revision only advances when the snapshot's visible content changes, and it is recomputed from the store on every catalog tick rather than accumulated in a dirty flag — so a stale advertised revision (which would wedge a client permanently) is not reachable.

A real bug this uncovered

secure::publish dropped the payload silently while returning Ok(()) whenever transport.seal() yielded None — i.e. while the secure channel has no established key. That window occurs twice: before the client's handshake completes, and again after a credential refresh cleared the channel.

A periodic publisher never notices (the next tick carries the same content), which is exactly how the old 20s self-heal masked it. A change-driven publisher loses that change permanently. This surfaced immediately as a failing start_runs_the_full_bridge_and_stop_winds_it_down, and the fix is what the change depends on:

  • publish now reports whether the payload actually left the endpoint.
  • Snapshots advance their signature only once sent; otherwise the next tick retries. This is "retry until delivered" rather than "send once and hope" — strictly more accurate than the timer it replaces.

Compatibility

No wire-format change and no new capability bit. An older desktop keeps publishing presence without catalogVersion; the client ignores an absent revision. An older client ignores the new field and keeps its existing request/arrival fencing, unchanged. get_presence still returns the full snapshot, so a fresh subscribe is never left waiting on a heartbeat.

Verification

  • Desktop remote module: 252 passed / 0 failed. Mobile: 128 suites / 2042 tests pass. cargo fmt --check, cargo clippy --all-targets -D warnings, tsc --noEmit, eslint clean.
  • New idle_catalog_advertises_a_revision_instead_of_resending pins both halves of the contract, and asserts the heartbeat stays under 512 B (measured 242 B) so a regression that ships the directory inside it fails here instead of as a silent cost.
  • Both new assertions were falsified: restoring the 20s re-send fails "an unchanged catalog must not be re-sent on a timer"; dropping the "already current" comparison fails "a presence revision newer than the applied snapshot pulls, a current one does not".
  • docs/internals/desktop/CONNECTION.md updated, along with the comments that still described the removed 20s behaviour.

Not verified

Payload sizes and broker-side behaviour are measured; a real phone against a real NATS broker over a full day is not. That remains a follow-up.

Follow-ups (deliberately out of scope)

Trimming history/live payload content (reasoning text, tool arguments, per-entry field trimming) is more invasive than it looked and is not in this PR: entry.role is read directly by the mobile projector, so removing it would break an older client and requires a per-client capability-gated rewrite layer in the desktop's history relay, which is currently pure pass-through.

An idle foreground phone received the full session list (15 KB) and
workspace list (2 KB) every 20s even when the directory had not changed:
73.5 MB/day per phone, on top of a 20 MB/day presence heartbeat. The
snapshots are change-driven now, and the heartbeat carries the revision
that replaces the timer.

- The presence heartbeat advertises `catalogVersion` {epoch, sessions,
  workspaces}, read O(1) from the revision cache without touching the
  store. A client whose applied revision is older pulls the catalogue
  itself, so a snapshot dropped on the at-most-once lane heals within one
  heartbeat instead of 20s — strictly faster than the timer it replaces.
- Presence cadence 1s -> 3s. Liveness only has to beat inside the client's
  15s receipt window (5x margin). The catalog change-detection tick stays
  at 1s, so a session created by the TUI, the CLI or loop still reaches
  the phone as fast as it did before.
- `secure::publish` now reports whether the payload actually left. Sealing
  yields nothing while the channel has no established key — before the
  client's handshake, and again after a credential refresh cleared it —
  and the payload was dropped silently while still returning Ok. A
  change-driven publisher has to see that to retry; the periodic one this
  replaces never noticed, which is exactly how the old self-heal masked
  the drop. Snapshots now advance their signature only once sent.
@huichen
huichen enabled auto-merge (squash) September 25, 2026 15:12
@huichen
huichen merged commit a98b251 into main Sep 25, 2026
12 checks passed
@huichen
huichen deleted the perf/lean-remote-lane branch September 25, 2026 15:22
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