#!/bin/bash # Ralph Auto Loop - Autonomous AI coding agent that implements specs # # This script runs an autonomous agent (via Claude Code) to implement a specific task. # A focus prompt is REQUIRED - the agent will only do what you ask. set -e set -o pipefail SKIP_CHECKS=false FOCUS_PROMPT="" MAX_ITERATIONS=0 USE_JUDGE=false JUDGE_FIRST=false while [[ $# -gt 0 ]]; do case $1 in --skip-checks) SKIP_CHECKS=true; shift ;; --max-iterations) if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then MAX_ITERATIONS="$2"; shift 2 else echo "Error: --max-iterations requires a positive integer"; exit 1 fi ;; --judge) USE_JUDGE=true; shift ;; --judge-first) JUDGE_FIRST=true; USE_JUDGE=true; shift ;; --help|-h) echo "Usage: ./scripts/ralph/auto.sh [options]" echo "" echo "Options:" echo " --skip-checks Skip CI checks" echo " --max-iterations Limit iterations" echo " --judge Enable judge agent" echo " --judge-first Run judge before starting" echo " --help Show this help" echo "" echo "Configuration: ralph-auto.jsonc (required)" echo " specsDir Directory containing specs" echo " model Claude model" echo " commitPrefix Git commit prefix" echo " checks Array of {name, command} CI checks" exit 0 ;; --*) echo "Unknown option: $1"; echo "Use --help for usage information"; exit 1 ;; *) if [[ -z "$FOCUS_PROMPT" ]]; then FOCUS_PROMPT="$1" else echo "Error: Multiple focus prompts provided"; exit 1 fi shift ;; esac done if [[ -z "$FOCUS_PROMPT" ]]; then echo "Error: A focus prompt is required" echo "Usage: ./scripts/ralph/auto.sh [options]" exit 1 fi COMPLETE_MARKER="NOTHING_LEFT_TO_DO" OUTPUT_DIR=".ralph-auto" CONFIG_FILE="ralph-auto.jsonc" CONTEXT_FILE="$OUTPUT_DIR/previous_iteration_context.md" # Strip JSONC comments and parse JSON parse_jsonc() { sed 's|//.*$||g; s|/\*.*\*/||g' "$CONFIG_FILE" | jq -c '.' } # Load config (called after config file check) load_config() { local config config=$(parse_jsonc) SPECS_DIR=$(echo "$config" | jq -r '.specsDir') CLAUDE_MODEL=$(echo "$config" | jq -r '.model') COMMIT_PREFIX=$(echo "$config" | jq -r '.commitPrefix') } RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' cleanup() { pkill -P $$ 2>/dev/null || true if [ -d "$OUTPUT_DIR" ]; then rm -rf "$OUTPUT_DIR" echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} Cleaned up $OUTPUT_DIR" fi } handle_signal() { echo "" echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} Received interrupt signal, shutting down..." cleanup exit 130 } trap cleanup EXIT trap handle_signal INT TERM mkdir -p "$OUTPUT_DIR" log() { local level=$1; shift local message="$@" local timestamp=$(date '+%Y-%m-%d %H:%M:%S') case $level in "INFO") echo -e "${BLUE}[$timestamp]${NC} $message" ;; "SUCCESS") echo -e "${GREEN}[$timestamp]${NC} $message" ;; "WARN") echo -e "${YELLOW}[$timestamp]${NC} $message" ;; "ERROR") echo -e "${RED}[$timestamp]${NC} $message" ;; esac echo "[$timestamp] [$level] $message" >> "$OUTPUT_DIR/ralph-auto.log" } check_prerequisites() { log "INFO" "Checking prerequisites..." command -v claude &> /dev/null || { log "ERROR" "Claude Code is not installed"; exit 1; } command -v jq &> /dev/null || { log "ERROR" "jq is not installed"; exit 1; } command -v pnpm &> /dev/null || { log "ERROR" "pnpm is not installed"; exit 1; } git rev-parse --git-dir > /dev/null 2>&1 || { log "ERROR" "Not in a git repository"; exit 1; } [ -f "$CONFIG_FILE" ] || { log "ERROR" "$CONFIG_FILE not found"; exit 1; } # Validate config has all required fields local config config=$(parse_jsonc) || { log "ERROR" "$CONFIG_FILE is not valid JSON"; exit 1; } local missing="" echo "$config" | jq -e '.specsDir' > /dev/null 2>&1 || missing+=" specsDir" echo "$config" | jq -e '.model' > /dev/null 2>&1 || missing+=" model" echo "$config" | jq -e '.commitPrefix' > /dev/null 2>&1 || missing+=" commitPrefix" echo "$config" | jq -e '.checks | length > 0' > /dev/null 2>&1 || missing+=" checks" [ -n "$missing" ] && { log "ERROR" "$CONFIG_FILE missing required fields:$missing"; exit 1; } # Load config values load_config log "INFO" "Model: $CLAUDE_MODEL" [ -d "$SPECS_DIR" ] || { log "ERROR" "$SPECS_DIR/ directory not found"; exit 1; } local spec_count=$(find "$SPECS_DIR" -name "*.md" -type f | wc -l | tr -d ' ') [ "$spec_count" -eq 0 ] && { log "ERROR" "No .md files in $SPECS_DIR/"; exit 1; } log "INFO" "Found $spec_count spec(s) in $SPECS_DIR/" # Check for .patterns directory (optional) if [ -d ".patterns" ]; then local pattern_count=$(find ".patterns" -name "*.md" -type f | wc -l | tr -d ' ') if [ "$pattern_count" -gt 0 ]; then log "INFO" "Found $pattern_count pattern(s) in .patterns/" fi fi # Initialize git submodules if needed if [ -f ".gitmodules" ]; then if git submodule status | grep -q "^-"; then log "INFO" "Initializing git submodules..." git submodule update --init fi fi log "SUCCESS" "Prerequisites check passed" } has_changes() { ! git diff --quiet || ! git diff --cached --quiet || [ -n "$(git ls-files --others --exclude-standard)" ] } # Filter Claude stream-json output for readability stream_filter() { while IFS= read -r line; do # Extract assistant text messages if echo "$line" | jq -e '.type == "assistant"' > /dev/null 2>&1; then local text text=$(echo "$line" | jq -r '.message.content[]? | select(.type == "text") | .text // empty' 2>/dev/null) if [ -n "$text" ]; then echo "$text" fi # Show tool calls with details local tool_info tool_info=$(echo "$line" | jq -r ' .message.content[]? | select(.type == "tool_use") | .name as $name | if $name == "Read" then "> Read: \(.input.file_path // "?")" elif $name == "Write" then "> Write: \(.input.file_path // "?")" elif $name == "Edit" then "> Edit: \(.input.file_path // "?")" elif $name == "Glob" then "> Glob: \(.input.pattern // "?")" elif $name == "Grep" then "> Grep: \(.input.pattern // "?") in \(.input.path // ".")" elif $name == "Bash" then "> Bash: \(.input.command // "?" | .[0:80])" elif $name == "Task" then "> Task: \(.input.description // "?")" else "> \($name): \(.input | tostring | .[0:60])" end ' 2>/dev/null) if [ -n "$tool_info" ]; then echo -e "${BLUE}$tool_info${NC}" fi fi # Show final result if echo "$line" | jq -e '.type == "result"' > /dev/null 2>&1; then local result result=$(echo "$line" | jq -r '.result // empty' 2>/dev/null) if [ -n "$result" ]; then echo "" echo "$result" fi fi done } load_ci_checks() { parse_jsonc | jq -c '.checks' } run_ci_checks() { log "INFO" "Running CI checks..." local ci_failed=0 local error_output="" local checks checks=$(load_ci_checks) local check_count check_count=$(echo "$checks" | jq 'length') echo "==========================================" echo "Running CI Checks" echo "==========================================" local i=0 while [ $i -lt "$check_count" ]; do local name command check_output name=$(echo "$checks" | jq -r ".[$i].name") command=$(echo "$checks" | jq -r ".[$i].command") echo -e "\n$((i+1)). $name...\n$(printf '%*s' ${#name} '' | tr ' ' '-')---" if check_output=$(eval "$command" 2>&1); then echo -e "${GREEN}$name passed${NC}" else echo -e "${RED}$name failed${NC}" ci_failed=1 error_output+="## $name Failed\n\`\`\`\n$check_output\n\`\`\`\n\n" fi ((i++)) done echo -e "\n==========================================" if [ $ci_failed -eq 0 ]; then echo -e "${GREEN}All CI checks passed!${NC}" log "SUCCESS" "CI checks passed" return 0 else echo -e "${RED}CI checks failed!${NC}" log "ERROR" "CI checks failed" echo -e "# CI Check Failures\n\n$error_output" > "$OUTPUT_DIR/ci_errors.txt" return 1 fi } commit_changes() { local iteration="$1" task_summary="$2" log "INFO" "Committing changes..." git add -A git diff --cached --quiet && { log "WARN" "No changes to commit"; return 0; } if git commit -m "$COMMIT_PREFIX: $task_summary Ralph-Auto-Iteration: $iteration Automated commit by Ralph Auto loop."; then log "SUCCESS" "Committed: $task_summary" else log "ERROR" "Commit failed"; return 1 fi } rollback_changes() { log "WARN" "Rolling back uncommitted changes..." git checkout -- . git clean -fd } build_prompt() { local iteration=$1 ci_errors="" focus_section="" local previous_context="" [ -f "$OUTPUT_DIR/ci_errors.txt" ] && ci_errors="## Previous Iteration Errors **CI checks failed. You MUST fix these errors.** Read: \`$OUTPUT_DIR/ci_errors.txt\` " [ -n "$FOCUS_PROMPT" ] && focus_section="## FOCUS MODE **Work ONLY on:** $FOCUS_PROMPT Signal TASK_COMPLETE when done. " [ -f "$CONTEXT_FILE" ] && previous_context="$(cat "$CONTEXT_FILE")" local specs_list=$(find "$SPECS_DIR" -name "*.md" -type f | sort | while read f; do echo "- \`$f\`"; done) # Build patterns list if .patterns/ exists local patterns_list="" local patterns_section="" if [ -d ".patterns" ]; then patterns_list=$(find ".patterns" -name "*.md" -type f | sort | while read f; do echo "- \`$f\`"; done) if [ -n "$patterns_list" ]; then patterns_section="**Available patterns:** $patterns_list " fi fi # Build CI checks list from config local checks_list="" local commands_table="" local checks checks=$(load_ci_checks) local check_count check_count=$(echo "$checks" | jq 'length') local i=0 while [ $i -lt "$check_count" ]; do local name command name=$(echo "$checks" | jq -r ".[$i].name") command=$(echo "$checks" | jq -r ".[$i].command") checks_list+="$((i+1)). \`$command\` - $name must pass " commands_table+="| \`$command\` | $name (CI) | " ((i++)) done # Add additional commands from config local extra_commands extra_commands=$(parse_jsonc | jq -c '.commands // []') local extra_count extra_count=$(echo "$extra_commands" | jq 'length') i=0 while [ $i -lt "$extra_count" ]; do local name command name=$(echo "$extra_commands" | jq -r ".[$i].name") command=$(echo "$extra_commands" | jq -r ".[$i].command") commands_table+="| \`$command\` | $name | " ((i++)) done cat <\` or \`NOTHING_LEFT_TO_DO\` if all done 8. **STOP** - do not continue ## Testing Guidelines - Test files are in \`packages/*/test/\` directories - Use \`@effect/vitest\` with \`it.effect\` for Effect-based tests - Import \`{ assert, describe, it }\` from \`@effect/vitest\` - Use \`TestClock\` for time-dependent tests - Run specific tests with: \`pnpm test \` ## Important Reminders - **Read \`AGENTS.md\`** for project structure and conventions - **DO NOT run git commands** - the script handles commits - **Create tasks as needed** - if you discover work that needs to be done within the focus topic, add it to the spec --- ## Iteration This is iteration $iteration of the autonomous loop. $focus_section $previous_context $ci_errors ## Begin Review the focus topic above and select one task to work on. When the task is complete: - If there are MORE tasks remaining: signal \`TASK_COMPLETE: \` and STOP - If this was the LAST task: signal BOTH \`TASK_COMPLETE: \` AND \`NOTHING_LEFT_TO_DO\`, then STOP PROMPT_EOF } extract_task_description() { local output_file="$1" local desc=$(cat "$output_file" | \ jq -r 'select(.type == "assistant") | .message.content[]? | select(.type == "text") | .text // empty' 2>/dev/null | \ grep "TASK_COMPLETE:" | \ head -1 | \ sed 's/.*TASK_COMPLETE:[[:space:]]*//') echo "${desc:-Autonomous improvements}" } build_previous_context() { local output_file="$1" local tool_trail tool_trail=$(cat "$output_file" | jq -r ' select(.type == "assistant") | .message.content[]? | select(.type == "tool_use") | .name as $name | if $name == "Read" then "> Read: \(.input.file_path // "")" elif $name == "Write" then "> Write: \(.input.file_path // "")" elif $name == "Edit" then "> Edit: \(.input.file_path // "")" elif $name == "Glob" then "> Glob: \(.input.pattern // "")" elif $name == "Grep" then "> Grep: \(.input.pattern // "")" elif $name == "Bash" then "> Bash: \(.input.command // "" | .[0:80])" elif $name == "Task" then "> Task: \(.input.description // "")" else "> \($name): \(.input | tostring | .[0:60])" end ' 2>/dev/null | sed '/^$/d') local last_note last_note=$(cat "$output_file" | \ jq -r 'select(.type == "assistant") | .message.content[]? | select(.type == "text") | .text // empty' 2>/dev/null | \ sed '/^\s*$/d' | tail -n 1) cat < "$CONTEXT_FILE" ## Previous Iteration Context The previous iteration stopped without TASK_COMPLETE but left changes. Review the changes and finish the task with a proper TASK_COMPLETE description. ### Tool trail $tool_trail ### Last assistant note $last_note EOF } build_judge_prompt() { local focus="$1" local specs_list=$(find "$SPECS_DIR" -name "*.md" -type f | sort | while read f; do echo "- \`$f\`"; done) cat < "$judge_prompt_file" local judge_exit_code=0 if cat "$judge_prompt_file" | claude --dangerously-skip-permissions --model "$CLAUDE_MODEL" --print --output-format stream-json 2>"$judge_stderr_file" | tee "$judge_output_file" | stream_filter; then log "SUCCESS" "Judge completed" else judge_exit_code=$? if [ $judge_exit_code -eq 130 ] || [ $judge_exit_code -eq 143 ]; then log "INFO" "Judge interrupted"; return 2 fi log "WARN" "Judge exited with status $judge_exit_code" fi local judge_text judge_text=$(cat "$judge_output_file" | \ jq -r 'select(.type == "assistant") | .message.content[]? | select(.type == "text") | .text // empty' 2>/dev/null) # If stream-json parsing yielded nothing, try treating the output as plain text if [ -z "$judge_text" ]; then judge_text=$(cat "$judge_output_file" 2>/dev/null) fi if [ -z "$judge_text" ]; then log "ERROR" "Judge produced no output" log "WARN" "Assuming MORE_WORK_TO_DO to be safe" return 1 fi log "INFO" "Judge output (last 5 lines):" echo "$judge_text" | tail -5 | while IFS= read -r line; do log "INFO" " $line"; done if echo "$judge_text" | grep -q "MORE_WORK_TO_DO"; then log "WARN" "Judge says: MORE_WORK_TO_DO" return 1 elif echo "$judge_text" | grep -q "ALL_WORK_DONE"; then log "SUCCESS" "Judge says: ALL_WORK_DONE" return 0 else log "ERROR" "Judge did not produce a verdict (expected MORE_WORK_TO_DO or ALL_WORK_DONE)" log "WARN" "Assuming MORE_WORK_TO_DO to be safe" return 1 fi } run_iteration() { local iteration=$1 local output_file="$OUTPUT_DIR/iteration_${iteration}_output.txt" local stderr_file="$OUTPUT_DIR/iteration_${iteration}_stderr.txt" local prompt_file="$OUTPUT_DIR/iteration_${iteration}_prompt.md" log "INFO" "Starting iteration $iteration" build_prompt "$iteration" > "$prompt_file" log "INFO" "Prompt: $(wc -l < "$prompt_file" | tr -d ' ') lines" log "INFO" "Running Claude Code agent..." echo "" local agent_exit_code=0 if cat "$prompt_file" | claude --dangerously-skip-permissions --verbose --model "$CLAUDE_MODEL" --print --output-format stream-json 2>"$stderr_file" | tee "$output_file" | stream_filter; then log "SUCCESS" "Agent completed iteration $iteration" else agent_exit_code=$? if [ $agent_exit_code -eq 130 ] || [ $agent_exit_code -eq 143 ]; then log "INFO" "Agent interrupted"; return 1 fi log "WARN" "Agent exited with status $agent_exit_code" fi local assistant_text=$(cat "$output_file" | \ jq -r 'select(.type == "assistant") | .message.content[]? | select(.type == "text") | .text // empty' 2>/dev/null) local has_task_complete=false has_nothing_left=false echo "$assistant_text" | grep -q "TASK_COMPLETE" && has_task_complete=true echo "$assistant_text" | grep -q "$COMPLETE_MARKER" && has_nothing_left=true if [ "$has_task_complete" = true ]; then log "INFO" "Agent signaled task completion" local task_desc=$(extract_task_description "$output_file") local ci_passed=true [ "$SKIP_CHECKS" = true ] && log "INFO" "Skipping CI checks" || { run_ci_checks || ci_passed=false; } if [ "$ci_passed" = true ]; then rm -f "$OUTPUT_DIR/ci_errors.txt" rm -f "$CONTEXT_FILE" commit_changes "$iteration" "$task_desc" || { rollback_changes; return 1; } log "SUCCESS" "Task completed: $task_desc" else log "WARN" "CI failed - keeping changes for next iteration" return 1 fi elif has_changes; then log "WARN" "No TASK_COMPLETE but has changes" local ci_passed=true [ "$SKIP_CHECKS" = true ] || { run_ci_checks || ci_passed=false; } [ "$ci_passed" = true ] && rm -f "$OUTPUT_DIR/ci_errors.txt" build_previous_context "$output_file" log "WARN" "Re-running agent to produce a proper TASK_COMPLETE" return 1 fi if [ "$has_nothing_left" = true ]; then log "SUCCESS" "Agent signaled NOTHING_LEFT_TO_DO" if [ "$USE_JUDGE" = true ]; then local judge_result=0 run_judge "$iteration" || judge_result=$? if [ $judge_result -eq 1 ]; then log "INFO" "Resuming main agent loop per judge verdict" return 1 elif [ $judge_result -eq 2 ]; then return 1 fi fi return 0 fi return 1 } main() { log "INFO" "==========================================" log "INFO" "Starting Ralph Auto Loop" log "INFO" "==========================================" log "INFO" "Focus: $FOCUS_PROMPT" [ "$MAX_ITERATIONS" -gt 0 ] && log "INFO" "Max iterations: $MAX_ITERATIONS" [ "$USE_JUDGE" = true ] && log "INFO" "Judge: enabled" [ "$JUDGE_FIRST" = true ] && log "INFO" "Judge-first: enabled" [ "$SKIP_CHECKS" = true ] && log "WARN" "Skip checks: enabled" check_prerequisites local start_time=$(date +%s) iteration=1 completed=false if [ "$SKIP_CHECKS" = true ]; then log "INFO" "Skipping initial CI checks" rm -f "$OUTPUT_DIR/ci_errors.txt" else log "INFO" "Running initial CI checks..." run_ci_checks && rm -f "$OUTPUT_DIR/ci_errors.txt" || log "WARN" "Initial CI failed" fi if [ "$JUDGE_FIRST" = true ] && [ "$USE_JUDGE" = true ]; then log "INFO" "Running judge before main loop..." local judge_result=0 run_judge 0 || judge_result=$? if [ $judge_result -eq 0 ]; then log "SUCCESS" "Judge says ALL_WORK_DONE before starting" completed=true elif [ $judge_result -eq 2 ]; then log "INFO" "Judge interrupted" exit 130 else log "INFO" "Judge found issues, proceeding to main loop" fi fi if [ "$completed" = true ]; then log "INFO" "==========================================" log "INFO" "Complete. Iterations: 0, Duration: $(($(date +%s) - start_time))s" log "SUCCESS" "All work completed (judge-first)!" exit 0 fi while true; do log "INFO" "------------------------------------------" log "INFO" "ITERATION $iteration" log "INFO" "------------------------------------------" if run_iteration $iteration; then log "SUCCESS" "Nothing left to do!" completed=true break fi [ "$MAX_ITERATIONS" -gt 0 ] && [ "$iteration" -ge "$MAX_ITERATIONS" ] && { log "WARN" "Reached max iterations"; break } sleep 2 ((iteration++)) done log "INFO" "==========================================" log "INFO" "Complete. Iterations: $iteration, Duration: $(($(date +%s) - start_time))s" [ "$completed" = true ] && log "SUCCESS" "All work completed!" log "INFO" "Recent commits:" git log --oneline -5 --grep="Ralph-Auto" || true exit 0 } main