Skip to content

fix(side-chat): deliver reply anchor on first turn - #2403

Merged
ymichael merged 4 commits into
get-bb:mainfrom
kongenpei:bb/fix-side-chat-reply-anchor-delivery-thr_zgfrdn7zhs
Aug 25, 2026
Merged

ymichael merged 4 commits into
get-bb:mainfrom
kongenpei:bb/fix-side-chat-reply-anchor-delivery-thr_zgfrdn7zhs

Conversation

@kongenpei

@kongenpei kongenpei commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

What was wrong

Side-chat forks persisted the selected-message reply anchor as agent-only input, but deliberately started the native provider fork with empty input so it remained idle. The first real send built its provider command only from the new message, leaving the anchor out of the first provider turn. This affected Codex, Claude Code, and Pi before provider-specific translation. See #2316.

What changed

  • Resolve deferred agent-only thread-start context before the first real provider-bound turn.
  • Prepend that context to flat provider input and the first grouped input for direct and queued sends.
  • Revalidate eligibility inside the send transaction so concurrent sends cannot deliver the anchor twice.
  • Keep context deferred across a dispatch failure until a provider turn/started exists; later turns never receive it again.
  • Exclude agent-only context from user-editable prompt history while retaining it on the accepted turn request.
  • Always create reply context for a non-empty side-chat anchor, including the latest source message, and remove the unnecessary source-timeline lookup.
  • Keep the initial native fork idle with an empty thread.start.input.
  • Increment HOST_DAEMON_PROTOCOL_VERSION to 168 because turn.submit.input semantics change. The wire shape and database schema are unchanged.

How you verified

The deterministic first-turn assertion failed on main before the fix: expected [seed, firstReply], received only [firstReply].

  • pnpm exec turbo run test --filter=@bb/server -- test/public/public-thread-fork.test.ts test/services/prompt-history.test.ts — 29 tests passed.
  • pnpm exec turbo run test --filter=bb-plugin-side-chat — 25 tests passed.
  • pnpm exec turbo run test --filter=@bb/host-daemon-contract -- test/contract.test.ts — 37 tests passed.
  • pnpm exec turbo run typecheck --filter=@bb/server --filter=bb-plugin-side-chat --filter=@bb/db --filter=@bb/host-daemon-contract — 8 Turbo tasks passed.
  • Browser flow with a side chat anchored to a BANANA instruction: before the fix the provider answered APPLE; after the fix it answered BANANA.

Fixes #2316

AGENT GENERATED

@ymichael ymichael self-assigned this Aug 25, 2026
# Conflicts:
#	apps/server/src/services/prompt-history.ts
#	apps/server/test/public/public-thread-fork.test.ts
#	apps/server/test/services/prompt-history.test.ts
#	packages/host-daemon-contract/src/protocol.ts
#	packages/host-daemon-contract/test/contract.test.ts
@ymichael
ymichael merged commit c65d2ec into get-bb:main Aug 25, 2026
15 checks passed
ymichael added a commit that referenced this pull request Nov 3, 2026
## Human comments

## What was wrong

A retry re-sends the failed turn's input with every block marked
`agent-only`. The send path then reads that request through
`resolveDeferredFirstTurnContext` (the side-chat anchor carry-over from
#2403): the last request is a `turn/start` on an `error` thread that
never reached `turn/started`, so its leading agent-only blocks — the
entire retried message — were prepended to the new retry. Each retry of
a never-started turn therefore carried two copies of the previous one.
Separately, a retry that had to reprovision the workspace (or restart an
interrupted startup) dropped its `retryOf` marker, so every failure
reported `attemptNumber: 1` and a retry policy's backoff and attempt cap
never applied. Together these let a failing provisioning loop grow the
stored input exponentially (994 B → 337.5 MB in #6558) into
`startup_context`.

## What changed

- `thread-send.ts`: a dispatch carrying `retryOf` skips the deferred
first-turn context. The retry re-sends the failed request's full input,
which already contains that context.
- `thread-turn-dispatch.ts`, `thread-provisioning.ts`,
`dispatch-attempt.ts`: `retryOf` is threaded through
`dispatchTurnDuringReprovision` → `requestThreadTargetReprovision` and
through `admitPendingThread` → `requestThreadProvision`, so the turn
request those paths append records `retryOfRequestId`/`retryAttempt`
like the ready-environment path already did.
- No wire, CLI, or schema changes.

Not in this PR: the issue's other two suggestions (leaving
`startup_context` out of thread-list queries, flagging oversized stored
context). They are defence in depth against large first prompts in
general and touch the `ThreadRow` shape every list caller uses, so they
belong in a separate change.

## How you verified

- New tests in `apps/server/test/threads/turn-failed-retry.test.ts`;
both fail on `main` and pass here:
- five consecutive retries of a never-started turn each re-send the
original input once with `retryAttempt` 2…6 (on `main` the input block
count goes 1, 2, 4, 8, 16, 32);
- a retry against a provider environment in `error` (the #6558
reprovision path) records `retryAttempt: 3` and stores one copy of the
input in `startup_context` (on `main` it records no `retryAttempt`).
- `pnpm exec turbo run typecheck lint --filter=@bb/server` passes.
- `pnpm exec turbo run test --filter=@bb/server`: 3855 passed, 1 failed
(`public-thread-timeline-push.test.ts`, unrelated; passes twice in
isolation).
- Live check with each checkout's source `bb` CLI against `pnpm
start:worktree` servers for `main` and this branch. The fixture is a
worktree thread whose `.bb-env-setup.sh` always exits 1, so every
provisioning attempt fails, then `bb thread retry` × 6:

| Retry | main: input bytes / `retryAttempt` | branch: input bytes /
`retryAttempt` |
  |---|---|---|
  | 1 | 104 / missing | 104 / 2 |
  | 2 | 208 / missing | 104 / 3 |
  | 3 | 416 / missing | 104 / 4 |
  | 4 | 832 / missing | 104 / 5 |
  | 5 | 1,664 / missing | 104 / 6 |
  | 6 | 3,328 / missing | 104 / 7 |

The queued retry card reads "attempt 2" on main after 7 failures and
"attempt 8" on this branch. A normal claude-code turn and a follow-up
still complete on the branch server.

Fixes #6558

> 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.

Side-chat reply anchor is persisted but omitted from the first provider turn

2 participants