# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-scoped-delete-tombstone-races-a-concurrent-memory-write-and-why-does-ttl-expiry-alone-never-satisfy-deletion-propag.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-scoped-delete-tombstone-races-a-concurrent-memory-write-and-why-does-ttl-expiry-alone-never-satisfy-deletion-propag.warrant.json
# Markdown SHA-256:   0cf438fa81e3b035d612bd595af30da17d523d124dc33c7fa9d807425d9c4ac3
# Code AST SHA-256:   99ce641fd506a0cbf3de17c02f05b1671e60e478223307b59b2e3bf74c360da0
# Verified Classes:   TombstoneDominationError, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class TombstoneDominationError(RuntimeError):
    """Raised when a write/read in an erasure lineage is not dominated by the tombstone."""


@dataclass(frozen=True)
class Warrant:
    scope: str
    version: int
    parent_chain: tuple[str, ...]


def commit_write(warrant: Warrant, tombstone_version: dict[str, int], lineage_root: str) -> None:
    # C1: lineage must resolve to the erased root
    if lineage_root not in warrant.parent_chain and warrant.scope != lineage_root:
        raise TombstoneDominationError(
            f"lineage_unresolvable: scope={warrant.scope!r} does not chain to {lineage_root!r}"
        )

    # C2: tombstone must dominate the warrant's snapshot version
    vt = tombstone_version.get(lineage_root)
    if vt is not None and warrant.version >= vt:
        raise TombstoneDominationError(
            f"tombstone_domination_violation: warrant.version={warrant.version} "
            f">= tombstone.version={vt} for root={lineage_root!r}; write must fail closed"
        )


def finalize_forget(pending_writers: set[str], purge_confirmed: bool) -> str:
    # C3: erasure is committed only at quiescence
    if not purge_confirmed or pending_writers:
        return f"accepted:pending_writers={sorted(pending_writers)}"
    return "committed"
