-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
Β·747 lines (612 loc) Β· 26.8 KB
/
start.sh
File metadata and controls
executable file
Β·747 lines (612 loc) Β· 26.8 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#!/bin/bash
# Ralph Start - Main execution loop with tmux support
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$SCRIPT_DIR/lib/utils.sh"
source "$SCRIPT_DIR/lib/circuit_breaker.sh"
source "$SCRIPT_DIR/lib/response_analyzer.sh"
# Configuration
CLAUDE_CMD="claude --dangerously-skip-permissions"
# Use gtimeout on macOS (from brew install coreutils), timeout on Linux
if command -v gtimeout &>/dev/null; then
TIMEOUT_CMD="gtimeout"
elif command -v timeout &>/dev/null; then
TIMEOUT_CMD="timeout"
else
echo "ERROR: 'timeout' command not found. Install with: brew install coreutils"
exit 1
fi
MAX_CALLS_PER_HOUR=${MAX_CALLS_PER_HOUR:-100}
CLAUDE_TIMEOUT_MINUTES=${CLAUDE_TIMEOUT_MINUTES:-20}
MAX_ITERATIONS=${MAX_ITERATIONS:-0} # 0 = unlimited
COMPLETE_TOKEN=${COMPLETE_TOKEN:-"<promise>COMPLETE</promise>"}
USE_TMUX=false
# Rate limiting files
CALL_COUNT_FILE=".call_count"
TIMESTAMP_FILE=".last_reset"
show_help() {
cat << HELPEOF
Ralph Start - Run the autonomous development loop
Usage: $0 <project-name> [OPTIONS]
Arguments:
project-name Name of the Ralph project to run
Options:
-m, --monitor Start with tmux session and live monitor
-n, --max-iterations N Max loop iterations (default: 0 = unlimited)
-c, --calls NUM Max calls per hour (default: $MAX_CALLS_PER_HOUR)
-t, --timeout MIN Claude timeout in minutes (default: $CLAUDE_TIMEOUT_MINUTES)
--complete-token STR Token that signals project completion
(default: $COMPLETE_TOKEN)
-s, --status Show project status and exit
-r, --reset Reset circuit breaker and continue
-h, --help Show this help
Examples:
$0 signals # Run unlimited until complete
$0 signals --monitor # Run with tmux monitoring
$0 signals -n 5 # Run max 5 iterations
$0 signals -n 10 --monitor # 10 iterations with monitoring
$0 signals --status # Show current status
$0 signals --reset # Reset circuit breaker
Environment Variables:
MAX_ITERATIONS Override max iterations
MAX_CALLS_PER_HOUR Override rate limit
CLAUDE_TIMEOUT_MINUTES Override Claude timeout
COMPLETE_TOKEN Override completion token
HELPEOF
}
# Initialize rate limiting
init_rate_limiting() {
local project_dir=$1
local current_hour=$(date +%Y%m%d%H)
local last_reset_hour=""
local count_file="$project_dir/$CALL_COUNT_FILE"
local ts_file="$project_dir/$TIMESTAMP_FILE"
if [[ -f "$ts_file" ]]; then
last_reset_hour=$(cat "$ts_file")
fi
if [[ "$current_hour" != "$last_reset_hour" ]]; then
echo "0" > "$count_file"
echo "$current_hour" > "$ts_file"
log "INFO" "Rate limit counter reset for new hour"
fi
}
# Check if we can make another call
can_make_call() {
local project_dir=$1
local count_file="$project_dir/$CALL_COUNT_FILE"
local calls_made=0
if [[ -f "$count_file" ]]; then
calls_made=$(cat "$count_file")
fi
[[ $calls_made -lt $MAX_CALLS_PER_HOUR ]]
}
# Increment call counter
increment_call_counter() {
local project_dir=$1
local count_file="$project_dir/$CALL_COUNT_FILE"
local calls_made=0
if [[ -f "$count_file" ]]; then
calls_made=$(cat "$count_file")
fi
calls_made=$((calls_made + 1))
echo "$calls_made" > "$count_file"
echo "$calls_made"
}
# Wait for rate limit reset
wait_for_reset() {
local project_dir=$1
local calls_made=$(cat "$project_dir/$CALL_COUNT_FILE" 2>/dev/null || echo "0")
log "WARN" "Rate limit reached ($calls_made/$MAX_CALLS_PER_HOUR). Waiting for reset..."
local wait_time=$(get_seconds_until_next_hour)
log "INFO" "Sleeping for $(format_duration $wait_time) until next hour..."
while [[ $wait_time -gt 0 ]]; do
printf "\r${YELLOW}Time until reset: $(format_duration $wait_time)${NC}"
sleep 1
wait_time=$((wait_time - 1))
done
printf "\n"
echo "0" > "$project_dir/$CALL_COUNT_FILE"
echo "$(date +%Y%m%d%H)" > "$project_dir/$TIMESTAMP_FILE"
log "SUCCESS" "Rate limit reset! Ready for new calls."
}
# Update status file
update_status() {
local project_dir=$1
local loop_count=$2
local status=$3
local current_story=${4:-""}
local prd_file="$project_dir/prd.json"
local complete=$(count_complete_stories "$prd_file")
local total=$(count_total_stories "$prd_file")
local calls=$(cat "$project_dir/$CALL_COUNT_FILE" 2>/dev/null || echo "0")
cat > "$project_dir/status.json" << EOF
{
"timestamp": "$(get_iso_timestamp)",
"status": "$status",
"loop_count": $loop_count,
"current_story": "$current_story",
"stories_complete": $complete,
"stories_total": $total,
"calls_this_hour": $calls,
"max_calls_per_hour": $MAX_CALLS_PER_HOUR
}
EOF
}
# Generate the FULL prompt with ALL stories (like original ralph.sh)
generate_full_prompt() {
local project_dir=$1
local prompt_template=$(cat "$project_dir/PROMPT.md")
local prd_content=$(cat "$project_dir/prd.json")
local progress_content=$(cat "$project_dir/progress.txt" 2>/dev/null || echo "No progress yet.")
local requirements_content=$(cat "$project_dir/requirements.md" 2>/dev/null || echo "No requirements yet.")
# Count stories
local complete=$(count_complete_stories "$project_dir/prd.json")
local total=$(count_total_stories "$project_dir/prd.json")
local incomplete=$((total - complete))
# Get branch name if available
local branch_name=$(get_branch_name "$project_dir/prd.json")
local branch_info=""
local branch_warning=""
if [[ -n "$branch_name" ]]; then
branch_info="**Branch:** \`$branch_name\`"
branch_warning="
## β BRANCH RESTRICTION β
**You are ONLY allowed to push to branch: \`$branch_name\`**
- DO NOT switch to any other branch, do not create new branches.
- DO NOT push to main, master, or any branch other than \`$branch_name\`
"
fi
cat << EOF
$prompt_template
---
## Project Files
| File | Description |
|------|-------------|
| @ralph/projects/$project_name/prd.md | Full PRD with context |
| @ralph/projects/$project_name/prd.json | User stories (\`passes: false\` = incomplete) |
| @ralph/projects/$project_name/progress.txt | What previous iterations accomplished |
| @ralph/projects/$project_name/requirements.md | Technical requirements |
$branch_info
**Progress: $complete/$total complete ($incomplete remaining)**
$branch_warning
## Completion Token
When ALL user stories have \`passes: true\`, reply with:
$COMPLETE_TOKEN
---
Now, follow the instructions above and implement the next incomplete story (lowest priority number that has \`passes: false\`). STOP after completing ONE story.
EOF
}
# Execute Claude Code with FULL context
# Returns: 0=success, 1=error, 2=project complete, 3=API limit, 4=timeout (after retries exhausted), 5=usage limit (with reset time)
execute_claude() {
local project_dir=$1
local loop_count=$2
local max_timeout_retries=2 # Retry up to 2 more times on timeout (3 total attempts)
local timeout_attempt=0
log "LOOP" "Executing Claude Code (Loop #$loop_count)"
# Generate prompt (not saved to disk)
local prompt_content
prompt_content=$(generate_full_prompt "$project_dir")
local timeout_seconds=$((CLAUDE_TIMEOUT_MINUTES * 60))
# Change to repo root for Claude
cd "$REPO_ROOT"
# Retry loop for timeout handling
while true; do
timeout_attempt=$((timeout_attempt + 1))
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
local output_file="$project_dir/logs/claude_${timestamp}.log"
if [[ $timeout_attempt -gt 1 ]]; then
log "INFO" "β³ Timeout retry $((timeout_attempt - 1))/$max_timeout_retries (timeout: ${CLAUDE_TIMEOUT_MINUTES}m)..."
else
log "INFO" "β³ Starting Claude (timeout: ${CLAUDE_TIMEOUT_MINUTES}m)..."
fi
# Execute Claude
if echo "$prompt_content" | $TIMEOUT_CMD ${timeout_seconds}s $CLAUDE_CMD > "$output_file" 2>&1; then
log "SUCCESS" "β
Claude execution completed"
# Check for project completion token
if grep -q "$COMPLETE_TOKEN" "$output_file"; then
log "SUCCESS" "π Claude signaled PROJECT COMPLETE!"
return 2 # Special code for project complete
fi
# Analyze response
analyze_response "$output_file" "$project_dir"
local analysis_result=$?
# Get analysis details
local files_modified=$(get_analysis_result "$project_dir" "files_modified")
local has_errors=$(get_analysis_result "$project_dir" "has_errors")
local error_message=$(get_analysis_result "$project_dir" "error_message")
local exit_signal=$(get_analysis_result "$project_dir" "exit_signal")
local output_length=$(wc -c < "$output_file")
# Record in circuit breaker
record_loop_result "$project_dir" "$loop_count" "${files_modified:-0}" "$has_errors" "$output_length" "$error_message"
# Log analysis
log_analysis_summary "$project_dir"
# Claude updates prd.json directly - no need to mark stories here
# Just log completion status for visibility
if [[ "$exit_signal" == "true" ]]; then
log "SUCCESS" "π Claude signaled loop completion"
fi
return 0
else
local exit_code=$?
# Check for "out of extra usage" first (before timeout retry logic)
# This catches cases where Claude showed the limit message before we timed out
if check_usage_limit "$output_file"; then
log "WARN" "π« Claude out of extra usage - detected limit message"
return 5 # Special code for usage limit with reset time
fi
# Check for timeout (exit code 124)
if [[ $exit_code -eq 124 ]]; then
if [[ $timeout_attempt -le $max_timeout_retries ]]; then
log "WARN" "β° Claude timed out after ${CLAUDE_TIMEOUT_MINUTES} minutes (attempt $timeout_attempt/$((max_timeout_retries + 1)))"
log "INFO" "Retrying in 10 seconds..."
sleep 10
continue # Retry
else
log "ERROR" "β Claude timed out after ${CLAUDE_TIMEOUT_MINUTES} minutes (all $((max_timeout_retries + 1)) attempts exhausted)"
return 4 # Special code for timeout after retries
fi
else
log "ERROR" "β Claude execution failed with code $exit_code"
fi
# Check for 5-hour API limit
if grep -qi "5.*hour.*limit\|limit.*reached\|usage.*limit" "$output_file" 2>/dev/null; then
log "ERROR" "π« Claude API 5-hour usage limit reached"
return 3 # Special code for API limit
fi
return 1
fi
done
}
# Setup tmux session
setup_tmux() {
local project_name=$1
local session_name="ralph-$project_name"
log "INFO" "Setting up tmux session: $session_name"
# Kill existing session if any
tmux kill-session -t "$session_name" 2>/dev/null || true
# Create new session with the loop
tmux new-session -d -s "$session_name" -c "$REPO_ROOT"
# Split horizontally
tmux split-window -h -t "$session_name" -c "$REPO_ROOT"
# Start monitor in right pane
tmux send-keys -t "$session_name:0.1" "$SCRIPT_DIR/monitor.sh $project_name" Enter
# Build command with options (without --monitor to avoid recursion)
local start_cmd="$SCRIPT_DIR/start.sh $project_name"
if [[ $MAX_ITERATIONS -gt 0 ]]; then
start_cmd="$start_cmd -n $MAX_ITERATIONS"
fi
if [[ $MAX_CALLS_PER_HOUR -ne 100 ]]; then
start_cmd="$start_cmd -c $MAX_CALLS_PER_HOUR"
fi
if [[ $CLAUDE_TIMEOUT_MINUTES -ne 20 ]]; then
start_cmd="$start_cmd -t $CLAUDE_TIMEOUT_MINUTES"
fi
if [[ "$COMPLETE_TOKEN" != "<promise>COMPLETE</promise>" ]]; then
start_cmd="$start_cmd --complete-token '$COMPLETE_TOKEN'"
fi
# Start loop in left pane
tmux send-keys -t "$session_name:0.0" "$start_cmd" Enter
# Select left pane
tmux select-pane -t "$session_name:0.0"
# Set window title
tmux rename-window -t "$session_name:0" "Ralph: $project_name"
log "SUCCESS" "tmux session created!"
echo ""
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo " tmux Controls"
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
echo " Ctrl+B, D - Detach (keeps Ralph running in background)"
echo " Ctrl+B, β/β - Switch between panes"
echo " Ctrl+B, [ - Scroll mode (use arrows, 'q' to exit)"
echo ""
echo " To reattach: tmux attach -t $session_name"
echo " To kill: tmux kill-session -t $session_name"
echo ""
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
# Attach to session
tmux attach-session -t "$session_name"
exit 0
}
# Validate that current branch matches prd.json branchName
validate_branch() {
local project_dir=$1
local prd_file="$project_dir/prd.json"
local current_branch=$(git branch --show-current 2>/dev/null)
local expected_branch=$(get_branch_name "$prd_file")
if [[ -z "$current_branch" ]]; then
log "WARN" "Not in a git repository or detached HEAD"
return 0
fi
if [[ -z "$expected_branch" ]]; then
log "WARN" "No branchName specified in prd.json - skipping branch validation"
return 0
fi
if [[ "$current_branch" != "$expected_branch" ]]; then
echo ""
log "ERROR" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
log "ERROR" " BRANCH MISMATCH DETECTED"
log "ERROR" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
log "ERROR" ""
log "ERROR" " Current branch: $current_branch"
log "ERROR" " Expected branch: $expected_branch"
log "ERROR" ""
log "ERROR" " The prd.json requires work to be done on: $expected_branch"
log "ERROR" ""
log "ERROR" " Please switch to the correct branch:"
log "ERROR" " git checkout $expected_branch"
log "ERROR" ""
log "ERROR" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
exit 1
fi
return 0
}
# Confirm current branch before starting
confirm_branch() {
local project_dir=$1
local prd_file="$project_dir/prd.json"
local current_branch=$(git branch --show-current 2>/dev/null)
local expected_branch=$(get_branch_name "$prd_file")
if [[ -z "$current_branch" ]]; then
log "WARN" "Not in a git repository or detached HEAD"
return 0
fi
echo ""
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo " Branch: $current_branch"
if [[ -n "$expected_branch" ]]; then
echo " β οΈ IMPORTANT: You can ONLY push to this branch!"
echo " β οΈ Switching branches during execution is FORBIDDEN."
fi
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
read -p "Continue? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log "INFO" "Aborted by user"
exit 0
fi
return 0
}
# Main loop
main_loop() {
local project_name=$1
local project_dir="$SCRIPT_DIR/projects/$project_name"
# Ensure logs directory exists
mkdir -p "$project_dir/logs"
# Set log file for this session
mkdir -p "$project_dir/logs"
export LOG_FILE="$project_dir/logs/ralph_$(date '+%Y%m%d').log"
log "SUCCESS" "π Ralph starting for project: $project_name"
log "INFO" "Max calls/hour: $MAX_CALLS_PER_HOUR | Timeout: ${CLAUDE_TIMEOUT_MINUTES}m"
if [[ $MAX_ITERATIONS -gt 0 ]]; then
log "INFO" "Max iterations: $MAX_ITERATIONS"
else
log "INFO" "Max iterations: unlimited"
fi
log "INFO" "Complete token: $COMPLETE_TOKEN"
# Validate branch matches prd.json branchName
validate_branch "$project_dir"
# Confirm current branch before starting
confirm_branch "$project_dir"
# Show initial status
local total=$(count_total_stories "$project_dir/prd.json")
local complete=$(count_complete_stories "$project_dir/prd.json")
log "INFO" "Stories: $complete/$total complete"
# Initialize
init_rate_limiting "$project_dir"
init_circuit_breaker "$project_dir"
local loop_count=0
# Main loop
while true; do
loop_count=$((loop_count + 1))
if [[ $MAX_ITERATIONS -gt 0 ]]; then
log "LOOP" "π Loop #$loop_count / $MAX_ITERATIONS"
else
log "LOOP" "π Loop #$loop_count"
fi
# Check max iterations
if [[ $MAX_ITERATIONS -gt 0 ]] && [[ $loop_count -gt $MAX_ITERATIONS ]]; then
log "WARN" "β Max iterations reached ($MAX_ITERATIONS)"
update_status "$project_dir" "$loop_count" "max_iterations" ""
break
fi
# Check circuit breaker
if should_halt_execution "$project_dir"; then
log "ERROR" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
log "ERROR" " π CIRCUIT BREAKER OPEN - EXECUTION HALTED"
log "ERROR" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
log "ERROR" ""
log "ERROR" " The circuit breaker tripped due to repeated failures"
log "ERROR" " or lack of progress."
log "ERROR" ""
log "ERROR" " To reset and continue:"
log "ERROR" " ./ralph/start.sh $project_name --reset"
log "ERROR" ""
log "ERROR" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
update_status "$project_dir" "$loop_count" "halted" ""
break
fi
# Check rate limit
if ! can_make_call "$project_dir"; then
wait_for_reset "$project_dir"
continue
fi
# Check if all stories are complete BEFORE running Claude
local incomplete=$(count_incomplete_stories "$project_dir/prd.json")
if [[ "$incomplete" -eq 0 ]]; then
log "SUCCESS" "π All stories completed!"
update_status "$project_dir" "$loop_count" "complete" ""
local total=$(count_total_stories "$project_dir/prd.json")
log "INFO" "Completed $total stories in $loop_count loops"
break
fi
log "INFO" "Incomplete stories: $incomplete"
update_status "$project_dir" "$loop_count" "running" ""
# Increment call counter
local calls=$(increment_call_counter "$project_dir")
log "INFO" "API call $calls/$MAX_CALLS_PER_HOUR this hour"
# Execute Claude with FULL context
# Capture exit code manually to prevent set -e from exiting on non-zero
local exec_result=0
execute_claude "$project_dir" "$loop_count" || exec_result=$?
if [[ $exec_result -eq 0 ]]; then
update_status "$project_dir" "$loop_count" "success" ""
log "INFO" "Pausing 5s before next loop..."
sleep 5
elif [[ $exec_result -eq 2 ]]; then
# Project complete
update_status "$project_dir" "$loop_count" "complete" ""
log "SUCCESS" "π Project marked as COMPLETE!"
break
elif [[ $exec_result -eq 5 ]]; then
# Usage limit with reset time - wait until reset
update_status "$project_dir" "$loop_count" "usage_limit" "" || true
# Find the latest log file and wait for reset
local latest_log
latest_log=$(ls -t "$project_dir/logs/claude_"*.log 2>/dev/null | head -1) || true
if [[ -n "$latest_log" ]]; then
wait_for_usage_reset "$latest_log" || sleep 3600
else
log "WARN" "Could not find log file, waiting 1 hour..."
sleep 3600
fi
log "SUCCESS" "β
Limit reset! Resuming execution..."
elif [[ $exec_result -eq 3 ]]; then
# API limit
update_status "$project_dir" "$loop_count" "api_limit" ""
log "ERROR" "API limit reached. Waiting 60 minutes..."
# Countdown
local wait_seconds=3600
while [[ $wait_seconds -gt 0 ]]; do
printf "\r${YELLOW}Time until retry: $(format_duration $wait_seconds)${NC}"
sleep 10
wait_seconds=$((wait_seconds - 10))
done
printf "\n"
elif [[ $exec_result -eq 4 ]]; then
# Timeout after all retries exhausted
update_status "$project_dir" "$loop_count" "timeout" ""
log "WARN" "All timeout retries exhausted, waiting 60s before next loop..."
sleep 60
else
update_status "$project_dir" "$loop_count" "error" ""
log "WARN" "Execution failed, waiting 30s before retry..."
sleep 30
fi
log "LOOP" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
log "LOOP" " End Loop #$loop_count"
log "LOOP" "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
done
log "INFO" "Ralph loop finished"
}
# Show project status
show_status() {
local project_name=$1
local project_dir="$SCRIPT_DIR/projects/$project_name"
if [[ ! -f "$project_dir/status.json" ]]; then
log "ERROR" "No status file found"
exit 1
fi
echo ""
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo " Project: $project_name"
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo ""
jq -r '
" Status: \(.status)",
" Loop count: \(.loop_count)",
" Current story: \(.current_story // "none")",
" Progress: \(.stories_complete)/\(.stories_total) stories",
" Calls this hour: \(.calls_this_hour)/\(.max_calls_per_hour)",
" Last updated: \(.timestamp)"
' "$project_dir/status.json"
echo ""
# Show incomplete stories (handle both formats)
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo " Pending Stories"
echo "βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
jq -r 'if type == "object" then .userStories else . end | [.[] | select(.passes == false)] | sort_by(.priority // 999) | .[:5][] | " β [\(.id)] P\(.priority // "?") \(.story | .[0:45])"' "$project_dir/prd.json" 2>/dev/null || echo " (none)"
echo ""
# Show circuit breaker status
show_circuit_status "$project_dir"
}
# Parse arguments
PROJECT_NAME=""
ACTION="run"
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-m|--monitor)
USE_TMUX=true
shift
;;
-n|--max-iterations)
MAX_ITERATIONS="$2"
shift 2
;;
-c|--calls)
MAX_CALLS_PER_HOUR="$2"
shift 2
;;
-t|--timeout)
CLAUDE_TIMEOUT_MINUTES="$2"
shift 2
;;
--complete-token)
COMPLETE_TOKEN="$2"
shift 2
;;
-s|--status)
ACTION="status"
shift
;;
-r|--reset)
ACTION="reset"
shift
;;
*)
if [[ -z "$PROJECT_NAME" ]]; then
PROJECT_NAME="$1"
fi
shift
;;
esac
done
# Validate project name
if [[ -z "$PROJECT_NAME" ]]; then
log "ERROR" "Project name is required"
show_help
exit 1
fi
PROJECT_DIR="$SCRIPT_DIR/projects/$PROJECT_NAME"
# Check if project exists
if [[ ! -d "$PROJECT_DIR" ]]; then
log "ERROR" "Project '$PROJECT_NAME' does not exist"
log "INFO" "Create it first with: ./ralph/new.sh $PROJECT_NAME"
exit 1
fi
# Check dependencies
check_dependencies || exit 1
# Execute action
case "$ACTION" in
"status")
show_status "$PROJECT_NAME"
;;
"reset")
reset_circuit_breaker "$PROJECT_DIR" "Manual reset via CLI"
;;
"run")
if [[ "$USE_TMUX" == "true" ]]; then
setup_tmux "$PROJECT_NAME"
else
main_loop "$PROJECT_NAME"
fi
;;
esac