Skip to content

Route edit, compaction and /clear through the dispatcher and keep queue intent - #6493

Merged
ymichael merged 2 commits into
mainfrom
bb/swing-1-one-inbox-per-thread-thr_ix35ge3jbm
Oct 30, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/swing-1-one-inbox-per-thread-thr_ix35ge3jbm

Conversation

@ymichael

@ymichael ymichael commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Edit-resend, compaction and /clear each called sendThreadMessage directly instead of going through the dispatcher (attemptDispatch), and the queue drain had a private fast path that sent straight to the daemon whenever no message.dispatch hook was installed. Rules fixed in the dispatcher therefore missed these paths. Two concrete bugs came out of that:

What changed

Path Before After
Edit-resend, compaction direct sendThreadMessage dispatcher operation source: no plugin pass, refuses instead of queueing, as before
/clear (sent or queued) direct sendThreadMessage dispatcher operation source; a queued /clear row is consumed in the same transaction as the context-clear event, so claim recovery cannot run it twice
Queue drain with no dispatch hooks private fast path to the daemon deleted; every drained row goes through the dispatcher. Default installs already took the general path (drafts and concurrency-limit both register message.dispatch)
First message of a pending thread author recomputed as the user the dispatcher's resolved author and sender-formatted input; the provision context stores the initiator for the cold start's permission escalation
Queued rows no record of steer vs wait delivery_mode (steer/queue) on each row; the drain re-sends a group as queue-if-active when any of its rows is queue, re-checked after the dispatch hook pass

Parent/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'), migration 0148_queued_message_delivery_mode. Existing rows keep today's drain behaviour. No public API, plugin SDK, or daemon wire change, so no SDK or HOST_DAEMON_PROTOCOL_VERSION bump.

Visible behaviour changes

  1. A queue-mode row (--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-at without --mode queue still steers.
  2. The first message of a pending thread sent by another thread is attributed to that thread: agent header, link to the sender, initiator: agent, and agent permission escalation (approval requests are refused rather than shown to the user), matching every later message from that thread.
  3. A group that mixes a steering queued message with one that waits for the turn is sent as one that waits: it is held until the running turn ends instead of steering in.
  4. On installs with no dispatch hooks, drained retries now carry initiator: system and their retry provenance (retryOf), as they already did on installs with hooks.

How you verified

  • Review findings that apply to this scope, each with a test that failed first: a queued /clear replayed 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.
  • New 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 /clear replay.
  • Existing tests changed: two drain tests expect thread_not_writable instead of the deleted fast path's queued_message_claim_lost; provisioning tests pass the author explicitly; migration rewind helpers drop the new column.
  • turbo run typecheck lint and 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

AGENT GENERATED

…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
ymichael force-pushed the bb/swing-1-one-inbox-per-thread-thr_ix35ge3jbm branch from ada5782 to 7b41cb2 Compare October 29, 2026 21:30
@ymichael ymichael changed the title Route every turn input through one thread inbox and dispatcher Route edit, compaction and /clear through the dispatcher and keep queue intent Oct 29, 2026
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
ymichael merged commit cfad1e4 into main Oct 30, 2026
51 checks passed
@ymichael
ymichael deleted the bb/swing-1-one-inbox-per-thread-thr_ix35ge3jbm branch October 30, 2026 17:13
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]>
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.

Attribute the message that starts a pending thread to its sender thread (senderThreadId is dropped on first dispatch)

1 participant