From 42f5df629966c25a360c695424a5a9ee41290ea9 Mon Sep 17 00:00:00 2001 From: Test User Date: Mon, 2 Feb 2026 09:47:57 -0700 Subject: [PATCH] fix: prevent session hijacking and improve reliability - Use --resume instead of --continue to avoid hijacking active Claude Code sessions in the same directory (Issue #151) - Write log_status output to stderr to prevent log messages from interfering with function return values The --continue flag resumes "most recent session in current directory" which can inadvertently take over an unrelated Claude Code session. Now Ralph only resumes its own sessions by explicit ID. Fixes #151 Credit: @dionny (PR #153) --- ralph_loop.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ralph_loop.sh b/ralph_loop.sh index 2374886..896a554 100755 --- a/ralph_loop.sh +++ b/ralph_loop.sh @@ -331,7 +331,8 @@ log_status() { "LOOP") color=$PURPLE ;; esac - echo -e "${color}[$timestamp] [$level] $message${NC}" + # Write to stderr so log messages don't interfere with function return values + echo -e "${color}[$timestamp] [$level] $message${NC}" >&2 echo "[$timestamp] [$level] $message" >> "$LOG_DIR/ralph.log" } @@ -981,9 +982,15 @@ build_claude_command() { fi # Add session continuity flag - if [[ "$CLAUDE_USE_CONTINUE" == "true" ]]; then - CLAUDE_CMD_ARGS+=("--continue") + # IMPORTANT: Use --resume with explicit session ID instead of --continue + # --continue resumes the "most recent session in current directory" which + # can hijack active Claude Code sessions. --resume with a specific session ID + # ensures we only resume Ralph's own sessions. (Issue #151) + if [[ "$CLAUDE_USE_CONTINUE" == "true" && -n "$session_id" ]]; then + CLAUDE_CMD_ARGS+=("--resume" "$session_id") fi + # If no session_id, start fresh - Claude will generate a new session ID + # which we'll capture via save_claude_session() for future loops # Add loop context as system prompt (no escaping needed - array handles it) if [[ -n "$loop_context" ]]; then