Skip to content

fix: session-distill-helper.sh jq error on grep -c zero matches#2791

Merged
marcusquinn merged 1 commit intomainfrom
bugfix/session-distill-jq-zero-matches
Mar 3, 2026
Merged

fix: session-distill-helper.sh jq error on grep -c zero matches#2791
marcusquinn merged 1 commit intomainfrom
bugfix/session-distill-jq-zero-matches

Conversation

@marcusquinn
Copy link
Owner

@marcusquinn marcusquinn commented Mar 3, 2026

Summary

  • grep -c exits non-zero when zero matches are found, causing || echo "0" to append a second 0 to the captured output — producing "0\n0" which is not valid JSON and fails jq --argjson
  • Replaced || echo "0" with || true on both affected lines, then added an empty-string guard to default to 0
  • Affects error_fixes (line 68) and completed_tasks (line 73) in analyze_session()

Verification

ShellCheck passes (only pre-existing SC1091 info-level finding for sourced script).

Closes #2790

Summary by CodeRabbit

  • Chores
    • Improved error handling and robustness in internal session analysis scripts to ensure reliable result counting in all scenarios.

grep -c exits non-zero when no matches found, causing '|| echo 0' to
append a second '0' to the captured output. The resulting '0\n0' string
is not valid JSON and fails jq --argjson.

Fix: replace '|| echo 0' with '|| true' to suppress the non-zero exit
without appending duplicate output, then guard with an empty-check.

Fixes #2790
@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 subtle bug in the session-distill-helper.sh script where grep -c's behavior with zero matches led to malformed JSON, causing subsequent jq operations to fail. The fix ensures robust handling of count variables by correctly defaulting them to zero when no matches are found, thereby stabilizing the script's data processing.

Highlights

  • Fix jq error with grep -c zero matches: Resolved an issue where grep -c exiting non-zero for zero matches would cause jq --argjson to fail due to invalid JSON output (e.g., '0\n0').
  • Modified grep command error handling: Replaced || echo "0" with || true in grep -c commands to prevent appending an extra '0' on zero matches.
  • Implemented empty-string guards: Added [[ -z "$var" ]] && var=0 checks to ensure that variables (error_fixes and completed_tasks) default to '0' if grep -c produces no output.
  • Affected variables: The changes specifically impact the calculation of error_fixes (line 68) and completed_tasks (line 73) within the analyze_session() function.
Changelog
  • .agents/scripts/session-distill-helper.sh
    • Updated the error_fixes assignment to correctly handle cases where grep -c finds no matches, preventing invalid JSON output.
    • Modified the completed_tasks assignment to ensure it defaults to '0' when grep -c yields no results from TODO.md.
Activity
  • The pull request author, marcusquinn, created this PR to fix a specific bug related to grep -c and jq parsing.
  • The author verified the fix using ShellCheck, noting only pre-existing informational findings.
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.

@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 acffcb2 and 24f3b95.

📒 Files selected for processing (1)
  • .agents/scripts/session-distill-helper.sh

Walkthrough

Fixed a shell script bug where grep -c with zero matches produced duplicate output ("0\n0") when combined with || echo "0", causing invalid JSON errors in jq. The fix replaces the fallback pattern with || true and explicit empty checks to ensure clean numeric values.

Changes

Cohort / File(s) Summary
Shell Script Bug Fix
.agents/scripts/session-distill-helper.sh
Corrected grep count fallback handling by replacing || echo "0" with || true and adding explicit guards [[ -z "$var" ]] && var=0 for two variable assignments (error_fixes and completed_tasks) to prevent duplicate output corruption when grep produces zero matches.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

Suggested labels

bug

Poem

🐚 grep counted zero, but echo made it two,
jq saw invalid JSON and cried "I'm through!"
A guard clause and true now keep output clean—
The fairest shell fix we've ever seen. ✨

🚥 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 reflects the main fix: correcting a jq error in session-distill-helper.sh caused by grep -c returning zero matches.
Linked Issues check ✅ Passed All coding requirements from issue #2790 are fully implemented: grep patterns use || true instead of || echo 0, and empty-string guards ensure variables default to numeric 0.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to fixing the two affected lines (68 and 73) in analyze_session() as specified in issue #2790; no extraneous modifications.
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/session-distill-jq-zero-matches

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 github-actions bot added the bug Auto-created from TODO.md tag label Mar 3, 2026
@github-actions
Copy link
Contributor

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 20:07:40 UTC 2026: Code review monitoring started
Tue Mar 3 20:07:41 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 20:07:44 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 3, 2026

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 correctly addresses a bug where grep -c with zero matches could produce invalid output. The use of || true combined with a check for an empty variable is a robust way to handle the non-zero exit code from grep while ensuring a valid number is always produced, preventing downstream jq errors. The changes are sound and I have no further comments.

@marcusquinn marcusquinn merged commit 72e40ca into main Mar 3, 2026
26 of 28 checks passed
@marcusquinn marcusquinn deleted the bugfix/session-distill-jq-zero-matches branch March 3, 2026 20:13
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.

bug: session-distill-helper.sh crashes with jq error on repos with zero completed tasks

1 participant