# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-a2a-warrant-whose-aaif-attestation-chain-re-roots-mid-delegation.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-a2a-warrant-whose-aaif-attestation-chain-re-roots-mid-delegation.warrant.json
# Markdown SHA-256:   cffb1fb04b3739a580874fcfc3f1c58b8d809e4db37c8804a94715e02dd3b7d4
# Code AST SHA-256:   ae1134680f507d6c2025ba846089b2e5fa7bed61896f408cb1bf8f424ff1cc59
# Verified Classes:   FederationScopeCollapseError, Scope, AttestationEdge
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class FederationScopeCollapseError(RuntimeError):
    """Raised when an A2A warrant is verified against a re-rooted AAIF
    attestation chain without intersecting cross-cert scope into the
    effective-authority lattice."""


@dataclass(frozen=True)
class Scope:
    ops: frozenset
    commit_cap: int

    def meet(self, other: "Scope") -> "Scope":
        return Scope(self.ops & other.ops, min(self.commit_cap, other.commit_cap))

    def allows(self, requested: "Scope") -> bool:
        return requested.ops <= self.ops and requested.commit_cap <= self.commit_cap


@dataclass(frozen=True)
class AttestationEdge:
    issuer_root: str
    subject_root: str
    cross_cert_scope: Scope | None  # None => native root, no federation edge


def effective_authority(
    warrant_scope: Scope,
    attestation_path: list[AttestationEdge],
    trusted_roots: frozenset,
) -> Scope:
    # Fold every cross-cert edge's scope into the effective-authority meet.
    effective = warrant_scope
    saw_federation_edge = False
    for edge in attestation_path:
        if edge.cross_cert_scope is not None:
            saw_federation_edge = True
            effective = effective.meet(edge.cross_cert_scope)
    terminal = attestation_path[-1].subject_root if attestation_path else None
    if terminal not in trusted_roots and not saw_federation_edge:
        raise FederationScopeCollapseError(
            f"attestation terminates at untrusted root {terminal!r} with no "
            f"federation edge to a trusted root; refuse rather than degrade "
            f"to identity-only trust"
        )
    return effective


def authorize(
    requested: Scope,
    warrant_scope: Scope,
    attestation_path: list[AttestationEdge],
    trusted_roots: frozenset,
) -> bool:
    effective = effective_authority(warrant_scope, attestation_path, trusted_roots)
    if not effective.allows(requested):
        # This is the exact boundary the naive verifier misses: warrant-level
        # attenuation passed, but the cross-cert scope meet did not.
        if warrant_scope.allows(requested):
            raise FederationScopeCollapseError(
                f"requested {requested} passes warrant scope {warrant_scope} "
                f"but violates federated effective authority {effective} "
                f"after cross-cert intersection"
            )
        return False
    return True
