fix(workspace-fs): re-attach the chokidar watch when the gitignored set grows

Merged
SA
7d ago
+355-15

Fix 1 from plans/20261117-host-service-memory.md (#9868): stop host-service watching gitignored directories that appear after a watch attaches.

What changed

On Linux, defaultWatchBackend() is chokidar, which holds one fs.watch per file and per directory. Gitignored dirs were pruned only if they existed at attach time. FsWatcherManager.refreshIgnores swapped the subscription only when the ignored set shrank, so a node_modules / .next / dist created later was watched file by file until the last client unwatched.

  • NativeWatchBackend gets an optional watchesEachEntry flag. Only the chokidar backend sets it.
  • refreshIgnores now records a changed ignored set on every backend. A dir that is pruned but not yet in the native subscription (filteredRelPrefixes) has its events dropped before they reach clients, and isPathPruned counts it. So a gitignored dir created after attach looks the same to clients on parcel and chokidar. Parcel never re-attaches for such a dir, either when it becomes ignored or when it is un-ignored again (no FSEvents re-subscribe, see the "deaf after subscribe" note in watch.ts).
  • refreshIgnores also re-attaches when the set grew, but only on a backend with watchesEachEntry, because only there does the prune free memory. Growth re-attaches are limited to one per 30 s per watch root (growthReattachMinMs). If a growth comes in inside that window, one trailing timer retries at the end of the window, so the last growth is not lost (git-watcher filters events under a dir it already knows is ignored, so nothing else would trigger the refresh again). Shrink swaps stay immediate, as before.
  • Each change of the pruned set emits one root overflow event: a re-attach emits it after the swap, and a filter-only change emits it at once. Clients already handle this event at attach and after an FSEvents overflow: the file tree and open documents refetch, and git-watcher does a broad status refresh. This covers events written during the swap gap.
  • GitWatcher.refreshIgnoredDirs calls refreshWatcherIgnores when its own listing grew, not only when ignore rules changed. It is still rate-limited by the existing 5 s floor.

Why re-attach, not pruning in the chokidar ignored callback

The callback option needs a synchronous "does git ignore this?" answer. A basename check against DEFAULT_IGNORE_DIR_NAMES stops watching a dist that is tracked in git. The callback also only stops new entries: dropping watches already made for a half-written tree needs chokidar's private _closers, because unwatch(dir) closes only that one path. A dir pruned that way would also not be in prunedRelPrefixes, so a later un-ignore would not be detected. Re-attaching uses the existing, tested swap path and has none of these gaps. Its cost is one re-crawl of the non-ignored tree, at most once per 30 s.

Requirements

  • A gitignored dir created after attach becomes unwatched (Linux/chokidar): measured below, 65,656 watches → 24.
  • A dir that is not gitignored, created after attach, is still watched and its events reach clients: create:…/newdir/b.ts was delivered over /events in both runs.
  • Un-ignore still works: that is the existing shrink path, unchanged. Its test (watch-gitignored-prune.test.ts › refreshIgnores) passes. I did not repeat it on the second instance.
  • No unbounded re-crawl: growth re-attaches happen at most once per 30 s per root. The unit test checks this with an injected clock.
  • macOS/Windows: parcel does not set watchesEachEntry, so it never re-attaches on growth. Its client-visible behaviour now matches Linux (table below). That is a change on macOS/Windows: events under a gitignored dir created after attach no longer reach fs:watch clients, which is what already happens to the same dir when it exists before attach.
  • isPathPruned and the new filter compare POSIX-style relative paths, so nested prefixes such as packages/a/node_modules also match on Windows (before, path.relative gave \ separators there). Also shared: git-watcher now calls refreshWatcherIgnores when the listing grows, which runs one extra git ls-files and nested-repo scan per growth (at most once per 5 s) and then returns false. This also happens once at watch start, because the first listing grows from empty.
  • Backend choice is unchanged.

Parity: parcel (macOS/Windows) and chokidar (Linux)

watch-ignore-growth.test.ts runs each case on a fake backend in both modes (watchesEachEntry true and false). The fake drops events with the same ignore matcher the real backends use. Each case asserts that both modes give the same transcript, and that it is the expected one. The transcript lists the events the client got and the isPathPruned answer.

CaseEvents the client gets (both backends)isPathPruned (both)chokidarparcel
(a) ignored dir present before attachnone from inside ittruepruned at attachpruned at attach
(b) ignored dir created after attachits events until the refresh, then one overflow, then nonetrue after the refreshre-attach (at most 1 per 30 s); filter in betweenfilter only, no re-attach
(c) dir un-ignored later (ignored at attach, or ignored after attach)one overflow per set change, then its eventsfalsere-attachre-attach if it was pruned at attach (as before); filter only if it was pruned after attach
(d) non-ignored dir created after attachits eventsfalseno changeno change

Linux delay: when a growth comes in during the 30 s window, the events are dropped at once, but the memory comes back only at the trailing re-attach. That re-attach sends one more overflow (it covers the swap gap). On parcel there is no gap and no second event. That extra event is the only difference, and it comes up to 30 s later.

Confirmed first that the previous commit had this gap: on that code the parity test failed for parcel in (b) (create:node_modules/b.js delivered, pruned=false) and in (c') (no overflow), while chokidar passed.

Measured saving

Measured on the first commit, then repeated on the final commit: after 45 s, 22 watches; at +3 min, 195 MB and 24 watches.

Second host-service instance on port 4979, SUPERSET_HOST_RUN_MODE=local, its own HOME, run dir and copy of host.db, SUPERSET_API_URL pointed at a closed port. Bundles built with bun run build.ts from main (before) and from this branch (after), run on the released runtime's node. A tmpfs git repo (/dev/shm, .gitignore = node_modules/) was the only workspace. A WebSocket client on /events sent git:watch + fs:watch. The live host-service of this workspace was not touched. Watches = inotify wd: lines in /proc/<pid>/fdinfo. heapUsed is from the [host-service:vitals] line, logged once per minute, so it lags by up to 60 s.

Stepbefore RSSbefore watchesbefore heapUsedafter RSSafter watchesafter heapUsed
Idle, before attach176 MB0176 MB0
git:watch + fs:watch attached (3-entry repo)229 MB22229 MB22
node_modules/ created after attach (60,000 files, 6,061 dirs), +0 s485 MB48,774491 MB57,461
same, +45 s529 MB65,654282 MB513 MB2242 MB
new non-ignored newdir/b.ts created and written559 MB65,656282 MB524 MB2442 MB
— event delivered to the /events clientcreate:…/newdir/b.tscreate:…/newdir/b.ts
same, +3 min371 MB65,656189 MB192 MB2433 MB

At +3 min the after instance is back to its attached baseline (192 MB, 24 watches), and the before instance still holds 65,656 watches. RSS lags the watch count because V8 gives back freed pages slowly. Using the report's 6.5 KB per entry, the expected saving for one bun install of this repo after attach is about 1.5 GB. During the burst the watch count still climbs until the next git-watcher refresh (5 s floor) re-attaches. The +0 s row shows that transient.

Both runs logged the existing throttler buffer full — dropping events warning during the 60k-file burst. That behaviour is unchanged by this PR.

Checks

Ran:

  • bun test in packages/workspace-fs: 198 pass, 2 skip, 0 fail. This includes watch-ignore-growth.test.ts: 5 parity cases × 2 backend modes, plus the rate-limit case, all on a fake backend with an injected clock (about 100 ms).
  • bun test in packages/host-service: 2896 pass, 23 fail. The 23 are health.check › serialises the current boot's stamps… and 22 session: … tests in test/integration/background-naming.integration.test.ts. The same tests fail on main without this change in a full-suite run. Run alone, the session tests pass on both, and health.check fails on both. None of them touch the watcher.
  • bun run lint:fix: one file reformatted, no remaining issues.
  • Typecheck: bun run typecheck (turbo, concurrency 3) was OOM-killed (exit 137 in host-service#build:types) on this 8 GB box with the dev stack running. I re-ran the same turbo task as turbo typecheck --concurrency=1: 42/42 successful on the first commit. On the final commit, I ran it for workspace-fs and host-service only: 6/6 successful.
  • Second-instance measurement above.

Not run:

  • Un-ignore on the second instance (covered by the existing unit test only).
  • macOS/Windows (the real parcel backend). Parcel parity is shown only with the fake backend. A real bun install was not run either (synthetic tree, as in the report).
  • Docker / image tests: not needed for this change.

Summary by CodeRabbit

  • Bug Fixes
    • File watching now updates when newly ignored directories appear, helping prevent changes beneath those directories from triggering unnecessary notifications.
    • Watcher refreshes for growing ignored-directory lists are rate-limited to avoid repeated reattachments. Existing ignored-directory changes that require a refresh continue to be applied promptly.
    • Filtering of ignored paths now works consistently across platforms, including Windows.