#!/usr/bin/env python3 """Generate deterministic S3-03 Worker/offline recovery evidence.""" from __future__ import annotations import hashlib import json import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUTPUT = ROOT / "coverage/recovery-s3.report.json" INPUTS = ( "context-lock.json", "web/worker/recovery-runtime.mjs", "web/worker/test_recovery_s3.mjs", "web/worker/sim-session.mjs", "web/workspace/recovery.mjs", "web/workspace/journal.mjs", "web/workspace/migration.mjs", ) 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")) recovery = run(["node", "web/worker/test_recovery_s3.mjs"]) legacy = run(["node", "web/worker/test_recovery_offline_faults.mjs"]) report: dict[str, object] = { "schema": "recovery-s3-report.v1", "task_id": "S3-03", "context_id": context["context_id"], "status": "verified" if recovery["status"] == "verified" and legacy["status"] == "verified" and (recovery["sample"] or {}).get("status") == "verified" else "blocked", "features": ["logical Worker crash/restart", "checkpoint event replay and operation reconciliation", "journal atomic prepare/commit/half-commit/quota", "offline package ready/network_required/path/quota validation", "deterministic migration and state hashes"], "boundaries": ["OPFS/Service Worker browser-specific transport remains represented by deterministic storage adapters", "official crash/browser oracle remains blocked", "real filesystem and wall clock are excluded"], "inputs": [{"path": path, "sha256": digest((ROOT / path).read_bytes())} for path in INPUTS], "tests": {"s3": recovery, "legacy_faults": legacy}, "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"recovery_s3={report['status']}")