setup-ralph-loop.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/bash
  2. # Ralph Loop Setup Script
  3. # Creates state file for in-session Ralph loop
  4. set -euo pipefail
  5. # Parse arguments
  6. PROMPT_PARTS=()
  7. MAX_ITERATIONS=0
  8. COMPLETION_PROMISE="null"
  9. # Parse options and positional arguments
  10. while [[ $# -gt 0 ]]; do
  11. case $1 in
  12. -h|--help)
  13. cat << 'HELP_EOF'
  14. Ralph Loop - Interactive self-referential development loop
  15. USAGE:
  16. /ralph-loop [PROMPT...] [OPTIONS]
  17. ARGUMENTS:
  18. PROMPT... Initial prompt to start the loop (can be multiple words without quotes)
  19. OPTIONS:
  20. --max-iterations <n> Maximum iterations before auto-stop (default: unlimited)
  21. --completion-promise '<text>' Promise phrase (USE QUOTES for multi-word)
  22. -h, --help Show this help message
  23. DESCRIPTION:
  24. Starts a Ralph Loop in your CURRENT session. The stop hook prevents
  25. exit and feeds your output back as input until completion or iteration limit.
  26. To signal completion, you must output: <promise>YOUR_PHRASE</promise>
  27. Use this for:
  28. - Interactive iteration where you want to see progress
  29. - Tasks requiring self-correction and refinement
  30. - Learning how Ralph works
  31. EXAMPLES:
  32. /ralph-loop Build a todo API --completion-promise 'DONE' --max-iterations 20
  33. /ralph-loop --max-iterations 10 Fix the auth bug
  34. /ralph-loop Refactor cache layer (runs forever)
  35. /ralph-loop --completion-promise 'TASK COMPLETE' Create a REST API
  36. STOPPING:
  37. Only by reaching --max-iterations or detecting --completion-promise
  38. No manual stop - Ralph runs infinitely by default!
  39. MONITORING:
  40. # View current iteration:
  41. grep '^iteration:' .claude/ralph-loop.local.md
  42. # View full state:
  43. head -10 .claude/ralph-loop.local.md
  44. HELP_EOF
  45. exit 0
  46. ;;
  47. --max-iterations)
  48. if [[ -z "${2:-}" ]]; then
  49. echo "❌ Error: --max-iterations requires a number argument" >&2
  50. echo "" >&2
  51. echo " Valid examples:" >&2
  52. echo " --max-iterations 10" >&2
  53. echo " --max-iterations 50" >&2
  54. echo " --max-iterations 0 (unlimited)" >&2
  55. echo "" >&2
  56. echo " You provided: --max-iterations (with no number)" >&2
  57. exit 1
  58. fi
  59. if ! [[ "$2" =~ ^[0-9]+$ ]]; then
  60. echo "❌ Error: --max-iterations must be a positive integer or 0, got: $2" >&2
  61. echo "" >&2
  62. echo " Valid examples:" >&2
  63. echo " --max-iterations 10" >&2
  64. echo " --max-iterations 50" >&2
  65. echo " --max-iterations 0 (unlimited)" >&2
  66. echo "" >&2
  67. echo " Invalid: decimals (10.5), negative numbers (-5), text" >&2
  68. exit 1
  69. fi
  70. MAX_ITERATIONS="$2"
  71. shift 2
  72. ;;
  73. --completion-promise)
  74. if [[ -z "${2:-}" ]]; then
  75. echo "❌ Error: --completion-promise requires a text argument" >&2
  76. echo "" >&2
  77. echo " Valid examples:" >&2
  78. echo " --completion-promise 'DONE'" >&2
  79. echo " --completion-promise 'TASK COMPLETE'" >&2
  80. echo " --completion-promise 'All tests passing'" >&2
  81. echo "" >&2
  82. echo " You provided: --completion-promise (with no text)" >&2
  83. echo "" >&2
  84. echo " Note: Multi-word promises must be quoted!" >&2
  85. exit 1
  86. fi
  87. COMPLETION_PROMISE="$2"
  88. shift 2
  89. ;;
  90. *)
  91. # Non-option argument - collect all as prompt parts
  92. PROMPT_PARTS+=("$1")
  93. shift
  94. ;;
  95. esac
  96. done
  97. # Join all prompt parts with spaces
  98. PROMPT="${PROMPT_PARTS[*]}"
  99. # Validate prompt is non-empty
  100. if [[ -z "$PROMPT" ]]; then
  101. echo "❌ Error: No prompt provided" >&2
  102. echo "" >&2
  103. echo " Ralph needs a task description to work on." >&2
  104. echo "" >&2
  105. echo " Examples:" >&2
  106. echo " /ralph-loop Build a REST API for todos" >&2
  107. echo " /ralph-loop Fix the auth bug --max-iterations 20" >&2
  108. echo " /ralph-loop --completion-promise 'DONE' Refactor code" >&2
  109. echo "" >&2
  110. echo " For all options: /ralph-loop --help" >&2
  111. exit 1
  112. fi
  113. # Create state file for stop hook (markdown with YAML frontmatter)
  114. mkdir -p .claude
  115. # Quote completion promise for YAML if it contains special chars or is not null
  116. if [[ -n "$COMPLETION_PROMISE" ]] && [[ "$COMPLETION_PROMISE" != "null" ]]; then
  117. COMPLETION_PROMISE_YAML="\"$COMPLETION_PROMISE\""
  118. else
  119. COMPLETION_PROMISE_YAML="null"
  120. fi
  121. cat > .claude/ralph-loop.local.md <<EOF
  122. ---
  123. active: true
  124. iteration: 1
  125. session_id: ${CLAUDE_CODE_SESSION_ID:-}
  126. max_iterations: $MAX_ITERATIONS
  127. completion_promise: $COMPLETION_PROMISE_YAML
  128. started_at: "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  129. ---
  130. $PROMPT
  131. EOF
  132. # Output setup message
  133. cat <<EOF
  134. 🔄 Ralph loop activated in this session!
  135. Iteration: 1
  136. Max iterations: $(if [[ $MAX_ITERATIONS -gt 0 ]]; then echo $MAX_ITERATIONS; else echo "unlimited"; fi)
  137. Completion promise: $(if [[ "$COMPLETION_PROMISE" != "null" ]]; then echo "${COMPLETION_PROMISE//\"/} (ONLY output when TRUE - do not lie!)"; else echo "none (runs forever)"; fi)
  138. The stop hook is now active. When you try to exit, the SAME PROMPT will be
  139. fed back to you. You'll see your previous work in files, creating a
  140. self-referential loop where you iteratively improve on the same task.
  141. To monitor: head -10 .claude/ralph-loop.local.md
  142. ⚠️ WARNING: This loop cannot be stopped manually! It will run infinitely
  143. unless you set --max-iterations or --completion-promise.
  144. 🔄
  145. EOF
  146. # Output the initial prompt if provided
  147. if [[ -n "$PROMPT" ]]; then
  148. echo ""
  149. echo "$PROMPT"
  150. fi
  151. # Display completion promise requirements if set
  152. if [[ "$COMPLETION_PROMISE" != "null" ]]; then
  153. echo ""
  154. echo "═══════════════════════════════════════════════════════════"
  155. echo "CRITICAL - Ralph Loop Completion Promise"
  156. echo "═══════════════════════════════════════════════════════════"
  157. echo ""
  158. echo "To complete this loop, output this EXACT text:"
  159. echo " <promise>$COMPLETION_PROMISE</promise>"
  160. echo ""
  161. echo "STRICT REQUIREMENTS (DO NOT VIOLATE):"
  162. echo " ✓ Use <promise> XML tags EXACTLY as shown above"
  163. echo " ✓ The statement MUST be completely and unequivocally TRUE"
  164. echo " ✓ Do NOT output false statements to exit the loop"
  165. echo " ✓ Do NOT lie even if you think you should exit"
  166. echo ""
  167. echo "IMPORTANT - Do not circumvent the loop:"
  168. echo " Even if you believe you're stuck, the task is impossible,"
  169. echo " or you've been running too long - you MUST NOT output a"
  170. echo " false promise statement. The loop is designed to continue"
  171. echo " until the promise is GENUINELY TRUE. Trust the process."
  172. echo ""
  173. echo " If the loop should stop, the promise statement will become"
  174. echo " true naturally. Do not force it by lying."
  175. echo "═══════════════════════════════════════════════════════════"
  176. fi