croft-software 0.2.232

VSCode-style TUI written in Rust
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
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
//! The pilot's own tests, moved out of pair.rs (#2062).

use super::*;
use crate::collab::{ResolvedSpan, relay_serve};
use crate::lsp::manager::is_on_path;

// Live smoke of the WHOLE local backend — the real seat_local worker,
// fence machine, collab apply, and owner convergence — against a real
// Anthropic-compatible endpoint. Gated so the normal suite/CI skips it.
//
//   CROFT_SPIKE_OLLAMA=1 \
//   CROFT_SPIKE_OLLAMA_URL=http://localhost:11434 \
//   CROFT_SPIKE_OLLAMA_MODEL=qwen3-coder:30b \
//   cargo test --release ollama_live -- --nocapture --ignored
#[test]
#[ignore = "needs a live Ollama; run explicitly with CROFT_SPIKE_OLLAMA=1"]
fn ollama_live_smoke_drives_seat_local_end_to_end() {
    if std::env::var("CROFT_SPIKE_OLLAMA").is_err() {
        eprintln!("skip: set CROFT_SPIKE_OLLAMA=1 to run the live smoke");
        return;
    }
    let url =
        std::env::var("CROFT_SPIKE_OLLAMA_URL").unwrap_or_else(|_| "http://localhost:11434".into());
    let model =
        std::env::var("CROFT_SPIKE_OLLAMA_MODEL").unwrap_or_else(|_| "qwen3-coder:30b".into());

    let harness = OwnerHarness::start("def greet(name):\n    return \"hi \" + name\n");
    let pilot = seat_local(&harness.socket, "pilot", &url, &model, None).unwrap();
    send_turn(
        &pilot.state,
        &pilot.sink,
        "@demo.txt Rewrite greet to return the f-string f\"Hello, {name}!\" \
             instead of concatenation, using an EDIT fence.",
    )
    .unwrap();
    let end = pilot
        .turn_rx
        .recv_timeout(Duration::from_secs(300))
        .expect("turn ends");
    assert!(!end.is_error, "turn failed: {}", end.text);
    harness.wait_until("the f-string edit to converge", |h| {
        h.doc().is_some_and(|d| d.contains("f\"Hello, {name}!\""))
    });
    let doc = harness.doc().unwrap();
    assert!(
        !doc.contains("+ name"),
        "the old concatenation must be gone:\n{doc}"
    );
    pilot.shutdown();
}

use std::sync::atomic::{AtomicBool, Ordering};

/// The Claude sink wraps a turn body in a stream-json user message and
/// writes it to the child's stdin, byte-identical to the pre-TurnSink
/// path (`cat` echoes its stdin back so the test can read what landed).
#[test]
fn turn_sink_claude_writes_stream_json_to_stdin() {
    let mut child = Command::new("cat")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("spawn cat");
    let stdin = child.stdin.take().unwrap();
    let stdout = child.stdout.take().unwrap();
    let sink = TurnSink::Claude(Arc::new(Mutex::new(Some(stdin))));
    sink.send_user("hello\nworld").unwrap();
    let TurnSink::Claude(w) = &sink else {
        unreachable!()
    };
    w.lock().unwrap().take(); // EOF so cat exits
    let mut line = String::new();
    std::io::BufReader::new(stdout)
        .read_line(&mut line)
        .unwrap();
    let _ = child.wait();
    let v: Value = serde_json::from_str(&line).expect("one stream-json line");
    assert_eq!(v["type"], "user");
    assert_eq!(v["message"]["role"], "user");
    assert_eq!(v["message"]["content"], "hello\nworld");
}

/// The local backend appends the whole-line-fence guidance to the shared
/// pair prompt: weaker models are steered to row ranges instead of
/// character columns (the spike's coordinate slip).
#[test]
fn local_provider_appends_line_range_guidance() {
    let prompt = system_prompt(&Provider::Local {
        base_url: String::from("http://localhost:11434"),
    });
    assert!(prompt.starts_with(PAIR_SYSTEM_PROMPT));
    assert!(
        prompt.contains("<start_row>-<end_row>"),
        "suffix must teach the two-integer whole-line header"
    );
}

/// The claude backend's prompt is byte-identical to what 0.1.635 shipped.
#[test]
fn claude_provider_prompt_is_unchanged() {
    assert_eq!(system_prompt(&Provider::Claude), PAIR_SYSTEM_PROMPT);
}

/// Bounded poll toward `expected` through a contention-lossy accessor,
/// returning the last value read. The host's UI-tick reads are
/// deliberately imprecise under momentary pump contention
/// (`notes_snapshot` serves its stale cache on a contended try_lock,
/// `is_busy` answers busy on a lost `lock_briefly` race), so a one-shot
/// read of a state-final value can flake on a starved runner. The
/// deadline only bounds a real regression.
fn settle<T: PartialEq>(expected: &T, mut read: impl FnMut() -> T) -> T {
    let deadline = Instant::now() + Duration::from_secs(60);
    loop {
        let got = read();
        if &got == expected || Instant::now() >= deadline {
            return got;
        }
        std::thread::sleep(Duration::from_millis(10));
    }
}

/// A relay plus a pumping owner session that serves `demo.txt`,
/// collecting every owner-side event for the assertions.
struct OwnerHarness {
    _dir: tempfile::TempDir,
    socket: PathBuf,
    owner: Arc<Mutex<CollabSession>>,
    events: Arc<Mutex<Vec<CollabEvent>>>,
    stop: Arc<AtomicBool>,
    pump: Option<std::thread::JoinHandle<()>>,
}

impl OwnerHarness {
    fn start(text: &'static str) -> Self {
        let dir = tempfile::tempdir().unwrap();
        let socket = dir.path().join("p.collab.sock");
        {
            let s = socket.clone();
            std::thread::spawn(move || {
                let _ = relay_serve(&s);
            });
        }
        let deadline = Instant::now() + Duration::from_secs(5);
        let owner = loop {
            if let Some(ch) = CollabChannel::connect(&socket, CollabRole::Owner) {
                break CollabSession::new(ch, "owner".into());
            }
            assert!(Instant::now() < deadline, "relay never came up");
            std::thread::sleep(Duration::from_millis(10));
        };
        let owner = Arc::new(Mutex::new(owner));
        let events: Arc<Mutex<Vec<CollabEvent>>> = Arc::new(Mutex::new(Vec::new()));
        let stop = Arc::new(AtomicBool::new(false));
        let pump = {
            let (owner, events, stop) = (owner.clone(), events.clone(), stop.clone());
            std::thread::spawn(move || {
                while !stop.load(Ordering::Relaxed) {
                    {
                        let mut o = owner.lock().unwrap();
                        let ev = o.poll(|_| Some(text.to_string()));
                        events.lock().unwrap().extend(ev);
                    }
                    std::thread::sleep(Duration::from_millis(5));
                }
            })
        };
        Self {
            _dir: dir,
            socket,
            owner,
            events,
            stop,
            pump: Some(pump),
        }
    }

    fn doc(&self) -> Option<String> {
        self.owner
            .lock()
            .unwrap()
            .doc_text("demo.txt")
            .map(str::to_string)
    }

    fn wait_until(&self, what: &str, mut cond: impl FnMut(&Self) -> bool) {
        // A liveness backstop, generous on purpose: the python3 helper
        // and relay round trips race the parallel suite's load (#47),
        // and a tight ceiling here measured the machine, not croft.
        let deadline = Instant::now() + Duration::from_secs(60);
        while !cond(self) {
            assert!(Instant::now() < deadline, "timed out waiting for {what}");
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    fn stream_states(&self) -> Vec<(String, bool)> {
        self.events
            .lock()
            .unwrap()
            .iter()
            .filter_map(|e| match e {
                CollabEvent::StreamState { name, active, .. } => Some((name.clone(), *active)),
                _ => None,
            })
            .collect()
    }

    fn remote_edit_count(&self) -> usize {
        self.events
            .lock()
            .unwrap()
            .iter()
            .filter(|e| matches!(e, CollabEvent::RemoteEdit { .. }))
            .count()
    }

    fn carets(&self) -> Vec<(String, usize, usize)> {
        self.events
            .lock()
            .unwrap()
            .iter()
            .filter_map(|e| match e {
                CollabEvent::Caret { name, row, col, .. } => Some((name.clone(), *row, *col)),
                _ => None,
            })
            .collect()
    }
}

impl Drop for OwnerHarness {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Some(p) = self.pump.take() {
            let _ = p.join();
        }
    }
}

/// A one-shot Anthropic-SSE stub: accepts one connection, drains the
/// request, and streams `deltas` as `content_block_delta`/`text_delta`
/// events followed by `message_stop`, closing to end the stream.
fn serve_sse_once(deltas: Vec<&'static str>) -> (String, std::thread::JoinHandle<()>) {
    let mut body = String::new();
    for d in deltas {
        let ev = json!({
            "type": "content_block_delta",
            "index": 0,
            "delta": { "type": "text_delta", "text": d },
        });
        body.push_str(&format!("event: content_block_delta\ndata: {ev}\n\n"));
    }
    body.push_str("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n");
    serve_http_once(format!(
        "HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
             connection: close\r\n\r\n{body}"
    ))
}

/// One-shot HTTP stub answering `resp` verbatim after draining the whole
/// request (headers + Content-Length body), per the SSE-stub trap.
fn serve_http_once(resp: String) -> (String, std::thread::JoinHandle<()>) {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let base_url = format!("http://{}", listener.local_addr().unwrap());
    let server = std::thread::spawn(move || {
        let (mut sock, _) = listener.accept().unwrap();
        // Drain the WHOLE request (headers + Content-Length body) before
        // responding: answering after the first read races the client's
        // body write — the close RSTs the socket and eats the response.
        let mut req = Vec::new();
        let mut buf = [0u8; 4096];
        loop {
            let n = sock.read(&mut buf).unwrap_or(0);
            if n == 0 {
                break;
            }
            req.extend_from_slice(&buf[..n]);
            let text = String::from_utf8_lossy(&req).to_ascii_lowercase();
            if let Some(head_end) = text.find("\r\n\r\n") {
                let content_length = text[..head_end]
                    .lines()
                    .find_map(|l| l.strip_prefix("content-length:"))
                    .and_then(|v| v.trim().parse::<usize>().ok())
                    .unwrap_or(0);
                if req.len() >= head_end + 4 + content_length {
                    break;
                }
            }
        }
        let _ = sock.write_all(resp.as_bytes());
    });
    (base_url, server)
}

/// A guest PairState over the harness relay plus the same pump thread
/// the seated pilot runs (bootstraps only land when someone polls).
fn pumped_state(
    harness: &OwnerHarness,
) -> (
    Arc<Mutex<PairState>>,
    Arc<AtomicBool>,
    std::thread::JoinHandle<()>,
) {
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Arc::new(Mutex::new(PairState::new(session, None)));
    let stop = Arc::new(AtomicBool::new(false));
    let pump = {
        let (state, stop) = (state.clone(), stop.clone());
        let sink = TurnSink::Claude(Arc::new(Mutex::new(None)));
        std::thread::spawn(move || {
            let req_id = AtomicU64::new(0);
            while !stop.load(Ordering::Relaxed) {
                pump_session(&state, &sink, &req_id);
                std::thread::sleep(Duration::from_millis(5));
            }
        })
    };
    (state, stop, pump)
}

/// The UI thread must NEVER wait on the pilot's state mutex: pilot
/// threads hold it across blocking relay writes (write_frame_blocking
/// spins on WouldBlock with no deadline), and the App draining the
/// relay is what completes those writes — so a blocking read from the
/// render/tick path closes a circular wait and hangs the editor. Every
/// UI-facing host method answers from a fallback when the mutex is
/// contended.
#[test]
fn ui_reads_answer_instantly_while_the_pilot_mutex_is_held() {
    let harness = OwnerHarness::start("hello world");
    let host = crate::pair_host::PairHost::spawn(PairConfig {
        socket: harness.socket.clone(),
        workspace: harness._dir.path().to_path_buf(),
        name: String::from("nav"),
        model: Some(String::from("m")),
        task: None,
        provider: Provider::Local {
            base_url: String::from("http://127.0.0.1:9"),
        },
    })
    .unwrap();
    let state = host.state_for_tests().expect("pilot state");
    let (locked_tx, locked_rx) = std::sync::mpsc::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel::<()>();
    let holder = std::thread::spawn(move || {
        let _guard = state.lock().unwrap();
        locked_tx.send(()).unwrap();
        let _ = release_rx.recv();
    });
    locked_rx.recv().unwrap();
    let (done_tx, done_rx) = std::sync::mpsc::channel();
    let reader = std::thread::spawn(move || {
        let busy = host.is_busy();
        let _ = host.caret_sites();
        let _ = host.notes_snapshot("demo.txt");
        let seen = host.last_seen("demo.txt");
        let rejected = host.send_yield_turn("demo.txt", "x").is_err();
        done_tx.send((busy, seen, rejected)).unwrap();
        drop(host);
    });
    let (busy, seen, rejected) = done_rx
        .recv_timeout(Duration::from_secs(2))
        .expect("UI reads must answer instantly while the pilot mutex is held");
    assert!(busy, "contended = the pilot is mid-work = busy");
    assert_eq!(seen, None, "no baseline is better than a blocked frame");
    assert!(rejected, "a send under contention reports mid-turn");
    release_tx.send(()).unwrap();
    holder.join().unwrap();
    reader.join().unwrap();
}

/// is_busy() must stay true from the send until the App CONSUMES the
/// turn's end: the reader clears turn_active before it queues TurnEnd,
/// so a tick landing in that window saw an idle seat and started a new
/// turn that stole the finished turn's origin and counters.
#[test]
fn the_seat_stays_busy_until_turn_done_is_consumed() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");
    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("stream");
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
    host.send_yield_turn("demo.txt", "hello world").unwrap();
    let state = host.state_for_tests().expect("pilot state");
    // Wait until the PILOT finished the turn (its reader cleared the
    // latch); the App has not polled yet.
    let deadline = Instant::now() + Duration::from_secs(60);
    while state.lock().unwrap().turn_active() {
        assert!(Instant::now() < deadline, "the fake turn never finished");
        std::thread::sleep(Duration::from_millis(5));
    }
    assert!(
        host.is_busy(),
        "the turn's end has not been consumed yet: the seat is still busy"
    );
    // The gate must hold at the SEND paths too, not just is_busy():
    // direct callers would otherwise overwrite the unconsumed turn's
    // staging in this window.
    let refused = host
        .send_yield_turn("demo.txt", "hello world")
        .expect_err("a send in the finish window must be refused");
    assert!(
        refused.to_string().contains("mid-turn"),
        "the refusal names the reason: {refused}"
    );
    let deadline = Instant::now() + Duration::from_secs(5);
    loop {
        if host
            .poll()
            .iter()
            .any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
        {
            break;
        }
        assert!(Instant::now() < deadline, "TurnDone never arrived");
        std::thread::sleep(Duration::from_millis(5));
    }
    assert!(
        !settle(&false, || host.is_busy()),
        "consumed: the seat is idle again"
    );
    drop(host);
}

/// A yield whose send failed must not keep the look: last_seen is the
/// diff baseline (a retry would diff the file against itself and show
/// the model nothing), and the latched comment_only would silently
/// edit-suppress the next successful turn.
#[test]
fn a_failed_yield_leaves_no_look_behind() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    // A child whose stdin is dead by the time Died is observable: it
    // closes stdin BEFORE stdout, so stdout's EOF (what fires Died)
    // proves the write end already has no reader. A child that merely
    // exits (`-c pass`) races process teardown: on a starved runner the
    // reader sees stdout close while the process, and with it stdin's
    // read end, is still winding down, and the send lands in the pipe
    // buffer instead of failing. The sleep keeps the pid alive for the
    // host's grace-kill teardown, like the real CLI's linger.
    let mut cmd = Command::new("python3");
    cmd.arg("-c")
        .arg("import os,time; os.close(0); os.close(1); time.sleep(60)");
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
    let deadline = Instant::now() + Duration::from_secs(60);
    loop {
        if host
            .poll()
            .iter()
            .any(|e| matches!(e, crate::pair_host::PairEvent::Died(_)))
        {
            break;
        }
        assert!(Instant::now() < deadline, "the child never died");
        std::thread::sleep(Duration::from_millis(10));
    }
    let err = host.send_yield_turn("demo.txt", "hello world").unwrap_err();
    // Pin the failure to the sink write AFTER staging: a pre-staging
    // rejection ("mid-turn", "pilot is gone") would pass the rollback
    // assertions below vacuously, without ever staging a look.
    assert!(
        format!("{err:#}").contains("claude"),
        "the send must fail at the claude sink, not before staging: {err:#}"
    );
    assert_eq!(
        host.last_seen("demo.txt"),
        None,
        "a failed yield keeps no baseline"
    );
    assert!(
        !settle(&false, || host.is_busy()),
        "a failed yield keeps no busy latch"
    );
    drop(host);
}

/// A send that never reached the endpoint must not wedge the seat: the
/// busy latch and the pending cancel note were committed BEFORE the
/// write, so a dead stdin left is_busy() true for the rest of the
/// seat's life and silently ate the "your edit was cancelled" preamble
/// owed to the model.
#[test]
fn a_failed_send_unlatches_the_turn_and_keeps_the_cancel_note() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    state.lock().unwrap().pending_note = Some(String::from("cancelled: f.rs"));
    let dead = TurnSink::Claude(Arc::new(Mutex::new(None)));
    let err = write_user_turn(&state, &dead, String::from("hi"))
        .expect_err("a dead stdin must fail the send");
    assert!(err.to_string().contains("stdin"), "{err}");
    {
        let st = state.lock().unwrap();
        assert!(
            !st.turn_active,
            "a turn that never left must not stay active"
        );
        assert_eq!(
            st.pending_note.as_deref(),
            Some("cancelled: f.rs"),
            "the cancel note is still owed to the model"
        );
    }
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// An unseat moments before exit must still reap the pilot's child:
/// Drop detaches its grace-kill onto a thread, and a thread that dies
/// with the process leaves the child running (orphaned, still holding
/// its MCP subprocesses). join_teardowns() is the exit path's barrier
/// for every detached teardown.
#[test]
fn join_teardowns_reaps_a_dropped_hosts_child() {
    if !crate::lsp::manager::is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let mut cmd = std::process::Command::new("python3");
    cmd.arg("-c").arg("import time; time.sleep(120)");
    let host = crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
    // Checked by process id, not by `pgrep -f` on a marker in the command
    // line: for a moment after spawn returns, the kernel is still putting
    // the new program's arguments in place, and a busy machine can stretch
    // that moment past pgrep's start-up, so pgrep missed a running child
    // (#2725). `kill(pid, 0)` answers from the moment the process exists
    // until it is reaped.
    let pid = host
        .child_id_for_tests()
        .expect("a claude seat has a child") as libc::pid_t;
    // SAFETY: signal 0 only checks that `pid` exists; nothing is sent.
    let exists = |pid| unsafe { libc::kill(pid, 0) } == 0;
    assert!(exists(pid), "the sleeper child must be running");
    drop(host);
    crate::pair_host::join_teardowns();
    assert!(
        !exists(pid),
        "after join_teardowns the dropped host's child must be reaped"
    );
}

/// A valid fence header arriving as the turn's LAST bytes (no trailing
/// newline) is protocol residue, not prose: flushing it as commentary
/// rendered the raw <<<NOTE ...>>> marker inside a comment box. A
/// malformed <<<-prefixed tail keeps the existing commentary behavior
/// (the malformed-header rule).
#[test]
fn an_unterminated_fence_header_never_leaks_into_commentary() {
    let mut m = FenceMachine::new();
    let mut events = m.push("thinking\n<<<NOTE src/f.rs:12>>>");
    events.extend(m.finish());
    let commentary = commentary_of(&events);
    assert!(commentary.contains("thinking"));
    assert!(
        !commentary.contains("<<<NOTE"),
        "protocol residue leaked: {commentary:?}"
    );
    let mut events = m.push("<<<EDIT src/f.rs:0:0-0:5>>>");
    events.extend(m.finish());
    assert!(
        !commentary_of(&events).contains("<<<EDIT"),
        "an EDIT header tail is the same class"
    );
    let mut events = m.push("<<<EDIT garbage");
    events.extend(m.finish());
    assert!(
        commentary_of(&events).contains("<<<EDIT garbage"),
        "a malformed tail stays commentary"
    );
}

/// The persistent AI caret: a park before the file is live resolves as
/// soon as the bootstrap lands (the pump broadcasts it), and a park on a
/// live file broadcasts immediately, clamped to the last line.
#[test]
fn park_caret_waits_for_bootstrap_then_broadcasts() {
    let harness = OwnerHarness::start("l0\nl1\nl2");
    let (state, stop, pump) = pumped_state(&harness);
    {
        let mut st = state.lock().unwrap();
        st.begin_turn("demo.txt", "l0\nl1\nl2", false); // requests the file
        st.park_caret("demo.txt", 1); // not live yet: pending
    }
    harness.wait_until("the parked caret to broadcast", |h| {
        h.carets()
            .iter()
            .any(|(n, r, c)| n == "pilot" && *r == 1 && *c == 0)
    });
    // Live now: an out-of-range park clamps to the last line.
    state.lock().unwrap().park_caret("demo.txt", 99);
    harness.wait_until("the clamped caret", |h| {
        h.carets().iter().any(|(n, r, _)| n == "pilot" && *r == 2)
    });
    stop.store(true, Ordering::Relaxed);
    let _ = pump.join();
}

/// Every landed note parks the pilot's caret at its anchor row: the AI
/// is visibly "looking" where it just commented.
#[test]
fn a_landed_note_parks_the_pilot_caret_at_its_row() {
    let harness = OwnerHarness::start("l0\nl1\nl2");
    let (state, stop, pump) = pumped_state(&harness);
    apply_fence_event(
        &state,
        FenceEvent::NoteStart {
            file: "demo.txt".into(),
            row: 2,
        },
    );
    apply_fence_event(&state, FenceEvent::NoteBody("look here".into()));
    apply_fence_event(&state, FenceEvent::NoteEnd);
    harness.wait_until("the note's caret", |h| {
        h.carets().iter().any(|(n, r, _)| n == "pilot" && *r == 2)
    });
    stop.store(true, Ordering::Relaxed);
    let _ = pump.join();
}

/// Ask and reply turns park the navigator's caret at their focus row
/// (the invoked range start / the answered note's row), so the caret is
/// visible from the first interaction even on comment-only turns.
#[test]
fn ask_and_reply_turns_park_the_navigator_caret() {
    let harness = OwnerHarness::start("l0\nl1\nl2");
    let mut cmd = Command::new("cat");
    cmd.stdin(Stdio::piped());
    let asker = crate::pair_host::PairHost::spawn_cmd(&harness.socket, "asker", None, cmd).unwrap();
    asker
        .send_ask_turn("demo.txt", (1, 1), "", "look at this", "l0\nl1\nl2")
        .unwrap();
    harness.wait_until("the ask turn's caret", |h| {
        h.carets().iter().any(|(n, r, _)| n == "asker" && *r == 1)
    });
    // The caret reaching the owner and the seat recording its site id
    // are two arrivals, not one: the assertion must wait for the state
    // it checks, not for a neighbour that usually lands first (#338).
    harness.wait_until("the seat's per-file site ids (its wire identity)", |_| {
        !asker.caret_sites().is_empty()
    });

    let mut cmd = Command::new("cat");
    cmd.stdin(Stdio::piped());
    let replier =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "replier", None, cmd).unwrap();
    replier
        .send_reply_turn("demo.txt", 2, "seed note", "why here?", "l0\nl1\nl2")
        .unwrap();
    harness.wait_until("the reply turn's caret", |h| {
        h.carets().iter().any(|(n, r, _)| n == "replier" && *r == 2)
    });
}

/// A rollback deferred past its moment (the failed send could not get
/// the pilot mutex, so the unstage was handed to a waiting thread) must
/// land as a no-op once a NEWER turn has staged: unconditionally
/// restoring the old turn's books would flip the live turn
/// comment-only, retarget it, and rot its yield-diff baseline.
#[test]
fn a_stale_rollback_never_stomps_a_newer_turn() {
    let harness = OwnerHarness::start("hello");
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Mutex::new(PairState::new(session, None));
    let mut st = state.lock().unwrap();
    let staged_a = st.stage_turn("demo.txt", "one", false);
    // Turn B stages before A's deferred rollback lands.
    let _staged_b = st.stage_turn("demo.txt", "two", true);
    st.unstage_turn(staged_a); // the deferred rollback finally runs
    assert_eq!(
        st.last_seen_of("demo.txt").as_deref(),
        Some("two"),
        "a stale rollback must not replace the live turn's baseline"
    );
    assert!(
        st.comment_only,
        "a stale rollback must not unlatch the live turn's comment-only"
    );
}

/// A failed send's undo that could not get the pilot mutex is parked,
/// not handed to an untracked thread: while parked (contended lock) a
/// new send is refused, and once the lock is free the undo applies and
/// the park clears — the books are never a mix of two turns.
#[test]
fn a_parked_rollback_blocks_sends_until_it_applies() {
    let harness = OwnerHarness::start("hello");
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Mutex::new(PairState::new(session, None));
    let staged = state.lock().unwrap().stage_turn("demo.txt", "one", false);
    let parked = Mutex::new(Some(staged));
    // Contended: still parked, the caller must refuse to stage.
    assert!(!crate::pair_host::drain_parked(&parked, || None));
    assert!(parked.lock().unwrap().is_some(), "the undo is not lost");
    // Free: the undo applies and the park clears.
    assert!(crate::pair_host::drain_parked(&parked, || Some(
        state.lock().unwrap()
    )));
    assert!(parked.lock().unwrap().is_none());
    assert_eq!(
        state.lock().unwrap().last_seen_of("demo.txt"),
        None,
        "the parked undo restored the pre-turn baseline"
    );
}

/// Rollbacks are undo records: applied newest-first they must unwind
/// all the way back to the original state. Turn A fails (rollback
/// parked), turn B stages over A's residue and fails too; B's undo
/// puts A's books back and must also re-admit A's undo, or the seat
/// stays latched to A's staged look, comment-only flag, and target.
#[test]
fn nested_rollbacks_unwind_to_the_original_state() {
    let harness = OwnerHarness::start("hello");
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Mutex::new(PairState::new(session, None));
    let mut st = state.lock().unwrap();
    let staged_a = st.stage_turn("demo.txt", "one", false);
    let staged_b = st.stage_turn("demo.txt", "two", true);
    st.unstage_turn(staged_b); // B's send failed: newest undo first
    st.unstage_turn(staged_a); // then A's parked undo lands
    assert_eq!(
        st.last_seen_of("demo.txt"),
        None,
        "both failed turns unwound: no baseline remains"
    );
    assert!(!st.comment_only, "the original comment-only state is back");
    assert_eq!(st.target_file, None, "the original target is back");
}

/// claude's stdout is an external input: a `result` line with no
/// dispatched turn behind it (a duplicate emission, or a spontaneous
/// one) must end nothing. Queuing a phantom TurnEnd would release the
/// host's busy gate under a newer turn, and the handler's flag resets
/// would clear that turn's comment-only staging.
#[test]
fn an_unsolicited_result_ends_no_turn() {
    let harness = OwnerHarness::start("hello");
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Mutex::new(PairState::new(session, None));
    let mut fence = FenceMachine::new();
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let result = json!({ "type": "result", "is_error": false, "result": "done" });

    // A dispatched turn ends exactly once.
    state.lock().unwrap().turn_active = true;
    handle_claude_event(&state, &mut fence, &result, &turn_tx);
    assert!(
        turn_rx.try_recv().is_ok(),
        "the genuine result ends the turn"
    );

    // A newer comment-only turn stages, then the duplicate arrives.
    state.lock().unwrap().stage_turn("demo.txt", "one", true);
    handle_claude_event(&state, &mut fence, &result, &turn_tx);
    assert!(
        turn_rx.try_recv().is_err(),
        "a duplicate result must not queue a phantom TurnEnd"
    );
    assert!(
        state.lock().unwrap().comment_only,
        "a duplicate result must not clear the newer turn's comment-only staging"
    );
}

/// An edit taking over the caret supersedes an unresolved pending park:
/// once the region opens (and parks the caret at its start), the stale
/// ask-row park must never fire afterwards and yank the caret back.
#[test]
fn an_open_region_supersedes_a_pending_caret_park() {
    let harness = OwnerHarness::start("l0\nl1\nl2");
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Mutex::new(PairState::new(session, None));
    {
        let mut st = state.lock().unwrap();
        st.begin_turn("demo.txt", "l0\nl1\nl2", false);
        st.park_caret("demo.txt", 1);
        assert!(st.pending_caret.is_some(), "not live yet: the park waits");
    }
    // Ingest the bootstrap by hand (no pump thread, so nothing can
    // resolve the pending park before the edit lands).
    let deadline = Instant::now() + Duration::from_secs(5);
    loop {
        let mut st = state.lock().unwrap();
        let _ = st.session.poll(|_| None);
        if st.session.is_live("demo.txt") {
            break;
        }
        drop(st);
        assert!(Instant::now() < deadline, "demo.txt never went live");
        std::thread::sleep(Duration::from_millis(5));
    }
    let mut st = state.lock().unwrap();
    open_region(&mut st, "demo.txt", (0, 0), (0, 0));
    assert!(
        st.pending_caret.is_none(),
        "the edit's caret supersedes the pending park"
    );
}

/// A PARSEABLE whole-line header with an inverted range (`5-3`) drops
/// the edit through the OUTPUT channel, never stderr: the resident
/// navigator runs in-process, so an eprintln! would land on the
/// alternate screen and corrupt the render. The inverted header is a
/// one-token slip for the weak local models the whole-line form serves.
#[test]
fn an_inverted_fence_range_drops_the_edit_via_output_not_stderr() {
    let harness = OwnerHarness::start("l0\nl1\nl2\nl3\nl4\nl5");
    let session = connect_session(&harness.socket, "pilot").unwrap();
    let state = Mutex::new(PairState::new(session, None));
    state
        .lock()
        .unwrap()
        .begin_turn("demo.txt", "l0\nl1\nl2\nl3\nl4\nl5", false);
    let deadline = Instant::now() + Duration::from_secs(5);
    loop {
        let mut st = state.lock().unwrap();
        let _ = st.session.poll(|_| None);
        if st.session.is_live("demo.txt") {
            break;
        }
        drop(st);
        assert!(Instant::now() < deadline, "demo.txt never went live");
        std::thread::sleep(Duration::from_millis(5));
    }
    let baseline = crate::output::snapshot("Navigator")
        .unwrap_or_default()
        .len();
    let mut st = state.lock().unwrap();
    open_region(&mut st, "demo.txt", (4, 0), (2, usize::MAX));
    assert!(st.discarding, "the inverted edit's body must be discarded");
    assert_eq!(
        st.session.doc_text("demo.txt"),
        Some("l0\nl1\nl2\nl3\nl4\nl5"),
        "nothing may be applied"
    );
    let lines = crate::output::snapshot("Navigator").unwrap_or_default();
    assert!(
        lines
            .iter()
            .skip(baseline)
            .any(|l| l.text.contains("inverted")),
        "the drop is reported on the OUTPUT channel"
    );
}

/// Offline slice of the local backend: one canned /v1/messages SSE turn
/// streams a fenced edit through the REAL fence machine and apply path
/// into the owner's replica, appends the assistant message, and ends
/// the turn cleanly. No Ollama needed.
#[test]
fn stream_turn_applies_a_fenced_edit() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let (base_url, server) = serve_sse_once(vec![
        "Let me fix that.\n",
        "<<<EDIT demo.txt:0:6-0:11>",
        ">>\n",
        "streamed",
        " edit",
        "\n<<<END>>>\nDone.\n",
    ]);

    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "fix demo.txt" })];
    local::stream_turn(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();

    harness.wait_until("the streamed edit to converge", |h| {
        h.doc().as_deref() == Some("hello streamed edit")
    });
    assert!(
        harness.remote_edit_count() >= 3,
        "the edit must arrive as multiple streamed ops, got {}",
        harness.remote_edit_count()
    );
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(!end.is_error && !end.cancelled);
    assert_eq!(messages.len(), 2, "assistant reply appended");
    assert_eq!(messages[1]["role"], "assistant");
    assert!(
        messages[1]["content"]
            .as_str()
            .unwrap()
            .contains("<<<EDIT demo.txt:0:6-0:11>>>"),
        "conversation history carries the fence verbatim"
    );
    assert!(!state.lock().unwrap().turn_active());
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// #1207: the stream is announced before its first edit (the region's
/// delete), so the host folds every edit of it, delete included, into
/// one undo step; announced after, the delete was a step of its own.
#[test]
fn a_stream_is_announced_before_its_first_edit() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let (base_url, server) = serve_sse_once(vec![
        "<<<EDIT demo.txt:0:6-0:11>>>\n",
        "streamed",
        "\n<<<END>>>\n",
    ]);
    let (turn_tx, _turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "fix demo.txt" })];
    local::stream_turn(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();
    harness.wait_until("the streamed edit to converge", |h| {
        h.doc().as_deref() == Some("hello streamed")
    });
    harness.wait_until("the stream to end", |h| {
        h.events
            .lock()
            .unwrap()
            .iter()
            .any(|e| matches!(e, CollabEvent::StreamState { active: false, .. }))
    });
    let events = harness.events.lock().unwrap();
    let first = |pred: &dyn Fn(&CollabEvent) -> bool| events.iter().position(pred);
    let announced = first(&|e| matches!(e, CollabEvent::StreamState { active: true, .. }))
        .expect("the stream is announced");
    let edited = first(&|e| matches!(e, CollabEvent::RemoteEdit { .. })).expect("the stream edits");
    let ended = first(&|e| matches!(e, CollabEvent::StreamState { active: false, .. }))
        .expect("the stream ends");
    assert!(
        announced < edited,
        "announced at {announced}, first edit at {edited}"
    );
    // Negative: the end still follows the last edit.
    let last_edit = events
        .iter()
        .rposition(|e| matches!(e, CollabEvent::RemoteEdit { .. }))
        .unwrap();
    assert!(
        last_edit < ended,
        "last edit at {last_edit}, ended at {ended}"
    );
    drop(events);
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// A dead endpoint fails the turn — naming the endpoint — instead of
/// wedging turn_active or killing the seat.
#[test]
fn stream_turn_names_a_dead_endpoint() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    // A bound-then-dropped port: connection refused.
    let dead = {
        let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        format!("http://{}", l.local_addr().unwrap())
    };
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn(
        &dead,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error);
    assert!(
        end.text.contains(&dead),
        "the failure names the endpoint: {}",
        end.text
    );
    assert!(!state.lock().unwrap().turn_active());
    assert!(
        messages.is_empty(),
        "the unanswered user message is popped so the next ask starts clean"
    );
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// A stream that dies before `message_stop` is a FAILED turn: the old
/// code reported success ("claude finished its turn") while the partial
/// edit had been reverted and nothing happened - and it appended the
/// partial text as an assistant message, corrupting the conversation.
#[test]
fn a_mid_stream_drop_reports_failure_and_balances_the_conversation() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let ev = json!({
        "type": "content_block_delta",
        "index": 0,
        "delta": { "type": "text_delta", "text": "half a rep" },
    });
    let (base_url, server) = serve_http_once(format!(
        "HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
             connection: close\r\n\r\nevent: content_block_delta\ndata: {ev}\n\n"
    ));
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(
        end.is_error,
        "an unclean stream end is a failure: {}",
        end.text
    );
    assert!(
        messages.is_empty(),
        "the conversation stays balanced for the next ask"
    );
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// The HTTP error body carries the fix ("model 'x' not found, try
/// pulling it first"); throwing it away left an opaque status code.
#[test]
fn an_http_error_body_reaches_the_surfaced_failure() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let (base_url, server) = serve_http_once(String::from(
        "HTTP/1.1 404 Not Found\r\ncontent-type: application/json\r\n\
             connection: close\r\n\r\n{\"error\":\"model 'qwn3' not found, try pulling it\"}",
    ));
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn(
        &base_url,
        "qwn3",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error);
    assert!(
        end.text.contains("not found, try pulling it"),
        "the body's own words must survive: {}",
        end.text
    );
    assert!(messages.is_empty());
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// `stop_reason: max_tokens` means the reply (and any fence in it) was
/// cut off; reporting success would pretend the half-edit was the turn.
#[test]
fn a_token_limit_truncation_is_a_failed_turn() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let delta = json!({
        "type": "content_block_delta",
        "index": 0,
        "delta": { "type": "text_delta", "text": "prose" },
    });
    let md = json!({
        "type": "message_delta",
        "delta": { "stop_reason": "max_tokens" },
    });
    let (base_url, server) = serve_http_once(format!(
        "HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
             connection: close\r\n\r\n\
             event: content_block_delta\ndata: {delta}\n\n\
             event: message_delta\ndata: {md}\n\n\
             event: message_stop\ndata: {{\"type\":\"message_stop\"}}\n\n"
    ));
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error, "truncation must not read as success");
    assert!(end.text.contains("truncated"), "{}", end.text);
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// The environment credential only ever travels to https or loopback
/// destinations; a cleartext remote hop gets the harmless placeholder.
#[test]
fn the_credential_never_rides_cleartext_to_a_remote_host() {
    use local::auth_for;
    assert_eq!(
        auth_for("http://box:8080", Some("secret")),
        (String::from("croft"), None),
    );
    assert_eq!(
        auth_for("http://localhost.evil.com:80", Some("secret")),
        (String::from("croft"), None),
    );
    let bearer = Some(String::from("Bearer t"));
    assert_eq!(
        auth_for("http://localhost:11434", Some("t")),
        (String::from("t"), bearer.clone()),
    );
    assert_eq!(
        auth_for("http://127.0.0.1:11434", Some("t")),
        (String::from("t"), bearer.clone()),
    );
    assert_eq!(
        auth_for("http://[::1]:11434", Some("t")),
        (String::from("t"), bearer.clone()),
    );
    assert_eq!(
        auth_for("https://gw.example.com", Some("t")),
        (String::from("t"), bearer),
    );
    assert_eq!(
        auth_for("http://localhost:11434", None),
        (String::from("croft"), None)
    );
    // Userinfo must not fool the gate: the destination here is the
    // remote host, not localhost.
    assert_eq!(
        auth_for("http://localhost:pass@evil.example", Some("secret")),
        (String::from("croft"), None),
    );
    assert_eq!(
        auth_for("http://127.0.0.2:11434", Some("t")),
        (String::from("t"), Some(String::from("Bearer t"))),
        "any 127.x address is loopback"
    );
}

/// The wall-clock ceiling is enforced per CHUNK: a stream of non-newline
/// bytes never completes a line, so a per-line check would have let a
/// trickling endpoint hold the seat busy indefinitely.
#[test]
fn a_trickling_stream_is_cut_at_the_deadline() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let base_url = format!("http://{}", listener.local_addr().unwrap());
    let stop_trickle = Arc::new(AtomicBool::new(false));
    let server = {
        let stop_trickle = Arc::clone(&stop_trickle);
        std::thread::spawn(move || {
            let (mut sock, _) = listener.accept().unwrap();
            let mut req = [0u8; 4096];
            let _ = sock.read(&mut req);
            let _ = sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n");
            // Non-newline bytes, forever (until the test releases us).
            while !stop_trickle.load(Ordering::Relaxed) {
                if sock.write_all(b".").is_err() {
                    break;
                }
                std::thread::sleep(Duration::from_millis(20));
            }
        })
    };
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    let started = Instant::now();
    local::stream_turn_until(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
        Instant::now() + Duration::from_millis(300),
    );
    let elapsed = started.elapsed();
    stop_trickle.store(true, Ordering::Relaxed);
    let _ = server.join();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error, "a deadline cut is a failure: {}", end.text);
    assert!(
        elapsed < Duration::from_secs(3),
        "the ceiling must interrupt the trickle promptly, took {elapsed:?}"
    );
    assert!(messages.is_empty(), "the conversation stays balanced");
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// The deadline used to be polled only BETWEEN reads: a peer that sent
/// headers and then held the body open without a single byte parked the
/// loop inside the blocking `read` (up to the 300s socket timeout) past
/// the ceiling. Each wait is bounded by the remaining turn time instead.
#[test]
fn a_silent_stream_body_is_cut_at_the_deadline() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let base_url = format!("http://{}", listener.local_addr().unwrap());
    // The thread ends on its own after the silence window; dropping the
    // handle detaches it.
    std::thread::spawn(move || {
        let (mut sock, _) = listener.accept().unwrap();
        let mut req = [0u8; 4096];
        let _ = sock.read(&mut req);
        let _ = sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n");
        // Not one body byte: hold the connection open in silence, long
        // enough that only the deadline can explain a prompt return.
        std::thread::sleep(Duration::from_secs(4));
    });
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    let started = Instant::now();
    local::stream_turn_until(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
        Instant::now() + Duration::from_millis(300),
    );
    let elapsed = started.elapsed();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error, "a deadline cut is a failure: {}", end.text);
    assert!(
        elapsed < Duration::from_secs(3),
        "the ceiling must interrupt a silent body promptly, took {elapsed:?}"
    );
    assert!(messages.is_empty(), "the conversation stays balanced");
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// A deadline cut must also RELEASE the connection. Returning on time
/// while a detached reader stayed blocked in `read` kept the socket and
/// a thread alive for up to the 300s socket timeout per failed turn, so
/// repeated failures accumulated both. The server observing EOF right
/// after the cut is the proof the transport let go.
#[test]
fn a_deadline_cut_releases_the_connection() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let base_url = format!("http://{}", listener.local_addr().unwrap());
    let (eof_tx, eof_rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let (mut sock, _) = listener.accept().unwrap();
        let mut req = [0u8; 4096];
        let _ = sock.read(&mut req);
        let _ = sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n");
        // Silent body: block until the CLIENT hangs up. EOF or a reset
        // is the release; draining any leftover request bytes first.
        let mut probe = [0u8; 64];
        loop {
            match sock.read(&mut probe) {
                Ok(0) | Err(_) => break,
                Ok(_) => {}
            }
        }
        let _ = eof_tx.send(());
    });
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn_until(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
        Instant::now() + Duration::from_millis(300),
    );
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error, "a deadline cut is a failure: {}", end.text);
    eof_rx
        .recv_timeout(Duration::from_secs(2))
        .expect("the deadline cut must close the connection, not park it until the socket timeout");
    assert!(messages.is_empty(), "the conversation stays balanced");
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// Malformed SSE data mid-stream silently dropped a chunk of the reply
/// and still reported success; a broken endpoint must fail the turn.
#[test]
fn malformed_sse_data_fails_the_turn() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let (base_url, server) = serve_http_once(String::from(
        "HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
             connection: close\r\n\r\ndata: {not json\n\n\
             event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
    ));
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error);
    assert!(end.text.contains("malformed"), "{}", end.text);
    assert!(messages.is_empty());
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// A 3xx from the endpoint must NOT be followed: ureq replays the
/// x-api-key header to the redirect target, so a loopback endpoint
/// answering 302 could bounce the credential to an arbitrary host. The
/// redirect surfaces as a failed turn naming the status.
#[test]
fn a_redirect_is_refused_not_followed() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    let (base_url, server) = serve_http_once(String::from(
        "HTTP/1.1 302 Found\r\nlocation: http://127.0.0.1:1/v1/messages\r\n\
             connection: close\r\n\r\n",
    ));
    let (turn_tx, turn_rx) = std::sync::mpsc::channel();
    let mut messages = vec![json!({ "role": "user", "content": "hi" })];
    state.lock().unwrap().turn_active = true;
    local::stream_turn(
        &base_url,
        "test-model",
        PAIR_SYSTEM_PROMPT,
        &mut messages,
        &state,
        &turn_tx,
    );
    server.join().unwrap();
    let end = turn_rx.try_recv().expect("turn ended");
    assert!(end.is_error);
    assert!(
        end.text.contains("302"),
        "the redirect is refused with its status, not followed: {}",
        end.text
    );
    assert!(messages.is_empty());
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// The childless local seat end to end: seat_local connects the guest
/// seat, the queued turn streams the stub endpoint's fenced edit into
/// the owner's replica, the turn ends cleanly, and shutdown returns
/// promptly (no child, no grace-kill).
#[test]
fn seat_local_drives_a_turn_end_to_end() {
    let harness = OwnerHarness::start("hello world");
    let (base_url, server) = serve_sse_once(vec![
        "Let me fix that.\n",
        "<<<EDIT demo.txt:0:6-0:11>",
        ">>\n",
        "streamed",
        " edit",
        "\n<<<END>>>\nDone.\n",
    ]);
    let pilot = seat_local(&harness.socket, "pilot", &base_url, "test-model", None).unwrap();
    assert!(!pilot.reader_finished(), "a local seat never loses a child");

    send_turn(&pilot.state, &pilot.sink, "fix demo.txt").unwrap();
    let end = pilot
        .turn_rx
        .recv_timeout(Duration::from_secs(60))
        .expect("turn ends");
    assert!(!end.is_error && !end.cancelled, "{}", end.text);
    server.join().unwrap();

    harness.wait_until("the streamed edit to converge", |h| {
        h.doc().as_deref() == Some("hello streamed edit")
    });
    harness.wait_until("stream-state inactive", |h| {
        h.stream_states().contains(&("pilot".to_string(), false))
    });

    let start = Instant::now();
    pilot.shutdown();
    assert!(
        start.elapsed() < Duration::from_secs(2),
        "local shutdown must not block on a grace-kill"
    );
}

/// PairHost::spawn dispatches on the provider: a Local config refuses to
/// seat without a model (there is no CLI default to fall back to), and
/// with one it seats childless and drives a whole-line ask turn from the
/// endpoint into the owner's replica.
#[test]
fn pair_host_spawns_local_backend_without_a_child() {
    let harness = OwnerHarness::start("hello world");
    let no_model = PairConfig {
        socket: harness.socket.clone(),
        workspace: harness._dir.path().to_path_buf(),
        name: String::from("claude"),
        model: None,
        task: None,
        provider: Provider::Local {
            base_url: String::from("http://127.0.0.1:9"),
        },
    };
    let err = match crate::pair_host::PairHost::spawn(no_model) {
        Err(e) => e,
        Ok(_) => panic!("a local provider without a model must refuse to seat"),
    };
    assert!(
        err.to_string().contains("--model"),
        "the refusal must hint at --model: {err}"
    );

    let (base_url, server) = serve_sse_once(vec![
        "<<<EDIT demo.txt:0-0>",
        ">>\nhello streamed edit\n",
        "<<<END>>>\n",
    ]);
    let mut host = crate::pair_host::PairHost::spawn(PairConfig {
        socket: harness.socket.clone(),
        workspace: harness._dir.path().to_path_buf(),
        name: String::from("claude"),
        model: Some(String::from("test-model")),
        task: None,
        provider: Provider::Local { base_url },
    })
    .unwrap();
    assert!(!host.is_busy());
    host.send_ask_turn("demo.txt", (0, 0), "", "rewrite line 0", "hello world")
        .unwrap();
    server.join().unwrap();
    harness.wait_until("the whole-line edit to converge", |h| {
        h.doc().as_deref() == Some("hello streamed edit")
    });
    harness.wait_until("the turn to end", |h| {
        let _ = h; // the turn end arrives on the host's own channel
        host.poll().iter().any(|e| {
            matches!(
                e,
                crate::pair_host::PairEvent::TurnDone {
                    cancelled: false,
                    failed: None
                }
            )
        })
    });
}

/// The badge title names who is actually typing: bare caret name on the
/// claude backend, "name (model)" on a local one.
#[test]
fn local_navigator_badge_names_the_model() {
    let harness = OwnerHarness::start("hello world");
    let host = crate::pair_host::PairHost::spawn(PairConfig {
        socket: harness.socket.clone(),
        workspace: harness._dir.path().to_path_buf(),
        name: String::from("claude"),
        model: Some(String::from("test-model")),
        task: None,
        provider: Provider::Local {
            base_url: String::from("http://127.0.0.1:9"),
        },
    })
    .unwrap();
    assert_eq!(host.title(), "claude (test-model)");

    let mut cmd = Command::new("cat");
    cmd.stdin(Stdio::piped());
    let host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
    assert_eq!(host.title(), "navigator");
}

/// Spawn the pilot against the scripted claude on a background thread
/// (it blocks until the turn ends and its input hits EOF).
fn spawn_pilot(
    socket: &Path,
    script: &Path,
    log: &Path,
    mode: &str,
) -> std::thread::JoinHandle<Result<()>> {
    let mut cmd = Command::new("python3");
    cmd.arg(script).arg(log).arg(mode);
    let socket = socket.to_path_buf();
    std::thread::spawn(move || {
        let pilot = seat_pilot(&socket, "pilot", cmd, None)?;
        run_pilot(
            pilot,
            "pilot",
            Some("fix demo.txt".into()),
            &mut std::io::Cursor::new(Vec::new()),
        )
    })
}

/// End to end: the fake claude's fenced edit streams through the pilot
/// into the owner's replica as MULTIPLE ops (token streaming, not one
/// bulk insert), bracketed by StreamState active/inactive.
#[test]
fn pilot_streams_a_fenced_edit_into_the_owner() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let pilot = spawn_pilot(&harness.socket, &script, &log, "stream");

    harness.wait_until("the streamed edit to converge", |h| {
        h.doc().as_deref() == Some("hello streamed edit")
    });
    harness.wait_until("stream-state inactive", |h| {
        h.stream_states().contains(&("pilot".to_string(), false))
    });
    assert!(
        harness
            .stream_states()
            .contains(&("pilot".to_string(), true)),
        "owner saw the stream start"
    );
    assert!(
        harness.remote_edit_count() >= 3,
        "the edit must arrive as multiple streamed ops, got {}",
        harness.remote_edit_count()
    );
    pilot.join().unwrap().expect("pilot exits cleanly");
}

/// A claude child that never exits on stdin EOF (the real CLI's MCP
/// teardown can linger) must not hang the pilot's exit: cleanup kills
/// it after a short grace instead of joining the reader forever.
#[test]
fn pilot_exit_does_not_hang_on_a_lingering_claude() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let pilot = spawn_pilot(&harness.socket, &script, &log, "linger");
    harness.wait_until("the streamed edit to converge", |h| {
        h.doc().as_deref() == Some("hello streamed edit")
    });
    // The pilot's REPL input is already at EOF; once the turn ends it
    // must return promptly despite the child sleeping for 60s — half
    // that sleep keeps the discriminant while absorbing suite load.
    let deadline = Instant::now() + Duration::from_secs(30);
    while !pilot.is_finished() {
        assert!(
            Instant::now() < deadline,
            "pilot exit hung on the lingering claude child"
        );
        std::thread::sleep(Duration::from_millis(50));
    }
    pilot.join().unwrap().expect("pilot exits cleanly");
}

/// Cancel mid-stream: the owner broadcasts StreamCancel while the fake
/// claude is blocked mid-body; the pilot reverts the streamed text,
/// writes a control_request interrupt to claude's stdin, and drops the
/// post-cancel deltas. The conversation still ends its turn cleanly.
#[test]
fn pilot_cancel_reverts_and_interrupts() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let pilot = spawn_pilot(&harness.socket, &script, &log, "cancel");

    // Wait until part of the body landed (stream mid-flight).
    harness.wait_until("the partial body to converge", |h| {
        h.doc().is_some_and(|d| d.contains("streamed"))
    });
    harness.owner.lock().unwrap().send_stream_cancel();

    // The pilot reverts and the turn still completes.
    harness.wait_until("the revert to converge", |h| {
        h.doc().as_deref() == Some("hello world")
    });
    pilot.join().unwrap().expect("pilot exits cleanly");

    assert_eq!(
        harness.doc().as_deref(),
        Some("hello world"),
        "post-cancel deltas must never land"
    );
    harness.wait_until("stream-state inactive", |h| {
        h.stream_states().contains(&("pilot".to_string(), false))
    });
    let log_text = std::fs::read_to_string(&log).unwrap();
    assert!(
        log_text.contains("control_request") && log_text.contains("interrupt"),
        "the interrupt must land on claude's stdin: {log_text:?}"
    );
}

/// Feed `input` to a fresh machine in `chunk`-sized pieces plus finish,
/// collecting every event.
fn run_chunks(input: &str, chunk: usize) -> Vec<FenceEvent> {
    let mut m = FenceMachine::new();
    let mut events = Vec::new();
    let chars: Vec<char> = input.chars().collect();
    for piece in chars.chunks(chunk) {
        let s: String = piece.iter().collect();
        events.extend(m.push(&s));
    }
    events.extend(m.finish());
    events
}

/// Concatenated body text of every EditBody event.
fn body_of(events: &[FenceEvent]) -> String {
    events
        .iter()
        .filter_map(|e| match e {
            FenceEvent::EditBody(s) => Some(s.as_str()),
            _ => None,
        })
        .collect()
}

/// Concatenated commentary of every Commentary event.
fn commentary_of(events: &[FenceEvent]) -> String {
    events
        .iter()
        .filter_map(|e| match e {
            FenceEvent::Commentary(s) => Some(s.as_str()),
            _ => None,
        })
        .collect()
}

/// Concatenated body of every NoteBody event.
fn note_body_of(events: &[FenceEvent]) -> String {
    events
        .iter()
        .filter_map(|e| match e {
            FenceEvent::NoteBody(s) => Some(s.as_str()),
            _ => None,
        })
        .collect()
}

/// A NOTE fence parses across arbitrary delta splits: the row binds
/// rightmost (file names may carry ':'), the body accumulates, and the
/// fence closes on END without leaking markers into commentary.
#[test]
fn note_fence_parses_across_delta_splits() {
    let input = "Look at this.\n\
                     <<<NOTE src/f.rs:12>>>\n\
                     the caller in cli.rs:88\nstill expects Config\n\
                     <<<END>>>\n\
                     trailing\n";
    for chunk in [1, 3, 7, input.len()] {
        let events = run_chunks(input, chunk);
        let start = events
            .iter()
            .find_map(|e| match e {
                FenceEvent::NoteStart { file, row } => Some((file.clone(), *row)),
                _ => None,
            })
            .unwrap_or_else(|| panic!("no NoteStart at chunk {chunk}"));
        assert_eq!(start, ("src/f.rs".to_string(), 12), "chunk {chunk}");
        assert_eq!(
            note_body_of(&events),
            "the caller in cli.rs:88\nstill expects Config",
            "chunk {chunk}"
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, FenceEvent::NoteEnd))
                .count(),
            1,
            "chunk {chunk}"
        );
        let commentary = commentary_of(&events);
        assert!(commentary.contains("Look at this."), "chunk {chunk}");
        assert!(commentary.contains("trailing"), "chunk {chunk}");
        assert!(!commentary.contains("<<<NOTE"), "chunk {chunk}");
    }
}

/// Malformed NOTE headers degrade to commentary, exactly like EDIT ones.
#[test]
fn malformed_note_headers_become_commentary() {
    for bad in [
        "<<<NOTE src/f.rs>>>\nbody\n<<<END>>>\n",
        "<<<NOTE src/f.rs:x>>>\nbody\n<<<END>>>\n",
        "<<<NOTE :3>>>\nbody\n<<<END>>>\n",
        "<<<NOTE>>>\nbody\n<<<END>>>\n",
    ] {
        let events = run_chunks(bad, 5);
        assert!(
            !events
                .iter()
                .any(|e| matches!(e, FenceEvent::NoteStart { .. })),
            "{bad:?} must not start a note"
        );
    }
}

/// A turn ending mid-note aborts it so nothing anchors.
#[test]
fn unterminated_note_aborts_at_finish() {
    let mut m = FenceMachine::new();
    let mut events = m.push("<<<NOTE f.rs:3>>>\nhalf a thought");
    events.extend(m.finish());
    assert!(events.iter().any(|e| matches!(e, FenceEvent::NoteAbort)));
    assert!(!events.iter().any(|e| matches!(e, FenceEvent::NoteEnd)));
}

/// Interop guard: EDIT and NOTE fences coexist in one turn and neither
/// disturbs the other's events (the 0.1.634 edit protocol is unchanged).
#[test]
fn edit_and_note_fences_coexist_in_one_turn() {
    let input = "<<<EDIT a.rs:0:0-0:0>>>\nnew\n<<<END>>>\n\
                     <<<NOTE b.rs:4>>>\nremark\n<<<END>>>\n";
    let events = run_chunks(input, 2);
    assert_eq!(body_of(&events), "new");
    assert_eq!(note_body_of(&events), "remark");
    assert_eq!(
        events
            .iter()
            .filter(|e| matches!(e, FenceEvent::EditEnd))
            .count(),
        1
    );
    assert_eq!(
        events
            .iter()
            .filter(|e| matches!(e, FenceEvent::NoteEnd))
            .count(),
        1
    );
}

/// The core streaming property: a full fenced edit parses identically no
/// matter how the deltas split it, down to one char at a time, and the
/// body arrives across multiple EditBody events (streaming), not one.
#[test]
fn fence_parses_identically_across_arbitrary_delta_splits() {
    let input = "I will fix the loop.\n\
                     <<<EDIT src/f.rs:3:0-5:10>>>\n\
                     for x in xs {\n    go(x);\n}\n\
                     <<<END>>>\n\
                     Done.\n";
    for chunk in [1, 2, 3, 7, input.len()] {
        let events = run_chunks(input, chunk);
        let start = events
            .iter()
            .find_map(|e| match e {
                FenceEvent::EditStart { file, start, end } => Some((file.clone(), *start, *end)),
                _ => None,
            })
            .unwrap_or_else(|| panic!("no EditStart at chunk {chunk}"));
        assert_eq!(
            start,
            ("src/f.rs".to_string(), (3, 0), (5, 10)),
            "chunk {chunk}"
        );
        assert_eq!(
            body_of(&events),
            "for x in xs {\n    go(x);\n}",
            "chunk {chunk}"
        );
        assert_eq!(
            events
                .iter()
                .filter(|e| matches!(e, FenceEvent::EditEnd))
                .count(),
            1,
            "chunk {chunk}"
        );
        let commentary = commentary_of(&events);
        assert!(
            commentary.contains("I will fix the loop."),
            "chunk {chunk}: {commentary:?}"
        );
        assert!(commentary.contains("Done."), "chunk {chunk}");
        assert!(
            !commentary.contains("<<<EDIT"),
            "header must not leak into commentary at chunk {chunk}"
        );
    }
    // Char-at-a-time must still stream the body incrementally.
    let events = run_chunks(input, 1);
    let bodies = events
        .iter()
        .filter(|e| matches!(e, FenceEvent::EditBody(_)))
        .count();
    assert!(bodies > 1, "body must stream, got {bodies} event(s)");
}

/// The whole-line header form `<file>:SR-ER` (two integers) opens an
/// edit covering those entire rows, inclusive: start (SR,0) and an end
/// column that clamps to the end of row ER.
#[test]
fn parse_header_accepts_whole_line_range() {
    let event = parse_header("<<<EDIT demo.txt:3-5>>>").expect("whole-line header parses");
    let FenceEvent::EditStart { file, start, end } = event else {
        panic!("not an EditStart: {event:?}");
    };
    assert_eq!(file, "demo.txt");
    assert_eq!(start, (3, 0));
    assert_eq!(end, (5, usize::MAX));
}

/// The spike's coordinate slip, fixed: a whole-line fence on the exact
/// spike buffer replaces the row entirely — no `+ name` leftover — using
/// the same range_bytes clamping the pilot's open_region applies.
#[test]
fn whole_line_edit_replaces_entire_rows() {
    let buffer = "def greet(name):\n    return \"hi \" + name\n";
    let input = "<<<EDIT greet.py:1-1>>>\n    return f\"Hello, {name}!\"\n<<<END>>>\n";
    let events = run_chunks(input, 3);
    let (start, end) = events
        .iter()
        .find_map(|e| match e {
            FenceEvent::EditStart { start, end, .. } => Some((*start, *end)),
            _ => None,
        })
        .expect("whole-line fence opens an edit");
    let lines: Vec<String> = buffer.split('\n').map(String::from).collect();
    let (s, e) = range_bytes(&lines, start, end);
    let result = format!("{}{}{}", &buffer[..s], body_of(&events), &buffer[e..]);
    assert_eq!(result, "def greet(name):\n    return f\"Hello, {name}!\"\n");
}

/// A truncated four-int header (`file:SR:SC-ER`, missing its end column)
/// must NOT fall back to the whole-line form: its "file" would be the
/// bogus `file:SR`. It stays commentary.
#[test]
fn truncated_four_int_header_stays_commentary() {
    assert!(parse_header("<<<EDIT src/f.rs:3:0-5>>>").is_none());
}

/// A header that fails to parse is commentary, never an edit.
#[test]
fn malformed_headers_become_commentary() {
    for bad in [
        "<<<EDIT src/f.rs:3:0-5>>>\nbody\n<<<END>>>\n",
        "<<<EDIT src/f.rs>>>\nbody\n<<<END>>>\n",
        "<<<EDIT src/f.rs:a:0-5:10>>>\nbody\n<<<END>>>\n",
        "<<<EDIT>>>\nbody\n<<<END>>>\n",
    ] {
        let events = run_chunks(bad, 5);
        assert!(
            !events
                .iter()
                .any(|e| matches!(e, FenceEvent::EditStart { .. })),
            "{bad:?} must not start an edit"
        );
        assert!(
            commentary_of(&events).contains("body"),
            "{bad:?}: the un-fenced body is commentary"
        );
    }
}

/// Exactly one trailing newline is stripped: the one that separates the
/// body from the END marker. A body that ends in a blank line keeps it.
#[test]
fn end_marker_strips_exactly_one_trailing_newline() {
    let input = "<<<EDIT f:0:0-0:0>>>\nabc\n\n<<<END>>>\n";
    for chunk in [1, 4, input.len()] {
        assert_eq!(body_of(&run_chunks(input, chunk)), "abc\n", "chunk {chunk}");
    }
}

/// A body line that merely contains the END marker text with a suffix is
/// body, not a terminator.
#[test]
fn end_marker_with_trailing_chars_is_body() {
    let input = "<<<EDIT f:0:0-0:0>>>\nkeep <<<END>>>ish\n<<<END>>>\n";
    for chunk in [1, 3, input.len()] {
        assert_eq!(
            body_of(&run_chunks(input, chunk)),
            "keep <<<END>>>ish",
            "chunk {chunk}"
        );
    }
}

/// Two fences in one turn each produce their own start/body/end, with
/// the commentary between them intact.
#[test]
fn multiple_fences_in_one_turn() {
    let input = "<<<EDIT a:0:0-0:0>>>\nfirst\n<<<END>>>\n\
                     between\n\
                     <<<EDIT b:1:2-3:4>>>\nsecond\n<<<END>>>\n";
    let events = run_chunks(input, 2);
    let starts: Vec<String> = events
        .iter()
        .filter_map(|e| match e {
            FenceEvent::EditStart { file, .. } => Some(file.clone()),
            _ => None,
        })
        .collect();
    assert_eq!(starts, ["a", "b"]);
    assert_eq!(body_of(&events), "firstsecond");
    assert_eq!(
        events
            .iter()
            .filter(|e| matches!(e, FenceEvent::EditEnd))
            .count(),
        2
    );
    assert!(commentary_of(&events).contains("between"));
}

/// The model stopping mid-fence (turn ends before <<<END>>>) aborts the
/// edit so the pilot can revert what already streamed in.
#[test]
fn unterminated_fence_aborts_at_finish() {
    let mut m = FenceMachine::new();
    let mut events = m.push("<<<EDIT f:0:0-0:0>>>\npartial body");
    events.extend(m.finish());
    assert!(
        events.iter().any(|e| matches!(e, FenceEvent::EditAbort)),
        "{events:?}"
    );
    // The machine is reusable for the next turn afterwards.
    let events = {
        let mut e = m.push("plain text\n");
        e.extend(m.finish());
        e
    };
    assert!(commentary_of(&events).contains("plain text"));
}

/// An END marker arriving exactly at end-of-turn (no trailing newline)
/// still closes the fence.
#[test]
fn end_marker_at_end_of_turn_closes_the_fence() {
    let mut m = FenceMachine::new();
    let mut events = m.push("<<<EDIT f:0:0-0:0>>>\nbody\n<<<END>>>");
    events.extend(m.finish());
    assert!(events.iter().any(|e| matches!(e, FenceEvent::EditEnd)));
    assert_eq!(body_of(&events), "body");
}

/// File names with colons and dashes still parse (coords bind rightmost).
#[test]
fn header_files_with_colons_and_dashes_parse() {
    let input = "<<<EDIT a-b:c.rs:10:2-11:0>>>\nx\n<<<END>>>\n";
    let events = run_chunks(input, input.len());
    match events
        .iter()
        .find(|e| matches!(e, FenceEvent::EditStart { .. }))
    {
        Some(FenceEvent::EditStart { file, start, end }) => {
            assert_eq!(file, "a-b:c.rs");
            assert_eq!((*start, *end), ((10, 2), (11, 0)));
        }
        other => panic!("expected EditStart, got {other:?}"),
    }
}

/// Anchor-shift math: spans entirely before an offset shift it by the
/// span's size delta; spans straddling it clamp to the span's new end;
/// spans after leave it alone.
#[test]
fn shift_offset_handles_before_straddle_and_after() {
    // Insert of 3 bytes at 2, entirely before offset 10.
    let ins = ResolvedSpan {
        at: 2,
        deleted: 0,
        inserted: "abc".into(),
    };
    assert_eq!(shift_offset(10, &ins), 13);
    // Delete of 4 bytes ending exactly at the offset: still "before".
    let del = ResolvedSpan {
        at: 6,
        deleted: 4,
        inserted: String::new(),
    };
    assert_eq!(shift_offset(10, &del), 6);
    // Straddle: delete [8, 14) around offset 10 with 1 byte inserted.
    let straddle = ResolvedSpan {
        at: 8,
        deleted: 6,
        inserted: "Z".into(),
    };
    assert_eq!(shift_offset(10, &straddle), 9);
    // Entirely after: change at the offset itself or beyond.
    let after = ResolvedSpan {
        at: 10,
        deleted: 3,
        inserted: "wxyz".into(),
    };
    assert_eq!(shift_offset(10, &after), 10);
    let insert_at = ResolvedSpan {
        at: 10,
        deleted: 0,
        inserted: "Q".into(),
    };
    // An insert exactly at the offset counts as before (pushes it right).
    assert_eq!(shift_offset(10, &insert_at), 11);
}

/// A sequence of spans replays through both region offsets like the
/// pilot's pump does, keeping start <= anchor.
#[test]
fn shift_offset_sequences_track_a_region() {
    let mut start = 20usize;
    let mut anchor = 30usize;
    let spans = [
        // 5 bytes inserted at 0: both shift right.
        ResolvedSpan {
            at: 0,
            deleted: 0,
            inserted: "aaaaa".into(),
        },
        // Delete [40, 45): after both, no change.
        ResolvedSpan {
            at: 40,
            deleted: 5,
            inserted: String::new(),
        },
        // Delete [23, 28): straddles start (clamps it to the span's new
        // end, 23) and sits entirely before anchor (shifts it left 5).
        ResolvedSpan {
            at: 23,
            deleted: 5,
            inserted: String::new(),
        },
    ];
    for span in &spans {
        start = shift_offset(start, span);
        anchor = shift_offset(anchor, span);
    }
    assert_eq!(start, 23);
    assert_eq!(anchor, 30);
    assert!(start <= anchor);
}

/// The char-coordinate bridge: fence coords convert to byte offsets via
/// collab::byte_offset, including multibyte lines.
#[test]
fn fence_range_converts_to_byte_offsets() {
    let lines: Vec<String> = ["let s = \"héllo\";", "next"]
        .iter()
        .map(|s| s.to_string())
        .collect();
    let (start, end) = range_bytes(&lines, (0, 10), (1, 2));
    // 'é' is 2 bytes: char col 10 sits after the quote + h + é.
    assert_eq!(start, crate::collab::byte_offset(&lines, 0, 10));
    assert_eq!(end, crate::collab::byte_offset(&lines, 1, 2));
    assert!(start < end);
}

/// The in-process host: a NOTE fence from the scripted claude surfaces
/// as a PairEvent::NoteAdded, the snapshot converts its offset back to a
/// row against the live replica, and the turn's end is observable.
#[test]
fn pair_host_emits_note_added_and_snapshots_rows() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world\nsecond line");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("notes");
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
    host.send_task("@demo.txt look this over").unwrap();

    // The reader queues NoteAdded (events channel) before TurnEnd (turn
    // channel), but poll() drains events FIRST: the poll that surfaces
    // TurnDone may have drained events before the note was queued, so
    // wait until BOTH landed; the deadline bounds a real regression.
    let deadline = Instant::now() + Duration::from_secs(60);
    let mut events: Vec<crate::pair_host::PairEvent> = Vec::new();
    while !(events
        .iter()
        .any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
        && events
            .iter()
            .any(|e| matches!(e, crate::pair_host::PairEvent::NoteAdded { .. })))
    {
        assert!(
            Instant::now() < deadline,
            "no TurnDone + NoteAdded; saw {events:?}"
        );
        events.extend(host.poll());
        std::thread::sleep(Duration::from_millis(10));
    }
    let note = events
        .iter()
        .find_map(|e| match e {
            crate::pair_host::PairEvent::NoteAdded {
                file, row, body, ..
            } => Some((file.clone(), *row, body.clone())),
            _ => None,
        })
        .unwrap_or_else(|| panic!("no NoteAdded; saw {events:?}"));
    assert_eq!(note.0, "demo.txt");
    assert_eq!(note.1, 1, "anchored to the fenced row");
    assert!(note.2.contains("second line could be tighter"));
    let expected = vec![(1, 1, "second line could be tighter".to_string())];
    assert_eq!(
        settle(&expected, || host.notes_snapshot("demo.txt")),
        expected,
        "(id, row, body): the first note of the seat gets id 1"
    );
    drop(host); // must not hang: the shutdown grace-kill path
}

/// A turn cannot be sent while one is still streaming: the host rejects
/// the overlapping turn (before begin_turn runs) so the shared
/// comment-only flag is never clobbered mid-stream. Without the guard, a
/// yield fired during an ask would flip comment_only and the yield's
/// edits could reach the buffer.
#[test]
fn a_turn_is_rejected_while_another_is_streaming() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("line zero\nline one");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");
    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("hang"); // reads one turn, never answers
    let host = crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();

    // First turn: an ask (edits allowed). turn_active is set synchronously
    // when the turn is written, so the host is busy immediately.
    host.send_ask_turn("demo.txt", (0, 0), "", "do a thing", "line zero\nline one")
        .unwrap();
    assert!(host.is_busy(), "the ask turn is streaming");

    // A yield fired now must be rejected, and must NOT have flipped the
    // in-flight turn into comment-only (begin_turn never ran).
    let err = host
        .send_yield_turn("demo.txt", "line zero\nline one")
        .unwrap_err();
    assert!(
        err.to_string().contains("mid-turn"),
        "expected a mid-turn rejection, got: {err}"
    );
    assert!(host.is_busy(), "the original ask turn is untouched");
    drop(host);
}

/// Comment boxes need stable identity: the host can inject a note
/// (commentary landing as a box), append the driver's reply to it, and
/// remove exactly one note by id (the box's Ignore button). Ids are
/// distinct and survive removal of a sibling.
#[test]
fn host_adds_appends_and_removes_notes_by_id() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world\nsecond line");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");
    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("notes");
    let host = crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();

    let id_a = host
        .add_note("demo.txt", 0, "turn summary")
        .expect("demo.txt is served by the owner");
    let id_b = host.add_note("demo.txt", 1, "second thought").unwrap();
    assert_ne!(id_a, id_b, "every note gets its own id");

    // Each read polls to the resolved value: `notes_snapshot` serves its
    // cache on a contended try_lock, which a loaded runner hits (see
    // `settle`).
    let expected = vec![
        (id_a, 0, "turn summary".to_string()),
        (id_b, 1, "second thought".to_string()),
    ];
    assert_eq!(
        settle(&expected, || host.notes_snapshot("demo.txt")),
        expected
    );

    host.append_to_note(id_a, "you: tell me more");
    let expected = vec![
        (id_a, 0, "turn summary\nyou: tell me more".to_string()),
        (id_b, 1, "second thought".to_string()),
    ];
    assert_eq!(
        settle(&expected, || host.notes_snapshot("demo.txt")),
        expected
    );

    host.remove_note(id_a);
    let expected = vec![(id_b, 1, "second thought".to_string())];
    assert_eq!(
        settle(&expected, || host.notes_snapshot("demo.txt")),
        expected,
        "only the ignored note is gone"
    );
    drop(host);
}

/// Boxes persist until the driver ignores them: a new turn on the same
/// file must NOT wipe its existing notes (the old supersession rule is
/// gone — under the box model it would close every open conversation on
/// each ask or reply).
#[test]
fn notes_survive_a_new_turn_on_the_same_file() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world\nsecond line");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");
    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("notes");
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
    host.send_task("@demo.txt look this over").unwrap();

    let deadline = Instant::now() + Duration::from_secs(60);
    while !host
        .poll()
        .iter()
        .any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
    {
        assert!(Instant::now() < deadline, "no TurnDone");
        std::thread::sleep(Duration::from_millis(10));
    }
    // The reader applies NoteEnd before it sends the TurnEnd, so note
    // state is final here; the snapshot accessor is not exact under
    // contention (see `settle`), so poll it to the resolved value.
    let deadline = Instant::now() + Duration::from_secs(60);
    let before = loop {
        let snap = host.notes_snapshot("demo.txt");
        if snap.len() == 1 {
            break snap;
        }
        assert!(
            Instant::now() < deadline,
            "the scripted note landed: {snap:?}"
        );
        std::thread::sleep(Duration::from_millis(10));
    };

    // A follow-up turn targeting the same file: begin_turn runs
    // synchronously inside send_yield_turn.
    host.send_yield_turn("demo.txt", "hello world\nsecond line")
        .unwrap();
    assert_eq!(
        settle(&before, || host.notes_snapshot("demo.txt")),
        before,
        "open boxes survive the next turn"
    );
    drop(host);
}

/// The reply composer: the driver answered a note's box. The turn names
/// the note (file, 0-based row, its body), carries the reply, lets the
/// model edit when the reply asks for a change (#1965), and grounds the
/// model with the numbered buffer.
#[test]
fn compose_reply_turn_carries_note_context_and_buffer() {
    let text = compose_reply_turn(
        "demo.txt",
        3,
        "this loop allocates per iteration",
        "is that actually hot?",
        "a\nb",
    );
    assert!(
        text.contains("replied to your note on demo.txt at line 3 (0-based"),
        "names the note's anchor: {text}"
    );
    assert!(
        text.contains("this loop allocates per iteration"),
        "quotes the note body: {text}"
    );
    assert!(
        text.contains("is that actually hot?"),
        "carries the reply: {text}"
    );
    assert!(
        text.contains("If their reply asks you to make a change, make it with EDIT fences"),
        "a reply asking for the change may edit: {text}"
    );
    assert!(
        !text.contains("COMMENT-ONLY") && !text.contains("will be discarded"),
        "a reply is not a comment-only turn: {text}"
    );
    assert!(
        text.contains("0|a\n1|b"),
        "grounds the model with the numbered buffer: {text}"
    );
}

/// Dropping the host (unseat / toggle-off) must not block the UI thread
/// on the 2s grace-kill: teardown runs on a detached thread. The claude
/// CLI lingers on stdin EOF, so the synchronous path would freeze the TUI
/// for the full grace period on every deactivation.
#[test]
fn dropping_the_host_does_not_block_the_ui_thread() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("line zero\nline one");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");
    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("linger"); // streams a turn, then sleeps 60s
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
    // Drive one turn so the child reaches its sleep (ignoring stdin EOF).
    host.send_ask_turn("demo.txt", (0, 0), "", "do a thing", "line zero\nline one")
        .unwrap();
    let deadline = Instant::now() + Duration::from_secs(60);
    while !host
        .poll()
        .iter()
        .any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
    {
        assert!(Instant::now() < deadline, "turn never finished");
        std::thread::sleep(Duration::from_millis(10));
    }
    let t = Instant::now();
    drop(host);
    assert!(
        t.elapsed() < Duration::from_secs(1),
        "Drop blocked on the grace-kill for {:?}; teardown must be detached",
        t.elapsed()
    );
}

/// claude resolves by bare name when on PATH, but a stripped GUI PATH
/// falls back to probing install dirs and pinning the absolute path so
/// the navigator can still seat on Croft.app launches.
#[test]
fn claude_resolves_to_an_absolute_path_off_a_stripped_path() {
    use std::os::unix::fs::PermissionsExt;
    // On PATH: spawn by bare name (PATH resolves it).
    assert_eq!(
        resolve_claude_in(true, &[]).as_deref(),
        Some("claude"),
        "an on-PATH claude spawns by bare name"
    );
    // Off PATH, not in any probed dir: fall back to the bare name so the
    // spawn error surfaces (caller uses unwrap_or("claude")).
    let empty = tempfile::tempdir().unwrap();
    assert_eq!(
        resolve_claude_in(false, &[empty.path().to_path_buf()]),
        None
    );
    // Off PATH but present in a probed dir: pin the absolute path.
    let dir = tempfile::tempdir().unwrap();
    let claude = dir.path().join("claude");
    std::fs::write(&claude, "#!/bin/sh\n").unwrap();
    std::fs::set_permissions(&claude, std::fs::Permissions::from_mode(0o755)).unwrap();
    let got = resolve_claude_in(false, &[dir.path().to_path_buf()]).unwrap();
    assert_eq!(got, claude.to_string_lossy());
    assert!(
        Path::new(&got).is_absolute(),
        "GUI launch must get an absolute path, got {got}"
    );
}

/// The system prompt must teach the NOTE fence (with a concrete
/// example), the numbered-buffer convention, and the yield rules the
/// host enforces.
#[test]
fn system_prompt_teaches_note_fence_and_yield_rules() {
    assert!(PAIR_SYSTEM_PROMPT.contains("<<<NOTE <file>:<row>>>>"));
    assert!(PAIR_SYSTEM_PROMPT.contains("<<<NOTE demo.txt:4>>>"));
    assert!(PAIR_SYSTEM_PROMPT.contains("COMMENT-ONLY"));
    assert!(
        PAIR_SYSTEM_PROMPT.contains("- A reply turn answers one of your notes"),
        "the reply turn kind is taught (#1965)"
    );
    assert!(PAIR_SYSTEM_PROMPT.contains("N|"));
    assert!(PAIR_SYSTEM_PROMPT.contains("0-based row"));
}

/// #1926: the navigator's claude loads only the user's own settings, so
/// a workspace's `.claude/settings.json` hooks never run; its built-in
/// tools are exactly Read, Grep and Glob; and those carry no allow rule,
/// so `dontAsk` keeps them inside the workspace.
#[test]
fn the_navigator_claude_cannot_reach_past_the_workspace() {
    let cfg = PairConfig {
        socket: PathBuf::from("/nonexistent/relay.sock"),
        workspace: std::env::temp_dir(),
        name: String::from("claude"),
        model: None,
        task: None,
        provider: Provider::Claude,
    };
    let cmd = claude_command(&cfg).expect("claude command");
    let args: Vec<String> = cmd
        .get_args()
        .map(|a| a.to_string_lossy().into_owned())
        .collect();
    let value_of = |flag: &str| {
        let at = args
            .iter()
            .position(|a| a == flag)
            .unwrap_or_else(|| panic!("{flag} missing from {args:?}"));
        args[at + 1].clone()
    };
    assert_eq!(value_of("--setting-sources"), "user");
    assert_eq!(value_of("--tools"), "Read,Grep,Glob");
    assert_eq!(value_of("--permission-mode"), "dontAsk");
    let allowed: Vec<String> = value_of("--allowedTools")
        .split(',')
        .map(|t| t.trim().to_string())
        .collect();
    assert!(
        allowed.iter().all(|t| t.starts_with("mcp__croft-collab__")),
        "a bare built-in allow rule matches every path: {allowed:?}"
    );
    assert!(args.iter().any(|a| a == "--strict-mcp-config"));
}

/// The ask-turn composer: instruction, invoked range, selected text,
/// and the numbered buffer all ride the turn.
#[test]
fn compose_ask_turn_numbers_content_and_includes_selection() {
    let text = compose_ask_turn(
        "src/a.rs",
        (3, 5),
        "let x = 1;\nlet y = 2;",
        "simplify these",
        "l0\nl1\nl2\nlet x = 1;\nmid\nlet y = 2;",
    );
    assert!(text.contains("simplify these"));
    assert!(text.contains("lines 3-5"));
    assert!(text.contains("0-based"));
    assert!(text.contains("--- SELECTED LINES ---"));
    assert!(text.contains("let y = 2;"));
    assert!(text.contains("0|l0"));
    assert!(text.contains("3|let x = 1;"));

    // Single line, no selection: the range names one line and the
    // selected block is omitted.
    let one = compose_ask_turn("src/a.rs", (2, 2), "", "why is this here", "a\nb\nc");
    assert!(one.contains("line 2"));
    assert!(!one.contains("--- SELECTED LINES ---"));
    assert!(one.contains("2|c"));
}

/// #1966: the gutter numbers lines from 1, so every line a turn names
/// carries that number beside its 0-based row, and the bare 0-based row
/// never stands alone where the model would quote it to its partner.
#[test]
fn turns_name_the_gutter_line_beside_the_row() {
    let one = compose_ask_turn("src/a.rs", (4, 4), "", "fix this", "a\nb\nc\nd\ne");
    assert!(
        one.contains("at line 4 (0-based; line 5 in your partner's gutter)"),
        "{one}"
    );
    assert!(
        !one.contains("line 4 (0-based)."),
        "bare 0-based row: {one}"
    );

    let range = compose_ask_turn("src/a.rs", (0, 2), "a\nb\nc", "tidy", "a\nb\nc");
    assert!(
        range.contains("at lines 0-2 (0-based; lines 1-3 in your partner's gutter)"),
        "{range}"
    );
    assert!(
        !range.contains("lines 0-2 (0-based)."),
        "bare 0-based rows: {range}"
    );

    let reply = compose_reply_turn("main.py", 4, "a note", "correct this", "a\nb\nc\nd\ne");
    assert!(
        reply.contains("at line 4 (0-based; line 5 in your partner's gutter)"),
        "{reply}"
    );
    assert!(
        !reply.contains("line 4 (0-based)."),
        "bare 0-based row: {reply}"
    );
    assert!(
        reply.contains("a NOTE re-anchored to row 4 continues this box"),
        "the re-anchor target is a row: {reply}"
    );
    assert!(
        !reply.contains("re-anchoring to line 4"),
        "the re-anchor row called a line: {reply}"
    );

    // The prose rule the model follows when it writes a line number.
    assert!(
        PAIR_SYSTEM_PROMPT.contains("row N is line N+1"),
        "the system prompt teaches gutter numbers for prose"
    );
}

/// #1966: every numbered "line" the prompt shows is a gutter line. Fence
/// coordinates are rows, so an example such as "pins a remark to line 4"
/// for `<<<NOTE demo.txt:4>>>` cannot teach the model to quote the
/// 0-based row to its partner. The local prompt carries the shared one.
#[test]
fn system_prompt_numbers_lines_only_by_the_gutter() {
    let prompt = system_prompt(&Provider::Local {
        base_url: String::new(),
    });
    for text in prompt.lines() {
        for (at, _) in text.match_indices("line") {
            let after = &text[at + "line".len()..];
            let after = after.strip_prefix('s').unwrap_or(after);
            let numbered = after
                .strip_prefix(' ')
                .is_some_and(|rest| rest.starts_with(|c: char| c.is_ascii_digit()));
            if numbered {
                assert!(
                    text[..at].ends_with("gutter "),
                    "a 0-based row called a line: {text}"
                );
            }
        }
    }
    // The examples still pair each row with its gutter line.
    assert!(prompt.contains("row 0 of demo.txt, your partner's gutter line 1"));
    assert!(prompt.contains("rows 3-4 of src/lib.rs, gutter lines 4-5"));
    assert!(prompt.contains("row 4 of demo.txt, gutter line 5"));
    assert!(prompt.contains("row 1 (gutter line 2)"));
}

/// The yield-turn composer: comment-only rule, the diff since the
/// navigator last saw the file, and the numbered buffer.
#[test]
fn compose_yield_turn_carries_diff_and_forbids_edits() {
    let with_diff = compose_yield_turn(
        "src/a.rs",
        "line one\nline 2\n",
        Some("@@ -1,2 +1,2 @@\n line one\n-line two\n+line 2\n"),
    );
    assert!(with_diff.contains("COMMENT-ONLY"));
    assert!(with_diff.contains("--- CHANGES SINCE YOUR LAST LOOK ---"));
    assert!(with_diff.contains("+line 2"));
    assert!(with_diff.contains("0|line one"));

    let first_look = compose_yield_turn("src/a.rs", "just this\n", None);
    assert!(first_look.contains("COMMENT-ONLY"));
    assert!(!first_look.contains("--- CHANGES SINCE YOUR LAST LOOK ---"));
}

/// Host-enforced comment-only: the scripted claude streams an EDIT fence
/// during a yielded turn; the owner's buffer must stay byte-identical
/// and the suppression must be spoken.
#[test]
fn yield_turn_suppresses_edits() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("stream"); // streams an EDIT fence
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
    host.send_yield_turn("demo.txt", "hello world").unwrap();

    let deadline = Instant::now() + Duration::from_secs(60);
    let mut events: Vec<crate::pair_host::PairEvent> = Vec::new();
    while !events
        .iter()
        .any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
    {
        assert!(Instant::now() < deadline, "no TurnDone; saw {events:?}");
        events.extend(host.poll());
        std::thread::sleep(Duration::from_millis(10));
    }
    // The owner never saw an edit land.
    assert_eq!(harness.remote_edit_count(), 0, "edit must be suppressed");
    // Diagnostics NEVER ride Commentary: the App anchors that channel
    // as the navigator's own comment boxes, so the suppression notice
    // (and claude's stderr chatter) would be rendered as the model's
    // remark in the user's file and counted as a comment.
    assert!(
        !events.iter().any(|e| matches!(
            e,
            crate::pair_host::PairEvent::Commentary(c)
                if c.contains("suppressed") || c.contains("[claude]")
        )),
        "diagnostics must not become comment boxes; saw {events:?}"
    );
    // Whole-snapshot search, no baseline index: OUTPUT is a shared
    // evicting ring, so a length captured earlier can drift under
    // concurrent tests and a skip() would step over the lines.
    let wait_output = |needle: &str| {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let lines = crate::output::snapshot("Navigator").unwrap_or_default();
            if lines.iter().any(|l| l.text.contains(needle)) {
                return;
            }
            assert!(
                Instant::now() < deadline,
                "OUTPUT never carried {needle:?}: {lines:?}"
            );
            std::thread::sleep(Duration::from_millis(10));
        }
    };
    wait_output("suppressed");
    wait_output("[claude] mcp server starting");
    drop(host);
}

/// Poll `host` until a turn ends, returning every event seen on the way.
fn wait_turn_done(host: &mut crate::pair_host::PairHost) -> Vec<crate::pair_host::PairEvent> {
    let deadline = Instant::now() + Duration::from_secs(60);
    let mut events = Vec::new();
    while !events
        .iter()
        .any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
    {
        assert!(Instant::now() < deadline, "no TurnDone; saw {events:?}");
        events.extend(host.poll());
        std::thread::sleep(Duration::from_millis(10));
    }
    events
}

/// #1965: a reply in a comment box is the driver's own words, so it is
/// an invited turn like an ask. "correct this" under a note proposing a
/// fix must be able to land that fix in the buffer.
#[test]
fn a_reply_turn_can_edit_the_buffer() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("stream"); // streams an EDIT fence
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
    host.send_reply_turn("demo.txt", 0, "rename world", "correct this", "hello world")
        .unwrap();
    wait_turn_done(&mut host);
    harness.wait_until("the reply's edit on the owner", |h| {
        h.doc().as_deref() == Some("hello streamed edit")
    });
    drop(host);
}

/// #1965, the gate that must survive it: an edit-capable reply must not
/// leave the next yielded turn able to edit. One seated model edits on
/// every turn: reply 1 lands, yield 2 is discarded, reply 3 lands.
#[test]
fn a_yield_after_a_reply_still_discards_edits() {
    if !is_on_path("python3") {
        eprintln!("SKIPPED: python3 not on PATH");
        return;
    }
    let harness = OwnerHarness::start("hello world");
    let script = harness._dir.path().join("fake_claude.py");
    std::fs::write(&script, FAKE_CLAUDE).unwrap();
    let log = harness._dir.path().join("stdin.log");

    let mut cmd = Command::new("python3");
    cmd.arg(&script).arg(&log).arg("repeat"); // inserts the turn number
    let mut host =
        crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
    host.send_reply_turn("demo.txt", 0, "a note", "correct this", "hello world")
        .unwrap();
    wait_turn_done(&mut host);
    harness.wait_until("reply 1's edit on the owner", |h| {
        h.doc().as_deref() == Some("1hello world")
    });

    host.send_yield_turn("demo.txt", "1hello world").unwrap();
    wait_turn_done(&mut host);
    // Presence: the yield reached the model, which streamed edit 2.
    let seen = std::fs::read_to_string(&log).unwrap();
    assert!(
        seen.contains("yielded the turn"),
        "the yield turn never reached the model: {seen}"
    );

    // Barrier: one pilot's edits reach the owner in order, so once
    // edit 3 has landed, a leaked edit 2 would already be in the doc.
    host.send_reply_turn("demo.txt", 0, "a note", "and again", "1hello world")
        .unwrap();
    wait_turn_done(&mut host);
    harness.wait_until("reply 3's edit on the owner", |h| {
        h.doc().is_some_and(|d| d.starts_with('3'))
    });
    assert_eq!(
        harness.doc().as_deref(),
        Some("31hello world"),
        "the yield's edit must be suppressed"
    );
    drop(host);
}

/// Remote-edit spans shift every note anchored in the edited file, and
/// only that file (the same replay the stream region does).
#[test]
fn note_offsets_shift_on_remote_edit() {
    let mut notes = vec![
        Note {
            id: 1,
            file: "a.rs".into(),
            offset: 10,
            body: "n1".into(),
        },
        Note {
            id: 2,
            file: "b.rs".into(),
            offset: 10,
            body: "n2".into(),
        },
    ];
    let spans = [
        // 3 bytes inserted at 0: before the offset, shifts it right.
        ResolvedSpan {
            at: 0,
            deleted: 0,
            inserted: "abc".into(),
        },
        // Delete far after: no effect.
        ResolvedSpan {
            at: 40,
            deleted: 2,
            inserted: String::new(),
        },
    ];
    shift_notes(&mut notes, "a.rs", &spans);
    assert_eq!(notes[0].offset, 13);
    assert_eq!(notes[1].offset, 10, "other file's note must not move");
}

/// Aborting a streamed edit must also un-shift the notes that edit
/// moved: open_region shifts notes for its delete and every EditBody
/// shifts them for its insert, so the revert has to apply the inverse
/// span. Without it, every cancel or abort left the turn's notes
/// permanently drifted from the rows the model anchored them to.
#[test]
fn an_aborted_edit_un_shifts_the_notes_it_moved() {
    let harness = OwnerHarness::start("hello world");
    let (state, stop, pump) = pumped_state(&harness);
    {
        let mut st = state.lock().unwrap();
        st.begin_turn("demo.txt", "hello world", false);
        // Anchors below, AT, and INSIDE the edited span [0, 5): the
        // below one is shifted and must shift back; the at/inside ones
        // are collapsed by the delete and ride the streamed inserts,
        // so only their recorded pre-edit offsets can restore them.
        for offset in [8usize, 0, 2] {
            let id = st.take_note_id();
            st.notes.push(Note {
                id,
                file: String::from("demo.txt"),
                offset,
                body: String::from("remark"),
            });
        }
    }
    apply_fence_event(
        &state,
        FenceEvent::EditStart {
            file: String::from("demo.txt"),
            start: (0, 0),
            end: (0, 5),
        },
    );
    apply_fence_event(
        &state,
        FenceEvent::EditBody(String::from("hi there friend")),
    );
    apply_fence_event(&state, FenceEvent::EditAbort);
    let offsets: Vec<usize> = state
        .lock()
        .unwrap()
        .notes
        .iter()
        .map(|n| n.offset)
        .collect();
    assert_eq!(
        offsets,
        vec![8, 0, 2],
        "the revert must restore every note anchor the edit had moved"
    );
    stop.store(true, Ordering::Relaxed);
    pump.join().unwrap();
}

/// A note row just past EOF clamps to the last line instead of panicking
/// or anchoring nowhere.
#[test]
fn note_anchor_row_clamps_past_eof() {
    let lines: Vec<String> = ["ab", "cd"].iter().map(|s| s.to_string()).collect();
    assert_eq!(note_offset(&lines, 0), 0);
    assert_eq!(note_offset(&lines, 1), 3);
    assert_eq!(note_offset(&lines, 99), 3);
}

/// Every composed turn names its target file, even when the buffer never
/// went live (field bug 2026-07-15: parse_task_line had stripped the
/// @file prefix, so a dead bootstrap left claude with no file at all).
#[test]
fn compose_turn_always_names_target_file() {
    let live = compose_turn_text("fix the error", Some("src/a.rs"), Some("body"));
    assert!(live.contains("--- CURRENT BUFFER (src/a.rs) ---"));
    assert!(live.contains("body"));

    let dead = compose_turn_text("fix the error", Some("src/a.rs"), None);
    assert!(
        dead.contains("src/a.rs"),
        "file must be named without a live buffer: {dead}"
    );
    assert!(dead.contains("fix the error"));

    let noted = with_pending_note(Some("stream was cancelled".into()), "task".into());
    assert!(noted.starts_with("stream was cancelled"));
    assert!(noted.contains("task"));
}