Skip to content

Commit 5337e85

Browse files
fix: preserve untouched bytes outside fuzzy-match span in edit tool (#116459) (#116509)
* fix(edit): preserve bytes outside fuzzy-match span on matched lines When an edit tool call falls back to fuzzy matching (e.g. plain space vs NBSP, straight quotes vs smart quotes), the entire overlapped line was rebuilt from the NFKC-normalized copy, silently destroying unrelated bytes on that line — fullwidth punctuation, halfwidth katakana, em-dashes, and other Unicode characters outside the edited span. Root cause: - applyEdits() set replacementBaseContent to normalizeForFuzzyMatch(normalizedContent) when any edit needed fuzzy matching. - applyReplacementsPreservingUnchangedLines then emitted untouched lines from the original content but rebuilt touched lines wholesale from the normalized base, applying NFKC folding to bytes the edit never referenced. Fix: - normalizeForFuzzyMatch now optionally returns an offset map that translates every normalized position back to the original text. - fuzzyFindText uses this map to translate match indices from fuzzy- normalized coordinates to original-content coordinates. - applyEdits keeps replacementBaseContent as the original (LF-normalized) content and uses direct applyReplacements instead of the line-preserving variant. Touch-untouched line distinction is no longer needed because replacements splice at byte granularity in the original content. Fixes #116459 * fix(edit): map fuzzy spans to stable source boundaries --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent 7bc2163 commit 5337e85

5 files changed

Lines changed: 397 additions & 97 deletions

File tree

‎src/agents/agent-tools.sandbox-mounted-paths.workspace-only.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ describe("tools.fs.workspaceOnly", () => {
138138
});
139139
});
140140

141+
it("preserves unrelated Unicode bytes through the real sandbox edit bridge", async () => {
142+
await withUnsafeMountedSandboxHarness(async ({ sandboxRoot, sandbox }) => {
143+
const filePath = path.join(sandboxRoot, "source.txt");
144+
const original =
145+
"const value\u00A0= 1; // keep \uFF08\uFF13\uFF09 \uFF71\uFF72\uFF73 \u2014 unchanged\r\n";
146+
const expected =
147+
"const value = 2; // keep \uFF08\uFF13\uFF09 \uFF71\uFF72\uFF73 \u2014 unchanged\r\n";
148+
const editTool = createSandboxedEditTool({
149+
root: sandbox.workspaceDir,
150+
bridge: sandbox.fsBridge!,
151+
});
152+
153+
await fs.writeFile(filePath, original, "utf8");
154+
await editTool.execute("sandbox-edit-fuzzy-unicode", {
155+
path: "source.txt",
156+
edits: [{ oldText: "const value = 1;", newText: "const value = 2;" }],
157+
});
158+
159+
await expect(fs.readFile(filePath, "utf8")).resolves.toBe(expected);
160+
});
161+
});
162+
141163
it("rejects invalid UTF-8 before sandbox edit or patch bridge writes", async () => {
142164
await withUnsafeMountedSandboxHarness(async ({ sandboxRoot, sandbox }) => {
143165
const filePath = path.join(sandboxRoot, "source.txt");

‎src/agents/sessions/tools/edit-diff.test.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,113 @@ describe("applyEditsToNormalizedContent uniqueness", () => {
151151
});
152152
});
153153

154+
describe("fuzzy edit source-span mapping", () => {
155+
it("preserves escaped Unicode bytes outside a fuzzy-matched span", () => {
156+
const content =
157+
"export const RETRY\u00A0MAX = 3; // \u518D\u8A66\u884C\uFF08\u6700\u5927\uFF13\u56DE\uFF09\uFF71\uFF72\uFF73 \u2014 \u8A2D\u5B9A\n" +
158+
"export const OTHER = 1;\n";
159+
160+
const result = applyEditsPreservingLineEndings(
161+
content,
162+
[{ oldText: "export const RETRY MAX = 3;", newText: "export const RETRY_MAX = 5;" }],
163+
"config.ts",
164+
);
165+
166+
expect(result.finalContent).toBe(
167+
"export const RETRY_MAX = 5; // \u518D\u8A66\u884C\uFF08\u6700\u5927\uFF13\u56DE\uFF09\uFF71\uFF72\uFF73 \u2014 \u8A2D\u5B9A\n" +
168+
"export const OTHER = 1;\n",
169+
);
170+
});
171+
172+
it("maps smart-quote folds while preserving a smart-quote comment", () => {
173+
const content =
174+
"const label = \u201Chello\u201D; // keep \u201Ccomment\u201D \u2014 unchanged\n";
175+
const result = applyEditsPreservingLineEndings(
176+
content,
177+
[{ oldText: 'const label = "hello";', newText: "const label = 'hi';" }],
178+
"test.ts",
179+
);
180+
181+
expect(result.finalContent).toBe(
182+
"const label = 'hi'; // keep \u201Ccomment\u201D \u2014 unchanged\n",
183+
);
184+
});
185+
186+
it("allows a match that covers a complete NFKC expansion", () => {
187+
const result = applyEditsPreservingLineEndings(
188+
"const value = \uFB01;\n",
189+
[{ oldText: "fi", newText: "pair" }],
190+
"test.ts",
191+
);
192+
193+
expect(result.finalContent).toBe("const value = pair;\n");
194+
});
195+
196+
it("rejects a match ending inside an NFKC expansion", () => {
197+
expect(() =>
198+
applyEditsPreservingLineEndings(
199+
"const value = \uFB01;\n",
200+
[{ oldText: "f", newText: "x" }],
201+
"test.ts",
202+
),
203+
).toThrow(/ambiguous Unicode-normalization or trimmed-whitespace boundary/);
204+
});
205+
206+
it("maps a complete combining sequence and preserves supplementary characters", () => {
207+
const content = "const caf\u0065\u0301 = 1; // \u{1F642}\n";
208+
const result = applyEditsPreservingLineEndings(
209+
content,
210+
[{ oldText: "const caf\u00E9 = 1;", newText: "const cafe = 2;" }],
211+
"test.ts",
212+
);
213+
214+
expect(result.finalContent).toBe("const cafe = 2; // \u{1F642}\n");
215+
});
216+
217+
it("preserves trimmed bytes at EOF", () => {
218+
const result = applyEditsPreservingLineEndings(
219+
"const value\u00A0= 1; ",
220+
[{ oldText: "const value = 1;", newText: "const value = 2;" }],
221+
"test.ts",
222+
);
223+
224+
expect(result.finalContent).toBe("const value = 2; ");
225+
});
226+
227+
it("keeps trim-folding behavior inside a complete fuzzy span", () => {
228+
const result = applyEditsPreservingLineEndings(
229+
"before\nfirst \nsecond\nafter \n",
230+
[{ oldText: "first\nsecond", newText: "combined" }],
231+
"test.ts",
232+
);
233+
234+
expect(result.finalContent).toBe("before\ncombined\nafter \n");
235+
});
236+
237+
it("preserves CRLF while mapping a fuzzy Unicode span", () => {
238+
const result = applyEditsPreservingLineEndings(
239+
"const value\u00A0= 1;\r\nnext\r\n",
240+
[{ oldText: "const value = 1;", newText: "const value = 2;" }],
241+
"test.ts",
242+
);
243+
244+
expect(result.finalContent).toBe("const value = 2;\r\nnext\r\n");
245+
});
246+
247+
it("keeps exact and fuzzy replacements in original coordinates", () => {
248+
const result = applyEditsPreservingLineEndings(
249+
"const a\u00A0= 1;\nconst b = 2;\n",
250+
[
251+
{ oldText: "const a = 1;", newText: "const a = 3;" },
252+
{ oldText: "const b = 2;", newText: "const b = 4;" },
253+
],
254+
"test.ts",
255+
);
256+
257+
expect(result.finalContent).toBe("const a = 3;\nconst b = 4;\n");
258+
});
259+
});
260+
154261
describe("applyEditsToNormalizedContent fuzzy uniqueness", () => {
155262
it("still rejects a fuzzy match that is ambiguous once normalization is applied", () => {
156263
const content = "foo(); \nfoo();\t\nbar();\n";

0 commit comments

Comments
 (0)