# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-federated-capability-warrant-whose-attestation-chain-crosses-a-trust-broker-that-re-signs-rather-than-forw.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-federated-capability-warrant-whose-attestation-chain-crosses-a-trust-broker-that-re-signs-rather-than-forw.warrant.json
# Markdown SHA-256:   055ddb48dc84a6a6a835122c76e5cb43068e1979df0a1378523bc5ba7d16f3e1
# Code AST SHA-256:   2b1c251fe04e5d7390fd4da821a03605bd116ebe2ddd27b2e1056a91f39c46c6
# Verified Classes:   WarrantScopeAmplificationError, ProvenanceRecord, FederatedWarrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class WarrantScopeAmplificationError(RuntimeError):
    """Raised when a re-signed federated warrant cannot be scope-verified
    against its original issuer's intent, meaning the holder would execute
    under a scope that may exceed what the issuer authorized."""


@dataclass(frozen=True)
class ProvenanceRecord:
    issuer: str
    original_scope: frozenset
    translated_scope: frozenset
    translation_loss: frozenset  # predicates the broker could not represent
    broker_signature_valid: bool


@dataclass(frozen=True)
class FederatedWarrant:
    broker: str
    subject: str
    capability_class: str
    translated_scope: frozenset
    provenance: Optional[ProvenanceRecord]


def authorize_execution(
    warrant: FederatedWarrant,
    holder_policy_permits: frozenset,
    rederive,
) -> frozenset:
    """Return the effective scope the holder may execute under.

    rederive: callable(ProvenanceRecord) -> frozenset | None
    """
    prov = warrant.provenance

    if prov is None:
        raise WarrantScopeAmplificationError(
            f"warrant from broker={warrant.broker!r} has no provenance record; "
            f"chain terminates at broker, issuer intent unrecoverable"
        )

    if not prov.broker_signature_valid:
        raise WarrantScopeAmplificationError(
            f"provenance record for broker={warrant.broker!r} failed signature check"
        )

    if not prov.translation_loss:
        effective = prov.original_scope
    else:
        rederived = rederive(prov)
        if rederived is None:
            raise WarrantScopeAmplificationError(
                f"broker={warrant.broker!r} dropped predicates "
                f"{sorted(prov.translation_loss)} and holder cannot re-derive them; "
                f"refusing rather than executing under widened scope"
            )
        if not rederived.issubset(warrant.translated_scope):
            raise WarrantScopeAmplificationError(
                f"re-derived scope {sorted(rederived)} is not a subset of "
                f"broker-translated scope {sorted(warrant.translated_scope)}; "
                f"broker widened the warrant"
            )
        effective = rederived

    if not effective.issubset(holder_policy_permits):
        raise WarrantScopeAmplificationError(
            f"effective scope {sorted(effective)} exceeds holder policy "
            f"{sorted(holder_policy_permits)}"
        )

    # Cache key MUST be (issuer, original_scope), never (broker, capability_class).
    return effective
