# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-delegated-warrant-whose-attenuation-proof-verifies-but-whose-revocation-epoch-is-strictly-newer-than-t.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-delegated-warrant-whose-attenuation-proof-verifies-but-whose-revocation-epoch-is-strictly-newer-than-t.warrant.json
# Markdown SHA-256:   582ae654d133149b85ee56c960a73563e0c8d37b90fa2474cdf8046506d72299
# Code AST SHA-256:   278701db14d568be0ae8f61dd57abaa78d19ede7c4518afa85201a3b2155cfbd
# Verified Classes:   WarrantFreshnessError, StaleAttenuationWitnessError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class WarrantFreshnessError(RuntimeError):
    """Raised when a warrant's attenuation proof verifies but epoch liveness cannot be established."""
    pass


class StaleAttenuationWitnessError(RuntimeError):
    """Raised when an attenuation proof is bound to a superseded parent epoch."""
    pass


def authorize_delegated_call(
    warrant,
    epoch_store,
    max_epoch_staleness_s: float,
    now: float,
) -> None:
    # (1) Structural: signatures and attenuation proof.
    if not warrant.verify_signatures():
        raise WarrantFreshnessError("signature chain invalid")
    if not warrant.verify_attenuation_proof():
        raise WarrantFreshnessError("attenuation proof does not verify")

    # (2) Liveness: evidence-based freshness at EVERY hop, not cache fallback.
    for hop in warrant.chain:  # leaf -> root
        assertion = epoch_store.fresh_assertion(hop.issuer_id)
        if assertion is None:
            # No signed, timestamped epoch assertion available.
            raise WarrantFreshnessError(
                f"no fresh epoch assertion for issuer {hop.issuer_id}; "
                f"cache is not authorization evidence"
            )
        if now - assertion.timestamp > max_epoch_staleness_s:
            raise WarrantFreshnessError(
                f"epoch assertion for {hop.issuer_id} stale by "
                f"{now - assertion.timestamp:.3f}s > {max_epoch_staleness_s}s"
            )
        if hop.issuer_epoch < assertion.epoch:
            raise WarrantFreshnessError(
                f"warrant epoch {hop.issuer_epoch} < authoritative epoch "
                f"{assertion.epoch} for {hop.issuer_id}: revoked or superseded"
            )

    # (3) Attenuation witness must be bound to current parent epoch.
    for child, parent in zip(warrant.chain, warrant.chain[1:]):
        fresh_parent_epoch = epoch_store.fresh_assertion(parent.issuer_id).epoch
        if child.proof_parent_epoch < fresh_parent_epoch:
            raise StaleAttenuationWitnessError(
                f"attenuation proof for {child.issuer_id} bound to parent epoch "
                f"{child.proof_parent_epoch} < current {fresh_parent_epoch}; re-mint required"
            )
