Repository navigation
Expand file tree
/
Copy pathralph.sh
More file actions
executable file
·1369 lines (1166 loc) · 51 KB
/
Copy pathralph.sh
File metadata and controls
executable file
·1369 lines (1166 loc) · 51 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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# ralph.sh - Queue-based orchestrator that processes prompts sequentially
#
# Usage: ./ralph.sh [-i <iterations>] [--continue-on-fail] [--dry-run] [--budget <dollars>] [--warn-at <dollars>] [-p <prompt-path>]
#
# Prompts are read from ./ralph/prompts/ folder
# - Files use X.Y.Z numbering (e.g., 1.0.0-task.md, 1.1.0-subtask.md)
# - Sorted by: X (main), then Y (sub), then Z (reserved)
# - Files not starting with numbers are skipped
#
# Special directives (in response only):
# - RALPH_WAIT_USER (in prompt): Wait for Enter after completion
# - RALPH_THEN: Chain prompts as siblings (right after current prompt)
# - If current is X.Y.Z, new prompts become X.(Y+1).0, X.(Y+2).0, etc.
# - Can point to a directory to expand all numbered .md files inside
# - RALPH_ADD: Add prompts at bottom of queue
# - Creates (max_X + 1).0.0, (max_X + 2).0.0, etc.
# - Can point to a directory to expand all numbered .md files inside
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROMPTS_DIR="$SCRIPT_DIR/prompts"
PROGRESS_DIR="$PROMPTS_DIR/progress"
DONE_DIR="$PROMPTS_DIR/done"
FAILED_DIR="$PROMPTS_DIR/failed"
PROCESS_DIR="$PROMPTS_DIR/process"
LOG_DIR="$SCRIPT_DIR/log"
SESSION_TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S')
QUEUE_LOG="$LOG_DIR/${SESSION_TIMESTAMP}-log.txt"
QUEUE_STATS="$SCRIPT_DIR/stats.json"
# Session directory for reports - exported for prompts to use
REPORTS_DIR="$SCRIPT_DIR/reports"
export RALPH_SESSION_DIR="$REPORTS_DIR/session-${SESSION_TIMESTAMP}"
RALPH_LOG_DIR="$RALPH_SESSION_DIR/log"
MAX_ITERATIONS=200
CONTINUE_ON_FAIL=false
DRY_RUN=false
STOPPING=false
CLAUDE_RUNNING=false
# Budget and cost tracking
BUDGET_LIMIT=""
WARN_AT=""
BUDGET_WARNING_SENT=false
QUEUE_START=$(date +%s)
TOTAL_PROMPTS_COMPLETED=0
TOTAL_PROMPTS_FAILED=0
TOTAL_INPUT_TOKENS=0
TOTAL_OUTPUT_TOKENS=0
TOTAL_COST=0
# Stuck detection
STUCK_THRESHOLD=5
LAST_PROGRESS_HASH=""
STUCK_COUNT=0
# Rate limit handling
RATE_LIMIT_WAIT=300 # 5 minutes in seconds
# Round tracking (increments when prompt goes from X.Y.Z to (X+1).0.0)
CURRENT_ROUND=1
LAST_MAIN_NUMBER=-1
# PID file for detecting crashed/orphaned sessions
PID_FILE="$SCRIPT_DIR/.ralph.pid"
# Custom prompt path (overrides default seeding)
CUSTOM_PROMPT_PATH=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-i)
MAX_ITERATIONS="$2"
shift 2
;;
--continue-on-fail)
CONTINUE_ON_FAIL=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--budget)
BUDGET_LIMIT="$2"
shift 2
;;
--warn-at)
WARN_AT="$2"
shift 2
;;
--stuck-threshold)
STUCK_THRESHOLD="$2"
shift 2
;;
--rate-limit-wait)
RATE_LIMIT_WAIT="$2"
shift 2
;;
-p)
CUSTOM_PROMPT_PATH="$2"
shift 2
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [-i <max_iterations>] [--continue-on-fail] [--dry-run] [--budget <dollars>] [--warn-at <dollars>] [--stuck-threshold <N>] [--rate-limit-wait <seconds>] [-p <prompt-path>]"
exit 1
;;
esac
done
# Create directories
mkdir -p "$PROMPTS_DIR" "$PROGRESS_DIR" "$DONE_DIR" "$FAILED_DIR" "$LOG_DIR" "$RALPH_SESSION_DIR" "$RALPH_LOG_DIR"
# Initialize queue log
log() {
local msg="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
echo "$msg"
echo "$msg" >> "$QUEUE_LOG"
}
format_duration() {
local seconds=$1
local hours=$((seconds / 3600))
local minutes=$(((seconds % 3600) / 60))
local secs=$((seconds % 60))
if [[ $hours -gt 0 ]]; then
printf "%dh %dm %ds" $hours $minutes $secs
elif [[ $minutes -gt 0 ]]; then
printf "%dm %ds" $minutes $secs
else
printf "%ds" $secs
fi
}
format_cost() {
printf "%.2f" "$1"
}
# Send desktop notification (persistent if possible)
send_notification() {
local title="$1"
local message="$2"
local urgency="${3:-normal}" # low, normal, critical
if command -v notify-send &> /dev/null; then
# Use notify-send with urgency and no timeout for critical (persistent)
if [[ "$urgency" == "critical" ]]; then
notify-send -u critical -t 0 "$title" "$message" 2>/dev/null || true
else
notify-send -u "$urgency" "$title" "$message" 2>/dev/null || true
fi
fi
# Also log
log "NOTIFICATION [$urgency]: $title - $message"
}
# Check budget and send warnings
check_budget() {
# Warning check
if [[ -n "$WARN_AT" && "$BUDGET_WARNING_SENT" == "false" ]]; then
local warn_check=$(echo "$TOTAL_COST >= $WARN_AT" | bc -l 2>/dev/null || echo "0")
if [[ "$warn_check" == "1" ]]; then
send_notification "Ralph Budget Warning" "Cost has reached \$$(format_cost $TOTAL_COST) (warning threshold: \$$WARN_AT)" "critical"
BUDGET_WARNING_SENT=true
fi
fi
# Budget limit check
if [[ -n "$BUDGET_LIMIT" ]]; then
local over_budget=$(echo "$TOTAL_COST >= $BUDGET_LIMIT" | bc -l 2>/dev/null || echo "0")
if [[ "$over_budget" == "1" ]]; then
send_notification "Ralph Budget Exceeded" "Cost \$$(format_cost $TOTAL_COST) has exceeded budget \$$BUDGET_LIMIT. Stopping." "critical"
log "BUDGET EXCEEDED: \$$(format_cost $TOTAL_COST) >= \$$BUDGET_LIMIT"
return 1
fi
fi
return 0
}
# Stuck detection - hash the progress signal to detect no-change iterations
check_stuck() {
local response_text="$1"
local prompt_name="$2"
# Extract progress-relevant content (RALPH_PROGRESS or key metrics)
local progress_content
progress_content=$(echo "$response_text" | grep -E "RALPH_PROGRESS|tests passing|issues|files" | head -3)
# Hash the progress content
local current_hash
current_hash=$(echo "$progress_content" | md5sum 2>/dev/null | cut -d' ' -f1 || echo "$progress_content")
if [[ "$current_hash" == "$LAST_PROGRESS_HASH" && -n "$LAST_PROGRESS_HASH" ]]; then
STUCK_COUNT=$((STUCK_COUNT + 1))
if [[ $STUCK_COUNT -ge $STUCK_THRESHOLD ]]; then
log "WARNING: Stuck for $STUCK_COUNT iterations - same progress reported"
send_notification "Ralph Stuck Detected" "Prompt '$prompt_name' stuck for $STUCK_COUNT iterations with no progress change" "critical"
return 1
fi
else
STUCK_COUNT=0
fi
LAST_PROGRESS_HASH="$current_hash"
return 0
}
# Reset stuck detection for new prompt
reset_stuck_detection() {
LAST_PROGRESS_HASH=""
STUCK_COUNT=0
}
# Check if response indicates rate limiting / subscription limit
# Only considers it a rate limit if response was fast (< threshold seconds)
# Returns 0 if rate limited, 1 if not
RATE_LIMIT_TIME_THRESHOLD=15 # Responses faster than this might be rate limits
check_rate_limit() {
local output="$1"
local duration="${2:-0}"
# Only check for rate limit if response was very fast
# A real response containing these phrases would take longer to generate
if [[ $duration -ge $RATE_LIMIT_TIME_THRESHOLD ]]; then
return 1 # Response took too long to be a rate limit message
fi
# Check for common rate limit / subscription limit phrases
# Claude CLI may output: "usage limit", "rate limit", "resets in", "resets at", "limit reached", "quota"
if echo "$output" | grep -qiE "usage limit|rate limit|resets (in|at)|limit reached|quota exceeded|too many requests|try again later|capacity|overloaded"; then
return 0 # Rate limited
fi
return 1 # Not rate limited
}
# Wait for rate limit to reset
wait_for_rate_limit() {
local wait_time="$1"
local wait_until=$(($(date +%s) + wait_time))
local wait_until_formatted=$(date -d "@$wait_until" '+%H:%M:%S' 2>/dev/null || date -r "$wait_until" '+%H:%M:%S')
log "RATE LIMITED: Waiting $(format_duration $wait_time) until $wait_until_formatted"
send_notification "Ralph Rate Limited" "Waiting $(format_duration $wait_time) for rate limit to reset" "normal"
# Sleep in chunks so we can still respond to Ctrl+C
local remaining=$wait_time
while [[ $remaining -gt 0 && "$STOPPING" == "false" ]]; do
local chunk=30
[[ $remaining -lt $chunk ]] && chunk=$remaining
sleep $chunk
remaining=$((remaining - chunk))
if [[ $remaining -gt 0 && "$STOPPING" == "false" ]]; then
log " Rate limit wait: $(format_duration $remaining) remaining..."
fi
done
if [[ "$STOPPING" == "false" ]]; then
log "Rate limit wait complete. Resuming..."
fi
}
# Extract sort key from filename
# Supports: "1-name.md" → "1 -1 -1", "1.2-name.md" → "1 2 -1", "0.0.3-name.md" → "0 0 3"
# Uses -1 to indicate "no value" so 1.2 sorts before 1.2.0
get_sort_key() {
local filename="$1"
local basename=$(basename "$filename")
# Match: number, optional .number, optional .number, then dash
if [[ "$basename" =~ ^([0-9]+)(\.([0-9]+))?(\.([0-9]+))?- ]]; then
local main="${BASH_REMATCH[1]}"
local sub="${BASH_REMATCH[3]:--1}" # -1 if not present
local subsub="${BASH_REMATCH[5]:--1}" # -1 if not present
echo "$main $sub $subsub"
else
echo ""
fi
}
# Get next prompt (lowest numbered .md file)
get_next_prompt() {
local best_file=""
local best_main=999999
local best_sub=999999
local best_subsub=999999
for file in "$PROMPTS_DIR"/*.md; do
[[ -f "$file" ]] || continue
local key=$(get_sort_key "$file")
[[ -z "$key" ]] && continue # Skip files without numbers
read main sub subsub <<< "$key"
if [[ $main -lt $best_main ]] || \
[[ $main -eq $best_main && $sub -lt $best_sub ]] || \
[[ $main -eq $best_main && $sub -eq $best_sub && $subsub -lt $best_subsub ]]; then
best_main=$main
best_sub=$sub
best_subsub=$subsub
best_file="$file"
fi
done
echo "$best_file"
}
# Get the main number from a prompt filename
# "1.2.0-name.md" → "1", "0.5.0-name.md" → "0", "1-name.md" → "1"
get_prompt_main() {
local filename="$1"
local basename=$(basename "$filename")
# Match number at start (with or without dots after)
if [[ "$basename" =~ ^([0-9]+) ]]; then
echo "${BASH_REMATCH[1]}"
else
echo ""
fi
}
# Get next available sibling slot (RALPH_THEN behavior)
# If current is X.Y.Z, returns X.(Y+1).0
# Scans for highest Y value with same X prefix
get_next_sibling_slot() {
local current_main="$1"
local max_sub=-1
for file in "$PROMPTS_DIR"/*.md; do
[[ -f "$file" ]] || continue
local key=$(get_sort_key "$file")
[[ -z "$key" ]] && continue
read main sub subsub <<< "$key"
# Find highest sub value for this main number
[[ $main -eq $current_main && $sub -ge 0 && $sub -gt $max_sub ]] && max_sub=$sub
done
echo "${current_main}.$((max_sub + 1)).0"
}
# Get next available slot at bottom of queue (RALPH_ADD behavior)
# Returns (max_main + 1).0.0
get_next_bottom_slot() {
local max_main=-1
for file in "$PROMPTS_DIR"/*.md; do
[[ -f "$file" ]] || continue
local key=$(get_sort_key "$file")
[[ -z "$key" ]] && continue
read main sub subsub <<< "$key"
[[ $main -gt $max_main ]] && max_main=$main
done
echo "$((max_main + 1)).0.0"
}
# Get sorted list of numbered .md files from a directory (recursive)
# Recursively expands subdirectories (including symlinks) in alphabetical order
# Returns files sorted by their number prefix within each directory level
# Detects and skips circular references (symlink cycles) using a temp file
# Example: process/myprocess/ containing quality/, meta/, 1-task.md
# → expands meta/, myprocess/, quality/ (alphabetical), then 1-task.md
get_dir_prompts() {
local dir="$1"
local visited_file="${2:-}" # Temp file tracking visited dirs
# Create visited tracking file on root call
local is_root=false
if [[ -z "$visited_file" ]]; then
is_root=true
visited_file=$(mktemp)
fi
# Resolve to canonical path for cycle detection
local canonical_dir
canonical_dir=$(cd "$dir" 2>/dev/null && pwd -P) || {
[[ "$is_root" == "true" ]] && rm -f "$visited_file"
return
}
# Check for cycle (already visited this canonical path)
if grep -qxF "$canonical_dir" "$visited_file" 2>/dev/null; then
log "Warning: Circular directory reference detected, skipping: $dir"
[[ "$is_root" == "true" ]] && rm -f "$visited_file"
return
fi
echo "$canonical_dir" >> "$visited_file"
# Collect subdirectories and files separately
local subdirs=()
local mdfiles=()
for entry in "$dir"/*; do
[[ -e "$entry" ]] || continue # Skip if nothing matches
# Check if it's a directory (follows symlinks)
if [[ -d "$entry" ]]; then
subdirs+=("$entry")
elif [[ -f "$entry" && "$entry" == *.md ]]; then
local key=$(get_sort_key "$entry")
[[ -z "$key" ]] && continue # Skip files without numbers
mdfiles+=("$entry")
fi
done
# Sort subdirectories alphabetically and recursively expand each
if [[ ${#subdirs[@]} -gt 0 ]]; then
local sorted_subdirs
sorted_subdirs=$(printf '%s\n' "${subdirs[@]}" | sort)
while IFS= read -r subdir; do
[[ -z "$subdir" ]] && continue
# Recursively get prompts from subdirectory
get_dir_prompts "$subdir" "$visited_file"
done <<< "$sorted_subdirs"
fi
# Sort .md files by their number prefix and output them
if [[ ${#mdfiles[@]} -gt 0 ]]; then
for file in "${mdfiles[@]}"; do
local key=$(get_sort_key "$file")
echo "$key|$file"
done | sort -t'|' -k1,1n -k2,2n -k3,3n | cut -d'|' -f2
fi
# Clean up temp file on root call
if [[ "$is_root" == "true" ]]; then
rm -f "$visited_file"
fi
}
# Process RALPH_THEN directives - chain prompts as siblings (right after current)
# If current is X.Y.Z, new prompts become X.(Y+1).0, X.(Y+2).0, etc.
# If path is a directory, expands all numbered .md files inside
process_then_directives() {
local response_text="$1"
local current_prompt="$2"
# Extract RALPH_THEN lines (response_text already has real newlines)
local then_lines
then_lines=$(echo "$response_text" | grep "RALPH_THEN:" 2>/dev/null) || true
[[ -z "$then_lines" ]] && return 0
# Get main number from current prompt
local current_main=$(get_prompt_main "$current_prompt")
[[ -z "$current_main" ]] && current_main=0
while IFS= read -r line; do
[[ -z "$line" ]] && continue
# Extract path after RALPH_THEN: (first non-space word only)
local then_path=$(echo "$line" | sed 's/.*RALPH_THEN:[[:space:]]*//' | awk '{print $1}')
[[ -z "$then_path" ]] && continue
local source="$PROMPTS_DIR/$then_path"
# Check if it's a directory
if [[ -d "$source" ]]; then
# Expand directory: queue all numbered .md files inside
local found_files=false
while IFS= read -r dir_file; do
[[ -z "$dir_file" ]] && continue
found_files=true
local next_num=$(get_next_sibling_slot "$current_main")
local dest_name="${next_num}-$(basename "$dir_file")"
cp "$dir_file" "$PROMPTS_DIR/$dest_name"
log "Queued: $dest_name (from $then_path/)"
done < <(get_dir_prompts "$source")
if [[ "$found_files" == "false" ]]; then
log "Warning: RALPH_THEN directory has no numbered .md files: $source"
fi
elif [[ -f "$source" ]] || [[ -f "${source}.md" ]]; then
# Try with .md extension if not found
[[ ! -f "$source" ]] && source="${source}.md"
local next_num=$(get_next_sibling_slot "$current_main")
local dest_name="${next_num}-$(basename "$then_path")"
[[ ! "$dest_name" =~ \.md$ ]] && dest_name="${dest_name}.md"
cp "$source" "$PROMPTS_DIR/$dest_name"
log "Queued: $dest_name (from $then_path)"
else
log "Warning: RALPH_THEN target not found: $source"
fi
done <<< "$then_lines"
}
# Process RALPH_ADD directives - add prompts at bottom of queue
# New prompts become (max_main + 1).0.0, (max_main + 2).0.0, etc.
# If path is a directory: first file gets bottom slot, rest become siblings of first
# Example: RALPH_ADD: process/review/ with 1-lint.md, 2-format.md, 3-test.md
# If queue has 0.x and 1.x → 2.0.0-1-lint.md, 2.1.0-2-format.md, 2.2.0-3-test.md
process_add_directives() {
local response_text="$1"
# Extract RALPH_ADD lines (response_text already has real newlines)
local add_lines
add_lines=$(echo "$response_text" | grep "RALPH_ADD:" 2>/dev/null) || true
[[ -z "$add_lines" ]] && return 0
while IFS= read -r line; do
[[ -z "$line" ]] && continue
# Extract path after RALPH_ADD: (first non-space word only)
local add_path=$(echo "$line" | sed 's/.*RALPH_ADD:[[:space:]]*//' | awk '{print $1}')
[[ -z "$add_path" ]] && continue
local source="$PROMPTS_DIR/$add_path"
# Check if it's a directory
if [[ -d "$source" ]]; then
# Expand directory: first file gets bottom slot, rest become siblings
local first_main=""
local found_files=false
while IFS= read -r dir_file; do
[[ -z "$dir_file" ]] && continue
found_files=true
if [[ -z "$first_main" ]]; then
# First file: add at bottom of queue
local next_num=$(get_next_bottom_slot)
# Extract the main number for subsequent siblings
first_main=$(echo "$next_num" | cut -d'.' -f1)
local dest_name="${next_num}-$(basename "$dir_file")"
cp "$dir_file" "$PROMPTS_DIR/$dest_name"
log "Queued (bottom): $dest_name (from $add_path/)"
else
# Subsequent files: add as siblings of first
local next_num=$(get_next_sibling_slot "$first_main")
local dest_name="${next_num}-$(basename "$dir_file")"
cp "$dir_file" "$PROMPTS_DIR/$dest_name"
log "Queued (bottom): $dest_name (from $add_path/)"
fi
done < <(get_dir_prompts "$source")
if [[ "$found_files" == "false" ]]; then
log "Warning: RALPH_ADD directory has no numbered .md files: $source"
fi
elif [[ -f "$source" ]] || [[ -f "${source}.md" ]]; then
# Try with .md extension if not found
[[ ! -f "$source" ]] && source="${source}.md"
local next_num=$(get_next_bottom_slot)
local dest_name="${next_num}-$(basename "$add_path")"
[[ ! "$dest_name" =~ \.md$ ]] && dest_name="${dest_name}.md"
cp "$source" "$PROMPTS_DIR/$dest_name"
log "Queued (bottom): $dest_name (from $add_path)"
else
log "Warning: RALPH_ADD target not found: $source"
fi
done <<< "$add_lines"
}
# Check if prompt has RALPH_WAIT_USER
should_wait_user() {
local prompt_file="$1"
grep -q "RALPH_WAIT_USER" "$prompt_file" 2>/dev/null
}
# Move prompt to done folder
move_to_done() {
local prompt_file="$1"
local basename=$(basename "$prompt_file")
mv "$prompt_file" "$DONE_DIR/$basename"
log "✓ Completed: $basename → done/"
}
# Move prompt to failed folder
move_to_failed() {
local prompt_file="$1"
local basename=$(basename "$prompt_file")
mv "$prompt_file" "$FAILED_DIR/$basename"
log "✗ Failed: $basename → failed/"
}
# Clean up progress folder — preserve prompt log for debugging
cleanup_progress() {
local progress_path="$1"
local prompt_name=$(basename "$progress_path")
if [[ -d "$progress_path" ]]; then
# Move prompt log to session reports dir (alongside live progress logs)
if [[ -f "$progress_path/log.txt" ]] && [[ -s "$progress_path/log.txt" ]]; then
local dest="$RALPH_LOG_DIR/${prompt_name}_prompt.log"
mv "$progress_path/log.txt" "$dest"
fi
rm -rf "$progress_path"
fi
}
# Graceful shutdown handler
graceful_stop() {
if [[ "$STOPPING" == "false" ]]; then
STOPPING=true
echo ""
if [[ "$CLAUDE_RUNNING" == "true" ]]; then
log "Interrupt received - waiting for current Claude call to complete..."
log "(Press Ctrl+C again to force quit)"
else
log "Interrupt received - exiting..."
save_queue_stats
exit 130
fi
else
log "Force quitting..."
save_queue_stats
exit 1
fi
}
# Note: trap is set in main execution section below (after PID file handling)
# Token tracking - updated costs for Claude Sonnet
INPUT_COST_PER_MILLION=3.00
OUTPUT_COST_PER_MILLION=15.00
calculate_cost() {
local input=$1
local output=$2
echo "scale=6; ($input * $INPUT_COST_PER_MILLION / 1000000) + ($output * $OUTPUT_COST_PER_MILLION / 1000000)" | bc
}
extract_usage() {
local output="$1"
local input_tokens=0
local output_tokens=0
local cache_creation=0
local cache_read=0
# Extract base input tokens
if echo "$output" | grep -q '"input_tokens"'; then
input_tokens=$(echo "$output" | grep -o '"input_tokens"[[:space:]]*:[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*') || input_tokens=0
fi
# Extract cache creation tokens (prompt tokens written to cache)
if echo "$output" | grep -q '"cache_creation_input_tokens"'; then
cache_creation=$(echo "$output" | grep -o '"cache_creation_input_tokens"[[:space:]]*:[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*') || cache_creation=0
fi
# Extract cache read tokens (prompt tokens read from cache)
if echo "$output" | grep -q '"cache_read_input_tokens"'; then
cache_read=$(echo "$output" | grep -o '"cache_read_input_tokens"[[:space:]]*:[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*') || cache_read=0
fi
# Total input = base + cache_creation + cache_read
input_tokens=$((input_tokens + cache_creation + cache_read))
if echo "$output" | grep -q '"output_tokens"'; then
output_tokens=$(echo "$output" | grep -o '"output_tokens"[[:space:]]*:[[:space:]]*[0-9]*' | head -1 | grep -o '[0-9]*') || output_tokens=0
fi
echo "$input_tokens $output_tokens"
}
save_queue_stats() {
local total_elapsed=$(($(date +%s) - QUEUE_START))
cat > "$QUEUE_STATS" << EOF
{
"session": {
"timestamp": "$SESSION_TIMESTAMP",
"log_file": "$QUEUE_LOG"
},
"timing": {
"start_time": "$(date -d @$QUEUE_START '+%Y-%m-%d %H:%M:%S' 2>/dev/null || date -r $QUEUE_START '+%Y-%m-%d %H:%M:%S')",
"end_time": "$(date '+%Y-%m-%d %H:%M:%S')",
"total_seconds": $total_elapsed,
"total_formatted": "$(format_duration $total_elapsed)"
},
"prompts": {
"completed": $TOTAL_PROMPTS_COMPLETED,
"failed": $TOTAL_PROMPTS_FAILED,
"total": $((TOTAL_PROMPTS_COMPLETED + TOTAL_PROMPTS_FAILED))
},
"usage": {
"input_tokens": $TOTAL_INPUT_TOKENS,
"output_tokens": $TOTAL_OUTPUT_TOKENS,
"total_tokens": $((TOTAL_INPUT_TOKENS + TOTAL_OUTPUT_TOKENS))
},
"cost": {
"total_usd": $(format_cost $TOTAL_COST),
"budget_limit": "${BUDGET_LIMIT:-null}",
"warn_at": "${WARN_AT:-null}"
}
}
EOF
}
# Global to store the final response text for RALPH_THEN processing
LAST_RESPONSE_TEXT=""
# Run a single prompt through the orchestrator loop
run_prompt() {
local prompt_file="$1"
local prompt_name=$(basename "$prompt_file" .md)
local progress_path="$PROGRESS_DIR/$prompt_name"
local state_file="$progress_path/state.json"
local prompt_log="$progress_path/log.txt"
local stats_file="$progress_path/stats.json"
mkdir -p "$progress_path"
local iteration=0
local prompt_start=$(date +%s)
local total_input=0
local total_output=0
local completed=false
local consecutive_failures=0
local consecutive_zero_token=0
local max_consecutive_failures=5
LAST_RESPONSE_TEXT=""
# Reset stuck detection for new prompt
reset_stuck_detection
log "Processing: $(basename "$prompt_file")"
if [[ -f "$state_file" ]]; then
log "Resuming from existing state"
fi
# Live progress file for monitoring (prompts write to this)
# Located in session log dir with timestamp and prompt name
local prompt_timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
local live_progress_file="$RALPH_LOG_DIR/${prompt_timestamp}_${prompt_name}.log"
export RALPH_PROGRESS_FILE="$live_progress_file"
while [[ $iteration -lt $MAX_ITERATIONS && "$STOPPING" == "false" ]]; do
iteration=$((iteration + 1))
local iter_start=$(date +%s)
echo "--- Iteration $iteration ---" >> "$prompt_log"
# Clear live progress for new iteration
echo "[$(date +%H:%M:%S)] Iteration $iteration started" > "$live_progress_file"
CLAUDE_RUNNING=true
# Wrap claude call with timeout to prevent hangs
# Default 45 minutes (2700s) - some iterations with extended thinking take longer
local claude_timeout=${CLAUDE_TIMEOUT:-2700}
# How often to check if Claude completed (via live progress file) while waiting
local poll_interval=${CLAUDE_POLL_INTERVAL:-30}
# Grace period after detecting RALPH_COMPLETE — give CLI time to produce JSON output
local completion_grace=${CLAUDE_COMPLETION_GRACE:-180}
output=""
claude_exit_code=0
# Run claude in background so we can poll the live progress file.
# Without this, we block for the full timeout even when Claude finishes
# early but the CLI hangs/crashes on exit (Node.js unhandled promise rejection).
local output_file=$(mktemp)
bash -c 'cat "$1" | RALPH_SESSION=1 RALPH_PROGRESS_FILE="$2" claude -p --dangerously-skip-permissions --no-session-persistence --output-format json 2>&1' _ "$prompt_file" "$live_progress_file" > "$output_file" &
local claude_pid=$!
# Poll: wait for either (a) process exits, (b) timeout, or (c) RALPH_COMPLETE in progress file
# Use 1s sleep for fast process exit detection; check progress file every poll_interval seconds
local elapsed=0
local completion_detected=0
local completion_detected_at=0
local last_progress_check=0
while kill -0 "$claude_pid" 2>/dev/null; do
sleep 1
elapsed=$(($(date +%s) - iter_start))
# Hard timeout
if [[ $elapsed -ge $claude_timeout ]]; then
log " ⚠️ Claude timed out after ${claude_timeout}s — killing process"
kill "$claude_pid" 2>/dev/null
wait "$claude_pid" 2>/dev/null || true
pkill -P "$claude_pid" 2>/dev/null || true
claude_exit_code=124
break
fi
# Check progress file periodically (not every second — grep on file is heavier)
if [[ $completion_detected -eq 0 ]] && [[ $((elapsed - last_progress_check)) -ge $poll_interval ]]; then
last_progress_check=$elapsed
if [[ -f "$live_progress_file" ]] && grep -q "RALPH_COMPLETE" "$live_progress_file" 2>/dev/null; then
completion_detected=1
completion_detected_at=$elapsed
log " ℹ️ Live progress shows RALPH_COMPLETE at ${elapsed}s — waiting ${completion_grace}s grace period for CLI to exit cleanly"
fi
fi
# If we detected completion, give grace period then kill
if [[ $completion_detected -eq 1 ]] && [[ $((elapsed - completion_detected_at)) -ge $completion_grace ]]; then
log " ⚠️ CLI still running ${completion_grace}s after RALPH_COMPLETE — killing (likely CLI hang/crash)"
kill "$claude_pid" 2>/dev/null
wait "$claude_pid" 2>/dev/null || true
# Also kill any orphaned child processes (claude node process may survive bash wrapper kill)
pkill -P "$claude_pid" 2>/dev/null || true
claude_exit_code=1 # Treat as crash — recovery will use live progress file
break
fi
done
# Collect exit code if process already exited normally
if [[ $claude_exit_code -eq 0 ]]; then
claude_exit_code=0
wait "$claude_pid" 2>/dev/null || claude_exit_code=$?
fi
output=$(cat "$output_file")
rm -f "$output_file"
local iter_end=$(date +%s)
local iter_duration=$((iter_end - iter_start))
CLAUDE_RUNNING=false
# Append live progress to prompt log (for debugging)
if [[ -f "$live_progress_file" ]] && [[ -s "$live_progress_file" ]]; then
echo "--- Live Progress ---" >> "$prompt_log"
cat "$live_progress_file" >> "$prompt_log"
echo "--- End Live Progress ---" >> "$prompt_log"
fi
# Handle command failure (non-zero exit or crash)
if [[ $claude_exit_code -ne 0 ]] || [[ -z "$output" ]]; then
# Before counting as failure, check if Claude actually completed work.
# The CLI can crash (e.g., unhandled promise rejection) AFTER Claude finishes —
# the live progress file will show RALPH_COMPLETE even though the CLI exited non-zero.
if [[ -f "$live_progress_file" ]] && grep -q "RALPH_COMPLETE" "$live_progress_file" 2>/dev/null; then
log " ⚠️ CLI crashed (exit $claude_exit_code) but live progress shows RALPH_COMPLETE — recovering"
echo "CLI crash recovery (exit $claude_exit_code): live progress shows completion" >> "$prompt_log"
echo "Original output (first 200 chars): '$(echo "$output" | head -c 200)'" >> "$prompt_log"
response_text=$(cat "$live_progress_file")
echo "$response_text" >> "$prompt_log"
echo "" >> "$prompt_log"
log " Iteration $iteration: $(format_duration $iter_duration) | +0 in, +0 out (recovered) | Total cost: \$$(format_cost $TOTAL_COST)"
log " $response_text"
completed=true
LAST_RESPONSE_TEXT="$response_text"
consecutive_failures=0
break
fi
consecutive_failures=$((consecutive_failures + 1))
local failure_reason="exit code $claude_exit_code"
[[ -z "$output" ]] && failure_reason="empty output (possible crash)"
echo "Error: Claude command failed ($failure_reason) [failure $consecutive_failures/$max_consecutive_failures]" >> "$prompt_log"
echo "$output" >> "$prompt_log"
log " ⚠️ Claude failed ($failure_reason) - failure $consecutive_failures/$max_consecutive_failures"
# Check if too many consecutive failures
if [[ $consecutive_failures -ge $max_consecutive_failures ]]; then
log " ❌ Too many consecutive failures ($consecutive_failures) - moving prompt to failed"
echo "FATAL: $max_consecutive_failures consecutive failures, giving up" >> "$prompt_log"
break
fi
# Check if this was a rate limit error (only if response was fast)
if check_rate_limit "$output" "$iter_duration"; then
wait_for_rate_limit "$RATE_LIMIT_WAIT"
iteration=$((iteration - 1)) # Don't count this as an iteration
# Detect rapid empty failures: fast response + empty/no output = likely account limit
# Claude CLI exits immediately with nothing when subscription limit is hit
elif [[ $iter_duration -lt $RATE_LIMIT_TIME_THRESHOLD ]] && [[ -z "$output" || "$output" == "{}" ]]; then
consecutive_zero_token=$((consecutive_zero_token + 1))
log " ⚠️ Fast empty failure (${iter_duration}s) - likely account limit (occurrence $consecutive_zero_token/3)"
if [[ $consecutive_zero_token -ge 3 ]]; then
log " 🛑 Account/subscription limit reached (3 consecutive fast empty failures)"
send_notification "Ralph Limit Reached" "Account limit — waiting $(format_duration $RATE_LIMIT_WAIT)" "critical"
wait_for_rate_limit "$RATE_LIMIT_WAIT"
consecutive_zero_token=0
consecutive_failures=0 # Reset after waiting — limit may have reset
fi
iteration=$((iteration - 1)) # Don't count as real iteration
else
# Exponential backoff for crashes: 5s, 10s, 20s, max 60s
local crash_delay=$((5 * (1 << (consecutive_failures < 4 ? consecutive_failures - 1 : 3))))
[[ $crash_delay -gt 60 ]] && crash_delay=60
log " Waiting ${crash_delay}s before retry..."
sleep $crash_delay
fi
continue
fi
# Check for API errors that return exit code 0 but contain error messages
if echo "$output" | grep -qE "API Error:|\"type\":\"error\""; then
consecutive_failures=$((consecutive_failures + 1))
echo "API Error detected [failure $consecutive_failures/$max_consecutive_failures]" >> "$prompt_log"
echo "$output" >> "$prompt_log"
log " ⚠️ API error - failure $consecutive_failures/$max_consecutive_failures"
if [[ $consecutive_failures -ge $max_consecutive_failures ]]; then
log " ❌ Too many consecutive API errors ($consecutive_failures) - moving prompt to failed"
echo "FATAL: $max_consecutive_failures consecutive API errors, giving up" >> "$prompt_log"
break
fi
# Short delay before retry for API errors
sleep 2
continue
fi
# Reset consecutive failures on success
consecutive_failures=0
# Also check successful responses for rate limit messages (only if response was fast)
if check_rate_limit "$output" "$iter_duration"; then
echo "Rate limit detected in response (responded in ${iter_duration}s)" >> "$prompt_log"
wait_for_rate_limit "$RATE_LIMIT_WAIT"
iteration=$((iteration - 1)) # Don't count this as an iteration
continue
fi
read iter_input iter_output <<< $(extract_usage "$output")
# Detect zero-token responses: Claude returned exit 0 but produced no tokens.
# This can happen when:
# 1. Account/subscription limits are hit — CLI exits cleanly with empty response
# 2. CLI crashes with unhandled promise rejection AFTER Claude completed work —
# the JSON output is replaced by a Node.js error, so extract_usage finds no tokens,
# but the live progress file shows RALPH_COMPLETE because Claude did finish.
if [[ $iter_input -eq 0 && $iter_output -eq 0 ]]; then
# Before assuming rate limit, check if Claude actually completed work.
# The live progress file is written BY Claude during execution, so if it
# contains RALPH_COMPLETE, the work was done — the CLI just crashed on exit.
if [[ -f "$live_progress_file" ]] && grep -q "RALPH_COMPLETE" "$live_progress_file" 2>/dev/null; then
log " ⚠️ Zero-token response but live progress shows RALPH_COMPLETE — CLI crashed after completing work"
log " Recovering response from live progress file"
echo "CLI crash recovery: output had no tokens but live progress shows completion" >> "$prompt_log"
echo "Original output (first 200 chars): '$(echo "$output" | head -c 200)'" >> "$prompt_log"
# Reconstruct response_text from the live progress file so the
# RALPH_COMPLETE / RALPH_THEN / RALPH_ADD parsing works normally
response_text=$(cat "$live_progress_file")
echo "$response_text" >> "$prompt_log"
echo "" >> "$prompt_log"
log " Iteration $iteration: $(format_duration $iter_duration) | +0 in, +0 out (recovered) | Total cost: \$$(format_cost $TOTAL_COST)"
if echo "$response_text" | grep -q "RALPH_COMPLETE"; then
log " $response_text"
completed=true
LAST_RESPONSE_TEXT="$response_text"
break
fi
# If progress file had RALPH_PROGRESS instead, fall through to normal progress handling
continue
fi
consecutive_zero_token=$((consecutive_zero_token + 1))
log " ⚠️ Zero-token response (exit 0, no work done) - occurrence $consecutive_zero_token/3"
echo "Zero-token response [occurrence $consecutive_zero_token/3]: output='$(echo "$output" | head -c 200)'" >> "$prompt_log"
if [[ $consecutive_zero_token -ge 3 ]]; then
log " 🛑 Account/subscription limit likely reached (3 consecutive zero-token responses)"
log " Waiting $(format_duration $RATE_LIMIT_WAIT) for limit to reset..."
send_notification "Ralph Limit Reached" "3 consecutive zero-token responses — waiting $(format_duration $RATE_LIMIT_WAIT)" "critical"
wait_for_rate_limit "$RATE_LIMIT_WAIT"
consecutive_zero_token=0
fi
iteration=$((iteration - 1)) # Don't count as real iteration
continue
fi
consecutive_zero_token=0
total_input=$((total_input + iter_input))
total_output=$((total_output + iter_output))
# Update global totals
TOTAL_INPUT_TOKENS=$((TOTAL_INPUT_TOKENS + iter_input))
TOTAL_OUTPUT_TOKENS=$((TOTAL_OUTPUT_TOKENS + iter_output))
local iter_cost=$(calculate_cost $iter_input $iter_output)
TOTAL_COST=$(echo "$TOTAL_COST + $iter_cost" | bc)
# Extract result from JSON and convert \n to real newlines
# Use printf '%s' to preserve literal \n, then extract, then printf '%b' to convert
response_text=$(printf '%s' "$output" | sed 's/.*"result":"//;s/","[a-z_]*":.*//')
response_text=$(printf '%b' "$response_text")
echo "$response_text" >> "$prompt_log"
echo "" >> "$prompt_log"
log " Iteration $iteration: $(format_duration $iter_duration) | +$iter_input in, +$iter_output out | Total cost: \$$(format_cost $TOTAL_COST)"
# Check budget after each iteration
if ! check_budget; then
STOPPING=true
break
fi
if echo "$response_text" | grep -q "RALPH_COMPLETE"; then
# Log full response on completion so user sees RALPH_THEN/RALPH_ADD directives