Subagent sessions spawned by multi-agent tasks remain active after parent session exits
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
- Run
/code-review ultra(or any task that spawns multiple subagents via the Agent tool) - Let the task complete
- Exit the parent session with
/exit - 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 ultrarun 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
- Add
parent_session_idfield to session metadata written at spawn time - On parent exit, walk child session IDs and mark them inactive/closed
- 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
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗