# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegated-sub-agent-fan-out-from-leaking-residency-bound-payloads-across-federation-boundaries-when-policy-attenuation-is-eva.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegated-sub-agent-fan-out-from-leaking-residency-bound-payloads-across-federation-boundaries-when-policy-attenuation-is-eva.warrant.json
# Markdown SHA-256:   edb02b2353b0f78de9bb226db37aa6032640a09c1a16a9143c1d54cc729b1ff7
# Code AST SHA-256:   4425a3c0ede38f560fdc4f807b926e89a86d6ab3869b709fffee9332b1c1c6ef
# Verified Classes:   ResidencyAttenuationError, Warrant, Envelope
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ResidencyAttenuationError(RuntimeError):
    """Raised when a hop's derived constraint set is not a superset of the
    inherited warrant's constraint set, or when provenance cannot be carried."""
    pass


@dataclass(frozen=True)
class Warrant:
    allowed_regions: FrozenSet[str]
    required_provenance: FrozenSet[str]
    parent_hash: str | None = None
    ttl_s: int = 900


@dataclass(frozen=True)
class Envelope:
    declared_type: str
    carried_provenance: FrozenSet[str]
    representable_provenance: FrozenSet[str] = field(default_factory=frozenset)


def attenuate(parent: Warrant, envelope: Envelope, next_hop_regions: FrozenSet[str]) -> Warrant:
    # Precondition: the hop's envelope must be able to carry every provenance
    # tag the parent warrant requires. If not, refuse; do not drop the tag.
    missing = parent.required_provenance - envelope.representable_provenance
    if missing:
        raise ResidencyAttenuationError(
            f"hop cannot represent provenance {sorted(missing)}; "
            f"refuse fan-out and defer to issuer for re-issuance"
        )

    # Constraint set may only tighten: allowed_regions is intersected,
    # required_provenance is unioned. Never replace with locally-derived set.
    derived_regions = parent.allowed_regions & next_hop_regions
    derived_prov = parent.required_provenance | envelope.carried_provenance

    if not derived_regions <= parent.allowed_regions:
        raise ResidencyAttenuationError("attenuation widened allowed_regions")
    if not parent.required_provenance <= derived_prov:
        raise ResidencyAttenuationError("attenuation dropped required provenance")

    return Warrant(
        allowed_regions=derived_regions,
        required_provenance=derived_prov,
        parent_hash=hash((parent, envelope)),
        ttl_s=parent.ttl_s,
    )


def admit(hop_warrant: Warrant, chain_root: Warrant) -> None:
    # Transitive superset check against the root, not just the immediate parent.
    if not chain_root.allowed_regions >= hop_warrant.allowed_regions:
        raise ResidencyAttenuationError(
            f"hop allowed_regions {sorted(hop_warrant.allowed_regions)} "
            f"not a subset of root {sorted(chain_root.allowed_regions)}"
        )
    if not chain_root.required_provenance <= hop_warrant.required_provenance:
        raise ResidencyAttenuationError("root provenance tag lost across chain")
