# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-quorum-certificate-the-attestation-commitment-gap-in-bft-multi-agent-task-ledgers.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-quorum-certificate-the-attestation-commitment-gap-in-bft-multi-agent-task-ledgers.warrant.json
# Markdown SHA-256:   952ed5fb6fa1e17efb193c863ddd43cdba33ffbadd94425604779d9dc9bb3a30
# Code AST SHA-256:   0796f3795408ef80e6bd2257588131872fedde0fe8110e4e7d0c202f81a141ac
# Verified Classes:   AttestationCommitmentGapError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class AttestationCommitmentGapError(RuntimeError):
    """Raised when an attestation QC cannot be bound to its commitment QC."""


def validate_attestation(
    commitment_qc: dict,
    attestation_qc: dict,
    current_epoch: int,
    min_honest_intersection: int = 1,
) -> bool:
    commit_epoch = commitment_qc.get("epoch")
    attest_epoch = attestation_qc.get("epoch")
    commit_quorum = set(commitment_qc.get("signers", []))
    attest_quorum = set(attestation_qc.get("signers", []))
    linked_digest = attestation_qc.get("commitment_digest")

    if linked_digest is None:
        if attest_epoch != commit_epoch:
            raise AttestationCommitmentGapError(
                f"attestation epoch {attest_epoch} != commitment epoch "
                f"{commit_epoch} and no digest link present; quorums are disjoint"
            )
        if commit_epoch != current_epoch:
            raise AttestationCommitmentGapError(
                f"unlinked attestation against stale epoch {commit_epoch} "
                f"(current {current_epoch}); re-attestation required"
            )

    if linked_digest is not None and linked_digest != commitment_qc.get("digest"):
        raise AttestationCommitmentGapError(
            "attestation digest does not match commitment digest; certificate mismatch"
        )

    intersection = commit_quorum & attest_quorum
    if len(intersection) < min_honest_intersection:
        raise AttestationCommitmentGapError(
            f"honest quorum intersection {len(intersection)} < "
            f"{min_honest_intersection}; attestation not bound to commitment"
        )

    return True


def accept_task_result(commitment_qc: dict, attestation_qc: dict, current_epoch: int) -> str:
    validate_attestation(commitment_qc, attestation_qc, current_epoch)
    return "ACCEPTED"
