# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-attenuates-a-capability-but-the-warrant-chain-re-widens-at-a-federated-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-attenuates-a-capability-but-the-warrant-chain-re-widens-at-a-federated-trust-boundary.warrant.json
# Markdown SHA-256:   3dd93f004f4a052973e4df1e1d232bd7928f0371e124ff3fce9faf6b90cac46a
# Code AST SHA-256:   f6cffd121754199725bf949a6be74e4ad1b6375a2631130bd7db4a62276a354f
# Verified Classes:   WarrantAttenuationError, WarrantLink
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class WarrantAttenuationError(RuntimeError):
    """Raised when a federated warrant chain re-widens a capability at a hop."""


@dataclass(frozen=True)
class WarrantLink:
    issuer: str
    subject: str
    transfer_ceiling_usd: Optional[float]  # None = unbounded (never trust this)
    scope_account: str


def effective_transfer_ceiling(chain: list[WarrantLink]) -> float:
    """Compute the tightest transfer ceiling across the whole chain.

    The invariant: effective capability is the intersection over all links,
    NOT the leaf link's declared bound. A None ceiling is treated as +inf
    so it cannot widen the intersection, but a chain that is *entirely*
    unbounded is rejected as unsafe.
    """
    if not chain:
        raise WarrantAttenuationError("empty warrant chain")

    ceiling = float("inf")
    saw_bounded = False
    for link in chain:
        if link.transfer_ceiling_usd is not None:
            saw_bounded = True
            ceiling = min(ceiling, link.transfer_ceiling_usd)

    if not saw_bounded:
        raise WarrantAttenuationError(
            "chain has no bounded transfer ceiling; refusing unbounded federation grant"
        )
    return ceiling


def authorize_transfer(chain: list[WarrantLink], amount_usd: float) -> None:
    """Authorize a transfer against the chain intersection, clamping to it.

    Raises WarrantAttenuationError if the leaf link declares a bound looser
    than the chain intersection (indicating a re-widening re-issuer), or if
    the requested amount exceeds the effective ceiling.
    """
    effective = effective_transfer_ceiling(chain)
    leaf = chain[-1]

    if leaf.transfer_ceiling_usd is not None and leaf.transfer_ceiling_usd > effective:
        # The leaf claims more than the chain permits: re-widening detected.
        # Clamp to effective; flag the leaf for remediation.
        raise WarrantAttenuationError(
            f"capability re-widening at leaf: leaf declares "
            f"{leaf.transfer_ceiling_usd} but chain intersection is {effective}"
        )

    if amount_usd > effective:
        raise WarrantAttenuationError(
            f"transfer {amount_usd} exceeds effective chain ceiling {effective}"
        )


# --- Trigger trace from the observed incident -------------------------------
chain = [
    WarrantLink("orgA", "orgB", 2000.0, "account:vendor-42"),   # A attenuates to 2000
    WarrantLink("orgB", "orgC", 5000.0, "account:vendor-42"),   # B re-derives from local default: 5000
]
# authorize_transfer(chain, 4800.0) would have executed under per-link validation.
# Under chain intersection it raises:
try:
    authorize_transfer(chain, 4800.0)
except WarrantAttenuationError as exc:
    print(f"BLOCKED: {exc}")
