# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-byzantine-consensus-collapse-in-federated-agent-planning-under-partial-trust.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-byzantine-consensus-collapse-in-federated-agent-planning-under-partial-trust.warrant.json
# Markdown SHA-256:   e4b133f9dd1241ccf41dba8a31bbb393dd627af1a1ba4eb0779899df2b8fb525
# Code AST SHA-256:   48a6fea1e2c1de7b97d4b3ee76e777f09532f26a6369187d8d21d30ba6830f2a
# Verified Classes:   AttestationTopologyError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class AttestationTopologyError(RuntimeError):
    """Raised when the local attestation graph cannot justify a commit."""
    pass


def verify_commit_eligibility(
    local_fragments: set[str],
    attestations: list[dict],
    global_membership: set[str],
    quorum_q: int,
    highest_epoch_seen: dict[str, int],
    current_epoch: int,
) -> bool:
    # 1. Node-count quorum (necessary, not sufficient)
    if len(local_fragments) < quorum_q:
        raise AttestationTopologyError(
            f"local fragment count {len(local_fragments)} < quorum {quorum_q}"
        )

    # 2. Edge-connectivity to global membership must meet quorum
    reachable = {a["signer"] for a in attestations} | local_fragments
    if len(reachable & global_membership) < quorum_q:
        raise AttestationTopologyError(
            f"edge-connectivity {len(reachable & global_membership)} < quorum {quorum_q}: "
            "topology split suspected"
        )

    # 3. Epoch monotonicity per signer (replay defense)
    for a in attestations:
        signer, epoch = a["signer"], a["epoch"]
        if epoch <= highest_epoch_seen.get(signer, -1):
            raise AttestationTopologyError(
                f"non-monotonic epoch from {signer}: {epoch} <= "
                f"{highest_epoch_seen.get(signer)} (replay or stale cross-component edge)"
            )
        if epoch > current_epoch:
            raise AttestationTopologyError(
                f"future epoch {epoch} from {signer} exceeds current {current_epoch}"
            )

    # 4. Edge symmetry: every attestation must have a counter-edge this epoch
    edges = {(a["signer"], a["target"]) for a in attestations}
    for signer, target in edges:
        if (target, signer) not in edges:
            raise AttestationTopologyError(
                f"asymmetric attestation edge {signer}->{target}: "
                "signature of topology split, commit refused"
            )

    return True
