fix(host): isolate watcher initialization from terminal traffic

Merged
KI
1mo ago
+1694-144

What & why

Switching to a large workspace starts Files/Changes watchers on the host loop that also carries terminal traffic. Move nested-repository scans, ignored-directory enumeration, Git-directory lookup, and recursive Git-directory watching into workers. Share pending watcher setup, cancel obsolete queued scans, remove eager search indexing, and emit a catch-up refresh after initialization. Preserve current main's watcher backend selection and attach backoff; cancellation must not trigger failure backoff.

A real-PTY host transport benchmark with 100,000 files and three runs per version reproduced the stall:

Worst observedBeforeAfter
Attachment acknowledgement264.8 ms5.3 ms
Keystroke round trip284.9 ms16.9 ms
Unsolicited output delivery274 ms9 ms
Host event-loop stall168.8 ms9.2 ms

For a 12-workspace burst cancelled after 20 ms, scans starting after cancellation fell from 20 to zero in all three runs. Benchmark source, raw results, methodology, and reproduction commands are in packages/host-service/scripts/watcher-terminal-bench/.

These recorded runs compare the original implementation against baseline 19bf0e8c6f4b, before rebasing onto current main. They measure host-side PTY/TCP traffic, not the full production attach handshake or renderer paint. They do not claim to benchmark current main against the rebased PR.

Interactive results page: Terminal responsiveness benchmark.

How I tested it

  • bun run lint — passed.
  • bun run typecheck — passed, all 42 tasks.
  • bun run check:plugins — passed.
  • Focused watcher/filesystem tests on the rebased branch — 79 passed; two additional attach-guard cancellation tests passed (8/8 guard tests).
  • Original implementation: repeated before/after PTY benchmarks, worker bundle execution under Node, cancellation and catch-up refresh regressions.
  • bun run test — failed in desktop tests: 34 failures involving Git worktree/branch and shell environment behavior. No desktop implementation is changed here.
  • Lazy watcher registration integration suite — blocked at import by missing host environment configuration (ORGANIZATION_ID, HOST_DB_PATH, and others).
  • No full desktop UI verification was performed.

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs — same-repository branch

Summary by CodeRabbit

  • Performance & Reliability
    • File and Git change monitoring is more responsive in large workspaces, with benchmark results showing lower terminal input and output latency and faster watcher registration.
    • Cancelling or closing a workspace watch stops pending initialization and scans, reducing unnecessary background work.
    • Changes made during a directory scan appear in subsequent listings, and watcher failures no longer leave stale subscriptions.
  • Documentation
    • Added benchmark results and instructions for measuring watcher performance.