Repository navigation
perf(worktrees): avoid cleanup stalls and maintenance fetches - #167030
Conversation
Read placement facts once through the worker for the advisory owner census and yield classification in batches. Preserve exact session and placement checks before destructive cleanup. Keep automatic Git maintenance and pack-index repair local-only so partial clones cannot block cleanup on promisor fetches.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 8, 2026, 3:41 AM ET / 07:41 UTC. ClawSweeper reviewWhat this changesThe branch batches worker-placement reads and yields during owner classification, while keeping background Git maintenance and pack-index repair local-only. Example: Automatic cleanup runs in a partial clone with a missing historical branch head.
Review scores
ProductKind: Performance · Worth it: Yes Merge readiness✅ Ready for maintainer review This PR remains useful: current main retains both bottlenecks, and the reviewed patch preserves cleanup safeguards. No actionable defect was found. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherManaged worktrees give agent tasks separate Git checkouts. Background cleanup classifies their owners, protects active work, removes eligible checkouts, and maintains shared Git storage. flowchart TD
A[Registered checkouts] --> B[Worker reads session and placement facts]
B --> C[Batched owner classification]
C --> D[Cleanup eligibility]
D --> E[Exact live owner checks]
E --> F[Preserve or remove checkout]
F --> G[Local-only Git maintenance]
Technical reviewBest possible solution: Keep advisory classification worker-backed and pass-scoped, retain exact checks before removal, and reserve object hydration for explicit fetches. Do we have a high-confidence way to reproduce the issue? Yes: source identifies synchronous placement reads during the census, and the supplied partial-clone and 609-owner measurements establish focused failing paths; no runtime execution occurred in this review. Is this the best way to solve the issue? Yes: it reuses the existing worker reader and classification owner, preserves live removal checks, and applies the established local-only Git contract. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 17bdf57ee5df. Provenance checked
TestingProof path: in-process harness. Added test files: 3. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +41, Tests +123, Docs +2, Other -1. Total +165 across 8 files. View PR surface stats
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment |
…aw#167030) Read placement facts once through the worker for the advisory owner census and yield classification in batches. Preserve exact session and placement checks before destructive cleanup. Keep automatic Git maintenance and pack-index repair local-only so partial clones cannot block cleanup on promisor fetches.
What Problem This Solves
Managed-worktree cleanup could block the Gateway while classifying hundreds of session owners, and automatic Git maintenance could wait on network hydration in a partial clone.
User Impact
Cleanup classifies owners from worker-read placement facts and yields between batches. Background Git maintenance uses local objects only; explicit fetch remains responsible for hydration. No configuration or schema changes.
Why This Change Was Made
The session census already used a database worker, but its placement checks synchronously queried SQLite for every owner before the cleanup loop reached its first checkpoint. The census now indexes one worker placement read and reuses the existing classification logic. Its advisory facts are discarded after the pass; exact session and placement checks still guard destructive cleanup. This retained boundary protects against active turns, changed placement generations, and foreign writers.
Git's commit-graph auto-threshold check resolves historical refs and can trigger promisor fetches even when no graph write is needed. Apply the existing local-only environment contract from #166461 to automatic maintenance and pack-index repair.
The reported 609 protections were 257 moved branches, 223 nested repositories, 70 prior snapshot timeouts, 55 owners classified as active, three unavailable Git metadata cases, and one unreadable checkout. Their totals are expected disposition accounting, not evidence of 609 leaked live leases. Existing registry revisions, checkout fingerprints, timed retry, and explicit retry own invalidation. The nine fresh failures were six snapshot write-tree timeouts, two missing-object write-tree failures, and one diff-files timeout. This patch does not discard those protected files or claim to repair missing snapshot objects.
Evidence
tbx_01m4d3pa7v1xsfg65kmvf0z6dr, 609 synthetic session owners, five passes: census mean 181.05 → 5.99 ms; longest census callback 216.32 → 3.41 ms. FullManagedWorktreeService.gclongest callback 194.38 → 4.59 ms, below the 50 ms target. Warm full-pass mean (four passes after initialization) 226.59 → 29.86 ms (7.6×). This retained-worktree census and sweep executed no caller-thread SQL; destructive removals retain their exact checks. Callback spans were measured with async hooks; these are isolated measurements, not a post-deployment production claim. Instrumented final full-pass RSS was 687.0 → 538.6 MiB; retained SQL-observer data contributes to that difference, so it is not an allocation claim.tbx_01m4d25kk938jwmvp3d8pj5kj3, focused owner-protection, GC-performance, and Gateway maintenance suites passed. The initial missing-dependency failure was resolved with frozen installation. Candidate sync then timed out; AWS and Hetzner fallback admission both hit the shared fleet limit. An isolated Linux rig reached 0 MiB free and could not complete snapshot tests; it was not used as passing proof. A fresh Testbox completed the same tests.node scripts/run-vitest.mjs src/agents/worktrees/owner-protection.test.ts src/agents/worktrees/service.gc-performance.test.ts --maxWorkers=1(36.86 s wall including cold worker compilation; new census case 491 ms, new partial-clone case 77 ms). Both new regressions fail against the original production code for caller-thread SQL and unexpected promisor transport. Sibling run: 601 tests passed, with 33 platform-dependent cases skipped; one old lifecycle mock lacked the async placement capability. After the fixture correction, its two tests passed (8.27 s wall with--maxWorkers=1). Two independent Codex P0–P2 reviews were scoped-clean. The staged gate also requires removing the now-obsolete mock-baseline entry; that exact shrink is included. Finalnode scripts/check-changed.mjs --stagedpassed on the same Testbox (28m50s), including all 27 core test-type partitions, production types, lint, dead-code scans, and runtime guards. CI run 37744244078 passed on the published head: 720 seconds from first job start to last completion; selected Node jobs totaled 5,470 job-seconds, with the longest taking 633 seconds. These are whole-run costs, not incremental costs of the new regressions.Production LOC grows to preserve the exact destructive-mutation contract while introducing a short-lived read adapter for advisory census facts; both paths use the same classification function. The removed per-owner census SQL is not retained as a fallback. Production TypeScript delta: +41 lines across three existing modules (no new module or export). The requested diff excluding tests/test support/docs is +40 net lines, including the one-line baseline reduction.