From c2d68f750d07efb73e6ba09b00d4acf78ab34d91 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Thu, 29 Oct 2026 01:01:20 +0100 Subject: [PATCH] STRA-326: cross-check the declared machine against hardware.machine_role / gpu_name validity.py trusted --machine alone, so laptop records declared "server" skipped the 100 W power floor and the owner attestation. derive_pairs now refuses (MACHINE_MISMATCH, exit 3, run ids named) records whose hardware.machine_role differs from the declared machine and a "server" declaration over a Laptop GPU. A schedule `machine` key pre-registers the machine; run_core finalize reads it from there (--machine only a fallback) and refuses a conflicting flag. The golden fixture builder stamps machine_role = machine; golden cases unchanged (0 differences). Co-Authored-By: Claude Sonnet 5.5 --- scripts/e2e/evidence/capture.py | 3 +- scripts/e2e/evidence/golden/gen_validity.py | 1 + scripts/e2e/evidence/run_core.py | 6 +- scripts/e2e/evidence/test_run_core.py | 33 ++++++++++ scripts/e2e/evidence/test_validity.py | 69 +++++++++++++++++++++ scripts/e2e/evidence/validity.py | 55 ++++++++++++++-- 6 files changed, 160 insertions(+), 7 deletions(-) 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 --schedule --ledger --machine laptop|server [--out FILE] + validity.py pairs --records --schedule --ledger [--machine laptop|server] [--out FILE] stdout: {"pairs": [{"pair": i, "valid": bool, "reasons": [...], "event": null|"FAIL"|"RERUN"|"OWNER"}], "manifest_ok": bool, "invalid_rate": x} validity.py schedule --check [--out FILE] @@ -19,6 +19,12 @@ INTERFACE v1 Machine: the `--machine` argument (the same one that selects the E2(1) laptop/server power rules) also selects the E2(2) rule: the owner attestation is required on laptop pairs only (ATTESTATION_MISSING / OWNER); a server pair needs none and a present one is ignored. +The declaration is cross-checked (STRA-326): every record's hardware.machine_role must equal the declared machine, and a `server` +declaration is refused for a record whose hardware.gpu_name names a Laptop GPU. The runner writes machine_role = laptop|server into +--meta (capture.py). A mismatch is MALFORMED INPUT (exit 3, MACHINE_MISMATCH naming the run ids): the power floor and the attestation +rule are never skipped by typing the wrong flag. A schedule that carries a `machine` key pre-registers the machine (it is part of the +schedule hash); a conflicting --machine is refused (resolve_machine). + Reason codes: POWER, WORKLOAD, CACHE, INFRASTRUCTURE, ATTESTATION_MISSING, FOREIGN_LOAD, COUNTABLE_MISMATCH. Events: RERUN (attributable infrastructure failure, build-id mismatch, vanilla failure not attributable up to the cap), OWNER (missing attestation, third non-attributable vanilla failure of a slot), FAIL (ours failure not attributable; the series stops FAILED). @@ -53,6 +59,7 @@ VANILLA_RERUN_CAP = 2 # E2(5): a vanilla failure not attributable # SC14: the foreign-load threshold itself is read from the schedule file (TL proposal: 5 percent of a core for 2 s; BA to confirm). INFRA_CAUSES = ("POWER", "WORKLOAD", "CACHE", "LOCK", "WATCHDOG", "TELEMETRY") # E2(5): causes a failed attempt can be attributed to OUTCOMES = ("CRASH", "TIMEOUT", "INTEGRITY_FAILURE") +MACHINES = ("laptop", "server") EVENT_RANK = {"RERUN": 1, "OWNER": 2, "FAIL": 3} GROUP_SIZE = 6 # D5: three arms -> all 6 permutations per group of 6 pairs METADATA_PATHS = ( # E2(3): the metadata of the common paragraph must be complete @@ -67,6 +74,10 @@ class Malformed(Exception): """Input that cannot be interpreted (exit 3).""" +class MachineMismatch(Malformed): + """The machine the series declares (--machine, schedule `machine`) disagrees with what a record says it was captured on (exit 3).""" + + class Refusal(Exception): """Ledger/manifest refusal (exit 2); .names lists the offending run ids.""" @@ -350,15 +361,51 @@ def check_manifest(records, last): raise Refusal("; ".join(parts), orphans + unlisted) +def resolve_machine(sched, flag): + """The machine a series is judged as. A schedule `machine` key (pre-registered, hashed with the schedule) wins; a --machine flag that + disagrees with it is refused (MachineMismatch). A schedule without the key falls back to the flag (the golden schedules have none); + the per-record cross-check in check_machine() still applies. Returns "laptop" or "server".""" + declared = sched.get("machine") if isinstance(sched, dict) else None + if declared is not None and declared not in MACHINES: + raise Malformed("schedule machine %r is not one of %s" % (declared, ", ".join(MACHINES))) + if flag is not None and flag not in MACHINES: + raise Malformed("machine must be laptop or server") + if declared is not None and flag is not None and declared != flag: + raise MachineMismatch("MACHINE_MISMATCH: the schedule pre-registers machine %r but %r was given on the command line" % (declared, flag)) + machine = declared if declared is not None else flag + if machine is None: + raise Malformed("no machine: the schedule has no `machine` key and no --machine was given") + return machine + + +def check_machine(records, machine): + """STRA-326: the declared machine must match what the records say they were captured on. Raises MachineMismatch naming run ids.""" + wrong, laptop_gpu = [], [] + for rid in sorted(records): + role = get_path(records[rid], "hardware.machine_role") + if role != machine: + wrong.append("%s (hardware.machine_role=%r)" % (rid, role)) + gpu = get_path(records[rid], "hardware.gpu_name") + if machine == "server" and isinstance(gpu, str) and "laptop" in gpu.lower(): + laptop_gpu.append("%s (hardware.gpu_name=%r)" % (rid, gpu)) + if wrong or laptop_gpu: + parts = [] + if wrong: + parts.append("record(s) whose hardware.machine_role is not %r: %s" % (machine, ", ".join(wrong))) + if laptop_gpu: + parts.append("record(s) of a Laptop GPU under a `server` declaration: %s" % ", ".join(laptop_gpu)) + raise MachineMismatch("MACHINE_MISMATCH: declared machine %r; %s" % (machine, "; ".join(parts))) + + def derive_pairs(records, sched, last, machine): """The pairs command as a function. records: run_id -> record; last: run_id -> last ledger line. Returns the result dict; raises Refusal (exit 2) or Malformed (exit 3).""" - if machine not in ("laptop", "server"): - raise Malformed("machine must be laptop or server") if not isinstance(sched, dict) or not isinstance(sched.get("arms"), list): raise Malformed("schedule has no arms list") + machine = resolve_machine(sched, machine) arms = list(sched["arms"]) check_manifest(records, last) + check_machine(records, machine) by_pair = {} for rid, line in last.items(): by_pair.setdefault(_pair_number(line), []).append((rid, line)) @@ -406,7 +453,7 @@ def main(argv=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=list(MACHINES), help="required unless the schedule carries a `machine` key; must agree with it") p.add_argument("--out", help="also write the JSON result here (run roots outside Git)") s = sub.add_parser("schedule", help="schedule checks") s.add_argument("--check", required=True, metavar="SCHEDULE_JSON")