Skip to content

Keep background work settlements across message edits - #5656

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-bb-issue-5481-thr_upmwm95nva
Oct 17, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-bb-issue-5481-thr_upmwm95nva

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Editing a sent message deletes every event from the edited request onward (deleteThreadEventSuffixInTransaction). When a background task started before that request and settled after it, the deletion removed the settlement but kept the item/started event. In #5481, the background download started at seq 140033, Escape stopped the thread (which settles open tasks with a completion after the edited request at 140146), the edit passed the background-work guard because the task was settled, and then the rewrite deleted that completion. The card showed "Running" indefinitely, and the orphan counted as active background work, so every later edit returned 409.

What changed

  • packages/db/src/data/events.ts: new listThreadEventSuffixBackgroundSettlementRows returns, for each background task or delegation that started before the cutoff and was not settled before it, its last settlement inside the deleted suffix.
  • apps/server/src/services/threads/thread-edit-message.ts: inside the edit transaction, those settlements are re-appended (thread-scoped, original item state) after the suffix is deleted. The predicted replacement request sequence accounts for the carried events.
  • Tasks that start and settle entirely inside the deleted suffix are still removed.
  • No wire, CLI, or guide changes. Orphans already stuck in existing threads are not repaired here; they are settled when the host daemon restarts.

How you verified

  • New regression test in apps/server/test/threads/thread-edit-message.test.ts (task started before the edited message, stopped after it, plus a task entirely inside the suffix). It fails on main and passes with the fix.
  • pnpm exec turbo run lint typecheck --filter=@bb/server --filter=@bb/db
  • pnpm exec turbo run test --filter=@bb/server -- test/internal/background-task-reconciliation.test.ts test/threads/thread-edit-message.test.ts (66 passed)
  • Manual, on a pnpm start:worktree instance with a real Claude Code thread: background sleep 3600 started (seq 23), second message (seq 33), stopped mid-turn (task settled stopped at 47), then bb thread edit-message --expected-request-sequence 33. Result: system/operation 48, carried item/backgroundTask/completed (stopped) 49, new request 50, new thread/identity 51; no running background work remained.

Fixes #5481

AGENT GENERATED

🤖 Generated with Claude Code

Editing a sent message deletes every event from the edited request onward.
When a background task or delegation started before that request and
settled after it (for example, Escape stops the thread and settles the
task, then the edit is accepted), the deletion removed the settlement
but kept the start, so the task showed as running forever and blocked
later edits. The edit now re-appends those settlements after the rewrite.

Fixes #5481

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit cb98dd7 into main Oct 17, 2026
30 checks passed
@ymichael
ymichael deleted the bb/fix-bb-issue-5481-thr_upmwm95nva branch October 17, 2026 00:02
ymichael added a commit that referenced this pull request Oct 19, 2026
## Human comments

## What was wrong

Editing a sent message was refused while the thread had background work
running (a background command, agent, or workflow). The server rejected
with 409 "Wait for background work to finish before editing the
message", and the app independently hid the Edit action and disabled the
edit composer's Submit button on the same condition. Once a turn ended
with background work still open, there was no way to edit without
stopping the thread first. The app kept its own copies of the server's
thread-level edit rules (provider supports session rewind, thread not
archived/deleted, no background work), so the rules could drift between
client and server, as they did while fixing this.

## What changed

- `apps/server/src/services/threads/thread-edit-message.ts`: removed the
background-work 409. After stopping the active turn, if the thread still
has open background tasks, the edit performs a full stop with
`requireStopped` (settling those tasks as stopped/interrupted) before
rewinding. If that stop fails, the edit is rejected and history is
untouched. Together with #5656, a task card that started before the
edited message is kept with its stopped status; cards after it are
removed with the replaced history.
- `apps/server/src/services/threads/thread-message-edit-policy.ts`
(new): one function for the thread-level edit rules (provider supports
session rewind, thread not archived or deleted). The edit route enforces
it at each checkpoint, and `toThreadResponseFromThread` exposes it as
the new `canEditMessages` field on the thread response
(`packages/server-contract`). The plugin runtime's thread DTO reports
`false` when the host has no provider registry.
- `apps/app`: `canEditSentMessages` reads `thread.canEditMessages`
instead of re-deriving provider capability and archive state, and the
background-work gates were removed from both the Edit action and the
edit composer's Submit (`ThreadDetailView.tsx`,
`ThreadDetailPromptArea.tsx`); the now-unused
`activeBackgroundAgentCount` prop is removed.
- Docs: the bb guide's "Editing a sent message" section describes
stopping background work on edit and the `canEditMessages` field.
- Fixtures updated for the new field (app, demo-server, plugin SDK
testing).

No host-daemon wire change.

## How you verified

- New server tests in
`apps/server/test/threads/thread-edit-message.test.ts`: for each of
`local_bash`, `local_agent`, `local_subagent`, `local_workflow`, the
edit stops background work before the rewind, the earlier card ends
`stopped`/`interrupted`, the later card is removed, and nothing is left
open; a failed stop leaves history unchanged; the thread payload's
`canEditMessages` and the edit route agree for an unsupported provider
and an archived thread.
- `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`:
122/122 tasks pass. Focused vitest runs: edit-message +
environment-restore server tests (66 passed), `ThreadDetailPromptArea`
tests (54 passed).
- Manual, on `pnpm start:worktree` with a Claude Code thread, driving
the UI in a browser:
- Editing the message that started a still-running background loop:
Submit is enabled, the loop process is killed, its card is removed with
the replaced history, and the replacement turn runs.
- Loop started by an earlier message, editing a later message: the loop
is killed, the earlier card remains with status `stopped`, the
replacement turn runs, and the running-background-command banner clears.
  - `bb thread show --json` reports `canEditMessages: true`.

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

Background command card stays "Running" forever after a message edit replaced the Claude session

1 participant