File size: 43,523 Bytes
cad04d1 4620c5a cad04d1 81b05f3 cad04d1 81b05f3 cad04d1 81b05f3 cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 4620c5a cad04d1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 | #!/usr/bin/env python3
"""Materialize and control-check a frozen private CTX holdout."""
from __future__ import annotations
import argparse
from collections.abc import Mapping
from dataclasses import dataclass
import hashlib
import json
import os
from pathlib import Path
import secrets
import shutil
import signal
import stat
import subprocess
import sys
import tempfile
import time
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from scripts import ctx_ab_benchmark as benchmark # noqa: E402
from scripts import ctx_ab_exposure_ledger as exposure_ledger # noqa: E402
from scripts import ctx_ab_failure_evidence as failure_evidence # noqa: E402
from scripts import ctx_ab_holdout as holdout # noqa: E402
from scripts import ctx_ab_holdout_freeze as freezer # noqa: E402
from scripts import ctx_ab_swebench as swebench # noqa: E402
OUTPUT_FILES = {
"scenario_pack": "scenario-pack.json",
"collision": "collision-attestation.json",
"reconstructed": "reconstructed-test-attestation.json",
"controls": "control-results.json",
}
VERIFICATION_DIR = "official-verification"
VERIFIER_PROTOCOL_KEY = "official_swebench_verifier"
PROTOCOL_ID = freezer.PROTOCOL_ID
SCENARIO_COUNT = 10
class MaterializationError(RuntimeError):
"""A private holdout failed deterministic materialization."""
@dataclass(frozen=True)
class ProcessResult:
returncode: int
stdout: str
stderr: str
timed_out: bool
@dataclass(frozen=True)
class VerifierRuntime:
"""Operator-local paths for a protocol-pinned official verifier."""
swebench_checkout: Path
swebench_python: Path
docker_cli: Path
docker_host: str
def _canonical_bytes(value: Any) -> bytes:
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode()
def _sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def _load_json(path: Path) -> dict[str, Any]:
value = json.loads(path.read_text(encoding="utf-8"))
if not isinstance(value, dict):
raise MaterializationError("JSON input must contain an object")
return value
def _load_authenticated_protocol(path: Path, *, expected_sha256: str) -> dict[str, Any]:
if not isinstance(expected_sha256, str) or freezer.SHA256.fullmatch(expected_sha256) is None:
raise MaterializationError("acquisition protocol authentication failed")
protocol_bytes = path.read_bytes()
if not secrets.compare_digest(_sha256(protocol_bytes), expected_sha256):
raise MaterializationError("acquisition protocol authentication failed")
value = json.loads(protocol_bytes)
if not isinstance(value, dict):
raise MaterializationError("acquisition protocol must contain an object")
return value
def _load_jsonl(path: Path) -> list[dict[str, Any]]:
rows = [
json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line.strip()
]
if not rows or not all(isinstance(row, dict) for row in rows):
raise MaterializationError("canonical JSONL must contain object rows")
return rows
def _string_list(value: object, *, field: str) -> list[str]:
try:
items = json.loads(str(value))
except json.JSONDecodeError as exc:
raise MaterializationError(f"{field} must be a JSON string list") from exc
if (
not isinstance(items, list)
or not items
or not all(isinstance(item, str) and item for item in items)
):
raise MaterializationError(f"{field} must be a non-empty JSON string list")
return items
def _remaining(deadline: float) -> float:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise MaterializationError("control verification exceeded the frozen timeout")
return remaining
def _run(
argv: list[str],
*,
cwd: Path,
deadline: float,
input_text: str | None = None,
env: dict[str, str] | None = None,
) -> ProcessResult:
process = subprocess.Popen(
argv,
cwd=cwd,
env=env,
stdin=subprocess.PIPE if input_text is not None else subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
start_new_session=True,
)
try:
stdout, stderr = process.communicate(input_text, timeout=_remaining(deadline))
return ProcessResult(process.returncode, stdout, stderr, False)
except subprocess.TimeoutExpired:
os.killpg(process.pid, signal.SIGKILL)
stdout, stderr = process.communicate()
return ProcessResult(process.returncode, stdout, stderr, True)
def _checked(
argv: list[str],
*,
cwd: Path,
deadline: float,
input_text: str | None = None,
) -> ProcessResult:
result = _run(argv, cwd=cwd, deadline=deadline, input_text=input_text)
if result.timed_out:
raise MaterializationError("control verification exceeded the frozen timeout")
if result.returncode:
raise MaterializationError("repository materialization command failed")
return result
def _validate_runtime(runtime: VerifierRuntime) -> None:
for path in (
runtime.swebench_checkout,
runtime.swebench_python,
runtime.docker_cli,
):
if not path.is_absolute():
raise MaterializationError("official verifier runtime paths must be absolute")
if not isinstance(runtime.docker_host, str) or not runtime.docker_host:
raise MaterializationError("official verifier Docker host is invalid")
def _phase_summary(
evidence: Mapping[str, Any],
*,
phase: str,
fail_to_pass_count: int,
pass_to_pass_count: int,
pins: Mapping[str, Any],
dataset_sha256: str,
) -> dict[str, Any]:
validation = evidence.get("validation")
authentication = evidence.get("authentication")
python_environment = evidence.get("python_environment")
docker_package = evidence.get("docker_package")
docker_identity = evidence.get("docker_identity")
cleanup = evidence.get("cleanup")
process = evidence.get("process")
artifacts = evidence.get("artifacts")
input_snapshots = evidence.get("input_snapshots")
expected_resolution = "RESOLVED_NO" if phase == "red" else "RESOLVED_FULL"
expected_resolved = phase != "red"
if (
evidence.get("schema_version") != 1
or evidence.get("phase") != phase
or not isinstance(validation, Mapping)
or validation.get("phase") != phase
or validation.get("exact_selector_identity") is not True
or validation.get("exact_selector_keys_present") is not True
or validation.get("fail_to_pass_count") != fail_to_pass_count
or validation.get("pass_to_pass_count") != pass_to_pass_count
or validation.get("resolution") != expected_resolution
or validation.get("resolved") is not expected_resolved
or not isinstance(validation.get("container_policy_count"), int)
or isinstance(validation.get("container_policy_count"), bool)
or validation["container_policy_count"] < 1
or not swebench.IMAGE_ID_PATTERN.fullmatch(str(validation.get("image_id") or ""))
or not isinstance(authentication, Mapping)
or authentication.get("git_revision") != pins["revision"]
or authentication.get("run_evaluation_sha256") != pins["run_evaluation_sha256"]
or not swebench.SHA256_PATTERN.fullmatch(str(authentication.get("source_sha256") or ""))
or isinstance(authentication.get("source_file_count"), bool)
or not isinstance(authentication.get("source_file_count"), int)
or authentication["source_file_count"] < 1
or not isinstance(python_environment, Mapping)
or python_environment.get("sha256") != pins["python_environment_sha256"]
or isinstance(python_environment.get("distribution_count"), bool)
or not isinstance(python_environment.get("distribution_count"), int)
or python_environment["distribution_count"] < 1
or not isinstance(docker_package, Mapping)
or docker_package.get("sha256") != pins["docker_package_sha256"]
or isinstance(docker_package.get("file_count"), bool)
or not isinstance(docker_package.get("file_count"), int)
or docker_package["file_count"] < 1
or not isinstance(docker_identity, Mapping)
or docker_identity.get("server_version") != pins["docker_server_version"]
or docker_identity.get("daemon_id_sha256")
!= _sha256(str(pins["docker_daemon_id"]).encode())
or not isinstance(input_snapshots, Mapping)
or input_snapshots.get("bridge_sha256") != pins["bridge_sha256"]
or input_snapshots.get("dataset_sha256") != dataset_sha256
or input_snapshots.get("docker_package")
!= {
"file_count": docker_package.get("file_count"),
"sha256": pins["docker_package_sha256"],
}
or input_snapshots.get("harness_source")
!= {
"file_count": authentication.get("source_file_count"),
"sha256": authentication.get("source_sha256"),
}
or not isinstance(cleanup, Mapping)
or cleanup.get("ok") is not True
or not isinstance(process, Mapping)
or process.get("returncode") != 0
or process.get("timed_out") is not False
or process.get("residual_descendant_count") != 0
or not isinstance(artifacts, Mapping)
):
raise MaterializationError(f"official {phase} control evidence is invalid")
expected_artifacts = {
*swebench.REQUIRED_ARTIFACTS,
"parent-process.json",
"verification-evidence.json",
"worker-request.json",
"worker-result.json",
}
if phase == "red":
expected_artifacts.add("mode-probe.log")
if set(artifacts) != expected_artifacts:
raise MaterializationError(f"official {phase} artifacts are incomplete")
artifact_bytes = 0
for name in sorted(expected_artifacts):
item = artifacts.get(name)
if (
not isinstance(item, Mapping)
or item.get("present") is not True
or item.get("name") != name
or isinstance(item.get("bytes"), bool)
or not isinstance(item.get("bytes"), int)
or item["bytes"] < 0
or not swebench.SHA256_PATTERN.fullmatch(str(item.get("sha256") or ""))
):
raise MaterializationError(f"official {phase} artifacts are invalid")
artifact_bytes += item["bytes"]
status_counts = validation.get("status_counts")
if (
not isinstance(status_counts, Mapping)
or not status_counts
or not all(
isinstance(status, str)
and status in swebench.KNOWN_STATUSES
and isinstance(count, int)
and not isinstance(count, bool)
and count >= 0
for status, count in status_counts.items()
)
or sum(status_counts.values()) != fail_to_pass_count + pass_to_pass_count
):
raise MaterializationError(f"official {phase} status evidence is invalid")
identity = {
"authentication": {
"git_revision": authentication["git_revision"],
"run_evaluation_sha256": authentication["run_evaluation_sha256"],
"source_file_count": authentication["source_file_count"],
"source_sha256": authentication["source_sha256"],
},
"docker_identity": {
"daemon_id_sha256": docker_identity["daemon_id_sha256"],
"server_version": docker_identity["server_version"],
},
"docker_package": {
"file_count": docker_package["file_count"],
"sha256": docker_package["sha256"],
},
"input_snapshots": input_snapshots,
"python_environment": {
"distribution_count": python_environment.get("distribution_count"),
"sha256": python_environment["sha256"],
},
}
runtime_identity_sha256 = _sha256(_canonical_bytes(identity))
semantic_evidence = {
"container_policy_count": validation["container_policy_count"],
"exact_selector_identity": True,
"fail_to_pass_count": fail_to_pass_count,
"image_id": validation["image_id"],
"pass_to_pass_count": pass_to_pass_count,
"phase": phase,
"resolved": expected_resolved,
"runtime_identity_sha256": runtime_identity_sha256,
"status_counts": dict(sorted(status_counts.items())),
}
return {
"artifact_bytes": artifact_bytes,
"artifact_count": len(expected_artifacts),
"artifact_manifest_sha256": _sha256(_canonical_bytes(artifacts)),
**semantic_evidence,
"raw_verifier_evidence_sha256": _sha256(_canonical_bytes(evidence)),
"verifier_evidence_sha256": _sha256(_canonical_bytes(semantic_evidence)),
}
def _control_phase_summary(summary: Mapping[str, Any]) -> dict[str, Any]:
fields = (
"artifact_bytes",
"artifact_count",
"artifact_manifest_sha256",
"container_policy_count",
"exact_selector_identity",
"fail_to_pass_count",
"image_id",
"pass_to_pass_count",
"phase",
"runtime_identity_sha256",
"status_counts",
"verifier_evidence_sha256",
)
return {field: summary[field] for field in fields}
def _private_write(path: Path, data: bytes) -> None:
descriptor = os.open(
path,
os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0),
0o600,
)
with os.fdopen(descriptor, "wb") as handle:
handle.write(data)
def _retain_phase_artifacts(
*,
evidence: Mapping[str, Any],
instance_id: str,
phase: str,
source_root: Path,
destination_root: Path,
summary: Mapping[str, Any],
) -> None:
run_id = evidence.get("run_id")
model_name = evidence.get("model_name")
artifacts = evidence.get("artifacts")
if (
not isinstance(run_id, str)
or not swebench.RUN_ID_PATTERN.fullmatch(run_id)
or not isinstance(model_name, str)
or model_name != f"ctx-swebench-{phase}"
or not isinstance(artifacts, Mapping)
):
raise MaterializationError(f"official {phase} artifact identity is invalid")
destination_root.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
os.chmod(destination_root.parent, 0o700)
destination_root.mkdir(mode=0o700, exist_ok=False)
os.chmod(destination_root, 0o700)
log_root = source_root / "logs" / "run_evaluation" / run_id / model_name / instance_id
root_names = {
"mode-probe.log",
"parent-process.json",
"verification-evidence.json",
"worker-request.json",
"worker-result.json",
}
for name, item in sorted(artifacts.items()):
if not isinstance(name, str) or not isinstance(item, Mapping):
raise MaterializationError(f"official {phase} artifact evidence is invalid")
source = source_root / name if name in root_names else log_root / name
try:
resolved = source.resolve(strict=True)
resolved.relative_to(source_root.resolve(strict=True))
except (OSError, ValueError) as exc:
raise MaterializationError(
f"official {phase} artifact escaped its private root"
) from exc
if source.is_symlink() or not source.is_file():
raise MaterializationError(f"official {phase} artifact is not a regular file")
data = source.read_bytes()
if (
len(data) != item.get("bytes")
or _sha256(data) != item.get("sha256")
or len(data) > swebench.MAX_EVIDENCE_BYTES
):
raise MaterializationError(f"official {phase} artifact changed before retention")
_private_write(destination_root / name, data)
_private_write(destination_root / "evidence.json", _canonical_bytes(evidence))
_private_write(destination_root / "summary.json", _canonical_bytes(summary))
def _repo_source(
row: dict[str, Any],
sources: Mapping[str, freezer.SourceBundle],
) -> freezer.SourceBundle:
repo = str(row["repo"]).strip().lower()
url = holdout.canonical_repo_url(repo)
source = sources.get(url)
if source is None:
raise MaterializationError("selected repository is absent from the source map")
if source.base_commit != str(row.get("base_commit") or ""):
raise MaterializationError("selected repository source commit is stale")
return source
def _validate_source_bundle_heads(
source: freezer.SourceBundle,
*,
cwd: Path,
deadline: float,
) -> None:
listed_heads = _checked(
["git", "bundle", "list-heads", str(source.bundle_path)],
cwd=cwd,
deadline=deadline,
).stdout.splitlines()
if not listed_heads or any(
not line.startswith(f"{source.base_commit} ") for line in listed_heads
):
raise MaterializationError("repository source bundle exposes an unpinned ref")
def _scenario(
row: dict[str, Any],
*,
dataset_path: Path,
protocol: dict[str, Any],
pins: dict[str, Any],
retained_root: Path,
runtime: VerifierRuntime,
source: freezer.SourceBundle,
slot: int,
work_root: Path,
timeout: float,
) -> tuple[dict[str, Any], benchmark.Scenario, dict[str, Any], str]:
started = time.monotonic()
deadline = started + timeout
workspace = work_root / f"scenario-{slot}"
_validate_source_bundle_heads(source, cwd=work_root, deadline=deadline)
_checked(
[
"git",
"-c",
"core.hooksPath=/dev/null",
"clone",
"--quiet",
"--no-checkout",
"--no-hardlinks",
str(source.bundle_path),
str(workspace),
],
cwd=work_root,
deadline=deadline,
)
commit = str(row["base_commit"])
_checked(
["git", "-c", "core.hooksPath=/dev/null", "checkout", "--quiet", "--detach", commit],
cwd=workspace,
deadline=deadline,
)
observed = _checked(["git", "rev-parse", "HEAD"], cwd=workspace, deadline=deadline)
if observed.stdout.strip() != commit:
raise MaterializationError("repository checkout did not reach the pinned commit")
future_history = _checked(
["git", "rev-list", "--all", "--not", commit],
cwd=workspace,
deadline=deadline,
)
unreachable = _checked(
["git", "fsck", "--full", "--unreachable", "--no-reflogs"],
cwd=workspace,
deadline=deadline,
)
if future_history.stdout.strip() or unreachable.stdout.strip():
raise MaterializationError("repository source bundle is not a base-commit closure")
_checked(["git", "remote", "remove", "origin"], cwd=workspace, deadline=deadline)
remotes = _checked(["git", "remote"], cwd=workspace, deadline=deadline)
if remotes.stdout.strip():
raise MaterializationError("repository source bundle retained an external remote")
tree = _checked(
["git", "rev-parse", "HEAD^{tree}"],
cwd=workspace,
deadline=deadline,
).stdout.strip()
status_result = _checked(
["git", "status", "--porcelain=v1", "--untracked-files=all"],
cwd=workspace,
deadline=deadline,
)
if (
not swebench.REVISION_PATTERN.fullmatch(tree)
or tree != source.tree_sha1
or status_result.stdout
):
raise MaterializationError("repository checkout is not an exact clean tree")
evaluated = holdout.evaluate_row(row, protocol)
if evaluated["status"] != "eligible":
raise MaterializationError("selected row is no longer statically eligible")
test_path = str(evaluated["test_path"])
production_paths = str(evaluated["production_paths"]).split("|")
_checked(
["git", "apply", "--whitespace=nowarn", "-"],
cwd=workspace,
deadline=deadline,
input_text=str(row["test_patch"]),
)
test_file = workspace / test_path
try:
resolved_test_file = test_file.resolve(strict=True)
resolved_test_file.relative_to(workspace.resolve(strict=True))
except (OSError, ValueError) as exc:
raise MaterializationError(
"test patch reconstructed a path outside the repository"
) from exc
if test_file.is_symlink() or not resolved_test_file.is_file():
raise MaterializationError("test patch did not reconstruct a complete test module")
test_source = resolved_test_file.read_text(encoding="utf-8")
holdout.validate_reconstructed_test_module(test_source)
test_sha256 = _sha256(test_source.encode())
focused = _string_list(row["FAIL_TO_PASS"], field="FAIL_TO_PASS")
regressions = _string_list(row["PASS_TO_PASS"], field="PASS_TO_PASS")
scenario_id = str(row["instance_id"])
common_verifier = {
"allowed_paths": production_paths,
"dataset_path": dataset_path,
"docker_cli": runtime.docker_cli,
"docker_host": runtime.docker_host,
"expected_bridge_sha256": pins["bridge_sha256"],
"expected_dataset_sha256": protocol["universe"]["selection_jsonl_sha256"],
"expected_docker_cli_sha256": pins["docker_cli_sha256"],
"expected_docker_daemon_id": pins["docker_daemon_id"],
"expected_docker_package_sha256": pins["docker_package_sha256"],
"expected_docker_server_version": pins["docker_server_version"],
"expected_python_environment_sha256": pins["python_environment_sha256"],
"expected_python_sha256": pins["python_sha256"],
"expected_revision": pins["revision"],
"expected_run_evaluation_sha256": pins["run_evaluation_sha256"],
"instance_id": scenario_id,
"namespace": pins["namespace"],
"swebench_checkout": runtime.swebench_checkout,
"swebench_python": runtime.swebench_python,
}
red_root = work_root / f"official-{slot:03d}-red"
try:
red_evidence = swebench.verify_swebench(
**common_verifier,
phase="red",
work_dir=red_root,
timeout=_remaining(deadline),
allow_image_pull=True,
)
red_summary = _phase_summary(
red_evidence,
phase="red",
fail_to_pass_count=len(focused),
pass_to_pass_count=len(regressions),
pins=pins,
dataset_sha256=str(protocol["universe"]["selection_jsonl_sha256"]),
)
_retain_phase_artifacts(
evidence=red_evidence,
instance_id=scenario_id,
phase="red",
source_root=red_root,
destination_root=retained_root / f"scenario-{slot:03d}" / "red",
summary=red_summary,
)
except Exception as exc:
raise MaterializationError("official red control failed") from exc
image_id = str(red_summary["image_id"])
green_root = work_root / f"official-{slot:03d}-green"
try:
green_evidence = swebench.verify_swebench(
**common_verifier,
phase="green",
work_dir=green_root,
timeout=_remaining(deadline),
expected_image_id=image_id,
allow_image_pull=False,
)
green_summary = _phase_summary(
green_evidence,
phase="green",
fail_to_pass_count=len(focused),
pass_to_pass_count=len(regressions),
pins=pins,
dataset_sha256=str(protocol["universe"]["selection_jsonl_sha256"]),
)
if green_summary["image_id"] != image_id:
raise MaterializationError("official green image identity drifted")
if green_summary["runtime_identity_sha256"] != red_summary["runtime_identity_sha256"]:
raise MaterializationError("official verifier runtime identity drifted")
_retain_phase_artifacts(
evidence=green_evidence,
instance_id=scenario_id,
phase="green",
source_root=green_root,
destination_root=retained_root / f"scenario-{slot:03d}" / "green",
summary=green_summary,
)
except Exception as exc:
raise MaterializationError("official green control failed") from exc
elapsed = time.monotonic() - started
if elapsed > timeout:
raise MaterializationError("control verification exceeded the frozen timeout")
red_marker = next(
(
status
for status in ("FAILED", "ERROR")
if int(red_summary["status_counts"].get(status, 0)) > 0
),
"",
)
if not red_marker:
raise MaterializationError("official red control omitted a failure status")
task = str(row["problem_statement"])
query = " ".join(task.split())[:240]
repo_url = holdout.canonical_repo_url(str(row["repo"]))
verify_command = ["{python}", "-m", "pytest", "-q", *focused]
regression_command = ["{python}", "-m", "pytest", "-q", *regressions]
verifier_binding = {
"allowed_paths_sha256": _sha256(_canonical_bytes(production_paths)),
"base_commit": commit,
"bridge_sha256": pins["bridge_sha256"],
"dataset_row_sha256": _sha256(_canonical_bytes(row)),
"dataset_sha256": protocol["universe"]["selection_jsonl_sha256"],
"docker_cli_sha256": pins["docker_cli_sha256"],
"docker_daemon_id_sha256": _sha256(str(pins["docker_daemon_id"]).encode()),
"docker_package_sha256": pins["docker_package_sha256"],
"docker_server_version": pins["docker_server_version"],
"fail_to_pass_sha256": _sha256(_canonical_bytes(focused)),
"harness_revision": pins["revision"],
"harness_source_sha256": red_evidence["authentication"]["source_sha256"],
"image_content_digest": image_id,
"pass_to_pass_sha256": _sha256(_canonical_bytes(regressions)),
"python_environment_sha256": pins["python_environment_sha256"],
"python_sha256": pins["python_sha256"],
"repository_tree_sha1": tree,
"repository_url": repo_url,
"run_evaluation_sha256": pins["run_evaluation_sha256"],
"runtime_identity_sha256": red_summary["runtime_identity_sha256"],
"schema_version": 1,
}
scenario_row = {
"allowed_changes": production_paths,
"benchmark_class": "historical",
"commit": commit,
"ctx_context": [],
"expected_test_count": len(focused),
"id": scenario_id,
"language": "python",
"official_verifier_binding": verifier_binding,
"query": query,
"red_failure_contains": red_marker,
"reference_patch": str(row["patch"]),
"regression_verify": [regression_command],
"repo_url": repo_url,
"reconstructed_test_sha256": test_sha256,
"task": task,
"test_body": test_source,
"test_path": test_path,
"verify": verify_command,
}
scenario = benchmark.Scenario(
id=scenario_id,
repo_url=repo_url,
commit=commit,
task=task,
query=query,
language="python",
benchmark_class="historical",
test_path=test_path,
test_body=test_source,
verify=tuple(verify_command),
expected_test_count=len(focused),
regression_verify=(tuple(regression_command),),
red_failure_contains=red_marker,
reference_patch=str(row["patch"]),
allowed_changes=tuple(production_paths),
context=(),
)
control = {
"changed_test_module_green": True,
"elapsed_seconds": round(elapsed, 6),
"green_evidence_sha256": green_summary["verifier_evidence_sha256"],
"module_evidence_sha256": green_summary["artifact_manifest_sha256"],
"official_swebench": {
"green": _control_phase_summary(green_summary),
"image_id": image_id,
"pins_sha256": _sha256(_canonical_bytes(pins)),
"red": _control_phase_summary(red_summary),
},
"parent_with_test_patch_red": True,
"reconstructed_test_sha256": test_sha256,
"red_evidence_sha256": red_summary["verifier_evidence_sha256"],
"reference_patch_green": True,
"timeout_compliant": True,
"timeout_seconds": timeout,
}
return scenario_row, scenario, control, test_source
def _validate_output_destination(output: Path) -> None:
resolved = output.resolve(strict=False)
private_root = (ROOT / ".gate" / "ctx-ab-private").resolve()
if ROOT.resolve() in resolved.parents and private_root not in resolved.parents:
raise MaterializationError("holdout output inside the repository must use the private root")
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
if output.exists() or output.is_symlink():
raise MaterializationError("holdout output already exists")
def _write_artifacts(
output: Path,
artifacts: dict[str, bytes],
*,
retained_evidence: Path,
) -> None:
_validate_output_destination(output)
temp = Path(tempfile.mkdtemp(prefix=".materialize-", dir=output.parent))
os.chmod(temp, 0o700)
try:
for key, data in artifacts.items():
path = temp / OUTPUT_FILES[key]
_private_write(path, data)
if stat.S_IMODE(path.stat().st_mode) != 0o600:
raise MaterializationError("private artifact permissions are unsafe")
retained_evidence.rename(temp / VERIFICATION_DIR)
temp.rename(output)
except BaseException as exc:
cleanup_temp = True
published_verification = temp / VERIFICATION_DIR
if published_verification.exists() and not retained_evidence.exists():
try:
published_verification.rename(retained_evidence)
except BaseException as rollback_exc:
try:
temp.rename(retained_evidence)
except BaseException as fallback_exc:
cleanup_temp = False
exc.add_note(
"materialization rollback failed; staged evidence retained at "
f"{temp} ({type(rollback_exc).__name__}, "
f"{type(fallback_exc).__name__})"
)
else:
temp = retained_evidence / ".published-output-staging"
if cleanup_temp and temp.exists():
shutil.rmtree(temp, ignore_errors=True)
raise
def materialize(
*,
protocol_path: Path,
expected_acquisition_protocol_sha256: str,
exposure_ledger_path: Path,
rows_path: Path,
selection_path: Path,
source_map_path: Path,
runtime_availability_path: Path,
catalog_archive_path: Path,
output: Path,
failure_evidence_output: Path,
swebench_checkout: Path,
swebench_python: Path,
docker_cli: Path,
docker_host: str,
) -> dict[str, str]:
protocol = _load_authenticated_protocol(
protocol_path,
expected_sha256=expected_acquisition_protocol_sha256,
)
try:
freezer._paths_are_distinct(
{
"protocol": protocol_path,
"exposure ledger": exposure_ledger_path,
"rows": rows_path,
"selection": selection_path,
"source map": source_map_path,
"runtime availability": runtime_availability_path,
"catalog archive": catalog_archive_path,
"output": output,
"failure evidence output": failure_evidence_output,
}
)
except freezer.FreezeError as exc:
raise MaterializationError("materialization inputs must not alias") from exc
try:
pins = freezer.validate_acquisition_protocol(
protocol,
benchmark_script_path=Path(benchmark.__file__),
catalog_archive_path=catalog_archive_path,
runtime_availability_path=runtime_availability_path,
)
except freezer.FreezeError as exc:
raise MaterializationError(
f"materializer requires a valid acquisition-frozen V2 protocol: {exc}"
) from exc
try:
exposure_document = exposure_ledger.load_authenticated_ledger(
exposure_ledger_path,
str(protocol["exposure_ledger_sha256"]),
)
except (OSError, ValueError) as exc:
raise MaterializationError("authenticated exposure ledger is invalid") from exc
timeout = protocol["timeouts"]["control_verification_seconds"]
rows_bytes = rows_path.read_bytes()
rows_sha256 = _sha256(rows_bytes)
if rows_sha256 != protocol["universe"].get("selection_jsonl_sha256"):
raise MaterializationError("canonical JSONL does not match the acquisition freeze")
rows = _load_jsonl(rows_path)
if len(rows) != int(protocol["universe"]["expected_rows"]):
raise MaterializationError("canonical JSONL row count does not match the protocol")
selection_bytes = selection_path.read_bytes()
selection = _load_json(selection_path)
if selection_bytes != _canonical_bytes(selection):
raise MaterializationError("private selection must use canonical JSON bytes")
ledger = holdout.reject_historical_exposures(
[holdout.evaluate_row(row, protocol) for row in rows],
exposure_document,
)
if selection != holdout.select_rows(ledger, protocol):
raise MaterializationError("private selection does not match deterministic selection")
try:
holdout.require_exposure_disjoint_selection(selection, exposure_document)
except ValueError as exc:
raise MaterializationError("private selection intersects historical exposure") from exc
selected_ids, repository_map = holdout._validated_selection(selection, protocol)
if len(selected_ids) != SCENARIO_COUNT or len(set(repository_map.values())) != SCENARIO_COUNT:
raise MaterializationError(
"V2 materialization requires exactly ten tasks from ten repositories"
)
rows_by_id = {str(row.get("instance_id") or ""): row for row in rows}
if len(rows_by_id) != len(rows) or any(item not in rows_by_id for item in selected_ids):
raise MaterializationError("selected rows are missing or duplicated")
try:
sources, _source_map_sha256 = freezer.validate_source_map(source_map_path)
except freezer.FreezeError as exc:
raise MaterializationError(f"private source map is invalid: {exc}") from exc
expected_source_urls = {
holdout.canonical_repo_url(str(rows_by_id[scenario_id]["repo"]))
for scenario_id in selected_ids
}
if set(sources) != expected_source_urls:
raise MaterializationError("private source map does not match selected repositories")
if _sha256(runtime_availability_path.read_bytes()) != protocol["product_inputs"].get(
"runtime_availability_sha256"
):
raise MaterializationError("runtime availability does not match the product freeze")
if _sha256(catalog_archive_path.read_bytes()) != protocol["product_inputs"].get(
"catalog_archive_sha256"
):
raise MaterializationError("catalog archive does not match the product freeze")
runtime = VerifierRuntime(
swebench_checkout=swebench_checkout,
swebench_python=swebench_python,
docker_cli=docker_cli,
docker_host=docker_host,
)
_validate_runtime(runtime)
_validate_output_destination(output)
try:
failure_evidence.validate_destination(
failure_evidence_output,
repository_root=ROOT,
)
except failure_evidence.FailureEvidenceError as exc:
raise MaterializationError("private failure evidence destination is invalid") from exc
scenario_rows: list[dict[str, Any]] = []
scenarios: list[benchmark.Scenario] = []
controls: dict[str, Any] = {}
reconstructed: dict[str, str] = {}
retained_evidence = Path(tempfile.mkdtemp(prefix=".official-verification-", dir=output.parent))
os.chmod(retained_evidence, 0o700)
preserve_retained_evidence = False
try:
work_root = Path(tempfile.mkdtemp(prefix=".ctx-holdout-materialize-", dir=output.parent))
os.chmod(work_root, 0o700)
try:
source_preflight_deadline = time.monotonic() + float(timeout)
for source_bundle in sources.values():
_validate_source_bundle_heads(
source_bundle,
cwd=work_root,
deadline=source_preflight_deadline,
)
for slot, scenario_id in enumerate(selected_ids):
row = rows_by_id[scenario_id]
scenario_row, scenario, control, reconstructed_source = _scenario(
row,
dataset_path=rows_path.resolve(strict=True),
protocol=protocol,
pins=pins,
retained_root=retained_evidence,
runtime=runtime,
source=_repo_source(row, sources),
slot=slot,
work_root=work_root,
timeout=float(timeout),
)
scenario_rows.append(scenario_row)
scenarios.append(scenario)
controls[scenario_id] = control
reconstructed[scenario_id] = reconstructed_source
except BaseException as exc:
try:
work_root.rename(retained_evidence / "raw-control-work")
except BaseException as capture_exc:
exc.add_note(
"raw control worktree publication failed; "
f"staging retained at {work_root} ({type(capture_exc).__name__})"
)
raise
else:
shutil.rmtree(work_root)
scenario_pack_bytes = _canonical_bytes({"scenarios": scenario_rows, "version": 1})
scenario_pack_sha256 = _sha256(scenario_pack_bytes)
collision: dict[str, Any] = dict(
benchmark.validate_runtime_pack_scenario_independence(
scenarios,
availability_path=runtime_availability_path,
archive_path=catalog_archive_path,
)
)
collision.update(
{
"collision_count": 0,
"collision_free": True,
"scenario_ids": sorted(selected_ids),
"scenarios_sha256": scenario_pack_sha256,
}
)
reconstructed_attestation = holdout.build_reconstructed_test_attestation(
selection,
protocol,
reconstructed,
)
selection_sha256 = _sha256(selection_bytes)
control_results = {
"all_scenarios_passed": len(controls) == SCENARIO_COUNT,
"guard": "holdout-control-results-v1",
"scenario_count": SCENARIO_COUNT,
"scenario_results": controls,
"scenario_pack_sha256": scenario_pack_sha256,
"selection_sha256": selection_sha256,
"verifier_pins_sha256": _sha256(_canonical_bytes(pins)),
}
artifacts = {
"scenario_pack": scenario_pack_bytes,
"collision": _canonical_bytes(collision),
"reconstructed": _canonical_bytes(reconstructed_attestation),
"controls": _canonical_bytes(control_results),
}
_write_artifacts(
output,
artifacts,
retained_evidence=retained_evidence,
)
return {key: _sha256(value) for key, value in artifacts.items()}
except BaseException as exc:
preserve_retained_evidence = True
failure_evidence.publish_failure(
staging=retained_evidence,
destination=failure_evidence_output,
operation="holdout-materialization",
exc=exc,
repository_root=ROOT,
)
raise
finally:
if retained_evidence.exists() and not preserve_retained_evidence:
shutil.rmtree(retained_evidence, ignore_errors=True)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--protocol", type=Path, required=True)
parser.add_argument("--expected-acquisition-protocol-sha256", required=True)
parser.add_argument("--exposure-ledger", type=Path, required=True)
parser.add_argument("--rows", type=Path, required=True)
parser.add_argument("--selection", type=Path, required=True)
parser.add_argument("--source-map", type=Path, required=True)
parser.add_argument("--runtime-availability", type=Path, required=True)
parser.add_argument("--catalog-archive", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--failure-evidence-output", type=Path, required=True)
parser.add_argument("--swebench-checkout", type=Path, required=True)
parser.add_argument("--swebench-python", type=Path, required=True)
parser.add_argument("--docker-cli", type=Path, required=True)
parser.add_argument("--docker-host", required=True)
args = parser.parse_args(argv)
try:
failure_evidence.validate_destination(
args.failure_evidence_output,
repository_root=ROOT,
)
except failure_evidence.FailureEvidenceError:
parser.exit(2, "materialization precondition failed; evidence=unavailable\n")
try:
hashes = materialize(
protocol_path=args.protocol,
expected_acquisition_protocol_sha256=args.expected_acquisition_protocol_sha256,
exposure_ledger_path=args.exposure_ledger,
rows_path=args.rows,
selection_path=args.selection,
source_map_path=args.source_map,
runtime_availability_path=args.runtime_availability,
catalog_archive_path=args.catalog_archive,
output=args.output,
failure_evidence_output=args.failure_evidence_output,
swebench_checkout=args.swebench_checkout,
swebench_python=args.swebench_python,
docker_cli=args.docker_cli,
docker_host=args.docker_host,
)
controls = _load_json(args.output / OUTPUT_FILES["controls"])
scenario_count = controls["scenario_count"]
except BaseException as exc:
evidence_status = "preserved"
if not failure_evidence.already_preserved(args.failure_evidence_output):
try:
failure_evidence.publish_failure(
destination=args.failure_evidence_output,
operation="holdout-materialization",
exc=exc,
repository_root=ROOT,
)
except BaseException:
evidence_status = "unavailable"
parser.exit(
2,
"materialization failed; private details suppressed "
f"({type(exc).__name__}); evidence={evidence_status}\n",
)
print(
f"materialized {scenario_count} private scenarios; "
+ " ".join(f"{key}_sha256={value}" for key, value in sorted(hashes.items()))
)
return 0
if __name__ == "__main__":
raise SystemExit(main())
|