Skip to content

Stop pruning tests depending on the 20 ms advance budget - #7298

Merged
ymichael merged 1 commit into
mainfrom
bb/freeze-pruning-budget-in-tests
Nov 9, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/freeze-pruning-budget-in-tests

Conversation

@ymichael

@ymichael ymichael commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

#7254 capped each pruning advance at a 20 ms wall-clock budget (THREAD_PRUNING_ADVANCE_WORK_BUDGET_MS). Several tests still assert how much a single advance removes, and they ran against the real clock. When a runner is slow, the budget runs out after fewer 16-row chunks and the counts come up short. The Windows host packages job has failed on main this way since bf09c7c5af:

  • thread-pruning.test.ts › "preserves the current latest row when resuming after truncation": expected 304 to be 500 (bf09c7c5af), 496 (60f81b3476), 256 (11175a0d52), and 272, then 128 on Seek item events directly when carrying settlements across a message edit #7287's run and its re-run.
  • query-plans.test.ts › "does not parse agentMessage/reasoning completion payloads for retention support" (60f81b3476).

What changed

These tests now freeze performance.now, so the budget never expires and each advance does its full batch:

  • packages/db/test/data/thread-pruning.test.ts: the whole describe block.
  • packages/db/test/query-plans.test.ts: the three tests that advance resolved-item pruning.
  • apps/server/test/system/thread-pruning-sweep.test.ts: the whole describe block. The two elapsed-budget tests still install their own clocks over it.
  • apps/server/test/services/threads/timeline-selection-memo.test.ts: only inside advanceSettledThreadPruning.
  • apps/server/test/system/event-pruning.test.ts and thread-pruning-invalidation.test.ts: only in the one test in each that counts live-advance removals.

Spies are restored in afterEach (or finally). resolved-item-pruning-resume.test.ts still covers the budget itself with its own fake clock. No production code changes.

How you verified

  • Reproduced the failure locally. I set THREAD_PRUNING_ADVANCE_WORK_BUDGET_MS to 1 ms to stand in for a slow runner. On main this fails the CI test exactly (expected 16 to be 500), and 13 tests fail in total:
    • db: 3 in thread-pruning.test.ts and 3 in query-plans.test.ts.
    • server: 4 in thread-pruning-sweep.test.ts, 1 in timeline-selection-memo.test.ts, 1 in event-pruning.test.ts and 1 in thread-pruning-invalidation.test.ts.
  • At 1 ms with this change: all of these pass. Across the db and server suites, the only remaining 1 ms failure is resolved-item-pruning-resume.test.ts › "stops an advance at its time budget…", which is designed around the real 20 ms value and uses its own fake clock.
  • At the real 20 ms budget: @bb/db 700/700 passed (turbo run test --filter=@bb/db), and the five server pruning test files 56/56 passed.
  • turbo run typecheck --filter=@bb/db --filter=@bb/server passed.

AGENT GENERATED

🤖 Generated with Claude Code

#7254 bounded each pruning advance to a 20 ms wall-clock budget. Tests
that assert how much one advance removes still ran against the real
clock, so on slow runners the budget ran out first and they removed
fewer rows than expected. The Windows host packages job fails on main
this way. Freeze performance.now in those tests so the budget never
expires; resolved-item-pruning-resume.test.ts keeps covering the budget
with its own fake clock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 480542c into main Nov 9, 2026
33 checks passed
@ymichael
ymichael deleted the bb/freeze-pruning-budget-in-tests branch November 9, 2026 21:44
ymichael added a commit that referenced this pull request Nov 9, 2026
…and diagnostics servers (#7302)

## 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. #7298 since removed the pruning tests' dependence on the 20
ms budget.

> 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