rab-agent 0.1.5

rab is a lightweight, extensible, Rust-based coding agent.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
use crate::agent::extension::{Extension, ToolDefinition};
use crate::agent::extension::{ToolRenderContext, ToolRenderer};
use crate::tui::Theme;
use crate::tui::ThemeKey;
use async_trait::async_trait;
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use unicode_normalization::UnicodeNormalization;

// ── EditOperations (pluggable) ─────────────────────────────────────

/// Pluggable operations for the edit tool (matching pi's EditOperations).
/// Override these to delegate file editing to remote systems (for example SSH).
#[async_trait]
pub trait EditOperations: Send + Sync {
    /// Read file contents as a String.
    async fn read_file(&self, absolute_path: &Path) -> anyhow::Result<String>;
    /// Write content to a file.
    async fn write_file(&self, absolute_path: &Path, content: &str) -> anyhow::Result<()>;
    /// Check if file is readable and writable (throw if not).
    async fn access(&self, absolute_path: &Path) -> anyhow::Result<()>;
}

struct DefaultEditOperations;

#[async_trait]
impl EditOperations for DefaultEditOperations {
    async fn read_file(&self, absolute_path: &Path) -> anyhow::Result<String> {
        Ok(std::fs::read_to_string(absolute_path)?)
    }

    async fn write_file(&self, absolute_path: &Path, content: &str) -> anyhow::Result<()> {
        Ok(std::fs::write(absolute_path, content)?)
    }

    async fn access(&self, absolute_path: &Path) -> anyhow::Result<()> {
        if !absolute_path.exists() {
            anyhow::bail!("File not found: {}", absolute_path.display());
        }
        if !absolute_path.is_file() {
            anyhow::bail!("Not a file: {}", absolute_path.display());
        }
        Ok(())
    }
}

pub struct EditExtension {
    cwd: PathBuf,
    operations: Arc<dyn EditOperations>,
}

impl EditExtension {
    pub fn new(cwd: PathBuf) -> Self {
        Self {
            cwd,
            operations: Arc::new(DefaultEditOperations),
        }
    }

    /// Set custom edit operations (e.g. for SSH targets).
    pub fn with_operations(mut self, operations: Arc<dyn EditOperations>) -> Self {
        self.operations = operations;
        self
    }
}

impl Extension for EditExtension {
    fn name(&self) -> Cow<'static, str> {
        "edit".into()
    }

    fn tools(&self) -> Vec<ToolDefinition> {
        vec![ToolDefinition {
            tool: Box::new(EditTool {
                cwd: self.cwd.clone(),
                operations: self.operations.clone(),
            }),
            snippet: "Make precise file edits with exact text replacement, including multiple disjoint edits in one call",
            guidelines: &[
                "Use edit for precise changes (edits[].oldText must match exactly)",
                "When changing multiple separate locations in one file, use one edit call with multiple entries in edits[] instead of multiple edit calls",
                "Each edits[].oldText is matched against the original file, not after earlier edits are applied. Do not emit overlapping or nested edits. Merge nearby changes into one edit.",
                "Keep edits[].oldText as small as possible while still being unique in the file. Do not pad with large unchanged regions.",
            ],
            prepare_arguments: Some(prepare_edit_args),
            before_tool_call: None,
            after_tool_call: None,
            renderer: Some(std::sync::Arc::new(EditRenderer::new())),
        }]
    }
}

struct EditTool {
    cwd: PathBuf,
    operations: Arc<dyn EditOperations>,
}

#[derive(serde::Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
struct Edit {
    old_text: String,
    new_text: String,
}

// ── BOM handling ──────────────────────────────────────────────────

/// Strip UTF-8 BOM if present. Returns (bom, content_without_bom).
fn strip_bom(content: &str) -> (&str, &str) {
    if content.starts_with('\u{FEFF}') {
        ("\u{FEFF}", &content['\u{FEFF}'.len_utf8()..])
    } else {
        ("", content)
    }
}

// ── Line ending handling ─────────────────────────────────────────

fn detect_line_ending(content: &str) -> &'static str {
    if content.contains("\r\n") {
        "\r\n"
    } else {
        "\n"
    }
}

fn normalize_to_lf(content: &str) -> String {
    content.replace("\r\n", "\n")
}

fn restore_line_endings(content: &str, ending: &str) -> String {
    if ending == "\r\n" {
        content.replace('\n', "\r\n")
    } else {
        content.to_string()
    }
}

// ── Fuzzy matching ───────────────────────────────────────────────

/// Normalize text for fuzzy matching (pi-compatible).
/// Applies progressive transformations:
/// - NFKC normalization (handles composed/decomposed Unicode)
/// - Strip trailing whitespace from each line
/// - Normalize Unicode smart quotes → ASCII quotes
/// - Normalize Unicode dashes/hyphens → ASCII hyphen
/// - Normalize special Unicode spaces → regular space
fn normalize_for_fuzzy_match(text: &str) -> String {
    // First: NFKC normalization (pi calls .normalize("NFKC"))
    let nfkc = text.nfkc().collect::<String>();

    // Second: strip trailing whitespace per line
    let mut intermediate = String::with_capacity(nfkc.len());
    for line in nfkc.lines() {
        if !intermediate.is_empty() {
            intermediate.push('\n');
        }
        intermediate.push_str(line.trim_end());
    }
    // Handle trailing newline: lines() strips final newline, re-add if present
    if nfkc.ends_with('\n') {
        intermediate.push('\n');
    }

    // Third: normalize Unicode characters to ASCII equivalents
    let mut result = String::with_capacity(intermediate.len());
    for ch in intermediate.chars() {
        match ch {
            '\u{2018}' | '\u{2019}' | '\u{201A}' | '\u{201B}' => result.push('\''),
            '\u{201C}' | '\u{201D}' | '\u{201E}' | '\u{201F}' => result.push('"'),
            '\u{2010}' | '\u{2011}' | '\u{2012}' | '\u{2013}' | '\u{2014}' | '\u{2015}'
            | '\u{2212}' => {
                result.push('-');
            }
            '\u{00A0}' | '\u{2002}' | '\u{2003}' | '\u{2004}' | '\u{2005}' | '\u{2006}'
            | '\u{2007}' | '\u{2008}' | '\u{2009}' | '\u{200A}' | '\u{202F}' | '\u{205F}'
            | '\u{3000}' => {
                result.push(' ');
            }
            other => result.push(other),
        }
    }

    result
}

// ── Input normalization ──────────────────────────────────────────

/// Normalize tool arguments: handle `edits` as JSON string, legacy `oldText`/`newText`.
fn prepare_edit_arguments(args: &serde_json::Value) -> Result<(String, Vec<Edit>), String> {
    let path = args["path"]
        .as_str()
        .ok_or_else(|| "Missing 'path' argument".to_string())?;

    let edits = if let Some(edits_val) = args.get("edits") {
        if let Some(s) = edits_val.as_str() {
            serde_json::from_str::<Vec<Edit>>(s)
                .map_err(|e| format!("Invalid edits JSON string: {}", e))?
        } else {
            serde_json::from_value::<Vec<Edit>>(edits_val.clone())
                .map_err(|e| format!("Invalid edits array: {}", e))?
        }
    } else if let (Some(old), Some(new)) = (args.get("oldText"), args.get("newText")) {
        let old_text = old
            .as_str()
            .ok_or_else(|| "Invalid 'oldText' argument: expected string".to_string())?;
        let new_text = new
            .as_str()
            .ok_or_else(|| "Invalid 'newText' argument: expected string".to_string())?;
        vec![Edit {
            old_text: old_text.to_string(),
            new_text: new_text.to_string(),
        }]
    } else if let (Some(old), Some(new)) = (args.get("old_text"), args.get("new_text")) {
        let old_text = old
            .as_str()
            .ok_or_else(|| "Invalid 'old_text' argument: expected string".to_string())?;
        let new_text = new
            .as_str()
            .ok_or_else(|| "Invalid 'new_text' argument: expected string".to_string())?;
        vec![Edit {
            old_text: old_text.to_string(),
            new_text: new_text.to_string(),
        }]
    } else {
        return Err("Missing 'edits' array (or 'oldText'/'newText' or 'old_text'/'new_text' for legacy format)".to_string());
    };

    if edits.is_empty() {
        return Err("At least one edit is required".to_string());
    }

    Ok((path.to_string(), edits))
}

/// Normalize tool arguments before execution.
/// Returns restructured JSON matching execute()'s expected format, or the
/// original args on error (execute() will produce its own error message).
pub fn prepare_edit_args(mut args: serde_json::Value) -> Result<serde_json::Value, String> {
    let (path_str, edits) = prepare_edit_arguments(&args)?;

    // Build the edits array in pi's camelCase format
    let edits_array: Vec<serde_json::Value> = edits
        .iter()
        .map(|e| {
            serde_json::json!({
                "oldText": e.old_text,
                "newText": e.new_text
            })
        })
        .collect();

    // Preserve all other fields (pi-compatible spread), only removing
    // the legacy fields that were merged into edits.
    if let Some(obj) = args.as_object_mut() {
        obj.remove("oldText");
        obj.remove("newText");
        obj.remove("old_text");
        obj.remove("new_text");
        obj.insert("path".to_string(), serde_json::Value::String(path_str));
        obj.insert("edits".to_string(), serde_json::Value::Array(edits_array));
    }

    Ok(args)
}

/// Normalize tool arguments before execution (test-only).
#[allow(dead_code)]
fn prepare_edit_tool_args(mut args: serde_json::Value) -> serde_json::Value {
    let (path_str, edits) = match prepare_edit_arguments(&args) {
        Ok(result) => result,
        Err(_) => return args,
    };

    let edits_array: Vec<serde_json::Value> = edits
        .iter()
        .map(|e| {
            serde_json::json!({
                "oldText": e.old_text,
                "newText": e.new_text
            })
        })
        .collect();

    if let Some(obj) = args.as_object_mut() {
        obj.remove("oldText");
        obj.remove("newText");
        obj.remove("old_text");
        obj.remove("new_text");
        obj.insert("path".to_string(), serde_json::Value::String(path_str));
        obj.insert("edits".to_string(), serde_json::Value::Array(edits_array));
    }

    args
}

// ── Line-span tracking for fuzzy mapping ────────────────────────

/// A line span tracking the byte offsets of a line in the content.
/// Matches pi's `LineSpan` struct.
#[derive(Debug, Clone, Copy)]
struct LineSpan {
    start: usize,
    end: usize,
}

/// Split content into lines, preserving each line's ending.
/// Returns Vec<&str> where each element includes its line ending if present.
fn split_lines_with_endings(content: &str) -> Vec<&str> {
    let mut result = Vec::new();
    let mut remaining = content;
    while let Some(pos) = remaining.find('\n') {
        result.push(&remaining[..=pos]);
        remaining = &remaining[pos + 1..];
    }
    if !remaining.is_empty() {
        result.push(remaining);
    }
    result
}

/// Get line spans for the content.
fn get_line_spans(content: &str) -> Vec<LineSpan> {
    let mut offset = 0;
    split_lines_with_endings(content)
        .iter()
        .map(|line| {
            let span = LineSpan {
                start: offset,
                end: offset + line.len(),
            };
            offset = span.end;
            span
        })
        .collect()
}

/// Get the line range that a replacement touches.
fn get_replacement_line_range(
    lines: &[LineSpan],
    match_index: usize,
    match_length: usize,
) -> (usize, usize) {
    let replacement_end = match_index + match_length;

    let mut start_line = 0;
    for (i, line) in lines.iter().enumerate() {
        if match_index >= line.start && match_index < line.end {
            start_line = i;
            break;
        }
    }

    let mut end_line = start_line;
    while end_line < lines.len() && lines[end_line].end < replacement_end {
        end_line += 1;
    }
    if end_line >= lines.len() {
        end_line = lines.len() - 1;
    }

    (start_line, end_line + 1)
}

/// Apply replacements to content (applied in reverse order to keep offsets stable).
/// Each replacement is (matchIndex, matchLength, newText).
fn apply_replacements(
    content: &str,
    replacements: &[(usize, usize, &str)],
    offset: usize,
) -> String {
    let mut result = content.to_string();
    for (start, length, new_text) in replacements.iter().rev() {
        let adj_start = start - offset;
        let adj_end = adj_start + length;
        result.replace_range(adj_start..adj_end, new_text);
    }
    result
}

/// Map changes made in fuzzy-normalized space back to the original (LF-normalized)
/// content, preserving the original bytes of unchanged lines (pi-compatible).
///
/// Uses line-span tracking and groups overlapping replacements, matching pi's
/// `applyReplacementsPreservingUnchangedLines`.
fn apply_replacements_preserving_unchanged_lines(
    original_content: &str,
    base_content: &str,
    replacements: &[(usize, usize, &str)], // (matchIndex, matchLength, newText) sorted by matchIndex
) -> String {
    let original_lines = split_lines_with_endings(original_content);
    let base_lines = get_line_spans(base_content);

    if original_lines.len() != base_lines.len() {
        // Line count mismatch — fall back to simple application
        let mut result = base_content.to_string();
        for (start, end, new_text) in replacements.iter().rev() {
            result.replace_range(*start..*end, new_text);
        }
        return result;
    }

    // Build groups of overlapping replacements
    struct Group {
        start_line: usize,
        end_line: usize,
        replacements: Vec<(usize, usize, String)>, // (matchIndex, matchLength, newText)
    }

    let mut groups: Vec<Group> = Vec::new();
    for &(start, end, new_text) in replacements {
        let (sl, el) = get_replacement_line_range(&base_lines, start, end);
        if let Some(last) = groups.last_mut()
            && sl < last.end_line
        {
            last.end_line = last.end_line.max(el);
            last.replacements.push((start, end, new_text.to_string()));
            continue;
        }
        groups.push(Group {
            start_line: sl,
            end_line: el,
            replacements: vec![(start, end, new_text.to_string())],
        });
    }

    let mut original_line_index = 0;
    let mut result = String::new();

    for group in &groups {
        // Copy unchanged original lines
        result.push_str(&original_lines[original_line_index..group.start_line].concat());

        // Apply replacements to the base content slice for this group
        let group_start_offset = base_lines[group.start_line].start;
        let group_end_offset = base_lines[group.end_line - 1].end;
        let group_slice = &base_content[group_start_offset..group_end_offset];
        let adjusted_replacements: Vec<(usize, usize, &str)> = group
            .replacements
            .iter()
            .map(|(s, e, t)| (*s - group_start_offset, *e, t.as_str()))
            .collect();
        result.push_str(&apply_replacements(group_slice, &adjusted_replacements, 0));

        original_line_index = group.end_line;
    }

    // Copy remaining original lines
    result.push_str(&original_lines[original_line_index..].concat());

    result
}

// ── Diff computation ─────────────────────────────────────────────

/// Replace tabs with 3 spaces for consistent rendering.
fn replace_tabs(text: &str) -> String {
    text.replace('\t', "   ")
}

/// Compute a display-oriented diff string with line numbers and context.
/// Produces pi-compatible format:
/// `+{lineNum} {content}` / `-{lineNum} {content}` / ` {lineNum} {content}` / `  ...`
/// With line numbers padded to the width of the max line number.
fn compute_diff(original: &str, modified: &str, _path: &str) -> String {
    let orig_lines: Vec<&str> = original.lines().collect();
    let mod_lines: Vec<&str> = modified.lines().collect();

    let max_line_num = orig_lines.len().max(mod_lines.len());
    let line_num_width = max_line_num.to_string().len();

    let mut output: Vec<String> = Vec::new();

    // Use LCS to find the diff
    let n = orig_lines.len();
    let m = mod_lines.len();
    let mut dp = vec![vec![0usize; m + 1]; n + 1];
    for i in 1..=n {
        for j in 1..=m {
            if orig_lines[i - 1] == mod_lines[j - 1] {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = dp[i - 1][j].max(dp[i][j - 1]);
            }
        }
    }

    // Backtrack to build sequence of changes
    let mut changes: Vec<(char, &str)> = Vec::new();
    let mut i = n;
    let mut j = m;
    while i > 0 || j > 0 {
        if i > 0 && j > 0 && orig_lines[i - 1] == mod_lines[j - 1] {
            changes.push((' ', orig_lines[i - 1]));
            i -= 1;
            j -= 1;
        } else if j > 0 && (i == 0 || dp[i][j - 1] >= dp[i - 1][j]) {
            changes.push(('+', mod_lines[j - 1]));
            j -= 1;
        } else {
            changes.push(('-', orig_lines[i - 1]));
            i -= 1;
        }
    }
    changes.reverse();

    // Group into hunks with context boundaries
    const CONTEXT_LINES: usize = 4;
    let mut old_line_num: usize = 1;
    let mut new_line_num: usize = 1;

    let pad = |num: usize| -> String { format!("{:width$}", num, width = line_num_width) };

    let mut k = 0;
    while k < changes.len() {
        let (tag, _text) = changes[k];

        if tag == ' ' {
            // Context line
            let mut ctx_buffer: Vec<&str> = Vec::new();
            let ctx_start = k;
            while k < changes.len() && changes[k].0 == ' ' {
                ctx_buffer.push(changes[k].1);
                k += 1;
            }
            let ctx_end = k;
            let has_leading_change = ctx_start > 0 && changes[ctx_start - 1].0 != ' ';
            let has_trailing_change = ctx_end < changes.len() - 1;

            if has_leading_change || has_trailing_change {
                // Show context around changes (pi-style)
                let total_ctx = ctx_buffer.len();

                if has_leading_change && has_trailing_change {
                    if total_ctx <= CONTEXT_LINES * 2 {
                        // Show all
                        for &line in &ctx_buffer {
                            output.push(format!(" {} {}", pad(old_line_num), replace_tabs(line)));
                            old_line_num += 1;
                            new_line_num += 1;
                        }
                    } else {
                        let leading = &ctx_buffer[..CONTEXT_LINES];
                        let trailing = &ctx_buffer[total_ctx - CONTEXT_LINES..];
                        let skipped = total_ctx - leading.len() - trailing.len();

                        for &line in leading {
                            output.push(format!(" {} {}", pad(old_line_num), replace_tabs(line)));
                            old_line_num += 1;
                            new_line_num += 1;
                        }

                        output.push(format!(" {} ...", " ".repeat(line_num_width)));
                        old_line_num += skipped;
                        new_line_num += skipped;

                        for &line in trailing {
                            output.push(format!(" {} {}", pad(old_line_num), replace_tabs(line)));
                            old_line_num += 1;
                            new_line_num += 1;
                        }
                    }
                } else if has_leading_change {
                    // Context after a change (change before context): show CONTEXT_LINES leading
                    let shown = ctx_buffer.len().min(CONTEXT_LINES);
                    let skipped = ctx_buffer.len() - shown;

                    for &line in &ctx_buffer[..shown] {
                        output.push(format!(" {} {}", pad(old_line_num), replace_tabs(line)));
                        old_line_num += 1;
                        new_line_num += 1;
                    }

                    if skipped > 0 {
                        output.push(format!(" {} ...", " ".repeat(line_num_width)));
                        old_line_num += skipped;
                        new_line_num += skipped;
                    }
                } else if has_trailing_change {
                    // Context before a change (change after context): show CONTEXT_LINES trailing
                    let shown = ctx_buffer.len().min(CONTEXT_LINES);
                    let skipped = ctx_buffer.len() - shown;

                    if skipped > 0 {
                        output.push(format!(" {} ...", " ".repeat(line_num_width)));
                        old_line_num += skipped;
                        new_line_num += skipped;
                    }

                    for &line in &ctx_buffer[ctx_buffer.len() - shown..] {
                        output.push(format!(" {} {}", pad(old_line_num), replace_tabs(line)));
                        old_line_num += 1;
                        new_line_num += 1;
                    }
                }
            } else {
                // No surrounding changes - skip entirely
                old_line_num += ctx_buffer.len();
                new_line_num += ctx_buffer.len();
            }
        } else {
            // Change (removed or added)
            let mut removed: Vec<&str> = Vec::new();
            while k < changes.len() && changes[k].0 == '-' {
                removed.push(changes[k].1);
                k += 1;
            }
            let mut added: Vec<&str> = Vec::new();
            while k < changes.len() && changes[k].0 == '+' {
                added.push(changes[k].1);
                k += 1;
            }

            // Show all removed lines first
            for &line in &removed {
                output.push(format!("-{} {}", pad(old_line_num), replace_tabs(line)));
                old_line_num += 1;
            }
            // Then all added lines
            for &line in &added {
                output.push(format!("+{} {}", pad(new_line_num), replace_tabs(line)));
                new_line_num += 1;
            }
        }
    }

    output.join("\n")
}

/// Parse path and edits from args without validation errors — returns None if
/// arguments are not yet complete (for preview computation).
fn parse_path_edits(args: &serde_json::Value) -> Option<(String, Vec<Edit>)> {
    let path = args.get("path").and_then(|v| v.as_str())?;
    let edits: Vec<Edit> = if let Some(edits_val) = args.get("edits") {
        if let Some(s) = edits_val.as_str() {
            serde_json::from_str(s).ok()?
        } else {
            serde_json::from_value(edits_val.clone()).ok()?
        }
    } else if let (Some(old), Some(new)) = (args.get("oldText"), args.get("newText")) {
        let old_text = old.as_str()?;
        let new_text = new.as_str()?;
        vec![Edit {
            old_text: old_text.to_string(),
            new_text: new_text.to_string(),
        }]
    } else {
        return None;
    };

    if edits.is_empty() {
        return None;
    }

    Some((path.to_string(), edits))
}

/// Apply edits to normalized content and return (normalized, base_content, new_content, diff).
/// This is the core edit logic extracted for reuse by both execute and preview.
///
/// Returns Ok((normalized, base_content, new_content, diff_string)) on success,
/// or Err(error_message) if edits can't be applied.
fn apply_edits_and_compute_diff(
    normalized: &str,
    edits: &[Edit],
    path_str: &str,
) -> Result<(String, String, String), String> {
    // Determine if fuzzy matching is needed
    let mut needs_fuzzy = false;
    for edit in edits {
        let old_lf = normalize_to_lf(&edit.old_text);
        if !normalized.contains(&old_lf) {
            needs_fuzzy = true;
            break;
        }
    }

    // Build work content: exact or fuzzy-normalized
    let fuzzy_owned;
    let (work_content, is_fuzzy_space) = if needs_fuzzy {
        fuzzy_owned = normalize_for_fuzzy_match(normalized);
        (fuzzy_owned.as_str(), true)
    } else {
        (normalized, false)
    };

    let mut matched_indices: Vec<(usize, usize)> = Vec::new();

    for (i, edit) in edits.iter().enumerate() {
        if edit.old_text.is_empty() {
            return if edits.len() == 1 {
                Err(format!("oldText must not be empty in {}.", path_str))
            } else {
                Err(format!(
                    "edits[{}].oldText must not be empty in {}.",
                    i, path_str
                ))
            };
        }

        let search_text = if is_fuzzy_space {
            normalize_for_fuzzy_match(&normalize_to_lf(&edit.old_text))
        } else {
            normalize_to_lf(&edit.old_text)
        };
        let count = work_content.matches(&search_text).count();

        if count == 0 {
            return if edits.len() == 1 {
                Err(format!(
                    "Could not find the exact text in {}. \
                     The old text must match exactly including all whitespace and newlines.",
                    path_str
                ))
            } else {
                Err(format!(
                    "Could not find edits[{}] in {}. \
                     The oldText must match exactly including all whitespace and newlines.",
                    i, path_str
                ))
            };
        }

        if count > 1 {
            return if edits.len() == 1 {
                Err(format!(
                    "Found {} occurrences of the text in {}. \
                     The text must be unique. Please provide more context to make it unique.",
                    count, path_str
                ))
            } else {
                Err(format!(
                    "Found {} occurrences of edits[{}] in {}. \
                     Each oldText must be unique. Please provide more context to make it unique.",
                    count, i, path_str
                ))
            };
        }

        let pos = work_content.find(&search_text).unwrap();
        matched_indices.push((pos, pos + search_text.len()));
    }

    // Check for overlapping edits
    for (idx_i, &(pos_i, end_i)) in matched_indices.iter().enumerate() {
        for (idx_j, &(pos_j, end_j)) in matched_indices.iter().enumerate().skip(idx_i + 1) {
            if pos_i < end_j && pos_j < end_i {
                return Err(format!(
                    "edits[{}] and edits[{}] overlap in {}. Merge them into one edit or target disjoint regions.",
                    idx_i, idx_j, path_str
                ));
            }
        }
    }

    // Apply edits (sorted left-to-right)
    let mut sorted: Vec<(usize, usize, &Edit)> = matched_indices
        .into_iter()
        .zip(edits.iter())
        .map(|((start, end), edit)| (start, end, edit))
        .collect();
    sorted.sort_by_key(|(pos, _, _)| *pos);

    let (base_content, new_content) = if is_fuzzy_space {
        // Build replacement tuples for the preserving function
        let mapped_refs: Vec<(usize, usize, &str)> = sorted
            .iter()
            .map(|(start, end, edit)| (*start, *end - *start, &edit.new_text[..]))
            .collect();

        let new_content =
            apply_replacements_preserving_unchanged_lines(normalized, work_content, &mapped_refs);

        (normalized.to_string(), new_content)
    } else {
        let mut modified = String::new();
        let mut cursor = 0usize;
        for (start, end, edit) in &sorted {
            modified.push_str(&normalized[cursor..*start]);
            modified.push_str(&normalize_to_lf(&edit.new_text));
            cursor = *end;
        }
        modified.push_str(&normalized[cursor..]);
        (normalized.to_string(), modified)
    };

    // No-change detection
    if base_content == new_content {
        return if edits.len() == 1 {
            Err(format!(
                "No changes made to {}. The replacement produced identical content. \
                 This might indicate an issue with special characters or the text not \
                 existing as expected.",
                path_str
            ))
        } else {
            Err(format!(
                "No changes made to {}. The replacements produced identical content.",
                path_str
            ))
        };
    }

    let diff = compute_diff(&base_content, &new_content, path_str);

    Ok((base_content, new_content, diff))
}

/// Read a file and compute what the diff would look like if edits were applied.
/// This is used for the preview rendering (matching pi's computeEditsDiff).
fn compute_edits_diff(
    path_str: &str,
    edits: &[Edit],
    cwd: &std::path::Path,
) -> Result<String, String> {
    let abs_path = {
        let p = std::path::Path::new(path_str);
        if p.is_absolute() {
            p.to_path_buf()
        } else {
            cwd.join(p)
        }
    };

    let raw_content =
        std::fs::read_to_string(&abs_path).map_err(|e| format!("Could not read file: {}", e))?;

    let (_bom, content) = strip_bom(&raw_content);
    let normalized = normalize_to_lf(content);

    let (_, _, diff) = apply_edits_and_compute_diff(&normalized, edits, path_str)?;

    Ok(diff)
}

#[async_trait::async_trait]
impl yoagent::types::AgentTool for EditTool {
    fn name(&self) -> &str {
        "edit"
    }
    fn label(&self) -> &str {
        "edit"
    }
    fn description(&self) -> &str {
        "Edit a single file using exact text replacement. Every edits[].oldText must match a \
         unique, non-overlapping region of the original file. If two changes affect the same \
         block or nearby lines, merge them into one edit instead of emitting overlapping edits. \
         Do not include large unchanged regions just to connect distant changes."
    }
    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "required": ["path", "edits"],
            "additionalProperties": false,
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Path to the file to edit"
                },
                "edits": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "required": ["oldText", "newText"],
                        "additionalProperties": false,
                        "properties": {
                            "oldText": {
                                "type": "string",
                                "description": "Text to search for"
                            },
                            "newText": {
                                "type": "string",
                                "description": "Text to replace with"
                            }
                        }
                    }
                }
            }
        })
    }
    async fn execute(
        &self,
        params: serde_json::Value,
        ctx: yoagent::types::ToolContext,
    ) -> std::result::Result<yoagent::types::ToolResult, yoagent::types::ToolError> {
        let path_str = params["path"]
            .as_str()
            .ok_or_else(|| {
                yoagent::types::ToolError::InvalidArgs("Missing 'path' argument".into())
            })?
            .to_string();
        let edits: Vec<Edit> = serde_json::from_value(params["edits"].clone())
            .map_err(|e| yoagent::types::ToolError::InvalidArgs(format!("Invalid edits: {}", e)))?;

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        let cwd = self.cwd.clone();
        let cancel = ctx.cancel.clone();
        let ops = self.operations.clone();
        let path_for_queue = path_str.clone();
        let cwd_for_closure = cwd.clone();
        let edits_for_closure = edits.clone();

        // Wrap the entire read-edit-write in a per-file mutation queue so
        // concurrent edits to the same file are serialized (pi-style).
        let output = crate::builtin::file_mutation_queue::with_file_mutation_queue(
            &path_for_queue,
            &cwd,
            || async move {
                let abs_path = {
                    let p = std::path::Path::new(&path_str);
                    if p.is_absolute() {
                        p.to_path_buf()
                    } else {
                        cwd_for_closure.join(p)
                    }
                };

                if cancel.is_cancelled() {
                    anyhow::bail!("Operation cancelled");
                }

                // Check file accessibility using operations
                ops.access(&abs_path).await?;

                if cancel.is_cancelled() {
                    anyhow::bail!("Operation cancelled");
                }

                // Read file using operations
                let raw_content = ops.read_file(&abs_path).await?;

                if cancel.is_cancelled() {
                    anyhow::bail!("Operation cancelled");
                }

                // ── 1. BOM handling ──
                let (bom, content) = strip_bom(&raw_content);

                // ── 2. Line ending handling ──
                let original_ending = detect_line_ending(content);
                let normalized = normalize_to_lf(content);

                // ── 3-8. Apply edits and compute diff ──
                let (_base_content, new_content, diff) =
                    apply_edits_and_compute_diff(&normalized, &edits_for_closure, &path_str)
                        .map_err(|e| anyhow::anyhow!("{}", e))?;

                if cancel.is_cancelled() {
                    anyhow::bail!("Operation cancelled");
                }

                // ── 9. Write back with original line endings and BOM ──
                let final_content =
                    bom.to_string() + &restore_line_endings(&new_content, original_ending);
                ops.write_file(&abs_path, &final_content).await?;

                if cancel.is_cancelled() {
                    anyhow::bail!("Operation cancelled");
                }

                // ── 10. Compute firstChangedLine and patch ──
                let first_changed_line = extract_first_changed_line(&diff);
                let patch = generate_unified_patch(&path_str, &_base_content, &new_content);

                // ── 11. Return result ──
                let noun = if edits.len() == 1 { "block" } else { "blocks" };
                let msg = format!(
                    "Successfully replaced {} {} in {}.",
                    edits.len(),
                    noun,
                    path_str
                );
                let details = serde_json::json!({
                    "diff": diff.trim_end(),
                    "path": path_str,
                    "patch": patch,
                    "firstChangedLine": first_changed_line,
                });
                Ok::<_, anyhow::Error>((msg, details))
            },
        )
        .await
        .map_err(|e| yoagent::types::ToolError::Failed(e.to_string()))?;

        let (msg, details) = output;
        Ok(yoagent::types::ToolResult {
            content: vec![yoagent::types::Content::Text { text: msg }],
            details,
        })
    }
}

// ── Edit tool renderer (stateful, with preview) ─────────────────

/// Cached preview of what the edit will look like.
#[derive(Debug, Clone)]
struct EditPreview {
    diff: String,
    error: Option<String>,
}

/// Tool renderer for the `edit` tool.
/// Uses `renderShell: "self"` - renders its own framing without colored box.
/// Shows a preview of what will change in the call header (matching pi behavior).
#[derive(Clone)]
struct EditRenderer {
    /// Cached diff preview, computed from file system during render_call.
    /// Protected by Mutex for interior mutability in a Sync trait impl.
    preview: std::sync::Arc<Mutex<Option<EditPreview>>>,
}

impl EditRenderer {
    fn new() -> Self {
        Self {
            preview: std::sync::Arc::new(Mutex::new(None)),
        }
    }
}

impl ToolRenderer for EditRenderer {
    fn render_self(&self) -> bool {
        true
    }

    fn render_bg_key(&self) -> Option<&'static str> {
        // Match pi's edit tool background management:
        // - If preview exists and has an error → toolErrorBg
        // - If preview exists and is valid → toolSuccessBg (preview succeeded)
        // - If settled error (post-exec) → toolErrorBg
        // - Otherwise → toolPendingBg (no preview yet)
        if let Ok(p) = self.preview.lock()
            && let Some(ref preview) = *p
        {
            if preview.error.is_some() {
                return Some("toolErrorBg");
            }
            return Some("toolSuccessBg");
        }
        None // Let compute_bg_key use default (toolPendingBg)
    }

    fn render_call(
        &self,
        args: &serde_json::Value,
        _width: usize,
        theme: &dyn Theme,
        ctx: &ToolRenderContext,
    ) -> Vec<String> {
        let path = args
            .get("file_path")
            .or_else(|| args.get("path"))
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let short = if let Ok(home) = std::env::var("HOME") {
            path.replacen(&home, "~", 1)
        } else {
            path.to_string()
        };
        let path_disp = if short.is_empty() {
            String::new()
        } else {
            theme.fg_key(ThemeKey::Accent, &short)
        };

        let header = format!(
            "{} {}",
            theme.fg_key(ThemeKey::ToolTitle, &theme.bold("edit")),
            path_disp
        );

        let mut lines = vec![header];

        // Decide what diff to show:
        // 1. If execution completed and details are available, use actual diff from details
        // 2. Otherwise, if args are complete and we have a cached preview, show that
        let actual_diff = ctx
            .details
            .as_ref()
            .and_then(|d| d.get("diff"))
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let diff_to_show = if let Some(ref d) = actual_diff {
            Some(d.clone())
        } else if ctx.args_complete && !ctx.is_partial {
            // After execution, if details aren't in context (unlikely), fallback to preview
            self.preview.lock().ok().and_then(|p| {
                p.as_ref().map(|preview| {
                    if let Some(ref err) = preview.error {
                        format!("error: {}", err)
                    } else {
                        preview.diff.clone()
                    }
                })
            })
        } else if ctx.args_complete && actual_diff.is_none() {
            // Pending state: try to use cached preview or spawn async computation
            let cached = self.preview.lock().ok().and_then(|p| p.clone());

            if let Some(preview) = cached {
                if let Some(ref err) = preview.error {
                    Some(format!("error: {}", err))
                } else {
                    Some(preview.diff.clone())
                }
            } else if let Some((path_str, edits)) = parse_path_edits(args) {
                // If no cached preview, check if preview is already being computed
                // (pending flag not started yet). If not, spawn async computation.
                // First, check a "pending" flag to avoid duplicate spawns.
                let mut preview_lock = self.preview.lock().unwrap();
                if preview_lock.is_some() {
                    // Preview was set between lock releases (race)
                    drop(preview_lock);
                    let cached = self.preview.lock().ok().and_then(|p| p.clone());
                    cached.map(|preview| {
                        if let Some(ref err) = preview.error {
                            format!("error: {}", err)
                        } else {
                            preview.diff.clone()
                        }
                    })
                } else {
                    // Mark as pending (store a place-holder)
                    *preview_lock = Some(EditPreview {
                        diff: String::new(),
                        error: Some("pending".to_string()),
                    });
                    drop(preview_lock);

                    // Spawn async computation (matching pi's computeEditsDiff called from renderCall)
                    let preview_arc = self.preview.clone();
                    let path_owned = path_str.clone();
                    let edits_owned = edits.clone();
                    let cwd_owned = ctx.cwd.clone();
                    let invalidate_tx = ctx.invalidate.clone();
                    tokio::spawn(async move {
                        let result = compute_edits_diff(
                            &path_owned,
                            &edits_owned,
                            std::path::Path::new(&cwd_owned),
                        );
                        let (diff, error) = match result {
                            Ok(d) => (d, None),
                            Err(e) => (String::new(), Some(e)),
                        };
                        if let Ok(mut p) = preview_arc.lock() {
                            *p = Some(EditPreview { diff, error });
                        }
                        // Notify UI to re-render
                        if let Some(ref tx) = invalidate_tx {
                            let _ = tx.send(());
                        }
                    });

                    // No diff to show yet (pending)
                    None
                }
            } else {
                None
            }
        } else {
            None
        };

        if let Some(ref diff) = diff_to_show {
            if diff.starts_with("error: ") {
                // Show error inline (dimmed, matching pi error display)
                lines.push(String::new());
                lines.push(theme.fg_key(ThemeKey::Muted, diff));
            } else if !diff.is_empty() {
                lines.push(String::new());
                let rendered_lines = crate::tui::components::diff::render_diff(diff, theme);
                lines.extend(rendered_lines);
            }
        }

        lines
    }

    fn render_result(
        &self,
        _content: &str,
        _width: usize,
        theme: &dyn Theme,
        ctx: &ToolRenderContext,
    ) -> Vec<String> {
        // Result is already shown in the call header (via render_call using ctx.details).
        // If there's an error message not already shown, return it.
        if ctx.is_error {
            // Error case: content has the error text
            if !_content.is_empty() {
                let msg = _content;
                // Check if this error is already shown as preview
                let preview_err = self
                    .preview
                    .lock()
                    .ok()
                    .and_then(|p| p.as_ref().and_then(|preview| preview.error.clone()));
                if preview_err.as_deref() != Some(msg) {
                    return vec![String::new(), theme.fg_key(ThemeKey::Error, msg)];
                }
            }
        }

        Vec::new()
    }
}

// ── Diff utility functions ───────────────────────────────────────

/// Extract the first changed line number from a diff string.
/// Scans for the first `+` or `-` prefixed line with a line number.
fn extract_first_changed_line(diff: &str) -> Option<usize> {
    for line in diff.lines() {
        let bytes = line.as_bytes();
        if bytes.is_empty() {
            continue;
        }
        let prefix = bytes[0] as char;
        if prefix != '+' && prefix != '-' {
            continue;
        }
        // Parse the line number from the rest
        let rest = &line[1..];
        let num_str: String = rest
            .chars()
            .take_while(|c| c.is_whitespace() || c.is_ascii_digit())
            .collect();
        if let Ok(num) = num_str.trim().parse::<usize>() {
            return Some(num);
        }
    }
    None
}

/// Generate a unified diff patch string from original and modified content.
/// Uses basic hunk structure matching pi's `generateUnifiedPatch`.
fn generate_unified_patch(path: &str, original: &str, modified: &str) -> String {
    let orig_lines: Vec<&str> = original.lines().collect();
    let mod_lines: Vec<&str> = modified.lines().collect();

    let n = orig_lines.len();
    let m = mod_lines.len();
    let mut dp = vec![vec![0usize; m + 1]; n + 1];
    for i in 1..=n {
        for j in 1..=m {
            if orig_lines[i - 1] == mod_lines[j - 1] {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = dp[i - 1][j].max(dp[i][j - 1]);
            }
        }
    }

    // Backtrack to build sequence of changes
    let mut changes: Vec<(char, &str)> = Vec::new();
    let mut i = n;
    let mut j = m;
    while i > 0 || j > 0 {
        if i > 0 && j > 0 && orig_lines[i - 1] == mod_lines[j - 1] {
            changes.push((' ', orig_lines[i - 1]));
            i -= 1;
            j -= 1;
        } else if j > 0 && (i == 0 || dp[i][j - 1] >= dp[i - 1][j]) {
            changes.push(('+', mod_lines[j - 1]));
            j -= 1;
        } else {
            changes.push(('-', orig_lines[i - 1]));
            i -= 1;
        }
    }
    changes.reverse();

    // Group into hunks
    const CTX: usize = 3;
    let mut hunks: Vec<String> = Vec::new();
    let mut pos = 0;

    while pos < changes.len() {
        while pos < changes.len() && changes[pos].0 == ' ' {
            pos += 1;
        }
        if pos >= changes.len() {
            break;
        }

        let hunk_start = pos.saturating_sub(CTX);
        let hunk_end = (pos + 3 * CTX).min(changes.len());

        // Compute old/new line ranges
        let mut old_line = 1usize;
        let mut new_line = 1usize;
        for (tag, _) in changes.iter().take(pos.saturating_sub(CTX)) {
            match tag {
                ' ' => {
                    old_line += 1;
                    new_line += 1;
                }
                '-' => old_line += 1,
                '+' => new_line += 1,
                _ => {}
            }
        }

        let old_start = old_line;
        let new_start = new_line;

        // Count hunk size
        let mut old_count = 0usize;
        let mut new_count = 0usize;
        for (tag, _) in changes[hunk_start..hunk_end].iter() {
            match tag {
                ' ' => {
                    old_count += 1;
                    new_count += 1;
                }
                '-' => old_count += 1,
                '+' => new_count += 1,
                _ => {}
            }
        }

        let mut hunk = format!(
            "@@ -{},{} +{},{} @@\n",
            old_start, old_count, new_start, new_count
        );

        for (tag, text) in changes[hunk_start..hunk_end].iter() {
            match tag {
                ' ' => hunk.push_str(&format!(" {}", text)),
                '-' => hunk.push_str(&format!("-{}", text)),
                '+' => hunk.push_str(&format!("+{}", text)),
                _ => {}
            }
            hunk.push('\n');
        }

        hunks.push(hunk);
        pos = hunk_end;
    }

    if hunks.is_empty() {
        return String::new();
    }

    let mut patch = format!("--- a/{}\n+++ b/{}\n", path, path);
    for hunk in &hunks {
        patch.push_str(hunk);
    }

    patch
}

// ═══════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use yoagent::AgentTool;

    fn tmp_dir() -> std::path::PathBuf {
        let d = std::env::temp_dir().join(format!("rab-edit-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&d).unwrap();
        d
    }

    fn make_tool() -> (EditTool, std::path::PathBuf) {
        let tmp = tmp_dir();
        let tool = EditTool {
            cwd: tmp.clone(),
            operations: Arc::new(DefaultEditOperations),
        };
        (tool, tmp)
    }

    fn tool_ctx() -> yoagent::types::ToolContext {
        yoagent::types::ToolContext {
            tool_call_id: "id".into(),
            tool_name: "edit".into(),
            cancel: tokio_util::sync::CancellationToken::new(),
            on_update: None,
            on_progress: None,
        }
    }

    fn yo_msg_text(content: &[yoagent::types::Content]) -> String {
        content
            .iter()
            .filter_map(|c| {
                if let yoagent::types::Content::Text { text } = c {
                    Some(text.as_str())
                } else {
                    None
                }
            })
            .collect::<Vec<_>>()
            .join("")
    }

    async fn exec_ok(tool: &EditTool, args: serde_json::Value) -> String {
        let args = prepare_edit_tool_args(args);
        let result = tool.execute(args, tool_ctx()).await.unwrap();
        yo_msg_text(&result.content)
    }

    async fn exec_ok_details(
        tool: &EditTool,
        args: serde_json::Value,
    ) -> (String, Option<serde_json::Value>) {
        let args = prepare_edit_tool_args(args);
        let result = tool.execute(args, tool_ctx()).await.unwrap();
        let text = yo_msg_text(&result.content);
        (text, Some(result.details))
    }

    async fn exec_err(tool: &EditTool, args: serde_json::Value) -> String {
        let args = prepare_edit_tool_args(args);
        tool.execute(args, tool_ctx())
            .await
            .unwrap_err()
            .to_string()
    }

    async fn is_err(tool: &EditTool, args: serde_json::Value) -> bool {
        let args = prepare_edit_tool_args(args);
        tool.execute(args, tool_ctx()).await.is_err()
    }

    #[tokio::test]
    async fn single_edit_replaces_text() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("file.txt");
        std::fs::write(&path, "hello world\nfoo bar\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "foo bar", "newText": "baz qux"}]
            }),
        )
        .await;

        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            "hello world\nbaz qux\n"
        );
    }

    #[tokio::test]
    async fn multiple_edits_replaces_all() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("file.txt");
        std::fs::write(&path, "aaa\nbbb\nccc\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [
                    {"oldText": "aaa", "newText": "111"},
                    {"oldText": "ccc", "newText": "333"}
                ]
            }),
        )
        .await;

        assert_eq!(std::fs::read_to_string(&path).unwrap(), "111\nbbb\n333\n");
    }

    #[tokio::test]
    async fn non_unique_oldtext_errors() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("file.txt");
        std::fs::write(&path, "dup\ndup\n").unwrap();

        assert!(
            is_err(
                &tool,
                serde_json::json!({
                    "path": path.to_str().unwrap(),
                    "edits": [{"oldText": "dup", "newText": "x"}]
                }),
            )
            .await
        );
    }

    #[tokio::test]
    async fn missing_oldtext_errors() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("file.txt");
        std::fs::write(&path, "content\n").unwrap();

        let err = exec_err(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "not found", "newText": "x"}]
            }),
        )
        .await;
        assert!(err.contains("Could not find"));
    }

    #[tokio::test]
    async fn overlapping_edits_error() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("file.txt");
        std::fs::write(&path, "abcdef\n").unwrap();

        assert!(
            is_err(
                &tool,
                serde_json::json!({
                    "path": path.to_str().unwrap(),
                    "edits": [
                        {"oldText": "abc", "newText": "1"},
                        {"oldText": "bcd", "newText": "2"}
                    ]
                }),
            )
            .await
        );
    }

    #[tokio::test]
    async fn empty_edits_errors() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("file.txt");
        std::fs::write(&path, "content\n").unwrap();

        assert!(
            is_err(
                &tool,
                serde_json::json!({"path": path.to_str().unwrap(), "edits": []}),
            )
            .await
        );
    }

    // ── BOM handling ─────────────────────────────────────────

    #[tokio::test]
    async fn handles_bom() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("bom.txt");
        std::fs::write(&path, "\u{FEFF}hello world\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "hello world", "newText": "goodbye"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.starts_with('\u{FEFF}'));
        assert!(content.contains("goodbye"));
    }

    #[tokio::test]
    async fn preserves_bom_when_no_edit_at_start() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("bom2.txt");
        std::fs::write(&path, "\u{FEFF}line1\nline2\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "line2", "newText": "modified"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.starts_with('\u{FEFF}'));
        assert!(content.contains("modified"));
    }

    // ── CRLF handling ────────────────────────────────────────

    #[tokio::test]
    async fn preserves_crlf() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("crlf.txt");
        std::fs::write(&path, "hello\r\nworld\r\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "world", "newText": "universe"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert_eq!(content, "hello\r\nuniverse\r\n");
    }

    #[tokio::test]
    async fn handles_mixed_line_endings() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("mixed.txt");
        std::fs::write(&path, "line1\r\nline2\nline3\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "line2", "newText": "modified"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert_eq!(content, "line1\r\nmodified\r\nline3\r\n");
    }

    #[tokio::test]
    async fn lf_only_stays_lf() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("lf.txt");
        std::fs::write(&path, "hello\nworld\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "world", "newText": "universe"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert_eq!(content, "hello\nuniverse\n");
    }

    // ── Fuzzy matching ───────────────────────────────────────

    #[tokio::test]
    async fn fuzzy_match_trailing_whitespace() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("trailing.txt");
        std::fs::write(&path, "hello world  \nnext line\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "hello world", "newText": "hi there"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        // Pi behavior: exact match ("hello world" is a substring of "hello world  "),
        // so trailing whitespace on unchanged suffix is preserved.
        assert_eq!(content, "hi there  \nnext line\n");
    }

    #[tokio::test]
    async fn fuzzy_match_smart_quotes() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("quotes.txt");
        std::fs::write(&path, "he said \u{201C}hello\u{201D}\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "he said \"hello\"", "newText": "she said \"hi\""}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert_eq!(content, "she said \"hi\"\n");
    }

    #[tokio::test]
    async fn fuzzy_match_dashes() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("dashes.txt");
        std::fs::write(&path, "foo \u{2014} bar\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "foo - bar", "newText": "baz"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert_eq!(content, "baz\n");
    }

    // ── No-change detection ──────────────────────────────────

    #[tokio::test]
    async fn no_change_identical_edit_errors() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("nochange.txt");
        std::fs::write(&path, "hello\nworld\n").unwrap();

        let err = exec_err(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "hello", "newText": "hello"}]
            }),
        )
        .await;
        assert!(
            err.contains("No changes made"),
            "expected no-change error but got: {}",
            err
        );
    }

    // ── Input normalization ──────────────────────────────────

    #[tokio::test]
    async fn legacy_oldtext_newtext() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("legacy.txt");
        std::fs::write(&path, "hello world\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "oldText": "hello world",
                "newText": "goodbye"
            }),
        )
        .await;

        assert_eq!(std::fs::read_to_string(&path).unwrap(), "goodbye\n");
    }

    #[tokio::test]
    async fn edits_as_json_string() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("jsonstr.txt");
        std::fs::write(&path, "aaa\nbbb\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": r#"[{"oldText": "bbb", "newText": "xxx"}]"#
            }),
        )
        .await;

        assert_eq!(std::fs::read_to_string(&path).unwrap(), "aaa\nxxx\n");
    }

    // ── Structured details (diff no longer embedded in content) ──

    #[tokio::test]
    async fn result_content_has_no_diff_block() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("diff_test.txt");
        std::fs::write(&path, "aaa\nbbb\nccc\n").unwrap();

        let (content, details) = exec_ok_details(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "bbb", "newText": "xxx"}]
            }),
        )
        .await;

        // Content should NOT contain a ```diff block anymore
        assert!(
            !content.contains("```diff"),
            "content should not contain diff block, got: {}",
            content
        );
        assert!(content.contains("Successfully replaced 1 block"));

        // Diff should be in details
        let details_obj = details.expect("details should be present");
        let diff = details_obj
            .get("diff")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        assert!(
            diff.contains("-2 bbb"),
            "diff should contain '-2 bbb' but got: {}",
            diff
        );
        assert!(
            diff.contains("+2 xxx"),
            "diff should contain '+2 xxx' but got: {}",
            diff
        );
    }

    // ── Fuzzy matching preserves unchanged lines (using new line-span mapping) ──

    #[tokio::test]
    async fn fuzzy_preserves_unchanged_line_trailing_whitespace() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("fuzzy_preserve.txt");
        // First line has trailing spaces, second has smart quotes (forces fuzzy)
        std::fs::write(&path, "keep this line  \nchange \u{201C}this\u{201D}\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "change \"this\"", "newText": "changed"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        // Unchanged first line preserves trailing spaces (pi behavior)
        assert!(
            content.starts_with("keep this line  "),
            "expected preserved trailing spaces but got: {:?}",
            content
        );
        assert!(content.contains("changed\n"), "got: {:?}", content);
    }

    // ── Empty oldText ────────────────────────────────────────

    #[tokio::test]
    async fn empty_oldtext_errors() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("empty.txt");
        std::fs::write(&path, "content\n").unwrap();

        let err = exec_err(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "", "newText": "x"}]
            }),
        )
        .await;
        assert!(err.contains("empty"));
    }

    // ── Relative paths ───────────────────────────────────────

    #[tokio::test]
    async fn relative_path_resolves_to_cwd() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("relative.txt");
        std::fs::write(&path, "hello\n").unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": "relative.txt",
                "edits": [{"oldText": "hello", "newText": "hi"}]
            }),
        )
        .await;

        assert_eq!(std::fs::read_to_string(&path).unwrap(), "hi\n");
    }

    // ── NFKC normalization test ─────────────────────────────

    #[tokio::test]
    async fn fuzzy_match_nfkc_composed_vs_decomposed() {
        let (tool, tmp) = make_tool();
        let path = tmp.join("nfkc.txt");
        // "café" in NFD (decomposed): cafe + combining acute accent
        let nfd: String = "cafe\u{0301}".chars().collect();
        std::fs::write(&path, format!("{} rest\n", nfd)).unwrap();

        exec_ok(
            &tool,
            serde_json::json!({
                "path": path.to_str().unwrap(),
                "edits": [{"oldText": "café", "newText": "changed"}]
            }),
        )
        .await;

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(
            content.starts_with("changed"),
            "expected 'changed' but got: {:?}",
            content
        );
    }
}

#[cfg(test)]
mod fuzzy_tests {
    use super::*;

    #[test]
    fn test_strip_trailing_whitespace() {
        assert_eq!(
            normalize_for_fuzzy_match("hello   \nworld  "),
            "hello\nworld"
        );
    }

    #[test]
    fn test_smart_quotes() {
        assert_eq!(
            normalize_for_fuzzy_match("\u{2018}hello\u{2019} \u{201C}world\u{201D}"),
            "'hello' \"world\""
        );
    }

    #[test]
    fn test_dashes() {
        assert_eq!(normalize_for_fuzzy_match("a\u{2014}b"), "a-b");
        assert_eq!(normalize_for_fuzzy_match("a\u{2013}b"), "a-b");
    }

    #[test]
    fn test_nbsp() {
        assert_eq!(normalize_for_fuzzy_match("a\u{00A0}b"), "a b");
    }

    #[test]
    fn test_preserves_trailing_newline() {
        assert_eq!(normalize_for_fuzzy_match("hello\n"), "hello\n");
        assert_eq!(
            normalize_for_fuzzy_match("hello\nworld\n"),
            "hello\nworld\n"
        );
    }

    #[test]
    fn test_nfkc_normalization() {
        // é composed (NFC) vs decomposed (NFD) + NFKC
        let composed = "café";
        let decomposed: String = "cafe\u{0301}".chars().collect();
        assert_eq!(
            normalize_for_fuzzy_match(composed),
            normalize_for_fuzzy_match(&decomposed),
            "NFKC should make composed and decomposed café match"
        );
    }
}

#[cfg(test)]
mod diff_tests {
    use super::*;

    #[test]
    fn test_simple_diff() {
        let orig = "aaa\nbbb\nccc\n";
        let modified = "aaa\nxxx\nccc\n";
        let diff = compute_diff(orig, modified, "test.txt");
        assert!(
            diff.contains("-2 bbb"),
            "diff should contain -2 bbb but got: {}",
            diff
        );
        assert!(
            diff.contains("+2 xxx"),
            "diff should contain +2 xxx but got: {}",
            diff
        );
    }

    #[test]
    fn test_no_changes() {
        let text = "hello\nworld\n";
        let diff = compute_diff(text, text, "f.txt");
        assert!(diff.is_empty(), "no changes should produce empty diff");
    }

    #[test]
    fn test_multiple_hunks() {
        let orig = "a\nb\nc\nd\ne\nf\ng\nh\n";
        let modified = "a\nX\nc\nd\ne\nY\ng\nh\n";
        let diff = compute_diff(orig, modified, "f.txt");
        assert!(
            diff.contains("-2 b"),
            "should contain -2 b but got: {}",
            diff
        );
        assert!(
            diff.contains("+2 X"),
            "should contain +2 X but got: {}",
            diff
        );
        assert!(
            diff.contains("-6 f"),
            "should contain -6 f but got: {}",
            diff
        );
        assert!(
            diff.contains("+6 Y"),
            "should contain +6 Y but got: {}",
            diff
        );
    }

    #[test]
    fn test_apply_replacements_preserving_unchanged_lines() {
        let original = "keep this  \nchange this\nkeep that  \n";
        let base = "keep this\nchange this\nkeep that\n";
        // matchIndex 10, matchLength 11 covers "change this" (bytes 10..21 in base)
        let replacements = vec![(10usize, 11usize, "modified")];
        let result = apply_replacements_preserving_unchanged_lines(original, base, &replacements);
        assert_eq!(result, "keep this  \nmodified\nkeep that  \n");
    }
}