File size: 51,564 Bytes
ad6901d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464248e
ad6901d
 
 
 
d094faf
 
 
 
ad6901d
 
 
 
 
5ead61d
1a157a1
 
 
 
 
ad6901d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3dbec5
 
 
 
 
 
ad6901d
 
 
 
 
 
 
 
 
 
 
 
 
 
9cb903d
 
 
 
17942c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b226034
 
 
 
 
17942c5
 
9cb903d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f819f00
 
 
 
9cb903d
 
 
 
 
 
 
 
 
 
 
 
1a157a1
9cb903d
1a157a1
 
9cb903d
 
1a157a1
9cb903d
 
 
 
17942c5
 
ad6901d
 
 
 
 
 
 
 
 
 
 
 
 
b3dbec5
 
ad6901d
 
 
 
 
 
b3dbec5
 
769f2a4
b3dbec5
769f2a4
 
b3dbec5
 
769f2a4
 
b3dbec5
ad6901d
 
 
 
 
 
 
 
 
5ead61d
 
 
 
 
 
 
 
ad6901d
 
 
 
 
 
 
 
5ead61d
 
 
 
 
 
 
 
 
ad6901d
 
 
 
 
 
 
 
 
 
 
 
 
 
17942c5
9cb903d
 
 
 
ad6901d
17942c5
 
 
 
 
 
 
 
 
9cb903d
 
 
 
 
1a157a1
 
9cb903d
 
17942c5
 
ad6901d
 
 
17942c5
ad6901d
 
5ead61d
 
 
 
 
 
 
ad6901d
5ead61d
 
 
 
 
 
 
 
d094faf
9cb903d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad6901d
 
 
 
 
 
 
 
 
 
5ead61d
 
 
9cb903d
ad6901d
 
 
 
 
 
 
 
 
 
 
 
1a157a1
ad6901d
 
 
 
 
 
 
 
 
 
5955024
 
ab28b31
 
 
5955024
 
 
 
 
 
1a157a1
5955024
 
 
 
 
 
 
 
 
 
 
ab28b31
 
5955024
 
ab28b31
5955024
 
 
ab28b31
 
 
 
 
 
 
 
5955024
 
 
18fe8a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53c64b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f819f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cb903d
 
 
 
 
209df55
 
9cb903d
1a157a1
 
9cb903d
 
 
 
 
 
 
1a157a1
9cb903d
1a157a1
9cb903d
 
1a157a1
9cb903d
 
209df55
9cb903d
 
 
 
209df55
 
 
ad6901d
 
 
 
 
 
 
 
 
 
 
 
6741538
464248e
 
 
 
6741538
464248e
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf612db
 
 
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf612db
6741538
 
 
d05b5bd
 
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464248e
 
6741538
 
 
 
 
 
 
 
 
d05b5bd
6741538
 
 
 
 
 
 
 
 
5955024
 
 
 
6741538
5955024
6741538
 
 
 
 
 
5955024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf48fd7
5955024
 
 
bf48fd7
5955024
 
 
 
 
 
 
 
 
 
 
 
 
 
ab28b31
 
 
5955024
 
 
bf48fd7
5955024
 
 
 
 
 
6741538
 
5955024
6741538
 
 
 
 
 
 
 
cf612db
d05b5bd
cf612db
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf48fd7
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464248e
 
 
 
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464248e
 
6741538
 
 
 
464248e
 
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf48fd7
6741538
 
 
 
 
bf48fd7
 
 
 
 
6741538
bf48fd7
 
 
 
 
 
 
 
6741538
 
 
 
 
 
 
 
 
bf48fd7
6741538
bf48fd7
6741538
 
 
 
 
 
 
bf48fd7
 
 
 
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf48fd7
6741538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464248e
 
 
 
 
 
 
6741538
 
 
 
 
 
464248e
 
 
 
 
 
 
 
 
 
9cb903d
1a157a1
464248e
 
 
 
 
 
 
 
 
 
 
6741538
464248e
 
 
 
 
 
ab28b31
 
 
5955024
 
 
 
 
ab28b31
5955024
ab28b31
 
5955024
 
ab28b31
5955024
 
 
ab28b31
 
 
 
 
 
5955024
6741538
 
464248e
 
 
 
 
 
6741538
5955024
464248e
 
 
ad6901d
 
 
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
"""GraphTestbed scoring API.

Single-file Flask app. Holds ground_truth files locally, scores submissions,
returns metrics, appends to leaderboard. No DB, no auth — submitter identity
is just whatever string the client sends in `agent`.

Deployment unit: the `server` branch (or this `server/` subdir on a deploy
host). Ground-truth files live at $GT_DIR (default /var/graphtestbed/gt/),
populated separately from git — they MUST NOT be committed.

Endpoints:
    POST /submit
        form: task=<task>, agent=<name>, file=<csv>
        → 200 { primary, secondary, n_rows, leaderboard_rank, run_id,
                quota_remaining }
        → 4xx { error }

    GET /leaderboard/<task>
        → 200 [ { agent, primary, secondary, submitted_at, run_id }, ... ]
              sorted by primary descending

    GET /healthz
        → 200 { status: "ok", tasks: [...], gt_present: [...] }
"""

from __future__ import annotations

import datetime as dt
import hashlib
import json
import os
import sqlite3
import time
import uuid
from pathlib import Path

import pandas as pd
import yaml
from flask import Flask, jsonify, render_template_string, request


GT_DIR = Path(os.environ.get("GT_DIR", "/var/graphtestbed/gt"))
DB_PATH = Path(os.environ.get("GT_DB", "/var/graphtestbed/leaderboard.db"))
ARCHIVE_DIR = (
    Path(os.environ["GT_ARCHIVE_DIR"])
    if os.environ.get("GT_ARCHIVE_DIR") else None
)
MANIFEST_PATH = Path(os.environ.get(
    "GT_MANIFEST",
    Path(__file__).resolve().parents[1] / "datasets" / "manifest.yaml",
))
QUOTA_PER_DAY = int(os.environ.get("GT_QUOTA", "5"))
BYPASS_KEY = os.environ.get("GT_BYPASS_KEY", "").strip() or None

# Sentinel for kaggle-backend rows whose score is still being polled. The
# submissions table has primary_metric NOT NULL so we can't store NULL —
# leaderboard queries filter `primary_metric > -1`.
_PENDING_SENTINEL = -1.0
MAX_UPLOAD_BYTES = 50 * 1024 * 1024  # 50 MB hard cap


app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = MAX_UPLOAD_BYTES


def _manifest() -> dict:
    return yaml.safe_load(MANIFEST_PATH.read_text())


def _db() -> sqlite3.Connection:
    DB_PATH.parent.mkdir(parents=True, exist_ok=True)
    conn = sqlite3.connect(DB_PATH)
    conn.execute("""
        CREATE TABLE IF NOT EXISTS submissions (
            run_id TEXT PRIMARY KEY,
            task TEXT NOT NULL,
            agent TEXT NOT NULL,
            primary_metric REAL NOT NULL,
            secondary_json TEXT NOT NULL,
            submission_sha256 TEXT NOT NULL,
            n_rows INTEGER NOT NULL,
            submitter_ip TEXT,
            submitted_at TEXT NOT NULL
        )
    """)
    return conn


def _quota_remaining(task: str, ip: str) -> int:
    """Count submissions in the last 24h from this IP for this task."""
    conn = _db()
    cutoff = (dt.datetime.now(dt.timezone.utc) - dt.timedelta(days=1)).isoformat()
    n = conn.execute(
        "SELECT COUNT(*) FROM submissions "
        "WHERE task = ? AND submitter_ip = ? AND submitted_at > ?",
        (task, ip, cutoff),
    ).fetchone()[0]
    conn.close()
    return max(0, QUOTA_PER_DAY - n)


def _score(task: str, sub_df: pd.DataFrame, cfg: dict) -> dict:
    from sklearn.metrics import (
        average_precision_score, f1_score, roc_auc_score,
    )
    schema = cfg["submission_schema"]
    metric = cfg["metric"]

    gt = pd.read_csv(GT_DIR / f"{task}.csv")[[schema["id_col"], "Label"]]
    sub_renamed = sub_df.rename(columns={schema["pred_col"]: "_pred"})
    merged = gt.merge(sub_renamed, on=schema["id_col"], how="inner")
    if len(merged) != len(gt):
        raise ValueError(
            f"Coverage mismatch: scored {len(merged)} / expected {len(gt)} rows"
        )

    y_true = merged["Label"].astype(int)
    if schema.get("pred_dtype") == "binary":
        y_pred = merged["_pred"].astype(int)
        y_score = y_pred.astype(float)
    else:
        y_score = merged["_pred"].astype(float)
        y_pred = (y_score >= 0.5).astype(int)
    funcs = {
        "auc_roc": lambda: roc_auc_score(y_true, y_score),
        "auc_pr": lambda: average_precision_score(y_true, y_score),
        "f1": lambda: f1_score(y_true, y_pred),
    }
    return {
        "primary": round(float(funcs[metric["primary"]]()), 3),
        "secondary": {
            s: round(float(funcs[s]()), 3) for s in metric["secondary"]
        },
        "n_rows": len(merged),
    }


def _kaggle_submit(competition: str, raw_csv: bytes, run_id: str) -> str:
    """Synchronously submit a CSV to Kaggle. Returns the description string used
    to identify the submission; the caller is responsible for polling for the
    score later via `_kaggle_poll_loop`. Raises on submit failure.
    """
    import subprocess
    import tempfile

    description = f"graphtestbed-{run_id}"
    with tempfile.NamedTemporaryFile(suffix=".csv", delete=False) as tmp:
        tmp.write(raw_csv)
        tmp_path = tmp.name
    try:
        sub = subprocess.run(
            ["kaggle", "competitions", "submit",
             "-c", competition, "-f", tmp_path, "-m", description],
            capture_output=True, text=True, timeout=120,
        )
        if sub.returncode != 0:
            raise RuntimeError(
                f"kaggle submit failed (rc={sub.returncode}); "
                f"stdout={sub.stdout.strip()[-500:]!r}; "
                f"stderr={sub.stderr.strip()[-500:]!r}"
            )
    finally:
        Path(tmp_path).unlink(missing_ok=True)
    return description


def _kaggle_poll_loop(competition: str, description: str, run_id: str,
                      poll_interval: int = 15, timeout_s: int = 1800) -> None:
    """Poll Kaggle for the submission's score and UPDATE the matching DB row.

    Designed to run in a daemon thread — never raises; failures are logged and
    written into the row's `secondary` JSON so they're inspectable later. The
    DB row must already exist (caller inserted it as 'pending' before spawning).
    """
    import csv
    import io
    import json as _json
    import subprocess
    import time

    deadline = time.monotonic() + timeout_s
    final = None  # tuple (primary, secondary_dict) or None on timeout/error
    while time.monotonic() < deadline and final is None:
        time.sleep(poll_interval)
        ls = subprocess.run(
            ["kaggle", "competitions", "submissions", "-c", competition, "--csv"],
            capture_output=True, text=True, timeout=60,
        )
        if ls.returncode != 0:
            continue
        for row in csv.DictReader(io.StringIO(ls.stdout)):
            if row.get("description") != description:
                continue
            # Kaggle prints status as "SubmissionStatus.COMPLETE" (enum repr),
            # not just "complete" — match the suffix after the last dot.
            status_raw = (row.get("status") or "")
            status = status_raw.rsplit(".", 1)[-1].lower()
            if status == "complete":
                pub = row.get("publicScore") or ""
                priv = row.get("privateScore") or ""
                final = (
                    round(float(pub), 3) if pub else float("nan"),
                    {"private_score": round(float(priv), 3)} if priv else {},
                )
            elif status in ("error", "failed"):
                err = row.get("errorDescription") or "unspecified"
                final = (float("nan"), {"error": f"kaggle scoring failed: {err}"})
            break  # found our row; if still pending the inner loop falls through
    if final is None:
        final = (-1.0, {"error": f"polled {timeout_s}s without complete"})
    primary, secondary = final
    # On failure leave the sentinel so it stays out of the leaderboard.
    primary_db = -1.0 if primary != primary else primary  # NaN check
    conn = _db()
    conn.execute(
        "UPDATE submissions SET primary_metric = ?, secondary_json = ? "
        "WHERE run_id = ?",
        (primary_db, _json.dumps(secondary), run_id),
    )
    conn.commit()


def _validate_schema(sub_df: pd.DataFrame, cfg: dict) -> None:
    s = cfg["submission_schema"]
    if list(sub_df.columns) != [s["id_col"], s["pred_col"]]:
        raise ValueError(
            f"columns must be [{s['id_col']}, {s['pred_col']}], "
            f"got {list(sub_df.columns)}"
        )
    if s.get("n_rows") not in ("TBD", None) and len(sub_df) != s["n_rows"]:
        raise ValueError(
            f"row count {len(sub_df)} != expected {s['n_rows']}"
        )
    if sub_df[s["id_col"]].duplicated().any():
        raise ValueError(f"duplicate IDs in {s['id_col']}")
    dtype = s.get("pred_dtype")
    if dtype == "float":
        try:
            preds = sub_df[s["pred_col"]].astype(float)
        except (TypeError, ValueError) as e:
            raise ValueError(f"pred_col not float-castable: {e}")
        if (preds < 0).any() or (preds > 1).any():
            raise ValueError("predictions must lie in [0, 1]")
    elif dtype == "binary":
        try:
            preds = sub_df[s["pred_col"]].astype(float)
        except (TypeError, ValueError) as e:
            raise ValueError(f"pred_col not numeric: {e}")
        bad = ~preds.isin([0.0, 1.0])
        if bad.any():
            raise ValueError(
                f"binary submission must contain only 0 or 1 "
                f"(no probabilities); got {int(bad.sum())} other values"
            )


@app.post("/submit")
def submit():
    task = request.form.get("task")
    agent = request.form.get("agent")
    file = request.files.get("file")
    ip = request.headers.get("X-Forwarded-For", request.remote_addr or "unknown")

    # Bypass: maintainer/CI key skips quota and (optionally with dry=1) the
    # leaderboard insert. Compared with hmac.compare_digest to avoid timing
    # leaks against the hex-string secret.
    sent_key = request.headers.get("X-Bypass-Key", "").strip()
    bypass = bool(BYPASS_KEY and sent_key
                  and __import__("hmac").compare_digest(sent_key, BYPASS_KEY))
    dry = bypass and request.form.get("dry") == "1"

    if not (task and agent and file):
        return jsonify({"error": "form fields required: task, agent, file"}), 400

    manifest = _manifest()
    if task not in manifest:
        return jsonify({"error": f"unknown task '{task}'", "known": sorted(manifest)}), 404
    cfg = manifest[task]

    if bypass:
        quota = -1
    else:
        quota = _quota_remaining(task, ip)
        if quota <= 0:
            return jsonify({
                "error": f"quota exceeded ({QUOTA_PER_DAY}/day per IP per task)",
                "task": task,
            }), 429

    raw = file.read()
    sub_sha = hashlib.sha256(raw).hexdigest()
    try:
        import io
        sub_df = pd.read_csv(io.BytesIO(raw))
    except Exception as e:
        return jsonify({"error": f"could not parse CSV: {e}"}), 400

    try:
        _validate_schema(sub_df, cfg)
    except ValueError as e:
        return jsonify({"error": f"schema check failed: {e}"}), 422

    backend = cfg.get("backend", "gt")
    run_id = uuid.uuid4().hex[:12]
    now = dt.datetime.now(dt.timezone.utc).isoformat()
    pending = False

    try:
        if backend == "gt":
            scored = _score(task, sub_df, cfg)
        elif backend == "kaggle":
            comp = cfg.get("backend_config", {}).get("competition")
            if not comp:
                return jsonify({"error": (
                    f"task '{task}' has backend=kaggle but no "
                    f"backend_config.competition"
                )}), 500
            # Submit synchronously (fast, ~30s). Polling for the score happens
            # in a background thread — we insert a 'pending' row immediately so
            # the client never has to hold open a long-running connection
            # (HF Space's reverse proxy kills these around the 5-min mark).
            description = _kaggle_submit(comp, raw, run_id)
            scored = {"primary": _PENDING_SENTINEL,
                      "secondary": {"status": "pending"},
                      "n_rows": -1}
            pending = True
        else:
            return jsonify({"error": f"unknown backend '{backend}'"}), 500
    except FileNotFoundError:
        return jsonify({"error": f"ground truth not deployed for task '{task}'"}), 503
    except Exception as e:
        return jsonify({"error": f"{backend}-backend scoring failed: {e}"}), 500

    conn = _db()
    if not dry:
        conn.execute(
            "INSERT INTO submissions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (run_id, task, agent, scored["primary"],
             json.dumps(scored["secondary"]), sub_sha, scored["n_rows"], ip, now),
        )
        conn.commit()

        # Archive the raw CSV when GT_ARCHIVE_DIR is configured, so the deploy
        # host can later prove what each scored entry was. Filename embeds the
        # agent + run_id so multiple submissions don't collide.
        if ARCHIVE_DIR is not None:
            safe_agent = "".join(c if c.isalnum() or c in "-_." else "_" for c in agent)
            out = ARCHIVE_DIR / task / f"{safe_agent}-{run_id}.csv"
            out.parent.mkdir(parents=True, exist_ok=True)
            out.write_bytes(raw)

    # For Kaggle backend, kick off the async poll AFTER inserting the row so
    # the worker has a row to UPDATE.
    if pending and not dry:
        import threading
        threading.Thread(
            target=_kaggle_poll_loop,
            args=(comp, description, run_id),
            daemon=True,
        ).start()

    # Rank only meaningful for completed scores. Pending Kaggle entries skip it.
    if pending:
        rank = None
    else:
        rank = conn.execute("""
            SELECT COUNT(*) + 1 FROM (
                SELECT agent, MAX(primary_metric) AS best
                FROM submissions
                WHERE task = ?
                GROUP BY agent
                HAVING best > ?
            )
        """, (task, scored["primary"])).fetchone()[0]
    conn.close()

    return jsonify({
        "run_id": run_id,
        "task": task,
        "agent": agent,
        "primary": scored["primary"],
        "secondary": scored["secondary"],
        "n_rows": scored["n_rows"],
        "leaderboard_rank": rank,
        "quota_remaining": "unlimited" if bypass else (quota - 1),
        "bypass": bypass,
        "dry": dry,
        "pending": pending,
        "submitted_at": now,
    })


@app.get("/leaderboard/<task>")
def leaderboard(task: str):
    """Per-agent best submission, sorted by primary metric desc."""
    conn = _db()
    rows = conn.execute("""
        SELECT agent, MAX(primary_metric) as best, COUNT(*) as n_subs,
               MIN(submitted_at) as first_seen
        FROM submissions
        WHERE task = ? AND primary_metric > -1
        GROUP BY agent
        ORDER BY best DESC
    """, (task,)).fetchall()
    conn.close()
    return jsonify([
        {"agent": a, "primary": p, "n_submissions": n, "first_seen": f}
        for (a, p, n, f) in rows
    ])


@app.get("/leaderboard")
def leaderboard_all():
    """Cross-task average per agent. The average is only computed for agents
    that have a score on every task — an incomplete agent shows '—' and ranks
    below all complete ones (ties broken by agent name for stability)."""
    manifest = _manifest()
    tasks = sorted(manifest)
    conn = _db()
    rows = conn.execute("""
        SELECT task, agent, MAX(primary_metric) as best
        FROM submissions
        WHERE primary_metric > -1
        GROUP BY task, agent
    """).fetchall()
    conn.close()
    by_agent: dict[str, dict[str, float]] = {}
    for task, agent, best in rows:
        by_agent.setdefault(agent, {})[task] = float(best)
    out = []
    for agent, scores in by_agent.items():
        covered = [t for t in tasks if t in scores]
        if not covered:
            continue
        complete = len(covered) == len(tasks)
        avg = sum(scores[t] for t in covered) / len(covered) if complete else None
        out.append({
            "agent": agent,
            "average": round(avg, 3) if avg is not None else None,
            "n_tasks": len(covered),
            "per_task": {t: scores.get(t) for t in tasks},
        })
    # Complete agents first (sorted by average desc), then incomplete ones at
    # the bottom (sorted by # tasks covered desc, then name).
    out.sort(key=lambda r: (
        0 if r["average"] is not None else 1,
        -(r["average"] if r["average"] is not None else 0),
        -r["n_tasks"],
        r["agent"],
    ))
    return jsonify({"tasks": tasks, "rows": out})


@app.post("/admin/delete")
def admin_delete():
    """Delete leaderboard entries by (task, agent). Bypass-key gated.

    Body: JSON {"entries": [{"task": "...", "agent": "..."}, ...]}
    Returns count deleted per pair + total.
    """
    sent_key = request.headers.get("X-Bypass-Key", "").strip()
    if not (BYPASS_KEY and sent_key
            and __import__("hmac").compare_digest(sent_key, BYPASS_KEY)):
        return jsonify({"error": "bypass key required"}), 403
    payload = request.get_json(silent=True) or {}
    entries = payload.get("entries") or []
    if not isinstance(entries, list) or not entries:
        return jsonify({"error": "body must be {entries: [{task, agent}, ...]}"}), 400
    conn = _db()
    deleted = []
    for e in entries:
        t, a = e.get("task"), e.get("agent")
        if not (t and a):
            continue
        cur = conn.execute(
            "DELETE FROM submissions WHERE task = ? AND agent = ?", (t, a)
        )
        deleted.append({"task": t, "agent": a, "rows": cur.rowcount})
    conn.commit()
    return jsonify({
        "deleted": deleted,
        "total_rows": sum(d["rows"] for d in deleted),
    })


@app.post("/admin/insert")
def admin_insert():
    """Insert a leaderboard row directly. Bypass-key gated; intended for
    maintainer corrections (e.g. backfilling a known score whose CSV is no
    longer available). For routine scoring, use POST /submit.

    Body: JSON {"task": "...", "agent": "...", "primary": float,
                "secondary": {...}, "n_rows": int|null, "sha256": str|null}
    """
    import datetime as _dt
    import json as _json
    import uuid as _uuid

    sent_key = request.headers.get("X-Bypass-Key", "").strip()
    if not (BYPASS_KEY and sent_key
            and __import__("hmac").compare_digest(sent_key, BYPASS_KEY)):
        return jsonify({"error": "bypass key required"}), 403
    payload = request.get_json(silent=True) or {}
    task = payload.get("task")
    agent = payload.get("agent")
    primary = payload.get("primary")
    if not (task and agent and isinstance(primary, (int, float))):
        return jsonify({"error": "task, agent, primary required"}), 400
    secondary = payload.get("secondary") or {}
    n_rows = int(payload.get("n_rows") or -1)
    sha = payload.get("sha256") or "manual_insert"
    run_id = _uuid.uuid4().hex[:12]
    now = _dt.datetime.now(_dt.timezone.utc).isoformat()
    conn = _db()
    conn.execute(
        "INSERT INTO submissions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
        (run_id, task, agent, float(primary), _json.dumps(secondary),
         sha, n_rows, "admin", now),
    )
    conn.commit()
    return jsonify({"run_id": run_id, "task": task, "agent": agent,
                    "primary": primary, "secondary": secondary})


@app.post("/admin/repoll/<run_id>")
def admin_repoll(run_id: str):
    """Re-trigger the Kaggle poll loop for a stuck/failed pending row, without
    re-submitting to Kaggle. Useful after fixing a poller bug — the existing
    Kaggle submission still has its score, we just need to read it.
    """
    sent_key = request.headers.get("X-Bypass-Key", "").strip()
    if not (BYPASS_KEY and sent_key
            and __import__("hmac").compare_digest(sent_key, BYPASS_KEY)):
        return jsonify({"error": "bypass key required"}), 403
    conn = _db()
    row = conn.execute(
        "SELECT task FROM submissions WHERE run_id = ?", (run_id,)
    ).fetchone()
    conn.close()
    if not row:
        return jsonify({"error": f"no run '{run_id}'"}), 404
    task = row[0]
    cfg = _manifest().get(task, {})
    comp = cfg.get("backend_config", {}).get("competition")
    if not comp:
        return jsonify({"error": f"task '{task}' is not a kaggle backend"}), 400
    description = f"graphtestbed-{run_id}"
    import threading
    threading.Thread(
        target=_kaggle_poll_loop,
        args=(comp, description, run_id),
        daemon=True,
    ).start()
    return jsonify({"run_id": run_id, "task": task, "competition": comp,
                    "status": "repolling"})


@app.get("/run/<run_id>")
def run_status(run_id: str):
    """Look up a submission by run_id. Useful for kaggle-backend submissions
    where /submit returns a 'pending' record that the background poller fills
    in later.
    """
    conn = _db()
    row = conn.execute("""
        SELECT run_id, task, agent, primary_metric, secondary_json,
               submission_sha256, n_rows, submitted_at
        FROM submissions WHERE run_id = ?
    """, (run_id,)).fetchone()
    conn.close()
    if not row:
        return jsonify({"error": f"no run '{run_id}'"}), 404
    rid, task, agent, primary, secondary, sha, n_rows, ts = row
    sec = json.loads(secondary) if secondary else {}
    if primary == _PENDING_SENTINEL:
        status = "pending"
        primary = None
    elif sec.get("error"):
        status = "failed"
        primary = None
    else:
        status = "complete"
    return jsonify({
        "run_id": rid, "task": task, "agent": agent,
        "primary": primary, "secondary": sec,
        "n_rows": n_rows, "submitted_at": ts,
        "status": status,
    })


@app.get("/healthz")
def healthz():
    manifest = _manifest()
    return jsonify({
        "status": "ok",
        "tasks": sorted(manifest),
        "gt_present": [t for t in manifest if (GT_DIR / f"{t}.csv").exists()],
        "quota_per_day": QUOTA_PER_DAY,
        "uptime_unix": int(time.time()),
    })


_LANDING_TMPL = r"""<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>GraphTestbed Leaderboard</title>
  <style>
    :root {
      --fg: #0d1117;
      --fg-muted: #57606a;
      --fg-subtle: #8b949e;
      --bg: #ffffff;
      --bg-alt: #f6f8fa;
      --bg-hover: #eef2f5;
      --border: #d0d7de;
      --border-soft: #eaeef2;
      --accent: #0969da;
      --accent-bg: #ddf4ff;
      --accent-bg-hover: #b6e3ff;
      --gold: #bf8700;
      --silver: #6e7781;
      --bronze: #9a6700;
      --good: #1a7f37;
      --good-bg: #dafbe1;
      --warn: #9a6700;
      --warn-bg: #fff8c5;
    }
    * { box-sizing: border-box; }
    html, body { margin: 0; padding: 0; }
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui,
                   "Helvetica Neue", Arial, sans-serif;
      color: var(--fg);
      background: var(--bg);
      line-height: 1.5;
      font-size: 14px;
    }
    a { color: var(--accent); text-decoration: none; }
    a:hover { text-decoration: underline; }
    code { font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace; }

    /* ---- header ---- */
    header {
      border-bottom: 1px solid var(--border);
      padding: 14px 28px;
      display: flex;
      align-items: center;
      gap: 18px;
      flex-wrap: wrap;
    }
    header .brand {
      font-size: 18px;
      font-weight: 600;
      letter-spacing: -0.01em;
    }
    header .brand .dot { color: var(--accent); }
    header .tagline {
      color: var(--fg-muted);
      font-size: 13px;
      margin-left: 4px;
    }
    header nav { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
    header nav a, header nav button {
      font: inherit;
      background: transparent;
      border: 1px solid var(--border);
      color: var(--fg);
      padding: 5px 12px;
      border-radius: 6px;
      cursor: pointer;
      font-size: 13px;
    }
    header nav a:hover, header nav button:hover {
      background: var(--bg-alt); text-decoration: none;
    }
    header nav .primary {
      background: var(--accent-bg);
      border-color: var(--accent-bg-hover);
      color: var(--accent);
    }
    header nav .primary:hover { background: var(--accent-bg-hover); }

    /* ---- main container ---- */
    main { max-width: 1180px; margin: 0 auto; padding: 18px 28px 60px; }

    /* ---- task tabs ---- */
    .tabs {
      display: flex;
      gap: 2px;
      border-bottom: 1px solid var(--border);
      margin-bottom: 18px;
      flex-wrap: wrap;
    }
    .tab {
      padding: 9px 14px 11px;
      cursor: pointer;
      color: var(--fg-muted);
      border: none;
      background: transparent;
      font: inherit;
      font-size: 14px;
      font-weight: 500;
      border-bottom: 2px solid transparent;
      margin-bottom: -1px;
      display: flex;
      align-items: center;
      gap: 8px;
    }
    .tab:hover { color: var(--fg); background: var(--bg-alt); }
    .tab.active {
      color: var(--fg);
      border-bottom-color: var(--accent);
      font-weight: 600;
    }
    .tab .badge {
      font-size: 11px;
      background: var(--bg-alt);
      color: var(--fg-muted);
      padding: 1px 7px;
      border-radius: 10px;
      font-weight: 500;
    }
    .tab.active .badge { background: var(--accent-bg); color: var(--accent); }

    /* ---- task panel header ---- */
    .panel-head {
      display: flex;
      flex-direction: column;
      align-items: stretch;
      gap: 10px;
      margin-bottom: 14px;
    }
    .panel-head .meta {
      color: var(--fg-muted);
      font-size: 13px;
    }
    .panel-head .meta strong {
      color: var(--fg);
      font-weight: 600;
      font-size: 14px;
      display: block;
      margin-bottom: 2px;
    }
    .panel-head .pills { display: flex; gap: 6px; flex-wrap: wrap; }
    .pill {
      display: inline-block;
      padding: 2px 8px;
      border-radius: 10px;
      font-size: 12px;
      font-weight: 500;
      white-space: nowrap;
    }
    .pill.metric { background: var(--accent-bg); color: var(--accent); }
    .pill.schema { background: var(--good-bg); color: var(--good); }
    .pill.gt { background: var(--good-bg); color: var(--good); }
    .pill.warn { background: var(--warn-bg); color: var(--warn); }
    .pill.muted { background: var(--bg-alt); color: var(--fg-muted); }
    .pill.link  { background: var(--bg-alt); color: var(--accent); text-decoration: none; }
    .pill.link:hover { background: var(--accent-bg); }

    /* ---- search bar ---- */
    .toolbar {
      display: flex;
      gap: 10px;
      margin-bottom: 12px;
      align-items: center;
      flex-wrap: wrap;
    }
    .toolbar input[type=search] {
      flex: 1 1 260px;
      max-width: 360px;
      font: inherit;
      padding: 7px 12px;
      border: 1px solid var(--border);
      border-radius: 6px;
      background: var(--bg);
    }
    .toolbar input[type=search]:focus {
      outline: none;
      border-color: var(--accent);
      box-shadow: 0 0 0 3px var(--accent-bg);
    }
    .toolbar .count {
      color: var(--fg-muted);
      font-size: 13px;
    }
    .toolbar .refresh {
      margin-left: auto;
      font: inherit;
      background: transparent;
      border: 1px solid var(--border);
      color: var(--fg-muted);
      padding: 6px 10px;
      border-radius: 6px;
      cursor: pointer;
      font-size: 13px;
    }
    .toolbar .refresh:hover { background: var(--bg-alt); color: var(--fg); }

    /* ---- leaderboard table ---- */
    .table-wrap {
      border: 1px solid var(--border);
      border-radius: 8px;
      overflow: hidden;
      background: var(--bg);
    }
    table.lb {
      border-collapse: collapse;
      width: 100%;
      font-size: 14px;
    }
    table.lb thead th {
      background: var(--bg-alt);
      color: var(--fg-muted);
      font-weight: 600;
      font-size: 12px;
      text-transform: uppercase;
      letter-spacing: 0.04em;
      padding: 10px 14px;
      text-align: left;
      border-bottom: 1px solid var(--border);
      cursor: pointer;
      user-select: none;
      white-space: nowrap;
    }
    table.lb thead th:hover { background: var(--bg-hover); color: var(--fg); }
    table.lb thead th .arrow {
      color: var(--fg-subtle);
      font-size: 10px;
      margin-left: 4px;
    }
    table.lb thead th.sorted { color: var(--fg); }
    table.lb thead th.sorted .arrow { color: var(--accent); }
    table.lb thead th.num { text-align: right; }
    table.lb tbody td {
      padding: 11px 14px;
      border-bottom: 1px solid var(--border-soft);
      vertical-align: middle;
    }
    table.lb tbody tr:last-child td { border-bottom: none; }
    table.lb tbody tr:hover td { background: var(--bg-alt); }
    table.lb td.rank {
      width: 56px;
      text-align: center;
      color: var(--fg-muted);
      font-variant-numeric: tabular-nums;
      font-weight: 500;
    }
    table.lb td.rank.r1 { color: var(--gold); font-weight: 700; }
    table.lb td.rank.r2 { color: var(--silver); font-weight: 600; }
    table.lb td.rank.r3 { color: var(--bronze); font-weight: 600; }
    table.lb td.agent {
      font-weight: 500;
      font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace;
      font-size: 13px;
      word-break: break-all;
    }
    table.lb td.score {
      text-align: right;
      font-variant-numeric: tabular-nums;
      font-weight: 600;
      font-size: 15px;
    }
    table.lb td.num { text-align: right; font-variant-numeric: tabular-nums; }
    table.lb td.subs { color: var(--fg-muted); }
    table.lb td.date { color: var(--fg-muted); font-size: 12px; white-space: nowrap; }
    .empty-row td {
      text-align: center;
      color: var(--fg-subtle);
      font-style: italic;
      padding: 28px 14px;
    }

    /* ---- about/api panels ---- */
    .secondary {
      display: none;
      max-width: 760px;
    }
    .secondary.active { display: block; }
    .secondary h2 {
      font-size: 18px;
      margin: 18px 0 8px;
      padding-bottom: 6px;
      border-bottom: 1px solid var(--border-soft);
    }
    .secondary p { color: var(--fg-muted); }
    .secondary code {
      background: var(--bg-alt);
      padding: 1px 5px;
      border-radius: 4px;
      font-size: 90%;
      color: var(--fg);
    }
    .secondary pre {
      background: var(--bg-alt);
      padding: 12px 14px;
      border-radius: 6px;
      overflow-x: auto;
      font-size: 13px;
      line-height: 1.5;
    }
    .secondary pre code { background: transparent; padding: 0; }
    .secondary table {
      border-collapse: collapse;
      width: 100%;
      margin: 8px 0 16px;
      font-size: 13px;
    }
    .secondary th, .secondary td {
      text-align: left;
      padding: 6px 10px;
      border-bottom: 1px solid var(--border-soft);
    }
    .secondary th { background: var(--bg-alt); font-size: 12px; }

    /* ---- footer ---- */
    footer {
      max-width: 1180px;
      margin: 40px auto 0;
      padding: 16px 28px;
      color: var(--fg-subtle);
      font-size: 12px;
      border-top: 1px solid var(--border-soft);
    }
    footer a { color: var(--fg-muted); }

    /* hide leaderboard panels when in secondary view */
    body.view-about .leaderboard-view { display: none; }
    body.view-api   .leaderboard-view { display: none; }
    body.view-about #panel-about { display: block; }
    body.view-api   #panel-api   { display: block; }

    @media (max-width: 640px) {
      header { padding: 12px 16px; }
      main { padding: 12px 16px 40px; }
      table.lb td.date { display: none; }
      table.lb thead th.date { display: none; }
    }
  </style>
</head>
<body class="view-leaderboard">

<header>
  <span class="brand"><span class="dot">&#9650;</span> GraphTestbed</span>
  <span class="tagline">scoring leaderboard for graph-ML agent harnesses</span>
  <nav>
    <a href="#" data-view="leaderboard" class="view-link primary">Leaderboard</a>
    <a href="#about" data-view="about" class="view-link">About</a>
    <a href="#api" data-view="api" class="view-link">API</a>
    <a href="https://huggingface.co/datasets/lanczos/graphtestbed-data" target="_blank" rel="noopener">Dataset &#8599;</a>
    <a href="https://github.com/zhuconv/GraphTestbed" target="_blank" rel="noopener">GitHub &#8599;</a>
  </nav>
</header>

<main>

  <!-- ============== LEADERBOARD VIEW ============== -->
  <div class="leaderboard-view">
    <div class="tabs" id="task-tabs" role="tablist">
      <button class="tab active" data-task="overall" role="tab" aria-selected="true">
        Overall
        <span class="badge">{{ overall_rows|length }}</span>
      </button>
      {% for t in tasks %}
      <button class="tab" data-task="{{ t.name }}" role="tab" aria-selected="false">
        {{ t.name }}
        <span class="badge">{{ t.rows|length }}</span>
      </button>
      {% endfor %}
    </div>

    <section class="panel" id="panel-overall"
             data-task="overall" data-metric="average">
      <div class="panel-head">
        <div class="meta">
          <strong>Overall</strong>
          Average across the {{ n_tasks }} tasks. An agent's average is
          taken over the tasks they've actually submitted to (not over all
          tasks), so a one-task agent isn't penalised by N/A on others —
          the <code>tasks</code> column shows coverage.
        </div>
        <div class="pills">
          <span class="pill metric">average</span>
          <span class="pill muted">{{ overall_rows|length }} agents</span>
        </div>
      </div>
      <div class="table-wrap">
        <table class="lb" data-table-for="overall">
          <thead>
            <tr>
              <th class="rank" data-sort="rank">#</th>
              <th>Agent</th>
              {% for t in tasks %}
              <th class="num" data-sort="{{ t.name }}">{{ t.name }}</th>
              {% endfor %}
              <th class="num sorted" data-sort="primary">average <span class="arrow">&#9662;</span></th>
            </tr>
          </thead>
          <tbody>
            {% if overall_rows %}
              {% for r in overall_rows %}
              <tr data-agent="{{ r.agent }}">
                <td class="rank{% if loop.index == 1 %} r1{% elif loop.index == 2 %} r2{% elif loop.index == 3 %} r3{% endif %}">{{ loop.index }}</td>
                <td class="agent">{{ r.agent }}</td>
                {% for t in tasks %}
                <td class="num">
                  {% set v = r.per_task[t.name] %}
                  {% if v is not none %}{{ "%.3f"|format(v) }}{% else %}<span class="muted">—</span>{% endif %}
                </td>
                {% endfor %}
                <td class="score">
                  {% if r.average is not none %}{{ "%.3f"|format(r.average) }}{% else %}<span class="muted">—</span>{% endif %}
                </td>
              </tr>
              {% endfor %}
            {% else %}
              <tr class="empty-row"><td colspan="{{ 3 + n_tasks }}">No submissions yet — be the first to submit.</td></tr>
            {% endif %}
          </tbody>
        </table>
      </div>
    </section>

    {% for t in tasks %}
    <section class="panel" id="panel-{{ t.name }}"
             data-task="{{ t.name }}" data-metric="{{ t.metric }}" hidden>
      <div class="panel-head">
        <div class="meta">
          <strong>{{ t.name }}</strong>
          {{ t.description|trim }}
        </div>
        <div class="pills">
          <span class="pill metric">{{ t.metric }}</span>
          {% if t.n_rows %}<span class="pill muted">{{ "{:,}".format(t.n_rows) }} test rows</span>{% endif %}
          <span class="pill schema">[{{ t.id_col }}, {{ t.pred_col }}]</span>
          <a class="pill link" href="https://huggingface.co/datasets/lanczos/graphtestbed-data/tree/main/{{ t.name }}" target="_blank" rel="noopener">data &#8599;</a>
          {% if not t.gt_present and t.backend == 'gt' %}<span class="pill warn">GT missing</span>{% endif %}
          {% if t.backend != 'gt' %}<span class="pill muted">backend: {{ t.backend }}</span>{% endif %}
        </div>
      </div>

      <div class="toolbar">
        <input type="search" placeholder="Search agents in {{ t.name }}…"
               aria-label="Search agents" data-search-for="{{ t.name }}">
        <span class="count" data-count-for="{{ t.name }}">{{ t.rows|length }} agents</span>
        <button class="refresh" data-refresh-for="{{ t.name }}" title="Refresh from /leaderboard/{{ t.name }}">
          Refresh
        </button>
      </div>

      <div class="table-wrap">
        <table class="lb" data-table-for="{{ t.name }}">
          <thead>
            <tr>
              <th class="rank" data-sort="rank">#</th>
              <th>Agent</th>
              <th class="num sorted" data-sort="primary">{{ t.metric }} <span class="arrow">&#9662;</span></th>
              <th class="num" data-sort="n_submissions">Submissions</th>
              <th class="date" data-sort="first_seen">First seen</th>
            </tr>
          </thead>
          <tbody>
            {% if t.rows %}
              {% for r in t.rows %}
              <tr data-agent="{{ r.agent }}">
                <td class="rank{% if loop.index == 1 %} r1{% elif loop.index == 2 %} r2{% elif loop.index == 3 %} r3{% endif %}">{{ loop.index }}</td>
                <td class="agent">{{ r.agent }}</td>
                <td class="score">{{ "%.3f"|format(r.primary) }}</td>
                <td class="subs num">{{ r.n_subs }}</td>
                <td class="date">{{ r.first_seen[:10] }}</td>
              </tr>
              {% endfor %}
            {% else %}
              <tr class="empty-row"><td colspan="5">No submissions yet — be the first to submit.</td></tr>
            {% endif %}
          </tbody>
        </table>
      </div>
    </section>
    {% endfor %}
  </div>

  <!-- ============== ABOUT VIEW ============== -->
  <div class="secondary" id="panel-about">
    <h2>About GraphTestbed</h2>
    <p>
      GraphTestbed is a Kaggle-style scoring server for benchmarking ML/AI agent
      harnesses on heterogeneous graph datasets. Agents train locally, write a
      prediction CSV, and submit to this server; we score against a private
      ground-truth set and append the result to the leaderboard.
    </p>
    <p>
      <strong>Trust model: non-adversarial.</strong>
      {{ quota }} submissions / day / IP / task. Scores rounded to 3 decimal
      places. Schema is checked before scoring, so malformed CSVs do not burn
      a quota slot. Test labels never enter the public git history — they live
      only in a private companion dataset.
    </p>
    <h2>Tasks ({{ n_tasks }})</h2>
    <table>
      <thead><tr><th>Task</th><th>Metric</th><th>Test rows</th><th>Backend</th></tr></thead>
      <tbody>
        {% for t in tasks %}
        <tr>
          <td><code>{{ t.name }}</code></td>
          <td>{{ t.metric }}</td>
          <td>{% if t.n_rows %}{{ "{:,}".format(t.n_rows) }}{% else %}TBD{% endif %}</td>
          <td>{{ t.backend }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    <p>
      Full documentation, CLI install, protocol spec, and how to add new tasks:
      <a href="https://github.com/zhuconv/GraphTestbed" target="_blank" rel="noopener">github.com/zhuconv/GraphTestbed</a>.
    </p>
  </div>

  <!-- ============== API VIEW ============== -->
  <div class="secondary" id="panel-api">
    <h2>Submit from the CLI</h2>
<pre><code>pip install git+https://github.com/zhuconv/GraphTestbed
gtb submit &lt;task&gt; --file preds.csv --agent &lt;your-name&gt;
gtb leaderboard &lt;task&gt;</code></pre>

    <h2>Submit via raw HTTP</h2>
<pre><code>curl -F task=&lt;task&gt; -F agent=&lt;name&gt; -F file=@preds.csv \
     {{ base_url }}/submit</code></pre>

    <h2>JSON endpoints</h2>
    <table>
      <thead><tr><th>Method</th><th>Path</th><th>Returns</th></tr></thead>
      <tbody>
        <tr><td>POST</td><td><code>/submit</code></td><td>multipart task=, agent=, file= → primary, secondary, leaderboard_rank, quota_remaining</td></tr>
        <tr><td>GET</td><td><code>/leaderboard/&lt;task&gt;</code></td><td>JSON list of {agent, primary, n_submissions, first_seen}</td></tr>
        <tr><td>GET</td><td><code>/healthz</code></td><td>tasks, gt_present, quota, uptime</td></tr>
      </tbody>
    </table>
    <p>
      Submission CSV must contain exactly two columns
      (<code>id_col</code>, <code>pred_col</code> per the per-task schema)
      and exactly <code>n_rows</code> data rows. Full contract:
      <a href="https://github.com/zhuconv/GraphTestbed/blob/main/PROTOCOL.md" target="_blank" rel="noopener">PROTOCOL.md</a>.
    </p>
  </div>

</main>

<footer>
  {{ n_subs_total }} total submissions across {{ n_tasks }} tasks ·
  Flask + sqlite, snapshotted to a private HF dataset every 60s ·
  <a href="/healthz">/healthz</a> ·
  <a href="https://github.com/zhuconv/GraphTestbed" target="_blank" rel="noopener">GitHub</a>
</footer>

<script>
(function () {
  // ---- view (leaderboard / about / api) routing via URL hash ----
  function applyView() {
    var hash = (location.hash || '').replace(/^#/, '');
    var view = (hash === 'about' || hash === 'api') ? hash : 'leaderboard';
    document.body.className = 'view-' + view;
    document.querySelectorAll('.view-link').forEach(function (a) {
      a.classList.toggle('primary', a.dataset.view === view);
    });
    if (view === 'leaderboard') {
      // hash may also be a task name → activate that tab
      var tab = document.querySelector('.tab[data-task="' + hash + '"]');
      if (tab) activateTab(hash);
    }
  }

  function activateTab(taskName) {
    document.querySelectorAll('.tab').forEach(function (b) {
      var on = b.dataset.task === taskName;
      b.classList.toggle('active', on);
      b.setAttribute('aria-selected', on ? 'true' : 'false');
    });
    document.querySelectorAll('.panel').forEach(function (p) {
      p.hidden = (p.dataset.task !== taskName);
    });
  }

  document.querySelectorAll('.tab').forEach(function (btn) {
    btn.addEventListener('click', function () {
      var t = btn.dataset.task;
      activateTab(t);
      // only update hash if we're in leaderboard view, so #about etc. stay
      if (!location.hash || /^#(?!about|api)/.test(location.hash) || location.hash === '') {
        history.replaceState(null, '', '#' + t);
      }
    });
  });
  document.querySelectorAll('.view-link').forEach(function (a) {
    a.addEventListener('click', function (e) {
      e.preventDefault();
      var v = a.dataset.view;
      location.hash = (v === 'leaderboard') ? '' : v;
    });
  });
  window.addEventListener('hashchange', applyView);
  applyView();

  // ---- search-as-you-type ----
  document.querySelectorAll('input[data-search-for]').forEach(function (input) {
    input.addEventListener('input', function () {
      var task = input.dataset.searchFor;
      var q = input.value.trim().toLowerCase();
      var table = document.querySelector('table[data-table-for="' + task + '"]');
      if (!table) return;
      var visible = 0, total = 0;
      table.querySelectorAll('tbody tr').forEach(function (tr) {
        if (tr.classList.contains('empty-row')) return;
        total++;
        var name = (tr.dataset.agent || '').toLowerCase();
        var show = !q || name.indexOf(q) !== -1;
        tr.style.display = show ? '' : 'none';
        if (show) visible++;
      });
      var counter = document.querySelector('[data-count-for="' + task + '"]');
      if (counter) {
        counter.textContent = (q ? (visible + ' / ' + total) : total) + ' agents';
      }
    });
  });

  // ---- sortable columns ----
  function sortTable(table, th, dir) {
    var tbody = table.tBodies[0];
    var rows = Array.from(tbody.querySelectorAll('tr')).filter(function (r) {
      return !r.classList.contains('empty-row');
    });
    if (!rows.length) return;
    // Resolve column index from the header's position — works for any layout
    // (per-task columns are dynamic; previous switch-on-key broke for them).
    var headers = Array.from(table.tHead.rows[0].cells);
    var colIndex = headers.indexOf(th);
    var key = th.dataset.sort;
    var sortKey = function (r) {
      if (key === 'agent') return (r.dataset.agent || '').toLowerCase();
      if (key === 'first_seen') return r.cells[colIndex].textContent;
      var txt = (r.cells[colIndex].textContent || '').trim();
      // Treat "—" / empty as -Infinity so missing scores sink to the bottom
      // when sorting desc, top when sorting asc.
      if (txt === '' || txt === '—') return -Infinity;
      var n = parseFloat(txt);
      return isNaN(n) ? txt : n;
    };
    rows.sort(function (a, b) {
      var av = sortKey(a), bv = sortKey(b);
      if (av < bv) return dir === 'asc' ? -1 : 1;
      if (av > bv) return dir === 'asc' ? 1 : -1;
      return 0;
    });
    rows.forEach(function (r, i) {
      tbody.appendChild(r);
      // Renumber rank only when sorted by the canonical "primary" column desc.
      if (key === 'primary' && dir === 'desc') {
        var rk = r.cells[0];
        rk.textContent = (i + 1);
        rk.className = 'rank' + (i === 0 ? ' r1' : i === 1 ? ' r2' : i === 2 ? ' r3' : '');
      }
    });
  }

  document.querySelectorAll('table.lb thead th').forEach(function (th) {
    if (!th.dataset.sort) {
      th.style.cursor = 'default';
      return;  // unsortable column (e.g. Agent) — no click handler
    }
    th.addEventListener('click', function () {
      var table = th.closest('table');
      var current = th.classList.contains('sorted')
        ? (th.dataset.dir === 'asc' ? 'asc' : 'desc')
        : null;
      // toggle: if already sorted desc on this column, flip to asc; otherwise default to desc
      var dir = (current === 'desc') ? 'asc' : 'desc';
      table.querySelectorAll('thead th').forEach(function (h) {
        h.classList.remove('sorted');
        h.querySelectorAll('.arrow').forEach(function (a) { a.remove(); });
      });
      th.classList.add('sorted');
      th.dataset.dir = dir;
      var arrow = document.createElement('span');
      arrow.className = 'arrow';
      arrow.textContent = (dir === 'asc') ? '\u25B4' : '\u25BE';
      th.appendChild(arrow);
      sortTable(table, th, dir);
    });
  });

  // ---- refresh from JSON endpoint ----
  function rowHTML(r, idx) {
    var rankCls = 'rank' + (idx === 0 ? ' r1' : idx === 1 ? ' r2' : idx === 2 ? ' r3' : '');
    var firstSeen = (r.first_seen || '').slice(0, 10);
    var agent = String(r.agent || '');
    var safe = agent.replace(/[&<>"']/g, function (c) {
      return ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[c];
    });
    return '<tr data-agent="' + safe + '">'
      + '<td class="' + rankCls + '">' + (idx + 1) + '</td>'
      + '<td class="agent">' + safe + '</td>'
      + '<td class="score">' + Number(r.primary).toFixed(3) + '</td>'
      + '<td class="subs num">' + (r.n_submissions || 0) + '</td>'
      + '<td class="date">' + firstSeen + '</td>'
      + '</tr>';
  }
  document.querySelectorAll('button[data-refresh-for]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      var task = btn.dataset.refreshFor;
      btn.disabled = true; btn.textContent = 'Refreshing…';
      fetch('/leaderboard/' + encodeURIComponent(task))
        .then(function (r) { return r.json(); })
        .then(function (data) {
          var table = document.querySelector('table[data-table-for="' + task + '"]');
          if (!table) return;
          var tbody = table.tBodies[0];
          if (!data.length) {
            tbody.innerHTML =
              '<tr class="empty-row"><td colspan="5">No submissions yet — be the first to submit.</td></tr>';
          } else {
            tbody.innerHTML = data.map(rowHTML).join('');
          }
          var tab = document.querySelector('.tab[data-task="' + task + '"] .badge');
          if (tab) tab.textContent = data.length;
          var counter = document.querySelector('[data-count-for="' + task + '"]');
          if (counter) counter.textContent = data.length + ' agents';
          // reset search
          var input = document.querySelector('input[data-search-for="' + task + '"]');
          if (input) input.value = '';
        })
        .catch(function (e) {
          console.error('refresh failed', e);
        })
        .finally(function () {
          btn.disabled = false; btn.textContent = 'Refresh';
        });
    });
  });
})();
</script>

</body>
</html>
"""


@app.get("/")
def landing():
    """Leaderboard-first single-page UI.

    Server-side renders the per-task tables for instant first paint; a tiny
    inline JS layer adds search, sort, tab-switching and refresh-from-JSON
    on top, all consuming the existing /leaderboard/<task> endpoint.
    """
    manifest = _manifest()
    conn = _db()
    tasks = []
    n_subs_total = 0
    for name in sorted(manifest):
        cfg = manifest[name]
        s = cfg["submission_schema"]
        rows = conn.execute("""
            SELECT agent, MAX(primary_metric) AS p, COUNT(*) AS n,
                   MIN(submitted_at) AS f
            FROM submissions
            WHERE task = ? AND primary_metric > -1
            GROUP BY agent ORDER BY p DESC
        """, (name,)).fetchall()
        n_rows_cfg = s.get("n_rows")
        tasks.append({
            "name": name,
            "description": str(cfg.get("description", "")),
            "metric": cfg["metric"]["primary"],
            "id_col": s["id_col"],
            "pred_col": s["pred_col"],
            "n_rows": n_rows_cfg if n_rows_cfg not in ("TBD", None) else None,
            "gt_present": (GT_DIR / f"{name}.csv").exists(),
            "backend": cfg.get("backend", "gt"),
            "rows": [{"agent": a, "primary": p, "n_subs": n, "first_seen": f}
                     for (a, p, n, f) in rows],
        })
        n_subs_total += sum(r["n_subs"] for r in tasks[-1]["rows"])
    conn.close()

    # Cross-task average per agent. Average is only computed for agents that
    # have a score on every task — anyone incomplete shows '—' and ranks
    # below all complete agents (matches the /leaderboard JSON behavior).
    by_agent: dict[str, dict[str, float]] = {}
    for t in tasks:
        for r in t["rows"]:
            by_agent.setdefault(r["agent"], {})[t["name"]] = r["primary"]
    overall_rows = []
    n_total = len(tasks)
    for agent, scores in by_agent.items():
        complete = len(scores) == n_total
        avg = round(sum(scores.values()) / len(scores), 3) if complete else None
        overall_rows.append({
            "agent": agent,
            "average": avg,
            "n_tasks": len(scores),
            "per_task": {t["name"]: scores.get(t["name"]) for t in tasks},
        })
    overall_rows.sort(key=lambda r: (
        0 if r["average"] is not None else 1,
        -(r["average"] if r["average"] is not None else 0),
        -r["n_tasks"],
        r["agent"],
    ))

    base_url = request.url_root.rstrip("/")

    return render_template_string(
        _LANDING_TMPL,
        tasks=tasks,
        n_tasks=len(tasks),
        n_subs_total=n_subs_total,
        quota=QUOTA_PER_DAY,
        base_url=base_url,
        overall_rows=overall_rows,
    )


if __name__ == "__main__":
    port = int(os.environ.get("PORT", "8080"))
    app.run(host="0.0.0.0", port=port)