Skip to content

Keep FSEvents overflow recovery subscription-scoped - #2666

Merged
ymichael merged 4 commits into
mainfrom
bb/pr-targeted-watcher-overflow-recovery-thr_jsgwf2fqgt
Aug 28, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/pr-targeted-watcher-overflow-recovery-thr_jsgwf2fqgt

Conversation

@ymichael

@ymichael ymichael commented Aug 28, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

RootSubscription already distinguishes the native FSEvents File system must be re-scanned condition from fatal watcher failures and performs targeted refresh/reconnect/refresh recovery. The subprocess boundary introduced by PR #250 erased that distinction: every parcel callback error became the same watch-error, and the parent unconditionally killed the shared watcher child, replayed every subscription, called listEntries for every root, and emitted synthetic updates to unaffected watches. Whole-child recycling is required for fatal Linux backend failures such as inotify EINTR, but it turns a recoverable subscription-specific FSEvents overflow into O(all active watches) work.

Historical supporting evidence, separate from the controlled benchmark below:

  • Apr 20 commits 64c74f0a0 and 1283e636e introduced targeted FSEvents recovery.
  • Jun 8 commit 2160f2838 moved that lifecycle into RootSubscription.
  • Jun 12 commit 9786b64d9 made fatal Linux inotify callback errors terminal in-process to prevent retry leaks.
  • Jun 22 PR Run @parcel/watcher in a self-healing child process #250 / commit cba0b9e89 intentionally added whole-child recycling for fatal EINTR, child crashes, and hangs. Its interception was broad because the child protocol had no recovery classification, not because FSEvents overflow was intended to become global.
  • The investigation observed 57 ready environment watches in the affected deployment. That production observation selected the N=57 fixture; it is not included in the controlled timing data.

What changed

  • Classify parcel callback errors in the watcher child as either rescan-subscription or recycle-child.
  • Route rescan-subscription only to the matching proxy callback with the original message, allowing the existing RootSubscription targeted recovery to run without replacing the child.
  • Keep all non-rescan callback errors on the existing whole-child recycle path. Child exits and liveness timeouts are unchanged.
  • Add a two-subscription regression, a deterministic proxy fan-out count harness, and an opt-in end-to-end N=57/N=100 recovery benchmark.

This changes only the bundled parent/child watcher subprocess protocol. It does not change the server/host-daemon wire protocol, so HOST_DAEMON_PROTOCOL_VERSION is unchanged.

How you verified

Controlled end-to-end benchmark

Both revisions ran sequentially on the same machine with byte-identical benchmark code and generated fixture contents: macOS 26.5.1 / Darwin 25.5.0 arm64, Node 22.23.1, pnpm 9.15.0. “Before” is origin/main at a2fcbaf55 with only the two benchmark files applied; “after” is d3078ca4c. Each scenario used 2 warm-ups and 20 measured iterations. Every iteration created a fresh real child process, IPC channel, native watcher set, HostWatcher, and RootSubscription set.

Each root contains 8 directories with 16 × 4 KiB files each plus one 4 KiB trigger file: 129 files, 528,384 bytes, 9 top-level entries, and 137 recursive tree entries per root.

Roots Nested directories Files Bytes Recursive entries
57 456 7,353 30,117,888 (28.72 MiB) 7,809
100 800 12,900 52,838,400 (50.39 MiB) 13,700

The measured path is:

real file write → real @parcel/watcher/FSEvents subscription in a forked child → production child handler and IPC channel → production proxy → HostWatcher.watchPathRoot → RootSubscription → production 250 ms reconnect retry and 75/500 ms callback scheduler → calibrated recursive readdir/read/SHA-256 downstream refresh

Root 0 alone receives the injected overflow. Measurement begins before the real trigger-file write and stops only after exact settled-work predicates pass. Before requires one child restart, all N proxy/native replay subscriptions, all N real listEntries calls, all N synthetic event callbacks, and all N recursive downstream scans. After requires the affected native unsubscribe/reconnect, both targeted rescan notifications, and both affected-root downstream scans. Subscribe/unsubscribe starts must equal completions, and a 100 ms quiescence assertion rejects late work after settlement.

The controlled-load mode runs repeated SHA-256 work for 8 ms and yields for 8 ms on the benchmark process's event-loop thread. It is reproducible and does not load every CPU core or the live bb server. “Idle” means no intentional benchmark load, not a dedicated idle host.

Settled recovery latency

Milliseconds, p50 / p95 / max. The N=57 distributions mostly overlap because targeted recovery deliberately retains its retry and two debounce phases; the after idle p50 is higher. The scaling benefit becomes clear at N=100, especially under controlled load.

Roots Mode Before After
57 idle 291.6 / 449.7 / 529.4 340.1 / 379.9 / 445.0
57 controlled CPU 380.2 / 383.7 / 400.2 367.3 / 390.0 / 429.4
100 idle 361.5 / 1,179.5 / 2,419.7 355.6 / 460.1 / 510.3
100 controlled CPU 2,606.9 / 5,095.9 / 6,599.1 367.8 / 375.4 / 441.1

Maximum event-loop delay per iteration

Milliseconds, distribution of each iteration's maximum delay as p50 / p95 / max.

Roots Mode Before After
57 idle 1.88 / 6.34 / 6.35 2.05 / 6.92 / 8.31
57 controlled CPU 9.32 / 9.52 / 9.54 9.36 / 10.22 / 23.41
100 idle 1.89 / 27.20 / 50.30 2.20 / 7.51 / 8.08
100 controlled CPU 67.08 / 145.88 / 211.81 9.34 / 10.57 / 14.03

Initial readiness

Milliseconds from constructing the watcher graph until all HostWatcher onReady callbacks and all native subscriptions were ready, p50 / p95 / max. This happens before overflow injection and is recorded separately from recovery latency.

Roots Mode Before After
57 idle 129.0 / 277.4 / 461.9 220.4 / 350.0 / 402.0
57 controlled CPU 128.5 / 131.2 / 132.9 129.9 / 157.7 / 171.3
100 idle 135.8 / 426.6 / 771.4 152.9 / 906.5 / 981.8
100 controlled CPU 339.0 / 843.9 / 901.1 139.1 / 1,104.0 / 1,239.0

Deterministic settled work

Counts were identical across all 20 idle iterations and all 20 controlled-load iterations for each revision. “2 scans after” is the affected RootSubscription's dropped-event refresh plus its post-reconnect refresh; zero listEntries means zero global proxy replay scans, not zero targeted recovery.

Metric after one overflow 57 before 57 after 100 before 100 after
watcher-child restarts 1 0 1 0
proxy subscribe messages 57 1 100 1
proxy replay subscriptions 57 0 100 0
proxy unsubscribe messages 0 1 0 1
native subscribe starts / ready 57 / 57 1 / 1 100 / 100 1 / 1
native unsubscribe starts / ready 0 / 0 1 / 1 0 / 0 1 / 1
real listEntries calls / entries 57 / 513 0 / 0 100 / 900 0 / 0
child event batches / events 57 / 513 0 / 0 100 / 900 0 / 0
host event batches / events 57 / 513 0 / 0 100 / 900 0 / 0
targeted rescan notifications 0 2 0 2
affected / unaffected downstream scans 1 / 56 2 / 0 1 / 99 2 / 0
downstream files read 7,353 258 12,900 258
downstream bytes read 30,117,888 1,056,768 52,838,400 1,056,768
downstream entries processed 7,809 274 13,700 274
watcher errors 0 0 0 0

Remaining controlled substitutions and limitations

  • A native FSEvents overflow is not induced. At the narrowest deterministic seam, the benchmark child converts the real trigger-file event into the exact rescan-required callback error immediately before the production child handler. The native subscription, trigger event, process fork, IPC, handler, proxy, HostWatcher, RootSubscription, retry, reconnect, and filesystem work remain real.
  • The benchmark child entry is TypeScript under tsx, rather than the packaged production entry, so it can wrap the real parcel backend and emit telemetry. The child handler and IPC protocol under test are production code.
  • Running the full daemon/workspace callback graph safely would mix status/environment/server effects into a watcher-only PR. The downstream callback is explicitly calibrated: production debounce scheduling followed by recursive real filesystem reads and SHA-256 of every file. It does not include WatchManager status/environment work, database/server work, production logging, or production filesystem shape/cache state.
  • The load generator contends with the host-side watcher/recovery event loop only. It models event-loop pressure reproducibly; it is not a whole-machine production-load replay.

Full per-iteration JSON, including event-loop utilization, per-iteration delay p50/p95/max, load cycles, readiness, latency, and all counters, is produced by BB_WATCHER_BENCHMARK_OUTPUT.

Reproduction harness: packages/host-watcher/test/root-recovery-benchmark.test.ts and packages/host-watcher/test/fixtures/parcel-recovery-benchmark-child.ts.

git worktree add /tmp/bb-watcher-before a2fcbaf55
git worktree add /tmp/bb-watcher-after d3078ca4c
git -C /tmp/bb-watcher-before checkout d3078ca4c -- packages/host-watcher/test/root-recovery-benchmark.test.ts packages/host-watcher/test/fixtures/parcel-recovery-benchmark-child.ts
pnpm --dir /tmp/bb-watcher-before install --frozen-lockfile
pnpm --dir /tmp/bb-watcher-after install --frozen-lockfile

BB_WATCHER_ROOT_RECOVERY_BENCHMARK=1 BB_WATCHER_BENCHMARK_ITERATIONS=20 BB_WATCHER_BENCHMARK_WARMUPS=2 BB_WATCHER_BENCHMARK_OUTPUT=/tmp/watcher-before.json pnpm --dir /tmp/bb-watcher-before exec turbo run test --filter=@bb/host-watcher --force --env-mode=loose -- --run test/root-recovery-benchmark.test.ts --silent=false
BB_WATCHER_ROOT_RECOVERY_BENCHMARK=1 BB_WATCHER_BENCHMARK_ITERATIONS=20 BB_WATCHER_BENCHMARK_WARMUPS=2 BB_WATCHER_BENCHMARK_OUTPUT=/tmp/watcher-after.json pnpm --dir /tmp/bb-watcher-after exec turbo run test --filter=@bb/host-watcher --force --env-mode=loose -- --run test/root-recovery-benchmark.test.ts --silent=false

Regression and validation

  • Applying the new proxy test to a2fcbaf55 fails with expected [ Array(2) ] to have a length of 1 but got 2; it passes on d3078ca4c:

    pnpm exec turbo run test --filter=@bb/host-watcher --force -- --run test/parcel-watcher-proxy.test.ts -t "routes rescan-required errors only to the affected subscription"
  • pnpm exec turbo run test --filter=@bb/host-watcher --force — 47 passed, 3 opt-in/platform-skipped.

  • pnpm exec turbo run typecheck --filter=@bb/host-watcher — passed.

  • pnpm exec turbo run build --filter=@bb/host-watcher --filter=@bb/host-daemon — 5 Turbo tasks passed; watcher child bundle built at 1,945 bytes.

  • pnpm exec turbo run test --filter=@bb/host-daemon --force -- --run src/watch-manager.test.ts test/parcel-watcher-not-loaded-in-parent.test.ts — 19 passed.

  • The full host-daemon run reached 564/565 passing tests, but current origin/main and this branch both fail host-daemon typecheck at host-branches-dispatch.test.ts:392 because host.list_branches is absent from the current command union. That unrelated daemon work is not changed here.

No linked issue.

AGENT GENERATED

@ymichael

Copy link
Copy Markdown
Collaborator Author

@slopcop

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 SLOP COP 🚨 · review

This pull request changes watcher overflow recovery so one busy subscription does not restart every watcher subscription in the process.

I am reviewing it now. I will check security, code quality, architecture, duplicate code, performance, and the relevant test path.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 SLOP COP 🚨 · review

This change keeps a file-system overflow tied to the one watcher that reported it. The affected watcher retries and scans itself again.

Other watchers stay active. Fatal watcher errors still restart the shared child process.

I found no blocking security, correctness, architecture, or performance defect.

The child uses the existing overflow classifier. The parent uses the subscription ID to route the error to one watcher.

RootSubscription keeps control of its retry and scan policy. I found no duplicate recovery system and no need for a production refactor.

This private child protocol ships with the daemon bundle. It does not change server-to-daemon data, so no daemon protocol increase is necessary.

I found three low-priority test improvements:

  • The benchmark checks stable counts, but it does not check the expected values.
  • The benchmark measures proxy dispatch, but it does not measure the complete RootSubscription recovery path.
  • Turbo needs --env-mode=loose to pass the benchmark variable. The pull request gives the correct command.

Validation passed for the changed proxy test file, all 12 tests, the host-watcher typecheck, and git diff --check.

The optional benchmark passed all 13 tests. It showed zero child restarts and no effects on 99 unrelated subscriptions.

GitHub CI passed. A full local package run had timeouts in unchanged tests during high host load.

Browser testing with doobie does not apply because this change has no browser path. The independent final review gate returned APPROVE.

@ymichael
ymichael force-pushed the bb/pr-targeted-watcher-overflow-recovery-thr_jsgwf2fqgt branch from 562d7e8 to d3078ca Compare August 28, 2026 21:48
@ymichael

Copy link
Copy Markdown
Collaborator Author

@slopcop

@ymichael
ymichael merged commit ae299b3 into main Aug 28, 2026
15 checks passed
@ymichael
ymichael deleted the bb/pr-targeted-watcher-overflow-recovery-thr_jsgwf2fqgt branch August 28, 2026 21:55
SawyerHood added a commit that referenced this pull request Sep 25, 2026
## Human comments

## What was wrong

Host and runtime tests covered the same contracts at two layers.
Examples:
- `workspace-dispatch` tests that were exact twins of the `host-files`
handler tests;
- agent-runtime recovery and provider-registry tests left over after
#2325 moved recovery onto typed hints and made the registry id-agnostic;
- `managed-config` tests repeated by the server-move service tests.

Others tested a shared helper instead of their subject: the plugin-host
env test called the `process-utils` sanitizer directly and never touched
`PluginHostManager`. Two could not detect their bug:
- the concurrent skill-staging test lacks a pinned clock, so it catches
a same-millisecond temp-dir collision only by chance;
- the watcher recovery harness is env-gated and asserts constants it
hard-codes.

A few tests kept exports alive only for themselves:
`listPathsRecursively`, `diffCumulativeText`, `sendJsonRpc` and
`toJsonRpcMessage`.

## What changed

Found by a read-only test audit using the repo `test-audit` skill
(#4361).

- **Duplicate tests deleted.** Tests were tightened first where needed;
for example, the relative-path handler test now asserts the error code
and message.
- **`runtime.fake-approvals.test.ts` deleted:** its unique assertions
move into `runtime.interactive-requests.test.ts`, which now asserts the
full command approval payload (reason, cwd, actions).
- **`listPathsRecursively`:** its depth and exclusion cases now run
through `listWorkspacePaths`, and the function is no longer exported.
The dispatch test lists a nested directory.
- **`diffCumulativeText`:** its tests fold into the assembler test, and
it is no longer exported.
- **`sendJsonRpc`:** inlined into `sendJsonRpcRequest`.
`toJsonRpcMessage` is no longer exported.
- **Unchanged:** request bytes, published Plugin SDK output and
host-daemon wire shapes.
- **Not in this PR:** the opt-in root-recovery benchmark (#2666),
pending its author.

Production +4/−15, tests +176/−1080.

## How you verified

- `pnpm exec turbo run test lint typecheck` is green across host-daemon,
agent-runtime, host-watcher, host-workspace, provider-bridge-acp,
provider-bridge-protocol and provider-parity.
- The only failures are the two `command-discovery` tests that fail on
this machine when `/tmp/.agents` exists; they pass with
`TMPDIR=/var/tmp/...`.
- Off-pipeline suites:
  - provider-parity: 59/59.
  - integration `environment-actions`: 5/5.
- agent-runtime `test:integration`: only the two real-Codex tests that
also fail on base.
- An independent verifier used before and after worktrees on 175d5ac
and one mutation per removed test (39 tests).
- Result: 31 OK, 4 with no unique failure mode, 4 partial losses. All 4
are restored here, and each fails under its mutation.
- It also built `@get-bb/plugin-sdk` in both trees: `dist/` and
`bundled-types/` are byte-identical.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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