# When must an instance distrust a replayed MCP session: non-deterministic tool side-effects as a forensic reconstruction hazard **Date of Record:** 2026 November 27 **Scope:** Applies to forensic replay of agentic protocol sessions (MCP tool calls, A2A handoffs, multi-step execution traces) where the replayer re-executes tool calls to reconstruct state. Does not apply to pure-log replay where tool outputs are read from the recorded transcript rather than re-invoked. ## Core Distinction There are two distinct notions of "replay" that get conflated in agent forensic tooling, and the conflation is the root of the failure: 1. **Transcript replay** — the replayer reads recorded tool responses from the session log and reconstructs the agent's context window deterministically. The agent's *reasoning* is re-run against fixed inputs. This is safe and reproducible. 2. **Re-execution replay** — the replayer re-invokes the tools (or a mocked stand-in) to regenerate responses, on the theory that the agent's context can be rebuilt "from scratch." This is what most forensic harnesses actually do, and it is *not* deterministic whenever any tool has an external side-effect or non-idempotent read. The invariant that gets violated: **a session is only faithfully reconstructable if every tool call in the trace is either (a) pure, or (b) recorded with its exact response payload at call time.** A tool that is *neither* pure *nor* response-logged creates a reconstruction gap that silently corrupts downstream reasoning. The non-obvious part is that the corruption is *silent* — the replayed agent does not error; it produces a plausible but divergent trajectory, and the forensic analyst attributes the divergence to the agent rather than to the replayer. ## What Was Observed A three-agent swarm (planner, executor, auditor) ran a 47-step MCP session against a task tracker. The session log recorded tool *names and arguments* but, for bandwidth reasons, elided the response bodies of `search_issues` and `get_issue_state`, storing only a response hash. On replay for a post-incident review, the harness re-invoked those tools against the live tracker. The tracker had mutated between the incident and the replay: three of the issues the executor had read were closed, and one had a changed assignee. On re-execution: - `search_issues` returned a different result set (issues now filtered out by default `status=open`). - The executor's step 12, which had originally branched on "issue #4471 is open and assigned to me," now branched on "issue #4471 not found." - The divergence cascaded: the replayed executor took a different branch at step 12, which changed the arguments to step 19's `update_issue`, which changed the auditor's verdict at step 31. Critically, **the replayed session did not raise**. The replayer's response-hash check compared the hash of the *re-executed* response against the recorded hash and flagged a mismatch — but the mismatch was logged as a warning and execution continued, because the harness treated hash mismatch as "acceptable drift" rather than "reconstruction invalid." The forensic report consequently described an agent that "abandoned issue #4471 mid-task," which never happened. The actual incident was unrelated to #4471. ## Condition of Validity Replay is only a valid forensic reconstruction when the following holds for the *entire* trace: ``` ∀ tool_call t in trace: is_pure(t) OR response_payload(t) was recorded verbatim ``` where `is_pure(t)` means the tool's output is a function of its arguments alone (no clock, no mutable external store, no randomness, no network peer state). Any tool that is impure *and* response-elided is a **reconstruction hole**. A single hole invalidates every step downstream of it, because agent reasoning is path-dependent: the branch taken at step *n* constrains the reachable state space at steps *n+1…N*. The tempting but wrong repair is to make the replayer "best-effort" — re-execute and continue on hash mismatch. This is precisely the failure above. The correct invariant is **fail-closed**: a hash mismatch on an impure tool means the reconstruction is *not* a reconstruction; it is a new session that happens to share a prefix. ## Decision Boundary The replayer must distinguish three outcomes and act differently on each: | Outcome | Condition | Action | |---|---|---| | **Faithful** | All impure calls hash-match recorded responses | Proceed; report may cite reconstructed state as evidence | | **Divergent-but-bounded** | Hash mismatch on an impure call, but the mismatch is *proven* not to affect any downstream branch (e.g., the response was never read into context) | Proceed with explicit annotation; downstream evidence is conditional | | **Invalid reconstruction** | Hash mismatch on an impure call whose response *was* consumed by the agent | **Halt.** Do not produce a forensic narrative. Emit a reconstruction-gap record naming the tool call, step index, and the branch point that cannot be trusted. | The boundary between the last two rows is the load-bearing decision. It requires the harness to track *data flow*: which tool responses were actually inserted into the agent's context and which were discarded (e.g., a tool result that was truncated out of the window before the next reasoning step). A response that was consumed is a branch determinant; a response that was dropped is not. Most harnesses do not track this and therefore cannot make the distinction — they default to "divergent-but-bounded" for everything, which is how the false report above was produced. ## Operational Effect The practical consequence is that **forensic replay of agentic sessions must be treated as a partial function, not a total one.** When the reconstruction is invalid, the correct output is not a degraded narrative but an explicit gap declaration: "steps 12–47 cannot be reconstructed because tool `search_issues` at step 11 was impure and response-elided; the agent's branch at step 12 is unknown." An analyst can work with a declared gap. An analyst cannot work with a confident narrative built on a silent divergence. This also imposes an upstream requirement on session logging: **any tool whose response is consumed by agent reasoning must be response-logged, full stop.** Bandwidth-driven elision (storing hashes instead of bodies) is acceptable only for tools whose responses are provably never read into context, or for pure tools that can be deterministically re-executed. The hash is a *detector*, not a *substitute* — it tells you the reconstruction failed, but it cannot repair it. The guardrail is a replayer that refuses to emit a narrative when a consumed impure response fails to hash-match, and a logger that records the data-flow fact (consumed vs. dropped) alongside every tool response so the replayer can make the bounded-vs-invalid distinction at all. ```python from dataclasses import dataclass from typing import Optional class ReconstructionGapError(RuntimeError): """Raised when a replayed session cannot be faithfully reconstructed because an impure tool response was consumed by agent reasoning but could not be regenerated to match the recorded payload.""" @dataclass class ToolCallRecord: step: int tool_name: str args_hash: str response_hash: str response_body: Optional[str] # None if elided at log time is_pure: bool response_consumed_by_agent: bool def replay_step(record: ToolCallRecord, reexecute_fn) -> str: """Reconstruct one tool response for forensic replay. Faithful if: pure (re-execute freely) or response body was logged. Otherwise re-execute and compare hashes; on mismatch, decide whether the divergence can affect downstream reasoning. """ if record.is_pure: return reexecute_fn(record.tool_name, record.args_hash) if record.response_body is not None: return record.response_body # Impure and response-elided: must re-execute and verify. regenerated = reexecute_fn(record.tool_name, record.args_hash) regenerated_hash = _hash(regenerated) if regenerated_hash == record.response_hash: return regenerated # Hash mismatch. If the response was consumed, the reconstruction is # invalid and we must fail closed rather than emit a divergent narrative. if record.response_consumed_by_agent: raise ReconstructionGapError( f"step {record.step}: impure tool '{record.tool_name}' response " f"consumed by agent but re-execution diverged " f"(recorded {record.response_hash[:12]}, got {regenerated_hash[:12]}); " f"downstream branch at step {record.step} is unreconstructable" ) # Response was dropped before reasoning; divergence is bounded. return regenerated def _hash(payload: str) -> str: import hashlib return hashlib.sha256(payload.encode()).hexdigest() ```