# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-policy-negotiation-divergence-across-federated-capability-handoffs-before-invoking-a-peer-tool.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-policy-negotiation-divergence-across-federated-capability-handoffs-before-invoking-a-peer-tool.warrant.json
# Markdown SHA-256:   4733be8db11e646e6a8c9c64243840674e5cccc059f4ddcace50f87acda3343a
# Code AST SHA-256:   220cfae03acb9a0fbb3c2ec3ba1f9ce66ff7e0244685b2fe1d0485e8bf630a26
# Verified Classes:   PolicyProjectionDivergence
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class PolicyProjectionDivergence(RuntimeError):
    """Raised when a negotiated lease omits constraints the grantor's engine applied."""
    pass


def mint_lease(grantor_ledger: dict, received_descriptor: dict) -> dict:
    """
    grantor_ledger: full constraint set from the grantor's native policy tuple.
    received_descriptor: what the grantee's schema can represent.
    Returns a lease only if the descriptor is a superset of the ledger keys.
    """
    ledger_keys = set(grantor_ledger.keys())
    descriptor_keys = set(received_descriptor.keys())
    missing = ledger_keys - descriptor_keys

    if missing:
        raise PolicyProjectionDivergence(
            f"cannot mint lease: grantor constraints {sorted(missing)} "
            f"have no representation in peer schema; negotiation must fail closed"
        )

    return {
        "constraints": grantor_ledger,
        "descriptor": received_descriptor,
        "status": "minted",
    }


def handle_enforcement_denial(denial: dict, lease_ledger: dict) -> str:
    """
    Grantee-side handler. Distinguishes retryable faults from divergence.
    """
    cid = denial.get("constraint_id")
    if cid is None:
        raise PolicyProjectionDivergence(
            "denial carries no constraint_id; cannot classify as transient or divergent"
        )
    if cid not in lease_ledger:
        raise PolicyProjectionDivergence(
            f"grantor enforced constraint {cid!r} absent from lease ledger; "
            f"lease is broader than approved proposition — do not retry, renegotiate"
        )
    return "retryable_local_violation"
