From bdbf84e2742754d69f02ba492f6cce9cf0ac10ef Mon Sep 17 00:00:00 2001
From: marcusquinn <6428977+marcusquinn@users.noreply.github.com>
Date: Thu, 15 Jan 2026 01:22:41 +0000
Subject: [PATCH] fix(ralph-loop): resolve unbound variable and use opencode
run
- Move output_file to script scope to fix EXIT trap unbound variable error
- Replace deprecated 'opencode --print' with 'opencode run --format json'
- Add RALPH_MODEL env var support for model selection
- Print last 20 lines of tool output on failure for debugging
---
.agent/scripts/ralph-loop-helper.sh | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/.agent/scripts/ralph-loop-helper.sh b/.agent/scripts/ralph-loop-helper.sh
index a709ae09ec..2deb5d50b9 100755
--- a/.agent/scripts/ralph-loop-helper.sh
+++ b/.agent/scripts/ralph-loop-helper.sh
@@ -69,6 +69,9 @@ readonly CYAN='\033[0;36m'
readonly BOLD='\033[1m'
readonly NC='\033[0m'
+# Output file for tool capture (shared with EXIT trap)
+output_file=""
+
# =============================================================================
# Helper Functions
# =============================================================================
@@ -258,9 +261,8 @@ run_v2_loop() {
echo ""
local iteration=1
- local output_file
- output_file=$(mktemp)
- trap 'rm -f "$output_file"' EXIT
+ output_file="$(mktemp)"
+ trap 'rm -f "${output_file:-}"' RETURN
while [[ $iteration -le $max_iterations ]]; do
print_step "=== Iteration $iteration/$max_iterations ==="
@@ -291,7 +293,11 @@ To complete, output: $completion_promise (ONLY when TRUE)"
case "$tool" in
opencode)
- echo "$reanchor_prompt" | opencode --print > "$output_file" 2>&1 || exit_code=$?
+ local opencode_args=("run" "$reanchor_prompt" "--format" "json")
+ if [[ -n "${RALPH_MODEL:-}" ]]; then
+ opencode_args+=("--model" "$RALPH_MODEL")
+ fi
+ opencode "${opencode_args[@]}" > "$output_file" 2>&1 || exit_code=$?
;;
claude)
echo "$reanchor_prompt" | claude --print > "$output_file" 2>&1 || exit_code=$?
@@ -307,10 +313,15 @@ To complete, output: $completion_promise (ONLY when TRUE)"
if [[ $exit_code -ne 0 ]]; then
print_warning "Tool exited with code $exit_code (continuing)"
+ if [[ -s "$output_file" ]]; then
+ print_warning "Tool output (last 20 lines):"
+ tail -n 20 "$output_file"
+ fi
fi
# Check for completion promise
if grep -q "$completion_promise" "$output_file" 2>/dev/null; then
+ # opencode run emits JSON events; grep still works on raw output
print_success "Completion promise detected!"
# Create success receipt