# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-in-a-byzantine-quorum-silently-ratify-a-divergent-output-when-the-auditor-s-vote-arrives-inside-the-same-reconciliation-window-as.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-in-a-byzantine-quorum-silently-ratify-a-divergent-output-when-the-auditor-s-vote-arrives-inside-the-same-reconciliation-window-as.warrant.json
# Markdown SHA-256:   6043cad030c417e703446b6d560bdc70f263518369a19b1408128e7b59de4a38
# Code AST SHA-256:   c8a71669c00d0fc8466168ce8023311aee3c0a4a87027e5c97b02b674762eae3
# Verified Classes:   QuorumAliasingError, TombstonedCandidateError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class QuorumAliasingError(RuntimeError):
    """Raised when a quorum commit aggregates votes across distinct candidate
    identities within a single slot — the temporal-aliasing failure mode."""


class TombstonedCandidateError(QuorumAliasingError):
    """Raised when a vote references a candidate that was superseded by a retry."""


def reconcile_commit(
    task_id: str,
    slot: str,
    vote_ledger: dict,          # {(candidate_hash, auditor_id): vote}
    tombstones: set,            # candidate_hashes superseded by retry
    live_candidates: set,       # candidate_hashes currently live in slot
    quorum_size: int,
) -> str:
    # Project the ledger onto live, non-tombstoned candidates only.
    tally: dict[str, set] = {}
    for (cand_hash, auditor_id) in vote_ledger:
        if cand_hash in tombstones:
            # Do not silently drop — a tombstoned vote is a signal that a
            # retry interleaved with delivery. Surface it.
            raise TombstonedCandidateError(
                f"task={task_id} slot={slot} auditor={auditor_id} "
                f"voted for tombstoned candidate {cand_hash}; "
                f"reconciliation window was open during retry"
            )
        if cand_hash not in live_candidates:
            continue
        tally.setdefault(cand_hash, set()).add(auditor_id)

    winners = [h for h, voters in tally.items() if len(voters) >= quorum_size]
    if len(winners) > 1:
        raise QuorumAliasingError(
            f"task={task_id} slot={slot} multiple candidates reached quorum: "
            f"{winners}; slot projection is unsound"
        )
    if not winners:
        raise QuorumAliasingError(
            f"task={task_id} slot={slot} no candidate reached quorum "
            f"(size={quorum_size}); refusing to commit"
        )

    committed = winners[0]
    # Invariant check: the committed candidate must be the sole live candidate
    # for its slot at commit instant, and its voter set must be hash-keyed.
    if committed not in live_candidates:
        raise QuorumAliasingError(
            f"task={task_id} slot={slot} committed {committed} is not live"
        )
    distinct = len(tally[committed])
    if distinct != quorum_size and distinct < quorum_size:
        raise QuorumAliasingError(
            f"task={task_id} slot={slot} quorum_size={quorum_size} "
            f"but distinct_auditor_hashes={distinct}"
        )
    return committed
