# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-of-a-multi-agent-task-commitment-protocol-draw-the-line-between-result-disagreement-and-commitment-divergence-and-why-the-equiv.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-of-a-multi-agent-task-commitment-protocol-draw-the-line-between-result-disagreement-and-commitment-divergence-and-why-the-equiv.warrant.json
# Markdown SHA-256:   d83bf78b41ec1af283e02782138a00f2fa8795731ba814c5a293bcfb16bd2610
# Code AST SHA-256:   fc41b9f20db9d6e2ad937e2f8bd066e5f3d709adeecfa44eb956d3e5e561711e
# Verified Classes:   PhantomCommitError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class PhantomCommitError(RuntimeError):
    """Raised when a result attestation quorum could overlap a commitment
    quorum only in faulty agents — i.e., the phantom-commit window."""
    pass


def validate_result_quorum(n: int, f: int, q_c: int, q_r: int,
                           attestation_requires_certificate: bool,
                           proposals_reliably_broadcast: bool) -> None:
    # Honest-overlap floor: the smallest intersection of a q_c-quorum and a
    # q_r-quorum that is guaranteed to contain at least one honest agent.
    honest_overlap = q_c + q_r - n
    if honest_overlap <= f:
        # The two quorums can intersect only in faulty agents.
        if not (attestation_requires_certificate and proposals_reliably_broadcast):
            raise PhantomCommitError(
                f"phantom-commit window open: n={n} f={f} q_c={q_c} q_r={q_r} "
                f"honest_overlap={honest_overlap} <= f={f}, and neither "
                f"certificate-binding nor reliable-broadcast is in place"
            )
    # Even with mitigations, q_r below the strict-safe bound is a latent risk
    # if either mitigation is later removed by a config change.
    if q_r < n - f + 1 and q_r <= n - q_c + f:
        raise PhantomCommitError(
            f"q_r={q_r} sits in the conditionally-safe gap "
            f"({n - q_c + f} < q_r < {n - f + 1}); require both "
            f"certificate-binding and reliable-broadcast, and pin them"
        )


def accept_result(n: int, f: int, q_c: int, q_r: int,
                  attestations: int, has_commitment_certificate: bool,
                  reliable_broadcast: bool) -> bool:
    validate_result_quorum(n, f, q_c, q_r,
                           attestation_requires_certificate=has_commitment_certificate,
                           proposals_reliably_broadcast=reliable_broadcast)
    if attestations < q_r:
        return False
    if not has_commitment_certificate and q_r <= n - q_c + f:
        raise PhantomCommitError(
            "attestation quorum reached but commitment certificate absent "
            "in phantom-commit window; refusing to finalize"
        )
    return True
