Skip to content

Add line numbers to file content for accurate code review placement#53

Merged
cosmocoder merged 1 commit intomainfrom
fix-comment-line-numbers
Mar 4, 2026
Merged

Add line numbers to file content for accurate code review placement#53
cosmocoder merged 1 commit intomainfrom
fix-comment-line-numbers

Conversation

@cosmocoder
Copy link
Copy Markdown
Owner

PR review comments were being attached to incorrect line numbers in GitHub PRs. The review body correctly identified the code issue, but the lineNumbers in the JSON output pointed to wrong locations (e.g., closing brackets instead of the actual problematic code).

Root Cause

File content was sent to the LLM as raw code blocks without line number annotations. LLMs cannot reliably count lines in large code blocks (>30-50 lines), causing them to drift and report inaccurate lineNumbers values. The GitHub API posting mechanism was verified to be correct — the issue was exclusively in the LLM-generated line numbers.

Changes

  • Add addLineNumbers() helper to src/utils/string-utils.js that prefixes each line with its 1-based number (e.g., 42 | const x = 1;)
  • Apply line numbering to all 3 LLM prompt paths in src/rag-analyzer.js:
    • Single-file code review (full file content + standalone file)
    • Single-file test review (full test file content + standalone test file)
    • Holistic PR review (full file content for each PR file)
  • Update LINE_NUMBERS_RULE in src/prompt-cache.js to instruct the LLM to read line numbers from the annotated content rather than counting lines
  • Add 10 tests for addLineNumbers() covering padding, edge cases, and formatting

- Implemented `addLineNumbers` function to prepend line numbers to source code, enhancing clarity during code reviews.
- Updated `rag-analyzer.js` and `prompt-cache.js` to utilize line numbers in displayed content for better reference.
@cosmocoder cosmocoder merged commit aaf8cd3 into main Mar 4, 2026
14 checks passed
@cosmocoder cosmocoder deleted the fix-comment-line-numbers branch March 4, 2026 20:13
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 4, 2026

🎉 This PR is included in version 1.2.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant