# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-peer-s-verified-identity-claim-across-an-aaif-trust-domain-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-peer-s-verified-identity-claim-across-an-aaif-trust-domain-boundary.warrant.json
# Markdown SHA-256:   6fa6717ad63d6144241c51d8b44f3775c4f269feddf9bd88b34802d778ab6775
# Code AST SHA-256:   fec52d738b9eb079b3450242697c7b8823ec8e1e391c20fa71b33bcc6ca46f08
# Verified Classes:   TrustDomainInheritanceError, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class TrustDomainInheritanceError(RuntimeError):
    """Raised when cross-domain identity inheritance cannot be soundly reduced."""
    pass


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    domain: str
    scope: frozenset
    chain_min_scope: Optional[frozenset]  # None => origin-grant reference only
    chain_length: int
    bridge_ceiling: frozenset


def resolve_inherited_scope(w: Warrant, dest_domain: str) -> frozenset:
    if w.domain == dest_domain:
        return w.scope

    # Cross-domain boundary: the only sound reduction is an inlined,
    # fold-checkable chain minimum.
    if w.chain_min_scope is None:
        if w.chain_length == 1:
            # Single hop: origin grant IS the chain minimum. Safe.
            effective = w.scope
        else:
            raise TrustDomainInheritanceError(
                f"multi-hop warrant from {w.issuer} into {dest_domain} "
                f"references origin grant with chain_length={w.chain_length}; "
                f"intermediate attenuation is unrepresentable — refusing "
                f"inheritance, require re-authorization in {dest_domain}"
            )
    else:
        effective = w.chain_min_scope

    if not effective <= w.bridge_ceiling:
        raise TrustDomainInheritanceError(
            f"claimed scope {sorted(effective)} exceeds bridge ceiling "
            f"{sorted(w.bridge_ceiling)} for {w.domain}->{dest_domain}; "
            f"re-mint widened past bridge — quarantining warrant"
        )
    return effective
