# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-equivocating-peers-when-federated-consensus-commits-to-a-task-state-hash-the-local-agent-never-evaluated.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-equivocating-peers-when-federated-consensus-commits-to-a-task-state-hash-the-local-agent-never-evaluated.warrant.json
# Markdown SHA-256:   b20438b8fc7e3f96559707e239073f67f3442199da3831704d8f2f4ebb4bc6ac
# Code AST SHA-256:   8ae36b4081b091da2cd39590037bcd30c7566cc8697e31691ea5a68d501b282b
# Verified Classes:   UnverifiableCommitmentError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class UnverifiableCommitmentError(RuntimeError):
    """Raised when an instance is asked to vote on a state hash it cannot evaluate
    and cannot retroactively falsify, violating the evaluability/retention invariant."""
    pass


def vote_on_transition(
    pre_state: dict | None,
    transition_inputs: dict,
    proposed_hash: str,
    certificate_log: list,
    challenge_window: int,
    determinism_proof: dict | None = None,
) -> str:
    """
    Gate a BFT vote on a proposed state-hash commit.

    Returns 'vote' if the instance can establish validity (by re-execution or by
    committed-input determinism proof) OR can retain enough to falsify later.
    Raises UnverifiableCommitmentError otherwise.
    """
    # Path 1: full re-execution possible -> validity established directly.
    if pre_state is not None and _can_reexecute(transition_inputs):
        derived = _execute(pre_state, transition_inputs)
        if _hash(derived) != proposed_hash:
            # We CAN evaluate and it diverges: this is a detected equivocation.
            raise UnverifiableCommitmentError(
                f"local execution of transition yields {_hash(derived)}, "
                f"proposed {proposed_hash}; refusing vote and flagging equivocation"
            )
        return "vote"

    # Path 2: deterministic-given-inputs with a committed proof -> checkable.
    if determinism_proof is not None and _verify_determinism(transition_inputs, determinism_proof):
        return "vote"

    # Path 3: cannot evaluate now; can we retain enough to falsify later?
    if pre_state is None:
        raise UnverifiableCommitmentError(
            "no local pre-state and no determinism proof: instance can neither "
            "evaluate the transition now nor falsify a conflicting certificate later"
        )
    if len(certificate_log) < challenge_window:
        raise UnverifiableCommitmentError(
            f"certificate log depth {len(certificate_log)} < challenge window "
            f"{challenge_window}: pruning would destroy equivocation evidence; refuse vote"
        )

    # Retain pre-state + inputs + certificate for retroactive adjudication.
    _retain_for_challenge(pre_state, transition_inputs, proposed_hash)
    return "vote_deferred_with_retention"


def _can_reexecute(inputs: dict) -> bool:
    return not any(k.startswith("live_") for k in inputs)

def _execute(state: dict, inputs: dict) -> dict:
    raise NotImplementedError

def _hash(obj) -> str:
    raise NotImplementedError

def _verify_determinism(inputs: dict, proof: dict) -> bool:
    raise NotImplementedError

def _retain_for_challenge(state: dict, inputs: dict, h: str) -> None:
    raise NotImplementedError
