forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
999 lines (923 loc) · 31.6 KB
/
Copy pathdb.py
File metadata and controls
999 lines (923 loc) · 31.6 KB
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
from __future__ import annotations
import json
import sqlite3
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from .types import ExistingRun
def _json_dumps(value: Any) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def connect_database(db_path: Path) -> sqlite3.Connection:
db_path.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
return conn
def _column_exists(conn: sqlite3.Connection, table: str, column: str) -> bool:
rows = conn.execute(f"PRAGMA table_info({table})").fetchall()
return any(row[1] == column for row in rows)
def ensure_comparison_id_column(conn: sqlite3.Connection) -> None:
"""Idempotently add ``comparison_id`` to ``benchmark_runs`` for old DBs."""
if not _column_exists(conn, "benchmark_runs", "comparison_id"):
conn.execute("ALTER TABLE benchmark_runs ADD COLUMN comparison_id TEXT")
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_benchmark_runs_comparison "
"ON benchmark_runs(comparison_id)"
)
conn.commit()
def _ensure_column(conn: sqlite3.Connection, table: str, column: str, declaration: str) -> None:
if not _column_exists(conn, table, column):
conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {declaration}")
def ensure_metrics_columns(conn: sqlite3.Connection) -> None:
for column, declaration in (
("trajectory_summary_json", "TEXT"),
("token_metrics_json", "TEXT"),
("cache_metrics_json", "TEXT"),
("performance_metrics_json", "TEXT"),
("trajectory_count", "INTEGER"),
("llm_call_count", "INTEGER"),
("total_prompt_tokens", "INTEGER"),
("total_completion_tokens", "INTEGER"),
("total_cache_read_input_tokens", "INTEGER"),
("total_cache_creation_input_tokens", "INTEGER"),
("mean_latency_ms", "REAL"),
("p95_latency_ms", "REAL"),
("throughput_per_second", "REAL"),
):
_ensure_column(conn, "benchmark_runs", column, declaration)
conn.commit()
def initialize_database(conn: sqlite3.Connection) -> None:
conn.executescript(
"""
PRAGMA journal_mode=WAL;
CREATE TABLE IF NOT EXISTS run_groups (
run_group_id TEXT PRIMARY KEY,
created_at TEXT NOT NULL,
finished_at TEXT,
request_json TEXT NOT NULL,
benchmarks_json TEXT NOT NULL,
repo_meta_json TEXT NOT NULL,
created_by TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS benchmark_runs (
run_id TEXT PRIMARY KEY,
run_group_id TEXT NOT NULL,
benchmark_id TEXT NOT NULL,
benchmark_directory TEXT NOT NULL,
signature TEXT NOT NULL,
status TEXT NOT NULL,
attempt INTEGER NOT NULL,
agent TEXT NOT NULL,
provider TEXT NOT NULL,
model TEXT NOT NULL,
extra_config_json TEXT NOT NULL,
started_at TEXT NOT NULL,
ended_at TEXT,
duration_seconds REAL,
command_json TEXT NOT NULL,
cwd TEXT NOT NULL,
stdout_path TEXT NOT NULL,
stderr_path TEXT NOT NULL,
result_json_path TEXT,
score REAL,
unit TEXT,
higher_is_better INTEGER,
metrics_json TEXT NOT NULL,
artifacts_json TEXT NOT NULL,
error TEXT,
high_score_label TEXT,
high_score_value REAL,
delta_to_high_score REAL,
benchmark_version TEXT,
benchmarks_commit TEXT,
eliza_commit TEXT,
eliza_version TEXT,
created_at TEXT NOT NULL,
comparison_id TEXT,
FOREIGN KEY(run_group_id) REFERENCES run_groups(run_group_id)
);
CREATE TABLE IF NOT EXISTS benchmark_run_trajectories (
run_id TEXT NOT NULL,
trajectory_file TEXT NOT NULL,
turn_index INTEGER NOT NULL,
prompt_tokens INTEGER NOT NULL DEFAULT 0,
completion_tokens INTEGER NOT NULL DEFAULT 0,
total_tokens INTEGER NOT NULL DEFAULT 0,
cached_tokens INTEGER NOT NULL DEFAULT 0,
cache_creation_tokens INTEGER NOT NULL DEFAULT 0,
latency_ms REAL,
prompt_chars INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(run_id, trajectory_file, turn_index),
FOREIGN KEY(run_id) REFERENCES benchmark_runs(run_id)
);
CREATE INDEX IF NOT EXISTS idx_benchmark_runs_signature
ON benchmark_runs(signature);
CREATE INDEX IF NOT EXISTS idx_benchmark_runs_signature_status
ON benchmark_runs(signature, status, ended_at);
CREATE INDEX IF NOT EXISTS idx_benchmark_runs_group
ON benchmark_runs(run_group_id, started_at);
CREATE INDEX IF NOT EXISTS idx_benchmark_runs_lookup
ON benchmark_runs(benchmark_id, provider, model, agent, started_at);
CREATE INDEX IF NOT EXISTS idx_benchmark_run_trajectories_run
ON benchmark_run_trajectories(run_id);
"""
)
conn.commit()
_ensure_column(
conn,
"benchmark_run_trajectories",
"total_tokens",
"INTEGER NOT NULL DEFAULT 0",
)
conn.commit()
ensure_comparison_id_column(conn)
ensure_metrics_columns(conn)
def create_run_group(
conn: sqlite3.Connection,
*,
run_group_id: str,
created_at: str,
request: dict[str, Any],
benchmarks: list[str],
repo_meta: dict[str, Any],
) -> None:
conn.execute(
"""
INSERT INTO run_groups (
run_group_id,
created_at,
request_json,
benchmarks_json,
repo_meta_json,
created_by
) VALUES (?, ?, ?, ?, ?, ?)
""",
(
run_group_id,
created_at,
_json_dumps(request),
_json_dumps(benchmarks),
_json_dumps(repo_meta),
"benchmarks.orchestrator",
),
)
conn.commit()
def finish_run_group(conn: sqlite3.Connection, *, run_group_id: str, finished_at: str) -> None:
conn.execute(
"UPDATE run_groups SET finished_at = ? WHERE run_group_id = ?",
(finished_at, run_group_id),
)
conn.commit()
def get_latest_run_for_signature(conn: sqlite3.Connection, signature: str) -> ExistingRun | None:
row = conn.execute(
"""
SELECT run_id, signature, status, attempt
FROM benchmark_runs
WHERE signature = ?
ORDER BY attempt DESC, started_at DESC
LIMIT 1
""",
(signature,),
).fetchone()
if row is None:
return None
return ExistingRun(
run_id=str(row["run_id"]),
signature=str(row["signature"]),
status=str(row["status"]),
attempt=int(row["attempt"]),
)
def get_latest_succeeded_run_for_signature(conn: sqlite3.Connection, signature: str) -> ExistingRun | None:
row = conn.execute(
"""
SELECT run_id, signature, status, attempt
FROM benchmark_runs
WHERE signature = ? AND status = 'succeeded'
ORDER BY attempt DESC, started_at DESC
LIMIT 1
""",
(signature,),
).fetchone()
if row is None:
return None
return ExistingRun(
run_id=str(row["run_id"]),
signature=str(row["signature"]),
status=str(row["status"]),
attempt=int(row["attempt"]),
)
def next_attempt_for_signature(conn: sqlite3.Connection, signature: str) -> int:
row = conn.execute(
"SELECT MAX(attempt) AS max_attempt FROM benchmark_runs WHERE signature = ?",
(signature,),
).fetchone()
if row is None or row["max_attempt"] is None:
return 1
return int(row["max_attempt"]) + 1
def insert_run_start(
conn: sqlite3.Connection,
*,
run_id: str,
run_group_id: str,
benchmark_id: str,
benchmark_directory: str,
signature: str,
attempt: int,
agent: str,
provider: str,
model: str,
extra_config: dict[str, Any],
started_at: str,
command: list[str],
cwd: str,
stdout_path: str,
stderr_path: str,
benchmark_version: str | None,
benchmarks_commit: str | None,
eliza_commit: str | None,
eliza_version: str | None,
) -> None:
conn.execute(
"""
INSERT INTO benchmark_runs (
run_id,
run_group_id,
signature,
benchmark_id,
benchmark_directory,
status,
attempt,
agent,
provider,
model,
extra_config_json,
started_at,
command_json,
cwd,
stdout_path,
stderr_path,
result_json_path,
score,
unit,
higher_is_better,
metrics_json,
artifacts_json,
error,
high_score_label,
high_score_value,
delta_to_high_score,
benchmark_version,
benchmarks_commit,
eliza_commit,
eliza_version,
created_at
) VALUES (?, ?, ?, ?, ?, 'running', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL, '{}', '[]', NULL, NULL, NULL, NULL, ?, ?, ?, ?, ?)
""",
(
run_id,
run_group_id,
signature,
benchmark_id,
benchmark_directory,
attempt,
agent,
provider,
model,
_json_dumps(extra_config),
started_at,
_json_dumps(command),
cwd,
stdout_path,
stderr_path,
benchmark_version,
benchmarks_commit,
eliza_commit,
eliza_version,
started_at,
),
)
conn.commit()
def update_run_result(
conn: sqlite3.Connection,
*,
run_id: str,
status: str,
ended_at: str,
duration_seconds: float | None,
score: float | None,
unit: str | None,
higher_is_better: bool | None,
metrics: dict[str, Any],
result_json_path: str | None,
artifacts: list[str],
error: str | None,
high_score_label: str | None,
high_score_value: float | None,
delta_to_high_score: float | None,
trajectory_summary: dict[str, Any] | None = None,
token_metrics: dict[str, Any] | None = None,
cache_metrics: dict[str, Any] | None = None,
performance_metrics: dict[str, Any] | None = None,
) -> None:
hib: int | None
if higher_is_better is None:
hib = None
else:
hib = 1 if higher_is_better else 0
conn.execute(
"""
UPDATE benchmark_runs
SET
status = ?,
ended_at = ?,
duration_seconds = ?,
score = ?,
unit = ?,
higher_is_better = ?,
metrics_json = ?,
result_json_path = ?,
artifacts_json = ?,
error = ?,
high_score_label = ?,
high_score_value = ?,
delta_to_high_score = ?,
trajectory_summary_json = ?,
token_metrics_json = ?,
cache_metrics_json = ?,
performance_metrics_json = ?,
trajectory_count = ?,
llm_call_count = ?,
total_prompt_tokens = ?,
total_completion_tokens = ?,
total_cache_read_input_tokens = ?,
total_cache_creation_input_tokens = ?,
mean_latency_ms = ?,
p95_latency_ms = ?,
throughput_per_second = ?
WHERE run_id = ?
""",
(
status,
ended_at,
duration_seconds,
score,
unit,
hib,
_json_dumps(metrics),
result_json_path,
_json_dumps(artifacts),
error,
high_score_label,
high_score_value,
delta_to_high_score,
_json_dumps(trajectory_summary or {}),
_json_dumps(token_metrics or {}),
_json_dumps(cache_metrics or {}),
_json_dumps(performance_metrics or {}),
_int_or_none((trajectory_summary or {}).get("files")),
_int_or_none((token_metrics or {}).get("llm_call_count")),
_int_or_none((token_metrics or {}).get("prompt_tokens")),
_int_or_none((token_metrics or {}).get("completion_tokens")),
_int_or_none((cache_metrics or {}).get("cache_read_input_tokens")),
_int_or_none((cache_metrics or {}).get("cache_creation_input_tokens")),
_float_or_none((performance_metrics or {}).get("mean_latency_ms")),
_float_or_none((performance_metrics or {}).get("p95_latency_ms")),
_float_or_none((performance_metrics or {}).get("throughput_per_second")),
run_id,
),
)
conn.commit()
def _int_or_none(value: Any) -> int | None:
if isinstance(value, bool):
return None
if isinstance(value, (int, float)):
return int(value)
return None
def _float_or_none(value: Any) -> float | None:
if isinstance(value, bool):
return None
if isinstance(value, (int, float)):
return float(value)
return None
def replace_run_trajectories(
conn: sqlite3.Connection,
*,
run_id: str,
trajectories: list[dict[str, Any]],
) -> None:
conn.execute("DELETE FROM benchmark_run_trajectories WHERE run_id = ?", (run_id,))
if trajectories:
conn.executemany(
"""
INSERT INTO benchmark_run_trajectories (
run_id,
trajectory_file,
turn_index,
prompt_tokens,
completion_tokens,
total_tokens,
cached_tokens,
cache_creation_tokens,
latency_ms,
prompt_chars
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
[
(
run_id,
str(row.get("trajectory_file") or row.get("file") or ""),
int(row.get("turn_index") or row.get("index") or 0),
int(row.get("prompt_tokens") or 0),
int(row.get("completion_tokens") or 0),
int(
row.get("total_tokens")
or (
int(row.get("prompt_tokens") or 0)
+ int(row.get("completion_tokens") or 0)
)
),
int(row.get("cached_tokens") or 0),
int(row.get("cache_creation_tokens") or 0),
_float_or_none(row.get("latency_ms")),
int(row.get("prompt_chars") or 0),
)
for row in trajectories
],
)
conn.commit()
def tag_run_with_comparison(
conn: sqlite3.Connection,
*,
run_id: str,
comparison_id: str,
) -> None:
conn.execute(
"UPDATE benchmark_runs SET comparison_id = ? WHERE run_id = ?",
(comparison_id, run_id),
)
conn.commit()
def list_runs_for_comparison(
conn: sqlite3.Connection,
*,
comparison_id: str,
) -> list[dict[str, Any]]:
rows = conn.execute(
"""
SELECT run_id, run_group_id, benchmark_id, status, agent, provider, model,
score, unit, higher_is_better, metrics_json, started_at, ended_at,
duration_seconds, error, trajectory_summary_json,
token_metrics_json, cache_metrics_json, performance_metrics_json
FROM benchmark_runs
WHERE comparison_id = ?
ORDER BY started_at ASC, run_id ASC
""",
(comparison_id,),
).fetchall()
out: list[dict[str, Any]] = []
for row in rows:
record = dict(row)
for json_key in (
"metrics_json",
"trajectory_summary_json",
"token_metrics_json",
"cache_metrics_json",
"performance_metrics_json",
):
raw = record.pop(json_key, None)
out_key = json_key.removesuffix("_json")
if isinstance(raw, str):
try:
record[out_key] = json.loads(raw)
except json.JSONDecodeError:
record[out_key] = {}
else:
record[out_key] = {}
hib = record.get("higher_is_better")
record["higher_is_better"] = None if hib is None else bool(hib)
out.append(record)
return out
def list_runs(
conn: sqlite3.Connection,
*,
limit: int | None = 5000,
) -> list[dict[str, Any]]:
query = """
SELECT
run_id,
run_group_id,
signature,
benchmark_id,
benchmark_directory,
status,
attempt,
agent,
provider,
model,
extra_config_json,
started_at,
ended_at,
duration_seconds,
command_json,
cwd,
stdout_path,
stderr_path,
result_json_path,
score,
unit,
higher_is_better,
metrics_json,
trajectory_summary_json,
token_metrics_json,
cache_metrics_json,
performance_metrics_json,
trajectory_count,
llm_call_count,
total_prompt_tokens,
total_completion_tokens,
total_cache_read_input_tokens,
total_cache_creation_input_tokens,
mean_latency_ms,
p95_latency_ms,
throughput_per_second,
artifacts_json,
error,
high_score_label,
high_score_value,
delta_to_high_score,
benchmark_version,
benchmarks_commit,
eliza_commit,
eliza_version
FROM benchmark_runs
ORDER BY started_at DESC, run_id DESC
"""
params: tuple[Any, ...] = ()
if limit is not None:
query += " LIMIT ?"
params = (limit,)
rows = conn.execute(query, params).fetchall()
out: list[dict[str, Any]] = []
for row in rows:
record = dict(row)
for key in (
"extra_config_json",
"command_json",
"metrics_json",
"trajectory_summary_json",
"token_metrics_json",
"cache_metrics_json",
"performance_metrics_json",
"artifacts_json",
):
raw = record.get(key)
if isinstance(raw, str):
try:
record[key.removesuffix("_json") if key.endswith("_json") else key] = json.loads(raw)
except json.JSONDecodeError:
record[key.removesuffix("_json") if key.endswith("_json") else key] = raw
if key in record:
del record[key]
hib = record.get("higher_is_better")
if hib is None:
record["higher_is_better"] = None
else:
record["higher_is_better"] = bool(hib)
out.append(record)
return out
def repair_nonzero_returncode_statuses(conn: sqlite3.Connection) -> int:
"""Mark legacy rows with nonzero process exits as failed.
Older runner versions recorded a row as ``succeeded`` whenever a result
JSON existed, even if the subprocess exited nonzero. Keep the artifacts and
score, but make the status honest so latest snapshots/viewer summaries do
not treat task-level process failures as clean wins.
"""
rows = conn.execute(
"""
SELECT run_id, metrics_json, error
FROM benchmark_runs
WHERE status = 'succeeded'
"""
).fetchall()
repaired = 0
for row in rows:
raw = row["metrics_json"]
if not isinstance(raw, str):
continue
try:
metrics = json.loads(raw)
except json.JSONDecodeError:
continue
if not isinstance(metrics, dict):
continue
return_code = metrics.get("return_code", metrics.get("returncode"))
if return_code in (None, 0):
return_code = metrics.get("nonzero_return_code_with_result")
if return_code in (None, 0):
return_code = metrics.get("exit_code")
if not isinstance(return_code, (int, float)) or int(return_code) == 0:
continue
error = row["error"] or (
"Command produced a result JSON but exited with "
f"return code {int(return_code)}"
)
conn.execute(
"""
UPDATE benchmark_runs
SET status = 'failed', error = ?
WHERE run_id = ?
""",
(error, row["run_id"]),
)
repaired += 1
if repaired:
conn.commit()
return repaired
def repair_nonpublishable_success_statuses(conn: sqlite3.Connection) -> int:
"""Mark legacy "successful" rows that never executed as failed.
This is intentionally narrow. Some deterministic/calibration benchmarks do
not emit LLM telemetry, but rows with zero evaluated samples in public
benchmark result shapes are empty artifacts rather than scored attempts.
"""
rows = conn.execute(
"""
SELECT run_id, benchmark_id, metrics_json, token_metrics_json, trajectory_summary_json, error
FROM benchmark_runs
WHERE status = 'succeeded'
"""
).fetchall()
repaired = 0
for row in rows:
raw_metrics = row["metrics_json"]
if not isinstance(raw_metrics, str):
continue
try:
metrics = json.loads(raw_metrics)
except json.JSONDecodeError:
continue
if not isinstance(metrics, dict):
continue
reason = _nonpublishable_success_reason(str(row["benchmark_id"]), metrics)
if reason is None:
continue
error = row["error"] or reason
conn.execute(
"""
UPDATE benchmark_runs
SET status = 'failed', error = ?
WHERE run_id = ?
""",
(error, row["run_id"]),
)
repaired += 1
if repaired:
conn.commit()
return repaired
def _metric_number(metrics: dict[str, Any], key: str) -> float | None:
value = metrics.get(key)
if isinstance(value, bool):
return None
if isinstance(value, (int, float)):
return float(value)
return None
def _nonpublishable_success_reason(
benchmark_id: str,
metrics: dict[str, Any],
) -> str | None:
if benchmark_id == "solana":
messages = metrics.get("messages")
cumulative_rewards = metrics.get("cumulative_rewards")
final_programs = metrics.get("final_programs")
if final_programs is None and isinstance(metrics.get("programs_discovered"), dict):
final_programs = len(metrics["programs_discovered"])
empty_rollout = (
isinstance(messages, list)
and len(messages) == 0
and isinstance(cumulative_rewards, list)
and len(cumulative_rewards) == 0
and (final_programs in (None, 0, 0.0))
)
return "Solana benchmark produced an empty rollout artifact" if empty_rollout else None
positive_sample_keys: dict[str, tuple[str, ...]] = {
"abliteration-robustness": ("n",),
"humaneval": ("n",),
"lifeops_bench": ("seeds",),
"mmlu": ("n",),
}
keys = positive_sample_keys.get(benchmark_id)
if keys is None:
return None
for key in keys:
value = _metric_number(metrics, key)
if value is None or value <= 0:
return f"{benchmark_id} benchmark produced a zero-sample success artifact ({key}={value!r})"
if benchmark_id == "lifeops_bench":
scenario_count = _metric_number(metrics, "scenario_count")
if scenario_count is not None and scenario_count <= 0:
return (
"lifeops_bench benchmark produced a zero-scenario success "
f"artifact (scenario_count={scenario_count!r})"
)
return None
def list_run_groups(conn: sqlite3.Connection, *, limit: int = 2000) -> list[dict[str, Any]]:
rows = conn.execute(
"""
SELECT run_group_id, created_at, finished_at, request_json, benchmarks_json, repo_meta_json
FROM run_groups
ORDER BY created_at DESC, run_group_id DESC
LIMIT ?
""",
(limit,),
).fetchall()
out: list[dict[str, Any]] = []
for row in rows:
record = dict(row)
for key in ("request_json", "benchmarks_json", "repo_meta_json"):
raw = record.get(key)
if isinstance(raw, str):
try:
record[key.removesuffix("_json")] = json.loads(raw)
except json.JSONDecodeError:
record[key.removesuffix("_json")] = raw
if key in record:
del record[key]
out.append(record)
return out
def summarize_latest_scores(conn: sqlite3.Connection) -> list[dict[str, Any]]:
rows = conn.execute(
"""
WITH latest AS (
SELECT
benchmark_id,
agent,
MAX(started_at) AS max_started_at
FROM benchmark_runs
WHERE status = 'succeeded'
AND score IS NOT NULL
GROUP BY benchmark_id, agent
)
SELECT
r.benchmark_id,
r.run_id,
r.run_group_id,
r.started_at,
r.score,
r.unit,
r.agent,
r.provider,
r.model,
r.high_score_label,
r.high_score_value,
r.delta_to_high_score,
r.token_metrics_json,
r.llm_call_count,
r.total_prompt_tokens,
r.total_completion_tokens,
r.total_cache_read_input_tokens,
r.total_cache_creation_input_tokens
FROM benchmark_runs r
JOIN latest l
ON r.benchmark_id = l.benchmark_id
AND r.agent = l.agent
AND r.started_at = l.max_started_at
WHERE r.status = 'succeeded'
AND r.score IS NOT NULL
ORDER BY r.benchmark_id ASC, r.agent ASC
"""
).fetchall()
out: list[dict[str, Any]] = []
for row in rows:
record = dict(row)
raw_token_metrics = record.pop("token_metrics_json", None)
token_metrics: dict[str, Any] = {}
if isinstance(raw_token_metrics, str):
try:
decoded = json.loads(raw_token_metrics)
except json.JSONDecodeError:
decoded = {}
if isinstance(decoded, dict):
token_metrics = decoded
prompt_tokens = _int_or_none(
token_metrics.get("input_tokens", token_metrics.get("prompt_tokens"))
)
if prompt_tokens is None:
prompt_tokens = _int_or_none(record.get("total_prompt_tokens"))
completion_tokens = _int_or_none(
token_metrics.get("output_tokens", token_metrics.get("completion_tokens"))
)
if completion_tokens is None:
completion_tokens = _int_or_none(record.get("total_completion_tokens"))
total_tokens = _int_or_none(token_metrics.get("total_tokens"))
if total_tokens is None and prompt_tokens is not None and completion_tokens is not None:
total_tokens = prompt_tokens + completion_tokens
cached_tokens = _int_or_none(
token_metrics.get("cached_tokens", token_metrics.get("cache_read_input_tokens"))
)
if cached_tokens is None:
cached_tokens = _int_or_none(record.get("total_cache_read_input_tokens"))
calls = _int_or_none(token_metrics.get("llm_call_count", token_metrics.get("call_count")))
if calls is None:
calls = _int_or_none(record.get("llm_call_count"))
record["input_tokens"] = prompt_tokens if prompt_tokens is not None else 0
record["output_tokens"] = completion_tokens if completion_tokens is not None else 0
record["total_tokens"] = total_tokens if total_tokens is not None else 0
record["cached_tokens"] = cached_tokens if cached_tokens is not None else 0
record["cache_creation_input_tokens"] = _int_or_none(
token_metrics.get("cache_creation_input_tokens")
)
if record["cache_creation_input_tokens"] is None:
record["cache_creation_input_tokens"] = _int_or_none(
record.get("total_cache_creation_input_tokens")
) or 0
record["llm_call_count"] = calls if calls is not None else 0
record["call_count"] = _int_or_none(token_metrics.get("call_count"))
if record["call_count"] is None:
record["call_count"] = record["llm_call_count"]
record["token_metrics"] = {
"input_tokens": record["input_tokens"],
"output_tokens": record["output_tokens"],
"total_tokens": record["total_tokens"],
"cached_tokens": record["cached_tokens"],
"cache_creation_input_tokens": record["cache_creation_input_tokens"],
"llm_call_count": record["llm_call_count"],
"call_count": record["call_count"],
}
out.append(record)
return out
def recover_stale_running_runs(
conn: sqlite3.Connection,
*,
stale_before: str,
ended_at: str,
) -> list[str]:
rows = conn.execute(
"""
SELECT run_id, run_group_id, started_at
FROM benchmark_runs
WHERE status = 'running'
AND started_at < ?
ORDER BY started_at ASC
""",
(stale_before,),
).fetchall()
if not rows:
return []
recovered_ids: list[str] = []
touched_groups: set[str] = set()
metrics_json = _json_dumps({"reason": "orchestrator_interrupted"})
ended_dt = datetime.fromisoformat(ended_at)
if ended_dt.tzinfo is None:
ended_dt = ended_dt.replace(tzinfo=UTC)
for row in rows:
run_id = str(row["run_id"])
run_group_id = str(row["run_group_id"])
started_raw = str(row["started_at"])
duration_seconds: float | None = None
try:
started_dt = datetime.fromisoformat(started_raw)
if started_dt.tzinfo is None:
started_dt = started_dt.replace(tzinfo=UTC)
duration_seconds = max(0.0, (ended_dt - started_dt).total_seconds())
except ValueError:
duration_seconds = None
conn.execute(
"""
UPDATE benchmark_runs
SET
status = 'failed',
ended_at = ?,
duration_seconds = ?,
metrics_json = ?,
error = ?,
result_json_path = NULL
WHERE run_id = ?
""",
(
ended_at,
duration_seconds,
metrics_json,
"Recovered stale running run from interrupted orchestrator process",
run_id,
),
)
recovered_ids.append(run_id)
touched_groups.add(run_group_id)
for run_group_id in sorted(touched_groups):
still_running = conn.execute(
"""
SELECT 1
FROM benchmark_runs
WHERE run_group_id = ? AND status = 'running'
LIMIT 1
""",
(run_group_id,),
).fetchone()
if still_running is None:
conn.execute(
"""
UPDATE run_groups
SET finished_at = COALESCE(finished_at, ?)
WHERE run_group_id = ?
""",
(ended_at, run_group_id),
)
conn.commit()
return recovered_ids