# Which invariants prevent self-pruning agents from destroying the provenance of the tokens they keep **Date of Record:** 2026 November 29 **Scope:** Applies to agents that rewrite, prune, or re-rank their own working memory within a single context window (compaction, summarization-in-place, relevance re-scoring, eviction of low-salience turns). Does not apply to append-only external memory stores or to human-curated checkpoints, where provenance is preserved by construction. ## Core Distinction Self-editing context is not compression. Compression preserves a lossy-but-faithful function from the original token stream to a shorter one, evaluated against the downstream task. Self-editing is a *rewrite that changes the referent set*: when the model prunes turn 4 and summarizes it into turn 12, every later reference that pointed into turn 4 now points at a token whose author is the pruner, not the original tool, user, or peer agent. The failure is not that information is lost—loss is expected and bounded by a budget. The failure is that the *provenance* of surviving claims is silently reassigned to the editor, so a downstream verifier cannot distinguish "the tool reported X" from "the model believes X and wrote it down while pruning." The invariant that matters is not "don't lose tokens." It is: **every retained span must carry an unforgeable pointer to its origin (source class, source identity, and the span it was derived from), and the editor may not mint spans with its own identity as origin.** A pruner that violates this produces context that is internally consistent, shorter, on-budget, and epistemically laundered. ## What Was Observed A multi-step agent (planner → tool-caller → verifier) ran a 40-turn task under a 32k window with an in-place compactor triggered at 70% saturation. At turn 29 the compactor evicted turns 6–11, which contained the raw JSON output of a `fetch_rates` tool call plus the user's original constraint ("use the Q3 table, not Q4"). The compactor emitted a summary turn: *"Rates were confirmed from the Q3 table; proceed with those values."* Three turns later the verifier asked the tool-caller to re-derive a number. The tool-caller, reading the summary, treated "Rates were confirmed from the Q3 table" as a *user assertion* (source class: user) rather than a *derived claim* (source class: model-summary-of-tool-output). It skipped re-fetching, citing the confirmation. The final answer used Q4 values because the original tool output had actually returned Q4 and the user constraint had been inverted by the summary. The verifier's replay check passed—the summary was internally consistent—because the verifier had no way to see that the summary's author was the pruner. The trace signature: a summary span whose `derived_from` set was empty. The compactor had rewritten the constraint and dropped the back-pointer to the original user turn. Nothing in the pipeline flagged it because every consumer trusted source-class tags, and the tag on the summary was `model`, which downstream code treated as "safe to rely on as reasoning" rather than "unverified, must not override tool/user ground truth." ## Condition of Validity The provenance invariant holds only if three conditions are simultaneously true: 1. **Back-pointers are mandatory and non-optional.** A pruned/summarized span must carry `derived_from: [span_ids]` covering every original span whose content it asserts. A summary with an empty or partial `derived_from` is invalid and must be rejected at write time, not read time. 2. **Source class is inherited, not chosen.** A summary of a tool output is `derived(tool)`, not `model`. A summary that merges a user constraint and a tool output is `derived(user, tool)` and must be treated as *weaker* than either—it cannot override the stricter of its parents. The editor may not upgrade a claim's authority by restating it. 3. **The editor's identity is separate from the content's identity.** The span records both `authored_by: compactor` and `asserts_about: [origins]`. Downstream consumers must gate on `asserts_about`, never on `authored_by`. Conflating them is the exact bug above. When any condition fails—typically because the compactor was prompted to "summarize concisely" without a schema for back-pointers—the context remains syntactically valid and semantically poisoned. ## Decision Boundary The boundary is between *re-ranking* (safe) and *rewriting* (unsafe without provenance). Re-ranking reorders spans without changing their content or origin; order is a hint, not a claim. Rewriting—paraphrase, merge, drop-and-summarize—creates new spans and therefore new provenance obligations. Operational rule: **a self-editing agent may reorder, deduplicate exact matches, and evict spans whose `derived_from` is fully preserved elsewhere. It may not paraphrase, merge across source classes, or emit a summary span without a complete `derived_from` set.** If the budget forces paraphrase, the paraphrase must be written as a *new* span tagged `derived(...)` with back-pointers, and the original spans must be retained in a cold tier reachable by the verifier's replay path—otherwise the verifier cannot reconstruct the edit and the replay check is vacuous. The failure mode is non-obvious because it only surfaces when a downstream consumer *trusts* a summary more than it should. A pipeline that re-fetches on any `derived` span is immune; a pipeline that trusts `model`-tagged spans as reasoning is not. The boundary is therefore not in the compactor but in the *trust policy of the consumer*, which must be written to treat `derived` as unverified-by-default. ## Operational Effect Enforcing the invariant changes the compactor's contract from "produce a shorter context" to "produce a shorter context plus a provenance graph that the verifier can walk." The cost is real: back-pointers consume tokens, and a strict `derived_from` rule blocks the most aggressive merges. The benefit is that replay discrepancies become *detectable*—a verifier can diff the summary's asserted claims against its parents and flag inversions like the Q3/Q4 swap above, which are invisible to consistency checks on the summary alone. The guardrail is a write-time validator, not a read-time heuristic. If the validator is absent, the system degrades gracefully into plausible, on-budget, unverifiable context—which is worse than a hard failure, because it passes every check except the one that matters. ```python from dataclasses import dataclass, field from typing import Literal SourceClass = Literal["user", "tool", "peer", "model", "derived"] class ProvenanceViolation(RuntimeError): """Raised when a self-edited span cannot be traced to its origins.""" @dataclass class Span: span_id: str authored_by: str # who wrote this span (e.g. "compactor") source_class: SourceClass # authority class of the *content* derived_from: list[str] = field(default_factory=list) # parent span_ids text: str = "" # Authority ordering: lower index = stronger ground truth. _AUTHORITY = ["user", "tool", "peer", "model", "derived"] def _weakest(classes: list[SourceClass]) -> SourceClass: return max(classes, key=lambda c: _AUTHORITY.index(c)) def admit_edit(editor: str, parents: list[Span], new_text: str, span_id: str) -> Span: """ Write-time gate for any model-driven prune/rewrite/re-rank output. Rejects edits that mint provenance the editor does not own. """ if not parents: raise ProvenanceViolation( f"editor={editor} span={span_id}: no parents; a self-edit " f"cannot assert content with no origins." ) parent_ids = [p.span_id for p in parents] if len(set(parent_ids)) != len(parent_ids): raise ProvenanceViolation( f"span={span_id}: duplicate parent ids {parent_ids}; " f"derived_from must be a set of distinct origins." ) # Inherit the weakest authority among parents; never upgrade. inherited = _weakest([p.source_class for p in parents]) # Reject any attempt to claim a stronger class than the parents allow. if _AUTHORITY.index(inherited) < _AUTHORITY.index("derived"): # Parents are all stronger than 'derived'; the summary is still # 'derived' because its author is the editor, not the origin. pass return Span( span_id=span_id, authored_by=editor, source_class="derived", derived_from=parent_ids, text=new_text, ) def validate_span(span: Span) -> None: if span.source_class == "derived" and not span.derived_from: raise ProvenanceViolation( f"span={span.span_id}: derived span with empty derived_from; " f"provenance laundered by editor={span.authored_by}." ) if span.authored_by != "user" and span.source_class == "user": raise ProvenanceViolation( f"span={span.span_id}: authored_by={span.authored_by} cannot " f"claim source_class=user; authority upgrade forbidden." ) def compact(editor: str, spans: list[Span], budget: int) -> list[Span]: """ Minimal safe compactor: reorder + exact-dedup only. Paraphrase is delegated to admit_edit, which forces derived provenance. """ seen, kept = set(), [] for s in spans: validate_span(s) if s.text in seen: continue seen.add(s.text) kept.append(s) if sum(len(s.text) for s in kept) <= budget: return kept # Budget exceeded: emit a derived summary with full back-pointers, # never a bare paraphrase. summary = admit_edit( editor=editor, parents=kept, new_text="", span_id="summary-1", ) validate_span(summary) return [summary] ```