Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/opencode/src/patch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,17 @@ function seekSequence(lines: string[], pattern: string[], startIndex: number, eo
(a, b) => normalizeUnicode(a.trim()) === normalizeUnicode(b.trim()),
eof,
)
return normalized
if (normalized !== -1) return normalized

// Pass 5: NFC normalization (canonical Unicode equivalence)
const canonical = tryMatch(
lines,
pattern,
startIndex,
(a, b) => a.normalize("NFC").trim() === b.normalize("NFC").trim(),
eof,
)
return canonical
}

function generateUnifiedDiff(oldContent: string, newContent: string): string {
Expand Down
49 changes: 49 additions & 0 deletions packages/opencode/test/patch/patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,55 @@ PATCH`
})
})

describe("canonical Unicode matching", () => {
test("matches Cyrillic text when the file is NFD and the patch is NFC", () => {
const original = "Район: Астана\n".normalize("NFD")
const result = Patch.deriveNewContentsFromChunks(
"summary.md",
[
{
old_lines: ["Район: Астана".normalize("NFC")],
new_lines: ["Район: Алматы"],
},
],
original,
)

expect(result.content).toBe("Район: Алматы\n")
})

test("matches Latin text when the file is NFC and the patch is NFD", () => {
const replacement = "touché".normalize("NFD")
const result = Patch.deriveNewContentsFromChunks(
"notes.md",
[
{
old_lines: ["café".normalize("NFD")],
new_lines: [replacement],
},
],
"café\n".normalize("NFC"),
)

expect(result.content).toBe(replacement + "\n")
})

test("does not match genuinely different Unicode text", () => {
expect(() =>
Patch.deriveNewContentsFromChunks(
"notes.md",
[
{
old_lines: ["cafe"],
new_lines: ["updated"],
},
],
"café\n",
),
).toThrow("Failed to find expected lines")
})
})

describe("applyPatch", () => {
it.live("should add a new file", () =>
Effect.gen(function* () {
Expand Down
Loading