# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-revoked-peer-manifest-when-trust-federation-and-revocation-propagation-disagree-on-epoch-ordering.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-revoked-peer-manifest-when-trust-federation-and-revocation-propagation-disagree-on-epoch-ordering.warrant.json
# Markdown SHA-256:   d4f88e1cb96df061563fc9b743bed6840324b8f36cfc25ef42b2b06cb3ca31a3
# Code AST SHA-256:   00ad7be12dadaf1f41463a3711611c4f59d972b3fc9c5def842cc1b1aea664b4
# Verified Classes:   RevocationCoverageError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class RevocationCoverageError(RuntimeError):
    """Raised when a peer manifest cannot be safely admitted or rejected
    because the receiver's revocation coverage for the issuer is incomplete
    or the manifest's coverage proof is malformed."""
    pass


def admit_manifest(manifest, trust_store):
    """
    Decide admission of a signed peer manifest under cross-org revocation
    propagation. Returns ("admit" | "reject" | "hold", reason).

    manifest fields: issuer_id, key_id, manifest_epoch, issuer_coverage_root,
                     signature (already verified upstream)
    trust_store fields (per issuer):
        revoked_tuples: set[(key_id, manifest_epoch)]
        coverage_root:  int (log index; -1 if none)
    """
    issuer = manifest.issuer_id
    state = trust_store.get(issuer)

    if state is None:
        # No trust anchor at all: cannot evaluate, cannot safely admit.
        raise RevocationCoverageError(
            f"no trust state for issuer {issuer!r}; cannot evaluate "
            f"manifest epoch {manifest.manifest_epoch}"
        )

    if state.coverage_root < 0:
        raise RevocationCoverageError(
            f"issuer {issuer!r} has no coverage root; revocation "
            f"completeness is undecidable"
        )

    # 1. Tuple-set membership is the ONLY sound revocation predicate.
    if (manifest.key_id, manifest.manifest_epoch) in state.revoked_tuples:
        return ("reject", "tuple explicitly revoked")

    # 2. Coverage gating: if our view of the issuer's revocation log is
    #    older than what the manifest claims, we may be missing revocations.
    if state.coverage_root < manifest.issuer_coverage_root:
        return (
            "hold",
            f"coverage gap: local root {state.coverage_root} < "
            f"manifest root {manifest.issuer_coverage_root}",
        )

    # 3. Only now is admission sound.
    return ("admit", "tuple not revoked; coverage complete")


def serve_peer(manifest, trust_store):
    decision, reason = admit_manifest(manifest, trust_store)
    if decision == "hold":
        # Bounded hold: must not silently serve, must not silently drop.
        raise RevocationCoverageError(
            f"manifest {manifest.issuer_id}/{manifest.key_id}@"
            f"{manifest.manifest_epoch} held pending coverage: {reason}"
        )
    if decision == "reject":
        raise PermissionError(
            f"manifest {manifest.issuer_id}/{manifest.key_id}@"
            f"{manifest.manifest_epoch} rejected: {reason}"
        )
    return "serving"
