Subagent sessions spawned by multi-agent tasks remain active after parent session exits

Open 💬 1 comment Opened Jun 19, 2026 by scheidydude

Summary

When Claude Code spawns subagents during multi-agent tasks (e.g. /code-review ultra),
each subagent creates its own session entry. After the parent session exits cleanly
(/exit), the subagent sessions remain marked as active indefinitely.

Repro Steps

  1. Run /code-review ultra (or any task that spawns multiple subagents via the Agent tool)
  2. Let the task complete
  3. Exit the parent session with /exit
  4. Open Claude Code session list → N sessions remain showing as active from the previous run

Observed Behavior

  • 14 sessions remained active after a /code-review ultra run the following day
  • Sessions display with "root" label — spawned without a tty, detached from parent
  • Sessions were not terminated when parent exited cleanly
  • No automatic cleanup occurs; sessions persist indefinitely (until the 30-day JSONL purge)

Expected Behavior

Subagent sessions spawned by a parent should either:

  • Cascade-close when the parent session exits, OR
  • Be automatically detected as orphaned (parent PID gone) and marked inactive

Root Cause

Session records have no parent_session_id or equivalent metadata. Claude Code has no
way to identify which sessions are children of a given parent, so no cascade-close or
orphan detection is possible.

The invocation chain is partially traceable — parent session JSONL contains tool_use
blocks with name: "Agent" — but there is no cross-reference to the spawned session IDs.

Proposed Fix

  1. Add parent_session_id field to session metadata written at spawn time
  2. On parent exit, walk child session IDs and mark them inactive/closed
  3. On session list load, detect orphaned sessions (parent_session_id set, parent session no longer active or parent PID gone) and auto-mark them inactive

Workaround

Manually delete stale session JSONL files:

\\\`bash
ls ~/.claude/projects/<encoded-cwd>/

identify sessions by timestamp, delete stale ones

rm ~/.claude/projects/<encoded-cwd>/<stale-session-id>.jsonl
\\\`

Environment

  • Claude Code CLI, terminal (zsh/macOS)
  • Triggered by: /code-review ultra (multi-agent subagent spawning)
  • Sessions persist across system sleep/next day with no active process

View original on GitHub ↗

This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗