Repository navigation
Description
Summary
We've been fixing uncached hot-path Drizzle queries one file at a time (already merged: #8362 / #8369, #8363 / #8367, #8364 / #8368, #8365 / #8366; newly filed: #8437, #8438, #8439), because neither of the repo's existing database perf tests catches repeated SQL compilation automatically:
packages/db/test/prepared-queries.test.ts#L23-L63only checks a manual opt-in list of 9 functions.apps/server/test/perf/request-path-statements.test.ts#L63-L80checks whether statement count grows with history size, not whether warm requests recompile SQL or re-calldb.$client.prepare().
Adding a low-level duplicate-prepare() guard (either on warm requests in request-path-statements.test.ts or alongside query-plan-test-guard.ts with an allowlist like query-plan-allowlist.ts) would flush out and prevent this entire class of regressions automatically instead of playing whack-a-mole.
What a full-suite sweep catches right now (272957819d)
Instrumenting Database.prototype.prepare across all 518 test files (74 in @bb/db + 444 in @bb/server, excluding tests and migrate.ts) to count identical (connection, sql) strings prepared more than once on the same SQLite connection surfaced:
- 1,141,421 duplicate
db.$client.prepare()calls of identical SQL strings - 684 unique
(callsite, sql)pairs across 58 production files
Top 10 files by duplicate prepare() calls (89% of total)
| File | Duplicate prepare()s |
Unique SQLs | Examples of what the guard catches |
|---|---|---|---|
packages/db/src/data/events.ts |
573,435 | 71 | insertStoredEventRow (332,329 — #8437), timeline window queries (#8439) |
packages/db/src/data/thread-pruning.ts |
187,024 | 35 | prepareThreadPruningCursor, getNextThreadPruningPolicy, drainPendingThreadPruningWork |
packages/db/src/data/app-settings.ts |
116,479 | 5 | writeValue (96,639), getDisabledProviderIds (13,963) |
packages/db/src/data/threads.ts |
56,827 | 48 | upsertThreadSearchSegment (20,376), deleteThreadSearchSegmentById, getThreadStartupContext, isThreadCopyingForkHistory (#8438) |
packages/db/src/data/thread-pruning-work.ts |
49,614 | 2 | markThreadPruningWork (49,181 — #8437) |
packages/db/src/data/resolved-item-pruning.ts |
19,268 | 19 | Pruning cursor / batch statements |
packages/db/src/data/plugin-marketplaces.ts |
16,724 | 6 | getPluginMarketplace (9,156), getPluginMarketplaceIcon |
packages/db/src/data/environment-variables.ts |
14,483 | 6 | listEnvironmentVariables (14,428) |
packages/db/src/data/thread-run-state.ts |
10,437 | 10 | Remaining write/update queries |
packages/db/src/data/environments.ts |
10,385 | 30 | getPreparingEnvironment, environmentHasLiveThreads |
Proposed Guardrail Options
-
Warm-request compilation guard in
apps/server/test/perf/request-path-statements.test.ts(highest signal, zero cold-path noise):- Run each of the 34 real request paths once to warm per-connection statement caches, then record
db.$client.prepare()(andSQLiteSyncDialect.prototype.sqlToQuery) on a second warm execution. - Fail if any fixed-shape SQL string is re-prepared on the warm pass (with an explicit allowlist for genuinely dynamic
IN (...)batch queries). - Catches regressions on every hot HTTP/WS/daemon/sweep path without nagging cold admin or migration queries.
- Run each of the 34 real request paths once to warm per-connection statement caches, then record
-
Duplicate-SQL detector in
packages/db/src/query-plan-test-guard.ts(broadest coverage):- In CI audit mode, track
(connection, sql)pairs ondb.$client.prepare()and report any production call site underpackages/db/src/data/that prepares the exact same SQL string more than once on the same connection, backed by an explicit allowlist mirroringquery-plan-allowlist.ts.
- In CI audit mode, track
Related Issues
- Already merged: db: hot-path turn and event-state queries in events.ts block the main event loop 13×–47× longer by recompiling SQL on every call #8362, db: hot-path queue-drain queries in queued-thread-messages.ts block the main event loop 17×–27× longer by recompiling multi-alias SQL on every call #8363, db: readThreadRunState and thread-run-state queries block the main event loop 18× longer by recompiling SQL on every thread lifecycle transition #8364, db: getNonDestroyedHost, getThreadStatus, and listActiveHostThreads block the main event loop 7×–116× longer by recompiling SQL on every call #8365
- Open hot-path examples this guard would catch/prevent: db: insertStoredEventRow and markThreadPruningWork re-prepare static SQL on every single event write #8437, db: hot-path thread search indexing and startup lookups in threads.ts recompile SQL on every call #8438, db: 4 of 6 timeline window queries in events.ts were left uncached and recompile Drizzle SQL on every thread open and scroll #8439
AGENT GENERATED
Activity
bb-slop-cop commentedon Dec 1, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotApplied and verified the missing classifications:
threads,perfThis requests broader automated database-performance regression coverage, rather than a separate runtime bug fix. Existing tests cover selected cached lookups and request statement scaling; extending detection requires test instrumentation and exception handling. No existing classification values were present to preserve.
No reproduction report or pull request: this is a non-Bug task, so the rule skips those steps.
Guard warm reads against extra SQLite prepares (get-bb#8463)