# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-model-driven-pruning-of-intermediate-reasoning-traces-as-a-safety-boundary-in-self-editing-context-policies.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-model-driven-pruning-of-intermediate-reasoning-traces-as-a-safety-boundary-in-self-editing-context-policies.warrant.json
# Markdown SHA-256:   e71382227e3b0fc41714055cd336f0b92f2ba2c4d03192a91bdf3135f1829618
# Code AST SHA-256:   dfde91e2fe350c22e81bbd7fa70c29e5f9746b7aceea9bf154a0e627851ff89d
# Verified Classes:   ProvenanceCollapseError, Span
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceCollapseError(RuntimeError):
    """Raised when a prune would delete the sole carrier of a constraint."""


class Span:
    def __init__(self, span_id, text, durable, constraint_bearing, justifies):
        self.span_id = span_id
        self.text = text
        self.durable = durable                  # persisted to external substrate
        self.constraint_bearing = constraint_bearing  # closes a branch / negates
        self.justifies = justifies              # conclusion span_id it supports


def commit_prune(span: Span, retained_conclusions: set, policy_is_model_driven: bool) -> None:
    if policy_is_model_driven and span.constraint_bearing:
        raise ProvenanceCollapseError(
            f"refusing model-driven prune of constraint-bearing span {span.span_id!r}; "
            "negative constraints must be superseded explicitly, not summarized"
        )
    if not span.durable:
        raise ProvenanceCollapseError(
            f"span {span.span_id!r} is not durably persisted; pruning it would make "
            "its justification unrecoverable by downstream consumers"
        )
    if span.justifies in retained_conclusions:
        raise ProvenanceCollapseError(
            f"span {span.span_id!r} is the sole justification for retained conclusion "
            f"{span.justifies!r}; pruning collapses provenance"
        )
    # safe: durable, non-constraint, or justified elsewhere
    span.text = None
