Skip to content

Prevent automatic queued messages from overtaking a pending send - #6247

Merged
ymichael merged 1 commit into
mainfrom
bb/queued-dispatch-claims-20261024
Oct 25, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/queued-dispatch-claims-20261024

Conversation

@ymichael

@ymichael ymichael commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

CI 40137789809 failed while dispatching an older queued message: Queued message claim expired before it could be sent. The claim had not expired. While that message prepared its command asynchronously, an automatic drain skipped its claimed row, claimed a later message, and activated the thread first. The older send then failed its lifecycle transition. This can reorder queued messages and produce a spurious send conflict in production.

What changed

Production change: inside both immediate automatic-claim transactions, reuse the existing per-thread claimed-message query to decline another automatic claim until the current dispatch finishes or releases its claim. Explicit send-now policy is unchanged. No schema, wire, SDK, CLI, timeout, CI selection, cache, or shard changes.

Extend the existing service-tier regression to hold real command preparation, run a competing next-group or targeted automatic claim, and then release preparation. It retains the queue snapshots and execution-tier assertions and checks that exactly one command was dispatched. The edit-hold database fixture now consumes its first claimed dispatch before checking the second row's expired hold; its existing assertions remain intact.

How you verified

  • Both controlled competing-claim cases fail against the original production code at queued-messages.ts:605, matching CI, and pass with the fix.
  • All 40 thread-send-dispatch tests and all 633 database tests pass through Turbo.
  • Server and database typechecks pass; neither package has a lint task. Formatting and git diff --check pass. Reviewed against docs/CODE_REVIEW.md and deslop guidance.
  • Full server run: 3,659 passing tests; 22 failures in npm cache writes denied by the local sandbox and filesystem watcher tests. All seven affected files pass with normal host permissions (172 tests), including both watcher cases and the installer process-identity case. The install-script failure was an inability to inspect its owned process inside the sandbox.
  • Final-head CI 40138842962 passes on 0e449d8b5a, including both Linux server shards, all three Windows server shards, Linux/Windows integration, foundation packages, lint/typecheck, and checks.

AGENT GENERATED

ymichael added a commit that referenced this pull request Oct 24, 2026
## Human comments

## What was wrong

Every integration harness reran database migrations and rebuilt the same
scripted-echo provider artifact. The Linux integration job also used
only two Vitest workers on its four-vCPU runner. Together these repeated
setup costs and limited concurrency lengthened a job that was among the
remaining CI bottlenecks.

## What changed

- Build one SQLite template with the real migrations, copy a fresh
database for every harness, and initialize its personal project
separately. Global setup returns the template cleanup callback.
- Reuse the immutable scripted-echo build promise per worker. Each
harness still creates a fresh registry, server, daemon, provider
processes, and sessions.
- Use four workers in the existing Linux integration job. Keep the
Windows worker count unchanged because the measured four-worker variant
was slower.

These are test-support and CI changes. No production code, assertions,
test selection, cache invalidation rules, shard counts, or timeouts
change.

## How you verified

[Controlled
benchmark](https://github.com/get-bb/bb/actions/runs/40137507494):
baseline, fixture reuse, and fixture reuse with four workers; two
replicas on each four-vCPU Linux/Windows runner type, Node 22, empty
Turbo caches. All 12 jobs passed the same 77 tests across 28 files.

| Integration test task | Baseline, two runs | Fixture reuse | Fixture
reuse + four workers |
| --- | --- | --- | --- |
| Linux | 46.53s / 39.16s | 34.40s / 33.11s | 27.13s / 24.77s |
| Windows | 65.66s / 42.54s | 42.92s / 41.64s | 54.23s / 68.06s |

Linux mean test-task time falls from 42.8s to 26.0s, about 39%. The
complete Linux command, including upstream tasks, falls from 40.5–48.1s
to 26.0–28.4s. Windows timings are noisier, so these runs do not
establish a precise percentage gain; its current worker count is
retained. These are integration-job measurements, not whole-pipeline
percentages.

Local full integration runs with three workers passed 77/77 in 39.39s
baseline, 36.36s with database reuse, and 34.28s with database plus
provider fixture reuse. Turbo lint/typecheck, formatting, diff checks,
and actionlint passed (excluding existing custom Blacksmith runner
labels). Real-provider suite setup/discovery passed; all 11
live-provider cases were deliberately filtered out, so no live-provider
execution is claimed.

Independent review found no issues: the database helper preserves
connection defaults and real schema, existing database tests cover
template-copy isolation, direct server initDb coverage remains, and no
tests/assertions were removed. The first final-head CI attempt passed
every check except Linux server shard 2, which hit
`queued_message_claim_lost` in the unchanged `keeps queued choices
separate until dispatch and preserves the next row` test. Its server
worker count and executed provider-registration path are unchanged. All
39 cases passed against the baseline helper in 11 focused runs; the
separate CI reliability thread then reproduced the production race
deterministically on the baseline by pausing the older send while a
competing drain claims the newer row. That fix is tracked in
[#6247](#6247). Only the failed shard
was rerun on the unchanged head. Final-head [CI is
green](https://github.com/get-bb/bb/actions/runs/40137789809/attempts/2)
on `46241a7b06c7572dee5530707e0243e804a9918d`; the passing retry does
not fix the queue race. The benchmark workflow lives on a separate
branch and is excluded from this PR.

> AGENT GENERATED
@ymichael
ymichael merged commit 367ca93 into main Oct 25, 2026
34 checks passed
@ymichael
ymichael deleted the bb/queued-dispatch-claims-20261024 branch October 25, 2026 02:53
mistakeknot added a commit to mistakeknot/aleph that referenced this pull request Oct 27, 2026
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