Repository navigation
Conversation
ymichael
force-pushed
the
bb/bound-thread-search-by-the-page-not-the-match-co-thr_xw9jr49act
branch
from
November 20, 2026 23:32
2739d9d to
ae196cf
Compare
Thread search ranked and counted every matching thread, and reached each match by reading its full thread_search_segments row (text inline), so a common word read and sorted thousands of threads to return twenty. Search now builds one in-memory set of the query's matching segments from FTS, then walks threads in result order through a new partial index on threads ((archived_at IS NOT NULL), updated_at, id) and stops when the page is full. Per-thread checks probe the set from the covering segment indexes, so only the page's own segments are read in full. When the rarest word matches at most 1,000 segments, the walk visits only those segments' threads. Group totals are counted up to 100; the response gains totalCapped, true when more threads match. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The rebased query-cost seed indexes about 48 segments per thread, and search grows 3.4-3.85x across the history worlds: the FTS key set holds every matching segment. Declaring it keeps the default ceiling in force instead of passing just under the undeclared 4x limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/bound-thread-search-by-the-page-not-the-match-co-thr_xw9jr49act
branch
from
November 20, 2026 23:44
ae196cf to
1a4abf2
Compare
ymichael
deleted the
bb/bound-thread-search-by-the-page-not-the-match-co-thr_xw9jr49act
branch
November 20, 2026 23:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
searchThreadsWithPendingInteractionStatereturns at mostlimitPerGroup(≤ 50) threads per group, but to get there it found every matching thread and ranked and counted all of them (ROW_NUMBER()andCOUNT(*) OVER (PARTITION BY archived)). It reached each matching segment's thread throughthread_search_segments, whose rows hold the message text, so a common word read and sorted roughly 200k text-heavy rows to return 20 threads. Measured on the Nov 20 production copy, FTS itself costs 3.5 ms forthe(218k segments); the time goes to these row reads and the ranking. In production this was 2,006 ms wall with 186 ms CPU (mostly I/O), and it caused the worst event-loop stall in the logged window (2,061 ms).What changed
packages/db/src/data/threads.ts: the search query is rewritten.INexpression inside one CTE, so SQLite builds its lookup once (REUSE LIST SUBQUERY). Separate per-word sets were rebuilt at every use and made multi-word queries ~3x slower.thread_search_segments. Full rows are read only for the page's own segments.source_seq, the same result as before: each message segment is keyed by its own event sequence, and titles have a nullsource_seq.packages/db/drizzle/0164_thread_search_order_index.sql: partial indexthreads ((archived_at IS NOT NULL), updated_at, id) WHERE deleted_at IS NULL AND visibility = 'visible'on the smallthreadstable. No index onevents.totalcounts matching threads up to 100, and each group gainstotalCapped: boolean, true when more than 100 threads match (totalis then 100). The field is additive, so existing readers keep working. The app does not show totals (the palette and archived settings read onlyresults), and the tasks plugin ignores them. The only place they show is the JSON frombb thread search --json/sdk.threads.search. I updated the CLI JSON shape (apps/cli/src/json-shapes.ts) andbb-guide-json.md. The contract change ships in the plugin SDK types, so the SDK is bumped to 0.6.175.query-plan-allowlist.ts: removed the staleordered_threadsentry. Addedcorrelated-subqueryandnested-loopentries onthread_search_segmentsfor this function, with reasons: the walk is bounded by the page and by 101 counted matches, andquery_tokenshas one row per search word.threads-cases.ts: on the realistic search seed from Time server and plugin SQL in the query cost check, and fail every case on its ceiling #7934 (about 48 segments per thread), the case grows 3.4–3.85x across the 16x history world, because the FTS key set holds every matching segment. It keeps agrowsWithdeclaration with that reason rather than passing just under the undeclared 4x limit, and drops Time server and plugin SQL in the query cost check, and fail every case on its ceiling #7934's raised 250 ms ceiling, so the default ceiling applies.How you verified
Real data: a private copy of the Nov 20 production database (5,150 threads, 248,531 search segments) with the migration applied. Old and new code ran in the same process on the same file, warm cache.
Median of 7 calls,
limitPerGroup=20:thefixtestbbpraethread searchserver perf queryfix the bugsearch rankingkubernetes(7 segments)zzzqx(no match)The last three are slower by a few milliseconds. Rare and no-match queries now build the set and probe the rare path. In
search rankingboth words are too common for the rare path but seldom appear together, so the walk visits more threads. One-letter words keep a ~57 ms floor: FTS has no prefix index for one character, so it scans the term list either way.Equivalence: 250 queries (random words and prefixes from real titles and prompts, plus hand-picked edge cases), each at limits 1, 5, 20 and 50, for 1,000 calls in total. Every page was identical to main, matches and snippets included. Every
totalequalledmin(main's total, 100), andtotalCappedwas set exactly when main's total was above 100. Total time for the 1,000 calls: 109.1 s on main, 43.5 s on this branch.check:query-costs --group threads, after rebasing onto #7934's seed:searchThreadsWithPendingInteractionStatemeasures 7.15 → 24.28 ms (3.4x) here, under the default ceiling; #7934 had raised its ceiling to 250 ms on main.Tests:
packages/db/test/data/thread-search.test.tsgains two tests.totalCapped: true; after one is deleted, 100 matches report 100 withtotalCapped: false.key / Kis no longer exact and the rare path returned nothing. This test fails when that bug is reintroduced.dropThreadSearchOrderIndexintest/helpers/rewind.ts, like earlier migrations.@bb/dbsuite (738), serverpublic-thread-search+request-path-statements(70), app palette/search/cache-owner (126), clijson-shapes+startup-graph(10).turbo run typecheckfor db, server-contract, sdk, server, app and cli.🤖 Generated with Claude Code