Skip to content

Scope child completion notices to the completed root turn - #6821

Merged
ymichael merged 2 commits into
get-bb:mainfrom
neoromantic:fix/child-completion-turn-excerpt
Nov 2, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
neoromantic:fix/child-completion-turn-excerpt

Conversation

@neoromantic

@neoromantic neoromantic commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

A child's completed-turn notice took its excerpt from getLastThreadOutput, the latest non-empty agent message of the whole thread, not of the turn that completed. A later turn that ended without output therefore re-sent an earlier turn's result to the parent as a new completion. See #6820.

What changed

  • packages/db/src/data/events.ts: getTurnFinalAgentMessageEventRow returns the last root agentMessage (parent_tool_call_id IS NULL) of one turn; it uses events_thread_turn_type_item_sequence_idx.
  • apps/server/src/services/threads/thread-data.ts: getTurnFinalOutput returns that message's text, or null when it is empty or whitespace.
  • apps/server/src/internal/events.ts: the parent-turn follow-up carries the completed turn's turnId.
  • apps/server/src/services/threads/child-thread-notifications.ts: the completion excerpt comes from that turn only. A completed turn without root output still notifies the parent with the existing "No final output was recorded." notice, because the guide promises that auto reports every finished turn and a parent may be waiting on it. It never carries an earlier turn's text.
  • The notification turnId is required and null when no turn applies (setup failure, queue-drain failure, provider process exit); interrupted notices carry the interrupted turn's ID.

Unchanged: failed, interrupted and needs-attention notices; manual-stop handling; batching; parent delivery and queueing; getLastThreadOutput for the thread output route and plugins. The legacy system/manager/user_message source is no longer part of the completion excerpt. No wire or protocol change.

How you verified

  • New apps/server/test/threads/child-completion-turn-output.test.ts, run through /internal/session/events against a migrated SQLite database:
    • a result turn followed by empty, missing and whitespace-only output turns sends four notices; the later three say "No final output was recorded." and do not repeat the earlier result;
    • the root final message is used over nested agent output, and a turn with only nested output does not report it.
      Both fail with the previous child-thread-notifications.ts and pass with the change.
  • pnpm exec turbo run test --filter=@bb/server -- test/threads/child-completion-turn-output.test.ts test/internal/internal-events-tool-calls.test.ts test/services/threads/child-thread-notifications.test.ts test/threads/child-outcome-gaps.test.ts test/threads/system-message-taxonomy-stamping.test.ts test/services/threads/issue-2677-repro.test.ts test/public/public-thread-data.test.ts: 130 passed.
  • pnpm exec turbo run test --filter=@bb/db -- test/data/events.test.ts test/query-plans.test.ts: 146 passed.
  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/db: passed.

Fixes #6820

AGENT GENERATED

@github-actions github-actions Bot added the needs-approval PR author is not on the approved contributor list label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @neoromantic. It was closed automatically because it does not meet the requirements below yet. This is an automated check, not a review of the change.

Contributor approval. bb reviews pull requests only from approved contributors, and you are not on the list yet. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to your pull request, and explain what it changes and why. A maintainer then adds you to the list and reopens the pull request.

See CONTRIBUTING.md for details.

neoromantic and others added 2 commits November 2, 2026 09:59
A child's completed-turn notice took its excerpt from the latest non-empty
agent message in the whole thread. A later turn that ended without output
re-sent the previous turn's result to the parent as a new completion.

The excerpt now comes from the final root agent message of the completed
turn. A completed turn without one sends no completed notice. Failed,
interrupted and needs-attention notices, batching and queue delivery are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The bb guide promises that auto notifications report every finished turn,
so a completed turn without root output still notifies the parent with the
existing "No final output was recorded." notice instead of being dropped.
Notification turn IDs are now required and null when no turn applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael

ymichael commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

/approve @neoromantic

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Approved @neoromantic for PRs. The name now stays on the allow list. The author can open a PR now.

@ymichael ymichael reopened this Nov 2, 2026
@github-actions github-actions Bot removed the needs-approval PR author is not on the approved contributor list label Nov 2, 2026
@ymichael
ymichael force-pushed the fix/child-completion-turn-excerpt branch from 41c5f96 to 9467e29 Compare November 2, 2026 18:06
@ymichael
ymichael merged commit 5d03800 into get-bb:main Nov 2, 2026
31 checks passed
ymichael added a commit that referenced this pull request Nov 3, 2026
…6893)

## Human comments

## What was wrong

`getLastThreadOutput` returned the latest non-empty agent message
anywhere in the thread, including subagent messages and legacy
`system/manager/user_message` events. It fed the `thread.idle`
`lastAssistantText` (push "turn finished" text, Workflows `agent()`
results) and `GET /threads/:id/output` (`bb thread output`). After a
turn with no top-level reply, these repeated an earlier turn's text or
showed a subagent's text. #6821 fixed the same drift for child
completion notices only.

## What changed

- `apps/server/src/services/threads/thread-data.ts`:
`getLatestTurnFinalOutput` returns the final top-level agent message of
the latest root turn, through the same `getTurnFinalOutput` query child
notices use. `getLastThreadOutput` is deleted.
- `thread.idle` (`plugin-service.ts`) and the output route
(`routes/threads/data.ts`) use it, so they still agree as the SDK doc
comment says.
- `packages/db`: deleted the now-unused `getLatestThreadOutputEventRow`
and its tests.
- Plugin authoring docs describe `lastAssistantText` as the latest
turn's final reply.

No plugin SDK, host-daemon protocol or RPC contract changes. The legacy
`system/manager/user_message` source is dropped, as #6821 did; nothing
writes it any more.

## How you verified

- New test in `plugin-thread-events.test.ts` runs three turns and checks
both `thread.idle` and the output route: a reply after subagent work, a
turn with no reply, and a turn with only subagent output. It failed
before the fix (the no-reply turn returned "First result.") and passes
after.
- Existing output-route and slack-bot hero fixtures now record
`turn/started` before their replies, matching real event order.
- `pnpm exec turbo run typecheck lint --filter=@bb/server
--filter=@bb/db`: passed.
- `pnpm exec turbo run test --filter=@bb/db --filter=@bb/server`: all
pass except `install-machine-script.test.ts` "treats launch-agent
readiness as authoritative after bootstrap". That test is intermittent
locally: it passes on unchanged `origin/main` and on this branch when
run alone, and failed in some runs alongside `heroes.test.ts` on a
loaded machine. This change does not touch the install script or its
test.

> AGENT GENERATED

Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 4, 2026
…rns (#7028)

## Human comments

## What was wrong

Two separate causes produced noisy child → parent completion notices.

1. **Notices piled up while the parent was blocked.** A child's turn-end
notice only coalesced into a queued row that was still inside its 2 s
scheduled window
([`getPendingCoalescedQueuedThreadMessage`](https://github.com/get-bb/bb/blob/4f973b851a/packages/db/src/data/queued-thread-messages.ts#L885-L906)
required `state = 'scheduled'` and `send_at > now`). Once that row moved
to `waiting` (pending question, starting turn, offline host), every
later outcome created another row. On 2026-11-03 a parent blocked on an
AskUserQuestion from 22:23 to 04:31 received ten separate "completed"
notices from one child, all steered in within two seconds of the answer.
The same pile-up existed before #6728 (pairs from one child appear in
the event log since September); this is the first time a parent stayed
blocked long enough for it to reach ten.
2. **`/compact` turns were reported as completions.** A `/compact` on a
child runs as its own turn whose only root item is the context
compaction. Its turn end notified the parent with "No final output was
recorded." just before the queued message behind it started the real
work. All 7 "No final output" child notices since #6821 were these
turns. Before #6821 the same turn re-sent the previous turn's output.

## What changed

| Area | Change |
|---|---|
| `packages/db` `getPendingCoalescedQueuedThreadMessage` /
`updateCoalescedQueuedThreadMessageInTransaction` | Match any
undelivered coalesced row (`queued`, `scheduled`, `waiting`). Only a row
still inside its time window has `send_at` pushed back; a waiting row
keeps its wait. |
| `apps/server` `postCoalescedSystemNotice`, child notifications |
Return the row as stored; arm the dispatch timer only for a scheduled
row. |
| `packages/db` `isStoredTurnContextCompactionOnly` | New: true when a
turn's completed root items are all `contextCompaction`. |
| `apps/server/src/internal/events.ts` | A completed child turn that
only compacted context does not notify the parent. A turn that compacts
and then answers still does. |
| `bb guide threads` | `--parent-notifications` text says
compaction-only turns are not reported. |

No daemon wire change, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged.
No schema change.

## How you verified

- `apps/server/test/threads/system-notice-dispatch.test.ts`: "merge
later outcomes into a row still waiting on the parent" (parent on a
pending question, two outcomes from one child plus one from another →
one waiting row, latest status per child). Fails on main, passes here.
It replaces "start a new row once the earlier window has expired", which
pinned the pile-up.
- `apps/server/test/threads/child-completion-turn-output.test.ts`: "does
not notify the parent for a turn that only compacted context"
(compaction-only turn → no notice; compaction + answer → notice with the
answer). Fails without the `events.ts` change. The existing turn-scoping
test now records the parent's latest notice per child turn, because its
harness parent never accepts a turn and the old assertion counted the
pile-up.
- `pnpm exec turbo run typecheck lint --filter=@bb/server
--filter=@bb/db` passes.
- `pnpm exec turbo run test --filter=@bb/server --filter=@bb/db`: 3857
passed / 1 skipped, and 653 passed. One run hit a 40 s timeout in
`builtin-plugins.test.ts` under load ~14; that file passes on its own
(27/27).
- Live QA against a `pnpm start:worktree` server: to follow in a
comment.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 18, 2026
## Human comments

## What was wrong

Creating an interaction has two entry points in
`PendingInteractionLifecycle`: `registerPendingInteraction` for provider
requests from the daemon, and `requestPluginInteraction` for plugin
cards (`bb.ui.requestInput`, which `AskUserQuestion` uses on Codex, Pi
and ACP). Each path ran its own copy of the side effects that follow
creation. The parent needs-attention notice was not in either copy. It
lived in the HTTP route `/internal/session/interactive-request`, so only
the provider path sent it. A child's plugin question therefore never
reached its parent in `auto` or `stuck` mode (#7457). #3397 was the same
split: the plugin copy was missing `interaction.pending`.

## What changed

- **One creation method:**
`PendingInteractionLifecycle.createInteraction` is now the only caller
of `createPendingInteraction`. In one transaction it inserts the row,
after the provider's lookup for an existing row. It then calls
`announceCreatedInteraction`, which appends the timeline event, sends
`interactions-changed`, emits `interaction.pending`, and requests the
parent notice. Both `registerPendingInteraction` and
`requestPluginInteraction` go through it, so a new creation path can't
store an interaction without those effects. A retried provider
registration (`outcome: "existing"`) returns before the announcement, so
the parent is still notified once.
- **Plugin waiter order:** the plugin path now registers its waiter
after the announcement rather than before. Nothing can answer the card
in between: plugin event handlers run on `setImmediate`, the hub
notification goes over the WebSocket, and the parent notice is deferred.
The two `signal.aborted` checks between the insert and the announcement
are gone; nothing asynchronous happens there, so they could never be
true. On setup failure the row is still interrupted ("Plugin interaction
setup failed") and the error rethrown.
- The parent notice and the blocker summary builder moved from
`internal/interactive-requests.ts` to
`services/interactions/pending-interaction-parent-notice.ts`. The code
is unchanged, except that it reuses the thread row the lifecycle already
loaded. The route now only registers the interaction.
- No wire, protocol, schema, CLI or SDK changes. Third-party plugins see
no API change. Plugins that call `bb.ui.requestInput` on a child thread
now cause a parent notice.

**Patches this deletes or supersedes**
- The route-only notice call
(`requestChildThreadNeedsAttentionNotification` in
`interactive-requests.ts`).
- The duplicate post-creation blocks in `registerPendingInteraction` and
`requestPluginInteraction`, which is the per-path emit #3398 had to
patch.
- #7458 (closed), which added a second notice call to
`requestPluginInteraction` and left two call sites. This PR leaves one
and keeps #7458's notice wording.

**Issues and fixes from this root cause (separate per-path producers of
creation side effects)**

| Item | What happened | Prevented by this change? |
|---|---|---|
| #3397 / #3398 | Plugin cards never emitted `interaction.pending`;
#3398 added the emit to the plugin copy | Yes. The emit is in the shared
step, and the guard fails if a path skips it |
| #7457 (this) | Plugin cards never notified the parent | Yes |
| #7458 (closed) | Per-call-site patch for #7457 | Superseded; its
wording is kept |

**Remaining follow-up: #7457 point 6.** When someone answers the child's
question, the parent is not told that a decision was made. Resolution,
expiry and dismissal notices are tracked in #3614 and are out of scope
here.

Not in this class, and not changed here: turn-end notices (#3956/#4356,
#6820/#6821), and parents never learning that a child was unblocked
(#3614).

## Decisions for review

- **Plugin cards in child threads now notify the parent.** This is the
#7457 ask. It also covers any third-party plugin that calls
`bb.ui.requestInput` on a child thread. The same filters apply as for
provider requests: `auto` and `stuck` notify, `off` and fork children do
not.
- **Wording.** Plugin cards don't hold the child's turn, so their notice
reads `Pending plugin request: <title>` followed by `Interaction: <id>`
(the wording from #7458), for example `Pending plugin request: Which
color?` / `Interaction: pint_…`. The ID lets the parent answer with `bb
thread interactions answer`. Provider requests keep `Blocked on
<kind>:`, because they do block the turn.

## How you verified

- **Guard (contract table):**
`apps/server/test/services/interaction-parent-notifications.test.ts`
covers 4 creation paths and 4 child kinds, 16 cases. The paths are a
provider command approval, a provider user question and a provider
plugin-defined request, all sent over the real HTTP route with a retried
registration, plus a plugin card. The child kinds are `auto`, `stuck`,
`off` and a fork child. Each case holds parent delivery and asserts that
exactly one `interaction.pending` was emitted and that the parent has
exactly the expected number (0 or 1) of `child-needs-attention` notices.
- On main: 2 of 16 fail, the plugin card in `auto` and `stuck`
(`expected [] to have a length of 1 but got +0`). That is #7457.
- On main with the plugin path's `emitPluginInteractionPending` removed
(the pre-#3398 shape): all 4 plugin-card cases fail (`expected [] to
deeply equal [ 'pint_…' ]`).
  - With this change: 16 of 16 pass.
- **Live reproduction on a `pnpm start:worktree` instance:** a Codex
parent and a Codex child (`--parent-thread`, `auto`), where the child
calls `AskUserQuestion`.
- main: the card stays `pending`, and the parent gets only
`child-completed`. No `child-needs-attention` arrives.
- This branch: the parent gets one `child-needs-attention` in the same
second the card is created, then `child-completed`. With a `stuck`
child, the parent gets only the notice: `@thread:… needs help.` /
`Pending plugin request: Which color?` / `Interaction: pint_…`.
- **Single-caller check:** the `interaction creation ownership` test
scans every `apps/*/src`, `packages/*/src` and `plugins/*/src` file
outside tests. It fails if `createPendingInteraction` is called anywhere
except the lifecycle; the function's own definition is excluded. Adding
a stray call in `internal/interactive-requests.ts` made it fail and name
that file. The `packages/db/scripts/query-costs` seed scripts are dev
tools that write fixture rows, so the check doesn't scan them.
- **Moved tests:** the route tests for `stuck`/`off` filtering and
fork-child filtering are now covered for every path by the contract
table, so they were removed. The route's message-content and retry test
stays.
- `pnpm exec turbo run test --filter=@bb/server --
test/services/interaction-parent-notifications.test.ts
test/services/pending-interaction-blocker-summary.test.ts
test/internal/internal-interactive-requests.test.ts
test/services/pending-interactions.test.ts
test/threads/system-notice-dispatch.test.ts
test/services/plugins/plugin-thread-events.test.ts
test/threads/thread-send-dispatch.test.ts
test/internal/internal-tool-call-regressions.test.ts
test/public/public-thread-interactions.test.ts
test/services/plugins/plugin-background.test.ts`: 10 files, 218 tests
pass.
- `pnpm exec turbo run typecheck --filter=@bb/server` passes. `oxfmt` is
clean on the changed files.

Fixes #7457

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

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.

Child "completed" notice repeats the previous turn's output after an empty turn

2 participants