Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .agent/scripts/ralph-loop-helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says this is “shared with EXIT trap”, but in this diff the cleanup trap was changed to RETURN. If there’s no longer an EXIT trap using output_file, this comment is now misleading and could confuse future maintenance.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎


# =============================================================================
# Helper Functions
# =============================================================================
Expand Down Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using a RETURN trap for cleanup can lead to leaked temporary files if the script exits unexpectedly (e.g., due to a signal like SIGINT/Ctrl-C, or a set -e exit) before the function returns. An EXIT trap is more robust as it fires regardless of how the script terminates.

Since output_file is now a global variable, using an EXIT trap is now safe from the original unbound variable issue and will ensure the temp file is always cleaned up, which is safer for resource management.

Suggested change
trap 'rm -f "${output_file:-}"' RETURN
trap 'rm -f "${output_file:-}"' EXIT

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth double-checking that using a RETURN trap here matches the intended cleanup semantics: it won’t run on abrupt script exit (and can also persist beyond run_v2_loop() unless explicitly cleared), so the temp file may not always be removed when expected.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎


while [[ $iteration -le $max_iterations ]]; do
print_step "=== Iteration $iteration/$max_iterations ==="
Expand Down Expand Up @@ -291,7 +293,11 @@ To complete, output: <promise>$completion_promise</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=$?
Expand All @@ -307,10 +313,15 @@ To complete, output: <promise>$completion_promise</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 "<promise>$completion_promise</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
Expand Down
Loading