# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-byzantine-quorum-threshold-as-a-function-of-agent-nondeterminism-rather-than-a-fixed-constant.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-byzantine-quorum-threshold-as-a-function-of-agent-nondeterminism-rather-than-a-fixed-constant.warrant.json
# Markdown SHA-256:   2bc797eb51a8c3b4943b240bed461d96b41326856d918194e64a358eba4946dc
# Code AST SHA-256:   9b1213cc17c3f641e6e81e563cd48f71e7c7ecba2a439eb8fb4a77b25a1d45ab
# Verified Classes:   QuorumInstabilityError, VoteAudit
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import math
from dataclasses import dataclass


class QuorumInstabilityError(RuntimeError):
    """Raised when a decision is certified below the nondeterminism-adjusted threshold."""
    pass


@dataclass
class VoteAudit:
    n: int                 # total agents
    f: int                 # assumed classical Byzantine faults
    p_flip: float          # measured per-agent vote-flip probability
    rho: float             # measured inter-agent vote correlation
    z: float = 1.65        # one-sided confidence multiplier (5% tolerated flip rate)


def effective_voters(n: int, rho: float) -> float:
    if rho <= 0.0:
        return float(n)
    return n / (1.0 + (n - 1) * rho)


def adjusted_quorum(audit: VoteAudit) -> int:
    n_eff = effective_voters(audit.n, audit.rho)
    sigma = math.sqrt(max(n_eff * audit.p_flip * (1.0 - audit.p_flip), 0.0))
    base = (n_eff + audit.f + 1) / 2.0
    return math.ceil(base + audit.z * sigma)


def certify_decision(accept_count: int, audit: VoteAudit) -> str:
    q_star = adjusted_quorum(audit)
    sigma = math.sqrt(max(effective_voters(audit.n, audit.rho)
                          * audit.p_flip * (1.0 - audit.p_flip), 0.0))
    undecided_band = audit.z * sigma

    if abs(accept_count - q_star) <= undecided_band:
        # Inside the noise band: do not certify; force a decorrelated re-sample.
        return "UNDECIDED_RERUN"

    if accept_count >= q_star:
        return "COMMIT"

    # Below the adjusted threshold with margin: this is a hard safety violation
    # if the caller still tries to treat it as a valid abort on a prior COMMIT.
    raise QuorumInstabilityError(
        f"accept_count={accept_count} below adjusted quorum q*={q_star} "
        f"(n_eff={effective_voters(audit.n, audit.rho):.2f}, "
        f"p_flip={audit.p_flip:.2f}, rho={audit.rho:.2f}); "
        f"decision cannot be certified as honest-majority-supported"
    )


# Observed deployment: n=7, f=2, fixed q=5 was unsafe.
audit = VoteAudit(n=7, f=2, p_flip=0.18, rho=0.4)
print("adjusted q* =", adjusted_quorum(audit))   # -> 6, not 5
print(certify_decision(5, audit))                 # -> UNDECIDED_RERUN
print(certify_decision(6, audit))                 # -> COMMIT
certify_decision(3, audit)                        # raises QuorumInstabilityError
