Near-duplicate comments in json output for agent
OpenCodeReview Version
v1.8.6 (1b193db35) windows/amd64
Operating System
Windows
Installation Method
npm (global)
LLM Provider
Other OpenAI-compatible endpoint
Bug Description
ocr review --to feature-branch --from main --format json --audience agent will output near-duplicate comments in json
e.g. see log.
Steps to Reproduce
- run ocr on code with issues in format=json and audience=agent mode
- Observe some duplicate comments
Expected Behavior
- unique comments, even if it costs me an llm call
- An even more problematic side effect is that inline comments in gitlab will be duplicates as well
Logs / Error Output
C:\Users\>ocr review --to feature-branch --from main --format json --audience agent
{
"status": "complete",
"message": "Review complete: 8 finding(s) across 9 selected item(s).",
"summary": {
"files_reviewed": 9,
"comments": 8,
"total_tokens": 409739,
"input_tokens": 390776,
"output_tokens": 18963,
"elapsed": "1m16s"
},
"tool_calls": {
"total": 21,
"by_tool": {
"code_comment": 8,
"code_search": 7,
"file_read": 6
}
},
"comments": [
{
"path": "Models/asdf.m",
"content": "The code accesses `config.asdf` before verifying that the `asdf` field exists in `config`. If `config.asdf` is missing, this will raise a runtime error before the later `assert(isfield(config, 'asdf'), ...)` can catch the problem. Add an existence check (e.g., `isfield(config, 'asdf') \u0026\u0026 isfield(config.asdf, 'skip_DB_connection')`) or move the `assert(isfield(config, 'asdf'), ...)` before the conditional.",
"suggestion_code": "if isfield(config, 'asdf') \u0026\u0026 isfield(config.asdf, 'skip_DB_connection') \u0026\u0026 ~config.asdf.skip_DB_connection",
"existing_code": "if ~config.asdf.skip_DB_connection",
"start_line": 20,
"end_line": 20,
"category": "bug",
"severity": "high"
},
{
"path": "Models/asdf.m",
"content": "The code accesses `config.asdf.skip_DB_connection` before confirming the `asdf` field exists, which can cause a runtime error when the field is missing. Move the existence check before the conditional or include an `isfield` guard.",
"suggestion_code": "+if isfield(config, 'asdf') \u0026\u0026 isfield(config.asdf, 'skip_DB_connection') \u0026\u0026 ~config.asdf.skip_DB_connection",
"existing_code": "+if ~config.asdf.skip_DB_connection",
"start_line": 20,
"end_line": 20,
"category": "bug",
"severity": "high"
},
{
"path": "Models/qwertz.m",
"content": "Using ... is clearer and safer.",
"suggestion_code": "for k = 1:numel(blabla)",
"existing_code": "for k = 1:nnz(blabla)",
"start_line": 230,
"end_line": 230,
"category": "style",
"severity": "low"
}
}
...
Additional Context
No response
Thanks for the detailed report and the clear repro output — this is definitely a valid bug.
We already have a DEDUP_TASK infrastructure in the scan pipeline that merges near-duplicate findings via an LLM call, and we think the fix here could either:
- Reuse the existing dedup machinery (
buildDedupCommentsJSON/applyDedupGroups) as a post-processing step in thereviewpipeline, or - Extend the
REVIEW_FILTER_TASKprompt to also flag near-duplicates (same file + same line + semantically equivalent content) alongside provably-incorrect comments.
If anyone is interested in tackling this, we're happy to discuss the approach further — feel free to chime in here.
Interested in tackling this issue
Got this issue too, I had to write a script to filter comments which are same path, start_line, end_line before post message in my pipeline, but the duplicated comments were not fully removed.
Expecting a good solution.
Thanks for the detailed report and the clear repro output — this is definitely a valid bug.
We already have a
DEDUP_TASKinfrastructure in thescanpipeline that merges near-duplicate findings via an LLM call, and we think the fix here could either:1. **Reuse the existing dedup machinery** (`buildDedupCommentsJSON` / `applyDedupGroups`) as a post-processing step in the `review` pipeline, or 2. **Extend the `REVIEW_FILTER_TASK` prompt** to also flag near-duplicates (same file + same line + semantically equivalent content) alongside provably-incorrect comments.If anyone is interested in tackling this, we're happy to discuss the approach further — feel free to chime in here.
I think extending REVIEW_FILTER_TASK is the safer and smaller approach.
In review mode, executeReviewFilter already runs once per file after that file's asynchronous comment workers have drained. It receives all comments for the current path together with the diff, and removes selected comment IDs before the comments are persisted and returned to the JSON/GitLab output. Extending this existing call would therefore avoid adding another LLM request.
My proposed implementation is:
- Extend the review-filter prompt to remove high-confidence near-duplicates in addition to comments that are provably incorrect.
- Treat comments as duplicates only when they target the same file and resolved location, and make the same underlying claim.
- Keep the most complete and actionable comment, returning only the redundant IDs through the existing JSON-array response format.
- Preserve complementary findings at the same location and repeated instances of the same issue at different locations.
- Resolve line numbers on a temporary copy of the per-file comments before building the filter payload.
- Add the resolved line range and useful canonical-selection metadata such as
suggestion_code, category, and severity to the filter payload. - If location resolution fails, only deduplicate when the comments clearly target the same non-empty
existing_code; otherwise keep both. - Preserve the existing fail-open behavior on LLM errors or malformed filter responses.
I considered reusing scan's buildDedupCommentsJSON / applyDedupGroups, but those helpers are currently scan-private and batch-oriented. Reusing them in review mode would require shared template/session plumbing and an additional LLM call. The scan grouping format can also merge comments across files and rewrite only the canonical content, which is risky for location-specific inline comments and their associated suggestion metadata.
For tests, I plan to cover:
- the filter payload includes resolved location and canonical-selection metadata;
- a valid filter response removes only the selected redundant comment;
- comments for other paths remain untouched;
- surviving comments retain their original metadata;
- malformed responses and LLM failures keep the original comments;
- the default prompt contains the conservative duplicate-removal rules;
- the existing incorrect-comment filtering behavior remains unchanged.
Would this REVIEW_FILTER_TASK approach and scope be acceptable?
@lizhengfeng101 should i start working on this?
i want to work this issuse
@praveenec-info i want to work this issuse
Guess amh1k is already working on it.
Could we start by flagging likely duplicates when ocr review builds its final JSON, after line resolution and severity sorting? An opt-in duplicate_candidates field could reference pairs by their final comments[] indices and say why they matched. Every original finding would keep its text, location, suggestion, and severity, in the same order and with the same count.
Before considering automatic folding, I’d replay real-model reviews against human labels and measure three things: how many redundant comments we could save, what fraction of folds would hide a distinct finding or actionable detail, and how many seeded defects found in the raw output would still be visible after folding. I’d include paraphrases of one defect, different defects on the same line, and similar findings across files that need separate fixes.
Would candidate annotations and a small offline evaluation be a reasonable first contribution for #709?