Skip to content

Queue messages sent while Pi runs an extension-started compaction - #6380

Closed
steveafrost wants to merge 2 commits into
get-bb:mainfrom
steveafrost:stevefrost/pi-queue-prompts-during-compaction
Closed

steveafrost wants to merge 2 commits into
get-bb:mainfrom
steveafrost:stevefrost/pi-queue-prompts-during-compaction

Conversation

@steveafrost

@steveafrost steveafrost commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

A Pi extension can start a compaction itself with ctx.compact(), for example after a run settles. Pi opens a bb turn for it, and rejects any prompt sent while it runs with "Cannot submit a prompt while compaction is in progress". The server only queues sends during a compaction it started with /compact (isManualCompactionActive checks the active turn's bb request), so a send during an extension compaction was steered into the compaction turn. Pi rejected it, the compaction turn was marked failed, the thread ended in error, and the message was lost.

Fixes #6379

What changed

  • packages/db/src/data/events.ts: new isStoredTurnProviderCompaction. It is true for a turn that started a contextCompaction item and accepted no client input, which is what a provider-started compaction turn looks like.
  • apps/server/src/services/threads/thread-events.ts: isManualCompactionActive also returns true for that kind of active turn. Sends then take the existing thread-busy queue path and go out when the turn completes, the same as during a bb /compact. A normal turn that compacts mid-run is unaffected, because it has accepted input.

No wire or protocol changes.

How you verified

  • New test queues sends while a provider-started compaction turn is active in public-thread-compaction.test.ts. It fails without the thread-events.ts change and passes with it.
  • The 22 server test files that touch queueing or compaction: 426 tests pass.
  • turbo run typecheck --filter=@bb/server --filter=@bb/db passes; oxfmt and oxlint are clean on the changed files.
  • Live with pnpm dev and a Pi extension that calls ctx.compact() on agent_settled and holds compaction for 12 s. Before the fix, a message sent mid-compaction produced the error above and a failed turn. After it, bb thread tell returned delivery: queued (waitingOn: thread-busy), compaction completed normally, and the message was sent 72 ms after it finished and answered.

AGENT GENERATED

Pi rejects prompt RPCs while a manual compaction (extension ctx.compact() or RPC compact) runs. bb only queues sends during compactions it started itself, so a message sent while a Pi extension compacts an idle thread failed with "Cannot submit a prompt while compaction is in progress".

The Pi bridge now tracks Pi manual compactions from compaction_start/compaction_end. A prompt that arrives during one is accepted at once and dispatched when compaction ends. A prompt Pi rejects for that reason is retried after compaction ends. Closing the session fails the queued prompt.
A Pi extension that calls ctx.compact() after a run settles opens its own bb turn holding a contextCompaction item. isManualCompactionActive only recognised a bb-issued /compact turn, so a message sent during that compaction was steered into the compaction turn. Pi rejected it with "Cannot submit a prompt while compaction is in progress", the compaction turn was marked failed and the thread ended in error.

isManualCompactionActive now also treats an active turn that started a contextCompaction item and accepted no client input as a manual compaction. Sends go to the existing thread-busy queue and are delivered when the compaction turn completes.
@github-actions github-actions Bot added the needs-approval PR author is not on the approved contributor list label Oct 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @steveafrost. 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.

@ymichael

Copy link
Copy Markdown
Collaborator

/approve @steveafrost

@github-actions

Copy link
Copy Markdown
Contributor

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

@ymichael

Copy link
Copy Markdown
Collaborator

Continued in #6420, which keeps both of your commits (including the Pi bridge fix) plus a follow-up. Thanks for the fix!

ymichael added a commit that referenced this pull request Oct 28, 2026
)

## Human comments

Replaces #6380 by @steveafrost, which can't be reopened because its
branch was force-pushed after it closed. His two commits keep him as
author. The branch is rebased on main, with one follow-up commit on top.

## What was wrong

A Pi extension can start a compaction itself with `ctx.compact()`, for
example when a run settles. While that compaction runs, Pi rejects every
prompt with "Cannot submit a prompt while compaction is in progress". bb
sent the message anyway: the turn failed, the thread ended in `error`,
and the message was lost.

It happened in two ways:

1. **The next message starts a new turn.** Pi runs the extension's
`agent_settled` handler before the bridge receives `agent_settled`, so
the compaction starts inside the user's turn, and that turn then closes.
The server sees an idle thread and sends the next message as a new turn.
The bridge passed it straight to Pi, which rejected it.
2. **The next message steers into a compaction turn.** When the
compaction opens its own bb turn, the server only queued sends behind a
`/compact` that bb had issued itself (`isManualCompactionActive` checks
the active turn's bb request). It steered the message into the
compaction turn, Pi rejected it, and the turn failed.

Fixes #6379

## What changed

- **Pi bridge (`plugins/provider-pi/src/bridge/rpc-session.ts`), fixes
case 1:**
- Tracks Pi manual compactions from `compaction_start` and
`compaction_end`.
- A prompt that arrives during one is accepted immediately and sent to
Pi once compaction ends.
- A prompt Pi rejects for this reason (because the bridge had not yet
seen `compaction_start`) is retried after compaction ends, up to 8
times.
- Closing the session or the Pi process exiting drops the held prompt
with an error and does not send it.
- **Server, fixes case 2:**
- `isManualCompactionActive` also returns true while the active turn is
a provider-started compaction that is still running. That means a root
`contextCompaction` item that was started and has not completed, in a
turn that accepted no client input.
- Sends then take the existing `thread-busy` queue path and go out when
the turn completes, as they do during a bb `/compact`.
- The check is `isStoredTurnProviderCompactionRunning` in
`packages/db/src/data/events.ts`.
- A normal turn that compacts mid-run is unaffected, and so are a
completed compaction item and a compaction nested under a child agent.

No wire or protocol changes.

## How you verified

- **Live repro against real Pi:**
- Setup: Pi 0.84 with a project extension that calls `ctx.compact()` on
`agent_settled` and holds compaction for 20s.
- On main, both cases produce "Cannot submit a prompt while compaction
is in progress", a failed turn, and a thread in `error`.
- On this branch, case 1 holds the message and answers it after
compaction. Case 2 shows it as queued and sends it after compaction.
- **Bridge (`bridge.manual-compaction-queue.test.ts`):**
- Drives the real bridge against the fake Pi. The fake Pi now rejects
prompts while compacting, as Pi does, and can run an extension-style
compaction on settle.
- It covers a prompt held until compaction ends, a prompt retried after
Pi rejected it, and a held prompt dropped when the thread is released.
- The first two fail without the bridge change, with the user-visible
error. The third fails without the closed-session guard.
- **Server (`public-thread-compaction.test.ts`):**
- A send during a provider compaction turn is queued and delivered after
the turn completes. This fails without the server change.
- Control cases (completed item, nested item, accepted input) still
steer. The first two fail with the looser original check.
- **Suites and checks:**
  - `turbo run test`:
    - `bb-plugin-provider-pi` passes 323 (7 skipped).
    - `@bb/db` passes 634.
- The full `@bb/server` suite passes 3696. One builtin-plugin build test
failed under load in that run and passes on its own; it's unrelated to
this change.
- `turbo run typecheck` passes for `@bb/server`, `@bb/db` and
`bb-plugin-provider-pi`.

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

> AGENT GENERATED

---------

Co-authored-by: Steve Frost <[email protected]>
Co-authored-by: Michael Yong <review@local>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@steveafrost

Copy link
Copy Markdown
Contributor Author

🙏 Thanks @ymichael. Looking forward to helping out whenever/wherever I can. Love bb!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-approval PR author is not on the approved contributor list

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pi: a message sent during an extension-started compaction is steered, rejected, and lost

2 participants