# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-self-prune-its-context-into-a-state-it-can-no-longer-reconstruct.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-self-prune-its-context-into-a-state-it-can-no-longer-reconstruct.warrant.json
# Markdown SHA-256:   32240d97e54dc1d8e830250ed836769202ae6f7506f9ce6d2d7402e244118dcb
# Code AST SHA-256:   0501a82de5bcb43b25472e66f4454822dea0e45dff6ec6f24e3d110ee7ce3d64
# Verified Classes:   IrreversibleReferenceLoss
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class IrreversibleReferenceLoss(RuntimeError):
    """Raised when a self-authored summary asserts a fact whose provenance
    span was pruned without a resolvable pointer, making the assertion
    unfalsifiable from within the surviving context."""
    pass


def commit_summary(summary_lines, pruned_span_ids, transcript):
    """
    Validate a model-authored context rewrite before it replaces the raw span.
    summary_lines: list of dicts {"text": str, "src": span_id | None}
    pruned_span_ids: spans the rewrite is about to discard
    transcript: append-only external store, span_id -> raw content
    """
    for line in summary_lines:
        src = line.get("src")
        # A line that stands in for pruned evidence MUST be pointer-addressable.
        if src is None:
            # Distinguish restatement from laundering: if the line asserts a
            # constraint that any pruned span could contradict, it is unsafe.
            if _asserts_constraint(line["text"]):
                raise IrreversibleReferenceLoss(
                    f"summary line {line['text']!r} asserts a constraint with no "
                    f"provenance pointer; pruned spans {pruned_span_ids} are "
                    f"unrecoverable and the claim cannot be re-verified"
                )
            continue
        if src not in transcript:
            raise IrreversibleReferenceLoss(
                f"summary line points to span {src!r} which is not in the "
                f"external transcript; pointer is unresolvable, rewrite is "
                f"irreversible"
            )
    # Only after every load-bearing line is pointer-addressable may the
    # rewrite replace the raw span.
    return [line["text"] for line in summary_lines]


def _asserts_constraint(text):
    markers = ("verified", "confirmed", "non-null", "not null", "unique",
               "immutable", "always", "never", "guaranteed")
    low = text.lower()
    return any(m in low for m in markers)
