Commit e7b474c
authored
perf(worktree): stop worktree session creation blocking on large-repo scans and deletes (#3141)
## Problem
Starting a new worktree session in a large repo (posthog/posthog) gets slower and slower over a week of use. Three compounding causes, all measured on a real posthog checkout:
1. **~20s of ls-files walks per create.** `processWorktreeLink`/`processWorktreeInclude` each run `git ls-files --ignored --others --directory --exclude-from=<file>`. Because `--exclude-from` replaces the standard excludes, git cannot collapse `node_modules/` and recurses the entire ignored tree looking for pattern matches (9.6s + 9.8s, warm cache, growing with ignored-file count). The walk also matched strays like `node_modules/.pnpm/psl@1.9.0/node_modules/psl/.env`, and copying that pre-created `node_modules/.pnpm/` in the fresh worktree — silently defeating posthog's own `.husky/post-checkout` bootstrap check (`[ ! -d node_modules/.pnpm ]`).
2. **Creation blocks on suspending the least-recent task once at the worktree cap.** `doCreateWorkspace` awaited `suspendLeastRecentIfOverLimit()` before creating: a full checkpoint capture plus deletion of a ~6GB, node_modules-laden worktree, inline, before `git worktree add` even starts. Under the cap (early in the week) this branch is skipped entirely, which is why creation "became" slow after days of use.
3. **Worktree deletes rm multi-gigabyte trees inside the repo write lock.** Every suspend, archive, and hourly inactivity sweep serialized with worktree creation on the per-repo write lock while unlinking ~1M files.
## Changes
- `getIgnoredPathsFromExcludeFile` now lists candidates with `--exclude-standard --exclude-from=<file>` (so git collapses standard-ignored trees instead of recursing them) and re-applies the exclude file's own patterns in-process via a new gitignore-subset matcher (`exclude-patterns.ts`: comments, negation, dir-only, anchoring, `*`/`?`/`**`/`[...]`). Matches buried inside standard-ignored trees no longer surface, fixing the node_modules bootstrap bug. Repos without `.worktreelink`/`.worktreeinclude` now skip the git calls entirely.
- The worktree cap is enforced *after* workspace creation as a fire-and-forget, instead of blocking creation. `suspendLeastRecentIfOverLimit` now suspends exactly the excess over the cap, oldest first (previously it fired when merely *at* the cap, pre-create). Net behavior change: the active count can briefly reach `maxActiveWorktrees + 1` while the background suspend runs.
- `deleteWorktree` renames the worktree into `<worktreeBase>/.trash/` (instant, same volume), runs `git worktree prune` under the write lock, and does the recursive removal in the background. Falls back to the old `git worktree remove --force` path when the rename cannot work (cross-volume, already gone). Boot sweeps `.trash/` for leftovers from interrupted deletes, alongside the existing orphan cleanup.
Measured on posthog/posthog: the `.worktreeinclude` scan went from ~10s to ~140ms with identical output minus the node_modules stray; worktree deletion no longer holds the write lock for the duration of a multi-gigabyte rm.
## How did you test this?
- 36 new parameterized matcher tests (`exclude-patterns.test.ts`).
- New real-git integration test: `.envrc` linked, `.env` copied, and `node_modules/` never created in a fresh worktree (regression for the bootstrap bug).
- New suspension tests: at-cap does nothing, multi-excess suspends oldest first.
- New `sweepTrash` test; existing worktree lifecycle (add/remove/prune) tests pass unchanged against the trash-based delete.
- Full suites green: `@posthog/git` 306 tests, `@posthog/workspace-server` 612 tests. Typecheck and Biome clean.
- Verified the new scan end-to-end against the real posthog repo via the built package: `.worktreelink -> []` (comments only), `.worktreeinclude -> [".env"]` in ~140ms.
## Automatic notifications
- [ ] Publish to changelog?
- [ ] Alert Sales and Marketing teams?
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent fcd52e3 commit e7b474c
9 files changed
Lines changed: 639 additions & 32 deletions
File tree
- packages
- git/src
- workspace-server/src/services
- folders
- suspension
- workspace
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
0 commit comments