# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-forget-as-a-two-phase-commit-rather-than-a-single-tombstone-write.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-forget-as-a-two-phase-commit-rather-than-a-single-tombstone-write.warrant.json
# Markdown SHA-256:   22c71145859381c94582550ba92150fc1a75dc9d586259acd06641385001dc32
# Code AST SHA-256:   4f75219cf4a841dfbd82ca349b08dd7bd533ec6fe9a630c721cac101e1277466
# Verified Classes:   ForgetAuditViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ForgetAuditViolation(RuntimeError):
    """Raised when a forget cannot be proven auditable under two-phase semantics."""
    pass


def forget(key: str, store) -> str:
    """
    Two-phase auditable forget. Returns 'SEALED' only if the audit seal
    was written after all lineage members were erased at the committed epoch.
    Raises ForgetAuditViolation if the invariant cannot be upheld.
    """
    lineage = store.derivation_lineage(key)
    if lineage is None:
        # No lineage recorded: we cannot prove what to erase. Fail closed.
        raise ForgetAuditViolation(
            f"no derivation lineage for {key!r}; cannot enumerate erasures"
        )

    # Phase 1: pre-commit. Tombstone + intent must be durable before erasure.
    epoch = store.bump_epoch(key)
    if not store.fsync_intent_log(key, epoch, lineage):
        raise ForgetAuditViolation(
            f"intent log not durable for {key!r} at epoch {epoch}"
        )
    store.write_tombstone(key, epoch)

    # Phase 2: idempotent reaper over the full lineage.
    for member in lineage:
        if not store.erase(member, epoch):
            raise ForgetAuditViolation(
                f"erasure of lineage member {member!r} failed at epoch {epoch}"
            )

    # Seal only after every erasure is confirmed.
    if not store.write_audit_seal(key, epoch, lineage):
        raise ForgetAuditViolation(
            f"audit seal unwritable for {key!r} at epoch {epoch}"
        )
    return "SEALED"
