-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffline_exact_replay.py
More file actions
221 lines (197 loc) · 7.52 KB
/
Copy pathoffline_exact_replay.py
File metadata and controls
221 lines (197 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""Offline exact-replay driver.
Builds or replays an exact-certification release bundle with network disabled.
Default replay verifies integrity + regenerability (``theorem_pending``).
Pass ``--require-lean`` / set ``MATHEVIDENCE_OFFLINE_LEAN=1`` to attempt Lake
declaration-identity inspect (``theorem_proved``). Missing dependencies are
setup/integrity failures, not theorem failures.
"""
from __future__ import annotations
import argparse
import json
import os
import sys
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from adapters.common.exact_replay.offline_bundle import ( # noqa: E402
DRIVER_VERSION,
both_modes_agree,
build_offline_exact_bundle,
mutate_bundle_for_tamper,
replay_offline_exact_bundle,
)
from adapters.common.exact_replay.plugins.ideal_membership import ( # noqa: E402
CAPABILITY as IDEAL_CAPABILITY,
)
def _ideal_reference_payload() -> tuple[dict[str, Any], dict[str, Any], str]:
"""Canonical exact ideal membership candidate (xy ∈ ⟨x, y⟩)."""
from adapters.common.canonical import bind_request_digest
def poly(m: int, coefficient: int, exponents: list[int]) -> dict[str, Any]:
return {
"varCount": m,
"terms": [{"coefficient": coefficient, "exponents": exponents}],
}
request = bind_request_digest(
{
"schemaVersion": "0.1.0",
"capability": IDEAL_CAPABILITY,
"capabilityVersion": "0.1.0",
"target": poly(2, 1, [1, 1]),
"generators": [poly(2, 1, [1, 0]), poly(2, 1, [0, 1])],
"requestedClaim": "witness",
}
)
certificate = {
"schemaVersion": "0.1.0",
"capability": IDEAL_CAPABILITY,
"capabilityVersion": "0.1.0",
"requestDigest": request["requestDigest"],
"target": request["target"],
"generators": request["generators"],
"multipliers": [poly(2, 1, [0, 1]), {"varCount": 2, "terms": []}],
"claimClass": "witness",
}
candidate_digest = "sha256:" + ("3" * 64)
return request, certificate, candidate_digest
def cmd_build(args: argparse.Namespace) -> int:
os.environ.setdefault("MATHEVIDENCE_OFFLINE", "1")
request, certificate, cand = _ideal_reference_payload()
out = Path(args.out)
result = build_offline_exact_bundle(
out,
capability_id=IDEAL_CAPABILITY,
request=request,
certificate=certificate,
candidate_bundle_digest=cand,
module_name="MathEvidence.Generated.Replay.exact_offline_xy",
declaration_name="exact_offline_xy",
repo_root=ROOT,
)
print(json.dumps({"status": "ok", "driverVersion": DRIVER_VERSION, **result}, indent=2))
return 0
def cmd_replay(args: argparse.Namespace) -> int:
os.environ.setdefault("MATHEVIDENCE_OFFLINE", "1")
if args.both_modes:
a, b = both_modes_agree(
args.bundle,
repo_root=ROOT,
check_live_toolchain=not args.skip_live_toolchain,
require_lean=args.require_lean,
)
payload = {
"regenerate-and-verify": a.__dict__,
"artifact-replay": b.__dict__,
"modesAgree": a.ok == b.ok
and a.logical_outcome == b.logical_outcome
and a.generated_source_hash == b.generated_source_hash,
}
print(json.dumps(payload, indent=2))
return 0 if a.ok and b.ok and payload["modesAgree"] else 1
result = replay_offline_exact_bundle(
args.bundle,
mode=args.mode,
repo_root=ROOT,
check_live_toolchain=not args.skip_live_toolchain,
require_lean=args.require_lean,
)
print(json.dumps(result.__dict__, indent=2))
return 0 if result.ok else 1
def cmd_tamper_selftest(args: argparse.Namespace) -> int:
"""Build a fresh bundle, apply each tamper case, assert failure."""
import tempfile
os.environ.setdefault("MATHEVIDENCE_OFFLINE", "1")
cases = [
"candidate",
"generated_source",
"artifact_delete",
"artifact_mutate",
"manifest",
"generator_version",
"declaration_identity",
"toolchain_lock",
"capability_id",
"capability_version",
]
request, certificate, cand = _ideal_reference_payload()
failures: list[str] = []
with tempfile.TemporaryDirectory() as tmp:
base = Path(tmp) / "good"
build_offline_exact_bundle(
base,
capability_id=IDEAL_CAPABILITY,
request=request,
certificate=certificate,
candidate_bundle_digest=cand,
module_name="MathEvidence.Generated.Replay.exact_offline_xy",
declaration_name="exact_offline_xy",
repo_root=ROOT,
)
good = replay_offline_exact_bundle(
base, repo_root=ROOT, check_live_toolchain=not args.skip_live_toolchain
)
if not good.ok:
print(json.dumps({"status": "fail", "reason": "clean bundle failed", **good.__dict__}))
return 1
for case in cases:
clone = Path(tmp) / f"tamper_{case}"
# Shallow file copy.
clone.mkdir()
for src in base.iterdir():
if src.is_file():
(clone / src.name).write_bytes(src.read_bytes())
mutate_bundle_for_tamper(clone, case=case)
bad = replay_offline_exact_bundle(
clone,
repo_root=ROOT,
check_live_toolchain=not args.skip_live_toolchain,
)
if bad.ok:
failures.append(case)
print(
json.dumps(
{
"case": case,
"ok": bad.ok,
"logical_outcome": bad.logical_outcome,
"error_kind": bad.error_kind,
}
)
)
status = "ok" if not failures else "fail"
print(json.dumps({"status": status, "failures": failures, "cases": cases}))
return 0 if not failures else 1
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
sub = parser.add_subparsers(dest="cmd", required=True)
p_build = sub.add_parser("build", help="Build ideal-membership reference offline bundle")
p_build.add_argument("--out", required=True, help="Output bundle directory")
p_build.set_defaults(func=cmd_build)
p_replay = sub.add_parser("replay", help="Replay an offline exact bundle")
p_replay.add_argument("--bundle", required=True)
p_replay.add_argument(
"--mode",
choices=["regenerate-and-verify", "artifact-replay"],
default="regenerate-and-verify",
)
p_replay.add_argument("--both-modes", action="store_true")
p_replay.add_argument(
"--skip-live-toolchain",
action="store_true",
help="Skip live lake-manifest lock compare (structure-only)",
)
p_replay.add_argument(
"--require-lean",
action="store_true",
help="Attempt Lake declaration-identity inspect (theorem_proved)",
)
p_replay.set_defaults(func=cmd_replay)
p_tamper = sub.add_parser("tamper-selftest", help="Run offline-bundle tamper matrix")
p_tamper.add_argument("--skip-live-toolchain", action="store_true")
p_tamper.set_defaults(func=cmd_tamper_selftest)
args = parser.parse_args(argv)
return int(args.func(args))
if __name__ == "__main__":
raise SystemExit(main())