[BUG] /background mid-turn silently drops the in-flight turn in the forked bg session
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When I call /background (or /bg) on a brand-new session while the first turn is still in flight, two things happen:
- The foreground
claudeprocess exits immediately. - The backgrounded session, when reattached via Agent View, shows the first user message but no processing is happening — the turn that was in flight is silently dropped. To make progress I have to re-send the message manually in the bg session.
The bg session is created successfully (it shows up in Agent View) — this is not "bg failed" — but the in-flight work it inherited is frozen.
What Should Happen?
Either:
- The in-flight turn that the foreground was processing should resume automatically in the bg session, or
/backgroundshould not be accepted while a turn is in flight (block with a hint to wait for the current turn to finish).
Right now there's no warning and the user only notices that the bg session is stuck after attaching to it.
Error Messages/Logs
No errors. The foreground exits cleanly with the usual "Session backgrounded" message.
Steps to Reproduce
- Launch
claudefresh (no-r, no-w). - Send your first message — Claude starts processing the turn.
- While the turn is still streaming, run
/background(no argument). claudeexits immediately. ✓ (expected — this is the documented "free the terminal" behavior)- Start
claudeagain and open Agent View (claude agents), or reattach to the bg session from anotherclaudeprocess. - Open the backgrounded session: your first user message is there, but nothing is happening — no spinner, no streaming, no tool calls. The session is idle at the first message.
- To get any output you must send a new message in the bg session.
Claude Model
Not relevant (reproduces regardless of model)
Is this a regression?
I don't know — I started noticing it on 2.1.150 but I haven't tested earlier versions for this specific case.
Last Working Version
Unknown
Claude Code Version
2.1.150
Platform
Anthropic API
Operating System
macOS (Darwin 25.5.0)
Terminal/Shell
zsh (macOS Terminal / iTerm2)
Additional Information
The user data isn't lost (the user message stays in the transcript) but the work — the assistant turn that was running — is silently dropped instead of being resumed in the bg session. The only way to recover is to re-type or re-send the message after attaching to the bg session.
4 Comments
One thing that would help here is making the forked bg session carry an explicit turn handoff record, not just a session clone. If the turn ID and last streaming offset were persisted before the background fork, the UI could show background turn continues instead of silently losing the in-flight work. That kind of receipt also gives users a way to trust background mode on long tasks.
Fix:
/backgroundMid-Turn Silently Drops In-Flight Turn in Forked BG SessionIssue: anthropics/claude-code #62118
Severity: High — silent data/work loss on a core workflow
Labels:
bug,has repro,area:agent-view,platform:macosAffected Version: 2.1.150+ (likely earlier)
---
1. Root Cause Analysis
1.1 Symptom
When
/background(or/bg) is invoked while a turn is still in flight (Claude is actively streaming or executing tool calls), two things happen:claudeprocess exits cleanly with the "Session backgrounded" message.1.2 Architecture Context
From the CHANGELOG and issue description, claude-code's
/backgroundcommand works as follows:claude agents(Agent View / FleetView).1.3 Root Cause
The
/backgroundhandler does not account for the in-flight turn state. When the command fires mid-turn:The critical gap is that the turn handoff is incomplete. The fork clones the session transcript but does not:
1.4 Why This Happens
The
/backgroundcommand handler likely follows this flow:Step 1 does not include the active turn state. The daemon starts with a transcript that ends at the last completed assistant message (or at the user message if no assistant response was written yet). Since the daemon has no record of an in-flight turn, it starts in an idle state.
---
2. Step-by-Step Fix Approach
2.1 Fix Strategy: Two Options
The issue suggests two valid approaches. We recommend Option A (resume the turn) as the primary fix, with Option B (block mid-turn) as a defensive fallback.
Option A: Resume the In-Flight Turn in the BG Session (Preferred)
Persist the turn handoff state before forking, and have the background daemon resume the turn automatically.
Option B: Block
/backgroundWhile a Turn Is In Flight (Defensive)Reject the command with a clear message if a turn is active, preventing data loss.
2.2 Option A Implementation - Turn Handoff Protocol
Step 1: Define the Turn Handoff Record
Add a new data structure to capture the in-flight turn state:
Step 2: Persist the Handoff Record Before Fork
Modify the
/backgroundcommand handler to capture and persist the handoff record:Step 3: Have the Daemon Resume the Turn
Modify the background daemon's startup logic to check for a handoff record and resume:
Step 4: Add UI Indicators in Agent View
Show the user that a turn is being resumed:
2.3 Option B Implementation - Block Mid-Turn Backgrounding (Fallback)
As a defensive measure, also implement a guard that blocks
/backgroundwhen a turn is active:2.4 Recommended Approach: Combine A + B
Implement Option A as the primary fix with Option B as a timeout fallback:
---
3. Testing Strategy
3.1 Unit Tests
| Test | Description | Expected |
|------|-------------|----------|
|
turn-handoff-capture| Capture handoff record during active streaming | All fields populated correctly ||
turn-handoff-persist| Write handoff record to disk | File exists, valid JSON ||
turn-handoff-restore| Daemon loads handoff and resumes turn | Partial content restored, tools replayed ||
turn-handoff-empty| No active turn at background time | No handoff record created ||
turn-handoff-tool-results| Handoff with pending tool results | Results fed back to model ||
turn-handoff-persist-fail| Disk write fails during handoff | Falls back to blocking ||
turn-handoff-daemon-timeout| Daemon doesn't acknowledge | Warning shown, foreground exits |3.2 Integration Tests
| Test | Steps | Expected |
|------|-------|----------|
|
bg-mid-turn-resume| 1. Start session, send message 2./backgroundmid-stream 3. Reattach via Agent View | Turn resumes automatically, output continues ||
bg-mid-turn-with-tools| 1. Send message that triggers tool calls 2./backgroundmid-tool 3. Reattach | Tool calls complete, results visible ||
bg-mid-stream-only| 1. Send message 2./backgroundduring text streaming (no tools) 3. Reattach | Text generation resumes from offset ||
bg-blocked-mid-turn-fallback| 1. Mock disk write failure 2. Send message,/backgroundmid-turn | Command blocked with clear error message ||
bg-idle-turn-no-op| 1. Send message, wait for completion 2./backgroundat idle prompt | Normal background (no handoff needed) |3.3 Manual Reproduction (from the issue)
3.4 Regression Tests
Ensure existing
/backgroundbehavior is preserved:---
4. Impact Assessment
4.1 User Impact
| Severity | Description |
|----------|-------------|
| High | Users lose in-progress work when they background mid-turn. This is silent - no error, no warning. The user must re-type or re-send their message. |
| Frequency | Likely common -
/backgroundis a natural action to take when Claude starts a long-running task, and users won't wait for it to complete before backgrounding. || Data Loss | The user message is preserved, but the assistant's partial response (including any tool calls and results) is lost. For long tasks, this could mean minutes of work. |
4.2 Code Impact
| Area | Risk | Mitigation |
|------|------|------------|
|
/backgroundhandler | Low - additive change, existing flow preserved | Feature flag / config toggle || Daemon initialization | Medium - new code path for handoff resume | Extensive integration tests |
| Transcript persistence | Low - handoff record is separate from transcript | Atomic file writes |
| Agent View UI | Low - new status indicator, existing rendering unchanged | Visual regression tests |
4.3 Edge Cases
/backgroundcalls in rapid succession: Handoff file is locked; second call blocked.4.4 Performance Impact
4.5 Security Considerations
.claude/turn-handoff.json) with the same permissions as other session files.---
5. Files to Modify
Based on the claude-code architecture (inferred from changelog and public interface):
| File (inferred path) | Change |
|----------------------|--------|
|
src/commands/background.ts| Add turn handoff capture and Option B guard ||
src/session/turn-handoff.ts| New -TurnHandoffRecordtype, capture/persist/restore functions ||
src/daemon/session-init.ts| Add handoff detection and turn resume logic ||
src/ui/agent-view/status.ts| Add "Background turn continues..." indicator ||
src/transcript/transcript.ts| SupportisResumedflag on assistant messages |---
6. Summary
The bug occurs because
/backgroundforks the session transcript but does not persist or transfer the in-flight turn state (partial assistant content, pending tool calls/results, streaming offset). The background daemon starts with a transcript that ends at the user message, with no record of the turn that was running.The fix introduces a turn handoff protocol:
TurnHandoffRecord./backgroundif handoff persistence fails.This ensures zero work loss when users background mid-turn, while maintaining backward compatibility with the existing idle-prompt backgrounding flow.
Addressed by a merged fix. Please reopen with a fresh repro if you still see this on a current version.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.