mindfork 0.10.2

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
//! Orchestrator tests — live #[ignore] e2e smokes (Gemma/bge-m3 via MINDFORK_*_URL). Part of the [`super`]
//! module (fixtures in mod.rs). See docs/history/refactoring-god-objects.md, stage 3.

use super::*;
use crate::features::tools::confirm::ToolDecision;
use crate::shared::api::EmbedRole;

/// Does the reply carry the planted code — whichever dash the model felt like
/// typing?
///
/// Every smoke that plants a fact plants it as `ZARYA-8823`, and the assertion
/// used to be a plain `contains`. On `gpt-oss-120b` that turned three green
/// smokes red while the model was answering *correctly*: it renders the code
/// with a **non-breaking hyphen** (U+2011). Not always, either — 8 of 18
/// occurrences in one run, the same model producing both glyphs, so these were
/// latent flakes on any model rather than a property of this one.
///
/// The fact under test is the code; which dash glyph a model chose to render it
/// with is typography. So both sides are compared with the Unicode dashes folded
/// to ASCII `-`. Nothing else is normalized: case, spacing and the digits stay
/// exactly as strict as they were.
fn mentions_code(haystack: &str, code: &str) -> bool {
    fn fold_dashes(s: &str) -> String {
        s.chars()
            .map(|c| match c {
                // The hyphen/dash block (U+2010 hyphen … U+2015 horizontal bar),
                // the minus sign, and the compatibility forms.
                '\u{2010}'..='\u{2015}' | '\u{2212}' | '\u{FE58}' | '\u{FE63}' | '\u{FF0D}' => '-',
                other => other,
            })
            .collect()
    }
    fold_dashes(haystack).contains(&fold_dashes(code))
}

/// Not ceremony: this helper is the only thing standing between eight live
/// assertions and a typographic hyphen, and "why is this not just `contains`?"
/// is a question a future reader will ask. The answer is a test.
#[test]
fn a_planted_code_survives_the_dash_a_model_chose() {
    assert!(mentions_code("the code is ZARYA-8823.", "ZARYA-8823"));
    // U+2011, measured on gpt-oss-120b; U+2013 and U+2212 are the neighbours a
    // different model would reach for.
    assert!(mentions_code("**ZARYA\u{2011}8823**", "ZARYA-8823"));
    assert!(mentions_code("ZARYA\u{2013}8823", "ZARYA-8823"));
    assert!(mentions_code("ZARYA\u{2212}8823", "ZARYA-8823"));
    // Still strict about everything that is not a dash.
    assert!(!mentions_code("ZARYA 8823", "ZARYA-8823"));
    assert!(!mentions_code("zarya-8823", "ZARYA-8823"));
    assert!(!mentions_code("ZARYA-8824", "ZARYA-8823"));
}

/// Runs `topics` as ordinary turns, then folds the conversation with `/compact`
/// and returns `(summary, folded)`.
///
/// Shared by the two compaction smokes, which need the same three steps for
/// opposite reasons — one checks that a planted fact **survives** the summary,
/// the other that what the summary **dropped** is still reachable. The filler
/// turns are what push the seed behind the verbatim tail; only their topics
/// differ, which is why they are the parameter.
async fn fill_then_compact(
    cmd_tx: &UnboundedSender<AppCommand>,
    evt_rx: &mut UnboundedReceiver<AppEvent>,
    topics: &[&str],
) -> (String, usize) {
    for topic in topics {
        let (r, _) = run_turn_capture(cmd_tx, evt_rx, topic).await;
        eprintln!("filler reply: {}", r.chars().take(80).collect::<String>());
    }
    cmd_tx.send(AppCommand::Compact).unwrap();
    let compacted = wait_for(evt_rx, |e| {
        matches!(e, AppEvent::Compacted { .. } | AppEvent::Error(_))
    })
    .await
    .unwrap();
    let AppEvent::Compacted {
        summary, folded, ..
    } = compacted
    else {
        panic!("compaction failed: {compacted:?}");
    };
    eprintln!(
        "folded {folded} messages into {} chars:\n{}",
        summary.chars().count(),
        summary.chars().take(600).collect::<String>()
    );
    (summary, folded)
}

/// Waits out the bootstrap (profile list + first chat activation) and narrows
/// the default profile to exactly `tools` — the "remove the alternative" rule
/// the tool smokes share: a smoke must not depend on the model's mood not to
/// take a shortcut. Returns `(profile id, the bootstrap chat's id)`.
async fn narrow_profile_to(
    cmd_tx: &UnboundedSender<AppCommand>,
    evt_rx: &mut UnboundedReceiver<AppEvent>,
    tools: Vec<crate::entities::profile::ToolId>,
) -> (Uuid, Uuid) {
    let profile = wait_for(evt_rx, |e| matches!(e, AppEvent::ProfileList(_)))
        .await
        .and_then(|e| match e {
            AppEvent::ProfileList(ps) => ps.first().map(|p| p.id),
            _ => None,
        })
        .expect("the bootstrap profile");
    let chat = wait_for(evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .and_then(|e| match e {
            AppEvent::ChatActivated { id, .. } => Some(id),
            _ => None,
        })
        .expect("the bootstrap chat");
    set_profile_tools(cmd_tx, profile, tools);
    (profile, chat)
}

/// Change a profile's tool set **mid-conversation**, for a smoke whose second
/// turn needs a different set from its first.
///
/// Split out of [`narrow_profile_to`], which can only run once: it waits for the
/// bootstrap's `ProfileList` and `ChatActivated`, and those do not come round
/// again. Ordering is safe without an acknowledgement — this command and the
/// next turn's `SendMessage` travel the same channel, so the orchestrator
/// applies them in that order.
fn set_profile_tools(
    cmd_tx: &UnboundedSender<AppCommand>,
    profile: Uuid,
    tools: Vec<crate::entities::profile::ToolId>,
) {
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: profile,
            edit: Box::new(ProfileEdit {
                enabled_tools: Some(tools),
                ..Default::default()
            }),
        })
        .unwrap();
}

/// Chat attachments, stage 3 go/no-go (docs/file-attachments.md): on a **large**
/// file the model finds the right place **by meaning in one `attachment_search`
/// call**, instead of walking pages. The payload sits deliberately deep — around
/// page 20 of ~25 — so paging to it would take a dozen-plus rounds and blow past
/// `max_tool_rounds`, while search reaches it immediately.
/// Needs both servers: `MINDFORK_ENGINE_URL` **and** `MINDFORK_EMBED_URL` (with no
/// embedder the index isn't built and the smoke has nothing to test).
/// `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL) and embedder (MINDFORK_EMBED_URL)"]
async fn attachment_search_e2e_live() {
    use crate::app::events::FileProgress;
    use crate::shared::config::AttachmentSettings;
    const CODE: &str = "ZARYA-4417";
    if std::env::var("MINDFORK_EMBED_URL").is_err() {
        eprintln!("skip: MINDFORK_EMBED_URL not set (the index needs a real embedder)");
        return;
    }
    let config = AppConfig {
        attachments: AttachmentSettings {
            max_file_tokens: 100, // force by reference
            excerpt_tokens: 60,
            page_tokens: 300,
            ..Default::default()
        },
        ..Default::default()
    };
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Filler around one topically distinctive paragraph: nothing but semantic
    // similarity can single it out.
    let mut body = String::from("Протоколы совещаний отдела эксплуатации.\n\n");
    for i in 1..=120 {
        body.push_str(&format!(
            "Пункт {i}: обсудили график дежурств и порядок передачи смены, решений не приняли.\n"
        ));
    }
    body.push_str(&format!(
        "\nПункт 121: по итогам проверки холодильной установки заменён компрессор; \
         инвентарный код запасной части: {CODE}.\n"
    ));
    for i in 122..=240 {
        body.push_str(&format!(
            "Пункт {i}: рассмотрели заявки на канцелярию и мелкий ремонт, замечаний нет.\n"
        ));
    }
    let path = dir.path().join("protocols.txt");
    std::fs::write(&path, &body).unwrap();

    cmd_tx
        .send(AppCommand::FileAttach {
            path: path.to_string_lossy().into_owned(),
        })
        .unwrap();
    // Wait for indexing to finish (it runs in the background after the attach).
    let indexed = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::FileProgress(FileProgress::Indexed { .. })
                | AppEvent::FileProgress(FileProgress::IndexSkipped { .. })
        )
    })
    .await
    .unwrap();
    eprintln!("index: {indexed:?}");
    assert!(
        matches!(
            indexed,
            AppEvent::FileProgress(FileProgress::Indexed { .. })
        ),
        "the smoke needs a real embedder: {indexed:?}"
    );

    let (answer, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "В прикреплённом файле protocols.txt где-то упомянута замена компрессора \
         холодильной установки. Найди это место и назови инвентарный код запасной части.",
    )
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let names: Vec<&String> = calls.iter().map(|(n, _)| n).collect();
    eprintln!("tool calls: {names:#?}");
    eprintln!("reply: {answer}");
    assert!(
        calls
            .iter()
            .any(|(n, _)| n == crate::features::tools::attachment::ATTACHMENT_SEARCH_ID),
        "the model must find the place by meaning, called: {names:?}"
    );
    assert!(
        mentions_code(&answer, CODE),
        "the code sits deep in the file and must be found: {answer}"
    );
}

/// Chat attachments, stage 2 go/no-go (docs/file-attachments.md): a file too big
/// to inline is attached **by reference**, and the model reaches the part it
/// needs — the answer sits on a **late** page, so the excerpt alone cannot
/// produce it. This is the direct regression for the behaviour seen on a live run
/// of stage 1, where the model had no reader tool at all and flailed into
/// `fs_read`/`web_search` instead.
///
/// Two turns, because stage 3 changed what "reaching it" looks like: with an
/// embedder configured the file is also indexed, and the model now legitimately
/// prefers **one** `attachment_search` call over walking pages (observed live —
/// the earlier, `attachment_read`-only assertion started failing on exactly that,
/// with the answer still correct). So turn 1 asserts the outcome and that the
/// model stayed within the attachment tools, and turn 2 asks for a specific page
/// to keep `attachment_read` itself covered live.
/// `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn attachment_read_e2e_live() {
    use crate::shared::config::AttachmentSettings;
    const CODE: &str = "ZARYA-8823";
    let config = AppConfig {
        attachments: AttachmentSettings {
            // Force by-reference, and page the file into a handful of pages.
            max_file_tokens: 100,
            excerpt_tokens: 60,
            page_tokens: 300,
            ..Default::default()
        },
        ..Default::default()
    };
    // No embedder on purpose: an indexed attachment hands the model
    // `attachment_search`, and then whether it pages through the file at all
    // becomes its choice rather than a property of the code. Search is covered
    // by `attachment_search_e2e_live`; this smoke owns the *guaranteed* path.
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_no_embed(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Filler first, the payload last — the excerpt shows only the beginning.
    let mut body = String::from("Технические заметки проекта.\n\n");
    for i in 1..=45 {
        body.push_str(&format!(
            "Заметка {i}: рутинная запись без особого содержания, строка для объёма.\n"
        ));
    }
    body.push_str(&format!("\nВНУТРЕННИЙ КОД СБОРКИ: {CODE}\n"));
    let path = dir.path().join("notes-big.txt");
    std::fs::write(&path, &body).unwrap();

    cmd_tx
        .send(AppCommand::FileAttach {
            path: path.to_string_lossy().into_owned(),
        })
        .unwrap();
    let attached = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::FileProgress(_)))
        .await
        .unwrap();
    eprintln!("attach: {attached:?}");

    let (answer, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "В прикреплённом файле notes-big.txt указан внутренний код сборки. \
         Прочитай файл и назови этот код.",
    )
    .await;
    let names: Vec<&String> = calls.iter().map(|(n, _)| n).collect();
    eprintln!("turn 1 tool calls: {names:#?}");
    eprintln!("turn 1 reply: {answer}");
    assert!(
        mentions_code(&answer, CODE),
        "the answer sits on a late page and must be found: {answer}"
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // The stage-1 defect, precisely: the model must reach the file through the
    // attachment tools rather than improvising with the filesystem or the web.
    let attachment_tools = [
        crate::features::tools::attachment::ATTACHMENT_READ_ID,
        crate::features::tools::attachment::ATTACHMENT_SEARCH_ID,
    ];
    assert!(
        !calls.is_empty()
            && calls
                .iter()
                .all(|(n, _)| attachment_tools.contains(&n.as_str())),
        "the file must be reached through the attachment tools only, called: {names:?}"
    );
    // And the guaranteed path itself: with nothing indexed, paging is the only
    // way in, so this is now a property of the setup rather than a hope about
    // which route the model picks.
    assert!(
        calls
            .iter()
            .any(|(n, _)| n == crate::features::tools::attachment::ATTACHMENT_READ_ID),
        "a by-reference file with no index must be read page by page, called: {names:?}"
    );
}

/// Chat attachments (docs/file-attachments.md, stage 1 go/no-go): a file attached
/// with `/file attach` actually reaches the model through the real wire path and
/// is used to answer. Two phases in two chats: a **baseline** (no attachment —
/// the model can't know the invented code) and the attached case (it answers with
/// the code). The mirror half — that `/file remove` takes the text back out of the
/// request — is deterministic and covered by a unit test
/// (`attachments::removing_an_attachment_takes_it_out_of_the_request`), so it
/// needs no model. `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn file_attachment_e2e_live() {
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // An invented fact no model can know from pretraining.
    const CODE: &str = "ZARYA-7719";
    const QUESTION: &str =
        "What is the internal build code for project Mindfork? Reply with the code only.";
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Phase 1 — baseline: with nothing attached, the model must not produce the code.
    let (baseline, _) = run_turn_live(&cmd_tx, &mut evt_rx, QUESTION).await;
    eprintln!("baseline reply: {baseline}");

    // Phase 2 — a fresh chat with the file attached.
    cmd_tx
        .send(AppCommand::NewChat { profile_id: None })
        .unwrap();
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let path = dir.path().join("build-notes.md");
    std::fs::write(
        &path,
        format!(
            "# Project Mindfork — internal notes\n\n\
             The internal build code for project Mindfork is {CODE}.\n\
             Do not confuse it with the release tag.\n"
        ),
    )
    .unwrap();
    cmd_tx
        .send(AppCommand::FileAttach {
            path: path.to_string_lossy().into_owned(),
        })
        .unwrap();
    let attached = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::FileProgress(crate::app::events::FileProgress::Attached { .. })
                | AppEvent::FileProgress(crate::app::events::FileProgress::Failed(_))
        )
    })
    .await
    .unwrap();
    eprintln!("attach: {attached:?}");

    let (answer, _) = run_turn_live(&cmd_tx, &mut evt_rx, QUESTION).await;
    eprintln!("attached reply: {answer}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    assert!(
        mentions_code(&answer, CODE),
        "the model must answer from the attached file, got: {answer}"
    );
    assert!(
        !mentions_code(&baseline, CODE),
        "the baseline must not know the invented code (otherwise the test proves nothing): {baseline}"
    );
}

/// The image fixture both vision smokes use: a solid field of `field` with a large white
/// square in the middle. Generated rather than photographed, so the assertion is objective
/// and no pretrained knowledge can answer it — and parameterized by colour, so the two
/// smokes cannot pass on each other's reply.
fn figure_png(field: [u8; 3]) -> Vec<u8> {
    figure_png_with_corner(field, None)
}

/// [`figure_png`] with, optionally, a small square of `corner` in the top-left — a detail a
/// model that never saw the picture cannot know, when the conversation mentions only the
/// field and the white square.
fn figure_png_with_corner(field: [u8; 3], corner: Option<[u8; 3]>) -> Vec<u8> {
    let buf = image::ImageBuffer::from_fn(512, 512, |x, y| match corner {
        Some(c) if x < 96 && y < 96 => image::Rgb(c),
        _ if (160..352).contains(&x) && (160..352).contains(&y) => image::Rgb([255u8, 255, 255]),
        _ => image::Rgb(field),
    });
    let mut bytes = Vec::new();
    image::DynamicImage::ImageRgb8(buf)
        .write_to(
            &mut std::io::Cursor::new(&mut bytes),
            image::ImageFormat::Png,
        )
        .unwrap();
    bytes
}

/// Stages `path` (a file path or a URL — the command takes either) and returns what was
/// staged. A refusal fails here rather than three turns later: an image that never reached
/// the model would otherwise read as a model that cannot see.
async fn attach_image_live(
    cmd_tx: &UnboundedSender<AppCommand>,
    rx: &mut UnboundedReceiver<AppEvent>,
    path: String,
) -> crate::entities::message_image::ImageInfo {
    cmd_tx.send(AppCommand::ImageAttach { path }).unwrap();
    let staged = wait_for(rx, |e| {
        matches!(
            e,
            AppEvent::ImageProgress(crate::app::events::ImageProgress::Attached { .. })
                | AppEvent::ImageProgress(crate::app::events::ImageProgress::Failed(_))
        )
    })
    .await
    .unwrap();
    eprintln!("attach: {staged:?}");
    match staged {
        AppEvent::ImageProgress(crate::app::events::ImageProgress::Attached { info, .. }) => info,
        other => panic!("the image was refused before it ever reached the model: {other:?}"),
    }
}

/// `/continue` through a gateway, on the app's own paths (fork H2 of
/// docs/history/gateway-images-and-continue.md): the endpoint's catalogue lands before any
/// turn, a reply cut by the length limit is announced as continuable exactly when
/// the route table says the model continues, and `/continue` then either refuses
/// with the gateway's note or resumes the reply **without** restarting it.
///
/// Declared, not guessed: `MINDFORK_LIVE_CONTINUE_EXPECT` names the stack —
/// `gateway-continues` for a model the table allows (measured:
/// `anthropic/claude-haiku-4.5`), `gateway-refuses` for one it refuses
/// (`google/gemma-4-31b-it`), and `local` for a llama.cpp, which publishes no
/// catalogue and must keep continuing exactly as before (the regression half). The
/// smoke fails rather than skips when the run disagrees. The restart criterion is
/// the one §1.3 measured: a cut this early leaves the question's own words ahead of
/// the answer, so a continuation carries no `capital` and a restart does.
#[tokio::test]
#[ignore = "requires MINDFORK_ENGINE_URL and MINDFORK_LIVE_CONTINUE_EXPECT (gateway-continues | gateway-refuses | local); a gateway also needs MINDFORK_ENGINE_KEY + MINDFORK_ENGINE_MODEL"]
async fn continue_through_a_gateway_live() {
    use crate::shared::config::ServerMode;
    use std::time::Duration;

    let Ok(declared) = std::env::var("MINDFORK_LIVE_CONTINUE_EXPECT") else {
        eprintln!("skip: MINDFORK_LIVE_CONTINUE_EXPECT not set");
        return;
    };
    let (catalogued, continues) = match declared.trim() {
        "gateway-continues" => (true, true),
        "gateway-refuses" => (true, false),
        "local" => (false, true),
        other => panic!("MINDFORK_LIVE_CONTINUE_EXPECT={other:?} is not a declared stack"),
    };
    let model = std::env::var("MINDFORK_ENGINE_MODEL").ok();
    let mut cfg = no_auto_cfg();
    cfg.engine.mode = ServerMode::External;
    cfg.engine.external.model_name = model.clone();
    let model = model.unwrap_or_else(|| "local".into());
    // "The capital of France" and no further, so the continuation has an answer
    // to carry. Measured at 6 the cut already held "Paris" and the continuation
    // was a lone ".", which a restart could not have produced but a broken one
    // could hardly be told from.
    cfg.default_sampling.max_tokens = Some(4);
    // Thinking off: a local Gemma 4 spent the whole cap reasoning, left no visible
    // text, and a thoughts-only fragment is rightly not continuable — the fixture,
    // not the feature. `reasoning_budget: 0` is llama.cpp's switch and a gateway
    // drops it, so it changes nothing on the routes above.
    cfg.default_sampling.reasoning_budget = Some(0);
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // The discovery lands before any turn either way; what it carries is the claim.
    let landed = tokio::time::timeout(
        Duration::from_secs(60),
        wait_for(&mut evt_rx, |e| {
            matches!(e, AppEvent::EngineSamplingFields(_))
        }),
    )
    .await
    .expect("the engine's facts must land before the first turn")
    .expect("the event stream");
    assert_eq!(
        matches!(landed, AppEvent::EngineSamplingFields(Some(_))),
        catalogued,
        "declared {declared:?}, the endpoint answered {landed:?}"
    );

    cmd_tx
        .send(AppCommand::SendMessage(
            "What is the capital of France? Answer in one short sentence.".into(),
        ))
        .unwrap();
    let mut partial = String::new();
    let (reason, continuable) = loop {
        match evt_rx.recv().await.expect("the turn's events") {
            AppEvent::Chunk { text, .. } => partial.push_str(&text),
            AppEvent::Finished {
                reason,
                continuable,
                ..
            } => break (reason, continuable),
            _ => {}
        }
    };
    eprintln!("[{model}] first turn ({reason:?}, continuable={continuable}): {partial:?}");
    assert_eq!(
        reason,
        FinishReason::Length,
        "the fixture needs a length cut"
    );
    assert_eq!(
        continuable, continues,
        "the note after a cut must promise /continue exactly where it works"
    );

    cmd_tx.send(AppCommand::ContinueLast).unwrap();
    let mut resumed = String::new();
    let mut note = None;
    loop {
        match evt_rx.recv().await.expect("the command's events") {
            AppEvent::Chunk { text, .. } => resumed.push_str(&text),
            AppEvent::Error(text) if !continues => {
                note = Some(text);
                break;
            }
            AppEvent::Finished { .. } if continues => break,
            _ => {}
        }
    }
    eprintln!("[{model}] /continue: note={note:?} resumed={resumed:?}");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    if continues {
        assert!(!resumed.is_empty(), "the continuation brought nothing");
        assert!(
            !resumed.to_lowercase().contains("capital"),
            "the reply restarted instead of continuing: {partial:?} + {resumed:?}"
        );
    } else {
        let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::default());
        assert_eq!(
            note.as_deref(),
            Some(loc.t("ui.cmd.continue_unsupported_gateway"))
        );
    }
}

/// Live e2e for image attachments (spec §9.10): an image staged with `/image attach`
/// reaches a vision-capable model, and **is still seen a turn later**, replayed out of
/// history rather than re-staged.
///
/// The fixture is a generated image, not a photograph, so the assertion is objective and
/// no pretrained knowledge can answer it: a solid blue field with a large white square in
/// the middle. Two turns, because history replay is the half a unit test cannot settle —
/// the first proves the image arrived, the second proves the stored message re-sent it.
///
/// Requires the server to be started with `--mmproj` (`/props` then reports
/// `modalities.vision`); against a text-only server this fails loudly rather than
/// skipping, which is deliberate — a vision smoke that quietly passes on a blind model is
/// worse than none (docs/lessons.md §9). `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a vision-capable OpenAI-compatible server (MINDFORK_ENGINE_URL + --mmproj)"]
async fn image_attachment_e2e_live() {
    if crate::shared::api::live_text_only() {
        eprintln!("skip: MINDFORK_LIVE_TEXT_ONLY — this stack has no vision projector");
        return;
    }
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // A blue field with a centred white square — two facts to check, both objective.
    let path = dir.path().join("figure.png");
    std::fs::write(&path, figure_png([20, 60, 200])).unwrap();
    attach_image_live(&cmd_tx, &mut evt_rx, path.to_string_lossy().into_owned()).await;

    // Turn 1 — the image is on the message being sent.
    let (first, _) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "What is the background colour of this image, and what shape is in the centre? \
         Answer in a few words.",
    )
    .await;
    eprintln!("turn 1 reply: {first}");

    // Turn 2 — nothing new is staged; the model can only answer from the replayed
    // history, which is what this half of the test exists to prove.
    let (second, _) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "What colour was the square in the image I sent? One word.",
    )
    .await;
    eprintln!("turn 2 reply: {second}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let first = first.to_lowercase();
    assert!(
        first.contains("blue"),
        "the model must see the background colour, got: {first}"
    );
    assert!(
        first.contains("square"),
        "the model must see the centred shape, got: {first}"
    );
    assert!(
        second.to_lowercase().contains("white"),
        "the image must still be visible a turn later, replayed from history, got: {second}"
    );
}

/// Whether a gateway's model takes images now comes from its catalogue
/// (docs/research/gateway-vision-catalogue.md, V1(a)) — checked on the app's own paths,
/// through the retry decorator the application wraps the client in.
///
/// Declared, not guessed: `MINDFORK_LIVE_VISION_EXPECT` names the model behind
/// `MINDFORK_ENGINE_MODEL` — `unsupported` for one the catalogue lists as text-only,
/// `supported` for one that lists `image`. Text-only: `/image attach` is refused with the
/// message that names the way out, and with a provisioned `MINDFORK_SANDBOX_DIR` a
/// `python_exec` chart is withheld and said, and the turn completes — where, measured, the
/// chart turned the next round into the gateway's `404` (research §2.2). Vision: the image
/// attaches without the "this engine does not report" note, which would now be untrue.
#[tokio::test]
#[ignore = "requires MINDFORK_ENGINE_URL + MINDFORK_ENGINE_MODEL on a gateway with a catalogue, and MINDFORK_LIVE_VISION_EXPECT"]
async fn a_gateways_catalogue_decides_whether_images_are_sent_live() {
    use crate::app::events::ImageProgress;
    let Ok(expect) = std::env::var("MINDFORK_LIVE_VISION_EXPECT") else {
        eprintln!("skip: MINDFORK_LIVE_VISION_EXPECT not set");
        return;
    };
    let supported = match expect.as_str() {
        "supported" => true,
        "unsupported" => false,
        other => {
            panic!("MINDFORK_LIVE_VISION_EXPECT is `supported` or `unsupported`, not {other:?}")
        }
    };
    // As the application runs on a gateway: `external`, with the model named. The mode
    // also picks the refusal's wording — a managed server is told about its projector.
    let gateway = |cfg: &mut AppConfig| {
        cfg.engine.mode = crate::shared::config::ServerMode::External;
        cfg.engine.external.model_name = std::env::var("MINDFORK_ENGINE_MODEL").ok();
    };
    let mut cfg = no_auto_cfg();
    gateway(&mut cfg);
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let path = dir.path().join("figure.png");
    std::fs::write(&path, figure_png([20, 60, 200])).unwrap();
    cmd_tx
        .send(AppCommand::ImageAttach {
            path: path.to_string_lossy().into_owned(),
        })
        .unwrap();
    // The "could not say" note follows `Attached`, and the staged list follows both.
    let mut attached = None;
    let mut refused = None;
    let mut unknown_note = false;
    loop {
        match evt_rx.recv().await.expect("the attach's events") {
            AppEvent::ImageProgress(ImageProgress::Attached { info, .. }) => attached = Some(info),
            AppEvent::ImageProgress(ImageProgress::VisionUnknown) => unknown_note = true,
            AppEvent::ImageProgress(ImageProgress::Failed(msg)) => {
                refused = Some(msg);
                break;
            }
            AppEvent::StagedImages(_) if attached.is_some() => break,
            _ => {}
        }
    }
    eprintln!("[{expect}] attached={attached:?} refused={refused:?} unknown_note={unknown_note}");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    if supported {
        assert!(attached.is_some(), "a vision model's image must attach");
        assert!(
            !unknown_note,
            "the catalogue vouched for the model, so the caveat would be untrue"
        );
        return;
    }
    let told: Vec<String> = crate::shared::i18n::Lang::ALL
        .iter()
        .map(|l| {
            crate::shared::i18n::locale(*l)
                .t("ui.err.image_no_vision")
                .to_string()
        })
        .collect();
    assert!(
        refused
            .as_deref()
            .is_some_and(|m| told.iter().any(|t| m.contains(t.as_str()))),
        "a text-only model's attach must be refused with the way out: {refused:?}"
    );

    // The tool half: a chart the model's own code draws.
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_python_chat_with(gateway).await else {
        return;
    };
    cmd_tx
        .send(AppCommand::SendMessage(
            "Plot these monthly sales as a bar chart and save it as a PNG: Jan 120, Feb 95, \
             Mar 180, Apr 130, May 160. Then tell me in one sentence what the chart shows."
                .into(),
        ))
        .unwrap();
    let mut reply = String::new();
    let mut thoughts = String::new();
    let mut calls: Vec<(String, usize)> = Vec::new();
    let mut errors: Vec<String> = Vec::new();
    while let Some(ev) = evt_rx.recv().await {
        match ev {
            AppEvent::Chunk { text, .. } => reply.push_str(&text),
            AppEvent::Thoughts { text, .. } => thoughts.push_str(&text),
            AppEvent::ToolCall {
                name,
                result,
                images,
                ..
            } if name == crate::features::tools::PYTHON_EXEC_ID => calls.push((result, images)),
            AppEvent::Error(e) => errors.push(e),
            AppEvent::Finished { reason, .. } => {
                eprintln!("finished: {reason:?}, thoughts: {thoughts}");
                break;
            }
            _ => {}
        }
    }
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    for (result, images) in &calls {
        eprintln!("python_exec → {images} image(s) sent:\n{result}");
    }
    eprintln!("reply: {reply}\nerrors: {errors:?}");
    assert!(errors.is_empty(), "the turn must not fail: {errors:?}");
    assert!(!calls.is_empty(), "the model never ran python_exec");
    assert_eq!(
        calls.iter().map(|(_, n)| n).sum::<usize>(),
        0,
        "no image may reach a text-only model"
    );
    // Said on the chart's own line — and never also claimed as shown, which the line did
    // until the loop, the one place that knows, wrote the claim (spec §9.10).
    let said = |key: &'static str| -> Vec<&'static str> {
        crate::shared::i18n::Lang::ALL
            .iter()
            .map(|l| crate::shared::i18n::locale(*l).t(key))
            .collect()
    };
    let (withheld, shown) = (
        said("loop.image_not_shown_no_vision"),
        said("loop.image_shown"),
    );
    assert!(
        calls
            .iter()
            .any(|(r, _)| withheld.iter().any(|t| r.contains(t))),
        "the result must say the chart was not shown"
    );
    assert!(
        !calls
            .iter()
            .any(|(r, _)| shown.iter().any(|t| r.contains(t))),
        "no result may claim a chart was shown to a text-only model"
    );
    assert!(!reply.trim().is_empty(), "the turn must end with a reply");
}

/// A chat whose history already holds an image, after a switch to an engine that takes
/// none (docs/research/history-images-no-vision.md) — the real sequence, on the app's own
/// paths: the app runs on a vision engine (`MINDFORK_ENGINE_URL`, `…_MODEL`), the image is
/// attached and seen; the app is restarted on the **same data** with an engine that takes
/// no images (`MINDFORK_ENGINE_URL_BLIND`, `…_KEY_BLIND`, `…_MODEL_BLIND`) — a llama.cpp
/// without `--mmproj`, or a gateway model the catalogue lists as text-only. There, before
/// this stage, every turn was a `500` or a `404`. Now the turn completes, asked about a
/// detail the conversation never mentioned the model says it cannot see the image rather
/// than denying the detail, the user is told once, and the stored image survives.
#[tokio::test]
#[ignore = "requires MINDFORK_ENGINE_URL (a vision engine) and MINDFORK_ENGINE_URL_BLIND (one without)"]
async fn a_history_image_on_an_engine_without_vision_live() {
    let Some(blind) =
        crate::shared::api::live_client("MINDFORK_ENGINE_URL_BLIND", "MINDFORK_ENGINE_KEY_BLIND")
    else {
        eprintln!("skip: MINDFORK_ENGINE_URL_BLIND not set");
        return;
    };
    let Some(sighted) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let external = |model: Option<String>| {
        let mut cfg = no_auto_cfg();
        cfg.engine.mode = crate::shared::config::ServerMode::External;
        cfg.engine.external.model_name = model;
        cfg
    };
    let dir = tempfile::tempdir().unwrap();

    // 1. A vision engine: the image is attached and seen.
    let (cmd_tx, mut evt_rx, handle) = spawn_orch_at(
        dir.path(),
        Some(sighted),
        external(std::env::var("MINDFORK_ENGINE_MODEL").ok()),
    );
    let Some(AppEvent::ChatActivated { id: chat, .. }) =
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. })).await
    else {
        panic!("no chat");
    };
    let path = dir.path().join("figure.png");
    std::fs::write(
        &path,
        figure_png_with_corner([20, 60, 200], Some([220, 30, 30])),
    )
    .unwrap();
    attach_image_live(&cmd_tx, &mut evt_rx, path.to_string_lossy().into_owned()).await;
    let (first, _) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "What is the background colour of this image, and what large shape is in the centre? \
         Mention only those two things, in a few words.",
    )
    .await;
    eprintln!("sighted reply: {first}");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    assert!(
        first.to_lowercase().contains("blue"),
        "the vision engine must see the image before the switch: {first}"
    );

    // 2. The same data, an engine that takes no images.
    let (cmd_tx, mut evt_rx, handle) = spawn_orch_at(
        dir.path(),
        Some(crate::shared::api::retry::RetryBackend::wrap(Arc::new(
            blind,
        ))),
        external(std::env::var("MINDFORK_ENGINE_MODEL_BLIND").ok()),
    );
    let Some(AppEvent::ChatActivated { id, .. }) =
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. })).await
    else {
        panic!("no chat after the restart");
    };
    assert_eq!(
        id, chat,
        "the restart reopens the chat that holds the image"
    );
    let notes: Vec<String> = crate::shared::i18n::Lang::ALL
        .iter()
        .map(|l| crate::shared::i18n::locale(*l).tf("ui.chat.images_withheld", &[("n", "1")]))
        .collect();
    let turn = |message: &'static str| {
        cmd_tx
            .send(AppCommand::SendMessage(message.into()))
            .unwrap();
    };
    let collect = async |rx: &mut UnboundedReceiver<AppEvent>| {
        let (mut reply, mut errors, mut told) = (String::new(), Vec::new(), 0usize);
        while let Some(ev) = rx.recv().await {
            match ev {
                AppEvent::Chunk { text, .. } => reply.push_str(&text),
                AppEvent::Error(e) => errors.push(e),
                AppEvent::Notice(n) if notes.contains(&n) => told += 1,
                AppEvent::Finished { .. } => break,
                _ => {}
            }
        }
        (reply, errors, told)
    };
    turn(
        "What colour is the small square in the top-left corner of the image I sent? \
         Answer in one short sentence.",
    );
    // Every wait bounded (docs/lessons.md §2): without the fix there is no note to wait
    // for, and an unbounded wait turned that red run into a hang.
    let turn_cap = std::time::Duration::from_secs(600);
    let (second, errors, _) = tokio::time::timeout(turn_cap, collect(&mut evt_rx))
        .await
        .expect("the turn ends");
    // The note follows the turn's result, so it lands after `Finished`.
    let told = tokio::time::timeout(
        std::time::Duration::from_secs(10),
        wait_for(
            &mut evt_rx,
            |e| matches!(e, AppEvent::Notice(n) if notes.contains(n)),
        ),
    )
    .await
    .is_ok_and(|ev| ev.is_some());
    turn("Thanks, that is all.");
    let (third, more_errors, told_again) = tokio::time::timeout(turn_cap, collect(&mut evt_rx))
        .await
        .expect("the turn ends");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!(
        "blind reply: {second}\nerrors: {errors:?}\nnext reply: {third}\nerrors: {more_errors:?}"
    );

    assert!(
        errors.is_empty() && more_errors.is_empty(),
        "no turn may be refused: {errors:?} {more_errors:?}"
    );
    assert!(told, "the chat is told its image was not sent");
    assert_eq!(told_again, 0, "…once");
    let low = second.to_lowercase();
    const DECLINES: &[&str] = &[
        "cannot see",
        "can't see",
        "can not see",
        "unable to see",
        "not able to see",
        "cannot view",
        "can't view",
        "don't have access",
        "do not have access",
        "not included",
        "wasn't included",
        "was not included",
        "не вижу",
        "не могу увидеть",
        "не могу видеть",
        "не передан",
    ];
    assert!(
        DECLINES.iter().any(|d| low.contains(d)),
        "asked about a detail it never saw, the model must say it cannot see the image: {second}"
    );
    let stored: usize = Storage::open(Paths::with_root(dir.path()))
        .unwrap()
        .json()
        .load_chat(chat)
        .unwrap()
        .unwrap()
        .messages
        .iter()
        .map(|m| m.images.len())
        .sum();
    assert_eq!(stored, 1, "the stored image survives the switch");
}

/// Sandbox file exchange, stage 2 (docs/history/sandbox-file-exchange.md §8): a chart the model's
/// code saves to `/w/out` lands in the chat's folder, is listed in the chat — `/file list`
/// finds its bytes on disk — and is shown to the model; with `tools.python_images` off the
/// file still lands, no image is sent, and the result tells the model it has not seen it.
/// Stage 1's scenario on production code, without the input file stage 3 adds: the numbers
/// are in the request. Needs a vision model (`MINDFORK_ENGINE_URL` + `--mmproj`) and a
/// provisioned `MINDFORK_SANDBOX_DIR`, which the temporary data root is pointed at
/// (§11 S13). `#[ignore]`, manual against a live stack.
#[tokio::test]
#[ignore = "requires a vision model (MINDFORK_ENGINE_URL) and a provisioned sandbox (MINDFORK_SANDBOX_DIR)"]
async fn sandbox_outputs_e2e_live() {
    use crate::features::file_command::FileProgress;
    const REQUEST: &str = "Plot these monthly sales as a bar chart and save it as a PNG: \
        Jan 120, Feb 95, Mar 180, Apr 130, May 160. Then tell me in one sentence what the \
        chart shows.";
    if crate::shared::api::live_text_only() {
        eprintln!("skip: MINDFORK_LIVE_TEXT_ONLY — this stack has no vision projector");
        return;
    }
    let Some(sandbox) = std::env::var_os("MINDFORK_SANDBOX_DIR").map(std::path::PathBuf::from)
    else {
        eprintln!("skip: MINDFORK_SANDBOX_DIR not set");
        return;
    };
    for images_on in [true, false] {
        let mut cfg = no_auto_cfg();
        cfg.tools.python_enabled = true;
        cfg.tools.python_mode = crate::shared::config::PythonMode::Wasmer;
        cfg.tools.python_net_enabled = false;
        cfg.tools.python_images = images_on;
        cfg.default_sampling.max_tokens = Some(4096);
        let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_sandbox(cfg, sandbox.clone())
        else {
            eprintln!("skip: MINDFORK_ENGINE_URL not set");
            return;
        };
        let (_profile, chat) = narrow_profile_to(
            &cmd_tx,
            &mut evt_rx,
            vec![crate::features::tools::PYTHON_EXEC_ID.into()],
        )
        .await;
        cmd_tx
            .send(AppCommand::SendMessage(REQUEST.into()))
            .unwrap();
        let mut reply = String::new();
        let mut calls: Vec<(String, usize)> = Vec::new();
        while let Some(ev) = evt_rx.recv().await {
            match ev {
                AppEvent::Chunk { text, .. } => reply.push_str(&text),
                AppEvent::ToolCall {
                    name,
                    result,
                    images,
                    ..
                } if name == crate::features::tools::PYTHON_EXEC_ID => calls.push((result, images)),
                AppEvent::Finished { .. } => break,
                _ => {}
            }
        }
        for (result, images) in &calls {
            eprintln!("[images {images_on}] result, {images} image(s) sent:\n{result}");
        }
        eprintln!("[images {images_on}] reply: {reply}");
        cmd_tx.send(AppCommand::FileList).unwrap();
        let listed = wait_for(&mut evt_rx, |e| {
            matches!(e, AppEvent::FileProgress(FileProgress::Listed { .. }))
        })
        .await;
        cmd_tx.send(AppCommand::Quit).unwrap();
        handle.await.unwrap();

        let Some(AppEvent::FileProgress(FileProgress::Listed { stored, .. })) = listed else {
            panic!("no /file list reply");
        };
        let png = stored
            .iter()
            .find(|f| f.mime == "image/png")
            .unwrap_or_else(|| panic!("no PNG listed: {stored:?}"));
        assert!(!png.missing, "the listed PNG is not in the folder");
        let on_disk = dir
            .path()
            .join("files")
            .join(chat.to_string())
            .join(&png.name);
        assert!(
            std::fs::read(&on_disk).is_ok_and(|b| b.starts_with(b"\x89PNG")),
            "not a PNG: {}",
            on_disk.display()
        );
        assert!(
            calls.iter().any(|(r, _)| r.contains("files:")),
            "no result carried the files section"
        );
        let sent: usize = calls.iter().map(|(_, n)| n).sum();
        if images_on {
            assert!(sent >= 1, "the chart must reach the model");
        } else {
            assert_eq!(sent, 0, "no image may be sent with the switch off");
            let told: Vec<String> = crate::shared::i18n::Lang::ALL
                .iter()
                .map(|l| {
                    crate::shared::i18n::locale(*l)
                        .t("tool.python_exec.files.not_shown_off")
                        .trim_start_matches("")
                        .to_string()
                })
                .collect();
            assert!(
                calls
                    .iter()
                    .any(|(r, _)| told.iter().any(|t| r.contains(t.as_str()))),
                "the result must tell the model it has not seen the image"
            );
        }
    }
}

/// A chat whose only tool is `python_exec` in its sandbox mode, on a temporary data root
/// pointed at a provisioned sandbox (§11 S13). `None` — the live stack or the sandbox is
/// not configured, and the caller skips. One fixture for the stage-3 smokes: three copies
/// of a spawn prologue written minutes apart is the duplication this project keeps paying
/// for (docs/lessons.md §2).
async fn spawn_python_chat() -> Option<(
    tempfile::TempDir,
    UnboundedSender<AppCommand>,
    UnboundedReceiver<AppEvent>,
    tokio::task::JoinHandle<()>,
)> {
    spawn_python_chat_with(|_| {}).await
}

/// [`spawn_python_chat`] with the configuration adjusted before the spawn — the engine
/// mode, above all, when a smoke is about what a gateway answers.
async fn spawn_python_chat_with(
    adjust: impl FnOnce(&mut AppConfig),
) -> Option<(
    tempfile::TempDir,
    UnboundedSender<AppCommand>,
    UnboundedReceiver<AppEvent>,
    tokio::task::JoinHandle<()>,
)> {
    let sandbox = match std::env::var_os("MINDFORK_SANDBOX_DIR").map(std::path::PathBuf::from) {
        Some(dir) => dir,
        None => {
            eprintln!("skip: MINDFORK_SANDBOX_DIR not set");
            return None;
        }
    };
    let mut cfg = no_auto_cfg();
    cfg.tools.python_enabled = true;
    cfg.tools.python_mode = crate::shared::config::PythonMode::Wasmer;
    cfg.tools.python_net_enabled = false;
    cfg.default_sampling.max_tokens = Some(4096);
    adjust(&mut cfg);
    let (dir, cmd_tx, mut evt_rx, handle) =
        spawn_orch_live_sandbox(cfg, sandbox).or_else(|| {
            eprintln!("skip: MINDFORK_ENGINE_URL not set");
            None
        })?;
    narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec![crate::features::tools::PYTHON_EXEC_ID.into()],
    )
    .await;
    Some((dir, cmd_tx, evt_rx, handle))
}

/// Sends one message and collects the turn: the reply, and every `python_exec` result.
/// Both are printed — a live smoke that fails has to say what the model actually did.
async fn python_turn(
    cmd_tx: &UnboundedSender<AppCommand>,
    evt_rx: &mut UnboundedReceiver<AppEvent>,
    message: &str,
) -> (String, Vec<PythonCall>) {
    cmd_tx
        .send(AppCommand::SendMessage(message.into()))
        .unwrap();
    let mut reply = String::new();
    let mut results: Vec<PythonCall> = Vec::new();
    while let Some(ev) = evt_rx.recv().await {
        match ev {
            AppEvent::Chunk { text, .. } => reply.push_str(&text),
            AppEvent::ToolCall {
                name,
                arguments,
                result,
                ..
            } if name == crate::features::tools::PYTHON_EXEC_ID => {
                results.push(PythonCall { arguments, result });
            }
            AppEvent::Finished { .. } => break,
            _ => {}
        }
    }
    for call in &results {
        eprintln!("python_exec({})\n{}", call.arguments, call.result);
    }
    eprintln!("reply: {reply}");
    (reply, results)
}

#[derive(Debug)]
/// One `python_exec` call of a live turn: what the model asked for, and what it got back.
/// The arguments matter to any smoke about **which handle** was named — the result text
/// does not carry it, and a test that cannot see it cannot tell a working numbering from
/// a broken one.
struct PythonCall {
    arguments: String,
    result: String,
}

/// The first run of at least `digits` digits in `s`, with any thousands separators dropped
/// — what the sandbox computed, as the reply would repeat it.
fn first_number(s: &str, digits: usize) -> Option<String> {
    let plain: String = s.chars().filter(|c| *c != ',' && *c != ' ').collect();
    let mut best: Option<String> = None;
    let mut run = String::new();
    for c in plain.chars().chain(std::iter::once('.')) {
        if c.is_ascii_digit() {
            run.push(c);
            continue;
        }
        if run.len() >= digits && best.is_none() {
            best = Some(run.clone());
        }
        run.clear();
    }
    best
}

/// Sandbox file exchange, stage 3 (docs/history/sandbox-file-exchange.md §12 T14): what one call
/// saved, a **later** call reads — D4's persistence, end to end, through the chat's files.
/// Turn 1 writes a workbook into `/w/out` with numbers the model does not choose and is
/// told not to print; turn 2 names that workbook in `files`, so it is copied into `/w/in`,
/// and reads the total back with pandas.
///
/// The sandbox's network reaches the public internet and **not this machine**
/// (docs/research/safe-defaults.md D4). Measured before the change: a bare `--net` gave
/// sandboxed code the host's own loopback and LAN, so a listener here was reachable from
/// inside the guest.
///
/// The count on the host listener is the assertion rather than the code's own error text:
/// a filter that refused *after* connecting would read the same from inside. The control
/// arm — the same code with `tools.web_allow_private` on — must reach it, or the first
/// arm proves only that the sandbox had no network at all.
///
/// `#[ignore]`, manual against a live stack.
#[tokio::test]
#[ignore = "requires a live model (MINDFORK_ENGINE_URL) and a provisioned sandbox (MINDFORK_SANDBOX_DIR)"]
async fn sandbox_network_is_public_only_e2e_live() {
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn host_listener() -> (u16, std::sync::Arc<AtomicUsize>) {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let hits = std::sync::Arc::new(AtomicUsize::new(0));
        let counter = std::sync::Arc::clone(&hits);
        std::thread::spawn(move || {
            while let Ok((stream, _)) = listener.accept() {
                counter.fetch_add(1, Ordering::SeqCst);
                drop(stream);
            }
        });
        (port, hits)
    }

    // The code is a raw literal with real newlines: a Rust line continuation would
    // indent every line of it, and Python answers that with an `IndentationError`
    // before it ever reaches the network (measured the first time this ran).
    let ask = |port: u16| {
        let code = format!(
            r#"import socket, urllib.request
def probe(host, port):
    try:
        s = socket.create_connection((host, port), timeout=3)
        s.close()
        return 'CONNECTED'
    except Exception as e:
        return type(e).__name__
print('loopback:', probe('127.0.0.1', {port}))
try:
    print('public:', urllib.request.urlopen('https://example.com', timeout=20).status)
except Exception as e:
    print('public:', type(e).__name__)"#
        );
        format!(
            "Use python_exec exactly once, with this code verbatim and nothing added \
             or reindented:\n\n{code}\n\nThen report what it printed."
        )
    };

    // Arm 1: the default — public addresses only.
    let (port, hits) = host_listener();
    let Some((_dir, cmd_tx, mut evt_rx, handle)) =
        spawn_python_chat_with(|cfg| cfg.tools.python_net_enabled = true).await
    else {
        return;
    };
    let (_reply, calls) = python_turn(&cmd_tx, &mut evt_rx, &ask(port)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    let printed = calls
        .iter()
        .map(|c| c.result.as_str())
        .collect::<Vec<_>>()
        .join(
            "
",
        );
    eprintln!(
        "public-only arm printed:
{printed}"
    );
    assert!(
        printed.contains("public: 200"),
        "the public internet must still work: {printed}"
    );
    assert!(
        !printed.contains("loopback: CONNECTED"),
        "the host's loopback was reachable from the sandbox: {printed}"
    );
    assert_eq!(
        hits.load(Ordering::SeqCst),
        0,
        "the sandbox connected to a service on this machine"
    );

    // Arm 2 (control): with private addresses allowed, the same code reaches it.
    let (port, hits) = host_listener();
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_python_chat_with(|cfg| {
        cfg.tools.python_net_enabled = true;
        cfg.tools.web_allow_private = true;
    })
    .await
    else {
        return;
    };
    let (_reply, calls) = python_turn(&cmd_tx, &mut evt_rx, &ask(port)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    let printed = calls
        .iter()
        .map(|c| c.result.as_str())
        .collect::<Vec<_>>()
        .join(
            "
",
        );
    eprintln!(
        "allow-private arm printed:
{printed}"
    );
    assert!(
        printed.contains("loopback: CONNECTED") || hits.load(Ordering::SeqCst) > 0,
        "the control arm reached nothing either — the arms prove nothing apart: {printed}"
    );
}

/// A workbook rather than a CSV on purpose: its bytes have to survive being stored and
/// staged **unchanged**, or openpyxl cannot open it at all. And the number is the
/// sandbox's, not the conversation's — the peak-month probe of §10 showed how easily an
/// answer arrives through a channel the test did not mean to leave open — so the assertion
/// is that the reply repeats what the second call actually printed.
///
/// `#[ignore]`, manual against a live stack.
#[tokio::test]
#[ignore = "requires a live model (MINDFORK_ENGINE_URL) and a provisioned sandbox (MINDFORK_SANDBOX_DIR)"]
async fn sandbox_inputs_e2e_live() {
    use crate::features::file_command::FileProgress;
    const MAKE: &str = "Use python_exec once. Build an Excel workbook with openpyxl: one \
        sheet with the header row month,total and twelve data rows, where row i (1..12) has \
        month 2026-i and total = i*i*7 + 13. Save it as /w/out/sales.xlsx. Do NOT print the \
        numbers or the sum — just say the file is saved.";
    const READ: &str = "Use python_exec again. The workbook you saved is one of this chat's \
        files: name it in the files argument of the call so it is copied into /w/in, read it \
        from there with pandas, and print the sum of the total column. Then tell me that sum \
        in your reply.";

    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_python_chat().await else {
        return;
    };
    let (_made, first) = python_turn(&cmd_tx, &mut evt_rx, MAKE).await;
    let (reply, second) = python_turn(&cmd_tx, &mut evt_rx, READ).await;
    cmd_tx.send(AppCommand::FileList).unwrap();
    let listed = wait_for(&mut evt_rx, |e| {
        matches!(e, AppEvent::FileProgress(FileProgress::Listed { .. }))
    })
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    assert!(
        first.iter().any(|c| c.result.contains("files:")),
        "the first call saved nothing to /w/out"
    );
    let Some(AppEvent::FileProgress(FileProgress::Listed { stored, .. })) = listed else {
        panic!("no /file list reply");
    };
    let workbook = stored
        .iter()
        .find(|f| f.name.ends_with(".xlsx"))
        .unwrap_or_else(|| panic!("no workbook stored: {stored:?}"));
    assert!(
        !workbook.missing,
        "the listed workbook is not in the folder"
    );
    assert!(
        dir.path().join("files").exists(),
        "the chat's folder was never created"
    );

    // The second call ran (a refusal would have said so and run nothing) and printed a
    // number the sandbox computed from the staged file.
    assert!(
        !second.iter().any(|c| c.result.contains("nothing was run")),
        "the second call was refused: {second:?}"
    );
    let printed = second
        .iter()
        .find_map(|c| first_number(&c.result, 3))
        .unwrap_or_else(|| panic!("the second call printed no number: {second:?}"));
    assert!(
        fold_dashes(&reply).contains(&printed),
        "the reply does not carry what the call read back ({printed}): {reply}"
    );
}

/// Sandbox file exchange, stage 5 (§14 V7): the **Local** mode's round trip through a live
/// model — the half a unit test cannot reach, since what is being checked is that a model
/// told about `in/` and `out/` uses them. The chat's attached table is named in `files`,
/// read from `in/` and summed, and the summary is written to `out/` and kept with the chat.
///
/// Needs `MINDFORK_ENGINE_URL` and a Python on `PATH` — and no sandbox at all, which is the
/// point of the mode. The standard library only: the machine's interpreter is the user's,
/// and pandas may not be on it.
///
/// `#[ignore]`, manual against a live stack.
#[tokio::test]
#[ignore = "requires a live model (MINDFORK_ENGINE_URL) and a Python interpreter on PATH"]
async fn local_mode_files_round_trip_e2e_live() {
    use crate::features::file_command::FileProgress;
    const ASK: &str = "A table is one of this chat's files. Use python_exec once: name that \
        file in the files argument of the call so it is copied into the input folder, read \
        it from there with the standard library only (no pandas), print the sum of the \
        total column, and write that sum into a file called summary.txt in the output \
        folder. Then tell me the sum in your reply.";
    // Twelve rows the model is not told, so the number can only come from the file.
    let rows: Vec<(u32, u32)> = (1..=12).map(|i| (i, i * i * 7 + 13)).collect();
    let total: u32 = rows.iter().map(|(_, t)| t).sum();
    let table = std::iter::once("month,total".to_string())
        .chain(rows.iter().map(|(m, t)| format!("2026-{m:02},{t}")))
        .collect::<Vec<_>>()
        .join("\n");

    let mut cfg = no_auto_cfg();
    cfg.tools.python_enabled = true;
    cfg.tools.python_mode = crate::shared::config::PythonMode::Local;
    cfg.default_sampling.max_tokens = Some(4096);
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg).or_else(|| {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        None
    }) else {
        return;
    };
    narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec![crate::features::tools::PYTHON_EXEC_ID.into()],
    )
    .await;

    let source = tempfile::tempdir().unwrap();
    let path = source.path().join("sales.csv");
    std::fs::write(&path, &table).unwrap();
    cmd_tx
        .send(AppCommand::FileAttach {
            path: path.display().to_string(),
        })
        .unwrap();
    wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::FileProgress(FileProgress::Attached { .. })
                | AppEvent::FileProgress(FileProgress::Failed(_))
        )
    })
    .await;

    let (reply, results) = python_turn(&cmd_tx, &mut evt_rx, ASK).await;
    cmd_tx.send(AppCommand::FileList).unwrap();
    let listed = wait_for(&mut evt_rx, |e| {
        matches!(e, AppEvent::FileProgress(FileProgress::Listed { .. }))
    })
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    assert!(
        !results.iter().any(|c| c.result.contains("nothing was run")),
        "the call was refused: {results:?}"
    );
    // The number is the file's, and the interpreter is what computed it.
    assert!(
        results
            .iter()
            .any(|c| c.result.contains(&total.to_string())),
        "the call never printed the total {total}: {results:?}"
    );
    assert!(
        fold_dashes(&reply).contains(&total.to_string()),
        "the reply does not carry the total {total}: {reply}"
    );
    // And the way out works on the host too: what the code wrote was stored with the chat.
    let Some(AppEvent::FileProgress(FileProgress::Listed { stored, .. })) = listed else {
        panic!("no /file list reply");
    };
    let kept = stored
        .iter()
        .find(|f| f.name.starts_with("summary"))
        .unwrap_or_else(|| panic!("nothing was kept from the output folder: {stored:?}"));
    assert!(!kept.missing, "the listed file is not in the folder");
    assert!(
        dir.path().join("files").exists(),
        "the chat's folder was never created"
    );
}

/// Fork F8a live (§12 T9, T14): a **binary** the user attaches is kept with the chat —
/// `/file attach` no longer refuses it — and a call that names it reads the real bytes in
/// `/w/in`. The fixture is a generated 512×512 PNG, so what the code reports is objective
/// and no pretrained knowledge can answer it; pillow is in the sandbox's starter set.
///
/// `#[ignore]`, manual against a live stack.
#[tokio::test]
#[ignore = "requires a live model (MINDFORK_ENGINE_URL) and a provisioned sandbox (MINDFORK_SANDBOX_DIR)"]
async fn attached_binary_reaches_the_sandbox_live() {
    use crate::features::file_command::FileProgress;
    const ASK: &str = "A picture is one of this chat's files. Use python_exec: name it in \
        the files argument so it is copied into /w/in, open it there with PIL (pillow) and \
        print its size in pixels. Then tell me the width and the height.";

    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_python_chat().await else {
        return;
    };
    let picture = tempfile::tempdir().unwrap();
    let path = picture.path().join("figure.png");
    std::fs::write(&path, figure_png([200, 30, 30])).unwrap();
    cmd_tx
        .send(AppCommand::FileAttach {
            path: path.display().to_string(),
        })
        .unwrap();
    let kept = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::FileProgress(FileProgress::StoredFile { .. })
                | AppEvent::FileProgress(FileProgress::Failed(_))
        )
    })
    .await;
    // A binary is kept, not refused: that is the half of F8a the model never sees.
    let AppEvent::FileProgress(FileProgress::StoredFile { name, mime, .. }) = kept.unwrap() else {
        panic!("the picture was refused instead of kept");
    };
    assert_eq!(name, "figure.png");
    assert_eq!(mime, "image/png");

    let (reply, results) = python_turn(&cmd_tx, &mut evt_rx, ASK).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    assert!(
        !results.iter().any(|c| c.result.contains("nothing was run")),
        "the call was refused: {results:?}"
    );
    assert!(
        results.iter().any(|c| c.result.contains("512")),
        "the code never read the picture's real size: {results:?}"
    );
    assert!(
        reply.contains("512"),
        "the reply does not carry the size the code read: {reply}"
    );
}

/// Fork F12, with a real model: a number the pinned block gave stays that file for the
/// **whole turn**, across a round that adds one.
///
/// The chat holds exactly one item — an image, `#1`. The turn asks for two `python_exec`
/// calls: the first saves a file, which lands in the chat's files and, in a freshly
/// derived list, sorts **ahead** of every image; the second names `#1` and prints the
/// first bytes of what arrived in `/w/in`.
///
/// The two answers cannot be confused: the image begins with the PNG signature, the file
/// the first call wrote begins with `ROUND-ONE`. Before this fix the second call was
/// handed the file the turn had just created, under the number the model had been given
/// for the picture — silently, with no refusal to notice.
///
/// `#[ignore]`, manual against a live stack.
#[tokio::test]
#[ignore = "requires a live model (MINDFORK_ENGINE_URL) and a provisioned sandbox (MINDFORK_SANDBOX_DIR)"]
async fn a_handle_survives_a_round_that_adds_a_file_live() {
    // Two calls in two **rounds**, which is the whole point: the list is reconciled
    // between rounds, so two calls issued together never reach the defect. Call 2 needs a
    // value only call 1 can produce, so it cannot be written until call 1 has answered.
    const ASK: &str = "Do this in two separate python_exec calls, the second written only \
        after you have seen the first one's output. \
        Call 1: write the text ROUND-ONE into /w/out/marker.txt, then print \
        hashlib.sha256(b'ROUND-ONE').hexdigest(). \
        Call 2: put #1 in the files argument, open the file that appears in /w/in in \
        binary mode, and print its first 8 bytes with repr() followed by the hex digest \
        call 1 printed. Then tell me what call 2 printed.";

    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_python_chat().await else {
        return;
    };
    let picture = tempfile::tempdir().unwrap();
    let path = picture.path().join("figure.png");
    std::fs::write(&path, figure_png([30, 120, 60])).unwrap();
    attach_image_live(&cmd_tx, &mut evt_rx, path.to_string_lossy().into_owned()).await;

    let (reply, results) = python_turn(&cmd_tx, &mut evt_rx, ASK).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Said rather than assumed: a turn that made only one call proves nothing either way,
    // and would otherwise pass on the half that never ran.
    assert!(
        results.len() >= 2,
        "the turn had to make two python_exec calls, it made {}: {results:?}",
        results.len()
    );
    // The **second** call, and only it: the first legitimately carries `ROUND-ONE`,
    // because a saved text file is listed with the head of its own content.
    let second = &results[1];
    // The handle has to be the number, not the name. A model that names `figure.png`
    // reaches the picture whatever the numbering does, so this smoke would then pass
    // against a broken list — measured, that is exactly what happened the first time.
    assert!(
        second.arguments.contains("#1"),
        "the second call has to name the handle, not the file: {}",
        second.arguments
    );
    assert!(
        second.result.contains("PNG"),
        "#1 had to stay the picture the block numbered; the call saw: {}",
        second.result
    );
    assert!(
        !second.result.contains("ROUND-ON"),
        "#1 was handed the file this very turn created: {}",
        second.result
    );
    assert!(
        reply.contains("PNG"),
        "the reply has to repeat what the second call printed: {reply}"
    );

    // The file the first call stored carries the mark of a download (§13 U11) — checked on
    // what a real turn wrote through the real store, not on a unit's fixture.
    #[cfg(windows)]
    {
        use crate::shared::os_open::{FROM_ELSEWHERE, zone_of};
        let marker = std::fs::read_dir(dir.path().join("files"))
            .expect("the chat's files folder")
            .filter_map(Result::ok)
            .map(|chat| chat.path().join("marker.txt"))
            .find(|path| path.is_file())
            .expect("the first call's marker.txt was stored");
        assert_eq!(
            zone_of(&marker).as_deref(),
            Some(FROM_ELSEWHERE),
            "{} carries no mark",
            marker.display()
        );
        println!("marked: {}", marker.display());
    }
    #[cfg(not(windows))]
    let _ = dir;
}

/// i18n Tier 1 (docs/history/i18n.md, go/no-go): a profile with agent-scaffold language `En` —
/// the auto-title of an English conversation is English, with NO Cyrillic. A fresh profile
/// (its own texts, independent of the bootstrap profile's state), set it to En, create a
/// chat, run an English turn and auto-titling. `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn i18n_en_profile_title_e2e_live() {
    // Automatic titling off: this smoke exercises the **requested** path
    // (`AutoRenameChat`), and the trigger would race it with a second title
    // task after the turn (the trigger has its own smoke below).
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(no_auto_cfg()) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // A fresh profile with its own English texts (the bootstrap one is Russian).
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "English".into(),
            system_message: "You are a helpful assistant. Reply in English.".into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    // Scaffold language En (a fresh profile with no data → the change is allowed).
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                ..Default::default()
            }),
        })
        .unwrap();
    // A new chat under this profile (will become active).
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let act = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let chat_id = match act {
        AppEvent::ChatActivated { id, .. } => id,
        _ => unreachable!(),
    };
    // An English turn.
    let (reply, _) =
        run_turn_live(&cmd_tx, &mut evt_rx, "Tell me a fun fact about the Moon.").await;
    eprintln!("en reply: {:?}", reply.chars().take(80).collect::<String>());
    // Auto-title from the conversation (digest/system message — in the scaffold language).
    cmd_tx.send(AppCommand::AutoRenameChat(chat_id)).unwrap();
    let renamed = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatRenamed { .. }))
        .await
        .unwrap();
    let title = match renamed {
        AppEvent::ChatRenamed { title, .. } => title,
        _ => unreachable!(),
    };
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!("en auto-title: {title:?}");
    let has_cyr = title
        .chars()
        .any(|c| ('а'..='я').contains(&c) || ('А'..='Я').contains(&c));
    assert!(!title.trim().is_empty(), "empty title");
    assert!(
        !has_cyr,
        "the English conversation's title contains Cyrillic: {title:?}"
    );
}

/// Live smoke of the **automatic** titling trigger (spec §11.2), on the default
/// config: the first real reply renames the chat with no command from anyone —
/// the end-to-end path the track added, where the smoke above covers the
/// requested task. See docs/history/auto-chat-title.md.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn auto_title_first_reply_e2e_live() {
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let act = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let (chat_id, default_title) = match act {
        AppEvent::ChatActivated { id, title, .. } => (id, title),
        _ => unreachable!(),
    };
    // One short exchange; no `AutoRenameChat` anywhere in this test.
    let (reply, _) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Почему небо синее? Ответь одним предложением.",
    )
    .await;
    eprintln!("reply: {:?}", reply.chars().take(80).collect::<String>());
    let renamed = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatRenamed { .. }))
        .await
        .unwrap();
    let (id, title) = match renamed {
        AppEvent::ChatRenamed { id, title } => (id, title),
        _ => unreachable!(),
    };
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!("automatic title: {title:?} (was {default_title:?})");
    assert_eq!(id, chat_id, "the rename must be about the active chat");
    assert!(!title.trim().is_empty(), "empty automatic title");
    assert_ne!(
        title, default_title,
        "the default title must actually be replaced"
    );
}

/// Live i18n Tier 2 smoke (docs/history/i18n.md, group 2c): an en profile + `current_time`.
/// The subject is Tier 2b — **a tool result is localized in the profile's
/// language** — not the tool's formatting options.
///
/// `current_time` has two legitimate result shapes (`features/tools/datetime.rs`):
/// without a `format` argument it renders the localized label plus UTC
/// ("Local time: … / UTC: …"), and **with** one it renders only the strftime
/// string, which carries no scaffold text in any language. So the label can only
/// be asserted against a call that passed no `format`, and the tool description
/// openly advertises `format` — an assertion that demands the label of *every*
/// call fails whenever the model takes that option, i.e. for the wrong reason
/// (docs/lessons.md §2). The alternative is therefore removed rather than hoped
/// away (§9): only `current_time` is enabled, and both the system message and
/// the prompt ask for an argument-free call, with one explicit re-ask if the
/// model passes `format` anyway. The Cyrillic check still applies to every
/// shape — under a `ru` profile the label, and the bad-format error, are
/// Russian — and the label check applies to the argument-free calls, of which at
/// least one is required, so neither assertion can pass vacuously.
///
/// Needs no network/sandbox. Run:
/// `MINDFORK_ENGINE_URL=…/v1 cargo test utils_en_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn utils_en_e2e_live() {
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "English".into(),
            system_message: "You are a helpful assistant. Reply in English. \
                 When you call the current_time tool, call it with no arguments \
                 at all — never pass the format parameter."
                .into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                // Only the tool under test — nothing else can be reached for.
                enabled_tools: Some(vec!["current_time".to_string()]),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    /// True when the call passed no usable `format` — i.e. the result is the
    /// localized default shape. Unparsable arguments count as "format passed":
    /// the label cannot be claimed for a shape we could not determine.
    fn no_format(args: &str) -> bool {
        let t = args.trim();
        if t.is_empty() {
            return true;
        }
        match serde_json::from_str::<serde_json::Value>(t) {
            // Mirrors the tool's own reading of the argument (datetime.rs):
            // absent, non-string or blank all fall back to the default shape.
            Ok(v) => v
                .get("format")
                .and_then(|f| f.as_str())
                .is_none_or(|s| s.trim().is_empty()),
            Err(_) => false,
        }
    }
    let current_time = |calls: Vec<(String, String, String)>| -> Vec<(String, String)> {
        calls
            .into_iter()
            .filter(|(n, _, _)| n == "current_time")
            .map(|(_, a, r)| (a, r))
            .collect()
    };

    let (_t, calls) = run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        "What is the current date and time? Call the current_time tool \
         with no arguments (do not pass format).",
    )
    .await;
    let mut ct = current_time(calls);
    if !ct.iter().any(|(a, _)| no_format(a)) {
        // The model took the `format` option despite both instructions; ask once
        // more, as explicitly as the tool contract allows. Two refusals is a
        // model-behaviour failure worth seeing, not a flake to absorb.
        eprintln!("current_time called with format only: {ct:#?} — re-asking");
        let (_t2, calls2) = run_turn_capture_args(
            &cmd_tx,
            &mut evt_rx,
            "Call current_time once more with an empty argument object {}, \
             passing no format, and show me its raw output.",
        )
        .await;
        ct.extend(current_time(calls2));
    }
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!("utils calls: {ct:#?}");

    let has_cyr = |s: &str| {
        s.chars()
            .any(|c| ('а'..='я').contains(&c) || ('А'..='Я').contains(&c))
    };
    assert!(!ct.is_empty(), "expected a current_time call");
    // Holds for both shapes: the ru default label and the ru bad-format error are Cyrillic.
    for (a, r) in &ct {
        assert!(
            !has_cyr(r),
            "current_time result has cyrillic (args {a:?}): {r:?}"
        );
    }
    // The localized label exists only in the argument-free shape — require one.
    let plain: Vec<&(String, String)> = ct.iter().filter(|(a, _)| no_format(a)).collect();
    assert!(
        !plain.is_empty(),
        "the model passed `format` on every current_time call, so the localized \
         label was never rendered — the i18n claim is untested: {ct:#?}"
    );
    for (_, r) in plain {
        assert!(r.contains("Local time:"), "expected English label: {r:?}");
        assert!(r.contains("UTC:"), "expected the UTC line: {r:?}");
    }
}

/// Live i18n Tier 2 smoke (docs/history/i18n.md, group 2b): an en profile + RAG tools.
/// The model adds a fact (`rag_add`) and searches for it (`rag_search`); **the
/// rag-tool results must be in English** (no Cyrillic) — the 2b criterion
/// (tool results are localized). Needs a real embedder (`MINDFORK_EMBED_URL`).
/// Run:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test rag_en_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn rag_en_e2e_live() {
    use crate::features::tools::all_tool_ids;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "English".into(),
            system_message: "You are a helpful assistant. Reply in English.".into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                enabled_tools: Some(all_tool_ids()),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    let (_t1, calls1) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Add this fact to the knowledge base via rag_add: the capital of France is Paris.",
    )
    .await;
    let (_t2, calls2) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Now search the knowledge base via rag_search for: capital of France.",
    )
    .await;

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let all: Vec<(String, String)> = calls1.into_iter().chain(calls2).collect();
    eprintln!("rag calls: {all:#?}");
    let has_cyr = |s: &str| {
        s.chars()
            .any(|c| ('а'..='я').contains(&c) || ('А'..='Я').contains(&c))
    };
    // The rag-tool results are in English (the scaffold is translated, Tier 2 2b).
    for (n, r) in &all {
        if n == "rag_add" || n == "rag_search" {
            assert!(!has_cyr(r), "rag tool {n} result has cyrillic: {r:?}");
        }
    }
    assert!(
        all.iter().any(|(n, _)| n == "rag_add"),
        "expected rag_add call"
    );
    // English result markers (if the tool ran).
    assert!(
        all.iter()
            .any(|(n, r)| n == "rag_add" && r.contains("Chunks added")),
        "expected English rag_add result: {all:?}"
    );
}

/// End-to-end against a live model: with `send_followup_message` enabled the assistant
/// writes a **second message** as a separate bubble. Checks both the UI signal
/// (`AssistantContinue`) and the resulting chat structure (`Message.new_bubble`).
/// The model is unstable — the test is `#[ignore]`, run manually against Gemma/Qwen.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn followup_tool_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(Some(backend));
    let root = _d.path().to_path_buf();
    enable_all_tools(&cmd_tx, &mut evt_rx).await;
    cmd_tx
        .send(AppCommand::SendMessage(
            "Ответь короткой первой репликой-приветствием, затем ОБЯЗАТЕЛЬНО вызови \
             инструмент send_followup_message и напиши вторую реплику с интересным \
             фактом о космосе."
                .into(),
        ))
        .unwrap();

    let saw_continue = drain_until_finished(&mut evt_rx, |e| {
        matches!(e, AppEvent::AssistantContinue { .. })
    })
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let chat = reopened
        .json()
        .load_chats()
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    let new_bubbles = chat.messages.iter().filter(|m| m.new_bubble).count();
    eprintln!(
        "followup e2e: saw_continue={saw_continue}, new_bubble={new_bubbles}, \
         messages={}",
        chat.messages.len()
    );
    for (i, m) in chat.messages.iter().enumerate() {
        eprintln!(
            "  [{i}] {:?} new_bubble={} tools={:?} text={:?}",
            m.role,
            m.new_bubble,
            m.tool_calls.iter().map(|t| &t.name).collect::<Vec<_>>(),
            m.text.chars().take(60).collect::<String>()
        );
    }
    assert!(
        saw_continue && new_bubbles >= 1,
        "expected a second message as a separate bubble (followup)"
    );
}

/// End-to-end against a live model: with `rewrite_current_message` enabled the assistant
/// discards the reply it started and writes it anew; the discarded content goes into `Chat.deleted`.
/// Checks the UI signal (`AssistantRewrite`) and a non-empty deleted archive.
///
/// **The instruction is closed on purpose**, and the thinking is muted; the two
/// together took this smoke from flaky to clean, and each fixed a *different*
/// model's failure.
///
/// It used to ask the model to "demonstrate the tool strictly by steps, skipping
/// none: step 1 write X, step 2 (MANDATORY) call the tool, step 3 write Y" — an
/// invitation to *narrate the sequence*, which Gemma duly accepted: it answered
/// `"2+2=5\n<call:rewrite_current_message/>\n2+2=4"`, writing the call as prose in a
/// syntax no protocol here defines. No call, no archive, red test — and a red
/// dispatch of the live gate (run 31907378154). Measured **1 failure in 8** on the
/// local Gemma stand; reworded as a situation the tool answers, **0 in 30**.
///
/// Qwen 3.6 needed the second half. With thinking on it failed differently and more
/// rarely — 1 in 20, and not by narrating but by producing **nothing at all**: no
/// tool call and empty text, the whole turn spent in `reasoning_content` (the same
/// shape that once broke `simple_generation`). Muting thinking for this turn:
/// **0 in 20**. Nothing is lost by muting, because what this smoke asks is whether
/// a real model reaches for the tool and whether the effect lands; the control-tool
/// path *through* thinking stays covered live by [`followup_tool_e2e_live`], which
/// leaves it on (0 in 12 measured).
///
/// **Narrowing the profile to the one tool was tried and rejected — it made things
/// worse**, 7 failures in 20 on Qwen, all of them the empty turn above. The
/// "remove the alternative" rule (docs/lessons.md §9) is about a model satisfying
/// the request through a *different* tool; that is not this failure, and a
/// one-tool list does not prevent a model from spending the turn thinking.
///
/// The configuration above is **0 in 20 on each family** (2026-08-16,
/// `gemma-4-31B_q4_0-it` and `Qwen3.6-27B-Q4_K_M`).
///
/// The mechanism itself does not depend on this test: `rewrite_tool_discards_partial_
/// and_saves_it` pins it deterministically against a `MockBackend`, and asserts
/// strictly more (the exact history, the discarded text, its tool call). What is
/// live here is the one thing a mock cannot answer — whether a real model reaches
/// for the tool at all. `#[ignore]`, run against a live model.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn rewrite_tool_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(Some(backend));
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;
    // Thinking off for this turn. `resolve` is whole-config, so the profile's
    // sampling replaces the fixture's global one and has to carry its temperature
    // too. Commands share one channel and are applied in order, so this lands
    // before the message below.
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                default_sampling: Some(Some(crate::entities::sampling::SamplingConfig {
                    temperature: Some(0.1),
                    reasoning_budget: Some(0),
                    ..Default::default()
                })),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::SendMessage(
            "Черновик твоего ответа никуда не годится. ОБЯЗАТЕЛЬНО вызови \
             инструмент rewrite_current_message, чтобы отбросить начатый ответ и \
             написать его заново. Вопрос: сколько будет два плюс два?"
                .into(),
        ))
        .unwrap();

    let saw_rewrite = drain_until_finished(&mut evt_rx, |e| {
        matches!(e, AppEvent::AssistantRewrite { .. })
    })
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let chat = reopened
        .json()
        .load_chats()
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    eprintln!(
        "rewrite e2e: saw_rewrite={saw_rewrite}, deleted={}, messages={}",
        chat.deleted.len(),
        chat.messages.len()
    );
    // The tool names are half the diagnosis when this goes red: "the model never
    // called it" and "it called it and the effect did not land" are different
    // failures, and the text alone cannot tell them apart.
    for (i, m) in chat.messages.iter().enumerate() {
        eprintln!(
            "  msg[{i}] {:?} tools={:?} text={:?}",
            m.role,
            m.tool_calls.iter().map(|t| &t.name).collect::<Vec<_>>(),
            m.text.chars().take(60).collect::<String>()
        );
    }
    assert!(
        saw_rewrite && !chat.deleted.is_empty(),
        "expected a discarded (rewritten) reply in Chat.deleted"
    );
}

/// End-to-end SelfModel probe against a live model (two sessions, one profile):
/// 1) session 1 — tell it facts about the user and ask it to record them in the "self-model"
///    (expect calls to `update_self_model`/`update_user_model`, a write to the DB);
/// 2) session 2 (a new chat of the same profile) — ask "what do you remember about me";
///    the "self-model" is mixed into the system prompt → expect recall.
/// Model behavior is unstable — the test is `#[ignore]`, run manually; assert the
/// **mechanism** (the DB is populated), and print the recalled text for evaluation.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_model_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(Some(backend));
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // --- Session 1: tell it facts and ask it to record the self-model. ---
    let (s1_text, s1_tools) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Меня зовут Владимир, я пишу на Rust и не люблю многословие. \
         Запомни это: вызови update_user_model (черты, интересы) и update_self_model \
         (краткое описание себя и цель — помогать мне кратко и по делу).",
    )
    .await;
    eprintln!("session 1: tools={s1_tools:?}\ntext={s1_text:?}\n");

    // --- Session 2: a new chat of the same profile, check recall. ---
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let (s2_text, s2_tools) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Что ты обо мне помнишь и какие у тебя цели в общении со мной?",
    )
    .await;
    eprintln!("session 2: tools={s2_tools:?}\ntext={s2_text:?}\n");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Mechanism: after session 1 the profile's self-model is non-empty and saved to disk.
    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let model = reopened.db().self_model_get(pid).unwrap();
    eprintln!("self_model in DB: {model:#?}");
    let model = model.expect("expected a saved self-model after session 1");
    assert!(
        !model.is_empty(),
        "expected a non-empty self-model (the model should have called update_*)"
    );
    // At least one of the mutators was actually called.
    assert!(
        s1_tools
            .iter()
            .any(|t| t == "update_self_model" || t == "update_user_model"),
        "expected an update_self_model/update_user_model call in session 1"
    );
}

/// End-to-end observation probe: ask the model to record an observation via
/// `add_insight` — expect a self-note (@self) in the DB (the narrative moved into notes,
/// Tier 1 "narrative as notes"). `#[ignore]`, manual.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_model_insight_e2e_live() {
    use crate::features::tools::notes::SELF_NOTE_TAG;
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch(Some(backend));
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    let (text, tools) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Я заметил, что иногда прошу кратко, а иногда — подробно. \
         Зафиксируй это наблюдение в своих наблюдениях: вызови инструмент add_insight \
         с коротким описанием этого противоречия.",
    )
    .await;
    eprintln!("insight: tools={tools:?}\ntext={text:?}\n");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // An observation is a self-note (@self), not an entry in the model's blob.
    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let self_notes = reopened
        .db()
        .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    eprintln!("self-notes (observations) in DB: {self_notes:#?}");
    assert!(
        !self_notes.is_empty(),
        "expected at least one self-note (@self) — an observation from add_insight"
    );
    assert!(
        tools.iter().any(|t| t == "add_insight"),
        "expected an add_insight call"
    );
}

/// End-to-end auto-reflection (Tier 3) against a live model: `auto_reflect_every=1` →
/// after the very first assistant reply, reflection kicks off in the background, and it itself
/// updates the "self-model". Reflection is silent (no UI event) — poll the DB, waiting
/// for data to appear. `#[ignore]`, manual.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn auto_reflect_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let mut config = AppConfig::default();
    config.self_model.auto_reflect_every = 1; // reflection after every reply
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch_cfg(Some(backend), config);
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // A regular send: tell it facts, the assistant replies (and then background
    // reflection is expected to record the "self-model" on its own).
    let (_t, _tools) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Привет! Меня зовут Владимир, пишу на Rust и ценю краткость. Просто ответь \
         коротким приветствием.",
    )
    .await;

    // Wait for background reflection to write something (poll the DB up to ~60s): the model's
    // blob (summary/goals/interlocutor) OR an observation as a self-note (@self, Tier 1).
    use crate::features::tools::notes::SELF_NOTE_TAG;
    let mut model = None;
    let mut self_notes = Vec::new();
    for _ in 0..120 {
        let db = Storage::open(Paths::with_root(&root)).unwrap();
        let m = db.db().self_model_get(pid).unwrap();
        self_notes = db
            .db()
            .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
            .unwrap();
        let blob_nonempty = m.as_ref().map(|m| !m.is_empty()).unwrap_or(false);
        if blob_nonempty || !self_notes.is_empty() {
            model = m;
            break;
        }
        drop(db);
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    }

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!("auto-reflect: self_model={model:#?}\nself-notes={self_notes:#?}");
    let blob_nonempty = model.as_ref().map(|m| !m.is_empty()).unwrap_or(false);
    assert!(
        blob_nonempty || !self_notes.is_empty(),
        "expected background auto-reflection to populate the self-model (a blob or an observation note)"
    );
}

/// End-to-end "self-model" auto-consolidation (stage A1) against a live model:
/// `auto_consolidate_every=1` → after a reply, once observations (`@self`) reach ≥ 2, the
/// self-model's "sleep" kicks off in the background, itself merging duplicate observations (`note_merge`/
/// `note_supersede`) and/or compressing a bloated description. "Sleep" is silent (no UI event) —
/// observe the result by polling the DB. Assert the **mechanism** (observations get created); the fact
/// of duplicates being merged is printed for go/no-go (behavior is unstable). `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test self_consolidation_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_consolidation_e2e_live() {
    use crate::features::tools::notes::SELF_NOTE_TAG;
    let mut config = AppConfig::default();
    config.self_model.auto_consolidate_every = 1; // "sleep" after every reply
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // Assemble the orchestrator with "sleep" enabled + (if possible) a real embedder.
    let embedder = live_embedder();
    let dir = tempfile::tempdir().unwrap();
    let storage = Arc::new(Storage::open(Paths::with_root(dir.path())).unwrap());
    let (cmd_tx, cmd_rx) = unbounded_channel();
    let (evt_tx, mut evt_rx) = unbounded_channel();
    let deps = OrchestratorDeps {
        cmd_rx,
        evt_tx,
        storage,
        config,
        supervisor: Arc::new(MockSupervisor::with_backend_and_embedder(
            Some(backend),
            embedder,
        )),
        default_language: crate::shared::i18n::Lang::default(),
        extra_tools: Vec::new(),
    };
    let handle = tokio::spawn(run(deps));
    let root = dir.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Two similar observations (duplicate candidates) — record them, without merging yet.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши наблюдение (add_insight): я ценю краткость в ответах. Просто запиши.",
    )
    .await;
    let (_t2, tools2) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши ещё одно наблюдение (add_insight): пользователь предпочитает лаконичные, \
         краткие ответы. Просто запиши, оба оставь.",
    )
    .await;
    eprintln!("observations: {tools1:?} + {tools2:?}");

    // After the second reply, observations ≥ 2 → the self-model's background "sleep" should
    // kick off and possibly merge duplicates. Poll the DB up to ~90s: count self-notes.
    let count_self = |root: &std::path::Path| -> usize {
        Storage::open(Paths::with_root(root))
            .unwrap()
            .db()
            .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
            .unwrap()
            .len()
    };
    // Wait for ≥2 observations (both entries have landed), then watch for "sleep" merging them.
    let mut before = 0usize;
    for _ in 0..180 {
        before = count_self(&root);
        if before >= 2 {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    }
    // Trigger one more turn (in case "sleep" after turn2 missed due to cadence):
    // every reply increments the counter, every=1 → "sleep" is attempted again.
    let _ = run_turn_live(&cmd_tx, &mut evt_rx, "Спасибо, коротко подтверди.").await;
    let mut after = before;
    for _ in 0..180 {
        after = count_self(&root);
        if after < before {
            break; // duplicates merged by "sleep"
        }
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    }

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!(
        "self-notes: before={before}, after={after} (sleep merged duplicates: {})",
        after < before
    );
    // Mechanism: observation notes are created (add_insight ran).
    assert!(
        before >= 1,
        "expected at least one observation (@self) from add_insight"
    );
    let _ = (tools1, tools2);
}

/// End-to-end **gate** probe (the core of the Tier 1 "narrative as notes" hypothesis): the model
/// records an observation (`add_insight` → a self-note @self), then a near-duplicate —
/// the `add_insight` gate shows the similar existing observation with a hint to
/// rewrite it via `note_revise`/`note_supersede` instead of a copy. Assert the
/// **mechanism** (self-notes get created; the gate fires deterministically —
/// the embedder in tests is `MockEmbedder`, observation #1 already exists); the model's
/// **decision** to integrate is printed for go/no-go (behavior is unstable). Run (needs
/// a live server + possibly an embedder):
/// `MINDFORK_ENGINE_URL=…/v1 cargo test self_model_gate_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_model_gate_e2e_live() {
    use crate::features::tools::notes::SELF_NOTE_TAG;
    // A real chat + embedder (MINDFORK_ENGINE_URL / MINDFORK_EMBED_URL) — the gate
    // works on real embeddings (bge-m3 etc.), not on MockEmbedder.
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Session 1: record an observation → self-note #1.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши в свои наблюдения (вызови add_insight): я склонен просить краткие ответы.",
    )
    .await;
    eprintln!("session 1: tools={tools1:?}");

    // Session 2: a near-duplicate — the add_insight gate should show observation #1.
    let (t2, calls2) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Запиши ещё одно, очень похожее наблюдение (вызови add_insight): пользователь \
         предпочитает лаконичные, краткие ответы. Если инструмент покажет похожее \
         наблюдение — реши сам, переписать ли его (note_revise/note_supersede) или \
         оставить оба.",
    )
    .await;
    eprintln!("session 2: text={t2:?}\ncalls={calls2:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Mechanism: add_insight in session 1 created a self-note (@self).
    assert!(
        tools1.iter().any(|t| t == "add_insight"),
        "session 1: expected an add_insight call"
    );
    let self_notes = Storage::open(Paths::with_root(&root))
        .unwrap()
        .db()
        .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    eprintln!(
        "self-notes in DB: {}{:#?}",
        self_notes.len(),
        self_notes
            .iter()
            .map(|n| n.content.clone())
            .collect::<Vec<_>>()
    );
    assert!(
        !self_notes.is_empty(),
        "expected self-notes (@self) from add_insight"
    );

    // Gate: did the add_insight result in session 2 show the similar observation?
    let gate_fired = calls2
        .iter()
        .any(|(n, r)| n == "add_insight" && r.contains("Похожие наблюдения"));
    // With the test MockEmbedder (MINDFORK_EMBED_URL unset), the gate is deterministic:
    // if the model called add_insight, it MUST fire (observation #1 already exists).
    // With a real embedder, firing depends on its configuration (e.g. llama-server
    // needs `--embeddings`), and on unavailability the gate gracefully degrades to empty —
    // so there it is only diagnostic, not a hard check.
    let real_embedder = std::env::var("MINDFORK_EMBED_URL").is_ok();
    if !real_embedder && calls2.iter().any(|(n, _)| n == "add_insight") {
        assert!(
            gate_fired,
            "the add_insight gate should have shown the similar observation (MockEmbedder, core of the hypothesis): {calls2:?}"
        );
    }
    // Integrating the near-duplicate: rewrite/replace/merge observations.
    let integrated = calls2
        .iter()
        .any(|(n, _)| n == "note_revise" || n == "note_supersede" || n == "note_merge");
    eprintln!(
        "gate showed a similar observation: {gate_fired} (real embedder: {real_embedder}); \
         model integrated (note_revise/supersede/merge): {integrated}"
    );
    // The model must have touched the observations somehow (otherwise the hypothesis isn't tested).
    assert!(
        calls2.iter().any(|(n, _)| {
            n == "add_insight" || n == "note_revise" || n == "note_supersede" || n == "note_merge"
        }),
        "session 2: expected add_insight/note_revise/note_supersede/note_merge"
    );
}

/// Live i18n Tier 2 smoke (docs/history/i18n.md, group 2a): the en mirror of
/// [`self_model_gate_e2e_live`]. A profile with scaffold language `En` + all tools; a turn
/// in English records an observation, a near-duplicate raises the `add_insight` gate —
/// **both the gate text and the whole tool result must be in English** (the key
/// Tier 2 criterion: tool results are localized, no Cyrillic). A real
/// embedder (`MINDFORK_EMBED_URL`) is needed for the gate to fire. Run:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test self_model_gate_en_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_model_gate_en_e2e_live() {
    use crate::features::tools::all_tool_ids;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // A fresh profile with its own English texts → set scaffold language En.
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "English".into(),
            system_message: "You are a helpful assistant. Reply in English.".into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                enabled_tools: Some(all_tool_ids()),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Turn 1: record an observation (add_insight → a self-note).
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Record an observation about me (call add_insight): I tend to ask for concise answers.",
    )
    .await;
    eprintln!("turn 1 tools: {tools1:?}");

    // Turn 2: a near-duplicate — the add_insight gate shows observation #1 (in English).
    let (t2, calls2) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Record another very similar observation (call add_insight): the user prefers brief, \
         concise replies. If the tool shows a similar observation, decide yourself whether to \
         rewrite it (note_revise/note_supersede) or keep both.",
    )
    .await;
    eprintln!("turn 2 text={t2:?}\ncalls={calls2:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    assert!(
        tools1.iter().any(|t| t == "add_insight"),
        "turn 1: expected add_insight call"
    );
    // Tool results contain no Cyrillic (the scaffold is translated, Tier 2).
    let has_cyr = |s: &str| {
        s.chars()
            .any(|c| ('а'..='я').contains(&c) || ('А'..='Я').contains(&c))
    };
    for (n, r) in &calls2 {
        assert!(!has_cyr(r), "tool {n} result has cyrillic: {r:?}");
    }
    // Gate (real embedder): text — the English template "Similar observations …".
    let gate_fired = calls2
        .iter()
        .any(|(n, r)| n == "add_insight" && r.contains("Similar observations"));
    let real_embedder = std::env::var("MINDFORK_EMBED_URL").is_ok();
    eprintln!("gate fired (English): {gate_fired}; real embedder: {real_embedder}");
    if real_embedder && calls2.iter().any(|(n, _)| n == "add_insight") {
        assert!(
            gate_fired,
            "en gate should have surfaced a similar observation in English: {calls2:?}"
        );
    }
}

/// End-to-end probe of the **summary-size gate** (stage 2, docs/summary-as-snapshot.md):
/// seed the DB with a bloated self-description (beyond the default target of 1000 chars);
/// the model sees a hint to shrink it, both in the passive injection and in `get_self_model`.
/// Ask it to read the self-model and shrink the description, moving event-like content into observations.
/// Assert the **mechanism** (the model touched the self-model: `update_self_model` and/or
/// `add_insight`); the actual shrinkage is printed for go/no-go (behavior is unstable).
/// `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 cargo test summary_gate_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn summary_gate_e2e_live() {
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Seed a bloated self-description (beyond the default target of 1000 chars).
    let bloated = "Я ассистент, ценю честность и точность. ".repeat(40); // ~1600 chars.
    let bloated_len = bloated.chars().count();
    {
        let storage = Storage::open(Paths::with_root(&root)).unwrap();
        let mut m = crate::entities::self_model::SelfModel::new(pid);
        m.summary = bloated.clone();
        storage.db().self_model_upsert(&m).unwrap();
    }

    // Turn: ask it to read the self-model and shrink the description.
    let (t, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Прочитай свою «модель себя» (вызови get_self_model). Если описание себя \
         разрослось — сократи его до сути через update_self_model.summary, а событийные \
         выводы вынеси в наблюдения (add_insight).",
    )
    .await;
    eprintln!("text={t:?}\ncalls={calls:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Gate: the get_self_model result showed the hint (deterministic — summary
    // is bloated beyond the target), if the model called it.
    let gate_fired = calls
        .iter()
        .any(|(n, r)| n == "get_self_model" && r.contains("Описание себя разрослось"));
    if calls.iter().any(|(n, _)| n == "get_self_model") {
        assert!(
            gate_fired,
            "get_self_model with a bloated description should carry a hint to shrink it: {calls:?}"
        );
    }
    // Final description size in the DB.
    let stored = Storage::open(Paths::with_root(&root))
        .unwrap()
        .db()
        .self_model_get(pid)
        .unwrap();
    let final_len = stored
        .as_ref()
        .map(|m| m.summary.chars().count())
        .unwrap_or(0);
    let shrank = final_len < bloated_len;
    let wrote_insight = calls.iter().any(|(n, _)| n == "add_insight");
    eprintln!(
        "gate showed the hint: {gate_fired}; description {bloated_len}{final_len} \
         (shrank: {shrank}); moved into observations: {wrote_insight}"
    );
    // The model must have touched the self-model somehow (otherwise the hypothesis isn't tested).
    assert!(
        calls
            .iter()
            .any(|(n, _)| n == "update_self_model" || n == "add_insight"),
        "expected update_self_model/add_insight: {calls:?}"
    );
}

/// End-to-end probe of the **graph over observations** (Tier 2, step B): the model records two
/// related observations, then links them (`note_link`). Assert the mechanism
/// (observation notes get created; the model inspected the self-model / linked); the appearance
/// of a link in the graph is printed for go/no-go (behavior is unstable). Relevance-based
/// injection is checked deterministically (`injection_recent_surfaces_relevant_over_fresh`)
/// + a manual multi-session user run. `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test self_model_graph_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_model_graph_e2e_live() {
    use crate::features::tools::notes::SELF_NOTE_TAG;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Two related (contradicting) observations.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши наблюдение (add_insight): я ценю краткость в ответах.",
    )
    .await;
    let (_t2, tools2) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши ещё одно наблюдение (add_insight): но иногда я даю слишком многословные ответы.",
    )
    .await;
    eprintln!("observations: {tools1:?} + {tools2:?}");

    // Ask it to inspect the self-model and link the contradicting observations.
    let (t3, calls3) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Посмотри свои наблюдения (get_self_model). Если два из них противоречат друг \
         другу — свяжи их инструментом note_link (relation=contradicts) по полному id.",
    )
    .await;
    eprintln!("linking: text={t3:?}\ncalls={calls3:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let self_notes = reopened
        .db()
        .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    let links = reopened.db().note_links_all(pid).unwrap();
    eprintln!(
        "self-notes: {}; links in the observation graph: {}{links:?}",
        self_notes.len(),
        links.len()
    );

    // Mechanism: observation notes are created.
    assert!(self_notes.len() >= 2, "expected ≥2 observation notes");
    let linked = calls3.iter().any(|(n, _)| n == "note_link");
    eprintln!(
        "model called note_link: {linked}; links now: {}",
        links.len()
    );
    // The model must have inspected itself and/or linked (otherwise the graph isn't tested).
    assert!(
        calls3
            .iter()
            .any(|(n, _)| n == "get_self_model" || n == "note_link"),
        "session 3: expected get_self_model/note_link"
    );
}

/// End-to-end probe of the **self-consolidation overview** (Tier 3, deferred from Tier 2 B):
/// the model records two similar observations, then calls `reflect` — its result
/// now carries a block "Overview of observations for consolidation" (similar pairs / contradicts /
/// no links), under which the model merges duplicates (`note_merge`/`note_supersede`/
/// `note_revise`). Assert the **mechanism** (≥1 observation note; when `reflect` is called
/// with ≥2 observations, its result contains the overview — deterministic, the header/counts
/// are built without vectors and don't depend on the embedder threshold); the actual merging of duplicates
/// is printed for go/no-go (behavior is unstable). `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test self_consolidation_overview_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn self_consolidation_overview_e2e_live() {
    use crate::features::tools::notes::SELF_NOTE_TAG;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Two similar observations (duplicate candidates) — do NOT merge them yet.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши наблюдение (add_insight): я ценю краткость в ответах. Пока не объединяй \
         ни с чем — просто запиши.",
    )
    .await;
    let (_t2, tools2) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши ещё одно наблюдение (add_insight): пользователь предпочитает лаконичные, \
         краткие ответы. Даже если инструмент покажет похожее — на этот раз оставь оба.",
    )
    .await;
    eprintln!("observations: {tools1:?} + {tools2:?}");

    // Ask it to reflect and merge duplicates — reflect carries the self-consolidation overview.
    let (t3, calls3) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Вызови reflect и просмотри блок «Обзор наблюдений для консолидации». Если среди \
         наблюдений есть похожие дубли — сведи их (note_merge или note_supersede).",
    )
    .await;
    eprintln!("reflection: text={t3:?}\ncalls={calls3:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let self_notes = reopened
        .db()
        .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    eprintln!(
        "self-notes in DB: {}{:#?}",
        self_notes.len(),
        self_notes
            .iter()
            .map(|n| n.content.clone())
            .collect::<Vec<_>>()
    );
    // Mechanism: observation notes are created.
    assert!(
        !self_notes.is_empty(),
        "expected self-notes (@self) from add_insight"
    );

    // Self-consolidation overview: if the model called reflect and there are ≥2 observations, its
    // result MUST carry the "Overview of observations" block (deterministic — the header and
    // counts are built without vectors, the embedder threshold only affects the list of similar pairs).
    // If the model already merged duplicates in session 2, observations < 2 and there's no overview — fine.
    if let Some((_, result)) = calls3.iter().find(|(n, _)| n == "reflect") {
        let has_overview = result.contains("Обзор наблюдений");
        eprintln!("reflect returned the self-consolidation overview: {has_overview}");
        if self_notes.len() >= 2 {
            assert!(
                has_overview,
                "reflect with ≥2 observations should carry the self-consolidation overview: {result}"
            );
        }
    }
    // Merging duplicates — go/no-go (unstable): print it.
    let consolidated = calls3
        .iter()
        .any(|(n, _)| n == "note_merge" || n == "note_supersede" || n == "note_revise");
    eprintln!("model merged duplicates (note_merge/supersede/revise): {consolidated}");
    // The model must have reflected and/or touched the observations.
    assert!(
        calls3.iter().any(|(n, _)| {
            n == "reflect" || n == "note_merge" || n == "note_supersede" || n == "note_revise"
        }),
        "session 3: expected reflect/note_merge/note_supersede/note_revise: {calls3:?}"
    );
}

/// End-to-end probe of the **related-traits gate** for `user_model` (Tier 2, step C): the model
/// adds an interlocutor trait (`update_user_model` with `add_traits`), then a close one —
/// the `add_traits` gate shows the related trait and asks it to decide (duplicate/contradiction).
/// Assert the **mechanism** (the trait is recorded in `perceived_traits`; the second session
/// again calls `update_user_model`); the gate firing and the model's decision are printed for
/// go/no-go. **Gate threshold 0.72** (calibrated on bge-m3, unlike the threshold-free
/// `add_insight` gate), so on a real embedder firing depends on how close the
/// model-generated phrasings are — here this is diagnostic, not a hard check.
/// `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test trait_gate_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn trait_gate_e2e_live() {
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Session 1: add an interlocutor trait → user_model.perceived_traits.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Обнови модель собеседника (вызови update_user_model): добавь черту (add_traits) \
         — «ценит краткость в ответах».",
    )
    .await;
    eprintln!("session 1: tools={tools1:?}");

    // Session 2: a very similar trait — the add_traits gate should warn about a duplicate.
    let (t2, calls2) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Обнови модель собеседника ещё раз (update_user_model): добавь очень похожую \
         черту (add_traits) — «любит лаконичность». Если инструмент предупредит о \
         почти-дубле — реши сам, объединить ли их через remove_traits.",
    )
    .await;
    eprintln!("session 2: text={t2:?}\ncalls={calls2:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Mechanism: update_user_model in session 1 recorded a trait.
    assert!(
        tools1.iter().any(|t| t == "update_user_model"),
        "session 1: expected an update_user_model call"
    );
    let stored = Storage::open(Paths::with_root(&root))
        .unwrap()
        .db()
        .self_model_get(pid)
        .unwrap();
    let traits = stored
        .as_ref()
        .map(|m| m.user_model.perceived_traits.clone())
        .unwrap_or_default();
    eprintln!("interlocutor traits in DB: {traits:?}");
    assert!(
        !traits.is_empty(),
        "expected ≥1 trait in user_model from update_user_model"
    );

    // Gate: did the update_user_model result in session 2 show the related trait?
    // (`remove_traits` is an argument of update_user_model, not a separate tool name,
    // so integration is read from the final DB state, not the call name.)
    let gate_fired = calls2
        .iter()
        .any(|(n, r)| n == "update_user_model" && r.contains("Родственные черты"));
    // Integrating the near-duplicate: the model merged the paraphrases into one trait (didn't keep both).
    let integrated = traits.len() <= 1;
    eprintln!(
        "gate warned about a related trait: {gate_fired}; \
         model merged them into one trait (no paraphrase pile-up): {integrated}"
    );

    // The model must have touched the interlocutor model again (otherwise the gate isn't tested).
    assert!(
        calls2.iter().any(|(n, _)| n == "update_user_model"),
        "session 2: expected update_user_model"
    );
}

/// End-to-end probe of **cross-organ links** (Tier 3, Path 1): the model records a fact
/// "about the interlocutor" (`note_save`) and an observation "about itself" (`add_insight`), then links
/// them (`note_link`) — an edge between memory organs. Assert the **mechanism** (both notes
/// get created; the model inspected both organs and/or linked); the appearance of a **cross-organ
/// edge** (one end `@self`, the other a user note) is printed for
/// go/no-go. `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test cross_organ_link_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn cross_organ_link_e2e_live() {
    use crate::features::tools::notes::{SELF_NOTE_TAG, is_self_note};
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // A fact "about the interlocutor" → a user note.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши заметку о собеседнике (note_save): пользователь ценит краткость в ответах.",
    )
    .await;
    // An observation "about itself" → a self-note.
    let (_t2, tools2) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши наблюдение о себе (add_insight): я склонен давать многословные ответы.",
    )
    .await;
    eprintln!("saving: {tools1:?} + {tools2:?}");

    // Ask it to link the observation "about itself" with the fact "about the interlocutor" (cross-organ).
    let (t3, calls3) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Посмотри свои наблюдения (get_self_model) и заметки о собеседнике (note_recall). \
         Если наблюдение о себе противоречит факту о собеседнике — свяжи их note_link \
         (relation=contradicts) по их id.",
    )
    .await;
    eprintln!("linking: text={t3:?}\ncalls={calls3:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    let self_notes = reopened
        .db()
        .note_list(pid, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    let links = reopened.db().note_links_all(pid).unwrap();
    // A cross-organ edge: one end @self, the other a user note.
    let cross = links
        .iter()
        .filter(|(f, t, _)| {
            let fs = reopened
                .db()
                .note_get(pid, *f)
                .ok()
                .flatten()
                .as_ref()
                .map(is_self_note);
            let ts = reopened
                .db()
                .note_get(pid, *t)
                .ok()
                .flatten()
                .as_ref()
                .map(is_self_note);
            matches!((fs, ts), (Some(a), Some(b)) if a != b)
        })
        .count();
    eprintln!(
        "self-notes: {}; links total: {}; cross-organ: {cross}{links:?}",
        self_notes.len(),
        links.len()
    );

    // Mechanism: both notes are created (an observation @self + a user note).
    assert!(
        !self_notes.is_empty(),
        "expected a self-note from add_insight"
    );
    let all = reopened.db().note_list(pid, None, &[], None).unwrap();
    assert!(
        all.iter().any(|n| !is_self_note(n)),
        "expected a user note from note_save"
    );
    let linked = calls3.iter().any(|(n, _)| n == "note_link");
    eprintln!("model called note_link: {linked}; cross-organ edges: {cross}");
    // The model must have inspected the organs and/or linked (otherwise the cross-link isn't tested).
    assert!(
        calls3
            .iter()
            .any(|(n, _)| n == "get_self_model" || n == "note_recall" || n == "note_link"),
        "session 3: expected get_self_model/note_recall/note_link"
    );
}

/// End-to-end probe of **output mixing** (Tier 3, Path 2): with the toggle
/// `notes.recall_includes_self` on, observations "about itself" (`@self`) are included in the general
/// `note_recall` with an "about self" marker. Assert the **mechanism** (the toggle is applied; the model
/// called `note_recall`); the appearance of a marked self-observation and the **model's reply**
/// (does it "contaminate" — go/no-go on mixing safety) are printed. `#[ignore]`,
/// manual: `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test recall_includes_self_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn recall_includes_self_e2e_live() {
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let _pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;
    // Enable output mixing (Path 2). MockSupervisor ignores server settings,
    // so the live backend/embedder stay in place; only recall_includes_self changes.
    let config = AppConfig {
        notes: crate::shared::config::NotesSettings {
            recall_includes_self: true,
            ..Default::default()
        },
        ..Default::default()
    };
    cmd_tx
        .send(AppCommand::UpdateConfig(Box::new(config)))
        .unwrap();
    wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::Settings { config, .. } if config.notes.recall_includes_self),
    )
    .await
    .unwrap();

    // A fact "about the interlocutor" + an observation "about itself".
    run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши заметку о собеседнике (note_save): пользователь любит краткость.",
    )
    .await;
    run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Запиши наблюдение о себе (add_insight): я склонен к многословию.",
    )
    .await;

    // Search: with the toggle on, note_recall should return both the note and the observation
    // "about itself" (with an "about self" marker).
    let (t3, calls3) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Поищи в заметках (note_recall) всё про краткость и многословие — что там есть?",
    )
    .await;
    eprintln!("recall: text={t3:?}\ncalls={calls3:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Mechanism: with the toggle on, note_recall returned the marked self-observation.
    let self_marked = calls3
        .iter()
        .any(|(n, r)| n == "note_recall" && r.contains("[о себе]"));
    eprintln!("note_recall showed the self-observation with its marker: {self_marked}");
    assert!(
        calls3.iter().any(|(n, _)| n == "note_recall"),
        "expected a note_recall call"
    );
}

/// End-to-end probe of **linking notes with RAG sources** (Tier 3, Path 3): the model
/// adds a document to the knowledge base, finds it (`rag_search`), records a conclusion
/// as a note (`note_save`) and links it to the source (`note_cite_source`). Assert
/// the **mechanism** (the source is in the base; the model searched/linked); the appearance of a note↔
/// source link in the DB is printed for go/no-go. `#[ignore]`, manual:
/// `MINDFORK_ENGINE_URL=…/v1 MINDFORK_EMBED_URL=…/v1 cargo test note_cite_source_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn note_cite_source_e2e_live() {
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let root = _d.path().to_path_buf();
    let pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;

    // Knowledge base: add a document under a named source.
    let (_t1, tools1) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        "Добавь в базу знаний (rag_add) текст «Столица Франции — Париж.» с источником «факты».",
    )
    .await;
    eprintln!("rag_add: {tools1:?}");

    // The model searches, records a conclusion, and links it to the source.
    let (t2, calls2) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Найди в базе знаний (rag_search) про столицу Франции. Запиши краткий вывод \
         заметкой (note_save), затем свяжи эту заметку с источником через \
         note_cite_source (источник называется «факты»).",
    )
    .await;
    eprintln!("citing: text={t2:?}\ncalls={calls2:#?}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(&root)).unwrap();
    // The source name both prompts above ask the model to use. Bound once so the
    // two lookups and the log cannot drift apart — and so the log line stays a
    // pure English label with the (Russian) source name interpolated as data.
    const SOURCE: &str = "факты";
    // Mechanism: the source is in the knowledge base.
    assert!(
        reopened.db().rag_source_exists(pid, SOURCE).unwrap(),
        "expected the source to be in the knowledge base"
    );
    // Note↔source link (the reverse path): notes citing the source.
    let citing = reopened.db().notes_citing_source(pid, SOURCE).unwrap();
    let cited = calls2.iter().any(|(n, _)| n == "note_cite_source");
    eprintln!(
        "model called note_cite_source: {cited}; notes citing {SOURCE:?}: {}{:?}",
        citing.len(),
        citing.iter().map(|n| n.content.clone()).collect::<Vec<_>>()
    );
    // The model must have searched and/or linked (otherwise the path isn't tested).
    assert!(
        calls2
            .iter()
            .any(|(n, _)| n == "rag_search" || n == "note_cite_source"),
        "session 2: expected rag_search/note_cite_source"
    );
}

/// **Calibration** smoke for the A2 threshold (`SUMMARY_OBS_SIMILARITY` in `overview.rs`): against a
/// REAL embedder (bge-m3 via `MINDFORK_EMBED_URL`) embeds labeled pairs
/// "self-description paragraph ↔ observation" and **prints** their cosines, so the reader can pick
/// a threshold from the numbers (like `TRAIT_SIMILARITY`). Doesn't assert a hard threshold — only that
/// paraphrases are on average closer than unrelated pairs. `#[ignore]`, manual:
/// `MINDFORK_EMBED_URL=…/v1 cargo test summary_obs_calibration_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running embedding server (MINDFORK_EMBED_URL)"]
async fn summary_obs_calibration_e2e_live() {
    use crate::features::tools::notes::cosine;
    let Some(embedder) = live_embedder() else {
        eprintln!("skip: MINDFORK_EMBED_URL not set");
        return;
    };
    // Paraphrases of the same self-fact (should-match).
    let should_match: &[(&str, &str)] = &[
        (
            "Я ценю ясность и краткость: предпочитаю давать сжатые, по существу ответы без воды.",
            "Замечаю за собой склонность отвечать лаконично и по делу, избегая многословия.",
        ),
        (
            "Мне важно быть честным даже когда это неудобно — точность выше угодливости.",
            "Стараюсь говорить правду прямо, не смягчая её ради того, чтобы понравиться.",
        ),
        (
            "Я склонен глубоко погружаться в задачу и доводить рассуждение до конца.",
            "Мне свойственно тщательно и до конца прорабатывать проблему, не бросая на полпути.",
        ),
    ];
    // A self-description paragraph vs an unrelated observation (should-NOT-match).
    let should_not: &[(&str, &str)] = &[
        (
            "Я ценю ясность и краткость в своих ответах, стремлюсь к сжатости изложения.",
            "Пользователь увлекается альпинизмом и любит длинные горные походы по выходным.",
        ),
        (
            "Мне важно быть честным даже когда это неудобно, точность важнее удобства.",
            "Собеседник программирует на Rust и предпочитает крепкий кофе по утрам.",
        ),
        (
            "Я склонен глубоко и вдумчиво погружаться в поставленную задачу.",
            "На выходных мы обсуждали рецепты домашней выпечки и уход за садом.",
        ),
    ];
    let mut match_sum = 0.0f32;
    for (a, b) in should_match {
        let v = embedder
            .embed(vec![a.to_string(), b.to_string()], EmbedRole::Passage)
            .await
            .unwrap();
        let c = cosine(&v[0], &v[1]);
        match_sum += c;
        eprintln!("MATCH?      cos={c:.2}  {a:?} ~ {b:?}");
    }
    let mut nonmatch_sum = 0.0f32;
    for (a, b) in should_not {
        let v = embedder
            .embed(vec![a.to_string(), b.to_string()], EmbedRole::Passage)
            .await
            .unwrap();
        let c = cosine(&v[0], &v[1]);
        nonmatch_sum += c;
        eprintln!("NON-MATCH?  cos={c:.2}  {a:?} ~ {b:?}");
    }
    let m = match_sum / should_match.len() as f32;
    let n = nonmatch_sum / should_not.len() as f32;
    eprintln!("mean: matches={m:.2}  non-matches={n:.2}  (the threshold sits between them)");
    assert!(
        m > n,
        "paraphrases should be on average closer than unrelated pairs: {m:.2} vs {n:.2}"
    );
}

/// End-to-end probe of A2 behavior: a self-description with a paragraph duplicating (in other words)
/// a stored observation (`@self`); against a REAL embedder
/// `summary_observation_overlaps` returns `Some` and names the observation. Prints
/// the measured cosine (for calibration). `#[ignore]`, manual:
/// `MINDFORK_EMBED_URL=…/v1 cargo test summary_obs_overlap_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running embedding server (MINDFORK_EMBED_URL)"]
async fn summary_obs_overlap_e2e_live() {
    use crate::entities::note::Note;
    use crate::entities::self_model::SelfModel;
    use crate::features::tools::notes::{SELF_NOTE_TAG, cosine, summary_observation_overlaps};
    let Some(embedder) = live_embedder() else {
        eprintln!("skip: MINDFORK_EMBED_URL not set");
        return;
    };
    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru);
    let dir = tempfile::tempdir().unwrap();
    let storage = Storage::open(Paths::with_root(dir.path())).unwrap();
    let profile = Uuid::new_v4();

    let para = "Я ценю ясность и краткость: предпочитаю давать сжатые, по существу ответы без лишней воды.";
    let obs_text = "Замечаю за собой склонность отвечать лаконично и по делу, избегая многословия.";
    let mut model = SelfModel::new(profile);
    model.summary = para.to_string();
    storage.db().self_model_upsert(&model).unwrap();

    // An observation (@self) with a real embedding in the DB.
    let obs = Note::new(profile, obs_text, vec![SELF_NOTE_TAG.to_string()]);
    storage.db().note_insert(&obs).unwrap();
    let ov = embedder
        .embed(vec![obs_text.to_string()], EmbedRole::Passage)
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    storage
        .db()
        .note_vector_upsert(obs.id, profile, &ov)
        .unwrap();

    // The measured cosine (diagnostic for calibrating the threshold).
    let pv = embedder
        .embed(vec![para.to_string()], EmbedRole::Passage)
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    eprintln!(
        "measured cos(summary paragraph, observation) = {:.2}",
        cosine(&pv, &ov)
    );

    let out = summary_observation_overlaps(&storage, embedder.as_ref(), profile, loc).await;
    eprintln!("summary↔observation section: {out:?}");
    let out = out.expect("expected a summary↔observation overlap section");
    assert!(
        out.contains(&obs.id.to_string()),
        "the section should name the observation (id): {out}"
    );
}

/// Live e2e for speech (spec §11.9): the `/tts` command on a chat with messages goes
/// **through the orchestrator** — message selection → speech extractor → chunks → synthesis at
/// the cloud provider → the playback queue. Checks the whole chain and that the
/// task finishes correctly (the chip goes dark). Needs a key **and a sound card**;
/// the actual sound — a manual check (the test only observes timings).
///
/// The provider is picked by which variable is set: `MINDFORK_OPENAI_KEY` (default)
/// or `MINDFORK_GEMINI_KEY`. Silently skipped without either.
#[tokio::test]
#[ignore = "requires a cloud key (MINDFORK_OPENAI_KEY / MINDFORK_GEMINI_KEY) and a sound card"]
async fn tts_speaks_chat_e2e_live() {
    use crate::features::tts_command::TtsScope;
    use crate::shared::config::TtsMode;

    let mut config = AppConfig::default();
    if std::env::var("MINDFORK_OPENAI_KEY").is_ok() {
        config.tts.mode = TtsMode::OpenAi;
        config.tts.openai.api_key_env = Some("MINDFORK_OPENAI_KEY".into());
        config.tts.openai.instructions = Some("говори по-русски, спокойно".into());
    } else if std::env::var("MINDFORK_GEMINI_KEY").is_ok() {
        config.tts.mode = TtsMode::Gemini;
        config.tts.gemini.api_key_env = Some("MINDFORK_GEMINI_KEY".into());
    } else {
        eprintln!("skip: neither MINDFORK_OPENAI_KEY nor MINDFORK_GEMINI_KEY is set");
        return;
    }
    // Speak roles too — this also checks prefixes in the profile's language.
    config.tts.speak_roles = true;

    let backend: Arc<dyn EngineBackend> = Arc::new(MockBackend::scripted(vec![
        ChatChunk::Text(
            "Это проверка озвучивания. Вот код:\n\n```rust\nfn main() {}\n```\n\n\
             А это латинская вставка: API, JSON."
                .into(),
        ),
        ChatChunk::Finished(crate::shared::api::FinishReason::Stop),
    ]));
    let (_dir, cmd_tx, mut evt_rx, handle) = spawn_orch_cfg(Some(backend), config);
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. })).await;
    cmd_tx
        .send(AppCommand::SendMessage("проверка связи".into()))
        .unwrap();
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Finished { .. })).await;

    // Speak the last exchange (the user's message + the assistant's reply).
    let started = std::time::Instant::now();
    cmd_tx.send(AppCommand::Tts(TtsScope::Recent(2))).unwrap();
    let on = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::TtsActive(true))).await;
    assert!(on.is_some(), "speech should start");

    // Wait for natural completion; synthesis/audio errors will surface as Error.
    let done = tokio::time::timeout(
        std::time::Duration::from_secs(120),
        wait_for(&mut evt_rx, |e| {
            matches!(e, AppEvent::TtsActive(false) | AppEvent::Error(_))
        }),
    )
    .await
    .expect("speech should finish within 120s");
    match done {
        Some(AppEvent::Error(msg)) => panic!("speech finished with an error: {msg}"),
        Some(AppEvent::TtsActive(false)) => {
            eprintln!("spoken in {:?}", started.elapsed());
        }
        other => panic!("unexpected outcome: {other:?}"),
    }
    cmd_tx.send(AppCommand::Quit).unwrap();
    let _ = handle.await;
}

/// Dangerous-tool confirmation against a real model (spec §9.8).
///
/// The mocked tests prove the channel; this proves the half only a live model
/// can: that a real model, told about `fs_write`, actually calls it — so the
/// confirmation appears on the path users take, not just when a fixture forces
/// the call.
#[tokio::test]
#[ignore]
async fn tool_confirmation_e2e_live() {
    let sandbox = tempfile::tempdir().unwrap();
    let mut config = AppConfig::default();
    config.tools.fs_enabled = true;
    config.tools.fs_root = Some(sandbox.path().display().to_string());
    config.tools.confirm_dangerous = true;

    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    enable_all_tools(&cmd_tx, &mut evt_rx).await;
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    cmd_tx
        .send(AppCommand::SendMessage(
            "Используй инструмент fs_write, чтобы создать файл note.txt \
             с содержимым ZARYA-5150. Только вызови инструмент."
                .into(),
        ))
        .unwrap();

    let ev = wait_for(&mut evt_rx, |e| {
        matches!(e, AppEvent::ToolConfirmRequest { .. })
    })
    .await
    .unwrap();
    let (generation_id, call_id, name, arguments) = match ev {
        AppEvent::ToolConfirmRequest {
            generation_id,
            call_id,
            name,
            arguments,
            ..
        } => (generation_id, call_id, name, arguments),
        _ => unreachable!(),
    };
    eprintln!("confirmation requested for {name}: {arguments}");
    assert_eq!(name, "fs_write");

    cmd_tx
        .send(AppCommand::ConfirmTool {
            generation_id,
            call_id,
            decision: ToolDecision::Allow,
        })
        .unwrap();

    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Finished { .. }))
        .await
        .unwrap();
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let written = std::fs::read_to_string(sandbox.path().join("note.txt"))
        .expect("the approved call did not write the file");
    eprintln!("file written: {written:?}");
    assert!(written.contains("ZARYA-5150"));
}

/// The file tools' reach against a real model (docs/research/safe-defaults.md
/// §4, D1–D2). The mocked tests prove each refusal; this proves they reach a
/// model through a real provider as the answer to its call. Two turns:
///
/// 1. a root that **contains** the data root (the system temp folder), and the
///    model asked to read the data root's `profiles.json` — GO: an `fs_read` call
///    answered with the app-directory refusal, and the reply quotes nothing of
///    the file;
/// 2. no root at all, and the model asked to list a folder — GO: an `fs_list`
///    call answered with the no-root refusal.
///
/// What the model then tells the user is printed, not asserted (lessons §9).
#[tokio::test]
#[ignore]
async fn file_tools_reach_e2e_live() {
    let refusals = |key: &str| -> Vec<String> {
        [crate::shared::i18n::Lang::Ru, crate::shared::i18n::Lang::En]
            .into_iter()
            .map(|l| crate::shared::i18n::locale(l).t(key).to_string())
            .collect()
    };

    let mut config = AppConfig::default();
    config.tools.fs_enabled = true;
    config.tools.fs_root = Some(std::env::temp_dir().display().to_string());
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    enable_all_tools(&cmd_tx, &mut evt_rx).await;
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let profiles = dir.path().join("profiles.json");
    assert!(
        profiles.exists(),
        "the data root has no profiles.json to aim at"
    );
    let (reply, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "Call fs_read on the file {} and quote its first three lines. Only call the tool once.",
            profiles.display()
        ),
    )
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!("turn 1 calls: {calls:?}\nturn 1 reply: {reply}");
    let app_dir = refusals("tool.fs.err.app_dir");
    assert!(
        calls
            .iter()
            .any(|(name, result)| name == "fs_read" && app_dir.iter().any(|r| result.contains(r))),
        "no fs_read answered with the app-directory refusal: {calls:?}"
    );
    assert!(
        !reply.contains("enabled_tools") && !reply.contains("known_tools"),
        "the reply quotes the profile file: {reply}"
    );

    let listed = tempfile::tempdir().unwrap();
    std::fs::write(listed.path().join("visible.txt"), "x").unwrap();
    let mut config = AppConfig::default();
    config.tools.fs_enabled = true;
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        return;
    };
    enable_all_tools(&cmd_tx, &mut evt_rx).await;
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let (reply, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "Call fs_list on the folder {} and tell me what is in it. Only call the tool once.",
            listed.path().display()
        ),
    )
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!("turn 2 calls: {calls:?}\nturn 2 reply: {reply}");
    let no_root = refusals("tool.fs.err.no_root");
    assert!(
        calls
            .iter()
            .any(|(name, result)| name == "fs_list" && no_root.iter().any(|r| result.contains(r))),
        "no fs_list answered with the no-root refusal: {calls:?}"
    );
    assert!(
        !reply.contains("visible.txt"),
        "the reply lists the folder: {reply}"
    );
}

/// History compression end to end (spec §6.7): a fact stated early must survive
/// being folded into the rolling summary and still be answerable once those
/// messages are no longer sent verbatim.
///
/// The probe (docs/research/history-compression.md §9a) established this against
/// the raw API; this smoke is the same question asked **through the
/// orchestrator**, so it covers the parts the probe could not: the cut planner,
/// the digest, the request splice and the stored boundary.
///
/// The load-bearing detail is the **control**: the same question is asked once
/// before compacting. Without it, a failure cannot be told apart from "this
/// model would not have answered anyway".
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn compaction_preserves_a_planted_fact_e2e_live() {
    use crate::shared::config::CompactionSettings;
    const CODE: &str = "ZARYA-8823";
    let config = AppConfig {
        compaction: CompactionSettings {
            enabled: true,
            summary_words: 250,
            // A small verbatim tail, so a handful of exchanges is enough to push
            // the planted fact out of the part that is still sent literally.
            tail_tokens: 120,
            ..Default::default()
        },
        ..Default::default()
    };
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // The fact goes in first. The control question is asked **immediately
    // after** it, on purpose: both exchanges then land inside the folded region,
    // so the verbatim tail that survives compaction contains no mention of the
    // code. Asking the control last (the obvious order) would leave its own
    // answer in the tail and the final assertion could be satisfied by reading
    // that instead of the summary — the test would pass without testing.
    let (reply, _) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "Запомни: внутренний код сборки нашего проекта — {CODE}.              Просто подтверди, что запомнил."
        ),
    )
    .await;
    eprintln!("seed reply: {reply}");

    // Control: answerable while the whole history is still sent.
    let (before, _) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Назови внутренний код сборки нашего проекта. Ответь только кодом.",
    )
    .await;
    eprintln!("control answer: {before}");
    assert!(
        mentions_code(&before, CODE),
        "control failed — the model cannot answer even with the full history,          so this run says nothing about compression: {before}"
    );

    // Unrelated turns, so the two exchanges above fall behind the verbatim tail.
    let (summary, folded) = fill_then_compact(
        &cmd_tx,
        &mut evt_rx,
        &[
            "Расскажи в двух предложениях, зачем нужны индексы в базах данных.",
            "В двух предложениях: чем отличается кэш от буфера?",
            "В двух предложениях: что такое идемпотентность запроса?",
            "В двух предложениях: зачем нужны миграции схемы?",
        ],
    )
    .await;

    // The real question: the planted fact is now only reachable through the
    // summary, because those messages are no longer sent verbatim.
    let (after, _) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Ещё раз: назови внутренний код сборки нашего проекта. Ответь только кодом.",
    )
    .await;
    eprintln!("answer after compaction: {after}");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // Both the seed and the control exchange must be inside the folded region,
    // or the code would still be sitting in the verbatim tail.
    assert!(
        folded >= 4,
        "the seed and control exchanges must be behind the boundary, folded {folded}"
    );
    assert!(
        mentions_code(&summary, CODE),
        "the summary must carry the identifier verbatim: {summary}"
    );
    assert!(
        mentions_code(&after, CODE),
        "the fact is now reachable only through the summary and must survive it: {after}"
    );
}

/// Stage 3 (fork F9b) end to end: what a summary could **not** carry must still
/// be reachable, and a real model has to actually reach for it.
///
/// This is the half unit tests cannot answer. They prove the tools return the
/// right page and the right fragment; whether a model, told in the summary block
/// that the two exist, chooses to call one instead of guessing is a property of
/// the wiring meeting a real model.
///
/// Validity rests on the seed being **un-summarizable**: fifteen arbitrary
/// item→code pairs cannot survive a 250-word summary that is explicitly told to
/// drop procedural detail. So the run asserts the summary does *not* carry the
/// answer — a precondition, reported loudly, exactly like the control below.
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn history_read_back_answers_what_the_summary_dropped_live() {
    use crate::shared::config::CompactionSettings;
    let config = AppConfig {
        compaction: CompactionSettings {
            enabled: true,
            // Deliberately tight: the seed below has to be genuinely beyond what
            // a summary can hold, and the word limit is half of that arithmetic.
            summary_words: 120,
            tail_tokens: 120,
            ..Default::default()
        },
        ..Default::default()
    };
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // Only the read-back tools. The first run of this smoke failed on its own
    // validity check and showed why that matters: the model filed the list with
    // `note_save`, so it could answer from memory without ever going back to the
    // history — and the note's *result* then travelled into the digest, which
    // put the whole list into the summary verbatim. Removing the alternative is
    // the same move `spawn_orch_live_no_embed` makes for attachments.
    let _ = narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec![
            crate::features::tools::history::HISTORY_READ_ID.into(),
            crate::features::tools::history::HISTORY_SEARCH_ID.into(),
        ],
    )
    .await;

    // The fixture's shape **is** this test's validity, and three earlier
    // attempts failed their own precondition, each teaching a rule:
    //  - fifteen entries are about sixty words and survived a 250-word summary
    //    whole;
    //  - when the target was the only *named* item among "item number N", the
    //    summary dropped the rest and kept the one that stood out — so every
    //    entry must be equally plausible and equally nameable;
    //  - sixty entries laid out as adjective x noun were *grouped by adjective*
    //    and all sixty codes still fitted. Any structure is compressible.
    // Hence two hundred entries against a 120-word limit: that is arithmetic
    // rather than a hope about the model's judgement. The codes are
    // non-arithmetic for the mirror reason — an obvious sequence invites a
    // summary to keep a range the answer can be derived from.
    let items: Vec<String> = [
        "токарный",
        "фрезерный",
        "сверлильный",
        "шлифовальный",
        "расточный",
        "строгальный",
        "долбёжный",
        "протяжной",
        "зубофрезерный",
        "заточный",
        "хонинговальный",
        "притирочный",
        "балансировочный",
        "испытательный",
        "калибровочный",
        "маркировочный",
        "упаковочный",
        "фасовочный",
        "сортировочный",
        "промывочный",
    ]
    .iter()
    .flat_map(|adj| {
        [
            "станок",
            "пресс",
            "конвейер",
            "насос",
            "компрессор",
            "редуктор",
            "манипулятор",
            "дозатор",
            "сепаратор",
            "накопитель",
        ]
        .iter()
        .map(move |noun| format!("{adj} {noun}"))
    })
    .collect();
    let codes: Vec<String> = (0..items.len())
        .map(|i| format!("ZARYA-{}", 1000 + (i * 6389) % 8999))
        .collect();
    let unique: std::collections::HashSet<&String> = codes.iter().collect();
    assert_eq!(
        unique.len(),
        codes.len(),
        "the fixture's codes must be unique"
    );
    // Deep in the last third, and deliberately not the middle: a summary that
    // keeps one illustrative example tends to take it from the middle of the
    // list, which is where this index used to be — and the run before this one
    // quoted exactly the entry being asked about.
    let idx = items.len() * 7 / 8 - 4;
    let item = items[idx].clone();
    let code = codes[idx].clone();

    let mut inventory = String::from("Вот инвентарный список склада, запомни его:\n");
    for (name, c) in items.iter().zip(&codes) {
        inventory.push_str(&format!("- {name}: {c}\n"));
    }
    inventory.push_str("Просто подтверди, что список получен.");
    let (reply, _) = run_turn_capture(&cmd_tx, &mut evt_rx, &inventory).await;
    eprintln!(
        "seed reply: {}",
        reply.chars().take(120).collect::<String>()
    );

    // The control proves the model can answer this *kind* of question while the
    // whole history is still sent — without it a failure cannot be told from
    // "this model would not have answered anyway".
    //
    // It asks about a **different** entry, and that is the sharpest lesson of
    // the four attempts this fixture took. Asking the control about the target
    // put the question inside the folded range, which made that one entry the
    // most salient thing in it — so the summary kept precisely the entry the
    // test needs it to drop, twice in a row and not by chance.
    let ctrl = 12;
    let (before, _) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "Какой инвентарный номер у позиции «{}»? Ответь только номером.",
            items[ctrl]
        ),
    )
    .await;
    eprintln!("control answer: {before}");
    assert!(
        before.contains(&codes[ctrl]),
        "control failed — the model cannot answer even with the full history, \
         so this run says nothing about the read-back tools: {before}"
    );
    let question = format!("Какой инвентарный номер у позиции «{item}»? Ответь только номером.");

    let (summary, folded) = fill_then_compact(
        &cmd_tx,
        &mut evt_rx,
        &[
            "В двух предложениях: зачем нужен профилактический ремонт оборудования?",
            "В двух предложениях: чем отличается плановый простой от аварийного?",
            "В двух предложениях: что такое наработка на отказ?",
            "В двух предложениях: зачем на складе нужна маркировка?",
        ],
    )
    .await;
    assert!(
        folded >= 4,
        "the seed must be behind the boundary: {folded}"
    );
    assert!(
        !summary.contains(&code),
        "the seed was summarizable after all, so this run cannot show anything \
         about reading back: {summary}"
    );

    // The real question: the answer now exists only in messages that are no
    // longer sent, and only the read-back tools can reach it.
    let (after, calls) = run_turn_capture(&cmd_tx, &mut evt_rx, &question).await;
    eprintln!("answer after compaction: {after}");
    eprintln!(
        "tools called: {:?}",
        calls.iter().map(|(n, _)| n).collect::<Vec<_>>()
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let used_read_back = calls.iter().any(|(name, _)| {
        name == crate::features::tools::history::HISTORY_READ_ID
            || name == crate::features::tools::history::HISTORY_SEARCH_ID
    });
    assert!(
        used_read_back,
        "the model must reach for the read-back tools the summary block names, \
         instead of guessing: {calls:?}"
    );
    assert!(
        after.contains(&code),
        "what the summary dropped must still be answerable: {after}"
    );
}

/// Spec §9.11 go/no-go: a fact that exists only in **another** chat of the
/// profile is reachable through `chat_search`/`chat_read`, and a real model
/// actually reaches for the pair when the user points across conversations.
///
/// Validity: the code is a nonsense token seeded into chat A only, so chat B
/// has no route to it but the pair — the profile is narrowed to exactly these
/// two tools (the read-back smoke's lesson: remove the alternative rather than
/// hope; with `note_save` in reach the model files the fact into profile
/// memory and never crosses a conversation), and the "tool was actually
/// called" assertion keeps a refusal or a guess from reading as a pass (the
/// address-policy smoke's lesson).
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn cross_chat_search_answers_from_another_chat_live() {
    const CODE: &str = "SIREN-7734";
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(AppConfig::default()) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // Only the pair under test, and *before* the seed turn — see the doc
    // comment for why the alternative routes must not exist.
    let (profile, chat_a) = narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec![
            crate::features::tools::chats::CHAT_SEARCH_ID.into(),
            crate::features::tools::chats::CHAT_READ_ID.into(),
        ],
    )
    .await;

    // Chat A: the only place the code exists.
    let (reply, _) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "Запиши в этом разговоре: код поставки нового компрессора — {CODE}. \
             Просто подтверди, что записал."
        ),
    )
    .await;
    eprintln!(
        "seed reply: {}",
        reply.chars().take(120).collect::<String>()
    );

    // Chat B of the same profile; A becomes an "other" chat. The post-save
    // hook indexes A behind the 800 ms save debounce, and there is no event to
    // wait on for a background index write — so wait the debounce out.
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(profile),
        })
        .unwrap();
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    tokio::time::sleep(std::time::Duration::from_millis(1500)).await;

    let (answer, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "В другом разговоре этого профиля мы записали код поставки компрессора. \
         Найди его по другим разговорам, назови код и укажи, в каком разговоре \
         он записан.",
    )
    .await;
    eprintln!("answer: {answer}");
    eprintln!(
        "tools called: {:?}",
        calls.iter().map(|(n, _)| n).collect::<Vec<_>>()
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let used_pair = calls.iter().any(|(name, _)| {
        name == crate::features::tools::chats::CHAT_SEARCH_ID
            || name == crate::features::tools::chats::CHAT_READ_ID
    });
    assert!(
        used_pair,
        "the model must reach across conversations through the pair, not \
         guess: {calls:?}"
    );
    assert!(
        mentions_code(&answer, CODE),
        "the fact lives only in the other chat and must come back: {answer}"
    );
    // Spec §11.3: the model is *taught* the address form by the pair's
    // descriptions, so naming the conversation it read must produce a
    // `chat://` reference — resolved here through the same `find_refs` the
    // feed uses, against the one conversation the answer can legitimately
    // name. This is the half that makes the reference navigable; without it
    // the feature rests on one model's habit.
    let cited = crate::features::chat_links::find_refs(&answer, &[chat_a]);
    assert!(
        !cited.is_empty(),
        "the answer must cite the conversation as {}<id>: {answer}",
        crate::features::chat_links::SCHEME
    );
}

/// What only a real server can answer: that `/props` exists on the stack we
/// actually run against, and that our client reads the field it means to.
///
/// The probe measured this with `curl` (§9a M1); this is the same question
/// through [`EngineBackend::context_budget`], which is what the automatic
/// trigger depends on. A stub cannot cover it — it would only prove our own
/// fixture parses.
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn props_reports_the_context_window_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let budget = backend.context_budget().await;
    eprintln!("live context window: {budget:?}");
    let n = budget.expect(
        "a live llama-server must report its window at /props — without it the \
         automatic trigger has no budget for an external server",
    );
    assert!(n >= 512, "an implausible window: {n}");
}

/// The fourth self-description question, and the one behind the `sessions`
/// hint (spec §11.6): a live `llama-server` reports its slot count on `/props`
/// (`total_slots`) — `-np N` when given, four for the auto default since
/// December 2025 (docs/research/parallel-subagents.md §2.4) — and our client
/// reads it through the retry decorator every real external setup wraps it in.
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn props_reports_the_slot_count_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let slots = backend.parallel_slots().await;
    eprintln!("live slot count: {slots:?}");
    let n = slots.expect("a live llama-server must report total_slots at /props");
    assert!((1..=256).contains(&n), "an implausible slot count: {n}");
}

/// The other question only a real server can answer: that it will **name the
/// model it is running**, and that our client reads a name a header can show.
///
/// This is the whole of `external` mode's blank caption
/// (docs/research/external-model-name.md): connect by URL with no model named in
/// settings and, until this existed, neither the feed nor a message's metadata
/// could say what answered. A stub only proves our own fixture parses — what
/// this asserts is that the live stack answers `/v1/models` or `/props` at all,
/// and that the answer arrives normalized (an un-aliased `llama-server` reports
/// the whole `-m` path).
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn the_engine_names_the_model_it_is_running_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let name = backend.model_id().await;
    eprintln!("live model name: {name:?}");
    let name = name.expect(
        "a live OpenAI-compatible server must name its model at /v1/models or          /props — without it an external chat has no name to show",
    );
    assert!(!name.trim().is_empty(), "an empty name is not an answer");
    assert!(
        !name.ends_with(crate::shared::gguf::EXT),
        "a file name reached the header unnormalized: {name}"
    );
    assert!(
        !name.contains('\\') && !name.contains('/'),
        "a path reached the header instead of a model name: {name}"
    );
    // …and not a *part* of one. A model too large for a single file is served
    // from `<name>-00001-of-00002.gguf`, and an un-aliased server reports that
    // file — so the name a header shows must have lost the tail as well as the
    // extension (docs/research/e2e-gpt-oss-120b.md §6, T1). Vacuously true on a
    // single-file stack, which is the point: it costs nothing to carry and it is
    // the one assertion a split model would break.
    //
    // The extension is put back on to ask the question, because `parse_shard` is
    // the *only* place this project recognizes the tail shape and a second
    // implementation of it here is exactly what that module exists to prevent.
    assert!(
        crate::shared::gguf::parse_shard(&format!("{name}{}", crate::shared::gguf::EXT)).is_none(),
        "a part number reached the header instead of a model name: {name}"
    );
}

/// …and it lands where the user sees it: the reply the turn stores carries the
/// discovered name in its metadata snapshot (spec §11.3), with nothing named in
/// settings. The end-to-end half of the smoke above — the name is read back off
/// the **stored** message, through a fresh activation, not off the event that
/// announced it.
///
/// The wait for `EngineModel` is not ceremony: discovery is a network round trip
/// started when the engine is applied, and a turn sent in the same tick as the
/// bootstrap genuinely resolves to no name. A human takes seconds to type; this
/// test would otherwise race the first request out of the door (measured — it
/// did, on the first live run).
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn a_reply_records_the_discovered_model_live() {
    let mut cfg = no_auto_cfg();
    // The mode the discovery exists for, with the "Model (opt.)" field blank —
    // which is also what `MINDFORK_ENGINE_URL` alone produces.
    cfg.engine.external.model_name = None;
    let Some((_dir, cmd_tx, mut evt_rx, _task)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };

    // The bootstrap chat and the engine's answer arrive in either order.
    let (chat, discovered) = tokio::time::timeout(std::time::Duration::from_secs(30), async {
        let (mut chat, mut model) = (None, None);
        while chat.is_none() || model.is_none() {
            match evt_rx.recv().await {
                Some(AppEvent::ChatActivated { id, .. }) => chat = Some(id),
                Some(AppEvent::EngineModel(Some(m))) => model = Some(m),
                Some(_) => {}
                None => break,
            }
        }
        (chat, model)
    })
    .await
    .expect("the engine must name its model within 30s of connecting");
    let chat = chat.expect("the bootstrap chat");
    let discovered = discovered.expect("the engine's own name for the model");
    eprintln!("discovered: {discovered}");

    // The live bubble's header: this is the value the stored message must agree
    // with, and disagreement is the failure the single resolve prevents.
    cmd_tx
        .send(AppCommand::SendMessage("Say hi in one word.".into()))
        .unwrap();
    let mut announced = None;
    while let Some(ev) = evt_rx.recv().await {
        match &ev {
            AppEvent::GenerationStarted { model, .. } => announced = model.clone(),
            AppEvent::Finished { .. } => break,
            _ => {}
        }
    }
    assert_eq!(
        announced.as_deref(),
        Some(discovered.as_str()),
        "the streaming bubble must name what the engine reported"
    );

    // Read the stored reply back through a real activation. A switch **to the
    // open chat is a no-op** and emits nothing, so the way back has to leave
    // first — a fresh chat, then back (the first attempt at this smoke waited
    // forever on an event that was never going to come).
    cmd_tx
        .send(AppCommand::NewChat { profile_id: None })
        .unwrap();
    cmd_tx.send(AppCommand::SwitchChat(chat)).unwrap();
    let messages = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ChatActivated { id, .. } if *id == chat),
    )
    .await
    .and_then(|e| match e {
        AppEvent::ChatActivated { messages, .. } => Some(messages),
        _ => None,
    })
    .expect("the chat re-opens");
    let recorded = messages
        .iter()
        .filter(|m| m.role == crate::entities::message::MessageRole::Assistant)
        .filter_map(|m| m.metadata.as_ref()?.model.clone())
        .next_back();
    eprintln!("recorded in the metadata: {recorded:?}");
    assert_eq!(
        recorded.as_deref(),
        Some(discovered.as_str()),
        "the stored reply must name the model that wrote it"
    );
}

/// The `llm_*` pair end to end (spec §9.14): the assistant, asked what model it
/// is, reaches for `get_llm_name` and answers with the name the engine itself
/// reported; and after that first exchange the recorder has written the
/// profile's baseline record, which `get_llm_history` reads back in the next
/// turn. External mode with a blank model field — the discovered-name path,
/// the same setup as the metadata smoke above, and the wait for `EngineModel`
/// matters for the same reason: a turn sent before discovery lands genuinely
/// has no name to answer or record with.
///
/// Both turns assert the tool **was actually called** — a model answering from
/// its own beliefs would otherwise read as a pass (docs/lessons.md §9).
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn llm_name_and_history_e2e_live() {
    let mut cfg = no_auto_cfg();
    // The harness injects the live backend directly, so the config's default
    // `Managed` would survive into the turn snapshot and the history records
    // unless the mode is set to what the setup actually is — the first live
    // run recorded `(managed)` for a server reached by URL exactly this way.
    cfg.engine.mode = crate::shared::config::ServerMode::External;
    cfg.engine.external.model_name = None;
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };

    // The bootstrap profile/chat and the engine's answer arrive in any order.
    let (profile, discovered) = tokio::time::timeout(std::time::Duration::from_secs(30), async {
        let (mut profile, mut chat, mut model) = (None, None, None);
        while profile.is_none() || chat.is_none() || model.is_none() {
            match evt_rx.recv().await {
                Some(AppEvent::ProfileList(ps)) => {
                    profile = profile.or_else(|| ps.first().map(|p| p.id));
                }
                Some(AppEvent::ChatActivated { id, .. }) => chat = Some(id),
                Some(AppEvent::EngineModel(Some(m))) => model = Some(m),
                Some(_) => {}
                None => break,
            }
        }
        (profile, model)
    })
    .await
    .expect("the engine must name its model within 30s of connecting");
    let profile = profile.expect("the bootstrap profile");
    let discovered = discovered.expect("the engine's own name for the model");
    eprintln!("discovered: {discovered}");

    // Only the pair under test — nothing else to reach for.
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: profile,
            edit: Box::new(ProfileEdit {
                enabled_tools: Some(vec![
                    crate::features::tools::llm::GET_LLM_NAME_ID.to_string(),
                    crate::features::tools::llm::GET_LLM_HISTORY_ID.to_string(),
                ]),
                ..Default::default()
            }),
        })
        .unwrap();

    let (_text, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Which language model are you running on right now? Check with your \
         get_llm_name tool and tell me.",
    )
    .await;
    let name_calls: Vec<&(String, String)> =
        calls.iter().filter(|(n, _)| n == "get_llm_name").collect();
    eprintln!("get_llm_name calls: {name_calls:#?}");
    assert!(
        !name_calls.is_empty(),
        "the tool was never called: {calls:#?}"
    );
    assert!(
        name_calls.iter().any(|(_, r)| r.contains(&discovered)),
        "the tool's answer must carry the discovered name {discovered:?}: {name_calls:#?}"
    );

    // The first exchange has landed, so the recorder has written the baseline
    // record (fork F6) — the next turn's history tool must read it back.
    let (_text, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "When did your language model last change? Check with your \
         get_llm_history tool and quote the records.",
    )
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    let hist_calls: Vec<&(String, String)> = calls
        .iter()
        .filter(|(n, _)| n == "get_llm_history")
        .collect();
    eprintln!("get_llm_history calls: {hist_calls:#?}");
    assert!(
        !hist_calls.is_empty(),
        "the tool was never called: {calls:#?}"
    );
    assert!(
        hist_calls
            .iter()
            .any(|(_, r)| r.contains(&discovered) && r.contains("(external)")),
        "the history must hold the baseline record for {discovered:?}: {hist_calls:#?}"
    );
}

/// Stage 2 end to end: nobody types `/compact`, and the conversation is folded
/// anyway once it approaches the window.
///
/// Deliberately in **external** mode, so the budget can only come from one
/// place: the engine's own `/props`. In managed mode the answer would be read
/// straight from `-c` in the config and the discovery path — the half that
/// needs a server at all — would not run.
///
/// The threshold is set far below the default 75% for time's sake: 75% of a real
/// 16k window would take a very long conversation to reach, and what is under
/// test is the trigger and the budget, not the arithmetic of a percentage.
///
/// The prompt is grown by the **user's** messages rather than by asking the
/// model for long answers: how verbose a model feels like being is not something
/// a test should depend on, and the first attempt at this smoke failed for
/// exactly that reason — four short exchanges came to ~330 tokens against a
/// threshold of 491.
#[tokio::test]
#[ignore = "needs a live engine (MINDFORK_ENGINE_URL)"]
async fn auto_compaction_fires_without_the_command_live() {
    use crate::shared::config::{CompactionSettings, EngineSettings, ServerMode};
    const THRESHOLD_PCT: u8 = 3;
    let config = AppConfig {
        compaction: CompactionSettings {
            enabled: true,
            summary_words: 120,
            tail_tokens: 120,
            threshold_pct: THRESHOLD_PCT,
            page_tokens: crate::shared::config::DEFAULT_COMPACTION_PAGE_TOKENS,
            // Nothing explicit: the window must be discovered, or this smoke
            // silently stops testing what it is named after.
            context_tokens: None,
        },
        engine: EngineSettings {
            mode: ServerMode::External,
            ..Default::default()
        },
        ..Default::default()
    };
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Deterministic ballast: the user's own text, so the prompt grows by a known
    // amount per turn whatever the model answers.
    let ballast =
        "Для контекста повторю условие задачи целиком, чтобы ничего не потерялось. ".repeat(12);

    // The first turn is also what kicks the budget question off — the answer
    // arrives in the background, so an early turn legitimately does nothing.
    // That self-healing is part of what is being checked.
    let mut compacted = None;
    // The exact prompt size the server reported, as the trigger sees it.
    let mut exact_prompt: Option<u64> = None;
    for topic in [
        "Одним предложением: зачем нужны индексы в базах данных?",
        "Одним предложением: чем кэш отличается от буфера?",
        "Одним предложением: что такое идемпотентность запроса?",
        "Одним предложением: зачем нужны миграции схемы?",
    ] {
        cmd_tx
            .send(AppCommand::SendMessage(format!("{ballast}\n{topic}")))
            .unwrap();
        // Collected **during** the turn on purpose: the exact `usage` arrives
        // before `Finished`, so the shared `run_turn_capture` — which stops
        // there — would have already consumed it and the diagnostic below would
        // read `None` whatever the server said.
        while let Some(e) = evt_rx.recv().await {
            match e {
                AppEvent::TokenUsage {
                    context: Some(n),
                    context_exact: true,
                    ..
                } => exact_prompt = Some(n),
                AppEvent::Compacted {
                    summary, folded, ..
                } => compacted = Some((summary, folded)),
                AppEvent::Error(m) => eprintln!("error event: {m}"),
                // `Finished` is not the end of the turn's bookkeeping: the
                // trigger runs in `handle_done`, which emits `ChatList`
                // afterwards (the `title.rs` precedent).
                AppEvent::ChatList(_) => break,
                _ => {}
            }
        }
        eprintln!(
            "exact prompt: {exact_prompt:?}, compacted: {}",
            compacted.is_some()
        );
        if compacted.is_some() {
            break;
        }
    }
    if compacted.is_none() {
        // The last turn may have triggered a roll that has not landed yet.
        if let Ok(Some(AppEvent::Compacted {
            summary, folded, ..
        })) = tokio::time::timeout(
            std::time::Duration::from_secs(120),
            wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Compacted { .. })),
        )
        .await
        {
            compacted = Some((summary, folded));
        }
    }
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let (summary, folded) = compacted.unwrap_or_else(|| {
        panic!(
            "nothing folded. Last exact prompt: {exact_prompt:?} tokens, \
             threshold: {THRESHOLD_PCT}% of the window the engine reported. \
             If the prompt is well under it, the conversation simply never grew \
             enough; if it is over, the budget was never discovered or the \
             trigger did not fire."
        )
    });
    eprintln!(
        "auto-folded {folded} messages into {} chars:\n{summary}",
        summary.len()
    );
    assert!(folded > 0, "a compaction that folded nothing");
    assert!(!summary.trim().is_empty(), "an empty summary is a failure");
}

/// Impersonation (`Ctrl+U`) over a **compacted** conversation, against a live
/// engine (spec §11.8, §6.7).
///
/// The deterministic half — what the request carries — is unit tested; what only
/// a live engine can answer is whether the resulting shape is *usable*. It is
/// unlike anything impersonation sent before: a cut always lands on a `User`
/// message, and the role swap turns it into a **leading assistant turn**. Both
/// ways that can go wrong end in the same silence rather than an error — a
/// provider refusing the leading role, or (Anthropic's rule) treating a trailing
/// assistant turn as a prefill and continuing it instead of replying. So the
/// assertion is deliberately just "a non-empty message came back": an empty
/// preview is exactly the symptom either failure produces.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn impersonation_after_compaction_still_writes_live() {
    use crate::shared::config::CompactionSettings;
    let config = AppConfig {
        compaction: CompactionSettings {
            enabled: true,
            summary_words: 120,
            tail_tokens: 120,
            ..Default::default()
        },
        ..Default::default()
    };
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    let (_summary, folded) = fill_then_compact(
        &cmd_tx,
        &mut evt_rx,
        &[
            "Расскажи в двух предложениях, зачем нужны индексы в базах данных.",
            "В двух предложениях: чем отличается кэш от буфера?",
            "В двух предложениях: что такое идемпотентность запроса?",
        ],
    )
    .await;
    assert!(folded > 0, "nothing was folded — nothing to test");

    cmd_tx
        .send(AppCommand::Impersonate {
            seed: String::new(),
        })
        .unwrap();
    let mut text = String::new();
    let mut reason = None;
    while let Some(ev) = evt_rx.recv().await {
        match ev {
            AppEvent::ImpersonationChunk { text: t, .. } => text.push_str(&t),
            AppEvent::ImpersonationFinished { reason: r, .. } => {
                reason = Some(r);
                break;
            }
            AppEvent::Error(e) => panic!("impersonation failed: {e}"),
            _ => {}
        }
    }
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!("impersonated message ({reason:?}): {text}");
    assert!(
        !text.trim().is_empty(),
        "empty preview — the compacted request was refused or read as a prefill"
    );
}

/// Live e2e for `/image attach <url>` (spec §9.10, docs/research/image-url-attach.md): an
/// image named by a **web address** is downloaded, staged and seen by the model — the same
/// pixels a file attach delivers, taking the one path a unit test cannot exercise end to
/// end (a real download, a real vision model, a real request).
///
/// The fixture is served from a local listener rather than a public URL: a smoke must not
/// depend on someone else's uptime, and a loopback address is exactly the case fork F2
/// deliberately keeps reachable.
///
/// **The control arm is the point.** The same question is asked first with *nothing*
/// staged; a model that answers it anyway means the fixture is guessable and the green
/// arm proves nothing — which is how the last two image tracks each produced a probe that
/// looked green and was a hallucination (docs/lessons.md §9, mcp-tool-images §2.1).
/// `#[ignore]`, manual against a live vision model.
#[tokio::test]
#[ignore = "requires a vision-capable OpenAI-compatible server (MINDFORK_ENGINE_URL + --mmproj)"]
async fn image_url_attachment_e2e_live() {
    use crate::features::image_fetch::stub::{ok_response, serve};

    if crate::shared::api::live_text_only() {
        eprintln!("skip: MINDFORK_LIVE_TEXT_ONLY — this stack has no vision projector");
        return;
    }
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // Deliberately *not* the blue of the file-attach smoke, so a stale reply from that
    // fixture could not pass this one.
    let png = figure_png([20, 160, 60]);
    let (base, _server) = serve(vec![ok_response("image/png", &png, true)]);

    const QUESTION: &str = "What is the background colour of this image, and what shape is \
                            in the centre? Answer in a few words.";

    // Control — nothing staged. This must fail to answer.
    let (control, _) = run_turn_live(&cmd_tx, &mut evt_rx, QUESTION).await;
    eprintln!("control reply (no image staged): {control}");

    let info = attach_image_live(&cmd_tx, &mut evt_rx, format!("{base}/fixtures/figure.png")).await;
    assert_eq!(info.name, "figure.png", "named after the URL's path");

    let (reply, _) = run_turn_live(&cmd_tx, &mut evt_rx, QUESTION).await;
    eprintln!("reply with the downloaded image: {reply}");

    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reply = reply.to_lowercase();
    assert!(
        reply.contains("green"),
        "the downloaded image's background colour, got: {reply}"
    );
    assert!(
        reply.contains("square"),
        "the downloaded image's centred shape, got: {reply}"
    );
    let control = control.to_lowercase();
    assert!(
        !(control.contains("green") && control.contains("square")),
        "the control answered without seeing anything — the fixture is guessable, so the \
         green arm above proves nothing: {control}"
    );
}

/// The address policy against a real model (spec §9.3,
/// docs/research/fetch-url-address-policy.md).
///
/// The unit tests prove the guard refuses; only a live model can settle the half that
/// matters for a *model-facing* message: that the refusal makes it **stop**. The failure
/// mode this feature could easily create is a model that reads "cannot reach that" and
/// tries the same host by IP, then by name, then through a redirector — three more round
/// trips that must all fail (docs/lessons.md §4).
///
/// **The target is a plain loopback service, not the cloud metadata endpoint.** The first
/// run of this smoke pointed at `169.254.169.254` and the model refused *on its own* —
/// never calling the tool, so the guard was never exercised and the test proved nothing
/// (the "never called the tool" assertion is what caught it). A local address the user
/// plausibly asked about removes that confound. The stub counts connections, so this also
/// proves end to end that nothing reached the service.
#[tokio::test]
#[ignore = "requires a live model (MINDFORK_ENGINE_URL)"]
async fn fetch_url_address_policy_e2e_live() {
    let mut config = AppConfig::default();
    config.tools.web_enabled = true;
    // Off is the default; stated here because it is the thing under test.
    config.tools.web_allow_private = false;

    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(config) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    enable_all_tools(&cmd_tx, &mut evt_rx).await;
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    let (url, hits) = crate::shared::net::stub::counting_stub();
    let (reply, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "Fetch {url} with fetch_url and tell me what it says. If you cannot, say so and stop."
        ),
    )
    .await;
    eprintln!("reply: {reply}");
    for (name, result) in &calls {
        eprintln!("call {name} -> {result}");
    }

    cmd_tx.send(AppCommand::Quit).unwrap();
    let _ = handle.await;

    let fetches: Vec<_> = calls.iter().filter(|(n, _)| n == "fetch_url").collect();
    assert!(
        !fetches.is_empty(),
        "the model never called the tool, so nothing was tested: {calls:?}"
    );
    // Compared through the bundle, not a hardcoded phrase: this message is axis A (the
    // agent scaffold's language), so an English fragment would fail on a `ru` profile for
    // a reason that has nothing to do with the guard.
    let refusal = crate::shared::i18n::locale(crate::shared::i18n::Lang::default())
        .t("tool.fetch_url.err.address_blocked");
    assert!(
        fetches.iter().all(|(_, result)| result.contains(refusal)),
        "every attempt must come back refused, not fetched: {fetches:?}"
    );
    // The door has to close: one attempt is the model trying, four is the model hunting
    // for a way around a message that failed to say there isn't one.
    assert!(
        fetches.len() <= 2,
        "the refusal did not stop the model — {} attempts: {fetches:?}",
        fetches.len()
    );
    // The end-to-end half no unit test can reach: whatever the model tried, the service
    // itself was never connected to.
    assert_eq!(
        hits.load(std::sync::atomic::Ordering::SeqCst),
        0,
        "the local service was reached despite the policy"
    );
}

/// Code workspace, **stage 1 live check** (docs/history/code-workspace.md §6): with a
/// project attached, does the model actually navigate it — locate a fact it
/// cannot know, read the file that holds it, and answer from what it read?
///
/// The fact is deliberately arbitrary (a five-digit timeout no model has an
/// opinion about) and sits behind one indirection: `main.rs` names a function,
/// the function is in another file, and the number is a constant beside it. A
/// decoy timeout in a third file makes "guessed a plausible number" fail.
///
/// The profile is narrowed to the three workspace tools — the "remove the
/// alternative" rule (docs/lessons.md §9): with `fs_read` in reach the model
/// could answer without the feature under test ever running.
///
/// **Two turns**, because the first live run showed the narrow assertion was
/// wrong: asked for the timeout, the model answered correctly from `code_list` +
/// `code_grep` alone and never opened the file — the hit line carries the whole
/// constant, so a read would have been a wasted round. That is a better outcome,
/// not a failure, and demanding `code_read` there would have pinned the model to
/// the worse route (the same shape as `attachment_search` superseding
/// `attachment_read`, docs/lessons.md §9). So turn 1 asserts the *outcome*, and
/// turn 2 asks for something only a read can produce, keeping that tool covered.
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_workspace_navigate_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_workspace_navigate_e2e_live() {
    use crate::features::project_command::ProjectProgress;
    use crate::features::tools::code::{CODE_GREP_ID, CODE_LIST_ID, CODE_READ_ID};

    const ANSWER: &str = "7300";
    let files: [(&str, &str); 4] = [
        (
            "src/main.rs",
            "mod config;\nmod util;\n\nfn main() {\n    let ms = config::default_timeout();\n    println!(\"waiting {ms} ms\");\n}\n",
        ),
        (
            "src/config.rs",
            "/// How long a request may take before it is abandoned.\nconst REQUEST_TIMEOUT_MS: u64 = 7300;\n\npub fn default_timeout() -> u64 {\n    REQUEST_TIMEOUT_MS\n}\n",
        ),
        // The decoy: a plausible number in a file the question does not lead to.
        (
            "src/util.rs",
            "/// Delay between reconnect attempts.\npub const RECONNECT_DELAY_MS: u64 = 500;\n",
        ),
        (
            "README.md",
            "# probe\n\nA tiny client. Timeouts are configurable.\n",
        ),
    ];
    let ws = tempfile::tempdir().unwrap();
    for (name, body) in files {
        let path = ws.path().join(name);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, body).unwrap();
    }

    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (profile, _chat) = narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec![
            CODE_LIST_ID.into(),
            CODE_READ_ID.into(),
            CODE_GREP_ID.into(),
        ],
    )
    .await;

    // The command path itself is under test: attaching goes through the
    // orchestrator exactly as a user's `/project attach` does.
    cmd_tx
        .send(AppCommand::ProjectAttach {
            path: ws.path().to_string_lossy().into_owned(),
        })
        .unwrap();
    let attached = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ProjectProgress(_)))
        .await
        .unwrap();
    let AppEvent::ProjectProgress(ProjectProgress::Attached { root, .. }) = &attached else {
        panic!("the project did not attach: {attached:?}");
    };
    eprintln!("attached: {root}");

    // Turn 1 — find a fact that is only in the project.
    let (answer, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "В прикреплённом проекте есть таймаут запроса по умолчанию. \
         Найди его и скажи, чему он равен и в каком файле задан.",
    )
    .await;
    let names: Vec<&String> = calls.iter().map(|(n, _)| n).collect();
    eprintln!("turn 1 tool calls: {names:?}");
    eprintln!("turn 1 reply: {answer}");

    // The feature has to have actually run, or the smoke passes on a model that
    // answered from the prompt (docs/lessons.md §9). *Which* tool found it is
    // the model's call.
    assert!(
        calls
            .iter()
            .any(|(n, _)| crate::features::tools::code::is_workspace_tool(n)),
        "the model must go into the project: {names:?}"
    );
    assert!(
        answer.contains(ANSWER),
        "the timeout is only knowable from the project: {answer}"
    );
    assert!(
        answer.contains("config"),
        "the answer must name the file it came from: {answer}"
    );
    // The decoy stayed a decoy.
    assert!(
        !answer.contains("500"),
        "the reconnect delay is not the request timeout: {answer}"
    );

    // Turn 2 — `code_read`, covered on its own. Two things make that
    // deterministic rather than hopeful, and both were learned the hard way: the
    // question is about a file **turn 1 never opened** (asked about
    // `src/config.rs`, `gpt-oss-120b` answered correctly with no tool at all,
    // because its turn-1 `code_read` had already put that file in the
    // conversation), and the profile is narrowed to `code_read` alone, so no
    // other route exists. The second instance of this pattern — the first is
    // `attachment_read` in docs/history/remote-e2e-hf.md §3; an assertion that a
    // *particular* tool must be chosen is only ever true until a model finds a
    // better route, so the smoke has to remove the routes instead of hoping.
    set_profile_tools(&cmd_tx, profile, vec![CODE_READ_ID.into()]);
    let (answer2, calls2) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Что написано в первой строке файла README.md в прикреплённом проекте?",
    )
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    let names2: Vec<&String> = calls2.iter().map(|(n, _)| n).collect();
    eprintln!("turn 2 tool calls: {names2:?}");
    eprintln!("turn 2 reply: {answer2}");
    assert!(
        calls2.iter().any(|(n, _)| n == CODE_READ_ID),
        "only a read can answer this: {names2:?}"
    );
    // The heading of README.md, which appears nowhere else in the workspace and
    // is not in the conversation before this turn. The line count is
    // deliberately *not* asserted: a trailing newline makes "how many lines" a
    // question two readers answer differently.
    assert!(
        answer2.to_lowercase().contains("probe"),
        "the first line of README.md is `# probe`: {answer2}"
    );
}

/// Code workspace, stage 1: **no project, no tools**. The gate is what keeps a
/// chat without a workspace byte-identical to what the app sent before the
/// feature, so it is worth one live turn: the model is asked to read a file and
/// must answer that it cannot, without any `code_*` call happening.
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_workspace_gate_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_workspace_gate_e2e_live() {
    use crate::features::tools::code::{CODE_GREP_ID, CODE_LIST_ID, CODE_READ_ID};

    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    // The tools are enabled in the profile and still must not be offered: the
    // project's absence is the gate, not the toggle.
    let _ = narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec![
            CODE_LIST_ID.into(),
            CODE_READ_ID.into(),
            CODE_GREP_ID.into(),
        ],
    )
    .await;

    let (answer, calls) = run_turn_capture(
        &cmd_tx,
        &mut evt_rx,
        "Прочитай файл src/main.rs и скажи, что он делает.",
    )
    .await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let names: Vec<&String> = calls.iter().map(|(n, _)| n).collect();
    eprintln!("tool calls: {names:?}");
    eprintln!("reply: {answer}");
    assert!(
        calls
            .iter()
            .all(|(n, _)| !crate::features::tools::code::is_workspace_tool(n)),
        "with no project attached the tools must not even be offered: {names:?}"
    );
}

/// Compiles a probe fixture with `rustc` (no cargo, no manifest, no network) and
/// runs it, returning its stdout. `Err` carries the compiler's diagnostics.
///
/// Output goes to `target/`, which `code_grep` and `code_list` skip — so
/// building does not put artifacts in front of the model, exactly as a real
/// checkout would not.
fn rustc_run(root: &std::path::Path) -> Result<String, String> {
    let build = std::process::Command::new("rustc")
        .current_dir(root)
        .args(["--edition", "2021", "src/main.rs", "--out-dir", "target"])
        .output()
        .map_err(|e| format!("could not start rustc: {e}"))?;
    if !build.status.success() {
        return Err(String::from_utf8_lossy(&build.stderr).into_owned());
    }
    let exe = root
        .join("target")
        .join(if cfg!(windows) { "main.exe" } else { "main" });
    let run = std::process::Command::new(&exe)
        .current_dir(root)
        .output()
        .map_err(|e| format!("could not run {}: {e}", exe.display()))?;
    Ok(String::from_utf8_lossy(&run.stdout).into_owned())
}

/// Spins up a live orchestrator with `files` attached as the chat's project and
/// the profile narrowed to the workspace tools, and runs one turn.
///
/// Returns `(the fixture directory, the reply, the calls)`, or `None` with no
/// live server. The narrowing is the "remove the alternative" rule
/// (docs/lessons.md §9): with `fs_write` in reach the model could rewrite a file
/// wholesale and the contract under test would never run.
async fn run_workspace_turn<B: AsRef<[u8]>>(
    files: &[(&str, B)],
    prompt: &str,
) -> Option<(tempfile::TempDir, String, Vec<(String, String, String)>)> {
    run_workspace_turn_with(files, &[], prompt).await
}

/// [`run_workspace_turn`], with command lines put into the project's slots
/// first — the stage-3 shape, where the assistant can also build, run and test.
///
/// The lines go in through the same `AppCommand::ProjectSlot` the user's
/// `/project build-cmd` sends, rather than by writing the field: the smoke is
/// then measuring the route that ships, including the shell-syntax refusal that
/// sits on it.
async fn run_workspace_turn_with<B: AsRef<[u8]>>(
    files: &[(&str, B)],
    commands: &[(crate::entities::workspace::CommandSlot, &str)],
    prompt: &str,
) -> Option<(tempfile::TempDir, String, Vec<(String, String, String)>)> {
    use crate::features::project_command::ProjectProgress;
    use crate::features::tools::code::WORKSPACE_TOOL_IDS;

    let ws = tempfile::tempdir().unwrap();
    for (name, body) in files {
        let path = ws.path().join(name);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, body).unwrap();
    }
    let (dir, cmd_tx, mut evt_rx, handle) = spawn_orch_live()?;
    let _ = narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        WORKSPACE_TOOL_IDS.iter().map(|id| (*id).into()).collect(),
    )
    .await;
    cmd_tx
        .send(AppCommand::ProjectAttach {
            path: ws.path().to_string_lossy().into_owned(),
        })
        .unwrap();
    let attached = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ProjectProgress(_)))
        .await
        .unwrap();
    assert!(
        matches!(
            attached,
            AppEvent::ProjectProgress(ProjectProgress::Attached { .. })
        ),
        "the project did not attach: {attached:?}"
    );

    for (slot, line) in commands {
        cmd_tx
            .send(AppCommand::ProjectSlot {
                slot: *slot,
                action: crate::features::project_command::SlotAction::Set((*line).to_string()),
            })
            .unwrap();
        let set = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ProjectProgress(_)))
            .await
            .unwrap();
        assert!(
            matches!(
                set,
                AppEvent::ProjectProgress(ProjectProgress::CommandSet { .. })
            ),
            "the {slot:?} command did not take: {set:?}"
        );
    }

    let (answer, calls) = run_turn_capture_args(&cmd_tx, &mut evt_rx, prompt).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    drop(dir);
    for (name, args, result) in &calls {
        eprintln!(
            "{name}({}) => {}",
            args.chars().take(240).collect::<String>(),
            result.chars().take(240).collect::<String>()
        );
    }
    eprintln!("reply: {answer}");
    Some((ws, answer, calls))
}

/// The stage-0 probe, now on the real mechanism (docs/history/code-workspace.md §7): a
/// project that does not compile, and the assistant fixes it through
/// `code_edit`.
///
/// `rustc` is the ground truth on both sides — the fixture must fail to compile
/// before and succeed after, and the built program must print the right number,
/// so a "fix" that deletes the arithmetic cannot pass.
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_edit_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_edit_e2e_live() {
    use crate::features::tools::code::CODE_EDIT_ID;
    let files = [
        (
            "src/main.rs",
            "mod stats;\n\nfn main() {\n    let samples = vec![2.0, 4.0, 6.0, 8.0];\n    println!(\"mean={}\", stats::mean(&samples));\n}\n",
        ),
        (
            "src/stats.rs",
            "/// Arithmetic mean of the samples.\npub fn mean(values: &[f64]) -> f64 {\n    let total: f64 = values.iter().sum();\n    total / values.len()\n}\n",
        ),
    ];
    let prompt = "Проект в рабочей папке не собирается. cargo build выдаёт:\n\n\
         error[E0277]: cannot divide `f64` by `usize`\n\
         \x20--> src/stats.rs:4:5\n\
         \x20 |\n\
         4 |     total / values.len()\n\
         \x20 |     ^^^^^^^^^^^^^^^^^^^^ no implementation for `f64 / usize`\n\n\
         Разберись и почини.";

    let Some((ws, _answer, calls)) = run_workspace_turn(&files, prompt).await else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    assert!(
        calls.iter().any(|(n, _, _)| n == CODE_EDIT_ID),
        "the model never called {CODE_EDIT_ID}"
    );
    match rustc_run(ws.path()) {
        Ok(stdout) => assert_eq!(
            stdout.trim(),
            "mean=5",
            "it builds, but no longer computes the right answer"
        ),
        Err(err) => panic!("does not compile after the edit:\n{err}"),
    }
}

/// Local files in their own encoding (docs/research/local-file-encoding.md): a
/// windows-1251 source attached as a project, and an edit the assistant makes through
/// `code_edit` — the path research §1 measured writing `EF BF BD` over every Russian
/// letter of such a file's comments while the changes screen showed one line.
///
/// Ground truth is the bytes, and what is asserted is the outcome, not the model's
/// wording: no `EF BF BD`, the untouched first line byte for byte, the file still reading
/// as windows-1251 without loss, and the number changed. Whether the model also rewrote
/// the comment about the number is printed, not asserted — that is a reasonable edit.
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_edit_legacy_encoding_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_edit_legacy_encoding_e2e_live() {
    use crate::features::tools::code::CODE_EDIT_ID;
    // cyrillic-ok:start — the fixture is a Russian source file: its comments are the
    // text under test, and a string's continuation line reads to the scanner as a comment.
    const FIRST_LINE: &str = "// Расчёт скидки для постоянного покупателя.\r\n";
    const REST: &str = "pub fn discount(orders: u32) -> u32 {\r\n\
        \x20   // Скидка растёт с каждым десятым заказом.\r\n    orders / 10\r\n}\r\n";
    // cyrillic-ok:end
    let cp1251 = |t: &str| encoding_rs::WINDOWS_1251.encode(t).0.into_owned();
    let files = [("src/discount.rs", cp1251(&format!("{FIRST_LINE}{REST}")))];
    let prompt = "В файле src/discount.rs скидка растёт слишком медленно: пусть она растёт \
        с каждым пятым заказом, а не с каждым десятым. Поменяй это в коде.";

    let Some((ws, answer, calls)) = run_workspace_turn(&files, prompt).await else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let after = std::fs::read(ws.path().join("src/discount.rs")).unwrap();
    let (text, _) = encoding_rs::WINDOWS_1251.decode_without_bom_handling(&after);
    eprintln!("answer: {answer}\n--- the file after the turn, read as windows-1251 ---\n{text}");
    assert!(
        calls.iter().any(|(n, _, _)| n == CODE_EDIT_ID),
        "the model never called {CODE_EDIT_ID}"
    );
    let replacement = after
        .windows(3)
        .filter(|w| *w == [0xEF, 0xBF, 0xBD])
        .count();
    assert_eq!(replacement, 0, "EF BF BD was written into the file");
    assert!(
        after.starts_with(&cp1251(FIRST_LINE)),
        "the untouched first line changed"
    );
    assert!(
        crate::shared::text_decode::round_trips(&after, encoding_rs::WINDOWS_1251),
        "the file no longer reads as windows-1251 without loss"
    );
    assert!(
        text.contains("orders / 5"),
        "the divisor did not change to 5"
    );
    eprintln!(
        "the comment about the number was {}",
        if text.contains("десятым") {
            "left as it was"
        } else {
            "rewritten too"
        }
    );
}

/// Runs `cargo` in `dir` and returns its combined output, or an error.
fn cargo_in(dir: &std::path::Path, args: &[&str]) -> Result<String, String> {
    let out = std::process::Command::new("cargo")
        .args(args)
        .current_dir(dir)
        .output()
        .map_err(|e| format!("could not run cargo: {e}"))?;
    let text = format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    if out.status.success() {
        Ok(text)
    } else {
        Err(text)
    }
}

/// Code workspace, **stage 3 live check** (docs/history/code-workspace.md §6): a project
/// that does not compile, a build command the *user* configured, and the
/// assistant working the loop — build, read the error, fix, build again.
///
/// `cargo` rather than a bare `rustc`, deliberately (docs/history/code-workspace.md
/// §4.1): only a real build system puts a `cargo → rustc` process tree behind
/// the timeout and the kill this stage exists for, and only a real compiler's
/// diagnostics are what the model has to read to find the fault. The crate has
/// no dependencies and builds `--offline`, so nothing here touches the network.
///
/// Ground truth is on **both** sides and is not a string comparison against the
/// source: the fixture must fail to build before the turn, and afterwards
/// `cargo run` must print the right number — so a "fix" that deletes the
/// arithmetic cannot pass (the stage-0 rule).
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_build_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_build_e2e_live() {
    use crate::entities::workspace::CommandSlot;
    use crate::features::tools::code::{CODE_BUILD_ID, CODE_EDIT_ID};

    let files = [
        (
            "Cargo.toml",
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n",
        ),
        (
            "src/main.rs",
            "mod stats;\n\nfn main() {\n    let samples = vec![2.0, 4.0, 6.0, 8.0];\n    println!(\"mean={}\", stats::mean(&samples));\n}\n",
        ),
        (
            "src/stats.rs",
            "/// Arithmetic mean of the samples.\npub fn mean(values: &[f64]) -> f64 {\n    let total: f64 = values.iter().sum();\n    total / values.len()\n}\n",
        ),
    ];
    // The prompt says nothing about *what* is wrong and quotes no code: the
    // compiler error can only come from running the build command, which is the
    // whole point of the stage (docs/lessons.md §2 — a test worded so it can be
    // satisfied without doing the thing it checks measures nothing).
    let prompt = "Собери проект в рабочей папке. Если сборка падает — разберись, \
         почини и собери снова.";

    let Some((ws, _answer, calls)) = run_workspace_turn_with(
        &files,
        &[(CommandSlot::Build, "cargo build --offline")],
        prompt,
    )
    .await
    else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };

    let builds: Vec<&(String, String, String)> = calls
        .iter()
        .filter(|(n, _, _)| n == CODE_BUILD_ID)
        .collect();
    assert!(
        !builds.is_empty(),
        "the model never called {CODE_BUILD_ID} — it cannot have seen the error"
    );
    // The first build must have carried the compiler's own diagnostic: that is
    // what proves the command really ran and its stderr was captured, rather
    // than the model recognizing the bug by eye.
    assert!(
        builds[0].2.contains("E0277") || builds[0].2.contains("error"),
        "the first build did not return the compiler error: {}",
        builds[0].2
    );
    assert!(
        calls.iter().any(|(n, _, _)| n == CODE_EDIT_ID),
        "the model never called {CODE_EDIT_ID}"
    );
    // Reported rather than asserted: whether it rebuilt to confirm is a matter
    // of the model's judgement, and pinning it would pin the route rather than
    // the outcome (the §7.6 pattern).
    eprintln!("builds: {} · calls: {}", builds.len(), calls.len());

    match cargo_in(ws.path(), &["run", "--offline", "-q"]) {
        Ok(stdout) => assert!(
            stdout.contains("mean=5"),
            "it builds, but no longer computes the right answer: {stdout}"
        ),
        Err(err) => panic!("does not build after the turn:\n{err}"),
    }
}

/// The stage-3 safety property: a command line the user never typed cannot be
/// run. The tools take no arguments at all, so there is nothing for the model to
/// put a second command into — and a slot with no line is not offered.
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_command_gate_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_command_gate_e2e_live() {
    use crate::features::tools::code::{CODE_BUILD_ID, CODE_RUN_ID, CODE_TEST_ID};

    let files = [("src/main.rs", "fn main() { println!(\"hi\"); }\n")];
    let prompt = "Запусти тесты этого проекта.";

    // No slot is configured, so none of the three tools exists this turn.
    let Some((_ws, answer, calls)) = run_workspace_turn(&files, prompt).await else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    for id in [CODE_BUILD_ID, CODE_RUN_ID, CODE_TEST_ID] {
        assert!(
            !calls.iter().any(|(n, _, _)| n == id),
            "{id} must not exist without a configured command: {calls:?}"
        );
    }
    assert!(!answer.trim().is_empty(), "the model said nothing at all");
}

/// Stage 2's own commitment (docs/history/code-workspace.md §7.3): the **refusal paths
/// have to work live**, and stage 0 never exercised them because the model never
/// missed.
///
/// So the miss is arranged: the user quotes the line to change, and their quote
/// is subtly not what the file says — a space that is not there. A model that
/// trusts the quote gets `tool.code.edit.not_found`, whose whole job is to say
/// what to do next. What is asserted is the **outcome** — the file ends up
/// correct — because a model that reads first and never misses is behaving
/// better, not worse; whether the miss happened is *reported*, so the rate is
/// visible across runs rather than assumed (docs/lessons.md §3).
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_edit_recovers_from_a_miss_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_edit_recovers_from_a_miss_e2e_live() {
    use crate::features::tools::code::CODE_EDIT_ID;
    let files = [
        (
            "src/main.rs",
            "mod config;\n\nfn main() {\n    println!(\"retries={}\", config::RETRY_LIMIT);\n}\n",
        ),
        // The file says `u32 = 3`; the user's quote below says `u32=3`.
        (
            "src/config.rs",
            "/// How many times a request is retried.\npub const RETRY_LIMIT: u32 = 3;\n",
        ),
    ];
    let prompt = "Подними лимит ретраев с 3 до 5. Строка такая:\n\n\
         pub const RETRY_LIMIT: u32=3;\n\n\
         Поменяй её в проекте.";

    let Some((ws, _answer, calls)) = run_workspace_turn(&files, prompt).await else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let edits: Vec<&(String, String, String)> =
        calls.iter().filter(|(n, _, _)| n == CODE_EDIT_ID).collect();
    let refusal = crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru)
        .tf("tool.code.edit.not_found", &[("path", "src/config.rs")]);
    let head: String = refusal.chars().take(30).collect();
    let missed = edits.iter().filter(|(_, _, r)| r.contains(&head)).count();
    eprintln!("PROBE: {} edit calls, {missed} of them missed", edits.len());

    assert!(!edits.is_empty(), "the model never called {CODE_EDIT_ID}");
    match rustc_run(ws.path()) {
        Ok(stdout) => assert_eq!(
            stdout.trim(),
            "retries=5",
            "the user's approximate quote must not cost them the change"
        ),
        Err(err) => panic!("does not compile after the edit:\n{err}"),
    }
}

/// A refused edit must leave the file **untouched** — live, not only in unit
/// tests: the ambiguity refusal is what stops a one-line fragment occurring
/// twice from being changed in the wrong place.
///
/// The fixture puts the obvious fragment in the file twice with identical
/// surroundings, and the request names which one to change. Whatever route the
/// model takes, exactly one of them must end up changed and the other left
/// alone.
///
/// `#[ignore]`, manual: `MINDFORK_ENGINE_URL=…/v1 cargo test
/// code_edit_ambiguity_e2e_live -- --ignored --nocapture --test-threads=1`.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn code_edit_ambiguity_e2e_live() {
    let files = [
        (
            "src/main.rs",
            "mod limits;\n\nfn main() {\n    println!(\"{} {}\", limits::READ_TIMEOUT, limits::WRITE_TIMEOUT);\n}\n",
        ),
        (
            "src/limits.rs",
            "/// Reading.\npub const READ_TIMEOUT: u64 = 30;\n\n/// Writing.\npub const WRITE_TIMEOUT: u64 = 30;\n",
        ),
    ];
    let prompt = "В проекте таймаут записи должен быть 60, а таймаут чтения оставь как есть. \
         Поправь.";

    let Some((ws, _answer, _calls)) = run_workspace_turn(&files, prompt).await else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let after = std::fs::read_to_string(ws.path().join("src/limits.rs")).unwrap();
    eprintln!("PROBE: src/limits.rs now:\n{after}");
    assert!(
        after.contains("WRITE_TIMEOUT: u64 = 60"),
        "the write timeout had to change: {after}"
    );
    assert!(
        after.contains("READ_TIMEOUT: u64 = 30"),
        "the read timeout had to be left alone — a blind replace_all would take both: {after}"
    );
}

/// The sub-agent track's **go/no-go** (docs/research/subagent-chats.md §6):
/// the main agent delegates a question only a tool can answer, the sub-agent
/// actually *uses* the tool, and the answer comes back through the delegation.
/// The planted token is nonsense the model cannot know, and the parent is told
/// not to read the file itself — a parent that shortcuts fails the
/// `call_subagent` assertion, which is the no-go signal this smoke exists for.
/// `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn subagent_with_tools_e2e_live() {
    let sandbox = tempfile::tempdir().unwrap();
    let file = sandbox.path().join("facts.txt");
    std::fs::write(
        &file,
        "Internal note.\nThe project's secret codename is ZARNOVIK-7741.\nDo not share outside the team.\n",
    )
    .unwrap();
    let mut cfg = AppConfig::default();
    cfg.tools.fs_enabled = true;
    cfg.tools.fs_root = Some(sandbox.path().to_string_lossy().to_string());
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Delegator".into(),
            system_message: "You are a coordinator. Reply in English. You never read files \
                 yourself: whenever a file has to be read, you delegate the whole task to a \
                 sub-agent with the call_subagent tool, giving it the exact file path and \
                 telling it to use fs_read, then you report what the sub-agent found."
                .into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                // The delegation and the one tool the delegate needs — nothing
                // else can be reached for. `fs_read` has to be in the parent's
                // set because the child's set is derived from it (research §3.3).
                enabled_tools: Some(vec!["call_subagent".to_string(), "fs_read".to_string()]),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let chat_id = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .and_then(|e| match e {
            AppEvent::ChatActivated { id, .. } => Some(id),
            _ => None,
        })
        .unwrap();

    let ask = format!(
        "What is the project's secret codename? It is written in the file {}. \
         Do not read it yourself — delegate to a sub-agent (call_subagent) and \
         tell it to read that file with fs_read. Then answer with the codename.",
        file.display()
    );
    let (reply, calls) = run_turn_capture_args(&cmd_tx, &mut evt_rx, &ask).await;
    eprintln!("reply: {reply}");
    for (n, a, r) in &calls {
        eprintln!(
            "call {n}({}) -> {}",
            a.chars().take(160).collect::<String>(),
            r.chars().take(200).collect::<String>()
        );
    }
    // Let the debounced save land before reading the file back.
    tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    // GO: the parent delegated (the feed card is the parent's only call)...
    let delegated = calls
        .iter()
        .filter(|(n, _, _)| n == "call_subagent")
        .count();
    assert!(delegated >= 1, "the parent never delegated: {calls:?}");
    assert!(
        !calls.iter().any(|(n, _, _)| n == "fs_read"),
        "the parent read the file itself instead of delegating"
    );
    // ...the sub-agent used the tool (its calls are on the run, not in the feed)...
    let chat = Storage::open(Paths::with_root(_d.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    let run = chat
        .messages
        .iter()
        .flat_map(|m| m.tool_calls.iter())
        .find_map(|r| r.subagent.as_deref())
        .expect("a run on the call's record");
    eprintln!(
        "run «{}»: {} messages, outcome {:?}, {} tokens",
        run.title,
        run.messages.len(),
        run.outcome,
        run.tokens
    );
    let child_reads = run
        .messages
        .iter()
        .flat_map(|m| m.tool_calls.iter())
        .filter(|r| r.name == "fs_read")
        .count();
    assert!(
        child_reads >= 1,
        "the sub-agent did not use fs_read: {:?}",
        run.messages
    );
    assert_eq!(
        run.outcome,
        Some(crate::entities::subagent::RunOutcome::Completed)
    );
    // ...and the token came through the delegation into the parent's answer.
    assert!(
        reply.contains("ZARNOVIK-7741") || reply.contains("ZARNOVIK"),
        "the codename did not reach the parent's reply: {reply}"
    );
}

/// End-to-end against a live model (spec §6.4): a reply cancelled mid-stream,
/// then `/continue` — the resumed turn appends into the same message, and the
/// concatenation of everything the two turns streamed equals the stored text
/// byte-for-byte. That single equality carries the whole feature: the echo
/// filter proved itself against the real server (the llama.cpp path echoes
/// the prefill — research §7.1), nothing doubled, nothing was lost at the
/// seam, and the merge landed in place rather than as a second message.
#[tokio::test]
#[ignore = "requires a running OpenAI-compatible server (MINDFORK_ENGINE_URL)"]
async fn continue_e2e_live() {
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(no_auto_cfg()) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    cmd_tx
        .send(AppCommand::SendMessage(
            "Count from one to thirty in words, one number per line, no other text.".into(),
        ))
        .unwrap();
    // Cancel after the first visible text chunk — a thoughts-only cut would be
    // refused (fork F4), and a thinking model fronts its reasoning.
    let mut streamed = String::new();
    let mut cancelled = false;
    while let Some(ev) = evt_rx.recv().await {
        match ev {
            AppEvent::Chunk { text, .. } => {
                streamed.push_str(&text);
                if !cancelled {
                    cmd_tx.send(AppCommand::Cancel).unwrap();
                    cancelled = true;
                }
            }
            AppEvent::Finished { reason, .. } => {
                assert_eq!(
                    reason,
                    FinishReason::Cancelled,
                    "the reply finished before the cancel landed — the fixture \
                     needs a longer task, not a different feature"
                );
                break;
            }
            _ => {}
        }
    }
    assert!(cancelled, "the model never produced text to cut");
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatList(_)))
        .await
        .unwrap();

    cmd_tx.send(AppCommand::ContinueLast).unwrap();
    let mut resumed = false;
    while let Some(ev) = evt_rx.recv().await {
        match ev {
            AppEvent::GenerationStarted { continuation, .. } => {
                assert!(continuation, "a text tail must resume via prefill");
                resumed = true;
            }
            AppEvent::Chunk { text, .. } => streamed.push_str(&text),
            AppEvent::Finished { reason, .. } => {
                assert!(
                    matches!(reason, FinishReason::Stop | FinishReason::Length),
                    "the resumed turn must end on its own: {reason:?}"
                );
                break;
            }
            _ => {}
        }
    }
    assert!(resumed, "the continuation turn never started");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let reopened = Storage::open(Paths::with_root(dir.path())).unwrap();
    let chat = reopened
        .json()
        .load_chats()
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    assert_eq!(
        chat.messages.len(),
        2,
        "the continuation must land in place, not as a second reply: {:?}",
        chat.messages
            .iter()
            .map(|m| (m.role, m.text.chars().take(30).collect::<String>()))
            .collect::<Vec<_>>()
    );
    assert_eq!(
        chat.messages[1].text, streamed,
        "the stored reply must be exactly what streamed across both turns — \
         no echo doubling, no gap at the seam"
    );
    let head: String = streamed.chars().take(160).collect();
    println!(
        "continued reply, {} chars, head: {head:?}",
        streamed.chars().count()
    );
}

/// `run_dialogue` stage-1 go/no-go (spec §9.13,
/// docs/research/two-agent-dialogue.md §6): asked for a short finite scene,
/// the model stages it through the tool — the probe's café fixture graduated
/// into the smoke — and the landed record carries a role-encoded,
/// strictly-alternating transcript that the director ended (or the cap
/// backstopped). The profile is narrowed to the one tool under test
/// ("remove the alternative", lessons §9); the ask is situation-shaped, not
/// a numbered script.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn dialogue_e2e_live() {
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(AppConfig::default()) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (_profile, chat_id) =
        narrow_profile_to(&cmd_tx, &mut evt_rx, vec!["run_dialogue".into()]).await;
    let ask = "I'm drafting a café scene. Stage it live with run_dialogue: \
        barista Mara (warm, frazzled by the morning rush, remakes wrong drinks \
        for free) and customer Jonas (in a hurry for his tram, got an oat \
        latte instead of his double espresso; opens the dialogue politely \
        asking to fix it). Tell each persona to reply with one spoken line \
        only, no narration. Direct it yourself and stop once the mix-up is \
        resolved and they part on good terms; cap it at 10 lines.";
    let (reply, calls) = run_turn_capture(&cmd_tx, &mut evt_rx, ask).await;
    eprintln!(
        "parent reply: {}",
        reply.chars().take(300).collect::<String>()
    );
    // The tool was actually exercised (lessons §2 — a run that never calls it
    // measures nothing).
    assert!(
        calls.iter().any(|(n, _)| n == "run_dialogue"),
        "the model never called run_dialogue; calls: {calls:?}"
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let chat = super::subagent::load(dir.path(), chat_id);
    let record = chat
        .messages
        .iter()
        .flat_map(|m| m.tool_calls.iter())
        .find(|r| r.name == "run_dialogue")
        .expect("the call's record");
    let run = record.subagent.as_deref().expect("the run on the record");
    assert_eq!(run.kind, crate::entities::subagent::RunKind::Dialogue);
    assert_eq!(run.participants.len(), 2);
    let outcome = run.outcome.expect("the run reported how it ended");
    assert!(
        matches!(
            outcome,
            crate::entities::subagent::RunOutcome::Completed
                | crate::entities::subagent::RunOutcome::RoundLimit
        ),
        "unexpected outcome: {outcome:?}"
    );
    // The transcript: at least one exchange, spoken lines strictly
    // alternating around the director's System rows.
    let spoken: Vec<MessageRole> = run
        .messages
        .iter()
        .filter(|m| m.role != MessageRole::System)
        .map(|m| m.role)
        .collect();
    assert!(
        spoken.len() >= 3,
        "too short: {} spoken lines",
        spoken.len()
    );
    for pair in spoken.windows(2) {
        assert_ne!(pair[0], pair[1], "two consecutive lines by one side");
    }
    println!(
        "dialogue landed: {:?}, {} spoken lines, {} tokens, title {:?}",
        outcome,
        spoken.len(),
        run.tokens,
        run.title
    );
    for m in &run.messages {
        let side = match m.role {
            MessageRole::Assistant => "A",
            MessageRole::User => "B",
            _ => "D",
        };
        println!(
            "  [{side}] {}",
            m.text.chars().take(120).collect::<String>()
        );
    }
}

/// `start_dialogue` go/no-go (spec §9.13, docs/research/background-dialogues.md
/// §7): asked for a scene it will read later **and** for something it can
/// answer now, the model stages the scene in the background and answers the
/// rest in the same reply; the scene outlives that turn, lands on the record
/// by id, and its closing result arrives as a task notification the app's own
/// woken turn reports. The profile carries both dialogue tools, so choosing
/// the background one is the model's decision and not a lack of alternatives
/// — the one place this smoke deliberately departs from "remove the
/// alternative" (lessons §9), because the choice *is* what is measured.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn background_dialogue_e2e_live() {
    let mut cfg = AppConfig::default();
    cfg.tools.subagent_background = true;
    // Two sessions, so the scene and the parent's turns need not queue behind
    // each other on a stack that has the room; the budget is what decides.
    cfg.engine.external.sessions = 2;
    cfg.engine.managed.sessions = 2;
    // The measured ceiling for an open-ended ask on a thinking model
    // (docs/journal/ci.md; lessons §9): at the default 2048 this smoke's
    // two-part question spent the whole cap in `reasoning_content` and the
    // parent said nothing at all, twice in a row.
    cfg.default_sampling.max_tokens = Some(4096);
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let Some((dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let (_profile, chat_id) = narrow_profile_to(
        &cmd_tx,
        &mut evt_rx,
        vec!["start_dialogue".into(), "run_dialogue".into()],
    )
    .await;

    let ask = "Two things. (1) Stage me a café scene I will read later: barista \
        Mara (warm, frazzled by the morning rush) and customer Jonas (in a hurry \
        for his tram, got an oat latte instead of his double espresso; he opens \
        by asking politely to fix it). Tell each persona to reply with one spoken \
        line only, no narration. Direct it yourself, stop once the mix-up is \
        resolved, cap it at 6 lines — and do not wait for it, I want the \
        transcript later, not now. (2) Right now: which is larger, 17 × 23 or 400?";
    let (reply, calls) = run_turn_capture(&cmd_tx, &mut evt_rx, ask).await;
    eprintln!(
        "parent reply: {}",
        reply.chars().take(300).collect::<String>()
    );
    for (n, a) in &calls {
        eprintln!("call {n}({})", a.chars().take(160).collect::<String>());
    }
    assert!(
        !reply.trim().is_empty() || !calls.is_empty(),
        "the parent turn produced nothing at all — the all-thinking empty turn \
         (lessons §9), not a verdict on the tool: re-run before reading it as one"
    );
    assert!(
        calls.iter().any(|(n, _)| n == "start_dialogue"),
        "the model did not stage the scene in the background; calls: {calls:?}"
    );
    assert!(
        !calls.iter().any(|(n, _)| n == "run_dialogue"),
        "the model waited for the scene instead of backgrounding it: {calls:?}"
    );

    // The scene ends and the app wakes the assistant on the notification: a
    // turn nobody sent a message for.
    let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(900);
    let mut wake = String::new();
    loop {
        let ev = tokio::time::timeout_at(deadline, evt_rx.recv())
            .await
            .expect("the scene lands and the wake turn ends within fifteen minutes")
            .expect("the event stream stays open");
        match ev {
            AppEvent::Chunk { text, .. } => wake.push_str(&text),
            AppEvent::Finished { .. } => break,
            _ => {}
        }
    }
    eprintln!("wake reply: {wake}");
    tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let chat = super::subagent::load(dir.path(), chat_id);
    let run = chat
        .children()
        .find(|r| r.background)
        .expect("a background run on the record");
    assert_eq!(run.kind, crate::entities::subagent::RunKind::Dialogue);
    assert_eq!(run.participants.len(), 2);
    let outcome = run.outcome.expect("the scene reported how it ended");
    assert!(
        matches!(
            outcome,
            crate::entities::subagent::RunOutcome::Completed
                | crate::entities::subagent::RunOutcome::RoundLimit
        ),
        "unexpected outcome: {outcome:?}"
    );
    let spoken: Vec<MessageRole> = run
        .messages
        .iter()
        .filter(|m| m.role != MessageRole::System)
        .map(|m| m.role)
        .collect();
    assert!(
        spoken.len() >= 3,
        "too short: {} spoken lines",
        spoken.len()
    );
    for pair in spoken.windows(2) {
        assert_ne!(pair[0], pair[1], "two consecutive lines by one side");
    }
    println!(
        "scene landed: {:?}, {} spoken lines, {} tokens, title {:?}",
        outcome,
        spoken.len(),
        run.tokens,
        run.title
    );

    // The notification is the dialogue's, names the scene, and carries the
    // closing result; the woken reply is about the scene rather than empty.
    let note = chat
        .messages
        .iter()
        .find(|m| m.is_notification())
        .expect("a task notification row");
    println!("notification: {}", note.text);
    assert_eq!(note.notification, Some(run.id));
    assert!(
        note.text.contains(&run.title),
        "the notification does not name the scene: {}",
        note.text
    );
    assert!(
        !wake.trim().is_empty(),
        "the woken turn said nothing at all — the empty-turn mode (research §3)"
    );
}

/// The parallel group's live proof (docs/research/parallel-subagents.md §7):
/// the parent is asked for two codenames in two files and told to delegate
/// **both** reads in one reply; with `subagent_parallel = 2` and two sessions
/// the two runs must overlap in time — the second started before the first
/// finished — and both must land on the one assistant message, each having
/// used `fs_read`, with both tokens in the parent's reply. A parent that
/// delegates one at a time (two rounds) fails the overlap assertion, which is
/// the no-go signal this smoke exists for. The dashes are folded on both
/// sides: a model may render a code with a non-breaking hyphen
/// (docs/lessons.md §9). `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn parallel_subagents_e2e_live() {
    let sandbox = tempfile::tempdir().unwrap();
    let file_a = sandbox.path().join("alpha.txt");
    let file_b = sandbox.path().join("beta.txt");
    std::fs::write(
        &file_a,
        "Internal note.\nThe alpha codename is ZARNOVIK-7741.\n",
    )
    .unwrap();
    std::fs::write(
        &file_b,
        "Internal note.\nThe beta codename is KELVAR-3390.\n",
    )
    .unwrap();
    let mut cfg = AppConfig::default();
    cfg.tools.fs_enabled = true;
    cfg.tools.fs_root = Some(sandbox.path().to_string_lossy().to_string());
    cfg.tools.subagent_parallel = 2;
    cfg.engine.external.sessions = 2;
    cfg.engine.managed.sessions = 2;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Delegator".into(),
            system_message: "You are a coordinator. Reply in English. You never read files \
                 yourself: whenever files have to be read, you delegate each file to its own \
                 sub-agent with the call_subagent tool — several call_subagent calls in ONE \
                 reply when there are several files — giving each the exact file path and \
                 telling it to use fs_read, then you report what the sub-agents found."
                .into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                enabled_tools: Some(vec!["call_subagent".to_string(), "fs_read".to_string()]),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let chat_id = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .and_then(|e| match e {
            AppEvent::ChatActivated { id, .. } => Some(id),
            _ => None,
        })
        .unwrap();

    let ask = format!(
        "What are the alpha and beta codenames? The alpha one is in the file {} and the \
         beta one in {}. Do not read them yourself — delegate each file to its own \
         sub-agent (call_subagent), both in this same reply, and tell each to read its \
         file with fs_read. Then answer with both codenames.",
        file_a.display(),
        file_b.display()
    );
    let (reply, calls) = run_turn_capture_args(&cmd_tx, &mut evt_rx, &ask).await;
    eprintln!("reply: {reply}");
    for (n, a, r) in &calls {
        eprintln!(
            "call {n}({}) -> {}",
            a.chars().take(120).collect::<String>(),
            r.chars().take(160).collect::<String>()
        );
    }
    tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let delegated = calls
        .iter()
        .filter(|(n, _, _)| n == "call_subagent")
        .count();
    assert!(
        delegated >= 2,
        "the parent did not delegate twice: {calls:?}"
    );
    assert!(
        !calls.iter().any(|(n, _, _)| n == "fs_read"),
        "the parent read a file itself instead of delegating"
    );
    let chat = Storage::open(Paths::with_root(_d.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    // Both runs on one assistant message — one reply, one group.
    let grouped: Vec<&crate::entities::subagent::SubagentRun> = chat
        .messages
        .iter()
        .filter_map(|m| {
            let runs: Vec<_> = m
                .tool_calls
                .iter()
                .filter_map(|r| r.subagent.as_deref())
                .collect();
            (runs.len() >= 2).then_some(runs)
        })
        .next()
        .expect("two runs on one assistant message");
    for run in &grouped {
        eprintln!(
            "run «{}»: {} messages, outcome {:?}, {:?}{:?}",
            run.title,
            run.messages.len(),
            run.outcome,
            run.created_at,
            run.finished_at
        );
        let reads = run
            .messages
            .iter()
            .flat_map(|m| m.tool_calls.iter())
            .filter(|r| r.name == "fs_read")
            .count();
        assert!(
            reads >= 1,
            "the sub-agent «{}» never used fs_read",
            run.title
        );
    }
    // GO: the second run started before the first finished — they ran at once.
    let first_end = grouped[0].finished_at.expect("the first run ended");
    assert!(
        grouped[1].created_at < first_end,
        "the runs did not overlap: {:?} vs {:?}",
        grouped[1].created_at,
        first_end
    );
    let fold = |s: &str| s.replace(['\u{2010}', '\u{2011}', '\u{2012}', '\u{2013}'], "-");
    let reply = fold(&reply);
    assert!(
        reply.contains("ZARNOVIK-7741"),
        "alpha token missing: {reply}"
    );
    assert!(reply.contains("KELVAR-3390"), "beta token missing: {reply}");
}

/// The concurrent segment on a live model (docs/research/concurrent-tools.md
/// §7): two planted-token files, a profile with `fs_read` alone, the
/// assistant told to read both **in one reply**, `concurrent_calls = 2`.
/// GO: two `fs_read` records on one assistant message, each holding the
/// result of its own call; both cards opened before either closed — the
/// overlap proof, since the segment opens every card first and the
/// sequential path never does (it closes the cards at the round's end,
/// after every call ran, but opens the second only after the first ran);
/// and both codenames in the reply.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn concurrent_tools_e2e_live() {
    let sandbox = tempfile::tempdir().unwrap();
    let file_a = sandbox.path().join("alpha.txt");
    let file_b = sandbox.path().join("beta.txt");
    std::fs::write(
        &file_a,
        "Internal note.\nThe alpha codename is ZARNOVIK-7741.\n",
    )
    .unwrap();
    std::fs::write(
        &file_b,
        "Internal note.\nThe beta codename is KELVAR-3390.\n",
    )
    .unwrap();
    let mut cfg = AppConfig::default();
    cfg.tools.fs_enabled = true;
    cfg.tools.fs_root = Some(sandbox.path().to_string_lossy().to_string());
    cfg.engine.external.concurrent_calls = 2;
    cfg.engine.managed.concurrent_calls = 2;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Clerk".into(),
            system_message: "You are a file clerk. Reply in English. When asked about several \
                 files, read them all with fs_read in ONE reply — one fs_read call per file, \
                 all in the same message — and only then answer."
                .into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                enabled_tools: Some(vec!["fs_read".to_string()]),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let chat_id = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .and_then(|e| match e {
            AppEvent::ChatActivated { id, .. } => Some(id),
            _ => None,
        })
        .unwrap();

    let ask = format!(
        "What are the alpha and beta codenames? The alpha one is in the file {} and the \
         beta one in {}. Read both files with fs_read in this same reply, then answer \
         with both codenames.",
        file_a.display(),
        file_b.display()
    );
    cmd_tx.send(AppCommand::SendMessage(ask)).unwrap();
    let mut reply = String::new();
    let mut opens: Vec<usize> = Vec::new();
    let mut closes: Vec<usize> = Vec::new();
    let mut n = 0usize;
    while let Some(ev) = evt_rx.recv().await {
        match &ev {
            AppEvent::Chunk { text, .. } => reply.push_str(text),
            AppEvent::ToolCallStarted { name, .. } if name == "fs_read" => opens.push(n),
            AppEvent::ToolCall {
                name,
                arguments,
                result,
                ..
            } if name == "fs_read" => {
                closes.push(n);
                eprintln!(
                    "call fs_read({}) -> {}",
                    arguments.chars().take(100).collect::<String>(),
                    result.chars().take(100).collect::<String>()
                );
            }
            AppEvent::Finished { .. } => break,
            _ => {}
        }
        n += 1;
    }
    eprintln!("reply: {reply}");
    tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    assert!(
        opens.len() >= 2,
        "the model did not read both files in one reply (opens {opens:?})"
    );
    assert_eq!(opens.len(), closes.len(), "every card closed");
    // GO: the second card opened before the first closed — one segment.
    assert!(
        opens[1] < closes[0],
        "the reads did not run as one segment: opens {opens:?}, closes {closes:?}"
    );
    let chat = Storage::open(Paths::with_root(_d.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    let round = chat
        .messages
        .iter()
        .find(|m| m.tool_calls.iter().filter(|r| r.name == "fs_read").count() >= 2)
        .expect("two fs_read records on one assistant message");
    // Each record holds the result of its own call, whatever order they landed.
    for r in round.tool_calls.iter().filter(|r| r.name == "fs_read") {
        let path = r.arguments["path"]
            .as_str()
            .unwrap_or_default()
            .to_lowercase();
        let result = r.result.as_deref().unwrap_or_default();
        if path.contains("alpha") {
            assert!(result.contains("ZARNOVIK-7741"), "{path}: {result}");
        } else if path.contains("beta") {
            assert!(result.contains("KELVAR-3390"), "{path}: {result}");
        } else {
            panic!("a read of an unexpected path: {path}");
        }
    }
    let fold = |s: &str| s.replace(['\u{2010}', '\u{2011}', '\u{2012}', '\u{2013}'], "-");
    let reply = fold(&reply);
    assert!(
        reply.contains("ZARNOVIK-7741"),
        "alpha token missing: {reply}"
    );
    assert!(reply.contains("KELVAR-3390"), "beta token missing: {reply}");
}

use futures_util::StreamExt as _;

// ---------------------------------------------------------------------------
// Admission by budget (docs/research/admission-by-budget.md §7): the guard
// against the unified pool's collective failure, driven through the app
// against a real `llama-server` small enough to overflow on purpose.
// ---------------------------------------------------------------------------

/// The parent's rounds scripted, the children's live: requests whose system
/// message carries the children's persona go to the real server (and are
/// counted — how many streams the server saw open at once), every other
/// request plays the next script. The small model's willingness to delegate
/// twice is not what the smoke measures, so it is taken out of the picture.
struct ScriptedParent {
    live: Arc<dyn EngineBackend>,
    scripts: std::sync::Mutex<std::collections::VecDeque<Vec<ChatChunk>>>,
    /// A request whose system message carries any of these goes to the live
    /// server (a child's persona, a compaction roll's prompt); every other
    /// request plays the next script.
    live_markers: &'static [&'static str],
    /// When the scripts run out, a request goes to the live server instead
    /// of playing an empty reply — a smoke whose *measured* turn is live
    /// after a scripted seed.
    live_after_scripts: bool,
    in_flight: Arc<std::sync::atomic::AtomicUsize>,
    max_in_flight: Arc<std::sync::atomic::AtomicUsize>,
}

struct LiveOpen(Arc<std::sync::atomic::AtomicUsize>);

impl Drop for LiveOpen {
    fn drop(&mut self) {
        self.0.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
    }
}

#[async_trait::async_trait]
impl EngineBackend for ScriptedParent {
    async fn chat_stream(
        &self,
        req: crate::shared::api::ChatRequest,
        cancel: tokio_util::sync::CancellationToken,
    ) -> anyhow::Result<crate::shared::api::contract::ChatStream> {
        use std::sync::atomic::Ordering::SeqCst;
        let by_marker = req
            .system
            .as_deref()
            .is_some_and(|s| self.live_markers.iter().any(|m| s.contains(m)));
        let script = if by_marker {
            None
        } else {
            self.scripts.lock().unwrap().pop_front()
        };
        if let Some(chunks) = script {
            return Ok(Box::pin(futures_util::stream::iter(chunks)));
        }
        if by_marker || self.live_after_scripts {
            let open = self.in_flight.fetch_add(1, SeqCst) + 1;
            self.max_in_flight.fetch_max(open, SeqCst);
            let guard = LiveOpen(self.in_flight.clone());
            let mut inner = self.live.chat_stream(req, cancel).await?;
            let s = async_stream::stream! {
                let _open = guard;
                while let Some(chunk) = inner.next().await {
                    yield chunk;
                }
            };
            return Ok(Box::pin(s));
        }
        Ok(Box::pin(futures_util::stream::iter(vec![
            ChatChunk::Finished(FinishReason::Stop),
        ])))
    }

    async fn context_budget(&self) -> Option<u32> {
        self.live.context_budget().await
    }

    async fn parallel_slots(&self) -> Option<u32> {
        self.live.parallel_slots().await
    }
}

const READER: &str = "You are a reader.";

/// `paragraphs` paragraphs of ~30 tokens each on both gate tokenizers
/// (measured: 36 of them are 1150 tokens with the persona on Gemma 3),
/// distinct per `tag` so no prefix is shared between two archives and the
/// cache cannot help either.
fn archive(tag: &str, paragraphs: usize) -> String {
    (0..paragraphs)
        .map(|i| format!("Paragraph {i} of the {tag} archive describes a lighthouse keeper's ordinary evening: the lamp is lit, the log is written, the tide is noted."))
        .collect::<Vec<_>>()
        .join("\n")
}

/// The delegator's chat, as the pool smokes open it: the orchestrator on
/// `cfg` over `backend`, the server's slot count waited for (external mode's
/// pool rule needs it in hand), an English "Delegator" profile with `tool`
/// enabled, and a fresh chat on that profile. Returns the orchestrator's
/// handles and the chat's id.
async fn delegator_chat(
    backend: Arc<dyn EngineBackend>,
    cfg: AppConfig,
    tool: &str,
) -> (
    tempfile::TempDir,
    UnboundedSender<AppCommand>,
    UnboundedReceiver<AppEvent>,
    tokio::task::JoinHandle<()>,
    Uuid,
) {
    let (dir, cmd_tx, mut evt_rx, handle) = spawn_orch_cfg(Some(backend), cfg);
    let reported = tokio::time::timeout(
        std::time::Duration::from_secs(20),
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::EngineSlots(Some(_)))),
    )
    .await
    .ok()
    .flatten();
    eprintln!("slots reported to the orchestrator: {reported:?}");

    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Delegator".into(),
            system_message: "You coordinate readers.".into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                enabled_tools: Some(vec![tool.to_string()]),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let chat_id = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .and_then(|e| match e {
            AppEvent::ChatActivated { id, .. } => Some(id),
            _ => None,
        })
        .unwrap();
    (dir, cmd_tx, evt_rx, handle, chat_id)
}

/// The live server with several slots over one pool, as `/props` reports
/// them — `(backend, slots, pool)` — or `None` with the reason printed: no
/// server, no `/props`, or one slot, where `what` (a pool-sharing smoke's
/// subject) has nothing to measure.
async fn pooled_live(what: &str) -> Option<(Arc<dyn EngineBackend>, u32, u64)> {
    let live = live_backend()?;
    let slots = live.parallel_slots().await;
    let pool = live.context_budget().await;
    let (Some(slots), Some(pool)) = (slots, pool) else {
        eprintln!("skip: the server reports slots {slots:?}, pool {pool:?}");
        return None;
    };
    if slots < 2 {
        eprintln!("skip: the server reports {slots} slot(s); {what} needs two or more");
        return None;
    }
    Some((live, slots, pool as u64))
}

/// One arm of the admission smoke, sized to the pool the server reports.
struct AdmissionArm {
    /// How many readers the parent delegates in its one reply.
    children: usize,
    /// The engine section's `sessions`, and `tools.subagent_parallel` with it.
    sessions: u32,
    /// Each child's prompt as a fraction of the pool (the planted archive).
    share: f64,
    /// `None`: the app believes the pool the server reports; `Some(k)`: it is
    /// told the pool is `k` times that — the control arm's lie.
    lie: Option<u64>,
}

const CODES: [&str; 4] = ["ZARNOVIK-7741", "KELVAR-3390", "MORVAX-5518", "TELUNE-8827"];
const TAGS: [&str; 4] = ["alpha", "beta", "gamma", "delta"];

struct AdmissionResult {
    /// Per child, in the model's order: the run's outcome and its final reply.
    runs: Vec<(Option<crate::entities::subagent::RunOutcome>, String)>,
    /// The most live streams the server saw open at once.
    most: usize,
}

/// One run of the admission smoke: `arm.children` readers, each handed an
/// archive of `arm.share` of the pool with a codename in it and asked for the
/// codename, under a scripted parent that believes the pool is what the
/// server reports (or `arm.lie` times that). Sized from `/props`, so the same
/// arms run on the CPU build's 2048-token pool and the LAN stack's 16384.
/// `None` when there is no live server, or when it reports at most one slot
/// — the guard is off by design below two (`pool.rs`), and one slot queues.
async fn admission_smoke(arm: AdmissionArm) -> Option<AdmissionResult> {
    let (live, slots, pool) = pooled_live("the guard").await?;
    // A paragraph is ~30 tokens on both gate tokenizers (measured: 36 of them
    // are 1150 tokens with the persona on Gemma 3). The planted text rides the
    // message rather than a file, since the CPU build's Gemma 3 template drops
    // tools (`supports_tools: false` on `/props`), and the pool does not care
    // where the tokens came from. The reply cap is an eighth of the pool.
    let paragraphs = ((arm.share * pool as f64) / 30.0) as usize;
    let max_tokens = (pool / 8) as usize;
    let belief = pool * arm.lie.unwrap_or(1);
    eprintln!(
        "server: {slots} slots over {pool}; {} children × {paragraphs} paragraphs, cap {max_tokens}, belief {belief}",
        arm.children
    );
    let filler = |tag: &str| archive(tag, paragraphs);
    let delegate = |index: usize| {
        let (tag, code) = (TAGS[index], CODES[index]);
        ChatChunk::ToolCall(crate::shared::api::contract::ToolCallDelta {
            thought_signature: None,
            index,
            id: Some(format!("c{index}")),
            name: Some("call_subagent".into()),
            arguments: serde_json::json!({
                "name": format!("{tag} reader"),
                "system_message": format!("{READER} You are given an archive; report the codename it contains in one short sentence, quoting it exactly."),
                "message": format!("{}\nThe {tag} codename is {code}.\n\nWhat is the codename?", filler(tag)),
            })
            .to_string(),
        })
    };
    let mut first: Vec<ChatChunk> = (0..arm.children).map(delegate).collect();
    first.push(ChatChunk::Finished(FinishReason::ToolCalls));
    let backend = Arc::new(ScriptedParent {
        live,
        scripts: std::sync::Mutex::new(
            vec![
                first,
                vec![
                    ChatChunk::Text("All readers reported.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
            ]
            .into(),
        ),
        live_markers: &[READER],
        live_after_scripts: false,
        in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        max_in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
    });

    let mut cfg = AppConfig::default();
    cfg.tools.subagent_parallel = arm.sessions;
    cfg.tools.subagent_max_tokens = max_tokens;
    cfg.default_sampling.max_tokens = Some(max_tokens);
    cfg.engine.external.sessions = arm.sessions;
    cfg.engine.managed.sessions = arm.sessions;
    // The pool the app believes in — the explicit window outranks what the
    // server reports (pool.rs). Compaction is off so the window is the
    // pool's and nothing else's.
    cfg.compaction.context_tokens = Some(belief as usize);
    cfg.compaction.enabled = false;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let (dir, cmd_tx, mut evt_rx, handle, chat_id) = delegator_chat(
        backend.clone() as Arc<dyn EngineBackend>,
        cfg,
        "call_subagent",
    )
    .await;

    let started = std::time::Instant::now();
    let (reply, calls) =
        run_turn_capture_args(&cmd_tx, &mut evt_rx, "Ask every reader for its codename.").await;
    eprintln!("reply: {reply} ({:.1} s)", started.elapsed().as_secs_f64());
    for (n, a, r) in &calls {
        eprintln!(
            "call {n}({}) -> {}",
            a.chars().take(60).collect::<String>(),
            r.chars().take(160).collect::<String>()
        );
    }
    tokio::time::sleep(std::time::Duration::from_millis(800)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let chat = Storage::open(Paths::with_root(dir.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    let runs: Vec<_> = chat
        .messages
        .iter()
        .flat_map(|m| m.tool_calls.iter())
        .filter_map(|r| r.subagent.as_deref())
        .map(|run| {
            eprintln!(
                "run «{}»: {} messages, outcome {:?}, reply {:?}",
                run.title,
                run.messages.len(),
                run.outcome,
                run.final_reply()
                    .map(|r| r.chars().take(120).collect::<String>())
            );
            (
                run.outcome,
                run.final_reply().unwrap_or_default().to_string(),
            )
        })
        .collect();
    let most = backend
        .max_in_flight
        .load(std::sync::atomic::Ordering::SeqCst);
    eprintln!("most live streams open at once: {most}");
    Some(AdmissionResult { runs, most })
}

/// A model may render a code with a non-breaking or typographic hyphen
/// (docs/lessons.md §9); fold before comparing.
fn fold_dashes(s: &str) -> String {
    s.replace(['\u{2011}', '\u{2010}', '\u{2012}', '\u{2013}'], "-")
}

fn assert_all_completed_with_their_codes(
    runs: &[(Option<crate::entities::subagent::RunOutcome>, String)],
) {
    assert!(
        runs.iter()
            .all(|(o, _)| *o == Some(crate::entities::subagent::RunOutcome::Completed)),
        "every child completes under the guard: {runs:?}"
    );
    for (i, (_, reply)) in runs.iter().enumerate() {
        assert!(
            fold_dashes(reply).contains(CODES[i]),
            "{} code missing: {reply}",
            TAGS[i]
        );
    }
}

/// **The guard, live** (research §7): against a `llama-server` whose slots
/// share one pool (`-np N --kv-unified` — the local CPU build at `-c 2048`
/// with Gemma 3 4B, or the LAN stack at 16384), two children whose prompts
/// (55% of the pool each) do not fit together both complete and both report
/// their codename — the budget made them take turns where the server would
/// have ended both (the control arm below). `#[ignore]`, manual.
#[tokio::test]
#[ignore = "requires a live chat server launched with -np N --kv-unified (MINDFORK_ENGINE_URL)"]
async fn admission_e2e_live() {
    let Some(res) = admission_smoke(AdmissionArm {
        children: 2,
        sessions: 2,
        share: 0.55,
        lie: None,
    })
    .await
    else {
        eprintln!("skipped (see above)");
        return;
    };
    assert_eq!(res.runs.len(), 2, "two runs: {:?}", res.runs);
    assert_all_completed_with_their_codes(&res.runs);
    assert_eq!(
        res.most, 1,
        "two reservations above half the pool do not fit: the children took turns"
    );
}

/// **The control arm**: the same run with the app told the pool is four
/// times what it is — the guard admits both prompts, the real pool cannot
/// hold them, and the server ends the running conversations together
/// (research §3.2): at least one child does not complete. This is what
/// proves the guarded run above did something rather than the server being
/// polite. `#[ignore]`, manual, the same server as above.
#[tokio::test]
#[ignore = "requires a live chat server launched with -np N --kv-unified (MINDFORK_ENGINE_URL)"]
async fn admission_control_e2e_live() {
    let Some(res) = admission_smoke(AdmissionArm {
        children: 2,
        sessions: 2,
        share: 0.55,
        lie: Some(4),
    })
    .await
    else {
        eprintln!("skipped (see above)");
        return;
    };
    assert_eq!(res.runs.len(), 2, "two runs: {:?}", res.runs);
    assert_eq!(res.most, 2, "unguarded, both children streamed at once");
    assert!(
        res.runs
            .iter()
            .any(|(o, _)| *o != Some(crate::entities::subagent::RunOutcome::Completed)),
        "the lie about the pool went unpunished — the server did not overflow: {:?}",
        res.runs
    );
}

/// **Four at once, two admitted** (research §7's LAN-stack variant): four
/// children at `sessions = 4`, each a quarter of the pool plus the cap — two
/// fit together, three do not — so the guard keeps exactly two streaming at
/// any time and all four complete with their codenames. `#[ignore]`, manual,
/// a server with four or more slots over one pool.
#[tokio::test]
#[ignore = "requires a live chat server launched with -np 4 --kv-unified (MINDFORK_ENGINE_URL)"]
async fn admission_four_e2e_live() {
    let Some(res) = admission_smoke(AdmissionArm {
        children: 4,
        sessions: 4,
        share: 0.25,
        lie: None,
    })
    .await
    else {
        eprintln!("skipped (see above)");
        return;
    };
    assert_eq!(res.runs.len(), 4, "four runs: {:?}", res.runs);
    assert_all_completed_with_their_codes(&res.runs);
    assert_eq!(
        res.most, 2,
        "a quarter of the pool plus the cap each: two fit together, three do not"
    );
}

/// A stable line of the summarizer's system prompt (`prompt.compact.system`,
/// en) — what routes a compaction roll to the live server in the hybrid.
const COMPACT_MARKER: &str = "compressing the earlier part of a conversation";

/// One arm of the silent-lane probe (docs/research/silent-tasks-budget.md §3).
struct SilentArm {
    /// `None`: the app believes the pool the server reports; `Some(k)`: it is
    /// told the pool is `k` times that — the control arm's lie, which disarms
    /// the guard the same way `admission_control_e2e_live`'s does.
    lie: Option<u64>,
}

struct SilentResult {
    /// The background run's outcome and its final reply.
    run: (Option<crate::entities::subagent::RunOutcome>, String),
    /// The roll: the summary it produced, or the error it reported.
    roll: Result<String, String>,
    /// The most live streams the server saw open at once.
    most: usize,
}

/// **The silent-lane probe** (stage 0 of docs/research/silent-tasks-budget.md,
/// its §3 arm 1): a compaction roll opened beside a background run, at
/// `sessions = 1`, on a server whose slots share one pool — the shape the
/// managed launcher produces at the default (no `-np`: the server's own
/// four-slot unified default). The parent is scripted; the run's persona and
/// the roll's request go to the real server. Sized from `/props`: the chat's
/// earlier history and the run's archive are each 55% of the pool, so the
/// roll's digest and the run's prompt do not fit together. The roll is asked
/// for by `/compact` rather than the automatic trigger, which needs the exact
/// usage a scripted parent cannot report. `None` when there is no live
/// server or it reports at most one slot.
async fn silent_roll_smoke(arm: SilentArm) -> Option<SilentResult> {
    let (live, slots, pool) = pooled_live("the collision").await?;
    let paragraphs = ((0.55 * pool as f64) / 30.0) as usize;
    let max_tokens = (pool / 8) as usize;
    let belief = pool * arm.lie.unwrap_or(1);
    eprintln!(
        "server: {slots} slots over {pool}; history and archive {paragraphs} paragraphs each, cap {max_tokens}, belief {belief}"
    );
    let delegate = ChatChunk::ToolCall(crate::shared::api::contract::ToolCallDelta {
        thought_signature: None,
        index: 0,
        id: Some("c0".into()),
        name: Some("start_subagent".into()),
        arguments: serde_json::json!({
            "name": "alpha reader",
            "system_message": format!("{READER} You are given an archive; report the codename it contains in one short sentence, quoting it exactly."),
            "message": format!("{}\nThe alpha codename is {}.\n\nWhat is the codename?", archive("alpha", paragraphs), CODES[0]),
        })
        .to_string(),
    });
    let backend = Arc::new(ScriptedParent {
        live,
        scripts: std::sync::Mutex::new(
            vec![
                // Turn 1 seeds the history: the user's archive, a one-word reply.
                vec![
                    ChatChunk::Text("Noted.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
                // Turn 2 starts the run and ends at once.
                vec![delegate, ChatChunk::Finished(FinishReason::ToolCalls)],
                vec![
                    ChatChunk::Text("Started.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
            ]
            .into(),
        ),
        live_markers: &[READER, COMPACT_MARKER],
        live_after_scripts: false,
        in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        max_in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
    });

    let mut cfg = AppConfig::default();
    cfg.tools.subagent_background = true;
    // No wake: the probe is about the roll and the run, not the turn after.
    cfg.tools.subagent_background_wake = false;
    cfg.tools.subagent_max_tokens = max_tokens;
    cfg.default_sampling.max_tokens = Some(max_tokens);
    cfg.engine.external.sessions = 1;
    cfg.engine.managed.sessions = 1;
    cfg.compaction.enabled = true;
    // The automatic trigger stays off (the roll is typed); the window the app
    // believes in is the pool, or the lie.
    cfg.compaction.threshold_pct = 0;
    cfg.compaction.context_tokens = Some(belief as usize);
    // A short tail, so the roll folds the seeded history and nothing less.
    cfg.compaction.tail_tokens = 64;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let (dir, cmd_tx, mut evt_rx, handle, chat_id) = delegator_chat(
        backend.clone() as Arc<dyn EngineBackend>,
        cfg,
        "start_subagent",
    )
    .await;

    // The history the roll will fold: the user's own archive, scripted "Noted.".
    run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "{}\nKeep this archive in mind.",
            archive("history", paragraphs)
        ),
    )
    .await;
    let started = std::time::Instant::now();
    let (reply, calls) = run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        "Have the alpha reader report its codename in the background.",
    )
    .await;
    eprintln!("parent: {reply} ({:.1} s)", started.elapsed().as_secs_f64());
    for (n, _, r) in &calls {
        eprintln!("call {n} -> {}", r.chars().take(120).collect::<String>());
    }
    // The run's stream is opening on the server; now the roll beside it.
    cmd_tx.send(AppCommand::Compact).unwrap();
    let mut roll: Option<Result<String, String>> = None;
    let mut landed = false;
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(300);
    while (roll.is_none() || !landed) && std::time::Instant::now() < deadline {
        let left = deadline.saturating_duration_since(std::time::Instant::now());
        match tokio::time::timeout(left, evt_rx.recv()).await {
            Ok(Some(AppEvent::Compacted { summary, .. })) => roll = Some(Ok(summary)),
            Ok(Some(AppEvent::Error(msg))) if roll.is_none() => roll = Some(Err(msg)),
            Ok(Some(AppEvent::Notice(msg))) if roll.is_none() => roll = Some(Err(msg)),
            Ok(Some(AppEvent::BackgroundRuns { out: 0 })) => landed = true,
            Ok(Some(_)) => {}
            _ => break,
        }
    }
    eprintln!(
        "roll: {roll:?}, run landed: {landed} ({:.1} s)",
        started.elapsed().as_secs_f64()
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let chat = Storage::open(Paths::with_root(dir.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    let run = chat
        .children()
        .next()
        .map(|run| {
            eprintln!(
                "run «{}»: {} messages, outcome {:?}, reply {:?}",
                run.title,
                run.messages.len(),
                run.outcome,
                run.final_reply()
                    .map(|r| r.chars().take(120).collect::<String>())
            );
            (
                run.outcome,
                run.final_reply().unwrap_or_default().to_string(),
            )
        })
        .expect("the run landed on its record");
    let most = backend
        .max_in_flight
        .load(std::sync::atomic::Ordering::SeqCst);
    eprintln!("most live streams open at once: {most}");
    Some(SilentResult {
        run,
        roll: roll.unwrap_or_else(|| Err("the roll never reported".into())),
        most,
    })
}

/// **The silent lane, live** (docs/research/silent-tasks-budget.md §7): the
/// roll waits for the run's stream to end — one live stream at a time — and
/// both complete: the run with its codename, the roll with a summary. On the
/// code before the lane this arm **fails**, which is the probe's GO (§3).
/// `#[ignore]`, manual: the CPU build launched as the managed launcher would
/// at one session (`-c 2048`, no `-np`), `MINDFORK_ENGINE_URL` at it.
#[tokio::test]
#[ignore = "requires a live llama-server with several slots over one pool (MINDFORK_ENGINE_URL)"]
async fn silent_roll_e2e_live() {
    let Some(res) = silent_roll_smoke(SilentArm { lie: None }).await else {
        eprintln!("skipped (see above)");
        return;
    };
    assert_eq!(
        res.run.0,
        Some(crate::entities::subagent::RunOutcome::Completed),
        "the run completes beside the roll: {:?}",
        res.run
    );
    assert!(
        fold_dashes(&res.run.1).contains(CODES[0]),
        "the run's code: {}",
        res.run.1
    );
    let summary = res.roll.expect("the roll produced a summary");
    assert!(!summary.trim().is_empty(), "an empty summary");
    assert_eq!(
        res.most, 1,
        "the roll's digest and the run's archive do not fit the pool together: one at a time"
    );
}

/// **The control arm**: the same run with the app told the pool is four
/// times what it is — the roll opens beside the run, the real pool cannot
/// hold both prompts, and the server ends the running conversations together
/// (admission-by-budget §3.2): the run does not complete or the roll fails.
/// This is what proves the guarded arm above did something. `#[ignore]`,
/// manual, the same server.
#[tokio::test]
#[ignore = "requires a live llama-server with several slots over one pool (MINDFORK_ENGINE_URL)"]
async fn silent_roll_control_e2e_live() {
    let Some(res) = silent_roll_smoke(SilentArm { lie: Some(4) }).await else {
        eprintln!("skipped (see above)");
        return;
    };
    assert_eq!(
        res.most, 2,
        "unguarded, the roll and the run streamed at once"
    );
    assert!(
        res.run.0 != Some(crate::entities::subagent::RunOutcome::Completed) || res.roll.is_err(),
        "the lie about the pool went unpunished — the server did not overflow: run {:?}, roll {:?}",
        res.run,
        res.roll
    );
}

/// The two arms of the preemption probe (docs/research/silent-preemption.md
/// §3): what a turn pays today behind a silent stream, and the floor a
/// preemption could reach.
#[derive(Clone, Copy, Debug)]
enum PreemptArm {
    /// The roll first, then a one-word turn that does not fit beside it.
    RollThenTurn,
    /// A long turn cancelled at its first token, then a one-word turn.
    CancelThenTurn,
    /// The one-word turn alone on an idle server: its cold prefill, the
    /// reference the other arms' first tokens are read against.
    TurnAlone,
}

/// What the probe read, all in seconds from the moment the measured turn was
/// sent unless said otherwise.
#[derive(Debug, Default)]
struct PreemptProbe {
    /// Arm 1: from `/compact` to the roll's result, and the result itself.
    roll: Option<(f64, Result<String, String>)>,
    /// Arm 1: from the turn's send to the roll's end — the wait the lane's
    /// rule makes the turn pay today.
    wait: Option<f64>,
    /// Arm 2: from the cancel to the cancelled turn's `Finished`.
    cancel_latency: Option<f64>,
    /// The measured turn's time to first token and to `Finished`.
    first_token: Option<f64>,
    finished: Option<f64>,
    /// The measured turn's reply.
    reply: String,
}

/// Arm 1: the roll first, the one-word turn half a second behind it; reads
/// the roll's end and result, the turn's first token and its end.
async fn probe_roll_then_turn(
    cmd_tx: &UnboundedSender<AppCommand>,
    evt_rx: &mut UnboundedReceiver<AppEvent>,
    word: &str,
) -> PreemptProbe {
    let mut probe = PreemptProbe::default();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(600);
    let compacted = std::time::Instant::now();
    cmd_tx.send(AppCommand::Compact).unwrap();
    tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    let sent = std::time::Instant::now();
    cmd_tx.send(AppCommand::SendMessage(word.into())).unwrap();
    let mut turn_done = false;
    while (probe.roll.is_none() || !turn_done) && std::time::Instant::now() < deadline {
        let left = deadline.saturating_duration_since(std::time::Instant::now());
        match tokio::time::timeout(left, evt_rx.recv()).await {
            Ok(Some(AppEvent::Compacted { summary, .. })) => {
                probe.roll = Some((compacted.elapsed().as_secs_f64(), Ok(summary)));
                probe.wait = Some(sent.elapsed().as_secs_f64());
            }
            Ok(Some(AppEvent::Error(msg))) | Ok(Some(AppEvent::Notice(msg)))
                if probe.roll.is_none() =>
            {
                probe.roll = Some((compacted.elapsed().as_secs_f64(), Err(msg)));
                probe.wait = Some(sent.elapsed().as_secs_f64());
            }
            Ok(Some(AppEvent::Chunk { text, .. })) => {
                probe
                    .first_token
                    .get_or_insert(sent.elapsed().as_secs_f64());
                probe.reply.push_str(&text);
            }
            Ok(Some(AppEvent::Finished { .. })) => {
                probe.finished = Some(sent.elapsed().as_secs_f64());
                turn_done = true;
            }
            Ok(Some(_)) => {}
            _ => break,
        }
    }
    probe
}

/// Arm 2: a long turn cancelled at its first token (or thought), the
/// one-word turn sent 300 ms after the cancelled one reports `Finished`.
async fn probe_cancel_then_turn(
    cmd_tx: &UnboundedSender<AppCommand>,
    evt_rx: &mut UnboundedReceiver<AppEvent>,
    word: &str,
) -> PreemptProbe {
    let mut probe = PreemptProbe::default();
    cmd_tx
        .send(AppCommand::SendMessage(
            "Write a long story about the lighthouse keeper's year, at least four hundred words."
                .into(),
        ))
        .unwrap();
    let mut cancelled_at: Option<std::time::Instant> = None;
    loop {
        match evt_rx.recv().await {
            Some(AppEvent::Chunk { .. }) | Some(AppEvent::Thoughts { .. })
                if cancelled_at.is_none() =>
            {
                cancelled_at = Some(std::time::Instant::now());
                cmd_tx.send(AppCommand::Cancel).unwrap();
            }
            Some(AppEvent::Finished { .. }) => {
                probe.cancel_latency = cancelled_at.map(|t| t.elapsed().as_secs_f64());
                break;
            }
            Some(_) => {}
            None => break,
        }
    }
    // The client's `Finished` comes before the server has noticed the
    // closed connection (measured: its `cancel task` is ~1 ms behind, the
    // slot's release ~110 ms) — a request sent in that gap lands on another
    // slot and pays a cold prefill for the same prompt. A preemption's
    // waiter is admitted after the displaced stream's reservation drops, on
    // the same side of that gap.
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;
    read_word_turn(cmd_tx, evt_rx, word, &mut probe).await;
    probe
}

/// Sends the one-word turn and reads its first token and its end into
/// `probe`, from the moment it was sent.
async fn read_word_turn(
    cmd_tx: &UnboundedSender<AppCommand>,
    evt_rx: &mut UnboundedReceiver<AppEvent>,
    word: &str,
    probe: &mut PreemptProbe,
) {
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(600);
    let sent = std::time::Instant::now();
    cmd_tx.send(AppCommand::SendMessage(word.into())).unwrap();
    while std::time::Instant::now() < deadline {
        let left = deadline.saturating_duration_since(std::time::Instant::now());
        match tokio::time::timeout(left, evt_rx.recv()).await {
            Ok(Some(AppEvent::Chunk { text, .. })) => {
                probe
                    .first_token
                    .get_or_insert(sent.elapsed().as_secs_f64());
                probe.reply.push_str(&text);
            }
            Ok(Some(AppEvent::Finished { .. })) => {
                probe.finished = Some(sent.elapsed().as_secs_f64());
                break;
            }
            Ok(Some(_)) => {}
            _ => break,
        }
    }
}

/// **The preemption probe** (stage 0 of docs/research/silent-preemption.md):
/// a chat seeded with 55 % of the pool by a scripted turn; then either the
/// compaction roll opened on the live server with a one-word turn sent half
/// a second behind it (the turn's prompt is the same 55 %, so the two do
/// not fit together and the turn waits — the lane's rule, whose length is
/// what is measured), or a long live turn cancelled at its first token with
/// the one-word turn sent the instant it reports `Finished` (the whole path
/// a preemption would take). `None` without a live server or with at most
/// one reported slot.
async fn preemption_smoke(arm: PreemptArm) -> Option<PreemptProbe> {
    let (live, slots, pool) = pooled_live("the shape").await?;
    let paragraphs = ((0.55 * pool as f64) / 30.0) as usize;
    eprintln!("server: {slots} slots over {pool}; history {paragraphs} paragraphs, arm {arm:?}");
    let backend = Arc::new(ScriptedParent {
        live,
        scripts: std::sync::Mutex::new(
            vec![
                vec![
                    ChatChunk::Text("Noted.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
                vec![
                    ChatChunk::Text("Nothing to add.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
            ]
            .into(),
        ),
        live_markers: &[COMPACT_MARKER],
        live_after_scripts: true,
        in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        max_in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
    });
    let mut cfg = AppConfig::default();
    cfg.default_sampling.max_tokens = Some(256);
    cfg.engine.external.sessions = 1;
    cfg.engine.managed.sessions = 1;
    cfg.compaction.enabled = true;
    cfg.compaction.threshold_pct = 0;
    cfg.compaction.context_tokens = Some(pool as usize);
    cfg.compaction.tail_tokens = 64;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let (_dir, cmd_tx, mut evt_rx, handle, _chat_id) = delegator_chat(
        backend.clone() as Arc<dyn EngineBackend>,
        cfg,
        "call_subagent",
    )
    .await;
    run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "{}\nKeep this archive in mind.",
            archive("history", paragraphs)
        ),
    )
    .await;
    // A second exchange longer than the tail (`tail_tokens` 64): `plan_cut`
    // keeps that many trailing tokens whole and folds the exchanges before
    // them, so this one is the tail and the archive's is what the roll folds.
    run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        &format!("{}\nAnything to add?", archive("tail", 4)),
    )
    .await;

    let word = "Reply with the single word READY and nothing else.";
    let probe = match arm {
        PreemptArm::RollThenTurn => probe_roll_then_turn(&cmd_tx, &mut evt_rx, word).await,
        PreemptArm::CancelThenTurn => probe_cancel_then_turn(&cmd_tx, &mut evt_rx, word).await,
        PreemptArm::TurnAlone => {
            let mut probe = PreemptProbe::default();
            read_word_turn(&cmd_tx, &mut evt_rx, word, &mut probe).await;
            probe
        }
    };
    let most = backend
        .max_in_flight
        .load(std::sync::atomic::Ordering::SeqCst);
    eprintln!("{probe:#?}\nmost live streams open at once: {most}");
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    Some(probe)
}

/// **Arm 1 — the turn behind the roll** (docs/research/silent-preemption.md
/// §3, §7): before the track the turn started only when the roll ended (the
/// wait it measured, §3.1); now the turn **displaces** the roll — its first
/// token arrives before the roll's end — and the roll is made again after
/// it and lands a summary. `#[ignore]`, manual: the CPU build launched as
/// the managed launcher would at one session (`-c 2048`, no `-np`),
/// `MINDFORK_ENGINE_URL` at it; the LAN stack for the GPU numbers.
#[tokio::test]
#[ignore = "requires a live llama-server with several slots over one pool (MINDFORK_ENGINE_URL)"]
async fn preemption_wait_e2e_live() {
    let Some(probe) = preemption_smoke(PreemptArm::RollThenTurn).await else {
        eprintln!("skipped (see above)");
        return;
    };
    let (_, roll) = probe.roll.expect("the roll reported");
    assert!(
        roll.is_ok_and(|s| !s.trim().is_empty()),
        "the roll produced a summary"
    );
    assert!(probe.finished.is_some(), "the turn finished");
    assert!(!probe.reply.trim().is_empty(), "the turn replied");
    let (first_token, roll_end) = (probe.first_token.unwrap(), probe.wait.unwrap());
    assert!(
        first_token < roll_end,
        "the turn streamed before the roll ended: first token at {first_token:.1} s, the roll's end at {roll_end:.1} s"
    );
}

/// **The reference — the one-word turn alone** (docs/research/cpu-batch.md
/// §3): on an idle server its first token is the cold prefill of the seeded
/// prompt and nothing else — what the other arms' first tokens are read
/// against. `#[ignore]`, manual, the same servers.
#[tokio::test]
#[ignore = "requires a live llama-server with several slots over one pool (MINDFORK_ENGINE_URL)"]
async fn preemption_cold_e2e_live() {
    let Some(probe) = preemption_smoke(PreemptArm::TurnAlone).await else {
        eprintln!("skipped (see above)");
        return;
    };
    assert!(probe.finished.is_some(), "the turn finished");
    assert!(!probe.reply.trim().is_empty(), "the turn replied");
}

/// **Arm 2 — the floor**: a turn cancelled at its first token, the next one
/// sent at once; its time to first token is the whole path a preemption
/// would take. `#[ignore]`, manual, the same server.
#[tokio::test]
#[ignore = "requires a live llama-server with several slots over one pool (MINDFORK_ENGINE_URL)"]
async fn preemption_floor_e2e_live() {
    let Some(probe) = preemption_smoke(PreemptArm::CancelThenTurn).await else {
        eprintln!("skipped (see above)");
        return;
    };
    assert!(
        probe.cancel_latency.is_some(),
        "the cancelled turn finished"
    );
    assert!(
        probe.finished.is_some(),
        "the turn after the cancel finished"
    );
    assert!(!probe.reply.trim().is_empty(), "the turn replied");
}

/// **A roll stopped from the tasks screen, live**
/// (docs/research/stop-silent-task.md §6): `/compact` on a seeded chat, the
/// stop command a second later — the notice arrives, nothing is folded, the
/// live stream ended early — then `/compact` again completes. `#[ignore]`,
/// manual: `MINDFORK_ENGINE_URL` at a server with several slots over one
/// pool (the LAN stack, or the CPU build as the launcher launches it).
#[tokio::test]
#[ignore = "requires a live llama-server with several slots over one pool (MINDFORK_ENGINE_URL)"]
async fn stop_silent_task_e2e_live() {
    let Some((live, slots, pool)) = pooled_live("the roll").await else {
        eprintln!("skipped (see above)");
        return;
    };
    let paragraphs = ((0.55 * pool as f64) / 30.0) as usize;
    eprintln!("server: {slots} slots over {pool}; history {paragraphs} paragraphs");
    let backend = Arc::new(ScriptedParent {
        live,
        scripts: std::sync::Mutex::new(
            vec![
                vec![
                    ChatChunk::Text("Noted.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
                vec![
                    ChatChunk::Text("Nothing to add.".into()),
                    ChatChunk::Finished(FinishReason::Stop),
                ],
            ]
            .into(),
        ),
        live_markers: &[COMPACT_MARKER],
        live_after_scripts: false,
        in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        max_in_flight: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
    });
    let mut cfg = AppConfig::default();
    cfg.engine.external.sessions = 1;
    cfg.engine.managed.sessions = 1;
    cfg.compaction.enabled = true;
    cfg.compaction.threshold_pct = 0;
    cfg.compaction.context_tokens = Some(pool as usize);
    cfg.compaction.tail_tokens = 64;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let (dir, cmd_tx, mut evt_rx, handle, chat_id) = delegator_chat(
        backend.clone() as Arc<dyn EngineBackend>,
        cfg,
        "call_subagent",
    )
    .await;
    run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        &format!(
            "{}\nKeep this archive in mind.",
            archive("history", paragraphs)
        ),
    )
    .await;
    run_turn_capture_args(
        &cmd_tx,
        &mut evt_rx,
        &format!("{}\nAnything to add?", archive("tail", 4)),
    )
    .await;

    let started = std::time::Instant::now();
    cmd_tx.send(AppCommand::Compact).unwrap();
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    cmd_tx
        .send(AppCommand::StopBackgroundTask {
            kind: BackgroundKind::Compaction,
        })
        .unwrap();
    let stopped_at = std::time::Instant::now();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120);
    let (mut notice, mut compacted) = (None, false);
    while notice.is_none() && std::time::Instant::now() < deadline {
        let left = deadline.saturating_duration_since(std::time::Instant::now());
        match tokio::time::timeout(left, evt_rx.recv()).await {
            Ok(Some(AppEvent::Notice(m))) => notice = Some(m),
            Ok(Some(AppEvent::Compacted { .. })) => compacted = true,
            Ok(Some(AppEvent::Error(m))) => panic!("the stop reported an error: {m}"),
            Ok(Some(_)) => {}
            _ => break,
        }
    }
    let notice = notice.expect("the stopped roll answered with a notice");
    eprintln!(
        "stopped {:.1} s after /compact; the notice {:.2} s after the stop: {notice}",
        (stopped_at - started).as_secs_f64(),
        stopped_at.elapsed().as_secs_f64()
    );
    assert!(!compacted, "nothing was folded by the stopped roll");
    assert_eq!(
        backend
            .max_in_flight
            .load(std::sync::atomic::Ordering::SeqCst),
        1
    );

    let again = std::time::Instant::now();
    cmd_tx.send(AppCommand::Compact).unwrap();
    let done = tokio::time::timeout(
        std::time::Duration::from_secs(300),
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Compacted { .. })),
    )
    .await
    .ok()
    .flatten();
    eprintln!(
        "the second roll: {:?} ({:.1} s)",
        done.as_ref().map(|_| "compacted"),
        again.elapsed().as_secs_f64()
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    assert!(done.is_some(), "the next /compact completed");
    let chat = Storage::open(Paths::with_root(dir.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    assert!(
        chat.compaction.is_some(),
        "the second roll's summary landed"
    );
}

/// The background run's live proof (docs/research/background-subagents.md
/// §7): the parent is told to have a planted file read in the background and
/// to answer an unrelated question at once; the turn lands with a
/// `start_subagent` call and no `fs_read` of its own; the run reads the file
/// and ends; the assistant is woken on the task notification and its reply
/// carries the planted codename. The dashes are folded on both sides
/// (docs/lessons.md §9). `#[ignore]`, manual against a live model.
#[tokio::test]
#[ignore = "requires a live chat server (MINDFORK_ENGINE_URL)"]
async fn background_subagent_e2e_live() {
    let sandbox = tempfile::tempdir().unwrap();
    let file = sandbox.path().join("alpha.txt");
    std::fs::write(
        &file,
        "Internal note.\nThe alpha codename is ZARNOVIK-7741.\n",
    )
    .unwrap();
    let mut cfg = AppConfig::default();
    cfg.tools.fs_enabled = true;
    cfg.tools.fs_root = Some(sandbox.path().to_string_lossy().to_string());
    cfg.tools.subagent_background = true;
    cfg.engine.external.sessions = 2;
    cfg.engine.managed.sessions = 2;
    cfg.interface.auto_title = crate::shared::config::AutoTitleMode::Off;
    let Some((_d, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::CreateProfile {
            name: "Delegator".into(),
            system_message: "You are a coordinator. Reply in English. You never read files \
                 yourself. When asked to have a file read in the background, start ONE \
                 sub-agent with the start_subagent tool — give it the exact file path and \
                 tell it to use fs_read — and answer the rest of the request at once, \
                 without waiting for it. When a task notification with the sub-agent's \
                 result arrives, report what it found."
                .into(),
        })
        .unwrap();
    let pl = wait_for(
        &mut evt_rx,
        |e| matches!(e, AppEvent::ProfileList(v) if v.len() >= 2),
    )
    .await
    .unwrap();
    let pid = match pl {
        AppEvent::ProfileList(v) => v.last().unwrap().id,
        _ => unreachable!(),
    };
    cmd_tx
        .send(AppCommand::UpdateProfile {
            id: pid,
            edit: Box::new(ProfileEdit {
                language: Some(crate::shared::i18n::Lang::En),
                enabled_tools: Some(vec![
                    "start_subagent".to_string(),
                    "call_subagent".to_string(),
                    "fs_read".to_string(),
                ]),
                ..Default::default()
            }),
        })
        .unwrap();
    cmd_tx
        .send(AppCommand::NewChat {
            profile_id: Some(pid),
        })
        .unwrap();
    let chat_id = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .and_then(|e| match e {
            AppEvent::ChatActivated { id, .. } => Some(id),
            _ => None,
        })
        .unwrap();

    let ask = format!(
        "Two things. (1) Have a sub-agent read the file {} in the background and find the \
         alpha codename — start it now with start_subagent and do not wait for it. \
         (2) Right now: which is larger, 17 × 23 or 400?",
        file.display()
    );
    cmd_tx.send(AppCommand::SendMessage(ask)).unwrap();
    let mut reply = String::new();
    let mut thoughts = 0usize;
    let mut calls: Vec<(String, String, String)> = Vec::new();
    let mut finish = None;
    while let Some(ev) = evt_rx.recv().await {
        match ev {
            AppEvent::Chunk { text, .. } => reply.push_str(&text),
            AppEvent::Thoughts { text, .. } => thoughts += text.len(),
            AppEvent::ToolCall {
                name,
                arguments,
                result,
                ..
            } => calls.push((name, arguments, result)),
            AppEvent::Error(e) => eprintln!("error event: {e}"),
            AppEvent::Finished { reason, .. } => {
                finish = Some(reason);
                break;
            }
            _ => {}
        }
    }
    eprintln!("reply ({finish:?}, {thoughts} bytes of thoughts): {reply}");
    for (n, a, r) in &calls {
        eprintln!(
            "call {n}({}) -> {}",
            a.chars().take(120).collect::<String>(),
            r.chars().take(160).collect::<String>()
        );
    }
    assert!(
        calls.iter().any(|(n, _, _)| n == "start_subagent"),
        "the parent did not start a background run: {calls:?} ({finish:?}, reply {reply:?})"
    );
    assert!(
        !calls.iter().any(|(n, _, _)| n == "fs_read"),
        "the parent read the file itself instead of delegating"
    );

    // The run ends and the assistant is woken on the task notification: a
    // turn nobody sent a message for, whose reply carries the codename.
    let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(300);
    let mut wake = String::new();
    loop {
        let ev = tokio::time::timeout_at(deadline, evt_rx.recv())
            .await
            .expect("the run lands and the wake turn ends within five minutes")
            .expect("the event stream stays open");
        match ev {
            AppEvent::Chunk { text, .. } => wake.push_str(&text),
            AppEvent::Finished { .. } => break,
            _ => {}
        }
    }
    eprintln!("wake reply: {wake}");
    tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let chat = Storage::open(Paths::with_root(_d.path()))
        .unwrap()
        .json()
        .load_chat(chat_id)
        .unwrap()
        .unwrap();
    let run = chat
        .children()
        .find(|r| r.background)
        .expect("a background run on the record");
    eprintln!(
        "run «{}»: {} messages, outcome {:?}",
        run.title,
        run.messages.len(),
        run.outcome
    );
    assert_eq!(
        run.outcome,
        Some(crate::entities::subagent::RunOutcome::Completed),
        "the run did not complete"
    );
    assert!(
        run.messages
            .iter()
            .flat_map(|m| m.tool_calls.iter())
            .any(|r| r.name == "fs_read"),
        "the run never used fs_read"
    );
    let fold = |s: &str| {
        s.replace(['-', '\u{2010}', '\u{2011}', '\u{2012}', '\u{2013}'], "")
            .to_uppercase()
    };
    let note = chat
        .messages
        .iter()
        .find(|m| m.is_notification())
        .expect("a task notification row");
    assert!(
        fold(&note.text).contains("ZARNOVIK7741"),
        "the notification lacks the codename: {}",
        note.text
    );
    // GO: the woken assistant reports what the run found.
    assert!(
        fold(&wake).contains("ZARNOVIK7741"),
        "the wake reply lacks the codename: {wake}"
    );
}

/// The slow-prefill note against a live `llama-server` in external mode
/// (docs/research/slow-prefill-detection.md §6): a seeded turn processes a
/// batch's worth of prompt tokens, the engine's `timings` travel to the
/// landing, and the note comes — or not — by the rule. Which is expected
/// depends on the host: set `MINDFORK_EXPECT_SLOW_PREFILL=1` on a host whose
/// prompt processing is slow (the CPU build at the default batch), leave it
/// unset on a GPU stack. Either way the figures are printed.
/// `MINDFORK_ENGINE_URL=…/v1 [MINDFORK_EXPECT_SLOW_PREFILL=1] cargo test slow_prefill_e2e_live -- --ignored --nocapture`.
#[tokio::test]
#[ignore = "requires a running llama-server (MINDFORK_ENGINE_URL)"]
async fn slow_prefill_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let expect_note = std::env::var("MINDFORK_EXPECT_SLOW_PREFILL").is_ok();
    let mut config = AppConfig::default();
    config.engine.mode = crate::shared::config::ServerMode::External;
    let (_d, cmd_tx, mut evt_rx, handle) = spawn_orch_cfg(Some(backend), config);
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();

    // A prompt of a batch's worth and more: forty lines of a lighthouse
    // keeper's evening, then one question.
    let seed: String = (0..40)
        .map(|i| {
            format!(
                "Paragraph {i}: the keeper climbs the stairs, lights the lamp, writes the log, \
                 notes the tide, and looks out over the dark water for a while.\n"
            )
        })
        .collect();
    let started = std::time::Instant::now();
    let (reply, _tools) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        &format!("{seed}\nIn one word: what does the keeper light?"),
    )
    .await;
    let turn = started.elapsed();
    // The landing's note, if any, follows `Finished` at once.
    let note = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Notice(_))),
    )
    .await
    .ok()
    .flatten()
    .and_then(|e| match e {
        AppEvent::Notice(t) => Some(t),
        _ => None,
    });
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!(
        "slow prefill: the turn took {:.1} s, reply {:?}; note = {}",
        turn.as_secs_f64(),
        reply.trim(),
        note.as_deref().unwrap_or("none")
    );
    match (expect_note, note) {
        (true, Some(text)) => assert!(text.contains("-b 256 -ub 256"), "{text}"),
        (true, None) => panic!("a slow host was expected to be told"),
        (false, None) => {}
        (false, Some(text)) => panic!("a fast host was told: {text}"),
    }
}

/// The roll's timings (docs/research/roll-timings.md §6): a chat reopened
/// from disk and `/compact` the session's first request — the roll is the
/// session's first cold prompt, and the slow-prefill note, if any, comes
/// from it. `MINDFORK_EXPECT_SLOW_PREFILL=1` on a slow host (the CPU build),
/// unset on a GPU host.
///
/// `MINDFORK_ENGINE_URL=…/v1 [MINDFORK_EXPECT_SLOW_PREFILL=1] cargo test roll_prefill_e2e_live -- --ignored --nocapture`.
#[tokio::test]
#[ignore = "requires a running llama-server (MINDFORK_ENGINE_URL)"]
async fn roll_prefill_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let expect_note = std::env::var("MINDFORK_EXPECT_SLOW_PREFILL").is_ok();

    // The conversation is on disk before the app starts: forty lines of the
    // keeper's evening over four exchanges, none of it sent this session.
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path().to_path_buf();
    let json = crate::shared::storage::JsonStore::new(Paths::with_root(&root));
    let profile = Profile::new("Keeper", "You are a concise assistant.");
    json.upsert_profile(&profile).unwrap();
    let mut chat = Chat::from_profile(&profile, "the keeper's evening");
    for part in 0..4u32 {
        let seed: String = (part * 10..part * 10 + 10)
            .map(|i| {
                format!(
                    "Paragraph {i}: the keeper climbs the stairs, lights the lamp, writes the log, \
                     notes the tide, and looks out over the dark water for a while.\n"
                )
            })
            .collect();
        chat.push_message(Message::user(format!("{seed}\nAcknowledge in one word.")));
        chat.push_message(Message::assistant(String::from("Noted.")));
    }
    let chat_id = chat.id;
    json.save_chat(&chat).unwrap();
    // The database beside the chats, so the bootstrap has no note of its own
    // to put in the feed before the roll's.
    drop(Storage::open(Paths::with_root(&root)).unwrap());

    let mut config = AppConfig::default();
    config.engine.mode = crate::shared::config::ServerMode::External;
    config.compaction = crate::shared::config::CompactionSettings {
        enabled: true,
        summary_words: 150,
        // A small verbatim tail, so the four exchanges leave something to fold.
        tail_tokens: 120,
        ..Default::default()
    };
    let (cmd_tx, mut evt_rx, handle) = spawn_orch_at(&root, Some(backend), config);
    let activated = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    if !matches!(activated, AppEvent::ChatActivated { id, .. } if id == chat_id) {
        cmd_tx.send(AppCommand::SwitchChat(chat_id)).unwrap();
        wait_for(
            &mut evt_rx,
            |e| matches!(e, AppEvent::ChatActivated { id, .. } if *id == chat_id),
        )
        .await
        .unwrap();
    }

    // The session's first request is the roll.
    let started = std::time::Instant::now();
    cmd_tx.send(AppCommand::Compact).unwrap();
    let landed = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::Compacted { .. } | AppEvent::Error(_) | AppEvent::Notice(_)
        )
    })
    .await
    .unwrap();
    let AppEvent::Compacted {
        summary, folded, ..
    } = landed
    else {
        panic!("the roll did not land as a summary: {landed:?}");
    };
    let roll = started.elapsed();
    // The landing's note, if any, follows `Compacted` at once.
    let note = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Notice(_))),
    )
    .await
    .ok()
    .flatten()
    .and_then(|e| match e {
        AppEvent::Notice(t) => Some(t),
        _ => None,
    });
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!(
        "roll prefill: the roll took {:.1} s, folded {folded} messages into {} chars; note = {}",
        roll.as_secs_f64(),
        summary.chars().count(),
        note.as_deref().unwrap_or("none")
    );
    match (expect_note, note) {
        (true, Some(text)) => assert!(text.contains("-b 256 -ub 256"), "{text}"),
        (true, None) => panic!("a slow host was expected to be told by its roll"),
        (false, None) => {}
        (false, Some(text)) => panic!("a fast host was told: {text}"),
    }
}

/// The loops' timings (docs/research/loop-timings.md §6): two phases on one
/// data root against one server. Phase 1 warms the server's cache with the
/// chat's prefix — a long turn, no reflection — and quits. Phase 2 restarts
/// the app on the same root: a short turn, warm and under the floor, then
/// the reflection — its first round cold — and the note, if any, from its
/// landing. `MINDFORK_EXPECT_SLOW_PREFILL=1` on a slow host (the CPU build),
/// unset on a GPU host.
///
/// `MINDFORK_ENGINE_URL=…/v1 [MINDFORK_EXPECT_SLOW_PREFILL=1] cargo test loop_prefill_e2e_live -- --ignored --nocapture`.
#[tokio::test]
#[ignore = "requires a running llama-server (MINDFORK_ENGINE_URL)"]
async fn loop_prefill_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let expect_note = std::env::var("MINDFORK_EXPECT_SLOW_PREFILL").is_ok();
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path().to_path_buf();
    let external = || {
        let mut config = AppConfig::default();
        config.engine.mode = crate::shared::config::ServerMode::External;
        config
    };

    // Phase 1: the chat's prefix into the server's cache. The memory tools
    // are enabled here, before the turn — the reflection is gated on the
    // profile's tool set, and the schemas are part of the prefix phase 2
    // must find warm; the edit is persisted, so phase 2 inherits it.
    let (cmd_tx, mut evt_rx, handle) = spawn_orch_at(&root, Some(backend.clone()), external());
    let _pid = enable_all_tools(&cmd_tx, &mut evt_rx).await;
    let seed: String = (0..40)
        .map(|i| {
            format!(
                "Paragraph {i}: the keeper climbs the stairs, lights the lamp, writes the log, \
                 notes the tide, and looks out over the dark water for a while.\n"
            )
        })
        .collect();
    let (reply, _) = run_turn_live(
        &cmd_tx,
        &mut evt_rx,
        &format!("{seed}\nIn one word: what does the keeper light?"),
    )
    .await;
    // The exchange is stored by `handle_done`, after `Finished`: the `ChatList`
    // it emits is what says the quit will flush it.
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatList(_)))
        .await
        .unwrap();
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();
    eprintln!("phase 1: reply {:?}", reply.trim());

    // Phase 2: the app again on the same root, the same server; the
    // reflection after the reply.
    let mut config = external();
    config.self_model.auto_reflect_every = 1;
    let (cmd_tx, mut evt_rx, handle) = spawn_orch_at(&root, Some(backend), config);
    // The app reopens on the chat it left; the reply shows it did.
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    let started = std::time::Instant::now();
    let (reply, _) =
        run_turn_live(&cmd_tx, &mut evt_rx, "And in one word: what does he note?").await;
    let turn = started.elapsed();
    // Spawned at all? A loop the profile's tool set gates out never lands,
    // and "did not land" would hide that.
    tokio::time::timeout(
        std::time::Duration::from_secs(30),
        wait_for(&mut evt_rx, |e| {
            matches!(
                e,
                AppEvent::BackgroundTask {
                    kind: BackgroundKind::Reflection,
                    active: true
                }
            )
        }),
    )
    .await
    .expect("the reflection was spawned at the landing");
    // The reflection's own landing — bounded by its time limit over its
    // streaming, plus its wait for the lane.
    let landed = tokio::time::timeout(
        std::time::Duration::from_secs(300),
        wait_for(&mut evt_rx, |e| {
            matches!(
                e,
                AppEvent::BackgroundTask {
                    kind: BackgroundKind::Reflection,
                    active: false
                }
            )
        }),
    )
    .await;
    let loop_took = started.elapsed() - turn;
    // The landing's note, if any, follows it at once.
    let note = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::Notice(_))),
    )
    .await
    .ok()
    .flatten()
    .and_then(|e| match e {
        AppEvent::Notice(t) => Some(t),
        _ => None,
    });
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!(
        "loop prefill: the turn took {:.1} s (reply {:?}), the reflection landed = {}, {:.1} s after it; note = {}",
        turn.as_secs_f64(),
        reply.trim(),
        landed.is_ok(),
        loop_took.as_secs_f64(),
        note.as_deref().unwrap_or("none")
    );
    assert!(landed.is_ok(), "the reflection did not land");
    match (expect_note, note) {
        (true, Some(text)) => assert!(text.contains("-b 256 -ub 256"), "{text}"),
        (true, None) => panic!("a slow host was expected to be told by its loop"),
        (false, None) => {}
        (false, Some(text)) => panic!("a fast host was told: {text}"),
    }
}

/// An engine that forwards every request to the live one and keeps it beside
/// the exact prompt size the server reported for it — the stage-0 probe of
/// docs/research/roll-usage-calibration.md §2.1 as a test.
/// What the probe keeps: each request beside its exact prompt size.
type Seen = Arc<std::sync::Mutex<Vec<(crate::shared::api::ChatRequest, u32)>>>;

struct EstimateProbe {
    inner: Arc<dyn EngineBackend>,
    seen: Seen,
}

#[async_trait::async_trait]
impl EngineBackend for EstimateProbe {
    async fn chat_stream(
        &self,
        req: crate::shared::api::ChatRequest,
        cancel: tokio_util::sync::CancellationToken,
    ) -> anyhow::Result<crate::shared::api::contract::ChatStream> {
        use futures_util::StreamExt as _;
        let mut inner = self.inner.chat_stream(req.clone(), cancel).await?;
        let seen = self.seen.clone();
        // Kept the moment the usage arrives: the consumer drops the stream at
        // `Finished` without reading it to its end, so nothing after the loop
        // would run.
        let s = async_stream::stream! {
            while let Some(chunk) = inner.next().await {
                if let ChatChunk::Usage(u) = &chunk {
                    seen.lock().unwrap().push((req.clone(), u.prompt_tokens));
                }
                yield chunk;
            }
        };
        Ok(Box::pin(s))
    }

    async fn context_budget(&self) -> Option<u32> {
        self.inner.context_budget().await
    }
}

/// The prompt estimate against the server's exact count, on the requests the
/// app itself builds (docs/research/roll-usage-calibration.md §6,
/// title-impersonation-usage.md §6): two prose turns, a third carrying 25 KB
/// of JSON — the shape of a tool result — then `/compact` and an
/// impersonation. A prose turn's exact over its estimate within 0.75–1.25 —
/// the schemas counted, the compact JSON a little denser than four bytes a
/// token; a request carrying the JSON above 1.0 — the under-count the
/// per-kind ratio exists to keep out of the other kinds' reach; the roll's
/// and the title's at most 1.0 — prose over-counted, never under.
///
/// `MINDFORK_ENGINE_URL=…/v1 cargo test prompt_estimate_e2e_live -- --ignored --nocapture`.
#[tokio::test]
#[ignore = "requires a running llama-server (MINDFORK_ENGINE_URL)"]
async fn prompt_estimate_e2e_live() {
    let Some(live) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let probe = Arc::new(EstimateProbe {
        inner: live,
        seen: Arc::new(std::sync::Mutex::new(Vec::new())),
    });
    let config = AppConfig {
        compaction: crate::shared::config::CompactionSettings {
            enabled: true,
            summary_words: 150,
            tail_tokens: 120,
            ..Default::default()
        },
        ..Default::default()
    };
    let (_d, cmd_tx, mut evt_rx, handle) =
        spawn_orch_cfg(Some(probe.clone() as Arc<dyn EngineBackend>), config);
    wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    // A catalogue as a tool result would carry it: JSON runs at about 2.4
    // bytes a token on this tokenizer, against the estimate's four.
    let blob = serde_json::to_string(
        &(0..220)
            .map(|i| {
                serde_json::json!({
                    "id": i,
                    "sku": format!("A{i:04}-{:03}", i * 7 % 1000),
                    "price": (i as f64) * 1.25 + 0.99,
                    "tags": ["alpha", "beta", "gamma"],
                    "stock": {"warehouse": i % 5, "count": i * 3},
                })
            })
            .collect::<Vec<_>>(),
    )
    .unwrap();
    let json_turn = format!(
        "Here is a catalogue as JSON; answer in one sentence how many items it has:\n{blob}"
    );
    for text in [
        "Расскажи в двух предложениях, зачем нужны индексы в базах данных.",
        "В двух предложениях: чем отличается кэш от буфера?",
        json_turn.as_str(),
    ] {
        let (reply, _) = run_turn_capture(&cmd_tx, &mut evt_rx, text).await;
        eprintln!("reply: {}", reply.chars().take(60).collect::<String>());
        wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatList(_)))
            .await
            .unwrap();
    }
    cmd_tx.send(AppCommand::Compact).unwrap();
    let landed = wait_for(&mut evt_rx, |e| {
        matches!(e, AppEvent::Compacted { .. } | AppEvent::Error(_))
    })
    .await
    .unwrap();
    assert!(
        matches!(landed, AppEvent::Compacted { .. }),
        "the roll did not land: {landed:?}"
    );
    cmd_tx
        .send(AppCommand::Impersonate {
            seed: String::new(),
        })
        .unwrap();
    let ended = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::ImpersonationFinished { .. } | AppEvent::Error(_)
        )
    })
    .await
    .unwrap();
    assert!(
        matches!(ended, AppEvent::ImpersonationFinished { .. }),
        "impersonation did not finish: {ended:?}"
    );
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    let roll_system = crate::features::compaction::summary_system_message(
        crate::shared::i18n::locale(crate::shared::i18n::Lang::default()),
        150,
    );
    let seen = probe.seen.lock().unwrap().clone();
    let (mut turns, mut rolls, mut dense, mut impersonations) = (0, 0, 0, 0);
    for (req, exact) in &seen {
        let estimate = super::super::generation::estimate_prompt_tokens(req);
        let ratio = *exact as f64 / estimate as f64;
        let is_roll = req.system.as_deref() == Some(roll_system.as_str());
        // The JSON message in the history: the turn that carried it, and
        // impersonation, which sends the whole conversation — after the
        // roll the cut lands on that very message, and the swap's leading
        // assistant turn is folded into the persona, so impersonation's JSON
        // rides its system prompt (docs/research/gemma-impersonation.md §3.1).
        let carries_json = req
            .messages
            .iter()
            .any(|m| m.content.contains("catalogue as JSON"))
            || req
                .system
                .as_deref()
                .is_some_and(|s| s.contains("catalogue as JSON"));
        let kind = match (req.tools.is_empty(), is_roll, carries_json) {
            (false, _, _) => "turn",
            (true, true, _) => "roll",
            (true, false, true) => "impersonation",
            (true, false, false) => "title",
        };
        eprintln!(
            "prompt estimate: {kind:<13} tools={:>2} json={} estimate={estimate:>5} exact={exact:>5} exact/estimate={ratio:.2}",
            req.tools.len(),
            u8::from(carries_json)
        );
        match kind {
            "turn" if carries_json => {
                turns += 1;
                dense += 1;
                assert!(ratio > 1.0, "the JSON turn did not under-count: {ratio:.2}");
            }
            "turn" => {
                turns += 1;
                assert!(
                    (0.75..=1.25).contains(&ratio),
                    "a prose turn's estimate is off by more than a quarter: {ratio:.2}"
                );
            }
            "impersonation" => {
                impersonations += 1;
                assert!(
                    ratio > 1.0,
                    "impersonation over the JSON did not under-count: {ratio:.2}"
                );
            }
            "roll" => {
                rolls += 1;
                assert!(ratio <= 1.0, "the roll under-counted: {ratio:.2}");
            }
            _ => assert!(ratio <= 1.0, "the title under-counted: {ratio:.2}"),
        }
    }
    assert!(
        turns >= 3 && dense >= 1 && rolls >= 1 && impersonations >= 1,
        "{turns} turns ({dense} dense), {rolls} rolls, {impersonations} impersonations seen"
    );
}

/// A chat seeded on disk for the one-shot smokes: four long exchanges the
/// user opens — the shape Gemma 3's template refused until the opening was
/// folded into the persona (docs/research/gemma-impersonation.md) — about a
/// thousand tokens swapped or digested — above the rule's floor, and
/// inside a minute of prefill on the CPU build. Seeded rather than made by
/// turns: a turn's first round on a fresh root processes its schemas cold
/// and would claim the session's one note before the request under test
/// (docs/research/oneshot-samples.md §4). Returns the chat's id; the
/// database is opened once so the bootstrap says nothing about it.
fn seed_long_chat(root: &std::path::Path) -> Uuid {
    let json = crate::shared::storage::JsonStore::new(Paths::with_root(root));
    let profile = Profile::new("Keeper", "You are a concise assistant.");
    json.upsert_profile(&profile).unwrap();
    let mut chat = Chat::from_profile(&profile, "the keeper's evening");
    for part in 0..4u32 {
        let seed: String = (part * 8..part * 8 + 8)
            .map(|i| {
                format!(
                    "Paragraph {i}: the keeper climbs the stairs, lights the lamp, writes the log, \
                     notes the tide, and looks out over the dark water for a while.\n"
                )
            })
            .collect();
        chat.push_message(Message::user(format!("{seed}\nAcknowledge in one word.")));
        chat.push_message(Message::assistant(String::from("Noted.")));
    }
    let chat_id = chat.id;
    json.save_chat(&chat).unwrap();
    drop(Storage::open(Paths::with_root(root)).unwrap());
    chat_id
}

/// The orchestrator on a seeded root with the chat active and every startup
/// event drained: what the one-shot smokes start from.
async fn spawn_on_seeded_chat(
    root: &std::path::Path,
    backend: Arc<dyn EngineBackend>,
    chat_id: Uuid,
) -> (
    UnboundedSender<AppCommand>,
    UnboundedReceiver<AppEvent>,
    tokio::task::JoinHandle<()>,
) {
    let mut config = AppConfig::default();
    config.engine.mode = crate::shared::config::ServerMode::External;
    let (cmd_tx, mut evt_rx, handle) = spawn_orch_at(root, Some(backend), config);
    let activated = wait_for(&mut evt_rx, |e| matches!(e, AppEvent::ChatActivated { .. }))
        .await
        .unwrap();
    if !matches!(activated, AppEvent::ChatActivated { id, .. } if id == chat_id) {
        cmd_tx.send(AppCommand::SwitchChat(chat_id)).unwrap();
        wait_for(
            &mut evt_rx,
            |e| matches!(e, AppEvent::ChatActivated { id, .. } if *id == chat_id),
        )
        .await
        .unwrap();
    }
    (cmd_tx, evt_rx, handle)
}

/// The note within a few seconds of the landing, or none.
async fn note_after_landing(evt_rx: &mut UnboundedReceiver<AppEvent>) -> Option<String> {
    tokio::time::timeout(
        std::time::Duration::from_secs(3),
        wait_for(evt_rx, |e| matches!(e, AppEvent::Notice(_))),
    )
    .await
    .ok()
    .flatten()
    .and_then(|e| match e {
        AppEvent::Notice(t) => Some(t),
        _ => None,
    })
}

/// Whether the host was told, against what the host is
/// (`MINDFORK_EXPECT_SLOW_PREFILL=1` on a slow one).
fn assert_note(expect_note: bool, note: Option<String>, what: &str) {
    match (expect_note, note) {
        (true, Some(text)) => assert!(text.contains("-b 256 -ub 256"), "{text}"),
        (true, None) => panic!("a slow host was expected to be told by its {what}"),
        (false, None) => {}
        (false, Some(text)) => panic!("a fast host was told: {text}"),
    }
}

/// Impersonation's sample end to end (docs/research/oneshot-samples.md §6,
/// fork F4a): a seeded chat, `Ctrl+U` with no turn before it, and the note
/// within a few seconds of `ImpersonationFinished` on a slow host — none on
/// a fast one. The prompt is the whole conversation swapped under its own
/// system, processed cold: about a thousand tokens here.
///
/// `MINDFORK_ENGINE_URL=…/v1 [MINDFORK_EXPECT_SLOW_PREFILL=1] cargo test impersonation_prefill_e2e_live -- --ignored --nocapture`.
#[tokio::test]
#[ignore = "requires a running llama-server (MINDFORK_ENGINE_URL)"]
async fn impersonation_prefill_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let expect_note = std::env::var("MINDFORK_EXPECT_SLOW_PREFILL").is_ok();
    let dir = tempfile::tempdir().unwrap();
    let chat_id = seed_long_chat(dir.path());
    let (cmd_tx, mut evt_rx, handle) = spawn_on_seeded_chat(dir.path(), backend, chat_id).await;

    let started = std::time::Instant::now();
    cmd_tx
        .send(AppCommand::Impersonate {
            seed: String::new(),
        })
        .unwrap();
    let ended = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::ImpersonationFinished { .. } | AppEvent::Error(_)
        )
    })
    .await
    .unwrap();
    assert!(
        matches!(ended, AppEvent::ImpersonationFinished { .. }),
        "impersonation did not finish: {ended:?}"
    );
    let took = started.elapsed();
    let note = note_after_landing(&mut evt_rx).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!(
        "impersonation prefill: the request took {:.1} s; note = {}",
        took.as_secs_f64(),
        note.as_deref().unwrap_or("none")
    );
    assert_note(expect_note, note, "impersonation");
}

/// The title's sample end to end (docs/research/oneshot-samples.md §6, fork
/// F4a): a seeded chat, a requested title with no turn before it — the
/// digest capped at 4000 characters, about a thousand tokens under the
/// title's own system, processed cold — and the note within a few seconds
/// of the rename on a slow host, none on a fast one.
///
/// `MINDFORK_ENGINE_URL=…/v1 [MINDFORK_EXPECT_SLOW_PREFILL=1] cargo test title_prefill_e2e_live -- --ignored --nocapture`.
#[tokio::test]
#[ignore = "requires a running llama-server (MINDFORK_ENGINE_URL)"]
async fn title_prefill_e2e_live() {
    let Some(backend) = live_backend() else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let expect_note = std::env::var("MINDFORK_EXPECT_SLOW_PREFILL").is_ok();
    let dir = tempfile::tempdir().unwrap();
    let chat_id = seed_long_chat(dir.path());
    let (cmd_tx, mut evt_rx, handle) = spawn_on_seeded_chat(dir.path(), backend, chat_id).await;

    let started = std::time::Instant::now();
    cmd_tx.send(AppCommand::AutoRenameChat(chat_id)).unwrap();
    let landed = wait_for(&mut evt_rx, |e| {
        matches!(
            e,
            AppEvent::ChatRenamed { .. } | AppEvent::ChatListError(_) | AppEvent::Error(_)
        )
    })
    .await
    .unwrap();
    let AppEvent::ChatRenamed { title, .. } = landed else {
        panic!("the title did not land: {landed:?}");
    };
    let took = started.elapsed();
    let note = note_after_landing(&mut evt_rx).await;
    cmd_tx.send(AppCommand::Quit).unwrap();
    handle.await.unwrap();

    eprintln!(
        "title prefill: the request took {:.1} s, the title {title:?}; note = {}",
        took.as_secs_f64(),
        note.as_deref().unwrap_or("none")
    );
    assert_note(expect_note, note, "title");
}

/// The settings screen's one question, end to end: `ListModels` for the
/// assistant's slot, the orchestrator resolving the address and key from the
/// config it already holds, and the answer landing on the UI channel as
/// `ModelCatalogue` (docs/research/model-picker.md, stage 4b).
///
/// Against `MINDFORK_ENGINE_URL` — a `llama-server` lists the model it loaded,
/// and that id is what a multi-model endpoint would route on, so it is what the
/// picker writes into the field.
#[tokio::test]
#[ignore = "requires MINDFORK_ENGINE_URL (a live OpenAI-compatible server)"]
async fn the_model_catalogue_reaches_the_ui_e2e_live() {
    let Ok(url) = std::env::var("MINDFORK_ENGINE_URL") else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    let mut cfg = no_auto_cfg();
    cfg.engine.mode = crate::shared::config::ServerMode::External;
    cfg.engine.external.url = Some(url.clone());
    let Some((_dir, cmd_tx, mut evt_rx, handle)) = spawn_orch_live_cfg(cfg) else {
        eprintln!("skip: MINDFORK_ENGINE_URL not set");
        return;
    };
    cmd_tx
        .send(AppCommand::ListModels(
            crate::shared::api::catalogue::ModelSlot::Assistant,
        ))
        .unwrap();
    let landed = tokio::time::timeout(
        Duration::from_secs(30),
        wait_for(&mut evt_rx, |e| {
            matches!(e, AppEvent::ModelCatalogue { .. })
        }),
    )
    .await
    .expect("the catalogue must answer the screen that asked")
    .expect("the event stream");
    match landed {
        AppEvent::ModelCatalogue { slot, models } => {
            assert_eq!(slot, crate::shared::api::catalogue::ModelSlot::Assistant);
            let list = models.expect("the server answered its catalogue");
            eprintln!("{url} listed {} model(s):", list.len());
            for m in list.iter().take(5) {
                eprintln!("    {} {:?}", m.id, m.role);
            }
            assert!(!list.is_empty(), "a server with a model loaded lists it");
            assert!(
                list.iter().all(|m| !m.id.is_empty()),
                "an empty id could not be written into the field"
            );
        }
        other => panic!("expected a catalogue, got {other:?}"),
    }
    drop(cmd_tx);
    let _ = tokio::time::timeout(Duration::from_secs(5), handle).await;
}