Skip to content

PR review comments occasionally land on wrong line (off by one) #256

@sentry-junior

Description

@sentry-junior

PR review comments sometimes land on the wrong line (e.g. line 5 instead of 6). The symptom surfaced via Check Run annotations using GitHub's "Check warning on line R633" UI notation.

Root cause hypothesis

Medium confidence. The hunk formatter (formatHunkForAnalysis in src/diff/context.ts) presents the diff to the LLM without per-line numbering. The LLM must infer absolute line numbers by counting from the newStart value in the @@ … @@ header. Context lines (unchanged lines prefixed with a space) make this counting ambiguous, especially across multi-hunk diffs or when the hunk contains a mix of context and changed lines.

The prompt instructs the LLM:

"location.startLine" MUST be within the hunk line range (shown in the "## Hunk" header).

But the diff content rendered to the LLM (via hunk.content) does not annotate individual line numbers inline — only the bounding range lines X–Y is stated. A mis-count of one context line produces an off-by-one in the returned startLine/endLine.

Secondary: the PR review comment renderer (src/output/renderer.ts) uses endLine ?? startLine as the GitHub comment anchor, while check annotations (src/output/github-checks.ts) use startLine as primary. These differ when endLine is set, which could surface as a one-line offset between the Check annotation and the PR review inline comment pointing at the same finding.

Reproduction

No minimal repro available, but this was observed when a PR review comment appeared on line N when the actual flagged line was N+1.

Suspected fix areas

  • src/diff/context.ts formatHunkForAnalysis: annotate each diff line with its absolute new-file line number (e.g. prefix each line with L{n}: ) so the LLM doesn't have to count from the hunk header.
  • src/output/renderer.ts: audit the inconsistency between using endLine as the PR comment anchor vs startLine for check annotations on the same finding.

Action taken on behalf of David Cramer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions