File tree Expand file tree Collapse file tree
src/agents/sessions/tools Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -46,6 +46,23 @@ describe("edit tool", () => {
4646 ) . rejects . toThrow ( / C u r r e n t f i l e c o n t e n t s : \n a c t u a l c u r r e n t c o n t e n t / ) ;
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.
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111} from "node:fs/promises" ;
1212import { Box , Container , Spacer , Text } from "@earendil-works/pi-tui" ;
1313import { Type } from "typebox" ;
14+ import { truncateUtf16Safe } from "../../../shared/utf16-slice.js" ;
1415import { renderDiff } from "../../modes/interactive/components/diff.js" ;
1516import type { AgentTool } from "../../runtime/index.js" ;
1617import { 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 } ) ;
You can’t perform that action at this time.
0 commit comments