Skip to content

fix: make queued-message dispatch atomic and composable - #2884

Merged
ymichael merged 9 commits into
mainfrom
bb/investigate-queued-message-bugs-thr_uawcey4qag
Sep 2, 2026
Merged

ymichael merged 9 commits into
mainfrom
bb/investigate-queued-message-bugs-thr_uawcey4qag

Conversation

@ymichael

@ymichael ymichael commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Queued-message readiness was decided independently by startup events, idle sweeps, plugin/timer drains, failed-row handling, and manual Stop handling. Each fix could pass alone while their predicates disagreed when composed: a settled turn-starting row could be discovered but rejected, a failed grouped sibling could ride with a healthy row, and a stale runtime event or in-flight hook could cross a manual Stop. Startup admission also trusted a stale active thread after it changed to error, which could strand a row or change steer semantics.

What changed

  • Made startup admission atomic: it either queues against the still-active startup or retries once from the current thread state.
  • Centralized automatic whole-group readiness across plugin, time, thread-busy, and turn-starting waits.
  • Replaced the positional sendNow/optional-callback claim API with an explicit automatic versus explicit-send policy.
  • Enforced failed-row exclusion and manual-Stop pause inside the group claim transaction. Explicit Send now remains the override; independent scheduled, plugin-held, and system-notice rows remain drainable.
  • Required a post-Stop client request before a later root turn can clear the pause, and rechecked the pause at final dispatch consumption.
  • Used one SQL IN predicate for idle-drainable wait kinds.

This supersedes #2866 and #2867 and includes the failed-group and Stop-boundary follow-ups. There is no host-daemon wire, schema, CLI, SDK, migration, or documentation contract change.

How you verified

  • Added deterministic regressions for the settled-startup lost wake, mixed-wait groups, failed grouped siblings, stale post-Stop starts, hook-held Stop races, and active-to-error startup admission for both steer modes.
  • Focused Turbo server matrix: 70/70 passed.
  • Focused Turbo DB/query-plan matrix: 68/68 passed.
  • Standalone production-path Stop race harness: both races passed.
  • Full DB suite: 444/444 passed.
  • Full server suite: 2,131 passed; 10 unrelated install-machine-script tests failed because their fake host daemon did not connect.
  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/db passed.
  • pnpm exec turbo run build --filter=@bb/server passed.
  • git diff --check passed.

AGENT GENERATED

Automatic grouped claims required every member to match the same sweep trigger, so groups mixing plugin and timer waits could never become eligible. Compose per-member plugin or due-time eligibility while retaining the immediate whole-group claim.
Automatic turn-start sends omitted an eligibility callback, so callback presence let a clean group member pull a failed sibling into a claim. Require explicit-send intent at the shared claim boundary so only Send now can admit failures.
@ymichael
ymichael merged commit 434b53a into main Sep 2, 2026
15 checks passed
@ymichael
ymichael deleted the bb/investigate-queued-message-bugs-thr_uawcey4qag branch September 2, 2026 00:21
ymichael added a commit that referenced this pull request Sep 2, 2026
## Human comments

## What was wrong

#2884 centralized atomic queue admission, whole-group eligibility,
failed-row exclusion, and manual-Stop policy, but automatic dispatch
orchestration was still split across lifecycle events, timer and plugin
walkers, wait-release handlers, and recovery. Those callers
independently chose candidate rows, pacing, coalescing, and failure
behavior. In particular, the `turn-started` path captured queued row IDs
and only logged terminal failures, while other drains persisted them,
and plugin recheck coalescing used process-global state rather than
server-instance state.

The manual-Stop pause also classified ordinary queued rows as only
unwaited or `thread-busy`. A user message parked while a turn was
starting uses `turn-starting`, so an automatic drain could claim that
older row after Stop and unexpectedly restart the thread.

The initial coordinator migration also grouped idle, scheduled, and
orphan recovery under one periodic job guard and expanded plugin removal
into one deferred task per affected thread. A slow idle pass could
therefore suppress due-schedule recovery on overlapping ticks, while
removing a plugin could create an unbounded burst of deferred queue
work.

## What changed

- Replaced the old `queue-drains` module and automatic-send entry points
with one queued-message dispatch coordinator whose interface accepts
typed wake-up facts.
- Migrated thread lifecycle, turn events, workspace readiness,
provisioning settlement, interaction settlement, host reconnect, plugin
recheck/removal, message creation, and periodic recovery to that
interface.
- Moved candidate discovery, wait release, per-thread dispatch
coalescing, requeue pacing, automatic eligibility, claims, timeout
treatment, persistent failure handling, and orphaned-plugin recovery
behind the coordinator seam.
- Kept lifecycle wake-ups immediate even after a recent requeue while
timer and plugin sweeps retain their anti-spin cooldown.
- Kept periodic idle, due-scheduled, and orphaned-plugin recovery as
independently guarded typed wakes, so an overlapping tick can deliver
due work while an earlier idle pass is still running.
- Kept plugin-unregister release and dispatch as one sequential
coordinator batch instead of one deferred task per affected thread.
- Scoped plugin-recheck request coalescing to each server instance.
- Kept explicit “Send now” separate behind an explicit-send-only
interface.
- Classified `turn-starting` user rows as ordinary for manual-Stop pause
policy, while keeping explicit Send now as the deliberate override.

There is no host-daemon wire, database schema, CLI, SDK, migration, or
documentation contract change.

## How you verified

- Added regressions for terminal failure persistence on `turn-started`,
lifecycle wake-ups during the requeue cooldown, per-server request
coalescing, and orphaned plugin waits.
- Added a public HTTP/session-event regression covering steer during
startup, manual Stop, a late `turn/started`, a later queued message,
automatic `thread-ready` dispatch, and explicit Send now. It failed
before the predicate fix and passed after it.
- Added an overlapping-periodic-tick regression that blocks idle
recovery and proves due scheduled work gets the next dispatch
opportunity. It failed before independent guards and passed after.
- Added a plugin-unregister regression covering multiple affected
threads. It failed with two deferred tasks before the batch fix and
passed with one afterward while draining both rows.
- Final focused Turbo queue/lifecycle matrix: 69/69 server tests passed.
- `@bb/server` Turbo typecheck and build passed.
- Full server suite before the review follow-up: 2,134 passed. The ten
unrelated `install-machine-script` cases failed both in the full run and
alone because this host has `/tmp/package.json` with `"type": "module"`,
which makes their extensionless CommonJS fixture fail with
`ReferenceError: require is not defined`.
- Changed-file formatting and `git diff --check` passed.

Follow-up to #2884.

> 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