Skip to content

perf(worktrees): back off worktree scans for agent-scratch repos - #9985

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/git-scan-churn-guards
Jul 22, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/git-scan-churn-guards

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

What & why

Crash-channel diagnostics from the renderer-OOM cluster surfaced a main-process problem along the way: one production install executed 10,216 git commands in 80 minutes (~128/min, 757s of git busy time — ~16% of wall-clock), 92% of them git worktree list --porcelain. The resolved-worktree scan (computeResolvedWorktrees) fans out over every registered repo on a 30-second per-repo cache TTL — and on the affected installs, most registered repos were agent-CLI scratch repos (~/.codex-tmp/foragent-capsule-*, ~/.codex/vendor_imports, ~/.claude/skills/*) that were registered once (nested-repo import) and never need 30-second freshness. #9535 hides agent-scratch rows from the sidebar, but the scan layer still shelled out to all of them every cycle, forever.

Fix: classify agent-scratch repo roots with the same curated shared-matcher pattern #9535 introduced (isAgentScratchRepoRootPath), and stamp their scan-cache entries with a 5-minute TTL instead of 30 seconds (resolveWorktreeScanCacheTtlMs, wired at the single cache-stamp site).

Nothing is skipped or hidden: scratch repos still scan — just 10× less often.

CLEAR fix evidence

  • Wiring test (fails without the fix): with fake timers and a two-repo store (normal + /tmp/.codex-tmp/capsule-a), advancing 31s re-scans the normal repo but not the scratch repo; advancing +5min re-scans the scratch repo. Reverting the one-line cache-stamp wiring makes this test fail (expected 2 to be 1) — verified both directions.
  • Quantified impact (perf review): on the measured install (~59 repos, ~54 scratch): ~118 scans/min → ~21/min, an ~82% reduction in steady-state git subprocess spawns.
  • Matcher: 17/17 tests (POSIX/Windows/WSL paths, negatives). Runtime file: 827/827 full-file, verified across multiple runs. Node typecheck + oxlint/oxfmt clean.

ELI5

Orca keeps a list of every project folder it has ever been shown, and every 30 seconds it walks the whole list asking git "anything new here?". On some machines, most of that list is temporary scratch folders that coding agents made for themselves — nobody is working in them, but Orca still knocked on every door twice a minute, thousands of times an hour. Now Orca recognizes those agent scratch folders and only checks them every 5 minutes. Real project folders keep the fast checks, and anything you do inside Orca still updates instantly.

Trade-offs

  • One deliberate behavior change, tightly bounded: a change made outside Orca (e.g. a CLI adding a worktree by hand) inside an agent-scratch repo can take up to 5 minutes to appear instead of 30 seconds. Every Orca-driven action (create/remove/repo change) bypasses the TTL via the per-repo generation bump and refreshes instantly — verified in review against all mutation call sites.
  • Fail-safe misclassification: a legitimately-registered repo under a scratch container (e.g. developing a skill in ~/.claude/skills/x) only gets slower passive pickup — never missing rows, never wrong data.
  • Known limits (documented, deliberate): SSH-hosted scratch repos keep the 30s cadence (classification reads local path conventions only), and a case-variant scratch path on macOS keeps the base TTL (safe-fail: missed optimization only).

Adversarial review & perf

  • Adversarial loop: 3 rounds → CLEAN (fresh sub-agent each round).
    • Round 1: CLEAN + one actionable minor — the cache-stamp wiring wasn't test-protected. Wiring test added.
    • Round 2: caught a real blocker in that new test — it asserted absolute counts against a shared mock whose history accumulates across the 35k-line file, so it passed in isolation but failed deterministically in a full-file (CI-shaped) run. Fixed with a baseline mockClear() matching the file's convention.
    • Round 3: verified closure — two full-file runs 827/827, order-independence confirmed, source diff confirmed unchanged since round 1.
  • Perf review (perf skill, sub-agent): CLEAN — smallest correct fix for the measured waste; 5-min constant validated (bounded blast radius beats a longer TTL); matcher cost trivial (runs only at the stamp site, never on the cache-hit path).

Follow-ups (ranked by the perf review, not this PR)

  1. Focus/visibility gating of the runtime resolution poll (biggest remaining lever; cross-cutting with mobile/SSH startup paths).
  2. Apply the same scratch segmentation to the IPC-layer 5s scan cache (UI-request-driven, not the measured steady-state source).
  3. Extend classification to SSH once remote scratch conventions are validated.
  4. Registration hygiene for repos whose path no longer exists (product decision: auto-deregistration needs buy-in).

Production crash diagnostics measured ~128 `git worktree list` execs/min
(9,400 in one 80-minute session, ~16% of wall-clock in git subprocesses):
the resolved-worktree scan fans out over every registered repo on a 30s
cache TTL, and most registered repos on the affected installs were
agent-CLI scratch repos (~/.codex-tmp capsules, vendor imports, skill
checkouts) that need no freshness.

Classify agent-scratch repo roots with a curated shared matcher and stamp
their scan-cache entries with a 5-minute TTL instead of 30s. Orca-driven
mutations still bypass the TTL via the per-repo generation bump, so only
passive pickup of external changes slows for scratch repos. Expected
steady-state reduction on the measured install: ~82% fewer git spawns.
@coderabbitai

coderabbitai Bot commented Jul 22, 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: b18c2f3e-18ad-4974-a9e6-54d11b1268fb

📥 Commits

Reviewing files that changed from the base of the PR and between 0121f57 and 69a0d81.

📒 Files selected for processing (4)
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/orca-runtime.ts
  • src/shared/agent-scratch-worktrees.test.ts
  • src/shared/agent-scratch-worktrees.ts

📝 Walkthrough

Walkthrough

Adds isAgentScratchRepoRootPath to recognize supported agent scratch repository root patterns across normalized path formats. Adds resolveWorktreeScanCacheTtlMs and uses it when setting worktree scan cache expiration, selecting a five-minute TTL for local scratch repositories and the existing 30-second TTL otherwise. Tests cover path matching, repository types, and cache expiration behavior.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is substantive but does not follow the required template and omits the Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes sections. Reformat the description to match the template and add the missing sections, including testing details, cross-platform review, security audit, screenshots or a no-visual-change note, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: reducing worktree scans for agent-scratch repos.
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.

@brennanb2025
brennanb2025 merged commit 4062ed5 into main Jul 22, 2026
2 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…blyai#9985)

Production crash diagnostics measured ~128 `git worktree list` execs/min
(9,400 in one 80-minute session, ~16% of wall-clock in git subprocesses):
the resolved-worktree scan fans out over every registered repo on a 30s
cache TTL, and most registered repos on the affected installs were
agent-CLI scratch repos (~/.codex-tmp capsules, vendor imports, skill
checkouts) that need no freshness.

Classify agent-scratch repo roots with a curated shared matcher and stamp
their scan-cache entries with a 5-minute TTL instead of 30s. Orca-driven
mutations still bypass the TTL via the per-repo generation bump, so only
passive pickup of external changes slows for scratch repos. Expected
steady-state reduction on the measured install: ~82% fewer git spawns.
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