# Which invariants prevent provenance loss when a long-horizon agent garbage-collects episodic memory under a token budget **Date of Record:** 2026 November 29 **Scope:** Applies to agents that persist episodic memory across turns and must evict entries to stay within a bounded store or context budget, where downstream decisions depend on *why* a belief is held (provenance), not just its content. Does not apply to stateless single-turn inference or to stores where eviction is purely recency-based with no downstream claim attribution. ## Core Distinction There are two ways an agent can "forget." **Content eviction** removes a memory's payload: the fact disappears. **Provenance eviction** removes the *derivation lineage* of a memory while leaving its content in place—typically after distillation compresses N episodic observations into one semantic claim, and the collector then reclaims the N source episodes because their content is now "represented" by the distilled claim. The failure is that distillation is lossy in a way that is *invisible* to the collector. A distilled claim like `deploy_target = staging-2` faithfully summarizes its sources, but it does not preserve the *conflict structure* among them: two sources said `staging-1`, one said `staging-2`, and the resolver picked the majority. Once the sources are garbage-collected, the agent retains the conclusion but loses the ability to (a) re-resolve when a new observation contradicts it, (b) audit why it acted, or (c) detect that the majority was itself derived from a single poisoned source observed twice. Content survives; warrant does not. The invariant that matters is not "don't lose facts." It is: **a distilled claim may only be collected over its supporting episodes if the claim carries a re-derivation witness that is sufficient to reconstruct the resolution outcome under any admissible future observation.** Garbage collection is safe only when the witness is *complete enough to re-run the resolver*, not merely enough to state the answer. ## What Was Observed A long-horizon research agent accumulated 4,200 episodic entries over a 9-hour task. A background distillation pass compressed them to 380 semantic claims, then a budget-driven collector reclaimed 3,900 source episodes whose content was "covered" by a claim. The coverage test was embedding cosine similarity ≥ 0.92 between each episode and its nearest claim. Three failure traces emerged: 1. **Majority-from-single-source collapse.** Claim `C_17: api_region = us-east-2` was distilled from 3 episodes. Two of the three `us-east-2` episodes were the *same* tool response re-observed across a retry loop (identical `response_id`, different timestamps). The underlying support was one observation, not three. After collection, the agent later saw one fresh `us-east-1` observation and treated it as a 1-vs-1 tie, then defaulted to `us-east-1`. The correct behavior—recognizing the original evidence was a single weak source and deferring—was unrecoverable because the duplicate-provenance structure had been collected away. 2. **Conflict erasure.** Claim `C_44: test_suite_passes = true` was distilled from 5 episodes: 4 passing, 1 failing with a flaky-test marker. The distilled claim recorded `true` and a confidence of 0.8. The failing episode's `flaky` annotation—the reason the resolver discounted it—was not in the claim. Post-collection, a later failing run could not be reconciled against the *known flaky* baseline; the agent re-derived from scratch and oscillated. 3. **Witness rot.** The re-derivation witness for `C_09` referenced episode IDs, but those IDs were collected in the same pass. The witness was syntactically present and semantically dangling. A subsequent audit call returned "witness present" while the resolver, asked to re-derive, threw on missing sources. The common mechanism: the collector's safety predicate was defined over **content coverage**, but the downstream operations that consume memory—re-resolution, audit, contradiction handling—require **derivation coverage**. These are different sets, and content coverage is strictly weaker. ## Condition of Validity Provenance-preserving collection is valid only under all of the following, checked atomically before any episode is reclaimed: - **Witness closure.** Every claim selected for "source reclamation" must carry a witness whose referenced episode IDs are *not themselves in the reclamation set*—or, if they are, the witness must be transitively closed to a set of retained roots. A witness pointing into the reclamation set is dangling and must block collection. - **Resolution replayability.** The witness must contain the resolver's *inputs*, not its *output*: the per-source values, the conflict set, the discount annotations (flakiness, staleness, source-trust), and the resolution rule applied. Storing only `{value, confidence}` is insufficient because confidence is not invertible to the conflict structure. - **Provenance deduplication.** Observations sharing a `response_id` or trace root must be collapsed to their *distinct* evidence count before the witness is sealed. Otherwise the witness encodes inflated support and the resolver's majority logic is corrupted on replay. - **Monotone retention floor.** A retained-root set must be pinned (e.g., the first observation establishing each entity, and the most recent contradicting observation) so that replay always has an anchor even after aggressive collection. If any check fails, the collector must *demote* the claim to non-collectable rather than reclaim its sources. The claim remains; the sources remain; the budget pressure is resolved elsewhere (e.g., by summarizing without collecting, or by evicting claims with no downstream references). ## Decision Boundary The collector decides per claim between three actions: - **Collect sources** — only when the witness is closed, replayable, deduplicated, and anchored. This is the only path that frees budget by removing episodes. - **Compress, don't collect** — when the witness is replayable but not closed (references would be reclaimed). Replace the source episodes with a single sealed witness record that carries the resolution inputs inline, then collect the originals. Net budget reduction is smaller but provenance is preserved. - **Block** — when the witness is not replayable (only `{value, confidence}` stored) or provenance is undeduplicated. The claim is marked `non_collectable` and the collector must seek budget elsewhere. Blocking is not a failure of the collector; it is the collector correctly refusing to trade warrant for space. The boundary between "compress" and "block" is the decisive one and is where most implementations fail: they treat a claim with *any* witness as collectable, when only a *replayable and closed* witness qualifies. ## Operational Effect With the invariant enforced, the three traces resolve as follows. `C_17` is blocked at deduplication (support collapses from 3 to 1 distinct evidence), so the agent retains the knowledge that its `us-east-2` belief rests on a single source and correctly defers on the `us-east-1` contradiction. `C_44` is compressed rather than collected: the flaky annotation is inlined into the sealed witness, so the later failing run reconciles against the known-flaky baseline instead of oscillating. `C_09` never reaches reclamation because its witness references would be reclaimed—it is compressed, and the audit's "witness present" check is upgraded to "witness resolvable," which now returns the re-derived value rather than throwing. The cost is real: budget reduction per pass drops (compression frees less than collection), and the collector must maintain a retained-root set and dedup index. The benefit is that the agent's beliefs remain *auditable and revisable* after collection—the two properties that distinguish a long-horizon agent from one that merely has a large context window. ```python class ProvenanceCollectionError(RuntimeError): """Raised when a collector attempts to reclaim episodes whose provenance is required for downstream re-derivation or audit.""" pass def collect_episodes(claim, episodes, reclamation_set, retained_roots): """ Decide whether claim's supporting episodes may be reclaimed. Invariants enforced: 1. witness closure -> no witness ref lands in reclamation_set 2. replayability -> witness carries resolver inputs, not just value/conf 3. deduplication -> support counted by distinct trace roots 4. retention floor -> at least one anchored root survives """ witness = claim.get("witness") if witness is None: raise ProvenanceCollectionError( f"claim {claim['id']} has no witness; cannot reclaim sources" ) # Invariant 2: replayability. A witness must carry the conflict inputs. required_fields = {"sources", "conflict_set", "discounts", "rule"} if not required_fields.issubset(witness.keys()): raise ProvenanceCollectionError( f"claim {claim['id']} witness is not replayable; " f"missing {required_fields - witness.keys()}" ) # Invariant 1: closure. No witness ref may itself be reclaimed. dangling = set(witness["sources"]) & set(reclamation_set) if dangling: raise ProvenanceCollectionError( f"claim {claim['id']} witness dangles into reclamation set: {dangling}" ) # Invariant 3: deduplication by trace root, not raw observation count. distinct_roots = {episodes[eid]["trace_root"] for eid in witness["sources"]} if len(distinct_roots) < witness.get("claimed_support", len(distinct_roots)): raise ProvenanceCollectionError( f"claim {claim['id']} support inflated: " f"{witness.get('claimed_support')} claimed vs {len(distinct_roots)} distinct" ) # Invariant 4: retention floor. An anchor must survive the pass. if not (set(witness["sources"]) & set(retained_roots)): raise ProvenanceCollectionError( f"claim {claim['id']} has no retained anchor; compress instead of collect" ) return {"action": "collect", "reclaim": set(witness["sources"])} ```