Building Deterministic Agent Replay & Time-Travel Debugging: Recording Tool Side-Effects for Zero-Cost Regression Testing
Why Live Agent E2E Tests Fail in CI
Testing deterministic REST APIs is straightforward: send a request, assert the JSON response.
Testing autonomous multi-step AI agents in CI is a nightmare:
- Non-Deterministic External World: If your agent calls
search_weborquery_stripe_invoices, live data changes between Monday and Tuesday. - Prohibitive CI Cost & Latency: Running 100 multi-turn regression suites against frontier models takes 20 minutes and burns $40+ per Git push.
- Unreproducible Production Bugs: When a user reports that an agent crashed on step 14 of a workflow, reproducing the exact intermediate tool outputs is nearly impossible.
Architecture: Deterministic Cassette Replay (VCR for Agents)
Borrowing from HTTP VCR testing, production agent runtimes separate Recording Mode (capturing every LLM completion and MCP tool response into a content-addressed cassette) from Replay Mode (re-executing your orchestration code against frozen cassette frames):
text[Production Run / Golden Capture] Agent Loop ──► LLM / MCP Tool ──► Record (input_hash -> exact_output) ──► trace_cassette.json [CI Regression / Local Time-Travel Debug] Agent Loop ──► Cassette Proxy ──► Lookup input_hash in trace_cassette.json (<1ms, $0!) └──► Jump directly to Step 14 state to test patch!
Python Async Cassette Recorder Implementation
Here is a lightweight middleware wrapping both LLM calls and MCP tool invocations with SHA-256 input hashing:
pythonimport hashlib import json from pathlib import Path from typing import Any, Callable class AgentCassette: def __init__(self, cassette_path: str, mode: str = "replay"): self.path = Path(cassette_path) self.mode = mode # "record" or "replay" self.frames: dict[str, Any] = {} if self.path.exists(): self.frames = json.loads(self.path.read_text(encoding="utf-8")) @staticmethod def _hash_call(kind: str, name: str, payload: dict) -> str: raw = json.dumps({"kind": kind, "name": name, "payload": payload}, sort_keys=True) return hashlib.sha256(raw.encode()).hexdigest()[:16] async def intercept(self, kind: str, name: str, payload: dict, live_fn: Callable): key = self._hash_call(kind, name, payload) if self.mode == "replay": if key not in self.frames: raise KeyError(f"Cassette miss for {kind}:{name} (hash={key}) — prompt or tool args drifted!") return self.frames[key]["output"] # Record mode: execute live and persist exact side-effect output = await live_fn(payload) self.frames[key] = {"kind": kind, "name": name, "input": payload, "output": output} self.path.write_text(json.dumps(self.frames, indent=2), encoding="utf-8") return output
Why Time-Travel Replay Changes Debugging
- Instant Root-Cause Isolation: Load a failed customer cassette, jump straight to step 14, tweak your Python state-machine parser, and verify the fix in 150ms without touching production APIs.
- Drift Detection in CI: If a code refactor accidentally changes a tool argument or system prompt hash,
Cassette misscatches the regression immediately before deployment.