Repository navigation
Queue a send whose thread changes twice under it instead of failing with 500 - #6956
Merged
Merged
Conversation
…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]>
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
A send re-decides once when the thread's status changes under its dispatch attempt (
runDispatchAttempt→reattemptDispatchForThreadChangeinapps/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 themessage.dispatchhook evaluation lock is contended. The second change threw500 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
reattemptDispatchForThreadChangeno longer throws on a second change. It checks the thread is still writable, records the message as athread-busyqueued row (or re-queues the rows a drain had claimed), and requests athread-readydrain. An idle thread sends it straight away; a busy one sends it when its turn ends. The caller getsdelivery: "queued". An info log line records each time this happens.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 tobb 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:worktreewith real agents: 4 Codex and 2 Claude Code threads. A script sent 432 messages over HTTP inqueue-if-active,steer-if-activeandautomodes against busy and idle threads. It then checked that each message was either in aclient/turn/requestedevent or still queued. One run froze the host daemon at random withkill -STOP/-CONTon its literal PID (8 pauses of 1–20 s).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 amessage.dispatchhook, 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 aturn.submit.Checks.
pnpm exec turbo run typecheck lint --filter='...[origin/main]'passes.dispatch-hooks.test.tspasses (54 tests). The full@bb/serversuite run locally had one timeout intimeline-in-turn-window.test.ts. That file also fails without this change on a heavily loaded machine.