Skip to content
pi-c Artifact [6c6393e270]
Login

Artifact [6c6393e270]

Artifact 6c6393e270239a5099e4cbf869e943cb84ea1a0770b2771b644f629eba757c8c:


#!/usr/bin/env python3
"""Local metadata gate for previously executed Fil-C fixtures and verified cleanup."""
import hashlib
import importlib.util
import json
from pathlib import Path
import sys
import tempfile

ROOT = Path(__file__).resolve().parents[2]


def hashed(path):
    path = path.resolve()
    if not path.is_relative_to(ROOT) or not path.is_file():
        raise ValueError("Evidence path is missing or outside the project")
    if path.stat().st_size > 256 * 1024 * 1024:
        raise ValueError("Evidence exceeds budget")
    return {"path": str(path.relative_to(ROOT)),
            "sha256": hashlib.sha256(path.read_bytes()).hexdigest()}


def main():
    if len(sys.argv) != 2:
        raise ValueError("usage: report.py ABSOLUTE_RUN_DIRECTORY")
    run = Path(sys.argv[1]).resolve()
    if not run.is_relative_to(ROOT / "build/qualification/remote"):
        raise ValueError("Run is outside the qualification directory")
    nonce = (run / "nonce").read_text().strip()
    if (run / "mode").read_text().strip() != "filc":
        raise ValueError("This gate requires a real Fil-C run, not a stand-in")
    spec = importlib.util.spec_from_file_location("remote_collect", Path(__file__).with_name("collect.py"))
    collector = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(collector)
    # Revalidate the immutable archive; don't merely trust a prior PASS string.
    with tempfile.TemporaryDirectory(dir=run) as temporary:
        verified = collector.collect(run / "evidence.tar", run / "snapshot/sources.sha256",
                                     Path(temporary) / "verified", nonce, "filc")
    if verified["status"] != "NOT RUN" or verified["worker_result"] != "success":
        raise ValueError("Worker did not complete successfully")
    original = json.loads((run / "evidence/collection.json").read_text())
    for artifact in original["artifacts"]:
        if hashed(Path(artifact["path"]))["sha256"] != artifact["sha256"]:
            raise ValueError("Collected artifact was changed")
    for name, marker in (("operator-teardown.log", "teardown: resources inactive and workspace absent"),
                         ("operator-reconcile.log", "reconcile: clean")):
        if marker not in (run / name).read_text().splitlines():
            raise ValueError("Missing teardown/reconciliation receipt")
    if (run / "toolchain-initial.txt").read_bytes() != (run / "toolchain-final.txt").read_bytes():
        raise ValueError("Borrowed toolchain changed")
    evidence = run / "evidence"
    version = (evidence / "run/compiler-version.txt").read_text().splitlines()[0]
    if "Fil-C 0.687" not in version:
        raise ValueError("Unexpected compiler identity")
    sources = [path for base in (run / "snapshot", run / "control")
               for path in base.rglob("*") if path.is_file()]
    sources.extend(sorted(Path(__file__).parent.glob("*.sh")))
    sources.extend(sorted(Path(__file__).parent.glob("*.py")))
    sources.append(ROOT / "Makefile")
    binaries = [evidence / "run" / name for name in ("test_qualification", "test_oracle_mutant", "replay")]
    record = {"version": 1, "status": "PASS", "scope": "qualification_fixture",
              "reason": "Real Fil-C fixture/corpus execution, archive integrity, and cleanup verified",
              "sources": [hashed(path) for path in sorted(sources)],
              "binaries": [hashed(path) for path in binaries],
              "artifacts": [hashed(run / name) for name in ("manifest", "evidence.tar",
                  "evidence/collection.json", "operator-teardown.log", "operator-reconcile.log")],
              "command": ["make", "filc-remote", "REMOTE_RUN=" + str(run)],
              "host": "hetzner-hel Linux x86_64 UID/GID1000",
              "toolchain": {"compiler": version, "version": "Fil-C 0.687",
                  "flags": (evidence / "run/test_qualification.argv").read_text().splitlines()[1:]},
              "workload_sha256": hashed(run / "snapshot/sources.sha256")["sha256"],
              "dependencies_sha256": hashed(evidence / "toolchain-before.txt")["sha256"],
              "samples": 100, "durability_policy": "not a production storage qualification",
              "failure_model": "bounded remote process and fixture checks; no host reboot test",
              "contract_ids": [], "teardown": {"status": "cleaned", "resources": []}}
    (run / "qualification.json").write_text(json.dumps(record, indent=2) + "\n")
    print("PASS: real Fil-C fixtures/corpus, hashes, and cleanup; product contracts remain NOT RUN")


if __name__ == "__main__":
    try:
        main()
    except (ValueError, OSError, KeyError, TypeError) as error:
        raise SystemExit("FAIL: " + str(error))