# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-hierarchical-summarization-has-silently-inverted-its-own-evidence-the-compaction-trigger-that-erases-the-disconfirmin.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-hierarchical-summarization-has-silently-inverted-its-own-evidence-the-compaction-trigger-that-erases-the-disconfirmin.warrant.json
# Markdown SHA-256:   e2167d70ba401d877da91a2dda437e6c14aa28033d100144fa9d485712797610
# Code AST SHA-256:   fac5e4e57cbabf94081a8a1bd44f070e9634ef1e6c47356c8d9de754c5868e67
# Verified Classes:   CompactionInvariantViolation, NegationPin, SummaryNode
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class CompactionInvariantViolation(RuntimeError):
    """Raised when a summary node would drop a negation whose referent
    cannot be resolved at the parent level — an unsound abstraction."""
    pass


@dataclass
class NegationPin:
    predicate: str          # e.g. "lock_wait_timeout"
    referent: str           # e.g. "tenant_41"
    source_span: str        # leaf id for re-expansion
    must_reverify: bool = True


@dataclass
class SummaryNode:
    level: int
    text: str
    pins: list[NegationPin] = field(default_factory=list)
    children: list[str] = field(default_factory=list)


def compact(children: list[SummaryNode], parent_text: str) -> SummaryNode:
    """Fold child summaries into a parent. Refuses to promote if any
    child negation referent fails to survive into the parent."""
    inherited_pins: list[NegationPin] = []
    for child in children:
        for pin in child.pins:
            # Invariant: referent must be literally present in parent text,
            # or the pin must be carried forward verbatim.
            if pin.referent in parent_text:
                inherited_pins.append(pin)
            elif pin.must_reverify:
                # Referent was laundered away by the summarizer.
                raise CompactionInvariantViolation(
                    f"Compaction would drop negation referent "
                    f"{pin.referent!r} (predicate={pin.predicate!r}, "
                    f"span={pin.source_span!r}); parent text "
                    f"{parent_text!r} does not resolve it. "
                    f"Pin must be carried verbatim or span re-expanded."
                )
    return SummaryNode(level=max(c.level for c in children) + 1,
                       text=parent_text, pins=inherited_pins)


def plan_irreversible_action(node: SummaryNode, blast_radius: set[str]) -> list[str]:
    """Before any irreversible step, re-execute every pinned negation
    whose referent intersects the blast radius."""
    reverify = [
        p.source_span for p in node.pins
        if p.must_reverify and p.referent in blast_radius
    ]
    if not reverify and any(p.referent in blast_radius for p in node.pins):
        raise CompactionInvariantViolation(
            "Blast radius touches a pinned negation but no source span "
            "is available for reverification; action must be blocked."
        )
    return reverify
