#!/usr/bin/env python3 """Generate deterministic configuration/HA/IO/workspace session 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") CONFIG = Path("coverage/configuration-startup-closure.report.json") HAL = Path("coverage/hal-component-manifest.report.json") DEVICES = Path("coverage/virtual-device-fault-matrix.report.json") RECOVERY = Path("coverage/workspace-recovery.report.json") MIGRATION = Path("coverage/workspace-migration.report.json") GATE = Path("coverage/release-candidate-gate.report.json") OUTPUT = Path("coverage/workspace-session-integration.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 artifact(root: Path, path: Path, role: str) -> dict[str, Any]: full = root / path if not full.is_file(): return {"path": path.as_posix(), "role": role, "status": "missing"} return {"path": path.as_posix(), "role": role, "status": "present", "bytes": full.stat().st_size, "sha256": hashlib.sha256(full.read_bytes()).hexdigest()} def sample(root: Path) -> tuple[dict[str, Any], dict[str, Any]]: command = ["node", "web/worker/test_workspace_session_integration.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("{")] value = json.loads(lines[-1]) if result.returncode == 0 and lines else {} runs.append({"status": "passed" if result.returncode == 0 and value else "blocked", "exit_code": result.returncode, "sample": value, "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")) config = json.loads((root / CONFIG).read_text(encoding="utf-8")) hal = json.loads((root / HAL).read_text(encoding="utf-8")) devices = json.loads((root / DEVICES).read_text(encoding="utf-8")) recovery = json.loads((root / RECOVERY).read_text(encoding="utf-8")) migration = json.loads((root / MIGRATION).read_text(encoding="utf-8")) gate = json.loads((root / GATE).read_text(encoding="utf-8")) first, second = sample(root) value = first.get("sample", {}) second_value = second.get("sample", {}) checks = { "context_consistency": {"status": "verified" if all(row.get("context_id") in {None, lock.get("context_id")} for row in (config, hal, recovery, migration, gate)) else "blocked", "context_id": lock.get("context_id")}, "configuration_dependency": {"status": "verified" if config.get("implementation_status") == "verified" else "blocked"}, "hal_dependency": {"status": "verified" if hal.get("implementation_status") == "verified" else "blocked"}, "device_dependency": {"status": "verified" if devices.get("implementation_status") == "verified" else "blocked"}, "recovery_migration_dependency": {"status": "verified" if recovery.get("implementation_status") == "verified" and migration.get("implementation_status") == "verified" else "blocked"}, "session_state_recovery": {"status": "verified" if value.get("session_state_hash") == value.get("recovery_state_hash") else "blocked", "session_state_hash": value.get("session_state_hash"), "recovery_state_hash": value.get("recovery_state_hash")}, "package_round_trip": {"status": "verified" if value.get("round_trip_equal") is True else "blocked", "package_v1_hash": value.get("package_v1_hash"), "package_v2_hash": value.get("package_v2_hash")}, "repeat_determinism": {"status": "verified" if first.get("status") == "passed" and second.get("status") == "passed" and value == second_value 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": "workspace-session-integration.v1", "context_id": lock.get("context_id"), "task_id": "M3-FS-01", "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, CONFIG, "configuration_startup_closure"), artifact(root, HAL, "hal_component_manifest"), artifact(root, DEVICES, "virtual_device_fault_matrix"), artifact(root, RECOVERY, "workspace_recovery"), artifact(root, MIGRATION, "workspace_migration"), artifact(root, GATE, "release_candidate_gate")], "checks": checks, "sample": value, "rebuild": {"command": "node web/worker/test_workspace_session_integration.mjs", "repeat_runs": 2}, "next_transition": "execute M3-PY-01 actual Pyodide virtual API bridge", "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"workspace_session_integration={report['status']} state={report['checks']['session_state_recovery']['status']} migration={report['checks']['package_round_trip']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())