Repository navigation
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
packages/db/src/query-plan-test-guard.tsattributed a query to its caller at two different moments. WithBB_QUERY_PLAN_EXECUTEDorBB_QUERY_PLAN_REPORTset (CI setsBB_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. AprepareCachedQuerystatement is prepared inside its builder and run from the outer function, so the modes disagree. #8367 (c0394a5) cachedisThreadQueueAutoSendPausedand renamed its allowlist entry toprepareIsThreadQueueAutoSendPaused, the name only CI produces. Since then, a plain localpnpm exec turbo run test --filter=@bb/dbfails at file teardown inqueued-thread-messages.test.ts/queued-message-state.test.tswith acorrelated-subquery on events in ...#isThreadQueueAutoSendPausedviolation, and about 37@bb/servertest files fail the same way.The production auditor (
observeFirstExecution, enabled byBB_QUERY_PLAN_AUDIT) also reads the stack at run time, anddocs/debugging-and-qa.mddefinescalleras 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. WithBB_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 toqueued-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#isThreadQueueAutoSendPausedand 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_EXECUTEDset. CPU is summed across workers.@bb/serverstack reads@bb/dbstack readsReading 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
#isThreadQueueAutoSendPaused. With the fix, it passes.pnpm exec turbo run test typecheck --filter=@bb/db --filter=@bb/serverwith 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, andexecuted.jsonlis written for both packages.BB_QUERY_PLAN_REPORT=<file>over the same suites passes, thenpnpm --filter @bb/db check:query-plan-report <file>prints "Query plan report matches the allowlist (855 findings) and runs all 554 query functions."apps/server/test/app/install-machine-script.test.tstime 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.🤖 Generated with Claude Code