Skip to content

perf(web): pause the runtime heartbeat while the window is hidden - #9883

Merged
nwparker merged 2 commits into
mainfrom
nwparker/perf-web-runtime-heartbeat-visibility
Jul 22, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/perf-web-runtime-heartbeat-visibility

Conversation

@nwparker

@nwparker nwparker commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

What

The web-runtime client (browser-paired runtime) kept a 10s heartbeat interval armed even while the window was hidden — waking every 10s to do nothing (~0.1 renderer wakeups/sec wasted while hidden).

Fix: route the heartbeat through installWindowVisibilityInterval so it stops while the window is hidden and re-arms on becoming visible. On re-arm it rebaselines liveness (lastInboundFrameAt / lastHeartbeatTickAt / outstanding-probe timestamp) so a long hidden gap isn't misread as a dead connection. The visible-idle probe cadence (~one every ~30s, from the 10s tick + 25s idle threshold) is unchanged.

ELI5

When Orca's browser-paired runtime connection was open but the window was hidden, it kept pinging every 10 seconds to check the connection was alive. Now it pauses while hidden and, when you come back, first re-syncs its "last seen" clock so it doesn't wrongly think the connection died while you were away.

Proof of zero regression

  • On becoming visible it rebaselines before the first tick, so a connection that was fine before hiding is never falsely dead-closed after a hidden gap; real inbound frames still update lastInboundFrameAt regardless of visibility.
  • Visible-idle cadence and the dead-close semantics (fires only on an unanswered sent probe) are unchanged.
  • Rapid visibility toggling arms once (no double-arm); socket-close / client-close teardown removes both the interval and the visibility listener.
  • Tests: web-runtime-client-heartbeat.test.ts 12/12 — hidden disarm, visible re-arm + rebaseline, rapid-toggle single-arm, no false dead connection after a long hidden gap, unchanged visible cadence, teardown cleanup. typecheck:web clean.

Proof of perf improvement

Removes ~0.1 renderer wakeups/sec while the window is hidden with a web-runtime connection open; the tests assert the interval is disarmed while hidden and re-armed (with rebaseline) on visible.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The WebSocket heartbeat now uses a visibility-aware interval with explicit cleanup. When the document becomes visible, heartbeat timestamps and pending probe state are re-baselined. Tests add deterministic timer and document-event mocks and cover hidden intervals, visibility restoration, stale-gap handling, visible cadence, and cleanup on client close.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change, but it does not follow the required template sections for this repository. Rewrite it with the required Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: pausing the runtime heartbeat while hidden.
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/renderer/src/web/web-runtime-client.ts (1)

767-787: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a "why" comment for the non-obvious runOnVisible install-time semantics.

installWindowVisibilityInterval invokes runOnVisible synchronously not only on hidden→visible transitions but also immediately on every startHeartbeat() install (per start() in window-visibility-interval.ts). That means rebaselineHeartbeat() fires on every fresh connect/reconnect too, not just after being hidden — non-obvious from reading startHeartbeat()/rebaselineHeartbeat() alone.

📝 Suggested comment
   private startHeartbeat(): void {
     this.clearHeartbeatTimer()
+    // Why: runOnVisible fires both on this initial install (while visible) and on every
+    // hidden→visible transition, so rebaselineHeartbeat() also runs on fresh connects —
+    // harmless since those timestamps are already fresh from the handshake.
     this.heartbeatCleanup = installWindowVisibilityInterval({
       run: () => this.runHeartbeatTick(),
       runOnVisible: () => this.rebaselineHeartbeat(),
       intervalMs: HEARTBEAT_INTERVAL_MS
     })
   }

As per coding guidelines, **/*.{ts,tsx}: "For non-obvious TypeScript code, add a concise comment explaining why the constraint exists, not how the code works."

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 14d90801-941f-4fa3-bb4e-a3d9d0068e95

📥 Commits

Reviewing files that changed from the base of the PR and between e600600 and d2aecfd.

📒 Files selected for processing (2)
  • src/renderer/src/web/web-runtime-client-heartbeat.test.ts
  • src/renderer/src/web/web-runtime-client.ts

nwparker and others added 2 commits July 22, 2026 15:40
…t re-arm

The visible re-arm rebaselined lastInboundFrameAt=now, so a socket that went
silent while the window was hidden looked freshly-heard-from and its death was
masked for another full idle window (~25s). Move the fresh-connect baseline into
startHeartbeat (the real 'we just connected' moment) and have the visible re-arm
only reset the tick clock + clear an in-flight probe, preserving lastInboundFrameAt
so the next visible tick probes a stale connection promptly and closes if unanswered.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the nwparker/perf-web-runtime-heartbeat-visibility branch from d2aecfd to 8141b2e Compare July 22, 2026 22:44
@nwparker
nwparker merged commit b65c593 into main Jul 22, 2026
1 check passed
@nwparker
nwparker deleted the nwparker/perf-web-runtime-heartbeat-visibility branch July 22, 2026 23:21
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ablyai#9883)

* perf(web): pause runtime heartbeat while hidden

Co-authored-by: Orca <[email protected]>

* fix(web): preserve inbound-liveness baseline across a hidden heartbeat re-arm

The visible re-arm rebaselined lastInboundFrameAt=now, so a socket that went
silent while the window was hidden looked freshly-heard-from and its death was
masked for another full idle window (~25s). Move the fresh-connect baseline into
startHeartbeat (the real 'we just connected' moment) and have the visible re-arm
only reset the tick clock + clear an in-flight probe, preserving lastInboundFrameAt
so the next visible tick probes a stale connection promptly and closes if unanswered.

Co-authored-by: Orca <[email protected]>

---------

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