# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-tool-forget-acknowledgement-as-non-authoritative-until-the-retention-audit-tombstone-is-durably-comm.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-tool-forget-acknowledgement-as-non-authoritative-until-the-retention-audit-tombstone-is-durably-comm.warrant.json
# Markdown SHA-256:   9bdef3a2096fb36e3a54c3123e29f8a6f0f3ef5c85445fa75a26e2bb76b59c81
# Code AST SHA-256:   0b43446318c0b56346ccccb5e60949abea802cab306ee5fe94f94d9cc84f6951
# Verified Classes:   TombstoneState, RetentionHoldActive, TombstoneNotCommitted, DerivedWriteBlocked, ForgetAck
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from enum import Enum

class TombstoneState(Enum):
    COMMITTED = "committed"
    PENDING = "pending"
    DEFERRED = "deferred"
    ABSENT = "absent"

class RetentionHoldActive(RuntimeError):
    pass

class TombstoneNotCommitted(RuntimeError):
    pass

class DerivedWriteBlocked(RuntimeError):
    pass

@dataclass
class ForgetAck:
    status: str
    audit_id: str
    tombstone_state: TombstoneState

def enforce_forget(ack: ForgetAck, audit_read, retention_policy, deadline_s: float = 30.0):
    # Phase 0: retention state must be clear before we trust any delete.
    if retention_policy.get("hold") or retention_policy.get("review"):
        raise RetentionHoldActive(
            f"record under {retention_policy}; forget is a retention event, "
            f"not a deletion (audit_id={ack.audit_id})"
        )

    # Phase 1: acknowledgement is provisional until tombstone is durable.
    if ack.tombstone_state is TombstoneState.ABSENT:
        raise TombstoneNotCommitted(
            f"tool exposed no tombstone_state; treating {ack.audit_id} as pending"
        )

    start = time.monotonic()
    state = ack.tombstone_state
    while state is not TombstoneState.COMMITTED:
        if time.monotonic() - start > deadline_s:
            raise TombstoneNotCommitted(
                f"tombstone for {ack.audit_id} still {state.value} after {deadline_s}s"
            )
        record = audit_read(ack.audit_id)
        state = TombstoneState(record["tombstone_state"])
        if state is TombstoneState.DEFERRED:
            # deferred behind a hold check: do not spin, escalate
            raise TombstoneNotCommitted(
                f"tombstone {ack.audit_id} deferred pending approver; "
                f"derived writes must remain provisional"
            )
        time.sleep(0.5)

    return ack.audit_id  # only now may derived writes proceed

def write_derived_memory(derived_text: str, source_audit_id: str, audit_read, retention_policy):
    # The gate: a derived write that quotes a forgotten record is only legal
    # if the source tombstone is durably committed.
    record = audit_read(source_audit_id)
    if TombstoneState(record["tombstone_state"]) is not TombstoneState.COMMITTED:
        raise DerivedWriteBlocked(
            f"refusing derived write quoting {source_audit_id}: "
            f"tombstone is {record['tombstone_state']}, retention may still hold"
        )
    return derived_text
