mermaid-cli 0.29.0

Open-source AI pair programmer with agentic capabilities. Local-first with Ollama, native tool calling, and beautiful TUI.
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
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
//! Filesystem tools ported to `ToolExecutor`.
//!
//! This is the proof-of-pattern tool impl for C3: `ReadFileTool` and
//! `WriteFileTool`. They hook the `ExecContext::token` so Ctrl+C
//! cancels mid-read (relevant for large files on slow storage), and
//! they emit `ProgressEvent::Status` breadcrumbs for multi-file
//! operations the old code couldn't surface without an observer
//! callback.
//!
//! The implementations don't try to out-clever the existing tool
//! behavior in `src/agents/filesystem.rs`. Same semantics, same error
//! shapes — just wrapped in the new trait so future tools only have
//! to learn this surface.

use mermaid_domain::ProgressEvent;
use std::path::{Path, PathBuf};

use async_trait::async_trait;

use mermaid_domain::{ToolDefinition, ToolMetadata, ToolOutcome, ToolRunMetadata};
use mermaid_model::constants::MAX_RESPONSE_CHARS as MAX_FILE_READ_BYTES;

use super::super::ctx::ExecContext;
use super::ToolExecutor;
use super::path_safety::{
    AllowedRoots, PathContainment, ResolvedInRoot, relative_within, resolve_in_roots,
    resolve_path_within,
};

/// Small helper for building a `ToolDefinition` with a typical
/// JSON-schema-shaped `input_schema`. Keeps the per-tool definitions
/// readable.
fn defn(name: &str, description: &str, input_schema: serde_json::Value) -> ToolDefinition {
    ToolDefinition {
        name: name.to_string(),
        description: description.to_string(),
        input_schema,
    }
}

/// Aggregate cap for a multi-file `read_file` result (#F45). Each file is
/// individually bounded at `MAX_RESPONSE_CHARS` by `read_one`, but a batch of up
/// to `MAX_BATCH_TOOL_ITEMS` files could otherwise sum to ~12.8 MB in a single
/// tool result — far past any sane model-context budget. This bounds the
/// combined total; single-file reads are already bounded and unaffected.
const MAX_READ_AGGREGATE_CHARS: usize = mermaid_model::constants::MAX_RESPONSE_CHARS;

/// Entries kept for same-turn duplicate-read suppression, across all live
/// scopes. Hashes only — never content — so the bound is about map hygiene,
/// not memory pressure.
const READ_DEDUP_CAP: usize = 128;

/// One remembered read: which context read which path, in which turn, and
/// the content hash that proves the repeat is byte-identical.
struct ReadDedupEntry {
    scope: String,
    path: String,
    turn: u64,
    hash: [u8; 32],
    line_count: usize,
}

/// Same-turn duplicate reads, process-global (mirrors `web.rs`'s snapshot
/// store). The `20260806` field logs show the same file read up to 14 times
/// per session at full length — sometimes twice within one turn, where the
/// earlier result is by construction still in the model's request. Only
/// that provably-safe window is deduped: across turns a re-read may be a
/// legitimate refresh (post-edit, post-compaction). Equality is proven by
/// content hash — not mtime, which lies on some drives — so a change made
/// by ANY path (`write_file`, `apply_patch`, `execute_command`, the user's
/// editor) yields full content again with no invalidation hooks to forget.
static READ_DEDUP: std::sync::OnceLock<
    std::sync::Mutex<std::collections::VecDeque<ReadDedupEntry>>,
> = std::sync::OnceLock::new();

/// The identity a dedup entry belongs to. Session/task ids separate
/// concurrent contexts (a subagent's context is not the parent's — each
/// must receive its own full read); the workdir separates anonymous test
/// harness contexts, which reuse small turn ids.
fn read_dedup_scope(ctx: &ExecContext) -> String {
    format!(
        "{}|{}|{}",
        ctx.session_id.as_deref().unwrap_or(""),
        ctx.task_id.as_deref().unwrap_or(""),
        ctx.workdir.display(),
    )
}

/// Returns the short reuse note when `content` is byte-identical to what an
/// earlier read of `path` already returned THIS turn; otherwise records the
/// read and returns `None` (full content flows). The note names the line
/// count and the recovery paths so the model is taught, not stonewalled.
fn duplicate_read_note(ctx: &ExecContext, path: &str, content: &str) -> Option<String> {
    use sha2::{Digest, Sha256};
    let hash: [u8; 32] = Sha256::digest(content.as_bytes()).into();
    let line_count = content.lines().count();
    let scope = read_dedup_scope(ctx);
    let mut store = READ_DEDUP
        .get_or_init(|| std::sync::Mutex::new(std::collections::VecDeque::new()))
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    if let Some(entry) = store
        .iter_mut()
        .find(|e| e.scope == scope && e.path == path)
    {
        let identical_this_turn = entry.turn == ctx.turn.0 && entry.hash == hash;
        entry.turn = ctx.turn.0;
        entry.hash = hash;
        entry.line_count = line_count;
        return identical_this_turn.then(|| {
            format!(
                "{path}: unchanged since your read earlier this turn — the full \
                 content ({line_count} lines) is already in this turn's tool \
                 results; reuse it. A read after the file changes, or in a later \
                 turn, returns the full content again."
            )
        });
    }
    store.push_back(ReadDedupEntry {
        scope,
        path: path.to_string(),
        turn: ctx.turn.0,
        hash,
        line_count,
    });
    if store.len() > READ_DEDUP_CAP {
        store.pop_front();
    }
    None
}

/// `read_file` — read one or more files and return their contents
/// joined with section markers.
pub struct ReadFileTool;

#[async_trait]
impl ToolExecutor for ReadFileTool {
    fn name(&self) -> &'static str {
        "read_file"
    }

    fn schema(&self) -> ToolDefinition {
        defn(
            "read_file",
            "Read the contents of one or more files from disk. Relative paths resolve relative to the project directory; absolute paths may resolve anywhere on disk.",
            serde_json::json!({
                "type": "object",
                "properties": {
                    "path": { "type": "string", "description": "File to read (single)." },
                    "paths": {
                        "type": "array",
                        "items": { "type": "string" },
                        "description": "Multiple files to read sequentially, in order."
                    }
                },
                "oneOf": [
                    { "required": ["path"] },
                    { "required": ["paths"] }
                ]
            }),
        )
    }

    async fn execute(&self, args: serde_json::Value, ctx: ExecContext) -> ToolOutcome {
        let paths = match extract_paths(&args) {
            Ok(p) => p,
            Err(e) => return ToolOutcome::error(e, None),
        };
        if paths.is_empty() {
            return ToolOutcome::error("read_file requires at least one path", None);
        }
        let view = match View::from_args(&args) {
            Ok(view) => view,
            Err(e) => return ToolOutcome::error(e, None),
        };

        let start = std::time::Instant::now();
        let roots = AllowedRoots::new(&ctx.workdir, ctx.scratchpad.as_deref());
        let mut combined = String::new();
        let mut any_truncated = false;

        for (idx, raw_path) in paths.iter().enumerate() {
            let target = match resolve_read_target(&roots, raw_path) {
                Ok(target) => target,
                Err(e) => {
                    return ToolOutcome::error(
                        format!("{raw_path}: {e}"),
                        Some(start.elapsed().as_secs_f64()),
                    );
                },
            };
            // A path outside every allowed root is an external side effect:
            // it goes through the policy gate before a byte is read, exactly
            // as an external write does.
            if let Some(abs) = &target.external
                && let Some(blocked) = external_read_gate(&ctx, raw_path, abs).await
            {
                return blocked;
            }
            // Race the file read against the turn's cancel token. If
            // the user Ctrl+C's mid-read, we bail immediately.
            tokio::select! {
                biased;
                _ = ctx.token.cancelled() => {
                    return ToolOutcome::cancelled();
                },
                read = read_target(target.root, target.rel) => {
                    let read = read.and_then(|(content, was_truncated, is_dir)| {
                        if is_dir {
                            return Ok((content, was_truncated));
                        }
                        view.apply(&content)
                            .map(|content| (content, was_truncated))
                            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))
                    });
                    match read {
                        Ok((content, was_truncated)) => {
                            // A byte-identical repeat of a read this same turn
                            // collapses to a short reuse note — the earlier
                            // result is still in the model's request. The note
                            // is not a truncation: nothing was cut that isn't
                            // already present in full.
                            let (content, was_truncated) =
                                duplicate_read_note(&ctx, raw_path, &content)
                                    .map_or((content, was_truncated), |note| (note, false));
                            any_truncated |= was_truncated;
                            if paths.len() > 1 {
                                let _ = ctx.progress.send(ProgressEvent::Status(
                                    format!("read {}/{}: {}", idx + 1, paths.len(), raw_path),
                                )).await;
                                combined.push_str(&format!(
                                    "=== {raw_path} ===\n{content}\n\n"
                                ));
                            } else {
                                combined = content;
                            }
                        },
                        Err(e) => {
                            return ToolOutcome::error(
                                format!("{raw_path}: {e}"),
                                Some(start.elapsed().as_secs_f64()),
                            );
                        },
                    }
                },
            }
        }

        // F45: bound the COMBINED multi-file result. Each file is already capped
        // at MAX_RESPONSE_CHARS by read_one, but a batch of files can still sum to
        // ~12.8 MB in one tool result — past any sane context budget. Only the
        // multi-file accumulation needs this (single-file output is already
        // bounded); truncate_middle keeps the head AND tail with an elision marker.
        if paths.len() > 1 && combined.len() > MAX_READ_AGGREGATE_CHARS {
            combined = mermaid_model::utils::truncate_middle(&combined, MAX_READ_AGGREGATE_CHARS);
            any_truncated = true;
        }

        let duration_secs = start.elapsed().as_secs_f64();
        let line_count = combined.lines().count();
        let byte_count = combined.len();
        // The REAL truncation flag from the bounded read — not a sniff for the
        // marker string, which a file containing that literal text would
        // falsely trip (#78).
        let truncated = any_truncated;
        ToolOutcome::success(
            combined,
            format!(
                "{} {} read",
                line_count,
                plural(line_count, "line", "lines")
            ),
            duration_secs,
        )
        .with_metadata(ToolRunMetadata {
            detail: ToolMetadata::ReadFile {
                paths,
                line_count,
                byte_count,
                truncated,
            },
            line_count: Some(line_count),
            byte_count: Some(byte_count),
            ..ToolRunMetadata::default()
        })
    }
}

/// `delete_file` — unlink a file. Errors on directories (use
/// `execute_command rm -rf` for those — the model shouldn't be
/// blowing away directories as a routine op).
pub struct DeleteFileTool;

#[async_trait]
impl ToolExecutor for DeleteFileTool {
    fn name(&self) -> &'static str {
        "delete_file"
    }

    fn schema(&self) -> ToolDefinition {
        defn(
            "delete_file",
            "Remove a file from disk. Paths may be relative to the project directory or absolute paths on disk. Fails on directories — use `execute_command rm -rf` for those.",
            serde_json::json!({
                "type": "object",
                "properties": { "path": { "type": "string" } },
                "required": ["path"]
            }),
        )
    }

    async fn execute(&self, args: serde_json::Value, ctx: ExecContext) -> ToolOutcome {
        let Some(raw_path) = args.get("path").and_then(|v| v.as_str()) else {
            return err("delete_file requires 'path'", 0.0);
        };
        let start = std::time::Instant::now();
        let roots = AllowedRoots::new(&ctx.workdir, ctx.scratchpad.as_deref());
        let ResolvedInRoot {
            abs,
            rel,
            root,
            containment,
        } = match resolve_in_roots(&roots, raw_path) {
            Ok(r) => r,
            Err(e) => return err(&format!("delete_file: {e}"), 0.0),
        };
        let pending_action = serde_json::json!({
            "tool": "delete_file",
            "args": { "path": raw_path },
            "workdir": ctx.workdir.display().to_string(),
            "turn_id": ctx.turn.0,
            "call_id": ctx.call_id.0,
            "task_id": ctx.task_id.clone(),
        });
        if let MutationGate::Blocked(outcome) = mutation_policy_outcome(
            &ctx,
            "delete_file",
            raw_path,
            std::slice::from_ref(&abs),
            pending_action,
            containment,
        )
        .await
        {
            return *outcome;
        }
        // Serialize writers to this canonical path: sibling tool calls in the same
        // turn run concurrently, so without this the checkpoint + delete could race
        // another writer to the same file. Distinct paths still overlap. Raced
        // against cancellation so a contended lock stays Ctrl+C-responsive.
        let _write_guard = tokio::select! {
            biased;
            _ = ctx.token.cancelled() => return ToolOutcome::cancelled(),
            g = super::path_lock::lock_path(&abs) => g,
        };
        // Scratchpad files are session-private and ephemeral — never
        // checkpointed into the project's restore history.
        if ctx.config.safety.checkpoint_on_mutation
            && containment != PathContainment::Scratchpad
            && let Err(e) = mermaid_runtime::create_checkpoint_for_task(
                &ctx.workdir,
                std::slice::from_ref(&abs),
                Some(serde_json::json!({
                    "tool": "delete_file",
                    "path": raw_path,
                })),
                ctx.checkpoint_origin(),
            )
        {
            return err(&format!("delete_file checkpoint failed: {e}"), 0.0);
        }
        let display = raw_path.to_string();

        tokio::select! {
            biased;
            _ = ctx.token.cancelled() => ToolOutcome::cancelled(),
            result = tokio::task::spawn_blocking(move || mermaid_runtime::remove_file_beneath(&root, &rel)) => {
                match result {
                    Ok(Ok(())) => {
                        let duration_secs = start.elapsed().as_secs_f64();
                        after_file_mutation(&ctx, "delete_file", &display);
                        ToolOutcome::success(
                            format!("Deleted {display}"),
                            "file deleted",
                            duration_secs,
                        )
                        .with_metadata(ToolRunMetadata {
                            detail: ToolMetadata::DeleteFile { path: display },
                            ..ToolRunMetadata::default()
                        })
                    },
                    Ok(Err(e)) => err(&format!("delete_file({display}): {e}"),
                                       start.elapsed().as_secs_f64()),
                    Err(e) => err(&format!("delete_file join error: {e}"),
                                   start.elapsed().as_secs_f64()),
                }
            }
        }
    }
}

/// `create_directory` — `mkdir -p` semantics.
pub struct CreateDirectoryTool;

#[async_trait]
impl ToolExecutor for CreateDirectoryTool {
    fn name(&self) -> &'static str {
        "create_directory"
    }

    fn schema(&self) -> ToolDefinition {
        defn(
            "create_directory",
            "Create a directory (and any missing parents) at the given path. Paths may be relative to the project directory or absolute paths on disk.",
            serde_json::json!({
                "type": "object",
                "properties": { "path": { "type": "string" } },
                "required": ["path"]
            }),
        )
    }

    async fn execute(&self, args: serde_json::Value, ctx: ExecContext) -> ToolOutcome {
        let Some(raw_path) = args.get("path").and_then(|v| v.as_str()) else {
            return err("create_directory requires 'path'", 0.0);
        };
        let start = std::time::Instant::now();
        let roots = AllowedRoots::new(&ctx.workdir, ctx.scratchpad.as_deref());
        let ResolvedInRoot {
            abs,
            rel,
            root,
            containment,
        } = match resolve_in_roots(&roots, raw_path) {
            Ok(r) => r,
            Err(e) => return err(&format!("create_directory: {e}"), 0.0),
        };
        let pending_action = serde_json::json!({
            "tool": "create_directory",
            "args": { "path": raw_path },
            "workdir": ctx.workdir.display().to_string(),
            "turn_id": ctx.turn.0,
            "call_id": ctx.call_id.0,
            "task_id": ctx.task_id.clone(),
        });
        if let MutationGate::Blocked(outcome) = mutation_policy_outcome(
            &ctx,
            "create_directory",
            raw_path,
            std::slice::from_ref(&abs),
            pending_action,
            containment,
        )
        .await
        {
            return *outcome;
        }
        // Serialize writers to this canonical path (see delete_file). mkdir -p is
        // idempotent, but a uniform gate keeps ordering consistent and cheap.
        let _write_guard = tokio::select! {
            biased;
            _ = ctx.token.cancelled() => return ToolOutcome::cancelled(),
            g = super::path_lock::lock_path(&abs) => g,
        };
        // Scratchpad dirs are session-private and ephemeral — never checkpointed.
        if ctx.config.safety.checkpoint_on_mutation
            && containment != PathContainment::Scratchpad
            && let Err(e) = mermaid_runtime::create_checkpoint_for_task(
                &ctx.workdir,
                std::slice::from_ref(&abs),
                Some(serde_json::json!({
                    "tool": "create_directory",
                    "path": raw_path,
                })),
                ctx.checkpoint_origin(),
            )
        {
            return err(&format!("create_directory checkpoint failed: {e}"), 0.0);
        }
        let display = raw_path.to_string();

        tokio::select! {
            biased;
            _ = ctx.token.cancelled() => ToolOutcome::cancelled(),
            result = tokio::task::spawn_blocking(move || mermaid_runtime::create_dir_all_beneath(&root, &rel)) => {
                match result {
                    Ok(Ok(())) => {
                        let duration_secs = start.elapsed().as_secs_f64();
                        after_file_mutation(&ctx, "create_directory", &display);
                        ToolOutcome::success(
                            format!("Created directory {display}"),
                            "directory created",
                            duration_secs,
                        )
                        .with_metadata(ToolRunMetadata {
                            detail: ToolMetadata::CreateDirectory { path: display },
                            ..ToolRunMetadata::default()
                        })
                    },
                    Ok(Err(e)) => err(&format!("create_directory({display}): {e}"),
                                       start.elapsed().as_secs_f64()),
                    Err(e) => err(&format!("create_directory join error: {e}"),
                                   start.elapsed().as_secs_f64()),
                }
            }
        }
    }
}

/// `write_file` — write a single file, creating parent dirs as needed.
pub struct WriteFileTool;

#[async_trait]
impl ToolExecutor for WriteFileTool {
    fn name(&self) -> &'static str {
        "write_file"
    }

    fn schema(&self) -> ToolDefinition {
        defn(
            "write_file",
            "Write (overwrite) a file at `path` with `content`. Creates parent directories automatically. Paths may be relative to the project directory or absolute. Prefer `apply_patch` for small targeted changes.",
            serde_json::json!({
                "type": "object",
                "properties": {
                    "path": { "type": "string" },
                    "content": { "type": "string" }
                },
                "required": ["path", "content"]
            }),
        )
    }

    async fn execute(&self, args: serde_json::Value, ctx: ExecContext) -> ToolOutcome {
        let Some(path) = args.get("path").and_then(|v| v.as_str()) else {
            return ToolOutcome::error("write_file requires 'path' (string)", None);
        };
        let Some(content) = args.get("content").and_then(|v| v.as_str()) else {
            return ToolOutcome::error("write_file requires 'content' (string)", None);
        };

        let start = std::time::Instant::now();
        let roots = AllowedRoots::new(&ctx.workdir, ctx.scratchpad.as_deref());
        // `rel` is the root-relative name for the confined fd write (the actual
        // byte path).
        let ResolvedInRoot {
            abs: abs_path,
            rel,
            root,
            containment,
        } = match resolve_in_roots(&roots, path) {
            Ok(r) => r,
            Err(e) => return ToolOutcome::error(format!("write_file: {e}"), None),
        };
        let pending_action = serde_json::json!({
            "tool": "write_file",
            "args": { "path": path, "content": content },
            "workdir": ctx.workdir.display().to_string(),
            "turn_id": ctx.turn.0,
            "call_id": ctx.call_id.0,
            "task_id": ctx.task_id.clone(),
        });
        if let MutationGate::Blocked(outcome) = mutation_policy_outcome(
            &ctx,
            "write_file",
            path,
            std::slice::from_ref(&abs_path),
            pending_action,
            containment,
        )
        .await
        {
            return *outcome;
        }
        // Serialize writers to this canonical path: two write_file/edit calls to
        // the same file in one turn run concurrently, so without this the last
        // atomic rename silently wins (lost update). Distinct paths still overlap.
        // The owned guard is Send and held across the spawn_blocking below.
        let _write_guard = tokio::select! {
            biased;
            _ = ctx.token.cancelled() => return ToolOutcome::cancelled(),
            g = super::path_lock::lock_path(&abs_path) => g,
        };
        // Scratchpad files are session-private and ephemeral — never checkpointed.
        if ctx.config.safety.checkpoint_on_mutation
            && containment != PathContainment::Scratchpad
            && let Err(e) = mermaid_runtime::create_checkpoint_for_task(
                &ctx.workdir,
                std::slice::from_ref(&abs_path),
                Some(serde_json::json!({
                    "tool": "write_file",
                    "path": path,
                })),
                ctx.checkpoint_origin(),
            )
        {
            return ToolOutcome::error(format!("write_file checkpoint failed: {e}"), None);
        }
        let display_path = path.to_string();
        let line_count = content.lines().count();
        let byte_count = content.len();
        let content = content.to_string();

        tokio::select! {
            biased;
            _ = ctx.token.cancelled() => ToolOutcome::cancelled(),
            // The prior-content read (for the display diff) now happens INSIDE
            // this blocking job and BOUNDED (#F44/RC-L) — never a synchronous
            // unbounded `read_to_string` on the async worker thread.
            result = tokio::task::spawn_blocking(move || write_with_diff_blocking(&root, &abs_path, &rel, &content)) => {
                match result {
                    Ok(Ok(write)) => {
                        let duration_secs = start.elapsed().as_secs_f64();
                        after_file_mutation(&ctx, "write_file", &display_path);
                        ToolOutcome::success(
                            format!("Wrote {} ({} lines)", display_path, write.line_count),
                            format!("{} {} written", write.line_count, plural(write.line_count, "line", "lines")),
                            duration_secs,
                        )
                        .with_metadata(ToolRunMetadata {
                            detail: ToolMetadata::WriteFile {
                                path: display_path,
                                line_count,
                                byte_count,
                                created: Some(write.created),
                            },
                            line_count: Some(line_count),
                            byte_count: Some(byte_count),
                            display_diff: Some(write.diff.display_diff),
                            diff_truncated: write.diff.truncated,
                            lines_added: write.diff.added,
                            lines_removed: write.diff.removed,
                            ..ToolRunMetadata::default()
                        })
                    },
                    Ok(Err(e)) => ToolOutcome::error(
                        format!("write_file({display_path}): {e}"),
                        Some(start.elapsed().as_secs_f64()),
                    ),
                    Err(e) => ToolOutcome::error(
                        format!("write_file join error: {e}"),
                        Some(start.elapsed().as_secs_f64()),
                    ),
                }
            }
        }
    }
}

/// `edit_file` — precise search-and-replace editing on a single file.
pub struct EditFileTool;

#[async_trait]
impl ToolExecutor for EditFileTool {
    fn name(&self) -> &'static str {
        "edit_file"
    }

    fn schema(&self) -> ToolDefinition {
        defn(
            "edit_file",
            "Perform a precise search-and-replace edit on an existing file. Replaces `target_content` with `replacement_content`. Fails if `target_content` is not found or matches multiple locations (unless `allow_multiple` is true). Matching tolerates minor whitespace and quotation drift. Paths may be relative to the project directory or absolute.",
            serde_json::json!({
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "description": "Path to the file to edit."
                    },
                    "target_content": {
                        "type": "string",
                        "description": "The exact or near-exact block of text to replace. Must match uniquely in the file unless allow_multiple is true."
                    },
                    "replacement_content": {
                        "type": "string",
                        "description": "The new replacement text."
                    },
                    "allow_multiple": {
                        "type": "boolean",
                        "description": "If true, replace all occurrences of target_content instead of requiring uniqueness. Defaults to false."
                    }
                },
                "required": ["path", "target_content", "replacement_content"]
            }),
        )
    }

    async fn execute(&self, args: serde_json::Value, ctx: ExecContext) -> ToolOutcome {
        let start = std::time::Instant::now();
        let Some(path) = args.get("path").and_then(|v| v.as_str()) else {
            return ToolOutcome::error("edit_file requires 'path' (string)", None);
        };
        let edit = match Edit::from_args(&args) {
            Ok(edit) => edit,
            Err(e) => return ToolOutcome::error(e, None),
        };

        let roots = AllowedRoots::new(&ctx.workdir, ctx.scratchpad.as_deref());
        let ResolvedInRoot {
            abs: abs_path,
            rel,
            root,
            containment,
        } = match resolve_in_roots(&roots, path) {
            Ok(r) => r,
            Err(e) => return ToolOutcome::error(format!("edit_file: {e}"), None),
        };

        let mut pending_args = edit.args();
        pending_args["path"] = serde_json::json!(path);
        let pending_action = serde_json::json!({
            "tool": "edit_file",
            "args": pending_args,
            "workdir": ctx.workdir.display().to_string(),
            "turn_id": ctx.turn.0,
            "call_id": ctx.call_id.0,
            "task_id": ctx.task_id.clone(),
        });
        if let MutationGate::Blocked(outcome) = mutation_policy_outcome(
            &ctx,
            "edit_file",
            path,
            std::slice::from_ref(&abs_path),
            pending_action,
            containment,
        )
        .await
        {
            return *outcome;
        }

        let _write_guard = tokio::select! {
            biased;
            _ = ctx.token.cancelled() => return ToolOutcome::cancelled(),
            g = super::path_lock::lock_path(&abs_path) => g,
        };

        if ctx.config.safety.checkpoint_on_mutation
            && containment != PathContainment::Scratchpad
            && let Err(e) = mermaid_runtime::create_checkpoint_for_task(
                &ctx.workdir,
                std::slice::from_ref(&abs_path),
                Some(serde_json::json!({
                    "tool": "edit_file",
                    "path": path,
                })),
                ctx.checkpoint_origin(),
            )
        {
            return ToolOutcome::error(format!("edit_file checkpoint failed: {e}"), None);
        }

        let display_path = path.to_string();
        tokio::select! {
            biased;
            _ = ctx.token.cancelled() => ToolOutcome::cancelled(),
            result = tokio::task::spawn_blocking(move || edit_file_blocking(&root, &rel, &edit)) => {
                match result {
                    Ok(Ok(edit)) => {
                        let duration_secs = start.elapsed().as_secs_f64();
                        after_file_mutation(&ctx, "edit_file", &display_path);
                        edit_success_outcome(&display_path, edit, duration_secs)
                    },
                    Ok(Err(e)) => ToolOutcome::error(
                        format!("edit_file({display_path}): {e}"),
                        Some(start.elapsed().as_secs_f64()),
                    ),
                    Err(e) => ToolOutcome::error(
                        format!("edit_file join error: {e}"),
                        Some(start.elapsed().as_secs_f64()),
                    ),
                }
            }
        }
    }
}

fn edit_success_outcome(display_path: &str, edit: EditResult, duration_secs: f64) -> ToolOutcome {
    let fuzzy_note = if edit.fuzzy {
        "\nnote: matched with fuzzy (whitespace/Unicode) context; verify the result."
    } else {
        ""
    };
    ToolOutcome::success(
        format!("Edited {display_path}{fuzzy_note}"),
        diff_summary(edit.diff.added, edit.diff.removed, duration_secs),
        duration_secs,
    )
    .with_metadata(ToolRunMetadata {
        detail: ToolMetadata::ApplyPatch {
            added: Vec::new(),
            modified: vec![display_path.to_string()],
            deleted: Vec::new(),
            renamed: Vec::new(),
            fuzzy: edit.fuzzy,
        },
        display_diff: Some(edit.diff.display_diff),
        diff_truncated: edit.diff.truncated,
        lines_added: edit.diff.added,
        lines_removed: edit.diff.removed,
        ..ToolRunMetadata::default()
    })
}

// ─── helpers ────────────────────────────────────────────────────────

fn extract_paths(args: &serde_json::Value) -> Result<Vec<String>, String> {
    // Accept both shapes: `{path: "x"}` and `{paths: ["x", "y"]}`.
    if let Some(p) = args.get("path").and_then(|v| v.as_str()) {
        reject_web_url(p)?;
        return Ok(vec![p.to_string()]);
    }
    if let Some(arr) = args.get("paths").and_then(|v| v.as_array()) {
        if arr.len() > mermaid_model::constants::MAX_BATCH_TOOL_ITEMS {
            return Err(format!(
                "read_file: too many paths ({}); cap is {} per call — split the request",
                arr.len(),
                mermaid_model::constants::MAX_BATCH_TOOL_ITEMS
            ));
        }
        let mut out = Vec::with_capacity(arr.len());
        for v in arr {
            let Some(s) = v.as_str() else {
                return Err("read_file 'paths' must be an array of strings".to_string());
            };
            reject_web_url(s)?;
            out.push(s.to_string());
        }
        return Ok(out);
    }
    Err("read_file requires 'path' or 'paths'".to_string())
}

/// `read_file` reads the local filesystem, but models under a web-gated
/// safety mode were observed pointing it at `https://` URLs and treating the
/// result as a fetch — and the path-resolution error that came back said
/// nothing about the actual mistake. Name the mistake and the right tool;
/// whether `web_fetch` is available is then that tool's own story to tell.
fn reject_web_url(path: &str) -> Result<(), String> {
    let head: String = path
        .trim_start()
        .chars()
        .take(8)
        .collect::<String>()
        .to_ascii_lowercase();
    if head.starts_with("http://") || head.starts_with("https://") {
        return Err(format!(
            "read_file reads local files; '{path}' is a web URL — use web_fetch for URLs"
        ));
    }
    Ok(())
}

/// Read-only carve-out for memory facts. Global and project-private memory
/// live under the OS data dir — outside both allowed roots — and the memory
/// index tells the model to `read_file` the fact's path, so reads resolve
/// against the memory roots too. Absolute paths only, with the same
/// canonical (symlink-resolving) containment as the scratchpad arm. The
/// write tools never consult this: memory mutation goes through the `memory`
/// tool, where the policy gate can see it.
fn resolve_in_memory_roots(workdir: &Path, raw: &str) -> Option<(PathBuf, PathBuf)> {
    if !Path::new(raw).is_absolute() {
        return None;
    }
    for (root, _scope) in crate::app::memory::memory_roots(workdir) {
        if let Ok((_abs, true)) = resolve_path_within(&root, raw)
            && let Ok(rel) = relative_within(&root, raw)
        {
            return Some((root, rel));
        }
    }
    None
}

/// Where a `read_file` target lands once the resolver and the memory-root
/// fallback have spoken.
struct ReadTarget {
    root: PathBuf,
    rel: PathBuf,
    /// `Some(abs)` when the target sits outside every allowed root and must
    /// clear [`external_read_gate`] before it is opened.
    external: Option<PathBuf>,
}

/// Resolve a read target through the canonical containment resolver, and
/// answer its three-way verdict: project and scratchpad reads are ungated;
/// durable-memory reads are ungated too (memory is agent-owned by design and
/// lives outside the project); anything else outside the roots is external and
/// carries its absolute path for the gate.
///
/// `Err` only when the path cannot be resolved at all (a workdir that will not
/// canonicalize, or no existing ancestor) and is not a memory path either.
fn resolve_read_target(roots: &AllowedRoots<'_>, raw: &str) -> std::io::Result<ReadTarget> {
    match resolve_in_roots(roots, raw) {
        Ok(ResolvedInRoot {
            abs,
            rel,
            root,
            containment,
        }) => match containment {
            PathContainment::Project | PathContainment::Scratchpad => Ok(ReadTarget {
                root,
                rel,
                external: None,
            }),
            PathContainment::External => Ok(resolve_in_memory_roots(roots.workdir, raw).map_or(
                ReadTarget {
                    root,
                    rel,
                    external: Some(abs),
                },
                |(root, rel)| ReadTarget {
                    root,
                    rel,
                    external: None,
                },
            )),
        },
        Err(msg) => {
            let (root, rel) = resolve_in_memory_roots(roots.workdir, raw)
                .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::PermissionDenied, msg))?;
            Ok(ReadTarget {
                root,
                rel,
                external: None,
            })
        },
    }
}

/// Gate a read of `abs`, a path outside the project and the scratchpad.
///
/// Filed as `ToolCategory::ExternalDirectory` — an external side effect, the
/// same class an out-of-project `working_dir` gets from the exec tool — so
/// `ask` prompts (allowlistable per directory), `auto` consults the intent
/// classifier, `full_access` proceeds, and read-only mode denies. Not replayable: a read has nothing to replay, so a
/// headless `ask` session refuses it unless untrusted tools were allowed.
///
/// Returns the blocking outcome, or `None` to proceed.
async fn external_read_gate(ctx: &ExecContext, raw: &str, abs: &Path) -> Option<ToolOutcome> {
    let mut request = mermaid_runtime::ActionRequest::new(
        "read_file",
        mermaid_runtime::ToolCategory::ExternalDirectory,
        format!("read_file {raw}"),
    );
    request.path = Some(abs.display().to_string());
    let pending_action = serde_json::json!({
        "tool": "read_file",
        "args": { "path": raw },
        "workdir": ctx.workdir.display().to_string(),
        "turn_id": ctx.turn.0,
        "call_id": ctx.call_id.0,
        "task_id": ctx.task_id.clone(),
    });
    match super::policy_gate::gate(ctx, request, &[], pending_action, false, false).await {
        super::policy_gate::Gate::Block(outcome) => Some(outcome),
        super::policy_gate::Gate::Proceed { .. } => None,
    }
}

/// How `read_file` presents a file: whole and as-is by default, or
/// line-numbered and optionally cut to a line range. The numbered form is the
/// text editor's `view` (see the Anthropic adapter's native tools), which the
/// model reads line numbers from to aim an `insert`. Neither field is in the
/// advertised schema: only that translation sends them.
#[derive(Debug, Default)]
struct View {
    line_numbers: bool,
    /// 1-based inclusive first and last line; `None` last means to the end.
    range: Option<(usize, Option<usize>)>,
}

impl View {
    fn from_args(args: &serde_json::Value) -> Result<Self, String> {
        let line_numbers = args
            .get("line_numbers")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let range = match args.get("view_range") {
            None | Some(serde_json::Value::Null) => None,
            Some(range) => {
                let bad = || {
                    format!(
                        "view_range must be [start, end] with 1 <= start and end >= start or \
                         end = -1, got {range}"
                    )
                };
                let [start, end] = range.as_array().map(Vec::as_slice).ok_or_else(bad)? else {
                    return Err(bad());
                };
                let start = start
                    .as_u64()
                    .filter(|s| *s >= 1)
                    .and_then(|s| usize::try_from(s).ok())
                    .ok_or_else(bad)?;
                let end = match end.as_i64() {
                    Some(-1) => None,
                    Some(e) => Some(
                        usize::try_from(e)
                            .ok()
                            .filter(|e| *e >= start)
                            .ok_or_else(bad)?,
                    ),
                    None => return Err(bad()),
                };
                Some((start, end))
            },
        };
        Ok(Self {
            line_numbers: line_numbers || range.is_some(),
            range,
        })
    }

    fn apply(&self, content: &str) -> Result<String, String> {
        if !self.line_numbers {
            return Ok(content.to_string());
        }
        let total = content.lines().count();
        let (start, end) = self.range.unwrap_or((1, None));
        if start > total.max(1) {
            return Err(format!(
                "view_range starts at line {start}, but the file has {total} lines"
            ));
        }
        let end = end.unwrap_or(total).min(total);
        let mut out = String::new();
        for (idx, line) in content.lines().enumerate().take(end).skip(start - 1) {
            out.push_str(&format!("{:>6}\t{line}\n", idx + 1));
        }
        Ok(out)
    }
}

/// Entries a directory listing shows before it stops.
const DIRECTORY_LISTING_CAP: usize = 1_000;

/// Read `rel` beneath `root`: a file's (bounded) text, or for a directory a
/// listing two levels deep, skipping hidden and ignored entries. The flag says
/// which it was, so a listing is never line-numbered as if it were a file.
async fn read_target(root: PathBuf, rel: PathBuf) -> std::io::Result<(String, bool, bool)> {
    let abs = root.join(&rel);
    if tokio::fs::metadata(&abs).await.is_ok_and(|m| m.is_dir()) {
        let listing = tokio::task::spawn_blocking(move || list_directory(&abs))
            .await
            .map_err(|e| std::io::Error::other(e.to_string()))?;
        return Ok((listing.0, listing.1, true));
    }
    let (content, truncated) = read_one(root, rel).await?;
    Ok((content, truncated, false))
}

/// `dir`'s entries two levels deep, one relative path per line (directories
/// end in `/`), and whether the listing hit [`DIRECTORY_LISTING_CAP`].
fn list_directory(dir: &Path) -> (String, bool) {
    let mut entries: Vec<String> = ignore::WalkBuilder::new(dir)
        .max_depth(Some(2))
        .hidden(true)
        .build()
        .filter_map(Result::ok)
        .filter(|entry| entry.depth() > 0)
        .filter_map(|entry| {
            let rel = entry.path().strip_prefix(dir).ok()?.display().to_string();
            let is_dir = entry.file_type().is_some_and(|t| t.is_dir());
            Some(if is_dir { format!("{rel}/") } else { rel })
        })
        .collect();
    entries.sort();
    let truncated = entries.len() > DIRECTORY_LISTING_CAP;
    entries.truncate(DIRECTORY_LISTING_CAP);
    let mut out = entries.join("\n");
    if truncated {
        out.push_str(&format!(
            "\n[TRUNCATED: listing stops at {DIRECTORY_LISTING_CAP} entries]"
        ));
    }
    (out, truncated)
}

/// Read one file (bounded) from `rel` beneath `root`. Returns the (possibly
/// marker-footed) text and the REAL truncation flag from the bounded read, so
/// the caller propagates that rather than sniffing the output for the marker
/// string — which a file whose own content contains that literal text would
/// otherwise falsely trip (#78).
///
/// The root-relative path feeds the confined fd read, so the bytes come from
/// the inode the kernel resolved under `RESOLVE_BENEATH` rather than whatever
/// a concurrently-swapped symlink now points at (#77).
async fn read_one(root: PathBuf, rel: PathBuf) -> std::io::Result<(String, bool)> {
    let result = tokio::task::spawn_blocking(move || {
        let file = mermaid_runtime::open_beneath(&root, &rel, mermaid_runtime::OpenIntent::Read)?;
        // Bounded read: never pull more than the cap (+1 probe byte) into RAM,
        // so a model pointing `read_file` at a multi-gigabyte file can't OOM the
        // process — a full read would have slurped the whole thing first (#15).
        let (data, truncated) = mermaid_model::utils::read_capped(file, MAX_FILE_READ_BYTES)?;
        let mut s = String::from_utf8_lossy(&data).into_owned();
        if truncated {
            // Char-boundary-safe truncation with a marker footer.
            let cut = s.floor_char_boundary(MAX_FILE_READ_BYTES);
            s.truncate(cut);
            s.push_str("\n\n[TRUNCATED: file exceeded read cap]");
        }
        Ok::<_, std::io::Error>((s, truncated))
    })
    .await
    .map_err(|e| std::io::Error::other(e.to_string()))??;
    Ok(result)
}

/// Write `content` to `rel` beneath `root` (the project workdir or the session
/// scratchpad) through the symlink-confined *atomic* writer, creating parent
/// dirs the same confined way. The bytes are written to a temp and
/// `renameat`-swapped over the target, all beneath the directory fd the kernel
/// resolved under `RESOLVE_BENEATH`: a parent dir swapped for an escaping
/// symlink can't redirect the write (#77), and a crash/kill/disk-full
/// mid-write leaves the previous file intact rather than a truncated or
/// half-written one.
fn write_one_blocking(root: &Path, rel: &Path, content: &str) -> std::io::Result<usize> {
    if let Some(parent) = rel.parent()
        && !parent.as_os_str().is_empty()
    {
        mermaid_runtime::create_dir_all_beneath(root, parent)?;
    }
    mermaid_runtime::write_atomic_beneath(root, rel, content.as_bytes())?;
    Ok(content.lines().count())
}

struct WriteResult {
    line_count: usize,
    created: bool,
    diff: mermaid_model::diff::DisplayDiff,
}

/// Write `content` and build the display diff against the prior file in ONE
/// blocking job (#F44/RC-L). The prior content is read BOUNDED via
/// [`mermaid_model::utils::read_file_capped`] — overwriting a multi-gigabyte file must
/// not slurp it into RAM on the async worker just to render a diff. A prior file
/// larger than the read cap (or otherwise unreadable) is elided from the diff
/// rather than read whole.
fn write_with_diff_blocking(
    root: &Path,
    abs_path: &Path,
    rel: &Path,
    content: &str,
) -> std::io::Result<WriteResult> {
    let (old_content, created, elide_diff) =
        match mermaid_model::utils::read_file_capped(abs_path, MAX_FILE_READ_BYTES) {
            Ok((data, false)) => (String::from_utf8_lossy(&data).into_owned(), false, false),
            // Existing file is past the read cap — don't pull it all into RAM.
            Ok((_, true)) => (String::new(), false, true),
            // Missing file → a fresh create; the diff shows the whole content added.
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => (String::new(), true, false),
            // Any other read error: don't fail the write over a diff preview.
            Err(_) => (String::new(), false, true),
        };
    let diff = if elide_diff {
        mermaid_model::diff::DisplayDiff {
            display_diff: format!(
                "[diff preview skipped: existing file exceeds the {MAX_FILE_READ_BYTES}-byte cap]"
            ),
            added: 0,
            removed: 0,
            truncated: true,
        }
    } else {
        mermaid_model::diff::generate_display_diff(&old_content, content)
    };
    let line_count = write_one_blocking(root, rel, content)?;
    Ok(WriteResult {
        line_count,
        created,
        diff,
    })
}

struct EditResult {
    diff: mermaid_model::diff::DisplayDiff,
    fuzzy: bool,
}

/// One `edit_file` change: a search-and-replace, or an insert after a line.
enum Edit {
    Replace {
        target: String,
        replacement: String,
        allow_multiple: bool,
    },
    Insert {
        after_line: usize,
        text: String,
    },
}

impl Edit {
    /// The change `args` asks for. `insert_line` is the text editor's
    /// `insert` (see the Anthropic adapter's native tools): the text goes
    /// after that line instead of over a match. It is not in the advertised
    /// schema; only that translation sends it.
    fn from_args(args: &serde_json::Value) -> Result<Self, String> {
        let text = |key: &str| args.get(key).and_then(serde_json::Value::as_str);
        let replacement = text("replacement_content")
            .ok_or("edit_file requires 'replacement_content' (string)")?
            .to_string();
        if let Some(line) = args.get("insert_line") {
            let after_line = line
                .as_u64()
                .and_then(|l| usize::try_from(l).ok())
                .ok_or("edit_file 'insert_line' must be a non-negative line number")?;
            return Ok(Self::Insert {
                after_line,
                text: replacement,
            });
        }
        Ok(Self::Replace {
            target: text("target_content")
                .ok_or("edit_file requires 'target_content' (string)")?
                .to_string(),
            replacement,
            allow_multiple: args
                .get("allow_multiple")
                .and_then(serde_json::Value::as_bool)
                .unwrap_or(false),
        })
    }

    /// The arguments that replay this change (without the path).
    fn args(&self) -> serde_json::Value {
        match self {
            Self::Replace {
                target,
                replacement,
                allow_multiple,
            } => serde_json::json!({
                "target_content": target,
                "replacement_content": replacement,
                "allow_multiple": allow_multiple,
            }),
            Self::Insert { after_line, text } => serde_json::json!({
                "insert_line": after_line,
                "replacement_content": text,
            }),
        }
    }
}

fn edit_file_blocking(root: &Path, rel: &Path, edit: &Edit) -> Result<EditResult, String> {
    let file = mermaid_runtime::open_beneath(root, rel, mermaid_runtime::OpenIntent::Read)
        .map_err(|e| format!("cannot open file: {e}"))?;
    let (data, truncated) = mermaid_model::utils::read_capped(file, MAX_FILE_READ_BYTES)
        .map_err(|e| format!("cannot read file: {e}"))?;
    if truncated {
        return Err(format!(
            "file exceeds maximum size limit ({MAX_FILE_READ_BYTES} bytes)"
        ));
    }
    let original = String::from_utf8_lossy(&data).into_owned();
    let applied = match edit {
        Edit::Replace {
            target,
            replacement,
            allow_multiple,
        } => mermaid_runtime::replace_content(&original, target, replacement, *allow_multiple),
        Edit::Insert { after_line, text } => {
            mermaid_runtime::insert_content(&original, *after_line, text)
        },
    }
    .map_err(|e| e.to_string())?;

    write_one_blocking(root, rel, &applied.new_contents)
        .map_err(|e| format!("cannot write file: {e}"))?;

    let diff = mermaid_model::diff::generate_display_diff(&original, &applied.new_contents);
    Ok(EditResult {
        diff,
        fuzzy: applied.fuzzy,
    })
}

/// Outcome of gating a file mutation.
pub(super) enum MutationGate {
    /// Blocked — return this outcome verbatim. Boxed to keep the enum small.
    Blocked(Box<ToolOutcome>),
    Proceed,
}

/// Gate a file mutation by where its target landed.
///
/// `containment` is the resolver's verdict for the path, matched here so that
/// every mutating tool answers the three-way question in one place:
/// - `Project`: an ordinary edit (`ToolCategory::Edit`).
/// - `Scratchpad`: the gate downgrades an `Ask`/`Classify` to proceed — scratch
///   files are session-private and ephemeral — while read-only mode and `Deny`
///   overrides still block it.
/// - `External`: a path outside both roots is an external side effect
///   (`ToolCategory::ExternalDirectory`), so it prompts in `ask`, goes through
///   the intent classifier in `auto`, and is denied by the read-only floor —
///   the same treatment an out-of-project `working_dir` gets from the exec
///   tool. Before this, an external write classified like an in-project one
///   and `auto` mode allowed it unasked.
pub(super) async fn mutation_policy_outcome(
    ctx: &ExecContext,
    tool: &str,
    path: &str,
    checkpoint_paths: &[PathBuf],
    pending_action: serde_json::Value,
    containment: PathContainment,
) -> MutationGate {
    let category = match containment {
        PathContainment::Project | PathContainment::Scratchpad => {
            mermaid_runtime::ToolCategory::Edit
        },
        PathContainment::External => mermaid_runtime::ToolCategory::ExternalDirectory,
    };
    let mut request = mermaid_runtime::ActionRequest::new(tool, category, format!("{tool} {path}"));
    request.path = Some(path.to_string());
    // File mutations are replayable: an Ask decision checkpoints, records an
    // approval, and blocks (handled inside the gate).
    match super::policy_gate::gate(
        ctx,
        request,
        checkpoint_paths,
        pending_action,
        true,
        containment == PathContainment::Scratchpad,
    )
    .await
    {
        super::policy_gate::Gate::Block(outcome) => MutationGate::Blocked(Box::new(outcome)),
        super::policy_gate::Gate::Proceed { .. } => {
            let _ = mermaid_runtime::run_plugin_hooks(
                "before_file_mutation",
                &serde_json::json!({
                    "task_id": ctx.task_id.clone(),
                    "turn_id": ctx.turn.0,
                    "call_id": ctx.call_id.0,
                    "tool": tool,
                    "path": path,
                }),
            );
            MutationGate::Proceed
        },
    }
}

pub(super) fn after_file_mutation(ctx: &ExecContext, tool: &str, path: &str) {
    let _ = mermaid_runtime::run_plugin_hooks(
        "after_file_mutation",
        &serde_json::json!({
            "task_id": ctx.task_id.clone(),
            "turn_id": ctx.turn.0,
            "call_id": ctx.call_id.0,
            "tool": tool,
            "path": path,
        }),
    );
}

fn err(msg: &str, duration_secs: f64) -> ToolOutcome {
    ToolOutcome::error(msg, Some(duration_secs))
}

fn plural(count: usize, singular: &'static str, plural: &'static str) -> &'static str {
    if count == 1 { singular } else { plural }
}

pub(super) fn diff_summary(added: usize, removed: usize, duration_secs: f64) -> String {
    format!(
        "+{} -{}, took {}",
        added,
        removed,
        format_duration_for_diff(duration_secs)
    )
}

fn format_duration_for_diff(seconds: f64) -> String {
    if seconds < 1.0 {
        format!("{}ms", (seconds * 1000.0).round().max(1.0) as u64)
    } else if seconds < 10.0 {
        format!("{seconds:.1}s")
    } else {
        format!("{}s", seconds.round() as u64)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::ctx::test_exec_context;
    use mermaid_domain::{ToolCallId, TurnId};
    use std::fs;

    /// Memory facts live outside the project/scratchpad roots and the index
    /// tells the model to `read_file` them — reads must resolve against the
    /// memory roots (here the `ProjectShared` root, reached by putting the
    /// workdir in a subdir of the git root), while unrelated outside paths
    /// stay rejected.
    #[tokio::test]
    async fn read_file_resolves_memory_roots_read_only() {
        let base = std::env::temp_dir().join(format!("mermaid_memread_{}", std::process::id()));
        let _ = fs::remove_dir_all(&base);
        let repo = base.join("repo");
        let workdir = repo.join("src");
        fs::create_dir_all(&workdir).unwrap();
        fs::create_dir_all(repo.join(".git")).unwrap();
        let mem_dir = repo.join(".mermaid").join("memory");
        fs::create_dir_all(&mem_dir).unwrap();
        let fact = mem_dir.join("fact.md");
        fs::write(&fact, "the fact body").unwrap();

        let roots = AllowedRoots::new(&workdir, None);
        // A memory path sits outside the project yet resolves ungated: memory
        // is agent-owned by design.
        let target = resolve_read_target(&roots, fact.to_str().unwrap()).unwrap();
        assert!(target.external.is_none(), "memory reads are not external");
        let (content, truncated) = read_one(target.root, target.rel).await.unwrap();
        assert!(!truncated);
        assert_eq!(content, "the fact body");

        // A stray path outside every root resolves too, but carries its
        // absolute path for the gate: `execute` must not read it unasked.
        let stray = base.join("stray.txt");
        fs::write(&stray, "nope").unwrap();
        let target = resolve_read_target(&roots, stray.to_str().unwrap()).unwrap();
        assert_eq!(
            target.external.as_deref().map(|p| p.ends_with("stray.txt")),
            Some(true),
            "a path outside every root is external"
        );

        let _ = fs::remove_dir_all(&base);
    }

    #[test]
    fn resolve_in_roots_contains_to_workdir() {
        let root = std::env::temp_dir().join(format!("mermaid_rps_{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(root.join("sub")).unwrap();
        let roots = AllowedRoots::new(&root, None);

        // In-root existing + not-yet-existing targets resolve inside root.
        assert!(resolve_in_roots(&roots, "sub").is_ok());
        let resolved = resolve_in_roots(&roots, "sub/new.txt").unwrap();
        let canon_root = fs::canonicalize(&root).unwrap();
        assert!(resolved.abs.starts_with(&canon_root));

        // External paths and relative .. escapes resolve successfully.
        assert!(resolve_in_roots(&roots, "../escape.txt").is_ok());
        let outside = std::env::temp_dir().join("definitely_outside.txt");
        let r = resolve_in_roots(&roots, &outside.display().to_string()).unwrap();
        assert!(r.abs.ends_with(Path::new("definitely_outside.txt")));

        let _ = fs::remove_dir_all(&root);
    }

    fn temp_root(name: &str) -> PathBuf {
        let p = std::env::temp_dir().join(format!("mermaid_providers_fs_{name}"));
        let _ = fs::remove_dir_all(&p);
        fs::create_dir_all(&p).expect("create tmpdir");
        p
    }

    #[tokio::test]
    async fn read_file_returns_content() {
        let dir = temp_root("read_ok");
        fs::write(dir.join("a.txt"), "hello").expect("write");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());

        let tool = ReadFileTool;
        let outcome = tool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        assert_eq!(outcome.output(), "hello");
        let _ = fs::remove_dir_all(&dir);
    }

    /// The text editor's `view` arrives as a numbered, optionally ranged
    /// `read_file`; the numbers are what the model aims an `insert` with.
    #[tokio::test]
    async fn read_file_numbers_lines_and_cuts_ranges_for_the_text_editor() {
        let dir = temp_root("read_view");
        fs::write(dir.join("a.txt"), "one\ntwo\nthree\n").expect("write");
        let read = |args: serde_json::Value, call: u64| {
            let (ctx, _rx) = test_exec_context(TurnId(call), ToolCallId(call), dir.clone());
            async move { ReadFileTool.execute(args, ctx).await }
        };
        let all = read(
            serde_json::json!({"path": "a.txt", "line_numbers": true}),
            1,
        )
        .await;
        assert_eq!(all.output(), "     1\tone\n     2\ttwo\n     3\tthree\n");
        let ranged = read(
            serde_json::json!({"path": "a.txt", "view_range": [2, -1]}),
            2,
        )
        .await;
        assert_eq!(ranged.output(), "     2\ttwo\n     3\tthree\n");
        let one = read(
            serde_json::json!({"path": "a.txt", "view_range": [2, 2]}),
            3,
        )
        .await;
        assert_eq!(one.output(), "     2\ttwo\n");
        for bad in [
            serde_json::json!([0, 2]),
            serde_json::json!([3, 1]),
            serde_json::json!([9, -1]),
            serde_json::json!("1-2"),
        ] {
            let outcome = read(serde_json::json!({"path": "a.txt", "view_range": bad}), 4).await;
            assert_eq!(outcome.status, mermaid_domain::ToolStatus::Error, "{bad}");
        }
        let plain = read(serde_json::json!({"path": "a.txt"}), 5).await;
        assert_eq!(plain.output(), "one\ntwo\nthree\n", "unchanged by default");
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_lists_a_directory_two_levels_deep() {
        let dir = temp_root("read_dir");
        fs::create_dir_all(dir.join("src/deep/deeper")).expect("mkdir");
        fs::write(dir.join("src/lib.rs"), "").expect("write");
        fs::write(dir.join("src/deep/deeper/x.rs"), "").expect("write");
        fs::write(dir.join(".hidden"), "").expect("write");
        fs::write(dir.join("README.md"), "").expect("write");
        for (turn, args) in [
            serde_json::json!({"path": "."}),
            serde_json::json!({"path": ".", "line_numbers": true}),
        ]
        .into_iter()
        .enumerate()
        {
            let turn = TurnId(turn as u64 + 1);
            let (ctx, _rx) = test_exec_context(turn, ToolCallId(1), dir.clone());
            let outcome = ReadFileTool.execute(args, ctx).await;
            assert!(outcome.is_success(), "{outcome:?}");
            assert_eq!(
                outcome.output(),
                format!(
                    "README.md\nsrc/\nsrc{0}deep/\nsrc{0}lib.rs",
                    std::path::MAIN_SEPARATOR
                )
            );
        }
        let _ = fs::remove_dir_all(&dir);
    }

    /// The text editor's `insert` arrives as an `edit_file` with
    /// `insert_line`: the text lands after that line, with no match needed.
    #[tokio::test]
    async fn edit_file_inserts_after_a_line() {
        let dir = temp_root("edit_insert");
        fs::write(dir.join("a.txt"), "one\ntwo\n").expect("write");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = EditFileTool
            .execute(
                serde_json::json!({"path": "a.txt", "insert_line": 1, "replacement_content": "mid"}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "{outcome:?}");
        assert_eq!(
            fs::read_to_string(dir.join("a.txt")).unwrap(),
            "one\nmid\ntwo\n"
        );

        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(2), dir.clone());
        let past_end = EditFileTool
            .execute(
                serde_json::json!({"path": "a.txt", "insert_line": 9, "replacement_content": "x"}),
                ctx,
            )
            .await;
        assert_eq!(past_end.status, mermaid_domain::ToolStatus::Error);
        assert_eq!(
            fs::read_to_string(dir.join("a.txt")).unwrap(),
            "one\nmid\ntwo\n"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_rejects_web_urls_with_a_web_fetch_hint() {
        // Observed in the field: a model under a web-gated safety mode fed
        // `read_file` an https:// URL and treated the reply as a fetch. The
        // rejection must name the right tool — and a plain local read next to
        // it must keep working (the guard cannot overmatch).
        let dir = temp_root("read_url");
        fs::write(dir.join("a.txt"), "hello").expect("write");
        for args in [
            serde_json::json!({"path": "https://learn.microsoft.com/clipboard"}),
            serde_json::json!({"path": "HTTP://example.com/x"}),
            serde_json::json!({"paths": ["a.txt", "https://example.com/x"]}),
        ] {
            let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
            let outcome = ReadFileTool.execute(args, ctx).await;
            assert_eq!(outcome.status, mermaid_domain::ToolStatus::Error);
            let msg = outcome.error_message().unwrap_or_default();
            assert!(msg.contains("web_fetch"), "must name the right tool: {msg}");
        }
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert!(outcome.is_success(), "plain local reads must still work");
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn duplicate_same_turn_read_collapses_to_a_reuse_note() {
        let dir = temp_root("read_dedup");
        fs::write(dir.join("a.txt"), "line one\nline two").expect("write");

        // First read: full content.
        let (ctx, _rx) = test_exec_context(TurnId(9), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert_eq!(outcome.output(), "line one\nline two");

        // Byte-identical repeat in the SAME turn: a short reuse note, not
        // the body again — the earlier result rides the same request.
        let (ctx, _rx) = test_exec_context(TurnId(9), ToolCallId(2), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert!(outcome.is_success());
        assert!(
            outcome
                .output()
                .contains("unchanged since your read earlier this turn"),
            "{}",
            outcome.output()
        );
        assert!(outcome.output().contains("2 lines"), "{}", outcome.output());
        assert!(
            !outcome.output().contains("line two"),
            "the body must not repeat: {}",
            outcome.output()
        );

        // Matched pair (a): the file CHANGED on disk — by any writer; here a
        // direct fs write stands in for write_file / apply_patch /
        // execute_command — so the same-turn re-read returns full content.
        // Content-hash equality is the invalidation; there is no hook to
        // forget.
        fs::write(dir.join("a.txt"), "line one\nline two\nline three").expect("write");
        let (ctx, _rx) = test_exec_context(TurnId(9), ToolCallId(3), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert_eq!(
            outcome.output(),
            "line one\nline two\nline three",
            "a changed file must read in full"
        );

        // ...and the byte-identical repeat of THAT read collapses again.
        let (ctx, _rx) = test_exec_context(TurnId(9), ToolCallId(4), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert!(
            outcome.output().contains("unchanged since"),
            "{}",
            outcome.output()
        );

        // Matched pair (b): a LATER turn always reads in full — a cross-turn
        // re-read may be a legitimate refresh (post-compaction, post-edit)
        // and is never suppressed.
        let (ctx, _rx) = test_exec_context(TurnId(10), ToolCallId(5), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert_eq!(outcome.output(), "line one\nline two\nline three");

        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_missing_path_errors() {
        let dir = temp_root("read_missing_path");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool.execute(serde_json::json!({}), ctx).await;
        assert_eq!(outcome.status, mermaid_domain::ToolStatus::Error);
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_nonexistent_errors() {
        let dir = temp_root("read_nonex");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "does_not_exist.txt"}), ctx)
            .await;
        assert_eq!(outcome.status, mermaid_domain::ToolStatus::Error);
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_with_multiple_paths_joins_contents() {
        let dir = temp_root("read_multi");
        fs::write(dir.join("a.txt"), "alpha").expect("write");
        fs::write(dir.join("b.txt"), "beta").expect("write");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"paths": ["a.txt", "b.txt"]}), ctx)
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        let output = outcome.output();
        assert!(output.contains("=== a.txt ==="));
        assert!(output.contains("alpha"));
        assert!(output.contains("=== b.txt ==="));
        assert!(output.contains("beta"));
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_multi_aggregate_is_capped() {
        // F45: many files in one call can't blow past the aggregate cap. Each
        // file is under the per-file cap, but their sum exceeds the aggregate.
        let dir = temp_root("read_aggregate_cap");
        let chunk = "a".repeat(MAX_READ_AGGREGATE_CHARS * 2 / 3);
        fs::write(dir.join("a.txt"), &chunk).expect("write a");
        fs::write(dir.join("b.txt"), &chunk).expect("write b");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"paths": ["a.txt", "b.txt"]}), ctx)
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        let output = outcome.output();
        assert!(
            output.len() <= MAX_READ_AGGREGATE_CHARS + 64,
            "combined must be capped, got {} bytes",
            output.len()
        );
        assert!(
            output.contains("elided"),
            "expected aggregate head+tail elision marker"
        );
        match &outcome.metadata.detail {
            ToolMetadata::ReadFile { truncated, .. } => {
                assert!(*truncated, "aggregate truncation must set truncated")
            },
            other => panic!("expected ReadFile metadata, got {other:?}"),
        }
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn write_file_elides_diff_for_oversized_existing_file() {
        // F44: overwriting a file larger than the read cap must NOT slurp it into
        // RAM for a diff — the diff is elided with a marker instead.
        let dir = temp_root("write_oversized_diff");
        let big = "a".repeat(MAX_FILE_READ_BYTES + 1);
        fs::write(dir.join("big.txt"), &big).expect("write fixture");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({"path": "big.txt", "content": "small\n"}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        let diff = outcome
            .metadata
            .display_diff
            .as_deref()
            .expect("display diff");
        assert!(
            diff.contains("diff preview skipped"),
            "expected elision marker, got: {diff}"
        );
        assert!(
            outcome.metadata.diff_truncated,
            "oversized diff must set diff_truncated"
        );
        match &outcome.metadata.detail {
            ToolMetadata::WriteFile { created, .. } => {
                assert_eq!(*created, Some(false), "existing file is not 'created'")
            },
            other => panic!("expected WriteFile metadata, got {other:?}"),
        }
        // The file was actually overwritten despite the elided diff.
        let written = fs::read_to_string(dir.join("big.txt")).expect("read");
        assert_eq!(written, "small\n");
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_with_marker_in_content_is_not_flagged_truncated() {
        // #78: a small file whose own content contains the truncation-marker
        // string must NOT be reported as truncated — the flag comes from the
        // bounded read now, not a substring sniff of the output.
        let dir = temp_root("read_marker_content");
        fs::write(
            dir.join("a.txt"),
            "before\n\n[TRUNCATED: file exceeded read cap]\nafter",
        )
        .expect("write");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());

        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "a.txt"}), ctx)
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        match &outcome.metadata.detail {
            ToolMetadata::ReadFile { truncated, .. } => assert!(
                !truncated,
                "a file whose content contains the marker must not be flagged truncated"
            ),
            other => panic!("expected ReadFile metadata, got {other:?}"),
        }
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn read_file_respects_cancellation() {
        let dir = temp_root("read_cancel");
        // Write a huge file so the read is slow enough to race cancel.
        // Actually spawn_blocking on read is fast on tmpfs — this test
        // just verifies the select! arm compiles + the token trips
        // the cancel path when pre-cancelled.
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        ctx.token.cancel();
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "x.txt"}), ctx)
            .await;
        assert!(outcome.was_cancelled());
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn write_file_creates_and_counts_lines() {
        let dir = temp_root("write_ok");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({"path": "out.txt", "content": "line1\nline2\nline3\n"}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        assert!(outcome.output().contains("3 lines"));
        let written = fs::read_to_string(dir.join("out.txt")).expect("read");
        assert!(written.contains("line1"));
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn concurrent_write_file_same_path_serializes_cleanly() {
        // The per-path write gate must let two writes to the same file in one turn
        // both succeed and leave the file as exactly one clean write (never a
        // corrupt interleave), and must not deadlock.
        let dir = temp_root("write_race");
        let (ctx1, _r1) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let (ctx2, _r2) = test_exec_context(TurnId(1), ToolCallId(2), dir.clone());
        let a = "AAAA\nAAAA\n";
        let b = "BBBB\nBBBB\n";
        let (o1, o2) = tokio::join!(
            WriteFileTool.execute(serde_json::json!({"path": "race.txt", "content": a}), ctx1),
            WriteFileTool.execute(serde_json::json!({"path": "race.txt", "content": b}), ctx2),
        );
        assert!(o1.is_success(), "first write failed: {o1:?}");
        assert!(o2.is_success(), "second write failed: {o2:?}");
        let final_content = fs::read_to_string(dir.join("race.txt")).expect("read");
        assert!(
            final_content == a || final_content == b,
            "file must be exactly one clean write, got {final_content:?}"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn write_file_new_file_records_added_display_diff() {
        let dir = temp_root("write_new_diff");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({"path": "out.txt", "content": "alpha\nbeta\n"}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        let diff = outcome
            .metadata
            .display_diff
            .as_deref()
            .expect("display diff");
        assert!(diff.contains("+ alpha"));
        assert!(diff.contains("+ beta"));
        // No unified-diff header clutter (`---`/`+++`/`@@`).
        assert!(
            !diff.contains("@@"),
            "diff should not carry hunk headers: {diff}"
        );
        assert!(!diff.contains("/dev/null"));
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn write_file_existing_file_records_added_and_removed_display_diff() {
        let dir = temp_root("write_existing_diff");
        fs::write(dir.join("out.txt"), "alpha\nold\nomega\n").expect("write fixture");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({"path": "out.txt", "content": "alpha\nnew\nomega\n"}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        let diff = outcome
            .metadata
            .display_diff
            .as_deref()
            .expect("display diff");
        assert!(diff.contains("- old"));
        assert!(diff.contains("+ new"));
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn write_file_creates_parent_dirs() {
        let dir = temp_root("write_parents");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({
                    "path": "sub/nested/out.txt",
                    "content": "deep",
                }),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        assert!(dir.join("sub/nested/out.txt").exists());
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn write_file_missing_content_errors() {
        let dir = temp_root("write_missing");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(serde_json::json!({"path": "x.txt"}), ctx)
            .await;
        assert_eq!(outcome.status, mermaid_domain::ToolStatus::Error);
        let _ = fs::remove_dir_all(&dir);
    }

    // ─── F10: absolute-path block ───────────────────────────────────

    /// Reading `/etc/passwd` (or any absolute path outside workdir)
    /// must fail with a clear "outside the project" error. The tool
    /// schema advertises this contract; before F10 it was a lie.
    /// Reading an absolute path outside workdir succeeds.
    #[tokio::test]
    async fn read_file_allows_absolute_path_outside_workdir() {
        let dir = temp_root("read_abs_escape");
        let external_dir = temp_root("read_abs_external");
        let external_file = external_dir.join("ext.txt");
        fs::write(&external_file, "external content").unwrap();
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(
                serde_json::json!({"path": external_file.to_string_lossy().to_string()}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        assert_eq!(outcome.output(), "external content");
        let _ = fs::remove_dir_all(&dir);
        let _ = fs::remove_dir_all(&external_dir);
    }

    /// Absolute path that lives INSIDE the workdir is allowed.
    #[tokio::test]
    async fn read_file_accepts_absolute_path_inside_workdir() {
        let dir = temp_root("read_abs_inside");
        let file = dir.join("hello.txt");
        fs::write(&file, "ok").expect("write fixture");
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = ReadFileTool
            .execute(
                serde_json::json!({"path": file.to_string_lossy().to_string()}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        let _ = fs::remove_dir_all(&dir);
    }

    /// Relative `..`-escape and external paths are supported.
    #[tokio::test]
    async fn write_file_allows_relative_parent_and_external_path() {
        let base = temp_root("write_dotdot_escape");
        let dir = base.join("project");
        fs::create_dir_all(&dir).unwrap();
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({
                    "path": "../escape.txt",
                    "content": "written outside",
                }),
                ctx,
            )
            .await;
        assert!(
            outcome.is_success(),
            "expected success for write to ../escape.txt"
        );
        let written = base.join("escape.txt");
        assert!(written.exists());
        assert_eq!(fs::read_to_string(&written).unwrap(), "written outside");
        let _ = fs::remove_dir_all(&base);
    }

    /// `create_directory` needs the lexical-normalization fallback
    /// because the target doesn't exist yet (can't canonicalize).
    /// Verify the escape check still fires for non-existent targets.
    /// `create_directory` creates directories outside workdir when called.
    #[tokio::test]
    async fn create_directory_allows_absolute_path_outside_workdir() {
        let dir = temp_root("mkdir_abs_proj");
        let target = std::env::temp_dir().join(format!("mermaid_fs_target_{}", std::process::id()));
        let _ = fs::remove_dir_all(&target);
        let (ctx, _rx) = test_exec_context(TurnId(1), ToolCallId(1), dir.clone());
        let outcome = CreateDirectoryTool
            .execute(
                serde_json::json!({"path": target.to_string_lossy().to_string()}),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "expected success: {outcome:?}");
        assert!(target.exists());
        let _ = fs::remove_dir_all(&target);
        let _ = fs::remove_dir_all(&dir);
    }

    // ─── session-scratchpad dual root ────────────────────────────────

    /// Build an `ExecContext` with an explicit safety mode, NO approval
    /// broker, and (optionally) a materialized scratchpad. Unlike
    /// `test_exec_context` (pinned to `FullAccess`) this exercises the gate.
    fn scratch_ctx(
        mode: mermaid_runtime::SafetyMode,
        workdir: PathBuf,
        scratchpad: Option<PathBuf>,
    ) -> (ExecContext, tokio::sync::mpsc::Receiver<ProgressEvent>) {
        let mut config = mermaid_domain::Config::default();
        config.safety.mode = mode;
        let (mut ctx, rx) = crate::providers::ctx::test_exec_context_with_config(
            TurnId(1),
            ToolCallId(1),
            workdir,
            config,
        );
        ctx.scratchpad = scratchpad;
        (ctx, rx)
    }

    /// Project + scratch fixture pair with a unique, greppable name.
    fn scratch_fixture(name: &str) -> (PathBuf, PathBuf) {
        let base = std::env::temp_dir().join(format!(
            "mermaid_fs_scratch_{}_{}",
            name,
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&base);
        let project = base.join("project");
        let scratch = base.join("scratch");
        fs::create_dir_all(&project).unwrap();
        fs::create_dir_all(&scratch).unwrap();
        (project, scratch)
    }

    /// True when any checkpoint manifest on disk references `marker`. The
    /// fixture paths are unique per test+pid, so a hit can only come from
    /// the mutation under test.
    fn any_checkpoint_mentions(marker: &str) -> bool {
        let Ok(data) = mermaid_runtime::data_dir() else {
            return false;
        };
        let Ok(entries) = fs::read_dir(data.join("checkpoints")) else {
            return false;
        };
        entries.flatten().any(|entry| {
            fs::read_to_string(entry.path().join("manifest.json"))
                .is_ok_and(|manifest| manifest.contains(marker))
        })
    }

    /// Scratchpad mutations proceed in Ask mode with NO approval broker
    /// bound (the gate is bypassed entirely) and never take a checkpoint.
    #[tokio::test]
    async fn scratch_mutations_are_ungated_and_never_checkpointed() {
        let (project, scratch) = scratch_fixture("ungated");
        let marker = scratch.display().to_string();

        // write_file into the scratchpad via absolute path.
        let file = scratch.join("notes.txt");
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Ask,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({
                    "path": file.to_str().unwrap(),
                    "content": "scratch note\n",
                }),
                ctx,
            )
            .await;
        assert!(outcome.is_success(), "scratch write: {outcome:?}");
        assert_eq!(fs::read_to_string(&file).unwrap(), "scratch note\n");

        // create_directory inside the scratchpad.
        let subdir = scratch.join("work/area");
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Ask,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = CreateDirectoryTool
            .execute(serde_json::json!({"path": subdir.to_str().unwrap()}), ctx)
            .await;
        assert!(outcome.is_success(), "scratch mkdir: {outcome:?}");
        assert!(subdir.is_dir());

        // delete_file inside the scratchpad.
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Ask,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = DeleteFileTool
            .execute(serde_json::json!({"path": file.to_str().unwrap()}), ctx)
            .await;
        assert!(outcome.is_success(), "scratch delete: {outcome:?}");
        assert!(!file.exists());

        // None of the mutations checkpointed the ephemeral scratch paths.
        assert!(
            !any_checkpoint_mentions(&marker),
            "scratch mutation must not create a checkpoint"
        );
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    /// `ReadOnly` still blocks scratchpad mutations — the bypass only skips
    /// the approval flow, never the mode's mutation ban.
    #[tokio::test]
    async fn scratch_mutation_blocked_in_read_only() {
        let (project, scratch) = scratch_fixture("readonly");
        let file = scratch.join("blocked.txt");
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::ReadOnly,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({
                    "path": file.to_str().unwrap(),
                    "content": "nope",
                }),
                ctx,
            )
            .await;
        let error = outcome.error_message().expect("expected block");
        assert!(
            error.contains("blocked by policy"),
            "expected policy block, got: {error}"
        );
        assert!(!file.exists());
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    /// A path outside scratchpad in Ask mode requires approval.
    #[tokio::test]
    async fn write_outside_both_roots_requires_approval_in_ask_mode() {
        let (project, scratch) = scratch_fixture("outside");
        let outside = project.parent().unwrap().join("elsewhere").join("out.txt");
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Ask,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({
                    "path": outside.to_str().unwrap(),
                    "content": "should require approval",
                }),
                ctx,
            )
            .await;
        let error = outcome.error_message().expect("expected approval required");
        assert!(
            error.contains("Approval required"),
            "expected approval block, got: {error}"
        );
        assert!(!outside.exists());
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    /// `read_file` follows a materialized scratchpad too.
    #[tokio::test]
    async fn read_file_reads_from_scratchpad() {
        let (project, scratch) = scratch_fixture("read");
        let file = scratch.join("stash.txt");
        fs::write(&file, "stashed").unwrap();
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Ask,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": file.to_str().unwrap()}), ctx)
            .await;
        assert!(outcome.is_success(), "scratch read: {outcome:?}");
        assert_eq!(outcome.output(), "stashed");
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    #[tokio::test]
    async fn edit_file_replaces_target_content_successfully() {
        let (project, _scratch) = scratch_fixture("edit_success");
        let file = project.join("src").join("main.rs");
        fs::create_dir_all(file.parent().unwrap()).unwrap();
        fs::write(&file, "fn main() {\n    println!(\"old\");\n}\n").unwrap();

        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::FullAccess,
            project.clone(),
            None,
        );
        let outcome = EditFileTool
            .execute(
                serde_json::json!({
                    "path": "src/main.rs",
                    "target_content": "    println!(\"old\");",
                    "replacement_content": "    println!(\"new\");",
                }),
                ctx,
            )
            .await;

        assert!(outcome.is_success(), "edit outcome: {outcome:?}");
        let new_content = fs::read_to_string(&file).unwrap();
        assert_eq!(new_content, "fn main() {\n    println!(\"new\");\n}\n");
        let diff = outcome
            .metadata
            .display_diff
            .as_deref()
            .expect("display diff");
        assert!(diff.contains("-     println!(\"old\");"));
        assert!(diff.contains("+     println!(\"new\");"));
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    #[tokio::test]
    async fn edit_file_target_not_found_returns_error() {
        let (project, _scratch) = scratch_fixture("edit_not_found");
        let file = project.join("hello.txt");
        fs::write(&file, "line 1\nline 2\n").unwrap();

        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::FullAccess,
            project.clone(),
            None,
        );
        let outcome = EditFileTool
            .execute(
                serde_json::json!({
                    "path": "hello.txt",
                    "target_content": "nonexistent text",
                    "replacement_content": "replacement",
                }),
                ctx,
            )
            .await;

        assert!(!outcome.is_success());
        let err = outcome.error_message().unwrap();
        assert!(err.contains("could not find target_content"), "{err}");
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    #[tokio::test]
    async fn edit_file_ambiguous_target_errors_unless_allow_multiple() {
        let (project, _scratch) = scratch_fixture("edit_ambig");
        let file = project.join("dup.txt");
        fs::write(&file, "foo\nbar\nfoo\n").unwrap();

        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::FullAccess,
            project.clone(),
            None,
        );
        let outcome_ambig = EditFileTool
            .execute(
                serde_json::json!({
                    "path": "dup.txt",
                    "target_content": "foo",
                    "replacement_content": "baz",
                }),
                ctx,
            )
            .await;
        assert!(!outcome_ambig.is_success());
        let err = outcome_ambig.error_message().unwrap();
        assert!(err.contains("found 2 times"), "{err}");

        let (ctx2, _rx2) = scratch_ctx(
            mermaid_runtime::SafetyMode::FullAccess,
            project.clone(),
            None,
        );
        let outcome_allow = EditFileTool
            .execute(
                serde_json::json!({
                    "path": "dup.txt",
                    "target_content": "foo",
                    "replacement_content": "baz",
                    "allow_multiple": true,
                }),
                ctx2,
            )
            .await;
        assert!(outcome_allow.is_success());
        let new_content = fs::read_to_string(&file).unwrap();
        assert_eq!(new_content, "baz\nbar\nbaz\n");
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    #[tokio::test]
    async fn edit_file_fuzzy_whitespace_matching_succeeds() {
        let (project, _scratch) = scratch_fixture("edit_fuzzy");
        let file = project.join("fuzzy.txt");
        fs::write(&file, "start\n    loose_whitespace();   \nend\n").unwrap();

        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::FullAccess,
            project.clone(),
            None,
        );
        let outcome = EditFileTool
            .execute(
                serde_json::json!({
                    "path": "fuzzy.txt",
                    "target_content": "loose_whitespace();",
                    "replacement_content": "    tight_whitespace();",
                }),
                ctx,
            )
            .await;
        assert!(outcome.is_success());
        assert!(outcome.output().contains("fuzzy"));
        let new_content = fs::read_to_string(&file).unwrap();
        assert_eq!(new_content, "start\n    tight_whitespace();\nend\n");
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    // ─── Reads outside the project go through the policy gate ─────────────

    /// A project dir plus a file that sits outside every allowed root.
    fn external_read_fixture(name: &str) -> (PathBuf, PathBuf) {
        let workdir = temp_root(&format!("{name}_project"));
        let external_dir = temp_root(&format!("{name}_external"));
        let external_file = external_dir.join("ext.txt");
        fs::write(&external_file, "external content").unwrap();
        (workdir, external_file)
    }

    fn ctx_in_mode(
        mode: mermaid_runtime::SafetyMode,
        workdir: PathBuf,
    ) -> (ExecContext, tokio::sync::mpsc::Receiver<ProgressEvent>) {
        let mut config = mermaid_domain::Config::default();
        config.safety.mode = mode;
        crate::providers::ctx::test_exec_context_with_config(
            TurnId(1),
            ToolCallId(1),
            workdir,
            config,
        )
    }

    async fn read_external(ctx: ExecContext, external_file: &Path) -> ToolOutcome {
        ReadFileTool
            .execute(
                serde_json::json!({"path": external_file.to_string_lossy().to_string()}),
                ctx,
            )
            .await
    }

    /// The read-only floor: an out-of-project read is an external side effect,
    /// denied in `read_only`. Before the gate, `read_file` handed back
    /// `~/.ssh/id_rsa` in every mode without a prompt.
    #[tokio::test]
    async fn read_outside_the_project_is_denied_under_the_read_only_floor() {
        let (workdir, external_file) = external_read_fixture("read_ext_readonly");
        let (ctx, _rx) = ctx_in_mode(mermaid_runtime::SafetyMode::ReadOnly, workdir.clone());
        let outcome = read_external(ctx, &external_file).await;
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Error,
            "read_only must deny an external read: {outcome:?}"
        );
        assert!(
            !outcome.output().contains("external content"),
            "read_only leaked the file body: {}",
            outcome.output()
        );
        let _ = fs::remove_dir_all(&workdir);
        let _ = fs::remove_dir_all(external_file.parent().unwrap());
    }

    /// Inside the project nothing changes: a `read_only` session still reads
    /// its own files without a prompt.
    #[tokio::test]
    async fn read_inside_the_project_stays_ungated_in_read_only_mode() {
        let workdir = temp_root("read_inside_readonly");
        fs::write(workdir.join("notes.md"), "project content").unwrap();
        let (ctx, _rx) = ctx_in_mode(mermaid_runtime::SafetyMode::ReadOnly, workdir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "notes.md"}), ctx)
            .await;
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Success,
            "{outcome:?}"
        );
        assert_eq!(outcome.output(), "project content");
        let _ = fs::remove_dir_all(&workdir);
    }

    /// `ask` mode prompts, and the prompt's "don't ask again" scope is the
    /// file's directory — never the whole tool, so approving one external
    /// read cannot silently cover a credential file later in the session.
    #[tokio::test]
    async fn read_outside_the_project_prompts_in_ask_mode_scoped_to_its_directory() {
        let (workdir, external_file) = external_read_fixture("read_ext_ask");
        let (tx, mut rx) = tokio::sync::mpsc::channel::<mermaid_domain::Msg>(8);
        let broker = crate::providers::ApprovalBroker::new(tx);
        let (mut ctx, _prx) = ctx_in_mode(mermaid_runtime::SafetyMode::Ask, workdir.clone());
        ctx.approval = Some(broker.clone());
        let file_for_task = external_file.clone();
        let handle = tokio::spawn(async move { read_external(ctx, &file_for_task).await });

        let (call_id, tool, scope) = match rx.recv().await.expect("approval requested") {
            mermaid_domain::Msg::ApprovalRequested {
                call_id,
                tool,
                allowlist_scope,
                ..
            } => (call_id, tool, allowlist_scope),
            other => panic!("expected ApprovalRequested, got {other:?}"),
        };
        assert_eq!(tool, "read_file");
        let expected_dir = external_file.canonicalize().unwrap();
        let expected_dir = expected_dir.parent().unwrap();
        assert_eq!(
            scope,
            format!("read_file:{}", expected_dir.display()),
            "external reads are allowlisted per directory"
        );

        broker.resolve(call_id, crate::providers::ApprovalDecision::Approve);
        let outcome = handle.await.unwrap();
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Success,
            "{outcome:?}"
        );
        assert_eq!(outcome.output(), "external content");
        let _ = fs::remove_dir_all(&workdir);
        let _ = fs::remove_dir_all(external_file.parent().unwrap());
    }

    /// The user's "No" is final: the read fails and the body never leaves disk.
    #[tokio::test]
    async fn read_outside_the_project_denied_by_the_user_is_an_error() {
        let (workdir, external_file) = external_read_fixture("read_ext_deny");
        let (tx, mut rx) = tokio::sync::mpsc::channel::<mermaid_domain::Msg>(8);
        let broker = crate::providers::ApprovalBroker::new(tx);
        let (mut ctx, _prx) = ctx_in_mode(mermaid_runtime::SafetyMode::Ask, workdir.clone());
        ctx.approval = Some(broker.clone());
        let file_for_task = external_file.clone();
        let handle = tokio::spawn(async move { read_external(ctx, &file_for_task).await });
        let call_id = match rx.recv().await.expect("approval requested") {
            mermaid_domain::Msg::ApprovalRequested { call_id, .. } => call_id,
            other => panic!("expected ApprovalRequested, got {other:?}"),
        };
        broker.resolve(call_id, crate::providers::ApprovalDecision::Deny);
        let outcome = handle.await.unwrap();
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Error,
            "{outcome:?}"
        );
        assert!(!outcome.output().contains("external content"));
        let _ = fs::remove_dir_all(&workdir);
        let _ = fs::remove_dir_all(external_file.parent().unwrap());
    }

    /// A symlink planted inside the project that resolves outside it is an
    /// external read: the resolver canonicalizes through the link, so the
    /// gate sees the real target, not the friendly-looking relative path.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_symlink_inside_the_project_that_points_outside_is_gated() {
        let (workdir, external_file) = external_read_fixture("read_ext_symlink");
        std::os::unix::fs::symlink(&external_file, workdir.join("innocent.txt")).unwrap();
        let (ctx, _rx) = ctx_in_mode(mermaid_runtime::SafetyMode::ReadOnly, workdir.clone());
        let outcome = ReadFileTool
            .execute(serde_json::json!({"path": "innocent.txt"}), ctx)
            .await;
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Error,
            "{outcome:?}"
        );
        assert!(!outcome.output().contains("external content"));
        let _ = fs::remove_dir_all(&workdir);
        let _ = fs::remove_dir_all(external_file.parent().unwrap());
    }

    /// `auto` mode classifies external writes instead of allowing them: with no
    /// classifier bound the gate escalates, so nothing lands outside the
    /// project unasked. Before this, an external write classified like an
    /// in-project edit and `auto` wrote it with a checkpoint and no question.
    #[tokio::test]
    async fn write_outside_the_project_is_not_silently_allowed_in_auto_mode() {
        let (project, scratch) = scratch_fixture("outside_auto");
        let outside = project.parent().unwrap().join("elsewhere").join("out.txt");
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Auto,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({
                    "path": outside.to_str().unwrap(),
                    "content": "must not land unasked",
                }),
                ctx,
            )
            .await;
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Error,
            "auto mode must escalate an external write: {outcome:?}"
        );
        assert!(!outside.exists(), "the external file was written anyway");
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }

    /// The control: an in-project write in `auto` mode is still an ordinary
    /// edit and proceeds without a prompt.
    #[tokio::test]
    async fn write_inside_the_project_stays_allowed_in_auto_mode() {
        let (project, scratch) = scratch_fixture("inside_auto");
        let (ctx, _rx) = scratch_ctx(
            mermaid_runtime::SafetyMode::Auto,
            project.clone(),
            Some(scratch.clone()),
        );
        let outcome = WriteFileTool
            .execute(
                serde_json::json!({"path": "inside.txt", "content": "fine"}),
                ctx,
            )
            .await;
        assert_eq!(
            outcome.status,
            mermaid_domain::ToolStatus::Success,
            "{outcome:?}"
        );
        assert_eq!(
            fs::read_to_string(project.join("inside.txt")).unwrap(),
            "fine"
        );
        let _ = fs::remove_dir_all(project.parent().unwrap());
    }
}