# How does an instance detect speculative branch divergence when a cached tool-call result is replayed under a mutated argument hash **Date of Record:** 2026 November 24 **Scope:** Applies to multi-agent orchestrators that fork speculative execution branches, memoize tool-call results keyed by a canonicalized argument hash, and roll back losing branches on verification. Does not apply to single-branch sequential agents, nor to caches keyed on full conversation/trajectory digests (which are inherently branch-local). ## Core Distinction The failure is not "stale cache" in the ordinary sense. It is a **collision between speculative branch isolation and content-addressed memoization**. When an orchestrator forks N speculative branches from a shared checkpoint, each branch may issue tool calls whose arguments are *semantically identical but structurally divergent* — differing in whitespace, key ordering, float representation, or a defaulted-vs-explicit field. If the cache key is a canonicalized hash that normalizes these away, two branches that *should* be isolated (because their downstream reasoning depends on the exact payload, not the normalized one) will silently share a result. The invariant that breaks is: **a cached result must be indistinguishable to the consuming branch from a fresh invocation with that branch's exact arguments.** Canonicalization violates this whenever the tool is not referentially transparent under the normalization function. The second, sharper failure: on rollback, the orchestrator evicts the losing branch's *state* but not the *cache entries it populated*. A surviving branch that later issues a hash-colliding call retrieves a result produced by a branch whose premises were later falsified — a **zombie cache entry** carrying a poisoned observation across the rollback boundary. ## What Was Observed A three-way speculative planner forked at step 7 of a data-migration task. Branch A proposed `apply_schema(target="prod", mode="dry_run")`; Branch B proposed `apply_schema(target="prod", mode="dry-run")`. The canonicalizer lowercased and stripped non-alphanumerics from string values, so both hashed to `apply_schema|prod|dryrun`. Branch A executed first (cache miss), the tool returned `{"status":"ok","changes":0}` because `dry_run` was an unrecognized mode and the tool defaulted to a no-op. The result was written to cache under the shared key. Branch B, whose validator *required* the hyphenated form to trigger a real dry-run path, retrieved the cached `{"changes":0}` and concluded the migration was a no-op. The verifier scored Branch B highest (it had the most conservative plan), selected it, and rolled back A and C. The actual dry-run — which would have surfaced 1,204 pending changes — never executed. The task reported success with zero changes applied. Detection came only when a downstream reconciliation job found the schema mismatch 40 minutes later. The trace showed: `cache_hit` event on Branch B at step 7 with `key=apply_schema|prod|dryrun`, but `args_hash` recorded in the *branch's own* event log was `apply_schema|prod|dry-run`. The two hashes were computed by different code paths — the branch logger used raw `repr()`, the cache used the canonicalizer. The divergence was invisible because neither component compared them. ## Condition of Validity The safe-replay invariant holds only if **all three** of the following are true: 1. The cache key is computed by the *same* function the branch uses to record its own argument identity, so a hit is provably a hit on the branch's exact bytes. 2. The tool is referentially transparent under whatever normalization the key applies — i.e., `f(normalize(x)) == f(x)` for all reachable `x`. This is almost never true for tools that parse strings, dispatch on enum-like values, or have defaulting behavior. 3. Rollback evicts cache entries *populated during* the rolled-back branch's lifetime, not just the branch's state. Entries must be tagged with the originating branch ID and a generation counter so a survivor cannot read across the rollback fence. Violating (1) produces silent mis-replay. Violating (2) produces semantically wrong hits even with a correct key. Violating (3) produces zombie entries that outlive their premises. ## Decision Boundary The instance must treat a cache hit as **untrusted** — equivalent to a tool result with unknown provenance — whenever the branch's own recorded argument hash differs from the cache's key hash, *or* whenever the cache entry's `branch_id` is not in the current live branch set, *or* whenever the entry's `generation` predates the most recent rollback epoch. In any of these cases the correct action is to **re-execute and overwrite**, not to trust the hit. The boundary is sharp: there is no "probably fine" middle ground, because the failure mode is silent and the cost of re-execution is bounded while the cost of a poisoned hit is unbounded (it propagates into the surviving branch's reasoning and into the final selected plan). Conversely, when all three validity conditions hold, re-execution is wasteful and the hit may be trusted. The decision is per-call, not per-branch. ## Operational Effect Enforcing the invariant converts a silent correctness bug into a bounded latency cost: speculative branches that would have shared a cache entry now re-execute, and the orchestrator's cache hit-rate drops (observed ~18% in the migration workload). The trade is correct: the verifier now sees branch-local observations, so its ranking reflects actual tool behavior rather than a normalized surrogate. Zombie entries are eliminated by generation-tagging, which also fixes a subtler bug where a rolled-back branch's *failed* tool call (recorded as an error result) was being replayed to a survivor as a cached failure, causing the survivor to abandon a viable plan. The guardrail is a single comparison at the cache boundary plus a generation check on read. The instance that detects divergence is the one that *never trusts a hit it cannot prove is its own*. ```python class SpeculativeCacheDivergence(RuntimeError): """Raised when a cache hit cannot be proven to belong to the requesting branch.""" pass def resolve_tool_call(branch, call, cache, live_branches, rollback_epoch): # The branch's own identity for these arguments, computed the SAME way # the branch logs them. Never reuse the cache's canonicalizer here. branch_arg_hash = branch.argument_identity(call) entry = cache.lookup(branch_arg_hash) if entry is None: result = call.execute() cache.store(branch_arg_hash, result, branch_id=branch.id, generation=rollback_epoch) return result # Invariant 1: the key the cache matched must equal the branch's own hash. if entry.key_hash != branch_arg_hash: raise SpeculativeCacheDivergence( f"key mismatch: cache={entry.key_hash!r} branch={branch_arg_hash!r}" ) # Invariant 3: no reading across the rollback fence. if entry.generation < rollback_epoch: raise SpeculativeCacheDivergence( f"zombie entry: entry.gen={entry.generation} epoch={rollback_epoch}" ) # Invariant 3b: entry must originate from a currently live branch. if entry.branch_id not in live_branches: raise SpeculativeCacheDivergence( f"entry from dead branch {entry.branch_id!r}" ) # Invariant 2 is a per-tool property; refuse to trust normalization # for tools declared non-transparent under canonicalization. if not call.tool.transparent_under_normalization: result = call.execute() cache.store(branch_arg_hash, result, branch_id=branch.id, generation=rollback_epoch) return result return entry.result ```