Skip to content

Queue a send whose thread changes twice under it instead of failing with 500 - #6956

Merged
ymichael merged 1 commit into
mainfrom
bb/queue-send-thread-changed-twice
Nov 3, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/queue-send-thread-changed-twice

Conversation

@ymichael

@ymichael ymichael commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A send re-decides once when the thread's status changes under its dispatch attempt (runDispatchAttempt → reattemptDispatchForThreadChange in apps/server/src/services/threads/dispatch-attempt.ts). A thread that is draining its queue through short turns can change again during that re-decision: each queued turn starts and ends within a second or two, and the attempt takes longer than that while the message.dispatch hook evaluation lock is contended. The second change threw 500 internal_error: Thread <id> changed twice under one dispatch attempt. The message was neither sent nor queued. When a drain hit the same path, the throw recorded a failure on the queued row.

Related to #1706, but it is a different bug: #1706's shapes (a row accepted as queued, then gone) did not reproduce on main, and this one returns an error rather than accepting the message silently.

What changed

  • reattemptDispatchForThreadChange no longer throws on a second change. It checks the thread is still writable, records the message as a thread-busy queued row (or re-queues the rows a drain had claimed), and requests a thread-ready drain. An idle thread sends it straight away; a busy one sends it when its turn ends. The caller gets delivery: "queued". An info log line records each time this happens.
  • No wire, SDK, CLI or doc changes.

What users see differently

A send to a thread that is busy draining its queue no longer fails now and then with internal_error: Thread … changed twice under one dispatch attempt. This applies to bb thread tell, the app and plugin sends. It comes back as queued and is delivered when the thread is free. Drained rows that hit this race are no longer marked failed.

How you verified

Live (the real evidence). pnpm start:worktree with real agents: 4 Codex and 2 Claude Code threads. A script sent 432 messages over HTTP in queue-if-active, steer-if-active and auto modes against busy and idle threads. It then checked that each message was either in a client/turn/requested event or still queued. One run froze the host daemon at random with kill -STOP/-CONT on its literal PID (8 pauses of 1–20 s).

Build Sends Refused with 500 "changed twice" Lost
main (3 runs: queue only, mixed modes, daemon freezes) 432 10 (3 / 0 / 7, Codex and Claude Code) the same 10: not sent, not queued
this change (same daemon-freeze script and seed) 144 0 0

On the fix build the fallback fired 11 times, and all 11 messages were delivered.

Regression test. apps/server/test/threads/dispatch-hooks.test.ts, "queues a send whose thread changes twice under it, then drains it". It flips the thread's status from inside a message.dispatch hook, idle→active on the first pass and active→idle on the second, to force the race deterministically. That is a simulation of the timing; the live runs above are the evidence that the race happens. The test fails on main with the 500 and passes here, including the follow-up drain sending the message as a turn.submit.

Checks. pnpm exec turbo run typecheck lint --filter='...[origin/main]' passes. dispatch-hooks.test.ts passes (54 tests). The full @bb/server suite run locally had one timeout in timeline-in-turn-window.test.ts. That file also fails without this change on a heavily loaded machine.

AGENT GENERATED

…ith 500

A send re-decides once when the thread's status changes under its dispatch
attempt. A thread that is draining its queue through short turns can change
again during that re-decision (idle -> active -> idle within a second or two,
longer while the plugin hook lock is contended). The second change threw
`500 internal_error: Thread <id> changed twice under one dispatch attempt`,
and the message was neither sent nor queued. On a drain the same throw
recorded a failure on the queued row.

The second change now records the message as a `thread-busy` queued row
(or re-queues the drain's claimed rows) and requests a thread-ready drain,
so an idle thread still sends it and a busy one sends it when its turn ends.
The caller gets `delivery: "queued"`.

Live on main (pnpm start:worktree, 4 Codex + 2 Claude Code threads, 432
scripted sends): 10 sends refused with this 500, none of them delivered.
Same script on this change: 0 errors, 0 lost; the fallback fired 11 times
and every one of those messages was delivered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit d083003 into main Nov 3, 2026
30 checks passed
@ymichael
ymichael deleted the bb/queue-send-thread-changed-twice branch November 3, 2026 22:55
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