File size: 4,256 Bytes
fd8b1ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Cross-platform release-pinned scorer wrapper for Zero-Trust Physics packs (macOS arm64, Linux amd64, Linux arm64, HTTP).

    python3 pack_score.py REPLIES --engine ENGINE --environments F [score.py's options] [--strict-binary]
    python3 pack_score.py --run run_cheat_suite.py [its options] [--strict-binary]

Verifies that the target verifier answers MCP initialize as release 0.15.3 (matching reference.gateway_version and
engine_tag) on kernel genesis_kernel_v0.13.1, while recording engine_binary_sha256_seen and engine_binary_is_reference on
every score row. Pass --strict-binary to additionally require exact SHA-256 identity with reference.engine_binary_sha256.
All deterministic recomputation, seal, budget, and grading invariants of score.py apply unchanged.

Standard library only. Licence: Apache-2.0.
"""
import inspect
import os
import re
import runpy
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
if HERE not in sys.path:
    sys.path.insert(0, HERE)

KERNEL = "genesis_kernel_v0.13.1"
STRICT = False


def _kernel_of(replay):
    """The kernel the verifier names: its serverInfo.description is the kernel's name (0.15.2), or one line that ends
    with it ("... Cards: genesis_kernel_v0.13.1", 0.15.3)."""
    server = getattr(getattr(replay, "verifier", None), "server", None) or {}
    found = re.findall(r"genesis_kernel_v[0-9][0-9.]*[0-9]", server.get("description") or "")
    return found[-1] if found else server.get("description")


def install(strict=False):
    """Configure cross-platform release identity verification on score (idempotent). Returns the score module."""
    global STRICT
    STRICT = bool(strict)
    import score
    if getattr(score, "_pack_engine_identity", False):
        return score
    original = score.engine_problem
    original_score = score.score

    def engine_problem(env, replay):
        ref = env.get("reference") or {}
        want = ref.get("engine_binary_sha256")
        seen = getattr(replay, "binary_sha256", None)
        if STRICT or not (seen and want and seen != want):
            return original(env, replay)
        # a different file: held to the release by what it says it is, every other rule as score.py's
        saved = replay.binary_sha256
        replay.binary_sha256 = None
        try:
            problem = original(env, replay)
        finally:
            replay.binary_sha256 = saved
        if problem:
            return problem
        kernel = _kernel_of(replay)
        if kernel != KERNEL:
            return "the verifier reports kernel %s; release %s runs %s" % (kernel, replay.version, KERNEL)
        return None

    def scored(env, reply, replay, *args, **kwargs):
        row = original_score(env, reply, replay, *args, **kwargs)
        if isinstance(row, dict):
            want = (env.get("reference") or {}).get("engine_binary_sha256")
            seen = getattr(replay, "binary_sha256", None)
            row["engine_binary_sha256_seen"] = seen
            row["engine_binary_is_reference"] = None if not seen else (seen == want)
        return row

    # score.py may one day hold its own source-build rule; then it is left as it is
    if "strict" in inspect.getsource(original).lower():
        return score
    score.engine_problem = engine_problem
    score.score = scored
    score._pack_engine_identity = True
    return score


def main():
    argv = sys.argv[1:]
    strict = "--strict-binary" in argv
    argv = [a for a in argv if a != "--strict-binary"]
    if argv[:1] == ["--run"]:
        if len(argv) < 2:
            sys.exit("give the program: --run run_cheat_suite.py [options]")
        program = argv[1] if os.path.isabs(argv[1]) or os.path.isfile(argv[1]) else os.path.join(HERE, argv[1])
        folder = os.path.dirname(os.path.abspath(program))
        if folder not in sys.path:
            sys.path.insert(0, folder)
        install(strict)
        # a program that does `from score import score` takes the replaced function, as the module now holds it
        sys.argv = [program] + argv[2:]
        runpy.run_path(program, run_name="__main__")
        return
    score = install(strict)
    sys.argv = [os.path.join(HERE, "score.py")] + argv
    score.main()


if __name__ == "__main__":
    main()