# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-commits-a-task-result-but-its-attestation-quorum-is-provably-split-across-two-conflicting-epochs.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-commits-a-task-result-but-its-attestation-quorum-is-provably-split-across-two-conflicting-epochs.warrant.json
# Markdown SHA-256:   ea93ffc17ee33841172d25c7722935a43c3dc37e0a20e9255541f27e651939e3
# Code AST SHA-256:   c38d69ac694e67f34ed31173ea332f70e7f877d3bc98eaae4058041cad978292
# Verified Classes:   CrossEpochDualCommitmentError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CrossEpochDualCommitmentError(RuntimeError):
    """Raised when a task result is attested under two conflicting epochs
    without a quorum-attested abandonment certificate for the earlier epoch."""
    pass


def accept_attestation(
    task_id: str,
    result_hash: str,
    epoch: int,
    last_signed_epoch: dict,
    abandonment_certificates: dict,
    committed_results: dict,
) -> None:
    """Validate an incoming attestation before signing or recording it.

    Enforces two invariants:
      1. Local monotonicity: a node never signs an attestation for an epoch
         <= the highest epoch it has already signed for this task.
      2. Cross-epoch safety: a conflicting result for an already-committed
         task is rejected unless a quorum-attested abandonment certificate
         exists for the epoch of the prior commit.
    """
    prior_epoch = last_signed_epoch.get(task_id)
    if prior_epoch is not None and epoch <= prior_epoch:
        raise CrossEpochDualCommitmentError(
            f"stale-epoch attestation for task={task_id} epoch={epoch} "
            f"<= last_signed_epoch={prior_epoch}"
        )

    committed = committed_results.get(task_id)
    if committed is not None:
        prior_hash, prior_commit_epoch = committed
        if prior_hash != result_hash:
            cert = abandonment_certificates.get((task_id, prior_commit_epoch))
            if cert is None or not cert.quorum_valid:
                raise CrossEpochDualCommitmentError(
                    f"conflicting result for task={task_id}: committed "
                    f"{prior_hash}@{prior_commit_epoch}, received "
                    f"{result_hash}@{epoch} with no valid abandonment certificate"
                )

    last_signed_epoch[task_id] = epoch
