# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-grant-after-policy-translation-silently-widens-its-effective-authority.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-grant-after-policy-translation-silently-widens-its-effective-authority.warrant.json
# Markdown SHA-256:   07c4222a7085d8193e36996714d2ffdd9b435db25a7b5c617a92dc84f9389fb0
# Code AST SHA-256:   9230a9479c30a35435de841b1eaef1bfb7363864b5730abbb494018c9edaa2df
# Verified Classes:   ResidualUndischarged, PredicateClass, TranslatedPredicate, TranslationEnvelope
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Callable


class ResidualUndischarged(RuntimeError):
    """Raised when a federated grant cannot be safely minted into a local warrant
    because a translated predicate's residual obligation was not discharged."""


class PredicateClass(Enum):
    PRESERVED = "preserved"   # image == source
    NARROWED = "narrowed"     # image subset of source (safe)
    WIDENED = "widened"       # image superset of source (unsafe)
    ELIDED = "elided"         # dropped entirely (unsafe unless residual is false)


@dataclass
class TranslatedPredicate:
    name: str
    classification: PredicateClass
    residual: Callable[[], bool] | None  # consumer-side implication prover


@dataclass
class TranslationEnvelope:
    source_org: str
    grant_id: str
    predicates: list[TranslatedPredicate] = field(default_factory=list)


def mint_local_warrant(
    envelope: TranslationEnvelope,
    *,
    envelope_residuals_present: bool,
) -> dict:
    # Schema stripping of the residual set is a widening, not a missing field.
    if not envelope_residuals_present:
        raise ResidualUndischarged(
            f"grant {envelope.grant_id} from {envelope.source_org}: "
            "residual set stripped by schema validation; treating as unverified "
            "grant to the complement of the intended authority set"
        )

    warrant: dict = {}
    for pred in envelope.predicates:
        if pred.classification is PredicateClass.PRESERVED:
            warrant[pred.name] = True
        elif pred.classification is PredicateClass.NARROWED:
            if pred.residual is None or not pred.residual():
                raise ResidualUndischarged(
                    f"grant {envelope.grant_id}: narrowed predicate "
                    f"{pred.name!r} residual failed to discharge locally"
                )
            warrant[pred.name] = True
        elif pred.classification is PredicateClass.ELIDED:
            # Elision is safe ONLY if the consumer can prove the residual is false,
            # which correctly yields an empty (deny) warrant on this attribute.
            discharged = pred.residual is not None and pred.residual()
            if discharged:
                raise ResidualUndischarged(
                    f"grant {envelope.grant_id}: elided predicate {pred.name!r} "
                    "residual proved true; elision would widen authority"
                )
            warrant[pred.name] = False
        else:  # WIDENED
            raise ResidualUndischarged(
                f"grant {envelope.grant_id}: predicate {pred.name!r} classified "
                "widened; refusing to mint unverified authority"
            )
    return warrant
