"""MPC Authority: issue and verify Manufacturing Proof Certificates; immutable, versioned.""" from typing import Any from fusionagi.maa.schemas.mpc import ( ManufacturingProofCertificate, MPCId, DecisionLineageEntry, SimulationProof, ProcessJustification, MachineDeclaration, RiskRegisterEntry, ) from fusionagi.maa.versioning import VersionStore class MPCAuthority: """Central issue and verify MPCs; immutable, versioned.""" def __init__(self) -> None: self._store = VersionStore() self._by_value: dict[str, ManufacturingProofCertificate] = {} # mpc_id.value -> cert def issue( self, mpc_id_value: str, decision_lineage: list[DecisionLineageEntry] | None = None, simulation_proof: SimulationProof | None = None, process_justification: ProcessJustification | None = None, machine_declaration: MachineDeclaration | None = None, risk_register: list[RiskRegisterEntry] | None = None, metadata: dict[str, Any] | None = None, ) -> ManufacturingProofCertificate: """Issue a new MPC; version auto-incremented.""" latest = self._store.get_latest_version(mpc_id_value) version = (latest or 0) + 1 mpc_id = MPCId(value=mpc_id_value, version=version) cert = ManufacturingProofCertificate( mpc_id=mpc_id, decision_lineage=decision_lineage or [], simulation_proof=simulation_proof, process_justification=process_justification, machine_declaration=machine_declaration, risk_register=risk_register or [], metadata=metadata or {}, ) self._store.put(mpc_id_value, version, cert) self._by_value[mpc_id_value] = cert return cert def verify(self, mpc_id: str | MPCId, version: int | None = None) -> ManufacturingProofCertificate | None: """Verify and return MPC if valid; None if not found or invalid.""" value = mpc_id.value if isinstance(mpc_id, MPCId) else mpc_id cert = self._store.get(value, version) if version is not None else self._by_value.get(value) if cert is None and version is None: cert = self._store.get(value, self._store.get_latest_version(value)) return cert def get(self, mpc_id_value: str, version: int | None = None) -> ManufacturingProofCertificate | None: """Return stored MPC by value and optional version.""" if version is not None: return self._store.get(mpc_id_value, version) return self._by_value.get(mpc_id_value) or self._store.get( mpc_id_value, self._store.get_latest_version(mpc_id_value) )