diff --git a/.github/workflows/current-site-native-registry-controls.yml b/.github/workflows/current-site-native-registry-controls.yml new file mode 100644 index 000000000..67e50d014 --- /dev/null +++ b/.github/workflows/current-site-native-registry-controls.yml @@ -0,0 +1,113 @@ +name: Current-site native registry controls + +'on': + pull_request: + branches: [main] + paths: + - tests/test_current_site_native_registry.py + - tools/current_site_native_registry.py + - .github/workflows/current-site-native-registry-controls.yml + +permissions: + contents: read + +jobs: + registry-controls: + name: native-registry-${{ matrix.mode }} + runs-on: ubuntu-24.04 + timeout-minutes: 10 + strategy: + fail-fast: false + matrix: + mode: [normal, optimized] + env: + IMAGE: python@sha256:83f339c1be6340ae1096010fdccf6552ac932d8f410d45d206014916bdf37e48 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + timeout-minutes: 1 + with: + persist-credentials: false + fetch-depth: 1 + + - name: Record checked source and prepare pinned runtime + timeout-minutes: 3 + shell: bash + env: + NATIVE_MODE: ${{ matrix.mode }} + run: | + set -euo pipefail + printf 'repository=%s\nevent=%s\nrun_id=%s\nrun_attempt=%s\nmode=%s\nevent_sha=%s\nworkflow_ref=%s\nworkflow_sha=%s\n' \ + "$GITHUB_REPOSITORY" "$GITHUB_EVENT_NAME" "$GITHUB_RUN_ID" "$GITHUB_RUN_ATTEMPT" \ + "$NATIVE_MODE" "$GITHUB_SHA" "$GITHUB_WORKFLOW_REF" "$GITHUB_WORKFLOW_SHA" + checked_commit="$(git rev-parse --verify HEAD)" + checked_tree="$(git rev-parse --verify 'HEAD^{tree}')" + printf 'checked_commit=%s\nchecked_tree=%s\n' "$checked_commit" "$checked_tree" + test "$checked_commit" = "$GITHUB_SHA" + for path in .github/workflows/current-site-native-registry-controls.yml tests/test_current_site_native_registry.py; do + test -f "$path" + test ! -L "$path" + git ls-files --stage -- "$path" + sha256sum -- "$path" + done + path=tools/current_site_native_registry.py + if [ -e "$path" ] || [ -L "$path" ]; then + test -f "$path" + test ! -L "$path" + git ls-files --stage -- "$path" + sha256sum -- "$path" + else + printf 'production_module=ABSENT\n' + fi + timeout --signal=TERM --kill-after=10s 120s docker pull --platform linux/amd64 "$IMAGE" + runtime_identity="$(docker image inspect --format '{{.Id}} {{.Os}} {{.Architecture}}' "$IMAGE")" + printf 'image_reference=%s\nimage_identity=%s\n' "$IMAGE" "$runtime_identity" + test "$runtime_identity" = 'sha256:44d8f4434bcb025b8cb5c3151be689a906223e47cc3146971feccda936d2e82b linux amd64' + + - name: Run unchanged native registry cases in this mode + timeout-minutes: 4 + shell: bash + env: + NATIVE_MODE: ${{ matrix.mode }} + run: | + set -euo pipefail + container="native-registry-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}-${NATIVE_MODE}" + sandbox=(--pull=never --rm --log-driver none --platform linux/amd64 + --network none --read-only --user 65532:65532 --cpus 2 --memory 1g --pids-limit 128 + --cap-drop ALL --security-opt no-new-privileges + --tmpfs /tmp:rw,noexec,nosuid,size=128m + --mount "type=bind,source=$GITHUB_WORKSPACE,target=/source,readonly" + --workdir /source) + timeout --signal=TERM --kill-after=10s 30s docker run --name "$container" "${sandbox[@]}" "$IMAGE" \ + python3 -I -B -S -c 'import platform, sys; print("runtime_python=" + sys.version); print("runtime_machine=" + platform.machine()); sys.exit(0 if sys.version_info[:3] == (3, 11, 14) and platform.machine() == "x86_64" else 2)' + flags=(-I -B -S) + case "$NATIVE_MODE" in + normal) ;; + optimized) flags+=(-O) ;; + *) exit 2 ;; + esac + printf 'test_command: ' + printf '%q ' python3 "${flags[@]}" -m unittest discover -s tests -p test_current_site_native_registry.py -v + printf '\n' + set +e + timeout --signal=TERM --kill-after=10s 180s docker run --name "$container" "${sandbox[@]}" "$IMAGE" \ + python3 "${flags[@]}" -m unittest discover -s tests -p test_current_site_native_registry.py -v 2>&1 + status=$? + set -e + printf 'mode=%s\ntest_exit_status=%s\n' "$NATIVE_MODE" "$status" + exit "$status" + + - name: Remove any timed-out container + if: always() + timeout-minutes: 1 + shell: bash + env: + NATIVE_MODE: ${{ matrix.mode }} + run: | + set -euo pipefail + container="native-registry-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}-${NATIVE_MODE}" + present="$(docker ps -aq --filter "name=^/${container}$")" + if [ -n "$present" ]; then + timeout --signal=TERM --kill-after=5s 15s docker rm --force "$container" + fi + remaining="$(docker ps -aq --filter "name=^/${container}$")" + test -z "$remaining" diff --git a/tests/test_current_site_native_registry.py b/tests/test_current_site_native_registry.py new file mode 100644 index 000000000..eaa5642c1 --- /dev/null +++ b/tests/test_current_site_native_registry.py @@ -0,0 +1,815 @@ +"""Private Task1 behavioral contract; execute ONLY on hosted ephemeral runners. + +Fixtures are independently constructed metadata/byte joins, not ZIP admission +or scientific evidence. No production import happens during test collection. +""" +from __future__ import annotations + +import copy +import hashlib +import importlib.util +from pathlib import Path +import sys +import json +import io +import os +import socket +import subprocess +import unittest +import urllib.request +from unittest.mock import patch + + +REPOSITORY = "d6g8k5htny-coder/main" +ORIGIN = "https://d6g8k5htny-coder.github.io/main/site/" +BUILD = "b" * 40 +PRODUCER = "a" * 40 +TREE = "c" * 40 +ARTIFACT = "1" * 64 +MIB = 1024 * 1024 +SOURCE = { + "repository": "d6g8k5htny-coder/Math-", + "commit": "7858329974e28be79f29b22644370084ff43da4f", + "captured_at": "2026-10-03T15:07:03Z", + "graph_sha256": "8822e9618678321a342d69cd0b8ae6552de1b5d578c331de5072b2892ee9dd09", + "gate_sha256": "a78f3e25f3b0cfe113e618a4c31a7a25d7f22af638c46dec1ecba221fa333ac8", +} +BINDING = { + "checked_commit": PRODUCER, "repository": REPOSITORY, + "run_id": "100", "run_attempt": "2", "receipt_sha256": "2" * 64, + "artifact_id": "300", "artifact_sha256": ARTIFACT, + "artifact_name": "research-architecture-100-2", +} +SELECTION = { + "workflow_id": "400", "workflow_path": ".github/workflows/workspace-landing.yml", + "event": "push", "original_created_at": "2026-10-25T12:00:00Z", + "producer_tree": TREE, +} +CONTEXT = {"repository": REPOSITORY, "ref": "refs/heads/main", "commit": BUILD, + "run_id": 500, "run_attempt": 1, "event": "schedule"} +BASE = {"receipt_commit": None, "public_receipt_commit": None, + "registry_sha256": None, "sequence": 0} +PAYLOAD = b'{"fixture":"current-payload"}\n' + + +def canonical(value): + """Independent serialization only; never calls a production helper.""" + return (json.dumps(value, sort_keys=True, ensure_ascii=True, + separators=(",", ":"), allow_nan=False) + "\n").encode("ascii") + + +def digest(raw): + return hashlib.sha256(raw).hexdigest() + + +def descriptor(path, raw): + return {"path": path, "bytes": len(raw), "sha256": digest(raw)} + + +def entry(prefix=ARTIFACT, lengths=(1, 1, 1)): + """Metadata policy fixture; no corresponding archive is claimed.""" + binding = dict(BINDING, artifact_sha256=prefix) + names = ("architecture-run-receipt.json", "graph.json", "projection.json") + files = [{"path": "native-graph/" + prefix + "/" + name, + "bytes": length, "sha256": "3" * 64} + for name, length in zip(names, lengths)] + return {"binding": binding, "selection": dict(SELECTION), "files": files} + + +def registry(entries=None, current=ARTIFACT, sequence=1, previous=None): + if entries is None: + entries = [entry()] + total = sum(item["bytes"] for row in entries for item in row["files"]) + return { + "schema_version": 1, "scientific_effect": "NONE", + "scientific_status_authority": False, "custody": "unknown", "origin": ORIGIN, + "sequence": sequence, "previous_registry_sha256": previous, + "current_artifact_sha256": current, "entries": entries, "immutable_bytes": total, + "capacity": {"archives": len(entries), "maximum_archives": 8192, + "bytes": total, "maximum_bytes": 536870912}, + } + + +def bundle_fixture(prefix=ARTIFACT, producer=PRODUCER): + """Small pure-byte closure fixture, never an acquired or admitted ZIP.""" + graph = {"schema_version": 1, "source": dict(SOURCE), "nodes": {}, "edges": [], + "dimensions": {}, "scientific_effect": "NONE", "scientific_status_authority": False} + graph_raw = canonical(graph) + image = [{"Id": "sha256:"+"4"*64, + "RepoDigests": ["python@sha256:83f339c1be6340ae1096010fdccf6552ac932d8f410d45d206014916bdf37e48"], + "Os": "linux", "Architecture": "amd64"}] + # Thirteen closed archive member descriptors; these fixture bytes are never zipped. + member_raws = { + "generated/graph.json": graph_raw, "checked-commit.txt": (producer+"\n").encode("ascii"), + "run-id.txt": b"100\n", "run-attempt.txt": b"2\n", "container-exit-status.txt": b"0\n", + "tests-normal.log": b"fixture-only-normal-not-an-admission-log\n", + "tests-optimized.log": b"fixture-only-optimized-not-an-admission-log\n", + "runtime-image.json": canonical(image), "exporter.stdout": b"/output/generated/graph.json\n", + "exporter.stderr": b"", "console.log": b"fixture-only-console\n", + } + receipt = { + "schema_version": 1, "scientific_effect": "NONE", "scientific_status_authority": False, + "checked_commit": producer, "repository": REPOSITORY, "run_id": "100", "run_attempt": "2", + "runtime_image": {"id": "sha256:" + "4" * 64, + "repo_digest": "python@sha256:83f339c1be6340ae1096010fdccf6552ac932d8f410d45d206014916bdf37e48"}, + "test_modes": [{"mode": mode, "test_count": 1, + "test_ids": ["test_architecture_fixture.Fixture.test_fixture"]} + for mode in ("normal", "optimized")], + "graph_source": dict(SOURCE), + "evidence_file_sha256": {name: digest(member_raws[name]) for name in ( + "generated/graph.json", "checked-commit.txt", "run-id.txt", "run-attempt.txt", + "container-exit-status.txt", "tests-normal.log", "tests-optimized.log", "runtime-image.json")}, + } + receipt["evidence_file_sha256"]["generated/graph.json"] = digest(graph_raw) + receipt_raw = canonical(receipt) + binding = dict(BINDING, artifact_sha256=prefix, checked_commit=producer, + receipt_sha256=digest(receipt_raw)) + member = descriptor("generated/graph.json", graph_raw) + member_raws["architecture-run-receipt.json"] = receipt_raw + member_raws["ARTIFACT_SHA256SUMS"] = "".join( + digest(member_raws[name])+" "+name+"\n" for name in sorted(member_raws)).encode("ascii") + admission = { + "schema_version": 1, "scientific_effect": "NONE", "scientific_status_authority": False, + "custody": "unknown", "verification_scope": "declared-native-pins-and-archive-content", + "binding": binding, + "archive": {"sha256": prefix, "bytes": 64000, + "members": [descriptor(name, member_raws[name]) for name in sorted(member_raws)]}, + "graph_member": member, "producer_receipt_sha256": digest(receipt_raw), + "producer_receipt": receipt, + } + projection = { + "schema_version": 1, "scientific_effect": "NONE", "scientific_status_authority": False, + "custody": "unknown", "graph": {"identity": {"kind": "native-artifact-member", + "binding": binding, "member": member}, "data": graph}, "admission": admission, + } + stem = "native-graph/" + prefix + "/" + files = {stem + "graph.json": graph_raw, stem + "architecture-run-receipt.json": receipt_raw, + stem + "projection.json": canonical(projection), + "proof-tree.html": b'Fixture\n'} + manifest = { + "schema_version": 1, "scientific_effect": "NONE", "scientific_status_authority": False, + "custody": "unknown", "verification_scope": "declared-build-context-and-admitted-native-archive", + "build": {"repository": REPOSITORY, "commit": BUILD, + "current_payload": descriptor("current.json", PAYLOAD)}, + "native": {"binding": binding, "graph_source": dict(SOURCE)}, + "files": [descriptor(path, files[path]) for path in sorted(files)], + } + files["current-build-manifest.json"] = canonical(manifest) + return files, binding, dict(SELECTION), manifest + + +def reframe_manifest(files): + """Rehash a deliberate mutation so its deeper identity join gets exercised.""" + manifest = json.loads(files["current-build-manifest.json"]) + manifest["files"] = [descriptor(path, files[path]) for path in sorted(files) + if path != "current-build-manifest.json"] + files["current-build-manifest.json"] = canonical(manifest) + + + +def publication_candidate(version=2, build_commit=BUILD, binding=None, selection=None): + """Full candidate shape independently copied from frozen v1 Source/R04.""" + if binding is None: + binding = dict(BINDING) + if selection is None: + selection = dict(SELECTION) + candidate = { + "schema_version": version, "scientific_effect": "NONE", "repository": REPOSITORY, + "receipt_branch": "codex/site-publication-receipts", + "source": {"commit": build_commit, "ref": "refs/heads/main", + "workflow": ".github/workflows/current-site.yml"}, + "run": {"id": 500, "attempt": 1}, + "artifact": {"sha256": "6"*64, "files": 12, "bytes": 10000}, + "manifest": {"bytes": 100, "sha256": "7"*64}, + "metadata": {"bytes": 100, "sha256": "8"*64}, + "payload": {"bytes": len(PAYLOAD), "sha256": digest(PAYLOAD)}, + "snapshot_ids": ["9"*64], + } + if version == 2: + candidate["native"] = { + "base": dict(BASE), "sequence": 1, "binding": copy.deepcopy(binding), + "selection": copy.deepcopy(selection), + "registry": {"path": "native-graph/index.json", "bytes": 1000, "sha256": "a"*64}, + "build_manifest": {"path": "current-build-manifest.json", "bytes": 2000, "sha256": "b"*64}, + "readback_scope": "legacy3+current-native5+registry;retained-native-assets-not-observed", + } + return candidate + + +def progress_current(build_commit=BUILD, producer=PRODUCER, prefix=ARTIFACT, + run_id="100", attempt="2", created_at="2026-10-25T12:00:00Z", + build_tree=TREE, producer_tree=TREE): + binding = dict(BINDING, checked_commit=producer, artifact_sha256=prefix, + run_id=run_id, run_attempt=attempt, + artifact_name="research-architecture-"+run_id+"-"+attempt) + selection = dict(SELECTION, original_created_at=created_at, producer_tree=producer_tree) + return {"build": {"repository": REPOSITORY, "commit": build_commit, "tree": build_tree}, + "producer": {"binding": binding, "selection": selection}} + + +def native_commit(sha, tree, parents=()): + """Ordinary complete native compare commit object, not a bool verdict.""" + api = "https://api.github.com/repos/"+REPOSITORY + web = "https://github.com/"+REPOSITORY + person = {"name": "Fixture", "email": "fixture@example.invalid", "date": "2026-10-25T12:00:00Z"} + return { + "sha": sha, "node_id": "fixture-native-commit-"+sha, + "commit": {"author": dict(person), "committer": dict(person), "message": "Fixture commit", + "tree": {"sha": tree, "url": api+"/git/trees/"+tree}, + "url": api+"/git/commits/"+sha, "comment_count": 0, + "verification": {"verified": False, "reason": "unsigned", "signature": None, + "payload": None, "verified_at": None}}, + "url": api+"/commits/"+sha, "html_url": web+"/commit/"+sha, + "comments_url": api+"/commits/"+sha+"/comments", "author": None, "committer": None, + "parents": [{"sha": parent, "url": api+"/commits/"+parent, + "html_url": web+"/commit/"+parent} for parent in parents], + } + + +def native_comparison(base, head, count=1, base_tree=TREE, head_tree="d"*40): + """Complete independently authored GitHub response object; no trust marker.""" + api = "https://api.github.com/repos/"+REPOSITORY + web = "https://github.com/"+REPOSITORY + previous = base + commits = [] + for index in range(count): + sha = head if index == count-1 else format(index+1000, "040x") + commits.append(native_commit(sha, head_tree, (previous,))) + previous = sha + original = native_commit(base, base_tree) + return { + "url": api+"/compare/"+base+"..."+head, + "html_url": web+"/compare/"+base+"..."+head, + "permalink_url": web+"/compare/"+base+"..."+head, + "diff_url": web+"/compare/"+base+"..."+head+".diff", + "patch_url": web+"/compare/"+base+"..."+head+".patch", + "base_commit": original, "merge_base_commit": copy.deepcopy(original), + "status": "ahead", "ahead_by": count, "behind_by": 0, + "total_commits": count, "commits": commits, "files": [], + } + + +class CurrentSiteNativeRegistryContract(unittest.TestCase): + def shortDescription(self): + return None + + def api(self, name): + module_path = Path(__file__).resolve().parents[1] / "tools" / "current_site_native_registry.py" + if not module_path.is_file(): + self.fail("missing Task1 exact Source file inside collected assertion: " + str(module_path)) + module_name = "_task1_current_site_native_registry_exact_source" + module = sys.modules.get(module_name) + if module is None: + spec = importlib.util.spec_from_file_location(module_name, module_path) + if spec is None or spec.loader is None: + self.fail("cannot load Task1 exact Source file inside collected assertion: " + str(module_path)) + module = importlib.util.module_from_spec(spec) + # Register before execution, as normal imports do; dataclasses may + # consult sys.modules while processing postponed annotations. + sys.modules[module_name] = module + try: + spec.loader.exec_module(module) + except BaseException: + if sys.modules.get(module_name) is module: + del sys.modules[module_name] + raise + else: + self.assertEqual(getattr(module, "__file__", None), str(module_path), + "cached Task1 module must come from the exact Source file") + function = getattr(module, name, None) + self.assertTrue(callable(function), "missing Task1 callable: " + name) + return function + + def admit(self, function, files, binding, selection, context=None, payload=PAYLOAD): + return function(files, context=dict(CONTEXT) if context is None else context, + current_payload_bytes=payload, binding=binding, selection=selection) + + def build(self, function, previous, files, binding, selection, base=None, context=None): + return function(previous, files, base=dict(BASE) if base is None else base, + context=dict(CONTEXT) if context is None else context, + current_payload_bytes=PAYLOAD, binding=binding, selection=selection) + + def test_registry_round_trip_exact_closed_bytes(self): + # Catches loss of any closed field or alteration of independent golden bytes. + validate = self.api("validate_native_registry") + expected = registry() + raw = canonical(expected) + self.assertEqual(validate(raw), expected) + self.assertEqual(canonical(validate(raw)), raw) + for field in expected: + bad = copy.deepcopy(expected); del bad[field] + with self.subTest(missing=field), self.assertRaises(ValueError): validate(canonical(bad)) + bad = dict(expected, extra="refuse") + with self.assertRaises(ValueError): validate(canonical(bad)) + + def test_registry_strict_json_types_and_duplicate_nonfinite_documents(self): + # Catches permissive JSON parsing and bool/int coercion. + validate = self.api("validate_native_registry") + raw = canonical(registry()) + bad_raws = [raw.replace(b'"schema_version":1', b'"schema_version":1,"schema_version":1'), + raw.replace(b'"sequence":1', b'"sequence":NaN'), + raw.replace(b'"sequence":1', b'"sequence":Infinity'), + raw + b'{}\n', b'\xff' + raw, raw.decode("ascii"), bytearray(raw), + b'null\n', b'[]\n', b'1\n', b'true\n'] + for bad in bad_raws: + with self.subTest(kind=type(bad).__name__), self.assertRaises(ValueError): validate(bad) + for field, value in [("schema_version", True), ("sequence", True), ("immutable_bytes", True), + ("scientific_status_authority", 0), ("sequence", 0), + ("sequence", 2**63), ("sequence", "1"), ("sequence", 1.0), + ("custody", "known"), ("scientific_effect", "PASS"), + ("origin", ORIGIN.replace("https:", "http:"))]: + bad = registry(); bad[field] = value + with self.subTest(field=field, value=value), self.assertRaises(ValueError): validate(canonical(bad)) + high = registry(sequence=2**63-1) + self.assertEqual(validate(canonical(high)), high) + + def test_registry_nested_closed_fields_and_pointer_capacity_joins(self): + # Catches forged current pointers, aggregate counters or nested extras. + validate = self.api("validate_native_registry") + changes = [lambda r: r.update(current_artifact_sha256="f"*64), + lambda r: r.update(previous_registry_sha256="F"*64), + lambda r: r.update(immutable_bytes=4), + lambda r: r["capacity"].update(archives=2), + lambda r: r["capacity"].update(bytes=4), + lambda r: r["capacity"].update(maximum_archives=8193), + lambda r: r["capacity"].update(maximum_bytes=536870913), + lambda r: r["capacity"].update(extra=0), + lambda r: r["entries"][0].update(extra=0), + lambda r: r["entries"][0]["binding"].update(extra="x"), + lambda r: r["entries"][0]["selection"].update(extra="x"), + lambda r: r["entries"][0]["files"][0].update(extra="x"), + lambda r: r["entries"][0]["files"][0].update(bytes=True), + lambda r: r["entries"][0]["files"][0].update(sha256="A"*64), + lambda r: r["entries"][0]["files"][0].update(sha256=True), + lambda r: r["capacity"].update(archives=True), + lambda r: r.update(entries=[])] + for index, mutate in enumerate(changes): + bad = registry(); mutate(bad) + with self.subTest(case=index), self.assertRaises(ValueError): validate(canonical(bad)) + for section in ("capacity",): + for key in registry()[section]: + bad = registry(); del bad[section][key] + with self.subTest(section=section, missing=key), self.assertRaises(ValueError): validate(canonical(bad)) + for key in ("path", "bytes", "sha256"): + bad = registry(); del bad["entries"][0]["files"][0][key] + with self.subTest(descriptor_missing=key), self.assertRaises(ValueError): validate(canonical(bad)) + for section in ("binding", "selection"): + for key in entry()[section]: + bad = registry(); del bad["entries"][0][section][key] + with self.subTest(section=section, missing=key), self.assertRaises(ValueError): validate(canonical(bad)) + + def test_registry_binding_selection_grammar_and_native_utc(self): + # Catches stale artifact-name joins and permissive decimal/date grammar. + validate = self.api("validate_native_registry") + cases = [("binding", "run_id", "01"), ("binding", "run_attempt", 2), + ("binding", "artifact_id", "0"), ("binding", "artifact_sha256", "A"*64), + ("binding", "checked_commit", "A"*40), ("binding", "receipt_sha256", "a"*63), + ("binding", "repository", "../main"), ("binding", "artifact_name", "research-architecture-100-1"), + ("selection", "workflow_id", "01"), ("selection", "workflow_id", 400), + ("selection", "workflow_path", ".github/workflows/other.yml"), + ("selection", "event", "schedule"), ("selection", "producer_tree", "C"*40), + ("selection", "original_created_at", "2026-02-30T12:00:00Z"), + ("selection", "original_created_at", "2026-10-25T12:00:00+00:00"), + ("selection", "original_created_at", "2026-10-25T12:00:00.0Z")] + for section, key, value in cases: + bad = registry(); bad["entries"][0][section][key] = value + with self.subTest(section=section, field=key, value=value), self.assertRaises(ValueError): + validate(canonical(bad)) + + def test_registry_exact_sorted_paths_and_unique_prefixes(self): + # Catches path traversal, case drift, duplicate descriptors and unsorted archives. + validate = self.api("validate_native_registry") + for path in ["native-graph/"+ARTIFACT+"/../graph.json", "native-graph/"+ARTIFACT+"/%67raph.json", + "native-graph/"+ARTIFACT+"/GRAPH.json", "native-graph\\"+ARTIFACT+"\\graph.json", + "native-graph/"+"f"*64+"/graph.json", "/native-graph/"+ARTIFACT+"/graph.json"]: + bad = registry(); bad["entries"][0]["files"][1]["path"] = path + with self.subTest(path=path), self.assertRaises(ValueError): validate(canonical(bad)) + for files in [entry()["files"][::-1], entry()["files"][:-1], entry()["files"]+[entry()["files"][0]], + [entry()["files"][0]]*3]: + bad = registry(); bad["entries"][0]["files"] = files + with self.subTest(files=files), self.assertRaises(ValueError): validate(canonical(bad)) + for entries in [[entry(), entry()], [entry("2"*64), entry()]]: + with self.subTest(prefixes=[r["binding"]["artifact_sha256"] for r in entries]), self.assertRaises(ValueError): + validate(canonical(registry(entries))) + + def test_manifest_payload_build_and_binding_joins_refuse_before_output(self): + # Catches independent manifest/binding/context or exact payload-byte substitution. + admit = self.api("admit_current_native_bundle") + files, binding, selection, expected = bundle_fixture() + admitted = self.admit(admit, files, binding, selection) + self.assertIs(type(admitted), dict) + self.assertEqual(admitted["build"], expected["build"]) + self.assertEqual(admitted["native"], expected["native"]) + self.assertEqual(admitted["files"], expected["files"]) + for field, value in [("commit", "d"*40), ("repository", "other/repository")]: + bad = dict(CONTEXT); bad[field] = value + with self.subTest(context=field), self.assertRaises(ValueError): self.admit(admit, files, binding, selection, bad) + with self.assertRaises(ValueError): self.admit(admit, files, binding, selection, payload=PAYLOAD+b" ") + for field, value in [("checked_commit", "d"*40), ("artifact_id", "301"), + ("receipt_sha256", "f"*64), ("run_attempt", "3")]: + bad = dict(binding); bad[field] = value + with self.subTest(binding=field), self.assertRaises(ValueError): self.admit(admit, files, bad, selection) + + def test_manifest_exact_four_descriptors_excludes_self_and_five_buffers(self): + # Catches sixth-output adoption, missing output, forged descriptors and self hashing. + admit = self.api("admit_current_native_bundle") + files, binding, selection, _ = bundle_fixture() + self.admit(admit, files, binding, selection) + for path in files: + bad = dict(files); del bad[path] + with self.subTest(missing=path), self.assertRaises(ValueError): self.admit(admit, bad, binding, selection) + for path in ["extra.txt", "native-graph/"+"f"*64+"/graph.json"]: + bad = dict(files); bad[path] = b"x" + with self.subTest(extra=path), self.assertRaises(ValueError): self.admit(admit, bad, binding, selection) + changes = [lambda m: m["files"].append(descriptor("current-build-manifest.json", b"x")), + lambda m: m["files"].reverse(), + lambda m: m["files"][0].update(bytes=True), + lambda m: m["files"][0].update(bytes=m["files"][0]["bytes"]+1), + lambda m: m["files"][0].update(sha256="f"*64), + lambda m: m["files"][0].update(extra="x"), + lambda m: m.update(extra="x"), lambda m: m["build"].update(extra="x"), + lambda m: m["native"].update(extra="x"), + lambda m: m["native"]["graph_source"].update(commit="d"*40), + lambda m: m["build"]["current_payload"].update(path="other.json"), + lambda m: m.update(scientific_status_authority=0)] + for index, mutate in enumerate(changes): + bad = dict(files); manifest = json.loads(bad["current-build-manifest.json"]); mutate(manifest) + bad["current-build-manifest.json"] = canonical(manifest) + with self.subTest(case=index), self.assertRaises(ValueError): self.admit(admit, bad, binding, selection) + + def test_manifest_strict_json_and_exact_byte_map_types(self): + # Catches forgiving duplicate/nonfinite parsers or mutable/nonbyte buffers. + admit = self.api("admit_current_native_bundle") + files, binding, selection, _ = bundle_fixture() + manifest = files["current-build-manifest.json"] + for raw in [manifest.replace(b'"schema_version":1', b'"schema_version":1,"schema_version":1'), + manifest.replace(b'"schema_version":1', b'"schema_version":NaN'), + manifest+b'{}', b'\xff', bytearray(manifest), manifest.decode("ascii")]: + bad = dict(files); bad["current-build-manifest.json"] = raw + with self.subTest(kind=type(raw).__name__), self.assertRaises(ValueError): self.admit(admit, bad, binding, selection) + for value in [bytearray(files["proof-tree.html"]), "page", None]: + bad = dict(files); bad["proof-tree.html"] = value + with self.subTest(kind=type(value).__name__), self.assertRaises(ValueError): self.admit(admit, bad, binding, selection) + + def test_manifest_and_original_receipt_projection_identity_joins(self): + # Catches accepting internally rehashed content with a different admitted binding. + admit = self.api("admit_current_native_bundle") + files, binding, selection, _ = bundle_fixture() + prefix = "native-graph/"+ARTIFACT+"/" + for path, location, field, value in [ + (prefix+"projection.json", ("admission", "binding"), "artifact_id", "301"), + (prefix+"projection.json", ("graph", "identity", "binding"), "checked_commit", "d"*40), + (prefix+"projection.json", ("admission",), "producer_receipt_sha256", "f"*64), + (prefix+"projection.json", ("graph", "identity", "member"), "sha256", "f"*64), + (prefix+"projection.json", ("admission", "graph_member"), "bytes", 1), + (prefix+"architecture-run-receipt.json", (), "run_id", "101"), + (prefix+"graph.json", ("source",), "commit", "d"*40)]: + bad = dict(files); document = json.loads(bad[path]); target = document + for key in location: target = target[key] + target[field] = value; bad[path] = canonical(document); reframe_manifest(bad) + with self.subTest(path=path, location=location, field=field), self.assertRaises(ValueError): + self.admit(admit, bad, binding, selection) + + def test_execution_context_is_closed_exact_and_main_only(self): + # Catches widened events/refs/repositories and integer coercion. + admit = self.api("admit_current_native_bundle") + files, binding, selection, _ = bundle_fixture() + for event in ["push", "schedule", "workflow_dispatch"]: + self.admit(admit, files, binding, selection, dict(CONTEXT, event=event)) + for field in CONTEXT: + bad = dict(CONTEXT); del bad[field] + with self.subTest(missing=field), self.assertRaises(ValueError): self.admit(admit, files, binding, selection, bad) + for field, value in [("extra", "x"), ("event", "pull_request"), ("ref", "refs/heads/other"), + ("repository", "D6g8k5htny-coder/main"), ("run_id", True), + ("run_id", "500"), ("run_id", 0), ("run_attempt", 0), ("run_attempt", 1.0), + ("commit", BUILD.upper())]: + bad = dict(CONTEXT); bad[field] = value + with self.subTest(field=field, value=value), self.assertRaises(ValueError): self.admit(admit, files, binding, selection, bad) + + def test_page_and_manifest_capacity_boundaries(self): + # Catches page/manifest raw-byte caps; no archive or aggregate-boundary claim. + admit = self.api("admit_current_native_bundle") + files, binding, selection, _ = bundle_fixture() + exact = dict(files); exact["proof-tree.html"] = b"x"*(4*MIB); reframe_manifest(exact) + self.admit(admit, exact, binding, selection) + over = dict(exact); over["proof-tree.html"] += b"x"; reframe_manifest(over) + with self.assertRaises(ValueError): self.admit(admit, over, binding, selection) + # A manifest may contain JSON whitespace on input; produced bytes are canonical. + # Exact manifest capacity is independently tested without adding a schema field. + raw = files["current-build-manifest.json"] + exact = dict(files); exact["current-build-manifest.json"] = raw[:-1] + b" "*(65536-len(raw)) + b"\n" + self.admit(admit, exact, binding, selection) + over = dict(exact); over["current-build-manifest.json"] += b" " + with self.assertRaises(ValueError): self.admit(admit, over, binding, selection) + + def test_build_registry_exact_independent_golden_and_original_base(self): + # Catches a changed serializer, sequence bootstrap or accidental sixth bundle output. + build = self.api("build_native_registry") + files, binding, selection, _ = bundle_fixture() + before = copy.deepcopy(files) + expected_entry = {"binding": binding, "selection": selection, + "files": [descriptor(path, files[path]) for path in sorted(files) + if path.startswith("native-graph/")]} + expected = canonical(registry([expected_entry])) + first = self.build(build, None, files, binding, selection) + second = self.build(build, None, files, binding, selection) + self.assertIs(type(first), bytes) + self.assertEqual(first, expected) + self.assertEqual(second, expected) + self.assertEqual(files, before) + self.assertEqual(len(files), 5) + + def test_union_conserves_all_old_entries_and_reuse_adds_zero_versions(self): + # Catches dropping old entries or treating each refresh as a new archive. + build = self.api("build_native_registry") + files, binding, selection, _ = bundle_fixture() + current_entry = {"binding": binding, "selection": selection, + "files": [descriptor(path, files[path]) for path in sorted(files) + if path.startswith("native-graph/")]} + older = entry("0"*64) + old = registry([older, current_entry], sequence=7) + old_raw = canonical(old) + base = {"receipt_commit": "d"*40, "public_receipt_commit": "e"*40, + "registry_sha256": digest(old_raw), "sequence": 7} + expected = registry([older, current_entry], sequence=8, previous=digest(old_raw)) + result = self.build(build, old_raw, files, binding, selection, base) + self.assertEqual(result, canonical(expected)) + self.assertEqual(json.loads(result)["immutable_bytes"], old["immutable_bytes"]) + self.assertEqual(json.loads(result)["capacity"]["archives"], 2) + added_files, added_binding, added_selection, _ = bundle_fixture("f"*64) + added_entry = {"binding": added_binding, "selection": added_selection, + "files": [descriptor(path, added_files[path]) for path in sorted(added_files) + if path.startswith("native-graph/")]} + want = registry([older, current_entry, added_entry], "f"*64, 8, digest(old_raw)) + self.assertEqual(self.build(build, old_raw, added_files, added_binding, added_selection, base), canonical(want)) + + def test_same_prefix_entire_old_binding_selection_and_descriptors_are_conserved(self): + # Catches overwriting a retained identity under the same original-ZIP prefix. + build = self.api("build_native_registry") + files, binding, selection, _ = bundle_fixture() + good = {"binding": binding, "selection": selection, + "files": [descriptor(path, files[path]) for path in sorted(files) if path.startswith("native-graph/")]} + changes = [lambda e: e["binding"].update(artifact_id="301"), + lambda e: e["selection"].update(workflow_id="401"), + lambda e: e["selection"].update(producer_tree="d"*40), + lambda e: e["files"][0].update(sha256="f"*64), + lambda e: e["files"][0].update(bytes=e["files"][0]["bytes"]+1)] + for index, mutate in enumerate(changes): + old_entry = copy.deepcopy(good); mutate(old_entry) + old_raw = canonical(registry([old_entry], sequence=4)) + base = {"receipt_commit": "d"*40, "public_receipt_commit": "e"*40, + "registry_sha256": digest(old_raw), "sequence": 4} + with self.subTest(case=index), self.assertRaises(ValueError): + self.build(build, old_raw, files, binding, selection, base) + + def test_unregistered_prefix_is_not_adopted(self): + # Catches silently adopting an orphan immutable path in the five-output input. + build = self.api("build_native_registry") + files, binding, selection, _ = bundle_fixture() + files["native-graph/"+"f"*64+"/graph.json"] = b"orphan" + with self.assertRaises(ValueError): self.build(build, None, files, binding, selection) + + def test_base_exact_closed_lineage_hash_sequence_and_types(self): + # Catches resetting lineage after unknown/mismatching prior state. + build = self.api("build_native_registry") + files, binding, selection, _ = bundle_fixture() + for field, value in [("sequence", True), ("sequence", 1), ("sequence", -1), + ("registry_sha256", "f"*64), ("receipt_commit", "D"*40), + ("public_receipt_commit", "x"), ("extra", None)]: + bad = dict(BASE); bad[field] = value + with self.subTest(field=field, value=value), self.assertRaises(ValueError): self.build(build, None, files, binding, selection, bad) + for field in BASE: + bad = dict(BASE); del bad[field] + with self.subTest(missing=field), self.assertRaises(ValueError): self.build(build, None, files, binding, selection, bad) + old_entry = {"binding": binding, "selection": selection, + "files": [descriptor(path, files[path]) for path in sorted(files) + if path.startswith("native-graph/")]} + old_raw = canonical(registry([old_entry], sequence=7)) + good_base = {"receipt_commit": "d"*40, "public_receipt_commit": "e"*40, + "registry_sha256": digest(old_raw), "sequence": 7} + self.assertEqual(json.loads(self.build(build, old_raw, files, binding, selection, good_base))["sequence"], 8) + for sequence, sha in [(0, digest(old_raw)), (7, "f"*64), (6, digest(old_raw))]: + base = {"receipt_commit": "d"*40, "public_receipt_commit": "e"*40, + "registry_sha256": sha, "sequence": sequence} + with self.subTest(sequence=sequence, sha=sha), self.assertRaises(ValueError): self.build(build, old_raw, files, binding, selection, base) + + def test8192_exact_and8193_refused(self): + # Catches an off-by-one archive count policy; metadata only, no ZIPs/reads. + validate = self.api("validate_native_registry") + rows = [entry(format(n, "064x")) for n in range(8192)] + expected = registry(rows, rows[-1]["binding"]["artifact_sha256"]) + self.assertEqual(validate(canonical(expected)), expected) + rows.append(entry(format(8192, "064x"))) + with self.assertRaises(ValueError): validate(canonical(registry(rows, rows[-1]["binding"]["artifact_sha256"]))) + + def test512mib_exact_and_one_over_refused_before_reads(self): + # Catches aggregate capacity overflow; API is pure and no archive is read. + validate = self.api("validate_native_registry") + rows = [entry(format(n, "064x"), (32*MIB-2, 1, 1)) for n in range(16)] + expected = registry(rows, rows[-1]["binding"]["artifact_sha256"]) + with patch("builtins.open", side_effect=AssertionError("pure validation attempted a read")): + self.assertEqual(validate(canonical(expected)), expected) + rows.append(entry("f"*64)) + # Use exactly +1 aggregate, preserving positive three-buffer descriptors: + rows[-2]["files"][0]["bytes"] -= 2 + over = registry(rows, "f"*64) + self.assertEqual(over["immutable_bytes"], 536870913) + with self.assertRaises(ValueError): validate(canonical(over)) + + def test_entry32mib_exact_and_one_over_and_nonpositive_descriptors(self): + # Catches invented 16MiB per-asset limits and entry aggregate overflow. + validate = self.api("validate_native_registry") + expected = registry([entry(lengths=(32*MIB-2, 1, 1))]) + self.assertEqual(validate(canonical(expected)), expected) + for length in [32*MIB-1, 32*MIB+1, 0, -1, True, "1", 1.0]: + bad = registry([entry(lengths=(length, 1, 1))]) if type(length) is int else registry() + if type(length) is not int: bad["entries"][0]["files"][0]["bytes"] = length + with self.subTest(length=length), self.assertRaises(ValueError): validate(canonical(bad)) + + def test_registry16mib_exact_and_one_over(self): + # Catches registry raw-byte off-by-one independently of archive capacity. + validate = self.api("validate_native_registry") + expected = registry() + # Positive workflow_id is a string without a specified digit-count cap. + raw = canonical(expected) + expected["entries"][0]["selection"]["workflow_id"] += "0"*(16*MIB-len(raw)) + exact = canonical(expected) + self.assertEqual(len(exact), 16777216) + self.assertEqual(validate(exact), expected) + expected["entries"][0]["selection"]["workflow_id"] += "0" + over = canonical(expected) + self.assertEqual(len(over), 16777217) + with self.assertRaises(ValueError): validate(over) + + + def test_progress_none_and_same_producer_need_no_comparison(self): + # Catches rejecting first native publication or charging fake repeated versions. + progress = self.api("validate_native_progress") + current = progress_current() + self.assertIsNone(progress(None, current, comparisons={})) + previous = publication_candidate() + before = copy.deepcopy((previous, current)) + self.assertIsNone(progress(previous, current, comparisons={})) + self.assertEqual((previous, current), before) + + def test_progress_v1_has_build_obligation_without_prior_native_producer(self): + # Catches incorrectly skipping build ancestry on a v1-to-v2 migration. + progress = self.api("validate_native_progress") + previous = publication_candidate(1, "e"*40) + current = progress_current() + proof = native_comparison("e"*40, BUILD, head_tree=TREE) + self.assertIsNone(progress(previous, current, comparisons={"build": proof})) + with self.assertRaises(ValueError): progress(previous, current, comparisons={}) + self.assertIsNone(progress(publication_candidate(1), current, comparisons={})) + + def test_progress_older_original_run_is_refused_and_numeric_ties_ordered(self): + # Catches timestamp-only, lexical-ID, or older-success producer fallback. + progress = self.api("validate_native_progress") + previous = publication_candidate(binding=dict(BINDING, run_id="100", artifact_name="research-architecture-100-2")) + for current in [progress_current(prefix="f"*64, run_id="999", created_at="2026-10-25T11:59:59Z"), + progress_current(prefix="f"*64, run_id="99")]: + with self.subTest(run=current["producer"]["binding"]["run_id"]), self.assertRaises(ValueError): + progress(previous, current, comparisons={}) + self.assertIsNone(progress(previous, progress_current(prefix="f"*64, run_id="101"), comparisons={})) + self.assertIsNone(progress(previous, progress_current(prefix="f"*64, run_id="9", created_at="2026-10-25T12:00:01Z"), comparisons={})) + + def test_progress_same_original_run_attempt_must_not_decrease(self): + # Catches same-run attempt rewind, including lexical 10-vs-2 ordering. + progress = self.api("validate_native_progress") + binding = dict(BINDING, run_attempt="10", artifact_name="research-architecture-100-10") + previous = publication_candidate(binding=binding) + with self.assertRaises(ValueError): + progress(previous, progress_current(prefix="f"*64, attempt="2"), comparisons={}) + self.assertIsNone(progress(previous, progress_current(prefix="f"*64, attempt="10"), comparisons={})) + self.assertIsNone(progress(previous, progress_current(prefix="f"*64, attempt="11"), comparisons={})) + + def test_progress_same_prefix_preserves_entire_binding_and_selection(self): + # Catches allowing retained-prefix identity drift merely because P is unchanged. + progress = self.api("validate_native_progress") + previous = publication_candidate() + for section, field, value in [("binding", "artifact_id", "301"), + ("binding", "receipt_sha256", "f"*64), + ("selection", "workflow_id", "401"), + ("selection", "producer_tree", "e"*40), + ("selection", "original_created_at", "2026-10-25T12:00:01Z")]: + current = progress_current(); current["producer"][section][field] = value + with self.subTest(section=section, field=field), self.assertRaises(ValueError): + progress(previous, current, comparisons={}) + + def test_progress_different_producer_requires_complete_ancestry_but_not_equal_trees(self): + # Catches assuming source-tree equality is needed for forward progress. + progress = self.api("validate_native_progress") + previous = publication_candidate() + current = progress_current(producer="e"*40, prefix="f"*64, producer_tree="d"*40) + proof = native_comparison(PRODUCER, "e"*40, base_tree=TREE, head_tree="d"*40) + self.assertNotEqual(proof["base_commit"]["commit"]["tree"]["sha"], proof["commits"][-1]["commit"]["tree"]["sha"]) + self.assertIsNone(progress(previous, current, comparisons={"producer": proof})) + with self.assertRaises(ValueError): progress(previous, current, comparisons={}) + + def test_progress_different_build_has_independent_ancestry_obligation(self): + # Catches accepting a rewound build because producer progress succeeds. + progress = self.api("validate_native_progress") + previous = publication_candidate() + current = progress_current(build_commit="e"*40, build_tree="d"*40) + proof = native_comparison(BUILD, "e"*40) + self.assertIsNone(progress(previous, current, comparisons={"build": proof})) + with self.assertRaises(ValueError): progress(previous, current, comparisons={}) + rewind = native_comparison(BUILD, "e"*40); rewind.update(status="behind", ahead_by=0, behind_by=1) + with self.assertRaises(ValueError): progress(previous, current, comparisons={"build": rewind}) + + def test_progress_identical_build_and_producer_pair_reuses_same_full_response(self): + # Catches a false independent-read requirement for an identical immutable pair. + progress = self.api("validate_native_progress") + previous = publication_candidate(build_commit=PRODUCER) + current = progress_current(build_commit="e"*40, producer="e"*40, prefix="f"*64, + build_tree="d"*40, producer_tree="d"*40) + proof = native_comparison(PRODUCER, "e"*40) + self.assertIsNone(progress(previous, current, comparisons={"build": proof, "producer": proof})) + + def test_progress_bool_partial_forged_or_truncated_comparison_never_qualifies(self): + # Catches caller bool verdicts, missing immutable endpoints and truncated commit lists. + progress = self.api("validate_native_progress") + previous = publication_candidate() + current = progress_current(producer="e"*40, prefix="f"*64, producer_tree="d"*40) + original = native_comparison(PRODUCER, "e"*40) + invalid = [True, False, None, {"status": "ahead"}, {"qualified": True}] + mutations = [lambda c: c.update(status="diverged"), lambda c: c.update(behind_by=1), + lambda c: c.update(ahead_by=True), lambda c: c.update(total_commits=True), + lambda c: c.update(behind_by=False), lambda c: c.update(ahead_by=0), + lambda c: c.update(total_commits=2), lambda c: c.update(commits=[]), + lambda c: c["base_commit"].update(sha="f"*40), + lambda c: c["merge_base_commit"].update(sha="f"*40), + lambda c: c["commits"][-1].update(sha="f"*40), + lambda c: c["commits"][-1]["commit"]["tree"].update(sha="D"*40), + lambda c: c["commits"][-1]["commit"]["tree"].update(sha="f"*40), + lambda c: c["base_commit"]["commit"]["tree"].update(sha="f"*40), + lambda c: c["commits"][-1].update(parents=[{"sha": "not-a-sha"}]), + lambda c: c["commits"][-1].pop("commit"), + lambda c: c["commits"][-1].pop("parents")] + for mutate in mutations: + bad = copy.deepcopy(original); mutate(bad); invalid.append(bad) + duplicate = native_comparison(PRODUCER, "e"*40, 2) + duplicate["commits"][0] = copy.deepcopy(duplicate["commits"][1]); invalid.append(duplicate) + truncated = native_comparison(PRODUCER, "e"*40, 2) + truncated["commits"].pop(0); invalid.append(truncated) + for index, bad in enumerate(invalid): + with self.subTest(case=index), self.assertRaises(ValueError): + progress(previous, current, comparisons={"producer": bad}) + + def test_progress250_exact_and251_refused_without_reset_or_fallback(self): + # Catches accepting overbound proof or mutating base identity after refusal. + progress = self.api("validate_native_progress") + previous = publication_candidate() + current = progress_current(producer="e"*40, prefix="f"*64, producer_tree="d"*40) + exact = native_comparison(PRODUCER, "e"*40, 250) + self.assertIsNone(progress(previous, current, comparisons={"producer": exact})) + before = copy.deepcopy((previous, current)) + over = native_comparison(PRODUCER, "e"*40, 251) + with self.assertRaises(ValueError): progress(previous, current, comparisons={"producer": over}) + self.assertEqual((previous, current), before) + with self.assertRaises(ValueError): progress(previous, current, comparisons={}) + self.assertEqual((previous, current), before) + + def test_progress_current_and_previous_identity_types_are_not_coerced(self): + # Catches bypassing identity/schema admission on first publication or equal commits. + progress = self.api("validate_native_progress") + for section, field, value in [("build", "repository", "other/repo"), + ("build", "commit", "B"*40), ("build", "tree", True), + ("build", "extra", True)]: + current = progress_current(); current[section][field] = value + with self.subTest(section=section, field=field), self.assertRaises(ValueError): + progress(None, current, comparisons={}) + for value in [True, [], {"source": {"commit": BUILD}}]: + with self.subTest(previous=value), self.assertRaises(ValueError): + progress(value, progress_current(), comparisons={}) + previous = publication_candidate(); previous["source"]["commit"] = True + with self.assertRaises(ValueError): progress(previous, progress_current(), comparisons={}) + current = progress_current(); current["producer"]["binding"]["run_id"] = 100 + with self.assertRaises(ValueError): progress(None, current, comparisons={}) + + + + def test_four_pure_apis_have_no_filesystem_network_or_subprocess_effects(self): + # Catches accidental I/O/project orchestration in these pure metadata APIs. + validate = self.api("validate_native_registry") + admit = self.api("admit_current_native_bundle") + build = self.api("build_native_registry") + progress = self.api("validate_native_progress") + files, binding, selection, manifest = bundle_fixture() + current_entry = {"binding": binding, "selection": selection, + "files": [descriptor(path, files[path]) for path in sorted(files) + if path.startswith("native-graph/")]} + expected_raw = canonical(registry([current_entry])) + failure = AssertionError("pure Task1 API attempted an external effect") + with patch("builtins.open", side_effect=failure), patch("io.open", side_effect=failure), \ + patch("os.open", side_effect=failure), patch("socket.socket", side_effect=failure), \ + patch("urllib.request.urlopen", side_effect=failure), patch("subprocess.run", side_effect=failure): + self.assertEqual(validate(expected_raw), registry([current_entry])) + self.assertEqual(self.admit(admit, files, binding, selection), manifest) + self.assertEqual(self.build(build, None, files, binding, selection), expected_raw) + self.assertIsNone(progress(None, progress_current(), comparisons={})) + + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/current_site_native_registry.py b/tools/current_site_native_registry.py new file mode 100644 index 000000000..ac8beae55 --- /dev/null +++ b/tools/current_site_native_registry.py @@ -0,0 +1,642 @@ +"""Pure native registry construction and declared current-bundle validation. + +These four APIs join already admitted bytes and immutable metadata. They do +not acquire artifacts, rerun archive admission, establish custody, publish, +restore files, or confer scientific authority. Every refusal is ValueError; +no validation relies on assertions or mutable publication state. +""" +from __future__ import annotations + +from datetime import datetime +import hashlib +import json +import math +import re + + +_MIB = 1024 * 1024 +_REPOSITORY = "d6g8k5htny-coder/main" +_ORIGIN = "https://d6g8k5htny-coder.github.io/main/site/" +_MAX_SEQUENCE = 2**63 - 1 +_MAX_ARCHIVES = 8192 +_MAX_IMMUTABLE = 512 * _MIB +_MAX_REGISTRY = 16 * _MIB +_MAX_ASSET = 32 * _MIB +_MAX_BUNDLE = 32 * _MIB +_MAX_PAGE = 4 * _MIB +_MAX_MANIFEST = 65536 +_HEX40 = re.compile(r"[0-9a-f]{40}") +_HEX64 = re.compile(r"[0-9a-f]{64}") +_DECIMAL = re.compile(r"[1-9][0-9]*") +_UTC = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z") +_TEST_ID = re.compile(r"test_architecture_[A-Za-z0-9_]+\.[A-Za-z_][A-Za-z0-9_]*\.test[A-Za-z0-9_]*") +_BINDING_FIELDS = frozenset({"checked_commit", "repository", "run_id", "run_attempt", + "receipt_sha256", "artifact_id", "artifact_sha256", "artifact_name"}) +_SELECTION_FIELDS = frozenset({"workflow_id", "workflow_path", "event", + "original_created_at", "producer_tree"}) +_SCIENCE_FIELDS = frozenset({"schema_version", "scientific_effect", "scientific_status_authority"}) +_REGISTRY_FIELDS = _SCIENCE_FIELDS | {"custody", "origin", "sequence", "previous_registry_sha256", + "current_artifact_sha256", "entries", "immutable_bytes", "capacity"} +_ASSET_NAMES = ("architecture-run-receipt.json", "graph.json", "projection.json") +_GRAPH_SOURCE = { + "repository": "d6g8k5htny-coder/Math-", + "commit": "7858329974e28be79f29b22644370084ff43da4f", + "captured_at": "2026-10-03T15:07:03Z", + "graph_sha256": "8822e9618678321a342d69cd0b8ae6552de1b5d578c331de5072b2892ee9dd09", + "gate_sha256": "a78f3e25f3b0cfe113e618a4c31a7a25d7f22af638c46dec1ecba221fa333ac8", +} +_MEMBER_LIMITS = { + "ARTIFACT_SHA256SUMS": 16384, "checked-commit.txt": 128, + "run-id.txt": 128, "run-attempt.txt": 128, "container-exit-status.txt": 128, + "exporter.stdout": 65536, "exporter.stderr": 65536, + "runtime-image.json": _MIB, "generated/graph.json": 4 * _MIB, + "architecture-run-receipt.json": 4 * _MIB, "console.log": 4 * _MIB, + "tests-normal.log": 16 * _MIB, "tests-optimized.log": 16 * _MIB, +} +_EVIDENCE_MEMBERS = frozenset({"generated/graph.json", "checked-commit.txt", "run-id.txt", + "run-attempt.txt", "container-exit-status.txt", "tests-normal.log", + "tests-optimized.log", "runtime-image.json"}) +_IMAGE_DIGEST = "python@sha256:83f339c1be6340ae1096010fdccf6552ac932d8f410d45d206014916bdf37e48" +_READBACK_SCOPE = "legacy3+current-native5+registry;retained-native-assets-not-observed" +_CANDIDATE_FIELDS = frozenset({"schema_version", "scientific_effect", "repository", + "receipt_branch", "source", "run", "artifact", "manifest", "metadata", "payload", "snapshot_ids"}) + + +def _require(condition, message="native metadata refused"): + if not condition: + raise ValueError(message) + + +def _closed(value, fields): + _require(type(value) is dict and all(type(key) is str for key in value) + and set(value) == set(fields), "closed native schema refused") + return value + + +def _integer(value, lower, upper): + _require(type(value) is int and lower <= value <= upper, "native integer refused") + return value + + +def _pattern(value, pattern): + _require(type(value) is str and pattern.fullmatch(value) is not None, "native identity refused") + return value + + +def _sha(raw): + return hashlib.sha256(raw).hexdigest() + + +def _canonical(value): + return (json.dumps(value, sort_keys=True, ensure_ascii=True, + separators=(",", ":"), allow_nan=False) + "\n").encode("ascii") + + +def _same(left, right): + # Python equality alone equates True with 1, and 1.0 with 1. + return _canonical(left) == _canonical(right) + + +def _json_types(value): + """Reject non-JSON Python values, nonfinite floats and lone surrogates.""" + try: + kind = type(value) + if kind is dict: + for key, child in value.items(): + _require(type(key) is str) + key.encode("utf-8") + _json_types(child) + elif kind is list: + for child in value: + _json_types(child) + elif kind is str: + value.encode("utf-8") + elif kind is float: + _require(math.isfinite(value)) + else: + _require(value is None or kind in (bool, int)) + except (UnicodeError, RecursionError, OverflowError) as error: + raise ValueError("native JSON value refused") from error + + +def _parse(raw, maximum): + """Read one strict UTF-8 JSON object; input whitespace need not be canonical.""" + _require(type(raw) is bytes and 0 < len(raw) <= maximum, "native JSON byte capacity refused") + + def unique(pairs): + result = {} + for key, value in pairs: + _require(key not in result, "duplicate native JSON key refused") + result[key] = value + return result + + def no_constant(value): + raise ValueError("nonfinite native JSON refused") + + try: + result = json.loads(raw.decode("utf-8"), object_pairs_hook=unique, + parse_constant=no_constant) + _require(type(result) is dict, "native JSON object required") + _json_types(result) + return result + except (UnicodeError, RecursionError, OverflowError) as error: + raise ValueError("native JSON refused") from error + + +def _science(document): + _integer(document["schema_version"], 1, 1) + _require(document["scientific_effect"] == "NONE" + and type(document["scientific_effect"]) is str + and document["scientific_status_authority"] is False) + + +def _custody(document): + _science(document) + _require(document["custody"] == "unknown" and type(document["custody"]) is str) + + +def _binding(value): + _closed(value, _BINDING_FIELDS) + _require(all(type(item) is str for item in value.values())) + _require(sum(len(item) for item in value.values()) <= 16 * _MIB) + for name, pattern in (("checked_commit", _HEX40), ("run_id", _DECIMAL), + ("run_attempt", _DECIMAL), ("receipt_sha256", _HEX64), + ("artifact_id", _DECIMAL), ("artifact_sha256", _HEX64)): + _pattern(value[name], pattern) + _require(value["repository"] == _REPOSITORY) + _require(value["artifact_name"] == "research-architecture-" + value["run_id"] + "-" + value["run_attempt"]) + return value + + +def _selection(value): + _closed(value, _SELECTION_FIELDS) + _require(all(type(item) is str for item in value.values())) + # This identity grammar has no digit-count cap. No integer conversion is + # needed for workflow_id, including the exact 16-MiB registry boundary. + _pattern(value["workflow_id"], _DECIMAL) + _pattern(value["producer_tree"], _HEX40) + _require(value["workflow_path"] == ".github/workflows/workspace-landing.yml" and value["event"] == "push") + _pattern(value["original_created_at"], _UTC) + try: + stamp = value["original_created_at"] + datetime(int(stamp[0:4]), int(stamp[5:7]), int(stamp[8:10]), + int(stamp[11:13]), int(stamp[14:16]), int(stamp[17:19])) + except (ValueError, OverflowError) as error: + raise ValueError("native producer UTC refused") from error + return value + + +def _descriptor(value, path, maximum, *, minimum=1): + _closed(value, {"path", "bytes", "sha256"}) + _require(type(value["path"]) is str and value["path"] == path, "native descriptor path refused") + _integer(value["bytes"], minimum, maximum) + _pattern(value["sha256"], _HEX64) + return value + + +def _describe(path, raw): + return {"path": path, "bytes": len(raw), "sha256": _sha(raw)} + + +def _immutable_paths(binding): + prefix = "native-graph/" + binding["artifact_sha256"] + "/" + return [prefix + name for name in _ASSET_NAMES] + + +def _entry(value): + _closed(value, {"binding", "selection", "files"}) + _binding(value["binding"]) + _selection(value["selection"]) + paths = _immutable_paths(value["binding"]) + _require(type(value["files"]) is list and len(value["files"]) == 3) + total = sum(_descriptor(item, path, _MAX_ASSET)["bytes"] + for item, path in zip(value["files"], paths)) + _require(total <= _MAX_ASSET, "native entry capacity refused") + return total + + +def validate_native_registry(raw: bytes) -> dict: + """Validate exact registry v1 identities, sorted inventory and capacity joins.""" + document = _closed(_parse(raw, _MAX_REGISTRY), _REGISTRY_FIELDS) + _custody(document) + _require(document["origin"] == _ORIGIN and type(document["origin"]) is str) + _integer(document["sequence"], 1, _MAX_SEQUENCE) + if document["previous_registry_sha256"] is not None: + _pattern(document["previous_registry_sha256"], _HEX64) + _pattern(document["current_artifact_sha256"], _HEX64) + entries = document["entries"] + _require(type(entries) is list and 1 <= len(entries) <= _MAX_ARCHIVES, "native archive count refused") + total = 0 + previous_prefix = None + found_current = False + for entry in entries: + total += _entry(entry) + prefix = entry["binding"]["artifact_sha256"] + _require(previous_prefix is None or previous_prefix < prefix, "native prefixes must be unique and sorted") + previous_prefix = prefix + found_current = found_current or prefix == document["current_artifact_sha256"] + _require(total <= _MAX_IMMUTABLE, "native immutable capacity refused") + _require(found_current, "native current pointer unresolved") + _integer(document["immutable_bytes"], 1, _MAX_IMMUTABLE) + _require(document["immutable_bytes"] == total) + capacity = _closed(document["capacity"], {"archives", "maximum_archives", "bytes", "maximum_bytes"}) + for key, expected in (("archives", len(entries)), ("maximum_archives", _MAX_ARCHIVES), + ("bytes", total), ("maximum_bytes", _MAX_IMMUTABLE)): + _integer(capacity[key], expected, expected) + _require(len(_canonical(document)) <= _MAX_REGISTRY, "canonical native registry capacity refused") + return document + + +def _context(value): + _closed(value, {"repository", "ref", "commit", "run_id", "run_attempt", "event"}) + _require(value["repository"] == _REPOSITORY and type(value["repository"]) is str + and value["ref"] == "refs/heads/main" and type(value["ref"]) is str) + _pattern(value["commit"], _HEX40) + _integer(value["run_id"], 1, _MAX_SEQUENCE) + _integer(value["run_attempt"], 1, 100000) + _require(type(value["event"]) is str and value["event"] in ("push", "schedule", "workflow_dispatch")) + + +def _graph_source(value): + _closed(value, _GRAPH_SOURCE) + _require(_same(value, _GRAPH_SOURCE), "fixed native graph source refused") + + +def _producer_receipt(value, binding, graph, members): + """Check declared receipt closure, without interpreting unavailable log bodies.""" + _closed(value, _SCIENCE_FIELDS | {"checked_commit", "repository", "run_id", "run_attempt", + "runtime_image", "test_modes", "graph_source", "evidence_file_sha256"}) + _science(value) + for key in ("checked_commit", "repository", "run_id", "run_attempt"): + _require(type(value[key]) is str and value[key] == binding[key]) + _graph_source(value["graph_source"]) + _require(_same(value["graph_source"], graph["source"])) + image = _closed(value["runtime_image"], {"id", "repo_digest"}) + _pattern(image["id"], re.compile(r"sha256:[0-9a-f]{64}")) + _require(type(image["repo_digest"]) is str and image["repo_digest"] == _IMAGE_DIGEST) + evidence = _closed(value["evidence_file_sha256"], _EVIDENCE_MEMBERS) + for name, digest in evidence.items(): + _pattern(digest, _HEX64) + _require(digest == members[name]["sha256"], "native evidence descriptor join refused") + modes = value["test_modes"] + _require(type(modes) is list and len(modes) == 2) + for row, mode in zip(modes, ("normal", "optimized")): + _closed(row, {"mode", "test_count", "test_ids"}) + _require(type(row["mode"]) is str and row["mode"] == mode) + ids = row["test_ids"] + _require(type(ids) is list and 1 <= len(ids) <= 4096) + for identity in ids: + _pattern(identity, _TEST_ID) + _require(ids == sorted(set(ids))) + _integer(row["test_count"], len(ids), len(ids)) + _require(modes[0]["test_ids"] == modes[1]["test_ids"]) + + +def _projection(document, binding, graph, receipt, graph_raw, receipt_raw): + """Join the prior admission's declared inventory to the exact public members.""" + _closed(document, _SCIENCE_FIELDS | {"custody", "graph", "admission"}) + _custody(document) + projected_graph = _closed(document["graph"], {"identity", "data"}) + identity = _closed(projected_graph["identity"], {"kind", "binding", "member"}) + _require(type(identity["kind"]) is str and identity["kind"] == "native-artifact-member") + _binding(identity["binding"]) + _require(_same(identity["binding"], binding) and _same(projected_graph["data"], graph)) + graph_member = _describe("generated/graph.json", graph_raw) + _descriptor(identity["member"], "generated/graph.json", _MAX_ASSET) + _require(_same(identity["member"], graph_member)) + admission = _closed(document["admission"], _SCIENCE_FIELDS | {"custody", "verification_scope", "binding", + "archive", "graph_member", "producer_receipt_sha256", "producer_receipt"}) + _custody(admission) + _require(type(admission["verification_scope"]) is str + and admission["verification_scope"] == "declared-native-pins-and-archive-content") + _binding(admission["binding"]) + _require(_same(admission["binding"], binding)) + archive = _closed(admission["archive"], {"sha256", "bytes", "members"}) + _pattern(archive["sha256"], _HEX64) + _require(archive["sha256"] == binding["artifact_sha256"]) + _integer(archive["bytes"], 22, 64 * _MIB) + rows = archive["members"] + names = sorted(_MEMBER_LIMITS) + _require(type(rows) is list and len(rows) == len(names)) + members = {} + for row, name in zip(rows, names): + _descriptor(row, name, _MEMBER_LIMITS[name], minimum=0) + members[name] = row + _require(sum(row["bytes"] for row in rows) <= 64 * _MIB) + _require(_same(members["generated/graph.json"], graph_member)) + _require(_same(members["architecture-run-receipt.json"], _describe("architecture-run-receipt.json", receipt_raw))) + _descriptor(admission["graph_member"], "generated/graph.json", _MAX_ASSET) + _require(_same(admission["graph_member"], graph_member)) + _pattern(admission["producer_receipt_sha256"], _HEX64) + _require(admission["producer_receipt_sha256"] == binding["receipt_sha256"] == _sha(receipt_raw)) + _require(_same(admission["producer_receipt"], receipt)) + _producer_receipt(receipt, binding, graph, members) + # The text members whose exact bodies are derivable from admitted pins can + # be checked without recovering logs, raw archives, or external artifacts. + for name, raw in (("checked-commit.txt", (binding["checked_commit"] + "\n").encode("ascii")), + ("run-id.txt", (binding["run_id"] + "\n").encode("ascii")), + ("run-attempt.txt", (binding["run_attempt"] + "\n").encode("ascii")), + ("container-exit-status.txt", b"0\n"), ("exporter.stderr", b""), + ("exporter.stdout", b"/output/generated/graph.json\n")): + _require(_same(members[name], _describe(name, raw))) + + +def admit_current_native_bundle(files: dict[str, bytes], *, context: dict, + current_payload_bytes: bytes, binding: dict, selection: dict) -> dict: + """Return the validated manifest directly after exact five-buffer closure. + + This consumes a previously admitted bundle. Descriptor metadata can be + checked here; original ZIP geometry, logs and source admission cannot be + re-established from these five public files and are not claimed. + """ + _context(context) + _binding(binding) + _selection(selection) + _require(type(current_payload_bytes) is bytes and len(current_payload_bytes) <= 16 * _MIB) + paths = _immutable_paths(binding) + expected_paths = set(paths) | {"proof-tree.html", "current-build-manifest.json"} + _require(type(files) is dict and all(type(path) is str for path in files) + and set(files) == expected_paths, "exact five-output native bundle required") + for path, raw in files.items(): + limit = _MAX_PAGE if path == "proof-tree.html" else _MAX_MANIFEST if path == "current-build-manifest.json" else _MAX_ASSET + _require(type(raw) is bytes and 0 < len(raw) <= limit, "native bundle buffer refused") + _require(sum(len(raw) for raw in files.values()) <= _MAX_BUNDLE, "native bundle aggregate refused") + manifest = _closed(_parse(files["current-build-manifest.json"], _MAX_MANIFEST), + _SCIENCE_FIELDS | {"custody", "verification_scope", "build", "native", "files"}) + _custody(manifest) + _require(type(manifest["verification_scope"]) is str + and manifest["verification_scope"] == "declared-build-context-and-admitted-native-archive") + build = _closed(manifest["build"], {"repository", "commit", "current_payload"}) + _require(type(build["repository"]) is str and build["repository"] == context["repository"]) + _pattern(build["commit"], _HEX40) + _require(build["commit"] == context["commit"]) + _descriptor(build["current_payload"], "current.json", 16 * _MIB, minimum=0) + _require(_same(build["current_payload"], _describe("current.json", current_payload_bytes))) + native = _closed(manifest["native"], {"binding", "graph_source"}) + _binding(native["binding"]) + _require(_same(native["binding"], binding)) + _graph_source(native["graph_source"]) + inventory = manifest["files"] + inventory_paths = sorted(expected_paths - {"current-build-manifest.json"}) + _require(type(inventory) is list and len(inventory) == 4) + for item, path in zip(inventory, inventory_paths): + _descriptor(item, path, _MAX_PAGE if path == "proof-tree.html" else _MAX_ASSET) + _require(_same(item, _describe(path, files[path])), "native manifest descriptor join refused") + prefix = "native-graph/" + binding["artifact_sha256"] + "/" + graph_raw = files[prefix + "graph.json"] + receipt_raw = files[prefix + "architecture-run-receipt.json"] + graph = _closed(_parse(graph_raw, _MAX_ASSET), _SCIENCE_FIELDS | {"source", "nodes", "edges", "dimensions"}) + _science(graph) + _graph_source(graph["source"]) + _require(type(graph["nodes"]) is dict and type(graph["edges"]) is list and type(graph["dimensions"]) is dict) + _require(_same(graph["source"], native["graph_source"])) + receipt = _parse(receipt_raw, _MAX_ASSET) + projection = _parse(files[prefix + "projection.json"], _MAX_ASSET) + _projection(projection, binding, graph, receipt, graph_raw, receipt_raw) + return manifest + + +def _base(value): + _closed(value, {"receipt_commit", "public_receipt_commit", "registry_sha256", "sequence"}) + for field in ("receipt_commit", "public_receipt_commit"): + if value[field] is not None: + _pattern(value[field], _HEX40) + if value["registry_sha256"] is not None: + _pattern(value["registry_sha256"], _HEX64) + _integer(value["sequence"], 0, _MAX_SEQUENCE - 1) + _require((value["registry_sha256"] is None) == (value["sequence"] == 0), "native base lineage refused") + return value + + +def build_native_registry(previous_raw: bytes | None, current_bundle: dict[str, bytes], *, + base: dict, context: dict, current_payload_bytes: bytes, binding: dict, selection: dict) -> bytes: + """Conserve the complete prior registry and add at most one immutable prefix. + + Sequence and previous hash always derive from the supplied original base; + repeated renders with that same base produce identical bytes. + """ + _base(base) + if previous_raw is None: + _require(base["registry_sha256"] is None and base["sequence"] == 0) + previous = None + else: + previous = validate_native_registry(previous_raw) + _require(base["registry_sha256"] == _sha(previous_raw) + and base["sequence"] == previous["sequence"], "native prior registry/base mismatch") + admit_current_native_bundle(current_bundle, context=context, current_payload_bytes=current_payload_bytes, + binding=binding, selection=selection) + entry = {"binding": dict(binding), "selection": dict(selection), + "files": [_describe(path, current_bundle[path]) for path in _immutable_paths(binding)]} + entries = [] if previous is None else list(previous["entries"]) + prefix = binding["artifact_sha256"] + found = next((row for row in entries if row["binding"]["artifact_sha256"] == prefix), None) + if found is not None: + _require(_same(found, entry), "retained native prefix identity conflict") + else: + entries.append(entry) + entries.sort(key=lambda row: row["binding"]["artifact_sha256"]) + total = sum(item["bytes"] for row in entries for item in row["files"]) + _require(len(entries) <= _MAX_ARCHIVES and total <= _MAX_IMMUTABLE, "native union capacity refused") + result = {"schema_version": 1, "scientific_effect": "NONE", "scientific_status_authority": False, + "custody": "unknown", "origin": _ORIGIN, "sequence": base["sequence"] + 1, + "previous_registry_sha256": base["registry_sha256"], "current_artifact_sha256": prefix, + "entries": entries, "immutable_bytes": total, "capacity": {"archives": len(entries), + "maximum_archives": _MAX_ARCHIVES, "bytes": total, "maximum_bytes": _MAX_IMMUTABLE}} + raw = _canonical(result) + validate_native_registry(raw) + return raw + + +def _candidate(value): + """Admit the full effective public candidate, excluding receipt phase fields.""" + _require(type(value) is dict) + version = value.get("schema_version") + _integer(version, 1, 2) + _closed(value, _CANDIDATE_FIELDS | ({"native"} if version == 2 else set())) + _require(type(value["scientific_effect"]) is str and value["scientific_effect"] == "NONE" + and type(value["repository"]) is str and value["repository"] == _REPOSITORY + and type(value["receipt_branch"]) is str and value["receipt_branch"] == "codex/site-publication-receipts") + source = _closed(value["source"], {"commit", "ref", "workflow"}) + _pattern(source["commit"], _HEX40) + _require(type(source["ref"]) is str and source["ref"] == "refs/heads/main" + and type(source["workflow"]) is str and source["workflow"] == ".github/workflows/current-site.yml") + run = _closed(value["run"], {"id", "attempt"}) + _integer(run["id"], 1, _MAX_SEQUENCE) + _integer(run["attempt"], 1, 100000) + artifact = _closed(value["artifact"], {"sha256", "files", "bytes"}) + _pattern(artifact["sha256"], _HEX64) + _integer(artifact["files"], 1, 1000000) + _integer(artifact["bytes"], 1, 1024**3) + for name in ("manifest", "metadata", "payload"): + row = _closed(value[name], {"bytes", "sha256"}) + _integer(row["bytes"], 1, 16 * _MIB) + _pattern(row["sha256"], _HEX64) + snapshots = value["snapshot_ids"] + _require(type(snapshots) is list and 1 <= len(snapshots) <= 512) + for item in snapshots: + _pattern(item, _HEX64) + _require(snapshots == sorted(set(snapshots))) + if version == 2: + native = _closed(value["native"], {"base", "sequence", "binding", "selection", + "registry", "build_manifest", "readback_scope"}) + _base(native["base"]) + _integer(native["sequence"], 1, _MAX_SEQUENCE) + _require(native["sequence"] == native["base"]["sequence"] + 1) + _binding(native["binding"]) + _selection(native["selection"]) + _descriptor(native["registry"], "native-graph/index.json", _MAX_REGISTRY) + _descriptor(native["build_manifest"], "current-build-manifest.json", _MAX_MANIFEST) + _require(type(native["readback_scope"]) is str and native["readback_scope"] == _READBACK_SCOPE) + return value + + +def _commit_identity(value): + """Validate material native commit identity; ordinary API extra fields remain.""" + _require(type(value) is dict and {"sha", "commit", "parents"} <= set(value)) + sha = _pattern(value["sha"], _HEX40) + commit = value["commit"] + _require(type(commit) is dict and type(commit.get("tree")) is dict) + tree = _pattern(commit["tree"].get("sha"), _HEX40) + parents = value["parents"] + _require(type(parents) is list) + identities = [] + for parent in parents: + _require(type(parent) is dict) + identities.append(_pattern(parent.get("sha"), _HEX40)) + _require(len(identities) == len(set(identities)) and sha not in identities) + return sha, tree, tuple(identities) + + +def _comparison_proof(value, old, new, old_tree, new_tree, known_trees, known_commits): + """Require a complete bounded forward native response with joined endpoints.""" + _require(type(value) is dict, "PROGRESS_UNKNOWN") + _json_types(value) + required = {"status", "ahead_by", "behind_by", "total_commits", "base_commit", "merge_base_commit", "commits"} + _require(required <= set(value), "PROGRESS_UNKNOWN") + _require(type(value["status"]) is str and value["status"] == "ahead", "PROGRESS_UNKNOWN") + count = value["ahead_by"] + _require(type(count) is int and count > 0, "PROGRESS_UNKNOWN") + _require(count <= 250, "PROGRESS_BOUND") + _integer(value["behind_by"], 0, 0) + _integer(value["total_commits"], count, count) + rows = value["commits"] + _require(type(rows) is list and len(rows) == count, "PROGRESS_BOUND") + base = _commit_identity(value["base_commit"]) + merge = _commit_identity(value["merge_base_commit"]) + _require(base == merge and base[0] == old, "PROGRESS_UNKNOWN") + if old_tree is not None: + _require(base[1] == old_tree, "PROGRESS_UNKNOWN") + for identity in (base, merge): + _join_commit(identity, known_trees, known_commits) + identities = {} + for row in rows: + identity = _commit_identity(row) + _require(identity[0] != old and identity[0] not in identities, "PROGRESS_UNKNOWN") + identities[identity[0]] = identity + _join_commit(identity, known_trees, known_commits) + _require(new in identities and identities[new][1] == new_tree, "PROGRESS_UNKNOWN") + # A complete directed parent chain must connect the known endpoints. Merge + # parents outside this comparison are permitted; a fabricated disconnected + # list cannot establish progress merely by claiming status=ahead. + pending = [(new, False)] + visiting = set() + seen = set() + found_base = False + while pending: + sha, finished = pending.pop() + if sha == old: + found_base = True + continue + if sha not in identities or sha in seen: + continue + if finished: + visiting.remove(sha) + seen.add(sha) + continue + _require(sha not in visiting, "PROGRESS_UNKNOWN") + visiting.add(sha) + pending.append((sha, True)) + pending.extend((parent, False) for parent in identities[sha][2]) + _require(found_base and len(seen) == len(identities), "PROGRESS_UNKNOWN") + + +def _join_commit(identity, known_trees, known_commits): + sha, tree, parents = identity + _require(sha not in known_trees or known_trees[sha] == tree, "PROGRESS_UNKNOWN") + _require(sha not in known_commits or known_commits[sha] == (tree, parents), "PROGRESS_UNKNOWN") + known_trees[sha] = tree + known_commits[sha] = tree, parents + + +def _comparison(value, old, new, old_tree, new_tree, known_trees, known_commits): + try: + _comparison_proof(value, old, new, old_tree, new_tree, known_trees, known_commits) + except ValueError as error: + reason = "PROGRESS_BOUND" if str(error) == "PROGRESS_BOUND" else "PROGRESS_UNKNOWN" + raise ValueError(reason) from error + + +def _decimal_order(value): + # Positive decimal grammar forbids leading zeros. Length and lexical order + # therefore equal numeric ordering without bounded Python int conversion. + return len(value), value + + +def validate_native_progress(previous: dict | None, current: dict, *, comparisons: dict) -> None: + """Refuse build/producer rewind or unknown ancestry without mutating inputs.""" + _closed(current, {"build", "producer"}) + build = _closed(current["build"], {"repository", "commit", "tree"}) + _require(type(build["repository"]) is str and build["repository"] == _REPOSITORY) + _pattern(build["commit"], _HEX40) + _pattern(build["tree"], _HEX40) + producer = _closed(current["producer"], {"binding", "selection"}) + binding = _binding(producer["binding"]) + selection = _selection(producer["selection"]) + known_trees = {build["commit"]: build["tree"]} + _require(binding["checked_commit"] not in known_trees + or known_trees[binding["checked_commit"]] == selection["producer_tree"], "native endpoint tree conflict") + known_trees[binding["checked_commit"]] = selection["producer_tree"] + known_commits = {} + _require(type(comparisons) is dict and all(type(key) is str for key in comparisons) + and set(comparisons) <= {"build", "producer"}) + for value in comparisons.values(): + _require(type(value) is dict, "PROGRESS_UNKNOWN") + _json_types(value) + if previous is None: + return None + _candidate(previous) + old_build = previous["source"]["commit"] + old_native = previous.get("native") + old_build_tree = None + if old_native is not None: + old_producer_sha = old_native["binding"]["checked_commit"] + old_producer_tree = old_native["selection"]["producer_tree"] + _require(old_producer_sha not in known_trees or known_trees[old_producer_sha] == old_producer_tree, + "native endpoint tree conflict") + known_trees[old_producer_sha] = old_producer_tree + if old_producer_sha == old_build: + old_build_tree = old_producer_tree + if old_build != build["commit"]: + _comparison(comparisons.get("build"), old_build, build["commit"], old_build_tree, build["tree"], + known_trees, known_commits) + if old_native is None: + return None + old_binding = old_native["binding"] + old_selection = old_native["selection"] + if old_binding["artifact_sha256"] == binding["artifact_sha256"]: + _require(_same(old_binding, binding) and _same(old_selection, selection), "retained native prefix identity conflict") + old_producer = old_binding["checked_commit"] + new_producer = binding["checked_commit"] + if old_producer != new_producer: + _comparison(comparisons.get("producer"), old_producer, new_producer, + old_selection["producer_tree"], selection["producer_tree"], known_trees, known_commits) + else: + _require(old_selection["producer_tree"] == selection["producer_tree"], "native producer tree conflict") + old_order = (old_selection["original_created_at"], _decimal_order(old_binding["run_id"])) + new_order = (selection["original_created_at"], _decimal_order(binding["run_id"])) + _require(new_order >= old_order, "native producer run rewind refused") + if old_binding["run_id"] == binding["run_id"]: + _require(old_selection["original_created_at"] == selection["original_created_at"], "native original run identity conflict") + _require(_decimal_order(binding["run_attempt"]) >= _decimal_order(old_binding["run_attempt"]), + "native producer attempt rewind refused") + return None