idea189-replicaweave-safe-m.../src/idea189_replicaweave_safe_m.../harness.py

23 lines
1.2 KiB
Python

"""Deterministic harness to run cartridges against a delta and produce TestReports and Manifests."""
from .test_cartridge import TestCartridge, TestReport
from .manifest import make_manifest
from .signer import sign_bytes
from typing import Dict, Any, List, Tuple
def run_cartridge(cartridge: TestCartridge, delta: Dict[str, Any], seed: int, signer_key: bytes = None) -> Tuple[TestReport, Dict[str, Any]]:
"""Run a cartridge deterministically and return the TestReport and Manifest.
signer_key: optional private key to sign the manifest (Ed25519)
"""
report = cartridge.run(delta, seed)
# build a minimal manifest and sign it if key provided
manifest = make_manifest(parent_hash=delta.get("parent_hash"), delta_ops_root=delta.get("ops_root"), test_report_root=report.score if isinstance(report.score, int) else 0)
if signer_key:
# create a canonical payload for signing and attach it so verifiers can reproduce the same bytes
canonical = str(manifest).encode("utf-8")
sig = sign_bytes(signer_key, canonical)
manifest["signed_payload"] = canonical.decode("utf-8")
manifest["signature"] = sig.hex()
return report, manifest