-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate_agent_metrics.py
More file actions
executable file
·1473 lines (1333 loc) · 58 KB
/
aggregate_agent_metrics.py
File metadata and controls
executable file
·1473 lines (1333 loc) · 58 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
1000
#!/usr/bin/env python3
"""Aggregate agent metrics NDJSON into a markdown summary."""
from __future__ import annotations
import datetime as _dt
import json
import os
import re
import sys
from collections import Counter
from collections.abc import Iterable
from dataclasses import dataclass, replace
from pathlib import Path
from typing import Any
_DEFAULT_METRICS_DIR = "agent-metrics"
_DEFAULT_OUTPUT = "agent-metrics-summary.md"
_DEFAULT_JSON_OUTPUT = "agent-metrics-summary.json"
_DEFAULT_DOWNLOAD_MANIFEST_PATH = "artifacts/metric-artifact-download-manifest.json"
_DEFAULT_ARTIFACT_SELECTION_PATH = "artifacts/metric-artifacts-selection.json"
_DEFAULT_UNSUPPORTED_VERIFIER_MODELS = {"gpt-5.2-codex"}
_DEFAULT_VERIFIER_MODEL_METADATA_REQUIRED_AFTER = ""
_NULL_EQUIVALENT_TOKENS = {
"",
"0",
"false",
"none",
"null",
"nil",
"n/a",
"na",
"off",
"disabled",
"undefined",
}
_KNOWN_VERIFIER_MODES = {"checkbox", "compare", "evaluate"}
_TERMINAL_ARTIFACT_FAMILIES = (
"review-thread-terminal-disposition",
"verifier-terminal-disposition",
)
_EXACT_ARTIFACT_FAMILIES = {
"keepalive-metrics",
"agents-autofix-metrics",
"agents-verifier-metrics",
"agents-verifier-disposition-metrics",
"codex-cli-freshness",
"pr-source-context",
}
_PREFIXED_ARTIFACT_FAMILIES = (
"autopilot-metrics-",
"issue-optimizer-metrics-",
"issue-intake-format-metrics-",
"codex-cli-freshness-",
"verifier-terminal-disposition-",
"review-thread-terminal-disposition-",
)
_PATTERNED_ARTIFACT_FAMILIES = (
(
"bot-comment-auth-coverage-wrapper",
re.compile(r"^bot-comment-auth-coverage-wrapper(?:-[A-Za-z0-9][A-Za-z0-9._-]*)?$"),
),
(
"bot-comment-auth-coverage-reusable",
re.compile(r"^bot-comment-auth-coverage-reusable(?:-[A-Za-z0-9][A-Za-z0-9._-]*)?$"),
),
)
_MAX_PARSE_ERROR_ROWS = 25
_MAX_STORED_PARSE_ERROR_DETAILS = 250
_MAX_LEGACY_JSON_FALLBACK_LINES = 5000
_MAX_LEGACY_JSON_FALLBACK_BYTES = 1024 * 1024
@dataclass(frozen=True)
class ParseErrorDetail:
path: str
artifact: str
artifact_family: str
line: int | None
reason: str
count: int = 1
def as_dict(self) -> dict[str, Any]:
return {
"path": self.path,
"artifact": self.artifact,
"artifact_family": self.artifact_family,
"line": self.line,
"reason": self.reason,
"count": self.count,
}
@dataclass(frozen=True)
class MetricSource:
path: str
artifact: str
artifact_family: str
def _parse_timestamp(value: Any) -> _dt.datetime | None:
if value is None:
return None
if isinstance(value, (int, float)):
try:
return _dt.datetime.fromtimestamp(float(value), tz=_dt.UTC)
except (ValueError, OSError, OverflowError):
return None
if isinstance(value, str):
text = value.strip()
if not text:
return None
if text.endswith("Z"):
text = f"{text[:-1]}+00:00"
try:
parsed = _dt.datetime.fromisoformat(text)
except ValueError:
return None
if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=_dt.UTC)
return parsed
return None
def _normalize_version_text(value: Any) -> str:
if value is None:
return ""
text = str(value).strip()
if not text:
return ""
match = re.search(r"(\d+\.\d+\.\d+(?:[-+][A-Za-z0-9._-]+)?)", text)
return match.group(1) if match else text
def _normalize_cli_version(value: Any) -> str:
text = str(value).strip() if value is not None else ""
if not text:
return ""
version = _normalize_version_text(text)
lower = text.lower().replace("_", "-")
if version and re.search(r"\bcodex(?:-|\s+)cli\b|\bopenai/codex\b|\bcodex\b", lower):
return f"codex-cli {version}".lower()
return lower
def _normalize_counter_token(value: Any, fallback: str = "unknown") -> str:
text = str(value).strip().lower().replace("_", "-") if value is not None else ""
return text or fallback
def _gather_metrics_files(metrics_paths: list[str], metrics_dir: str) -> list[Path]:
if metrics_paths:
return [Path(path) for path in metrics_paths if path]
root = Path(metrics_dir)
if not root.exists():
return []
return sorted(path for path in root.rglob("*.ndjson") if path.is_file())
def _artifact_family(artifact: str) -> str:
if artifact in _EXACT_ARTIFACT_FAMILIES:
return artifact
for family, pattern in _PATTERNED_ARTIFACT_FAMILIES:
if pattern.match(artifact):
return family
for prefix in _PREFIXED_ARTIFACT_FAMILIES:
if artifact.startswith(prefix):
return prefix.rstrip("-")
return "unknown"
def _is_artifact_id_segment(value: str) -> bool:
return bool(value) and value.isdigit()
def _infer_artifact_name(path: Path) -> str:
parts = path.parts
for index, part in enumerate(parts):
if part == "agent-metrics" and index > 0:
candidate = parts[index - 1]
if _is_artifact_id_segment(candidate) and index > 1:
return parts[index - 2]
return candidate
parent = path.parent.name
if _is_artifact_id_segment(parent) and path.parent.parent.name:
return path.parent.parent.name
if path.parent.name:
return path.parent.name
return "unknown"
def _metric_source(path: Path) -> MetricSource:
artifact = _infer_artifact_name(path)
return MetricSource(
path=path.as_posix(),
artifact=artifact,
artifact_family=_artifact_family(artifact),
)
def _attach_metric_source(entry: dict[str, Any], path: Path) -> dict[str, Any]:
source = _metric_source(path)
enriched = dict(entry)
enriched.setdefault("artifact_name", source.artifact)
enriched.setdefault("artifact_family", source.artifact_family)
enriched.setdefault("metric_artifact", source.artifact)
enriched.setdefault("metric_artifact_family", source.artifact_family)
enriched.setdefault("metric_path", source.path)
return enriched
def _parse_error_detail(path: Path, line: int | None, reason: str) -> ParseErrorDetail:
source = _metric_source(path)
return ParseErrorDetail(
path=source.path,
artifact=source.artifact,
artifact_family=source.artifact_family,
line=line,
reason=reason,
)
def _append_parse_error_detail(
details: list[ParseErrorDetail],
detail: ParseErrorDetail,
*,
detail_limit: int = _MAX_STORED_PARSE_ERROR_DETAILS,
) -> None:
if detail_limit <= 0:
return
if len(details) < detail_limit:
details.append(detail)
return
for index, existing in enumerate(details):
if (
existing.path == detail.path
and existing.artifact == detail.artifact
and existing.artifact_family == detail.artifact_family
and existing.reason == detail.reason
):
details[index] = replace(existing, line=None, count=existing.count + detail.count)
return
displaced = details[-1]
for index, existing in enumerate(details[:-1]):
if (
existing.path == displaced.path
and existing.artifact == displaced.artifact
and existing.artifact_family == displaced.artifact_family
and existing.reason == displaced.reason
):
details[index] = replace(existing, line=None, count=existing.count + displaced.count)
details[-1] = detail
return
overflow_reason = "additional-parse-errors-after-detail-limit"
for index, existing in enumerate(details):
if (
existing.path == "__multiple__"
and existing.artifact == "__multiple__"
and existing.artifact_family == "__multiple__"
and existing.reason == overflow_reason
and existing.line is None
):
details[index] = replace(existing, count=existing.count + detail.count)
return
details[-1] = ParseErrorDetail(
path="__multiple__",
artifact="__multiple__",
artifact_family="__multiple__",
line=None,
reason=overflow_reason,
count=displaced.count + detail.count,
)
def _parse_error_count(parse_error_details: list[ParseErrorDetail]) -> int:
return sum(detail.count for detail in parse_error_details)
def _parse_error_counter(parse_error_details: list[ParseErrorDetail], field: str) -> Counter[str]:
counts: Counter[str] = Counter()
for detail in parse_error_details:
counts[str(getattr(detail, field))] += detail.count
return counts
def _read_ndjson(files: Iterable[Path]) -> tuple[list[dict[str, Any]], list[ParseErrorDetail]]:
entries: list[dict[str, Any]] = []
errors: list[ParseErrorDetail] = []
for path in files:
try:
handle = path.open("r", encoding="utf-8")
except OSError:
errors.append(_parse_error_detail(path, None, "unreadable-file"))
continue
file_entries: list[dict[str, Any]] = []
file_errors: list[ParseErrorDetail] = []
raw_lines_for_fallback: list[str] = []
raw_fallback_bytes = 0
raw_fallback_truncated = False
with handle:
for line_number, line in enumerate(handle, start=1):
raw = line.strip()
if not raw:
continue
if not file_entries and not raw_fallback_truncated:
raw_bytes = len(raw.encode("utf-8")) + 1
fallback_within_limit = (
len(raw_lines_for_fallback) < _MAX_LEGACY_JSON_FALLBACK_LINES
and raw_fallback_bytes + raw_bytes <= _MAX_LEGACY_JSON_FALLBACK_BYTES
)
if fallback_within_limit:
raw_fallback_bytes += raw_bytes
raw_lines_for_fallback.append(raw)
else:
raw_fallback_truncated = True
raw_lines_for_fallback = []
try:
parsed = json.loads(raw)
except json.JSONDecodeError:
_append_parse_error_detail(
file_errors,
_parse_error_detail(path, line_number, "invalid-json"),
)
continue
if isinstance(parsed, dict):
file_entries.append(_attach_metric_source(parsed, path))
raw_lines_for_fallback = []
else:
_append_parse_error_detail(
file_errors,
_parse_error_detail(path, line_number, "non-object-json"),
)
if file_errors and not file_entries and raw_fallback_truncated:
_append_parse_error_detail(
file_errors,
_parse_error_detail(path, None, "legacy-json-fallback-buffer-limit"),
)
if (
file_errors
and not file_entries
and raw_lines_for_fallback
and not raw_fallback_truncated
):
try:
parsed_file = json.loads("\n".join(raw_lines_for_fallback))
except json.JSONDecodeError:
pass
else:
if isinstance(parsed_file, dict):
file_entries.append(_attach_metric_source(parsed_file, path))
file_errors = []
elif isinstance(parsed_file, list) and all(
isinstance(item, dict) for item in parsed_file
):
file_entries.extend(_attach_metric_source(item, path) for item in parsed_file)
file_errors = []
entries.extend(file_entries)
errors.extend(file_errors)
return entries, errors
def _classify_entry(entry: dict[str, Any]) -> str:
schema = entry.get("schema")
if schema == "workflows-terminal-disposition/v1":
return "terminal_disposition"
if schema == "workflows-verifier-followup-ledger/v1":
return "verifier_followup_ledger"
if schema == "workflows-codex-cli-freshness/v1":
return "codex_cli_freshness"
explicit = entry.get("metric_type") or entry.get("type") or entry.get("workflow")
if isinstance(explicit, str):
lowered = explicit.lower()
if "keepalive" in lowered:
return "keepalive"
if "autofix" in lowered:
return "autofix"
if "verifier" in lowered or "verify" in lowered:
return "verifier"
if lowered in ("step", "cycle", "escalation"):
return "autopilot"
if any(key in entry for key in ("iteration_count", "stop_reason", "tasks_total")):
return "keepalive"
if any(key in entry for key in ("attempt_number", "trigger_reason", "fix_applied")):
return "autofix"
if any(key in entry for key in ("verdict", "issues_created", "acceptance_criteria_count")):
return "verifier"
# Auto-pilot step/cycle records have step_name + duration_ms
if "step_name" in entry and "duration_ms" in entry:
return "autopilot"
if "escalation_reason" in entry:
return "autopilot"
return "unknown"
def _safe_float(value: Any) -> float | None:
if value is None or value == "":
return None
try:
return float(value)
except (TypeError, ValueError):
return None
def _safe_int(value: Any) -> int | None:
if value is None or value == "":
return None
try:
return int(value)
except (TypeError, ValueError):
return None
def _unsupported_verifier_models() -> set[str]:
raw = ""
for env_name in (
"UNSUPPORTED_VERIFIER_MODELS",
"TERMINAL_DISPOSITION_UNSUPPORTED_CODEX_MODELS",
):
candidate = os.environ.get(env_name, "")
if candidate.strip():
raw = candidate
break
if not raw:
return set(_DEFAULT_UNSUPPORTED_VERIFIER_MODELS)
return {item.strip().lower() for item in raw.split(",") if item.strip()}
def _verifier_model_metadata_required_after() -> _dt.datetime | None:
raw = (
os.environ.get("TERMINAL_DISPOSITION_VERIFIER_MODEL_METADATA_REQUIRED_AFTER")
or os.environ.get("VERIFIER_MODEL_METADATA_REQUIRED_AFTER")
or _DEFAULT_VERIFIER_MODEL_METADATA_REQUIRED_AFTER
).strip()
if raw.lower() in _NULL_EQUIVALENT_TOKENS:
return None
return _parse_timestamp(raw)
def _verifier_model_metadata_required() -> bool:
raw = (
os.environ.get("TERMINAL_DISPOSITION_VERIFIER_MODEL_METADATA_REQUIRED_AFTER")
or os.environ.get("VERIFIER_MODEL_METADATA_REQUIRED_AFTER")
or _DEFAULT_VERIFIER_MODEL_METADATA_REQUIRED_AFTER
).strip()
return raw.lower() not in _NULL_EQUIVALENT_TOKENS
def _is_pre_contract_verifier_model_record(
entry: dict[str, Any], required_after: _dt.datetime | None
) -> bool:
if required_after is None:
return False
for key in ("created_at", "timestamp", "run_started_at", "time"):
timestamp = _parse_timestamp(entry.get(key))
if timestamp is not None:
return timestamp < required_after
return False
def _is_verifier_terminal_entry(entry: dict[str, Any]) -> bool:
if entry.get("schema") != "workflows-terminal-disposition/v1":
return False
artifact_family = str(entry.get("artifact_family") or "").strip().lower()
workflow = str(entry.get("workflow") or "").strip().lower()
verifier_mode = _normalize_verifier_mode(entry.get("verifier_mode"))
return (
artifact_family == "verifier-terminal-disposition"
or bool(verifier_mode)
or "verifier" in workflow
)
def _normalize_verifier_mode(value: Any) -> str:
verifier_mode = str(value).strip().lower() if value is not None else ""
if verifier_mode in _NULL_EQUIVALENT_TOKENS:
return ""
return verifier_mode
def _verifier_mode_requires_model_metadata(entry: dict[str, Any]) -> bool:
verifier_mode = _normalize_verifier_mode(entry.get("verifier_mode"))
return bool(verifier_mode) and verifier_mode != "evaluate"
def _summarise_keepalive(entries: list[dict[str, Any]]) -> dict[str, Any]:
stop_reasons: Counter[str] = Counter()
actions: Counter[str] = Counter()
gate_results: Counter[str] = Counter()
iterations: list[int] = []
prs: set[int] = set()
tasks_complete = 0
for entry in entries:
stop_reason = entry.get("stop_reason")
if stop_reason:
stop_reasons[str(stop_reason)] += 1
action = entry.get("action")
if action:
actions[str(action)] += 1
gate = entry.get("gate_conclusion") or entry.get("gate_result")
if gate:
gate_results[str(gate)] += 1
iteration = _safe_int(entry.get("iteration_count") or entry.get("iteration"))
if iteration is not None:
iterations.append(iteration)
pr_number = _safe_int(entry.get("pr_number") or entry.get("pr"))
if pr_number is not None:
prs.add(pr_number)
tasks_total = _safe_int(entry.get("tasks_total"))
entry_tasks_complete = _safe_int(entry.get("tasks_complete"))
derived_complete = (
tasks_total is not None
and tasks_total > 0
and entry_tasks_complete is not None
and entry_tasks_complete >= tasks_total
)
if stop_reason == "tasks-complete" or derived_complete:
tasks_complete += 1
avg_iterations = sum(iterations) / len(iterations) if iterations else 0.0
return {
"runs": len(entries),
"prs": len(prs),
"avg_iterations": avg_iterations,
"stop_reasons": stop_reasons,
"actions": actions,
"gate_results": gate_results,
"tasks_complete": tasks_complete,
}
def _summarise_autofix(entries: list[dict[str, Any]]) -> dict[str, Any]:
triggers: Counter[str] = Counter()
gate_results: Counter[str] = Counter()
prs: set[int] = set()
fixes_applied = 0
for entry in entries:
trigger = entry.get("trigger_reason")
if trigger:
triggers[str(trigger)] += 1
gate = entry.get("gate_result_after") or entry.get("gate_result")
if gate:
gate_results[str(gate)] += 1
pr_number = _safe_int(entry.get("pr_number") or entry.get("pr"))
if pr_number is not None:
prs.add(pr_number)
if entry.get("fix_applied") in (True, "true", "True", "1", 1):
fixes_applied += 1
return {
"attempts": len(entries),
"prs": len(prs),
"fixes_applied": fixes_applied,
"triggers": triggers,
"gate_results": gate_results,
}
def _summarise_verifier(
entries: list[dict[str, Any]],
ledger_entries: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
verdicts: Counter[str] = Counter()
terminal_dispositions: Counter[str] = Counter()
terminal_sources: Counter[str] = Counter()
verifier_models: Counter[str] = Counter()
model_selection_reasons: Counter[str] = Counter()
verifier_cli_versions: Counter[str] = Counter()
unsupported_verifier_models: Counter[str] = Counter()
unsupported_model_dispositions: Counter[str] = Counter()
missing_verifier_model_metadata: Counter[str] = Counter()
unsupported_models = _unsupported_verifier_models()
model_metadata_required = _verifier_model_metadata_required()
model_metadata_required_after = _verifier_model_metadata_required_after()
legacy_missing_verifier_model_metadata: Counter[str] = Counter()
verifier_modes: Counter[str] = Counter()
unknown_verifier_modes: Counter[str] = Counter()
ledger_dispositions: Counter[str] = Counter()
ledger_followup_issues: set[int] = set()
ledger_prs: set[int] = set()
ledger_needs_human = 0
ledger_chain_depths: list[int] = []
ledger_policy_records = 0
ledger_policy_actions: Counter[str] = Counter()
ledger_policy_triggers: Counter[str] = Counter()
ledger_policy_depth_limit_exceeded = 0
verifier_run_keys: set[str] = set()
prs: set[int] = set()
issues_created = 0
acceptance_counts: list[int] = []
terminal_records = 0
for index, entry in enumerate(entries):
is_terminal_disposition = entry.get("schema") == "workflows-terminal-disposition/v1"
is_verifier_terminal = _is_verifier_terminal_entry(entry)
if not is_terminal_disposition:
run_id = entry.get("run_id") or entry.get("workflow_run_id")
run_attempt = entry.get("run_attempt")
pr_number_for_key = _safe_int(entry.get("pr_number") or entry.get("pr"))
if run_id:
verifier_run_keys.add(f"run:{run_id}:attempt:{run_attempt or ''}")
elif pr_number_for_key is not None:
verifier_run_keys.add(f"pr:{pr_number_for_key}")
else:
verifier_run_keys.add(f"entry:{index}")
verdict = entry.get("verdict")
if verdict:
verdicts[str(verdict)] += 1
if is_terminal_disposition:
terminal_records += 1
disposition = entry.get("disposition") or entry.get("terminal_state") or "unknown"
terminal_dispositions[str(disposition)] += 1
source_type = entry.get("source_type") or "unknown"
source_id = entry.get("source_id") or "unknown"
terminal_sources[f"{source_type}:{source_id}"] += 1
model = entry.get("codex_model") or entry.get("llm_model") or entry.get("model")
model_text = str(model).strip() if model is not None else ""
if model_text:
normalized_model_text = model_text.lower()
verifier_models[normalized_model_text] += 1
if normalized_model_text in unsupported_models:
unsupported_verifier_models[normalized_model_text] += 1
disposition = entry.get("disposition") or entry.get("terminal_state") or "unknown"
unsupported_model_dispositions[str(disposition)] += 1
elif (
is_verifier_terminal
and model_metadata_required
and _verifier_mode_requires_model_metadata(entry)
):
disposition = entry.get("disposition") or entry.get("terminal_state") or "unknown"
if _is_pre_contract_verifier_model_record(entry, model_metadata_required_after):
legacy_missing_verifier_model_metadata[str(disposition)] += 1
else:
missing_verifier_model_metadata[str(disposition)] += 1
model_selection_reason = entry.get("codex_model_selection_reason") or entry.get(
"model_selection_reason"
)
if model_selection_reason:
model_selection_reasons[str(model_selection_reason)] += 1
cli_version = (
entry.get("codex_cli_version")
or entry.get("llm_cli_version")
or entry.get("cli_version")
)
cli_version_text = str(cli_version).strip() if cli_version is not None else ""
if cli_version_text:
verifier_cli_versions[_normalize_cli_version(cli_version_text)] += 1
verifier_mode = _normalize_verifier_mode(entry.get("verifier_mode"))
if verifier_mode:
verifier_modes[verifier_mode] += 1
if verifier_mode not in _KNOWN_VERIFIER_MODES:
unknown_verifier_modes[verifier_mode] += 1
pr_number = _safe_int(entry.get("pr_number") or entry.get("pr"))
if pr_number is not None:
prs.add(pr_number)
created = _safe_int(entry.get("issues_created"))
if created is not None:
issues_created += created
acceptance = _safe_int(entry.get("acceptance_criteria_count"))
if acceptance is not None:
acceptance_counts.append(acceptance)
for entry in ledger_entries or []:
disposition = str(entry.get("disposition") or "unknown")
ledger_dispositions[disposition] += 1
pr_number = _safe_int(entry.get("pr_number") or entry.get("pr"))
if pr_number is not None:
ledger_prs.add(pr_number)
followup_issue = _safe_int(entry.get("followup_issue_number"))
if followup_issue is not None:
ledger_followup_issues.add(followup_issue)
if bool(entry.get("needs_human")) or disposition == "needs-human":
ledger_needs_human += 1
chain_depth = _safe_int(entry.get("chain_depth"))
if chain_depth is not None:
ledger_chain_depths.append(chain_depth)
policy = entry.get("followup_policy")
if isinstance(policy, dict):
ledger_policy_records += 1
action = str(policy.get("action") or "unknown")
trigger = str(policy.get("trigger") or "unknown")
ledger_policy_actions[action] += 1
ledger_policy_triggers[trigger] += 1
if policy.get("depth_limit_exceeded") in (True, "true", "True", "1", 1):
ledger_policy_depth_limit_exceeded += 1
avg_acceptance = sum(acceptance_counts) / len(acceptance_counts) if acceptance_counts else 0.0
avg_ledger_chain_depth = (
sum(ledger_chain_depths) / len(ledger_chain_depths) if ledger_chain_depths else 0.0
)
return {
"runs": len(verifier_run_keys),
"prs": len(prs),
"verdicts": verdicts,
"issues_created": issues_created,
"avg_acceptance": avg_acceptance,
"terminal_records": terminal_records,
"terminal_dispositions": terminal_dispositions,
"terminal_sources": terminal_sources,
"verifier_models": verifier_models,
"verifier_cli_versions": verifier_cli_versions,
"unsupported_verifier_models": unsupported_verifier_models,
"unsupported_model_dispositions": unsupported_model_dispositions,
"missing_verifier_model_metadata": missing_verifier_model_metadata,
"legacy_missing_verifier_model_metadata": legacy_missing_verifier_model_metadata,
"model_selection_reasons": model_selection_reasons,
"verifier_modes": verifier_modes,
"unknown_verifier_modes": unknown_verifier_modes,
"ledger_records": len(ledger_entries or []),
"ledger_dispositions": ledger_dispositions,
"ledger_prs": len(ledger_prs),
"ledger_followup_issues": len(ledger_followup_issues),
"ledger_needs_human": ledger_needs_human,
"ledger_avg_chain_depth": avg_ledger_chain_depth,
"ledger_max_chain_depth": max(ledger_chain_depths) if ledger_chain_depths else 0,
"ledger_policy_records": ledger_policy_records,
"ledger_policy_actions": ledger_policy_actions,
"ledger_policy_triggers": ledger_policy_triggers,
"ledger_policy_depth_limit_exceeded": ledger_policy_depth_limit_exceeded,
}
def _summarise_autopilot(entries: list[dict[str, Any]]) -> dict[str, Any]:
"""Summarise auto-pilot step/cycle/escalation records."""
issues: set[int] = set()
step_durations: dict[str, list[float]] = {}
step_successes: dict[str, int] = {}
step_failures: dict[str, int] = {}
cycle_counts: Counter[str] = Counter()
failure_reasons: Counter[str] = Counter()
escalation_reasons: Counter[str] = Counter()
cycle_records = 0
cycle_steps_attempted = 0
cycle_steps_completed = 0
escalation_count = 0
needs_human_count = 0
for entry in entries:
issue_num = _safe_int(entry.get("issue_number") or entry.get("issue"))
if issue_num is not None:
issues.add(issue_num)
metric_type = str(entry.get("metric_type", "")).lower()
if metric_type == "escalation":
escalation_count += 1
reason = entry.get("escalation_reason", "unknown")
escalation_reasons[str(reason)] += 1
if "needs-human" in str(reason).lower() or "needs_human" in str(reason).lower():
needs_human_count += 1
continue
if metric_type == "cycle":
cycle_records += 1
cycle_count = _safe_int(entry.get("cycle_count"))
if cycle_count is not None:
cycle_counts[str(cycle_count)] += 1
steps_attempted = _safe_int(entry.get("steps_attempted"))
if steps_attempted is not None:
cycle_steps_attempted += steps_attempted
steps_completed = _safe_int(entry.get("steps_completed"))
if steps_completed is not None:
cycle_steps_completed += steps_completed
continue
step_name = entry.get("step_name")
if not step_name:
continue
duration = _safe_float(entry.get("duration_ms"))
if duration is not None:
step_durations.setdefault(step_name, []).append(duration)
success = entry.get("success")
if success in (True, "true", "True", "1", 1):
step_successes[step_name] = step_successes.get(step_name, 0) + 1
elif success in (False, "false", "False", "0", 0):
step_failures[step_name] = step_failures.get(step_name, 0) + 1
reason = entry.get("failure_reason", "unknown")
if reason and reason != "none":
failure_reasons[str(reason)] += 1
# Compute per-step averages
step_avg_duration: dict[str, float] = {}
for step, durations in step_durations.items():
step_avg_duration[step] = sum(durations) / len(durations) if durations else 0.0
total_steps = sum(step_successes.values()) + sum(step_failures.values())
return {
"records": len(entries),
"issues": len(issues),
"total_steps": total_steps,
"step_avg_duration_ms": step_avg_duration,
"step_successes": step_successes,
"step_failures": step_failures,
"cycle_records": cycle_records,
"cycle_counts": cycle_counts,
"cycle_steps_attempted": cycle_steps_attempted,
"cycle_steps_completed": cycle_steps_completed,
"failure_reasons": failure_reasons,
"escalation_count": escalation_count,
"escalation_reasons": escalation_reasons,
"needs_human_count": needs_human_count,
}
def _summarise_codex_cli_freshness(entries: list[dict[str, Any]]) -> dict[str, Any]:
statuses: Counter[str] = Counter()
packages: Counter[str] = Counter()
pinned_versions: Counter[str] = Counter()
latest_versions: Counter[str] = Counter()
max_major_delta = 0
max_minor_delta = 0
max_patch_delta = 0
update_targets: Counter[str] = Counter()
for entry in entries:
status = _normalize_counter_token(entry.get("status"))
statuses[status] += 1
package = str(entry.get("package") or "unknown").strip() or "unknown"
packages[package] += 1
pinned = _normalize_version_text(entry.get("pinned_version")) or "unknown"
latest = _normalize_version_text(entry.get("latest_version")) or "unknown"
pinned_versions[pinned] += 1
latest_versions[latest] += 1
delta = entry.get("version_delta")
if isinstance(delta, dict):
max_major_delta = max(max_major_delta, _safe_int(delta.get("major")) or 0)
max_minor_delta = max(max_minor_delta, _safe_int(delta.get("minor")) or 0)
max_patch_delta = max(max_patch_delta, _safe_int(delta.get("patch")) or 0)
targets = entry.get("update_targets")
if isinstance(targets, list):
for target in targets:
if not isinstance(target, dict):
continue
path = str(target.get("path") or "").strip()
if path:
update_targets[path] += 1
return {
"records": len(entries),
"statuses": statuses,
"packages": packages,
"pinned_versions": pinned_versions,
"latest_versions": latest_versions,
"outdated_records": statuses.get("outdated", 0),
"latest_unavailable_records": statuses.get("latest-unavailable", 0),
"max_version_delta": {
"major": max_major_delta,
"minor": max_minor_delta,
"patch": max_patch_delta,
},
"update_targets": update_targets,
}
def _format_counter(counter: Counter[str]) -> str:
if not counter:
return "n/a"
parts = [f"{key} ({count})" for key, count in counter.most_common()]
return ", ".join(parts)
def _markdown_table_cell(value: Any) -> str:
text = str(value).replace("\r\n", "\n").replace("\r", "\n")
text = " ".join(part.strip() for part in text.split("\n"))
return text.replace("|", "\\|")
def _format_rate(numerator: int, denominator: int) -> str:
if denominator <= 0:
return "n/a"
rate = (numerator / denominator) * 100
return f"{rate:.1f}% ({numerator}/{denominator})"
def _json_contract_value(value: Any) -> Any:
if isinstance(value, Counter):
return dict(sorted(value.items()))
if isinstance(value, dict):
return {str(key): _json_contract_value(item) for key, item in sorted(value.items())}
if isinstance(value, list):
return [_json_contract_value(item) for item in value]
return value
def _bucket_entries(entries: list[dict[str, Any]]) -> dict[str, list[dict[str, Any]]]:
buckets: dict[str, list[dict[str, Any]]] = {
"keepalive": [],
"autofix": [],
"verifier": [],
"terminal_disposition": [],
"verifier_followup_ledger": [],
"codex_cli_freshness": [],
"autopilot": [],
"unknown": [],
}
for entry in entries:
bucket = _classify_entry(entry)
buckets.setdefault(bucket, []).append(entry)
return buckets
def _summary_metrics_contract(buckets: dict[str, list[dict[str, Any]]]) -> dict[str, Any]:
return _json_contract_value(
{
"keepalive": _summarise_keepalive(buckets["keepalive"]),
"autofix": _summarise_autofix(buckets["autofix"]),
"verifier": _summarise_verifier(
buckets["verifier"] + buckets["terminal_disposition"],
buckets["verifier_followup_ledger"],
),
"autopilot": _summarise_autopilot(buckets["autopilot"]),
"codex_cli_freshness": _summarise_codex_cli_freshness(buckets["codex_cli_freshness"]),
"unknown": {"records": len(buckets["unknown"])},
}
)
def _format_parse_error_details(parse_error_details: list[ParseErrorDetail]) -> list[str]:
if not parse_error_details:
return []
family_counts = _parse_error_counter(parse_error_details, "artifact_family")
artifact_counts = _parse_error_counter(parse_error_details, "artifact")
lines = [
"",
"## Parse Error Details",
f"- By artifact family: {_format_counter(family_counts)}",
f"- By artifact: {_format_counter(artifact_counts)}",
"",
"| Artifact family | Artifact | File | Line | Reason | Count |",
"|-----------------|----------|------|------|--------|-------|",
]
displayed_count = 0
for detail in parse_error_details[:_MAX_PARSE_ERROR_ROWS]:
line = str(detail.line) if detail.line is not None else "n/a"
displayed_count += detail.count
lines.append(
"| "
f"{_markdown_table_cell(detail.artifact_family)} | "
f"{_markdown_table_cell(detail.artifact)} | "
f"{_markdown_table_cell(detail.path)} | "
f"{_markdown_table_cell(line)} | "
f"{_markdown_table_cell(detail.reason)} | "
f"{detail.count} |"
)
remaining = _parse_error_count(parse_error_details) - displayed_count
if remaining > 0:
lines.append("")
lines.append(f"- Additional parse errors omitted from table: {remaining}")
return lines
def _parse_error_contract(parse_error_details: list[ParseErrorDetail]) -> dict[str, Any]:
family_counts = _parse_error_counter(parse_error_details, "artifact_family")
artifact_counts = _parse_error_counter(parse_error_details, "artifact")
reason_counts = _parse_error_counter(parse_error_details, "reason")
details = [detail.as_dict() for detail in parse_error_details[:_MAX_PARSE_ERROR_ROWS]]
detail_count = _parse_error_count(parse_error_details)
emitted_count = sum(detail["count"] for detail in details)
omitted_count = max(0, detail_count - emitted_count)
return {
"count": detail_count,
"stored_detail_count": len(parse_error_details),
"by_artifact_family": dict(sorted(family_counts.items())),
"by_artifact": dict(sorted(artifact_counts.items())),
"by_reason": dict(sorted(reason_counts.items())),
"details": details,
"details_truncated": omitted_count > 0,
"omitted_count": omitted_count,
}
def _metric_source_contract(entries: list[dict[str, Any]]) -> dict[str, Any]:
family_counts = Counter(
str(entry.get("metric_artifact_family") or entry.get("artifact_family") or "unknown")
for entry in entries
)
artifact_counts = Counter(
str(entry.get("metric_artifact") or entry.get("artifact_name") or "unknown")
for entry in entries
)
file_counts = Counter(str(entry.get("metric_path") or "unknown") for entry in entries)
return {
"by_artifact_family": dict(sorted(family_counts.items())),
"by_artifact": dict(sorted(artifact_counts.items())),
"by_file": dict(sorted(file_counts.items())),
}
def _artifact_download_contract(manifest: dict[str, Any], manifest_path: Path) -> dict[str, Any]:
artifacts = manifest.get("artifacts")
failed_artifacts: list[dict[str, Any]] = []
pending_artifacts: list[dict[str, Any]] = []
if isinstance(artifacts, list):
for artifact in artifacts:
if not isinstance(artifact, dict):
continue
download_status = str((artifact.get("download") or {}).get("status") or "pending")
unzip_status = str((artifact.get("unzip") or {}).get("status") or "pending")