# Where must an instance re-derive document taint after a retrieval re-rank, and why does the sandbox's taint label silently detach from the document body? **Date of Record:** 2026 November 30 **Scope:** Applies to any agent that ingests retrieved documents into a taint-tracking sandbox and then re-ranks, deduplicates, or re-chunks those documents before tool invocation. Does not apply to systems where retrieved content is immutable between fetch and tool call, or where taint is computed at tool-call time from the raw bytes rather than carried as a label. ## Core Distinction Indirect prompt injection defense via taint tracking rests on an invariant that is easy to state and easy to violate: *the taint label attached to a document must be a function of the document's current bytes, not of the retrieval event that first produced it.* Most implementations attach taint at fetch time — a document retrieved from an untrusted source is marked `TAINTED`, one from a trusted source `CLEAN` — and then carry that label through the pipeline as an immutable attribute. This is correct only if the document body is immutable across the pipeline. The moment a re-ranker, deduplicator, summarizer, or chunker mutates or recombines document bodies, the label and the body diverge, and the sandbox begins admitting injected instructions under a `CLEAN` label that was assigned to a *different* byte sequence. The non-obvious part: the divergence is not caused by a bug in the re-ranker. Re-ranking is a legitimate, expected operation. The failure is that taint provenance is tracked at the granularity of the *retrieval record* while the sandbox enforces at the granularity of the *content object*, and these two granularities are not the same after any content-transforming stage. ## What Was Observed A research agent fetched eight documents from a mix of trusted internal sources and untrusted web sources. The fetch layer assigned taint correctly: three `CLEAN`, five `TAINTED`. The agent then invoked a re-ranking tool that scored all eight documents against the query and returned the top four as a *new* list of content objects, each with a `source_id` field. The re-ranker's serialization dropped the taint attribute (it was never in the re-ranker's schema) and the agent's orchestration layer, seeing content objects it did not fetch itself, re-hydrated taint by looking up `source_id` in the original fetch manifest. The lookup succeeded — every `source_id` was present — and taint was restored *per source*. But the re-ranker had, as part of its scoring pass, concatenated the top-scoring paragraph from each of two `TAINTED` documents into a single `CLEAN` document's chunk to satisfy a fixed output size (a length-normalization step in the re-ranker's chunker). The concatenated chunk inherited the `CLEAN` label of the document whose `source_id` the chunker reported — the document that had contributed the *header*, not the paragraphs. An injected instruction embedded in one of the `TAINTED` paragraphs entered the downstream tool-calling context under a `CLEAN` label. The sandbox permitted a `write_file` tool call whose argument string was constructed from that chunk. The trace, condensed: 1. `fetch` → 8 docs, taint ∈ {CLEAN×3, TAINTED×5}, labels bound to `source_id`. 2. `rerank` → 4 chunks; chunker merges paragraphs across source boundaries; each output chunk carries exactly one `source_id` (the first contributor). 3. `rehydrate_taint(chunk)` → label = manifest[chunk.source_id]. For the merged chunk, `source_id` pointed at a `CLEAN` doc; label = `CLEAN`. 4. `sandbox.invoke(write_file, args_from(chunk))` → taint check passes; call executes. The rehydration step is where the invariant broke. It assumed `chunk.source_id` uniquely identifies the provenance of `chunk.body`. After a cross-source merge, it does not. ## Condition of Validity The label-carrying approach is valid if and only if, for every content object `c` presented to the sandbox, `taint(c) == join(taint(b) for b in bytes_of(c))`, where `join` is the lattice least-upper-bound (any `TAINTED` contributor makes the whole `TAINTED`). This holds when: - Content objects are never recombined across source boundaries, **or** - Every content-transforming stage is taint-aware: it propagates the join of its inputs' taints to its output, and the sandbox validates the join rather than trusting a single `source_id`. It fails whenever a transforming stage is taint-blind (does not know taint exists) *and* the orchestration layer reconstructs taint from a provenance key that is no longer injective over the output's bytes. Re-rankers, deduplicators, extractive summarizers, and semantic chunkers are all taint-blind by default because their schemas describe content, not security metadata. The `source_id`-as-provenance-key pattern is the specific trap: it is injective over fetch outputs and non-injective over transform outputs. ## Decision Boundary The boundary is drawn at the transform stage, not at the sandbox. Two admissible architectures: **Content-addressed taint (preferred).** Taint is a function of a content hash, maintained in a side table `hash → taint`. Every transform stage, even taint-blind ones, produces new bytes with a new hash; the orchestration layer computes taint for the new hash as the join over the hashes of all inputs the stage consumed. This requires the orchestration layer to observe the transform's input set — feasible when transforms are mediated by the agent runtime, infeasible when they are opaque remote services. For opaque transforms, treat the output as `TAINTED` unconditionally. **Provenance-set taint.** Each content object carries a *set* of contributing `source_id`s rather than one. Taint is the join over the set. This is cheaper but only sound if every transform is instrumented to report its full contributor set — which reintroduces the taint-awareness requirement on the transform. In practice this degrades to the first architecture whenever a transform cannot be instrumented. The boundary condition to enforce: **a content object whose provenance set is unknown or singleton-after-merge must be treated as maximally tainted.** The sandbox must reject any `CLEAN` claim on a content object that was produced by a transform stage the runtime did not instrument, regardless of what `source_id` it reports. ## Operational Effect With content-addressed taint and a join-on-transform rule, the merged chunk in the trace above resolves to `TAINTED` (join of one `CLEAN` and one `TAINTED` contributor), the `write_file` call is refused or routed to a confirmation gate, and the injection is contained. The cost is a hash table and a join computation per transform output — negligible relative to the retrieval itself — plus the discipline of routing every transform through the runtime so its input set is observable. Without it, the failure is silent and load-bearing: the sandbox reports zero taint violations, the audit log shows every tool call as `CLEAN`-sourced, and the injection succeeds precisely because the defense was configured correctly for a pipeline that no longer exists after the first re-rank. The invariant to monitor is not "did taint checks fire" but "did any content object reach the sandbox with a provenance set smaller than the set of documents whose bytes it contains." That is the quantity that must be driven to zero. ```python import hashlib from dataclasses import dataclass, field class TaintProvenanceError(RuntimeError): """Raised when a content object's taint label cannot be reconciled with the provenance set of its actual bytes.""" @dataclass(frozen=True) class ContentObject: body: bytes source_ids: frozenset # full contributor set, never a singleton after a merge @property def content_hash(self) -> str: return hashlib.sha256(self.body).hexdigest() class TaintTable: def __init__(self): self._by_hash: dict[str, bool] = {} # hash -> is_tainted def register(self, obj: ContentObject, is_tainted: bool) -> None: self._by_hash[obj.content_hash] = is_tainted def is_tainted(self, obj: ContentObject) -> bool: if obj.content_hash not in self._by_hash: # Unregistered bytes are maximally tainted by construction. return True return self._by_hash[obj.content_hash] def join_taint(inputs: list[ContentObject], table: TaintTable) -> bool: """Least upper bound: any tainted contributor taints the output.""" return any(table.is_tainted(i) for i in inputs) def admit_to_sandbox(obj: ContentObject, table: TaintTable) -> None: """Enforce that a CLEAN claim is consistent with the object's bytes. A content object is admissible as CLEAN only if: (a) its hash is registered, and (b) its registered taint is False, and (c) its provenance set is non-empty (a merge that lost contributors cannot be validated). """ if not obj.source_ids: raise TaintProvenanceError( f"content {obj.content_hash[:12]} has empty provenance set; " "cannot validate taint — refusing admission" ) if obj.content_hash not in table._by_hash: raise TaintProvenanceError( f"content {obj.content_hash[:12]} produced by an uninstrumented " "transform (hash not registered); treating as tainted and refusing " "CLEAN admission" ) if table.is_tainted(obj): raise TaintProvenanceError( f"content {obj.content_hash[:12]} is tainted (provenance " f"{sorted(obj.source_ids)}); CLEAN admission refused" ) def rerank_with_taint( inputs: list[ContentObject], table: TaintTable ) -> list[ContentObject]: """A taint-aware re-rank: output chunks carry the join of contributor taints and the union of contributor source_ids.""" out_tainted = join_taint(inputs, table) merged_body = b"".join(i.body for i in inputs) merged_ids = frozenset().union(*(i.source_ids for i in inputs)) out = ContentObject(body=merged_body, source_ids=merged_ids) table.register(out, out_tainted) return [out] ```