orca icon indicating copy to clipboard operation
orca copied to clipboard

perf(git-status): throttle trailing refreshes and coalesce main-process reads

Open wolfiesch opened this issue 5 months ago • 1 comments

Summary

Consolidates the two halves of the active git-status backoff path into one change:

Renderer-side (trailing-refresh throttle):

  • add an optional cooldown to the coalesced poll runner so trailing refreshes can be delayed after a run completes
  • route active git status polling through that runner with a one-visible-poll settling interval
  • update polling tests so sustained file-watch triggers still produce a trailing refresh without immediate back-to-back git status runs

Main-process (read coalescing, folded from #6021, now closed):

  • coalesce concurrent main-process getStatus() reads for the same worktree/options key so overlapping renderer requests share one in-flight git invocation instead of spawning duplicates

The two layers compose: the renderer throttles how often trailing refreshes are issued, and the main process coalesces any that still arrive concurrently before they reach git.

Related:

  • #6289 added file-watch-triggered Source Control status refresh through the existing coalesced runner. This PR follows up on that path by adding a post-completion cooldown so sustained file-watch triggers cannot immediately chain git status runs.
  • #6324 reduces broader Git subprocess fanout, and its notes call active git status polling/backoff separate work. This PR targets that separate active-status backoff path.

Screenshots

No visual change.

Testing

  • [ ] pnpm lint
  • [ ] pnpm typecheck
  • [ ] pnpm test
  • [ ] pnpm build
  • [x] Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

Targeted checks run:

  • pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/right-sidebar/coalesced-poll-runner.test.ts src/renderer/src/components/right-sidebar/useGitStatusPolling.test.ts src/renderer/src/components/right-sidebar/useGitStatusPolling.rerender.test.ts
  • pnpm run typecheck:web
  • git diff --check

Full lint/typecheck/test/build were not run for this narrow renderer-side follow-up. A local pnpm test -- src/renderer/src/components/right-sidebar/coalesced-poll-runner.test.ts attempt expanded to the broader suite and hit unrelated existing failures outside these files, so the direct Vitest command above was used for scoped verification.

Empirical Regression Verification (Identity Churn)

We added a real-React + real-store regression test (useGitStatusPolling.rerender.test.ts) that simulates a rapid burst of file-watch events during a UI rerender (triggered by openFiles changing). Running the test against both codebases provides deterministic proof:

Metric Buggy Code (useMemo([runFetchStatus])) Fixed Code (useRef + useEffect)
Status calls at +200 ms (during cooldown) 2 (cooldown bypassed/reset) 1 (cooldown correctly enforced)
Status calls at +3000 ms (after cooldown) 2 2 (trailing call executed)
  • Under the old code: The runner was recreated on rerender, resetting the internal lastRunEndedAt timestamp. An event during cooldown immediately spawned git status (call count = 2 at 200 ms).
  • Under the fixed code: The runner stays stable in a ref scoped to the active worktree, properly scheduling the trailing run (call count = 1 at 200 ms, 2 at 3000 ms).

Worktree Switch Verification (No Delay)

We added a second test (useGitStatusPolling.rerender.test.ts) that asserts switching activeWorktreeId to a different repository immediately triggers refreshGitStatusForWorktree on the new worktree:

  • Mount: Calls refreshMock 1 time.
  • Switch: setState({ activeWorktreeId: WORKTREE_ID2 }) immediately triggers refreshMock (total 2 calls) without advancing the 3000 ms cooldown. This verifies the change handles repository switching instantly without introducing lag.

AI Review Report

Reviewed the change for polling correctness, performance risk, and cleanup behavior. The key risk was dropping a trailing status refresh or delaying the first poll; tests now cover immediate first run, one in-flight task, delayed trailing runs, and disposing a scheduled trailing run. The cooldown is measured from the previous run's completion, so sustained file-watch events get a settling interval before spawning git again.

Update (Reviewer Feedback):

  1. Fixed a bug where statusPollRunner was memoized on runFetchStatus. Since runFetchStatus changes identity on UI updates (like openFiles changes), the runner was recreated mid-cooldown, bypassing the throttle. The runner now lives in a ref scoped to the active worktree and delegates to the latest closure via a ref, ensuring it is never recreated during UI updates.
  2. Fixed a bug where switching activeWorktreeId introduced up to 3 seconds of lag before polling the new worktree. Adding activeWorktreeId as a dependency on fetchStatus's useCallback causes the visibility interval effect to clean up and re-install, immediately polling the new worktree while keeping the visibility check intact.

Cross-platform review: this change is timer and React-hook based, with no OS-specific shortcuts, labels, path separators, shell behavior, Electron menu accelerators, or platform branches touched. SSH/remote/local behavior uses the existing refreshGitStatusForWorktree path and connection readiness checks.

Security Audit

  • Input handling: no new external input parsing.
  • Command execution: no new git commands or IPC surface; existing status refreshes are rate-limited.
  • Paths: no new path construction or path normalization changes.
  • Auth/secrets: no new auth, token, credential, or environment handling.
  • Dependencies: no new dependencies.
  • Resource cleanup: dispose() now clears any delayed trailing run so unmount/worktree changes do not leave stale timers.

Notes

  • The visible poll interval now also acts as a post-completion cooldown for trailing active git status refreshes; it is a settling interval, not a strict fixed-rate cadence.
  • Provider-neutral git behavior; no GitHub-specific source-control logic.
  • Consolidates the renderer-side throttle with the main-process read coalescing from #6021 — the same active-git-status backoff story, verified together (71/71 across status.test.ts + the three renderer poll suites via direct vitest). Supersedes #6021.
  • X handle: @wolfie_

wolfiesch avatar Jun 25 '26 08:06 wolfiesch

Review Change Stack

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▶️ Resume reviews
  • [ ] 🔍 Trigger review

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: 9e48da8d-a308-4ea8-b759-f9d60a261e1e

📥 Commits

Reviewing files that changed from the base of the PR and between 30edee140c646eb04a891b76eca1d9daa1bc338e and 4f404261d904fdb06d18abb236c1925c60940828.

📒 Files selected for processing (2)
  • src/main/git/status-upstream-negative-cache.test.ts
  • src/main/git/status.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/git/status.ts

📝 Walkthrough

Walkthrough

getStatus now deduplicates concurrent reads by cache key and clears its in-flight tracking in test reset code. createCoalescedPollRunner now supports an optional minimum interval between completed runs and clears pending timeouts on disposal. useGitStatusPolling now routes active-worktree refreshes through that runner, and the related Vitest cases were updated for the new timing behavior.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: throttling trailing git-status refreshes and coalescing main-process reads.
Description check ✅ Passed All required sections are present, and the summary, testing, AI review, security audit, and notes are sufficiently filled out.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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[bot] avatar Jun 25 '26 08:06 coderabbitai[bot]

Taking a look, thanks for the PR!

brennanb2025 avatar Jun 25 '26 20:06 brennanb2025

Pushed a small follow-up fix in 4f404261d:

  • Included bypassEffectiveUpstreamNegativeCache in the in-flight getStatus() coalescing key, so strict upstream refreshes cannot reuse automatic status reads.
  • Added a regression assertion covering the automatic-vs-strict concurrent read case.

Targeted status/polling tests and web typecheck passed locally.

brennanb2025 avatar Jun 25 '26 20:06 brennanb2025