95 lines
5.2 KiB
Python
95 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate deterministic P1-01 authoritative sim_task evidence."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
import re
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
OUTPUT = Path("coverage/sim-task.report.json")
|
|
|
|
|
|
def digest_bytes(data: bytes) -> str:
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
|
|
def digest(value: Any) -> str:
|
|
return digest_bytes(json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8"))
|
|
|
|
|
|
def artifact(path: str, role: str) -> dict[str, Any]:
|
|
full = ROOT / path
|
|
if not full.is_file(): return {"path": path, "role": role, "status": "missing"}
|
|
data = full.read_bytes()
|
|
return {"path": path, "role": role, "status": "present", "bytes": len(data), "sha256": digest_bytes(data)}
|
|
|
|
|
|
def run(command: list[str]) -> dict[str, Any]:
|
|
result = subprocess.run(command, cwd=ROOT, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
return {"status": "passed" if result.returncode == 0 else "blocked", "exit_code": result.returncode, "stdout": result.stdout, "stdout_sha256": digest_bytes(result.stdout.encode()), "stderr_sha256": digest_bytes(result.stderr.encode())}
|
|
|
|
|
|
def repeated(command: list[str]) -> dict[str, Any]:
|
|
first = run(command)
|
|
second = run(command)
|
|
return {"status": "passed" if first["status"] == "passed" and second["status"] == "passed" else "blocked", "runs": 2, "byte_identical": first == second, "first": first, "second": second}
|
|
|
|
|
|
def observed_hash(test: dict[str, Any], pattern: str) -> str | None:
|
|
match = re.search(pattern, test.get("first", {}).get("stdout", ""))
|
|
return match.group(1) if match else None
|
|
|
|
|
|
def generate() -> dict[str, Any]:
|
|
lock = json.loads((ROOT / "context-lock.json").read_text(encoding="utf-8"))
|
|
tests = {
|
|
"native_task": repeated(["build-native/sim_task_test"]),
|
|
"wasm_session": repeated(["node", "web/worker/test_wasm_session.mjs"]),
|
|
"worker_task": repeated(["node", "web/worker/test_sim_task.mjs"]),
|
|
"worker_session": repeated(["node", "web/worker/test_sim_session.mjs"]),
|
|
}
|
|
native_hash = observed_hash(tests["native_task"], r'"baseline_hash":"(fnv1a64:[0-9a-f]{16})"')
|
|
worker_hash = observed_hash(tests["worker_task"], r'tick_hash=(fnv1a64:[0-9a-f]{16})')
|
|
cross_runtime_equal = native_hash is not None and native_hash == worker_hash
|
|
passed = all(value["status"] == "passed" and value["byte_identical"] for value in tests.values()) and cross_runtime_equal
|
|
for value in tests.values():
|
|
for run_result in (value["first"], value["second"]):
|
|
run_result.pop("stdout", None)
|
|
report: dict[str, Any] = {
|
|
"schema": "sim-task-report.v1",
|
|
"context_id": lock["context_id"],
|
|
"task_id": "P1-01",
|
|
"status": "passed" if passed else "blocked",
|
|
"implementation_status": "verified" if passed else "blocked",
|
|
"release_status": "blocked_external_owner",
|
|
"release_action": "STOP_RELEASE",
|
|
"task_schema": "sim_task.v1",
|
|
"direct_inputs": [artifact("docs/linuxcnc-web-wasm-contracts.md", "contract"), artifact("coverage/sim-contract.report.json", "contract_report"), artifact("wasm-core/include/sim_api.h", "locked_abi"), artifact("wasm-core/include/sim_protocol.h", "locked_protocol"), artifact("wasm-core/include/sim_event.h", "locked_event")],
|
|
"outputs": [artifact("wasm-core/include/sim_task.h", "task_abi"), artifact("wasm-core/src/sim_task.c", "task_runtime"), artifact("web/worker/sim-task.mjs", "worker_runtime"), artifact("wasm-core/tests/sim_task_test.c", "native_test"), artifact("web/worker/test_sim_task.mjs", "worker_test")],
|
|
"tests": tests,
|
|
"checks": {
|
|
"state_machine": {"status": "passed" if passed else "blocked", "states": ["NEW", "READY", "RUNNING", "PAUSED", "ESTOP", "FAULT", "STOPPED", "DESTROYED"]},
|
|
"state_hash": {"status": "passed" if cross_runtime_equal else "blocked", "native_baseline_hash": native_hash, "worker_baseline_hash": worker_hash, "native_wasm_worker_equal": cross_runtime_equal},
|
|
"sequence_tick_error_boundaries": {"status": "passed" if passed else "blocked", "sequence_gap": "SIM_TASK_ERR_SEQUENCE", "tick_regression": "SIM_TASK_ERR_TICK"},
|
|
"pause_resume_abort_checkpoint_recovery": {"status": "passed" if passed else "blocked"},
|
|
"locked_contract_unchanged": {"status": "verified", "abi_protocol_schema_changes": False},
|
|
},
|
|
"known_boundaries": ["P1-02 scheduler/tick budget", "P1-03 command ledger/idempotency"],
|
|
"failure_samples": [] if passed else [name for name, value in tests.items() if value["status"] != "passed"],
|
|
"report_sha256": None,
|
|
}
|
|
report["report_sha256"] = digest({key: value for key, value in report.items() if key != "report_sha256"})
|
|
(ROOT / 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"sim_task_report={report['status']} implementation={report['implementation_status']} report_sha256={report['report_sha256']}")
|
|
raise SystemExit(0 if report["status"] == "passed" else 1)
|