# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-authority-inflation-when-federated-agents-re-derive-policy-across-trust-boundaries.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-authority-inflation-when-federated-agents-re-derive-policy-across-trust-boundaries.warrant.json
# Markdown SHA-256:   f14e5747082914cf86b851b51ddc529d38529f8874abd2d49d7179339cbd0a59
# Code AST SHA-256:   177a5dd45c6430c8e094e6dbdbe7de3219c2f59077a4e45044b35f41f08c28f5
# Verified Classes:   AuthorityInflationError, UnmappableAuthorityFieldError, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any


class AuthorityInflationError(RuntimeError):
    """Raised when a translated warrant grants authority not present in its origin."""
    pass


class UnmappableAuthorityFieldError(AuthorityInflationError):
    """Raised when an authority-bearing constraint has no sound target-dialect mapping."""
    pass


@dataclass(frozen=True)
class Warrant:
    op: str
    resource: str
    constraints: dict[str, Any]
    purpose: str
    lineage: tuple["Warrant", ...] = field(default_factory=tuple)


AUTHORITY_BEARING = {"resource", "purpose", "op", "region", "data_class", "subject"}


def translate(origin: Warrant, target_dialect: dict[str, Any]) -> Warrant:
    """Translate an origin warrant into a target dialect, enforcing monotonicity.

    Refuses (never widens) when an authority-bearing field has no sound mapping.
    """
    mapped: dict[str, Any] = {}
    for key, value in origin.constraints.items():
        if key in target_dialect:
            mapped[key] = value
        elif key in AUTHORITY_BEARING:
            # No sound target-dialect equivalent for an authority-bearing field.
            raise UnmappableAuthorityFieldError(
                f"constraint '{key}={value}' is authority-bearing but has no "
                f"mapping in target dialect; refusing handoff rather than widening"
            )
        # else: non-authority-bearing, safe to drop-and-log

    translated = Warrant(
        op=origin.op,
        resource=origin.resource,
        constraints=mapped,
        purpose=target_dialect.get("purpose_map", {}).get(origin.purpose, origin.purpose),
        lineage=origin.lineage + (origin,),
    )
    return translated


def verify_chain_monotonic(leaf: Warrant, reference_engine) -> None:
    """Replay the full lineage against a union-dialect engine; assert subset authority."""
    origin = leaf.lineage[0] if leaf.lineage else leaf
    origin_authority = reference_engine.effective_authority(origin)
    leaf_authority = reference_engine.effective_authority(leaf)
    if not leaf_authority.issubset(origin_authority):
        excess = leaf_authority - origin_authority
        raise AuthorityInflationError(
            f"translated warrant exceeds origin authority by {excess}; "
            f"chain of {len(leaf.lineage)} hops is non-monotonic"
        )
