# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-brokered-capability-attestation-whose-trust-chain-terminates-in-a-foreign-root-it-cannot-re-verify.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-brokered-capability-attestation-whose-trust-chain-terminates-in-a-foreign-root-it-cannot-re-verify.warrant.json
# Markdown SHA-256:   12be68bd190491378fadf52032b38497e4539d42960d28b1b04736b5b7e6f91e
# Code AST SHA-256:   60291933b9fa483e1bbc63177401839729e9c5c0db0d7d7634d431eb0efb6639
# Verified Classes:   UnverifiableAttestation, AttestationBundle
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class UnverifiableAttestation(RuntimeError):
    """Raised when a brokered attestation's authority cannot be established,
    even though its cryptographic integrity is valid."""
    pass


@dataclass
class AttestationBundle:
    root_claim: str            # terminal root asserted by the broker
    root_resolvable_locally: bool  # can C resolve this root WITHOUT the broker?
    issuer: str
    depth: int
    max_depth: int
    issued_at: float
    revocation_last_checked: float
    revocation_max_staleness: float
    sig_valid: bool


def consume_brokered_attestation(bundle: AttestationBundle, now: float | None = None) -> None:
    now = time.time() if now is None else now

    # Integrity gate: necessary, never sufficient.
    if not bundle.sig_valid:
        raise UnverifiableAttestation(f"signature invalid for issuer {bundle.issuer}")

    # Authority gate 1: root must be resolvable out of band.
    if not bundle.root_resolvable_locally:
        raise UnverifiableAttestation(
            f"root {bundle.root_claim!r} is broker-asserted only; "
            f"cannot re-verify foreign root for issuer {bundle.issuer}"
        )

    # Authority gate 2: revocation staleness bound.
    staleness = now - bundle.revocation_last_checked
    if staleness > bundle.revocation_max_staleness:
        raise UnverifiableAttestation(
            f"revocation state for {bundle.issuer} stale by {staleness:.0f}s "
            f"(max {bundle.revocation_max_staleness:.0f}s); authority unestablished"
        )

    # Authority gate 3: delegation depth budget.
    if bundle.depth > bundle.max_depth:
        raise UnverifiableAttestation(
            f"delegation depth {bundle.depth} exceeds budget {bundle.max_depth}"
        )
