# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-authority-inflation-when-a-federated-agent-s-brokered-capability-set-is-re-translated-across-three-or-more-trust-domai.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-authority-inflation-when-a-federated-agent-s-brokered-capability-set-is-re-translated-across-three-or-more-trust-domai.warrant.json
# Markdown SHA-256:   d58da9d622e757cb6b0d4c66646ee498a2b8460d83f3c988e76e6a71c72c02c9
# Code AST SHA-256:   cfe7f132fd6c349511794a86abe22fe77b772aa10f3f96794c3af2967a16483a
# Verified Classes:   AuthorityInflationError, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet, List, Callable


class AuthorityInflationError(RuntimeError):
    """Raised when a federated capability chain would grant authority
    not present in the root warrant, due to non-injective translation
    or nearest-match widening across >= 3 hops."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    issuer: str
    scope: FrozenSet[str]
    root_warrant_id: str
    translation_chain: List[str] = field(default_factory=list)


def translate_filter(
    upstream: Warrant,
    downstream_taxonomy: FrozenSet[str],
    broker_id: str,
) -> Warrant:
    """Correct translation: restrict to capabilities present in the
    downstream taxonomy. Capabilities with no exact match are DROPPED,
    not approximated. If the drop is non-empty and the caller requires
    fidelity, the delegation must fail rather than silently narrow."""
    mapped = upstream.scope & downstream_taxonomy
    dropped = upstream.scope - downstream_taxonomy
    if dropped:
        # Fail-closed: refuse to emit a warrant that lost capabilities
        # silently. A dropped capability may reappear downstream under a
        # stronger primitive (the observed failure mode).
        raise AuthorityInflationError(
            f"broker {broker_id}: capabilities {sorted(dropped)} have no "
            f"exact counterpart in downstream taxonomy; refusing to "
            f"approximate (nearest-match widening is forbidden)"
        )
    return Warrant(
        warrant_id=f"{broker_id}:{upstream.warrant_id}",
        issuer=broker_id,
        scope=mapped,
        root_warrant_id=upstream.root_warrant_id,
        translation_chain=upstream.translation_chain + [broker_id],
    )


def executor_guard(
    incoming: Warrant,
    root_warrant: Warrant,
    action_scope: FrozenSet[str],
    transitive_image: Callable[[Warrant, List[str]], FrozenSet[str]],
) -> None:
    """Root-anchored check at the executor, the last point before effect.
    Recomputes the transitive image of the root warrant along the recorded
    chain and refuses any action outside it."""
    if incoming.root_warrant_id != root_warrant.warrant_id:
        raise AuthorityInflationError(
            f"root warrant mismatch: incoming claims {incoming.root_warrant_id}, "
            f"executor holds {root_warrant.warrant_id}"
        )
    image = transitive_image(root_warrant, incoming.translation_chain)
    if not action_scope <= image:
        raise AuthorityInflationError(
            f"authority inflation: action scope {sorted(action_scope)} "
            f"exceeds transitive image {sorted(image)} of root warrant "
            f"{root_warrant.warrant_id} across chain {incoming.translation_chain}"
        )
