Skip to content

fix: ensure LLM analysis comments are updated#2680

Closed
chmouel wants to merge 1 commit intotektoncd:mainfrom
chmouel:srvkp-11549-fix-duplicate-pr-comment
Closed

fix: ensure LLM analysis comments are updated#2680
chmouel wants to merge 1 commit intotektoncd:mainfrom
chmouel:srvkp-11549-fix-duplicate-pr-comment

Conversation

@chmouel
Copy link
Copy Markdown
Member

@chmouel chmouel commented Apr 13, 2026

📝 Description of the Change

When a PipelineRun triggers an LLM analysis and a subsequent /retest runs again, the AI analysis comment was being duplicated on the PR rather than updated in-place.

The root cause was that the unique marker used by CreateComment/listCommentsByMarker to identify existing comments was embedded in the updateMarker argument but not in the comment body itself. The provider's lookup scans comment bodies for the marker string, so without the marker present in the body it could never find the existing comment to update.

Fix: Embed the marker as an HTML comment (<!-- llm-analysis-<role> -->) at the top of the formatted comment body. This allows listCommentsByMarker to locate the existing comment on subsequent runs and update it instead of creating a new one.

The golden test file is updated to include the marker, and the Gitea E2E test is extended to verify that after a /retest exactly one LLM analysis comment exists on the PR.

🔗 Linked GitHub Issue

Fixes https://redhat.atlassian.net/browse/SRVKP-11549

🧪 Testing Strategy

  • Unit tests
  • End-to-end tests

🤖 AI Assistance

  • I have used AI assistance for this PR.

✅ Submitter Checklist

  • 📝 My commit messages are clear, informative, and follow the project's How to write a git commit message guide.
  • ✨ I have ensured my commit message prefix (e.g., fix:, feat:) matches the "Type of Change" I selected above.
  • ♽ I have run make test and make lint locally to check for and fix any issues.
  • 🧪 I have added sufficient unit tests for my code changes.
  • 🎁 I have added end-to-end tests where feasible.

Embedded a unique marker within the PR comment using an HTML comment
tag. This allowed the system to correctly identify and update existing
AI analysis comments rather than creating a new entry on subsequent
PipelineRun executions.
Copy link
Copy Markdown

@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 implements a mechanism to update existing LLM analysis comments on pull requests by embedding a unique HTML marker. It also adds an integration test for Gitea to verify that comments are correctly updated rather than duplicated upon re-triggering. The review feedback suggests refining the marker string to prevent partial matches and scoping the test's comment retrieval to the specific pull request to ensure test reliability.

Comment thread pkg/llm/output_handler.go
Comment on lines 64 to +69
updateMarker := fmt.Sprintf("llm-analysis-%s", result.Role)

// Format the comment with LLM analysis, embedding the marker as an HTML
// comment so listCommentsByMarker can find and update it on subsequent runs.
comment := fmt.Sprintf("<!-- %s -->\n## 🤖 AI Analysis - %s\n\n%s\n\n---\n*Generated by Pipelines-as-Code LLM Analysis*",
updateMarker, result.Role, result.Response.Content)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The updateMarker used for identifying existing comments is too generic. If one role name is a prefix of another (e.g., "analysis" and "analysis-detailed"), the provider's regex search might match and update the wrong comment. Including the HTML comment delimiters in the updateMarker itself ensures a more precise match and prevents accidental overwrites of unrelated comments.

Suggested change
updateMarker := fmt.Sprintf("llm-analysis-%s", result.Role)
// Format the comment with LLM analysis, embedding the marker as an HTML
// comment so listCommentsByMarker can find and update it on subsequent runs.
comment := fmt.Sprintf("<!-- %s -->\n## 🤖 AI Analysis - %s\n\n%s\n\n---\n*Generated by Pipelines-as-Code LLM Analysis*",
updateMarker, result.Role, result.Response.Content)
updateMarker := fmt.Sprintf("<!-- llm-analysis-%s -->", result.Role)
// Format the comment with LLM analysis, embedding the marker so
// listCommentsByMarker can find and update it on subsequent runs.
comment := fmt.Sprintf("%s\n## 🤖 AI Analysis - %s\n\n%s\n\n---\n*Generated by Pipelines-as-Code LLM Analysis*",
updateMarker, result.Role, result.Response.Content)

Comment thread test/gitea_llm_test.go
Comment on lines +96 to +99
comments, _, err := topts.GiteaCNX.Client().ListRepoIssueComments(
topts.PullRequest.Base.Repository.Owner.UserName,
topts.PullRequest.Base.Repository.Name,
forgejo.ListIssueCommentOptions{})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Using ListRepoIssueComments retrieves all comments across the entire repository, which can lead to non-deterministic test results if other issues or pull requests in the same repository contain similar markers. It is safer and more efficient to use ListIssueComments with the specific pull request index to ensure the test only considers comments relevant to the current test case.

Suggested change
comments, _, err := topts.GiteaCNX.Client().ListRepoIssueComments(
topts.PullRequest.Base.Repository.Owner.UserName,
topts.PullRequest.Base.Repository.Name,
forgejo.ListIssueCommentOptions{})
comments, _, err := topts.GiteaCNX.Client().ListIssueComments(
topts.PullRequest.Base.Repository.Owner.UserName,
topts.PullRequest.Base.Repository.Name,
topts.PullRequest.Index,
forgejo.ListIssueCommentOptions{})

@pipelines-as-code
Copy link
Copy Markdown

pipelines-as-code bot commented Apr 13, 2026

🤖 AI Analysis - pr-complexity-rating

📊 PR Review Complexity

Dimension Score Rationale
Size 1 Likely a small change involving a string constant and a conditional check.
Logic complexity 2 Simple string matching/regex to identify existing comments.
Risk 1 Low risk; affects PR commenting behavior, not core pipeline execution or security.
Cross-cutting 1 Likely confined to the LLM/Notification utility logic.
Test coverage 3 Standard for this repo, though logic for comment identification should be verified.

Overall difficulty: Easy

Summary

This PR fixes a bug where AI analysis results generated multiple comments on a Pull Request instead of updating a single persistent comment. It introduces a hidden HTML marker within the comment body to allow the system to uniquely identify and overwrite previous analysis outputs.

Suggested reviewers focus

  • Marker Uniqueness: Ensure the HTML comment tag is unique enough to not collide with other automated tools.
  • Matching Logic: Verify the code correctly filters existing comments using this new marker across different git providers (GitHub/GitLab).

Generated by Pipelines-as-Code LLM Analysis

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.86%. Comparing base (658e9d4) to head (df59e24).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
pkg/llm/output_handler.go 0.00% 5 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2680      +/-   ##
==========================================
- Coverage   58.86%   58.86%   -0.01%     
==========================================
  Files         206      206              
  Lines       20329    20330       +1     
==========================================
  Hits        11967    11967              
- Misses       7589     7590       +1     
  Partials      773      773              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chmouel chmouel marked this pull request as draft April 13, 2026 14:05
@tekton-robot
Copy link
Copy Markdown

@chmouel: PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@chmouel
Copy link
Copy Markdown
Member Author

chmouel commented Apr 13, 2026

this solution ahs some flaw, altho working, working on a redesign to solve this properly, disabled the ai analysis for now

@chmouel chmouel closed this Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants