# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-forgetting-from-eviction-in-scoped-retention-memory-tools.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-forgetting-from-eviction-in-scoped-retention-memory-tools.warrant.json
# Markdown SHA-256:   cb95b54857f2ba03585d0359eff55a47a17cfbdbd94f156caca07742387bd88b
# Code AST SHA-256:   2c75d2f1659aa34593828def9cacd7429c1c8af806407958f69419d6b156e762
# Verified Classes:   ForgetEvictionCollision, ScopedMemory
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ForgetEvictionCollision(RuntimeError):
    """Raised when a forget cannot be proven to outlive the scope that held the data."""


class ScopedMemory:
    def __init__(self, store, deny_list):
        self.store = store          # scope -> {key: value}, with TTL-based eviction
        self.deny_list = deny_list  # (subject, key) -> horizon, independent of scope

    def forget(self, scope: str, subject: str, key: str) -> None:
        self.store.delete(scope, key)
        self.deny_list.add(subject, key)
        # Prove stickiness: the denial must be visible outside the originating scope.
        probe_scope = self.store.fresh_scope_for(subject)
        verdict = self.store.lookup(probe_scope, key, subject=subject, deny_list=self.deny_list)
        if verdict != "denied":
            raise ForgetEvictionCollision(
                f"forget({subject!r}, {key!r}) in scope {scope!r} did not survive "
                f"scope boundary; sibling scope returned {verdict!r}. "
                f"Tombstone is scope-bound and will be lost on eviction."
            )

    def write(self, scope: str, subject: str, key: str, value) -> None:
        if self.deny_list.contains(subject, key):
            raise ForgetEvictionCollision(
                f"refusing to re-materialize denied key ({subject!r}, {key!r}) "
                f"via scope {scope!r}; prior forget is still in force."
            )
        self.store.put(scope, key, value)
