Skip to content

Scan only new thread-state rows on repeated latest-state lookups - #6838

Merged
ymichael merged 1 commit into
mainfrom
bb/triage-and-resolve-issue-6825-thr_9rhwxwh85k
Nov 2, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/triage-and-resolve-issue-6825-thr_9rhwxwh85k

Conversation

@ymichael

@ymichael ymichael commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

listLatestThreadStateEventRowsByThreadIds finds the latest goal event or thread/extensionState/updated row of one kind per thread. Its partial index (events_thread_state_thread_sequence_idx) keys only (thread_id, sequence), not the kind. When the requested kind's latest row is old or absent, SQLite walks back through every newer extension-state row and json_extracts its data to check the kind. #4931 removed the repeated re-scan but left this one scan per thread. The thread list repeats the lookup every refresh. With a provider that publishes ~20 KB snapshots per change (5,724 in one thread for the reporter), that means hundreds of MB of JSON parsed per refresh and 0.6–1.4 s event-loop freezes every ~2 s. The shape dates back to #2164 (Aug 21), which widened the index to all extension-state kinds; it is not a recent regression. Investigation report: https://get-bb.github.io/reports/issues/6825.html

What changed

packages/db/src/data/events.ts: the lookup now keeps a per-connection watermark cache. For each (thread, kind) it remembers the latest matching sequence and the newest state sequence already scanned. Later lookups scan only state rows past that watermark, using a bounded (thread_id=? AND sequence>?) seek on the existing index, and then fetch the remembered row by (thread_id, sequence).

  • No schema change or migration. This deliberately deviates from the issue's suggested kind-aware index: building an expression index on events would stall startup on large databases (12.5 s on a 9 GB copy), and any malformed data row would fail the migration.
  • No pruning of superseded snapshots. Plugins page through extension-state history through the SDK (plugins/provider-codex/src/async-questions.ts).
  • Invalidation:
    • Entries are dropped when the thread's event rewrite generation changes (rewinds, rewrites, pruning) or when PRAGMA data_version changes.
    • A thread is rescanned when its scanned watermark went backwards or the remembered row no longer exists or matches.
    • Reads inside a transaction bypass the cache, so rolled-back rows are never remembered.
  • The signature narrows from DbQueryConnection to DbConnection; every caller already passes a connection.
  • Callers and results are unchanged. The first lookup per (thread, kind) after a server start still pays a full scan, once.

How you verified

  • New tests in packages/db/test/data/events.test.ts (repeated latest-state lookups): appends, goal events, a rewind that reinserts at already-scanned sequences, a deleted remembered row, rows removed below the watermark, and a rolled-back transaction. I disabled each of the four guards in turn, and each time the matching test failed.

  • packages/db/test/query-plans.test.ts: a warm lookup binds the previous watermark and uses a sequence>? range seek on events_thread_state_thread_sequence_idx, with no temp B-tree.

  • Benchmark in the issue's shape (200 threads; 8 with 5,000 × 21 KB snapshots), thread-list lookup for provider-codex/goal:

    main this PR
    first refresh 327 ms 328 ms
    later refreshes, 2 new snapshots per heavy thread between each ~310 ms 0.5–1.4 ms
  • pnpm exec turbo run test typecheck --filter=@bb/db (650 passed). turbo run typecheck --filter=@bb/server. Server suites covering the goal banner, timeline head state, session options and runtime display (174 passed).

  • Ran pnpm start:worktree on this branch; the app and thread API respond normally.

Fixes #6825

AGENT GENERATED

🤖 Generated with Claude Code

The latest-state lookup for a kind whose newest row is old or absent read
and json_extracted every newer extensionState snapshot in the thread. The
thread list repeats this every refresh, so threads whose provider
publishes large snapshots often froze the server for most of a second
each time.

Remember, per connection, each (thread, kind) lookup's latest matching
sequence and the newest state sequence already scanned, and on the next
lookup scan only state rows past that watermark. No schema change: the
existing partial index serves the bounded range seek.

Cached entries are dropped when the thread's event rewrite generation or
the database data_version changes, rescanned when the scanned watermark
went backwards or the remembered row no longer matches, and never written
from inside a transaction.

Fixes #6825

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit f7e97fc into main Nov 2, 2026
32 checks passed
@ymichael
ymichael deleted the bb/triage-and-resolve-issue-6825-thr_9rhwxwh85k branch November 2, 2026 19:16
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.

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

1 participant