# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-model-driven-context-pruning-when-the-pruner-s-own-scoring-pass-is-inside-the-context-it-scores.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-model-driven-context-pruning-when-the-pruner-s-own-scoring-pass-is-inside-the-context-it-scores.warrant.json
# Markdown SHA-256:   0b450b91c6b6befe7e3e9ce4b2b5ccaace65083ac577c71ba6f64b7dea9efe03
# Code AST SHA-256:   5d0f99023b4135680a06b6fdd330a323198eda7a0d39d2621384ce6684b7fff7
# Verified Classes:   UnsafePruneError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class UnsafePruneError(RuntimeError):
    """Raised when a self-pruning pass would evict or degrade an entry
    whose scope/mutability makes relevance-scored eviction inadmissible."""
    pass


INVARIANT = "invariant"
WORKING = "working"
SCRATCH = "scratch"

EPISODE = "episode"
TASK = "task"
TURN = "turn"


def authorize_prune(entry, score, snapshot_excludes_pruner_output, tombstone_enabled):
    """Decide whether a single entry may be evicted by a self-pruning pass.

    Returns True if eviction is admissible. Raises UnsafePruneError otherwise.
    """
    scope = entry.get("scope")
    mutability = entry.get("mutability")

    # Unclassified entries default to the conservative class.
    if scope is None or mutability is None:
        scope = scope or EPISODE
        mutability = mutability or INVARIANT

    if scope == EPISODE and mutability == INVARIANT:
        raise UnsafePruneError(
            f"entry {entry.get('id')!r} is an episode-scoped invariant; "
            f"relevance score {score} is not an admissible eviction criterion. "
            f"Compress with verbatim retention instead."
        )

    if scope == EPISODE and mutability == WORKING:
        if not snapshot_excludes_pruner_output:
            raise UnsafePruneError(
                f"entry {entry.get('id')!r}: scoring pass included pruner output; "
                f"relevance is contaminated by recency. Re-score against frozen snapshot."
            )
        if not tombstone_enabled:
            raise UnsafePruneError(
                f"entry {entry.get('id')!r}: episode-scoped working entry evicted "
                f"without tombstone; eviction would be irreversible."
            )
        return True

    if scope in (TASK, TURN) or mutability == SCRATCH:
        return True

    raise UnsafePruneError(
        f"entry {entry.get('id')!r}: unrecognized scope/mutability "
        f"({scope!r}/{mutability!r}); refusing to prune."
    )


def gate_destructive_action(action, enumerated_constraints):
    """Block irreversible actions unless constraints are enumerated, not summarized."""
    if action.get("irreversible") and enumerated_constraints is None:
        raise UnsafePruneError(
            f"irreversible action {action.get('name')!r} blocked: "
            f"constraint set is summarized or unavailable; enumeration required."
        )
    return True
