File size: 85,983 Bytes
c453128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Integration tests for agent_gui.server FastAPI endpoints.

Subprocess spawning is mocked so tests don't need hermes installed.
The server's HermesDB reads from a temp SQLite-less directory (returns [] gracefully).
"""
import asyncio
import json
import os
import tempfile
import unittest.mock as mock
from pathlib import Path

import pytest
import pytest_asyncio
import yaml
from httpx import ASGITransport, AsyncClient

from agent_gui import server as srv
from agent_gui.server import create_app


# ── Fixtures ───────────────────────────────────────────────────────────────────


@pytest.fixture()
def tmp_home(tmp_path: Path):
    """Minimal hermes_home with no state.db (HermesDB returns [] gracefully)."""
    return tmp_path


@pytest.fixture()
def app(tmp_home: Path, tmp_path: Path):
    ws_root = tmp_path / "workspace"
    ws_root.mkdir()
    return create_app(hermes_home=str(tmp_home), workspace_root=str(ws_root))


@pytest_asyncio.fixture()
async def client(app):
    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
        yield c


def _make_fake_proc(lines: list[str], returncode: int = 0):
    """Return a mock asyncio subprocess that streams the given newline-delimited JSON lines."""

    async def _readline_gen():
        for line in lines:
            yield (line + "\n").encode()

    proc = mock.AsyncMock()
    proc.stdout = _readline_gen()
    proc.returncode = returncode
    # wait() completes immediately
    proc.wait = mock.AsyncMock(return_value=returncode)
    proc.terminate = mock.MagicMock()
    proc.kill = mock.MagicMock()
    return proc


# ── /api/sessions ──────────────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_list_sessions_empty(client: AsyncClient):
    r = await client.get("/api/sessions")
    assert r.status_code == 200
    assert r.json() == []


@pytest.mark.asyncio
async def test_hermes_status_keys(client: AsyncClient):
    r = await client.get("/api/hermes/status")
    assert r.status_code == 200
    body = r.json()
    assert "available" in body
    assert "hermes" in body
    assert "ollama" in body


# ── /api/sessions/new ──────────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_new_session_returns_session_id(client: AsyncClient, tmp_home: Path):
    session_id = "20240101_120000_abc123"
    fake_proc = _make_fake_proc([
        json.dumps({"type": "session_id", "id": session_id}),
        json.dumps({"type": "log", "msg": "hello"}),
        json.dumps({"type": "done"}),
    ])

    with mock.patch("asyncio.create_subprocess_exec", return_value=fake_proc):
        r = await client.post("/api/sessions/new", json={"content": "do something"})

    assert r.status_code == 200
    body = r.json()
    assert "session_id" in body
    assert body["session"].get("is_running") is True
    assert body["session"].get("title") == "do something"
    # Clean up live queue so other tests don't see stale state
    srv._live_queues.pop(body["session_id"], None)
    srv._running_procs.pop(body["session_id"], None)


@pytest.mark.asyncio
async def test_new_session_missing_content(client: AsyncClient):
    r = await client.post("/api/sessions/new", json={})
    assert r.status_code == 400


def _make_persistent_proc():
    """Fake persistent worker: stdin capture + a stdout that blocks (stays warm)."""
    class _Blocking:
        def __aiter__(self):
            return self
        async def __anext__(self):
            await asyncio.Event().wait()  # never yields β†’ pump stays alive
    proc = mock.AsyncMock()
    proc.returncode = None
    proc.stdout = _Blocking()
    proc.stderr = _Blocking()
    proc.stdin = mock.Mock()
    proc.stdin.write = mock.Mock()
    proc.stdin.drain = mock.AsyncMock()
    return proc


@pytest.mark.asyncio
async def test_persistent_worker_reused_across_turns(client: AsyncClient, monkeypatch):
    """With HERMES_GUI_PERSISTENT_WORKER on, a desk spawns ONE worker and sends
    successive turns over stdin instead of respawning."""
    monkeypatch.setattr(srv, "_PERSISTENT_WORKERS", True)
    spawns = []
    proc = _make_persistent_proc()

    def _spawn(*args, **kwargs):
        spawns.append(args)
        return proc

    with mock.patch("asyncio.create_subprocess_exec", side_effect=_spawn):
        r = await client.post("/api/sessions/new", json={"content": "task one"})
        sid = r.json()["session_id"]
        # turn ended in the real flow via turn_done; simulate idle so resume is allowed
        srv._running_procs.pop(sid, None)
        await client.post(f"/api/sessions/{sid}/resume", json={"content": "task two"})

    try:
        # Exactly one process spawned, launched in --persistent mode.
        assert len(spawns) == 1
        assert "--persistent" in spawns[0]
        # Both turns were delivered as "run" commands over stdin.
        writes = b"".join(c.args[0] for c in proc.stdin.write.call_args_list)
        assert b'"cmd": "run"' in writes
        assert b"task one" in writes and b"task two" in writes
    finally:
        srv._persistent_procs.pop(sid, None)
        srv._running_procs.pop(sid, None)
        srv._live_queues.pop(sid, None)
        srv._turn_done_events.pop(sid, None)


def _make_inspect_echo_proc():
    """Fake persistent worker that answers `inspect` commands on stdin by emitting
    a matching `inspect_result` line on stdout (mirrors the real worker)."""
    out_q: asyncio.Queue = asyncio.Queue()

    class _Stdout:
        def __aiter__(self):
            return self
        async def __anext__(self):
            return await out_q.get()

    proc = mock.AsyncMock()
    proc.returncode = None
    proc.stdout = _Stdout()

    class _BlockingErr:
        def __aiter__(self):
            return self
        async def __anext__(self):
            await asyncio.Event().wait()

    proc.stderr = _BlockingErr()
    proc.stdin = mock.Mock()

    def _write(b: bytes):
        cmd = json.loads(b.decode().strip())
        if cmd.get("cmd") == "inspect":
            reply = {"type": "inspect_result", "id": cmd["id"], "ok": True,
                     "tool": cmd["tool"], "result": f"ran {cmd['tool']} {cmd['args']}"}
            out_q.put_nowait((json.dumps(reply) + "\n").encode())

    proc.stdin.write = mock.Mock(side_effect=_write)
    proc.stdin.drain = mock.AsyncMock()
    return proc


@pytest.mark.asyncio
async def test_inspect_runs_tool_via_persistent_worker(client: AsyncClient, tmp_home: Path):
    """POST /inspect routes a whitelisted tool to the desk's worker and returns its
    result, correlated by request id (not leaked into the activity stream)."""
    sid = "20260607_100000_insp01"
    ws = tmp_home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    ws.mkdir(parents=True)
    srv._session_workspaces[sid] = str(ws)
    proc = _make_inspect_echo_proc()

    with mock.patch("asyncio.create_subprocess_exec", return_value=proc):
        r = await client.post(f"/api/sessions/{sid}/inspect",
                              json={"tool": "search_files",
                                    "args": {"pattern": "foo", "path": "/workspace"}})
    assert r.status_code == 200, r.text
    body = r.json()
    assert body["ok"] is True
    assert body["tool"] == "search_files"
    assert "ran search_files" in body["result"]
    assert not srv._inspect_waiters  # waiter cleaned up

    srv._persistent_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_inspect_requires_tool_and_sandbox(client: AsyncClient, tmp_home: Path):
    r = await client.post("/api/sessions/nope/inspect", json={"tool": "search_files"})
    assert r.status_code == 404  # no sandbox for this desk
    sid = "20260607_100000_insp02"
    ws = tmp_home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    ws.mkdir(parents=True)
    srv._session_workspaces[sid] = str(ws)
    r = await client.post(f"/api/sessions/{sid}/inspect", json={})
    assert r.status_code == 400  # tool required
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_new_session_gives_each_desk_a_private_sandbox(client: AsyncClient, tmp_home: Path):
    """Each new desk must get its own TERMINAL_SANDBOX_DIR so Hermes bind-mounts a
    private /workspace (no cross-desk file leakage)."""
    envs = []

    def _capture(*args, **kwargs):
        envs.append(kwargs.get("env", {}))
        return _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", side_effect=_capture):
        r1 = await client.post("/api/sessions/new", json={"content": "task one"})
        r2 = await client.post("/api/sessions/new", json={"content": "task two"})

    sid1, sid2 = r1.json()["session_id"], r2.json()["session_id"]
    sb1, sb2 = envs[0].get("TERMINAL_SANDBOX_DIR"), envs[1].get("TERMINAL_SANDBOX_DIR")
    assert sb1 and sb2 and sb1 != sb2                     # distinct private sandboxes
    assert sb1.endswith(f"gui_sandboxes/{sid1}")
    assert sb2.endswith(f"gui_sandboxes/{sid2}")
    # HERMES_WORKDIR is that sandbox's docker workspace (file tools + bash share it)
    assert envs[0]["HERMES_WORKDIR"] == f"{sb1}/docker/default/workspace"

    for sid in (sid1, sid2):
        srv._live_queues.pop(sid, None)
        srv._running_procs.pop(sid, None)
        srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_new_session_passes_task_inline_without_taskmd_read_prompt(client: AsyncClient):
    """The augmented message must contain the task inline and must NOT instruct
    the agent to read TASK.md β€” that wording caused a spurious read_file tool
    call adding ~4 min latency on every new session."""
    captured: list[tuple] = []

    async def fake_exec(*args, **kwargs):
        captured.append(args)
        return _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", side_effect=fake_exec):
        r = await client.post("/api/sessions/new", json={"content": "tell me a hiking joke"})

    assert r.status_code == 200
    assert captured, "subprocess was never called"

    # augmented_content is the 3rd positional arg: (venv_py, worker_script, content)
    augmented = captured[0][2]
    assert isinstance(augmented, str)

    # Task text must be in the message so the agent can act on it immediately
    assert "tell me a hiking joke" in augmented

    # Must NOT prompt the agent to read TASK.md
    assert "Task instructions are in TASK.md" not in augmented
    assert "read" not in augmented.lower().split("[")[0]  # not in the bracketed preamble

    # Must explicitly tell the agent to proceed without reading files
    assert "immediately" in augmented or "without reading" in augmented

    sid = r.json()["session_id"]
    srv._live_queues.pop(sid, None)
    srv._running_procs.pop(sid, None)


@pytest.mark.asyncio
async def test_new_session_creates_task_md(client: AsyncClient, tmp_home: Path):
    """TASK.md should exist in workspace immediately after the POST returns."""
    fake_proc = _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", return_value=fake_proc):
        r = await client.post("/api/sessions/new", json={"content": "my task"})

    assert r.status_code == 200
    sid = r.json()["session_id"]
    ws_path = r.json().get("workspace_path", "")
    if ws_path:
        task_md = Path(ws_path) / "TASK.md"
        assert task_md.exists()
        assert "my task" in task_md.read_text()

    srv._live_queues.pop(sid, None)
    srv._running_procs.pop(sid, None)


# ── /api/sessions/{id}/interrupt ──────────────────────────────────────────────


@pytest.mark.asyncio
async def test_interrupt_calls_terminate(client: AsyncClient):
    fake_proc = _make_fake_proc([json.dumps({"type": "done"})])
    sid = "test-interrupt-session"
    srv._running_procs[sid] = fake_proc
    queue: asyncio.Queue = asyncio.Queue()
    srv._live_queues[sid] = queue

    r = await client.post(f"/api/sessions/{sid}/interrupt")
    assert r.status_code == 200
    assert r.json()["ok"] is True
    fake_proc.terminate.assert_called_once()
    # Proc should be removed from running map immediately
    assert sid not in srv._running_procs


@pytest.mark.asyncio
async def test_interrupt_unknown_session_is_ok(client: AsyncClient):
    """Interrupting a session that isn't running should not error."""
    r = await client.post("/api/sessions/nonexistent-xyz/interrupt")
    assert r.status_code == 200
    assert r.json()["ok"] is True


# ── profile concurrency (every desk is isolated, so any profile may run on many) ──


@pytest.mark.asyncio
async def test_multiple_default_desks_can_run_together(client: AsyncClient, tmp_home: Path):
    """Default desks are fully isolated (private HERMES_HOME + per-desk container),
    so several may run at once β€” no profile is locked to one desk."""
    def _capture(*args, **kwargs):
        return _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", side_effect=_capture):
        r1 = await client.post("/api/sessions/new", json={"content": "one"})
        r2 = await client.post("/api/sessions/new", json={"content": "two"})
    try:
        assert r1.status_code == 200 and r2.status_code == 200
        sid1, sid2 = r1.json()["session_id"], r2.json()["session_id"]
        assert sid1 != sid2
    finally:
        for r in (r1, r2):
            sid = r.json().get("session_id")
            if sid:
                srv._live_queues.pop(sid, None)
                srv._running_procs.pop(sid, None)
                srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_resume_allowed_when_same_named_profile_runs_elsewhere(client: AsyncClient, tmp_home: Path, monkeypatch):
    """A profile is no longer locked to one desk: each desk is isolated (private
    HERMES_HOME + per-desk Docker container), so resuming a desk whose profile is
    already running on another desk is allowed."""
    busy_ws = tmp_home / "gui_sandboxes" / "desk-busy" / "ws"
    idle_ws = tmp_home / "gui_sandboxes" / "desk-idle" / "ws"
    busy_ws.mkdir(parents=True)
    idle_ws.mkdir(parents=True)
    (busy_ws / ".hermes_profile").write_text("alpha", encoding="utf-8")
    (idle_ws / ".hermes_profile").write_text("alpha", encoding="utf-8")
    srv._session_workspaces["desk-busy"] = str(busy_ws)
    srv._session_workspaces["desk-idle"] = str(idle_ws)
    srv._running_procs["desk-busy"] = _make_fake_proc([json.dumps({"type": "done"})])

    monkeypatch.setattr("agent_gui.server._PERSISTENT_WORKERS", False, raising=False)
    with mock.patch("asyncio.create_subprocess_exec",
                    return_value=_make_fake_proc([json.dumps({"type": "done"})])):
        try:
            r = await client.post("/api/sessions/desk-idle/resume", json={"content": "Continue."})
            # Same profile runs on desk-busy, but desk-idle still resumes (isolated).
            assert r.status_code != 409
        finally:
            srv._running_procs.pop("desk-busy", None)
            srv._running_procs.pop("desk-idle", None)
            srv._live_queues.pop("desk-idle", None)
            srv._session_workspaces.pop("desk-busy", None)
            srv._session_workspaces.pop("desk-idle", None)


# ── DELETE /api/sessions/{id} ─────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_delete_session_removes_sandbox_and_db(client: AsyncClient, tmp_home: Path):
    """Deleting a desk session stops workers, purges its private state.db, and removes sandbox."""
    import sqlite3

    fake_proc = _make_fake_proc([json.dumps({"type": "done"})])
    with mock.patch("asyncio.create_subprocess_exec", return_value=fake_proc):
        r = await client.post("/api/sessions/new", json={"content": "temporary task"})
    sid = r.json()["session_id"]
    sandbox = tmp_home / "gui_sandboxes" / sid
    assert sandbox.exists()
    # Worker is mocked, so seed the per-desk state.db the real worker would create.
    desk_db = sandbox / "state.db"
    conn = sqlite3.connect(desk_db)
    conn.execute(
        "CREATE TABLE sessions(id TEXT PRIMARY KEY, started_at REAL, ended_at REAL, "
        "source TEXT, model TEXT, parent_session_id TEXT, title TEXT, "
        "message_count INT, input_tokens INT, output_tokens INT)"
    )
    conn.execute(
        "CREATE TABLE messages(id INTEGER PRIMARY KEY, session_id TEXT, role TEXT, "
        "content TEXT, tool_calls TEXT, tool_call_id TEXT, tool_name TEXT, timestamp REAL)"
    )
    conn.execute(
        "INSERT INTO sessions VALUES(?,?,?,?,?,?,?,?,?,?)",
        (sid, 1000.0, None, "cli", "m", None, "", 1, 0, 0),
    )
    conn.commit()
    conn.close()

    r = await client.delete(f"/api/sessions/{sid}")
    assert r.status_code == 200
    body = r.json()
    assert body["ok"] is True
    assert body["deleted"] is True
    assert body["sandbox"] is True
    assert not sandbox.exists()
    assert sid not in srv._running_procs
    assert sid not in srv._session_workspaces


@pytest.mark.asyncio
async def test_delete_unknown_session_404(client: AsyncClient):
    r = await client.delete("/api/sessions/does-not-exist-xyz")
    assert r.status_code == 404


# ── /api/sessions/{id}/resume ─────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_resume_returns_immediately(client: AsyncClient, tmp_home: Path, tmp_path: Path):
    """resume_session should NOT block on the session_id handshake (non-blocking path)."""
    sid = "20240101_120000_resume01"
    ws = tmp_path / "workspace" / "my-task"
    ws.mkdir(parents=True)
    (ws / ".hermes_session_id").write_text(sid)
    srv._session_workspaces[sid] = str(ws)

    fake_proc = _make_fake_proc([
        json.dumps({"type": "log", "msg": "[worker] starting..."}),
        json.dumps({"type": "session_id", "id": sid}),
        json.dumps({"type": "done"}),
    ])

    with mock.patch("asyncio.create_subprocess_exec", return_value=fake_proc):
        r = await client.post(f"/api/sessions/{sid}/resume", json={"content": "continue"})

    assert r.status_code == 200
    assert r.json()["ok"] is True
    assert r.json()["session_id"] == sid
    assert sid in srv._running_procs

    srv._live_queues.pop(sid, None)
    srv._running_procs.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_resume_rejects_already_running(client: AsyncClient):
    sid = "already-running-sid"
    srv._running_procs[sid] = mock.AsyncMock()

    r = await client.post(f"/api/sessions/{sid}/resume", json={"content": "go"})
    assert r.status_code == 409

    srv._running_procs.pop(sid, None)


@pytest.mark.asyncio
async def test_resume_requires_content(client: AsyncClient):
    r = await client.post("/api/sessions/some-sid/resume", json={})
    assert r.status_code == 400


# ── /api/sessions/reassign ────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_reassign_interrupts_from_and_starts_to(client: AsyncClient, tmp_path: Path):
    """Core invariant: terminate() is called on from proc, new worker started for to."""
    from_sid = "from-session-111"
    to_sid = "to-session-222"

    ws = tmp_path / "workspace" / "to-task"
    ws.mkdir(parents=True)
    (ws / ".hermes_session_id").write_text(to_sid)
    srv._session_workspaces[to_sid] = str(ws)

    from_proc = _make_fake_proc([])
    srv._running_procs[from_sid] = from_proc
    from_queue: asyncio.Queue = asyncio.Queue()
    srv._live_queues[from_sid] = from_queue

    to_proc = _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", return_value=to_proc):
        r = await client.post("/api/sessions/reassign", json={
            "from_id": from_sid,
            "to_id": to_sid,
            "message": "Continue.",
        })

    assert r.status_code == 200
    body = r.json()
    assert body["ok"] is True
    assert body["session_id"] == to_sid

    # from proc must have been terminated
    from_proc.terminate.assert_called_once()
    # from proc must have been awaited (proc.wait() called)
    from_proc.wait.assert_called_once()
    # from proc must be removed from running map
    assert from_sid not in srv._running_procs
    # to session must now be registered
    assert to_sid in srv._running_procs

    srv._live_queues.pop(to_sid, None)
    srv._running_procs.pop(to_sid, None)
    srv._session_workspaces.pop(to_sid, None)


@pytest.mark.asyncio
async def test_reassign_skips_interrupt_when_from_eq_to(client: AsyncClient, tmp_path: Path):
    """Self-reassign (from == to) must not interrupt the session."""
    sid = "self-reassign-sid"

    ws = tmp_path / "workspace" / "self-task"
    ws.mkdir(parents=True)
    (ws / ".hermes_session_id").write_text(sid)
    srv._session_workspaces[sid] = str(ws)

    proc = _make_fake_proc([])
    srv._running_procs[sid] = proc
    srv._live_queues[sid] = asyncio.Queue()

    # Because to_id is already running, reassign should return immediately
    r = await client.post("/api/sessions/reassign", json={
        "from_id": sid,
        "to_id": sid,
        "message": "Continue.",
    })

    assert r.status_code == 200
    # Terminate must NOT have been called (from_id == to_id guard)
    proc.terminate.assert_not_called()
    assert sid in srv._running_procs

    srv._live_queues.pop(sid, None)
    srv._running_procs.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_reassign_nonexistent_from_still_starts_to(client: AsyncClient, tmp_path: Path):
    """A nonexistent from_id should be silently skipped; to_id should still start."""
    to_sid = "to-session-333"
    ws = tmp_path / "workspace" / "to-task2"
    ws.mkdir(parents=True)
    (ws / ".hermes_session_id").write_text(to_sid)
    srv._session_workspaces[to_sid] = str(ws)

    to_proc = _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", return_value=to_proc):
        r = await client.post("/api/sessions/reassign", json={
            "from_id": "nonexistent-abc",
            "to_id": to_sid,
            "message": "Continue.",
        })

    assert r.status_code == 200
    assert r.json()["session_id"] == to_sid
    assert to_sid in srv._running_procs

    srv._live_queues.pop(to_sid, None)
    srv._running_procs.pop(to_sid, None)
    srv._session_workspaces.pop(to_sid, None)


@pytest.mark.asyncio
async def test_reassign_to_already_running_is_noop(client: AsyncClient, tmp_path: Path):
    """If to_id is already running, reassign should return ok without double-spawning."""
    from_sid = "from-running"
    to_sid = "to-already-running"

    existing_proc = _make_fake_proc([])
    srv._running_procs[to_sid] = existing_proc
    srv._live_queues[to_sid] = asyncio.Queue()

    from_proc = _make_fake_proc([])
    srv._running_procs[from_sid] = from_proc
    srv._live_queues[from_sid] = asyncio.Queue()

    call_count = {"n": 0}
    original_cse = asyncio.create_subprocess_exec

    async def counting_cse(*args, **kwargs):
        call_count["n"] += 1
        return _make_fake_proc([])

    with mock.patch("asyncio.create_subprocess_exec", side_effect=counting_cse):
        r = await client.post("/api/sessions/reassign", json={
            "from_id": from_sid,
            "to_id": to_sid,
        })

    assert r.status_code == 200
    # No new subprocess should have been spawned for to_id
    assert call_count["n"] == 0

    srv._running_procs.pop(to_sid, None)
    srv._running_procs.pop(from_sid, None)
    srv._live_queues.pop(to_sid, None)
    srv._live_queues.pop(from_sid, None)


# ── /api/warmup ───────────────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_warmup_ok(client: AsyncClient):
    """Warmup should return ok=True even if the venv python isn't found."""
    fake_proc = mock.AsyncMock()
    fake_proc.wait = mock.AsyncMock(return_value=0)

    with mock.patch("asyncio.create_subprocess_exec", return_value=fake_proc):
        r = await client.post("/api/warmup")

    assert r.status_code == 200
    assert r.json()["ok"] is True


# ── Periodic title refresh throttling ────────────────────────────────────────


@pytest.mark.asyncio
async def test_title_refresh_throttled_by_token_growth(monkeypatch):
    """First turn generates; then only regenerate after ~10k more tokens."""
    calls: list[str] = []

    async def fake_gen(sid: str) -> None:
        calls.append(sid)

    class _FakeSession:
        def __init__(self, t: int) -> None:
            self.token_estimate = t

    class _FakeDB:
        tokens = 0
        def get_session(self, sid: str):
            return _FakeSession(self.tokens)

    fdb = _FakeDB()
    srv._session_title_tokens.clear()
    monkeypatch.setattr(srv, "_generate_title", fake_gen)
    monkeypatch.setattr(srv, "_db_ref", fdb)

    sid = "sess-title"
    fdb.tokens = 3000
    await srv._maybe_refresh_title(sid)          # first time β†’ generate
    assert len(calls) == 1
    fdb.tokens = 8000
    await srv._maybe_refresh_title(sid)          # +5k < 10k β†’ skip
    assert len(calls) == 1
    fdb.tokens = 20000
    await srv._maybe_refresh_title(sid)          # +12k β‰₯ 10k β†’ generate
    assert len(calls) == 2

    srv._session_title_tokens.pop(sid, None)


# ── Heartbeat auto-continue ──────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_autocontinue_endpoint_toggles(client: AsyncClient):
    sid = "ac-endpoint"
    try:
        r = await client.post(f"/api/sessions/{sid}/autocontinue", json={"enabled": True})
        assert r.status_code == 200
        body = r.json()
        assert body["enabled"] is True and body["max"] == srv._AUTO_CONTINUE_MAX
        assert srv._session_autocontinue.get(sid) is True
        r2 = await client.post(f"/api/sessions/{sid}/autocontinue", json={"enabled": False})
        assert r2.json()["enabled"] is False
        assert srv._session_autocontinue.get(sid) is False
    finally:
        srv._session_autocontinue.pop(sid, None)
        srv._session_continue_count.pop(sid, None)


def _setup_heartbeat(tmp_path, monkeypatch, sid: str, verdict):
    ws = tmp_path / "hb"
    ws.mkdir()
    (ws / "TASK.md").write_text("# Task\n\nLoop 100 iterations of the action.")
    srv._session_workspaces[sid] = str(ws)
    srv._session_autocontinue[sid] = True
    srv._session_continue_count.pop(sid, None)
    srv._session_user_stopped.discard(sid)
    srv._running_procs.pop(sid, None)
    spawned: list[tuple[str, str]] = []

    async def fake_spawn(s, content, *a, **k):
        spawned.append((s, content))

    async def fake_judge(goal, recent):
        return verdict

    monkeypatch.setattr(srv, "_spawn_resume_ref", fake_spawn)
    monkeypatch.setattr(srv, "_judge_complete", fake_judge)
    return spawned


def _cleanup_heartbeat(sid: str):
    for d in (srv._session_workspaces, srv._session_autocontinue, srv._session_continue_count):
        d.pop(sid, None)
    srv._session_user_stopped.discard(sid)


@pytest.mark.asyncio
async def test_heartbeat_resumes_when_task_incomplete(tmp_path, monkeypatch):
    sid = "hb-incomplete"
    spawned = _setup_heartbeat(tmp_path, monkeypatch, sid, (False, "do iteration 2"))
    try:
        await srv._heartbeat_check(sid)
        assert len(spawned) == 1
        assert spawned[0][0] == sid
        assert "auto-continue" in spawned[0][1] and "iteration 2" in spawned[0][1]
        assert srv._session_continue_count[sid] == 1
    finally:
        _cleanup_heartbeat(sid)


@pytest.mark.asyncio
async def test_heartbeat_stops_when_task_done(tmp_path, monkeypatch):
    sid = "hb-done"
    spawned = _setup_heartbeat(tmp_path, monkeypatch, sid, (True, ""))
    try:
        await srv._heartbeat_check(sid)
        assert spawned == []
        assert srv._session_continue_count.get(sid, 0) == 0
    finally:
        _cleanup_heartbeat(sid)


@pytest.mark.asyncio
async def test_heartbeat_respects_cap(tmp_path, monkeypatch):
    sid = "hb-cap"
    spawned = _setup_heartbeat(tmp_path, monkeypatch, sid, (False, "more work"))
    srv._session_continue_count[sid] = srv._AUTO_CONTINUE_MAX
    try:
        await srv._heartbeat_check(sid)
        assert spawned == []  # over the cap β†’ no resume
    finally:
        _cleanup_heartbeat(sid)


# ── /api/file/preview path containment ──────────────────────────────────────────


@pytest.mark.asyncio
async def test_preview_file_inside_workspace_ok(client: AsyncClient, tmp_path: Path):
    """A file inside the workspace root previews normally."""
    f = tmp_path / "workspace" / "hello.py"
    f.write_text("print('hi')\n")

    r = await client.get("/api/file/preview", params={"path": str(f)})

    assert r.status_code == 200
    body = r.json()
    assert body["type"] == "code"
    assert "print('hi')" in body["content"]


@pytest.mark.asyncio
async def test_preview_file_outside_roots_is_forbidden(client: AsyncClient, tmp_path_factory):
    """A readable file outside the workspace/home roots must be rejected (403)."""
    outside = tmp_path_factory.mktemp("outside") / "secret.env"
    outside.write_text("API_KEY=supersecret\n")

    r = await client.get("/api/file/preview", params={"path": str(outside)})

    assert r.status_code == 403


# ── CORS scoping ─────────────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_cors_rejects_arbitrary_origin(client: AsyncClient):
    """A random website's origin must NOT be reflected back β€” no wildcard CORS,
    so a drive-by site can't read API responses cross-origin."""
    r = await client.get("/api/sessions", headers={"Origin": "https://evil.example.com"})
    assert "access-control-allow-origin" not in {k.lower() for k in r.headers}


@pytest.mark.asyncio
async def test_cors_allows_localhost_origin(client: AsyncClient):
    """The local dev/app origin is allowed (so the proxied/dev flows keep working)."""
    origin = "http://localhost:8765"
    r = await client.get("/api/sessions", headers={"Origin": origin})
    assert r.headers.get("access-control-allow-origin") == origin


# ── /api/docker/cleanup ──────────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_docker_cleanup_removes_when_idle(client: AsyncClient):
    """With no workers running, all hermes-* containers are reaped."""
    srv._running_procs.clear()
    with mock.patch.object(srv, "_list_hermes_containers", return_value=["a", "b", "c"]), \
         mock.patch.object(srv, "_remove_containers", return_value=3) as rm:
        r = await client.post("/api/docker/cleanup")

    assert r.status_code == 200
    body = r.json()
    assert body["skipped"] is False
    assert body["removed"] == 3
    rm.assert_called_once_with(["a", "b", "c"])


@pytest.mark.asyncio
async def test_docker_cleanup_skips_when_worker_running(client: AsyncClient):
    """A running worker means we can't tell which container is in use β†’ skip, report kept."""
    srv._running_procs["sess-active"] = mock.MagicMock()
    try:
        with mock.patch.object(srv, "_list_hermes_containers", return_value=["a", "b"]), \
             mock.patch.object(srv, "_remove_containers") as rm:
            r = await client.post("/api/docker/cleanup")
    finally:
        srv._running_procs.pop("sess-active", None)

    assert r.status_code == 200
    body = r.json()
    assert body["skipped"] is True
    assert body["kept"] == 2
    rm.assert_not_called()


# ── /api/sessions/{id}/workspace_tree ────────────────────────────────────────


@pytest.mark.asyncio
async def test_workspace_tree_lists_dirs_and_files(client: AsyncClient, tmp_path: Path):
    """Lists the real workspace dir: subdirs + all files, dotfiles skipped, with
    preview_type on files."""
    ws = tmp_path / "workspace" / "wt-task"
    (ws / "sub").mkdir(parents=True)
    (ws / "report.pdf").write_bytes(b"%PDF-1.4")
    (ws / "script.py").write_text("print(1)\n")
    (ws / "sub" / "notes.txt").write_text("hi\n")
    (ws / ".hermes_session_id").write_text("wt-sid")  # dotfile β†’ skipped

    sid = "wt-sid"
    srv._session_workspaces[sid] = str(ws)
    try:
        r = await client.get(f"/api/sessions/{sid}/workspace_tree")
    finally:
        srv._session_workspaces.pop(sid, None)

    assert r.status_code == 200
    tree = r.json()
    names = {n["name"] for n in tree}
    assert "sub" in names and "report.pdf" in names and "script.py" in names
    assert ".hermes_session_id" not in names

    sub = next(n for n in tree if n["name"] == "sub")
    assert sub["is_dir"] is True
    assert {c["name"] for c in sub["children"]} == {"notes.txt"}

    pdf = next(n for n in tree if n["name"] == "report.pdf")
    assert pdf["is_dir"] is False
    assert pdf["preview_type"] == "pdf"


@pytest.mark.asyncio
async def test_workspace_tree_empty_when_no_workspace(client: AsyncClient):
    """Unresolvable workspace β†’ [] so the client can fall back to the touched-files list."""
    r = await client.get("/api/sessions/no-such-session/workspace_tree")
    assert r.status_code == 200
    assert r.json() == []


@pytest.mark.asyncio
async def test_workspace_tree_lists_team_repo_under_team_files(client: AsyncClient, tmp_home: Path):
    """team_files/ on disk is an empty mount point; tree should show repo contents."""
    tid = "team-tree"
    repo = tmp_home / "gui_team_repos" / tid
    repo.mkdir(parents=True)
    (repo / "data.csv").write_text("a,b\n1,2\n", encoding="utf-8")
    (repo / "nested").mkdir()
    (repo / "nested" / "notes.txt").write_text("hi\n", encoding="utf-8")

    ws = tmp_home / "gui_sandboxes" / "tree-sid" / "docker" / "default" / "workspace"
    ws.mkdir(parents=True)
    (ws / "TASK.md").write_text("task\n", encoding="utf-8")
    (ws / "team_files").mkdir()
    (ws / ".hermes_team_id").write_text(tid, encoding="utf-8")

    sid = "tree-sid"
    srv._session_workspaces[sid] = str(ws)
    try:
        r = await client.get(f"/api/sessions/{sid}/workspace_tree")
    finally:
        srv._session_workspaces.pop(sid, None)

    assert r.status_code == 200
    tree = r.json()
    tf = next(n for n in tree if n["name"] == "team_files")
    assert tf["is_dir"] is True
    top = {n["name"] for n in tf["children"]}
    assert "data.csv" in top and "nested" in top
    nested = next(n for n in tf["children"] if n["name"] == "nested")
    assert {c["name"] for c in nested["children"]} == {"notes.txt"}
    # Preview paths must point at the canonical repo (host mount point is empty).
    csv = next(n for n in tf["children"] if n["name"] == "data.csv")
    assert str(repo / "data.csv") == csv["path"]


def test_audit_files_includes_team_repo(tmp_path: Path, monkeypatch):
    """Manager audits must read team File Repo files under team_files/, not empty mount."""
    from agent_gui import server as srv

    home = tmp_path
    monkeypatch.setattr(srv, "_home_ref", home)
    ws = home / "ws"
    ws.mkdir()
    (ws / "TASK.md").write_text("Analyze the dataset\n", encoding="utf-8")
    (ws / "team_files").mkdir()
    tid = "team-audit"
    repo = home / "gui_team_repos" / tid
    repo.mkdir(parents=True)
    (repo / "data.csv").write_text("x=1\n", encoding="utf-8")
    (ws / ".hermes_team_id").write_text(tid, encoding="utf-8")

    files = srv._audit_files(ws)
    assert "team_files/data.csv" in files
    assert srv._read_evidence(ws, "team_files/data.csv") == "x=1\n"


@pytest.mark.asyncio
async def test_cached_audit_resumes_idle_desk_but_waits_on_running(tmp_path, monkeypatch):
    """A cached audit (unchanged state) with issues should still let the manager
    resume an IDLE desk (the agent stopped) β€” bounded by the cap β€” but not re-nudge
    a desk that's still actively running its turn."""
    sid = "audit-idle"
    ws = tmp_path / "ws"
    ws.mkdir()
    # Deterministic state hash so the cache always hits.
    monkeypatch.setattr(srv, "_audit_state", lambda s, w: ("h1", "do the task", [], 1))
    srv._session_audits[sid] = {
        "session_id": sid, "state_hash": "h1",
        "summary": {"passed": 1, "failed": 1, "unsure": 0, "total": 2},
        "results": [], "should_intervene": True,
    }
    srv._session_manager_resumes.pop(sid, None)
    srv._running_procs.pop(sid, None)
    try:
        # Idle β†’ bounded resume.
        out = await srv._run_audit(sid, ws)
        assert out["cached"] is True
        assert out["should_intervene"] is True
        assert out["intervention_count"] == 1

        # Past the cap β†’ escalate (no more resumes).
        srv._session_manager_resumes[sid] = srv._MANAGER_MAX_INTERVENTIONS
        out_capped = await srv._run_audit(sid, ws)
        assert out_capped["should_intervene"] is False

        # Running β†’ never re-nudge identical work.
        srv._session_manager_resumes.pop(sid, None)
        srv._running_procs[sid] = object()
        out_running = await srv._run_audit(sid, ws)
        assert out_running["should_intervene"] is False
    finally:
        srv._session_audits.pop(sid, None)
        srv._session_manager_resumes.pop(sid, None)
        srv._running_procs.pop(sid, None)


@pytest.mark.asyncio
async def test_failed_adjudication_caches_nothing(tmp_path, monkeypatch):
    """If adjudication yields no verdicts (timeout/bad JSON), the audit must fail
    like a decompose failure β€” no cached 0/0 audit, no AUDIT.md. A cached empty
    audit keeps matching the idle desk's unchanged state hash, making the desk
    permanently un-auditable."""
    sid = "audit-empty-adjudication"
    ws = tmp_path / "ws"
    ws.mkdir()
    monkeypatch.setattr(srv, "_audit_state", lambda s, w: ("h2", "tell a joke", [], 1))
    monkeypatch.setattr(srv, "_aux_model_config", lambda *a, **k: ("http://test", "m"))
    monkeypatch.setattr(srv, "hermes_model_config", lambda home: ("http://test", "m"))

    calls = {"n": 0}

    async def fake_aux_json(base_url, model, prompt, max_tokens, retries=2):
        calls["n"] += 1
        if calls["n"] == 1:  # decompose succeeds
            return [{"id": 1, "task": "Task 1: joke", "criterion": "a joke exists",
                     "source": "conversation"}]
        return None  # adjudicate fails every retry

    monkeypatch.setattr(srv, "_aux_json", fake_aux_json)
    try:
        out = await srv._run_audit(sid, ws)
        assert out is None
        assert sid not in srv._session_audits
        assert not (ws / "AUDIT.md").exists()
    finally:
        srv._session_audits.pop(sid, None)
        srv._session_manager_resumes.pop(sid, None)
        srv._session_audit_best.pop(sid, None)


@pytest.mark.asyncio
async def test_fresh_audit_skipped_when_running_unless_forced(tmp_path, monkeypatch):
    """Auto patrol must not run a fresh LLM audit mid-turn; Ask manager (force) may."""
    sid = "audit-running-fresh"
    ws = tmp_path / "ws"
    ws.mkdir()
    (ws / "TASK.md").write_text("# Task\n\ndo the thing\n", encoding="utf-8")
    srv._running_procs[sid] = object()
    srv._session_audits.pop(sid, None)
    monkeypatch.setattr(srv, "_aux_model_config", lambda *a, **k: ("http://test", "m"))

    async def boom(*a, **k):
        raise AssertionError("fresh audit LLM should not run")

    monkeypatch.setattr(srv, "_aux_json", boom)
    try:
        out = await srv._run_audit(sid, ws, force=False)
        assert out["skipped_running"] is True
        assert out["should_intervene"] is False
    finally:
        srv._running_procs.pop(sid, None)


# ── Manager aux transport (anthropic `claude` profile vs OpenAI-compat) ──────


@pytest.mark.asyncio
async def test_ollama_aux_chat_routes_anthropic_profile_to_messages_api(monkeypatch):
    """A selected `claude` (anthropic) manager profile must use the Anthropic
    Messages transport, never OpenAI /chat/completions (api.anthropic.com β†’ 404)."""
    monkeypatch.setattr(srv, "_MGR_PROVIDER", "anthropic")

    async def fake_anthropic(base_url, model, prompt, max_tokens):
        return "ANTHROPIC"

    async def fake_openai(*a, **k):
        raise AssertionError("anthropic profile must not use the OpenAI path")

    monkeypatch.setattr(srv, "_anthropic_aux_chat", fake_anthropic)
    monkeypatch.setattr(srv, "_openai_aux_chat", fake_openai)

    out = await srv._ollama_aux_chat("https://api.anthropic.com", "claude-opus-4-8", "hi", 30)
    assert out == "ANTHROPIC"


@pytest.mark.asyncio
async def test_ollama_aux_chat_non_anthropic_uses_openai_path(monkeypatch):
    """Default / vLLM backends keep the OpenAI-compat transport."""
    monkeypatch.setattr(srv, "_MGR_PROVIDER", "")  # no anthropic profile selected

    async def fake_anthropic(*a, **k):
        raise AssertionError("non-anthropic backend must not use the Messages path")

    async def fake_openai(base_url, model, prompt, max_tokens):
        return "OPENAI"

    async def passthrough(base_url, model):
        return model

    monkeypatch.setattr(srv, "_anthropic_aux_chat", fake_anthropic)
    monkeypatch.setattr(srv, "_openai_aux_chat", fake_openai)
    monkeypatch.setattr(srv, "_fallback_ollama_model", passthrough)

    out = await srv._ollama_aux_chat("http://127.0.0.1:8111/v1", "Qwen/Qwen3.5-27B", "hi", 30)
    assert out == "OPENAI"


@pytest.mark.asyncio
async def test_anthropic_aux_chat_builds_messages_request(monkeypatch):
    """POSTs /v1/messages with x-api-key + anthropic-version (no Bearer), and
    concatenates the response's text blocks. base_url's trailing /v1 is tolerated."""
    monkeypatch.setattr(srv, "_MGR_API_KEY", "sk-ant-test")
    captured: dict = {}

    class FakeResp:
        status_code = 200

        def json(self):
            return {"content": [{"type": "text", "text": "Tic Tac Toe"},
                                 {"type": "text", "text": " CLI"}]}

    class FakeClient:
        def __init__(self, *a, **k):
            pass

        async def __aenter__(self):
            return self

        async def __aexit__(self, *a):
            return False

        async def post(self, url, headers=None, json=None):
            captured["url"] = url
            captured["headers"] = headers
            captured["json"] = json
            return FakeResp()

    monkeypatch.setattr(srv.httpx, "AsyncClient", FakeClient)

    out = await srv._anthropic_aux_chat("https://api.anthropic.com", "claude-opus-4-8", "title please", 30)
    assert out == "Tic Tac Toe CLI"
    assert captured["url"] == "https://api.anthropic.com/v1/messages"
    assert captured["headers"]["x-api-key"] == "sk-ant-test"
    assert captured["headers"]["anthropic-version"]
    assert "Authorization" not in captured["headers"]
    assert captured["json"]["model"] == "claude-opus-4-8"
    assert captured["json"]["messages"][0]["content"] == "title please"

    # A base_url that already carries /v1 must not become /v1/v1/messages.
    await srv._anthropic_aux_chat("https://api.anthropic.com/v1/", "m", "x", 30)
    assert captured["url"] == "https://api.anthropic.com/v1/messages"


def test_aux_model_config_uses_selected_profile_verbatim(monkeypatch):
    """A selected manager profile's (base_url, model) are returned as-is for every
    manager call β€” no separate 'aux' model, no override leaking the default model."""
    monkeypatch.setattr(srv, "_MGR_BASE_URL", "http://127.0.0.1:8111/v1")
    monkeypatch.setattr(srv, "_MGR_MODEL", "Qwen/Qwen3.5-27B")
    monkeypatch.setattr(srv, "_EFFECTIVE_MODEL", "claude-opus-4-8")  # must NOT leak in
    assert srv._aux_model_config() == ("http://127.0.0.1:8111/v1", "Qwen/Qwen3.5-27B")


def test_aux_model_config_default_uses_effective_model(monkeypatch):
    """No profile selected β†’ the configured default backend + the effective model
    (the same model agents run on), never a small 'aux' model."""
    monkeypatch.setattr(srv, "_MGR_BASE_URL", "")
    monkeypatch.setattr(srv, "_MGR_MODEL", "")
    monkeypatch.setattr(srv, "_EFFECTIVE_MODEL", "Qwen/Qwen3.5-27B")
    monkeypatch.setattr(srv, "hermes_model_config", lambda home: ("http://default/v1", "cfg-model"))
    assert srv._aux_model_config() == ("http://default/v1", "Qwen/Qwen3.5-27B")


@pytest.mark.asyncio
async def test_set_manager_profile_rejects_claude_sdk(client: AsyncClient):
    """The Claude Agent SDK isn't an LLM endpoint (it resolves models itself), so
    it can't run the manager β€” every reserved SDK id is rejected, not silently set."""
    for sdk_id in ("claude-sdk", "claude-agent-sdk", "claude-code"):
        r = await client.post("/api/manager/profile", json={"profile": sdk_id})
        assert r.status_code == 400, sdk_id


# ── Real per-event timestamps (overlay vs. coarse flush time) ────────────────


def _ev(event_type: str, ts: str = "2026-06-01T00:00:00+00:00", tool_name: str = ""):
    from agent_gui.activity_parser import ActivityEvent
    return ActivityEvent(timestamp=ts, event_type=event_type, icon="", title="",
                         detail="", tool_name=tool_name)


def test_apply_real_times_overlays_in_order_per_kind():
    sid = "ts-overlay"
    try:
        # Recorded stream (real emit times), interleaved across kinds.
        srv._record_event_time(sid, "user_message", 1000.0)
        srv._record_event_time(sid, "message", 1002.0)
        srv._record_event_time(sid, "tool_call", 1003.0, "bash")
        srv._record_event_time(sid, "tool_result", 1005.0, "bash")
        srv._record_event_time(sid, "tool_call", 1007.0, "read_file")
        srv._record_event_time(sid, "tool_result", 1009.0, "read_file")
        # DB-derived events (parse_activity emits tool_call(s) before message text).
        events = [
            _ev("user_message"), _ev("tool_call"), _ev("tool_result"),
            _ev("message"), _ev("tool_call"), _ev("tool_result"),
        ]
        srv._apply_real_times(events, sid)
        from datetime import datetime, timezone
        def sec(e): return datetime.fromisoformat(e.timestamp).timestamp()
        # Each kind consumed its recorded times FIFO, regardless of inter-kind order.
        assert [sec(e) for e in events] == [1000.0, 1003.0, 1005.0, 1002.0, 1007.0, 1009.0]
        assert all(e.time_exact for e in events)
    finally:
        srv._session_event_times.pop(sid, None)


def test_apply_real_times_no_data_keeps_flush_time_as_approximate():
    sid = "ts-none"
    events = [_ev("tool_call", ts="2026-06-01T12:00:00+00:00"),
              _ev("tool_result", ts="2026-06-01T12:00:00+00:00")]
    srv._apply_real_times(events, sid)  # nothing recorded
    # Timestamps untouched (no fabrication) and explicitly marked not-exact.
    assert all(e.timestamp == "2026-06-01T12:00:00+00:00" for e in events)
    assert all(e.time_exact is False for e in events)


def test_apply_real_times_partial_match_marks_rest_approximate():
    sid = "ts-partial"
    try:
        srv._record_event_time(sid, "tool_call", 2000.0, "bash")  # only one marker
        events = [_ev("tool_call"), _ev("tool_call")]
        srv._apply_real_times(events, sid)
        assert events[0].time_exact is True            # matched β†’ exact
        assert events[1].time_exact is False           # unmatched β†’ approximate
    finally:
        srv._session_event_times.pop(sid, None)


def test_record_worker_evt_time_token_runs_and_tools():
    sid = "ts-runs"
    try:
        state: dict = {}
        # token run β†’ one 'message'; tool boundary; another token run β†’ another
        # 'message'. Text must clear MIN_MESSAGE_LEN to match parse_activity, which
        # only emits a message event for substantive content (a tiny run would be a
        # surplus marker β€” see test_realtime_markers).
        srv._record_worker_evt_time(sid, {"type": "token", "text": "Hello there", "ts": 1.0}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "!", "ts": 1.1}, state)
        srv._record_worker_evt_time(sid, {"type": "tool_start", "name": "bash", "ts": 2.0}, state)
        srv._record_worker_evt_time(sid, {"type": "tool_done", "name": "bash", "ts": 3.0}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "All finished now", "ts": 4.0}, state)
        kinds = [k for k, _ts, _n in srv._session_event_times[sid]]
        assert kinds == ["message", "tool_call", "tool_result", "message"]
    finally:
        srv._session_event_times.pop(sid, None)


def test_record_worker_evt_time_records_thinking_runs():
    """A run of `thinking` events marks one 'thinking_start' time. Without this
    the reasoning event keeps Hermes's batch-flush time and the overview's
    timestamp sort shoves reasoning to the back of the turn (see #overview-bug)."""
    sid = "ts-think"
    try:
        state: dict = {}
        # think run β†’ reasoning step; tokens (response); tool; think run again.
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": "hm", "ts": 1.0}, state)
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": "mm", "ts": 1.1}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "okay then", "ts": 2.0}, state)
        srv._record_worker_evt_time(sid, {"type": "tool_start", "name": "bash", "ts": 3.0}, state)
        srv._record_worker_evt_time(sid, {"type": "tool_done", "name": "bash", "ts": 4.0}, state)
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": "next", "ts": 5.0}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "all done now", "ts": 6.0}, state)
        recorded = srv._session_event_times[sid]
        kinds = [k for k, _ts, _n in recorded]
        # One marker per run; thinking_start recorded at the run's FIRST event.
        assert kinds == ["thinking_start", "message", "tool_call", "tool_result",
                         "thinking_start", "message"]
        assert recorded[0][1] == 1.0 and recorded[4][1] == 5.0
    finally:
        srv._session_event_times.pop(sid, None)


def test_record_worker_evt_time_skips_whitespace_only_thinking():
    """Whitespace-only reasoning (qwen3-style empty <think>\\n\\n</think>) must NOT
    record a thinking_start marker. parse_activity drops it from the feed (its
    `.strip()` gate), so a marker for it would make thinking_start markers
    outnumber the feed's reasoning events β€” and _apply_real_times (FIFO per kind)
    would then hand each real reasoning step an *earlier* run's time, so every
    trace after the first showed a stuck, too-early timestamp."""
    sid = "ts-think-ws"
    try:
        state: dict = {}
        # Run 1: substantive reasoning β†’ one marker at its first token.
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": "real", "ts": 1.0}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "a", "ts": 2.0}, state)
        # Run 2: whitespace-only reasoning β†’ NO marker (dropped from the feed).
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": "\n\n", "ts": 3.0}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "b", "ts": 4.0}, state)
        # Run 3: leading blank delta then content β†’ marker at the CONTENT's time.
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": " ", "ts": 5.0}, state)
        srv._record_worker_evt_time(sid, {"type": "thinking", "text": "more", "ts": 5.1}, state)
        srv._record_worker_evt_time(sid, {"type": "token", "text": "c", "ts": 6.0}, state)
        think = [(k, t) for k, t, _n in srv._session_event_times[sid] if k == "thinking_start"]
        # Exactly two reasoning markers (runs 1 and 3); the whitespace run is skipped.
        assert think == [("thinking_start", 1.0), ("thinking_start", 5.1)]
    finally:
        srv._session_event_times.pop(sid, None)


def test_apply_real_times_keeps_reasoning_interleaved_after_sort():
    """End-to-end: reasoning that got a real (early) time stays interleaved when
    the overview sorts by timestamp, instead of collapsing to the flush time at
    the end of the turn."""
    sid = "ts-think-sort"
    try:
        # Real per-kind emit times (reasoning precedes each tool step).
        srv._record_event_time(sid, "thinking_start", 1001.0)
        srv._record_event_time(sid, "tool_call", 1002.0, "bash")
        srv._record_event_time(sid, "tool_result", 1003.0, "bash")
        srv._record_event_time(sid, "thinking_start", 1004.0)
        srv._record_event_time(sid, "message", 1005.0)
        # DB-derived events all share one batch-flush time (the turn's end).
        flush = "2026-06-01T12:00:00+00:00"
        events = [
            _ev("thinking_start", ts=flush), _ev("tool_call", ts=flush),
            _ev("tool_result", ts=flush), _ev("thinking_start", ts=flush),
            _ev("message", ts=flush),
        ]
        srv._apply_real_times(events, sid)
        from datetime import datetime
        events.sort(key=lambda e: datetime.fromisoformat(e.timestamp).timestamp())
        # Reasoning is NOT shoved to the back β€” order matches real emit times.
        assert [e.event_type for e in events] == [
            "thinking_start", "tool_call", "tool_result", "thinking_start", "message"]
    finally:
        srv._session_event_times.pop(sid, None)


# ── Resume with a different agent (drag-from-bench) ───────────────────────────


@pytest.mark.asyncio
async def test_resume_can_switch_agent(tmp_path: Path):
    """Dropping a bench agent on a desk resumes the SAME conversation with a
    DIFFERENT profile: the .hermes_profile marker is rewritten and the resumed
    worker's env points at the new profile's config home."""
    from agent_gui.gui_config import GuiConfig

    profiles = tmp_path / "profiles"
    for name in ("coder", "researcher"):
        d = profiles / name
        d.mkdir(parents=True)
        (d / "config.yaml").write_text("model:\n  default: m\n  base_url: http://x/v1\n")
    home = tmp_path / "home"; home.mkdir()
    ws_root = tmp_path / "ws"; ws_root.mkdir()
    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))

    envs: list[dict] = []

    def _cap(*args, **kwargs):
        envs.append(kwargs.get("env", {}))
        return _make_fake_proc([json.dumps({"type": "done"})])

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch("asyncio.create_subprocess_exec", side_effect=_cap):
            r = await client.post("/api/sessions/new", json={"content": "hi", "agent": "coder"})
            sid = r.json()["session_id"]
            srv._running_procs.pop(sid, None)
            srv._live_queues.pop(sid, None)
            r2 = await client.post(f"/api/sessions/{sid}/resume",
                                   json={"content": "keep going", "agent": "researcher"})
            assert r2.status_code == 200

    ws = home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    assert (ws / ".hermes_profile").read_text().strip() == "researcher"
    # new_session spawned with coder, resume spawned with researcher.
    assert envs[0].get("HERMES_GUI_CONFIG_HOME", "").endswith("coder")
    assert envs[-1].get("HERMES_GUI_CONFIG_HOME", "").endswith("researcher")

    srv._running_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_new_session_ollama_agent_model_override(tmp_path: Path):
    from agent_gui.gui_config import GuiConfig

    profiles = tmp_path / "profiles"
    ollama_dir = profiles / "coder"
    ollama_dir.mkdir(parents=True)
    (ollama_dir / "config.yaml").write_text(yaml.safe_dump({
        "model": {"default": "qwen3:4b", "base_url": "http://127.0.0.1:11434/v1"},
    }))
    home = tmp_path / "home"; home.mkdir()
    ws_root = tmp_path / "ws"; ws_root.mkdir()
    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))

    envs: list[dict] = []

    def _cap(*args, **kwargs):
        envs.append(kwargs.get("env", {}))
        return _make_fake_proc([json.dumps({"type": "done"})])

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch("asyncio.create_subprocess_exec", side_effect=_cap):
            with mock.patch.object(srv, "_resolve_gui_model", side_effect=lambda m, *_a: m):
                r = await client.post("/api/sessions/new", json={
                    "content": "hi", "agent": "coder", "model": "llama3:8b",
                })
        assert r.status_code == 200
        sid = r.json()["session_id"]
        assert r.json()["session"]["agent_model"] == "llama3:8b"

    assert envs[0].get("HERMES_MODEL") == "llama3:8b"
    ws = home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    assert (ws / ".hermes_model").read_text().strip() == "llama3:8b"

    srv._running_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_new_session_does_not_pin_profile_default_model(tmp_path: Path):
    """A model equal to the profile default is NOT pinned, so the desk tracks the
    profile config live (model + base_url together) β€” prevents vLLM↔Ollama drift."""
    from agent_gui.gui_config import GuiConfig

    profiles = tmp_path / "profiles"
    coder = profiles / "coder"
    coder.mkdir(parents=True)
    (coder / "config.yaml").write_text(yaml.safe_dump({
        "model": {"default": "qwen3:4b", "base_url": "http://127.0.0.1:11434/v1"},
    }))
    home = tmp_path / "home"; home.mkdir()
    ws_root = tmp_path / "ws"; ws_root.mkdir()
    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))

    def _cap(*args, **kwargs):
        return _make_fake_proc([json.dumps({"type": "done"})])

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch("asyncio.create_subprocess_exec", side_effect=_cap):
            with mock.patch.object(srv, "_resolve_gui_model", side_effect=lambda m, *_a: m):
                r = await client.post("/api/sessions/new", json={
                    "content": "hi", "agent": "coder", "model": "qwen3:4b",
                })
        assert r.status_code == 200
        sid = r.json()["session_id"]

    ws = home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    assert not (ws / ".hermes_model").exists()

    srv._running_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_new_session_agent_uses_profile_tool_preset(tmp_path: Path):
    """When tools are omitted, agent desks inherit the profile's default preset."""
    from agent_gui.gui_config import GuiConfig

    profiles = tmp_path / "profiles"
    pdir = profiles / "researcher"
    pdir.mkdir(parents=True)
    (pdir / "config.yaml").write_text("model:\n  default: m\n  base_url: http://x/v1\n")
    (pdir / ".gui-meta.yaml").write_text(yaml.safe_dump({
        "default_tool_preset": "chat",
        "tool_presets": {
            "chat": [],
            "lean": ["file", "terminal", "search"],
            "full": ["file", "terminal", "search", "browser"],
        },
    }))
    home = tmp_path / "home"; home.mkdir()
    ws_root = tmp_path / "ws"; ws_root.mkdir()
    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))

    envs: list[dict] = []

    def _cap(*args, **kwargs):
        envs.append(kwargs.get("env", {}))
        return _make_fake_proc([json.dumps({"type": "done"})])

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch("asyncio.create_subprocess_exec", side_effect=_cap):
            r = await client.post("/api/sessions/new", json={"content": "hi", "agent": "researcher"})
        assert r.status_code == 200
        sid = r.json()["session_id"]

    assert envs[0].get("HERMES_GUI_ENABLED_TOOLSETS") == ""
    ws = home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    assert (ws / ".hermes_tools").read_text() == ""

    srv._running_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_patch_agent_syncs_profile_default_tools(tmp_path: Path):
    """Assigning an agent profile must rewrite .hermes_tools to that profile's default."""
    from dataclasses import dataclass
    from agent_gui.gui_config import GuiConfig
    from agent_gui.db import HermesDB

    profiles = tmp_path / "profiles"
    pdir = profiles / "researcher"
    pdir.mkdir(parents=True)
    (pdir / "config.yaml").write_text("model:\n  default: m\n")
    (pdir / ".gui-meta.yaml").write_text(yaml.safe_dump({
        "default_tool_preset": "chat",
        "tool_presets": {
            "chat": [],
            "lean": ["file", "terminal", "search"],
            "full": ["file", "terminal", "search", "browser"],
        },
    }))
    home = tmp_path / "home"
    home.mkdir()
    ws_root = tmp_path / "ws"
    ws_root.mkdir()
    sid = "desk-chat-tools"
    ws = home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    ws.mkdir(parents=True)
    (ws / ".hermes_tools").write_text("file,terminal,search,browser")

    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))
    srv._session_workspaces[sid] = str(ws)

    @dataclass
    class _FakeSession:
        id: str

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch.object(HermesDB, "get_session", return_value=_FakeSession(sid)):
            r = await client.patch(f"/api/sessions/{sid}/desk-config", json={"agent": "researcher"})

    assert r.status_code == 200
    assert (ws / ".hermes_tools").read_text() == ""
    assert r.json().get("desk_tools") == []

    srv._session_workspaces.pop(sid, None)


@pytest.mark.asyncio
async def test_delete_agent_profile(tmp_path: Path):
    from agent_gui.gui_config import GuiConfig

    profiles = tmp_path / "profiles"
    home = tmp_path / "home"
    clone = home / "profiles" / "my-clone"
    clone.mkdir(parents=True)
    (clone / "config.yaml").write_text("model: {}\n")
    ws_root = tmp_path / "ws"; ws_root.mkdir()
    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch("agent_gui.server.delete_profile") as delete_mock:
            delete_mock.return_value = None
            r = await client.delete("/api/agents/coder")
        assert r.status_code == 200
        delete_mock.assert_called_once()
        with mock.patch("agent_gui.server.delete_profile") as delete_mock:
            delete_mock.return_value = None
            r2 = await client.delete("/api/agents/my-clone")
        assert r2.status_code == 200
        assert delete_mock.call_count == 1


@pytest.mark.asyncio
async def test_delete_agent_unbinds_deployed_desks(tmp_path: Path):
    from agent_gui.gui_config import GuiConfig

    profiles = tmp_path / "profiles"
    home = tmp_path / "home"
    agent_dir = home / "profiles" / "my-clone"
    agent_dir.mkdir(parents=True)
    (agent_dir / "config.yaml").write_text("model: {}\n")
    ws_root = tmp_path / "ws"; ws_root.mkdir()
    sid = "desk-session-1"
    ws = home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    ws.mkdir(parents=True)
    (ws / ".hermes_profile").write_text("my-clone", encoding="utf-8")

    cfg = GuiConfig(hermes_home=home, agent_profiles_dir=profiles)
    app = create_app(gui_config=cfg, workspace_root=str(ws_root))
    srv = app.state.gui_server if hasattr(app.state, "gui_server") else None

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        with mock.patch("agent_gui.server.delete_profile") as delete_mock:
            delete_mock.return_value = None
            r = await client.delete("/api/agents/my-clone")
        assert r.status_code == 200
        assert r.json().get("unbound_desks") == [sid]
        assert not (ws / ".hermes_profile").exists()


# ── Team file repo ────────────────────────────────────────────────────────────


def _data_url(blob: bytes) -> str:
    import base64 as _b64
    return "data:application/octet-stream;base64," + _b64.b64encode(blob).decode()


@pytest.mark.asyncio
async def test_team_file_repo_upload_list_and_desk_sync(client: AsyncClient, tmp_home: Path):
    tid = "team-abc"
    r = await client.post(f"/api/teams/{tid}/files",
                          json={"path": "docs/readme.txt", "data": _data_url(b"hello world")})
    assert r.status_code == 200
    repo_file = tmp_home / "gui_team_repos" / tid / "docs" / "readme.txt"
    assert repo_file.read_bytes() == b"hello world"

    r = await client.get(f"/api/teams/{tid}/files")
    assert "docs" in [n["name"] for n in r.json()["files"]]

    # A new desk on that team gets a host-visible team_files symlink to the repo
    # (+ Docker volume env). The symlink makes the files visible from a host
    # terminal; the container still gets the repo via the team_files bind mount.
    with mock.patch("asyncio.create_subprocess_exec",
                    return_value=_make_fake_proc([json.dumps({"type": "done"})])):
        r2 = await client.post("/api/sessions/new", json={"content": "task", "team_id": tid})
    sid = r2.json()["session_id"]
    ws = tmp_home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    mount = ws / "team_files"
    assert mount.is_symlink()
    assert mount.resolve() == (tmp_home / "gui_team_repos" / tid).resolve()
    # Files in the repo are now visible through the desk's team_files symlink.
    assert (mount / "docs" / "readme.txt").read_bytes() == b"hello world"
    assert (tmp_home / "gui_team_repos" / tid / "docs" / "readme.txt").read_bytes() == b"hello world"

    r = await client.delete(f"/api/teams/{tid}/files", params={"path": "docs/readme.txt"})
    assert r.status_code == 200
    assert not repo_file.exists()

    srv._running_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)
    srv._session_team.pop(sid, None)
    srv._team_sessions.pop(tid, None)


@pytest.mark.asyncio
async def test_team_file_repo_rejects_traversal(client: AsyncClient):
    bad = await client.post("/api/teams/team-x/files",
                            json={"path": "../escape.txt", "data": _data_url(b"x")})
    assert bad.status_code in (400, 403)
    # An id with illegal characters (space) is rejected before any filesystem touch.
    bad_id = await client.post("/api/teams/bad%20id%21/files",
                               json={"path": "ok.txt", "data": _data_url(b"x")})
    assert bad_id.status_code == 400


@pytest.mark.asyncio
async def test_team_register_syncs_existing_desk(client: AsyncClient, tmp_home: Path):
    """Registering desks after upload copies the repo even without new_session team_id."""
    tid = "team-reg"
    sid = "20260605_120000_reg001"
    ws = tmp_home / "gui_sandboxes" / sid / "docker" / "default" / "workspace"
    ws.mkdir(parents=True)
    (ws / ".hermes_session_id").write_text(sid)
    srv._session_workspaces[sid] = str(ws)

    await client.post(f"/api/teams/{tid}/files",
                      json={"path": "shared.txt", "data": _data_url(b"team data")})

    r = await client.post(f"/api/teams/{tid}/register", json={"session_ids": [sid]})
    assert r.status_code == 200
    assert r.json()["registered"] == 1
    mount = ws / "team_files"
    assert mount.is_symlink()
    assert mount.resolve() == (tmp_home / "gui_team_repos" / tid).resolve()
    assert (mount / "shared.txt").read_bytes() == b"team data"
    assert (tmp_home / "gui_team_repos" / tid / "shared.txt").read_bytes() == b"team data"
    assert (ws / ".hermes_team_id").read_text().strip() == tid

    srv._session_workspaces.pop(sid, None)
    srv._session_team.pop(sid, None)
    srv._team_sessions.pop(tid, None)


@pytest.mark.asyncio
async def test_team_session_env_allows_repo_writes(client: AsyncClient, tmp_home: Path):
    """Worker env must expose team repo for host write_file + Docker bind-mount."""
    tid = "team-write-env"
    envs: list[dict] = []

    def _cap(*args, **kwargs):
        envs.append(kwargs.get("env", {}))
        return _make_fake_proc([json.dumps({"type": "done"})])

    with mock.patch("asyncio.create_subprocess_exec", side_effect=_cap):
        r = await client.post("/api/sessions/new", json={"content": "task", "team_id": tid})
    assert r.status_code == 200
    sid = r.json()["session_id"]
    repo = (tmp_home / "gui_team_repos" / tid).resolve()
    env = envs[0]
    assert env.get("HERMES_GUI_TEAM_REPO") == str(repo)
    assert env.get("HERMES_GUI_DOCKER_WORKSPACE") == "/workspace"
    vols = json.loads(env.get("TERMINAL_DOCKER_VOLUMES", "[]"))
    ws = (tmp_home / "gui_sandboxes" / sid / "docker" / "default" / "workspace").resolve()
    assert f"{ws}:/workspace" in vols
    assert f"{repo}:/workspace/team_files" in vols

    srv._running_procs.pop(sid, None)
    srv._live_queues.pop(sid, None)
    srv._session_workspaces.pop(sid, None)
    srv._session_team.pop(sid, None)
    srv._team_sessions.pop(tid, None)


# ── Agent profiles (bench create / persona edit) ─────────────────────────────


def _seed_profile(root: Path, pid: str, *, soul: str = "", memory: str = "") -> Path:
    p = root / pid
    p.mkdir(parents=True, exist_ok=True)
    (p / "config.yaml").write_text("model:\n  default: test-model\n")
    if soul:
        (p / "SOUL.md").write_text(soul, encoding="utf-8")
    if memory:
        (p / "memories").mkdir(exist_ok=True)
        (p / "memories" / "MEMORY.md").write_text(memory, encoding="utf-8")
    return p


@pytest.mark.asyncio
async def test_agents_list_excludes_jedi_padawan(client: AsyncClient, tmp_home: Path):
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder")
    _seed_profile(profiles, "jedi")
    _seed_profile(profiles, "researcher")
    r = await client.get("/api/agents")
    assert r.status_code == 200
    ids = [a["id"] for a in r.json()["agents"]]
    assert "coder" in ids and "researcher" in ids
    assert "jedi" not in ids


@pytest.mark.asyncio
async def test_agent_prototypes(client: AsyncClient, tmp_home: Path):
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder")
    _seed_profile(profiles, "researcher")
    r = await client.get("/api/agents/prototypes")
    assert [p["id"] for p in r.json()["prototypes"]] == ["coder", "researcher"]


@pytest.mark.asyncio
async def test_agent_persona_get_and_put(client: AsyncClient, tmp_home: Path):
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder", soul="hello soul", memory="hello mem")
    r = await client.get("/api/agents/coder/persona")
    assert r.status_code == 200
    body = r.json()
    assert body["soul"] == "hello soul"
    assert body["memory"] == "hello mem"
    assert body["is_prototype"] is True

    r2 = await client.put("/api/agents/coder/persona",
                          json={"soul": "updated soul", "memory": "updated mem"})
    assert r2.status_code == 200
    assert (profiles / "coder" / "SOUL.md").read_text() == "updated soul"
    assert (profiles / "coder" / "memories" / "MEMORY.md").read_text() == "updated mem"


@pytest.mark.asyncio
async def test_agent_persona_updates_model_default(client: AsyncClient, tmp_home: Path):
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder")
    (profiles / "coder" / "config.yaml").write_text(
        "model:\n  default: old\n  base_url: http://localhost:8010/v1\n"
    )
    r = await client.put("/api/agents/coder/persona", json={"model_default": "Qwen/New"})
    assert r.status_code == 200
    import yaml as _yaml
    cfg = _yaml.safe_load((profiles / "coder" / "config.yaml").read_text())
    assert cfg["model"]["default"] == "Qwen/New"


@pytest.mark.asyncio
async def test_list_llm_models(client: AsyncClient, monkeypatch):
    async def fake_fetch(base_url: str, **kwargs):
        assert "8010" in base_url
        return ["Qwen/A", "Qwen/B"]

    monkeypatch.setattr("agent_gui.server.fetch_llm_models", fake_fetch)
    r = await client.get("/api/llm/models?base_url=http://localhost:8010/v1&agent_id=coder")
    assert r.status_code == 200
    body = r.json()
    assert body["models"] == ["Qwen/A", "Qwen/B"]
    assert body["base_url"] == "http://localhost:8010/v1"


@pytest.mark.asyncio
async def test_agent_capabilities(client: AsyncClient, tmp_home: Path):
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder")
    bundle = profiles / "coder" / "skills" / "dev"
    (bundle / "my-skill").mkdir(parents=True)
    r = await client.get("/api/agents/coder/capabilities")
    assert r.status_code == 200
    body = r.json()
    assert body["id"] == "coder"
    assert "chat" in body["presets"] and body["presets"]["chat"] == []
    assert "search" in body["presets"]["lean"]
    assert body["skill_count"] == 1
    assert body["skill_bundles"][0]["bundle"] == "dev"


@pytest.mark.asyncio
async def test_create_agent_profile(client: AsyncClient, tmp_home: Path):
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder")
    dest = profiles / "newone"

    with mock.patch("agent_gui.server.create_profile_via_hermes") as cp:
        def _fake_create(home, pid, clone_from):
            dest.mkdir(parents=True, exist_ok=True)
            (dest / "config.yaml").write_text("model: {}\n")
            return dest
        cp.side_effect = _fake_create
        r = await client.post("/api/agents", json={
            "id": "newone",
            "clone_from": "coder",
            "name": "New One",
            "soul": "custom soul",
            "memory": "custom mem",
        })
    assert r.status_code == 200
    assert r.json()["agent"]["id"] == "newone"
    assert (dest / "SOUL.md").read_text() == "custom soul"
    assert (dest / "memories" / "MEMORY.md").read_text() == "custom mem"
    meta = yaml.safe_load((dest / ".gui-meta.yaml").read_text())
    assert meta["clone_from"] == "coder"
    assert meta["name"] == "New One"


@pytest.mark.asyncio
async def test_create_agent_profile_with_provider(client: AsyncClient, tmp_home: Path):
    """Cloning with a chosen provider writes the model block into the clone."""
    profiles = tmp_home / "profiles"
    _seed_profile(profiles, "coder")
    dest = profiles / "ollama-clone"

    with mock.patch("agent_gui.server.create_profile_via_hermes") as cp:
        def _fake_create(home, pid, clone_from):
            dest.mkdir(parents=True, exist_ok=True)
            (dest / "config.yaml").write_text("model: {}\n")
            return dest
        cp.side_effect = _fake_create
        r = await client.post("/api/agents", json={
            "id": "ollama-clone",
            "clone_from": "coder",
            "model_default": "qwen3.5:4b",
            "base_url": "http://127.0.0.1:11434/v1",
            "provider": "ollama-launch",
        })
    assert r.status_code == 200
    cfg = yaml.safe_load((dest / "config.yaml").read_text())
    assert cfg["model"]["default"] == "qwen3.5:4b"
    assert cfg["model"]["base_url"] == "http://127.0.0.1:11434/v1"
    assert cfg["model"]["provider"] == "ollama-launch"


@pytest.mark.asyncio
async def test_global_persona_get_put(tmp_home: Path, tmp_path: Path):
    home = tmp_home
    (home / "memories").mkdir(parents=True)
    (home / "SOUL.md").write_text("hello soul")
    (home / "memories" / "MEMORY.md").write_text("hello memory")
    (home / "config.yaml").write_text("model:\n  default: gpt-test\n  base_url: http://localhost/v1\n")
    ws_root = tmp_path / "workspace"
    ws_root.mkdir()
    app = create_app(hermes_home=str(home), workspace_root=str(ws_root))
    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        r = await client.get("/api/global/persona")
        assert r.status_code == 200
        body = r.json()
        assert body["soul"] == "hello soul"
        assert body["memory"] == "hello memory"
        assert body["model"] == "gpt-test"

        r2 = await client.put("/api/global/persona", json={"soul": "new soul", "memory": "new mem"})
        assert r2.status_code == 200
        assert (home / "SOUL.md").read_text() == "new soul"
        assert (home / "memories" / "MEMORY.md").read_text() == "new mem"


@pytest.mark.asyncio
async def test_model_reasoning_qwen_off_on(tmp_home: Path, tmp_path: Path):
    home = tmp_home
    (home / "config.yaml").write_text(
        "model:\n  default: Qwen/Qwen3.6-27B\n  base_url: http://127.0.0.1:8010/v1\n"
    )
    ws_root = tmp_path / "workspace"
    ws_root.mkdir()
    app = create_app(hermes_home=str(home), workspace_root=str(ws_root))

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        # Global config is vLLM β€” without desk base_url, reasoning is unavailable.
        r0 = await client.get("/api/models/reasoning", params={"model": "qwen3.5:4b"})
        assert r0.json()["options"] == []

        # Desk Ollama profile backend β†’ off/on for qwen.
        r = await client.get(
            "/api/models/reasoning",
            params={"model": "qwen3.5:4b", "base_url": "http://127.0.0.1:11434/v1"},
        )
    assert r.status_code == 200
    assert r.json()["options"] == [
        {"value": "none", "label": "off"},
        {"value": "medium", "label": "on"},
    ]


@pytest.mark.asyncio
async def test_model_reasoning_qwen_guictx_alias(tmp_home: Path, tmp_path: Path, monkeypatch):
    home = tmp_path / "home"
    home.mkdir()
    (home / "config.yaml").write_text(
        "model:\n  default: qwen3.5:4b\n  base_url: http://127.0.0.1:11434/v1\n"
    )
    ws_root = tmp_path / "workspace"
    ws_root.mkdir()
    app = create_app(hermes_home=str(home), workspace_root=str(ws_root))

    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
        r = await client.get(
            "/api/models/reasoning",
            params={"model": "qwen3.5:4b-guictx65536", "base_url": "http://127.0.0.1:11434/v1"},
        )
    assert r.status_code == 200
    assert r.json()["options"] == [
        {"value": "none", "label": "off"},
        {"value": "medium", "label": "on"},
    ]


# ── Claude model selection (opus/sonnet/haiku) ──────────────────────────────────
# The Claude Code agent isn't an HTTP backend, so /api/llm/models must serve its
# alias list (otherwise the desk picker shows only the current model), and a model
# change on an existing Claude desk must land in .claude_model (the worker reads
# that, not .hermes_model) without 404ing on the DB-free desk.


@pytest.mark.asyncio
async def test_llm_models_lists_claude_aliases_by_agent_id(client: AsyncClient):
    r = await client.get("/api/llm/models", params={"agent_id": "claude-sdk"})
    assert r.status_code == 200
    body = r.json()
    assert body["models"] == ["sonnet", "opus", "haiku"]
    assert "opus" in body["models"]


@pytest.mark.asyncio
async def test_llm_models_lists_claude_aliases_by_base_url(client: AsyncClient):
    # The desk model picker sends the Claude card's base_url ("claude-agent-sdk").
    r = await client.get("/api/llm/models", params={"base_url": "claude-agent-sdk"})
    assert r.status_code == 200
    assert "opus" in r.json()["models"]


@pytest.mark.asyncio
async def test_patch_claude_desk_model_writes_claude_marker(client: AsyncClient, tmp_path: Path):
    # A DB-free Claude desk: stand up its sandbox + agent marker directly (no worker).
    sid = "20260101_120000_clauded"
    ws = tmp_path / "claude_ws"
    ws.mkdir()
    (ws / ".hermes_profile").write_text("claude-sdk", encoding="utf-8")
    srv._session_workspaces[sid] = str(ws)
    try:
        r = await client.patch(f"/api/sessions/{sid}/desk-config", json={"model": "opus"})
        assert r.status_code == 200                    # DB-free Claude stub, not a 404
        assert "opus" in json.dumps(r.json())          # model surfaced in the desk view
        assert (ws / ".claude_model").read_text() == "opus"   # the Claude worker reads THIS
        assert not (ws / ".hermes_model").exists()            # NOT the Hermes marker
    finally:
        srv._session_workspaces.pop(sid, None)
        srv._session_autocontinue.pop(sid, None)


@pytest.mark.asyncio
async def test_patch_claude_desk_rejects_non_claude_model(client: AsyncClient, tmp_path: Path):
    # A bogus/Ollama model id must be dropped (not written) so the SDK doesn't 400.
    sid = "20260101_120000_claude2"
    ws = tmp_path / "claude_ws2"
    ws.mkdir()
    (ws / ".hermes_profile").write_text("claude-sdk", encoding="utf-8")
    srv._session_workspaces[sid] = str(ws)
    try:
        r = await client.patch(f"/api/sessions/{sid}/desk-config",
                               json={"model": "qwen3.5:4b"})
        assert r.status_code == 200
        assert not (ws / ".claude_model").exists()     # invalid alias β†’ not pinned
    finally:
        srv._session_workspaces.pop(sid, None)
        srv._session_autocontinue.pop(sid, None)


@pytest.mark.asyncio
async def test_new_claude_desk_worker_env_is_oauth_only(tmp_home: Path, tmp_path: Path, monkeypatch):
    """End-to-end: a new Claude SDK desk's worker must be spawned with NO
    ANTHROPIC_API_KEY (or other outranking cred) in its env, so a stale key in the
    server's environment can't shadow the `claude /login` subscription. Unrelated
    inherited vars must still pass through."""
    # Creds that would shadow OAuth if they leaked into the worker.
    monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-stale-DO-NOT-LEAK")
    monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "gw-DO-NOT-LEAK")
    monkeypatch.setenv("CLAUDE_CODE_USE_BEDROCK", "1")
    # A normal inherited var that MUST survive (proves the env wasn't wiped wholesale).
    monkeypatch.setenv("AGENT_GUI_OAUTH_TEST_SENTINEL", "keep-me")

    # Claude desks are gated behind --experimental.
    ws_root = tmp_path / "ws_claude_oauth"
    ws_root.mkdir()
    app = create_app(hermes_home=str(tmp_home), workspace_root=str(ws_root), experimental=True)

    captured: dict = {}

    def _capture(*args, **kwargs):
        captured["args"] = args
        captured["env"] = kwargs.get("env", {})
        return _make_persistent_proc()

    sid = None
    try:
        with mock.patch("asyncio.create_subprocess_exec", side_effect=_capture):
            async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as cl:
                r = await cl.post("/api/sessions/new",
                                  json={"content": "hello", "agent": "claude-sdk", "model": "opus"})
        assert r.status_code == 200, r.text
        sid = r.json()["session_id"]

        env = captured["env"]
        # The Claude worker was spawned (not the Hermes worker)...
        assert captured["args"][1].endswith("claude_worker.py")
        assert env.get("HERMES_GUI_AGENT_KIND") == "claude"   # the Claude branch ran
        # ...with NONE of the outranking credentials.
        for k in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "CLAUDE_CODE_USE_BEDROCK"):
            assert k not in env, f"{k} leaked into the Claude worker env"
        # ...but unrelated inherited env still passed through.
        assert env.get("AGENT_GUI_OAUTH_TEST_SENTINEL") == "keep-me"
    finally:
        if sid is not None:
            for m in (srv._persistent_procs, srv._running_procs, srv._live_queues,
                      srv._turn_done_events, srv._session_workspaces,
                      srv._session_docker_vols, srv._session_worker_opts):
                m.pop(sid, None)