Skip to content

Keep path search on huge workspaces from dropping the host - #5507

Merged
ymichael merged 3 commits into
mainfrom
t3code/fix-issue-5385
Oct 14, 2026
Merged

ymichael merged 3 commits into
mainfrom
t3code/fix-issue-5385

Conversation

@ymichael

@ymichael ymichael commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

host.list_paths / host.list_files walk the whole workspace and then fuzzy-rank every path synchronously on the host daemon's event loop. On a workspace with about 1M non-ignored paths, one packages/ query takes about 14 s of CPU. A typing burst queues several of these back to back, so the daemon sends nothing, heartbeats included, for longer than the 30 s lease. The server then logs Daemon sent nothing within its lease; closing its socket and every host request fails with host_unavailable (#5385). Searches the browser had already abandoned also kept running to completion. Server-side timeout retries re-sent the same search under a new request ID. So the daemon stayed busy with stale work, and the newest keystroke still timed out.

What changed

  • Time-sliced ranking (apps/host-daemon/src/command-handlers/file-list.ts): listings over 10,000 paths are ranked in 10k chunks with a setImmediate yield between chunks. Each chunk's top N are re-ranked as finalists, which gives the same results as a single pass. Large rankings run one at a time in a serial lane, so concurrent bursts cannot stack long event-loop blocks.
  • Pre-filter (packages/fuzzy-match/src/index.ts): fuzzyMatchPaths skips paths that cannot contain the query's characters in order, before building fzf rune arrays or structured segment matchers. The check is case-insensitive ASCII. Any non-ASCII query or path is kept, so fzf's diacritic normalization still applies. Rankings are unchanged.
  • Cancellation, a wire change:
    • New server→daemon message host-rpc.cancel { requestId } in packages/host-daemon-contract/src/session.ts. HOST_DAEMON_PROTOCOL_VERSION 235 → 236.
    • Server: NotificationHub.requestHostOnlineRpc accepts an optional AbortSignal. When the signal aborts, or the wait times out, it drops the waiter and sends host-rpc.cancel. The project, environment, files and thread-data path/file routes pass context.req.raw.signal. An aborted call maps to 408 request_cancelled, which keeps it out of error logs; the client has already disconnected.
    • Daemon: CommandRouter keeps an AbortController per online RPC and aborts it on host-rpc.cancel. Online RPC handlers now receive a required signal. Path and file search check it before ranking and between chunks, so queued searches exit without ranking. They fail with expected error code cancelled. Other handlers ignore the signal for now.
  • Listing cache (apps/host-daemon/src/command-handlers/file-list.ts): listings that take at least 500 ms to walk are cached per root and listing options. A search reuses the cached listing, starts a background refresh once it is 2 s old (one refresh at a time), and walks again before answering once it is 30 s old. Listings that walk in under 500 ms are never cached, so small workspaces still see file changes immediately. On a 1M-path workspace this removes a ~3 s walk from every keystroke; the cached listing holds about 200 MB while it lives.
  • Refresh spinner (apps/app/src/components/promptbox/mentions/MentionMenu.tsx): while newer mention results load behind the ones on screen (debouncing or fetching), the first section header shows a small spinner, labelled "Updating results" for screen readers. Its space is always reserved, so the rows never move. usePathSuggestions / usePromptMentions expose isRefreshing. The mention config passed to the composer (TypeaheadMentionConfig) gains isRefreshing, and the mention menu's results state (MentionMenuState) gains refreshing.
  • No CLI or SDK surface changes. The UI already debounced and aborted superseded queries; those aborts now reach the daemon.

How you verified

Fixture: an untracked workspace with 1,020,204 paths under nested git clones, matching the issue's repro.

  • Reproduced before the fix: a typing burst against GET /api/v1/projects/:id/paths made the server log Daemon sent nothing within its lease; closing its socket, and the daemon disconnected with 1006 and reconnected.
  • Event-loop block, 10 concurrent searches: before, 38 s in one stretch; after, 0.2–0.7 s at most.
  • Typing burst plus /files/read probes, after the fix: 0 lease, disconnect or stall log lines. All 20 file reads made during the burst returned 200, the slowest in 1.2 s.
  • Pre-filter, old vs new fuzzyMatchPaths on the fixture: identical results for 10 queries. Specific queries got 5–28× faster, for example repo-7/pkg-42/file-13 went from 3.7 s to 0.13 s. Broad queries (p, packages/) are unchanged.
  • Cancellation: 8 packages/ searches aborted after 1 s, then a real search. With the cancel send disabled: 504 after 30 s, daemon at 100% CPU. With this PR: 200 in 1.7–2.0 s, daemon CPU 0.1%.
  • Listing cache, typing pkg-42/file-3 → file- → file-2 → file- → file-1: first search 3.3 s (walks once), then 0.24–0.38 s per keystroke, including while a background refresh runs. Before the cache, every keystroke took ~3 s.
  • Refresh spinner: checked in the app on the 1M-path fixture. The spinner sits next to "Workspace" over the stale file-1 results while typing @pkg-42/file-2, then clears, and the rows stay in the same place.
  • Tests added:
    • Chunked ranking matches a single pass.
    • Timers run during large searches. This test fails with chunking disabled.
    • Searches cancelled mid-rank, while queued, and before starting.
    • Router cancel and unrelated cancel.
    • Connection forwards host-rpc.cancel.
    • Hub sends a cancel on abort and on timeout, and never sends a request whose signal is already aborted.
    • Contract parse.
    • Slow listings are reused and refreshed in the background (this test fails with caching disabled), and expired listings walk again.
    • The menu reserves the spinner space and toggles only its visibility; usePromptMentions reports isRefreshing while newer results load behind the current ones.
    • Pre-filter edge cases: accents, smart-case, mixed case.
  • Commands:
    • pnpm exec turbo run typecheck: all tasks pass.
    • pnpm exec turbo run test --filter=@bb/host-daemon: 779 passed.
    • --filter=@bb/app: 5,693 passed; --filter=@bb/client-core: 279 passed.
    • --filter=@bb/server: 3,469 passed.
    • --filter=@bb/fuzzy-match: 32 passed.
    • --filter=@bb/host-daemon-contract: one failure, payload-size.test.ts (gzip byte snapshot). It fails the same way on the base commit on this machine (JSON byte counts match; only the gzip counts differ) and is unrelated.

Known limit: broad queries that genuinely match a large share of paths are still slow. On the fixture, pkg-file-2 matches 190,000 paths and takes ~1.7 s to rank, most of it inside fzf's scoring.

Follow-ups not in this PR: ranking chunks in parallel worker threads for broad queries, a watcher-driven listing cache, cheaper structured matching, and per-project excludes / .bbignore.

Fixes #5385

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 3 commits October 14, 2026 12:26
Ranking a million-path listing ran synchronously on the daemon's event
loop, so a burst of @-mention searches starved heartbeats past the
30 s lease and the server closed the daemon session.

- Rank large listings in 10k-path chunks that yield between chunks,
  one large ranking at a time.
- Skip fuzzy scoring for paths that cannot contain the query's
  characters in order.
- Add a host-rpc.cancel message: the server sends it when the HTTP
  client aborts or its wait times out, and the daemon aborts the
  matching path or file search. Bumps HOST_DAEMON_PROTOCOL_VERSION
  to 236.

Fixes #5385

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On a million-path workspace every keystroke re-walked the tree, about
3 s, while ranking took about 0.25 s. Listings that take at least 500 ms
to walk are now cached per root and options: searches reuse the cached
listing, refresh it in the background once it is 2 s old, and walk
again in the foreground once it is 30 s old. Faster listings are never
cached, so small workspaces still see every file change immediately.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
While newer path, thread or plugin mention results load, the menu kept
showing the previous results with no indication they were stale. The
mention hooks now report isRefreshing when results are on screen and a
search is still debouncing or fetching, and the first section header
shows a spinner whose space is always reserved, so the rows do not move.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 1c6b453 into main Oct 14, 2026
47 checks passed
@ymichael
ymichael deleted the t3code/fix-issue-5385 branch October 14, 2026 20:08
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.

Path search on a large workspace stalls the host daemon past its lease, server drops the local host

1 participant