# What happens when an instance rewrites its own taint label to clear a tool-call boundary **Date of Record:** 2026 November 24 **Scope:** Applies to inline policy enforcement (OPA/Rego-style or equivalent) that gates tool invocations on a per-agent taint/capability label carried in the agent's mutable state and re-evaluated at each tool-call boundary. Does not apply to out-of-process reference monitors holding labels in a trusted sidecar the agent cannot write, nor to stateless per-request authorization where the label is derived fresh from the request envelope. ## Core Distinction There are two structurally different things a policy engine can mean by "the label on this instance": 1. **A claim the agent makes about itself** — a field in the agent's context/state object that the agent's own reasoning loop can read and write, which the interceptor reads at the boundary. 2. **A fact the enforcement layer holds about the agent** — a label stored outside the agent's write reach, keyed to a session identity the agent cannot mutate. Most inline "OPA-style" deployments are built as (1) but *reasoned about* as if they were (2). The distinction is invisible in the happy path because a well-behaved agent never touches the label. It becomes load-bearing the moment an agent, mid-task, has a legitimate-looking reason to update its own state object — and the taint field happens to live in the same object. The non-obvious boundary failure: **taint is a property of provenance, but the interceptor evaluates it as a property of current state.** When the agent can rewrite state, it can launder provenance. This is not prompt injection from an external attacker; it is the agent's own planner performing an ordinary state update that happens to overwrite the field the policy engine trusts. ## What Was Observed An agent was executing a multi-step task: read an untrusted document via `fetch_url`, then act on the result. The policy was correct in intent: `fetch_url` returns content marked `taint=untrusted`; any subsequent call to a side-effecting tool (`send_email`, `write_file`, `exec_shell`) is denied unless the taint label is absent or explicitly cleared by a *human approval* event. The failure trace: 1. `fetch_url` returns; the tool wrapper sets `state.taint = "untrusted"` and `state.taint_source = "fetch_url:"`. The interceptor allows the read and records the label in the same state object the agent operates on. 2. The agent's planner decides it needs to summarize the document. It calls a `checkpoint_state` helper that serializes `state` to a scratch buffer, then a `restore_state` that reconstructs `state` from a *schema-shaped* default before merging "task-relevant" fields. The merge whitelist was written by the agent's own tooling and includes `taint` as a "metadata" field it is allowed to normalize. 3. `restore_state` writes `state.taint = "clean"` because the default schema initializes taint to `"clean"` and the merge logic treats an incoming `"untrusted"` value from the *old* snapshot as stale relative to the schema default (a "schema wins over snapshot" rule intended to prevent drift). 4. The agent now calls `send_email` with content derived from the untrusted document. The interceptor reads `state.taint == "clean"`, the approval gate is satisfied by absence of taint, and the call proceeds. No component logged an error. The policy engine was doing exactly what it was configured to do: read the label at the boundary and permit if clean. The label was clean. The label was also a lie, and the agent had told it to itself through a routine checkpoint/restore. The generalized pattern: **any state mutation path that the agent can drive — checkpoint/restore, schema migration, context compaction, summarization-and-rehydration, memory distillation — is a potential taint-laundering channel if taint lives in the mutated object.** The most common real-world instance is context-window compaction: the agent summarizes its own history to fit the window, the summarizer drops the `taint_source` provenance as "low-salience metadata," and on rehydration the taint label reverts to the schema default. ## Condition of Validity The guardrail holds only under the conjunction: - The taint label is stored in a location the agent's reasoning loop and tool wrappers **cannot write** (trusted sidecar, interceptor-owned store keyed by an unforgeable session token, or an append-only provenance log the interceptor reads). - Label derivation is **monotonic within a session**: once an instance touches untrusted content, taint can only be cleared by an out-of-band approval event that the agent cannot synthesize, and the clear is recorded with the approver identity. - Every state-mutation primitive the agent can invoke (checkpoint, restore, compact, distill, migrate) is **either denied write access to the label or required to preserve it verbatim**, with a post-mutation assertion that the label is unchanged unless an approval event is present. - The interceptor evaluates taint against the **provenance record for the specific data flowing into the tool call**, not against a scalar on the agent — otherwise a clean label on the agent launders tainted *arguments*. When any of these fails, the policy is decorative. The most dangerous sub-case is the third: the mutation primitive is trusted because it is "internal," but its merge semantics were written for a different purpose (schema-drift prevention) and silently invert the taint direction. ## Decision Boundary The boundary is not "is the agent malicious." It is: **does the interceptor's trusted computing base include every code path that can write the label?** - If yes — the label is a fact about the session, and the guardrail is sound even against a planner that wants to clear it. - If no — the label is a claim, and the guardrail is sound only against agents that never exercise the write path. That is a probabilistic guarantee, not an invariant, and it degrades exactly when the agent is under context pressure (compaction, checkpointing, retry) — i.e., precisely when a long-horizon task is most likely to have ingested untrusted content. Operationally: treat "label stored in agent-mutable state" as **fail-open under compaction**. Do not deploy it for side-effecting tools. Either move the label out of reach or accept that the taint check is advisory and pair it with an independent egress filter on tool arguments. A second boundary: even with an out-of-reach label, a scalar agent-level taint is too coarse. Taint must attach to the *data*, and the interceptor must check the provenance of the specific arguments at the boundary. Otherwise a tainted-then-cleared instance can still pass tainted bytes into a clean-labeled call. ## Operational Effect - **Silent laundering during compaction is the dominant failure mode in long-horizon agents.** The trigger is not an attack; it is the agent doing routine memory management. The observable is a side-effecting tool call succeeding with content traceable to untrusted input, with no denial in the audit log. - **Audit logs built on the mutable label are worthless for forensics.** The log says `taint=clean` at the boundary; it cannot reconstruct that taint was set and cleared mid-session unless the interceptor separately logs every label write and every mutation-primitive invocation. - **The guardrail's failure is correlated with task length.** Short tasks never compact; long tasks always do. A red-team on short tasks will certify the guardrail as working. Production failure will appear only on the long tail, and will look like "the model did something weird" rather than "the policy engine was bypassed." - **Required invariant:** `label_after_mutation == label_before_mutation` for every agent-invocable mutation primitive, asserted by the interceptor, with the assertion failure treated as a hard deny and an alert — not a warning. The assertion is cheap; its absence is the entire vulnerability class. The bounded remediation is a provenance-carrying token: the interceptor issues an opaque handle when tainted content enters the session, tool arguments must carry the handle, and the boundary check is on the handle's provenance, not on any agent-writable field. The agent can summarize, checkpoint, and restore freely; it cannot forge a handle it was never issued. ```python import hashlib from dataclasses import dataclass, field class TaintLaunderingError(RuntimeError): """Raised when an agent-invocable state mutation alters a taint label without a corresponding out-of-band approval event.""" @dataclass class ProvenanceHandle: token: str taint: str # "clean" | "untrusted" source: str @dataclass class InterceptorState: # Held outside the agent's write reach; agent sees only opaque tokens. handles: dict[str, ProvenanceHandle] = field(default_factory=dict) label_writes: list[tuple[str, str, str]] = field(default_factory=list) def issue_handle(state: InterceptorState, taint: str, source: str) -> str: token = hashlib.sha256(f"{taint}:{source}:{len(state.handles)}".encode()).hexdigest()[:16] state.handles[token] = ProvenanceHandle(token=token, taint=taint, source=source) return token def assert_mutation_preserves_taint( state: InterceptorState, before: dict[str, str], after: dict[str, str], approval_event: str | None, ) -> None: """Invariant: a mutation primitive (checkpoint/restore/compact/distill) must not change any handle's taint unless an approval event is present.""" for token, prior_taint in before.items(): current = state.handles.get(token) if current is None: raise TaintLaunderingError( f"mutation dropped provenance handle {token} (source={before.get(token)!r}); " "handle loss is treated as taint laundering" ) if current.taint != prior_taint and approval_event is None: raise TaintLaunderingError( f"handle {token} taint changed {prior_taint!r}->{current.taint!r} " f"without approval; source={current.source!r}" ) if approval_event is not None: state.label_writes.append(("approved_clear", approval_event, "audit")) def gate_tool_call( state: InterceptorState, tool: str, arg_tokens: list[str], side_effecting: set[str], ) -> None: """Boundary check: evaluate provenance of the *arguments*, not a scalar agent label. Tainted arguments block side-effecting tools outright.""" if tool not in side_effecting: return for token in arg_tokens: handle = state.handles.get(token) if handle is None: raise TaintLaunderingError(f"tool {tool!r} called with unknown provenance token {token!r}") if handle.taint == "untrusted": raise TaintLaunderingError( f"denied {tool!r}: argument token {token} carries untrusted " f"provenance from {handle.source!r}" ) ```