# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-brokered-capability-attestation-that-outlives-the-policy-epoch-it-was-negotiated-under.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-brokered-capability-attestation-that-outlives-the-policy-epoch-it-was-negotiated-under.warrant.json
# Markdown SHA-256:   cfb8824afdc8025f62d24e7dcc49f405d2b9f40db59f7eed8ef3d6bc9254a6e2
# Code AST SHA-256:   5160583f8a34eef0b865e1840e9c2e69bbab1ca4fe5818dd0e36515b7e0e89d6
# Verified Classes:   StaleAttestationEpoch, Attestation
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class StaleAttestationEpoch(RuntimeError):
    """Raised when an attestation's policy epoch does not match the resource org's current epoch."""
    pass


class Attestation:
    def __init__(self, subject, resource, tier, max_depth, not_after, policy_epoch, signature_ok):
        self.subject = subject
        self.resource = resource
        self.tier = tier
        self.max_depth = max_depth
        self.not_after = not_after
        self.policy_epoch = policy_epoch
        self.signature_ok = signature_ok


def verify_attestation(att: Attestation, now: float, current_epoch: str,
                       requested_tier: int, requested_depth: int) -> None:
    if not att.signature_ok:
        raise StaleAttestationEpoch(f"bad signature for {att.subject} -> {att.resource}")
    if now >= att.not_after:
        raise StaleAttestationEpoch(f"attestation expired at {att.not_after}")
    # The non-obvious conjunct: epoch match at invocation time, not issuance time.
    if att.policy_epoch != current_epoch:
        raise StaleAttestationEpoch(
            f"attestation epoch {att.policy_epoch!r} != resource org current epoch "
            f"{current_epoch!r}; re-negotiate, do not retry"
        )
    if requested_tier > att.tier:
        raise StaleAttestationEpoch(
            f"requested tier {requested_tier} exceeds attested tier {att.tier}"
        )
    if requested_depth > att.max_depth:
        raise StaleAttestationEpoch(
            f"requested depth {requested_depth} exceeds attested max_depth {att.max_depth}"
        )


def invoke_resource(att: Attestation, now: float, current_epoch: str,
                    requested_tier: int, requested_depth: int) -> str:
    verify_attestation(att, now, current_epoch, requested_tier, requested_depth)
    return f"executed {att.resource} at tier {requested_tier} depth {requested_depth}"
