Skip to content

fix(opencode): match canonically-equivalent Unicode in apply_patch - #32216

Closed
sainzs wants to merge 1 commit into
anomalyco:devfrom
sainzs:seek-nfc
Closed

sainzs wants to merge 1 commit into
anomalyco:devfrom
sainzs:seek-nfc

Conversation

@sainzs

@sainzs sainzs commented Jun 13, 2026

Copy link
Copy Markdown

Summary

apply_patch fails with Failed to find expected lines in <file> when the file on disk and the model's context lines are canonically-equivalent Unicode but different byte sequences (NFC vs NFD). None of the 4 existing seekSequence passes handle combining-character equivalence, so any NFD content on disk is permanently unpatchable — the model re-reads, retries, fails, and burns tokens in a loop.

Root cause

seekSequence (packages/opencode/src/patch/index.ts) had 4 passes:

  1. exact
  2. rstrip
  3. trim
  4. normalizeUnicode + trim — but normalizeUnicode only maps smart quotes / dashes / ellipsis / NBSP; it never normalizes combining characters.

A file with decomposed й (и U+0438 + U+0306) can never match a patch containing precomposed й (U+0439), even though the strings are canonically equivalent. Models essentially always emit NFC.

Fix

A 5th pass after pass 4 that normalizes both sides to NFC before comparing:

return tryMatch(
  lines,
  pattern,
  startIndex,
  (a, b) => a.normalize("NFC").trim() === b.normalize("NFC").trim(),
  eof,
)

This is safe: NFC normalization followed by exact equality only matches strings Unicode defines as the same text — no false positives. (Approach proposed by the issue reporter.)

Test plan

  • Added regression tests: exact match, NFD-file ↔ NFC-pattern match (Cyrillic й), and a negative case (no false match)
  • packages/opencode patch suite: 23/23 pass
  • No new typecheck errors in patch/index.ts (the only typecheck error on dev is a pre-existing, unrelated one in src/bus/global.ts)

Fixes #31651

@github-actions

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jun 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential duplicate found:

This appears to be addressing the same issue as PR #32216. Both PRs are fixing Unicode normalization in the apply_patch function to handle canonically-equivalent characters (NFC vs NFD). PR #31847 likely predates PR #32216 and may already be addressing the same root cause. You should check the state of PR #31847 to see if it's been merged or if PR #32216 should be based on that work.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jun 13, 2026
@github-actions github-actions Bot closed this Jun 13, 2026
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.

apply_patch verification fails on canonically-equivalent Unicode — seekSequence needs an NFC pass

1 participant