fix(ralph-loop): resolve unbound variable and use opencode run#104
Conversation
- 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
WalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧹 Recent nitpick comments
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (1).agent/scripts/*.sh⚙️ CodeRabbit configuration file
Files:
🧬 Code graph analysis (1).agent/scripts/ralph-loop-helper.sh (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
🔇 Additional comments (4)
✏️ Tip: You can disable this entire section by setting Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @marcusquinn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical runtime error in the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
🔍 Code Quality Report�[0;35m[MONITOR]�[0m Code Review Monitoring Report �[0;34m[INFO]�[0m Latest Quality Status: �[0;34m[INFO]�[0m Recent monitoring activity: 📈 Current Quality Metrics
Generated on: Thu Jan 15 01:24:04 UTC 2026 Generated by AI DevOps Framework Code Review Monitoring |
There was a problem hiding this comment.
Code Review
This pull request effectively resolves a critical unbound variable crash in the ralph-loop by correctly scoping the output_file variable. The changes also modernize the opencode invocation and improve debugging by showing tool output on failure, which are great enhancements. The implementation is clean, especially the safe handling of command arguments. I have one suggestion to further improve the robustness of the temporary file cleanup logic.
| output_file=$(mktemp) | ||
| trap 'rm -f "$output_file"' EXIT | ||
| output_file="$(mktemp)" | ||
| trap 'rm -f "${output_file:-}"' RETURN |
There was a problem hiding this comment.
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.
| trap 'rm -f "${output_file:-}"' RETURN | |
| trap 'rm -f "${output_file:-}"' EXIT |
🤖 Augment PR SummarySummary: Fixes the ralph loop’s temp-file cleanup bug (unbound 🤖 Was this summary useful? React with 👍 or 👎 |
| readonly NC='\033[0m' | ||
|
|
||
| # Output file for tool capture (shared with EXIT trap) | ||
| output_file="" |
There was a problem hiding this comment.
| output_file=$(mktemp) | ||
| trap 'rm -f "$output_file"' EXIT | ||
| output_file="$(mktemp)" | ||
| trap 'rm -f "${output_file:-}"' RETURN |
There was a problem hiding this comment.
…se args (GH#3816) Address remaining unactioned CodeRabbit review feedback from PR #14: - ralph-loop-helper.sh run_v2_loop: validate --max-iterations is a positive integer, --completion-promise is non-empty, --max-attempts is a positive integer. Previously these accepted any value silently. - ralph-loop-helper.sh setup_loop: add matching validation for --max-iterations and --completion-promise for consistency. - quality-loop-helper.sh: add --max-iterations validation to preflight_loop, pr_review_loop, and postflight_loop. All now reject non-positive-integer values with a clear error message instead of silently accepting them. Other PR #14 findings (aider duplicate prompt, || true hiding failures, missing tool existence check, invalid --monitor-duration, relative git_common_dir) were already fixed in subsequent PRs #35, #104, #111, #396, #480.



Summary
output_file: unbound variableerror in ralph-loop-helper.sh EXIT trapopencode --printwithopencode run --format jsonRALPH_MODELenvironment variable support for model selectionChanges
The ralph loop was crashing with
line 786: output_file: unbound variablebecause:output_filewas declared aslocalinsiderun_v2_loop()functiontrap ... EXITfires at script exit, after the function scope endsset -u, referencing the now-unset variable caused the crashFix: Move
output_fileto script scope (line 72) so it persists for the EXIT trap.Also updated the opencode invocation from the deprecated
--printflag toopencode run --format json, and added debugging output when the tool fails.Testing
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.