Repository navigation
perf(worktrees): skip ineligible scans during scheduled cleanup - #1321
Merged
Merged
Conversation
Filter scheduled candidates by thread state and scoped cleanup policy. Reuse successful repository refreshes on each worker link while keeping fresh per-checkout validation before removal. Fixes: #82
nixuuu
enabled auto-merge (squash)
November 26, 2026 18:57
This was referenced Nov 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
The hourly cleanup, startup scan and worker reconnect used to fetch and inspect each managed checkout on an available worker. A read-only production snapshot on November 26 had 287 live entries. After external entries and offline workers, 276 could receive a scan. Of those, 1 was busy, 40 were protected by recent archival and 208 could not match the enabled policy. The remaining 27 shared one worker, repository path and remote. Worker availability in this estimate uses the database lease.
The gateway now checks the owner-scoped inventory and effective policy before sending a scheduled scan. A scope snapshot only filters candidates. Each admitted candidate uses current settings and its own checkout status. After one successful scan, later candidates for the same repository path and remote reuse fetched refs on that worker connection for the rest of the pass. The worker also skips a duplicate merge check when both ref names are identical.
Reserved commands retain their replay path. The worker fetches again and checks the expected HEAD, local changes and required merge evidence before removing a checkout. Manual preview and execution still scan each managed checkout in scope. A newly merged branch can be picked up at the following refresh.
Isolated measurement
Three alternating pairs of runs through the global scheduler tick, using a fresh PostgreSQL database, a real gateway, a WorkerHost and local bare remotes per run. The baseline has the same source tree as the merged shutdown fix in #1320. Each of three projects had 12 worktrees on one worker with 512 tracked files per repository. The policies were merged, all rules disabled, and archived or 30 days inactive. The fixture included busy threads, recent and older archives, dirty files and unpushed commits.
All runs removed the same five checkouts on disk and in PostgreSQL. The seven remaining fetches comprise two candidate-repository refreshes and five pre-removal rechecks. RPC durations include worker execution, so the Git timings overlap them. The local remotes exercise Git merge detection without network latency or a GitHub API account. The measurement harness runs outside required CI.
Verification
68 integration tests passed locally across worktree-lifecycle, worktree-merged, worktree-closed-cleanup and worktree-github, with a clean check:tmp result. New regressions exercise the skipped scan RPC plus full manual preview, and the loss of the remote between the first removal and the next candidate. In the latter case the worker rejects the reserved removal and the gateway releases the reservation. The suite also covers reconnect replay, cancellation during a read, settlement after shutdown begins, and owner/project-scoped merge proof.
Lint and formatting passed on the six edited TypeScript files. The repository-wide typecheck, builds and remaining tests run in CI.
The first CI run (733fe3e) exposed a Git-version difference in the new regression fixture. Pushing by URL had already advanced the worker’s tracking ref on the runner. The fixture now advances the bare repository from the other side, leaving that tracking ref stale until the scheduled fetch. All seven worktree-merged tests passed locally with the adjusted fixture. The first run also rejected the skill preview in config-sync-environment.test.ts with a stale-report error on shard 8. That focused linked-computer test passed locally on the original revision.
Fixes: #82
Related: #1320