# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-resume-from-a-hierarchical-summary-the-provenance-starvation-boundary-in-auto-compaction.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-resume-from-a-hierarchical-summary-the-provenance-starvation-boundary-in-auto-compaction.warrant.json
# Markdown SHA-256:   e9f18c98fc841f9fa887980216f05d24cbbea6df23972070ea7d0139a337268f
# Code AST SHA-256:   e4151b45e74f21fbfcef39d112a09014e9d8f568d2e9adcae34a17a93ec0034f
# Verified Classes:   ProvenanceStarvationError, SummaryNode
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ProvenanceStarvationError(RuntimeError):
    """Raised when a recall-recovery pass would confirm a claim using a
    summary node that is not identity-bound to a traceable source span."""
    pass


@dataclass(frozen=True)
class SummaryNode:
    node_id: str
    source_span_id: str
    eviction_epoch: int
    observed_epoch: int
    content_hash: str
    is_identity_bound: bool


def confirm_from_node(node: SummaryNode, current_epoch: int) -> str:
    """Use a summary node to CONFIRM a root claim. Confirmation requires a
    traceable source; orphaned or mislabeled nodes must not be trusted."""
    if not node.is_identity_bound:
        raise ProvenanceStarvationError(
            f"node {node.node_id}: not identity-bound to span "
            f"{node.source_span_id}; cannot confirm, only refute"
        )
    if node.eviction_epoch > node.observed_epoch:
        raise ProvenanceStarvationError(
            f"node {node.node_id}: summarizer observed epoch "
            f"{node.observed_epoch} but span was evicted at "
            f"{node.eviction_epoch}; read a replacement span"
        )
    if node.observed_epoch > current_epoch:
        raise ProvenanceStarvationError(
            f"node {node.node_id}: observed epoch {node.observed_epoch} "
            f"is ahead of current epoch {current_epoch}; clock inversion"
        )
    return node.content_hash


def refute_from_node(node: SummaryNode) -> Optional[str]:
    """Refutation may use ANY node, including orphaned ones: a contradiction
    is evidence even from a corrupt source. Returns contradiction marker."""
    return f"contradiction_candidate:{node.node_id}"


def resume_or_halt(nodes: list[SummaryNode], current_epoch: int) -> str:
    """Attempt to resume from a compaction tree; halt on provenance starvation."""
    for node in nodes:
        try:
            confirm_from_node(node, current_epoch)
        except ProvenanceStarvationError:
            # Do not guess. Mark the segment unverified and require
            # re-derivation from live evidence before resuming.
            raise ProvenanceStarvationError(
                f"refusing to resume: history segment {node.source_span_id} "
                f"is UNVERIFIED; re-derive from live tool output"
            )
    return "resumed_with_traceable_history"
