# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-halt-rather-than-tombstone-when-a-federated-erasure-request-races-an-in-flight-episodic-write.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-halt-rather-than-tombstone-when-a-federated-erasure-request-races-an-in-flight-episodic-write.warrant.json
# Markdown SHA-256:   882030ac251b875983952a1e50fac80f57a9b32b9df1c41fe9efa65a8b32e716
# Code AST SHA-256:   fd9766bd393696cce8dbcf18ca605d517955b1ad10678bc8dfcd959df410300b
# Verified Classes:   ErasureBarrierViolation, ErasureWarrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ErasureBarrierViolation(RuntimeError):
    """Raised when an instance cannot prove a federated erasure warrant is
    quiesced and durable before acknowledging erasure."""
    pass


class ErasureWarrant:
    def __init__(self, subject_id: str, logical_clock: int):
        self.subject_id = subject_id
        self.logical_clock = logical_clock  # T_e
        self.state = "pending"  # pending | halted | erased


def commit_write(
    write,
    warrant: ErasureWarrant,
    durable_barrier_set: set,
    commit_clock: int,
    provenance_complete: bool,
) -> None:
    """Commit-time barrier. Must run inside the persisting transaction."""
    intersects = (
        write.provenance_subjects() & {warrant.subject_id}
    )
    if not intersects:
        return

    # A write whose read predates T_e but commits after it must not land
    # unless the barrier is durable and provenance is closed.
    if commit_clock > warrant.logical_clock:
        if warrant.subject_id not in durable_barrier_set:
            raise ErasureBarrierViolation(
                f"warrant for {warrant.subject_id} at T_e={warrant.logical_clock} "
                f"is not durable; write at commit_clock={commit_clock} would "
                f"resurrect erased subject"
            )
        if not provenance_complete:
            raise ErasureBarrierViolation(
                f"derived write for {warrant.subject_id} lacks provenance tags; "
                f"tombstone cannot bound the derivative"
            )


def acknowledge_erasure(
    warrant: ErasureWarrant,
    durable_barrier_set: set,
    in_flight_derivations: int,
    provenance_complete: bool,
) -> str:
    """Only ack 'erased' when the barrier is durable, derivations drained,
    and provenance is closed. Otherwise halt."""
    if warrant.subject_id not in durable_barrier_set:
        raise ErasureBarrierViolation(
            f"cannot ack erasure for {warrant.subject_id}: barrier not durable"
        )
    if in_flight_derivations > 0:
        warrant.state = "halted"
        return "halted"  # bounded block; do NOT report erased
    if not provenance_complete:
        raise ErasureBarrierViolation(
            f"cannot ack erasure for {warrant.subject_id}: derivation closure "
            f"unproven; a tombstone would be a false guarantee"
        )
    warrant.state = "erased"
    return "erased"
