Skip to content

Latest thread-state lookup is still linear in extension-state history after #4931; 0.6–1.4 s freezes every ~2 s #6825

Closed

Description

@VladimirGVP

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.db 17.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

  1. Put the kind in the index: a stored/generated kind column 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.
  2. Or keep a small "latest state per (thread, kind)" table updated on insert.
  3. Compact superseded thread/extensionState/updated rows (only the latest per kind is read), or document that providers should throttle extension.state deltas.
  4. 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

  1. added theissue type on Nov 2, 2026
  2. added
    threads
    confirmed-repro
    on Nov 2, 2026
  3. bb-slop-cop commented on Nov 2, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority High · Effort High; labels threads, perf.
    • Report: Reproduction and root-cause analysis.
    • Verdict: REPRODUCED · root-cause confidence high · confirmed-repro.
    • Root cause: The partial index keys only thread and sequence. Old or absent kinds require scanning newer extension-state rows and evaluating their JSON kind.
    • Verification: Two clean checkouts reproduce the same growth: 565 → 48,053 VM instructions for 64 → 6,000 unrelated snapshots. The actual production function also slows with history; 145 existing tests and the database typecheck pass. Exact deployment stall cadence and proxy resets remain unverified.
    • No PR: A kind-aware index requires a schema migration, which the automatic simple-fix rules exclude.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproperfthreads

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions