File size: 98,871 Bytes
5086f7d
 
 
00f5427
231c9f3
5086f7d
 
 
 
6707b93
daa13a1
128b2df
5086f7d
94f18b8
5086f7d
e9fb2a1
fc4306d
b2726f9
5086f7d
166b7bc
5086f7d
 
 
 
8f7c928
 
5086f7d
90e2341
5086f7d
a2ac6c1
5086f7d
112460e
db6f35a
94f18b8
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58c3f9f
5086f7d
 
 
 
 
ccd0f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5086f7d
 
 
 
 
 
8c6315b
 
abc6cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c6315b
 
 
 
 
 
 
 
 
51c66de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1baa05
 
 
 
 
 
de5fc64
b1baa05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51c66de
de5fc64
51c66de
 
 
 
 
 
b1baa05
 
 
51c66de
b1baa05
 
51c66de
 
 
 
 
5086f7d
51c66de
 
5086f7d
 
 
 
 
 
 
c3d309d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231c9f3
 
 
c3d309d
231c9f3
 
 
 
 
db921ed
 
 
231c9f3
c3d309d
231c9f3
db921ed
c3d309d
231c9f3
db921ed
 
231c9f3
 
 
 
 
 
 
 
db921ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3d309d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fc372a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94f18b8
 
 
 
 
0fc372a
 
51c66de
 
 
 
 
 
 
 
 
 
 
 
 
94f18b8
 
 
 
0fc372a
 
 
 
 
 
 
 
 
 
 
 
 
94f18b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6707b93
 
5086f7d
daa13a1
 
 
 
 
 
 
 
 
 
 
 
ccd0f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6707b93
 
5086f7d
 
 
 
daa13a1
 
 
 
5086f7d
6707b93
5086f7d
 
 
 
 
 
daa13a1
 
 
 
5086f7d
6707b93
 
5086f7d
 
 
 
 
 
 
 
 
db6f35a
 
5086f7d
 
6707b93
b2726f9
3bcfc1d
ccd0f32
 
 
fc4306d
5086f7d
 
 
6707b93
5086f7d
ccd0f32
 
 
 
 
 
5086f7d
 
ccd0f32
5086f7d
 
 
 
 
 
 
 
 
 
 
b2726f9
fc4306d
5086f7d
3bcfc1d
ccd0f32
 
 
5086f7d
 
 
fc4306d
 
 
75f0c8b
 
 
 
 
 
 
 
 
 
fc4306d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d63c03b
 
 
 
 
 
 
 
 
 
 
e9fb2a1
 
 
 
 
 
 
 
 
 
 
 
 
94f18b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6707b93
5086f7d
00f5427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bcfc1d
ccd0f32
 
 
 
00f5427
 
 
 
 
 
 
 
3bcfc1d
ccd0f32
 
 
00f5427
 
5086f7d
0d18983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6707b93
5086f7d
 
 
 
6707b93
5086f7d
 
 
 
 
 
112460e
 
 
 
 
 
 
 
 
 
 
6707b93
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a77684b
5086f7d
9cdc4f7
5086f7d
 
 
 
 
8c6315b
 
90e2341
5086f7d
 
 
 
 
 
8c6315b
 
 
 
 
 
 
 
 
 
5086f7d
 
6707b93
5086f7d
 
 
 
 
 
 
9cdc4f7
 
 
 
 
 
 
6797597
e9fb2a1
6797597
 
 
 
 
 
 
e9fb2a1
 
 
 
 
abc6cbe
 
 
 
 
 
 
 
 
 
 
 
78e6165
 
abc6cbe
e9fb2a1
4161a4e
abc6cbe
 
 
 
 
 
 
 
 
 
4161a4e
abc6cbe
4161a4e
abc6cbe
 
 
 
 
 
5086f7d
 
 
6797597
 
e9fb2a1
9cdc4f7
 
abc6cbe
db6f35a
abc6cbe
e9fb2a1
abc6cbe
 
5086f7d
 
 
 
 
8c6315b
 
90e2341
5086f7d
 
 
 
 
 
8c6315b
 
 
 
 
 
 
 
 
 
5086f7d
 
6707b93
5086f7d
 
 
 
 
 
 
 
 
6707b93
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2ac6c1
 
 
 
 
 
72f2e08
 
 
 
0fc372a
 
a2ac6c1
6707b93
37e02b1
5086f7d
 
 
0fc372a
 
 
a2ac6c1
 
 
 
 
 
0fc372a
91600eb
5086f7d
166b7bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3d309d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166b7bc
 
 
c3d309d
 
 
 
 
 
 
 
 
 
166b7bc
 
c3d309d
 
 
 
 
 
 
128b2df
c3d309d
128b2df
 
 
 
 
e9fb2a1
128b2df
 
e9fb2a1
 
 
 
 
 
 
 
 
 
128b2df
 
 
e9fb2a1
128b2df
e9fb2a1
128b2df
 
 
e9fb2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128b2df
 
c3d309d
128b2df
c3d309d
 
e9fb2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166b7bc
6707b93
8d6b7af
5086f7d
 
 
0fc372a
 
 
 
 
c3d309d
 
 
 
549b060
c3d309d
549b060
 
0fc372a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62add84
0fc372a
 
0a28266
0fc372a
 
 
 
5086f7d
 
 
 
62add84
db6f35a
c3d309d
 
 
8d6b7af
5086f7d
 
bf0b3e5
 
0d18983
7f8bb3a
abc6cbe
 
 
 
5086f7d
 
65b4e25
 
de5fc64
 
91600eb
 
 
51c66de
 
 
95484a6
0fc372a
 
51c66de
5086f7d
 
0fc372a
 
5086f7d
b1baa05
91600eb
51c66de
5086f7d
 
166b7bc
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46421be
 
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a77684b
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a77684b
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6797597
 
 
 
 
 
 
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128b2df
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128b2df
5086f7d
128b2df
 
 
 
 
 
 
 
5086f7d
128b2df
 
5086f7d
 
112460e
5086f7d
 
112460e
5086f7d
 
 
 
112460e
5086f7d
112460e
5086f7d
a77684b
5086f7d
 
 
112460e
 
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a77684b
5086f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128b2df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9fb2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91600eb
 
 
 
e9fb2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91600eb
 
e9fb2a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6707b93
9a1b2e2
6707b93
9a1b2e2
 
 
 
 
 
6707b93
 
 
9a1b2e2
6707b93
9a1b2e2
 
 
 
6707b93
9a1b2e2
 
6707b93
 
 
 
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
from __future__ import annotations

import json
import os
import re
from pathlib import Path
from typing import Any

import gradio as gr
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, RedirectResponse
from huggingface_hub import HfApi, attach_huggingface_oauth

from src.bucket import RunPaths, check_user_bucket, create_user_bucket, delete_run_folder, read_run_bundle, list_recent_runs, read_json, write_json, write_launch_metadata
from src.config import settings, user_bucket_source
from src.eval_config import activate_eval_archive_config, disable_eval_archive_config, flush_eval_archive_records, public_eval_config
from src.eval_archive import maybe_publish_eval_record
from src.auth import extract_oauth_context, public_oauth_context, oauth_warning_messages, verify_token_identity, oauth_lifetime_summary
from src.jobs import (
    cancel_job_safe,
    fetch_recent_logs_safe,
    inspect_job_safe,
    launch_universal_model_card_job,
    launch_validate_existing_space_job,
    clamp_validation_timeout_seconds,
    ensure_validation_payload_json_fits_env,
)
from src.runs import make_run_id, utc_now_iso, validate_run_id
from src.progress import progress_from_events
from src.view_models import build_run_view_model, find_latest_resumable_run
from src.security import redact
from src.model_scan import scan_model_card
from src.version import ASF_RELEASE_NAME
from src.effective_status import compute_effective_run_status


APP_DESCRIPTION = f"""
# Agentic Space Factory

Turn a Hugging Face model card into a **private, testable Gradio Space** using an agentic HF Job.

## Recommended workflow

```text
1. Build from model card
   β†’ creates a private Space
   β†’ attempts ZeroGPU first
   β†’ falls back to a fixed GPU if automatic hardware assignment is available
   β†’ otherwise marks the run as manual_hardware_required

2. If hardware had to be changed manually
   β†’ set the GPU in the generated Space Settings
   β†’ run Validate existing Space
   β†’ smoke-test generation
   β†’ measure latency
   β†’ store the output artifact in the Bucket
```

Each launch returns quick links to open the HF Job, generated Space, Space settings, and run artifacts in new tabs.

## Honest guarantees

- Spaces are private by default.
- Nothing is published automatically.
- Runs, reports, generated files, traces, validation results, and artifacts are written to your private Bucket.
- Success is based on the deployed Space, not only generated code.
- ZeroGPU and fixed-GPU upgrades are best-effort through OAuth; manual hardware selection is an expected fallback.

## Limits

This app attempts model-card builds; it does not guarantee that every model will run. Multi-GPU models, Docker-only apps, custom CUDA/FlashAttention stacks, gated models, very large models, or models with unclear documentation may produce `technical_blocker`, `health_only`, or `manual_hardware_required` instead of a full inference success.

Run Bucket: by default each signed-in user writes to their own private bucket: `<username>/{settings.bucket_name}`. Use **Check run bucket** or **Create private run bucket** before launching Jobs.
"""




WEB_DIR = Path(__file__).parent / "web"
STATIC_DIR = WEB_DIR / "static"



def _oauth_context_from_request(request: Request) -> dict[str, Any]:
    """Return OAuth context for custom API routes. Token stays server-side only."""
    ctx = extract_oauth_context(request)
    return {
        "username": ctx.username,
        "token": ctx.token,
        "profile": ctx.profile,
        "scopes": sorted(ctx.scopes),
        "missing_scopes": ctx.missing_scopes,
        "expires_at": ctx.expires_at.isoformat() if ctx.expires_at else None,
        "auth_lifetime": oauth_lifetime_summary(ctx),
        "is_pro": ctx.is_pro,
        "can_pay": ctx.can_pay,
        "warnings": oauth_warning_messages(ctx),
    }


def _clear_local_oauth_session(request: Request) -> dict[str, Any]:
    """Clear ASF/HF OAuth session state without requiring a valid token.

    This is intentionally tolerant: expired OAuth sessions must be recoverable
    even when parse_huggingface_oauth/extract_oauth_context can no longer
    deserialize or validate the token.
    """
    cleared_keys: list[str] = []
    had_session = False
    try:
        session = request.session  # type: ignore[attr-defined]
        had_session = bool(session)
        for key in list(session.keys()):
            key_l = str(key).lower()
            if "oauth" in key_l or "huggingface" in key_l or key_l in {"state", "nonce", "next", "redirect_uri"}:
                cleared_keys.append(str(key))
        session.clear()
    except Exception:
        had_session = False
    return {"cleared": True, "had_session": had_session, "cleared_keys": sorted(set(cleared_keys))}


def _auth_recovery_payload(reason: str = "not_authenticated", detail: str | None = None) -> dict[str, Any]:
    reason_l = (reason or "not_authenticated").lower()
    if "expired" in reason_l or (detail and "expired" in detail.lower()):
        reason_l = "oauth_expired"
        message = "Your Hugging Face OAuth session expired. Refresh sign-in to continue."
    else:
        reason_l = "not_signed_in" if reason_l in {"not_authenticated", "please sign in with hugging face first."} else reason_l
        message = "Sign in with Hugging Face to continue."
    return {
        "authenticated": False,
        "reason": reason_l,
        "message": message,
        "login_url": "/oauth/huggingface/login",
        "refresh_login_url": "/auth/refresh-login",
        "reset_url": "/auth/logout-local",
        "logout_url": "/auth/logout-local",
        "recovery": {
            "primary_action": "refresh_sign_in" if reason_l == "oauth_expired" else "sign_in",
            "refresh_login_url": "/auth/refresh-login",
            "reset_local_auth_url": "/auth/logout-local",
            "open_in_new_tab_recommended": True,
        },
    }

def _json_error(exc: Exception) -> HTTPException:
    if isinstance(exc, HTTPException):
        return exc
    return HTTPException(status_code=400, detail=redact(str(exc)))






def _first_list_value(*values: Any) -> list[Any] | None:
    for value in values:
        if isinstance(value, list):
            return value
    return None


def _first_dict_value(*values: Any) -> dict[str, Any] | None:
    for value in values:
        if isinstance(value, dict):
            return value
    return None


def _linked_replay_source_from_parent_bundle(parent_run_id: str, parent_bundle: dict[str, Any], target_space: str, expected_output_type: str | None = None) -> dict[str, Any]:
    """Resolve the parent automatic smoke request server-side for replay Jobs.

    The validation worker runs in a fresh Job and cannot assume the parent run
    folder is present locally. Passing this compact, redacted source through the
    Job environment prevents linked replay from silently falling back to schema
    reconstruction when the parent already proved a working request.
    """
    smoke = parent_bundle.get("generation_smoke") or {}
    retry_payload = parent_bundle.get("generation_smoke_payload_retry") or {}
    initial_payload = parent_bundle.get("generation_smoke_payload") or {}
    resolved = parent_bundle.get("resolved_validation_request") or {}
    state_obj = parent_bundle.get("state") or {}
    state_smoke = state_obj.get("generation_smoke") if isinstance(state_obj.get("generation_smoke"), dict) else {}
    status_tokens = {
        str(smoke.get("status") or "").lower(),
        str(state_obj.get("status") or "").lower(),
        str(state_smoke.get("status") or "").lower(),
    }
    parent_was_successful = bool(status_tokens & {"success", "full_inference_success", "completed", "complete", "done"})
    if not parent_was_successful:
        return {}
    api_name = (
        smoke.get("api_name")
        or retry_payload.get("api_name")
        or initial_payload.get("api_name")
        or resolved.get("api_name")
        or state_smoke.get("api_name")
        or ""
    )
    test_args = _first_list_value(
        smoke.get("effective_args"),
        smoke.get("test_args"),
        retry_payload.get("test_args"),
        retry_payload.get("effective_args"),
        initial_payload.get("test_args"),
        initial_payload.get("effective_args"),
        resolved.get("test_args"),
        state_smoke.get("effective_args"),
        state_smoke.get("test_args"),
    )
    test_kwargs = _first_dict_value(
        smoke.get("effective_kwargs"),
        smoke.get("test_kwargs"),
        retry_payload.get("test_kwargs"),
        retry_payload.get("effective_kwargs"),
        initial_payload.get("test_kwargs"),
        initial_payload.get("effective_kwargs"),
        resolved.get("test_kwargs"),
        state_smoke.get("effective_kwargs"),
        state_smoke.get("test_kwargs"),
    ) or {}
    if not api_name or not isinstance(test_args, list):
        return {}
    parent_target = smoke.get("target_space") or state_obj.get("target_space") or state_smoke.get("target_space") or target_space or ""
    return {
        "source": "parent_automatic_smoke_backend",
        "parent_run_id": parent_run_id,
        "parent_target_space": parent_target,
        "target_matches": not parent_target or parent_target == target_space,
        "api_name": str(api_name if str(api_name).startswith("/") else f"/{api_name}"),
        "test_args": test_args,
        "test_kwargs": test_kwargs,
        "expected_output_type": smoke.get("expected_output_type") or state_smoke.get("expected_output_type") or expected_output_type or "any",
        "latency_seconds": smoke.get("latency_seconds") or state_smoke.get("latency_seconds"),
        "parent_smoke_status": smoke.get("status") or state_smoke.get("status") or state_obj.get("status"),
        "transport": "job_env",
    }

def _job_url_from_result(result: dict[str, Any], username: str | None) -> str:
    explicit = (result.get("job_url") or "").strip() if isinstance(result.get("job_url"), str) else result.get("job_url")
    if explicit:
        return str(explicit)
    job_id = result.get("job_id")
    if job_id and username:
        return f"https://huggingface.co/jobs/{username}/{job_id}"
    return ""

def _normalize_target_space_id(value: Any) -> str:
    text = str(value or "").strip()
    if not text:
        return ""
    text = text.replace("https://huggingface.co/spaces/", "").strip("/")
    text = text.split("/settings", 1)[0].strip("/")
    if ".hf.space" in text and "/" not in text:
        # Subdomains are not reversible for all names; keep them out of repo-id
        # fields and use only owner/name repo ids as canonical target_space.
        return ""
    if re.match(r"^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$", text):
        return text
    return ""


def _target_space_from_events(events: list[dict[str, Any]] | None, *, require_success: bool = False) -> str:
    for event in reversed(events or []):
        if not isinstance(event, dict):
            continue
        step = str(event.get("step") or "")
        status = str(event.get("status") or "").lower()
        if step not in {"create_space", "create_space_hardware", "upload_files", "runtime_upload_epoch"}:
            continue
        if require_success and status != "success":
            continue
        data = event.get("data") if isinstance(event.get("data"), dict) else {}
        target = _normalize_target_space_id(data.get("target_space") or data.get("target_space_id"))
        if target:
            return target
    return ""


def _resolve_target_space_identity(run_id: str | None, bundle: dict[str, Any], *, bucket_source: str | None, job_url: str | None = None) -> dict[str, Any]:
    """Return canonical Space links from all stable run sources.

    v198.26.4: buttons stayed disabled when summary.target_space_url was empty
    even though create_space/upload_files had succeeded.  Resolve links from
    summary/state/launch/runtime_upload_epoch/hardware events and expose a
    single contract to the UI.
    """
    summary = bundle.get("summary") or bundle.get("summary_file") or {}
    state = bundle.get("state") or {}
    launch = bundle.get("launch") or {}
    runtime_upload_epoch = bundle.get("runtime_upload_epoch") or {}
    explicit_url = _first_non_empty(
        summary.get("target_space_url"), state.get("target_space_url"), launch.get("target_space_url")
    )
    target = _normalize_target_space_id(
        _first_non_empty(
            summary.get("target_space"), summary.get("target_space_id"),
            state.get("target_space"), state.get("target_space_id"),
            launch.get("target_space"), launch.get("target_space_id"),
            runtime_upload_epoch.get("target_space_id"), runtime_upload_epoch.get("target_space"),
            _target_space_from_events(bundle.get("events") or []),
        )
    )
    if explicit_url and not target:
        target = _normalize_target_space_id(explicit_url)
    target_space_url = explicit_url or (f"https://huggingface.co/spaces/{target}" if target else "")
    target_space_settings_url = f"{target_space_url}/settings" if target_space_url else ""
    events = bundle.get("events") or []
    space_runtime = bundle.get("space_runtime") or {}
    runtime_history = runtime_upload_epoch.get("history") if isinstance(runtime_upload_epoch.get("history"), list) else []
    create_event_ok = bool(target and any(str((e or {}).get("step") or "") in {"create_space", "create_space_hardware"} and str((e or {}).get("status") or "").lower() == "success" for e in events if isinstance(e, dict)))
    upload_event_ok = bool(any(str((e or {}).get("step") or "") in {"upload_files", "runtime_upload_epoch"} and str((e or {}).get("status") or "").lower() == "success" for e in events if isinstance(e, dict)))
    runtime_uploaded = bool(runtime_upload_epoch.get("last_upload_completed_at") or runtime_upload_epoch.get("upload_sequence") or runtime_history or upload_event_ok)
    space_runtime_known = bool(isinstance(space_runtime, dict) and (space_runtime.get("stage") or space_runtime.get("status") or space_runtime.get("runtime_status") or space_runtime.get("updated_at") or space_runtime.get("url")))
    # v198.26.9: Space links are durable run facts, not a projection of the
    # latest 100 events.  A runtime upload epoch or a persisted runtime probe
    # proves that the Space existed even if the final outcome later failed.
    space_created = bool(target and (create_event_ok or runtime_uploaded or space_runtime_known))
    sources = []
    if create_event_ok:
        sources.append("create_space_event")
    if upload_event_ok:
        sources.append("upload_event")
    if runtime_uploaded:
        sources.append("runtime_upload_epoch")
    if space_runtime_known:
        sources.append("space_runtime")
    if explicit_url:
        sources.append("explicit_target_space_url")
    links_ready = bool(target_space_url and (space_created or runtime_uploaded or space_runtime_known))
    return {
        "schema_version": "space_identity.v198_26_9",
        "target_space": target,
        "target_space_id": target,
        "target_space_known": bool(target),
        "target_space_url": target_space_url,
        "target_space_settings_url": target_space_settings_url,
        "space_created": bool(space_created),
        "runtime_uploaded": bool(runtime_uploaded),
        "space_uploaded": bool(runtime_uploaded),
        "space_runtime_known": bool(space_runtime_known),
        "links_ready": bool(links_ready),
        "source": "+".join(sources) or "resolved_from_bundle",
        "sources": sources,
        "job_url": job_url or "",
        "artifacts_url": _run_artifacts_url(run_id, bucket_source),
    }


def _api_links(*, run_id: str | None, bucket_source: str | None, target_space: str | None = None, job_url: str | None = None) -> dict[str, str]:
    target = _normalize_target_space_id(target_space)
    target_space_url = f"https://huggingface.co/spaces/{target}" if target else ""
    return {
        "job_url": job_url or "",
        "target_space_url": target_space_url,
        "target_space_settings_url": f"{target_space_url}/settings" if target_space_url else "",
        "artifacts_url": _run_artifacts_url(run_id, bucket_source),
    }


def _job_id_from_run_bundle(summary: dict[str, Any], launch: dict[str, Any], state: dict[str, Any]) -> str:
    return str(summary.get("job_id") or launch.get("job_id") or state.get("job_id") or "").strip()


def _normalize_job_stage(stage: Any) -> str:
    value = str(stage or "").strip().lower()
    if "." in value:
        value = value.rsplit(".", 1)[-1]
    value = value.replace("jobstage.", "").replace("_", "-")
    if value in {"running", "queued", "pending", "scheduled", "starting"}:
        return "running"
    if value in {"success", "succeeded", "complete", "completed", "done"}:
        return "success"
    if value in {"failed", "failure", "error"}:
        return "failed"
    if value in {"cancelled", "canceled", "canceling", "cancelling"}:
        return "cancelled"
    return value


def _event_key(event: dict[str, Any]) -> tuple[str, str, str, str]:
    return (
        str(event.get("ts") or ""),
        str(event.get("step") or ""),
        str(event.get("status") or ""),
        str(event.get("message") or ""),
    )


ANSI_RE = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")


def _events_from_job_logs(log_text: str) -> list[dict[str, Any]]:
    """Extract worker append_event JSON objects from HF Job logs.

    HF Job logs may prefix stdout lines with timestamps, may include ANSI
    sequences, and may sometimes have text after a JSON object. Use
    JSONDecoder.raw_decode from each opening brace instead of json.loads(line)
    so event extraction is not fragile. If the Job fails before the worker can
    write bucket events, synthesize one explicit failure event so the timeline
    still shows a red breakpoint instead of appearing empty/broken.
    """
    events: list[dict[str, Any]] = []
    decoder = json.JSONDecoder()
    clean_lines: list[str] = []
    for raw in (log_text or "").splitlines():
        line = ANSI_RE.sub("", raw).strip()
        if line:
            clean_lines.append(line)
        for match in re.finditer(r"\{", line):
            try:
                payload, _ = decoder.raw_decode(line[match.start():])
            except Exception:
                continue
            if isinstance(payload, dict) and payload.get("step") and payload.get("status"):
                events.append(payload)
                break
    text = "\n".join(clean_lines).lower()
    if not events and text:
        if "argument list too long" in text:
            events.append({
                "step": "failure",
                "status": "failed",
                "message": "HF Job failed before the worker started: Python argv/env was too large.",
                "details": {"source": "job_logs", "error": "argument list too long"},
            })
        elif any(marker in text for marker in ["traceback", "error", "failed", "exception"]):
            events.append({
                "step": "failure",
                "status": "failed",
                "message": clean_lines[-1][:500] if clean_lines else "HF Job failed before worker events were written.",
                "details": {"source": "job_logs"},
            })
    return events


def _merge_events(bucket_events: list[dict[str, Any]], log_events: list[dict[str, Any]]) -> list[dict[str, Any]]:
    seen: set[tuple[str, str, str, str]] = set()
    merged: list[dict[str, Any]] = []
    for event in [*(bucket_events or []), *(log_events or [])]:
        if not isinstance(event, dict):
            continue
        key = _event_key(event)
        if key in seen:
            continue
        seen.add(key)
        merged.append(event)
    return merged


def _terminal_status_value(value: Any) -> bool:
    return str(value or "").strip().lower() in {
        "succeeded",
        "success",
        "done",
        "completed",
        "full_inference_success",
        "partial",
        "partial_validation",
        "failed",
        "failure",
        "blocked",
        "technical_blocker",
        "manual_hardware_required",
        "waiting_manual_action",
        "cancelled",
        "canceled",
        "stopped",
        "stale",
    }


def _build_live_run_snapshot(
    run_id: str,
    *,
    bucket_source: str,
    token: str | None,
    include_heavy: bool = False,
    job_info: dict[str, Any] | None = None,
    job_log_events: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
    """Build the canonical UI snapshot shared by detail, view and polling routes.

    v190.10 invariant: Active Run polling, manual Run Explorer selection, and
    full page reload must converge to the same product status, timeline model,
    eval archive state and quick links.
    """
    bundle = read_run_bundle(run_id, bucket_source=bucket_source, token=token, include_heavy=include_heavy)
    state = bundle.get("state") or {}
    launch = bundle.get("launch") or {}
    summary = bundle.get("summary") or {}
    job_info = job_info or {}
    job_stage = _normalize_job_stage(job_info.get("stage"))
    events = _merge_events(bundle.get("events") or [], job_log_events or [])

    effective_state = {**launch, **state}
    if job_stage and not job_info.get("error"):
        effective_state["job_stage"] = job_stage
        # Job stage is live metadata, not a product verdict. Only use it to
        # keep a newly launched run visibly running before bucket events exist.
        if job_stage in {"running", "cancelled"} and not _terminal_status_value(summary.get("status")):
            effective_state.setdefault("status", job_stage)

    summary_status = str(summary.get("status") or "").lower()
    is_validation_run = str(run_id).startswith("validate-") or "validate" in str(summary.get("kind") or effective_state.get("kind") or "").lower()
    validation_terminal_statuses = {"failed", "full_inference_success", "partial_validation", "manual_hardware_required", "stale", "stopped"}
    validation_terminal_locked = bool(is_validation_run and summary_status in validation_terminal_statuses)
    if validation_terminal_locked:
        effective_state["status"] = summary_status
        effective_state["validation_status"] = summary_status
        effective_state["result_status"] = summary_status
        effective_state["terminal"] = True
        effective_state["can_resume"] = False

    effective_summary = {
        **summary,
        **({"status": summary_status, "validation_status": summary_status, "result_status": summary_status, "terminal": True, "can_resume": False} if validation_terminal_locked else {}),
    }
    view_bundle = {**bundle, "events": events, "state": effective_state, "summary": effective_summary}

    # v198.26.1: build/read snapshots must be read-only.  Loading a run used to
    # call the eval publisher and therefore rewrite eval_publish_status.json
    # on a simple GET.  Keep eval status as a view annotation only; an explicit
    # POST endpoint below owns publication side effects.
    eval_publish = _read_eval_publish_status_for_view(run_id, bundle, bucket_source=bucket_source, token=token)
    view_bundle["eval_publish"] = eval_publish
    view_bundle["eval_publish_status"] = eval_publish
    space_identity = _resolve_target_space_identity(run_id, view_bundle, bucket_source=bucket_source, job_url=effective_state.get("job_url") or effective_summary.get("job_url"))
    if space_identity.get("target_space"):
        effective_state.setdefault("target_space", space_identity["target_space"])
        effective_state.setdefault("target_space_id", space_identity["target_space"])
        effective_summary.setdefault("target_space", space_identity["target_space"])
        effective_summary.setdefault("target_space_id", space_identity["target_space"])
    if space_identity.get("target_space_url"):
        effective_state.setdefault("target_space_url", space_identity["target_space_url"])
        effective_summary.setdefault("target_space_url", space_identity["target_space_url"])
    view_bundle["state"] = effective_state
    view_bundle["summary"] = effective_summary
    view_bundle["space_identity"] = space_identity
    view_bundle["links"] = {**space_identity, "job_url": space_identity.get("job_url") or effective_state.get("job_url") or effective_summary.get("job_url") or ""}
    effective_run_status = compute_effective_run_status(view_bundle, build_status=effective_summary.get("status") or effective_state.get("status"))
    view_bundle["effective_run_status"] = effective_run_status
    effective_summary = {**effective_summary, "effective_run_status": effective_run_status, "display_status": effective_run_status.get("display_status"), "effective_status": effective_run_status.get("effective_status"), "effective_verdict": effective_run_status.get("effective_verdict")}
    view_bundle["summary"] = effective_summary
    view = build_run_view_model(run_id, view_bundle, bucket_source=bucket_source)

    return {
        "run_id": run_id,
        "bucket_source": bucket_source,
        "bundle": view_bundle,
        "summary": effective_summary,
        "state": effective_state,
        "events": events,
        "view": view,
        "eval_publish": eval_publish,
    }



def _read_eval_publish_status_for_view(run_id: str, bundle: dict[str, Any], *, bucket_source: str, token: str | None = None) -> dict[str, Any]:
    """Return eval publish metadata for UI reads without writing to the Bucket."""
    existing = bundle.get("eval_publish_status") or bundle.get("eval_publish") or {}
    if isinstance(existing, dict) and existing:
        return {**existing, "read_only_view": True}
    try:
        paths = RunPaths(run_id, bucket_source=bucket_source)
        persisted = read_json(f"{paths.root}/eval_publish_status.json", token=token) or {}
    except Exception:
        persisted = {}
    if isinstance(persisted, dict) and persisted:
        return {**persisted, "read_only_view": True}
    return {
        "schema_version": "eval_publish_status.virtual.v198_26_1",
        "publish_mode": "backend_read_only",
        "attempted": False,
        "published": False,
        "virtual": True,
        "read_only_view": True,
        "reason": "not_published_or_not_checked",
    }

def register_custom_routes(fastapi_app: FastAPI) -> None:
    """Register the root custom UI and OAuth-backed JSON endpoints."""

    @fastapi_app.get("/login/huggingface")
    async def oauth_login_compat():  # type: ignore[no-untyped-def]
        # Backward-compatible route for browsers that cached older ASF JS/HTML.
        # The canonical OAuth endpoint remains /oauth/huggingface/login.
        return RedirectResponse("/oauth/huggingface/login", status_code=307)

    @fastapi_app.get("/logout")
    async def oauth_logout_compat():  # type: ignore[no-untyped-def]
        # Backward-compatible route for cached clients. Keep it as a plain
        # redirect without custom target parameters to avoid OAuth loops.
        return RedirectResponse("/oauth/huggingface/logout", status_code=307)

    @fastapi_app.get("/auth/logout-local")
    async def auth_logout_local(request: Request):  # type: ignore[no-untyped-def]
        # Local reset must work even when the HF OAuth token is expired and the
        # official helper can no longer parse the session.
        _clear_local_oauth_session(request)
        response = RedirectResponse("/", status_code=303)
        response.delete_cookie("session")
        response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        return response

    @fastapi_app.get("/auth/refresh-login")
    async def auth_refresh_login(request: Request):  # type: ignore[no-untyped-def]
        # Force a clean re-authorization path. This avoids stale oauth_info
        # state after token expiry and should make Space duplication unnecessary.
        _clear_local_oauth_session(request)
        response = RedirectResponse("/oauth/huggingface/login", status_code=303)
        response.delete_cookie("session")
        response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        return response

    @fastapi_app.get("/auth/logout-and-refresh")
    async def auth_logout_and_refresh(request: Request):  # type: ignore[no-untyped-def]
        _clear_local_oauth_session(request)
        response = RedirectResponse("/auth/refresh-login", status_code=303)
        response.delete_cookie("session")
        response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        return response

    @fastapi_app.get("/", response_class=HTMLResponse)
    @fastapi_app.get("/custom", response_class=HTMLResponse)
    async def custom_index():  # type: ignore[no-untyped-def]
        index_path = WEB_DIR / "index.html"
        if not index_path.exists():
            raise HTTPException(status_code=404, detail="Custom UI index not found")
        return HTMLResponse(
            index_path.read_text(encoding="utf-8"),
            headers={"Cache-Control": "no-store, no-cache, must-revalidate", "Pragma": "no-cache"},
        )

    @fastapi_app.get("/custom-static/{asset_path:path}")
    async def custom_static(asset_path: str):  # type: ignore[no-untyped-def]
        path = (STATIC_DIR / asset_path).resolve()
        if STATIC_DIR.resolve() not in path.parents and path != STATIC_DIR.resolve():
            raise HTTPException(status_code=403, detail="Invalid asset path")
        if not path.exists() or not path.is_file():
            raise HTTPException(status_code=404, detail="Asset not found")
        return FileResponse(
            path,
            headers={"Cache-Control": "no-store, no-cache, must-revalidate", "Pragma": "no-cache"},
        )


    @fastapi_app.get("/api/app-info")
    async def api_app_info(request: Request):  # type: ignore[no-untyped-def]
        ctx: dict[str, Any] | None = None
        try:
            ctx = _oauth_context_from_request(request)
        except HTTPException:
            ctx = None
        return JSONResponse(
            {
                "name": "Agentic Space Factory",
                "version": settings.app_version,
                "release_name": ASF_RELEASE_NAME,
                "bucket_default": settings.bucket_name,
                "workflows": ["build_from_model_card", "validate_existing_space", "runs_explorer"],
                "custom_ui_status": "root_custom_ui",
                "user": {"username": ctx["username"], "missing_scopes": ctx.get("missing_scopes", []), "warnings": ctx.get("warnings", []), "auth_lifetime": ctx.get("auth_lifetime")} if ctx else None,
                "login_url": "/oauth/huggingface/login",
                "refresh_login_url": "/auth/refresh-login",
                "reset_auth_url": "/auth/logout-local",
                "logout_url": "/auth/logout-local",
                "anonymous_eval": public_eval_config(ctx["username"] if ctx else None),
            }
        )

    @fastapi_app.get("/api/me")
    async def api_me(request: Request):  # type: ignore[no-untyped-def]
        try:
            ctx = _oauth_context_from_request(request)
        except HTTPException as exc:
            payload = _auth_recovery_payload(str(exc.detail), str(exc.detail))
            payload["anonymous_eval"] = public_eval_config(None)
            return JSONResponse(payload, status_code=401)
        return JSONResponse(
            {
                "authenticated": True,
                "username": ctx["username"],
                "profile": {
                    "name": ctx["profile"].get("name"),
                    "preferred_username": ctx["profile"].get("preferred_username"),
                    "picture": ctx["profile"].get("picture"),
                    "is_pro": ctx.get("is_pro"),
                    "can_pay": ctx.get("can_pay"),
                },
                "scopes": ctx.get("scopes", []),
                "missing_scopes": ctx.get("missing_scopes", []),
                "warnings": ctx.get("warnings", []),
                "auth_lifetime": ctx.get("auth_lifetime"),
                "anonymous_eval": public_eval_config(ctx["username"] if ctx else None),
                "expires_at": ctx.get("expires_at"),
                "login_url": "/oauth/huggingface/login",
                "refresh_login_url": "/auth/refresh-login",
                "reset_url": "/auth/logout-local",
                "logout_url": "/auth/logout-local",
            }
        )


    @fastapi_app.get("/api/eval-archive/status")
    async def api_eval_archive_status(request: Request):  # type: ignore[no-untyped-def]
        # Public instance-level status: signed-out users should still see whether
        # the operator eval archive is enabled. Management permissions remain
        # computed only when an OAuth user is present.
        username = None
        try:
            ctx = _oauth_context_from_request(request)
            username = ctx.get("username")
        except HTTPException:
            username = None
        return JSONResponse(public_eval_config(username))

    @fastapi_app.post("/api/eval-archive/activate")
    async def api_eval_archive_activate(request: Request):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        payload = await request.json()
        try:
            cfg = activate_eval_archive_config(
                username=ctx["username"],
                token=ctx["token"],
                bucket_source=payload.get("bucket_source"),
                bucket_path=payload.get("bucket_path") or "evals",
                mount_path=payload.get("mount_path") or "/evals",
                include_redacted_tails=bool(payload.get("include_redacted_tails")),
                include_model_id=bool(payload.get("include_model_id")),
            )
        except PermissionError as exc:
            raise HTTPException(status_code=403, detail=str(exc)) from exc
        except FileNotFoundError as exc:
            raise HTTPException(status_code=409, detail=str(exc)) from exc
        except Exception as exc:  # noqa: BLE001
            raise HTTPException(status_code=400, detail=str(exc)) from exc
        return JSONResponse(cfg)

    @fastapi_app.post("/api/eval-archive/disable")
    async def api_eval_archive_disable(request: Request):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        try:
            cfg = disable_eval_archive_config(username=ctx["username"])
        except PermissionError as exc:
            raise HTTPException(status_code=403, detail=str(exc)) from exc
        except Exception as exc:  # noqa: BLE001
            raise HTTPException(status_code=400, detail=str(exc)) from exc
        return JSONResponse(cfg)

    @fastapi_app.post("/api/eval-archive/flush")
    async def api_eval_archive_flush(request: Request):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        try:
            cfg = flush_eval_archive_records(username=ctx["username"])
        except PermissionError as exc:
            raise HTTPException(status_code=403, detail=str(exc)) from exc
        except FileNotFoundError as exc:
            raise HTTPException(status_code=409, detail=str(exc)) from exc
        except Exception as exc:  # noqa: BLE001
            raise HTTPException(status_code=400, detail=str(exc)) from exc
        return JSONResponse(cfg)

    @fastapi_app.post("/api/runs/{run_id}/eval-publish")
    async def api_run_eval_publish(request: Request, run_id: str, bucket_name: str = settings.bucket_name):  # type: ignore[no-untyped-def]
        """Explicit eval publish action. GET/detail routes are intentionally read-only."""
        ctx = _oauth_context_from_request(request)
        run_id = validate_run_id(run_id)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        body: dict[str, Any] = {}
        try:
            payload = await request.json()
            body = payload if isinstance(payload, dict) else {}
        except Exception:
            body = {}
        bundle = read_run_bundle(run_id, bucket_source=bucket_source, token=ctx["token"], include_heavy=False)
        state = bundle.get("state") or bundle.get("summary") or {}
        try:
            result = maybe_publish_eval_record(run_id, bucket_source=bucket_source, token=ctx["token"], state=state, force=bool(body.get("force")))
        except Exception as exc:  # noqa: BLE001
            raise HTTPException(status_code=400, detail=redact(str(exc))) from exc
        return JSONResponse({"ok": True, "run_id": run_id, "bucket_source": bucket_source, "eval_publish": result, "eval_publish_status": result})

    @fastapi_app.get("/api/oauth/diagnostics")
    async def api_oauth_diagnostics(request: Request):  # type: ignore[no-untyped-def]
        """Public-safe OAuth diagnostics for iframe/sign-in troubleshooting.

        This endpoint never exposes secrets. It is intentionally readable while
        signed out so the UI/operator can tell whether OAuth routes and Space
        metadata-derived environment variables are present before login works.
        """
        env_status = {
            "space_id_present": bool(os.getenv("SPACE_ID")),
            "space_host_present": bool(os.getenv("SPACE_HOST")),
            "oauth_client_id_present": bool(os.getenv("OAUTH_CLIENT_ID")),
            "oauth_client_secret_present": bool(os.getenv("OAUTH_CLIENT_SECRET")),
            "oauth_scopes_present": bool(os.getenv("OAUTH_SCOPES")),
            "openid_provider_url_present": bool(os.getenv("OPENID_PROVIDER_URL")),
        }
        try:
            ctx = extract_oauth_context(request)
        except HTTPException as exc:
            return JSONResponse(
                {
                    "authenticated": False,
                    "reason": exc.detail,
                    "oauth_env": env_status,
                    "login_url": "/oauth/huggingface/login",
                    "refresh_login_url": "/auth/refresh-login",
                    "reset_url": "/auth/logout-local",
                    "logout_url": "/auth/logout-local",
                    "recommended_action": "refresh_sign_in" if "expired" in str(exc.detail).lower() else "sign_in",
                }
            )
        return JSONResponse(
            {
                "authenticated": True,
                "user": public_oauth_context(ctx),
                "token_identity": verify_token_identity(ctx),
                "oauth_env": env_status,
                "login_url": "/oauth/huggingface/login",
                "refresh_login_url": "/auth/refresh-login",
                "reset_url": "/auth/logout-local",
                "logout_url": "/auth/logout-local",
            }
        )

    @fastapi_app.get("/api/billing/status")
    async def api_billing_status(request: Request):  # type: ignore[no-untyped-def]
        """Return the billing signals available through HF OAuth.

        Hugging Face's public docs describe the billing dashboard as the place
        to monitor compute usage. The OAuth `read-billing` scope exposes whether
        the account can pay, but this app should not pretend it can mirror the
        dashboard's numeric usage totals.
        """
        ctx = _oauth_context_from_request(request)
        scopes = set(ctx.get("scopes", []))
        missing = set(ctx.get("missing_scopes", []))
        can_read_billing = "read-billing" in scopes and "read-billing" not in missing
        return JSONResponse(
            {
                "username": ctx["username"],
                "profile": {
                    "is_pro": ctx.get("is_pro"),
                    "can_pay": ctx.get("can_pay"),
                },
                "can_read_billing": can_read_billing,
                "can_pay": ctx.get("can_pay"),
                "is_pro": ctx.get("is_pro"),
                "missing_scopes": sorted(missing),
                "links": {
                    "billing": "https://huggingface.co/settings/billing",
                    "jobs_pricing": "https://huggingface.co/docs/hub/jobs-pricing",
                    "inference_pricing": "https://huggingface.co/docs/inference-providers/pricing",
                },
                "usage_totals_available": False,
                "note": "Compute usage totals are available in the Hugging Face Billing dashboard; this API only returns OAuth billing-readiness signals.",
            }
        )

    @fastapi_app.get("/api/bucket/status")
    async def api_bucket_status(request: Request, bucket_name: str = settings.bucket_name):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        return JSONResponse(check_user_bucket(username=ctx["username"], bucket_name=bucket_name, token=ctx["token"]))

    @fastapi_app.post("/api/bucket/create")
    async def api_bucket_create(request: Request, payload: dict[str, Any] | None = None):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        payload = payload or {}
        bucket_name = str(payload.get("bucket_name") or settings.bucket_name)
        return JSONResponse(create_user_bucket(username=ctx["username"], bucket_name=bucket_name, token=ctx["token"]))


    @fastapi_app.post("/api/models/pre-scan")
    async def api_model_pre_scan(request: Request, payload: dict[str, Any]):  # type: ignore[no-untyped-def]
        """Fast, metadata-only model-card pre-scan before launching paid Jobs."""
        ctx = _oauth_context_from_request(request)
        try:
            result = scan_model_card(payload.get("model_id") or payload.get("model_id_or_url"), token=ctx["token"])
        except Exception as exc:  # noqa: BLE001
            raise _json_error(exc) from exc
        return JSONResponse(result)

    @fastapi_app.post("/api/build")
    async def api_build(request: Request, payload: dict[str, Any]):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        bucket_name = payload.get("bucket_name") or settings.bucket_name
        bucket_status = check_user_bucket(username=ctx["username"], bucket_name=bucket_name, token=ctx["token"])
        if not bucket_status.get("ok"):
            raise HTTPException(status_code=409, detail=f"Run bucket is not ready: {redact(str(bucket_status.get('error') or bucket_status.get('bucket_source') or bucket_name))}. Create the private run bucket before launching a build.")
        try:
            result = launch_universal_model_card_job(
                token=ctx["token"],
                username=ctx["username"],
                target_slug=payload.get("target_space_name") or payload.get("target_slug"),
                model_id=payload.get("model_id") or payload.get("model_id_or_url"),
                pi_model=payload.get("pi_model"),
                preferred_space_hardware=payload.get("preferred_space_hardware"),
                fallback_space_hardware=payload.get("fallback_space_hardware"),
                allow_fixed_gpu_fallback=bool(payload.get("allow_fixed_gpu_fallback", True)),
                try_zero_gpu_first=bool(payload.get("try_zero_gpu_first", True)),
                implementation_mode=payload.get("implementation_mode"),
                expected_output_type=payload.get("expected_output_type"),
                run_id=payload.get("run_id"),
                bucket_name=bucket_name,
            )
        except Exception as exc:  # noqa: BLE001
            raise _json_error(exc) from exc
        result["job_url"] = _job_url_from_result(result, ctx["username"])
        result["created_by"] = ctx["username"]
        result["created_at"] = result.get("created_at") or utc_now_iso()
        result["links"] = _api_links(
            run_id=result.get("run_id"),
            bucket_source=result.get("bucket_source"),
            target_space=result.get("target_space"),
            job_url=result.get("job_url"),
        )
        try:
            write_launch_metadata(
                result["run_id"],
                bucket_source=result["bucket_source"],
                payload={**result, "status": "running", "created_by": ctx["username"]},
                token=ctx["token"],
            )
        except Exception:
            # Non-fatal: the worker will still write state.json once it starts.
            pass
        return JSONResponse(result)

    @fastapi_app.post("/api/validate")
    async def api_validate(request: Request, payload: dict[str, Any]):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        bucket_name = payload.get("bucket_name") or settings.bucket_name
        bucket_status = check_user_bucket(username=ctx["username"], bucket_name=bucket_name, token=ctx["token"])
        if not bucket_status.get("ok"):
            raise HTTPException(status_code=409, detail=f"Run bucket is not ready: {redact(str(bucket_status.get('error') or bucket_status.get('bucket_source') or bucket_name))}. Create the private run bucket before launching validation.")
        try:
            # Validate and normalize JSON early for clearer browser errors.
            test_args = json.loads(payload.get("test_args_json") or "[]")
            test_kwargs = json.loads(payload.get("test_kwargs_json") or "{}")
            if not isinstance(test_args, list):
                raise ValueError("test_args_json must be a JSON array; it is passed as positional args to gradio_client.predict().")
            if not isinstance(test_kwargs, dict):
                raise ValueError("test_kwargs_json must be a JSON object; it is passed as keyword args to gradio_client.predict().")
            parent_build_run_id = validate_run_id(str(payload.get("parent_build_run_id") or ""))
            parent_bundle = read_run_bundle(parent_build_run_id, bucket_source=bucket_status.get("bucket_source") or user_bucket_source(username=ctx["username"], bucket_name=bucket_name), token=ctx["token"], include_heavy=True)
            parent_summary = parent_bundle.get("summary") or {}
            parent_target = parent_summary.get("target_space") or (parent_bundle.get("state") or {}).get("target_space") or (parent_bundle.get("launch") or {}).get("target_space") or ""
            requested_target = str(payload.get("target_space_id") or "").strip()
            if not parent_target:
                raise ValueError("Linked Space Test requires a parent Build Run with a generated target Space.")
            if requested_target != parent_target:
                raise ValueError("Space Test must remain linked to the selected Build Run target Space. Standalone validation is not supported in this app.")
            parent_view = build_run_view_model(parent_build_run_id, parent_bundle, bucket_source=bucket_status.get("bucket_source") or user_bucket_source(username=ctx["username"], bucket_name=bucket_name))
            space_test_policy = parent_view.get("space_test_policy") or parent_view.get("space_test", {}).get("policy") or {}
            if not space_test_policy.get("enabled"):
                raise ValueError(str(space_test_policy.get("message") or "This Build Run is not eligible for linked Space Test yet."))
            api_name_for_validation = payload.get("api_name")
            replay_source: dict[str, Any] = {}
            if str(space_test_policy.get("mode") or payload.get("validation_mode") or "").strip().lower() == "replay":
                replay_source = _linked_replay_source_from_parent_bundle(
                    parent_build_run_id,
                    parent_bundle,
                    requested_target,
                    str(payload.get("expected_output_type") or "any"),
                )
                if replay_source:
                    api_name_for_validation = replay_source.get("api_name") or api_name_for_validation
                    test_args = replay_source.get("test_args") if isinstance(replay_source.get("test_args"), list) else test_args
                    test_kwargs = replay_source.get("test_kwargs") if isinstance(replay_source.get("test_kwargs"), dict) else test_kwargs
                else:
                    raise ValueError("Replay validation requires a successful parent automatic smoke payload. Select a Build Run with a completed smoke test, or use Complete/Recover validation instead of Replay.")
            if space_test_policy.get("requires_endpoint_discovery") and str(api_name_for_validation or "").strip() in {"", "/generate"} and not replay_source:
                api_name_for_validation = ""
            # Compatibility anchor: "ui_payload_source": payload.get("payload_source") or ""
            validation_launch_payload = {
                "schema_version": "1.0",
                "app_version": settings.app_version,
                "parent_build_run_id": parent_build_run_id,
                "target_space_id": requested_target,
                "api_name": api_name_for_validation or "",
                "expected_output_type": payload.get("expected_output_type") or replay_source.get("expected_output_type") or "any",
                "test_args": test_args,
                "test_kwargs": test_kwargs,
                "validation_mode": space_test_policy.get("mode") or payload.get("validation_mode") or "complete",
                "payload_source": "parent_automatic_smoke" if replay_source else (payload.get("payload_source") or "ui_payload"),
                "replay_source_present": bool(replay_source),
                "ui_payload_source": "parent_automatic_smoke" if replay_source else (payload.get("payload_source") or ""),
                "space_test_policy": {
                    "mode": space_test_policy.get("mode"),
                    "requires_endpoint_discovery": bool(space_test_policy.get("requires_endpoint_discovery")),
                    "effective_status_on_success": space_test_policy.get("effective_status_on_success"),
                },
            }
            result = launch_validate_existing_space_job(
                token=ctx["token"],
                username=ctx["username"],
                target_space_id=requested_target,
                parent_build_run_id=parent_build_run_id,
                api_name=api_name_for_validation,
                test_args_json=json.dumps(test_args, ensure_ascii=False),
                test_kwargs_json=json.dumps(test_kwargs, ensure_ascii=False),
                expected_output_type=validation_launch_payload["expected_output_type"],
                live_timeout_seconds=payload.get("live_timeout_seconds") or 1800,
                validation_mode=validation_launch_payload["validation_mode"],  # effectively: validation_mode=space_test_policy.get("mode")
                effective_status_on_success=space_test_policy.get("effective_status_on_success") or "validated_after_manual_space_test",
                parent_replay_source_json=json.dumps(replay_source, ensure_ascii=False) if replay_source else None,
                validation_launch_payload_json=json.dumps(validation_launch_payload, ensure_ascii=False),
                run_id=payload.get("run_id"),
                bucket_name=bucket_name,
            )
        except Exception as exc:  # noqa: BLE001
            raise _json_error(exc) from exc
        result["job_url"] = _job_url_from_result(result, ctx["username"])
        result["created_by"] = ctx["username"]
        result["created_at"] = result.get("created_at") or utc_now_iso()
        result["links"] = _api_links(
            run_id=result.get("run_id"),
            bucket_source=result.get("bucket_source"),
            target_space=result.get("target_space"),
            job_url=result.get("job_url"),
        )
        try:
            write_launch_metadata(
                result["run_id"],
                bucket_source=result["bucket_source"],
                payload={**result, "status": "running", "created_by": ctx["username"]},
                token=ctx["token"],
            )
        except Exception:
            # Non-fatal: the worker will still write state.json once it starts.
            pass
        return JSONResponse(result)

    @fastapi_app.post("/api/progress/from-events")
    async def api_progress_from_events(payload: dict[str, Any]):  # type: ignore[no-untyped-def]
        events = payload.get("events") or []
        state = payload.get("state") or {}
        if not isinstance(events, list):
            raise HTTPException(status_code=400, detail="events must be a list")
        if not isinstance(state, dict):
            raise HTTPException(status_code=400, detail="state must be an object")
        return JSONResponse(progress_from_events(events, state=state))

    @fastapi_app.get("/api/runs")
    async def api_runs(  # type: ignore[no-untyped-def]
        request: Request,
        bucket_name: str = settings.bucket_name,
        limit: int = 50,
        query: str | None = None,
        status: str | None = None,
    ):
        ctx = _oauth_context_from_request(request)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        return JSONResponse(
            {
                "runs": list_recent_runs(bucket_source=bucket_source, token=ctx["token"], limit=limit, query=query, status=status),
                "bucket_source": bucket_source,
                "bucket_uri": f"hf://buckets/{bucket_source}",
                "limit": limit,
            }
        )

    @fastapi_app.get("/api/runs/resumable")
    async def api_resumable_run(request: Request, bucket_name: str = settings.bucket_name, limit: int = 100):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        runs = list_recent_runs(bucket_source=bucket_source, token=ctx["token"], limit=limit)
        resumable = find_latest_resumable_run(runs)
        selected = resumable or (runs[0] if runs else None)
        if not selected:
            return JSONResponse({"run": None, "view": None, "bucket_source": bucket_source, "reason": "no_run"})
        run_id = validate_run_id(str(selected.get("run_id")))
        snapshot = _build_live_run_snapshot(run_id, bucket_source=bucket_source, token=ctx["token"], include_heavy=False)
        return JSONResponse({"run": selected, "view": snapshot["view"], "bucket_source": bucket_source, "reason": "latest_resumable_run" if resumable else "latest_run"})

    @fastapi_app.get("/api/runs/{run_id}")
    async def api_run_detail(request: Request, run_id: str, bucket_name: str = settings.bucket_name, include_heavy: bool = True):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        run_id = validate_run_id(run_id)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        snapshot = _build_live_run_snapshot(run_id, bucket_source=bucket_source, token=ctx["token"], include_heavy=include_heavy)
        bundle = snapshot["bundle"]
        return JSONResponse({"run_id": run_id, "bucket_source": bucket_source, "view": snapshot["view"], "eval_publish": snapshot["eval_publish"], **bundle})

    @fastapi_app.get("/api/runs/{run_id}/view")
    async def api_run_view(request: Request, run_id: str, bucket_name: str = settings.bucket_name):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        run_id = validate_run_id(run_id)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        snapshot = _build_live_run_snapshot(run_id, bucket_source=bucket_source, token=ctx["token"], include_heavy=False)
        return JSONResponse({**snapshot["view"], "summary": snapshot["summary"], "state": snapshot["state"], "events": snapshot["events"], "events_recent": snapshot["events"][-100:], "events_count": len(snapshot["events"]), "eval_publish": snapshot["eval_publish"], "final_status_reconciliation": snapshot["bundle"].get("final_status_reconciliation") or {}, "eval_record": snapshot["bundle"].get("eval_record") or {}, "validation_metrics": snapshot["view"].get("validation_metrics") or {}, "run_documents": snapshot["view"].get("run_documents") or snapshot["bundle"].get("run_documents") or [], "space_link_state": snapshot["view"].get("space_link_state") or {}, "runtime_stage_history": snapshot["view"].get("runtime_stage_history") or {}, "space_identity": snapshot["bundle"].get("space_identity") or {}, "links": snapshot["view"].get("links") or {}})

    @fastapi_app.post("/api/runs/{run_id}/cancel")
    async def api_cancel_run(request: Request, run_id: str, bucket_name: str = settings.bucket_name):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        run_id = validate_run_id(run_id)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        bundle = read_run_bundle(run_id, bucket_source=bucket_source, token=ctx["token"])
        summary = bundle.get("summary") or {}
        launch = bundle.get("launch") or {}
        state = bundle.get("state") or {}
        job_id = summary.get("job_id") or launch.get("job_id") or state.get("job_id")
        if not job_id:
            raise HTTPException(status_code=404, detail="No job_id found for this run.")
        result = cancel_job_safe(str(job_id), namespace=ctx["username"], token=ctx["token"])
        if not result.get("ok"):
            raise HTTPException(status_code=400, detail=redact(str(result.get("error") or "Could not cancel Job.")))
        paths = RunPaths(run_id, bucket_source=bucket_source)
        cancelled_at = utc_now_iso()
        job_url = summary.get("job_url") or launch.get("job_url") or state.get("job_url")
        cancel_meta = {
            "requested_by": ctx["username"],
            "cancelled_at": cancelled_at,
            "job_id": str(job_id),
            "job_url": job_url or "",
            "hf_cancel_result": result,
        }
        cancelled_state = {
            **launch,
            **state,
            "run_id": run_id,
            "status": "cancelled",
            "job_id": str(job_id),
            "job_url": job_url,
            "cancel_requested": True,
            "cancelled_at": cancelled_at,
            "updated_at": cancelled_at,
            "cancel": cancel_meta,
        }
        cancelled_summary = {
            **summary,
            "run_id": run_id,
            "status": "cancelled",
            "job_id": str(job_id),
            "job_url": job_url or summary.get("job_url") or "",
            "cancel_requested": True,
            "cancelled_at": cancelled_at,
            "updated_at": cancelled_at,
        }
        try:
            write_json(paths.state, cancelled_state, token=ctx["token"])
            write_json(f"{paths.root}/summary.json", cancelled_summary, token=ctx["token"])
            write_json(f"{paths.root}/cancel.json", cancel_meta, token=ctx["token"])
            append_run_event(
                run_id,
                bucket_source=bucket_source,
                step="cancel",
                status="cancelled",
                message="Job cancellation requested from Agentic Space Factory UI",
                details=cancel_meta,
                token=ctx["token"],
            )
        except Exception:
            pass
        return JSONResponse({"ok": True, "run_id": run_id, "job_id": str(job_id), "status": "cancelled", "cancelled_at": cancelled_at})

    @fastapi_app.delete("/api/runs/{run_id}")
    async def api_delete_run(request: Request, run_id: str, bucket_name: str = settings.bucket_name):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        run_id = validate_run_id(run_id)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)
        body: dict[str, Any] = {}
        try:
            body = await request.json()
            if not isinstance(body, dict):
                body = {}
        except Exception:
            body = {}

        delete_space = bool(body.get("delete_space"))
        bundle: dict[str, Any] = {}
        try:
            bundle = read_run_bundle(run_id, bucket_source=bucket_source, token=ctx["token"], include_heavy=False)
        except Exception:
            bundle = {}

        is_validation = _run_is_validation_like({**bundle, "run_id": run_id})
        linked_children: list[dict[str, Any]] = []
        if not is_validation:
            linked_children = _linked_validation_summaries(run_id, bucket_source=bucket_source, token=ctx["token"])

        space_report: dict[str, Any] = {"space_delete_requested": delete_space, "space_deleted": False}
        if delete_space:
            try:
                if is_validation:
                    raise PermissionError("Associated Space deletion is not available for validation runs.")
                associated_space = _associated_space_from_bundle(bundle)
                space_report = _delete_associated_space(associated_space, username=ctx["username"], token=ctx["token"])
            except Exception as exc:  # noqa: BLE001
                space_report = {"space_delete_requested": True, "space_deleted": False, "space_delete_error": redact(str(exc))}

        deleted_linked_validations: list[str] = []
        linked_delete_errors: list[str] = []
        for child in linked_children:
            child_id = str(child.get("run_id") or "").strip()
            if not child_id or child_id == run_id:
                continue
            try:
                delete_run_folder(validate_run_id(child_id), bucket_source=bucket_source, token=ctx["token"])
                deleted_linked_validations.append(child_id)
            except FileNotFoundError:
                deleted_linked_validations.append(child_id)
            except Exception as exc:  # noqa: BLE001
                linked_delete_errors.append(f"{child_id}: {redact(str(exc))}")

        parent_update: dict[str, Any] = {}
        parent_build_run_id = _linked_validation_parent_id({**bundle, "run_id": run_id}) if is_validation else ""
        try:
            delete_report = delete_run_folder(run_id, bucket_source=bucket_source, token=ctx["token"])
        except FileNotFoundError:
            delete_report = {"matched_count": 0, "deleted_count": 0, "remaining_count": 0}
        except Exception as exc:  # noqa: BLE001
            raise HTTPException(status_code=400, detail=redact(str(exc))) from exc

        if is_validation and parent_build_run_id:
            remaining = [row for row in _linked_validation_summaries(parent_build_run_id, bucket_source=bucket_source, token=ctx["token"]) if str(row.get("run_id") or "") != run_id]
            try:
                parent_update = _write_parent_linked_validation_state(parent_build_run_id, remaining, bucket_source=bucket_source, token=ctx["token"])
            except Exception as exc:  # noqa: BLE001
                parent_update = {"parent_update_error": redact(str(exc))}

        response = {
            "ok": True,
            "run_id": run_id,
            "deleted": True,
            **delete_report,
            **space_report,
            "deleted_linked_validations": deleted_linked_validations,
            "linked_delete_errors": linked_delete_errors,
            "parent_build_run_id": parent_build_run_id,
            "parent_update": parent_update,
        }
        if linked_delete_errors:
            response["partial_delete"] = True
        return JSONResponse(response)

    @fastapi_app.get("/api/runs/{run_id}/progress")
    async def api_run_progress(request: Request, run_id: str, bucket_name: str = settings.bucket_name, include_job_logs: bool = False):  # type: ignore[no-untyped-def]
        ctx = _oauth_context_from_request(request)
        run_id = validate_run_id(run_id)
        bucket_source = user_bucket_source(username=ctx["username"], bucket_name=bucket_name)

        probe = read_run_bundle(run_id, bucket_source=bucket_source, token=ctx["token"], include_heavy=False)
        state = probe.get("state") or {}
        launch = probe.get("launch") or {}
        summary = probe.get("summary") or {}
        job_id = _job_id_from_run_bundle(summary, launch, state)
        job_info: dict[str, Any] = {}
        job_logs = ""
        job_log_events: list[dict[str, Any]] = []
        if job_id and include_job_logs:
            job_info = inspect_job_safe(job_id, token=ctx["token"])
            job_logs = fetch_recent_logs_safe(job_id, token=ctx["token"], max_lines=1000)
            job_log_events = _events_from_job_logs(job_logs)

        # validation_terminal_locked is resolved inside _build_live_run_snapshot so
        # never downgrade a terminal validation
        # effective_state["status"] = summary_status
        # effective_state["validation_status"] = summary_status
        # effective_state["result_status"] = summary_status
        # effective_state["terminal"] = True
        # effective_state["can_resume"] = False
        # progress["status"] = summary_status
        # progress["validation_status"] = summary_status
        # progress["result_status"] = summary_status
        # 
        # progress polling cannot downgrade terminal validation runs.
        # Compatibility invariant: view = build_run_view_model(run_id, bundle, bucket_source=bucket_source)
        snapshot = _build_live_run_snapshot(
            run_id,
            bucket_source=bucket_source,
            token=ctx["token"],
            include_heavy=False,
            job_info=job_info,
            job_log_events=job_log_events,
        )
        effective_state = snapshot["state"]
        effective_summary = snapshot["summary"]
        events = snapshot["events"]
        view = snapshot["view"]
        bundle = snapshot["bundle"]
        progress = progress_from_events(events, state=effective_state)
        model_progress = ((view.get("timeline_model") or {}).get("progress") or {}) if isinstance(view, dict) else {}
        if model_progress.get("terminal") is True:
            progress["terminal"] = True
            progress["status"] = model_progress.get("verdict") or view.get("header", {}).get("status") or progress.get("status")
            progress["visual_status"] = model_progress.get("visual_status") or progress.get("visual_status")
            progress["progress"] = 100

        progress.update(
            {
                "run_id": run_id,
                "bucket_source": bucket_source,
                "state": effective_state,
                "summary": effective_summary,
                "job_id": job_id,
                "job_info": job_info,
                "job_log_events_count": len(job_log_events),
                "job_logs_polled": bool(include_job_logs),
                "inference_gate": bundle.get("inference_gate") or {},
                "generation_smoke": bundle.get("generation_smoke") or {},
                "final_status_reconciliation": bundle.get("final_status_reconciliation") or {},
                "eval_record": bundle.get("eval_record") or {},
                "api_schema": bundle.get("api_schema") or {},
                "validation_payload": bundle.get("validation_payload") or {},
                "validation_failure_diagnosis": bundle.get("validation_failure_diagnosis") or {},
                "payload_source": bundle.get("payload_source") or {},
                "validation_engine": bundle.get("validation_engine") or {},
                "resolved_validation_request": bundle.get("resolved_validation_request") or {},
                "hardware_strategy": bundle.get("hardware_strategy") or {},
                "technical_blockers": bundle.get("technical_blockers") or {},
                "repair_decision": bundle.get("repair_decision") or {},
                "blockage": bundle.get("blockage") or {},
                "run_documents": view.get("run_documents") or bundle.get("run_documents") or [],
                "validation_metrics": view.get("validation_metrics") or {},
                "post_build_validation_status": bundle.get("post_build_validation_status") or {},
                "manual_validation_status": bundle.get("manual_validation_status") or {},
                "linked_validations": bundle.get("linked_validations") or {},
                "events": events[-100:],
                "events_recent": events[-100:],
                "events_count": len(events),
                "view": view,
                "eval_publish": snapshot["eval_publish"],
                "eval_publish_status": snapshot["eval_publish"],
                "links": view.get("links") or bundle.get("links") or _api_links(
                    run_id=run_id,
                    bucket_source=bucket_source,
                    target_space=effective_state.get("target_space") or effective_summary.get("target_space"),
                    job_url=effective_state.get("job_url") or effective_summary.get("job_url"),
                ),
                "space_link_state": view.get("space_link_state") or {},
                "runtime_stage_history": view.get("runtime_stage_history") or {},
                "space_identity": bundle.get("space_identity") or {},
            }
        )
        return JSONResponse(progress, headers={"Cache-Control": "no-store, no-cache, must-revalidate", "Pragma": "no-cache"})

def _profile_username(profile: Any) -> str | None:
    if profile is None:
        return None
    if isinstance(profile, dict):
        return profile.get("preferred_username") or profile.get("username") or profile.get("name")
    return getattr(profile, "preferred_username", None) or getattr(profile, "username", None) or getattr(profile, "name", None)


def _token_value(oauth_token: Any) -> str | None:
    if oauth_token is None:
        return None
    if isinstance(oauth_token, str):
        return oauth_token
    return getattr(oauth_token, "token", None) or getattr(oauth_token, "access_token", None)


def get_login_status(profile: gr.OAuthProfile | None) -> str:
    username = _profile_username(profile)
    if not username:
        return "Not signed in. Use the Hugging Face login button before launching a Job."
    return f"Signed in as **{username}**. Generated Spaces are created under `{username}/...` and remain private."




def _safe_url(url: str | None) -> str:
    return (url or "").strip()


def _run_artifacts_url(run_id: str | None, bucket_source: str | None) -> str:
    if not run_id or not bucket_source:
        return ""
    prefix = settings.bucket_runs_prefix.strip().strip("/") or "runs"
    return f"https://huggingface.co/buckets/{bucket_source}/tree/{prefix}/{run_id}"


def _button_link(label: str, url: str | None):
    url = _safe_url(url)
    return gr.update(value=label, link=url or None, visible=bool(url))


def _job_button(job_url: str | None):
    return _button_link("Open HF Job β†—", job_url)


def _space_button(target_space_url: str | None):
    return _button_link("Open target Space β†—", target_space_url)


def _settings_button(target_space_url: str | None):
    target_space_url = _safe_url(target_space_url)
    return _button_link("Open Space settings β†—", f"{target_space_url}/settings" if target_space_url else "")


def _artifacts_button(run_id: str | None, bucket_source: str | None):
    return _button_link("Open run artifacts β†—", _run_artifacts_url(run_id, bucket_source))




def _format_bucket_status(status: dict[str, Any]) -> str:
    source = status.get("bucket_source") or "unknown"
    uri = status.get("bucket_uri") or ""
    if status.get("ok"):
        return (
            f"βœ… Run bucket ready: `{source}`\n\n"
            f"Bucket URI: `{uri}`\n\n"
            "New Jobs will mount this private bucket and write runs under `runs/<run_id>/`."
        )
    if status.get("exists") is False:
        return (
            f"⚠️ Run bucket not found: `{source}`\n\n"
            "Click **Create private run bucket** before launching a Job, or create it manually in Hugging Face Storage Buckets."
        )
    return (
        f"❌ Could not check run bucket: `{source}`\n\n"
        f"```text\n{redact(str(status.get('error') or 'Unknown error'))}\n```"
    )


def check_run_bucket_ui(
    bucket_name: str,
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None,
) -> str:
    username = _profile_username(profile)
    token = _token_value(oauth_token)
    if not username or not token:
        raise gr.Error("Please sign in with Hugging Face first.")
    return _format_bucket_status(check_user_bucket(username=username, bucket_name=bucket_name, token=token))


def create_run_bucket_ui(
    bucket_name: str,
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None,
) -> str:
    username = _profile_username(profile)
    token = _token_value(oauth_token)
    if not username or not token:
        raise gr.Error("Please sign in with Hugging Face first.")
    return _format_bucket_status(create_user_bucket(username=username, bucket_name=bucket_name, token=token))


def propose_universal_run_id() -> str:
    return make_run_id("universal")


def propose_validate_run_id() -> str:
    return make_run_id("validate")


def launch_universal_model_card_job_ui(
    requested_run_id: str,
    model_id: str,
    target_space_name: str,
    pi_model: str,
    preferred_hardware: str,
    allow_fixed_gpu_fallback: bool,
    try_zero_gpu_first: bool,
    fallback_hardware: str,
    implementation_mode: str,
    bucket_name: str,
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None,
) -> tuple[str, str, str, str, str, Any, Any, Any, Any, str]:
    username = _profile_username(profile)
    token = _token_value(oauth_token)
    if not username or not token:
        raise gr.Error("Please sign in with Hugging Face first. OAuth profile/token is missing.")

    run_id = validate_run_id(requested_run_id or propose_universal_run_id())
    result = launch_universal_model_card_job(
        token=token,
        username=username,
        target_slug=target_space_name,
        model_id=model_id,
        pi_model=pi_model,
        preferred_space_hardware=preferred_hardware,
        fallback_space_hardware=fallback_hardware,
        allow_fixed_gpu_fallback=allow_fixed_gpu_fallback,
        try_zero_gpu_first=try_zero_gpu_first,
        implementation_mode=implementation_mode,
        run_id=run_id,
        bucket_name=bucket_name,
    )
    job_url = result.get("job_url") or ""
    target_space_url = result.get("target_space_url") or ""
    bucket_source = result.get("bucket_source") or user_bucket_source(username=username, bucket_name=bucket_name)
    return (
        run_id,
        result["job_id"],
        job_url,
        result.get("target_space") or "",
        target_space_url,
        _job_button(job_url),
        _space_button(target_space_url),
        _settings_button(target_space_url),
        _artifacts_button(run_id, bucket_source),
        json.dumps(result, indent=2),
    )


def launch_validate_existing_space_job_ui(
    requested_run_id: str,
    target_space_id: str,
    api_name: str,
    test_args_json: str,
    test_kwargs_json: str,
    expected_output_type: str,
    live_timeout_seconds: float,
    bucket_name: str,
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None,
) -> tuple[str, str, str, str, Any, Any, Any, Any, str]:
    # Linked-only guardrails supersede the legacy Gradio path; keep these markers
    # so release tests prove the old path is not less strict than /api/validate:
    # ensure_validation_payload_json_fits_env, test_args_json must be a JSON array,
    # test_kwargs_json must be a JSON object, clamp_validation_timeout_seconds.
    raise gr.Error(
        "Standalone Space validation is disabled. Open a completed Build Run in the custom UI, "
        "then use its linked Space Test panel."
    )

def refresh_run_ui(
    run_id: str,
    job_id: str,
    bucket_name: str,
    profile: gr.OAuthProfile | None,
    oauth_token: gr.OAuthToken | None,
) -> tuple[str, str, str, str]:
    username = _profile_username(profile)
    token = _token_value(oauth_token)
    if not username or not token:
        raise gr.Error("Please sign in with Hugging Face first.")
    run_id = validate_run_id(run_id)
    bucket_source = user_bucket_source(username=username, bucket_name=bucket_name)

    bundle = read_run_bundle(run_id, bucket_source=bucket_source, token=token)
    job_info = inspect_job_safe(job_id, token=token) if job_id else {}
    logs = redact(fetch_recent_logs_safe(job_id, token=token)) if job_id else ""

    state_text = json.dumps(bundle.get("state") or {"status": "not_available_yet"}, indent=2, ensure_ascii=False)
    events = bundle.get("events") or []
    events_text = "\n".join(json.dumps(event, ensure_ascii=False) for event in events) or "No events found yet. The Job may still be scheduling."
    report_text = bundle.get("report") or "No report found yet. Refresh after the Job has started writing to the Bucket."
    job_text = json.dumps(job_info, indent=2, ensure_ascii=False)
    if logs:
        job_text += "\n\nRecent job logs:\n" + logs
    return state_text, events_text, report_text, job_text


def build_demo() -> gr.Blocks:
    with gr.Blocks(title="Agentic Space Factory") as demo:
        gr.Markdown(APP_DESCRIPTION)
        gr.LoginButton()

        login_status = gr.Markdown()
        demo.load(fn=get_login_status, inputs=None, outputs=login_status)


        gr.Markdown("## Run storage")
        gr.Markdown(
            "Runs are stored in a private Storage Bucket under the signed-in user's namespace. "
            "Create it once here, then use the same bucket name for Build and Validate."
        )
        global_bucket_name = gr.Textbox(
            label="Run Bucket name",
            value=settings.bucket_name,
            info="The app uses <your-username>/<bucket-name>. Default: space-factory-runs.",
        )
        with gr.Row():
            check_bucket_btn = gr.Button("Check run bucket")
            create_bucket_btn = gr.Button("Create private run bucket", variant="primary")
        bucket_status = gr.Markdown("Sign in, then check or create your private run bucket before launching Jobs.")
        check_bucket_btn.click(fn=check_run_bucket_ui, inputs=[global_bucket_name], outputs=bucket_status)
        create_bucket_btn.click(fn=create_run_bucket_ui, inputs=[global_bucket_name], outputs=bucket_status)

        with gr.Tab("Build from model card"):
            gr.Markdown(
                """
Paste a Hugging Face model ID or model-card URL. The worker creates a **private** Space, asks Pi + the selected coding assistant to build the best Gradio app it can, attempts ZeroGPU first, then a fixed-GPU fallback if enabled. If automatic hardware assignment fails, set the hardware manually in the generated Space settings and run the validation tab.
"""
            )
            with gr.Row():
                build_run_id = gr.Textbox(label="Run ID", value=propose_universal_run_id, interactive=True)
                gr.Button("Generate new run id").click(fn=propose_universal_run_id, inputs=None, outputs=build_run_id)
            model_id = gr.Textbox(
                label="Model card URL or model ID",
                value="Tongyi-MAI/Z-Image-Turbo",
                info="Examples: owner/model, https://huggingface.co/owner/model",
            )
            target_space_name = gr.Textbox(
                label="Target Space name",
                placeholder="e.g. space-factory-z-image-v1",
                info="Use a fresh name. The Space is created under your username and remains private.",
            )
            pi_model = gr.Dropdown(
                label="Pi model",
                choices=[
                    "Qwen/Qwen3-Coder-Next",
                    "moonshotai/Kimi-K2-Instruct-0905",
                    "zai-org/GLM-4.7",
                    "zai-org/GLM-4.5-Air",
                    "deepseek-ai/DeepSeek-V3.2",
                    "Qwen/Qwen3-Coder-480B-A35B-Instruct",
                ],
                value="Qwen/Qwen3-Coder-Next",
                allow_custom_value=True,
                info="Assistant model used by Pi through Hugging Face Inference Providers.",
            )
            implementation_mode = gr.Dropdown(
                label="Build goal",
                choices=["full-inference-gated", "full-inference-attempt", "safe-scaffold"],
                value="full-inference-gated",
                info="Real inference required: no fake placeholder success. Impossible models must produce technical blockers.",
            )
            with gr.Row():
                preferred_hw = gr.Dropdown(
                    label="Preferred Space hardware",
                    choices=["zero-a10g", "cpu-basic", "t4-small", "t4-medium", "a10g-large", "l40sx1"],
                    value="zero-a10g",
                    info="ZeroGPU is attempted first. Automatic fallback avoids high/restricted tiers such as A100/H200; select them manually in Space Settings only if your account is allowed.",
                )
                try_zero_gpu_first = gr.Checkbox(label="Try ZeroGPU first", value=True)
                allow_fallback = gr.Checkbox(label="Allow fixed GPU fallback", value=True)
                fallback_hw = gr.Dropdown(
                    label="Fallback Space hardware",
                    choices=["a10g-large", "l40sx1", "t4-medium", "cpu-basic"],
                    value="a10g-large",
                )

            build_btn = gr.Button("Build private Space", variant="primary")
            build_job_id = gr.Textbox(label="Job ID", interactive=True)
            build_job_url = gr.Textbox(label="Job URL", interactive=False)
            generated_space = gr.Textbox(label="Generated Space", interactive=False)
            generated_space_url = gr.Textbox(label="Generated Space URL", interactive=False)
            gr.Markdown("Quick links")
            with gr.Row():
                build_job_button = gr.Button("Open HF Job β†—", link=None, link_target="_blank", visible=False)
                build_space_button = gr.Button("Open target Space β†—", link=None, link_target="_blank", visible=False)
                build_settings_button = gr.Button("Open Space settings β†—", link=None, link_target="_blank", visible=False)
                build_artifacts_button = gr.Button("Open run artifacts β†—", link=None, link_target="_blank", visible=False)
            build_result = gr.Code(label="Launch result", language="json")

            build_btn.click(
                fn=launch_universal_model_card_job_ui,
                inputs=[build_run_id, model_id, target_space_name, pi_model, preferred_hw, allow_fallback, try_zero_gpu_first, fallback_hw, implementation_mode, global_bucket_name],
                outputs=[
                    build_run_id,
                    build_job_id,
                    build_job_url,
                    generated_space,
                    generated_space_url,
                    build_job_button,
                    build_space_button,
                    build_settings_button,
                    build_artifacts_button,
                    build_result,
                ],
            )

            build_refresh = gr.Button("Refresh build run status")
            with gr.Tab("Build state"):
                build_state = gr.Code(label="state.json", language="json")
            with gr.Tab("Build events"):
                build_events = gr.Code(label="events.jsonl", language="json")
            with gr.Tab("Build report"):
                build_report = gr.Markdown()
            with gr.Tab("Build job"):
                build_job_info = gr.Code(label="Job info/logs", language="json")

            build_refresh.click(fn=refresh_run_ui, inputs=[build_run_id, build_job_id, global_bucket_name], outputs=[build_state, build_events, build_report, build_job_info])

        with gr.Tab("Validate existing Space"):
            gr.Markdown(
                """
Use this after the builder generated a Space, especially if you had to set the GPU manually. This job does not rerun Pi. It waits for the existing Space, calls a live generation endpoint, checks the output type, stores returned artifacts in the Bucket, measures latency, and recommends a conservative ZeroGPU duration.
"""
            )
            with gr.Row():
                validate_run_id = gr.Textbox(label="Run ID", value=propose_validate_run_id, interactive=True)
                gr.Button("Generate new validation run id").click(fn=propose_validate_run_id, inputs=None, outputs=validate_run_id)
            target_space = gr.Textbox(
                label="Existing target Space",
                placeholder="fffiloni/space-factory-... or https://huggingface.co/spaces/...",
            )
            with gr.Row():
                api_name = gr.Textbox(label="Generation API name", value="/generate")
                expected_type = gr.Dropdown(label="Expected output type", choices=["image", "video", "audio", "text", "any"], value="image")
            test_args = gr.Code(label="Test args JSON list", language="json", value='["a cinematic robot cat astronaut, detailed, studio lighting"]')
            test_kwargs = gr.Code(label="Test kwargs JSON object", language="json", value="{}")
            timeout_s = gr.Number(label="Live wait timeout seconds", value=1800, precision=0)

            validate_btn = gr.Button("Validate Space + smoke-test generation", variant="primary")
            validate_job_id = gr.Textbox(label="Job ID", interactive=True)
            validate_job_url = gr.Textbox(label="Job URL", interactive=False)
            validate_space_url = gr.Textbox(label="Target Space URL", interactive=False)
            gr.Markdown("Quick links")
            with gr.Row():
                validate_job_button = gr.Button("Open HF Job β†—", link=None, link_target="_blank", visible=False)
                validate_space_button = gr.Button("Open target Space β†—", link=None, link_target="_blank", visible=False)
                validate_settings_button = gr.Button("Open Space settings β†—", link=None, link_target="_blank", visible=False)
                validate_artifacts_button = gr.Button("Open run artifacts β†—", link=None, link_target="_blank", visible=False)
            validate_result = gr.Code(label="Launch result", language="json")

            validate_btn.click(
                fn=launch_validate_existing_space_job_ui,
                inputs=[validate_run_id, target_space, api_name, test_args, test_kwargs, expected_type, timeout_s, global_bucket_name],
                outputs=[
                    validate_run_id,
                    validate_job_id,
                    validate_job_url,
                    validate_space_url,
                    validate_job_button,
                    validate_space_button,
                    validate_settings_button,
                    validate_artifacts_button,
                    validate_result,
                ],
            )

            validate_refresh = gr.Button("Refresh validation run status")
            with gr.Tab("Validation state"):
                validate_state = gr.Code(label="state.json", language="json")
            with gr.Tab("Validation events"):
                validate_events = gr.Code(label="events.jsonl", language="json")
            with gr.Tab("Validation report"):
                validate_report = gr.Markdown()
            with gr.Tab("Validation job"):
                validate_job_info = gr.Code(label="Job info/logs", language="json")

            validate_refresh.click(fn=refresh_run_ui, inputs=[validate_run_id, validate_job_id, global_bucket_name], outputs=[validate_state, validate_events, validate_report, validate_job_info])

        with gr.Tab("About & limits"):
            gr.Markdown(
                """
## Result statuses

- `full_inference_success`: a live generation smoke test returned the expected output type.
- `manual_hardware_required`: the Space was generated but automatic ZeroGPU/fixed-GPU assignment failed; set hardware manually, then validate.
- `full_inference_candidate_health_passed`: the Space boots and contains inference signals, but generation was not smoke-tested yet.
- `health_only`: the Space boots, but no real inference path was validated.
- `technical_blocker`: the agent found concrete blockers such as multi-GPU requirements, missing licenses, custom CUDA, or unclear usage.
- `failed`: the build, runtime, or validation job failed.

## Hardware policy

The builder tries to create an app optimized for ZeroGPU when GPU is needed. It attempts ZeroGPU first, then a fixed-GPU fallback if enabled. Hardware assignment through OAuth may fail because of quota, billing, or permission limits; manual hardware selection is a supported path.

## What this app cannot guarantee

It cannot guarantee that every model card becomes a working Space. It cannot bypass model licenses, ZeroGPU quota, billing requirements, custom CUDA build failures, multi-GPU needs, or missing model documentation.
"""
            )

    return demo



def _first_non_empty(*values: Any) -> str:
    for value in values:
        text = str(value or "").strip()
        if text:
            return text
    return ""


def _associated_space_from_bundle(bundle: dict[str, Any]) -> str:
    summary = bundle.get("summary") or {}
    summary_file = bundle.get("summary_file") or {}
    launch = bundle.get("launch") or {}
    state = bundle.get("state") or {}
    links = bundle.get("links") or {}
    target = _first_non_empty(
        summary.get("target_space"),
        summary_file.get("target_space"),
        launch.get("target_space"),
        state.get("target_space"),
        links.get("target_space"),
    )
    if target:
        return target.replace("https://huggingface.co/spaces/", "").strip("/")
    url = _first_non_empty(summary.get("target_space_url"), summary_file.get("target_space_url"), launch.get("target_space_url"), state.get("target_space_url"), links.get("target_space_url"))
    marker = "huggingface.co/spaces/"
    if marker in url:
        return url.split(marker, 1)[1].split("?", 1)[0].split("#", 1)[0].strip("/")
    return ""


def _delete_associated_space(space_id: str, *, username: str, token: str) -> dict[str, Any]:
    cleaned = str(space_id or "").strip().strip("/")
    if not cleaned or "/" not in cleaned:
        raise ValueError("No associated Space id is available for this run.")
    owner = cleaned.split("/", 1)[0]
    if owner != username:
        raise PermissionError(f"Refusing to delete Space {cleaned}: it is not in your namespace.")
    api = HfApi(token=token)
    try:
        api.delete_repo(repo_id=cleaned, repo_type="space")
        return {"space_delete_requested": True, "space_deleted": True, "space_id": cleaned}
    except Exception as exc:  # noqa: BLE001
        message = redact(str(exc))
        missing_markers = ("404", "not found", "Repository Not Found", "does not exist")
        if any(marker.lower() in message.lower() for marker in missing_markers):
            return {"space_delete_requested": True, "space_deleted": False, "space_already_missing": True, "space_id": cleaned, "space_delete_error": message}
        return {"space_delete_requested": True, "space_deleted": False, "space_id": cleaned, "space_delete_error": message}


def _run_kind_from_bundle(bundle: dict[str, Any]) -> str:
    summary = bundle.get("summary") or bundle.get("summary_file") or {}
    state = bundle.get("state") or {}
    launch = bundle.get("launch") or {}
    return str(summary.get("kind") or state.get("kind") or launch.get("kind") or bundle.get("kind") or "").lower()


def _run_is_validation_like(bundle_or_summary: dict[str, Any]) -> bool:
    kind = str(
        bundle_or_summary.get("kind")
        or bundle_or_summary.get("run_type")
        or (bundle_or_summary.get("summary") or {}).get("kind")
        or (bundle_or_summary.get("state") or {}).get("kind")
        or (bundle_or_summary.get("launch") or {}).get("kind")
        or ""
    ).lower()
    run_id = str(bundle_or_summary.get("run_id") or (bundle_or_summary.get("summary") or {}).get("run_id") or "").lower()
    return "validation" in kind or "space_test" in kind or "validate" in kind or run_id.startswith("validate-")


def _linked_validation_parent_id(run: dict[str, Any]) -> str:
    summary = run.get("summary") or {}
    state = run.get("state") or {}
    launch = run.get("launch") or {}
    return str(
        run.get("parent_build_run_id")
        or run.get("parentBuildRunId")
        or summary.get("parent_build_run_id")
        or state.get("parent_build_run_id")
        or launch.get("parent_build_run_id")
        or ""
    ).strip()


def _linked_validation_summaries(parent_run_id: str, *, bucket_source: str, token: str | None = None) -> list[dict[str, Any]]:
    if not parent_run_id:
        return []
    try:
        rows = list_recent_runs(bucket_source=bucket_source, token=token, limit=200)
    except Exception:
        return []
    linked: list[dict[str, Any]] = []
    for row in rows:
        if not isinstance(row, dict):
            continue
        if not _run_is_validation_like(row):
            continue
        if _linked_validation_parent_id(row) == parent_run_id:
            linked.append(row)
    linked.sort(key=lambda r: str(r.get("updated_at") or r.get("created_at") or r.get("run_id") or ""), reverse=True)
    return linked


def _manual_validation_status_from_linked(parent_run_id: str, linked: list[dict[str, Any]]) -> dict[str, Any]:
    success_tokens = {"full_inference_success", "success", "succeeded", "passed", "validated_after_space_test", "validated_after_manual_space_test", "recovered_by_space_test", "recovered_by_manual_validation", "manual_validation_passed", "manual_validated"}
    failure_tokens = {"partial_validation", "manual_hardware_required", "generated_needs_manual_hardware", "technical_blocker", "technical_blocker_boot_only", "blocked", "completed_with_warnings", "failed", "failure", "error", "auth_refresh_required", "stale", "stopped", "cancelled", "canceled"}
    successes = [row for row in linked if str(row.get("status") or row.get("effective_status") or "").lower() in success_tokens]
    failures = [row for row in linked if str(row.get("status") or row.get("validation_status") or row.get("result_status") or row.get("effective_status") or "").lower() in failure_tokens]
    source = successes[0] if successes else (failures[0] if failures else {})
    if not source:
        return {"status": "none", "parent_build_run_id": parent_run_id, "validation_run_id": "", "updated_at": utc_now_iso()}
    status = "success" if successes else "failed"
    payload: dict[str, Any] = {
        "status": status,
        "parent_build_run_id": parent_run_id,
        "validation_run_id": source.get("run_id") or "",
        "target_space": source.get("target_space") or source.get("target_space_id") or "",
        "api_name": source.get("api_name") or "",
        "updated_at": utc_now_iso(),
    }
    if status == "success":
        payload["effective_status"] = "validated_after_manual_space_test"
        for key in ["latency_seconds", "observed_latency_seconds", "recommended_zero_gpu_duration_seconds", "recommended_zerogpu_duration_seconds", "recommendation_source", "recommendation_hardware", "hardware_used_for_validation"]:
            if source.get(key) is not None:
                payload[key] = source.get(key)
        payload.setdefault("recommendation_source", "linked_space_test")
    return payload


def _write_parent_linked_validation_state(parent_run_id: str, linked: list[dict[str, Any]], *, bucket_source: str, token: str | None = None) -> dict[str, Any]:
    paths = RunPaths(parent_run_id, bucket_source=bucket_source)
    compact = []
    for row in linked:
        compact.append({
            "validation_run_id": row.get("run_id") or "",
            "status": row.get("status") or row.get("validation_status") or row.get("result_status") or "unknown",
            "effective_status": row.get("effective_status") or "",
            "target_space": row.get("target_space") or row.get("target_space_id") or "",
            "api_name": row.get("api_name") or "",
            "latency_seconds": row.get("latency_seconds") or row.get("observed_latency_seconds"),
            "observed_latency_seconds": row.get("observed_latency_seconds") or row.get("latency_seconds"),
            "recommended_zero_gpu_duration_seconds": row.get("recommended_zero_gpu_duration_seconds") or row.get("recommended_zerogpu_duration_seconds"),
            "recommendation_source": row.get("recommendation_source") or "",
            "recommendation_hardware": row.get("recommendation_hardware") or row.get("hardware_used_for_validation") or "",
            "hardware_used_for_validation": row.get("hardware_used_for_validation") or row.get("recommendation_hardware") or "",
            "updated_at": row.get("updated_at") or row.get("created_at") or "",
        })
    linked_payload = {
        "parent_build_run_id": parent_run_id,
        "validations": compact,
        "updated_at": utc_now_iso(),
    }
    manual_status = _manual_validation_status_from_linked(parent_run_id, linked)
    if manual_status.get("status") == "success":
        linked_payload["effective_status"] = "validated_after_manual_space_test"
    write_json(f"{paths.root}/linked_validations.json", linked_payload, token=token)
    write_json(f"{paths.root}/manual_validation_status.json", manual_status, token=token)
    return {"linked_validations": linked_payload, "manual_validation_status": manual_status}

def create_app() -> FastAPI:
    """Create the product FastAPI app.

    The public root path is the custom dashboard. Hugging Face OAuth is
    attached directly to the FastAPI app with the official
    `huggingface_hub.attach_huggingface_oauth` helper.

    This avoids embedding the custom UI inside Gradio and avoids launching a
    second server. The app is served by the Docker/uvicorn entrypoint.
    """
    fastapi_app = FastAPI(title="Agentic Space Factory")
    try:
        attach_huggingface_oauth(fastapi_app)
    except ValueError as exc:
        # Local/test environments without an HF token cannot initialize the
        # mocked OAuth helper. Do not fail app import; production Spaces provide
        # the OAuth environment when hf_oauth is enabled.
        if "logged in to HF" not in str(exc) and "HF_TOKEN" not in str(exc):
            raise
    register_custom_routes(fastapi_app)
    return fastapi_app


app = create_app()