#!/usr/bin/env python3 """Generate deterministic P6-03 migration/offline/rollback evidence.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any OUTPUTS = (Path("web/workspace/migration.mjs"), Path("web/workspace/recovery.mjs"), Path("web/workspace/journal.mjs"), Path("web/workspace/test_migration.mjs"), Path("coverage/workspace-migration.report.json"), Path("coverage/workspace-rollback.package.json"), Path("coverage/offline-cache.package.json")) TESTS = (("migration", ["node", "web/workspace/test_migration.mjs"]), ("fault_matrix", ["node", "web/worker/test_recovery_offline_faults.mjs"])) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run(root: Path, command: list[str]) -> dict[str, Any]: result = subprocess.run(command, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) return {"status": "verified" if result.returncode == 0 else "blocked", "command": " ".join(command), "returncode": result.returncode, "stdout": result.stdout.decode("utf-8", errors="replace")[-2500:], "stderr": result.stderr.decode("utf-8", errors="replace")[-2000:]} def generate(root: Path, report_path: Path) -> dict[str, Any]: tests = {name: run(root, command) for name, command in TESTS} report: dict[str, Any] = { "schema": "offline-migration-report.v1", "task_id": "P6-03", "context_id": "linuxcnc-2.10.0-pre0-wasm-m1-py-c1", "status": "verified" if all(item["status"] == "verified" for item in tests.values()) else "blocked", "packages": {"workspace_versions": [1, 2], "migration_lossless": True, "rollback_schema": "workspace-rollback.v1", "offline_schema": "offline-cache-package.v1"}, "boundaries": ["package_hash", "content_hash", "migration_round_trip", "rollback_target", "rollback_failure", "offline_missing", "offline_corrupted", "online_degraded", "context_mismatch"], "tests": tests, "outputs": [{"path": path.as_posix(), "bytes": (root / path).stat().st_size, "sha256": sha256(root / path)} for path in OUTPUTS], "oracle": {"status": "blocked", "reason": "official browser OPFS migration/offline oracle is absent from locked source"}, "known_boundaries": ["Service Worker/browser matrix is P8-04", "release owner/browser/real device gates remain blocked"], } report["report_sha256"] = hashlib.sha256(canonical(report).encode("utf-8")).hexdigest() report_path.parent.mkdir(parents=True, exist_ok=True) report_path.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("--report", type=Path, default=Path("coverage/offline-migration.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"offline_migration={report['status']} tests={len(report['tests'])} oracle={report['oracle']['status']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())