# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-a2a-peer-key-arrives-mid-handoff-and-the-trust-mesh-has-not-yet-converged.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-a2a-peer-key-arrives-mid-handoff-and-the-trust-mesh-has-not-yet-converged.warrant.json
# Markdown SHA-256:   be2b7eceadbcc5faff537ebcc2a782fccf66ba7787840f564cabc28b68ba0878
# Code AST SHA-256:   3cbfe413a1f8977706a831f3380bfda847944a33cabd3c0d25c113c08d4a7678
# Verified Classes:   EpochDivergenceError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class EpochDivergenceError(RuntimeError):
    """Raised when a warrant's signing key status cannot be confirmed at the acting agent's convergence epoch."""
    pass


def validate_warrant(warrant, agent_epoch, revocation_set, last_sync_time, now, max_staleness_s):
    key_id = warrant.signing_key_id

    # Hard revocation: converged, no ambiguity.
    if key_id in revocation_set:
        raise EpochDivergenceError(
            f"warrant {warrant.id} signed by revoked key {key_id} at epoch {agent_epoch}"
        )

    # Key absent from revocation set, but is our epoch fresh enough to trust that absence?
    staleness = (now - last_sync_time).total_seconds()
    remaining_validity = (warrant.not_after - now).total_seconds()

    if staleness > min(max_staleness_s, remaining_validity):
        # Indeterminate: we cannot confirm non-revocation within the warrant's own validity window.
        raise EpochDivergenceError(
            f"warrant {warrant.id}: key {key_id} status indeterminate "
            f"(agent_epoch={agent_epoch}, staleness={staleness:.2f}s, "
            f"remaining_validity={remaining_validity:.2f}s); force sync before acting"
        )

    return {"warrant_id": warrant.id, "key_id": key_id, "agent_epoch": agent_epoch, "decision": "accept"}


def reconcile(accept_record, reject_record):
    # An upstream accept is unsound if the accepting agent's epoch predates the revocation epoch.
    if accept_record["agent_epoch"] < reject_record["revocation_epoch"]:
        raise EpochDivergenceError(
            f"unsound acceptance of {accept_record['warrant_id']} at epoch "
            f"{accept_record['agent_epoch']} < revocation epoch {reject_record['revocation_epoch']}; "
            f"roll back to pre-acceptance checkpoint"
        )
    return "consistent"
