@@ -73,7 +73,47 @@ export const edit = defineTool({
7373 const code = params . code ;
7474 const relativePath = path . relative ( getHostWorkspace ( ) , filePath ) . replace ( / \\ / g, '/' ) ;
7575
76- // Determine edit range based on targeting priority
76+ // ── Input validation ──────────────────────────────────────────
77+
78+ // Bug #5: startLine > endLine
79+ if ( params . startLine !== undefined && params . endLine !== undefined && params . startLine > params . endLine ) {
80+ response . appendResponseLine (
81+ `❌ Invalid line range: startLine (${ params . startLine } ) is greater than endLine (${ params . endLine } ).` ,
82+ ) ;
83+ return ;
84+ }
85+
86+ // Bug #2: Empty code targeting a symbol = accidental deletion
87+ if ( params . target && code . trim ( ) . length === 0 ) {
88+ response . appendResponseLine (
89+ `❌ Refusing to apply empty code to symbol "${ params . target } " — this would delete it. ` +
90+ `If deletion is intended, remove the symbol explicitly or use startLine/endLine.` ,
91+ ) ;
92+ return ;
93+ }
94+
95+ // Bug #6: Validate line ranges are within file bounds
96+ if ( params . startLine !== undefined || params . endLine !== undefined ) {
97+ try {
98+ const contentResult = await fileReadContent ( filePath ) ;
99+ const totalLines = contentResult . totalLines ;
100+
101+ if ( params . startLine !== undefined && ( params . startLine < 1 || params . startLine > totalLines ) ) {
102+ response . appendResponseLine (
103+ `❌ startLine ${ params . startLine } is out of bounds (file has ${ totalLines } lines).` ,
104+ ) ;
105+ return ;
106+ }
107+ if ( params . endLine !== undefined && ( params . endLine < 1 || params . endLine > totalLines ) ) {
108+ response . appendResponseLine (
109+ `❌ endLine ${ params . endLine } is out of bounds (file has ${ totalLines } lines).` ,
110+ ) ;
111+ return ;
112+ }
113+ } catch {
114+ // File might not exist yet; proceed and let the edit fail naturally
115+ }
116+ }
77117 let editStartLine : number ;
78118 let editEndLine : number ;
79119 let targetLabel : string | undefined ;
0 commit comments