Skip to content

fix: remaining grep -c || echo multiline output breaking pulse-wrapper arithmetic#2745

Merged
alex-solovyev merged 1 commit intomainfrom
bugfix/pulse-grep-c-multiline
Mar 3, 2026
Merged

fix: remaining grep -c || echo multiline output breaking pulse-wrapper arithmetic#2745
alex-solovyev merged 1 commit intomainfrom
bugfix/pulse-grep-c-multiline

Conversation

@alex-solovyev
Copy link
Collaborator

@alex-solovyev alex-solovyev commented Mar 3, 2026

Summary

  • Fixes 2 remaining instances of grep -c ... || echo "0" pattern in cleanup_worktrees() that were missed in bd3225e
  • This pattern produces multiline output ("0\n0") when grep finds no matches, breaking bash arithmetic and causing pulse-wrapper to crash every 2 minutes

Root Cause

grep -c 'pattern' || echo "0" — when grep finds 0 matches, it outputs 0 with exit code 1. The || echo "0" appends another 0, resulting in "0\n0". This value in $((...)) arithmetic triggers syntax error in expression.

Fix

Use count=$(grep -c ...) || count=0 — assignment captures grep's stdout; || handles the exit code separately without concatenation.

Closes the recurring error:

pulse-wrapper.sh: line 1311: 0
0: syntax error in expression (error token is "0")

Summary by CodeRabbit

  • Chores
    • Improved error handling and control flow in the worktree cleanup process.

…n pulse-wrapper

Two instances of `grep -c ... || echo "0"` were missed in bd3225e.
When grep -c finds zero matches, it outputs "0" with exit code 1,
triggering the || echo which appends another "0", producing "0\n0".
This multiline value breaks bash arithmetic: $(( ... + 0\n0 )).

Fix: use `count=$(grep -c ...) || count=0` pattern (assignment
captures grep output; || handles the exit code separately).
@gemini-code-assist
Copy link

Summary of Changes

Hello, 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 issue in the pulse-wrapper.sh script where an incorrect grep -c || echo "0" pattern was causing shell arithmetic errors. When grep -c found no matches, the pattern would produce a multiline string ("0\n0"), leading to syntax error in expression and script crashes. The fix involves modifying how the count from grep -c is captured, ensuring that the || operator correctly handles the exit code without concatenating output, thereby stabilizing the script's cleanup operations.

Highlights

  • Fix grep -c output: Corrected two remaining instances of grep -c ... || echo "0" in cleanup_worktrees() to prevent multiline output when no matches are found.
  • Prevent arithmetic errors: Eliminated syntax error in expression crashes in pulse-wrapper caused by grep -c producing "0\n0" when no matches were found, which broke shell arithmetic.
Changelog
  • .agents/scripts/pulse-wrapper.sh
    • Modified the count assignment to count=$(echo "$clean_result" | grep -c 'Removing') || count=0 to correctly handle grep -c output and exit codes.
    • Introduced fallback_count and applied the same fix for the fallback cleanup logic to prevent multiline output from grep -c.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added the bug Auto-created from TODO.md tag label Mar 3, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ab4a16e and 2e1ad7b.

📒 Files selected for processing (1)
  • .agents/scripts/pulse-wrapper.sh

Walkthrough

This PR refactors error handling in the cleanup_worktrees function within a shell script, improving count assignment logic when grep operations fail. It introduces explicit fallback pathways for legacy branches with conditional logging based on actual removal counts rather than combining grep results.

Changes

Cohort / File(s) Summary
Cleanup worktree count handling
.agents/scripts/pulse-wrapper.sh
Refactored removal-count reporting in cleanup_worktrees to avoid fallback to literal "0" on grep failures; added explicit fallback_count computation in legacy path with conditional logging only when count is positive, separating standard output parsing from logging side-effects.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • marcusquinn

Poem

🔧 When grep falls through and counts go wrong,
A fallback path sings cleanup's song,
Logic flows clear, explicit and bright,
Error handling polished to A-grade height. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing a grep -c || echo pattern that causes multiline output breaking arithmetic in pulse-wrapper.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/pulse-grep-c-multiline

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.

@github-actions
Copy link

github-actions bot commented Mar 3, 2026

🔍 Code Quality Report

�[0;35m[MONITOR]�[0m Code Review Monitoring Report

�[0;34m[INFO]�[0m Latest Quality Status:
SonarCloud: 0 bugs, 0 vulnerabilities, 113 code smells

�[0;34m[INFO]�[0m Recent monitoring activity:
Tue Mar 3 03:20:13 UTC 2026: Code review monitoring started
Tue Mar 3 03:20:13 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 113

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 113
  • VULNERABILITIES: 0

Generated on: Tue Mar 3 03:20:16 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 3, 2026

@alex-solovyev alex-solovyev merged commit fee96f7 into main Mar 3, 2026
26 of 27 checks passed
@marcusquinn marcusquinn deleted the bugfix/pulse-grep-c-multiline branch March 3, 2026 03:23
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a bug where multiline output from a grep ... || echo pattern caused arithmetic errors in bash. The fix, which correctly separates the command's output assignment from its exit code handling, is applied consistently. The changes also include a beneficial refactoring that improves efficiency by replacing two grep calls with a single one. I've added a couple of minor suggestions to further improve efficiency by using here-strings, which avoid creating unnecessary subshells.


local count
count=$(echo "$clean_result" | grep -c 'Removing' || echo "0")
count=$(echo "$clean_result" | grep -c 'Removing') || count=0

Choose a reason for hiding this comment

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

medium

This is a good fix. For a minor efficiency improvement, you can use a here-string (<<<) instead of piping from echo. This avoids creating an extra process and subshell.

Suggested change
count=$(echo "$clean_result" | grep -c 'Removing') || count=0
count=$(grep -c 'Removing' <<< "$clean_result") || count=0

if echo "$cleaned_output" | grep -q "Removing\|removed"; then
echo "[pulse-wrapper] Worktree cleanup: $(echo "$cleaned_output" | grep -c 'Removing') worktree(s) removed" >>"$LOGFILE"
local fallback_count
fallback_count=$(echo "$cleaned_output" | grep -c 'Removing') || fallback_count=0

Choose a reason for hiding this comment

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

medium

This refactoring is a good improvement as it avoids running grep twice. Similar to the previous comment, you can use a here-string (<<<) here for a minor efficiency gain by avoiding the echo subshell.

Suggested change
fallback_count=$(echo "$cleaned_output" | grep -c 'Removing') || fallback_count=0
fallback_count=$(grep -c 'Removing' <<< "$cleaned_output") || fallback_count=0

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

Labels

bug Auto-created from TODO.md tag

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant