Skip to content

Cache prepared statements for thread lifecycle state - #8368

Merged
ymichael merged 2 commits into
mainfrom
slopcop/issue-8364
Nov 30, 2026
Merged

ymichael merged 2 commits into
mainfrom
slopcop/issue-8364

Conversation

@bb-slop-cop

@bb-slop-cop bb-slop-cop Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · new-issue-autopilot

Human comments

What was wrong

Fixed lifecycle-state reads and cleanup queries rebuilt their SQL and prepared fresh SQLite statements on every invocation. A warm-path regression against real migrated databases records 48 SQL compilations and 48 SQLite preparations across four cycles, including separate transactions and two thread IDs. This failure repeats in a second clean trusted-main checkout.

Report: https://get-bb.github.io/reports/issues/8364.html

What changed

  • Cache the two run-state reads, thread-start existence check, and two cleanup DELETE statements per connection with bound identifiers.
  • Reuse the existing cached getThread lookup in the lifecycle writer.
  • Add readThreadRunState and hasThreadStartTurnRequest to the existing warm-lookup table in prepared-queries.test.ts; both rows fail on main and pass here.
  • Keep inserts, status updates, recovery scans, public APIs, schemas, stored data, and protocols unchanged.

Changed-line count from git diff --numstat origin/main: 201 total text lines — 166 additions and 35 deletions across two files, entirely in packages/db. No binary changes or new dependencies.

How you verified

  • Before the production fix: the authored regression fails on trusted main in both clean checkouts with { compilations: 48, preparations: 48 }, expected zero.
  • After the fix: focused regression, prepared-query, thread-lifecycle, and thread tests pass — 54 tests in 4 files.
  • pnpm exec turbo run test typecheck lint --filter=@bb/db passes — 763 database tests in 73 files, with typecheck.
  • pnpm exec turbo run lint:type-aware --filter=@bb/db passes with zero warnings and errors.
  • pnpm exec turbo run build passes — 74 successful tasks, after building missing local Plugin SDK artifacts with pnpm exec turbo run build build:types --filter=@get-bb/plugin-sdk.
  • git diff --check origin/main passes; numstat confirms two text files and the 201-line limit.

The original report's precise timing multiplier is not claimed by this fix; the deterministic repeated-compilation failure is reproduced and repaired.

Fixes #8364

AGENT GENERATED

@ymichael
ymichael merged commit 0872334 into main Nov 30, 2026
36 checks passed
@ymichael
ymichael deleted the slopcop/issue-8364 branch November 30, 2026 20:51
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
🚨 **SLOP COP** 🚨 · `new-issue-autopilot`

## Human comments

## What was wrong

Fixed lifecycle-state reads and cleanup queries rebuilt their SQL and
prepared fresh SQLite statements on every invocation. A warm-path
regression against real migrated databases records 48 SQL compilations
and 48 SQLite preparations across four cycles, including separate
transactions and two thread IDs. This failure repeats in a second clean
trusted-main checkout.

Report: https://get-bb.github.io/reports/issues/8364.html

## What changed

- Cache the two run-state reads, thread-start existence check, and two
cleanup DELETE statements per connection with bound identifiers.
- Reuse the existing cached `getThread` lookup in the lifecycle writer.
- Add `readThreadRunState` and `hasThreadStartTurnRequest` to the
existing warm-lookup table in `prepared-queries.test.ts`; both rows fail
on main and pass here.
- Keep inserts, status updates, recovery scans, public APIs, schemas,
stored data, and protocols unchanged.

Changed-line count from `git diff --numstat origin/main`: **201 total
text lines** — 166 additions and 35 deletions across two files, entirely
in `packages/db`. No binary changes or new dependencies.

## How you verified

- Before the production fix: the authored regression fails on trusted
main in both clean checkouts with `{ compilations: 48, preparations: 48
}`, expected zero.
- After the fix: focused regression, prepared-query, thread-lifecycle,
and thread tests pass — **54 tests in 4 files**.
- `pnpm exec turbo run test typecheck lint --filter=@bb/db` passes —
**763 database tests in 73 files**, with typecheck.
- `pnpm exec turbo run lint:type-aware --filter=@bb/db` passes with zero
warnings and errors.
- `pnpm exec turbo run build` passes — **74 successful tasks**, after
building missing local Plugin SDK artifacts with `pnpm exec turbo run
build build:types --filter=@get-bb/plugin-sdk`.
- `git diff --check origin/main` passes; numstat confirms two text files
and the 201-line limit.

The original report's precise timing multiplier is not claimed by this
fix; the deterministic repeated-compilation failure is reproduced and
repaired.

Fixes get-bb#8364

> AGENT GENERATED

<!-- slopcop:rule=new-issue-autopilot run=run_si418rigmx27hldk
sha=issue-8364 kind=summary -->

---------

Co-authored-by: SawyerHood <[email protected]>
Co-authored-by: Michael Yong <[email protected]>
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.

db: readThreadRunState and thread-run-state queries block the main event loop 18× longer by recompiling SQL on every thread lifecycle transition

2 participants