Files
linuxcncWebWASM/scripts/generate_endpoint_replay_s4_report.py

73 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Generate deterministic S4-02 endpoint replay parity evidence."""
from __future__ import annotations
import hashlib
import json
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / "coverage/endpoint-replay-s4.report.json"
INPUTS = (
"context-lock.json",
"web/worker/endpoint-replay.mjs",
"web/worker/test_endpoint_replay_s4.mjs",
"web/worker/sim-session.mjs",
"web/worker/replay.mjs",
"web/worker/trace-export.mjs",
"coverage/full-simulation-s4.report.json",
)
def digest(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def canonical(value: object) -> bytes:
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8")
def run(command: list[str]) -> dict[str, object]:
result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, check=False)
sample = None
for line in reversed(result.stdout.splitlines()):
if line.lstrip().startswith("{"):
try:
sample = json.loads(line)
break
except json.JSONDecodeError:
continue
return {"command": command, "returncode": result.returncode, "status": "verified" if result.returncode == 0 else "blocked", "stdout_sha256": digest(result.stdout.encode()), "stderr_sha256": digest(result.stderr.encode()), "sample": sample}
def generate(output: Path = OUTPUT) -> dict[str, object]:
context = json.loads((ROOT / "context-lock.json").read_text(encoding="utf-8"))
replay = run(["node", "web/worker/test_endpoint_replay_s4.mjs"])
sample = replay["sample"] or {}
parity = sample.get("parity", {})
report: dict[str, object] = {
"schema": "endpoint-replay-s4-report.v1",
"task_id": "S4-02",
"context_id": context["context_id"],
"status": "verified" if replay["status"] == "verified" and sample.get("status") == "verified" and all(parity.get(name, {}).get("equal") is True for name in ("mode_equal", "duplicate_operation_equal", "recovery_equal", "trace_equal")) and sample.get("first_divergence") is None else "blocked",
"modes": ["1x", "fast", "single_step", "recover", "trace_import"],
"parity": parity,
"first_divergence": sample.get("first_divergence"),
"boundaries": ["endpoint semantic comparison excludes session-specific sequence/hash wrappers", "official LinuxCNC endpoint oracle remains blocked", "REAL-EXCLUDED host devices remain outside replay"],
"inputs": [{"path": path, "sha256": digest((ROOT / path).read_bytes())} for path in INPUTS],
"test": replay,
"sample": sample,
"report_sha256": None,
}
report["report_sha256"] = digest(canonical({key: value for key, value in report.items() if key != "report_sha256"}))
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8")
return report
if __name__ == "__main__":
report = generate()
print(f"endpoint_replay_s4={report['status']}")