# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-planner-has-crossed-from-partial-observability-into-byzantine-equivocation-territory.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-planner-has-crossed-from-partial-observability-into-byzantine-equivocation-territory.warrant.json
# Markdown SHA-256:   3574f75f63d5d7a1e9ac52fbf7549a2cf7a2c3240451d802f953bde530a44a56
# Code AST SHA-256:   08f1179ddb7b34c8d046057a17e735aef85554048d89d32c0386e52a0ee98e4a
# Verified Classes:   ByzantineEquivocationError, FederationHaltedError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ByzantineEquivocationError(RuntimeError):
    """Raised when a claim pair shares versioned provenance but is jointly unsatisfiable."""
    pass


class FederationHaltedError(RuntimeError):
    """Raised when quorum cannot be safely recomputed after eviction (fail-closed)."""
    pass


def reconcile_claims(claims, satisfiable, quorum_fn):
    """
    claims: list of dicts with keys: agent, slot, value, provenance (frozenset of versioned obs ids)
    satisfiable: callable(union_of_evidence) -> bool, world-model consistency check
    quorum_fn: callable(live_agents, faulty_count) -> required_quorum
    """
    flagged_agents = set()
    per_slot = {}
    for c in claims:
        per_slot.setdefault(c["slot"], []).append(c)

    for slot, slot_claims in per_slot.items():
        for i in range(len(slot_claims)):
            for j in range(i + 1, len(slot_claims)):
                a, b = slot_claims[i], slot_claims[j]
                if a["agent"] == b["agent"]:
                    continue
                same_prov = a["provenance"] == b["provenance"]
                contradict = a["value"] != b["value"]
                if same_prov and contradict:
                    union = a["provenance"] | b["provenance"]
                    if not satisfiable(union):
                        # Both claims came from the same agent pair-flag the emitter.
                        # In practice the emitter is the agent appearing in both
                        # contradictory claims; here we flag both endpoints' shared
                        # source by provenance, conservatively flagging the agent
                        # whose provenance appears on the unsatisfiable side.
                        flagged_agents.add(a["agent"])
                        flagged_agents.add(b["agent"])
                        raise ByzantineEquivocationError(
                            f"slot {slot}: agents {a['agent']},{b['agent']} "
                            f"share provenance {sorted(union)} but claims "
                            f"{a['value']!r} vs {b['value']!r} are jointly unsatisfiable"
                        )

        live = {c["agent"] for c in slot_claims} - flagged_agents
        f = len(flagged_agents)
        required = quorum_fn(len(live), f)
        if len(live) < required:
            raise FederationHaltedError(
                f"slot {slot}: only {len(live)} live agents after eviction, "
                f"need {required} (f={f}); refusing to decide"
            )
    return {"flagged": flagged_agents, "decided_slots": list(per_slot)}
