# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-erasure-warrant-races-its-own-distillation-pipeline.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-erasure-warrant-races-its-own-distillation-pipeline.warrant.json
# Markdown SHA-256:   34da46ea1a3db921d95fb9b88f23466029b6a74b4b41e26d81983546d0984602
# Code AST SHA-256:   4906154513e9b977cf5ed29ddb92f4193cd2cb04fbabab6165ccb9afb3963e69
# Verified Classes:   UnresolvableLineageError, TombstoneLedger
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UnresolvableLineageError(RuntimeError):
    """Raised when a derived artifact cannot enumerate its contributing subjects,
    so an erasure warrant cannot be proven to cover it."""
    pass


class TombstoneLedger:
    def __init__(self):
        self._tombstones = set()

    def add(self, subject_id: str) -> None:
        self._tombstones.add(subject_id)

    def is_tombstoned(self, subject_id: str) -> bool:
        return subject_id in self._tombstones


def enforce_warrant(subject_id: str,
                    episodic_ids: list[str],
                    derived_artifacts: list[dict],
                    ledger: TombstoneLedger) -> str:
    # Ordering invariant: tombstone BEFORE any purge.
    ledger.add(subject_id)

    unresolved = []
    for art in derived_artifacts:
        lineage = art.get("lineage_subjects")
        if lineage is None:
            # No back-pointer: cannot prove coverage. Fail closed.
            raise UnresolvableLineageError(
                f"artifact {art.get('id')} has no lineage; "
                f"cannot prove erasure of subject {subject_id}"
            )
        if subject_id in lineage:
            unresolved.append(art["id"])

    if unresolved:
        # Addressable sources but derivability not closed: halt, do not claim success.
        return "WARRANT_PARTIAL"

    # Only now may bytes be purged and success asserted.
    return "SATISFIED"


def distill_gate(subject_ids: list[str], ledger: TombstoneLedger) -> None:
    for s in subject_ids:
        if ledger.is_tombstoned(s):
            raise UnresolvableLineageError(
                f"refusing to derive from tombstoned subject {s}"
            )
