Repository navigation
improve: reduce Gateway pauses during worktree cleanup - #154545
Conversation
|
🦞👀 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 September 21, 2026, 11:16 AM ET / 15:16 UTC (Revision 9). ClawSweeper reviewWhat this changesMoves worktree cleanup inventory reads into the existing database worker and fetches only live checkout IDs when recalculating cleanup pressure. Merge readiness✅ Ready for maintainer review The optimization remains useful on current main, and no actionable correctness or security findings remain. Recorded lifecycle proof covers cleanup protections and recovery, and GitHub confirms successful CI for the reviewed head. Priority: P2 Review scores
Verification
How this fits togetherManaged worktree cleanup applies count and size limits to agent-owned Git checkouts. Registry reads identify candidates, while the removal service checks activity, ownership, leases, locks, and recovery snapshots before deleting a checkout. flowchart TD
A[Cleanup limits] --> B[Cleanup planner]
C[Worktree registry] --> D[Database worker]
D -->|Fresh live IDs| B
B --> E[Removal safety checks]
E -->|Protected| F[Keep checkout]
E -->|Eligible| G[Snapshot and remove]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +16, Tests +15. Total +31 across 6 files. View PR surface stats
Review metricsNone. Technical reviewBest possible solution: Keep fresh cleanup inventory reads in the existing worker while retaining removal safety decisions in the worktree service. Do we have a high-confidence way to reproduce the issue? Yes: current main visibly decodes and sorts retained registry records for each candidate, and the PR documents a matched populated-history replay through the real cleanup entry point; this review did not execute it. Is this the best way to solve the issue? Yes: a narrow live-ID projection through the existing worker removes repeated decoding without duplicating storage ownership or changing cleanup policy. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 28acdfc4504d. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (8 earlier review cycles)
|
962ffde to
122e8cd
Compare
122e8cd to
f6cc9f2
Compare
e2a3961 to
8acbd44
Compare
Read fresh live IDs through the existing shared-state worker for every cleanup candidate instead of materializing retained records. Preserve live removal authority, active leases, owner protection, snapshots, and activity checks.
Share the worktree read dispatch guard and existing kernel namespace while preserving each operation and its database. This keeps the shared-state runtime within its existing line cap without changing the ratchet or adding a wrapper.
8acbd44 to
4aa9e98
Compare
|
Merged via squash.
|
* perf(worktrees): narrow cleanup registry reads Read fresh live IDs through the existing shared-state worker for every cleanup candidate instead of materializing retained records. Preserve live removal authority, active leases, owner protection, snapshots, and activity checks. * refactor(state): group worktree registry reads Share the worktree read dispatch guard and existing kernel namespace while preserving each operation and its database. This keeps the shared-state runtime within its existing line cap without changing the ratchet or adding a wrapper.
What Problem This Solves
Cleanup above the live-worktree limit repeatedly decoded and sorted retained registry history on the Gateway thread for every eviction candidate.
User Impact
Cleanup applies the same limits and protections with less work on the Gateway thread. Configuration, retention, snapshots, persisted state, and update behavior are unchanged.
Why This Change Was Made
The existing shared-state worker now selects only live IDs through the existing
removed_atindex. Cleanup rereads those IDs for every candidate, so a competing removal reduces pressure before the next eviction. The initial candidate inventory also uses the existing worker reader. Removal still owns fresh activity, owner, lease, Git-lock, and snapshot checks.Evidence
Performance measurements below are from the reviewed initial head
962ffde2b97a. Main subsequently extracted the same limit loop intogc-limits.tsand added cleanup outcome reporting. The query cutover now uses that owner; the measurements were not rerun after this integration. Fresh review, focused tests, and the real Git lifecycle proof validate the integrated behavior.A matched ABBA replay of the real cleanup entry point used 553 live synthetic rows (29 manual) plus 974 retained rows. Each row includes a valid synthetic cleanup outcome with a 360-character failure reason; this is a populated-history workload, not a measurement of production row payloads. Each process ran one cold pass and three warm passes. On macOS / Node 26.8.2 without allocation profiling, medians across the six warm passes were:
The local host was shared and loaded. The matching Linux / Node 26.8.2 ABBA run, with allocation sampling enabled, reduced median whole-process CPU from 4,040.07 to 224.56 ms, main-thread CPU from 3,145.20 to 82.64 ms, wall time from 3,179.66 to 187.14 ms, and sampled main-isolate allocations from 2.522 GB to 68.807 MB. The two cold candidate passes took 2.890 and 1.447 seconds. Linux proof completed with payload exit 0 and a stopped lease; the enclosing Testbox workflow may show cancellation after its owned cleanup. RSS was mixed across platforms/runs, so this does not establish a resident-memory or production-wide CPU saving.
Real Git lifecycle proof passed on macOS and Linux: an active run lease and a manual checkout remain protected, a deterministic competing remover stops cleanup at the correct count, and a recovery snapshot restores edited contents. Only task-owned synthetic repositories and state were used.
pnpm test src/agents/worktrees/registry-read.test.ts --maxWorkers=1: 2 tests passed, 106.66 s command wall / 97.22 s Vitest duration on the loaded host (85% transform, about 10% tests). The existing fixture is reused; no production test seam was added. In exact-head CI run35599970314, both cases passed and reported 0.871 s and 1.185 s (2.056 s summed case time; the verbose reporter does not provide a separate file wall time).git diff --checkpassed.The previous exact-head CI run failed in an unchanged library-read test fixture: temporary state was deleted before asynchronous worker lease drainage. The refreshed branch includes canonical main fix
103115f2f0b1, which drains agent workers before shared state and asserts settled custody. Its separate Linux / Node 24.19.0 proof passed three original-group runs (626 tests each), 20 standalone runs (9 tests each), and 72 sibling tests. The cleanup patch itself is unchanged by this refresh.CI also caught the shared-state runtime exceeding its existing 700-line cap after nearby main changes. Grouping its two worktree read dispatches and using the existing kernel namespace brought it from 704 to 700 counted lines; the ratchet, behavior, and budget are unchanged. The exact failing ratchet now passes, as do targeted lint, independent review, and both registry-reader cases (one setup was initially refused by the disk reserve, then passed after space was restored).
Run
35590723180also failed an unchanged Bun base64 memory assertion (71,474,209 bytes against a 67,108,864-byte budget). Its canonical repair is tracked separately in #154808; this PR does not relax the threshold or skip the test.Run
35595268335failed the unchanged UI rail fixture because its focus update had not settled before the assertion. This branch includes canonical main fix0883394977d1: it moves the existing flush before the assertion, with production bytes unchanged. The owner verified the source against the failing CI merge and passed the 21-case rail suite plus 2,342 UI/component tests.ClawSweeper review completed without findings for pre-refresh head
8acbd44d5d22. Its CI run35599970314failed the unchanged cron testholds adopted on-exit work until the previous scheduler drains (start)and a separate job that never acquired a runner or executed any steps. The branch now includes canonical main fixfd2c001cad2d, which synchronizes the cron fixture with owned callback entry and completion instead of elapsed time. The original 13-file group reproduced two failures on fresh main; the repaired group passed all 283 tests, followed by clean independent review and full selected checks. No runtime behavior, timeout, or assertion budget changed. The refreshed cleanup patch at4aa9e9859bbfis aggregate patch-identical; its line-cap ratchet and diff sanity check pass. Fresh exact-head CI run35610067697passed all 159 jobs for4aa9e9859bbf, and ClawSweeper completed its review of that head without findings. Native landing follows these completed gates.