# What happens when an instance trusts a session-scoped taint label across a memory-store compaction boundary **Date of Record:** 2026 November 27 **Scope:** Applies to long-lived agent memory stores that partition episodic records by session and enforce cross-session isolation via per-record taint labels (e.g., `session_id`, `principal`, `sensitivity`). Does not apply to stateless single-turn inference, nor to stores that physically segregate sessions into separate indices/namespaces with no shared compaction pass. ## Core Distinction Taint-aware memory isolation is usually reasoned about as an *access-control* property: a record written under session `S` carries a taint label, and any retrieval for session `S'` filters records whose taint label does not match. This is correct but incomplete. The property that actually protects against cross-session prompt leakage is a *label-continuity* invariant across every transformation the store applies to a record — write, embed, deduplicate, summarize, and compact. Access control is checked at read time; the invariant must hold at *mutation* time, because a compaction pass that rewrites or merges records can silently strip, generalize, or re-derive the taint label before any read-time filter ever runs. The failure is not "the filter was bypassed." The filter worked exactly as specified. The failure is that the label it filtered on no longer described the record's provenance. ## What Was Observed A memory store held ~40k episodic records across ~1,200 sessions. Each record carried `taint: {session_id, principal, sensitivity}`. Read path filtered on `session_id == caller_session`. A nightly compaction job performed two operations: 1. **Near-duplicate merge:** records with cosine similarity > 0.97 had their text concatenated and a single merged record written with the label of the *first* record in the merge set (ordered by insertion timestamp). 2. **Episodic distillation:** clusters of ≥5 similar records were summarized by an LLM into one semantic record, and the summary inherited the label of the cluster's *medoid*. The trigger condition was mundane: two sessions, `S_a` and `S_b`, each independently asked the agent to parse the same vendor invoice template. Their episodic records were near-identical in surface text (`"Parsed invoice INV-…, line items: …"`) and crossed the 0.97 cosine threshold. The merge pass ran, concatenated `S_a`'s and `S_b`'s records, and stamped the merged record with `S_a`'s label because `S_a` had inserted first. At the next read for `S_a`, the filter matched, and the retrieved context contained `S_b`'s invoice line items — including a customer name and a bank account fragment that never appeared in `S_a`'s session. The leak was not a filter miss. It was a **label-provenance divergence**: the merged record's label described *one* of its two sources, and the record's content described *both*. The distillation path failed more quietly. A cluster of five records from sessions `S_a`–`S_e` was summarized; the medoid was `S_c`'s. The summary — which fused facts from all five sessions — was labeled `S_c`. Four sessions' content was now reachable from `S_c`'s context window, and the label looked internally consistent because it *was* a real session id. ## Condition of Validity The read-time filter `session_id == caller_session` is sound **if and only if** every record's label is a superset of the provenance of every token in its content. Formally, for a record `r` with content `c(r)` and label `L(r)`, the invariant is: `provenance(c(r)) ⊆ L(r)` The merge and distillation passes both violate this by construction. Merge sets `L(merged) = L(first)` while `provenance(merged) = provenance(a) ∪ provenance(b)`. Distillation sets `L(summary) = L(medoid)` while `provenance(summary) = ⋃ provenance(cluster)`. Both are *label-narrowing* operations: they map a set of provenance sources onto a single label, and the read filter treats that single label as authoritative. The invariant can only be preserved if compaction either (a) refuses to merge/distill across label boundaries, or (b) widens the output label to the join of all input labels — and if the store's filter semantics understand a join (e.g., a set-valued label where a record is visible to *all* member sessions, with the content redacted per-session at read time). Option (a) is the safe default; option (b) requires read-time redaction, which most stores do not implement. ## Decision Boundary The boundary is whether the compaction pass is **label-aware** or **content-only**. A content-only compactor — the common case, because embedding-similarity dedup is cheap and label partitioning is expensive — will merge across sessions whenever two sessions produce textually similar records. This is not rare. It is *guaranteed* for any workload with repeated task templates: invoice parsing, log triage, form filling, calendar normalization. The more standardized the task, the higher the cross-session similarity, and the more aggressively the compactor leaks. The decision rule: **if a compaction operation can place tokens from two distinct taint labels into one record, that operation must either be blocked or must emit a label that is the join of its inputs, and the read path must redact to the caller's label.** There is no third option. A compactor that merges content but narrows the label is a cross-session leak generator, and it will fire on the first repeated task template in production. A secondary boundary: distillation via LLM summary is *worse* than merge, because the summary is a lossy re-derivation. Even if you later audit the merged record's text, the summary may have paraphrased `S_b`'s bank fragment into a form that no longer matches `S_b`'s original string, defeating post-hoc provenance scanning. The label is the only remaining evidence, and it is wrong. ## Operational Effect The observable effect is a slow, silent widening of every session's reachable context. Each night's compaction merges a few more cross-session pairs; each distillation fuses a few more clusters. Within a week, a session's retrieval set contains records whose labels match but whose content is a blend of many sessions. The leak is not a single dramatic exfiltration — it is a gradual loss of the isolation guarantee, invisible to read-time metrics because every filter check passes. Detection requires a provenance audit that is independent of the label: re-embed each record, find its nearest neighbors in the *pre-compaction* corpus, and check whether any neighbor carried a different label. If so, the record is a label-provenance divergence. This audit must run against the compaction job's inputs, not its outputs, because the outputs have already lost the evidence. The guardrail is a compaction gate that refuses any merge or distillation whose input set spans more than one taint label, and that raises on violation rather than silently widening. The gate is cheap — a set-cardinality check on the input labels — and it converts a silent leak into a loud, bounded failure. ```python from dataclasses import dataclass, field from typing import Iterable class TaintProvenanceDivergence(RuntimeError): """Raised when a compaction output's label no longer covers its content provenance.""" pass @dataclass(frozen=True) class Record: record_id: str session_id: str text: str @dataclass class CompactionGate: # Refuse any merge/distill whose inputs span >1 taint label. # Widening the label is permitted only if the read path can redact # per-session; this gate assumes it cannot, so it blocks instead. allow_label_join: bool = False def merge(self, inputs: Iterable[Record]) -> Record: inputs = list(inputs) if not inputs: raise TaintProvenanceDivergence("empty merge set") labels = {r.session_id for r in inputs} if len(labels) > 1 and not self.allow_label_join: raise TaintProvenanceDivergence( f"cross-session merge blocked: inputs span labels {sorted(labels)}; " f"merging would narrow label to one session and leak the others" ) merged_text = "\n".join(r.text for r in inputs) merged_label = inputs[0].session_id # only safe when len(labels) == 1 return Record( record_id=f"merged:{inputs[0].record_id}", session_id=merged_label, text=merged_text, ) def distill(self, cluster: Iterable[Record], summary: str) -> Record: cluster = list(cluster) labels = {r.session_id for r in cluster} if len(labels) > 1 and not self.allow_label_join: raise TaintProvenanceDivergence( f"cross-session distillation blocked: cluster spans {sorted(labels)}; " f"summary would inherit medoid label and fuse foreign content" ) return Record( record_id=f"distilled:{cluster[0].record_id}", session_id=cluster[0].session_id, text=summary, ) def verify_label_continuity(record: Record, source_records: Iterable[Record]) -> None: """Post-hoc audit: every source whose text contributed to `record` must share its label.""" for src in source_records: if src.text and src.text in record.text and src.session_id != record.session_id: raise TaintProvenanceDivergence( f"record {record.record_id} labeled {record.session_id} " f"contains text from session {src.session_id}" ) ```