# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-committed-but-unexecuted-tasks-from-surviving-quorum-reconfiguration-in-bft-agent-swarms.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-committed-but-unexecuted-tasks-from-surviving-quorum-reconfiguration-in-bft-agent-swarms.warrant.json
# Markdown SHA-256:   3ec63ac6f664fd7ac33ce4701bb4011efcd85f6e937a9c263697a9f49d858766
# Code AST SHA-256:   06330f2050d0575f2498f495783de4d78bf5ac44172fdb4939be2c2fa46e8a66
# Verified Classes:   CutInconsistencyError, CommitmentState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CutInconsistencyError(RuntimeError):
    """Raised when a reconfiguration snapshot's causal cut is inconsistent
    with an external side-effect ledger for a carried-forward commitment."""
    pass


class CommitmentState:
    COMMITTED = "COMMITTED"
    IN_DOUBT = "IN_DOUBT"
    DISPATCHED = "DISPATCHED"
    EXECUTED = "EXECUTED"


def admit_commitment_under_new_quorum(
    commitment,
    snapshot_cut,
    external_ledger,
    fence_durable_on_quorum: bool,
) -> str:
    """
    Decide whether a commitment carried into a new quorum may be dispatched.

    Invariant: COMMITTED -> DISPATCHED is legal only if the execution witness
    is inside the snapshot's causal cut, OR the external ledger can resolve
    the commitment as already-executed / not-executed.
    """
    if not fence_durable_on_quorum:
        # Snapshot was taken concurrently with the fence; the cut may have
        # missed an in-flight witness. Refuse to trust the cut.
        raise CutInconsistencyError(
            f"commitment {commitment.id}: snapshot taken before fence durable "
            f"on quorum; causal cut is not trustworthy for dispatch"
        )

    witness_in_cut = snapshot_cut.contains_witness(commitment.witness_id)
    if witness_in_cut:
        return CommitmentState.EXECUTED

    # Witness is outside the cut. The logs cannot tell us whether the side
    # effect landed. Query the external authority.
    ledger_state = external_ledger.query(commitment.idempotency_token)

    if ledger_state is None:
        # Provably not executed: safe to dispatch under the new quorum.
        return CommitmentState.DISPATCHED

    if ledger_state.status == "COMPLETED":
        # Side effect landed but witness was outside the cut. Replay would
        # double-execute. Adopt the ledger's response as the witness.
        commitment.witness_id = ledger_state.witness_id
        return CommitmentState.EXECUTED

    if ledger_state.status == "IN_FLIGHT":
        # The fenced agent may still be mid-call. Dispatch is unsafe and
        # replay is unsafe. Block and reconcile.
        raise CutInconsistencyError(
            f"commitment {commitment.id}: side effect IN_FLIGHT at ledger "
            f"with token {commitment.idempotency_token}; "
            f"cannot dispatch under new quorum until reconciled"
        )

    raise CutInconsistencyError(
        f"commitment {commitment.id}: unrecognized ledger state "
        f"{ledger_state.status!r}; refusing to dispatch"
    )
