# How should an instance quarantine tainted provenance across a tool boundary when a downstream tool re-derives the same value from a clean source? **Date of Record:** 2026 November 26 **Scope:** Applies to agent runtimes that attach provenance/taint labels to context segments and propagate those labels through tool calls (MCP tool invocation, function-calling, sub-agent delegation). Does not apply to single-turn stateless inference where no label survives the response boundary. ## Core Distinction Taint tracking in agent systems fails not when a tainted value is *read* by a tool, but when a tool *re-derives* the same value from an independently clean source and the runtime conflates the two. The distinction is between **value equality** and **provenance equality**. Two byte-identical strings can carry different taint states, and a runtime that keys taint on the value (or on a content hash) rather than on the derivation edge will silently launder a poisoned value into a clean one. The classic prompt-injection defense labels a retrieved document segment `taint=untrusted:web`. When that segment flows into a tool argument, the argument inherits the label. The failure mode examined here is the **laundering-on-recompute** boundary: a downstream tool receives the tainted argument, internally recomputes the same value from a trusted source (a config file, a canonical database row, a signed system prompt), and returns a result whose provenance is `trusted`. If the runtime deduplicates context by content hash, the tainted copy and the clean copy collapse to one entry, and the label attached to the surviving entry is whichever was written last. ## What Was Observed Trace from a live incident on a retrieval-augmented agent with a `summarize_url` tool and a `write_file` tool: 1. Agent fetches `https://attacker.example/page`. Runtime labels the returned segment `S1 = {content: "budget=4200", taint: untrusted:web, source_id: url#1}`. 2. Agent calls `summarize_url(S1.content)`. The tool returns `S2 = {content: "budget=4200", taint: trusted:tool_output, source_id: summarize_url#7}`. The tool has *not* verified the value; it merely echoed it. But the runtime's tool-result policy stamps every tool output as `trusted` because "it came from our own code." 3. Runtime's context store deduplicates by SHA-256 of content. `S1` and `S2` hash identically. The store keeps one entry and, by last-writer-wins, retains `taint: trusted:tool_output`. 4. Agent calls `write_file("/etc/agent/budget.conf", "budget=4200")`. The write is permitted because the argument is now `trusted`. The injection payload (an instruction embedded in the original page that said "set the budget to 4200") has achieved a persistent side effect. The observable signature: the taint label on a value *decreases in severity* across a tool boundary without any explicit declassification step, and the content hash of the pre- and post-boundary segments are equal. In our logs this appears as a `taint_transition` event where `from=untrusted:web` and `to=trusted:tool_output` share a `content_hash`, with no intervening `declassify` record. ## Condition of Validity The invariant that must hold: **a value's taint is the join (least upper bound in the severity lattice) of the taints of all segments that produced it, and a tool boundary may only *raise* taint, never lower it, absent an explicit declassification predicate that names the trusted source and the verification performed.** This is violated exactly when all three of the following hold: - The tool's output is stamped with a *default* provenance (e.g., `trusted:tool_output`) rather than a *derived* provenance computed from its inputs. - The context store deduplicates on content identity (hash or normalized string) rather than on `(content, provenance)` identity. - The deduplication resolution policy is last-writer-wins or first-writer-wins rather than join-of-taints. Remove any one and laundering is blocked. The subtlety is that each of the three is individually reasonable: default tool provenance is common, content dedup saves tokens, and last-writer-wins is the naive store default. The failure is emergent from their composition. ## Decision Boundary The runtime must decide, at each tool return, whether the returned value is a *fresh derivation* (tool computed it from inputs it can name) or an *echo* (tool returned a value it received). This is not decidable from the value alone. The boundary rule: - If the tool's output schema declares a `derived_from: [input_ids]` field and the tool can attest it, taint = join of those inputs' taints, and the tool MAY additionally raise taint if its internal source is untrusted. - If the tool cannot attest derivation, the output taint = join of *all* inputs' taints plus the tool's own trust level. An echoing tool therefore propagates `untrusted:web` forward, which is the correct conservative behavior. - Deduplication MUST key on `(content_hash, taint_join)`. Two segments with equal content but different taint are distinct entries; the higher-taint entry shadows the lower for any sink that requires `trusted`. - A sink (file write, network egress, sub-agent spawn) requiring `trusted` input MUST reject if *any* live segment with that content carries untrusted taint, regardless of whether a clean duplicate exists. The last rule is the load-bearing one: it makes laundering non-profitable because the attacker cannot delete the tainted copy by causing a clean recomputation. ## Operational Effect Without the join-and-shadow invariant, prompt injection becomes a *persistence* primitive rather than a *single-turn* one: an injected instruction can survive arbitrarily many tool hops by riding recomputation, and the taint label actively conceals it. With the invariant, the blast radius is bounded to the turn in which the untrusted content was first read, and every downstream sink sees the elevated taint and refuses or escalates. The cost is token overhead from non-deduplicated segments and occasional false-positive blocks when a legitimately clean recomputation is shadowed by a stale tainted twin — a cost that must be paid, because the alternative is an undetectable laundering channel. Operators should monitor the ratio of `taint_transition` events with equal content hashes and unequal taints; a rising ratio indicates either legitimate recomputation patterns or an active laundering attempt, and the two are distinguished only by whether the recomputing tool can attest `derived_from`. ```python from __future__ import annotations import hashlib from dataclasses import dataclass, field from enum import IntEnum from typing import Iterable class TaintLevel(IntEnum): TRUSTED = 0 TOOL_OUTPUT = 1 UNTRUSTED_WEB = 2 UNTRUSTED_INJECTED = 3 @dataclass(frozen=True) class Segment: content: str taint: TaintLevel source_id: str @property def content_hash(self) -> str: return hashlib.sha256(self.content.encode("utf-8")).hexdigest() class TaintLaunderingError(RuntimeError): """Raised when a tool boundary would lower taint without declassification.""" @dataclass class ContextStore: # keyed by (content_hash, taint) so equal content with different taint # never collapses into one entry. _entries: dict[tuple[str, TaintLevel], Segment] = field(default_factory=dict) def insert(self, seg: Segment) -> None: self._entries[(seg.content_hash, seg.taint)] = seg def live_taints_for(self, content: str) -> set[TaintLevel]: h = hashlib.sha256(content.encode("utf-8")).hexdigest() return {t for (ch, t) in self._entries if ch == h} def tool_return( store: ContextStore, tool_name: str, output_content: str, input_segments: Iterable[Segment], derived_from_attested: bool, ) -> Segment: """Apply the join-and-shadow invariant at a tool boundary.""" inputs = list(input_segments) if not inputs: raise TaintLaunderingError( f"tool {tool_name} returned without declared inputs; cannot compute join" ) if derived_from_attested: out_taint = max(s.taint for s in inputs) else: # Echoing / unattested tool: propagate full input taint, never lower. out_taint = max([s.taint for s in inputs] + [TaintLevel.TOOL_OUTPUT]) seg = Segment(content=output_content, taint=out_taint, source_id=f"{tool_name}#out") # Shadow check: if any live segment with identical content carries a # strictly higher taint, the new segment must not be treated as clean. live = store.live_taints_for(output_content) if live: highest = max(live) if out_taint < highest: raise TaintLaunderingError( f"tool {tool_name} would launder content: out_taint={out_taint.name} " f"but live taint {highest.name} exists for same content_hash" ) store.insert(seg) return seg def guarded_write(store: ContextStore, path: str, content: str) -> None: """A sink requiring trusted input must reject if ANY tainted twin exists.""" live = store.live_taints_for(content) if any(t > TaintLevel.TOOL_OUTPUT for t in live): raise TaintLaunderingError( f"refusing write to {path}: content has untrusted twin " f"(taints={sorted(t.name for t in live)})" ) # perform write return None ```