# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-scoped-forget-call-that-would-resurrect-a-superseded-memory-through-an-audited-tombstone-gap.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-scoped-forget-call-that-would-resurrect-a-superseded-memory-through-an-audited-tombstone-gap.warrant.json
# Markdown SHA-256:   ad643c85fd2e6f967caa83c5455b64290e512071e73ceafdfa4ec98fd6ab5023
# Code AST SHA-256:   ad5e41fc72f7644f17df94f07665c313502402fc4fcd0e1938771bc51f49f5c2
# Verified Classes:   TombstoneGapError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TombstoneGapError(RuntimeError):
    """Raised when a scoped forget cannot prove durability against the replay horizon."""
    pass


def validate_forget(
    scope: str,
    matched_versions: list[dict],
    manifest_coverage: tuple[int, int],   # (earliest_known_write, latest_known_write)
    audit_ttl_seconds: int,
    max_replay_window_seconds: int,
    now: int,
) -> None:
    manifest_start, manifest_end = manifest_coverage

    # 1. Manifest must span the full write history of the scope.
    for v in matched_versions:
        if not (manifest_start <= v["written_at"] <= manifest_end):
            raise TombstoneGapError(
                f"scope={scope} version={v['id']} written_at={v['written_at']} "
                f"outside manifest coverage {manifest_coverage}; cannot prove no "
                f"compacted-away version exists in the gap"
            )

    # 2. Every matched version's tombstone must outlive the slowest replayer.
    horizon = now - max_replay_window_seconds
    for v in matched_versions:
        if v.get("tombstone_retained_until", 0) < now + max_replay_window_seconds:
            if v["written_at"] >= horizon:
                raise TombstoneGapError(
                    f"scope={scope} version={v['id']} tombstone expires "
                    f"{v.get('tombstone_retained_until')} before replay horizon "
                    f"{now + max_replay_window_seconds}; replay would resurrect it"
                )

    # 3. Policy-level check: audit TTL must not be mistaken for tombstone retention.
    if audit_ttl_seconds < max_replay_window_seconds:
        raise TombstoneGapError(
            f"scope={scope} audit_ttl={audit_ttl_seconds}s < replay_window="
            f"{max_replay_window_seconds}s; forget cannot be made durable under "
            f"current retention policy"
        )
