# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-decayed-memory-reads-against-a-forget-tombstone-that-arrives-mid-recall.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-decayed-memory-reads-against-a-forget-tombstone-that-arrives-mid-recall.warrant.json
# Markdown SHA-256:   9fb476b454c425cf1f8d2e560b30e52c1dcbfcccf7987fb06a18003f4d1bf508
# Code AST SHA-256:   01f83e0e1911616698f574446e068f7e929200b71b78291c88c40fa6c7c9c83e
# Verified Classes:   RetentionEpochViolation, RetentionEpoch, DerivedArtifact
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class RetentionEpochViolation(RuntimeError):
    """Raised when a recall would surface a record or derived artifact
    whose source epoch has been tombstoned by a forget commit."""
    pass


class RetentionEpoch:
    def __init__(self, epoch_id: int, tombstoned: bool = False):
        self.epoch_id = epoch_id
        self.tombstoned = tombstoned


class DerivedArtifact:
    def __init__(self, artifact_id: str, source_epochs: list[RetentionEpoch], content: str):
        self.artifact_id = artifact_id
        self.source_epochs = source_epochs
        self.content = content


def recall_with_epoch_guard(
    artifact: DerivedArtifact,
    tombstone_index: dict[int, bool],
    linearization_epoch: int,
) -> str:
    # Guard: every source epoch must be untombstoned as of the read's
    # linearization point. A stale tombstone index is detected by comparing
    # the index's known epoch against the read's linearization epoch.
    for src in artifact.source_epochs:
        if src.epoch_id > linearization_epoch:
            raise RetentionEpochViolation(
                f"artifact {artifact.artifact_id} references epoch {src.epoch_id} "
                f"newer than read linearization {linearization_epoch}; "
                f"tombstone index is stale"
            )
        if tombstone_index.get(src.epoch_id, False):
            raise RetentionEpochViolation(
                f"artifact {artifact.artifact_id} derives from tombstoned "
                f"epoch {src.epoch_id}; quarantine and re-derive from surviving sources"
            )
    return artifact.content
