Skip to content

perf: prune event history only for changed threads - #5124

Merged
ymichael merged 1 commit into
mainfrom
perf/idle-event-pruning
Oct 7, 2026
Merged

ymichael merged 1 commit into
mainfrom
perf/idle-event-pruning

Conversation

@ymichael

@ymichael ymichael commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Idle event-history pruning repeatedly visited unchanged threads to find obsolete events. On the settled audit database, a pass used 39.289 ms of CPU even with no new work.

What changed

Track changed threads in a small thread_pruning_work queue and consume that queue during pruning. Event ingestion coalesces one pending marker per affected thread in the same transaction. Event-data rewrites and archive/unarchive mark the relevant policy work in application code. Cursor revisions preserve changes during an in-progress scan.

One generated migration creates and bootstraps the queue and adds cursor revision tracking. No SQL triggers or events-table indexes are added. This PR contains only event pruning, independent of the other idle-sweep changes.

Measured idle CPU fell to 0.217 ms/pass (99.4% lower). The tradeoff is about 11–12 µs extra CPU per single-event ingestion transaction: roughly 10% more streaming ingestion CPU at 10/25/50 threads, and 5.4% more CPU for raw inserts batched 32 at a time. Harnesses and results are kept outside the repository.

How you verified

  • Full DB suite: pnpm exec turbo run test --filter=@bb/db.
  • Focused server tests: pnpm exec turbo run test --filter=@bb/server -- test/system/thread-pruning-sweep.test.ts test/services/periodic-sweeps.test.ts.
  • pnpm exec turbo run typecheck --filter=@bb/db --filter=@bb/server.
  • Regression coverage includes ingestion transaction rollback, archive/unarchive ownership, rewritten-output markers, queue fairness, and truncation.
  • Fresh isolated pnpm start:worktree server: a real Codex turn through the BB CLI, history reload, UI and CLI archive/unarchive. Production storage insertion of 100 synthetic rate-limit snapshots queued work; the running server retained the newest snapshot, removed 99 obsolete rows, drained the queue, and preserved the assistant answer.
  • Before rebase, three alternating paired streaming runs and five paired raw-insert runs compared the change with its main base; 100 idle pruning passes per side used the same settled history.

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