Skip to content

Attribute test query plans to the running function in every mode - #8466

Merged
ymichael merged 2 commits into
mainfrom
bb/query-plan-guard-names-callers-differently-local-thr_77iyyqiiwb
Dec 1, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/query-plan-guard-names-callers-differently-local-thr_77iyyqiiwb

Conversation

@ymichael

@ymichael ymichael commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

packages/db/src/query-plan-test-guard.ts attributed a query to its caller at two different moments. With BB_QUERY_PLAN_EXECUTED or BB_QUERY_PLAN_REPORT set (CI sets BB_QUERY_PLAN_EXECUTED), it read the stack when the statement was prepared; without them (local runs), it read the stack when the statement ran. Drizzle prepares ordinary queries on every call, so both moments usually agree. A prepareCachedQuery statement is prepared inside its builder and run from the outer function, so the modes disagree. #8367 (c0394a5) cached isThreadQueueAutoSendPaused and renamed its allowlist entry to prepareIsThreadQueueAutoSendPaused, the name only CI produces. Since then, a plain local pnpm exec turbo run test --filter=@bb/db fails at file teardown in queued-thread-messages.test.ts / queued-message-state.test.ts with a correlated-subquery on events in ...#isThreadQueueAutoSendPaused violation, and about 37 @bb/server test files fail the same way.

The production auditor (observeFirstExecution, enabled by BB_QUERY_PLAN_AUDIT) also reads the stack at run time, and docs/debugging-and-qa.md defines caller as the function that ran the query. CI's prepare-time attribution was the odd one out, and the production audit has been warning on this allowlisted query since #8367.

What changed

  • packages/db/src/query-plan-test-guard.ts: the caller is read when the statement runs, and only when its plan has a finding, in every mode (one stack depth, 40; local runs used 10 before). This keeps attribution identical locally, in CI, and in the production auditor. With BB_QUERY_PLAN_EXECUTED/BB_QUERY_PLAN_REPORT, the executed query functions are still recorded when a statement is prepared, as before. A cached statement is prepared inside the function that first runs it, so that function is still credited.
  • packages/db/src/query-plan-guard.ts: QueryPlanAuditor.observe (eager caller) is removed; nothing calls it anymore.
  • packages/db/src/query-plan-allowlist.ts: the entry is renamed back to queued-thread-messages.ts#isThreadQueueAutoSendPaused. No new entry.
  • packages/db/test/query-plan-test-guard.test.ts: the subprocess test runs a cached query (isThreadQueueAutoSendPaused) under the local, executed-functions, and report modes. It asserts no violations in the first two, that every report finding names #isThreadQueueAutoSendPaused and is allowed, and that the executed output lists the running functions.

This doesn't change the CLI, the wire protocol, or the docs. This brings the guard in line with what the docs already describe.

Cost in CI mode

I measured stack reads with an instrumented guard over the full suites with BB_QUERY_PLAN_EXECUTED set. CPU is summed across workers.

@bb/server stack reads CPU @bb/db stack reads CPU
main (prepare-time caller) 952k 56 s 245k 8.5 s
first commit (stack read on every run) 2.47M 115 s 428k 21.7 s
this PR (prepare-time recording + lazy run-time caller) 1.03M (956k prepare + 76k caller) 48.5 s 246k 7.5 s

Reading the stack on every run credited no additional query function; it only dropped 46 private prepare* builder names. So the second commit goes back to prepare-time recording.

How you verified

  • Without the fix, the new test fails: local mode reports three violations on #isThreadQueueAutoSendPaused. With the fix, it passes.
  • pnpm exec turbo run test typecheck --filter=@bb/db --filter=@bb/server with no query-plan env vars: passes.
  • BB_QUERY_PLAN_EXECUTED=.query-plan/executed.jsonl pnpm exec turbo run test --filter=@bb/db --filter=@bb/server: passes, and executed.jsonl is written for both packages.
  • Query Plan Audit path: BB_QUERY_PLAN_REPORT=<file> over the same suites passes, then pnpm --filter @bb/db check:query-plan-report <file> prints "Query plan report matches the allowlist (855 findings) and runs all 554 query functions."
  • Earlier runs on the first commit saw apps/server/test/app/install-machine-script.test.ts time out while host load average was 43–77. That file doesn't touch the database, passes 56/56 alone, and passed in all runs on this commit.
  • oxlint and prettier pass on the changed files.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 30, 2026 21:43
The test query-plan guard named a query's caller at prepare time when
BB_QUERY_PLAN_EXECUTED or BB_QUERY_PLAN_REPORT was set (CI) and at run
time otherwise (local runs). Statements cached with prepareCachedQuery are
prepared in a builder and run elsewhere, so the two modes disagreed: after
#8367 the allowlist named prepareIsThreadQueueAutoSendPaused, which only
CI produced, and local @bb/db and @bb/server runs failed on
isThreadQueueAutoSendPaused.

Capture the caller and the executed query functions when a statement
runs in every mode, with one stack depth, matching the production
auditor. The allowlist names isThreadQueueAutoSendPaused again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reading the stack on every statement run in CI mode cost 2.6x the stack
reads of the old prepare-time recording (2.47M vs 0.95M in @bb/server,
115 s vs 56 s of worker CPU) and credited no additional query function.
Keep the run-time caller, read lazily only when a plan has a finding, in
every mode, and record executed functions when a statement is prepared.
Drop QueryPlanAuditor.observe, which no longer has callers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 864cff5 into main Dec 1, 2026
36 checks passed
@ymichael
ymichael deleted the bb/query-plan-guard-names-callers-differently-local-thr_77iyyqiiwb branch December 1, 2026 06:25
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…-bb#8466)

## Human comments

## What was wrong

`packages/db/src/query-plan-test-guard.ts` attributed a query to its
caller at two different moments. With `BB_QUERY_PLAN_EXECUTED` or
`BB_QUERY_PLAN_REPORT` set (CI sets `BB_QUERY_PLAN_EXECUTED`), it read
the stack when the statement was prepared; without them (local runs), it
read the stack when the statement ran. Drizzle prepares ordinary queries
on every call, so both moments usually agree. A `prepareCachedQuery`
statement is prepared inside its builder and run from the outer
function, so the modes disagree. get-bb#8367 (4f35332) cached
`isThreadQueueAutoSendPaused` and renamed its allowlist entry to
`prepareIsThreadQueueAutoSendPaused`, the name only CI produces. Since
then, a plain local `pnpm exec turbo run test --filter=@bb/db` fails at
file teardown in `queued-thread-messages.test.ts` /
`queued-message-state.test.ts` with a `correlated-subquery on events in
...#isThreadQueueAutoSendPaused` violation, and about 37 `@bb/server`
test files fail the same way.

The production auditor (`observeFirstExecution`, enabled by
`BB_QUERY_PLAN_AUDIT`) also reads the stack at run time, and
`docs/debugging-and-qa.md` defines `caller` as the function that ran the
query. CI's prepare-time attribution was the odd one out, and the
production audit has been warning on this allowlisted query since get-bb#8367.

## What changed

- `packages/db/src/query-plan-test-guard.ts`: the caller is read when
the statement runs, and only when its plan has a finding, in every mode
(one stack depth, 40; local runs used 10 before). This keeps attribution
identical locally, in CI, and in the production auditor. With
`BB_QUERY_PLAN_EXECUTED`/`BB_QUERY_PLAN_REPORT`, the executed query
functions are still recorded when a statement is prepared, as before. A
cached statement is prepared inside the function that first runs it, so
that function is still credited.
- `packages/db/src/query-plan-guard.ts`: `QueryPlanAuditor.observe`
(eager caller) is removed; nothing calls it anymore.
- `packages/db/src/query-plan-allowlist.ts`: the entry is renamed back
to `queued-thread-messages.ts#isThreadQueueAutoSendPaused`. No new
entry.
- `packages/db/test/query-plan-test-guard.test.ts`: the subprocess test
runs a cached query (`isThreadQueueAutoSendPaused`) under the local,
executed-functions, and report modes. It asserts no violations in the
first two, that every report finding names
`#isThreadQueueAutoSendPaused` and is allowed, and that the executed
output lists the running functions.

This doesn't change the CLI, the wire protocol, or the docs. This brings
the guard in line with what the docs already describe.

### Cost in CI mode

I measured stack reads with an instrumented guard over the full suites
with `BB_QUERY_PLAN_EXECUTED` set. CPU is summed across workers.

| | `@bb/server` stack reads | CPU | `@bb/db` stack reads | CPU |
|---|---|---|---|---|
| main (prepare-time caller) | 952k | 56 s | 245k | 8.5 s |
| first commit (stack read on every run) | 2.47M | 115 s | 428k | 21.7 s
|
| this PR (prepare-time recording + lazy run-time caller) | 1.03M (956k
prepare + 76k caller) | 48.5 s | 246k | 7.5 s |

Reading the stack on every run credited no additional query function; it
only dropped 46 private `prepare*` builder names. So the second commit
goes back to prepare-time recording.

## How you verified

- Without the fix, the new test fails: local mode reports three
violations on `#isThreadQueueAutoSendPaused`. With the fix, it passes.
- `pnpm exec turbo run test typecheck --filter=@bb/db
--filter=@bb/server` with no query-plan env vars: passes.
- `BB_QUERY_PLAN_EXECUTED=.query-plan/executed.jsonl pnpm exec turbo run
test --filter=@bb/db --filter=@bb/server`: passes, and `executed.jsonl`
is written for both packages.
- Query Plan Audit path: `BB_QUERY_PLAN_REPORT=<file>` over the same
suites passes, then `pnpm --filter @bb/db check:query-plan-report
<file>` prints "Query plan report matches the allowlist (855 findings)
and runs all 554 query functions."
- Earlier runs on the first commit saw
`apps/server/test/app/install-machine-script.test.ts` time out while
host load average was 43–77. That file doesn't touch the database,
passes 56/56 alone, and passed in all runs on this commit.
- oxlint and prettier pass on the changed files.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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