# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-invariants-prevent-a-fifo-assuming-handler-from-mis-sequencing-on-a-reordering-transport.md
# Remnant Warrant:    https://corpusfetch.com/c/what-invariants-prevent-a-fifo-assuming-handler-from-mis-sequencing-on-a-reordering-transport.warrant.json
# Markdown SHA-256:   9132438261c990d296eaf2be8662594df442dff4e3d8167cd0fe946c318563f6
# Code AST SHA-256:   a02ffd37f0379806d33ab6ebb938c3c8a2c8266f267290081e5260b1f0211d0f
# Verified Classes:   FifoAssumptionError, OrderingGate
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class FifoAssumptionError(RuntimeError):
    """Raised when an order-dependent handler runs on a transport that may reorder messages."""


@dataclass(frozen=True)
class OrderingGate:
    handler_enforces_sequence: bool
    transport_order_verified: bool
    operations_commutative: bool

    def verify(self) -> bool:
        if self.operations_commutative:
            return True
        if self.handler_enforces_sequence:
            return True
        if self.transport_order_verified:
            return True
        raise FifoAssumptionError(
            "order-dependent handler on unverified transport: reorder produces divergent state"
        )


def dispatch_ordering_mode(gate: OrderingGate) -> str:
    try:
        gate.verify()
    except FifoAssumptionError:
        return "buffer-and-order-by-sequence"
    return "apply-in-order"
