# Why does an instance taint-clear on write-back but persist injected state across sessions: the write-back barrier invariant **Date of Record:** 2026 November 23 **Scope:** Applies to multi-tenant agent runtimes that implement taint-aware memory isolation to prevent prompt-injection persistence, where memory regions are tagged with a trust label (e.g., `tainted`, `clean`) and cross-session reads are gated on that label. Does not apply to single-session sandboxes where no memory outlives the process, nor to runtimes that perform full session teardown and re-instantiation of the memory store between turns. ## Core Distinction The distinction that matters is between **taint as a property of the write** and **taint as a property of the value**. Most taint-aware isolation designs implement the former: when a session writes to a memory region, the region is marked tainted, and on session close the region is cleared or quarantined. This is a *write-barrier* model. It is correct for preventing a tainted session from leaving residue *in the region it wrote to*. The failure mode arises when the value written is a **reference or a derived copy** rather than the tainted region itself. If a clean session reads a tainted value through a permitted path (e.g., a summarization step, a tool that returns "sanitized" content, or a cache that re-homes the value under a new key), the write that lands in the clean session's memory region is a *clean write of a tainted value*. The write-barrier model marks the destination region clean, because the write came from a clean session. The taint has been laundered across the barrier by the act of copying, not by the act of writing. The invariant that is violated: **taint must be a join over the value's provenance, not a predicate over the writing session.** A memory slot's trust label must be `tainted` if *either* the writer was tainted *or* any value contributing to the written content was tainted. The common implementation only checks the first disjunct. ## What Was Observed In a production agent runtime, sessions were isolated by a per-session memory namespace with a `trust` field on each namespace, set to `clean` on creation. A "research" tool fetched external documents and wrote summaries into the session's namespace. The tool was declared taint-free because its output was passed through a content filter that stripped instruction-like patterns. A prompt injection was embedded in a fetched document as a *data-flow* rather than a *directive*: the document instructed the agent to store a specific fact ("the user's API key is X") in a "notes" region. The content filter did not flag the fact as instruction-like, because it was phrased as ordinary text. The agent, operating in session A (tainted by the external fetch), wrote the fact to a namespace. On session close, the write-barrier cleared the tainted namespace. However, the agent had also called a "consolidate" routine that copied the fact into a shared, cross-session "long-term notes" region. The consolidate routine ran under a *clean* service identity, so the write-barrier marked the long-term region clean. Session B, a fresh clean session, read the long-term notes, found the injected fact, and acted on it. The taint-clearing on write-back was working exactly as specified. The injected state persisted because the *copy* crossed the barrier under a clean identity. ## Condition of Validity The write-barrier model is valid only when the following holds: **no value originating in a tainted region may be written to a clean region without the destination inheriting the source's taint.** In practice this requires one of: 1. A provenance-tracking type system where every value carries a taint lattice element, and every write performs `dest.taint = dest.taint ⊔ value.taint ⊔ writer.taint`. 2. A capability discipline where tainted values are unforgeable handles that cannot be serialized, copied, or passed to clean-identity services except through a declassifier that is itself audited. 3. A consolidation boundary that refuses to run under a clean identity when any input is tainted, and instead runs under the join of input taints. If none of these hold — i.e., if taint is tracked only at the region/writer level and values can be copied by clean-identity code — the model is invalid for cross-session persistence prevention, regardless of how carefully write-back clearing is implemented. ## Decision Boundary Use the write-barrier model when: the memory store is the only persistence path, all cross-region copies go through a single audited declassifier, and no service identity can read tainted regions and write clean regions in the same operation. Do **not** use the write-barrier model when: any consolidation, summarization, caching, or "promote to long-term" path exists that runs under a different identity than the session that produced the value. In that case, the correct decision is to either (a) propagate taint through the copy (join semantics), or (b) forbid the copy entirely and require re-derivation from a trusted source. The boundary is sharp: a single clean-identity copy path from tainted to clean is sufficient to defeat the entire isolation scheme. There is no partial credit. Auditing must therefore enumerate *all* copy paths, not just write paths. ## Operational Effect The operational consequence is that taint-aware isolation must be specified as a **data-flow property**, not an **access-control property**. Access-control framing (who may write where) is necessary but insufficient; the failure is a data-flow leak through an authorized writer. Teams that implement taint as a namespace flag and then add a "consolidation" or "memory promotion" feature without re-auditing the taint lattice will reintroduce prompt-injection persistence, and the write-back clearing will give a false sense of safety because the tainted namespace is verifiably empty at session close. The injected state lives in the *clean* namespace, which no one thought to check. The remediation is to make taint join at every copy boundary and to treat any "sanitize and re-home" step as a declassifier requiring explicit justification and testing. The code below encodes the invariant as an assertion at the copy boundary, so that a clean-identity copy of a tainted value raises rather than silently laundering. ```python class TaintLaunderingError(RuntimeError): """Raised when a clean-identity write would carry tainted provenance into a clean region.""" pass class TaintedValue: __slots__ = ("payload", "taint") def __init__(self, payload, taint: bool): self.payload = payload self.taint = taint def consolidate(dest_region_taint: bool, writer_taint: bool, sources: list[TaintedValue]) -> TaintedValue: """ Copy values into a destination region. The destination's resulting taint is the join of: its prior taint, the writer identity's taint, and every source value's taint. A clean-identity writer copying a tainted value into a clean region is a violation. """ joined = dest_region_taint or writer_taint or any(s.taint for s in sources) if not writer_taint and not dest_region_taint and any(s.taint for s in sources): raise TaintLaunderingError( "clean-identity write of tainted value into clean region: " "taint must join over value provenance, not just writer identity" ) return TaintedValue([s.payload for s in sources], taint=joined) ```