#!/usr/bin/env python3 """Inspect fresh interrupted files with dry runs; never repair the evidence.""" import argparse import datetime import json import os from pathlib import Path import platform import shutil import subprocess import tempfile import observe POLICIES = {"checksum-update": ["--checksum", "--update"], "checksum-only": ["--checksum"]} def validate_case(row): observe.require(row["mode"] in ("inplace", "temporary"), "Unknown write mode") before = row["destinationBeforeInspection"] source = row["sourceBeforeInspection"] observe.require(row["interruption"]["returncode"] == -9, "No real interruption") written = row["interruption"]["writtenFile"] observe.require(observe.BARRIER_BYTES <= written["newPrefixBytes"] < observe.SIZE, "Missing partial-write evidence") observe.require(before["bytes"] == observe.SIZE, "Unexpected published length") observe.require(before["sha256"] != source["sha256"], "Fixture already equal") if row["mode"] == "inplace": observe.require(before == written, "In-place state differs from written file") observe.require(before["newPrefixBytes"] + before["oldSuffixBytes"] == observe.SIZE and before["oldSuffixBytes"] > 0, "Missing mixed generations") observe.require(int(before["mtimeNs"]) > int(source["mtimeNs"]), "Not newer") else: observe.require(before["newBytes"] == 0 and before["oldBytes"] == observe.SIZE, "Temporary mode changed published content") observe.require(before["mtimeNs"] == str(observe.DESTINATION_NS), "Changed old time") observe.require(len(row["inspections"]) == 2, "Wrong inspection count") observe.require({x["policy"] for x in row["inspections"]} == set(POLICIES), "Missing or duplicate policy") for inspection in row["inspections"]: expected = row["mode"] == "temporary" or inspection["policy"] == "checksum-only" observe.require("--dry-run" in inspection["command"], "Not a dry run") observe.require(inspection["returncode"] == 0, "Inspection failed") observe.require(inspection["selected"] == expected, "Wrong selection") observe.require(inspection["destinationAfter"] == before, "Inspection changed target") observe.require(inspection["sourceAfter"] == source, "Inspection changed source") lines = [line for line in inspection["stdout"].splitlines() if line.startswith("HESELA:")] observe.require(len(lines) == int(expected), "Raw output disagrees") if expected: observe.require(lines[0].startswith(("HESELA:>f", "HESELA:f", "HESELA:= 64 * 1024**2, "Insufficient scratch") results = [] for name in args.binary: resolved = shutil.which(name) observe.require(resolved is not None, "Binary not found") binary = str(Path(resolved).resolve(strict=True)) version = subprocess.run([binary, "--version"], capture_output=True, text=True, check=True) results.append({"binary": binary, "binarySha256": observe.digest(Path(binary).read_bytes()), "version": version.stdout, "cases": [inspect_case(binary, scratch, mode) for mode in ("inplace", "temporary")]}) receipt = {"schemaVersion": 1, "generatedAt": datetime.datetime.now(datetime.timezone.utc).isoformat(), "scope": "Dry-run selection on fresh interrupted fixtures, not repair or durability", "environment": {"kernel": platform.release(), "python": platform.python_version(), "machine": platform.machine(), "scratch": str(scratch)}, "codeSha256": {name: observe.digest(Path(__file__).with_name(name).read_bytes()) for name in ("diagnose.py", "test_diagnose.py", "observe.py")}, "runs": results} with output.open("x") as stream: json.dump(receipt, stream, indent=2) stream.write("\n") print(json.dumps({"binaryRuns": len(results), "interruptions": 2 * len(results), "inspections": 4 * len(results), "output": str(output)})) if __name__ == "__main__": main()