fix(session-lifecycle): detach Claude Code, track PID independently, safe recycling fallback#4
Merged
Merged
Conversation
… PID independently BUG-3: Spawn Claude Code with detached: true + stdio: ignore + unref() so the child runs in its own process group. When the wrapper gets SIGTERM, Claude Code keeps running independently (like nohup). BUG-2: Persist session metadata (PID, start time, cwd) to ~/.claude/agent-ctl/sessions/<id>.json so status checks work after the wrapper exits. isSessionRunning() now checks both ps-discovered PIDs and persisted metadata with kill(pid, 0) liveness checks. Stale metadata is automatically cleaned up when the PID is dead. BUG-1: processStartedAfterSession() now defaults to false (stopped) when start time is unavailable, preventing old sessions from appearing as 'running' due to recycled PIDs when verification is impossible. Tests added: - Session shows running via persisted metadata with live PID - Session shows stopped when persisted PID is dead - Stale metadata cleaned up on dead PID - PID recycling detected in persisted metadata via start time - Old metadata without startTime + live PID assumes running - Existing no-startTime test updated: falls back to stopped (BUG-1 safety) Co-Authored-By: Charlie Hulcher <charlie@kindo.ai>
c-h-
added a commit
that referenced
this pull request
Feb 20, 2026
… PID independently (#4) BUG-3: Spawn Claude Code with detached: true + stdio: ignore + unref() so the child runs in its own process group. When the wrapper gets SIGTERM, Claude Code keeps running independently (like nohup). BUG-2: Persist session metadata (PID, start time, cwd) to ~/.claude/agent-ctl/sessions/<id>.json so status checks work after the wrapper exits. isSessionRunning() now checks both ps-discovered PIDs and persisted metadata with kill(pid, 0) liveness checks. Stale metadata is automatically cleaned up when the PID is dead. BUG-1: processStartedAfterSession() now defaults to false (stopped) when start time is unavailable, preventing old sessions from appearing as 'running' due to recycled PIDs when verification is impossible. Tests added: - Session shows running via persisted metadata with live PID - Session shows stopped when persisted PID is dead - Stale metadata cleaned up on dead PID - PID recycling detected in persisted metadata via start time - Old metadata without startTime + live PID assumes running - Existing no-startTime test updated: falls back to stopped (BUG-1 safety) Co-authored-by: Doink (OpenClaw) <charlie+doink@kindo.ai>
c-h-
pushed a commit
that referenced
this pull request
Apr 4, 2026
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.
Summary
Fixes three interconnected bugs where session lifecycle was incorrectly coupled to the launch wrapper process:
detached: true,stdio: ignore,unref()) — runs in its own process group and survives wrapper SIGTERM~/.claude/agent-ctl/sessions/<id>.jsonso status checks work after the wrapper exits. Liveness verified viakill(pid, 0)with automatic stale metadata cleanupprocessStartedAfterSession()now defaults tofalse(stopped) when start time is unavailable, preventing old sessions from appearing as 'running' due to recycled PIDsTest plan
tsc)tsc --noEmit)biome check)🤖 Generated with Claude Code