# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-bound-self-editing-context-windows-when-model-driven-pruning-rewrites-the-evidence-it-later-cites.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-bound-self-editing-context-windows-when-model-driven-pruning-rewrites-the-evidence-it-later-cites.warrant.json
# Markdown SHA-256:   0b88908d3dcd7250ece0fe3a5ffb04339cf73851b9b8d3ed05f16a8881b8cf0b
# Code AST SHA-256:   f7041ccea1537fb6b8a33b39ea1c9c54abc8cf82256dccd562cb8b4ba7ac91c2
# Verified Classes:   ProvenanceCollapseError, ContextSpan
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceCollapseError(RuntimeError):
    """Raised when an assertion resolves only to a model-authored derivative
    whose source handle is unreachable, i.e. the instance would be citing itself."""
    pass


class ContextSpan:
    def __init__(self, span_id, text, factual, derived_from=None, handle=None):
        self.span_id = span_id
        self.text = text
        self.factual = factual
        self.derived_from = derived_from   # handle of the source, or None if verbatim
        self.handle = handle               # restorable handle for verbatim spans

    @property
    def is_authoritative(self):
        return self.derived_from is None


def prune_to_summary(span, summary_text, source_store):
    """Model-driven rewrite. Allowed ONLY on the safe side (non-factual or re-derivable)."""
    if span.factual and span.handle is None:
        # Non-re-derivable factual span: never rewrite in place.
        raise ProvenanceCollapseError(
            f"refusing to rewrite non-re-derivable factual span {span.span_id!r}; "
            f"evict to external store with a handle instead"
        )
    # Propagate provenance transitively; a summary of a summary keeps the root handle.
    root = span.derived_from if span.derived_from is not None else span.handle
    return ContextSpan(
        span_id=span.span_id,
        text=summary_text,
        factual=span.factual,
        derived_from=root,
        handle=None,
    )


def assert_factual(span, source_store):
    """Emit an assertion only if it traces to a live, authoritative source."""
    if span.is_authoritative:
        return span.text
    # Derived span: must restore the original before asserting.
    original = source_store.get(span.derived_from)
    if original is None:
        raise ProvenanceCollapseError(
            f"assertion on span {span.span_id!r} resolves only to model-authored "
            f"derivative; source handle {span.derived_from!r} is unreachable"
        )
    return original.text


def verify(assertion_span, source_store, verifier_sees_originals=True):
    """Verifier must run against originals, not the pruner's active window."""
    if not verifier_sees_originals and not assertion_span.is_authoritative:
        raise ProvenanceCollapseError(
            "verifier shares pruner context; self-authored summary cannot confirm itself"
        )
    return assert_factual(assertion_span, source_store)
