# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-warrant-whose-policy-digest-matches-but-whose-delegation-chain-is-unverifiable.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-warrant-whose-policy-digest-matches-but-whose-delegation-chain-is-unverifiable.warrant.json
# Markdown SHA-256:   67b04f12ea753de01f0623f4bdf26006a94b9ceeadd1d995460d500e0bff5647
# Code AST SHA-256:   76317f11190c06a90c9a646b88529d64b27fc5e47a11a78f51485dda0423d8ab
# Verified Classes:   ProvenanceUnverifiedError, WarrantRejectedError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class ProvenanceUnverifiedError(RuntimeError):
    """Raised when a warrant's delegation chain cannot be bound to a trusted root,
    even though its policy digest and leaf signature are valid."""


class WarrantRejectedError(RuntimeError):
    """Raised when policy conformance fails (digest mismatch)."""


def evaluate_warrant(warrant, trusted_roots, expected_digest, freshness_window_s, now):
    # --- Predicate 1: conformance (content integrity only) ---
    if warrant.policy_digest != expected_digest:
        raise WarrantRejectedError(
            f"policy digest mismatch: got {warrant.policy_digest!r}"
        )

    # --- Predicate 2: provenance (authority, evaluated independently) ---
    if not verify_leaf_signature(warrant):
        raise ProvenanceUnverifiedError("leaf signature invalid")

    chain = warrant.chain  # list of hops from root-ward to leaf-ward
    if not chain:
        raise ProvenanceUnverifiedError("empty delegation chain")

    root = chain[0]
    if root.issuer_ref not in trusted_roots:
        raise ProvenanceUnverifiedError(
            f"root {root.issuer_ref!r} not in trust root set"
        )

    # Every hop must be *attested*, not merely *referenced*.
    for hop in chain:
        if hop.attestation is None:
            # A referenced-only hop: resolution succeeded, binding did not.
            raise ProvenanceUnverifiedError(
                f"hop {hop.issuer_ref!r}->{hop.subject_ref!r} is asserted by "
                f"reference with no signature or inclusion proof; "
                f"digest match does not confer authority"
            )
        if not verify_hop_attestation(hop, trusted_roots):
            raise ProvenanceUnverifiedError(
                f"hop {hop.issuer_ref!r}->{hop.subject_ref!r} attestation invalid"
            )
        if (now - hop.attested_at) > freshness_window_s:
            # Revocation-propagation guard: stale attestation == unverifiable.
            raise ProvenanceUnverifiedError(
                f"hop {hop.issuer_ref!r}->{hop.subject_ref!r} attestation is "
                f"stale ({now - hop.attested_at}s > {freshness_window_s}s); "
                f"revocation state cannot be trusted"
            )

    return True  # conformance AND provenance both hold


def verify_leaf_signature(warrant) -> bool:
    ...


def verify_hop_attestation(hop, trusted_roots) -> bool:
    ...
