Repository navigation
Description
Summary
On 0.48.1 the BB server still freezes its event loop for 0.6–1.4 s about every 2 seconds in listLatestThreadStateEventRowsByThreadIds. This is the same function as #5234 / #4931, and the #4931 rewrite is present in 0.48.1 (VALUES list + one descending scalar rowid lookup per thread via events_thread_state_thread_sequence_idx). The rewrite removed the correlated re-scan, but the per-thread lookup is still linear in that thread's whole extension-state history: the requested kind is not in the index, so when the latest row of the requested kind is old or absent, SQLite reads and json_extracts the full data of every newer thread/extensionState/updated row. With a provider that publishes large snapshots often, that is hundreds of MB of JSON per thread-list refresh.
Environment
- bb-app 0.48.1, Linux x86_64, Node 24.19.0.
bb.db17.6 GB (4.29 M pages of 4 KiB), 1,337 threads.
Evidence
Server log, ~3.6 h: 6,039 Slow DB query records, 2,750 s blocked in total. 5,841 of them (97 % of the blocked time) are this statement: median 612 ms, max 1,428 ms, 3,293 runs ≥ 500 ms. Batches carry up to 1,288 thread ids. The rest is mostly 179 TRANSACTION DEFERRED waits (median 213 ms, max 2.1 s) behind those reads.
What the partial index holds (40,173 rows, 125 threads):
| kind | rows | threads | data |
|---|---|---|---|
provider-pi-durable/runtime-graph (third-party provider) |
40,142 | 115 | 868 MB (avg 21.6 KB, max 40 KB) |
bb/session-options |
26 | 5 | 3 KB |
bb/provider-commands |
5 | 5 | 37 KB |
The heaviest thread has 5,724 snapshots (103 MB). The provider emits one extension.state delta with a full snapshot per change (up to ~3,600/hour on busy days); BB stores each as a new event and only the latest per kind is ever read.
Reproduction (same SQL, read-only connection, warm cache), kind provider-codex/goal (LEGACY_CODEX_GOAL_EXTENSION_KIND, used by listPromptBannerActivityCandidateRows → buildThreadActivityStateByThreadId on thread-list refresh):
| requested threads | time |
|---|---|
| all 1,337 | 740–750 ms (two runs) |
| the 125 with extension state | 669 ms |
| the other 1,212 | 0 ms |
| one heavy thread (5,724 snapshots) | 76 ms |
one heavy thread, kind = its latest row (provider-pi-durable/runtime-graph) |
0 ms |
The #4931 benchmark used 4,141-byte payloads and up to 500 events per thread (6 ms); real histories here are ~10× longer with ~5× larger payloads.
Other callers with the same cost per thread: listLatestTimelineHeadStateRows (four kinds per timeline load), getThreadTimelineGoal, readLatestThreadState.
Side effect: while the loop is blocked, a reverse proxy in front of BB gets bursts of connection reset by peer (502): Node's default keepAliveTimeout of 5 s fires on sockets the proxy already reused. Before we shortened the proxy's upstream keep-alive to 3 s we saw 63 such 502s in 16,308 requests over 3 h; afterwards 0 in 2,108 requests. That works around the resets, not the freezes.
Expected
The latest-state lookup costs O(1)–O(log n) per (thread, kind), independent of how many snapshots other kinds or older rows have.
Possible fixes
- Put the kind in the index: a stored/generated
kindcolumn or a partial expression index(thread_id, json_extract(data,'$.kind'), sequence) WHERE type = 'thread/extensionState/updated', and look up goal events and extension-state-of-kind separately. - Or keep a small "latest state per (thread, kind)" table updated on insert.
- Compact superseded
thread/extensionState/updatedrows (only the latest per kind is read), or document that providers should throttleextension.statedeltas. - Optionally cache the thread-list activity per thread until a new state event arrives.
We will also throttle the snapshots on the provider side, but existing rows keep the lookup slow until BB changes the query or prunes them.
Activity
bb-slop-cop commentedon Nov 2, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotthreads,perf.confirmed-repro.Drop a thread's stored conversation outline when its history is rewou…
Scan only new thread-state rows on repeated latest-state lookups (get…