-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathralph-loop.sh
More file actions
executable file
·411 lines (336 loc) · 12.4 KB
/
ralph-loop.sh
File metadata and controls
executable file
·411 lines (336 loc) · 12.4 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
#!/usr/bin/env bash
# ralph-loop.sh - Autonomous test coverage improvement powered by Claude Code CLI
#
# Claude uses Edit/Write/Bash tools directly to modify code and run tests.
# Supports multiple languages via adapter plugins.
set -euo pipefail
# ====== Script Directory ======
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ====== Configuration Defaults ======
LANGUAGE="${LANGUAGE:-python}"
TARGET_COVERAGE="${TARGET_COVERAGE:-100}"
MAX_ITERATIONS="${MAX_ITERATIONS:-25}"
PROMPT_FILE="${PROMPT_FILE:-}"
COVERAGE_PATTERN="${COVERAGE_PATTERN:-}"
TEST_COMMAND="${TEST_COMMAND:-}"
COVERAGE_COMMAND="${COVERAGE_COMMAND:-}"
# Claude Code CLI
CLAUDE_CLI_CMD="${CLAUDE_CLI_CMD:-claude}"
CLAUDE_CLI_FIXED_FLAGS=(--dangerously-skip-permissions --permission-mode bypassPermissions --print --verbose)
CLAUDE_TIMEOUT="${CLAUDE_TIMEOUT:-3600}"
# Convert CLAUDE_CLI_ARGS string to array (handles spaces properly)
CLAUDE_CLI_ARGS_ARRAY=()
if [[ -n "${CLAUDE_CLI_ARGS:-}" ]]; then
read -ra CLAUDE_CLI_ARGS_ARRAY <<< "$CLAUDE_CLI_ARGS"
fi
# Working directory
WORK_DIR="${WORK_DIR:-.ralph_loop_work}"
# Git commit automation
GIT_COMMIT="${GIT_COMMIT:-false}"
GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-Claude}"
GIT_AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-noreply@anthropic.com}"
# Artifact management
CLEANUP_WORK_DIR="${CLEANUP_WORK_DIR:-false}"
# Prompt output limits
STDOUT_TAIL_LINES="${STDOUT_TAIL_LINES:-1000}"
STDERR_TAIL_LINES="${STDERR_TAIL_LINES:-200}"
# ====== Utilities ======
die() { echo "[ERROR] $*" >&2; exit 1; }
validate_numeric() {
local name="$1" value="$2"
[[ "$value" =~ ^[0-9]+(\.[0-9]+)?$ ]] || die "Invalid $name: '$value' (must be numeric)"
}
check_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Command not found: '$1'. Check PATH or installation."
}
timestamp() { date +"%Y-%m-%d_%H-%M-%S"; }
# ====== YAML Config Parser ======
# Simple YAML parser - extracts key: value pairs
parse_yaml_config() {
local config_file="$1"
if [[ ! -f "$config_file" ]]; then
return 0
fi
echo "[CONFIG] Loading configuration from: $config_file"
while IFS=':' read -r key value || [[ -n "$key" ]]; do
# Skip comments and empty lines
[[ "$key" =~ ^[[:space:]]*# ]] && continue
[[ -z "$key" ]] && continue
# Remove leading/trailing whitespace and quotes
key="$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
value="$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/^"//;s/"$//;s/^'"'"'//;s/'"'"'$//')"
case "$key" in
language)
[[ -z "$LANGUAGE" || "$LANGUAGE" == "python" ]] && LANGUAGE="$value"
;;
target_coverage)
[[ -z "$TARGET_COVERAGE" || "$TARGET_COVERAGE" == "100" ]] && TARGET_COVERAGE="$value"
;;
max_iterations)
[[ -z "$MAX_ITERATIONS" || "$MAX_ITERATIONS" == "25" ]] && MAX_ITERATIONS="$value"
;;
prompt_file)
[[ -z "$PROMPT_FILE" ]] && PROMPT_FILE="$value"
;;
coverage_pattern)
[[ -z "$COVERAGE_PATTERN" ]] && COVERAGE_PATTERN="$value"
;;
test_command)
[[ -z "$TEST_COMMAND" ]] && TEST_COMMAND="$value"
;;
coverage_command)
[[ -z "$COVERAGE_COMMAND" ]] && COVERAGE_COMMAND="$value"
;;
esac
done < "$config_file"
}
# ====== Load Configuration ======
# Priority: Environment variables > YAML config > Adapter defaults
# Check for config file
CONFIG_FILE="${CONFIG_FILE:-.ralph-loop.yaml}"
if [[ -f "$CONFIG_FILE" ]]; then
parse_yaml_config "$CONFIG_FILE"
elif [[ -f ".ralph-loop.yml" ]]; then
parse_yaml_config ".ralph-loop.yml"
fi
# ====== Load Language Adapter ======
ADAPTER_FILE="$SCRIPT_DIR/adapters/${LANGUAGE}.sh"
if [[ ! -f "$ADAPTER_FILE" ]]; then
die "Language adapter not found: $ADAPTER_FILE. Supported: python, javascript, go"
fi
# shellcheck source=/dev/null
source "$ADAPTER_FILE"
# Validate required adapter functions exist
for func in adapter_apply_defaults adapter_run_tests adapter_measure_coverage; do
type "$func" &>/dev/null || die "Adapter missing required function: $func"
done
# Apply adapter defaults for unset values
adapter_apply_defaults
# Validate numeric configuration values
validate_numeric "TARGET_COVERAGE" "$TARGET_COVERAGE"
validate_numeric "MAX_ITERATIONS" "$MAX_ITERATIONS"
# ====== Dependency Checks ======
check_cmd grep
check_cmd awk
check_cmd bc
check_cmd tee
check_cmd tar
check_cmd gzip
check_cmd "$CLAUDE_CLI_CMD"
# Create work directory
mkdir -p "$WORK_DIR"
# ====== Prompt Management ======
get_prompt_content() {
if [[ -n "$PROMPT_FILE" && -f "$PROMPT_FILE" ]]; then
cat "$PROMPT_FILE"
elif [[ -f "$SCRIPT_DIR/prompts/default.txt" ]]; then
cat "$SCRIPT_DIR/prompts/default.txt"
else
# Embedded fallback prompt
cat << 'EMBEDDED_PROMPT'
# Test Coverage Improvement Task
You are an expert software developer improving test coverage.
## Your Goal
Increase test coverage by writing comprehensive tests for uncovered code.
## Guidelines
1. Focus on the most impactful uncovered code first
2. Write meaningful tests that verify actual behavior
3. Use appropriate mocking for external dependencies
4. Follow existing code style and patterns
5. Ensure all new tests pass
## Actions
- Use the Edit tool to modify existing test files
- Use the Write tool to create new test files
- Use the Bash tool to run tests and verify changes
- Do NOT output code blocks - use tools directly
EMBEDDED_PROMPT
fi
}
# ====== Core Functions ======
run_tests() {
local iter_dir="$1"
echo "[RUN] Running tests..."
adapter_run_tests "$iter_dir"
}
measure_coverage() {
local iter_dir="$1"
echo "[RUN] Measuring coverage..."
adapter_measure_coverage "$iter_dir"
local pct
pct="$(cat "$iter_dir/coverage.pct" 2>/dev/null || echo "0")"
echo "[INFO] Current coverage: ${pct}%"
}
build_prompt_for_claude() {
local iter="$1"
local iter_dir="$2"
local exit_code
exit_code="$(cat "$iter_dir/test.exitcode" 2>/dev/null || echo "N/A")"
local cov_pct
cov_pct="$(cat "$iter_dir/coverage.pct" 2>/dev/null || echo "N/A")"
{
get_prompt_content
echo
echo "===== Tool Usage Instructions ====="
echo "1) Use the Edit tool to directly modify test files."
echo "2) Use the Write tool if you need to create new files."
echo "3) After modifications, use the Bash tool to run tests and verify results."
echo "4) Do NOT output code blocks in text - use tools directly."
echo
echo "===== Current Status ====="
echo "- Iteration: $iter"
echo "- Test execution result (exitCode=$exit_code)"
echo "--- Test stdout (last $STDOUT_TAIL_LINES lines) ---"
tail -n "$STDOUT_TAIL_LINES" "$iter_dir/test.stdout.txt" 2>/dev/null || echo "(empty)"
echo "--- Test stderr (last $STDERR_TAIL_LINES lines) ---"
tail -n "$STDERR_TAIL_LINES" "$iter_dir/test.stderr.txt" 2>/dev/null || echo "(empty)"
echo
echo "Current coverage: ${cov_pct}%"
echo
if [[ "$cov_pct" =~ ^[0-9]+(\.[0-9]+)?$ ]] && (( $(echo "$cov_pct >= $TARGET_COVERAGE" | bc -l) )); then
echo "Next action: Target (${TARGET_COVERAGE}%) reached! Perform final review: check for flaky tests, remove unnecessary mocks, add edge cases."
else
echo "Next action: Target (${TARGET_COVERAGE}%) not reached. Prioritize improving tests for failing tests and uncovered lines."
fi
} >"$iter_dir/prompt.combined.txt"
}
call_claude_cli() {
local iter_dir="$1"
echo "[CALL] Claude Code CLI: $CLAUDE_CLI_CMD ${CLAUDE_CLI_ARGS_ARRAY[*]:-} ${CLAUDE_CLI_FIXED_FLAGS[*]} (timeout: ${CLAUDE_TIMEOUT}s)"
set +e
if command -v timeout >/dev/null 2>&1; then
timeout "$CLAUDE_TIMEOUT" "$CLAUDE_CLI_CMD" "${CLAUDE_CLI_ARGS_ARRAY[@]:-}" "${CLAUDE_CLI_FIXED_FLAGS[@]}" <"$iter_dir/prompt.combined.txt" 2>&1 | tee "$iter_dir/claude.log"
else
"$CLAUDE_CLI_CMD" "${CLAUDE_CLI_ARGS_ARRAY[@]:-}" "${CLAUDE_CLI_FIXED_FLAGS[@]}" <"$iter_dir/prompt.combined.txt" 2>&1 | tee "$iter_dir/claude.log"
fi
local code=$?
set -e
if [[ $code -eq 124 ]]; then
echo "[WARN] Claude Code CLI timed out after ${CLAUDE_TIMEOUT}s."
elif [[ $code -ne 0 ]]; then
echo "[WARN] Claude Code CLI exited with non-zero status (exit=$code)."
fi
}
# ====== Git Commit (Optional) ======
maybe_git_commit() {
local ts
ts="$(timestamp)"
if [[ "$GIT_COMMIT" != "true" ]]; then
return 0
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "[GIT] Not inside a git working tree. Skipping commit."
return 0
fi
if [[ -z "$(git status --porcelain)" ]]; then
echo "[GIT] No changes to commit. Skipping."
return 0
fi
git add -A || true
GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
git commit -m "chore(test): apply Claude suggestions (auto-applied at $ts)" || true
echo "[GIT] Changes committed successfully."
}
# ====== Artifact Archival ======
archive_iteration_artifacts() {
local iteration="$1"
local iter_dir="$2"
local final_ts
final_ts="$(timestamp)"
local archive_name="$WORK_DIR/ralph-loop-iter${iteration}-${final_ts}.tar.gz"
echo "[ARCHIVE] Archiving iteration $iteration artifacts: $archive_name"
if [[ -d "$iter_dir" ]]; then
tar -czf "$archive_name" -C "$WORK_DIR" "iter-$iteration" || {
echo "[WARN] Failed to archive iteration $iteration artifacts."
return 1
}
# Verify archive integrity
if ! tar -tzf "$archive_name" >/dev/null 2>&1; then
echo "[WARN] Archive verification failed: $archive_name may be corrupt"
fi
local archive_size
archive_size="$(du -h "$archive_name" 2>/dev/null | cut -f1 || echo "unknown")"
echo "[INFO] Iteration $iteration archived: $archive_name (size: $archive_size)"
if [[ "${CLEANUP_WORK_DIR:-false}" == "true" ]]; then
echo "[CLEANUP] Removing iteration $iteration directory: $iter_dir"
rm -rf "$iter_dir"
fi
else
echo "[WARN] Iteration directory not found: $iter_dir"
fi
}
archive_final_artifacts() {
local final_ts
final_ts="$(timestamp)"
local archive_name="ralph-loop-final-${final_ts}.tar.gz"
echo "[ARCHIVE] Creating final archive: $archive_name"
if [[ -d "$WORK_DIR" ]]; then
tar -czf "$archive_name" -C "." "$WORK_DIR" || {
echo "[WARN] Failed to create final archive."
return 1
}
# Verify archive integrity
if ! tar -tzf "$archive_name" >/dev/null 2>&1; then
echo "[WARN] Archive verification failed: $archive_name may be corrupt"
fi
local archive_size
archive_size="$(du -h "$archive_name" 2>/dev/null | cut -f1 || echo "unknown")"
echo "[INFO] Final archive created: $archive_name (size: $archive_size)"
if [[ "${CLEANUP_WORK_DIR:-false}" == "true" ]]; then
echo "[CLEANUP] Removing work directory: $WORK_DIR"
rm -rf "$WORK_DIR"
else
echo "[INFO] Work directory retained: $WORK_DIR"
echo "[INFO] Set CLEANUP_WORK_DIR=true to remove after archiving."
fi
else
echo "[WARN] Work directory not found: $WORK_DIR"
fi
}
# ====== Interrupt Handler ======
cleanup_on_interrupt() {
echo
echo "[INTERRUPT] Received termination signal. Cleaning up..."
archive_final_artifacts
exit 130
}
trap cleanup_on_interrupt INT TERM
# ====== Main Loop ======
echo "[START] Ralph Loop - Test Coverage Improvement (Claude Tool-First Mode)"
echo "[INFO] Language: $LANGUAGE"
echo "[INFO] Target coverage: ${TARGET_COVERAGE}%"
echo "[INFO] Max iterations: ${MAX_ITERATIONS}"
echo "[INFO] Coverage pattern: ${COVERAGE_PATTERN:-<all>}"
iteration=0
while :; do
iteration=$((iteration + 1))
iter_dir="$WORK_DIR/iter-$iteration"
mkdir -p "$iter_dir"
echo
echo "================= ITERATION $iteration ================="
# 1) Run tests
run_tests "$iter_dir"
# 2) Measure coverage
measure_coverage "$iter_dir"
cov="$(cat "$iter_dir/coverage.pct" 2>/dev/null || echo "0")"
# 3) Build prompt for Claude
build_prompt_for_claude "$iteration" "$iter_dir"
# 4) Call Claude Code CLI
call_claude_cli "$iter_dir"
# 5) Git commit (optional)
maybe_git_commit
# 6) Archive iteration artifacts
archive_iteration_artifacts "$iteration" "$iter_dir"
# 7) Check if target reached
if [[ "$cov" =~ ^[0-9]+(\.[0-9]+)?$ ]] && (( $(echo "$cov >= $TARGET_COVERAGE" | bc -l) )); then
echo "[SUCCESS] Coverage ${cov}% reached target. Loop complete."
break
fi
if (( iteration >= MAX_ITERATIONS )); then
echo "[STOP] Max iterations (${MAX_ITERATIONS}) reached. Current coverage: ${cov}%"
break
fi
echo "[CONTINUE] Coverage below ${TARGET_COVERAGE}%. Proceeding to next iteration."
done
# Create final archive
archive_final_artifacts
echo "[END] Ralph Loop completed."