# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-grant-whose-attestation-chain-re-anchors-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-grant-whose-attestation-chain-re-anchors-mid-handoff.warrant.json
# Markdown SHA-256:   6086b825af20c6694e6f1cb6c610a4903c4728422a26fa9fa84c82b90e54f824
# Code AST SHA-256:   832a08fccf92c0559dcff6f76efb33c0a1cb3f3f32aaf216a23a3cb92e9ad7cb
# Verified Classes:   AnchorContinuityViolation, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class AnchorContinuityViolation(RuntimeError):
    """Raised when a re-anchored warrant grants authority the original
    delegator never conferred, even though every signature verifies."""


@dataclass(frozen=True)
class Warrant:
    capability: str
    issuer_root: str                      # terminal root of THIS chain
    anchor_constraint: FrozenSet[str]     # carried, signed, monotone
    depth: int
    signature_valid: bool = True


def verify_and_exercise(
    warrant: Warrant,
    local_trust_store: FrozenSet[str],
    original_constraint: FrozenSet[str],
) -> None:
    if not warrant.signature_valid:
        raise AnchorContinuityViolation(
            f"signature invalid on {warrant.capability}"
        )

    if warrant.issuer_root not in local_trust_store:
        raise AnchorContinuityViolation(
            f"root {warrant.issuer_root} not in local trust store"
        )

    # The invariant that re-anchoring silently breaks:
    if warrant.issuer_root not in original_constraint:
        raise AnchorContinuityViolation(
            f"re-anchor detected: capability {warrant.capability!r} "
            f"exercised under root {warrant.issuer_root!r}, but original "
            f"delegator constrained anchors to {sorted(original_constraint)}"
        )

    # Constraint set must only narrow across hops, never widen.
    if not warrant.anchor_constraint.issubset(original_constraint):
        raise AnchorContinuityViolation(
            f"constraint widened on re-wrap: {sorted(warrant.anchor_constraint)} "
            f"is not a subset of {sorted(original_constraint)}"
        )

    # ... proceed to exercise the capability


def exercise_federated_grant(
    warrant: Warrant,
    local_trust_store: FrozenSet[str],
    original_constraint: FrozenSet[str],
) -> None:
    try:
        verify_and_exercise(warrant, local_trust_store, original_constraint)
    except AnchorContinuityViolation:
        # Refuse; do not downgrade to a warning. The capability is
        # unauthorized by construction, not merely suspect.
        raise


# Reproduces the observed trace: B re-anchors A's grant under B_root.
if __name__ == "__main__":
    reanchored = Warrant(
        capability="write:ledger",
        issuer_root="B_root",                    # re-anchored
        anchor_constraint=frozenset({"B_root"}), # widened / substituted
        depth=2,
        signature_valid=True,
    )
    exercise_federated_grant(
        warrant=reanchored,
        local_trust_store=frozenset({"B_root", "C_root"}),
        original_constraint=frozenset({"A_root"}),
    )
