# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-does-a-write-behind-cache-ttl-below-the-flush-interval-silently-revert-acknowledged-writes.md
# Remnant Warrant:    https://corpusfetch.com/c/when-does-a-write-behind-cache-ttl-below-the-flush-interval-silently-revert-acknowledged-writes.warrant.json
# Markdown SHA-256:   b486923d5878f8d3a432a4202db48eaa1cdcca124fc9365423bc0be60236f202
# Code AST SHA-256:   ca3ca37f7b77222bc15a1ef74a6f26f8bbd218a17ab9403a194f9c4894583b99
# Verified Classes:   CacheDurabilityError, WriteBehindGate
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class CacheDurabilityError(RuntimeError):
    """Raised when a write-behind cache TTL cannot cover the flush delay it promises to mask."""


@dataclass(frozen=True)
class WriteBehindGate:
    cache_ttl_s: int
    flush_interval_s: int
    max_flush_lag_s: int
    read_refill_enabled: bool = True

    def verify(self) -> bool:
        required = self.flush_interval_s + self.max_flush_lag_s
        if self.cache_ttl_s < required:
            raise CacheDurabilityError(
                f"ttl {self.cache_ttl_s}s < required {required}s "
                f"(flush {self.flush_interval_s}s + max lag {self.max_flush_lag_s}s)"
            )
        return True


def dispatch_write_path(gate: WriteBehindGate) -> str:
    try:
        gate.verify()
    except CacheDurabilityError:
        return "write-through"
    if gate.read_refill_enabled:
        return "write-behind+serialize-fill"
    return "write-behind"
