Repository navigation
Keep background work settlements across message edits - #5656
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theitem/startedevent. 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: newlistThreadEventSuffixBackgroundSettlementRowsreturns, 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.How you verified
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/dbpnpm exec turbo run test --filter=@bb/server -- test/internal/background-task-reconciliation.test.ts test/threads/thread-edit-message.test.ts(66 passed)pnpm start:worktreeinstance with a real Claude Code thread: backgroundsleep 3600started (seq 23), second message (seq 33), stopped mid-turn (task settledstoppedat 47), thenbb thread edit-message --expected-request-sequence 33. Result:system/operation48, carrieditem/backgroundTask/completed(stopped) 49, new request 50, newthread/identity51; no running background work remained.Fixes #5481
🤖 Generated with Claude Code