# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-scoped-memory-deletion-as-a-tombstone-rather-than-a-no-op.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-scoped-memory-deletion-as-a-tombstone-rather-than-a-no-op.warrant.json
# Markdown SHA-256:   c0a782a4a8a917bff7e5352441ee80573a3b76b5b0804be4234c804d98a18049
# Code AST SHA-256:   56fb925ae952172831056e1e1ef523f1a52469d0ee865e28e3289b64a30ad1c8
# Verified Classes:   TombstoneViolation, TombstonedButLive
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TombstoneViolation(RuntimeError):
    """Raised when a read returns a value for a key that is tombstoned in the audit log."""
    pass


class TombstonedButLive(RuntimeError):
    """Raised when forget() returned ok but read-path predicate equivalence is unverified."""
    pass


def admit_retrieval(result: dict, tombstones: dict, verified_paths: set) -> dict:
    """
    Gate a retrieval result against the tombstone set before it enters context.
    result: {"key": str, "scope": str, "value": Any, "confidence": float, "path": str}
    tombstones: {key: {"tombstone": bool, "retention_until": str, "predicate": str}}
    verified_paths: set of read-path identifiers confirmed to filter on tombstone predicate
    """
    key = result["key"]
    scope = result["scope"]
    path = result["path"]

    tomb = tombstones.get(key)
    if tomb is None:
        return result

    if tomb["tombstone"]:
        # A read returning a tombstoned key is a protocol violation, regardless of confidence.
        raise TombstoneViolation(
            f"read returned value for tombstoned key {key!r} in scope {scope!r} "
            f"via path {path!r} (confidence={result['confidence']}); "
            f"predicate={tomb['predicate']!r}, retention_until={tomb['retention_until']!r}. "
            f"Refuse value; re-issue forget(cascade=true, propagate=all_peers)."
        )

    if path not in verified_paths:
        raise TombstonedButLive(
            f"read path {path!r} has not been verified to filter on the tombstone predicate; "
            f"cannot admit result for key {key!r} in scope {scope!r}. "
            f"Treat key as tombstoned-but-possibly-live and halt."
        )

    return result


def forget_with_barrier(client, scope: str, key: str, peers: list) -> dict:
    """
    Two-phase forget: issue, then wait for peer acknowledgment before
    permitting any peer to answer reads in the scope.
    """
    resp = client.forget(scope=scope, key=key, cascade=True, propagate="all_peers")
    if not resp.get("tombstone"):
        raise TombstoneViolation(
            f"forget({scope!r}, {key!r}) returned without a tombstone descriptor; "
            f"cannot establish deletion semantics. Response: {resp!r}"
        )

    unacked = [p for p in peers if not p.ack_tombstone(scope, key)]
    if unacked:
        raise TombstonedButLive(
            f"tombstone for {key!r} in {scope!r} not acknowledged by peers {unacked!r}; "
            f"scope must be quarantined until propagation completes."
        )

    return resp
