Skip to content
Source
Sign inSign up
pingdotgg/t3code

Fix Git ref refresh resource storms #4727

Mergedjuliusmarmingemerged 8 commits intomain fromrefs/pull/4727/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

What changed

  • remove the five-second client-side ref polling loop and refresh once per connection generation
  • expire idle ref atoms after 30 seconds
  • key repository/ref snapshots by the common Git directory
  • coalesce duplicate refreshes, single-flight full scans, and bound Git subprocess concurrency
  • replace separate local/remote ref enumeration with one for-each-ref call
  • invalidate cached snapshots after ref mutations

Root cause

Each retained vcs.listRefs atom polled independently. On this repository that multiplied 31 concurrent requests into repeated full scans across 838 worktrees and roughly 16,700 refs. The server had no shared snapshot, single-flight guard, or concurrency bound, so identical requests launched many expensive git branch, git worktree list, and ref-enumeration processes at once.

Impact

Ref lists remain fresh on connection changes and explicit refreshes, while background idle work no longer creates a Git subprocess storm. Mutations still invalidate snapshots immediately.

Verification

  • 34 focused client/server tests
  • targeted formatting, lint, and affected-package typechecks
  • integrated web verification: at most two expensive Git children and one worktree scan
  • integrated mobile idle verification: zero expensive Git samples across 60 settled samples

Co-authored-by: codex <codex@users.noreply.github.com>

Note

Fix Git ref refresh resource storms with coalesced snapshots, exponential backoff, and invalidation hooks

  • Replaces periodic polling for VCS refs with a single refresh per connection generation, retried on failure using exponential backoff (1s base, 30s cap), and a 30s idle TTL instead of 5 minutes.
  • Adds a coalesced, cached listRefs snapshot in GitVcsDriverCore so concurrent paginated calls share one underlying for-each-ref scan, with generation tracking to prevent stale cache reuse.
  • Invalidates cached VCS refs (both in-memory and persisted) after any ref-affecting mutation (pull, push, createRef, switchRef, worktree ops, publish, etc.) on both success and failure via new onSettled hooks on RPC commands.
  • Adds removeVcsRefs and clearVcsRefs to EnvironmentCacheStore (web IndexedDB, mobile SQLite) and a per-environment persistence lock to serialize cache mutations.
  • Fixes branch list pagination in BranchToolbarBranchSelector to only trigger next-page loads on downward scroll past a threshold, not on layout or array length changes.
  • Risk: Idle list-refs atoms now expire after 30s instead of 5 minutes, causing more frequent re-fetches when the atom is re-subscribed after a period of inactivity.
Macroscope summarized 2c4c29c.

Note

Medium Risk Large changes to GitVcsDriverCore caching, invalidation, and client ref streams affect branch lists and worktree-heavy repos; behavior is heavily tested but touches core VCS paths.

Overview Stops Git ref refresh from spawning subprocess storms by replacing per-atom 5s polling with one refresh per connection generation (exponential retry, 30s idle atom TTL) and a server-side coalesced snapshot keyed by common Git dir (for-each-ref + worktree metadata instead of separate branch listings).

Invalidation: VCS mutations (pull, worktree, create/switch ref, publish, stacked actions) clear persisted vcs-refs via new clearVcsRefs / removeVcsRefs on web and mobile stores, bump a per-environment revision, and invalidate server snapshots after Git-changing RPCs; listRefs accepts optional refresh: true (e.g. PR local branch lookup).

UI: Branch picker pagination loads the next page only on downward scroll near the bottom, with a dedicated isFetchingNextPage flag so background refreshes are not shown as “loading more.”

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 2c4c29ca4c1909ff5610c175714d3830e647bf34. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: bf4cae2f-84f1-4725-a1b6-03f7acf28615

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `t3code/git-ref-refresh-resource-storm`

      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      This PR introduces substantial new caching infrastructure for Git ref operations, including multi-layer caches with TTL management, coalescing of concurrent requests, environment-wide invalidation on mutations, and exponential backoff for failures. The complexity of the new state management and fundamental changes to ref fetching behavior warrant human review.

      No code changes detected at 2c4c29c. Prior analysis still applies.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. apps/server/src/vcs/GitVcsDriverCore.ts line 2422Resolvedby juliusmarminge
    5. packages/client-runtime/src/state/vcs.ts line 124Resolvedby juliusmarminge
    6. packages/client-runtime/src/state/vcs.ts line 185OutdatedResolvedby juliusmarminge
    7. packages/client-runtime/src/state/vcs.ts line 243OutdatedResolvedby juliusmarminge
    8. packages/client-runtime/src/state/vcs.ts line 170Resolvedby juliusmarminge
      packages/client-runtime/src/state/vcsAction.ts line 497OutdatedResolvedby juliusmarminge
    9. packages/client-runtime/src/state/vcs.ts line 90Resolvedby juliusmarminge
    10. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium

      https://github.com/pingdotgg/t3code/blob/25c0c7abc81393ea9f52fb054823b928589327ba/packages/client-runtime/src/state/vcs.ts#L251-L256

      pull only invalidates cached VCS refs in onSuccess, so a git pull that fetches new remote-tracking refs but then fails during merge/rebase leaves stale cached refs. The RPC reports failure, so onSuccess never fires, yet the remote refs have already changed — branch pickers keep showing the pre-pull snapshot until reconnect or another mutation. Consider invalidating on both success and failure (e.g. via Effect.tapBoth), or invalidating before the RPC so the next ref stream always re-fetches.

      -      onSuccess: invalidateRefs,
      +      onSettled: invalidateRefs,
      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @packages/client-runtime/src/state/vcs.ts around lines 251-256:
      
      `pull` only invalidates cached VCS refs in `onSuccess`, so a `git pull` that fetches new remote-tracking refs but then fails during merge/rebase leaves stale cached refs. The RPC reports failure, so `onSuccess` never fires, yet the remote refs have already changed — branch pickers keep showing the pre-pull snapshot until reconnect or another mutation. Consider invalidating on both success and failure (e.g. via `Effect.tapBoth`), or invalidating before the RPC so the next ref stream always re-fetches.
    11. apps/server/src/vcs/GitVcsDriverCore.ts line 2390Resolvedby juliusmarminge
    12. Fixed in 7fa76c88e2. The shared RPC command helper now supports an onSettled hook, and pull uses it to clear persisted refs and publish a new ref generation on success or failure. The focused client regression test forces pull to fail and verifies both cache clearing and the revision bump.

    13. packages/client-runtime/src/state/vcs.ts line 297OutdatedResolvedby juliusmarminge
      apps/server/src/vcs/GitVcsDriverCore.ts line 2414Resolvedby juliusmarminge
      packages/client-runtime/src/state/vcsRefInvalidation.ts line 66OutdatedResolvedby juliusmarminge
    14. packages/client-runtime/src/state/vcs.ts line 291Resolvedby juliusmarminge
    15. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      Fix All in Cursor Bugbot Autofix is [ON](https://www.cursor.com/dashboard/bugbot), but a cloud agent failed to start. Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 2c4c29ca4c1909ff5610c175714d3830e647bf34. Configure [here](https://www.cursor.com/dashboard/bugbot).
      apps/web/src/state/paginatedBranches.ts line 35Resolvedby juliusmarminge
    16. juliusmarminge merged this38a6e3c

    17. juliusmarminge closed this

Sign in to join this conversation.