-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathralph-loop.sh
More file actions
executable file
·146 lines (125 loc) · 6.3 KB
/
Copy pathralph-loop.sh
File metadata and controls
executable file
·146 lines (125 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
#
# ralph-loop.sh - Autonomous Claude Code development loop
#
# A working alternative to the broken ralph-wiggum plugin.
# Includes timing stats and CLAUDE.md integration.
#
# Usage: ./ralph-loop.sh [max_iterations]
# Default: 50 iterations
MAX_ITERATIONS=${1:-50}
ITERATION=0
PROMPT_FILE=".claude/RALPH_PROMPT.md"
CLAUDE_MD="CLAUDE.md"
START_TIME=$(date +%s)
# Function to format seconds as HH:MM:SS
format_time() {
local total_seconds=$1
local hours=$((total_seconds / 3600))
local minutes=$(( (total_seconds % 3600) / 60 ))
local seconds=$((total_seconds % 60))
printf "%02d:%02d:%02d" $hours $minutes $seconds
}
# Function to display stats
show_stats() {
local current_time=$(date +%s)
local elapsed=$((current_time - START_TIME))
local elapsed_formatted=$(format_time $elapsed)
if [ $ITERATION -gt 0 ]; then
local avg_per_iteration=$((elapsed / ITERATION))
local remaining_iterations=$((MAX_ITERATIONS - ITERATION))
local estimated_remaining=$((avg_per_iteration * remaining_iterations))
local estimated_formatted=$(format_time $estimated_remaining)
local total_estimated=$((elapsed + estimated_remaining))
local total_formatted=$(format_time $total_estimated)
echo "├─ Elapsed: $elapsed_formatted"
echo "├─ Avg per iteration: $(format_time $avg_per_iteration)"
echo "├─ Est. remaining: $estimated_formatted"
echo "└─ Est. total: $total_formatted"
else
echo "└─ Elapsed: $elapsed_formatted"
fi
}
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ RALPH LOOP - Autonomous Development ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo ""
echo "Max iterations: $MAX_ITERATIONS"
echo "Prompt file: $PROMPT_FILE"
echo "CLAUDE.md: $([ -f "$CLAUDE_MD" ] && echo "Found ✓" || echo "Not found")"
echo "Started at: $(date '+%Y-%m-%d %H:%M:%S')"
echo "Press Ctrl+C to stop"
echo ""
# Check prompt file exists
if [ ! -f "$PROMPT_FILE" ]; then
echo "ERROR: $PROMPT_FILE not found!"
echo ""
echo "Create your prompt file at $PROMPT_FILE"
echo "See PROMPT_TEMPLATE.md for an example."
exit 1
fi
# Build the full prompt (CLAUDE.md + RALPH_PROMPT.md)
build_prompt() {
local full_prompt=""
# Include CLAUDE.md if it exists
if [ -f "$CLAUDE_MD" ]; then
full_prompt+="## Project Context (from CLAUDE.md)
$(cat $CLAUDE_MD)
---
"
fi
# Add the main prompt
full_prompt+="$(cat $PROMPT_FILE)"
echo "$full_prompt"
}
# Main loop
while [ $ITERATION -lt $MAX_ITERATIONS ]; do
ITERATION=$((ITERATION + 1))
ITER_START=$(date +%s)
echo ""
echo "═══════════════════════════════════════════════════════════"
echo " ITERATION $ITERATION / $MAX_ITERATIONS • $(date '+%H:%M:%S')"
echo "───────────────────────────────────────────────────────────"
show_stats
echo "═══════════════════════════════════════════════════════════"
echo ""
# Run Claude with the combined prompt
FULL_PROMPT=$(build_prompt)
OUTPUT=$(claude -p --dangerously-skip-permissions "$FULL_PROMPT" 2>&1)
echo "$OUTPUT"
# Calculate iteration time
ITER_END=$(date +%s)
ITER_DURATION=$((ITER_END - ITER_START))
echo ""
echo "───────────────────────────────────────────────────────────"
echo " Iteration $ITERATION completed in $(format_time $ITER_DURATION)"
echo "───────────────────────────────────────────────────────────"
# Check for completion promise
if echo "$OUTPUT" | grep -q "<promise>.*</promise>"; then
PROMISE=$(echo "$OUTPUT" | grep -o "<promise>.*</promise>" | head -1)
TOTAL_TIME=$(($(date +%s) - START_TIME))
echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ ✓ COMPLETION PROMISE DETECTED ║"
echo "╠═══════════════════════════════════════════════════════════╣"
echo " Promise: $PROMISE"
echo " Iterations: $ITERATION"
echo " Total time: $(format_time $TOTAL_TIME)"
echo "╚═══════════════════════════════════════════════════════════╝"
exit 0
fi
# Safety check
if [ ! -f "$PROMPT_FILE" ]; then
echo "ERROR: Prompt file was deleted! Stopping."
exit 1
fi
done
TOTAL_TIME=$(($(date +%s) - START_TIME))
echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ Max iterations ($MAX_ITERATIONS) reached ║"
echo "╠═══════════════════════════════════════════════════════════╣"
echo " Total time: $(format_time $TOTAL_TIME)"
echo " Avg per iteration: $(format_time $((TOTAL_TIME / MAX_ITERATIONS)))"
echo "╚═══════════════════════════════════════════════════════════╝"
exit 1