# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-distrust-a-unanimous-agent-quorum-when-byzantine-resilient-aggregation-silently-collapses-under-correlated-vote-lau.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-distrust-a-unanimous-agent-quorum-when-byzantine-resilient-aggregation-silently-collapses-under-correlated-vote-lau.warrant.json
# Markdown SHA-256:   58ccd3f2d94dbe1fc17f08c68906b3ff92e3cf1ae13a338a53b8a444ec31a5e5
# Code AST SHA-256:   52b7004d2740f3ea9912ca3c701365efe92d4973c5296c48349b802dac9903e7
# Verified Classes:   CorrelatedQuorumError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CorrelatedQuorumError(RuntimeError):
    """Raised when a BFT quorum's majority is numerically valid but shares a single evidence lineage."""
    pass


def validate_quorum(votes, input_lineages, fault_budget):
    """
    votes: dict[agent_id -> bool]  (True = approve)
    input_lineages: dict[agent_id -> frozenset[str]]  distinct causal input paths per voter
    fault_budget: int  f in n >= 3f + 1
    """
    approvers = [a for a, v in votes.items() if v]
    n = len(votes)

    if n < 3 * fault_budget + 1:
        raise CorrelatedQuorumError(
            f"swarm size {n} below classical bound for f={fault_budget}"
        )

    if len(approvers) <= n // 2:
        return "reject"  # no majority; ordinary path

    # Independence precondition: distinct causal evidence paths backing the majority
    majority_paths = set().union(*(input_lineages[a] for a in approvers))
    if len(majority_paths) <= fault_budget:
        shared = set.intersection(*(input_lineages[a] for a in approvers))
        raise CorrelatedQuorumError(
            f"majority of {len(approvers)}/{n} backed by only "
            f"{len(majority_paths)} evidence path(s) (f={fault_budget}); "
            f"shared lineage(s)={sorted(shared)} — quorum numerically valid, "
            f"epistemically forged by correlated vote laundering"
        )

    # Single-point-of-correlation test: does removing one shared input flip > f voters?
    for path in majority_paths:
        reliant = [a for a in approvers if path in input_lineages[a]]
        if len(reliant) > fault_budget and len(majority_paths) - 1 <= fault_budget:
            raise CorrelatedQuorumError(
                f"single input {path!r} is load-bearing for {len(reliant)} "
                f"majority voters (> f={fault_budget}); effective independence "
                f"collapses to 1"
            )

    return "accept"


def aggregate(votes, input_lineages, fault_budget):
    try:
        return validate_quorum(votes, input_lineages, fault_budget)
    except CorrelatedQuorumError as e:
        # Escalate rather than apply: preserve dissent, request diversified re-vote
        return {"decision": "escalate", "reason": str(e)}
