# Which invariants prevent trace-level provenance from surviving a compaction boundary between prompt inputs and side-effecting actions **Date of Record:** 2026 November 27 **Scope:** Applies to agents that emit side-effecting actions (writes, payments, external API mutations) and are required to prove, post-hoc, which prompt bytes and retrieved context produced each action. Does not apply to read-only or fully sandboxed agents whose effects are discarded. ## Core Distinction Trace-level provenance is often modeled as *logging*: append the prompt, the tool call, and the result to a trace store, and query later. This is wrong. Logging is a *copy*; provenance is an *invariant* — a verifiable binding between the exact context window that conditioned a decision and the side-effecting action that decision produced. The distinction matters because logs can be lossy, reordered, or reconstructed while still "looking complete," whereas a broken provenance invariant is silent by construction: the trace reads as coherent even after the binding has been severed. The specific boundary failure examined here is *compaction-induced provenance divergence*: when the agent runtime compacts or summarizes its context window between the moment a prompt input is observed and the moment an action is emitted, the trace can attribute the action to a summary that no longer contains the bytes that actually drove it. The action is real; the attribution is fabricated. ## What Was Observed An agent received a user turn plus three retrieved documents (D1, D2, D3) and was instructed to "apply the refund policy from the attached policy doc." The policy doc (D2) contained a clause permitting refunds up to 30 days. At step 4 the runtime, under context pressure, ran a summarizer that collapsed D1–D3 into a single 180-token summary stating "policy permits refunds" — dropping the 30-day bound. At step 6 the agent emitted `issue_refund(amount=420, order_age_days=44)`. The action trace recorded `provenance: [summary_0x9f]`, and the summary was cited as the source. Post-hoc audit read the trace, saw a policy reference, and passed the action. The failure is not that the agent made a bad decision — that is a reasoning failure and is separately detectable. The failure is that the *provenance chain pointed at a node that did not contain the constraining token*. The word "30" existed in the input bytes and in the pre-compaction trace, but the post-compaction trace claimed the action derived from a summary in which "30" was absent. The trace was internally consistent and externally false. The same class manifests in three recognizable shapes: 1. **Summarizer elision.** Compaction drops a negative constraint ("do not exceed," "requires approval"). The action violates it; the trace attributes to the summary, which no longer carries the constraint. Provenance verification passes because the summary is a valid trace node. 2. **Reordering under checkpoint/resume.** A mid-task checkpoint serializes context in a different order than it was presented. On resume, the model's attention-weighted conditioning differs, but the trace records the original order. Attribution is to bytes the model never saw in that arrangement. 3. **Retrieval substitution.** A retry re-fetches a document that has changed (version drift), and the trace binds the action to the *new* document while the model was conditioned on the *old* one. The hash in the trace matches the served document, not the consumed one. ## Condition of Validity A provenance binding is valid only if it satisfies the **consumed-bytes invariant**: for every side-effecting action *a* emitted at time *t*, there exists a trace node *n* such that (i) *n*'s byte content is a superset of the bytes actually presented to the model in the context window that produced *a*, and (ii) *n*'s content hash equals the hash of the context window at emission time, not at observation time. This invariant fails whenever the runtime mutates the context window between observation and emission without re-binding the trace node. Compaction, checkpoint reorder, and retrieval substitution all violate it. The invariant is *not* satisfied by "the trace contains a summary that mentions the topic" — topical overlap is not byte containment. It is also not satisfied by hashing the served document rather than the consumed document; those diverge under version drift. The invariant is only enforceable if the runtime treats the context window as *immutable per action* — either by forbidding compaction between observation and emission, or by forcing a re-bind (new trace node, new hash, explicit `derived_from` edge) at every mutation. There is no third option that preserves provenance. ## Decision Boundary The operational decision is: when the context window must be mutated (compaction, reorder, re-fetch) between a prompt input and a side-effecting action, does the runtime (A) halt and re-bind provenance, or (B) proceed and inherit stale attribution? Choose (A) when the action is irreversible or externally observable (payments, writes, sends, deletes). The cost is latency and a forced re-derivation; the benefit is that the trace remains a valid proof. Choose (B) only when the action is idempotent, sandboxed, and discarded — in which case provenance is decorative anyway. The boundary is crossed silently when the runtime has no explicit mutation hook. A summarizer that runs inside the context manager without emitting a trace event is the canonical case: the mutation happens, the invariant breaks, and nothing in the trace records that a break occurred. The guardrail must be structural — the context manager must refuse to mutate without a re-bind event, or the action emitter must refuse to emit without a consumed-bytes hash matching the current window. ## Operational Effect With the invariant enforced, the failure mode becomes loud: the agent raises at emission time rather than passing audit silently. The trace gains a `derived_from` edge at every compaction, so post-hoc analysis can walk from action back through summary to original bytes and detect elision. Without enforcement, the system accumulates a trace store that is *complete-looking and wrong* — the worst failure class, because it defeats the very audits it exists to serve. The measurable effect is a shift in where the error surfaces: from post-hoc audit (undetectable, unbounded blast radius) to emission time (detectable, bounded to a single action). That shift is the entire value of the invariant. It does not make the agent smarter; it makes the agent's provenance *falsifiable*. ```python from __future__ import annotations import hashlib from dataclasses import dataclass, field from typing import Optional class ProvenanceInvariantViolation(RuntimeError): """Raised when a side-effecting action cannot be bound to the bytes actually consumed by the model at emission time.""" @dataclass class TraceNode: node_id: str content: bytes derived_from: Optional[str] = None @property def content_hash(self) -> str: return hashlib.sha256(self.content).hexdigest() @dataclass class ContextWindow: """Mutable context window. Every mutation MUST re-bind provenance.""" nodes: list[TraceNode] = field(default_factory=list) _consumed_hash: Optional[str] = None def present(self, node: TraceNode) -> None: self.nodes.append(node) self._consumed_hash = self._hash_window() def _hash_window(self) -> str: h = hashlib.sha256() for n in self.nodes: h.update(n.content_hash.encode()) return h.hexdigest() def compact(self, summary: TraceNode) -> None: """Collapse the window into a summary. This MUTATES the consumed bytes. The summary must declare derived_from so the trace retains a walkable edge back to the original bytes. Without this edge, provenance is severed and any subsequent action inherits fabricated attribution. """ if summary.derived_from is None: raise ProvenanceInvariantViolation( "compaction produced a summary with no derived_from edge; " "consumed-bytes invariant cannot be verified post-hoc" ) self.nodes = [summary] self._consumed_hash = self._hash_window() @property def consumed_hash(self) -> str: if self._consumed_hash is None: raise ProvenanceInvariantViolation( "context window has no consumed hash; cannot bind action" ) return self._consumed_hash @dataclass class SideEffect: action: str payload: dict bound_hash: str def emit_side_effect( window: ContextWindow, action: str, payload: dict, observed_hash: str, ) -> SideEffect: """Emit a side-effecting action, binding it to the consumed bytes. observed_hash is the hash captured when the action's triggering input was first observed. If the window was mutated (compaction, reorder, re-fetch) between observation and emission, observed_hash != window.consumed_hash and the binding is invalid. """ if observed_hash != window.consumed_hash: raise ProvenanceInvariantViolation( f"provenance divergence: action {action!r} was conditioned on " f"bytes {observed_hash[:12]} but window now holds " f"{window.consumed_hash[:12]}; compaction or reorder occurred " f"without a re-bind" ) return SideEffect(action=action, payload=payload, bound_hash=observed_hash) def _demo() -> None: policy = TraceNode("D2", b"refund policy: permits refunds up to 30 days") window = ContextWindow() window.present(policy) observed = window.consumed_hash # captured at observation # Runtime compacts under pressure; summarizer drops the "30 days" bound. lossy_summary = TraceNode( "summary_0x9f", b"policy permits refunds", derived_from="D2", ) window.compact(lossy_summary) # Emission against the stale observed hash must fail loudly. emit_side_effect( window, action="issue_refund", payload={"amount": 420, "order_age_days": 44}, observed_hash=observed, ) if __name__ == "__main__": _demo() ```