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

Serves a browser-based chat interface at http://localhost:7080 by default.

Usage
-----
    # Preferred: via the main agent entry point
    python core/agent/main.py --web [--port 7080] [--host 127.0.0.1]

    # Or run the web server directly
    python core/web/server.py [--port 7080] [--host 127.0.0.1]

Dependencies (install via the setup wizard or manually)
---------------------------------------------------------
    pip install "fastapi[standard]" uvicorn
"""


import argparse
import asyncio
import importlib.util
import io
import json
import os
import subprocess
import time
import re
import queue as stdlib_queue
import sys
import threading
from pathlib import Path
from typing import Any

REPO_ROOT = Path(__file__).parent.parent.parent.resolve()
STATIC_DIR = Path(__file__).parent / "static"
SHELL_STATUS_FILE = Path("/tmp/neuroclaw_claw_shell_status.json")
AGENT_SHELL_STATUS_FILE = Path("/tmp/neuroclaw_agent_shell_status.json")

DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = 7080
ATTACHMENT_MAX_FILE_BYTES = 15 * 1024 * 1024
ATTACHMENT_MAX_EMBED_CHARS = 12000
SUPPORTED_ATTACHMENT_EXTENSIONS = {
    ".txt", ".md", ".markdown", ".json", ".yaml", ".yml", ".csv", ".tsv",
    ".py", ".js", ".ts", ".tsx", ".jsx", ".sh", ".bash", ".zsh", ".sql",
    ".html", ".css", ".xml", ".log", ".rst", ".ini", ".toml", ".cfg",
    ".pdf", ".docx", ".xlsx", ".pptx",
}


def _fallback_title_from_user_text(text: str) -> str:
    """Create a short deterministic title when LLM title generation fails."""
    cleaned = re.sub(r"\s+", " ", str(text or "")).strip()
    if not cleaned:
        return "New Chat"
    words = cleaned.split(" ")[:8]
    title = " ".join(words)
    return title[:64] if title else "New Chat"


def _sanitize_title(raw: str, user_text: str) -> str:
    """Normalize model output into a single short title line."""
    t = str(raw or "").strip()
    if not t:
        return _fallback_title_from_user_text(user_text)

    t = t.splitlines()[0].strip()
    t = t.strip("\"'` ")
    t = re.sub(r"^[\-\*\d\.)\s]+", "", t)
    t = re.sub(r"\s+", " ", t).strip(" .:-")
    if not t:
        return _fallback_title_from_user_text(user_text)
    return t[:64]


def _safe_skill_summary_fallback(skill_name: str, description: str) -> dict[str, str]:
    """Return conservative bilingual fallback summary when LLM summarization fails."""
    base_en = (description or "").strip()
    if not base_en:
        base_en = f"{skill_name} provides a specialized workflow for task execution in NeuroClaw."
    base_en = re.sub(r"\s+", " ", base_en).strip()
    if len(base_en) > 220:
        base_en = base_en[:217].rstrip() + "..."
    base_zh = f"θ―₯ζŠ€θƒ½ε›΄η»•γ€Œ{skill_name}γ€ζδΎ›δΈ“η”¨ζ΅η¨‹ζ”―ζŒοΌŒε―η”¨δΊŽη›Έε…³δ»»εŠ‘ηš„ζ‰§θ‘ŒδΈŽη»„η»‡γ€‚"
    return {"en": base_en, "zh": base_zh}


def _parse_bilingual_summary_json(raw: str) -> dict[str, str] | None:
    """Parse model output into {'en':..., 'zh':...} with defensive handling."""
    text = str(raw or "").strip()
    if not text:
        return None

    try:
        obj = json.loads(text)
        en = str(obj.get("en", "")).strip()
        zh = str(obj.get("zh", "")).strip()
        if en and zh:
            return {"en": en, "zh": zh}
    except Exception:
        pass

    match = re.search(r"\{[\s\S]*\}", text)
    if match:
        try:
            obj = json.loads(match.group(0))
            en = str(obj.get("en", "")).strip()
            zh = str(obj.get("zh", "")).strip()
            if en and zh:
                return {"en": en, "zh": zh}
        except Exception:
            return None
    return None


def _strip_frontmatter(text: str) -> str:
    """Remove optional YAML front-matter from SKILL.md text."""
    return re.sub(r"^---\s*\n.*?\n---\s*\n", "", text, flags=re.DOTALL)


def _read_shell_status() -> dict[str, Any] | None:
    """Read transient shell status from agent-shell first, then tmux claw-shell."""
    if AGENT_SHELL_STATUS_FILE.exists():
        try:
            data = json.loads(AGENT_SHELL_STATUS_FILE.read_text(encoding="utf-8"))
            command = str(data.get("command", "")).strip()
            started_at = data.get("started_at")
            pid = int(data.get("pid", 0))
            if command and isinstance(started_at, (int, float)) and pid > 0:
                alive = False
                try:
                    os.kill(pid, 0)
                    alive = True
                except Exception:
                    alive = False

                if alive:
                    return {
                        "active": True,
                        "source": "agent_shell",
                        "command": command,
                        "current_command": "agent_shell",
                        "started_at": int(started_at),
                        "elapsed_ms": max(0, int((time.time() * 1000) - int(started_at))),
                    }
        except Exception:
            pass
        try:
            AGENT_SHELL_STATUS_FILE.unlink()
        except Exception:
            pass

    if not SHELL_STATUS_FILE.exists():
        return None

    try:
        data = json.loads(SHELL_STATUS_FILE.read_text(encoding="utf-8"))
    except Exception:
        try:
            SHELL_STATUS_FILE.unlink()
        except Exception:
            pass
        return None

    command = str(data.get("command", "")).strip()
    started_at = data.get("started_at")
    if not command or not isinstance(started_at, (int, float)):
        try:
            SHELL_STATUS_FILE.unlink()
        except Exception:
            pass
        return None

    try:
        proc = subprocess.run(
            ["tmux", "display-message", "-p", "-t", "claw", "#{pane_current_command}"],
            capture_output=True,
            text=True,
            check=False,
            timeout=2,
        )
        current = (proc.stdout or "").strip().lower()
    except Exception:
        current = ""

    shell_names = {"bash", "zsh", "fish", "sh", "dash", "tmux"}
    active = bool(current) and current not in shell_names
    elapsed_ms = max(0, int((time.time() * 1000) - int(started_at)))

    if not active:
        try:
            SHELL_STATUS_FILE.unlink()
        except Exception:
            pass
        return None

    return {
        "active": True,
        "command": command,
        "current_command": current,
        "started_at": int(started_at),
        "elapsed_ms": elapsed_ms,
    }


def _skill_md_excerpt(skill_md: Path, max_chars: int = 1800) -> str:
    """Return a compact excerpt from SKILL.md for prompt context injection."""
    try:
        raw = skill_md.read_text(encoding="utf-8")
    except Exception:
        return ""
    body = _strip_frontmatter(raw)
    body = re.sub(r"```[\s\S]*?```", "", body)
    body = re.sub(r"\n{3,}", "\n\n", body).strip()
    if len(body) > max_chars:
        body = body[:max_chars].rstrip() + "\n..."
    return body


def _selected_skills_context(selected_names: list[str], skills: list[dict[str, Any]]) -> str:
    """Build selected-skill references from discovered SKILL.md files."""
    if not selected_names:
        return ""

    by_name: dict[str, dict[str, Any]] = {}
    for s in skills:
        n = str(s.get("name", "")).strip().lower()
        if n:
            by_name[n] = s

    chunks: list[str] = []
    for raw_name in selected_names:
        key = raw_name.strip().lower()
        if not key:
            continue
        s = by_name.get(key)
        if not s:
            continue
        excerpt = _skill_md_excerpt(Path(s.get("skill_md")))
        if not excerpt:
            continue
        chunks.append(
            f"[Skill: {s.get('name', raw_name)}]\n"
            f"Description: {s.get('description', '')}\n"
            f"SKILL.md excerpt:\n{excerpt}"
        )

    return "\n\n".join(chunks)


def _attachment_extension(filename: str) -> str:
    return Path(str(filename or "")).suffix.lower().strip()


def _truncate_text(text: str, max_chars: int = ATTACHMENT_MAX_EMBED_CHARS) -> tuple[str, bool]:
    raw = str(text or "")
    if len(raw) <= max_chars:
        return raw, False
    return raw[:max_chars].rstrip() + "\n[Content truncated]", True


def _decode_text_attachment(raw: bytes) -> str:
    for encoding in ("utf-8", "utf-8-sig", "gb18030", "latin-1"):
        try:
            return raw.decode(encoding)
        except Exception:
            continue
    return raw.decode("utf-8", errors="replace")


def _extract_pdf_text(raw: bytes) -> str:
    from pypdf import PdfReader  # type: ignore

    reader = PdfReader(io.BytesIO(raw))
    chunks: list[str] = []
    for page in reader.pages[:120]:
        txt = (page.extract_text() or "").strip()
        if txt:
            chunks.append(txt)
    return "\n\n".join(chunks)


def _extract_docx_text(raw: bytes) -> str:
    from docx import Document  # type: ignore

    doc = Document(io.BytesIO(raw))
    chunks: list[str] = []

    for para in doc.paragraphs:
        txt = (para.text or "").strip()
        if txt:
            chunks.append(txt)

    for table in doc.tables:
        for row in table.rows:
            vals = [str(cell.text or "").strip() for cell in row.cells]
            vals = [v for v in vals if v]
            if vals:
                chunks.append(" | ".join(vals))

    return "\n".join(chunks)


def _extract_xlsx_text(raw: bytes) -> str:
    from openpyxl import load_workbook  # type: ignore

    wb = load_workbook(io.BytesIO(raw), read_only=True, data_only=True)
    lines: list[str] = []
    for ws in wb.worksheets[:12]:
        lines.append(f"[Sheet] {ws.title}")
        row_count = 0
        for row in ws.iter_rows(max_row=200, max_col=30, values_only=True):
            cells = ["" if v is None else str(v).strip() for v in row]
            if not any(cells):
                continue
            lines.append("\t".join(cells))
            row_count += 1
            if row_count >= 200:
                lines.append("[Rows truncated]")
                break
        lines.append("")
    return "\n".join(lines).strip()


def _extract_pptx_text(raw: bytes) -> str:
    from pptx import Presentation  # type: ignore

    prs = Presentation(io.BytesIO(raw))
    lines: list[str] = []
    for idx, slide in enumerate(prs.slides[:80], start=1):
        lines.append(f"[Slide {idx}]")
        for shape in slide.shapes:
            txt = ""
            try:
                txt = (shape.text or "").strip()  # type: ignore[attr-defined]
            except Exception:
                txt = ""
            if txt:
                lines.append(txt)
        lines.append("")
    return "\n".join(lines).strip()


def _parse_attachment_content(filename: str, content_type: str, raw: bytes) -> dict[str, Any]:
    ext = _attachment_extension(filename)
    if ext not in SUPPORTED_ATTACHMENT_EXTENSIONS:
        return {
            "ok": False,
            "error": (
                f"Unsupported extension '{ext or 'unknown'}'. "
                "Supported: " + ", ".join(sorted(SUPPORTED_ATTACHMENT_EXTENSIONS))
            ),
        }

    if ext in {
        ".txt", ".md", ".markdown", ".json", ".yaml", ".yml", ".csv", ".tsv",
        ".py", ".js", ".ts", ".tsx", ".jsx", ".sh", ".bash", ".zsh", ".sql",
        ".html", ".css", ".xml", ".log", ".rst", ".ini", ".toml", ".cfg",
    }:
        text = _decode_text_attachment(raw)
    elif ext == ".pdf":
        try:
            text = _extract_pdf_text(raw)
        except ImportError:
            return {"ok": False, "error": "PDF parser missing. Install: pypdf"}
    elif ext == ".docx":
        try:
            text = _extract_docx_text(raw)
        except ImportError:
            return {"ok": False, "error": "DOCX parser missing. Install: python-docx"}
    elif ext == ".xlsx":
        try:
            text = _extract_xlsx_text(raw)
        except ImportError:
            return {"ok": False, "error": "XLSX parser missing. Install: openpyxl"}
    elif ext == ".pptx":
        try:
            text = _extract_pptx_text(raw)
        except ImportError:
            return {"ok": False, "error": "PPTX parser missing. Install: python-pptx"}
    else:
        return {"ok": False, "error": "Unsupported file type"}

    cleaned = str(text or "").strip()
    if not cleaned:
        return {"ok": False, "error": "No readable text found in file"}

    truncated_text, truncated = _truncate_text(cleaned)
    return {
        "ok": True,
        "text": truncated_text,
        "truncated": truncated,
        "ext": ext,
        "content_type": content_type,
    }


def _normalize_skill_token(text: str) -> str:
    """Normalize a skill token for robust mention matching."""
    t = str(text or "").lower()
    t = re.sub(r"[^a-z0-9]+", "", t)
    return t


def _infer_skills_from_user_text(user_text: str, skills: list[dict[str, Any]]) -> list[str]:
    """
    Infer referenced skills from free-form user text.

    Matches both skill name and skill directory name in normalized form.
    """
    norm_msg = _normalize_skill_token(user_text)
    if not norm_msg:
        return []

    inferred: list[str] = []
    for s in skills:
        skill_name = str(s.get("name", "")).strip()
        if not skill_name:
            continue
        dir_name = ""
        try:
            dir_name = Path(s.get("path")).name
        except Exception:
            dir_name = ""

        candidates = [skill_name, dir_name]
        for c in candidates:
            token = _normalize_skill_token(c)
            if token and token in norm_msg:
                inferred.append(skill_name)
                break

    # Preserve order and remove duplicates
    seen: set[str] = set()
    out: list[str] = []
    for name in inferred:
        key = name.lower()
        if key in seen:
            continue
        seen.add(key)
        out.append(name)
    return out[:5]


# ── Module import helpers ──────────────────────────────────────────────────────

def _import_from_path(module_name: str, path: Path) -> Any:
    """Import a Python module from an absolute file path (handles hyphenated dirs)."""
    spec = importlib.util.spec_from_file_location(module_name, path)
    if spec is None or spec.loader is None:
        raise ImportError(f"Cannot load module from {path}")
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)  # type: ignore[union-attr]
    return mod


# ── Dependency check ───────────────────────────────────────────────────────────

def _require_webdeps() -> None:
    """Raise a descriptive RuntimeError if fastapi/uvicorn are not installed."""
    missing = []
    for pkg in ("fastapi", "uvicorn"):
        try:
            __import__(pkg)
        except ImportError:
            missing.append(pkg)
    if missing:
        raise RuntimeError(
            f"Web UI dependencies not installed: {', '.join(missing)}\n"
            "Install with:  pip install 'fastapi[standard]' uvicorn\n"
            "Or re-run the installer:  python installer/setup.py"
        )


# ── Streaming helpers ──────────────────────────────────────────────────────────

async def _stream_openai(
    websocket: Any, llm_client: Any, model: str, history: list[dict]
) -> str:
    """
    Stream an OpenAI response chunk-by-chunk.

    Uses a producer thread + asyncio queue so the blocking OpenAI iterator
    does not stall the event loop while still delivering incremental updates
    to the browser.
    """
    q: stdlib_queue.Queue[tuple[str, str | None]] = stdlib_queue.Queue()

    def _produce() -> None:
        try:
            stream = llm_client.chat.completions.create(
                model=model, messages=history, stream=True
            )
            for chunk in stream:
                content = ""
                if chunk.choices and chunk.choices[0].delta:
                    content = chunk.choices[0].delta.content or ""
                if content:
                    q.put(("chunk", content))
        except Exception as exc:
            q.put(("error", str(exc)))
        finally:
            q.put(("done", None))

    threading.Thread(target=_produce, daemon=True).start()

    full = ""
    while True:
        try:
            kind, data = q.get_nowait()
        except stdlib_queue.Empty:
            await asyncio.sleep(0.01)
            continue

        if kind == "chunk":
            full += data  # type: ignore[operator]
            await websocket.send_text(json.dumps({"type": "chunk", "content": data}))
        elif kind == "error":
            raise RuntimeError(data)
        else:  # "done"
            break

    return full


async def _respond(websocket: Any, session: Any) -> str:
    """
    Generate a reply for the latest message in session.history.

    Streams chunks to the browser for OpenAI; falls back to asyncio.to_thread
    for other backends (Anthropic, local).  Always sends a final
    ``{"type": "done", "content": "…"}`` frame.
    """
    provider = session.env.get("llm_backend", {}).get("provider", "openai")
    model = session.env.get("llm_backend", {}).get("model", "gpt-4o")

    if provider == "openai" and session._llm is not None:
        full = await _stream_openai(websocket, session._llm, model, session.history)
    else:
        # Non-streaming fallback: run blocking _chat() in a thread pool
        full = await asyncio.to_thread(session._chat)

    await websocket.send_text(json.dumps({"type": "done", "content": full}))
    return full


# ── FastAPI application factory ────────────────────────────────────────────────

def create_app() -> Any:
    """Build and return the FastAPI application object."""
    _require_webdeps()

    from fastapi import FastAPI, File, UploadFile, WebSocket, WebSocketDisconnect  # type: ignore
    from fastapi.responses import FileResponse, HTMLResponse, JSONResponse  # type: ignore
    from fastapi.staticfiles import StaticFiles  # type: ignore

    # Ensure repo root is on sys.path so `from core.agent.main import …` resolves
    if str(REPO_ROOT) not in sys.path:
        sys.path.insert(0, str(REPO_ROOT))

    from core.agent.main import (  # type: ignore[import]
        AgentSession,
        build_llm_client,
        load_environment,
        save_environment,
    )

    # SkillLoader lives in core/skill_loader/, so we use importlib for dynamic loading.
    _loader_mod = _import_from_path(
        "neuroclaw_skill_loader",
        REPO_ROOT / "core" / "skill_loader" / "loader.py",
    )
    SkillLoader = _loader_mod.SkillLoader

    app = FastAPI(title="NeuroClaw Web UI", docs_url=None, redoc_url=None)

    # ── Static files ────────────────────────────────────────────────────────────
    if STATIC_DIR.exists():
        app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
    materials_dir = REPO_ROOT / "materials"
    if materials_dir.exists():
        app.mount("/materials", StaticFiles(directory=str(materials_dir)), name="materials")

    # ── HTTP endpoints ──────────────────────────────────────────────────────────

    @app.get("/")
    async def root() -> Any:
        index = STATIC_DIR / "index.html"
        if index.exists():
            return FileResponse(str(index))
        return HTMLResponse(
            "<h1>NeuroClaw Web UI</h1>"
            f"<p>Static files not found at <code>{STATIC_DIR}</code>.</p>",
            status_code=500,
        )

    @app.get("/api/health")
    async def health() -> dict:
        return {"status": "ok"}

    @app.get("/api/shell/status")
    async def shell_status() -> dict:
        status = _read_shell_status()
        if not status:
            return {"active": False}
        return status

    @app.get("/api/skills")
    async def list_skills() -> dict:
        loader = SkillLoader(REPO_ROOT / "skills")
        skills = loader.load_all()
        return {
            "skills": [
                {
                    "name": s["name"],
                    "description": s.get("description", ""),
                    "summary_en": s.get("summary_en", ""),
                    "summary_zh": s.get("summary_zh", ""),
                    "layer": s.get("layer", ""),
                    "skill_type": s.get("skill_type", ""),
                    "dependencies": s.get("dependencies", []),
                    "complementary_skills": s.get("complementary_skills", []),
                }
                for s in skills
            ]
        }

    @app.post("/api/attachments/parse")
    async def parse_attachments(files: list[UploadFile] = File(...)) -> Any:
        if not files:
            return JSONResponse({"type": "error", "message": "No files uploaded"}, status_code=400)

        parsed_files: list[dict[str, Any]] = []
        for upload in files[:24]:
            name = str(upload.filename or "untitled")
            content_type = str(upload.content_type or "unknown")
            raw = await upload.read()
            size = len(raw)

            item: dict[str, Any] = {
                "name": name,
                "size": size,
                "content_type": content_type,
            }

            if size > ATTACHMENT_MAX_FILE_BYTES:
                item.update(
                    {
                        "ok": False,
                        "error": (
                            f"File too large ({size} bytes). "
                            f"Limit is {ATTACHMENT_MAX_FILE_BYTES} bytes."
                        ),
                    }
                )
                parsed_files.append(item)
                continue

            try:
                item.update(_parse_attachment_content(name, content_type, raw))
            except Exception as exc:
                item.update({"ok": False, "error": f"Parse failed: {exc}"})
            parsed_files.append(item)

        return {
            "type": "done",
            "files": parsed_files,
            "max_file_bytes": ATTACHMENT_MAX_FILE_BYTES,
            "supported_extensions": sorted(SUPPORTED_ATTACHMENT_EXTENSIONS),
        }

    @app.get("/api/env")
    async def get_env() -> dict:
        """Return non-sensitive parts of the runtime environment config."""
        env = load_environment()
        llm = env.get("llm_backend", {})
        provider = llm.get("provider", "unknown")
        api_key_env = llm.get("api_key_env", "")
        api_key_present = bool(api_key_env and __import__('os').environ.get(api_key_env))
        return {
            "provider": provider,
            "model": llm.get("model", "unknown"),
            "available_models": llm.get("available_models", []),
            "cuda_device": env.get("cuda", {}).get("device", "cpu"),
            "setup_type": env.get("setup_type", "unknown"),
            "conda_env": env.get("conda_env"),
            "api_key_present": api_key_present,
        }

    @app.post("/api/env/model")
    async def set_model(payload: dict) -> Any:
        """Switch current provider/model to one of the configured options."""
        provider = str(payload.get("provider", "")).strip()
        model = str(payload.get("model", "")).strip()
        if not provider or not model:
            return JSONResponse(
                {"type": "error", "message": "provider and model are required"},
                status_code=400,
            )

        env = load_environment()
        llm = env.setdefault("llm_backend", {})
        available_models = llm.get("available_models", [])
        if not any(
            isinstance(item, dict)
            and str(item.get("provider", "")).strip() == provider
            and str(item.get("model", item.get("id", item.get("name", "")))).strip() == model
            for item in available_models
        ):
            return JSONResponse(
                {"type": "error", "message": "Requested provider/model is not configured"},
                status_code=400,
            )

        llm["provider"] = provider
        llm["model"] = model
        save_environment(env)
        return {
            "type": "done",
            "provider": provider,
            "model": model,
            "available_models": llm.get("available_models", []),
        }

    @app.post("/api/chat")
    async def chat_http(payload: dict) -> Any:
        """HTTP fallback for chat when WebSocket is unavailable."""
        user_text = str(payload.get("message", "")).strip()
        raw_history = payload.get("history", [])
        raw_selected_skills = payload.get("selected_skills", [])
        if not user_text:
            return JSONResponse({"type": "error", "message": "Empty message"}, status_code=400)

        session = AgentSession()

        try:
            loader = SkillLoader(REPO_ROOT / "skills")
            skills = loader.load_all()
        except Exception:
            skills = []

        selected_skills: list[str] = []
        if isinstance(raw_selected_skills, list):
            selected_skills = [
                str(x).strip() for x in raw_selected_skills if str(x).strip()
            ]
        if not selected_skills:
            selected_skills = _infer_skills_from_user_text(user_text, skills)

        env_file = REPO_ROOT / "neuroclaw_environment.json"
        if not env_file.exists():
            return JSONResponse({
                "type": "error",
                "message": "neuroclaw_environment.json not found. Run python installer/setup.py to configure NeuroClaw.",
            }, status_code=400)

        try:
            session.set_llm_client(build_llm_client(session.env))
        except Exception as exc:
            return JSONResponse({"type": "error", "message": f"LLM backend error: {exc}"}, status_code=500)

        soul_path = REPO_ROOT / "SOUL.md"
        soul = soul_path.read_text(encoding="utf-8") if soul_path.exists() else ""
        skill_names = ", ".join(s["name"] for s in skills)
        session.history = [{"role": "system", "content": f"{soul}\n\nLoaded skills: {skill_names}"}]

        if isinstance(raw_history, list):
            for msg in raw_history:
                if not isinstance(msg, dict):
                    continue
                role = str(msg.get("role", "")).strip().lower()
                content = str(msg.get("content", "")).strip()
                if role not in {"user", "assistant"} or not content:
                    continue
                session.history.append({"role": role, "content": content})

        selected_ctx = _selected_skills_context(selected_skills, skills)
        user_payload = user_text
        if selected_ctx:
            user_payload = (
                f"{user_text}\n\n"
                "[Selected skill references from local SKILL.md files]\n"
                f"{selected_ctx}"
            )

        session.history.append({"role": "user", "content": user_payload})

        reply = await asyncio.to_thread(session._chat)
        llm_cfg = session.env.get("llm_backend", {})
        provider_used = str(llm_cfg.get("provider", "unknown"))
        model_used = str(llm_cfg.get("model", "unknown"))
        return {
            "type": "done",
            "content": reply,
            "provider_used": provider_used,
            "model_used": model_used,
        }

    @app.post("/api/chat/title")
    async def chat_title(payload: dict) -> Any:
        """Generate a concise chat title using the configured default model."""
        user_text = str(payload.get("user", "")).strip()
        assistant_text = str(payload.get("assistant", "")).strip()
        if not user_text and not assistant_text:
            return JSONResponse({"type": "error", "message": "Missing conversation content"}, status_code=400)

        env_file = REPO_ROOT / "neuroclaw_environment.json"
        if not env_file.exists():
            return JSONResponse({"type": "error", "message": "Environment not configured"}, status_code=400)

        session = AgentSession()
        try:
            session.set_llm_client(build_llm_client(session.env))
        except Exception as exc:
            return JSONResponse({"type": "error", "message": f"LLM backend error: {exc}"}, status_code=500)

        title_system = (
            "You are a conversation title generator. "
            "Return exactly one short title in plain text, 3-10 words, no quotes, no markdown."
        )
        convo = f"User message:\n{user_text}\n\nAssistant reply:\n{assistant_text}"
        session.history = [
            {"role": "system", "content": title_system},
            {"role": "user", "content": convo},
        ]

        try:
            raw_title = await asyncio.to_thread(session._chat)
            title = _sanitize_title(raw_title, user_text)
        except Exception:
            title = _fallback_title_from_user_text(user_text)

        return {"type": "done", "title": title}

    @app.post("/api/skills/summary")
    async def skill_summary(payload: dict) -> Any:
        """Summarize one SKILL.md into bilingual 1-5 sentence summaries."""
        skill_name = str(payload.get("name", "")).strip()
        if not skill_name:
            return JSONResponse({"type": "error", "message": "Missing skill name"}, status_code=400)

        try:
            loader = SkillLoader(REPO_ROOT / "skills")
            skills = loader.load_all()
        except Exception:
            skills = []

        target = None
        for s in skills:
            if str(s.get("name", "")).strip().lower() == skill_name.lower():
                target = s
                break
        if target is None:
            return JSONResponse({"type": "error", "message": f"Skill not found: {skill_name}"}, status_code=404)

        skill_md = Path(target.get("skill_md"))
        if not skill_md.exists():
            fb = _safe_skill_summary_fallback(skill_name, str(target.get("description", "")))
            return {"type": "done", "summary_en": fb["en"], "summary_zh": fb["zh"]}

        md_text = skill_md.read_text(encoding="utf-8")
        excerpt = md_text[:7000]

        session = AgentSession()
        try:
            session.set_llm_client(build_llm_client(session.env))
        except Exception:
            fb = _safe_skill_summary_fallback(skill_name, str(target.get("description", "")))
            return {"type": "done", "summary_en": fb["en"], "summary_zh": fb["zh"]}

        prompt = (
            "Summarize the following SKILL.md into concise bilingual summaries.\n"
            "Requirements:\n"
            "1) Return valid JSON only, no markdown, no extra text.\n"
            "2) JSON keys: en, zh.\n"
            "3) en: 1-5 complete English sentences.\n"
            "4) zh: 1-5 complete Chinese sentences.\n"
            "5) Focus on what this skill can do and when to use it.\n\n"
            f"Skill Name: {skill_name}\n"
            f"SKILL.md Content:\n{excerpt}"
        )
        session.history = [
            {
                "role": "system",
                "content": "You are a precise technical summarizer. Output strict JSON only.",
            },
            {"role": "user", "content": prompt},
        ]

        try:
            raw = await asyncio.to_thread(session._chat)
            parsed = _parse_bilingual_summary_json(raw)
            if not parsed:
                raise ValueError("Invalid summary JSON")
            return {
                "type": "done",
                "summary_en": parsed["en"],
                "summary_zh": parsed["zh"],
            }
        except Exception:
            fb = _safe_skill_summary_fallback(skill_name, str(target.get("description", "")))
            return {"type": "done", "summary_en": fb["en"], "summary_zh": fb["zh"]}

    # ── Checkpoint API endpoints ─────────────────────────────────────────────

    @app.get("/api/checkpoints")
    async def list_checkpoints() -> Any:
        from core.checkpoint.manager import ShadowCheckpointManager
        mgr = ShadowCheckpointManager(repo_root=REPO_ROOT)
        cps = mgr.list_checkpoints(REPO_ROOT)
        return {"checkpoints": cps}

    @app.get("/api/checkpoints/{checkpoint_id}/diff")
    async def checkpoint_diff(checkpoint_id: str) -> Any:
        from core.checkpoint.manager import ShadowCheckpointManager
        mgr = ShadowCheckpointManager(repo_root=REPO_ROOT)
        try:
            diff = mgr.diff_checkpoint(REPO_ROOT, checkpoint_id)
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=400)
        return diff

    @app.post("/api/checkpoints/{checkpoint_id}/restore")
    async def restore_checkpoint(checkpoint_id: str, payload: dict = {}) -> Any:
        from core.checkpoint.manager import ShadowCheckpointManager
        mgr = ShadowCheckpointManager(repo_root=REPO_ROOT)
        filepath = payload.get("filepath") if payload else None
        try:
            result = mgr.restore_checkpoint(REPO_ROOT, checkpoint_id, filepath=filepath)
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=400)
        return {"type": "done", **result}

    @app.get("/api/checkpoints/{checkpoint_id}/files")
    async def checkpoint_files(checkpoint_id: str) -> Any:
        from core.checkpoint.manager import ShadowCheckpointManager
        mgr = ShadowCheckpointManager(repo_root=REPO_ROOT)
        try:
            files = mgr.get_files_at_checkpoint(REPO_ROOT, checkpoint_id)
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=400)
        return {"files": files}

    # ── WebSocket chat endpoint ─────────────────────────────────────────────────

    @app.websocket("/ws/chat")
    async def chat_endpoint(websocket: WebSocket) -> None:
        try:
            await websocket.accept()
            print("[WS] Client connected", flush=True)

            # Create a per-connection agent session
            session = AgentSession()

            # Load skills
            try:
                loader = SkillLoader(REPO_ROOT / "skills")
                skills = loader.load_all()
            except Exception:
                skills = []

            # Send init metadata
            llm_cfg = session.env.get("llm_backend", {})
            await websocket.send_text(json.dumps({
                "type": "init",
                "skills": [
                    {
                        "name": s["name"],
                        "description": s.get("description", ""),
                        "summary_en": s.get("summary_en", ""),
                        "summary_zh": s.get("summary_zh", ""),
                        "layer": s.get("layer", ""),
                        "skill_type": s.get("skill_type", ""),
                        "dependencies": s.get("dependencies", []),
                        "complementary_skills": s.get("complementary_skills", []),
                    }
                    for s in skills
                ],
                "provider": llm_cfg.get("provider", "unconfigured"),
                "model": llm_cfg.get("model", "unconfigured"),
            }))

            env_file = REPO_ROOT / "neuroclaw_environment.json"
            if not env_file.exists():
                await websocket.send_text(json.dumps({
                    "type": "error",
                    "message": (
                        "neuroclaw_environment.json not found. "
                        "Run python installer/setup.py to configure NeuroClaw."
                    ),
                }))
                await websocket.close()
                return

            # Initialise LLM client
            try:
                session.set_llm_client(build_llm_client(session.env))
            except Exception as exc:
                await websocket.send_text(json.dumps({
                    "type": "error",
                    "message": f"LLM backend error: {exc}",
                }))

            # Build system prompt
            soul_path = REPO_ROOT / "SOUL.md"
            soul = soul_path.read_text(encoding="utf-8") if soul_path.exists() else ""
            skill_names = ", ".join(s["name"] for s in skills)
            session.history = [
                {"role": "system", "content": f"{soul}\n\nLoaded skills: {skill_names}"}
            ]

            # Main chat loop
            try:
                while True:
                    raw = await websocket.receive_text()
                    msg = json.loads(raw)
                    user_text = msg.get("message", "").strip()
                    raw_selected = msg.get("selected_skills", [])
                    if not user_text:
                        continue

                    selected = []
                    if isinstance(raw_selected, list):
                        selected = [str(x).strip() for x in raw_selected if str(x).strip()]
                    if not selected:
                        selected = _infer_skills_from_user_text(user_text, skills)

                    selected_ctx = _selected_skills_context(selected, skills)
                    user_payload = user_text
                    if selected_ctx:
                        user_payload = (
                            f"{user_text}\n\n"
                            "[Selected skill references from local SKILL.md files]\n"
                            f"{selected_ctx}"
                        )

                    session.history.append({"role": "user", "content": user_payload})
                    try:
                        reply = await _respond(websocket, session)
                        session.history.append({"role": "assistant", "content": reply})
                    except Exception as exc:
                        err_msg = f"[Agent error: {exc}]"
                        await websocket.send_text(
                            json.dumps({"type": "error", "message": str(exc)})
                        )
                        session.history.append({"role": "assistant", "content": err_msg})

            except WebSocketDisconnect:
                pass

        except Exception as e:
            print(f"[WS] Error occurred: {type(e).__name__}: {e}", flush=True)
            import traceback
            traceback.print_exc()

    # ── Knowledge Graph Explorer ───────────────────────────────────────────

    _kg_state: dict[str, Any] = {"loaded": False, "loading": False, "error": None}
    _kg_lock = threading.Lock()

    KG_DATA_DIR = REPO_ROOT / "core" / "knowledge_graph" / "data"
    KG_PATH = KG_DATA_DIR / "knowledge_graph.json"
    KG_QUICK_DIR = KG_DATA_DIR / "quick"
    HYPOTHESIS_SOURCES = [
        "hypotheses_critic.json",
        "hypotheses_imaging_ukb.json",
        "hypotheses_imaging_adni.json",
        "hypotheses_imaging_hcp.json",
    ]
    RECIPES_PATH = KG_QUICK_DIR / "recipes_top10.json"

    DOMAIN_COLORS = {
        "biomarker": "#10b981",
        "imaging_feature": "#3b82f6",
        "cognitive_function": "#8b5cf6",
        "disease": "#ef4444",
        "gene": "#f59e0b",
        "neuroanatomy": "#06b6d4",
        "drug": "#ec4899",
        "neurotransmitter": "#f97316",
        "cell_type": "#14b8a6",
        "paradigm": "#a855f7",
        "connectivity": "#0ea5e9",
        "dataset_variable": "#84cc16",
        "claim": "#94a3b8",
    }
    RELATION_COLORS = {
        "is_a": "#94a3b8",
        "part_of": "#64748b",
        "causes": "#dc2626",
        "associated_with": "#3b82f6",
        "is_associated_with": "#3b82f6",
        "predisposes": "#f97316",
        "treats": "#10b981",
        "modulates": "#8b5cf6",
        "reduces": "#ef4444",
        "increases": "#16a34a",
        "correlates_with": "#0ea5e9",
        "is_biomarker_of": "#06b6d4",
        "is_risk_factor_for": "#f59e0b",
        "predicts": "#0891b2",
        "mediates": "#7c3aed",
        "inhibits": "#b91c1c",
        "distinguishes": "#c026d3",
        "projects_to": "#0369a1",
        "connects_to": "#0284c7",
        "activates": "#15803d",
        "coactivates": "#22c55e",
        "gene_associated_with_disease": "#eab308",
        "about": "#cbd5e1",
        "supported_by": "#94a3b8",
        "contradicts": "#dc2626",
    }
    DEFAULT_EDGE_COLOR = "#94a3b8"

    # ── Noise filter (query-time, does not mutate KG) ─────────────────────
    _NOISE_PREFIXES = (
        "impaired ", "increased ", "decreased ", "reduced ",
        "altered ", "elevated ", "abnormal ", "deficient ",
        "excessive ", "diminished ", "enhanced ", "disrupted ",
        "lower ", "higher ", "greater ", "lesser ",
    )
    _NOISE_SUFFIXES = (
        " findings", " levels", " changes", " symptoms",
        " deficits", " manifestations", " abnormalities",
        " dysfunctions", " status", " outcomes", " profile",
        " profiles", " patterns", " features",
    )
    NOISE_THRESHOLD = 0.3
    # Curated-vocab prefixes β€” trust these names even if they match noise patterns.
    # This prevents false positives on short MSH terms like "Brain", "Pons", "Sleep"
    # that trigger HypothesisEngine._is_noisy_entity's short-word regex.
    _CURATED_PREFIXES = (
        "MSH:", "NN:", "COGAT_TASK:", "COGAT_CONCEPT:", "COGAT_DISORDER:",
        "DISGENET:", "BM_REGION:", "BM_PARADIGM:", "BM_EXP:",
        "HGNC:", "NCBI_Gene:",
    )

    def _compute_noise_score(node_id: str, name: str, n_claims: int, n_hyps: int) -> float:
        """Combined heuristic score in [0, 1]. >= NOISE_THRESHOLD == noise."""
        if not name:
            return 1.0
        # Curated vocab β†’ trust (still apply prefix/suffix check but skip token check)
        is_curated = any(node_id.startswith(p) for p in _CURATED_PREFIXES)

        score = 0.0
        if not is_curated:
            try:
                from neurooracle.src.hypothesis_engine import HypothesisEngine
                if HypothesisEngine._is_noisy_entity(name):
                    score += 0.5
            except Exception:
                pass
        lname = name.lower()
        if any(lname.startswith(p) for p in _NOISE_PREFIXES):
            score += 0.3
        if any(lname.endswith(s) for s in _NOISE_SUFFIXES):
            score += 0.3
        if node_id.startswith("CLM_CONCEPT:") and n_claims < 3:
            score += 0.15
        if n_hyps == 0 and len(name) > 40:
            score += 0.05
        return min(score, 1.0)

    def _noise_reasons(node_id: str, name: str, n_claims: int, n_hyps: int) -> list[str]:
        """Human-readable reasons why a concept was flagged as noise."""
        reasons = []
        if not name:
            return ["empty name"]
        is_curated = any(node_id.startswith(p) for p in _CURATED_PREFIXES)
        if not is_curated:
            try:
                from neurooracle.src.hypothesis_engine import HypothesisEngine
                if HypothesisEngine._is_noisy_entity(name):
                    reasons.append("generic/nominalized token (risk/effect/findings/...)")
            except Exception:
                pass
        lname = name.lower()
        for p in _NOISE_PREFIXES:
            if lname.startswith(p):
                reasons.append(f"noise prefix: '{p.strip()}'")
                break
        for s in _NOISE_SUFFIXES:
            if lname.endswith(s):
                reasons.append(f"noise suffix: '{s.strip()}'")
                break
        if node_id.startswith("CLM_CONCEPT:") and n_claims < 3:
            reasons.append("auto-extracted (CLM_CONCEPT) with <3 claims")
        if n_hyps == 0 and len(name) > 40:
            reasons.append("no hypotheses + long name")
        return reasons

    def _external_links(external_ids: dict) -> list[dict]:
        links: list[dict] = []
        for key, value in (external_ids or {}).items():
            if not value:
                continue
            url = None
            label = f"{key} Β· {value}"
            k = key.lower()
            v = str(value)
            if k in ("mesh_ui", "msh", "mesh"):
                url = f"https://meshb.nlm.nih.gov/record/ui?ui={v}"
                label = f"MeSH Β· {v}"
            elif k in ("umls", "cui"):
                url = f"https://uts.nlm.nih.gov/uts/umls/concept/{v}"
                label = f"UMLS Β· {v}"
            elif k in ("disgenet_id", "disgenet"):
                url = f"https://www.disgenet.org/search?source=ALL&search={v}"
                label = f"DisGeNET Β· {v}"
            elif k in ("nn_id", "nn", "neuronames"):
                clean = v.replace("NN:", "") if v.startswith("NN:") else v
                url = f"https://braininfo.rprc.washington.edu/centraldirectory.aspx?ID={clean}"
                label = f"NeuroNames Β· {clean}"
            elif k == "hgnc":
                url = f"https://www.genenames.org/data/gene-symbol-report/#!/hgnc_id/{v}"
                label = f"HGNC Β· {v}"
            elif k in ("ncbi_gene", "ncbi", "ncbigene"):
                url = f"https://www.ncbi.nlm.nih.gov/gene/{v}"
                label = f"NCBI Gene Β· {v}"
            elif k == "cogat_id":
                url = f"https://www.cognitiveatlas.org/term/id/{v}"
                label = f"Cognitive Atlas Β· {v}"
            elif k == "doid":
                url = f"https://disease-ontology.org/?id={v}"
                label = f"DOID Β· {v}"
            links.append({"key": key, "value": v, "label": label, "url": url})
        return links

    def _pmid_url(pmid: str) -> str | None:
        return f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/" if pmid else None

    def _doi_url(doi: str) -> str | None:
        if not doi:
            return None
        return f"https://doi.org/{doi}" if not doi.startswith("http") else doi

    def _load_kg_blocking() -> dict:
        """Load KG + hypotheses + recipes; build reverse indexes. Called once."""
        from neurooracle.src.storage import load_graph
        from neurooracle.src.hypothesis_engine import Hypothesis

        t0 = time.time()
        print(f"[kg] loading knowledge graph from {KG_PATH} ...", flush=True)
        kg = load_graph(KG_PATH)

        # name_index: lower(name|alias) -> [node_id]
        name_index: dict[str, list[str]] = {}
        concept_to_claims: dict[str, list[str]] = {}
        claim_nodes: dict[str, dict] = {}
        for nid, node in kg._index.items():
            is_claim = "claim" in node.domain_tags
            if is_claim:
                meta = node.metadata or {}
                claim_nodes[nid] = meta
                subj = meta.get("subject_id", "")
                obj = meta.get("object_id", "")
                if subj:
                    concept_to_claims.setdefault(subj, []).append(nid)
                if obj and obj != subj:
                    concept_to_claims.setdefault(obj, []).append(nid)
                continue
            # Index non-claim concepts by name and aliases
            key = node.preferred_name.strip().lower()
            if key:
                name_index.setdefault(key, []).append(nid)
            for alias in node.aliases or []:
                ak = alias.strip().lower()
                if ak and ak != key:
                    name_index.setdefault(ak, []).append(nid)

        # Load hypotheses β€” critic first (priority), then imaging
        hypotheses_by_id: dict[str, Hypothesis] = {}
        for fname in HYPOTHESIS_SOURCES:
            fpath = KG_QUICK_DIR / fname
            if not fpath.exists():
                # fallback: try parent data/ dir (without quick/)
                fpath = KG_DATA_DIR / fname
            if not fpath.exists():
                print(f"[kg] skip missing hypothesis file: {fname}", flush=True)
                continue
            try:
                data = json.loads(fpath.read_text(encoding="utf-8"))
                for h_dict in data.get("hypotheses", []):
                    h = Hypothesis.from_dict(h_dict)
                    if h.id and h.id not in hypotheses_by_id:
                        # tag source file for provenance
                        h.metadata = dict(h.metadata or {})
                        h.metadata.setdefault("_source_file", fname)
                        hypotheses_by_id[h.id] = h
                print(f"[kg] loaded {len(data.get('hypotheses', []))} hypotheses from {fname}", flush=True)
            except Exception as exc:
                print(f"[kg] failed to load {fname}: {exc}", flush=True)

        # Reverse index: concept_id -> [hypothesis_id]
        concept_to_hyps: dict[str, set] = {}
        for hid, h in hypotheses_by_id.items():
            touched: set[str] = set()
            if h.source_id:
                touched.add(h.source_id)
            if h.target_id:
                touched.add(h.target_id)
            for link in h.path or []:
                if link.from_id:
                    touched.add(link.from_id)
                if link.to_id:
                    touched.add(link.to_id)
            for cid in touched:
                concept_to_hyps.setdefault(cid, set()).add(hid)

        # Recipes (optional)
        recipes_by_hyp: dict[str, dict] = {}
        if RECIPES_PATH.exists():
            try:
                rdata = json.loads(RECIPES_PATH.read_text(encoding="utf-8"))
                for r in rdata.get("recipes", []):
                    hid = r.get("hypothesis_id")
                    if hid:
                        recipes_by_hyp[hid] = r
                print(f"[kg] loaded {len(recipes_by_hyp)} recipes", flush=True)
            except Exception as exc:
                print(f"[kg] failed to load recipes: {exc}", flush=True)

        # Compute noise scores for non-claim concepts, then rebuild a clean name_index
        t_noise = time.time()
        noise_map: dict[str, float] = {}
        clean_name_index: dict[str, list[str]] = {}
        n_noisy = 0
        for nid, node in kg._index.items():
            if "claim" in (node.domain_tags or []):
                continue
            n_cl = len(concept_to_claims.get(nid, []))
            n_hy = len(concept_to_hyps.get(nid, set()))
            score = _compute_noise_score(nid, node.preferred_name or "", n_cl, n_hy)
            if score > 0:
                noise_map[nid] = score
            if score < NOISE_THRESHOLD:
                key = (node.preferred_name or "").strip().lower()
                if key:
                    clean_name_index.setdefault(key, []).append(nid)
                for alias in node.aliases or []:
                    ak = alias.strip().lower()
                    if ak and ak != key:
                        clean_name_index.setdefault(ak, []).append(nid)
            else:
                n_noisy += 1
        print(
            f"[kg] scored noise in {time.time() - t_noise:.2f}s: "
            f"{n_noisy} flagged (>= {NOISE_THRESHOLD})",
            flush=True,
        )

        stats = kg.stats()
        elapsed = time.time() - t0
        print(
            f"[kg] ready in {elapsed:.1f}s: {stats['n_concepts']} concepts, "
            f"{stats['n_edges']} edges, {len(claim_nodes)} claims, "
            f"{len(hypotheses_by_id)} hypotheses, {len(recipes_by_hyp)} recipes, "
            f"{n_noisy} noise-flagged",
            flush=True,
        )

        # ── Build trigram inverted index for fast substring search ──────
        t_tri = time.time()

        def _trigrams(s: str) -> set[str]:
            s = s.lower()
            if len(s) < 3:
                return {s} if s else set()
            return {s[i:i+3] for i in range(len(s) - 2)}

        def _build_trigram_index(idx: dict[str, list[str]]) -> dict[str, set[str]]:
            tri_idx: dict[str, set[str]] = {}
            for key in idx:
                for tri in _trigrams(key):
                    tri_idx.setdefault(tri, set()).add(key)
            return tri_idx

        trigram_index = _build_trigram_index(name_index)
        clean_trigram_index = _build_trigram_index(clean_name_index)
        print(f"[kg] built trigram indexes in {time.time() - t_tri:.2f}s", flush=True)

        # ── Pre-compute top-ranked concept lists (avoids 86k scan per request) ──
        t_top = time.time()

        def _build_top_list(idx: dict[str, list[str]], quality_strict: bool = False) -> list[dict]:
            seen: set[str] = set()
            candidates: list[dict] = []
            for nids in idx.values():
                for nid in nids:
                    if nid in seen:
                        continue
                    seen.add(nid)
                    node = kg._index.get(nid)
                    if node is None:
                        continue
                    n_cl = len(concept_to_claims.get(nid, []))
                    n_hy = len(concept_to_hyps.get(nid, set()))
                    if n_cl == 0 and n_hy == 0:
                        continue
                    if quality_strict and not (n_hy > 0 or n_cl >= 3):
                        continue
                    noise = noise_map.get(nid, 0.0)
                    candidates.append({
                        "id": nid,
                        "name": node.preferred_name,
                        "domain_tags": list(node.domain_tags or []),
                        "aliases": list(node.aliases or [])[:6],
                        "n_claims": n_cl,
                        "n_hypotheses": n_hy,
                        "noise_score": noise,
                        "is_noise": noise >= NOISE_THRESHOLD,
                    })
            candidates.sort(key=lambda r: (-(r["n_claims"] * 2 + r["n_hypotheses"]), r["noise_score"], len(r["name"])))
            return candidates

        top_all = _build_top_list(name_index)
        top_clean = _build_top_list(clean_name_index)
        print(f"[kg] pre-computed top lists in {time.time() - t_top:.2f}s ({len(top_clean)} clean, {len(top_all)} all)", flush=True)

        return {
            "loaded": True,
            "loading": False,
            "error": None,
            "kg": kg,
            "name_index": name_index,
            "clean_name_index": clean_name_index,
            "trigram_index": trigram_index,
            "clean_trigram_index": clean_trigram_index,
            "top_all": top_all,
            "top_clean": top_clean,
            "concept_to_claims": concept_to_claims,
            "claim_nodes": claim_nodes,
            "hypotheses_by_id": hypotheses_by_id,
            "concept_to_hyps": concept_to_hyps,
            "recipes_by_hyp": recipes_by_hyp,
            "noise_map": noise_map,
            "stats": {
                "n_concepts": stats["n_concepts"],
                "n_edges": stats["n_edges"],
                "n_claims": len(claim_nodes),
                "n_hypotheses": len(hypotheses_by_id),
                "n_recipes": len(recipes_by_hyp),
                "n_with_recipe": len(recipes_by_hyp),
                "n_noise_flagged": n_noisy,
                "domains": stats.get("domains", {}),
            },
        }

    async def _get_kg_state() -> dict:
        """Lazy-load the KG on first request; subsequent calls return cached state."""
        if _kg_state.get("loaded"):
            return _kg_state
        # serialize concurrent first-load attempts
        should_load = False
        with _kg_lock:
            if not _kg_state.get("loaded") and not _kg_state.get("loading"):
                _kg_state["loading"] = True
                should_load = True
        if should_load:
            try:
                new_state = await asyncio.to_thread(_load_kg_blocking)
                _kg_state.update(new_state)
            except Exception as exc:
                _kg_state["loading"] = False
                _kg_state["error"] = str(exc)
                raise
        else:
            # another request is loading β€” poll briefly
            for _ in range(600):  # up to ~60s
                if _kg_state.get("loaded") or _kg_state.get("error"):
                    break
                await asyncio.sleep(0.1)
        if _kg_state.get("error"):
            raise RuntimeError(_kg_state["error"])
        return _kg_state

    def _node_summary(state: dict, node_id: str) -> dict | None:
        kg = state["kg"]
        node = kg._index.get(node_id)
        if node is None:
            return None
        noise = state.get("noise_map", {}).get(node_id, 0.0)
        return {
            "id": node.id,
            "name": node.preferred_name,
            "domain_tags": list(node.domain_tags or []),
            "aliases": list(node.aliases or [])[:6],
            "n_claims": len(state["concept_to_claims"].get(node_id, [])),
            "n_hypotheses": len(state["concept_to_hyps"].get(node_id, set())),
            "noise_score": noise,
            "is_noise": noise >= NOISE_THRESHOLD,
        }

    def _serialize_claim(state: dict, claim_id: str) -> dict | None:
        kg = state["kg"]
        node = kg._index.get(claim_id)
        if node is None or "claim" not in node.domain_tags:
            return None
        meta = node.metadata or {}
        paper = meta.get("source_paper") or {}
        evidence = meta.get("evidence") or {}
        pmid = paper.get("pmid", "") or ""
        doi = paper.get("doi", "") or ""
        return {
            "claim_id": claim_id,
            "subject_id": meta.get("subject_id", ""),
            "subject_name": meta.get("subject_name", ""),
            "predicate": meta.get("predicate", ""),
            "object_id": meta.get("object_id", ""),
            "object_name": meta.get("object_name", ""),
            "confidence": float(meta.get("confidence", 0.0)),
            "negated": bool(meta.get("negated", False)),
            "raw_text": meta.get("raw_text", ""),
            "paper": {
                "pmid": pmid,
                "doi": doi,
                "title": paper.get("title", ""),
                "authors": paper.get("authors", ""),
                "year": paper.get("year"),
                "journal": paper.get("journal", ""),
                "pubmed_url": _pmid_url(pmid),
                "doi_url": _doi_url(doi),
            },
            "evidence": {
                "study_type": evidence.get("study_type", ""),
                "methodology": evidence.get("methodology", ""),
                "p_value": evidence.get("p_value"),
                "effect_size": evidence.get("effect_size"),
                "effect_metric": evidence.get("effect_metric", ""),
                "sample_size": evidence.get("sample_size"),
                "replicability": evidence.get("replicability", ""),
                "direction": evidence.get("direction", ""),
            },
        }

    def _serialize_hypothesis(state: dict, h, include_full_path: bool = True) -> dict:
        path_out: list[dict] = []
        pmids: set[str] = set()
        if include_full_path:
            for link in h.path or []:
                sp = link.source_paper or {}
                pmid = sp.get("pmid", "") or ""
                doi = sp.get("doi", "") or ""
                if pmid:
                    pmids.add(pmid)
                path_out.append({
                    "from_id": link.from_id,
                    "from_name": link.from_name,
                    "to_id": link.to_id,
                    "to_name": link.to_name,
                    "relation_type": link.relation_type,
                    "confidence": link.confidence,
                    "claim_id": link.claim_id,
                    "raw_text": (link.raw_text or "")[:400],
                    "paper": {
                        "pmid": pmid,
                        "doi": doi,
                        "title": sp.get("title", ""),
                        "year": sp.get("year"),
                        "journal": sp.get("journal", ""),
                        "pubmed_url": _pmid_url(pmid),
                        "doi_url": _doi_url(doi),
                    },
                })
        recipe = state["recipes_by_hyp"].get(h.id)
        return {
            "id": h.id,
            "hypothesis_type": h.hypothesis_type,
            "source_id": h.source_id,
            "source_name": h.source_name,
            "target_id": h.target_id,
            "target_name": h.target_name,
            "confidence_score": h.confidence_score,
            "novelty_score": h.novelty_score,
            "evidence_score": h.evidence_score,
            "testability_score": h.testability_score,
            "composite_score": h.composite_score,
            "critic_score": h.critic_score,
            "critic_rounds": h.critic_rounds,
            "testability_reason": h.testability_reason,
            "explanation": h.explanation,
            "path": path_out,
            "supporting_claims": list(h.supporting_claims or [])[:20],
            "source_file": (h.metadata or {}).get("_source_file", ""),
            "pmids": sorted(pmids)[:10],
            "has_recipe": recipe is not None,
            "recipe": (
                {
                    "id": recipe.get("id"),
                    "dataset": recipe.get("dataset"),
                    "model_arch": recipe.get("model_arch"),
                    "atlas": recipe.get("atlas"),
                    "target_outcome": recipe.get("target_outcome"),
                    "input_modalities": recipe.get("input_modalities"),
                    "rationale": recipe.get("rationale", "")[:300],
                }
                if recipe else None
            ),
        }

    # ── KG routes ──────────────────────────────────────────────────────────

    @app.get("/explore")
    async def kg_explore_page() -> Any:
        page = STATIC_DIR / "explore.html"
        if page.exists():
            return FileResponse(str(page))
        return HTMLResponse(
            "<h1>Knowledge Graph Explorer</h1>"
            f"<p>explore.html not found in <code>{STATIC_DIR}</code>.</p>",
            status_code=500,
        )

    @app.get("/api/kg/stats")
    async def kg_stats() -> Any:
        if not _kg_state.get("loaded"):
            if _kg_state.get("loading"):
                return {"loaded": False, "loading": True}
            # Kick off load in background without waiting
            asyncio.create_task(_get_kg_state())
            return {"loaded": False, "loading": True}
        return {"loaded": True, **_kg_state["stats"]}

    @app.post("/api/kg/load")
    async def kg_load() -> Any:
        try:
            state = await _get_kg_state()
            return {"loaded": True, **state["stats"]}
        except Exception as exc:
            return JSONResponse({"loaded": False, "error": str(exc)}, status_code=500)

    @app.get("/api/kg/search")
    async def kg_search(
        q: str = "",
        domain: str = "",
        limit: int = 20,
        quality: str = "clean",
    ) -> Any:
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        q_norm = (q or "").strip().lower()
        domain_filter = {d.strip() for d in (domain or "").split(",") if d.strip()}
        quality = quality.lower() if quality else "clean"
        if quality not in ("all", "clean", "strict"):
            quality = "clean"
        idx = state["name_index"] if quality == "all" else state["clean_name_index"]
        tri_idx = state["trigram_index"] if quality == "all" else state["clean_trigram_index"]

        def passes_strict(s: dict) -> bool:
            return s["n_hypotheses"] > 0 or s["n_claims"] >= 3

        def _trigrams_q(s: str) -> set[str]:
            if len(s) < 3:
                return {s} if s else set()
            return {s[i:i+3] for i in range(len(s) - 2)}

        # Default listing when no query text: return pre-computed top list
        if len(q_norm) < 2:
            top_list = state["top_all"] if quality == "all" else state["top_clean"]
            if domain_filter:
                filtered = [r for r in top_list if domain_filter & set(r["domain_tags"])]
                if quality == "strict":
                    filtered = [r for r in filtered if passes_strict(r)]
                return {"results": filtered[:max(1, int(limit))], "query": q, "quality": quality, "mode": "top"}
            if quality == "strict":
                filtered = [r for r in top_list if passes_strict(r)]
                return {"results": filtered[:max(1, int(limit))], "query": q, "quality": quality, "mode": "top"}
            return {"results": top_list[:max(1, int(limit))], "query": q, "quality": quality, "mode": "top"}

        seen: set[str] = set()
        results: list[dict] = []

        # Exact key hit first
        for nid in idx.get(q_norm, []):
            if nid in seen:
                continue
            summary = _node_summary(state, nid)
            if summary is None:
                continue
            if domain_filter and not (domain_filter & set(summary["domain_tags"])):
                continue
            if quality == "strict" and not passes_strict(summary):
                continue
            summary["match"] = "exact"
            results.append(summary)
            seen.add(nid)

        # Trigram-accelerated substring search
        if len(results) < limit:
            tris = _trigrams_q(q_norm)
            if tris:
                candidate_keys: set[str] | None = None
                for tri in tris:
                    keys = tri_idx.get(tri)
                    if keys is None:
                        candidate_keys = set()
                        break
                    if candidate_keys is None:
                        candidate_keys = set(keys)
                    else:
                        candidate_keys &= keys
                for key in (candidate_keys or set()):
                    if q_norm not in key:
                        continue
                    for nid in idx.get(key, []):
                        if nid in seen:
                            continue
                        summary = _node_summary(state, nid)
                        if summary is None:
                            continue
                        if domain_filter and not (domain_filter & set(summary["domain_tags"])):
                            continue
                        if quality == "strict" and not passes_strict(summary):
                            continue
                        summary["match"] = "substring"
                        results.append(summary)
                        seen.add(nid)
                        if len(results) >= limit * 3:
                            break
                    if len(results) >= limit * 3:
                        break

        results.sort(
            key=lambda r: (
                0 if r.get("match") == "exact" else 1,
                r.get("noise_score", 0.0),
                -(r["n_hypotheses"] * 2 + r["n_claims"]),
                len(r["name"]),
            )
        )
        return {"results": results[:limit], "query": q, "quality": quality, "mode": "search"}

    @app.get("/api/kg/node/{node_id}")
    async def kg_node(node_id: str) -> Any:
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        kg = state["kg"]
        node = kg._index.get(node_id)
        if node is None:
            return JSONResponse({"error": f"node not found: {node_id}"}, status_code=404)
        n_claims = len(state["concept_to_claims"].get(node_id, []))
        n_hyps = len(state["concept_to_hyps"].get(node_id, set()))
        noise = state.get("noise_map", {}).get(node_id, 0.0)
        reasons = _noise_reasons(node_id, node.preferred_name or "", n_claims, n_hyps) if noise >= NOISE_THRESHOLD else []
        return {
            "id": node.id,
            "name": node.preferred_name,
            "definition": node.definition or "",
            "domain_tags": list(node.domain_tags or []),
            "semantic_types": list(node.semantic_types or []),
            "source_vocab": node.source_vocab or "",
            "aliases": list(node.aliases or []),
            "external_ids": dict(node.external_ids or {}),
            "external_links": _external_links(node.external_ids),
            "atlas_mapping": node.atlas_mapping,
            "n_claims": n_claims,
            "n_hypotheses": n_hyps,
            "noise_score": noise,
            "is_noise": noise >= NOISE_THRESHOLD,
            "noise_reasons": reasons,
            "color": DOMAIN_COLORS.get(
                (node.domain_tags or ["unknown"])[0], "#94a3b8"
            ),
        }

    @app.get("/api/kg/node/{node_id}/neighborhood")
    async def kg_neighborhood(
        node_id: str,
        depth: int = 1,
        edge_types: str = "",
        limit: int = 80,
    ) -> Any:
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        kg = state["kg"]
        if node_id not in kg._index:
            return JSONResponse({"error": f"node not found: {node_id}"}, status_code=404)

        depth = max(1, min(2, int(depth)))
        limit = max(10, min(200, int(limit)))
        type_filter = {t.strip() for t in edge_types.split(",") if t.strip() and t.strip() != "all"}

        G = kg.G
        visited = {node_id}
        depth_map: dict[str, int] = {node_id: 0}
        frontier = {node_id}
        edges_collected: list[tuple[str, str, dict]] = []
        for hop in range(depth):
            next_frontier: set[str] = set()
            for n in frontier:
                for _, tgt, data in G.out_edges(n, data=True):
                    rt = data.get("relation_type", "")
                    if type_filter and rt not in type_filter:
                        continue
                    if rt == "about":
                        continue
                    if hop > 0 and tgt in visited:
                        continue
                    edges_collected.append((n, tgt, data))
                    if tgt not in visited:
                        next_frontier.add(tgt)
                        depth_map.setdefault(tgt, hop + 1)
                for src, _, data in G.in_edges(n, data=True):
                    rt = data.get("relation_type", "")
                    if type_filter and rt not in type_filter:
                        continue
                    if rt == "about":
                        continue
                    if hop > 0 and src in visited:
                        continue
                    edges_collected.append((src, n, data))
                    if src not in visited:
                        next_frontier.add(src)
                        depth_map.setdefault(src, hop + 1)
            visited |= next_frontier
            # For depth>=2: after hop 0, narrow the frontier to the top-N
            # depth-1 nodes by degree so hop 1 doesn't explode, but still runs.
            if depth >= 2 and hop == 0 and len(next_frontier) > limit:
                ranked = sorted(next_frontier, key=lambda n: G.degree(n), reverse=True)
                frontier = set(ranked[:limit])
            else:
                frontier = next_frontier

        # depth=2: drop edges that connect two nodes already at depth=1 to avoid
        # cluttering the graph with sibling cross-links (the user asked that
        # depth=2 not consider two peer neighbors being connected). Keep edges
        # that touch the center or a depth=2 node.
        if depth >= 2:
            edges_collected = [
                (s, t, d) for (s, t, d) in edges_collected
                if not (depth_map.get(s) == 1 and depth_map.get(t) == 1)
            ]

        # Rank candidate nodes by degree, but when depth>=2 reserve slots for
        # depth-2 nodes so the user actually sees A→B→C chains.
        node_ids: list[str] = [node_id]
        slots = max(0, limit - 1)
        if depth >= 2:
            d1 = [n for n in visited if n != node_id and depth_map.get(n) == 1]
            d2 = [n for n in visited if n != node_id and depth_map.get(n) == 2]
            d1.sort(key=lambda n: G.degree(n), reverse=True)
            d2.sort(key=lambda n: G.degree(n), reverse=True)
            # Reserve ~40% of slots for depth-2 nodes (at least 5 if available)
            d2_slots = max(5, slots * 2 // 5)
            d1_slots = slots - min(d2_slots, len(d2))
            node_ids.extend(d1[:d1_slots])
            node_ids.extend(d2[:slots - len(node_ids) + 1])
        else:
            candidates = [n for n in visited if n != node_id]
            candidates.sort(key=lambda n: G.degree(n), reverse=True)
            node_ids.extend(candidates[:slots])
        keep = set(node_ids)

        nodes_out: list[dict] = []
        noise_map = state.get("noise_map", {})
        for nid in node_ids:
            nd = kg._index.get(nid)
            if nd is None:
                continue
            domain = (nd.domain_tags or ["unknown"])[0]
            is_claim = "claim" in (nd.domain_tags or [])
            # Prefer a biology domain over "claim" for color
            color_domain = domain
            if is_claim and len(nd.domain_tags or []) > 1:
                for d in nd.domain_tags:
                    if d != "claim":
                        color_domain = d
                        break
            label = nd.preferred_name or nid
            if is_claim and len(label) > 60:
                label = label[:57] + "…"
            noise = noise_map.get(nid, 0.0)
            is_noisy = noise >= NOISE_THRESHOLD
            base_size = 14 if nid == node_id else (6 if is_claim else 9)
            nodes_out.append({
                "id": nid,
                "label": label,
                "color": DOMAIN_COLORS.get(color_domain, "#94a3b8"),
                "domain": color_domain,
                "domains": list(nd.domain_tags or []),
                "is_claim": is_claim,
                "is_center": nid == node_id,
                "depth": depth_map.get(nid, 0),
                "size": base_size if not (is_noisy and nid != node_id) else max(3, int(base_size * 0.55)),
                "noise_score": noise,
                "is_noise": is_noisy,
            })

        # Aggregate edges by unordered pair so bidirectional or multi-predicate
        # edges render as a single visual line (prevents label overlap).
        # Additionally scan claim nodes to surface predicates that the DiGraph
        # collapsed (graph_manager keeps only the highest-confidence relation).
        pair_info: dict[frozenset, dict] = {}
        for src, tgt, data in edges_collected:
            if src not in keep or tgt not in keep:
                continue
            rt = data.get("relation_type", "")
            if not rt:
                continue
            pair = frozenset({src, tgt}) if src != tgt else frozenset({src})
            entry = pair_info.setdefault(pair, {
                "src": src, "tgt": tgt,   # may be overwritten; used for first-seen direction
                "relations_fwd": [],     # ordered, deduped
                "relations_rev": [],
                "confidence": 0.0,
            })
            entry["confidence"] = max(entry["confidence"], float(data.get("confidence", 1.0)))
            # Track which direction this relation was seen in relative to (src, tgt)
            if (src, tgt) == (entry["src"], entry["tgt"]):
                if rt not in entry["relations_fwd"]:
                    entry["relations_fwd"].append(rt)
            else:
                if rt not in entry["relations_rev"]:
                    entry["relations_rev"].append(rt)

        # Pull additional claim-backed predicates between kept pairs
        concept_to_claims = state["concept_to_claims"]
        for pair, entry in pair_info.items():
            a_raw = list(pair)
            if len(a_raw) == 1:
                continue  # self-loop; skip extra claim scan
            a, b = a_raw[0], a_raw[1]
            a_claims = set(concept_to_claims.get(a, []))
            b_claims = set(concept_to_claims.get(b, []))
            shared = a_claims & b_claims
            if not shared:
                continue
            for cid in shared:
                cn = kg._index.get(cid)
                if cn is None:
                    continue
                meta = cn.metadata or {}
                pred = (meta.get("predicate") or "").strip()
                if not pred:
                    continue
                if type_filter and pred not in type_filter:
                    continue
                subj = meta.get("subject_id", "")
                obj = meta.get("object_id", "")
                if (subj, obj) == (entry["src"], entry["tgt"]):
                    if pred not in entry["relations_fwd"]:
                        entry["relations_fwd"].append(pred)
                elif (subj, obj) == (entry["tgt"], entry["src"]):
                    if pred not in entry["relations_rev"]:
                        entry["relations_rev"].append(pred)

        # Emit merged edges
        edges_out: list[dict] = []
        for pair, entry in pair_info.items():
            fwd = entry["relations_fwd"]
            rev = entry["relations_rev"]
            if not fwd and not rev:
                continue
            # Combine labels. If bidirectional, join both with ⇄ so the user sees
            # there are multiple relations.
            parts: list[str] = []
            if fwd:
                parts.append(" Β· ".join(fwd[:3]) + (f" +{len(fwd)-3}" if len(fwd) > 3 else ""))
            if rev:
                parts.append("← " + " Β· ".join(rev[:3]) + (f" +{len(rev)-3}" if len(rev) > 3 else ""))
            label = "  ⇄  ".join(parts) if (fwd and rev) else (parts[0] if parts else "")
            primary = (fwd[0] if fwd else rev[0])
            edges_out.append({
                "id": f"e{len(edges_out)}",
                "source": entry["src"],
                "target": entry["tgt"],
                "label": label,
                "relations_fwd": fwd,
                "relations_rev": rev,
                "bidirectional": bool(fwd and rev),
                "color": RELATION_COLORS.get(primary, DEFAULT_EDGE_COLOR),
                "confidence": entry["confidence"],
            })

        return {
            "center": node_id,
            "depth": depth,
            "nodes": nodes_out,
            "edges": edges_out,
            "depth_map": {nid: depth_map.get(nid, 0) for nid in keep},
            "truncated": len(visited) > limit,
        }

    @app.get("/api/kg/node/{node_id}/claims")
    async def kg_claims(
        node_id: str,
        limit: int = 50,
        predicate: str = "",
        neighbor_id: str = "",
    ) -> Any:
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        kg = state["kg"]
        if node_id not in kg._index:
            return JSONResponse({"error": f"node not found: {node_id}"}, status_code=404)
        claim_ids = state["concept_to_claims"].get(node_id, [])
        # parse predicate filter (may be comma-separated list from the UI)
        pred_filter = {p.strip() for p in (predicate or "").split(",") if p.strip() and p.strip() != "all"}
        items = []
        for cid in claim_ids:
            s = _serialize_claim(state, cid)
            if not s:
                continue
            if pred_filter and s.get("predicate") not in pred_filter:
                continue
            if neighbor_id:
                if s.get("subject_id") != neighbor_id and s.get("object_id") != neighbor_id:
                    continue
            items.append(s)
        # Sort: confidence desc, year desc
        items.sort(key=lambda c: (
            -(c.get("confidence") or 0.0),
            -((c.get("paper") or {}).get("year") or 0),
        ))
        return {"node_id": node_id, "total": len(items), "claims": items[: max(1, int(limit))]}

    @app.get("/api/kg/edge-sources")
    async def kg_edge_sources(source: str = "", target: str = "", limit: int = 50) -> Any:
        """Return all claims + curated edges that connect two concepts (either direction)."""
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        kg = state["kg"]
        if not source or not target:
            return JSONResponse({"error": "source and target are required"}, status_code=400)
        if source not in kg._index or target not in kg._index:
            return JSONResponse({"error": "node(s) not found"}, status_code=404)

        src_node = kg._index[source]
        tgt_node = kg._index[target]

        # 1. Claims where {subject, object} == {source, target}
        claim_items: list[dict] = []
        seen_claims: set[str] = set()
        src_claims = set(state["concept_to_claims"].get(source, []))
        tgt_claims = set(state["concept_to_claims"].get(target, []))
        for cid in src_claims & tgt_claims:
            if cid in seen_claims:
                continue
            seen_claims.add(cid)
            s = _serialize_claim(state, cid)
            if s:
                claim_items.append(s)
        claim_items.sort(key=lambda c: (
            -(c.get("confidence") or 0.0),
            -((c.get("paper") or {}).get("year") or 0),
        ))

        # 2. Curated edges (non-claim) between the two nodes, both directions
        curated_edges: list[dict] = []
        G = kg.G
        for u, v in ((source, target), (target, source)):
            if G.has_edge(u, v):
                data = G.edges[u, v]
                src_str = data.get("source", "")
                if src_str.startswith("claim:"):
                    continue  # already counted above
                curated_edges.append({
                    "from_id": u,
                    "from_name": kg._index[u].preferred_name,
                    "to_id": v,
                    "to_name": kg._index[v].preferred_name,
                    "relation_type": data.get("relation_type", ""),
                    "confidence": float(data.get("confidence", 1.0)),
                    "source_vocab": src_str or "curated",
                    "evidence_ref": data.get("evidence_ref", ""),
                })

        return {
            "source": {"id": source, "name": src_node.preferred_name},
            "target": {"id": target, "name": tgt_node.preferred_name},
            "total_claims": len(claim_items),
            "total_curated_edges": len(curated_edges),
            "claims": claim_items[: max(1, int(limit))],
            "curated_edges": curated_edges,
        }

    @app.get("/api/kg/node/{node_id}/hypotheses")
    async def kg_hypotheses(
        node_id: str,
        limit: int = 20,
        min_score: float = 0.0,
        recipe_only: bool = False,
    ) -> Any:
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        kg = state["kg"]
        if node_id not in kg._index:
            return JSONResponse({"error": f"node not found: {node_id}"}, status_code=404)
        hyp_ids = state["concept_to_hyps"].get(node_id, set())
        hyps = [state["hypotheses_by_id"][hid] for hid in hyp_ids if hid in state["hypotheses_by_id"]]
        # Filters
        if min_score > 0:
            hyps = [h for h in hyps if (h.composite_score or 0.0) >= min_score]
        if recipe_only:
            hyps = [h for h in hyps if h.id in state["recipes_by_hyp"]]
        hyps.sort(key=lambda h: (h.composite_score or 0.0), reverse=True)
        items = [_serialize_hypothesis(state, h) for h in hyps[: max(1, int(limit))]]
        return {
            "node_id": node_id,
            "total": len(hyps),
            "hypotheses": items,
            "has_recipes": len(state["recipes_by_hyp"]) > 0,
        }

    @app.get("/api/kg/hypothesis/{hyp_id}")
    async def kg_hypothesis_detail(hyp_id: str) -> Any:
        try:
            state = await _get_kg_state()
        except Exception as exc:
            return JSONResponse({"error": str(exc)}, status_code=500)
        h = state["hypotheses_by_id"].get(hyp_id)
        if h is None:
            return JSONResponse({"error": f"hypothesis not found: {hyp_id}"}, status_code=404)
        return _serialize_hypothesis(state, h)

    return app


# ── Entry point ────────────────────────────────────────────────────────────────

def run_server(host: str = DEFAULT_HOST, port: int = DEFAULT_PORT) -> None:
    """Start the uvicorn server (blocking call β€” returns only when the server stops)."""
    _require_webdeps()
    import uvicorn  # type: ignore

    app = create_app()
    print(f"\n  NeuroClaw Web UI  β†’  http://{host}:{port}\n")
    uvicorn.run(app, host=host, port=port, log_level="info")


def main() -> None:
    parser = argparse.ArgumentParser(
        description="NeuroClaw Web UI β€” start the browser-based chat interface."
    )
    parser.add_argument(
        "--host", default=DEFAULT_HOST,
        help=f"Bind host (default: {DEFAULT_HOST})",
    )
    parser.add_argument(
        "--port", type=int, default=DEFAULT_PORT,
        help=f"Port number (default: {DEFAULT_PORT})",
    )
    args = parser.parse_args()
    run_server(host=args.host, port=args.port)


if __name__ == "__main__":
    main()