-
Notifications
You must be signed in to change notification settings - Fork 517
feat(loop): validate Claude Code CLI command at startup (#97) #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ Ralph is an implementation of the Geoffrey Huntley's technique for Claude Code t | |
|
|
||
| **Version**: v0.11.5 - Active Development | ||
| **Core Features**: Working and tested | ||
| **Test Coverage**: 556 tests, 100% pass rate | ||
| **Test Coverage**: 566 tests, 100% pass rate | ||
|
|
||
|
Comment on lines
+21
to
22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align test-count references after the 566 update. Several lines now say 566 tests, but other nearby lines still show 556/556 passing and “Added 8 new tests” even though 548 → 566 implies +18. Please update the badge/summary text so counts and deltas match. Based on learnings: Documentation MUST remain synchronized with codebase: inline script comments for all functions, README updates for features, template file updates for patterns, and CLAUDE.md updates for new commands and behaviors. Also applies to: 50-51, 639-640, 665-667, 773-773 🤖 Prompt for AI Agents |
||
| ### What's Working Now | ||
| - Autonomous development loops with intelligent exit detection | ||
|
|
@@ -47,7 +47,7 @@ Ralph is an implementation of the Geoffrey Huntley's technique for Claude Code t | |
| - Three-layer API limit detection: timeout guard → structural JSON (`rate_limit_event`) → filtered text fallback | ||
| - Unattended mode: API limit prompt now auto-waits on timeout instead of exiting | ||
| - Fixed bash 3.x compatibility: `${,,}` lowercase substitution replaced with POSIX `tr` (#187) | ||
| - Added 8 new tests for API limit detection (548 → 556 tests) | ||
| - Added 8 new tests for API limit detection (548 → 566 tests) | ||
|
|
||
| **v0.11.4 - Bug Fixes & Compatibility** | ||
| - Fixed progress detection: Git commits within a loop now count as progress (#141) | ||
|
|
@@ -400,6 +400,10 @@ Each Ralph project can have a `.ralphrc` configuration file: | |
| PROJECT_NAME="my-project" | ||
| PROJECT_TYPE="typescript" | ||
|
|
||
| # Claude Code CLI command (auto-detected, override if needed) | ||
| CLAUDE_CODE_CMD="claude" | ||
| # CLAUDE_CODE_CMD="npx @anthropic-ai/claude-code" # Alternative: use npx | ||
|
|
||
| # Loop settings | ||
| MAX_CALLS_PER_HOUR=100 | ||
| CLAUDE_TIMEOUT_MINUTES=15 | ||
|
|
@@ -613,7 +617,7 @@ my-project/ | |
| ## System Requirements | ||
|
|
||
| - **Bash 4.0+** - For script execution | ||
| - **Claude Code CLI** - `npm install -g @anthropic-ai/claude-code` | ||
| - **Claude Code CLI** - `npm install -g @anthropic-ai/claude-code` (or use npx — set `CLAUDE_CODE_CMD` in `.ralphrc`) | ||
| - **tmux** - Terminal multiplexer for integrated monitoring (recommended) | ||
| - **jq** - JSON processing for status tracking | ||
| - **Git** - Version control (projects are initialized as git repos) | ||
|
|
@@ -632,7 +636,7 @@ If you want to run the test suite: | |
| # Install BATS testing framework | ||
| npm install -g bats bats-support bats-assert | ||
|
|
||
| # Run all tests (556 tests) | ||
| # Run all tests (566 tests) | ||
| npm test | ||
|
|
||
| # Run specific test suites | ||
|
|
@@ -658,7 +662,7 @@ bats tests/integration/test_installation.bats | |
| ``` | ||
|
|
||
| Current test status: | ||
| - **556 tests** across 18 test files | ||
| - **566 tests** across 18 test files | ||
| - **100% pass rate** (556/556 passing) | ||
| - Comprehensive unit and integration tests | ||
| - Specialized tests for JSON parsing, CLI flags, circuit breaker, EXIT_SIGNAL behavior, enable wizard, and installation workflows | ||
|
|
@@ -728,6 +732,7 @@ tail -f .ralph/logs/ralph.log | |
|
|
||
| ### Common Issues | ||
|
|
||
| - **Ralph exits silently on first loop** - Claude Code CLI may not be installed or not in PATH. Ralph validates the command at startup and shows installation instructions. If using npx, add `CLAUDE_CODE_CMD="npx @anthropic-ai/claude-code"` to `.ralphrc` | ||
| - **Rate Limits** - Ralph automatically waits and displays countdown | ||
| - **5-Hour API Limit** - Ralph detects and prompts for user action (wait or exit) | ||
| - **Stuck Loops** - Check `fix_plan.md` for unclear or conflicting tasks | ||
|
|
@@ -765,7 +770,7 @@ cd ralph-claude-code | |
|
|
||
| # Install dependencies and run tests | ||
| npm install | ||
| npm test # All 556 tests must pass | ||
| npm test # All 566 tests must pass | ||
| ``` | ||
|
|
||
| ### Priority Contribution Areas | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -681,6 +681,14 @@ generate_ralphrc() { | |
| local project_type="${2:-unknown}" | ||
| local task_sources="${3:-local}" | ||
|
|
||
| # Auto-detect Claude Code CLI command | ||
| local claude_cmd="claude" | ||
| if ! command -v claude &>/dev/null; then | ||
| if command -v npx &>/dev/null; then | ||
| claude_cmd="npx @anthropic-ai/claude-code" | ||
| fi | ||
| fi | ||
|
Comment on lines
+684
to
+690
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify the Claude Code CLI package name used for the npx fallback. The fallback uses Based on learnings: All integration points must be with: Claude Code CLI (npx anthropic/claude-code), tmux terminal multiplexer, Git repositories, jq for JSON processing, GitHub Actions CI/CD, and standard Unix tools (bash, grep, date, etc.). 🤖 Prompt for AI Agents |
||
|
|
||
| cat << RALPHRCEOF | ||
| # .ralphrc - Ralph project configuration | ||
| # Generated by: ralph enable | ||
|
|
@@ -690,6 +698,12 @@ generate_ralphrc() { | |
| PROJECT_NAME="${project_name}" | ||
| PROJECT_TYPE="${project_type}" | ||
|
|
||
| # Claude Code CLI command | ||
| # If "claude" is not in your PATH, set to your installation: | ||
| # "npx @anthropic-ai/claude-code" (uses npx, no global install needed) | ||
| # "/path/to/claude" (custom path) | ||
| CLAUDE_CODE_CMD="${claude_cmd}" | ||
|
|
||
| # Loop settings | ||
| MAX_CALLS_PER_HOUR=100 | ||
| CLAUDE_TIMEOUT_MINUTES=15 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,11 @@ set -e | |
|
|
||
| # Configuration | ||
| CLAUDE_CODE_CMD="claude" | ||
| # Load CLAUDE_CODE_CMD from .ralphrc if available | ||
| if [[ -f ".ralphrc" ]]; then | ||
| _ralphrc_cmd=$(grep "^CLAUDE_CODE_CMD=" ".ralphrc" 2>/dev/null | cut -d= -f2- | tr -d '"' | tr -d "'") | ||
| [[ -n "$_ralphrc_cmd" ]] && CLAUDE_CODE_CMD="$_ralphrc_cmd" | ||
| fi | ||
|
Comment on lines
+9
to
+13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle npx-style CLAUDE_CODE_CMD in dependency checks + camelCase naming. Loading Suggested fix for the dependency check- if ! command -v "$CLAUDE_CODE_CMD" &> /dev/null 2>&1; then
+ local claudeCmdBin="${CLAUDE_CODE_CMD%% *}"
+ if ! command -v "$claudeCmdBin" &> /dev/null 2>&1; then🤖 Prompt for AI Agents |
||
|
|
||
| # Modern CLI Configuration (Phase 1.1) | ||
| # These flags enable structured JSON output and controlled file operations | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ _env_CLAUDE_SESSION_EXPIRY_HOURS="${CLAUDE_SESSION_EXPIRY_HOURS:-}" | |
| _env_VERBOSE_PROGRESS="${VERBOSE_PROGRESS:-}" | ||
| _env_CB_COOLDOWN_MINUTES="${CB_COOLDOWN_MINUTES:-}" | ||
| _env_CB_AUTO_RESET="${CB_AUTO_RESET:-}" | ||
| _env_CLAUDE_CODE_CMD="${CLAUDE_CODE_CMD:-}" | ||
|
|
||
| # Now set defaults (only if not already set by environment) | ||
| MAX_CALLS_PER_HOUR="${MAX_CALLS_PER_HOUR:-100}" | ||
|
|
@@ -116,6 +117,7 @@ RALPHRC_LOADED=false | |
| # - CB_SAME_ERROR_THRESHOLD | ||
| # - CB_OUTPUT_DECLINE_THRESHOLD | ||
| # - RALPH_VERBOSE | ||
| # - CLAUDE_CODE_CMD (path or command for Claude Code CLI) | ||
| # | ||
| load_ralphrc() { | ||
| if [[ ! -f "$RALPHRC_FILE" ]]; then | ||
|
|
@@ -152,11 +154,69 @@ load_ralphrc() { | |
| [[ -n "$_env_VERBOSE_PROGRESS" ]] && VERBOSE_PROGRESS="$_env_VERBOSE_PROGRESS" | ||
| [[ -n "$_env_CB_COOLDOWN_MINUTES" ]] && CB_COOLDOWN_MINUTES="$_env_CB_COOLDOWN_MINUTES" | ||
| [[ -n "$_env_CB_AUTO_RESET" ]] && CB_AUTO_RESET="$_env_CB_AUTO_RESET" | ||
| [[ -n "$_env_CLAUDE_CODE_CMD" ]] && CLAUDE_CODE_CMD="$_env_CLAUDE_CODE_CMD" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium
🤖 Prompt for AI |
||
|
|
||
| RALPHRC_LOADED=true | ||
| return 0 | ||
| } | ||
|
|
||
| # validate_claude_command - Verify the Claude Code CLI is available | ||
| # | ||
| # Checks that CLAUDE_CODE_CMD resolves to an executable command. | ||
| # For npx-based commands, validates that npx is available. | ||
| # Returns 0 if valid, 1 if not found (with helpful error message). | ||
| # | ||
| validate_claude_command() { | ||
| local cmd="$CLAUDE_CODE_CMD" | ||
|
|
||
| # For npx-based commands, check that npx itself is available | ||
| if [[ "$cmd" == npx\ * ]] || [[ "$cmd" == "npx" ]]; then | ||
| if ! command -v npx &>/dev/null; then | ||
| echo "" | ||
| echo -e "${RED}╔════════════════════════════════════════════════════════════╗${NC}" | ||
| echo -e "${RED}║ NPX NOT FOUND ║${NC}" | ||
| echo -e "${RED}╚════════════════════════════════════════════════════════════╝${NC}" | ||
| echo "" | ||
| echo -e "${YELLOW}CLAUDE_CODE_CMD is set to use npx, but npx is not installed.${NC}" | ||
| echo "" | ||
| echo -e "${YELLOW}To fix this:${NC}" | ||
| echo " 1. Install Node.js (includes npx): https://nodejs.org" | ||
| echo " 2. Or install Claude Code globally:" | ||
| echo " npm install -g @anthropic-ai/claude-code" | ||
| echo " Then set in .ralphrc: CLAUDE_CODE_CMD=\"claude\"" | ||
| echo "" | ||
| return 1 | ||
| fi | ||
| return 0 | ||
| fi | ||
|
|
||
| # For direct commands, check that the command exists | ||
| if ! command -v "$cmd" &>/dev/null; then | ||
| echo "" | ||
| echo -e "${RED}╔════════════════════════════════════════════════════════════╗${NC}" | ||
| echo -e "${RED}║ CLAUDE CODE CLI NOT FOUND ║${NC}" | ||
| echo -e "${RED}╚════════════════════════════════════════════════════════════╝${NC}" | ||
| echo "" | ||
| echo -e "${YELLOW}The Claude Code CLI command '${cmd}' is not available.${NC}" | ||
| echo "" | ||
| echo -e "${YELLOW}Installation options:${NC}" | ||
| echo " 1. Install globally (recommended):" | ||
| echo " npm install -g @anthropic-ai/claude-code" | ||
| echo "" | ||
| echo " 2. Use npx (no global install needed):" | ||
| echo " Add to .ralphrc: CLAUDE_CODE_CMD=\"npx @anthropic-ai/claude-code\"" | ||
| echo "" | ||
| echo -e "${YELLOW}Current configuration:${NC} CLAUDE_CODE_CMD=\"${cmd}\"" | ||
| echo "" | ||
| echo -e "${YELLOW}After installation or configuration:${NC}" | ||
| echo " ralph --monitor # Restart Ralph" | ||
| echo "" | ||
| return 1 | ||
| fi | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| # Colors for terminal output | ||
| RED='\033[0;31m' | ||
| GREEN='\033[0;32m' | ||
|
|
@@ -1256,6 +1316,35 @@ execute_claude_code() { | |
| local claude_pid=$! | ||
| local progress_counter=0 | ||
|
|
||
| # Early failure detection: if the command doesn't exist or fails immediately, | ||
| # the backgrounded process dies before the monitoring loop starts (Issue #97) | ||
| sleep 1 | ||
| if ! kill -0 $claude_pid 2>/dev/null; then | ||
| wait $claude_pid 2>/dev/null | ||
| local early_exit=$? | ||
|
Comment on lines
+1322
to
+1324
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium
- if ! kill -0 $claude_pid 2>/dev/null; then
- wait $claude_pid 2>/dev/null
- local early_exit=$?
+ if ! kill -0 $claude_pid 2>/dev/null; then
+ wait $claude_pid 2>/dev/null
+ local early_exit=$?
+ # Only treat non-zero exit as failure (Issue #97)
+ if [[ $early_exit -eq 0 ]]; then
+ log_status "INFO" "Claude Code completed quickly (exit code 0)"
+ else
🤖 Prompt for AI |
||
| local early_output="" | ||
| if [[ -f "$output_file" && -s "$output_file" ]]; then | ||
| early_output=$(tail -5 "$output_file" 2>/dev/null) | ||
| fi | ||
| log_status "ERROR" "❌ Claude Code process exited immediately (exit code: $early_exit)" | ||
| if [[ -n "$early_output" ]]; then | ||
| log_status "ERROR" "Output: $early_output" | ||
| fi | ||
| echo "" | ||
| echo -e "${RED}Claude Code failed to start.${NC}" | ||
| echo "" | ||
| echo -e "${YELLOW}Possible causes:${NC}" | ||
| echo " - '${CLAUDE_CODE_CMD}' command not found or not executable" | ||
| echo " - Claude Code CLI not installed" | ||
| echo " - Authentication or configuration issue" | ||
| echo "" | ||
| echo -e "${YELLOW}To fix:${NC}" | ||
| echo " 1. Verify Claude Code works: ${CLAUDE_CODE_CMD} --version" | ||
| echo " 2. Or set a different command in .ralphrc: CLAUDE_CODE_CMD=\"npx @anthropic-ai/claude-code\"" | ||
| echo "" | ||
| return 1 | ||
| fi | ||
|
Comment on lines
+1319
to
+1346
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Early-failure check can misclassify fast successes. If the CLI exits within 1s with exit code 0, this block still logs an error and returns 1. Consider only treating non‑zero early exits as failures or short‑circuit to the normal success path. Also, As per coding guidelines: 🤖 Prompt for AI Agents |
||
|
|
||
| # Show progress while Claude Code is running | ||
| while kill -0 $claude_pid 2>/dev/null; do | ||
| progress_counter=$((progress_counter + 1)) | ||
|
|
@@ -1453,6 +1542,12 @@ main() { | |
| fi | ||
| fi | ||
|
|
||
| # Validate Claude Code CLI is available before starting | ||
| if ! validate_claude_command; then | ||
| log_status "ERROR" "Claude Code CLI not found: $CLAUDE_CODE_CMD" | ||
| exit 1 | ||
| fi | ||
|
|
||
| log_status "SUCCESS" "🚀 Ralph loop starting with Claude Code" | ||
| log_status "INFO" "Max calls per hour: $MAX_CALLS_PER_HOUR" | ||
| log_status "INFO" "Logs: $LOG_DIR/ | Docs: $DOCS_DIR/ | Status: $STATUS_FILE" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: frankbria/ralph-claude-code
Length of output: 2497
🏁 Script executed:
Repository: frankbria/ralph-claude-code
Length of output: 2775
🌐 Web query:
Claude Code CLI npm package name official documentation💡 Result:
@anthropic-ai/claude-code(installed e.g.npm install -g@anthropic-ai/claude-code``). [1][2]claudecommand). [1]Sources: [1] Anthropic docs, Claude Code SDK; [2] npm package page for
@anthropic-ai/claude-code.Fix inconsistent Claude Code CLI package name references across the codebase.
The canonical npm package is
@anthropic-ai/claude-code, but inconsistencies exist:@anthropic-ai/claude-code)@anthropic/claude-code(missing-ai)create_files.shalso incorrectly uses@anthropic/claude-codeUpdate these references to
@anthropic-ai/claude-codefor consistency across setup documentation and scripts to avoid user confusion during installation.🤖 Prompt for AI Agents