Skip to content

Commit f27d494

Browse files
mikasa0818steipete
andauthored
fix(edit): keep mismatch hint truncation unicode-safe (#99527)
* fix(edit): keep mismatch hint truncation unicode-safe * fix(edit): preserve mismatch hint size bound --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 82610d3 commit f27d494

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ describe("edit tool", () => {
4646
).rejects.toThrow(/Current file contents:\nactual current content/);
4747
});
4848

49+
it("truncates exact-match mismatch hints without splitting UTF-16 surrogate pairs", async () => {
50+
const boundaryEmoji = "🙂";
51+
const filePath = await createTempFile(`${"a".repeat(799)}${boundaryEmoji}tail`);
52+
const tool = createEditTool(tmpDir);
53+
54+
await expect(
55+
tool.execute(
56+
"call-1",
57+
{
58+
path: filePath,
59+
edits: [{ oldText: "missing", newText: "replacement" }],
60+
},
61+
undefined,
62+
),
63+
).rejects.toThrow(`${"a".repeat(799)}\n... (truncated)`);
64+
});
65+
4966
it("recovers success after a post-write throw when the edit already applied", async () => {
5067
// Some backends throw after flushing content; a readback match is the
5168
// contract that lets the tool report success without duplicating edits.

‎src/agents/sessions/tools/edit.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "node:fs/promises";
1212
import { Box, Container, Spacer, Text } from "@earendil-works/pi-tui";
1313
import { Type } from "typebox";
14+
import { truncateUtf16Safe } from "../../../shared/utf16-slice.js";
1415
import { renderDiff } from "../../modes/interactive/components/diff.js";
1516
import type { AgentTool } from "../../runtime/index.js";
1617
import { textResult } from "../../tools/common.js";
@@ -177,7 +178,7 @@ function appendMismatchHint(error: Error, currentContent: string): Error {
177178
const snippet =
178179
currentContent.length <= EDIT_MISMATCH_HINT_LIMIT
179180
? currentContent
180-
: `${currentContent.slice(0, EDIT_MISMATCH_HINT_LIMIT)}\n... (truncated)`;
181+
: `${truncateUtf16Safe(currentContent, EDIT_MISMATCH_HINT_LIMIT)}\n... (truncated)`;
181182
const enhanced = new Error(`${error.message}\nCurrent file contents:\n${snippet}`, {
182183
cause: error,
183184
});

0 commit comments

Comments
 (0)