Repository navigation
Expand file tree
/
Copy pathagent_loop_report.py
More file actions
1423 lines (1303 loc) · 64.6 KB
/
Copy pathagent_loop_report.py
File metadata and controls
1423 lines (1303 loc) · 64.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
1000
#!/usr/bin/env python3
"""Offline Agent Loop profiler for existing WebCodex observability evidence.
The profiler consumes payload-safe metadata only. ActionAudit is the preferred
source for canonical outer-call, Window, and ModelErgonomics facts. Per-trace
``events.jsonl`` is an optional supplement for observed Runner request metadata;
payload blobs are never opened.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import math
import sqlite3
import sys
from collections import Counter
from pathlib import Path
from typing import Any, Iterable
SCHEMA_VERSION = 1
DEFAULT_CASE_MANIFEST = Path(__file__).with_name("agent_loop_cases.json")
CODE_MODE_TOOLS = frozenset(("code_mode_exec", "code_mode_exec_effectful", "code_mode_exec_mutating"))
CANONICAL_CODE_MODE_SURFACES = ("read_only", "validation", "guarded_edit")
CODE_MODE_SURFACES = frozenset(CANONICAL_CODE_MODE_SURFACES)
LEGACY_CODE_MODE_SURFACE_ALIASES = {
"e1": "read_only",
"e2a": "validation",
"e2b": "guarded_edit",
}
CODE_MODE_SURFACE_CHOICES = (*CANONICAL_CODE_MODE_SURFACES, *LEGACY_CODE_MODE_SURFACE_ALIASES)
RUN_ANNOTATION_SCHEMA_VERSION = 1
REPAIR_REASONS = frozenset((
"invalid_arguments",
"unknown_callable",
"wrong_result_shape",
"javascript_runtime",
"tool_selection_repair",
"other_contract_repair",
))
CORRECTNESS_VERDICTS = frozenset(("pass", "fail", "unavailable"))
VALIDATION_VERDICTS = frozenset(("pass", "fail", "not_required", "unavailable"))
CODE_MODE_COMPOSITION_NUMERIC_FIELDS = (
"nested_calls",
"nested_successes",
"nested_failures",
"max_in_flight",
"duration_ms",
"slot_wait_ms",
"returned_bytes",
"nested_raw_result_bytes_total",
"consequential_calls",
"known_results",
"job_handoffs",
"outcome_unknown",
)
# Additive fields may be absent from historical ActionAudit rows. They get their
# own availability/missing accounting and never invalidate the core composition.
CODE_MODE_COMPOSITION_OPTIONAL_NUMERIC_FIELDS = ("input_bytes",)
class ReportError(ValueError):
"""Deterministic user-facing report input error."""
def _stable_json(value: Any, *, pretty: bool = False) -> str:
if pretty:
return json.dumps(value, indent=2, sort_keys=True, ensure_ascii=False) + "\n"
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
def _write_json(value: Any, output: Path | None) -> None:
text = _stable_json(value, pretty=True)
if output is None:
sys.stdout.write(text)
return
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(text, encoding="utf-8")
def _require_nonempty_string(value: Any, field: str) -> str:
if not isinstance(value, str) or not value.strip():
raise ReportError(f"{field} must be a non-empty string")
return value
def _canonical_code_mode_surface(value: Any) -> str | None:
if not isinstance(value, str):
return None
if value in CODE_MODE_SURFACES:
return value
return LEGACY_CODE_MODE_SURFACE_ALIASES.get(value)
def validate_case_manifest(value: Any) -> dict[str, Any]:
if not isinstance(value, dict):
raise ReportError("case manifest must be a JSON object")
if value.get("schema_version") != 1:
raise ReportError("unsupported case manifest schema_version")
cases = value.get("cases")
if not isinstance(cases, list) or not cases:
raise ReportError("case manifest cases must be a non-empty array")
seen: set[str] = set()
for index, case in enumerate(cases):
if not isinstance(case, dict):
raise ReportError(f"cases[{index}] must be an object")
case_id = _require_nonempty_string(case.get("id"), f"cases[{index}].id")
if case_id in seen:
raise ReportError(f"duplicate case id: {case_id}")
seen.add(case_id)
for field in ("title", "target", "prompt"):
_require_nonempty_string(case.get(field), f"cases[{index}].{field}")
for field in ("correctness", "validation"):
if not isinstance(case.get(field), dict) or not case[field]:
raise ReportError(f"cases[{index}].{field} must be a non-empty object")
if not isinstance(case["validation"].get("required"), bool):
raise ReportError(f"cases[{index}].validation.required must be a boolean")
surface = case.get("code_mode_surface")
if surface is not None and _canonical_code_mode_surface(surface) is None:
raise ReportError(
f"cases[{index}].code_mode_surface must be one of read_only, validation, or guarded_edit"
)
focus = case.get("dogfood_focus")
if focus is not None:
if (
not isinstance(focus, list)
or not focus
or len(focus) > 8
or any(not isinstance(item, str) or not item.strip() for item in focus)
or len(set(focus)) != len(focus)
):
raise ReportError(
f"cases[{index}].dogfood_focus must be 1..8 unique non-empty strings"
)
return value
def load_case_manifest(path: Path) -> dict[str, Any]:
try:
value = json.loads(path.read_text(encoding="utf-8"))
except OSError as exc:
raise ReportError(f"could not read case manifest: {path}") from exc
except json.JSONDecodeError as exc:
raise ReportError(f"case manifest is not valid JSON: {path}:{exc.lineno}") from exc
return validate_case_manifest(value)
def _case_by_id(manifest: dict[str, Any], case_id: str) -> dict[str, Any]:
for case in manifest["cases"]:
if case["id"] == case_id:
return case
raise ReportError(f"unknown case id: {case_id}")
def _require_nonnegative_int(value: Any, field: str) -> int:
if isinstance(value, bool) or not isinstance(value, int) or value < 0:
raise ReportError(f"{field} must be a non-negative integer")
return value
def _reject_unknown_fields(value: dict[str, Any], allowed: set[str], field: str) -> None:
unknown = sorted(set(value) - allowed)
if unknown:
raise ReportError(f"{field} contains unsupported fields: {', '.join(unknown)}")
def validate_run_annotation(value: Any) -> dict[str, Any]:
if not isinstance(value, dict):
raise ReportError("run annotation must be a JSON object")
_reject_unknown_fields(
value,
{
"schema_version",
"case_id",
"variant",
"surface",
"base_revision",
"case_fingerprint",
"repair_turns",
"task_timing",
"correctness",
},
"run annotation",
)
if value.get("schema_version") != RUN_ANNOTATION_SCHEMA_VERSION:
raise ReportError("unsupported run annotation schema_version")
_require_nonempty_string(value.get("case_id"), "run annotation case_id")
variant = value.get("variant")
if variant not in ("direct", "code_mode"):
raise ReportError("run annotation variant must be direct or code_mode")
surface = value.get("surface")
if variant == "direct":
if surface != "direct":
raise ReportError("direct run annotation requires surface=direct")
elif _canonical_code_mode_surface(surface) is None:
raise ReportError("code_mode run annotation requires surface=read_only, validation, or guarded_edit")
if not _is_exact_git_revision(value.get("base_revision")):
raise ReportError("run annotation base_revision must be an exact 40-hex Git commit")
if not _is_exact_sha256(value.get("case_fingerprint")):
raise ReportError("run annotation case_fingerprint must be an exact 64-hex SHA-256")
repair_turns = value.get("repair_turns")
if repair_turns is not None:
if not isinstance(repair_turns, dict):
raise ReportError("run annotation repair_turns must be an object")
_reject_unknown_fields(repair_turns, {"total", "by_reason"}, "run annotation repair_turns")
total = _require_nonnegative_int(
repair_turns.get("total"), "run annotation repair_turns.total"
)
by_reason = repair_turns.get("by_reason")
if not isinstance(by_reason, dict):
raise ReportError("run annotation repair_turns.by_reason must be an object")
normalized_total = 0
for reason, count in by_reason.items():
if reason not in REPAIR_REASONS:
raise ReportError(f"unsupported repair reason: {reason}")
normalized_total += _require_nonnegative_int(
count, f"run annotation repair_turns.by_reason.{reason}"
)
if normalized_total != total:
raise ReportError("run annotation repair_turns.total must equal by_reason sum")
task_timing = value.get("task_timing")
if task_timing is not None:
if not isinstance(task_timing, dict):
raise ReportError("run annotation task_timing must be an object")
_reject_unknown_fields(
task_timing,
{"started_at_ms", "ended_at_ms"},
"run annotation task_timing",
)
started = _require_nonnegative_int(
task_timing.get("started_at_ms"), "run annotation task_timing.started_at_ms"
)
ended = _require_nonnegative_int(
task_timing.get("ended_at_ms"), "run annotation task_timing.ended_at_ms"
)
if ended < started:
raise ReportError("run annotation task_timing ended_at_ms precedes started_at_ms")
correctness = value.get("correctness")
if correctness is not None:
if not isinstance(correctness, dict):
raise ReportError("run annotation correctness must be an object")
_reject_unknown_fields(
correctness, {"task_verdict", "validation_verdict"}, "run annotation correctness"
)
if correctness.get("task_verdict") not in CORRECTNESS_VERDICTS:
raise ReportError(
"run annotation correctness.task_verdict must be pass, fail, or unavailable"
)
if correctness.get("validation_verdict") not in VALIDATION_VERDICTS:
raise ReportError(
"run annotation correctness.validation_verdict must be pass, fail, not_required, or unavailable"
)
return value
def load_run_annotation(path: Path) -> dict[str, Any]:
try:
value = json.loads(path.read_text(encoding="utf-8"))
except OSError as exc:
raise ReportError(f"could not read run annotation: {path}") from exc
except json.JSONDecodeError as exc:
raise ReportError(f"run annotation is not valid JSON: {path}:{exc.lineno}") from exc
return validate_run_annotation(value)
def load_trace_events(trace_root: Path) -> tuple[set[str], list[dict[str, Any]], int]:
if not trace_root.is_dir():
raise ReportError(f"trace root is not a directory: {trace_root}")
paths = sorted(trace_root.rglob("events.jsonl"))
trace_ids: set[str] = set()
events: list[dict[str, Any]] = []
for path in paths:
seen: set[str] = set()
try:
lines = path.read_text(encoding="utf-8").splitlines()
except (OSError, UnicodeDecodeError) as exc:
raise ReportError(f"could not read trace metadata: {path}") from exc
for line_number, raw in enumerate(lines, start=1):
if not raw.strip():
continue
try:
event = json.loads(raw)
except json.JSONDecodeError as exc:
raise ReportError(f"malformed JSONL in {path}:{line_number}: {exc.msg}") from exc
if not isinstance(event, dict):
raise ReportError(f"trace event must be an object: {path}:{line_number}")
fingerprint = _stable_json(event)
if fingerprint in seen:
continue
seen.add(fingerprint)
trace_id = event.get("server_trace_id")
if isinstance(trace_id, str) and trace_id:
trace_ids.add(trace_id)
events.append(event)
return trace_ids, events, len(paths)
_AUDIT_COLUMNS = """
e.event_id, e.operation, e.action_name, e.project, e.status,
e.ids_json, e.summary_json, e.client_window_key, e.server_trace_id,
e.principal_correlation_kind, e.principal_correlation_id,
e.window_started_at_ms, e.request_observed_at_ms, e.response_handed_at_ms,
e.window_transition_kind, e.response_streaming,
e.window_continuity_eligible, e.window_meaningful, e.started_at
""".strip()
_CONTINUITY_COLUMNS = """
e.event_id, e.action_name, e.client_window_key,
e.principal_correlation_kind, e.principal_correlation_id,
e.window_started_at_ms, e.request_observed_at_ms, e.response_handed_at_ms,
e.window_transition_kind, e.window_continuity_eligible,
e.window_meaningful, e.started_at
""".strip()
def _open_sqlite_readonly(path: Path) -> sqlite3.Connection:
if not path.is_file():
raise ReportError(f"audit DB is not a file: {path}")
try:
connection = sqlite3.connect(f"{path.resolve().as_uri()}?mode=ro", uri=True)
except sqlite3.Error as exc:
raise ReportError(f"could not open audit DB read-only: {path}") from exc
connection.row_factory = sqlite3.Row
return connection
def _parse_json_object(raw: Any, field: str, event_id: str) -> dict[str, Any]:
if not isinstance(raw, str):
raise ReportError(f"{field} is not text for action event {event_id}")
try:
value = json.loads(raw)
except json.JSONDecodeError as exc:
raise ReportError(f"{field} is invalid JSON for action event {event_id}") from exc
if not isinstance(value, dict):
raise ReportError(f"{field} is not an object for action event {event_id}")
return value
def _row_to_audit_event(row: sqlite3.Row) -> dict[str, Any]:
event_id = str(row["event_id"])
return {
"event_id": event_id,
"operation": row["operation"],
"action_name": row["action_name"],
"project": row["project"],
"status": row["status"],
"ids": _parse_json_object(row["ids_json"], "ids_json", event_id),
"summary": _parse_json_object(row["summary_json"], "summary_json", event_id),
"client_window_key": row["client_window_key"],
"server_trace_id": row["server_trace_id"],
"principal_correlation_kind": row["principal_correlation_kind"],
"principal_correlation_id": row["principal_correlation_id"],
"window_started_at_ms": row["window_started_at_ms"],
"request_observed_at_ms": row["request_observed_at_ms"],
"response_handed_at_ms": row["response_handed_at_ms"],
"window_transition_kind": row["window_transition_kind"],
"response_streaming": None if row["response_streaming"] is None else bool(row["response_streaming"]),
"window_continuity_eligible": None if row["window_continuity_eligible"] is None else bool(row["window_continuity_eligible"]),
"window_meaningful": bool(row["window_meaningful"]),
"started_at": row["started_at"],
}
def _row_to_continuity_event(row: sqlite3.Row) -> dict[str, Any]:
return {
"event_id": str(row["event_id"]),
"action_name": row["action_name"],
"client_window_key": row["client_window_key"],
"principal_correlation_kind": row["principal_correlation_kind"],
"principal_correlation_id": row["principal_correlation_id"],
"window_started_at_ms": row["window_started_at_ms"],
"request_observed_at_ms": row["request_observed_at_ms"],
"response_handed_at_ms": row["response_handed_at_ms"],
"window_transition_kind": row["window_transition_kind"],
"window_continuity_eligible": None if row["window_continuity_eligible"] is None else bool(row["window_continuity_eligible"]),
"window_meaningful": bool(row["window_meaningful"]),
"started_at": row["started_at"],
}
def load_audit_events(audit_db: Path, *, workflow_session_id: str | None, trace_ids: set[str]) -> list[dict[str, Any]]:
connection = _open_sqlite_readonly(audit_db)
try:
tables = {row[0] for row in connection.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()}
if "action_events" not in tables:
raise ReportError("audit DB does not contain action_events")
if workflow_session_id is not None:
if "action_event_workflow_links" not in tables:
raise ReportError("audit DB does not contain action_event_workflow_links")
sql = f"""
SELECT DISTINCT {_AUDIT_COLUMNS}
FROM action_events e
JOIN action_event_workflow_links l ON l.event_id = e.event_id
WHERE l.workflow_session_id = ?
ORDER BY COALESCE(e.request_observed_at_ms, e.window_started_at_ms, e.started_at * 1000), e.event_id
"""
rows = connection.execute(sql, (workflow_session_id,)).fetchall()
elif trace_ids:
rows = []
ordered_ids = sorted(trace_ids)
for start in range(0, len(ordered_ids), 400):
chunk = ordered_ids[start:start + 400]
placeholders = ",".join("?" for _ in chunk)
sql = f"""
SELECT {_AUDIT_COLUMNS}
FROM action_events e
WHERE e.server_trace_id IN ({placeholders})
ORDER BY COALESCE(e.request_observed_at_ms, e.window_started_at_ms, e.started_at * 1000), e.event_id
"""
rows.extend(connection.execute(sql, chunk).fetchall())
rows.sort(key=lambda row: (
row["request_observed_at_ms"] if row["request_observed_at_ms"] is not None
else row["window_started_at_ms"] if row["window_started_at_ms"] is not None
else int(row["started_at"]) * 1000,
row["event_id"],
))
else:
raise ReportError("audit DB selection requires --workflow-session-id or trace ids from --trace-root")
return [_row_to_audit_event(row) for row in rows]
except sqlite3.Error as exc:
raise ReportError(f"could not query audit DB: {exc}") from exc
finally:
connection.close()
def load_audit_continuity_events(
audit_db: Path, selected_events: list[dict[str, Any]]
) -> list[dict[str, Any]]:
selected_meaningful = [
event
for event in selected_events
if event.get("action_name") == "toolsCall" and event.get("window_meaningful")
]
keyed = [
event
for event in selected_meaningful
if all(
isinstance(event.get(field), str) and event.get(field)
for field in (
"client_window_key",
"principal_correlation_kind",
"principal_correlation_id",
)
)
and isinstance(event.get("request_observed_at_ms"), int)
]
if not keyed:
return selected_meaningful
keys = {
(
event["client_window_key"],
event["principal_correlation_kind"],
event["principal_correlation_id"],
)
for event in keyed
}
windows = sorted({key[0] for key in keys})
first_started = min(event["request_observed_at_ms"] for event in keyed)
last_started = max(event["request_observed_at_ms"] for event in keyed)
connection = _open_sqlite_readonly(audit_db)
rows: list[sqlite3.Row] = []
try:
for start in range(0, len(windows), 400):
chunk = windows[start:start + 400]
placeholders = ",".join("?" for _ in chunk)
sql = f"""
SELECT {_CONTINUITY_COLUMNS}
FROM action_events e
WHERE e.action_name = 'toolsCall'
AND e.window_meaningful = 1
AND e.request_observed_at_ms BETWEEN ? AND ?
AND e.client_window_key IN ({placeholders})
ORDER BY COALESCE(e.request_observed_at_ms, e.window_started_at_ms, e.started_at * 1000), e.event_id
"""
rows.extend(
connection.execute(sql, [first_started, last_started, *chunk]).fetchall()
)
except sqlite3.Error as exc:
raise ReportError(f"could not query audit continuity context: {exc}") from exc
finally:
connection.close()
by_id = {event["event_id"]: event for event in selected_meaningful}
for row in rows:
event = _row_to_continuity_event(row)
key = (
event.get("client_window_key"),
event.get("principal_correlation_kind"),
event.get("principal_correlation_id"),
)
if key in keys:
by_id[event["event_id"]] = event
return sorted(by_id.values(), key=_audit_sort_key)
def _telemetry(event: dict[str, Any]) -> dict[str, Any] | None:
value = event.get("summary", {}).get("model_ergonomics")
return value if isinstance(value, dict) else None
def _code_mode_composition(event: dict[str, Any]) -> dict[str, Any] | None:
value = event.get("summary", {}).get("code_mode_composition")
if not isinstance(value, dict):
return None
normalized: dict[str, Any] = {}
for field in CODE_MODE_COMPOSITION_NUMERIC_FIELDS:
item = value.get(field)
if isinstance(item, bool) or not isinstance(item, int) or item < 0:
return None
normalized[field] = item
for field in CODE_MODE_COMPOSITION_OPTIONAL_NUMERIC_FIELDS:
item = value.get(field)
if item is None:
continue
if isinstance(item, bool) or not isinstance(item, int) or item < 0:
return None
normalized[field] = item
counts = value.get("nested_tool_counts")
if not isinstance(counts, dict):
return None
normalized_counts: dict[str, int] = {}
for tool, count in counts.items():
if (
not isinstance(tool, str)
or not tool
or isinstance(count, bool)
or not isinstance(count, int)
or count < 0
):
return None
normalized_counts[tool] = count
if normalized["nested_successes"] + normalized["nested_failures"] != normalized["nested_calls"]:
return None
if sum(normalized_counts.values()) != normalized["nested_calls"]:
return None
if normalized["known_results"] + normalized["job_handoffs"] + normalized["outcome_unknown"] != normalized["consequential_calls"]:
return None
normalized["nested_tool_counts"] = normalized_counts
return normalized
def _nearest_rank(values: Iterable[int], percentile: float) -> int | None:
ordered = sorted(values)
if not ordered:
return None
rank = max(1, math.ceil(percentile * len(ordered)))
return ordered[rank - 1]
def _metric_distribution(values: list[int], *, missing: int = 0) -> dict[str, Any]:
observed_total = sum(values)
return {
"total": observed_total if missing == 0 else None,
"observed_total": observed_total,
"p50": _nearest_rank(values, 0.50),
"p95": _nearest_rank(values, 0.95),
"samples": len(values),
"missing": missing,
}
def _summarize_code_mode_composition(
outer: list[dict[str, Any]],
variant: str | None,
*,
action_audit_available: bool = True,
) -> tuple[dict[str, Any], dict[str, Any]]:
if not action_audit_available:
metrics = {
field: _metric_distribution([], missing=1)
for field in (
*CODE_MODE_COMPOSITION_NUMERIC_FIELDS,
*CODE_MODE_COMPOSITION_OPTIONAL_NUMERIC_FIELDS,
)
}
return (
{
"outer_code_mode_calls": None,
"outer_calls_with_summary": None,
"nested_tool_counts": {},
**metrics,
},
{
"available": False,
"reason": "Code Mode composition is persisted in ActionAudit summary metadata, not in trace-only evidence",
},
)
code_mode_outer = [event for event in outer if event.get("operation") in CODE_MODE_TOOLS]
parsed = [
composition
for event in code_mode_outer
if (composition := _code_mode_composition(event)) is not None
]
missing = len(code_mode_outer) - len(parsed)
reason = None
if variant == "code_mode" and not code_mode_outer:
missing = max(missing, 1)
reason = (
"no Code Mode outer ActionAudit row was selected; benchmark calls must link their outer "
"ActionAudit rows with recording_session_id"
)
elif variant == "direct" and code_mode_outer:
missing = max(missing, 1)
reason = "a direct report selected one or more Code Mode outer calls"
elif missing:
reason = "one or more Code Mode outer calls lack a valid code_mode_composition summary"
metrics = {
field: _metric_distribution([int(value[field]) for value in parsed], missing=missing)
for field in CODE_MODE_COMPOSITION_NUMERIC_FIELDS
}
for field in CODE_MODE_COMPOSITION_OPTIONAL_NUMERIC_FIELDS:
values = [int(value[field]) for value in parsed if field in value]
metrics[field] = _metric_distribution(
values,
missing=len(code_mode_outer) - len(values),
)
nested_tool_counts: Counter[str] = Counter()
for value in parsed:
nested_tool_counts.update(value["nested_tool_counts"])
available = missing == 0 and reason is None
return (
{
"outer_code_mode_calls": len(code_mode_outer),
"outer_calls_with_summary": len(parsed),
"nested_tool_counts": dict(sorted(nested_tool_counts.items())),
**metrics,
},
{"available": available, "reason": reason},
)
def _audit_sort_key(event: dict[str, Any]) -> tuple[int, str]:
for field in ("request_observed_at_ms", "window_started_at_ms"):
value = event.get(field)
if isinstance(value, int):
return value, str(event["event_id"])
started_at = event.get("started_at")
return (int(started_at) * 1000 if isinstance(started_at, int) else 0, str(event["event_id"]))
def _window_timing(
outer: list[dict[str, Any]],
continuity_events: list[dict[str, Any]] | None = None,
) -> tuple[dict[str, Any], int, int]:
selected_ids = {str(event["event_id"]) for event in outer}
previous: dict[tuple[str, str, str], dict[str, Any]] = {}
gaps: list[int] = []
missing_serial = 0
overlap_count = 0
source = continuity_events if continuity_events is not None else outer
for event in sorted(source, key=_audit_sort_key):
if not event.get("window_meaningful"):
continue
selected = str(event["event_id"]) in selected_ids
window_key = event.get("client_window_key")
principal_kind = event.get("principal_correlation_kind")
principal_id = event.get("principal_correlation_id")
if not all(isinstance(value, str) and value for value in (window_key, principal_kind, principal_id)):
if selected and event.get("window_transition_kind") == "serial":
missing_serial += 1
continue
key = (window_key, principal_kind, principal_id)
predecessor = previous.pop(key, None)
transition = event.get("window_transition_kind")
if selected and transition == "overlap":
overlap_count += 1
if selected and transition == "serial":
current_started = event.get("request_observed_at_ms")
previous_handed = predecessor.get("response_handed_at_ms") if predecessor else None
predecessor_selected = (
predecessor is not None
and str(predecessor["event_id"]) in selected_ids
)
if (
predecessor_selected
and isinstance(current_started, int)
and isinstance(previous_handed, int)
and current_started >= previous_handed
):
gaps.append(current_started - previous_handed)
else:
missing_serial += 1
if event.get("window_continuity_eligible") is True:
previous[key] = event
return _metric_distribution(gaps, missing=missing_serial), overlap_count, missing_serial
def _observed_span_ms(outer: list[dict[str, Any]]) -> int | None:
starts = [event["request_observed_at_ms"] for event in outer if isinstance(event.get("request_observed_at_ms"), int)]
ends = [event["response_handed_at_ms"] for event in outer if event.get("response_streaming") is False and isinstance(event.get("response_handed_at_ms"), int)]
if not starts or not ends:
return None
span = max(ends) - min(starts)
return span if span >= 0 else None
def _summarize_audit(
audit_events: list[dict[str, Any]],
variant: str | None,
continuity_events: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
outer = [event for event in audit_events if event.get("action_name") == "toolsCall"]
statuses = Counter(str(event.get("status") or "unknown") for event in outer)
outer_tools = Counter(event["operation"] for event in outer if isinstance(event.get("operation"), str))
meaningful_calls = sum(bool(event.get("window_meaningful")) for event in outer)
composition, composition_availability = _summarize_code_mode_composition(outer, variant)
failed_child_calls = (
composition["nested_failures"]["total"]
if composition_availability["available"]
else None
)
service_values: list[int] = []
for event in outer:
started = event.get("request_observed_at_ms")
handed = event.get("response_handed_at_ms")
if event.get("response_streaming") is False and isinstance(started, int) and isinstance(handed, int) and handed >= started:
service_values.append(handed - started)
service = _metric_distribution(service_values, missing=len(outer) - len(service_values))
telemetries = [(event, _telemetry(event)) for event in outer]
present_telemetries = [(event, value) for event, value in telemetries if value is not None]
runtime_durations = [int(value["duration_ms"]) for _, value in present_telemetries if isinstance(value.get("duration_ms"), int) and value["duration_ms"] >= 0]
runtime_duration = _metric_distribution(runtime_durations, missing=len(outer) - len(runtime_durations))
result_bytes = [int(value["serialized_result_bytes"]) for _, value in present_telemetries if isinstance(value.get("serialized_result_bytes"), int) and value["serialized_result_bytes"] >= 0]
result_byte_metric = _metric_distribution(result_bytes, missing=len(outer) - len(result_bytes))
failure_kinds: Counter[str] = Counter()
recovery_guidance: Counter[str] = Counter()
error_kinds: Counter[str] = Counter()
for _, value in present_telemetries:
for field, counter in (("failure_kind", failure_kinds), ("recovery_kind", recovery_guidance), ("error_kind", error_kinds)):
item = value.get(field)
if isinstance(item, str) and item:
counter[item] += 1
gaps, overlap_count, missing_serial = _window_timing(outer, continuity_events)
canonical_observed = len(present_telemetries)
canonical_by_name = Counter(value["tool_name"] for _, value in present_telemetries if isinstance(value.get("tool_name"), str) and value["tool_name"])
if variant == "direct":
canonical_total = canonical_observed if canonical_observed == len(outer) else None
canonical_reason = None if canonical_total is not None else "one or more outer calls lack ModelErgonomics evidence"
elif variant == "code_mode":
canonical_total = None
canonical_reason = "canonical_calls.total keeps the outer/direct counting contract; use composition.nested_calls for persisted Code Mode child-call totals"
else:
canonical_total = None
canonical_reason = "declare --variant direct or code_mode before interpreting canonical call count"
return {
"observed_span_ms": _observed_span_ms(outer),
"model_round_trips": None,
"model_round_trip_proxy": meaningful_calls,
"outer_calls": {
"total": len(outer),
"meaningful": meaningful_calls,
"successful": statuses.get("success", 0),
"failed": statuses.get("failed", 0),
"timeout_or_unknown": len(outer) - statuses.get("success", 0) - statuses.get("failed", 0),
"by_status": dict(sorted(statuses.items())),
},
"tools": {"outer_by_name": dict(sorted(outer_tools.items()))},
"canonical_calls": {
"total": canonical_total,
"observed_outer_runtime_records": canonical_observed,
"by_name": dict(sorted(canonical_by_name.items())),
},
"composition": composition,
"child_calls": {
"failed": failed_child_calls,
"failure_kind_by_name": None,
},
"timing": {
"webcodex_service_ms": service,
"tool_runtime_ms": runtime_duration,
"outside_webcodex_gap_ms": gaps,
"overlap_count": overlap_count,
},
"results": {"serialized_tool_result_bytes": result_byte_metric},
"failures": {
"error_kind_by_name": dict(sorted(error_kinds.items())),
"failure_kind_by_name": dict(sorted(failure_kinds.items())),
"recovery_guidance_by_kind": dict(sorted(recovery_guidance.items())),
"child_failure_kind_by_name": None,
"resolved_recoveries": None,
},
"availability": {
"action_audit": {"available": True},
"model_round_trips": {
"available": False,
"reason": "ActionAudit records meaningful outer tool calls but not the model response/turn identity needed to prove exact model round trips",
},
"model_round_trip_proxy": {
"available": True,
"reason": "proxy equals meaningful outer tool calls; parallel outer calls may share one model response",
},
"failed_child_calls": {
"available": failed_child_calls is not None,
"reason": (
None
if failed_child_calls is not None
else "Code Mode child failure count requires a complete ActionAudit code_mode_composition summary"
),
},
"child_failure_kinds": {
"available": False,
"reason": "ActionAudit composition persists authoritative nested failure counts but not per-child failure-kind distribution",
},
"webcodex_service_timing": {"available": service["total"] is not None, "reason": None if service["total"] is not None else "one or more outer calls lack non-streaming request-observed/response-handoff timestamps"},
"tool_runtime_timing": {"available": runtime_duration["total"] is not None, "reason": None if runtime_duration["total"] is not None else "one or more outer calls lack ModelErgonomics runtime duration evidence"},
"window_timing": {"available": missing_serial == 0, "reason": None if missing_serial == 0 else "one or more canonical serial transitions lack the predecessor timestamps needed for a gap"},
"canonical_calls": {"available": canonical_total is not None, "reason": canonical_reason},
"code_mode_composition": composition_availability,
"serialized_tool_result_bytes": {"available": result_byte_metric["total"] is not None, "reason": None if result_byte_metric["total"] is not None else "one or more outer calls lack a serialized ToolResult byte count; missing values are not treated as zero"},
"resolved_recoveries": {"available": False, "reason": "recovery_kind is guidance metadata, not proof that a later call resolved the failure"},
},
}
def _trace_handler_events(trace_events: list[dict[str, Any]]) -> list[dict[str, Any]]:
grouped: dict[str, list[dict[str, Any]]] = {}
for event in trace_events:
if event.get("event") != "tool_handler_returned":
continue
trace_id = event.get("server_trace_id")
if isinstance(trace_id, str) and trace_id:
grouped.setdefault(trace_id, []).append(event)
handlers = []
for trace_id in sorted(grouped):
candidates = grouped[trace_id]
candidates.sort(key=lambda event: (int(event.get("duration_ms") or -1), _stable_json(event)))
handlers.append(candidates[-1])
return handlers
def _summarize_trace_only(trace_events: list[dict[str, Any]], variant: str | None) -> dict[str, Any]:
handlers = _trace_handler_events(trace_events)
statuses: Counter[str] = Counter()
tools: Counter[str] = Counter()
for event in handlers:
tool_success = event.get("tool_success")
protocol_success = event.get("protocol_success")
if tool_success is True or (tool_success is None and protocol_success is True):
statuses["success"] += 1
elif tool_success is False or protocol_success is False:
statuses["failed"] += 1
else:
statuses["unknown"] += 1
tool_name = event.get("tool_name")
if isinstance(tool_name, str) and tool_name:
tools[tool_name] += 1
composition, composition_availability = _summarize_code_mode_composition(
[], variant, action_audit_available=False
)
unavailable = {
"webcodex_service_timing": {"available": False, "reason": "tool_handler_returned duration is not the canonical request-observed to response-handoff service interval"},
"tool_runtime_timing": {"available": False, "reason": "events.jsonl does not persist canonical ModelErgonomics runtime duration evidence"},
"window_timing": {"available": False, "reason": "events.jsonl does not persist canonical meaningful/continuity transition facts"},
"canonical_calls": {"available": False, "reason": "outer trace lifecycle metadata is not a complete canonical nested-call ledger"},
"serialized_tool_result_bytes": {"available": False, "reason": "estimated HTTP response bytes are not ToolResult serialized bytes"},
"resolved_recoveries": {"available": False, "reason": "trace lifecycle metadata does not prove recovery completion"},
}
return {
"observed_span_ms": None,
"model_round_trips": None,
"model_round_trip_proxy": None,
"outer_calls": {"total": len(handlers), "meaningful": None, "successful": statuses.get("success", 0), "failed": statuses.get("failed", 0), "timeout_or_unknown": statuses.get("unknown", 0), "by_status": dict(sorted(statuses.items()))},
"tools": {"outer_by_name": dict(sorted(tools.items()))},
"canonical_calls": {"total": None, "observed_outer_runtime_records": None, "by_name": {}},
"composition": composition,
"child_calls": {"failed": None, "failure_kind_by_name": None},
"timing": {
"webcodex_service_ms": _metric_distribution([], missing=len(handlers)),
"tool_runtime_ms": _metric_distribution([], missing=len(handlers)),
"outside_webcodex_gap_ms": _metric_distribution([]),
"overlap_count": None,
},
"results": {"serialized_tool_result_bytes": _metric_distribution([], missing=len(handlers))},
"failures": {"error_kind_by_name": {}, "failure_kind_by_name": {}, "recovery_guidance_by_kind": {}, "child_failure_kind_by_name": None, "resolved_recoveries": None},
"availability": {
"action_audit": {"available": False, "reason": "no ActionAudit DB evidence was provided"},
"model_round_trips": {
"available": False,
"reason": "events.jsonl does not persist model response/turn identity",
},
"model_round_trip_proxy": {
"available": False,
"reason": "events.jsonl does not persist canonical meaningful outer-call classification",
},
"failed_child_calls": {
"available": False,
"reason": "Code Mode child failure count requires ActionAudit code_mode_composition evidence",
},
"child_failure_kinds": {
"available": False,
"reason": "trace metadata does not persist authoritative per-child failure-kind distribution",
},
"code_mode_composition": composition_availability,
**unavailable,
},
}
def _runner_summary(trace_events: list[dict[str, Any]], trace_root_present: bool) -> tuple[dict[str, Any], dict[str, Any]]:
if not trace_root_present:
return ({"requests_observed": None, "by_kind": {}}, {"available": False, "complete": False, "reason": "no trace-root evidence was provided"})
requests: dict[str, dict[str, Any]] = {}
anonymous: set[str] = set()
for event in trace_events:
if event.get("event") != "tool_runner_request_enqueued":
continue
request_id = event.get("runner_request_id")
if isinstance(request_id, str) and request_id:
requests.setdefault(request_id, event)
else:
anonymous.add(_stable_json(event))
kinds: Counter[str] = Counter()
for event in [*requests.values(), *(json.loads(item) for item in anonymous)]:
kind = event.get("runner_request_kind")
if isinstance(kind, str) and kind:
kinds[kind] += 1
return (
{"requests_observed": len(requests) + len(anonymous), "by_kind": dict(sorted(kinds.items()))},
{"available": True, "complete": False, "reason": "events.jsonl proves observed Runner enqueue events but has no fail-open completeness marker"},
)
def _job_summary(trace_events: list[dict[str, Any]], trace_root_present: bool) -> tuple[dict[str, Any], dict[str, Any]]:
job_ids = {event.get("runner_job_id") for event in trace_events if event.get("event") == "tool_runner_request_enqueued" and isinstance(event.get("runner_job_id"), str) and event.get("runner_job_id")}
return (
{"runner_job_ids_observed": len(job_ids) if trace_root_present else None, "handoffs": None, "terminal": None},
{"available": False, "reason": "runner_job_id may be allocated before synchronous completion and does not prove a same-execution model-visible handoff or terminal observation"},
)
def _is_exact_hex(value: Any, length: int) -> bool:
return (
isinstance(value, str)
and len(value) == length
and all(char in "0123456789abcdefABCDEF" for char in value)
)
def _is_exact_git_revision(value: Any) -> bool:
return _is_exact_hex(value, 40)
def _is_exact_sha256(value: Any) -> bool:
return _is_exact_hex(value, 64)
def _case_fingerprint(case: dict[str, Any]) -> str:
return hashlib.sha256(_stable_json(case).encode("utf-8")).hexdigest()
def _benchmark_metadata(*, case_manifest: Path | None, case_id: str | None, variant: str | None, surface: str | None = None, base_revision: str | None = None) -> dict[str, Any] | None:
if case_id is None:
if case_manifest is not None or base_revision is not None or surface is not None:
raise ReportError("--case-manifest/--base-revision/--surface require --case-id")
return None
if variant is None:
raise ReportError("--case-id requires --variant")
if variant == "direct":
if surface is None:
surface = "direct"
elif surface != "direct":
raise ReportError("--variant direct requires --surface direct")
elif variant == "code_mode":
if surface is None:
raise ReportError("--variant code_mode benchmark runs require --surface read_only, validation, or guarded_edit")
if _canonical_code_mode_surface(surface) is None:
raise ReportError("--variant code_mode requires --surface read_only, validation, or guarded_edit")
if not _is_exact_git_revision(base_revision):
raise ReportError("--case-id requires --base-revision as an exact 40-hex Git commit")
manifest = load_case_manifest(case_manifest or DEFAULT_CASE_MANIFEST)
case = _case_by_id(manifest, case_id)
expected_surface = case.get("code_mode_surface")
if (
variant == "code_mode"
and expected_surface is not None
and _canonical_code_mode_surface(surface)
!= _canonical_code_mode_surface(expected_surface)
):
raise ReportError(
f"case {case_id} requires Code Mode surface {expected_surface}"
)
return {
"manifest_schema_version": manifest["schema_version"],
"case_id": case_id,
"case_title": case["title"],
"target": case["target"],
"variant": variant,
"surface": surface,