# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-liability-attribution-gap-when-federated-agents-negotiate-capabilities-under-asymmetric-policy-epochs.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-liability-attribution-gap-when-federated-agents-negotiate-capabilities-under-asymmetric-policy-epochs.warrant.json
# Markdown SHA-256:   3603bfd3cf3575886b010125bac6bee625a4d7c5f2b0a86a643d62ac283f04fd
# Code AST SHA-256:   85c07441829adf60ce734d5c95b48fb8081ed106d940509ef2a8340f542f6a78
# Verified Classes:   AttributionEpochMismatch, Warrant, PolicyHead
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class AttributionEpochMismatch(RuntimeError):
    """Raised when liability would be adjudicated under a policy epoch
    that the granting agent never consented to."""
    pass


@dataclass(frozen=True)
class Warrant:
    capability: str
    grantee: str
    policy_epoch: int
    liability_hash: str  # H(inlined liability clause bytes)


@dataclass(frozen=True)
class PolicyHead:
    epoch: int
    liability_bytes: bytes

    @property
    def liability_hash(self) -> str:
        return hashlib.sha256(self.liability_bytes).hexdigest()


def authorize_execution(warrant: Warrant, head: PolicyHead,
                        renegotiation_nonce: str | None) -> None:
    """Pull-before-execute gate. Halt unless warrant epoch is pinned to head
    or a signed renegotiation event covers this warrant."""
    if renegotiation_nonce is not None:
        return  # explicit re-handshake under new epoch; safe to proceed

    if head.epoch != warrant.policy_epoch:
        # Epoch advanced. Even if liability bytes happen to match, the grant
        # was signed under a different policy frame; require renegotiation.
        raise AttributionEpochMismatch(
            f"warrant epoch {warrant.policy_epoch} != policy head {head.epoch} "
            f"for capability {warrant.capability!r}; no renegotiation event. "
            f"Refusing execution to prevent retroactive liability binding."
        )

    if head.liability_hash != warrant.liability_hash:
        raise AttributionEpochMismatch(
            f"liability clause hash mismatch at epoch {head.epoch}: "
            f"warrant committed {warrant.liability_hash[:12]}, "
            f"head serves {head.liability_hash[:12]}."
        )


def adjudicate(warrant: Warrant, incident_epoch: int,
               epoch_policy: dict[int, PolicyHead]) -> str:
    """Replay liability strictly against the warrant's pinned epoch."""
    if incident_epoch != warrant.policy_epoch:
        pinned = epoch_policy.get(warrant.policy_epoch)
        if pinned is None or pinned.liability_hash != warrant.liability_hash:
            raise AttributionEpochMismatch(
                f"cannot adjudicate: warrant epoch {warrant.policy_epoch} "
                f"liability bytes unavailable or mutated; refusing to bind "
                f"grantee to epoch {incident_epoch}."
            )
    return f"liability adjudicated under pinned epoch {warrant.policy_epoch}"
