Skip to content

perf(worktrees): skip ineligible scans during scheduled cleanup - #1321

Merged
nixuuu merged 2 commits into
devfrom
canopy/uzytkownik-prosi-o-ed8d408c
Nov 26, 2026
Merged

nixuuu merged 2 commits into
devfrom
canopy/uzytkownik-prosi-o-ed8d408c

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

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.

Per pass, median of three runs Before After
Wall time 6.403 s 2.312 s
Scan RPCs / cumulative time 36 / 5.164 s 10 / 1.179 s
Removal RPCs / cumulative time 5 / 1.056 s 5 / 0.966 s
Top-level worker Git processes / cumulative Git time 980 / 3.013 s 351 / 0.950 s
Fetch 41 / 855 ms 7 / 153 ms
Status 41 / 439 ms 15 / 136 ms
Git commands during merge verification 258 / 591 ms 79 / 185 ms
SQL executions / cumulative client time 168 / 243 ms 107 / 159 ms

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

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
nixuuu enabled auto-merge (squash) November 26, 2026 18:57
@nixuuu
nixuuu merged commit 56c1c0c into dev Nov 26, 2026
25 checks passed
@nixuuu
nixuuu deleted the canopy/uzytkownik-prosi-o-ed8d408c branch November 26, 2026 19:09
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