Commit fc482ca
committed
feat(fs_write): fuzzy str_replace with 3-strategy fallback chain + file freshness check
The current str_replace implementation uses exact byte matching only.
When the model's old_str has minor differences from the file (indentation
drift, whitespace, or small context edits), the match fails and the model
either retries wastefully or falls back to destructive shell commands.
Implement str_replace_fuzzy() with a 3-strategy fallback chain inspired
by opencode and cline's diff-apply approaches:
1. Exact match — unchanged behaviour for the common case
2. Line-trimmed match — compares lines after trim(), then replaces using
byte offsets (prefix-sum table) into the original content. Handles
indentation drift (tab vs spaces, different indent levels).
3. Block-anchor match — uses first+last line as anchors, scores middle
lines with Levenshtein similarity, picks the best candidate above a
0.6 threshold. Handles minor edits in surrounding context lines.
Also adds file freshness checking:
- fs_read now records the file mtime into FileLineTracker.last_read_mtime
whenever it reads a file
- fs_write str_replace checks the current mtime against the recorded value
before writing; if the file was modified externally it returns a clear
error asking the model to re-read before retrying
Also:
- validate() rejects empty old_str before reaching fuzzy matching
- tool_index.json description updated to reflect fuzzy tolerance and
reinforce read-before-write / no-sed-fallback guidance
Key correctness properties:
- Strategies 2 and 3 return byte ranges — replacement is always at the
correct position even if matched text appears elsewhere in the file
- block_anchor_match skips first==last anchors (false positive guard)
- similarity_score respects actual content window bounds
- levenshtein uses O(n) rolling-row space, char count for denominator
- build_line_offsets prefix-sum gives O(1) offset lookup
- strip_empty_boundary_lines handles both leading and trailing empty lines
11 tests cover all strategies, edge cases, and error messages.1 parent e14ea18 commit fc482ca
File tree
5 files changed
+464
-88
lines changed- crates/chat-cli/src/cli/chat
- tools
5 files changed
+464
-88
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
22 | 28 | | |
23 | 29 | | |
24 | 30 | | |
| |||
30 | 36 | | |
31 | 37 | | |
32 | 38 | | |
| 39 | + | |
33 | 40 | | |
34 | 41 | | |
35 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
266 | 268 | | |
267 | 269 | | |
268 | 270 | | |
269 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
270 | 290 | | |
271 | 291 | | |
272 | 292 | | |
| |||
358 | 378 | | |
359 | 379 | | |
360 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
361 | 390 | | |
362 | 391 | | |
363 | 392 | | |
| |||
943 | 972 | | |
944 | 973 | | |
945 | 974 | | |
946 | | - | |
| 975 | + | |
947 | 976 | | |
948 | 977 | | |
949 | 978 | | |
| |||
977 | 1006 | | |
978 | 1007 | | |
979 | 1008 | | |
980 | | - | |
| 1009 | + | |
981 | 1010 | | |
982 | 1011 | | |
983 | 1012 | | |
| |||
1010 | 1039 | | |
1011 | 1040 | | |
1012 | 1041 | | |
1013 | | - | |
| 1042 | + | |
1014 | 1043 | | |
1015 | 1044 | | |
1016 | 1045 | | |
| |||
1029 | 1058 | | |
1030 | 1059 | | |
1031 | 1060 | | |
1032 | | - | |
| 1061 | + | |
1033 | 1062 | | |
1034 | 1063 | | |
1035 | 1064 | | |
| |||
1055 | 1084 | | |
1056 | 1085 | | |
1057 | 1086 | | |
1058 | | - | |
| 1087 | + | |
1059 | 1088 | | |
1060 | 1089 | | |
1061 | 1090 | | |
| |||
1102 | 1131 | | |
1103 | 1132 | | |
1104 | 1133 | | |
1105 | | - | |
| 1134 | + | |
1106 | 1135 | | |
1107 | 1136 | | |
1108 | 1137 | | |
| |||
1134 | 1163 | | |
1135 | 1164 | | |
1136 | 1165 | | |
1137 | | - | |
| 1166 | + | |
1138 | 1167 | | |
1139 | 1168 | | |
1140 | 1169 | | |
| |||
1171 | 1200 | | |
1172 | 1201 | | |
1173 | 1202 | | |
1174 | | - | |
| 1203 | + | |
1175 | 1204 | | |
1176 | 1205 | | |
1177 | 1206 | | |
| |||
1195 | 1224 | | |
1196 | 1225 | | |
1197 | 1226 | | |
1198 | | - | |
| 1227 | + | |
1199 | 1228 | | |
1200 | 1229 | | |
1201 | 1230 | | |
| |||
1232 | 1261 | | |
1233 | 1262 | | |
1234 | 1263 | | |
1235 | | - | |
| 1264 | + | |
1236 | 1265 | | |
1237 | 1266 | | |
1238 | 1267 | | |
| |||
1272 | 1301 | | |
1273 | 1302 | | |
1274 | 1303 | | |
1275 | | - | |
| 1304 | + | |
1276 | 1305 | | |
1277 | 1306 | | |
1278 | 1307 | | |
| |||
1302 | 1331 | | |
1303 | 1332 | | |
1304 | 1333 | | |
1305 | | - | |
| 1334 | + | |
1306 | 1335 | | |
1307 | 1336 | | |
1308 | 1337 | | |
| |||
1321 | 1350 | | |
1322 | 1351 | | |
1323 | 1352 | | |
1324 | | - | |
| 1353 | + | |
1325 | 1354 | | |
1326 | 1355 | | |
1327 | 1356 | | |
| |||
1353 | 1382 | | |
1354 | 1383 | | |
1355 | 1384 | | |
1356 | | - | |
| 1385 | + | |
1357 | 1386 | | |
1358 | 1387 | | |
1359 | 1388 | | |
| |||
0 commit comments