# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-federated-capability-grant-whose-policy-predicate-is-satisfied-only-under-a-residency-assumption-the-peer.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-federated-capability-grant-whose-policy-predicate-is-satisfied-only-under-a-residency-assumption-the-peer.warrant.json
# Markdown SHA-256:   1e10299bf07758abe435157bb3a4ecf9c0cc9344f0f563d8e71ad52610bd8a9e
# Code AST SHA-256:   850dc9512b6fd45bb16c6815d0c7440a4e8efc82298d8a89329744f4527d3e39
# Verified Classes:   ResidencyAttestationError, Provenance, Claim, Grant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class ResidencyAttestationError(RuntimeError):
    """Raised when a residency predicate cannot be evaluated from attested claims."""


class Provenance(Enum):
    SIGNED = "signed"
    SELF_ASSERTED = "self_asserted"
    DEFAULTED = "defaulted"


@dataclass(frozen=True)
class Claim:
    name: str
    value: str
    provenance: Provenance


@dataclass(frozen=True)
class Grant:
    capability_id: str
    signed_claims: dict[str, Claim]
    predicate: str
    predicate_signed: bool


RESIDENCY_VARS = {"peer.jurisdiction", "data.locus", "peer.subprocessor_chain"}


def evaluate_residency_predicate(grant: Grant, requester_context: dict[str, str]) -> bool:
    if not grant.predicate_signed:
        raise ResidencyAttestationError(
            f"grant {grant.capability_id}: predicate authored outside signed region; "
            "cannot be evaluated as a binding constraint"
        )

    referenced = {v for v in RESIDENCY_VARS if v in grant.predicate}
    for var in referenced:
        claim = grant.signed_claims.get(var)
        if claim is None:
            raise ResidencyAttestationError(
                f"grant {grant.capability_id}: residency attribute {var!r} absent "
                "from signed claim set; refusal required (no default-fill permitted)"
            )
        if claim.provenance is not Provenance.SIGNED:
            raise ResidencyAttestationError(
                f"grant {grant.capability_id}: attribute {var!r} has provenance "
                f"{claim.provenance.value}, not signed; inadmissible into residency predicate"
            )

    # Only now, with every variable bound to a signed claim, is evaluation sound.
    # requester_context is deliberately NOT consulted here.
    bindings = {v: grant.signed_claims[v].value for v in referenced}
    return _eval(grant.predicate, bindings)


def _eval(predicate: str, bindings: dict[str, str]) -> bool:
    # Placeholder for the real expression evaluator; must not read requester_context.
    if "data.locus == peer.jurisdiction" in predicate:
        return bindings.get("data.locus") == bindings.get("peer.jurisdiction")
    raise ResidencyAttestationError(f"unrecognized predicate: {predicate!r}")
