Skip to content

fix: stop drain after schema contention#519

Merged
EtanHey merged 1 commit into
mainfrom
fix/brain-store-busy-defer-drain-followup
Jun 19, 2026
Merged

fix: stop drain after schema contention#519
EtanHey merged 1 commit into
mainfrom
fix/brain-store-busy-defer-drain-followup

Conversation

@EtanHey

@EtanHey EtanHey commented Jun 19, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up to #516, which Etan squash-merged while the final review-response commit was still running local pre-push.

This PR carries only that final small fix:

  • stop drain_once after forced schema bootstrap contention so later queue files are not applied or unlinked while an earlier store file remains queued
  • count burn_drain_once applied/skipped events only after the transaction commits, so missing-chunks retry does not double-count rolled-back work
  • add regressions that preserve queued payload contents and verify burn-drain result counters

Verification

Red/green before the fix:

  • test_drain_stops_after_forced_schema_bootstrap_writer_in_use failed because drain_once advanced to the second queue file after forced schema bootstrap contention.
  • test_burn_drain_missing_chunks_retry_counts_events_once failed with applied_events == 3 before counters were moved behind COMMIT.

Current local verification:

  • pytest tests/test_write_queue.py::TestFlushPendingStores::test_drain_stops_after_forced_schema_bootstrap_writer_in_use tests/test_write_queue.py::TestFlushPendingStores::test_burn_drain_missing_chunks_retry_counts_events_once -q -> 2 passed
  • ruff check src/brainlayer/drain.py tests/test_write_queue.py -> passed
  • ruff format --check src/brainlayer/drain.py tests/test_write_queue.py -> 2 files already formatted
  • pre-push: 3012 passed, 9 skipped, 61 deselected, 1 xfailed, 103 warnings, MCP registration 3 passed, isolated eval/hook routing 40 passed, Bun 1 pass, FTS determinism passed

@codex review
@cursor @BugBot re-review
@coderabbitai review


Note

Medium Risk
Changes ordering and metrics in the durable JSONL drain path; mistakes could skip or reorder queued writes, but scope is small and covered by new tests.

Overview
Fixes two queue-drain edge cases in drain_once and burn_drain_once.

drain_once: When a store file hits a missing-chunks error and forced schema bootstrap fails (e.g. another writer holds the DB), the loop now sets stop_draining and exits instead of moving on to the next JSONL file. That keeps FIFO order: later files are not drained or unlinked while an earlier store file is still blocked on schema init.

burn_drain_once: applied_events and skipped_verified_stale are accumulated only after COMMIT, using per-attempt counters. Rolled-back work from a failed first attempt (e.g. missing chunks then successful retry) no longer inflates reported counts.

Regressions cover stopping after contended forced bootstrap (both queue files unchanged, schema helper called once) and burn-drain counting exactly two committed events after a missing-chunks retry.

Reviewed by Cursor Bugbot for commit 245a744. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Fix drain to stop processing queue files after schema contention

  • When forced schema bootstrap fails (e.g. WriterInUseError) in the missing-chunks retry path of drain_once, processing now stops immediately rather than continuing to later queue files, preserving queue order.
  • In burn_drain_once, event counts (applied_events, skipped_verified_stale) are now accumulated into per-attempt local counters and only added to the result after a successful COMMIT, preventing double-counting on retries.

Macroscope summarized 245a744.

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@EtanHey, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 26 minutes and 31 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1958ba8c-43b1-4ca0-b687-346944702db8

📥 Commits

Reviewing files that changed from the base of the PR and between 6a89a92 and 245a744.

📒 Files selected for processing (2)
  • src/brainlayer/drain.py
  • tests/test_write_queue.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/brain-store-busy-defer-drain-followup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 245a744527

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@EtanHey EtanHey merged commit 539a7bc into main Jun 19, 2026
7 checks passed
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