diff --git a/scripts/e2e/evidence/capture.py b/scripts/e2e/evidence/capture.py index 53e5007e..4ccfc2f8 100644 --- a/scripts/e2e/evidence/capture.py +++ b/scripts/e2e/evidence/capture.py @@ -22,7 +22,8 @@ How the pieces are obtained by SUBPROCESS and its `--csv` output is parsed (STRA-251 round-5 re-cut). The two logs are staged under their fixed names (strata-next.log, inference.log) in a private temporary directory, with CRLF folded to LF (raw bytes are never stored; comparisons are on the normalised text), because the script reads a RUN directory. - --meta (written by the runner, STRA-257): arm, series_id, block, pair_id, position, predecessor, schedule_hash, h0_id, machine and + --meta (written by the runner, STRA-257): arm, series_id, block, pair_id, position, predecessor, schedule_hash, h0_id, machine (a real series writes laptop|server: validity.py requires + hardware.machine_role == the declared machine, STRA-326) and ours_build_id (an object commit / target_triple / cargo_features / profile_value_sha256 = the FROZEN build id, STRA-304/322). Optional: owner_attestation {present, identifier, time_utc}, foreign_load {samples}, synthetic, long_session_argv, source_snapshot_sha256. --replay-output: the run JSON of replay_session.py (STRA-308); status must be "ok". diff --git a/scripts/e2e/evidence/golden/gen_validity.py b/scripts/e2e/evidence/golden/gen_validity.py index 2658c8e9..5cc8270f 100644 --- a/scripts/e2e/evidence/golden/gen_validity.py +++ b/scripts/e2e/evidence/golden/gen_validity.py @@ -262,6 +262,7 @@ def build_record(templates, machine, n, arm, order, sched_hash, spec, valid, rea ident["pair_id"] = pair_id(n) rec["synthetic"] = True rec["thresholds_source"] = "S3.7" + rec["hardware"]["machine_role"] = machine # STRA-326: validity.py cross-checks the declared machine against the record rec["hardware"]["owner_attestation"] = {"present": True, "identifier": "golden-attestation-1", "time_utc": "2026-01-01T00:00:00Z"} base = machine_defaults(machine) if base is not None: diff --git a/scripts/e2e/evidence/run_core.py b/scripts/e2e/evidence/run_core.py index e8fd6408..4e02e6e7 100644 --- a/scripts/e2e/evidence/run_core.py +++ b/scripts/e2e/evidence/run_core.py @@ -17,7 +17,9 @@ CLI v1 (fixed by the leaf) Additions (same exit-code table) run_core.py finish --ledger FILE --run-id ID --state DONE|INVALID [--outcome O --attributed-to C] [--records DIR] [--run-dir DIR] run_core.py unlock --run-root DIR --run-id ID [--lock-dir DIR] [--mode file|mkdir] - run_core.py finalize --records DIR --schedule FILE --ledger FILE --machine laptop|server + run_core.py finalize --records DIR --schedule FILE --ledger FILE [--machine laptop|server] + the machine is read from the pre-registered schedule (`machine` key, hashed with it); --machine is only a fallback for a + schedule without the key and a flag that conflicts with the schedule is refused (exit 3, MACHINE_MISMATCH; STRA-326) Exit codes 0 ok | 2 not the next scheduled slot (the message names the scheduled slot) | 3 malformed input or command-line usage error @@ -842,7 +844,7 @@ def main(argv=None, ctx=None): p.add_argument("--records", required=True) p.add_argument("--schedule", required=True) p.add_argument("--ledger", required=True) - p.add_argument("--machine", required=True, choices=["laptop", "server"]) + p.add_argument("--machine", choices=["laptop", "server"], help="only for a schedule without a `machine` key; must agree with it otherwise") args = parser.parse_args(argv) try: if args.command == "start": diff --git a/scripts/e2e/evidence/test_run_core.py b/scripts/e2e/evidence/test_run_core.py index 8b0c1000..fa2fd58c 100644 --- a/scripts/e2e/evidence/test_run_core.py +++ b/scripts/e2e/evidence/test_run_core.py @@ -629,6 +629,39 @@ class TestFinalize(Base): self.assertIn("pair-0001.ours", out + err) + def _with_schedule_machine(self, sched, machine): + with open(sched, "r", encoding="utf-8") as fh: + doc = json.load(fh) + doc["machine"] = machine + with open(sched, "w", encoding="utf-8", newline="\n") as fh: + json.dump(doc, fh) + + def test_finalize_takes_the_machine_from_the_schedule(self): # STRA-326 AC4 + records, sched, ledger = self._fixture([{"pair": 1, "arms": {}}], {1: {"pair": 1, "valid": True, "reasons": [], "event": None}}) + self._with_schedule_machine(sched, "laptop") + rc, out, err = self.cli("finalize", "--records", records, "--schedule", sched, "--ledger", ledger) + self.assertEqual(rc, 0, err) + self.assertEqual(json.loads(out)["valid_pairs"], 1) + + def test_finalize_refuses_a_flag_that_conflicts_with_the_schedule(self): # STRA-326 AC4 + records, sched, ledger = self._fixture([{"pair": 1, "arms": {}}], {1: {"pair": 1, "valid": True, "reasons": [], "event": None}}) + self._with_schedule_machine(sched, "laptop") + rc, out, err = self.cli("finalize", "--records", records, "--schedule", sched, "--ledger", ledger, "--machine", "server") + self.assertEqual(rc, RC.EXIT_MALFORMED) + self.assertIn("MACHINE_MISMATCH", out + err) + + def test_finalize_refuses_laptop_records_declared_server(self): # STRA-326 AC1 through the runner + records, sched, ledger = self._fixture([{"pair": 1, "arms": {}}], {1: {"pair": 1, "valid": True, "reasons": [], "event": None}}) + rc, out, err = self.cli("finalize", "--records", records, "--schedule", sched, "--ledger", ledger, "--machine", "server") + self.assertEqual(rc, RC.EXIT_MALFORMED) + self.assertIn("MACHINE_MISMATCH", out + err) + + def test_finalize_without_any_machine_is_refused(self): + records, sched, ledger = self._fixture([{"pair": 1, "arms": {}}], {1: {"pair": 1, "valid": True, "reasons": [], "event": None}}) + rc, out, err = self.cli("finalize", "--records", records, "--schedule", sched, "--ledger", ledger) + self.assertEqual(rc, RC.EXIT_MALFORMED) + + class TestMachineFacts(Base): def test_pid_alive_and_dead(self): self.assertTrue(RC.pid_alive(os.getpid())) diff --git a/scripts/e2e/evidence/test_validity.py b/scripts/e2e/evidence/test_validity.py index 7d0fd1bd..e1060ce2 100644 --- a/scripts/e2e/evidence/test_validity.py +++ b/scripts/e2e/evidence/test_validity.py @@ -302,5 +302,74 @@ class ImportedFunctions(unittest.TestCase): self.assertEqual(DOC["thresholds"]["vanilla_rerun_cap"], V.VANILLA_RERUN_CAP) +LAPTOP_GPU = "NVIDIA GeForce RTX 5090 Laptop GPU" + + +class MachineCrossCheck(unittest.TestCase): + """STRA-326: the declared machine must match what the records say; the flag alone never selects the rule set.""" + + BELOW_FLOOR = pw(51.0, 51.0, 51.0) # a laptop sample below the 100 W floor + + def test_laptop_records_declared_server_fail_with_a_named_error(self): # AC1 + fx = Fixture(self, "laptop", [{"pair": 1, "arms": {"ours": self.BELOW_FLOOR}}]) + fx.machine = "server" + rc, out, err = fx.run() + self.assertEqual(rc, 3, err) + self.assertIn("MACHINE_MISMATCH", err) + self.assertIn("pair-0001.ours", err) + + def test_laptop_gpu_name_under_a_server_declaration_fails(self): # AC1, the second half of the item + fx = Fixture(self, "server", [{"pair": 1, "arms": {"ours": {"set": {"hardware.gpu_name": LAPTOP_GPU}}}}]) + rc, out, err = fx.run() + self.assertEqual(rc, 3, err) + self.assertIn("MACHINE_MISMATCH", err) + self.assertIn("Laptop GPU", err) + + def test_server_records_declared_laptop_fail(self): # AC2 + fx = Fixture(self, "server", [{"pair": 1, "arms": {}}]) + fx.machine = "laptop" + rc, out, err = fx.run() + self.assertEqual(rc, 3, err) + self.assertIn("MACHINE_MISMATCH", err) + + def test_missing_machine_role_is_refused(self): + fx = Fixture(self, "laptop", [{"pair": 1, "arms": {"ours": {"unset": ["hardware.machine_role"]}}}]) + self.assertEqual(fx.run()[0], 3) + + def test_matching_declarations_behave_as_before(self): # AC3 (the golden vectors are the bulk of it) + for machine in ("laptop", "server"): + fx = Fixture(self, machine, [{"pair": 1, "arms": {}}]) + rc, out, err = fx.run() + self.assertEqual(rc, 0, err) + self.assertTrue(by_pair(out)[1]["valid"]) + + def test_in_process_error_is_a_malformed_subclass(self): + self.assertTrue(issubclass(V.MachineMismatch, V.Malformed)) + + def test_schedule_machine_is_used_and_a_conflicting_flag_is_refused(self): # AC4 (validity side) + sched = {"arms": ["ours"], "machine": "laptop"} + self.assertEqual(V.resolve_machine(sched, None), "laptop") + self.assertEqual(V.resolve_machine(sched, "laptop"), "laptop") + with self.assertRaises(V.MachineMismatch): + V.resolve_machine(sched, "server") + self.assertEqual(V.resolve_machine({"arms": []}, "server"), "server") # a schedule without the key falls back to the flag + with self.assertRaises(V.Malformed): + V.resolve_machine({"arms": []}, None) + with self.assertRaises(V.Malformed): + V.resolve_machine({"machine": "desktop"}, None) + + def test_cli_machine_flag_is_optional_when_the_schedule_has_it(self): + fx = Fixture(self, "laptop", [{"pair": 1, "arms": {}}]) + with open(fx.schedule, "r", encoding="utf-8") as fh: + sched = json.load(fh) + sched["machine"] = "laptop" + fx.write(fx.schedule, sched) + rc, out, err = run_cli("pairs", "--records", fx.records, "--schedule", fx.schedule, "--ledger", fx.ledger) + self.assertEqual(rc, 0, err) + rc, out, err = run_cli("pairs", "--records", fx.records, "--schedule", fx.schedule, "--ledger", fx.ledger, "--machine", "server") + self.assertEqual(rc, 3, err) + self.assertIn("MACHINE_MISMATCH", err) + + if __name__ == "__main__": unittest.main() diff --git a/scripts/e2e/evidence/validity.py b/scripts/e2e/evidence/validity.py index a96daa81..e587eba8 100644 --- a/scripts/e2e/evidence/validity.py +++ b/scripts/e2e/evidence/validity.py @@ -8,7 +8,7 @@ dabfd08e) and the balanced-schedule rule of E1 (D5) are coded. The runner (STRA- Stdlib only, Python >= 3.9 (SC15). Offline, no GPU, no network. Writes nothing unless --out is given. INTERFACE v1 - validity.py pairs --records