# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgment-as-a-tombstone-rather-than-a-deletion-and-audit-the-divergence-before-the-ttl-win.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgment-as-a-tombstone-rather-than-a-deletion-and-audit-the-divergence-before-the-ttl-win.warrant.json
# Markdown SHA-256:   ea499995ea770ae01bf08a4d1e1deb8f4762d8c60a3f47a9ccf8dc4243e906fb
# Code AST SHA-256:   800c81e93374a17942ee4e595fa9811632d175aa64ffbe85efbe2d0d93a17fb8
# Verified Classes:   ForgetVerificationError, ForgetAck, AuditObligation
# Defensive Guards:   7 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from typing import Optional


class ForgetVerificationError(RuntimeError):
    """Raised when a forget acknowledgment is treated as deletion without audit closure."""
    pass


@dataclass
class ForgetAck:
    ok: bool
    tombstoned: int
    erased: int
    audit_pending: bool
    key_ttl_seconds: float
    ack_wall_clock: float = field(default_factory=time.time)


@dataclass
class AuditObligation:
    scope: str
    keys: list
    deadline_wall_clock: float
    obligation_ttl_seconds: float


def assert_absence_permitted(ack: ForgetAck, replica_read_none: bool) -> None:
    """
    Gate a negative fact ('this scope contains no PII') on erasure proof,
    not on the forget acknowledgment alone.
    """
    if not ack.ok:
        raise ForgetVerificationError(
            f"forget did not acknowledge: ok={ack.ok}"
        )

    if ack.erased != ack.tombstoned:
        raise ForgetVerificationError(
            f"tombstoned={ack.tombstoned} but erased={ack.erased}; "
            "key is tombstoned, not erased — absence unverified"
        )

    if ack.audit_pending:
        raise ForgetVerificationError(
            "audit_pending=True: erasure not yet confirmed by audit path"
        )

    if not replica_read_none:
        raise ForgetVerificationError(
            "replica-routed read resolved a tombstoned key; "
            "replication lag or promotion resurrected payload"
        )


def schedule_audit_obligation(ack: ForgetAck, scope: str, keys: list) -> AuditObligation:
    """
    The re-audit obligation's TTL MUST dominate the forgotten key's TTL,
    or the agent forgets its own obligation before the sweep runs.
    """
    key_expiry = ack.ack_wall_clock + ack.key_ttl_seconds
    # Obligation must outlive the key's TTL by a margin; otherwise the
    # obligation itself is evicted before the sweep that would confirm erasure.
    obligation_ttl = ack.key_ttl_seconds * 2.0
    obligation_deadline = ack.ack_wall_clock + obligation_ttl

    if obligation_deadline <= key_expiry:
        raise ForgetVerificationError(
            f"obligation deadline {obligation_deadline} does not dominate "
            f"key expiry {key_expiry}; re-audit would be lost before sweep"
        )

    return AuditObligation(
        scope=scope,
        keys=list(keys),
        deadline_wall_clock=obligation_deadline,
        obligation_ttl_seconds=obligation_ttl,
    )


def resolve_audit(obligation: AuditObligation, audit_erased: int, now: Optional[float] = None) -> None:
    now = now if now is not None else time.time()
    if now > obligation.deadline_wall_clock:
        raise ForgetVerificationError(
            f"audit deadline {obligation.deadline_wall_clock} passed at {now}; "
            "negative facts derived from this forget must be demoted to UNVERIFIED"
        )
    if audit_erased != len(obligation.keys):
        raise ForgetVerificationError(
            f"audit erased {audit_erased} of {len(obligation.keys)} keys; "
            "tombstone divergence persists past TTL window"
        )
