# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-resume-a-checkpoint-whose-attestation-epoch-has-advanced.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-resume-a-checkpoint-whose-attestation-epoch-has-advanced.warrant.json
# Markdown SHA-256:   727fbaaaecdc5e50c01668c2c7ac3d2b50e16edaa5f27069fa4f8a53bc9ea50f
# Code AST SHA-256:   b36213059d14fd5c2191f3bfb341548c30f2f79359cf1ad17d8437c0eec53d94
# Verified Classes:   StaleAttestationEpochError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class StaleAttestationEpochError(RuntimeError):
    """Raised when a checkpoint's attestation epoch/measurement does not match
    the current enclave, i.e. resuming would launder stale authority."""


def resume_checkpoint(checkpoint: dict, current_attestation: dict) -> dict:
    """
    Fail-closed resume gate for confidential multi-agent checkpoints.

    checkpoint:          {epoch, measurement_hash, sealing_key_id, warrant_set, state}
    current_attestation: {epoch, measurement_hash, sealing_key_id}

    The seal check is necessary but NOT sufficient. We require measurement and
    epoch equality; a sealing-key match alone is a provenance bypass.
    """
    if checkpoint["measurement_hash"] != current_attestation["measurement_hash"]:
        raise StaleAttestationEpochError(
            f"measurement mismatch: checkpoint={checkpoint['measurement_hash']} "
            f"current={current_attestation['measurement_hash']}; "
            f"re-attestation required, refusing to resume"
        )

    if checkpoint["epoch"] != current_attestation["epoch"]:
        raise StaleAttestationEpochError(
            f"epoch mismatch: checkpoint={checkpoint['epoch']} "
            f"current={current_attestation['epoch']}; "
            f"seal continuity is not provenance, refusing to resume"
        )

    # Only now, with provenance established, may warrants be trusted.
    for warrant in checkpoint.get("warrant_set", []):
        if warrant.get("signer_measurement") != current_attestation["measurement_hash"]:
            raise StaleAttestationEpochError(
                f"warrant signed under stale measurement "
                f"{warrant.get('signer_measurement')}; evict and re-attest"
            )

    return checkpoint["state"]


def guarded_resume(checkpoint: dict, current_attestation: dict) -> dict:
    # Explicitly demonstrate the barrier: a re-wrapped ciphertext under a new
    # sealing key still fails here because measurement/epoch are checked.
    try:
        return resume_checkpoint(checkpoint, current_attestation)
    except StaleAttestationEpochError:
        # Cold-start from a re-attested seed instead of resuming stale state.
        raise StaleAttestationEpochError(
            "cross-epoch resume rejected; cold-start from re-attested seed"
        )
