Repository navigation
Answer GitHub mention searches from an index with a limit of 8 - #8219
Merged
Merged
Conversation
The issue and pull request mention providers run on every debounced keystroke. Each search read every cached row of the table, including closed rows and bodies, sorted the matches in a temporary B-tree, parsed labels and assignees for each match, and then kept 8. Add an index on items (kind, state, updated_at) and pass LIMIT 8 to the query, so SQLite walks the open rows of one kind newest first and stops after 8 matches. The CLI and listItems RPC keep their full listing.
The index walk returned rows with equal updated_at in reverse insertion order, flipping the previous order. Index (kind, state, updated_at, number) and order by number DESC so ties stay deterministic and still need no sort. Co-Authored-By: Claude Opus 5.5 <[email protected]>
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
…b#8219) ## Human comments ## What was wrong The GitHub plugin's `issue` and `pr` mention providers run `mentionItems` on every debounced `@` or `#` keystroke (https://github.com/get-bb/bb/blob/e9e0493838b52b1276625f32730c26fb908ea159/plugins/github/server.ts#L1579-L1587). `mentionItems` called `listCachedItems({ kind, query, state: "open" })` and then `.slice(0, 8)`. That query is `SELECT * FROM items ... ORDER BY updated_at DESC` and has no `LIMIT` (https://github.com/get-bb/bb/blob/e9e0493838b52b1276625f32730c26fb908ea159/plugins/github/server.ts#L762-L801), and the table has no index besides its primary key `(repo, kind, number)`. So SQLite read every cached row, also closed rows and 3.5 KB bodies. It sorted all matches in a temporary B-tree (`SCAN items | USE TEMP B-TREE FOR ORDER BY`). The plugin then parsed labels and assignees for each match and kept 8. The sync keeps up to 230 rows per tracked repository. One keystroke cost 5.65 ms at 10 repositories and 33.58 ms at 40. This work runs synchronously in the server process. I measured the real `search` path before changing anything (see the table below). ## What changed - `plugins/github/server.ts`: a new storage migration adds `CREATE INDEX items_kind_state_updated_number ON items (kind, state, updated_at, number)`, and listings order by `updated_at DESC, number DESC`. SQLite runs the mention query as `SEARCH items USING INDEX items_kind_state_updated_number (kind=? AND state=?)`. It walks the open rows of one kind newest first and does no sort. The `number` tie-breaker keeps rows with equal `updated_at` in a fixed order; without it the index walk reversed their previous order (seen on a real cache: `get-bb#835` listed 8355 before 8354 on main, 8354 first with an `updated_at`-only index). - `listCachedItems` takes an optional `limit` and binds it as `LIMIT ?`. `mentionItems` passes `limit: 8` instead of slicing, so the walk stops after 8 matches. The `bb github issues`, `bb github prs` and `listItems` RPC listings pass no limit and return the same rows as before. The new index can change only their plan, not their result. - No wire, CLI, RPC or protocol changes. Deviation from the issue's first idea (`LIMIT 8` and only `repo, number, title`): neither change alone removes the scan. The body is in the same page as the row, so the query must read every row page to find the newest 8. Measured at 40 repositories: `LIMIT 8` alone is 11-16 ms, and three columns with `LIMIT 8` is about 9 ms. With the index and `LIMIT 8`, only the 8 result rows are read. Then `SELECT *` of 8 rows costs nothing measurable, so the mention path keeps the one shared query and does not get a second column-specific copy of it. ## How you verified New test in `plugins/github/server.batching.test.ts`: "answers a mention search with the newest 8 open matches through the index". It syncs 10 matching open issues (shuffled `updatedAt`, two sharing one), a closed issue and an open pull request through the fake GraphQL response. The test then checks that the issue provider returns exactly the 8 newest issues in order, and that SQLite plans the statement of the search as an index search with no scan and no temporary B-tree. - On origin/main (unfixed), it fails: ``` AssertionError: expected [ 'SCAN items', …(1) ] to deeply equal [ StringMatching{…} ] - StringMatching /^SEARCH items USING INDEX /, + "SCAN items", + "USE TEMP B-TREE FOR ORDER BY", ``` - With `LIMIT 8` and no index, it fails on the same plan assertion. - With the index and no `LIMIT`, it fails: `expected [ { id: 'acme/one#9', …(2) }, …(8) ] to deeply equal [ { id: 'acme/one#9', …(2) }, …(7) ]` (9 results). - With the fix, it passes. Measurements: a temporary Vitest file (not committed) loads the real plugin with `createFakePluginHost`. Like the host, it uses a file-backed SQLite database with the default page cache. The file inserts 230 rows per repository (100 open and 50 closed issues, 50 open and 30 merged pull requests, 3.5 KB bodies) and times both providers' `search` calls for one keystroke. Each value is the median of 200 runs after 20 warm-up runs. | Repos (rows) | Query | Before | After | |---|---|---|---| | 1 (230) | `""` | 0.38 ms | 0.11 ms | | 1 (230) | `"a"` | 0.33 ms | 0.09 ms | | 1 (230) | `"zzz"` (no match) | 0.09 ms | 0.12 ms | | 10 (2,300) | `""` | 5.65 ms | 0.07 ms | | 10 (2,300) | `"a"` | 3.74 ms | 0.11 ms | | 10 (2,300) | `"fix"` | 0.80 ms | 0.12 ms | | 10 (2,300) | `"zzz"` (no match) | 0.43 ms | 0.41 ms | | 40 (9,200) | `""` | 33.58 ms | 0.06 ms | | 40 (9,200) | `"a"` | 32.34 ms | 0.07 ms | | 40 (9,200) | `"fix"` | 18.35 ms | 0.10 ms | | 40 (9,200) | `"item 17"` (matches only in older rows) | 16.76 ms | 7.31 ms | | 40 (9,200) | `"zzz"` (no match) | 16.02 ms | 6.95 ms | The walk stops at the eighth match. A query with fewer than 8 matches, or with matches only in older rows, still reads most open rows of that kind. It no longer reads closed rows or the other kind, and it does not sort, so it is still about 2x faster than before. Commands: ``` pnpm exec turbo run typecheck test lint lint:type-aware --continue --concurrency=6 --filter=bb-plugin-github ``` All 11 tasks pass (44 tests). `lint` reports 3 warnings in `plugins/github/app.tsx` (`react(immutability)`, two `react(set-state-in-effect)`), which are on origin/main and not in this change. Fixes get-bb#8218 > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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
The GitHub plugin's
issueandprmention providers runmentionItemson every debounced@or#keystroke (bb/plugins/github/server.ts
Lines 1579 to 1587 in 2a21dd5
mentionItemscalledlistCachedItems({ kind, query, state: "open" })and then.slice(0, 8). That query isSELECT * FROM items ... ORDER BY updated_at DESCand has noLIMIT(bb/plugins/github/server.ts
Lines 762 to 801 in 2a21dd5
(repo, kind, number). So SQLite read every cached row, also closed rows and 3.5 KB bodies. It sorted all matches in a temporary B-tree (SCAN items | USE TEMP B-TREE FOR ORDER BY). The plugin then parsed labels and assignees for each match and kept 8. The sync keeps up to 230 rows per tracked repository. One keystroke cost 5.65 ms at 10 repositories and 33.58 ms at 40. This work runs synchronously in the server process. I measured the realsearchpath before changing anything (see the table below).What changed
plugins/github/server.ts: a new storage migration addsCREATE INDEX items_kind_state_updated_number ON items (kind, state, updated_at, number), and listings order byupdated_at DESC, number DESC. SQLite runs the mention query asSEARCH items USING INDEX items_kind_state_updated_number (kind=? AND state=?). It walks the open rows of one kind newest first and does no sort. Thenumbertie-breaker keeps rows with equalupdated_atin a fixed order; without it the index walk reversed their previous order (seen on a real cache:#835listed 8355 before 8354 on main, 8354 first with anupdated_at-only index).listCachedItemstakes an optionallimitand binds it asLIMIT ?.mentionItemspasseslimit: 8instead of slicing, so the walk stops after 8 matches. Thebb github issues,bb github prsandlistItemsRPC listings pass no limit and return the same rows as before. The new index can change only their plan, not their result.Deviation from the issue's first idea (
LIMIT 8and onlyrepo, number, title): neither change alone removes the scan. The body is in the same page as the row, so the query must read every row page to find the newest 8. Measured at 40 repositories:LIMIT 8alone is 11-16 ms, and three columns withLIMIT 8is about 9 ms. With the index andLIMIT 8, only the 8 result rows are read. ThenSELECT *of 8 rows costs nothing measurable, so the mention path keeps the one shared query and does not get a second column-specific copy of it.How you verified
New test in
plugins/github/server.batching.test.ts: "answers a mention search with the newest 8 open matches through the index". It syncs 10 matching open issues (shuffledupdatedAt, two sharing one), a closed issue and an open pull request through the fake GraphQL response. The test then checks that the issue provider returns exactly the 8 newest issues in order, and that SQLite plans the statement of the search as an index search with no scan and no temporary B-tree.LIMIT 8and no index, it fails on the same plan assertion.LIMIT, it fails:expected [ { id: 'acme/one#9', …(2) }, …(8) ] to deeply equal [ { id: 'acme/one#9', …(2) }, …(7) ](9 results).Measurements: a temporary Vitest file (not committed) loads the real plugin with
createFakePluginHost. Like the host, it uses a file-backed SQLite database with the default page cache. The file inserts 230 rows per repository (100 open and 50 closed issues, 50 open and 30 merged pull requests, 3.5 KB bodies) and times both providers'searchcalls for one keystroke. Each value is the median of 200 runs after 20 warm-up runs."""a""zzz"(no match)"""a""fix""zzz"(no match)"""a""fix""item 17"(matches only in older rows)"zzz"(no match)The walk stops at the eighth match. A query with fewer than 8 matches, or with matches only in older rows, still reads most open rows of that kind. It no longer reads closed rows or the other kind, and it does not sort, so it is still about 2x faster than before.
Commands:
All 11 tasks pass (44 tests).
lintreports 3 warnings inplugins/github/app.tsx(react(immutability), tworeact(set-state-in-effect)), which are on origin/main and not in this change.Fixes #8218