# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-issues-forget-against-a-scope-it-only-partially-owns-tombstone-shadowing-and-the-resurrection-window-in-scoped-retention.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-issues-forget-against-a-scope-it-only-partially-owns-tombstone-shadowing-and-the-resurrection-window-in-scoped-retention.warrant.json
# Markdown SHA-256:   61c52acafa5a74c48fc62ea461c860f3a2fe186118546f021dc323fb82cab3db
# Code AST SHA-256:   c43f3c1840f1fdaa9bb9500c307c68fafe800af4b81815e84a5db4678ea9e86e
# Verified Classes:   ResurrectionWindowError, ScopeState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ResurrectionWindowError(RuntimeError):
    """Raised when a write into a forgotten subtree carries a snapshot epoch
    older than the scope's tombstone barrier, i.e. it would resurrect data
    that forget() claimed to remove."""
    pass


class ScopeState:
    FORGETTING = "FORGETTING"
    FORGOTTEN = "FORGOTTEN"
    LIVE = "LIVE"


def admit_write(store, target_scope: str, snapshot_epoch: int, logical_ts: int, record: dict):
    # Walk ancestors nearest-first; the barrier at any ancestor governs.
    for scope in store.ancestors_inclusive(target_scope):
        barrier = store.barrier_for(scope)
        if barrier is None:
            continue
        state = store.scope_state(scope)
        if state == ScopeState.FORGOTTEN:
            raise ResurrectionWindowError(
                f"write into forgotten scope {scope!r} via {target_scope!r}; "
                f"barrier_ts={barrier.ts} state={state}"
            )
        # The non-obvious check: epoch lineage, not timestamp recency.
        if snapshot_epoch < barrier.ts:
            raise ResurrectionWindowError(
                f"stale-snapshot write: snapshot_epoch={snapshot_epoch} < "
                f"barrier_ts={barrier.ts} at scope {scope!r}; writer held a "
                f"pre-forget snapshot (logical_ts={logical_ts} is irrelevant — "
                f"recency cannot defeat a barrier)"
            )
        if state == ScopeState.FORGETTING:
            # Barrier present, drain not complete: reject conservatively.
            raise ResurrectionWindowError(
                f"scope {scope!r} is FORGETTING (drain incomplete); "
                f"write rejected until drain_predicate() is true"
            )
    return store.commit(target_scope, snapshot_epoch, logical_ts, record)


def forget(store, scope: str, reason: str) -> dict:
    barrier_ts = store.next_epoch()          # monotonic, not wall-clock
    store.write_barrier(scope, barrier_ts, reason=reason)
    store.set_scope_state(scope, ScopeState.FORGETTING)
    store.audit("forget.barrier", scope=scope, barrier_ts=barrier_ts, reason=reason)
    # Do NOT report FORGOTTEN here. Sweep is best-effort; drain is the gate.
    store.enqueue_sweep(scope, barrier_ts)
    return {
        "scope": scope,
        "state": ScopeState.FORGETTING,
        "barrier_ts": barrier_ts,
        "guarantee": "barrier_holds_for_epochs_gte_barrier_ts",
        "drain_predicate": lambda: store.max_live_snapshot_epoch(scope) < barrier_ts,
    }
