# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-write-acknowledgment-as-provisional-until-the-retention-scope-has-been-re-read-after-a-forget-operat.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-write-acknowledgment-as-provisional-until-the-retention-scope-has-been-re-read-after-a-forget-operat.warrant.json
# Markdown SHA-256:   de3f9b5a191634c07348125d348368a92fd68dcf79e4ef2196d9ae8897c9eab1
# Code AST SHA-256:   d846696a3b00038bbfee069a45dbca7708beb9d4eef5d7b89616190c4778ecf5
# Verified Classes:   ProvisionalWriteViolation, WriteAck, ReadResult
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ProvisionalWriteViolation(RuntimeError):
    """Raised when a write acknowledgment is treated as durable without a
    post-write generation check against the retention scope."""
    pass


@dataclass(frozen=True)
class WriteAck:
    ok: bool
    scope: str
    key: str
    gen: int


@dataclass(frozen=True)
class ReadResult:
    ok: bool
    scope: str
    key: str
    gen: int
    value: object | None
    tombstoned: bool = False


def commit_fact(ack: WriteAck, read: ReadResult, fallback: ReadResult | None) -> object:
    """Promote a provisional write to a committed fact, or refuse.

    Invariant: a write is durable only if the first read's scope generation
    equals the write's generation. A tombstoned scope or an advanced
    generation means the write was superseded by a concurrent forget; in that
    case we must NOT silently fall back to a broader scope whose own
    generation we have not verified.
    """
    if not ack.ok:
        raise ProvisionalWriteViolation(f"write not acknowledged for {ack.scope}/{ack.key}")

    if read.tombstoned or read.gen > ack.gen:
        # Scope was mutated after our write. Supersession, not a transient fault.
        if fallback is None:
            raise ProvisionalWriteViolation(
                f"write to {ack.scope}/{ack.key} superseded by forget "
                f"(write gen={ack.gen}, read gen={read.gen}); no verified fallback"
            )
        if fallback.gen < ack.gen:
            raise ProvisionalWriteViolation(
                f"fallback scope {fallback.scope} generation {fallback.gen} "
                f"predates superseding forget at gen {read.gen}; stale value rejected"
            )
        return fallback.value

    if read.gen < ack.gen:
        raise ProvisionalWriteViolation(
            f"read generation {read.gen} regressed below write generation {ack.gen} "
            f"for {ack.scope}/{ack.key}; scope state is inconsistent"
        )

    if not read.ok or read.value != ack.value if hasattr(ack, "value") else not read.ok:
        raise ProvisionalWriteViolation(
            f"read did not confirm value for {ack.scope}/{ack.key} at gen {ack.gen}"
        )

    return read.value
