Skip to content

perf(mobile): stop polling live worktree names - #8051

Merged
nwparker merged 4 commits into
stablyai:mainfrom
nwparker:nwparker/perf-6
Jul 11, 2026
Merged

nwparker merged 4 commits into
stablyai:mainfrom
nwparker:nwparker/perf-6

Conversation

@nwparker

Copy link
Copy Markdown
Contributor

Summary

Description

Stop polling worktree.show every 3 seconds while a mobile session is focused. The mobile title hook now:

  • performs one initial authoritative read;
  • subscribes to the existing runtime.clientEvents stream;
  • refreshes only for the current repo's worktreesChanged event or a provider-neutral reposChanged event;
  • re-reads once after a reconnect because client events are not queued while disconnected;
  • retains the 3-second poll until the stream is proven available and a read succeeds, preserving older-runtime and transient-failure behavior; and
  • explicitly unsubscribes the server-side event listener, including the dispose-before-ready race.

Evidence — this is a performance issue

The old request-volume test was run against the pre-fix hook with fake timers:

30 seconds focused and idle
= 1 initial worktree.show request
+ 10 interval requests at 3-second cadence
= 11 worktree.show requests

That RPC is not a cheap metadata lookup. The runtime call chain is:

worktree.show
→ showManagedWorktree()
→ resolveWorktreeSelector()
→ listResolvedWorktrees()
→ computeResolvedWorktrees()
→ every configured repo:
   local: listRepoWorktrees(...)
   SSH:   provider.listWorktrees(...)

The resolved-worktree cache TTL is 1 second, shorter than the 3-second mobile poll, so an otherwise-idle poll can miss the cache every time. With R configured repos, the 10 idle ticks above can therefore cause up to 10 × R local Git or SSH worktree-list scans every 30 seconds.

The committed regression test now proves:

Focused mobile session, 30 seconds Before After
worktree.show RPCs while idle 11 1
Recurring idle RPCs after stream readiness 10 0
First-window request reduction — 90.9%

It also proves that an unrelated repo event causes 0 requests, while the current repo event causes exactly 1 immediate refresh. The compatibility test proves an older runtime still makes 4 requests over the first 9 seconds (initial + three fallback ticks), matching the old cadence.

Reproduce the focused after measurement:

pnpm --dir mobile test src/session/use-live-worktree-name.test.ts --reporter=verbose

ELI5

The phone used to ask the desktop “did this workspace's name change?” every three seconds. Answering could make the desktop check every local and remote repo. Now the desktop rings a bell when workspace metadata changes, and the phone asks only then. If the desktop is too old to have the bell, the phone keeps using the old check.

Trade-offs / end-user regressions

  • No expected user-visible regression. Modern runtimes update names immediately on the existing invalidation event instead of waiting up to 3 seconds.
  • A focused session holds one lightweight event subscription. It is removed on blur/unmount; tests cover cleanup both before and after the server returns the subscription ID.
  • Older desktop runtimes receive no performance improvement because the compatibility fallback intentionally keeps the old 3-second cadence.
  • A failed initial read also keeps the fallback active until a read succeeds, so this does not trade correctness for fewer requests.

Screenshots

No visual change.

Testing

  • pnpm --dir mobile lint
  • pnpm --dir mobile exec tsc --noEmit -p tsconfig.json
  • pnpm --dir mobile test — 187 files passed; 1,321 tests passed; 2 skipped
  • pnpm --dir mobile format:check
  • pnpm check:max-lines-ratchet
  • Added deterministic request-count, old-runtime fallback, retry, reconnect, and subscription-cleanup regressions
  • pnpm build — not run; no dependency, native, route, or packaging changes

AI Review Report

Reviewed the request hot path, cache TTL, per-repo fanout, focus lifecycle, reconnect replay, stale async responses, older-runtime behavior, and server-listener cleanup.

The review found two risks and the implementation addresses both:

  1. Older desktops do not expose the event stream. Polling now stops only after a ready event and a successful read; an RPC error leaves the original fallback running.
  2. Disposal can race the stream's ready response. The RPC client retains a cancelled tombstone until it receives the subscription ID, then sends the matching runtime.clientEvents.unsubscribe without delivering the late event.

Cross-platform review: no shortcuts, labels, filesystem paths, shell behavior, or Electron platform branches changed. Worktree IDs use the shared cross-platform parser. The event is provider-neutral and the avoided scan covers both local and SSH providers, so macOS, Linux, Windows, SSH, and non-GitHub git providers retain the same behavior.

Security Audit

  • Uses the existing authenticated/encrypted runtime RPC channel and existing event/unsubscribe methods.
  • Adds no command execution, path access, auth state, secrets, logging, dependencies, or persisted data.
  • The event's repoId is used only for equality against the repo parsed by the shared worktree-ID utility.
  • No security follow-up is required.

Notes

Mobile backwards-compatibility verdict: compatible. Persisted keys, route/deep-link shapes, required RPC payloads, and shared desktop behavior are unchanged. The new subscription is additive and feature-detected by its ready response.

@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: f3b9755c-1941-4d11-b482-4f8a3716eafd

📥 Commits

Reviewing files that changed from the base of the PR and between cd11d30 and 670c59d.

📒 Files selected for processing (3)
  • src/main/ipc/worktrees.test.ts
  • src/main/ipc/worktrees.ts
  • src/main/runtime/orca-runtime.ts

📝 Walkthrough

Walkthrough

The RPC client now generalizes streaming readiness handling and disposes runtime client-event subscriptions through the corresponding unsubscribe method, including subscriptions cancelled before readiness. The live worktree-name hook now coordinates event readiness, repository-scoped refreshes, fallback polling, stale response handling, reconnect refreshes, and cleanup. Worktree renames now invalidate cached data and notify remote clients, while read-state updates remain notification-free. New tests cover these behaviors.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: reducing polling for live worktree names on mobile.
Description check ✅ Passed The description matches the required template and includes summary, screenshots, testing, AI review, security audit, 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.

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.

nwparker and others added 3 commits July 10, 2026 22:32
worktrees:updateMeta deliberately skips the renderer notifier (PR stablyai#209),
but paired mobile clients no longer poll for titles, so a manual rename
would never reach them. Emit the remote-only worktreesChanged client
event (with resolved-cache invalidation), gated on displayName so
per-click isUnread writes stay event-free.

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

getRepoIdFromWorktreeId matches the mobile client's event filter exactly
and cannot throw after the meta write already persisted.

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

Copy link
Copy Markdown
Contributor Author

Reviewed and validated. Added three commits on top: manual desktop renames now push a remote-client-only worktreesChanged (the updateMeta path intentionally skips renderer notification, which the removed poll was masking), plus a typecheck fix and a non-throwing repoId parse. Request-count, old-runtime fallback, reconnect, and unsubscribe-race behavior all covered by deterministic tests; rename flow verified end-to-end in the dev app.

@nwparker
nwparker merged commit 4f7fe78 into stablyai:main Jul 11, 2026
3 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* perf(mobile): replace worktree name polling with events

* fix(worktrees): push rename invalidation to remote clients

worktrees:updateMeta deliberately skips the renderer notifier (PR stablyai#209),
but paired mobile clients no longer poll for titles, so a manual rename
would never reach them. Emit the remote-only worktreesChanged client
event (with resolved-cache invalidation), gated on displayName so
per-click isUnread writes stay event-free.

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

* test(worktrees): add missing runtimeStub type member for typecheck

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

* fix(worktrees): derive rename event repoId with the shared non-throwing parser

getRepoIdFromWorktreeId matches the mobile client's event filter exactly
and cannot throw after the meta write already persisted.

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