Skip to content

t1392: Fix CodeRabbit sweep first-run delta inflation#2834

Closed
alex-solovyev wants to merge 1 commit intomainfrom
bugfix/coderabbit-sweep
Closed

t1392: Fix CodeRabbit sweep first-run delta inflation#2834
alex-solovyev wants to merge 1 commit intomainfrom
bugfix/coderabbit-sweep

Conversation

@alex-solovyev
Copy link
Collaborator

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

Summary

  • Fixes CodeRabbit @coderabbitai review request being triggered unconditionally on the first sweep run after PR t1390: Make CodeRabbit sweep conditional on quality gate changes #2808 (t1390)
  • Root cause: on first run, _load_sweep_state() returns prev_issues=0, so the delta calculation (113 - 0 = 113) exceeds the spike threshold of 10, triggering an active review even when metrics are stable
  • Fix: detect first run via prev_gate==UNKNOWN and skip delta-based conditions (issue spike, high/critical increase) — only quality gate status (condition 1) is evaluated on first run since it doesn't depend on deltas

Root Cause Analysis

PR #2808 added conditional logic with three trigger conditions:

  1. Quality gate is ERROR/WARN
  2. Issue count spike >= 10 since last sweep
  3. High/critical severity count increased

The state persistence (_load_sweep_state / _save_sweep_state) was correct, but the first run had no prior state file, so _load_sweep_state returned defaults: UNKNOWN|0|0. This made the delta calculation compare current metrics against zero:

  • issue_delta = 113 - 0 = 113 (exceeds threshold of 10)
  • high_critical_delta = 8 - 0 = 8 (> 0)

Both conditions 2 and 3 fired, triggering the @coderabbitai mention.

Verification

  • ShellCheck: zero violations
  • Bash syntax check: passes
  • Functional tests: all 7 pass (17 assertions):
    1. First run, stable metrics → suppressed (FIXED)
    2. Second run, no change → suppressed
    3. Issue spike +15 on subsequent run → triggered
    4. Gate ERROR on first run → triggered (condition 1 still works)
    5. High/critical increase on subsequent run → triggered
    6. Exact reproduction of 05:50 UTC bug → suppressed (FIXED)
    7. Small delta +4 below threshold → suppressed

Closes #2832

Summary by CodeRabbit

  • Improvements
    • Enhanced first-run quality sweep behavior: delta-based triggers for issue spikes and new critical findings are now deferred to subsequent sweeps, reducing false positives during baseline establishment.
    • Added baseline messaging throughout monitoring summaries to inform users that delta metrics will be computed starting from the next sweep.

On first run (no state file), _load_sweep_state returns prev_issues=0.
The delta calculation (e.g., 113 - 0 = 113) exceeds the spike threshold,
causing an unconditional @coderabbitai trigger even when metrics are stable.

Fix: detect first run via prev_gate==UNKNOWN and skip conditions 2 (issue
spike) and 3 (high/critical increase). Condition 1 (gate status) remains
active on first run since it doesn't depend on deltas. The first run now
seeds the baseline; deltas are computed from the second sweep onward.

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

coderabbitai bot commented Mar 4, 2026

Walkthrough

Adds first-run detection logic to the quality-sweep decision process, skipping delta-based triggers when prev_gate equals UNKNOWN and including baseline context messaging to inform users that delta comparisons begin on subsequent runs.

Changes

Cohort / File(s) Summary
First-run Sweep Gating
.agents/scripts/pulse-wrapper.sh
Introduces is_first_run flag to conditionally skip delta-based conditions (spike detection and new high/critical findings) on initial runs. Adds baseline note messaging for transparency and propagates context into summary text blocks for affected code paths.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

bug

Poem

🎯 First runs need no deltas to compare,
Just gates and baselines, fresh and fair.
Next sweep will measure what's changed,
Until then, defaults arranged. ✨

🚥 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 clearly and specifically summarizes the main change: fixing first-run delta inflation in CodeRabbit sweep logic, with a reference to the ticket number for traceability.
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/coderabbit-sweep

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 4, 2026

🔍 Code Quality Report

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

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

�[0;34m[INFO]�[0m Recent monitoring activity:
Wed Mar 4 16:31:07 UTC 2026: Code review monitoring started
Wed Mar 4 16:31:07 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 107

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 107
  • VULNERABILITIES: 0

Generated on: Wed Mar 4 16:31:10 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@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 resolves an issue where the CodeRabbit review system would incorrectly trigger an active review on its initial run for a new pull request. The fix ensures that delta-based conditions (issue spikes, high/critical increases) are skipped during the first sweep, as there's no prior state to compare against, preventing misleading "spikes" and unnecessary review requests. Only the quality gate status is evaluated on the first run.

Highlights

  • CodeRabbit First-Run Fix: Addressed an issue where CodeRabbit review requests were triggered unconditionally on the first sweep run due to delta calculations comparing current metrics against a zero baseline, leading to false positives.
  • Delta Calculation Logic: Modified the logic for issue spike and high/critical increase conditions to explicitly skip delta comparisons during the initial run when no prior state exists.
Changelog
  • .agents/scripts/pulse-wrapper.sh
    • Introduced an is_first_run flag to detect when no prior sweep state exists for CodeRabbit.
    • Modified the issue spike condition to only trigger if it's not the first run.
    • Modified the high/critical severity increase condition to only trigger if it's not the first run.
    • Added a baseline_note to the CodeRabbit monitoring message for first runs, indicating that deltas will be computed from the next sweep.
Activity
  • Identified and fixed an issue where CodeRabbit was triggered unconditionally on the first sweep run.
  • Performed root cause analysis, attributing the problem to delta calculations against a zero baseline on the first run.
  • Verified the fix with comprehensive functional tests, including scenarios for first runs, subsequent runs, issue spikes, gate errors, and high/critical increases.
  • Closed issue t1392: Fix CodeRabbit sweep still posting unconditionally after t1390 fix (PR #2808) #2832.
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.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 4, 2026

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.agents/scripts/pulse-wrapper.sh:
- Around line 1911-1919: The current first-run sentinel uses prev_gate ==
"UNKNOWN", which is ambiguous; instead detect first run by checking for the
presence of the persisted state file (or a persisted baseline_initialized flag)
and set is_first_run based on that. Update the is_first_run assignment to test
for state-file existence (e.g. [[ ! -f "$STATE_FILE" ]] ) or read a persisted
baseline_initialized boolean, and replace any other occurrences that rely on
prev_gate (including the analogous block around the later duplicate at lines
~1929-1940) so Conditions 2/3 are only suppressed when no prior baseline
actually exists.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d6576835-3c94-4c03-a2d7-fd2d56fea3dd

📥 Commits

Reviewing files that changed from the base of the PR and between 4a2fdcb and d2920f4.

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

Comment on lines +1911 to +1919
local is_first_run=false

# Condition 1: Quality gate is failing
# Detect first run: prev_gate is UNKNOWN when no state file exists.
# On first run, delta comparisons are meaningless (comparing against 0
# inflates every metric into a "spike"). Only condition 1 (gate status)
# is valid on first run. Conditions 2 and 3 require a prior baseline.
if [[ "$prev_gate" == "UNKNOWN" ]]; then
is_first_run=true
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

First-run detection is coupled to gate_status and can suppress delta triggers indefinitely.

is_first_run currently depends on prev_gate == "UNKNOWN". But UNKNOWN is also a valid persisted state when gate data is unavailable, so Conditions 2/3 can stay disabled forever on later sweeps. Use state-file existence (or an explicit baseline_initialized flag) as the first-run sentinel instead.

Suggested fix
-	local trigger_active=false
-	local trigger_reasons=""
-	local is_first_run=false
+	local trigger_active=false
+	local trigger_reasons=""
+	local slug_safe="${repo_slug//\//-}"
+	local state_file="${QUALITY_SWEEP_STATE_DIR}/${slug_safe}.json"
+	local is_first_run=false

-	# Detect first run: prev_gate is UNKNOWN when no state file exists.
+	# Detect first run from state file existence (stable sentinel).
 	# On first run, delta comparisons are meaningless (comparing against 0
 	# inflates every metric into a "spike"). Only condition 1 (gate status)
 	# is valid on first run. Conditions 2 and 3 require a prior baseline.
-	if [[ "$prev_gate" == "UNKNOWN" ]]; then
+	if [[ ! -f "$state_file" ]]; then
 		is_first_run=true
 	fi

Also applies to: 1929-1940

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.agents/scripts/pulse-wrapper.sh around lines 1911 - 1919, The current
first-run sentinel uses prev_gate == "UNKNOWN", which is ambiguous; instead
detect first run by checking for the presence of the persisted state file (or a
persisted baseline_initialized flag) and set is_first_run based on that. Update
the is_first_run assignment to test for state-file existence (e.g. [[ ! -f
"$STATE_FILE" ]] ) or read a persisted baseline_initialized boolean, and replace
any other occurrences that rely on prev_gate (including the analogous block
around the later duplicate at lines ~1929-1940) so Conditions 2/3 are only
suppressed when no prior baseline actually exists.

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

The pull request effectively addresses the issue of delta inflation on the first run of the CodeRabbit sweep by introducing an is_first_run flag. This flag correctly prevents delta-based trigger conditions (issue spikes and high/critical increases) from firing when no prior baseline exists. The implementation is clear, well-commented, and directly resolves the described bug, ensuring that only the quality gate status is evaluated during the initial sweep. The addition of a baseline_note for the first run also enhances clarity in the monitoring output.

Note: Security Review is unavailable for this PR.

@alex-solovyev
Copy link
Collaborator Author

Closing in favour of PR #2835 (t1392: Fix CodeRabbit sweep first-run baseline detection) which takes a cleaner approach to the same underlying issue. The CodeRabbit feedback here has been incorporated into the newer PR's approach.

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.

t1392: Fix CodeRabbit sweep still posting unconditionally after t1390 fix (PR #2808)

1 participant