forked from ChelseaKR/permit-bearings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconformance_evaluation.py
More file actions
1474 lines (1349 loc) · 54.3 KB
/
Copy pathconformance_evaluation.py
File metadata and controls
1474 lines (1349 loc) · 54.3 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
"""Held-out evaluation scaffolding for the presence-based conformance scanner.
The committed manifest is deliberately ``not_run``. This module validates
that planning record and provides deterministic building blocks for a later,
independently frozen passage/check evaluation. It does not supply passages,
reviewer judgments, or an accuracy claim.
Labels describe review-queue behavior only:
* ``should_flag`` means one exact passage/check pair should enter review;
* ``should_stay_quiet`` means that pair should not enter review; and
* ``reference_abstain`` means reviewers could not establish a defensible
expected queue behavior. It is counted separately and never enters the
binary matrix.
The current scanner itself is binary. Scanner errors fail a run; they are not
converted into machine abstentions.
"""
from __future__ import annotations
import hashlib
import json
import re
from collections import Counter
from dataclasses import dataclass
from datetime import UTC, date, datetime
from pathlib import Path
from typing import Any
from urllib.parse import urlsplit
from .conformance import load_checks, scan
SCHEMA_VERSION = 1
REFERENCE_LABELS = (
"should_flag",
"should_stay_quiet",
"reference_abstain",
)
RAW_COUNT_FIELDS = (
"expected_flag_observed_flag",
"expected_flag_observed_quiet",
"expected_quiet_observed_flag",
"expected_quiet_observed_quiet",
"reference_abstain",
"machine_abstain",
)
PARTITIONS = (
"official_targeted",
"synthetic_targeted",
"official_incidental",
"synthetic_incidental",
"overall",
)
NEAR_DUPLICATE_DISPOSITION = (
"Materially overlapping passages are excluded before freeze."
)
# This pins the complete policy text, so a contradictory sentence cannot be
# appended while preserving a few required substrings.
_CLAIM_BOUNDARY_SHA256 = (
"43201a7fc960ccf3cbc615cddbc78b94ad81be78d0dd30e68092c5c47da0566b"
)
_IDENTIFIER = re.compile(r"^[a-z][a-z0-9]*(?:[-_.][a-z0-9]+)*$")
_SHA256 = re.compile(r"^[0-9a-f]{64}$")
_COMMIT_SHA = re.compile(r"^[0-9a-f]{40}$")
_SAFE_PATH_COMPONENT = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$")
_MAX_JSON_BYTES = 8 * 1024 * 1024
_MAX_CASES = 500
_MAX_TEXT_BYTES = 256 * 1024
_MANIFEST_KEYS = {
"schema_version",
"evaluation_id",
"status",
"scoring_unit",
"scanner",
"freeze",
"inputs",
"output",
"reference_labels",
"raw_count_fields",
"coverage_contract",
"development_source_exclusions",
"external_blockers",
"claim_boundary",
}
_SCANNER_KEYS = {
"module",
"scanner_path",
"scanner_sha256",
"checks_path",
"checks_sha256",
"check_ids",
"evaluator_path",
"evaluator_sha256",
}
_FREEZE_KEYS = {
"freeze_id",
"corpus_frozen_at",
"corpus_repository_commit_sha",
"prediction_generated_at",
"prediction_repository_commit_sha",
"answer_key_unblinded_at",
"scoring_run_at",
"scoring_repository_commit_sha",
"corpus_status",
}
_INPUT_KEYS = {
"cases_path",
"cases_sha256",
"answer_key_path",
"answer_key_sha256",
"predictions_path",
"predictions_sha256",
}
_COVERAGE_KEYS = {
"official_targeted_pairs_per_check",
"synthetic_targeted_pairs_per_check",
"required_category_ids",
"incidental_findings_count_toward_coverage",
"synthetic_controls_reported_separately",
"pair_universe",
"target_checks_per_case",
"multi_target_cases_supported",
"reporting_grains",
}
_MINIMUM_KEYS = {"should_flag", "should_stay_quiet"}
_EXCLUSION_KEYS = {
"source_id",
"canonical_url",
"retained_raw_sha256",
"reason",
"near_duplicate_disposition",
}
_CASES_KEYS = {
"schema_version",
"evaluation_id",
"freeze_id",
"frozen_at",
"corpus_repository_commit_sha",
"selection",
"cases",
}
_SELECTION_KEYS = {
"method",
"custodian_id",
"selected_before_scanner_run",
"near_duplicate_review_completed",
}
_CASE_KEYS = {
"case_id",
"category_id",
"stratum",
"target_check_id",
"selection_role",
"selection_rationale",
"passage",
"passage_sha256",
"source",
}
_SOURCE_KEYS = {
"source_id",
"canonical_url",
"document_sha256",
"passage_locator",
"retrieved_on",
}
_ANSWER_KEY_KEYS = {
"schema_version",
"evaluation_id",
"freeze_id",
"cases_sha256",
"checks_sha256",
"law_as_of",
"check_registry_as_of",
"reviewers",
"adjudication",
"unblinded_at",
}
_REVIEWER_KEYS = {
"reviewer_id",
"qualification",
"method",
"reviewed_at",
"predictions_seen_before_initial_labels",
"judgments",
}
_JUDGMENT_KEYS = {"case_id", "check_id", "label"}
_ADJUDICATION_KEYS = {
"adjudicator_id",
"method",
"adjudicated_at",
"disagreements",
"final_judgments",
}
_DISAGREEMENT_KEYS = {
"case_id",
"check_id",
"rationale",
"citation",
}
_PREDICTIONS_KEYS = {
"schema_version",
"evaluation_id",
"freeze_id",
"generated_at",
"repository_commit_sha",
"bindings",
"machine_output",
"machine_abstain",
"cases",
}
_PREDICTION_BINDING_KEYS = {
"manifest_sha256",
"cases_sha256",
"checks_sha256",
"scanner_sha256",
"evaluator_sha256",
}
_PREDICTION_CASE_KEYS = {
"case_id",
"observed_check_ids",
"finding_counts",
}
_RESULT_KEYS = {
"schema_version",
"evaluation_id",
"freeze_id",
"status",
"scored_at",
"repository_commit_sha",
"bindings",
"chronology",
"reference_as_of",
"machine_output",
"machine_abstain",
"raw_counts_by_partition",
"raw_counts_by_check",
"pair_outcomes",
"corpus_lifecycle",
"claim_boundary",
}
_RESULT_BINDING_KEYS = {
"manifest_sha256",
"cases_sha256",
"answer_key_sha256",
"predictions_sha256",
"checks_sha256",
"scanner_sha256",
"evaluator_sha256",
"corpus_repository_commit_sha",
"prediction_repository_commit_sha",
}
class _DuplicateKey(ValueError):
"""Raised before JSON decoding can overwrite a duplicate object key."""
def _unique_object(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
result: dict[str, Any] = {}
for key, value in pairs:
if key in result:
raise _DuplicateKey(key)
result[key] = value
return result
def _reject_constant(value: str) -> None:
raise ValueError(f"unsupported JSON constant {value!r}")
def _read_json_bytes(raw: bytes, field: str) -> Any:
try:
return json.loads(
raw.decode("utf-8"),
object_pairs_hook=_unique_object,
parse_constant=_reject_constant,
)
except _DuplicateKey as error:
raise ValueError(f"{field}: duplicate JSON key {error}") from error
except (UnicodeDecodeError, json.JSONDecodeError, RecursionError) as error:
raise ValueError(f"{field}: invalid UTF-8 JSON") from error
def _read_json_file(path: Path, field: str) -> tuple[Any, bytes]:
try:
metadata = path.lstat()
except OSError as error:
raise ValueError(f"{field}: expected a readable regular file") from error
if path.is_symlink() or not path.is_file() or metadata.st_size > _MAX_JSON_BYTES:
raise ValueError(f"{field}: expected a bounded regular file")
raw = path.read_bytes()
if len(raw) > _MAX_JSON_BYTES:
raise ValueError(f"{field}: file exceeds the size limit")
return _read_json_bytes(raw, field), raw
def _object(value: Any, field: str) -> dict[str, Any]:
if not isinstance(value, dict):
raise ValueError(f"{field}: expected an object")
return value
def _exact_keys(value: dict[str, Any], keys: set[str], field: str) -> None:
if set(value) != keys:
raise ValueError(f"{field}: invalid fields")
def _text(value: Any, field: str) -> str:
if not isinstance(value, str) or not value or value != value.strip():
raise ValueError(f"{field}: expected exact non-blank text")
return value
def _identifier(value: Any, field: str) -> str:
result = _text(value, field)
if not _IDENTIFIER.fullmatch(result):
raise ValueError(f"{field}: invalid stable identifier")
return result
def _sha256(value: Any, field: str) -> str:
result = _text(value, field)
if not _SHA256.fullmatch(result):
raise ValueError(f"{field}: expected lowercase SHA-256")
return result
def _optional_sha256(value: Any, field: str) -> str | None:
return None if value is None else _sha256(value, field)
def _commit(value: Any, field: str) -> str:
result = _text(value, field)
if not _COMMIT_SHA.fullmatch(result):
raise ValueError(f"{field}: expected a full lowercase Git SHA")
return result
def _iso_date(value: Any, field: str) -> str:
result = _text(value, field)
try:
parsed = date.fromisoformat(result)
except ValueError as error:
raise ValueError(f"{field}: expected an ISO date") from error
if parsed.isoformat() != result:
raise ValueError(f"{field}: expected an exact ISO date")
return result
def _iso_datetime(value: Any, field: str) -> str:
result = _text(value, field)
if not result.endswith("Z"):
raise ValueError(f"{field}: expected a UTC timestamp ending in Z")
try:
parsed = datetime.fromisoformat(result[:-1] + "+00:00")
except ValueError as error:
raise ValueError(f"{field}: expected an ISO UTC timestamp") from error
if parsed.tzinfo != UTC or parsed.microsecond:
raise ValueError(f"{field}: expected second-precision UTC")
return result
def _timestamp(value: str) -> datetime:
return datetime.fromisoformat(value[:-1] + "+00:00")
def _https_url(value: Any, field: str) -> str:
result = _text(value, field)
parsed = urlsplit(result)
if (
not result.isascii()
or parsed.scheme != "https"
or not parsed.netloc
or parsed.username is not None
or parsed.query
or parsed.fragment
):
raise ValueError(f"{field}: expected an HTTPS URL")
try:
port = parsed.port
except ValueError as error:
raise ValueError(f"{field}: invalid URL port") from error
host = parsed.hostname
path_parts = parsed.path.split("/")
if (
host is None
or host.endswith(".")
or "%" in parsed.path
or "\\" in parsed.path
or any(part in {".", ".."} for part in path_parts)
or any(not part for part in path_parts[1:])
):
raise ValueError(f"{field}: invalid canonical URL")
normalized_host = host.casefold()
if ":" in normalized_host:
normalized_host = f"[{normalized_host}]"
netloc = normalized_host if port in {None, 443} else f"{normalized_host}:{port}"
normalized = f"https://{netloc}{parsed.path or '/'}"
if result != normalized:
raise ValueError(f"{field}: expected a canonical HTTPS URL")
return result
def _safe_path(value: Any, field: str) -> str:
result = _text(value, field)
if not result.isascii() or result.startswith("/") or "\\" in result:
raise ValueError(f"{field}: expected a portable relative path")
parts = result.split("/")
if any(
part in {"", ".", ".."} or not _SAFE_PATH_COMPONENT.fullmatch(part)
for part in parts
):
raise ValueError(f"{field}: unsafe path")
return result
def _string_list(value: Any, field: str) -> tuple[str, ...]:
if not isinstance(value, list):
raise ValueError(f"{field}: expected a list")
result = tuple(_text(item, f"{field}[{index}]") for index, item in enumerate(value))
if len(result) != len(set(result)):
raise ValueError(f"{field}: duplicate values")
return result
def _raw_sha256(raw: bytes) -> str:
return hashlib.sha256(raw).hexdigest()
def _file_sha256(root: Path, relative: str, field: str) -> str:
path = root / Path(*relative.split("/"))
try:
resolved = path.resolve(strict=True)
except OSError as error:
raise ValueError(f"{field}: expected a readable file") from error
if (
not resolved.is_relative_to(root.resolve())
or path.is_symlink()
or not path.is_file()
):
raise ValueError(f"{field}: expected a regular file inside the repository")
return _raw_sha256(path.read_bytes())
@dataclass(frozen=True)
class DevelopmentExclusion:
source_id: str
canonical_url: str
retained_raw_sha256: str | None
@dataclass(frozen=True)
class EvaluationManifest:
evaluation_id: str
check_ids: tuple[str, ...]
scanner_path: str
scanner_sha256: str
checks_path: str
checks_sha256: str
evaluator_path: str
exclusions: tuple[DevelopmentExclusion, ...]
official_flag_minimum: int
official_quiet_minimum: int
synthetic_flag_minimum: int
synthetic_quiet_minimum: int
required_category_ids: tuple[str, ...]
raw_sha256: str
payload: dict[str, Any]
def load_evaluation_manifest( # noqa: C901 - strict schema branches fail closed.
path: Path, repository_root: Path
) -> EvaluationManifest:
"""Validate the committed ``not_run`` planning manifest and its live pins."""
raw_payload, raw = _read_json_file(path, "evaluation manifest")
payload = _object(raw_payload, "evaluation manifest")
_exact_keys(payload, _MANIFEST_KEYS, "evaluation manifest")
if type(payload.get("schema_version")) is not int or payload["schema_version"] != 1:
raise ValueError("evaluation manifest.schema_version: expected integer 1")
evaluation_id = _identifier(payload.get("evaluation_id"), "evaluation_id")
if (
payload.get("status") != "not_run"
or payload.get("scoring_unit") != "case_check_pair"
):
raise ValueError("evaluation manifest: expected not_run case_check_pair plan")
scanner = _object(payload.get("scanner"), "scanner")
_exact_keys(scanner, _SCANNER_KEYS, "scanner")
if scanner.get("module") != "permit_pathways.conformance":
raise ValueError("scanner.module: unexpected scanner module")
scanner_path = _safe_path(scanner.get("scanner_path"), "scanner.scanner_path")
scanner_sha = _sha256(scanner.get("scanner_sha256"), "scanner.scanner_sha256")
checks_path = _safe_path(scanner.get("checks_path"), "scanner.checks_path")
checks_sha = _sha256(scanner.get("checks_sha256"), "scanner.checks_sha256")
evaluator_path = _safe_path(scanner.get("evaluator_path"), "scanner.evaluator_path")
if scanner.get("evaluator_sha256") is not None:
raise ValueError("scanner.evaluator_sha256: not_run plan must remain null")
check_ids = _string_list(scanner.get("check_ids"), "scanner.check_ids")
if not check_ids or check_ids != tuple(sorted(check_ids)):
raise ValueError("scanner.check_ids: expected a sorted non-empty list")
root = repository_root.resolve()
if _file_sha256(root, scanner_path, "scanner.scanner_path") != scanner_sha:
raise ValueError("scanner.scanner_sha256: scanner bytes drifted")
if _file_sha256(root, checks_path, "scanner.checks_path") != checks_sha:
raise ValueError("scanner.checks_sha256: check registry bytes drifted")
live_check_ids = tuple(
sorted(check.check_id for check in load_checks(root / checks_path))
)
if live_check_ids != check_ids:
raise ValueError("scanner.check_ids: check registry coverage drifted")
if not (root / evaluator_path).is_file():
raise ValueError("scanner.evaluator_path: evaluator is unavailable")
freeze = _object(payload.get("freeze"), "freeze")
_exact_keys(freeze, _FREEZE_KEYS, "freeze")
if freeze.get("corpus_status") != "not_frozen" or any(
freeze.get(key) is not None for key in _FREEZE_KEYS - {"corpus_status"}
):
raise ValueError("freeze: not_run plan must have null execution fields")
inputs = _object(payload.get("inputs"), "inputs")
_exact_keys(inputs, _INPUT_KEYS, "inputs")
if any(value is not None for value in inputs.values()):
raise ValueError("inputs: not_run plan must have null inputs")
output = _object(payload.get("output"), "output")
_exact_keys(output, {"result_path"}, "output")
if output.get("result_path") is not None:
raise ValueError("output.result_path: not_run plan must remain null")
if (
_string_list(payload.get("reference_labels"), "reference_labels")
!= REFERENCE_LABELS
):
raise ValueError("reference_labels: invalid queue labels")
if (
_string_list(payload.get("raw_count_fields"), "raw_count_fields")
!= RAW_COUNT_FIELDS
):
raise ValueError("raw_count_fields: invalid raw-count contract")
coverage = _object(payload.get("coverage_contract"), "coverage_contract")
_exact_keys(coverage, _COVERAGE_KEYS, "coverage_contract")
official = _minimums(
coverage.get("official_targeted_pairs_per_check"),
"coverage_contract.official_targeted_pairs_per_check",
)
synthetic = _minimums(
coverage.get("synthetic_targeted_pairs_per_check"),
"coverage_contract.synthetic_targeted_pairs_per_check",
)
categories = tuple(
_identifier(item, f"coverage_contract.required_category_ids[{index}]")
for index, item in enumerate(
_string_list(
coverage.get("required_category_ids"),
"coverage_contract.required_category_ids",
)
)
)
if len(categories) != len(set(categories)):
raise ValueError("coverage_contract.required_category_ids: duplicates")
if coverage.get("incidental_findings_count_toward_coverage") is not False:
raise ValueError("coverage_contract: incidental pairs cannot satisfy coverage")
if coverage.get("synthetic_controls_reported_separately") is not True:
raise ValueError("coverage_contract: synthetic controls must be separate")
if coverage.get("pair_universe") != "full_case_check_cartesian_product":
raise ValueError("coverage_contract: full Cartesian pair universe is required")
if (
type(coverage.get("target_checks_per_case")) is not int
or coverage.get("target_checks_per_case") != 1
):
raise ValueError("coverage_contract: exactly one target check is required")
if coverage.get("multi_target_cases_supported") is not False:
raise ValueError(
"coverage_contract: schema v1 does not support multi-target cases"
)
if _string_list(
coverage.get("reporting_grains"), "coverage_contract.reporting_grains"
) != (
"overall",
"per_check",
"official_targeted",
"synthetic_targeted",
"official_incidental",
"synthetic_incidental",
):
raise ValueError("coverage_contract: invalid reporting grains")
exclusions_raw = payload.get("development_source_exclusions")
if not isinstance(exclusions_raw, list) or not exclusions_raw:
raise ValueError("development_source_exclusions: expected a non-empty list")
exclusions = tuple(
_parse_exclusion(item, index) for index, item in enumerate(exclusions_raw)
)
if len({item.source_id for item in exclusions}) != len(exclusions):
raise ValueError("development_source_exclusions: duplicate source ID")
if len({item.canonical_url for item in exclusions}) != len(exclusions):
raise ValueError("development_source_exclusions: duplicate canonical URL")
blockers = _string_list(payload.get("external_blockers"), "external_blockers")
if len(blockers) < 3:
raise ValueError(
"external_blockers: expected source, review, and custody blockers"
)
claim_boundary = _text(payload.get("claim_boundary"), "claim_boundary")
if _raw_sha256(claim_boundary.encode("utf-8")) != _CLAIM_BOUNDARY_SHA256:
raise ValueError("claim_boundary: expected the exact schema-v1 policy")
return EvaluationManifest(
evaluation_id=evaluation_id,
check_ids=check_ids,
scanner_path=scanner_path,
scanner_sha256=scanner_sha,
checks_path=checks_path,
checks_sha256=checks_sha,
evaluator_path=evaluator_path,
exclusions=exclusions,
official_flag_minimum=official["should_flag"],
official_quiet_minimum=official["should_stay_quiet"],
synthetic_flag_minimum=synthetic["should_flag"],
synthetic_quiet_minimum=synthetic["should_stay_quiet"],
required_category_ids=categories,
raw_sha256=_raw_sha256(raw),
payload=payload,
)
def _minimums(value: Any, field: str) -> dict[str, int]:
result = _object(value, field)
_exact_keys(result, _MINIMUM_KEYS, field)
for key, minimum in result.items():
if type(minimum) is not int or minimum < 0:
raise ValueError(f"{field}.{key}: expected a non-negative integer")
return result
def _parse_exclusion(value: Any, index: int) -> DevelopmentExclusion:
field = f"development_source_exclusions[{index}]"
record = _object(value, field)
_exact_keys(record, _EXCLUSION_KEYS, field)
_text(record.get("reason"), f"{field}.reason")
if record.get("near_duplicate_disposition") != NEAR_DUPLICATE_DISPOSITION:
raise ValueError(
f"{field}.near_duplicate_disposition: expected the exclusion policy"
)
return DevelopmentExclusion(
source_id=_identifier(record.get("source_id"), f"{field}.source_id"),
canonical_url=_https_url(record.get("canonical_url"), f"{field}.canonical_url"),
retained_raw_sha256=_optional_sha256(
record.get("retained_raw_sha256"), f"{field}.retained_raw_sha256"
),
)
@dataclass(frozen=True)
class EvaluationCase:
case_id: str
category_id: str
stratum: str
target_check_id: str
selection_role: str
passage: str
source_id: str
canonical_url: str | None
document_sha256: str | None
@dataclass(frozen=True)
class CaseSet:
evaluation_id: str
freeze_id: str
frozen_at: str
corpus_repository_commit_sha: str
cases: tuple[EvaluationCase, ...]
raw_sha256: str
def load_case_set(path: Path, manifest: EvaluationManifest) -> CaseSet:
"""Load a frozen passage set without opening or requiring an answer key."""
raw_payload, raw = _read_json_file(path, "case set")
payload = _object(raw_payload, "case set")
_exact_keys(payload, _CASES_KEYS, "case set")
if type(payload.get("schema_version")) is not int or payload["schema_version"] != 1:
raise ValueError("case set.schema_version: expected integer 1")
if payload.get("evaluation_id") != manifest.evaluation_id:
raise ValueError("case set.evaluation_id: does not match manifest")
freeze_id = _identifier(payload.get("freeze_id"), "case set.freeze_id")
frozen_at = _iso_datetime(payload.get("frozen_at"), "case set.frozen_at")
corpus_commit = _commit(
payload.get("corpus_repository_commit_sha"),
"case set.corpus_repository_commit_sha",
)
selection = _object(payload.get("selection"), "case set.selection")
_exact_keys(selection, _SELECTION_KEYS, "case set.selection")
_text(selection.get("method"), "case set.selection.method")
_identifier(selection.get("custodian_id"), "case set.selection.custodian_id")
if selection.get("selected_before_scanner_run") is not True:
raise ValueError(
"case set.selection: cases must be selected before scanner run"
)
if selection.get("near_duplicate_review_completed") is not True:
raise ValueError("case set.selection: near-duplicate review is required")
cases_raw = payload.get("cases")
if not isinstance(cases_raw, list) or not cases_raw or len(cases_raw) > _MAX_CASES:
raise ValueError("case set.cases: expected a bounded non-empty list")
cases = tuple(
_parse_case(
item,
index,
manifest,
frozen_on=_timestamp(frozen_at).date(),
)
for index, item in enumerate(cases_raw)
)
case_ids = tuple(item.case_id for item in cases)
if case_ids != tuple(sorted(case_ids)) or len(case_ids) != len(set(case_ids)):
raise ValueError("case set.cases: expected sorted unique case IDs")
passage_digests = tuple(_raw_sha256(item.passage.encode("utf-8")) for item in cases)
if len(passage_digests) != len(set(passage_digests)):
raise ValueError("case set.cases: duplicate passages cannot be counted twice")
_validate_case_source_identity(cases)
categories = {item.category_id for item in cases}
if not set(manifest.required_category_ids) <= categories:
raise ValueError("case set.cases: required category coverage is missing")
return CaseSet(
evaluation_id=manifest.evaluation_id,
freeze_id=freeze_id,
frozen_at=frozen_at,
corpus_repository_commit_sha=corpus_commit,
cases=cases,
raw_sha256=_raw_sha256(raw),
)
def _validate_case_source_identity(cases: tuple[EvaluationCase, ...]) -> None:
by_source_id: dict[str, tuple[str, str]] = {}
by_url: dict[str, tuple[str, str]] = {}
for case in cases:
if case.stratum != "official":
continue
if case.canonical_url is None or case.document_sha256 is None:
raise ValueError("case set.cases: official source binding is incomplete")
source_binding = (case.canonical_url, case.document_sha256)
previous_source = by_source_id.setdefault(case.source_id, source_binding)
if previous_source != source_binding:
raise ValueError("case set.cases: source ID maps to conflicting documents")
url_binding = (case.source_id, case.document_sha256)
previous_url = by_url.setdefault(case.canonical_url, url_binding)
if previous_url != url_binding:
raise ValueError(
"case set.cases: canonical URL maps to conflicting sources"
)
def _parse_case(
value: Any,
index: int,
manifest: EvaluationManifest,
*,
frozen_on: date,
) -> EvaluationCase:
field = f"case set.cases[{index}]"
record = _object(value, field)
_exact_keys(record, _CASE_KEYS, field)
case_id = _identifier(record.get("case_id"), f"{field}.case_id")
category_id = _identifier(record.get("category_id"), f"{field}.category_id")
stratum = record.get("stratum")
if stratum not in {"official", "synthetic"}:
raise ValueError(f"{field}.stratum: expected official or synthetic")
target_check_id = _identifier(
record.get("target_check_id"), f"{field}.target_check_id"
)
if target_check_id not in manifest.check_ids:
raise ValueError(f"{field}.target_check_id: unknown check")
selection_role = record.get("selection_role")
if selection_role not in {"candidate_flag", "candidate_near_miss"}:
raise ValueError(
f"{field}.selection_role: expected candidate_flag or candidate_near_miss"
)
_text(record.get("selection_rationale"), f"{field}.selection_rationale")
passage = _text(record.get("passage"), f"{field}.passage")
if len(passage.encode("utf-8")) > _MAX_TEXT_BYTES:
raise ValueError(f"{field}.passage: text exceeds the size limit")
if _raw_sha256(passage.encode("utf-8")) != _sha256(
record.get("passage_sha256"), f"{field}.passage_sha256"
):
raise ValueError(f"{field}.passage_sha256: passage bytes drifted")
source = _object(record.get("source"), f"{field}.source")
_exact_keys(source, _SOURCE_KEYS, f"{field}.source")
source_id, canonical_url, document_sha = _parse_case_source(
source, field, stratum, manifest, frozen_on
)
return EvaluationCase(
case_id=case_id,
category_id=category_id,
stratum=stratum,
target_check_id=target_check_id,
selection_role=selection_role,
passage=passage,
source_id=source_id,
canonical_url=canonical_url,
document_sha256=document_sha,
)
def _parse_case_source(
source: dict[str, Any],
field: str,
stratum: str,
manifest: EvaluationManifest,
frozen_on: date,
) -> tuple[str, str | None, str | None]:
source_id = _identifier(source.get("source_id"), f"{field}.source.source_id")
_text(source.get("passage_locator"), f"{field}.source.passage_locator")
if stratum == "official":
canonical_url = _https_url(
source.get("canonical_url"), f"{field}.source.canonical_url"
)
document_sha = _sha256(
source.get("document_sha256"), f"{field}.source.document_sha256"
)
retrieved_on = _iso_date(
source.get("retrieved_on"), f"{field}.source.retrieved_on"
)
if date.fromisoformat(retrieved_on) > frozen_on:
raise ValueError(f"{field}.source.retrieved_on: retrieval postdates freeze")
_reject_development_source(
manifest, source_id, canonical_url, document_sha, field
)
else:
if not source_id.startswith("synthetic-"):
raise ValueError(f"{field}.source.source_id: synthetic ID must be explicit")
if (
source.get("canonical_url") is not None
or source.get("document_sha256") is not None
):
raise ValueError(
f"{field}.source: synthetic controls cannot claim a document"
)
if source.get("retrieved_on") is not None:
raise ValueError(
f"{field}.source.retrieved_on: synthetic control must be null"
)
canonical_url = None
document_sha = None
return source_id, canonical_url, document_sha
def _reject_development_source(
manifest: EvaluationManifest,
source_id: str,
canonical_url: str,
document_sha256: str,
field: str,
) -> None:
for exclusion in manifest.exclusions:
if source_id == exclusion.source_id or canonical_url == exclusion.canonical_url:
raise ValueError(
f"{field}.source: development-influencing source is excluded"
)
if exclusion.retained_raw_sha256 == document_sha256:
raise ValueError(f"{field}.source: development-source digest is excluded")
def expected_pair_keys(
cases: CaseSet, manifest: EvaluationManifest
) -> tuple[tuple[str, str], ...]:
return tuple(
(case.case_id, check_id)
for case in cases.cases
for check_id in manifest.check_ids
)
@dataclass(frozen=True)
class AnswerKey:
final_labels: dict[tuple[str, str], str]
unblinded_at: str
law_as_of: str
check_registry_as_of: str
raw_sha256: str
def load_answer_key( # noqa: C901 - reviewer/adjudication invariants are explicit.
path: Path,
manifest: EvaluationManifest,
cases: CaseSet,
*,
today: date | None = None,
) -> AnswerKey:
"""Load a two-reviewer, pair-complete key after blind predictions exist."""
raw_payload, raw = _read_json_file(path, "answer key")
payload = _object(raw_payload, "answer key")
_exact_keys(payload, _ANSWER_KEY_KEYS, "answer key")
if type(payload.get("schema_version")) is not int or payload["schema_version"] != 1:
raise ValueError("answer key.schema_version: expected integer 1")
if payload.get("evaluation_id") != manifest.evaluation_id:
raise ValueError("answer key.evaluation_id: does not match manifest")
if payload.get("freeze_id") != cases.freeze_id:
raise ValueError("answer key.freeze_id: does not match cases")
if (
_sha256(payload.get("cases_sha256"), "answer key.cases_sha256")
!= cases.raw_sha256
):
raise ValueError("answer key.cases_sha256: case set drifted")
if (
_sha256(payload.get("checks_sha256"), "answer key.checks_sha256")
!= manifest.checks_sha256
):
raise ValueError("answer key.checks_sha256: check registry drifted")
law_as_of = _iso_date(payload.get("law_as_of"), "answer key.law_as_of")
registry_as_of = _iso_date(
payload.get("check_registry_as_of"), "answer key.check_registry_as_of"
)
current_date = date.today() if today is None else today
if date.fromisoformat(law_as_of) > current_date:
raise ValueError("answer key.law_as_of: future source state is not allowed")
if date.fromisoformat(registry_as_of) > current_date:
raise ValueError(
"answer key.check_registry_as_of: future source state is not allowed"
)
reviewers_raw = payload.get("reviewers")
if not isinstance(reviewers_raw, list) or len(reviewers_raw) != 2:
raise ValueError("answer key.reviewers: exactly two reviewers are required")
expected = set(expected_pair_keys(cases, manifest))
reviewer_maps: list[dict[tuple[str, str], str]] = []
reviewer_ids: list[str] = []
reviewed_at: list[str] = []
for index, raw_reviewer in enumerate(reviewers_raw):
field = f"answer key.reviewers[{index}]"
reviewer = _object(raw_reviewer, field)
_exact_keys(reviewer, _REVIEWER_KEYS, field)
reviewer_ids.append(
_identifier(reviewer.get("reviewer_id"), f"{field}.reviewer_id")
)
_text(reviewer.get("qualification"), f"{field}.qualification")
_text(reviewer.get("method"), f"{field}.method")
reviewed_at.append(
_iso_datetime(reviewer.get("reviewed_at"), f"{field}.reviewed_at")
)
if reviewer.get("predictions_seen_before_initial_labels") is not False:
raise ValueError(f"{field}: initial judgments were not blind")
reviewer_maps.append(
_judgment_map(reviewer.get("judgments"), expected, f"{field}.judgments")
)
if len(reviewer_ids) != len(set(reviewer_ids)):
raise ValueError("answer key.reviewers: reviewer IDs must be distinct")
adjudication = _object(payload.get("adjudication"), "answer key.adjudication")
_exact_keys(adjudication, _ADJUDICATION_KEYS, "answer key.adjudication")
adjudicator_id = _identifier(
adjudication.get("adjudicator_id"), "answer key.adjudication.adjudicator_id"
)
if adjudicator_id in reviewer_ids:
raise ValueError("answer key.adjudication: adjudicator ID must be distinct")
_text(adjudication.get("method"), "answer key.adjudication.method")
adjudicated_at = _iso_datetime(
adjudication.get("adjudicated_at"), "answer key.adjudication.adjudicated_at"
)
final_labels = _judgment_map(
adjudication.get("final_judgments"),
expected,
"answer key.adjudication.final_judgments",
)
disagreements = {
pair
for pair in expected
if len({mapping[pair] for mapping in reviewer_maps}) > 1
}
recorded_disagreements = _disagreement_keys(
adjudication.get("disagreements"), expected
)
if disagreements != recorded_disagreements:
raise ValueError(
"answer key.adjudication.disagreements: does not match reviewers"
)
for pair in expected - disagreements:
if final_labels[pair] != reviewer_maps[0][pair]:
raise ValueError("answer key.adjudication: agreement was changed")
unblinded_at = _iso_datetime(payload.get("unblinded_at"), "answer key.unblinded_at")
if any(_timestamp(cases.frozen_at) > _timestamp(item) for item in reviewed_at):
raise ValueError("answer key: review predates corpus freeze")
if max(_timestamp(item) for item in reviewed_at) > _timestamp(adjudicated_at):
raise ValueError("answer key: adjudication predates review")
if _timestamp(adjudicated_at) > _timestamp(unblinded_at):
raise ValueError("answer key: unblinding predates adjudication")
unblinded_date = _timestamp(unblinded_at).date()
if date.fromisoformat(law_as_of) > unblinded_date:
raise ValueError("answer key.law_as_of: source state postdates unblinding")
if date.fromisoformat(registry_as_of) > unblinded_date:
raise ValueError(
"answer key.check_registry_as_of: source state postdates unblinding"