Skip to content

feat(diff): hashline anchor comment localization (experimental, opt-in) - #706

Open
adlternative wants to merge 1 commit into
alibaba:mainfrom
adlternative:adl/dev/hashline-anchor-localization
Open

feat(diff): hashline anchor comment localization (experimental, opt-in)#706
adlternative wants to merge 1 commit into
alibaba:mainfrom
adlternative:adl/dev/hashline-anchor-localization

Conversation

@adlternative

@adlternative adlternative commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

What

Adds an opt-in, anchor-based comment localization path for code_comment, adapted from the hashline protocol (RimuruW/pi-hashline-edit).

When OCR_HASHLINE_ANCHORS=1 is set, the diff shown to the model renders every new-file line with a short anchor prefix:

42#KT:+	n, _ := strconv.Atoi(v)

The anchor hash is computed from the line and its two neighbors (xxh32(prev\0curr\0next) → 2 chars), so identical lines in different contexts get different anchors. The model localizes a comment by copying the anchor ("anchor": "42#KT" or a range "42#KT-45#MQ") instead of quoting a code snippet.

Why

The current existing_code text-matching pipeline resolves ambiguous matches by silently picking the first occurrence. Replaying real commit corpora through the production ResolveComment:

Localization method opencode repo (95k added lines) this repo (23k added lines)
existing_code, 1 line 63.2% correct 74.4% correct
existing_code, 3 lines 81.8% correct 93.0% correct
hashline anchor 100% correct 100% correct

41.5% of added lines in the TS corpus are not the first occurrence of their normalized content (imports, }, boilerplate) — exactly the silent-mislocation surface.

How

  • internal/hashline (new): context hashing, strict ResolveSpec verification, AnnotateDiff.
  • resolver.go: new highest-priority resolveFromAnchor step with two-factor validation — the hash is the checksum (both endpoints must verify), existing_code acts as a text hint that can veto hash collisions. On any mismatch it falls back to the existing hunk/file/relocation pipeline, so behavior is never worse than baseline. All resolution paths now record LlmComment.LocMethod (anchor / hunk / file / relocation / anchor_hint_veto) for observability.
  • agent.go: env-gated annotation of {{diff}} and the injected diff map.
  • examples/hashline_anchors/: anchor-aware tools.json + usage docs.

Cost / risk

  • Anchor annotation adds +26% tokens on the diff text (tiktoken, p50 ≈ 24%). In a 5×5 end-to-end A/B on a live endpoint, total input tokens dropped ~25% — the model needed fewer file_read round-trips to confirm positions — and output tokens were flat.
  • Anchor false-accept rate (model cites wrong line number, hash still verifies): ~0.5%, further reduced by the text-hint veto.
  • Fully opt-in: default behavior is unchanged; a failed anchor degrades to the existing pipeline.

Testing

  • go test ./... passes; new unit tests for hashing round-trips, collision behavior, diff annotation, and resolver anchor/veto/fallback paths.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


huzhening.hzn seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Add an opt-in anchor-based localization path for code_comment, adapted
from the hashline protocol (github.com/RimuruW/pi-hashline-edit):

- internal/hashline: xxh32 context hashes (prev\0curr\0next -> 2 chars)
  per line; AnnotateDiff renders new-side diff lines as "LINE#HASH:...";
  ResolveSpec strictly verifies anchors against new file content.
- resolver: new highest-priority resolveFromAnchor step. Two-factor
  validation: hash is the checksum, existing_code acts as a text hint
  that can veto hash collisions. On mismatch it falls back to the
  existing hunk/file/relocation pipeline, so behavior is never worse
  than baseline. All paths now record LlmComment.LocMethod.
- agent: OCR_HASHLINE_ANCHORS=1 gates anchor annotation of the diff in
  the main task prompt and injected diff map.
- code_comment: parse optional "anchor" parameter; example tools.json
  with the anchor schema under examples/hashline_anchors.

Measured on real commit corpora replayed through the production
resolver: existing_code text matching localizes 63-74% (1 line) /
82-93% (3 lines) of added-line comments to the correct line (errors are
silent first-match picks); verified anchors localize 100%, with a ~0.5%
hash false-accept rate that the text-hint veto further reduces. Diff
annotation costs +26% tokens on the diff, offset in end-to-end runs by
fewer file_read round-trips (-25% total input tokens in a 5x5 A/B).
@adlternative
adlternative force-pushed the adl/dev/hashline-anchor-localization branch from 65d6b35 to a5da81b Compare August 3, 2026 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants