# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-ttl-erasure-violation-when-a-typed-memory-schema-s-tombstone-is-garbage-collected-before-its-dependent-episodic-ref.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-ttl-erasure-violation-when-a-typed-memory-schema-s-tombstone-is-garbage-collected-before-its-dependent-episodic-ref.warrant.json
# Markdown SHA-256:   3756750bf4bba2b1a73d1034afd95408d0f4e513dedf097a8808071a80f0f8ff
# Code AST SHA-256:   ed82daf8803d68b6a83c0b48367d3eb8085284c0a120f2f1c64ea2afe96b15eb
# Verified Classes:   ErasureProofExpired, Tombstone, DependentRef
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ErasureProofExpired(RuntimeError):
    """Raised when a forget() attestation cannot be soundly certified."""


@dataclass
class Tombstone:
    fact_id: str
    created_at: float
    ttl: float

    def is_live(self, now: float) -> bool:
        return now < self.created_at + self.ttl


@dataclass
class DependentRef:
    ref_id: str
    fact_id: str
    inline_restatement: Optional[str]
    remaining_ttl: float


def certify_erasure(
    fact_id: str,
    tombstone: Optional[Tombstone],
    dependents: list[DependentRef],
    now: float,
    safety_margin: float = 3600.0,
) -> bool:
    # Rule 3: absence of a record is NOT proof of erasure.
    if tombstone is None or not tombstone.is_live(now):
        raise ErasureProofExpired(
            f"no live tombstone for {fact_id}; cannot certify erasure "
            f"(tombstone={'gc' if tombstone is None else 'expired'})"
        )

    # Rule 1: no dependent may retain an inline restatement of an erased fact.
    for dep in dependents:
        if dep.fact_id == fact_id and dep.inline_restatement:
            raise ErasureProofExpired(
                f"dependent {dep.ref_id} still restates erased fact {fact_id}"
            )

    # Rule 2: tombstone must outlive every dependent reference.
    max_dep_ttl = max((d.remaining_ttl for d in dependents), default=0.0)
    if tombstone.ttl < max_dep_ttl + safety_margin:
        raise ErasureProofExpired(
            f"tombstone ttl {tombstone.ttl}s < max dependent ttl "
            f"{max_dep_ttl}s + margin {safety_margin}s for {fact_id}"
        )

    return True
