Repository navigation
Route edit, compaction and /clear through the dispatcher and keep queue intent - #6493
Merged
Merged
Conversation
…ue intent Edit-resend, compaction and /clear called sendThreadMessage directly, and the queue drain had a private fast path to the daemon whenever no message.dispatch hook was installed. They now go through attemptDispatch: edit, compaction and /clear as an "operation" source (no plugin pass, refuse instead of queueing, as before); the fast path is deleted. - A queued /clear row is consumed in the same transaction as the context-clear event, so stale-claim recovery cannot run it twice. - Each queued row records delivery_mode (steer/queue). The drain re-sends queue rows as queue-if-active and re-checks main-run activity after the dispatch hook pass, so `--mode queue --send-at` and retries wait for a running turn instead of steering into it (#4607, in part). - Grouping a steering row with a waiting one is refused. - The first message of a pending thread keeps the dispatcher's resolved author and sender-formatted input, and the provision context stores the initiator for the cold start's permission escalation (#5010). Parent/child system notices keep their own delivery path. Migration 0148_queued_message_delivery_mode adds the column (default 'steer'). No public API, plugin SDK or daemon wire change. Fixes #5010 Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/swing-1-one-inbox-per-thread-thr_ix35ge3jbm
branch
from
October 29, 2026 21:30
ada5782 to
7b41cb2
Compare
Grouping a steering queued message with one that waits for the turn was refused with a 409. A group is sent one way, so it now waits for the running turn whenever any of its messages asked to wait; no message is pushed into a turn its sender wanted to stay out of. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 2, 2026
## Human comments ## What was wrong Parent/child system notices (a child finished, needs attention, changed owner, a detached tool result, an environment-directory continuation) had their own delivery path in `parent-system-messages.ts` ([`queueParentSystemMessage`](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/parent-system-messages.ts#L453-L533) → [`deliverParentSystemMessage` / `attemptParentSystemMessageDelivery`](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/parent-system-messages.ts#L548-L624)). It was separate from the dispatcher (`attemptDispatch`) that every other message has used since #6493, kept its own copies of core's waits, and skipped the `message.dispatch` plugin hooks. Child turn-end notices were also batched in an [in-memory `Map`](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/child-thread-notifications.ts#L92-L95) flushed by a [2 s `setTimeout`](https://github.com/get-bb/bb/blob/a0e513398f02d47ec5df874b89fec4edfc248894/apps/server/src/services/threads/child-thread-notifications.ts#L422-L429). A server restart or a failed delivery inside that window lost them. ## What changed | Path | Before | After | |---|---|---| | Parent/system notices | own executor (`deliverParentSystemMessage`) with its own waits and no plugin hooks; queued only when a question, compaction, `/clear` or an offline host blocked it | saved to the parent's queue first as a `deliveryMode: "steer"` row, then attempted through the dispatcher as an `initiator: "system"` turn; execution options resolved from the parent at delivery | | Drained notice rows | `sendClaimedSystemNotice` → `deliverParentSystemMessage` | deleted; an ordinary drained row with a typed `systemNotice` | | Child turn-end notices | in-memory `Map` + `setTimeout`, lost on restart or failure | one coalesced queued row per parent (`coalesce_key = child-outcomes`); `sendAt` moves 2 s later as each child finishes; a timer wakes the drain (and sets itself again if it fires before the wall clock reaches `sendAt`, which happens on Windows), and the existing due sweep is the backstop after a restart | | Parent with no environment yet | delivery threw (`requireThreadEnvironment`) and the notice was dropped | the notice waits for provisioning | | Notice whose first attempt never ran on a busy parent | a notice row on a busy parent had no recovery path | the idle-thread recovery sweep also retries ready notices on busy threads | A notice to a parent that is mid-turn still steers into that turn, the same as on main: - an active parent: `join-turn` attempt, steered with `expectedTurnId`; - a parent starting a turn: parked as `turn-starting`, then steered into the turn once it starts; - a pending question: waits on `interaction`, then steers into the same turn; - an offline host: waits on `host-offline`, then steers into the same turn on reconnect; - a held row is stored with `deliveryMode: "steer"`, so a drain joins the running turn instead of waiting for it to end. **Schema:** `queued_thread_messages.coalesce_key` and `coalesce_state` (nullable text), with a partial index on `(thread_id, coalesce_key)`. Migration `0149_coalesced_system_notices`. No daemon wire change, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged. **Plugin SDK:** the `message.dispatch` doc now says core's own notices go through the hook with `initiator: "system"`. SDK 0.6.114 → 0.6.115 (doc-only). ## Visible behaviour changes 1. With Concurrency Limit at its cap, a notice that would start a new turn on an idle parent now waits instead of bypassing the limit. A notice that steers into a running parent turn is unaffected: Concurrency Limit always lets `join-turn` through. 2. A third-party plugin with a `message.dispatch` hook can now hold notices. 3. A child-finished notice to a parent survives a server restart during its 2 s batching window. 4. A notice to a parent that has no environment yet waits for provisioning instead of being dropped. ## How you verified - New `apps/server/test/threads/system-notice-dispatch.test.ts` (15 tests). Run against main's source, the original 14 gave 10 failures. The 4 that pass on main are the steer and recovery guards (running turn, pending question, offline host, recovery on a busy parent), which pin behaviour main already has. - The dispatch hook sees notices as system turns and can hold them. - A parent without an environment waits. - Child outcomes coalesce into one durable row, which the due sweep delivers as one turn; a later outcome after the window starts a new row; an edited coalesced row (new content, execution options or send time) is left alone. - A notice whose hook throws is kept for the retry sweep. - A parent that finished provisioning while the hook ran still gets the notice. - Execution options are resolved at delivery. - The window timer delivers the row even when it fires while the wall clock is still short of `sendAt`. Without the re-arm this fails. It is the cause of the first CI run's Windows failure in `internal-event-append-ownership.test.ts` ("notifies a parent when a child provider process exits"). - A notice whose first attempt never ran on a parent that stays active is recovered. - Mid-turn parent: the notice steers into the running turn. A limiter at its cap that only admits `join-turn` sees the notice as `join-turn` and lets it steer. A held notice is a steer row that joins the turn when released. A pending question makes it wait, then it steers into the same turn. An offline host makes it wait, then it steers into the same turn on reconnect. - The existing "parks a parent system notice with its taxonomy while a turn starts" test (`thread-send-dispatch.test.ts`) covers the parent-starting-a-turn case. `plugins/concurrency-limit/server.test.ts` "does not re-admit running threads or join-turn attempts" covers the limiter side. - Existing child-notification tests now flush through the queue drain instead of the in-memory timer. - `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`: 153/153 tasks passed. - `pnpm exec turbo run test` for `@bb/server` (3803 passed), `@bb/db` (634), `@get-bb/plugin-sdk` (410), `@bb/domain` (294). - `node .github/workflows/check-plugin-sdk-version.mjs`: passes. `drizzle-kit generate` after the rebase reports no schema changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) > AGENT GENERATED --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
3 tasks done
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
Edit-resend, compaction and
/cleareach calledsendThreadMessagedirectly instead of going through the dispatcher (attemptDispatch), and the queue drain had a private fast path that sent straight to the daemon whenever nomessage.dispatchhook was installed. Rules fixed in the dispatcher therefore missed these paths. Two concrete bugs came out of that:senderThreadId: null, so the turn was recorded as the user's, without the agent header, and the cold start always used user permission escalation (Attribute the message that starts a pending thread to its sender thread (senderThreadId is dropped on first dispatch) #5010).auto, sobb thread tell --mode queue --send-at …and retries (documented as "never steer") steered into whatever turn was running when they came due (A message waiting for a future send-at reaches an active thread before its clock #4607, in part).What changed
sendThreadMessageoperationsource: no plugin pass, refuses instead of queueing, as before/clear(sent or queued)sendThreadMessageoperationsource; a queued/clearrow is consumed in the same transaction as the context-clear event, so claim recovery cannot run it twicedraftsandconcurrency-limitboth registermessage.dispatch)delivery_mode(steer/queue) on each row; the drain re-sends a group asqueue-if-activewhen any of its rows isqueue, re-checked after the dispatch hook passParent/child system notices are unchanged and still use their own delivery path; moving them onto the dispatcher is a separate PR.
Schema:
queued_thread_messages.delivery_mode(text NOT NULL DEFAULT 'steer'), migration0148_queued_message_delivery_mode. Existing rows keep today's drain behaviour. No public API, plugin SDK, or daemon wire change, so no SDK orHOST_DAEMON_PROTOCOL_VERSIONbump.Visible behaviour changes
--mode queue, explicit queue, retries) that comes due, or is released by a plugin, while the main run is working waits for the turn to end instead of steering into it. Plain--send-atwithout--mode queuestill steers.initiator: agent, and agent permission escalation (approval requests are refused rather than shown to the user), matching every later message from that thread.initiator: systemand their retry provenance (retryOf), as they already did on installs with hooks.How you verified
/clearreplayed by stale-claim recovery, queue intent lost when a message is sent while only background work runs, a mixed steer/queue group steering in (now it waits), and a queue-mode row steering when the main run resumes during the dispatch hook.apps/server/test/threads/dispatcher-paths.test.ts: Attribute the message that starts a pending thread to its sender thread (senderThreadId is dropped on first dispatch) #5010 attribution, queue vs steer rows coming due mid-turn, queue intent under background work and across the hook, mixed steer/queue groups waiting for the turn in either order, and the queued/clearreplay.thread_not_writableinstead of the deleted fast path'squeued_message_claim_lost; provisioning tests pass the author explicitly; migration rewind helpers drop the new column.turbo run typecheck lintand tests for@bb/server,@bb/db,@bb/domain: 153/153 typecheck and lint tasks pass on everything affected since main; tests pass for@bb/server(3,769),@bb/db(634) and@bb/domain(294).Fixes #5010
🤖 Generated with Claude Code