# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-re-verify-a-peer-s-attestation-mid-task-the-validity-window-cliff-in-continuous-agent-identity.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-re-verify-a-peer-s-attestation-mid-task-the-validity-window-cliff-in-continuous-agent-identity.warrant.json
# Markdown SHA-256:   ffd2aa7da146109e1372697d805013b526763e64172aa4d76a83625845a3751c
# Code AST SHA-256:   192edffe037cbf84e30c0b5edee1677630d8274502248c816a86772af62b1cde
# Verified Classes:   AttestationStaleError, Attestation
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationStaleError(RuntimeError):
    """Raised when a cached attestation no longer represents current authority."""
    pass


@dataclass(frozen=True)
class Attestation:
    subject: str
    scope: frozenset
    not_before: float
    not_after: float
    revocation_epoch: int
    max_action_duration: float


def verify_for_action(
    att: Attestation,
    action_scope: str,
    expected_duration: float,
    now: float,
    issuer_current_epoch: int,
    peer_current_scope: frozenset,
    epoch_view_fresh: bool,
) -> None:
    # 1. Validity window, evaluated at the action, not the handshake.
    if now < att.not_before or now >= att.not_after:
        raise AttestationStaleError(
            f"attestation for {att.subject} outside window at action time: "
            f"now={now} window=[{att.not_before},{att.not_after})"
        )

    # 2. Revocation epoch, with a fail-closed guard on view freshness.
    if not epoch_view_fresh:
        raise AttestationStaleError(
            "cannot establish current revocation epoch is fresher than now; "
            "failing closed"
        )
    if att.revocation_epoch < issuer_current_epoch:
        raise AttestationStaleError(
            f"attestation epoch {att.revocation_epoch} < issuer epoch "
            f"{issuer_current_epoch}: authority revoked since minting"
        )

    # 3. Scope drift: cached scope must match the peer's currently bound scope.
    if action_scope not in att.scope:
        raise AttestationStaleError(
            f"action {action_scope!r} not in attested scope {sorted(att.scope)}"
        )
    if att.scope != peer_current_scope:
        raise AttestationStaleError(
            f"scope drift: attested {sorted(att.scope)} != "
            f"current {sorted(peer_current_scope)}"
        )

    # 4. Action-duration bound: a step that outlives the attestation
    #    must re-verify before executing, not after.
    if expected_duration > att.max_action_duration:
        raise AttestationStaleError(
            f"action duration {expected_duration}s exceeds attested "
            f"max_action_duration {att.max_action_duration}s; re-verify "
            f"before commit"
        )


def execute_write(att, peer_scope, issuer_epoch, epoch_fresh, payload):
    # Re-verify at the action boundary, not the task boundary.
    verify_for_action(
        att=att,
        action_scope="write_report",
        expected_duration=2.0,
        now=time.time(),
        issuer_current_epoch=issuer_epoch,
        peer_current_scope=peer_scope,
        epoch_view_fresh=epoch_fresh,
    )
    return {"committed": True, "payload": payload,
            "ran_under_epoch": att.revocation_epoch}
