Skip to content

perf(remote): coalesce session.tabs snapshot fan-out under title/status churn - #8092

Closed
Jinwoo-H wants to merge 1 commit into
mainfrom
fix/session-tabs-snapshot-churn-coalescing
Closed

Jinwoo-H wants to merge 1 commit into
mainfrom
fix/session-tabs-snapshot-churn-coalescing

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Spinner-in-title agents (e.g. the Claude Code braille spinner) flip a PTY title several times per second. On every flip, touchMobileSessionSnapshotsForPty bumped each PTY-backed snapshot's snapshotVersion and immediately called notifyMobileSessionTabsChanged, which rebuilds the session.tabs result and fans it out to every subscriber — where the ws layer then JSON.stringifys it per client. That is O(clients × snapshot size) of stringify/emit work on every title tick, with no debounce, even though clients only ever apply the newest snapshotVersion per worktree. (For contrast: files.watch batches at 150ms; native-chat debounces at 40ms.)

This routes the title/status churn path through a small purpose-named per-worktree trailing-edge coalescer (mobile-session-tabs-notify-coalescer.ts, modeled on file-watch-event-batcher.ts). The emit re-reads the latest stored snapshot, so collapsing intermediate versions is safe and correct — clients gate on snapshotVersion freshness (shouldApplyWebSessionTabsSnapshot), so a dropped intermediate version is exactly what they'd discard anyway.

Chosen delay: 50ms trailing window, 250ms max-wait cap. Title/status is latency-sensitive UI, so this is tighter than files.watch's 150ms but looser than native-chat's 40ms; the 250ms cap guarantees a title that never stops spinning still emits at least every 250ms rather than starving indefinitely.

Correctness constraints honored:

  • First emit is not delayed unboundedly — trailing coalesce + 250ms max-wait cap.
  • snapshotVersion monotonicity preserved — the stored map is bumped per touch as before; only the emit is coalesced, and it always reads the freshest stored version.
  • Structural changes (tab add/remove/activate) still propagate promptly: notifyMobileSessionTabsChanged emits immediately and cancels any pending coalesced notify so it can't re-emit a superseded frame.
  • Pending notify flushes on subscription close (so a subscriber closing mid-window still gets the final state) and is cancelled on worktree removal (so a stale snapshot can't land after the removed:true frame).

Before / after emit counts

Deterministic repro: seed one PTY-backed worktree snapshot, register a session.tabs listener, flip the title 20× within ~200ms (10ms apart), then let the window elapse.

Scenario Emits before Emits after
20 rapid title flips, one worktree 20 1
title churn + a structural change 2 (churn emit + structural emit) 1 (structural emit; coalesced churn cancelled)
pending window + subscriber closes eager emit per flip 1 flush on close

The three integration tests all fail on main (pre-fix: emit-per-flip) and pass with this change — verified by stashing the runtime wiring and re-running.

Screenshots

No visual change.

Testing

  • pnpm lint (oxlint clean on all changed files; max-lines ratchet OK)
  • pnpm typecheck (exit 0)
  • pnpm test — new mobile-session-tabs-notify-coalescer.test.ts (8/8), new churn cases in mobile-subscribe-integration.test.ts, plus session-tabs* + web-session-tabs-sync suites (84/84)
  • pnpm build
  • Added high-quality tests that would catch regressions (repro proven to fail on main)

AI Review Report

Reviewed: (1) coalescing correctness against the client freshness gate — confirmed shouldApplyWebSessionTabsSnapshot only accepts a strictly-newer snapshotVersion within the same publicationEpoch, so dropping intermediate versions cannot regress client state; (2) no unbounded first-emit delay (250ms max-wait cap); (3) structural-change promptness (immediate emit + cancel-pending); (4) lifecycle leaks — timers are unref()'d, cancelled on worktree removal, and flushed on subscription close. Cross-platform: pure JS timers (setTimeout/unref), no path/shell/shortcut/Electron surface touched — identical on macOS/Linux/Windows. SSH/remote: this is on the shared-control emit path used by both SSH remote-runtime and remote Orca Server, and reduces per-client stringify fan-out on exactly that connection.

Security Audit

No new input handling, command execution, path handling, auth, secrets, dependency, or IPC surface. The change only debounces an existing internal notification; payload contents are unchanged. No follow-up needed.

Notes

The notifyMobileSessionTabsChanged(undefined) full-fan-out path (used during broad navigation) is intentionally left immediate and does not cancel per-worktree pending notifies; a subsequent coalesced emit would re-publish the same-or-newer version, which the client freshness gate discards — no correctness impact. A dispose() is provided on the coalescer for teardown/tests; the runtime is process-lifetime and its timers are unref()'d, so no explicit runtime-level dispose call is required.

Made with Orca 🐋

…us churn

Spinner-in-title agents (e.g. the Claude Code braille spinner) flip a PTY
title several times per second. Each flip called touchMobileSessionSnapshotsForPty,
which bumped every PTY-backed snapshot's version and immediately fanned a fresh
session.tabs emit out to every subscriber — where the ws layer JSON.stringifies
it per client. That is O(clients × snapshot size) of churn work with no debounce,
while files.watch batches at 150ms and native-chat debounces at 40ms.

Route the title/status churn path through a small per-worktree trailing-edge
coalescer (50ms window, 250ms max-wait cap). Clients gate on snapshotVersion
freshness, so collapsing intermediate versions is safe — the emit re-reads the
latest snapshot, so only the freshest version ships. Structural changes
(tab add/remove/activate) still emit immediately and cancel any pending
coalesced notify. Pending notifies flush on subscription close and are dropped
on worktree removal so no final state is lost or lands stale.
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e8f49edc-c8d5-440f-9329-f7958456b193

📥 Commits

Reviewing files that changed from the base of the PR and between 2622419 and d5809ee.

📒 Files selected for processing (4)
  • src/main/runtime/mobile-session-tabs-notify-coalescer.test.ts
  • src/main/runtime/mobile-session-tabs-notify-coalescer.ts
  • src/main/runtime/mobile-subscribe-integration.test.ts
  • src/main/runtime/orca-runtime.ts

📝 Walkthrough

Walkthrough

Adds a per-worktree coalescer for mobile session tab notifications with trailing delays and a maximum wait. Runtime title and status updates are coalesced, while structural changes emit immediately and cancel pending notifications. Pending notifications are flushed during listener teardown and canceled when worktrees are removed. Unit and integration tests cover scheduling, flushing, cancellation, disposal, churn, and subscription behavior.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: coalescing session.tabs fan-out under title/status churn.
Description check ✅ Passed The description follows the template and covers summary, screenshots, testing, AI review, security, and notes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jinwoo-H

Copy link
Copy Markdown
Contributor Author

Superseded by #8141, which combines all ten remote-reliability audit fixes, incorporates the valid CodeRabbit follow-ups, and has full CI plus live Electron/SSH validation. Closing this standalone PR so #8141 is the single review target; this fix remains included there.

@Jinwoo-H Jinwoo-H closed this Jul 10, 2026
@AmethystLiang AmethystLiang added the orca-remote-server Remote Orca Server / orca serve / paired remote runtime label Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

orca-remote-server Remote Orca Server / orca serve / paired remote runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants