Files
linuxcncWebWASM/scripts/generate_virtual_device_fault_matrix.py

110 lines
6.0 KiB
Python

#!/usr/bin/env python3
"""Generate deterministic Worker virtual-device fault and replay evidence."""
from __future__ import annotations
import argparse
import hashlib
import json
import subprocess
from pathlib import Path
from typing import Any
CONTEXT = Path("context-lock.json")
IO_REPORT = Path("coverage/io-runtime.report.json")
HAL_REPORT = Path("coverage/hal-component-manifest.report.json")
CONFIG_REPORT = Path("coverage/configuration-startup-closure.report.json")
GATE = Path("coverage/release-candidate-gate.report.json")
OUTPUT = Path("coverage/virtual-device-fault-matrix.report.json")
def canonical(value: Any) -> str:
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
def digest(value: Any) -> str:
return hashlib.sha256(canonical(value).encode("utf-8")).hexdigest()
def file_digest(path: Path) -> str | None:
if not path.is_file():
return None
return hashlib.sha256(path.read_bytes()).hexdigest()
def artifact(root: Path, path: Path, role: str) -> dict[str, Any]:
full = root / path
sha = file_digest(full)
return {"path": path.as_posix(), "role": role, "status": "present", "bytes": full.stat().st_size, "sha256": sha} if sha else {"path": path.as_posix(), "role": role, "status": "missing"}
def run_sample(root: Path) -> tuple[dict[str, Any], dict[str, Any]]:
command = ["node", "web/worker/test_virtual_devices.mjs"]
runs = []
for _ in range(2):
result = subprocess.run(command, cwd=root, check=False, capture_output=True, text=True)
lines = [line.strip() for line in result.stdout.splitlines() if line.lstrip().startswith("{")]
sample = json.loads(lines[-1]) if result.returncode == 0 and lines else {}
runs.append({"status": "passed" if result.returncode == 0 and sample else "blocked", "exit_code": result.returncode, "sample": sample, "stdout_sha256": digest(result.stdout), "stderr_sha256": digest(result.stderr)})
return runs[0], runs[1]
def generate(root: Path, output: Path = OUTPUT) -> dict[str, Any]:
root = root.resolve()
lock = json.loads((root / CONTEXT).read_text(encoding="utf-8"))
io_report = json.loads((root / IO_REPORT).read_text(encoding="utf-8"))
hal_report = json.loads((root / HAL_REPORT).read_text(encoding="utf-8"))
config_report = json.loads((root / CONFIG_REPORT).read_text(encoding="utf-8"))
gate = json.loads((root / GATE).read_text(encoding="utf-8"))
first, second = run_sample(root)
sample = first.get("sample", {})
second_sample = second.get("sample", {})
scenarios = sample.get("scenarios", [])
scenario_codes = {str(row.get("kind")): row.get("code") for row in scenarios}
checks = {
"native_wasm_io_baseline": {"status": "verified" if io_report.get("implementation_status") == "verified" else "blocked"},
"hal_configuration_dependency": {"status": "verified" if hal_report.get("implementation_status") == "verified" and config_report.get("implementation_status") == "verified" else "blocked"},
"normal_fault_abort_recovery_coverage": {"status": "verified" if all(kind in scenario_codes for kind in ("SPINDLE_COMMAND", "TOOL_CHANGE", "TOOL_ABORT", "TOOL_FAULT", "TOOL_CLEAR_FAULT", "PROBE_CONFIG", "ESTOP", "IO_RECOVER")) else "blocked", "scenario_count": len(scenarios)},
"nonfinite_input_blocked": {"status": "verified" if "sim_io_nonfinite" in {row.get("code") for row in scenarios} else "blocked"},
"replay_state_hash": {"status": "verified" if sample.get("state_hash_equal") is True and sample.get("state_hash") == sample.get("replay_state_hash") else "blocked", "state_hash": sample.get("state_hash"), "replay_state_hash": sample.get("replay_state_hash")},
"repeat_determinism": {"status": "verified" if first.get("status") == "passed" and second.get("status") == "passed" and sample == second_sample else "blocked"},
"release_stop_preserved": {"status": "verified" if gate.get("release_action") == "STOP_RELEASE" and gate.get("candidate_eligible") is False else "blocked", "release_action": gate.get("release_action")},
}
checks_ok = all(row["status"] == "verified" for row in checks.values())
report: dict[str, Any] = {
"schema": "virtual-device-fault-matrix.v1",
"context_id": lock.get("context_id"),
"task_id": "M3-RT-03",
"status": "partial" if checks_ok else "blocked",
"implementation_status": "verified" if checks_ok else "blocked",
"candidate_eligible": False,
"release_action": "STOP_RELEASE",
"inputs": [artifact(root, CONTEXT, "context_lock"), artifact(root, IO_REPORT, "io_runtime"), artifact(root, HAL_REPORT, "hal_component_manifest"), artifact(root, CONFIG_REPORT, "configuration_startup_closure"), artifact(root, GATE, "release_candidate_gate")],
"checks": checks,
"scenarios": scenarios,
"sample": sample,
"rebuild": {"command": "node web/worker/test_virtual_devices.mjs", "repeat_runs": 2, "native_wasm_baseline": "coverage/io-runtime.report.json"},
"next_transition": "execute M3-FS-01 workspace recovery and configuration session integration",
"report_sha256": None,
}
report["report_sha256"] = digest({key: value for key, value in report.items() if key != "report_sha256"})
destination = output if output.is_absolute() else root / output
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8")
return report
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1])
parser.add_argument("--output", type=Path, default=OUTPUT)
args = parser.parse_args()
report = generate(args.repo_root.resolve(), args.output)
print(f"virtual_device_fault_matrix={report['status']} scenarios={len(report['scenarios'])} replay={report['checks']['replay_state_hash']['status']}")
return 0
if __name__ == "__main__":
raise SystemExit(main())