# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-brokered-federation-contract-has-silently-diverged-from-its-negotiated-policy-envelope.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-brokered-federation-contract-has-silently-diverged-from-its-negotiated-policy-envelope.warrant.json
# Markdown SHA-256:   981780564b8f7fe00164887336d470937fff1271bdbe9ef493f9750cdf8225db
# Code AST SHA-256:   ed84427995087eb8688de0e79c96453dcf6489685fb8b813ad35ec6034f2b672
# Verified Classes:   SilentPolicyDivergence
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SilentPolicyDivergence(RuntimeError):
    """Raised when a warrant would be issued against a contract whose compiled
    meaning has not been reconciled across both federation parties."""
    pass


# Compiler fallback behaviors. Only REJECT restores the invariant.
REJECT = "reject"
NEAREST_ANCESTOR = "nearest_ancestor"
DEFAULT_BRANCH = "default_branch"
NON_REJECTING = {NEAREST_ANCESTOR, DEFAULT_BRANCH}


def issue_warrant(contract, requester_compiler, provider_compiler,
                  warrant_binding):
    """Issue a federation warrant only if the contract's compiled meaning is
    identical on both sides of the boundary.

    contract: dict with 'hash', 'symbolic_terms' (list of locally-resolved
              references such as purpose/role/field enums), and
              'resolved_identities' (mapping symbol -> {requester, provider}).
    requester_compiler / provider_compiler: dict with 'fallback' behavior.
    warrant_binding: 'contract_hash' or 'compiled_policy_hash'.
    """
    has_symbolic = bool(contract.get("symbolic_terms"))
    any_non_rejecting = (
        requester_compiler["fallback"] in NON_REJECTING
        or provider_compiler["fallback"] in NON_REJECTING
    )
    bound_to_contract_only = warrant_binding == "contract_hash"

    # The exact three-condition structural signal for silent divergence.
    if has_symbolic and any_non_rejecting and bound_to_contract_only:
        raise SilentPolicyDivergence(
            "Refusing warrant: contract {h} contains locally-resolved symbols "
            "{s}, at least one compiler has a non-rejecting fallback, and the "
            "warrant is bound to contract hash alone. Compiled meaning is "
            "unreconciled across the boundary.".format(
                h=contract["hash"], s=contract["symbolic_terms"])
        )

    # Even with a compiled-policy binding, resolved identities must agree.
    if has_symbolic:
        for sym in contract["symbolic_terms"]:
            ids = contract.get("resolved_identities", {}).get(sym, {})
            if ids.get("requester") != ids.get("provider"):
                raise SilentPolicyDivergence(
                    "Symbol {s!r} resolves to {r!r} on requester side but "
                    "{p!r} on provider side; contract {h} is not "
                    "executable.".format(
                        s=sym, r=ids.get("requester"),
                        p=ids.get("provider"), h=contract["hash"])
                )

    return {
        "warrant": "issued",
        "contract_hash": contract["hash"],
        "binding": warrant_binding,
    }
