# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-zero-knowledge-capability-proof-whose-scope-predicate-outlives-the-delegation-epoch-it-was-minted-under.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-zero-knowledge-capability-proof-whose-scope-predicate-outlives-the-delegation-epoch-it-was-minted-under.warrant.json
# Markdown SHA-256:   f1a22b9b416e2a54aedc8ce9e2012cab588236327e8291e1bf15d496cc2e6d6c
# Code AST SHA-256:   15d62b67bc2768b866426ab1bbc4e98fc17863425186341315b431e9c1df0c69
# Verified Classes:   DelegationStandingExpired, EpochSkewUnresolved, ScopedToken, EpochAttestation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class DelegationStandingExpired(RuntimeError):
    """Raised when a proof verifies but the delegator's authority has lapsed."""


class EpochSkewUnresolved(RuntimeError):
    """Raised when verifiers disagree on the current epoch floor."""


@dataclass(frozen=True)
class ScopedToken:
    subject: str
    scope_prefix: str
    minted_at_epoch: int
    not_after_epoch: int
    delegator_standing_expiry_epoch: int


@dataclass(frozen=True)
class EpochAttestation:
    epoch_floor: int
    signed_by_quorum: bool


def authorize_tool_call(
    token: ScopedToken,
    path: str,
    attestation: EpochAttestation,
    proof_verifies: bool,
) -> bool:
    if not attestation.signed_by_quorum:
        raise EpochSkewUnresolved(
            f"epoch floor {attestation.epoch_floor} lacks quorum signature; "
            "verifiers may disagree on current epoch"
        )

    if not proof_verifies:
        return False

    if not path.startswith(token.scope_prefix):
        return False

    # The boundary that ZK proofs are structurally silent on:
    # proof validity does not imply delegator standing at use-time.
    if attestation.epoch_floor > token.delegator_standing_expiry_epoch:
        raise DelegationStandingExpired(
            f"token minted at epoch {token.minted_at_epoch}, "
            f"delegator standing expired at epoch "
            f"{token.delegator_standing_expiry_epoch}, "
            f"use-time epoch floor {attestation.epoch_floor}"
        )

    if attestation.epoch_floor > token.not_after_epoch:
        raise DelegationStandingExpired(
            f"token not_after_epoch {token.not_after_epoch} < "
            f"use-time epoch floor {attestation.epoch_floor}"
        )

    return True
