Repository navigation
perf(worktree): park the git-common existence poll while the window is hidden - #10528
Conversation
…s hidden startGitCommonNarrowWatch was the only watch entry point that never received WorktreePollerWindowVisibility, so its `worktrees/` existence poll kept stat'ing every repo without a linked worktree forever in the background (0.5 stat/sec/repo at the 2s default). Its siblings — the primary-metadata snapshot poller and the non-darwin git-common polling — already park. Threads visibility through and matches the snapshot poller's park/re-arm pattern: the poll stops on the first hidden tick, and re-checks immediately on onWindowBecameVisible so a worktrees dir created while hidden still upgrades to the native stream and emits its create event. The visibility listener is dropped in the dispose path. darwin-only: the narrow watch is the `platform === 'darwin'` branch.
📝 WalkthroughWalkthroughDarwin worktree existence polling now receives window visibility state, pauses while hidden, and immediately retries when the window becomes visible. The watch removes its visibility listener during unsubscription. Tests add filesystem stat tracking, multi-listener visibility handling, and coverage for hidden polling, visible resumption, event forwarding, continued polling, and listener cleanup. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/ipc/worktree-git-common-watch.ts (1)
211-221: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low valueConsider swallowing rejections on the
void-ed upgrade promise.
tryUpgradeToNarrowWatchonly rejects if theonEventsconsumer throws, but.finally()re-throws it on a promise nobody handles — an unhandled rejection in the main process. Re-arming still happens either way.♻️ Proposed guard
- void tryUpgradeToNarrowWatch().finally(() => { - armExistencePoll() - }) + void tryUpgradeToNarrowWatch() + .catch(() => {}) + .finally(() => { + armExistencePoll() + })
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 2d3bc0b6-4816-417c-bc85-d25bef085691
📒 Files selected for processing (2)
src/main/ipc/worktree-git-common-watch.test.tssrc/main/ipc/worktree-git-common-watch.ts
|
Re: CodeRabbit — "Consider swallowing rejections on the Declining, with reasoning. The observation is technically correct:
Behavior is unaffected either way: Leaving as-is for scope. Verified alongside this: |
…s hidden (stablyai#10528) startGitCommonNarrowWatch was the only watch entry point that never received WorktreePollerWindowVisibility, so its `worktrees/` existence poll kept stat'ing every repo without a linked worktree forever in the background (0.5 stat/sec/repo at the 2s default). Its siblings — the primary-metadata snapshot poller and the non-darwin git-common polling — already park. Threads visibility through and matches the snapshot poller's park/re-arm pattern: the poll stops on the first hidden tick, and re-checks immediately on onWindowBecameVisible so a worktrees dir created while hidden still upgrades to the native stream and emits its create event. The visibility listener is dropped in the dispose path. darwin-only: the narrow watch is the `platform === 'darwin'` branch.
Problem
startGitCommonNarrowWatch(src/main/ipc/worktree-git-common-watch.ts) is the only watch entry point that never received theWorktreePollerWindowVisibilityobject. Every sibling takes one:startSnapshotDiffPollerparks onisWindowVisible()and re-arms viaonWindowBecameVisiblestartGitCommonPolling(the non-darwin branch) is handedvisibilityand gates the same wayAt the call site the two darwin watches start side by side — the primary-metadata poller got
visibility, the narrow watch did not.Inside,
armExistencePollran a baresetInterval(2s default) that stats<common>/.git/worktreesevery tick to detect the first linked worktree appearing. Nothing parked it when the window was hidden, so a repo with no linked worktree kept paying ~0.5 stat/sec forever with the app in the background — 0.5×N across N such repos.Scope: macOS only. The narrow watch is the
platform === 'darwin'branch ofstartGitCommonWatch; every other platform already goes throughstartGitCommonPolling, which is gated.Fix
Thread
WorktreePollerWindowVisibilityintostartGitCommonNarrowWatchand matchstartSnapshotDiffPoller's existing park/re-arm pattern rather than inventing a second mechanism:clearIntervals itself on the first tick that observes a hidden window (and never arms at all if the window is already hidden)onWindowBecameVisiblere-checks immediately instead of waiting a full interval, then resumes the interval if the dir still isn't thereworktrees/dir that appeared while hidden still upgrades to the native stream and emits{type:'create'}on show, so the repo's worktree list refreshesThe existing subscribe-attempt body is extracted to
tryUpgradeToNarrowWatchso the interval and the became-visible re-check share one path. No other restructuring.Tests
src/main/ipc/worktree-git-common-watch.test.ts. The visibility harness now holds a set of listeners rather than one slot — the darwin path parks two independent watches, and a single slot silently dropped the narrow watch's registration.node:fs/promises#statis wrapped with a delegating recorder so a test can assert on stat targets directly rather than on a proxy signal.New cases:
parks the existence poll while the window is hidden— createsworktrees/while hidden, asserts zero stats of that path, no subscribe, no eventsre-checks on show and still reports a worktrees dir created while hidden— asserts nothing happened while hidden, then thatshow()stats synchronously (promptly, not an interval later), subscribes, and emits thecreatedrops both visibility subscriptions on dispose— listener count 2 → 0resumes polling when the dir is still absent on showandkeeps polling and reporting while the window stays visible— visible-path behavior unchanged (these are controls; they pass with and without the fix)Revert check
Source change reverted, tests kept — 3 of the 5 new tests fail, and the 2 control tests pass as intended:
With the fix:
Tests 19 passed (19).pnpm run typecheckandpnpm run lintboth exit 0.