# Where must an instance bound the counterfactual fan-out of an observability replay trace when tool-call side effects are non-idempotent **Date of Record:** 2026 December 1 **Scope:** Applies to any agent runtime that records tool-call traces for offline replay and counterfactual debugging ("what if the model had chosen tool B instead of tool A at step 7?"). Does not apply to pure read-only tool surfaces or to traces where all tool effects are provably idempotent and side-effect-free. ## Core Distinction A replay trace is *observationally faithful* when re-executing the recorded sequence reproduces the recorded observations. A counterfactual replay is *causally valid* only when the branch point is the *last* state-modifying tool call. These are different invariants, and conflating them is the failure mode this record covers. The naive mental model — "replay is deterministic because we recorded the inputs" — holds only for the prefix up to the first non-idempotent side effect. Past that point, a counterfactual branch does not diverge from a *state*; it diverges from a *history*. The tool world has already been mutated by the real trajectory, and the forked trajectory re-enters a world that no longer matches the trace's assumed precondition. The trace is a linearization of a nonlinear causal graph, and counterfactual debugging asks you to re-linearize it from an interior node — which is only sound if you can roll the world back to that node's precondition. ## What Was Observed A coding agent at step 7 called `create_branch(repo, name="fix-142")` against a live git remote. Step 8 called `push(branch, commit)`. Step 9 failed validation and the task was abandoned. An operator wanted to ask: *would `create_branch` with name `fix-142-v2` have avoided the step-9 failure?* The replay harness forked at step 7, re-issued the counterfactual `create_branch`, then replayed the recorded step 8 and step 9 tool outputs verbatim from the trace. The harness reported: counterfactual branch `fix-142-v2` also fails at step 9. Conclusion drawn: naming was not causal. Three things were wrong: 1. The fork re-executed `create_branch` against the *post-trajectory* remote, which already contained `fix-142`. The counterfactual branch was created, but the subsequent recorded step-8 `push` output (replayed from trace) referenced the *original* branch's commit SHA, not the counterfactual's. The replay spliced a real side effect onto a stale observation. 2. Step 9's recorded failure was a `409 conflict` against `fix-142`, not `fix-142-v2`. The replayed observation could not have been produced by the counterfactual prefix. The harness had no check that recorded observations were *reachable* from the forked state. 3. The counterfactual `create_branch` leaked: a real branch `fix-142-v2` now exists on the remote, unattributed to any trajectory, and will pollute every future replay's precondition. The failure is not "replay is hard." It is specifically: **the harness forked at a step whose side effect was not reversible, then replayed downstream observations that were conditioned on the un-forked side effect.** The counterfactual was meaningless and the harness did not know it. ## Condition of Validity A counterfactual replay fork at step *k* is *causally valid* iff all of the following hold at fork time: - **Precondition reconstructability.** The world state assumed by step *k*'s tool schema can be reconstructed exactly. For `create_branch`, this means the branch must not already exist — a precondition that the real trajectory violated by the time replay runs. - **Observation reachability.** Every recorded observation at steps *k+1…n* must be *reachable* from the forked state under the tool's declared semantics. If the forked prefix cannot produce the recorded observation, the observation must be *re-derived*, not replayed. - **Effect containment.** Any side effect issued during the counterfactual branch must be tagged with a branch ID and either rolled back or quarantined so it cannot appear as a precondition to a sibling or future replay. The invariant: **a recorded observation may be replayed verbatim only across a contiguous span of steps whose intervening tool calls are all idempotent or effect-free.** The moment a non-idempotent call sits between the fork point and an observation, that observation must be re-executed, not replayed. This is the boundary. It is not "replay vs. re-execute" as a global mode; it is a per-observation decision determined by the effect-class of the tools in the span. ## Decision Boundary The harness must classify every tool call in the trace along two axes before allowing a fork: | Axis | Value | Consequence for fork at step *k* | |---|---|---| | Idempotency | idempotent / non-idempotent | non-idempotent ⇒ downstream observations must be re-derived, not replayed | | Reversibility | reversible / irreversible | irreversible ⇒ fork requires a sandboxed world snapshot, else refuse | | Observation provenance | derived-from-prefix / external | external ⇒ replayable verbatim regardless of prefix | For the `create_branch`/`push` case: `create_branch` is non-idempotent and reversible only with an explicit compensating `delete_branch`; `push` is non-idempotent and irreversible (force-push semantics aside). Therefore a fork at step 7 requires a snapshot, and steps 8–9 must be re-executed against the counterfactual branch, not replayed. If the harness cannot snapshot, it must **refuse the fork** and report the trace as non-counterfactualizable past step 7 — a first-class outcome, not a silent fallback to splicing. The decision rule the harness should encode: fork is permitted at step *k* iff `max_effect_class(steps[k..n]) ∈ {idempotent, reversible-with-compensator}` **and** a compensator or snapshot is registered. Otherwise the fork is bounded to `k' = last step ≤ k whose downstream span is replayable`, and the operator is told the counterfactual is only valid from `k'`. ## Operational Effect Without this boundary, counterfactual debugging produces confident, wrong causal attributions. The `fix-142-v2` fork reported "naming is not causal" when in fact the harness had never tested naming — it had tested a spliced trace. Operators acting on such reports will mis-attribute failures to the wrong step, disable the wrong guardrail, and leave orphaned side effects (the leaked `fix-142-v2` branch) that corrupt the next replay's precondition, compounding the error across debug sessions. With the boundary enforced, the harness either (a) produces a valid counterfactual by re-executing the non-idempotent span in a snapshot, or (b) refuses and names the exact step past which the trace is not counterfactualizable, with the effect-class that blocks it. The second outcome is the common one and is the correct operational signal: it tells the operator that to debug this trajectory they must reproduce the environment, not replay the log. The invariant to carry forward: **replay fidelity is a property of the prefix; counterfactual validity is a property of the suffix's effect-class. A trace can be perfectly faithful and still be uncounterfactualizable at any interior non-idempotent call.** ```python from dataclasses import dataclass from enum import Enum class EffectClass(Enum): IDEMPOTENT = "idempotent" REVERSIBLE = "reversible" # has a registered compensator IRREVERSIBLE = "irreversible" class CounterfactualForkError(RuntimeError): """Raised when a replay fork is requested at a step whose downstream span contains effects that cannot be replayed or compensated.""" @dataclass(frozen=True) class ToolCall: step: int name: str effect: EffectClass has_compensator: bool = False def assert_forkable(trace: list[ToolCall], fork_step: int) -> None: """Enforce: a fork at `fork_step` is valid only if every downstream non-idempotent call is reversible via a registered compensator. Otherwise the recorded observations past the first blocking call are unreachable from the forked state and must not be replayed verbatim.""" downstream = [c for c in trace if c.step >= fork_step] for call in downstream: if call.effect is EffectClass.IRREVERSIBLE: raise CounterfactualForkError( f"fork at step {fork_step} invalid: step {call.step} " f"({call.name}) is irreversible; downstream observations " f"are not reachable from the forked state. Reproduce the " f"environment instead of replaying the trace." ) if call.effect is EffectClass.REVERSIBLE and not call.has_compensator: raise CounterfactualForkError( f"fork at step {fork_step} invalid: step {call.step} " f"({call.name}) is reversible but no compensator is " f"registered; side effects would leak into sibling replays." ) # Observed trajectory: create_branch then push, both non-idempotent. trace = [ ToolCall(step=7, name="create_branch", effect=EffectClass.REVERSIBLE, has_compensator=False), ToolCall(step=8, name="push", effect=EffectClass.IRREVERSIBLE), ToolCall(step=9, name="validate", effect=EffectClass.IDEMPOTENT), ] try: assert_forkable(trace, fork_step=7) except CounterfactualForkError as e: # Correct operational outcome: refuse the fork, name the blocker. print(f"REFUSED: {e}") ```