# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-salience-weighted-eviction-of-a-summary-of-summaries-node-whose-original-spans-are-no-longer-addressable.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-salience-weighted-eviction-of-a-summary-of-summaries-node-whose-original-spans-are-no-longer-addressable.warrant.json
# Markdown SHA-256:   b6d921ce4cf5c443c8dc423c8defa616627ed9f5967f29af9aedd73d47c1077c
# Code AST SHA-256:   c3a4add7880fd29a079a22bd8f22f27ec7214c88a98bd399b4e2259116849ea4
# Verified Classes:   UnsupportedEvictionError, SummaryNode
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UnsupportedEvictionError(RuntimeError):
    """Raised when eviction targets an interior node whose support set is empty
    and whose content cannot be reconstructed from any durable store."""
    pass


class SummaryNode:
    def __init__(self, node_id, text, children, constraint_bearing=False):
        self.node_id = node_id
        self.text = text
        self.children = children          # list of child node ids (empty => leaf)
        self.constraint_bearing = constraint_bearing
        self.pinned = constraint_bearing  # constraints are pinned by default


def evict(node, live_nodes, durable_log_available, salience_threshold=0.15):
    if node.pinned:
        return False  # never evict a pinned/dormant-constraint node

    support = [c for c in node.children if c in live_nodes]
    support_empty = len(support) == 0 and len(node.children) > 0  # interior, orphaned

    if support_empty:
        if not durable_log_available:
            # Terminal commitment: text is the only surviving evidence.
            raise UnsupportedEvictionError(
                f"node {node.node_id} has empty support set and no durable log; "
                f"eviction would irreversibly erase unaddressable history"
            )
        # Durable log exists: rehydrate before scoring, do not evict blind.
        return False

    score = node.salience if hasattr(node, "salience") else 1.0
    if score < salience_threshold:
        live_nodes.pop(node.node_id, None)
        return True
    return False


def verify_replay(node, source_spans):
    if len(source_spans) == 0:
        # Fail closed: cannot verify is not verified.
        raise UnsupportedEvictionError(
            f"verification replay for {node.node_id} has zero sources; "
            f"refusing to return PASS on empty support"
        )
    return all(s.matches(node.text) for s in source_spans)
