claude-code - 💡(How to fix) Fix /clear orphans background tasks by modifying session state in place [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#54658Fetched 2026-04-30 06:39:36
View on GitHub
Comments
2
Participants
3
Timeline
5
Reactions
0
Timeline (top)
labeled ×3commented ×2
RAW_BUFFERClick to expand / collapse

/clear modifies the harness's session state in place rather than starting a fresh session. Background tasks the previous conversation started are left orphaned: they keep running, their completion notifications keep arriving in the new conversation, but the new conversation has no way to enumerate them — so their task_ids are unknown and TaskStop can't be used.

Repro

  1. Start a long-running background bash task (e.g. sleep 600 via Bash with run_in_background: true).
  2. Run /clear.
  3. Ask the model to list or stop background tasks.

Observed

  • Tasks continue running.
  • <task-notification> events for them arrive in the new conversation, referencing the pre-clear session-id.
  • There is no <background-tasks> system reminder and no list tool, so the model cannot discover the task_ids. The only way to learn one is to wait for the task to terminate and read it off the completion notification.
  • Falling back to OS-level ps/kill by PID works, but requires the user to know which PIDs.

Suggested fix

Either adopt the framing implied by /clear and tear down orphaned tasks with the conversation, or surface them to the new conversation (e.g. a <background-tasks> system reminder listing inherited task_ids and descriptions) so the notification stream matches the model's visibility.

Environment

Claude Code CLI.

extent analysis

TL;DR

The issue can be addressed by either tearing down orphaned tasks when /clear is called or surfacing them to the new conversation.

Guidance

  • To fix the issue, consider modifying the /clear command to either stop all background tasks started in the previous conversation or to pass their task_ids to the new conversation.
  • Verify the fix by running the repro steps and checking if the background tasks are properly stopped or if their task_ids are correctly listed in the new conversation.
  • To mitigate the issue, users can currently use OS-level commands like ps and kill to manage the tasks, but this requires knowledge of the PIDs.
  • Consider adding a <background-tasks> system reminder to list inherited task_ids and descriptions, making it easier for the model to discover and manage the tasks.

Example

No code snippet is provided as the issue does not contain specific code references.

Notes

The suggested fix requires changes to how the /clear command handles background tasks, which may have implications for the overall design of the system.

Recommendation

Apply a workaround by surfacing orphaned tasks to the new conversation, as this seems to be a more feasible and less disruptive solution compared to tearing down the tasks. This approach allows for better task management and visibility.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix /clear orphans background tasks by modifying session state in place [2 comments, 3 participants]