Skip to content

Check query function coverage per PR, audit plans nightly and on dev and diagnostics servers - #7302

Merged
ymichael merged 4 commits into
mainfrom
bb/query-plan-coverage
Nov 9, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/query-plan-coverage

Conversation

@ymichael

@ymichael ymichael commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The slow-query-plan guard from #7292 only judges queries that tests run, and nothing noticed when an allowlist entry outlived its fix. #7288 and #7289 merged just before #7292 and #7287 just after it, so main carries seven entries for queries that are no longer slow.

What changed

Nightly coverage and stale-entry check.

  • A new Query Plan Audit workflow (.github/workflows/query-plan-audit.yml, nightly on main and runnable by hand) runs @bb/db and @bb/server tests unsharded in report mode. It is not part of PR CI: an unsharded run took 5.2 min per PR push. PRs still fail on new slow plans through the per-file guard in their existing test shards.
  • check:query-plan-report (renamed from check:query-plan-allowlist) fails on:
    • findings missing from the allowlist;
    • allowlist entries no test reported;
    • exported query functions in packages/db/src/data that no test runs.
  • packages/db/scripts/query-functions.ts finds those functions with the TypeScript AST: exported functions that call a query builder or prepare on a connection. In report mode, the guard records the data functions on each prepared statement's stack.
  • Four functions never ran:
    • getEarlyAdopter had no callers and is deleted.
    • updateProject, getSkippedAppUpdate and setPluginArtifactGitCheckoutRoot gain tests.
  • The seven fixed entries are removed.

Per-PR check for changed query functions.

  • The Linux test shards set BB_QUERY_PLAN_EXECUTED=.query-plan/executed.jsonl. The @bb/db and @bb/server test guards record the data functions on each prepared statement's stack while still failing on slow plans as before.
  • The file is a Turbo test output (.query-plan/**), so a cached run restores it.
  • The shard that runs @bb/db also writes the query function list.
  • A new Query functions run in tests job (scripts/check-pr-query-functions.mjs) downloads both, installs nothing, and fails when an exported query function in a packages/db/src/data file the PR changed never ran. On this PR it took 11 s, and the server and packages-host shards showed no measurable change.
  • Untouched files and stale allowlist entries stay with the nightly audit. Their tests may replay from cache, and a stale entry only over-permits.

Runtime audit on dev and diagnostics servers.

  • When BB_QUERY_PLAN_AUDIT=1 or the performanceDiagnostics experiment is on, the server logs Slow SQLite query plan once per rule, table and caller for unallowed findings.
  • pnpm dev, pnpm start:worktree and pnpm desktop:worktree default BB_QUERY_PLAN_AUDIT to 1. start:worktree runs its server with NODE_ENV=production, so NODE_ENV can't identify dev servers.
  • The connection's existing statement instrumentation (packages/db/src/connection.ts) hands each new statement and parameter shape to the auditor, which also covers cached statements. The auditor captures the stack synchronously. It plans the statement on setImmediate against a read-only connection to the same file (apps/server/src/services/system/query-plan-audit.ts).
  • Bundled frames are attributed through the // path module comments esbuild writes into apps/server/dist, resolved against apps/server. The bundle's 17 MB source map took about 0.9 s and 122 MB of heap to load. Resolving against the filesystem would also misattribute an npm-installed bb-app, whose bundle sits under node_modules.

Test guard.

  • Audits are queued and run in each test file's afterAll, so EXPLAIN work no longer runs inside time-budgeted code.
  • The guard core now lives in query-plan-guard.ts, shared by tests and the server. The prototype patch, report rows and report comparison live in query-plan-test-guard.ts, exported as @bb/db/query-plan-test-guard.

Docs.

  • docs/configuration.md: the env var row and the performanceDiagnostics description.
  • docs/debugging-and-qa.md: a new "Slow SQLite query plans" section.
  • The bb guide customization chapter.

Windows. Callers are read from POSIX stack paths, as in #7292, so the runtime audit logs nothing on Windows and the bundle test skips there.

Server/daemon wire. No daemon wire change.

How you verified

  • Full report run (db then server, report mode): check:query-plan-report printed "Query plan report matches the allowlist (45 findings) and runs all 362 query functions."
    • Before removing the seven entries, the same check listed them as unreported.
    • Before the new tests, it listed the four untested functions.
  • Live, pnpm start:worktree (ESM dist bundle): with the listThreadSearchMatchRows allowlist entry temporarily removed, GET /api/v1/threads/search?query=hello logged Slow SQLite query plan with caller: "packages/db/src/data/threads.ts#listThreadSearchMatchRows".
    • A repeat search logged nothing more.
  • Experiment gate, with BB_QUERY_PLAN_AUDIT=0 pnpm start:worktree --perf-diagnostics: a search logged nothing while the experiment was off. After bb settings experiment performanceDiagnostics true, the same search logged the finding without a restart.
  • Overhead: a microbenchmark of a cached lookup on a real connection measured 1.2 µs per query with the audit off and 1.5 µs with it on, for an already-audited shape (+0.33 µs across 3 rounds of 200k). Off, the hook is one call that returns null.
  • Per-PR check, locally: a temporary commit added an untested countAppSettingsRows to app-settings.ts. The check named only that function, given the db record plus server records. Against this PR's real diff it passed: "Every query function in 1 changed data file(s) runs in a test."
  • Per-PR check, on CI: the job downloaded the packages-host, server-1 and server-2 records and passed in 11 s.
  • Cache replay: deleting packages/db/.query-plan and rerunning the cached @bb/db test task restored all 603 rows.
  • Tests:
    • query-plan-guard.test.ts bundles a query function with esbuild and loads it with Node's native require. It asserts the audit is deferred until flush() and that the caller is attributed to apps/demo/src/latest-event.ts. It fails when bundle-marker resolution is disabled.
    • ci-selection.test.mjs covers the job selection.
    • The run-dev and run-desktop env tests cover the new default.
  • pnpm exec turbo run typecheck lint --filter=@bb/db --filter=@bb/server --filter=@bb/config --filter=@bb/scripts passes.
  • Local timing flakes: under load averages above 100 on this machine, thread-pruning.test.ts (with or without the guard) and builtin-plugins.test.ts hot-reload failed once each. Both pass on their own. Stop pruning tests depending on the 20 ms advance budget #7298 since removed the pruning tests' dependence on the 20 ms budget.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 3 commits November 9, 2026 13:51
…s servers

The test-suite guard from #7292 only judges queries that tests execute, and
its allowlist could keep entries after their fixes landed: #7288 and #7289
merged before it, so it shipped with seven stale entries.

A Query plan audit CI job now runs @bb/db and @bb/server unsharded in report
mode. check:query-plan-report fails on findings missing from the allowlist,
on allowlist entries no test reported, and on exported query functions in
packages/db/src/data that no test runs. A TypeScript scan finds those
functions: exported functions that call a query builder or prepare on a
connection. The report records the data functions on each prepared
statement's stack. Four functions never ran: getEarlyAdopter had no callers
and is deleted; updateProject, getSkippedAppUpdate and
setPluginArtifactGitCheckoutRoot gain tests.

The server now runs the same audit at runtime when BB_QUERY_PLAN_AUDIT is set
(pnpm dev, pnpm start:worktree and pnpm desktop:worktree default it to 1) or
the performanceDiagnostics experiment is on. The connection's statement
instrumentation hands each new statement and parameter shape to the auditor,
which captures the stack and plans the statement on setImmediate against a
read-only connection. Unallowed findings log "Slow SQLite query plan" once per
caller. Bundled frames are attributed through esbuild's module comments
rather than the 17 MB source map, which costs about 120 MB of heap to load.
A shape already seen adds about 0.3 us per query.

Audits in tests are also deferred to each file's afterAll, so EXPLAIN work no
longer runs inside time-budgeted code such as thread pruning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The guard reads callers from POSIX stack paths, so attribution is inactive on
Windows; the docs now say the audit logs nothing there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The unsharded db and server run took about five minutes per PR push. PRs
still fail on new slow plans through the per-file guard in their existing
test shards; the nightly workflow on main catches stale allowlist entries and
untested query functions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Check query plan coverage in CI and audit plans on dev and diagnostics servers Audit query plan coverage nightly and on dev and diagnostics servers Nov 9, 2026
The Linux test shards set BB_QUERY_PLAN_EXECUTED, so the @bb/db and
@bb/server test guards record the data functions on each prepared statement's
stack in .query-plan/executed.jsonl. It is a Turbo test output, so a cached
run restores it. The shard that runs @bb/db also writes the query function
list. A new "Query functions run in tests" job downloads both, installs
nothing, and fails when an exported query function in a packages/db/src/data
file the PR changed never ran. Stale allowlist entries and functions in
untouched files stay with the nightly audit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Audit query plan coverage nightly and on dev and diagnostics servers Check query function coverage per PR, audit plans nightly and on dev and diagnostics servers Nov 9, 2026
@ymichael
ymichael merged commit f8893ee into main Nov 9, 2026
53 checks passed
@ymichael
ymichael deleted the bb/query-plan-coverage branch November 9, 2026 22:21
ymichael added a commit that referenced this pull request Nov 9, 2026
## Human comments

## What was wrong

The query-plan test guard formats and parses a stack for every prepared
SQLite statement on Windows, although its POSIX-only attribution cannot
identify Windows callers. This unsupported-platform limitation was
documented in #7302, and the bundled attribution test already skips
Windows. The work consumes time without producing findings or
query-function coverage there.

## What changed

Return an empty test guard on Windows before patching SQLite's prepare
method. This is a three-line test-support change in
`packages/db/src/query-plan-test-guard.ts`; its callers are the database
and server test setup files. Runtime query auditing and Linux/macOS test
auditing and function-coverage reporting are unchanged. No test or
assertion is removed. Windows attribution remains unsupported; enabling
it later must include this platform gate.

## How you verified

- Same-runner Windows ABBA comparison of the four slow server files
passed every round (137 tests passed, five existing platform skips):
[diagnostic run](https://github.com/get-bb/bb/actions/runs/42714915975).
Baseline wall times were 90.22/68.09 seconds; candidate 62.68/54.48
seconds. Average 79.16 → 58.58 seconds, a 20.58-second (26%) reduction
for this four-file invocation, not a claim about total CI time. Both
candidate rounds beat both baseline rounds despite machine/warmup
variation.
- The same deliberate real `events` full-scan query passes with zero
guard findings on unmodified Windows baseline, confirming the existing
attribution limitation: [baseline Windows
proof](https://github.com/get-bb/bb/actions/runs/42716605198). The
diagnostic prints `DELIBERATE FULL SCAN win32 []`. Earlier
instrumentation also observed 611 and 1,406 prepares with zero
attributed callers; that initial run had diagnostic temp-path failures
and is excluded from timing claims.
- On macOS, a temporary real `events` full-scan query fails with the
expected `full-scan` finding and exact source function under the changed
guard. The probe was removed afterward.
- Existing query-plan auditor tests and database typecheck pass through
Turbo; formatting and diff checks pass. Independent review found no
issues. Normal final-head CI is green at
`2a31a2745eff30b9575e9de9296bf301cff3b609`, including all three Windows
server shards and both Linux server shards:
https://github.com/get-bb/bb/actions/runs/42716684868.

> AGENT GENERATED
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.

1 participant