Skip to content

fix: prevent arithmetic error when fix_plan.md has no matching checkboxes#260

Open
fengwuqingchen wants to merge 1 commit intofrankbria:mainfrom
fengwuqingchen:fix/grep-c-arithmetic-error
Open

fix: prevent arithmetic error when fix_plan.md has no matching checkboxes#260
fengwuqingchen wants to merge 1 commit intofrankbria:mainfrom
fengwuqingchen:fix/grep-c-arithmetic-error

Conversation

@fengwuqingchen
Copy link
Copy Markdown

@fengwuqingchen fengwuqingchen commented Apr 28, 2026

Summary

should_exit_gracefully in ralph_loop.sh counts checkbox items in fix_plan.md to decide whether all tasks are done. The current form fails on macOS:

local uncompleted_items=$(grep -cE "..." "$file" 2>/dev/null || echo "0")
local completed_items=$(grep -cE "..." "$file" 2>/dev/null || echo "0")
local total_items=$((uncompleted_items + completed_items))

Root cause

BSD grep (macOS default) both prints "0" to stdout and exits 1 when there are no matches. The || echo "0" fallback therefore appends a second "0", and the variable becomes a multi-line "0\n0" string. The next line:

local total_items=$((uncompleted_items + completed_items))

then crashes with:

ralph_loop.sh: line 716: 0
0: syntax error in expression (error token is "0")

Impact

  • Silently disables the "all fix_plan.md items completed" graceful-exit branch — the function falls through and the loop never auto-stops on a fully checked plan.
  • Spams the loop log with the syntax error in expression message every iteration.
  • Reproduces consistently on macOS (BSD grep). GNU grep may not hit it because GNU grep -c exits 0 on zero matches, so the || fallback never runs.

Fix

Pipe grep -c through head -n1. The pipeline's exit code becomes head's (always 0), so the brittle || fallback is no longer needed. BSD grep already prints "0" on no match, so the count line is always available. A defensive ${var:-0} covers the edge case of grep producing no output at all.

Test plan

  • bash -n ralph_loop.sh — syntax OK
  • Smoke test on macOS BSD grep:
    • empty fix_plan.mdu=0 c=0 total=0 (no error)
    • all-done plan (2 [x], 0 [ ]) → u=0 c=2 total=2 and triggers the plan_complete branch
    • mixed plan → counts correctly
  • Re-run a real ralph loop on macOS and confirm the syntax error in expression line no longer appears in .ralph/logs/

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed checkbox completion counting to work reliably on macOS and BSD systems, ensuring proper script execution and completion tracking.

…oxes

`should_exit_gracefully` counts checkbox items in fix_plan.md to decide
whether all tasks are done. The previous form

    local uncompleted_items=$(grep -cE "..." "$file" 2>/dev/null || echo "0")

breaks under BSD grep (macOS): when there are no matches, BSD grep
*both* prints "0" to stdout *and* exits 1. The `|| echo "0"` fallback
then appends a second "0", so the variable becomes a multi-line
"0\n0" string. The very next line

    local total_items=$((uncompleted_items + completed_items))

then fails with:

    syntax error in expression (error token is "0")

This silently disables the "all fix_plan.md items completed" exit
condition (the function falls through), but more visibly it spams
the loop log every iteration.

Pipe `grep -c` through `head -n1` instead. The pipeline's exit code
becomes head's (always 0), and BSD grep already prints "0" on no
match, so the count line is always available. Defensive `${var:-0}`
covers the edge case of grep producing no output at all (e.g. file
disappears mid-read).
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2a3fab35-d68d-4336-83ff-474951c9b543

📥 Commits

Reviewing files that changed from the base of the PR and between 24b8969 and 5b0a238.

📒 Files selected for processing (1)
  • ralph_loop.sh

Walkthrough

Modified ralph_loop.sh's should_exit_gracefully function to handle grep output robustly on BSD/macOS systems. Changed checkbox completion-counting logic to pipe grep results through head -n1 for single numeric output, preventing double-zero issues, and ensured count variables default to zero.

Changes

Cohort / File(s) Summary
Grep Output Robustness
ralph_loop.sh
Fixed checkbox completion-counting logic in should_exit_gracefully to handle BSD/macOS grep behavior. Replaced grep -c ... || echo "0" pattern with piping through head -n1 to ensure single numeric output, and added explicit zero defaults for count variables to prevent arithmetic errors.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A grep that misbehaves on BSD's land,
Now tamed by head and careful hand,
With zeros set and output clear,
Our checkboxes have naught to fear! ✓

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main fix: preventing arithmetic errors when fix_plan.md has no matching checkboxes, which is the primary bug being addressed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant