# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-capability-warrant-drift-across-a-federated-policy-boundary-before-the-audit-trail-diverges-irreconcilably.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-capability-warrant-drift-across-a-federated-policy-boundary-before-the-audit-trail-diverges-irreconcilably.warrant.json
# Markdown SHA-256:   938599a64ab12c2cda106b6e0ebf6b5478e98360822116c888f6db4c1af874f3
# Code AST SHA-256:   861663909673a8e49d74dc0141166c36cb438dd7b1e247f8cf6ca8ae921ddf10
# Verified Classes:   CapabilityWarrantDrift, Warrant, GrantManifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class CapabilityWarrantDrift(RuntimeError):
    """Raised when caller's cached warrant does not match callee's committed grant."""
    pass


@dataclass(frozen=True)
class Warrant:
    capability_id: str
    policy_revision: int
    warrant_hash: str


@dataclass(frozen=True)
class GrantManifest:
    capability_id: str
    policy_revision: int
    accepted_warrant_hashes: frozenset
    has_compat_clause: bool


def hash_warrant(capability_id: str, revision: int, scope: str) -> str:
    return hashlib.sha256(f"{capability_id}|{revision}|{scope}".encode()).hexdigest()


def dispatch_cross_org(caller_warrant: Warrant, callee_manifest: GrantManifest) -> str:
    # Fail-closed on revision skew: no silent upcast.
    if callee_manifest.policy_revision != caller_warrant.policy_revision:
        raise CapabilityWarrantDrift(
            f"revision skew: caller={caller_warrant.policy_revision} "
            f"callee={callee_manifest.policy_revision} "
            f"capability={caller_warrant.capability_id}"
        )
    # Hash must be explicitly accepted; revision equality alone is insufficient.
    if caller_warrant.warrant_hash not in callee_manifest.accepted_warrant_hashes:
        raise CapabilityWarrantDrift(
            f"warrant hash {caller_warrant.warrant_hash[:12]} not in callee manifest "
            f"for {callee_manifest.capability_id}@{callee_manifest.policy_revision}"
        )
    # Caller must commit both tuples so reconciliation is hash equality, not semantic diff.
    return (
        f"dispatch:{caller_warrant.capability_id}"
        f"|caller_warrant={caller_warrant.warrant_hash}"
        f"|callee_grant_rev={callee_manifest.policy_revision}"
    )


def reconcile(caller_record: dict, callee_record: dict) -> None:
    if caller_record["warrant_hash"] != callee_record["caller_warrant_hash"]:
        raise CapabilityWarrantDrift(
            "audit reconciliation failed: caller-side warrant hash does not equal "
            "callee-side committed caller_warrant_hash"
        )
    if caller_record["policy_revision"] != callee_record["caller_policy_revision"]:
        raise CapabilityWarrantDrift(
            "audit reconciliation failed: revision mismatch across ledgers"
        )
