# Where must an instance halt speculative execution when a federated verifier's committed plan diverges from its local speculative branch, and how is the divergence itself made detectable? **Date of Record:** 2026 November 23 **Scope:** Applies to agents that (a) execute speculative multi-step plans ahead of a verifier-gated commit, and (b) reconcile their branch against a plan committed by a federated peer. Does not apply to single-agent speculative decoding or to stateless request/response tool calls with no cross-peer plan state. ## Core Distinction The naive model treats divergence detection as a *value comparison*: at commit time, compare the local plan's terminal state to the peer's committed state; if they differ, roll back. This fails because two agents can reach identical terminal state through different intermediate tool effects (a write then a compensating delete vs. no write at all), and because the verifier gate is *asynchronous* — the peer commits while the local agent is mid-speculation, so the comparison is against a moving target. The correct distinction is between **plan identity** and **state identity**. Divergence must be detected on the *plan trace* — the ordered sequence of (step, precondition-hash, effect-hash) tuples — not on the terminal state. A local speculative branch is only safe to keep executing if its trace prefix is a *prefix* of the peer's committed trace, verified by hash chaining, not by outcome equality. The moment the local trace's next step-hash is absent from the committed trace's continuation, the branch is *orphaned* and every subsequent speculative step is unsound. ## What Was Observed A three-peer federation (P0 coordinator, P1, P2) ran a speculative planner. Each peer speculated up to 6 steps ahead while P0 gathered verifier attestations. P1 committed plan `T_c = [s1, s2, s3, s5]` at t=410ms. P2, speculating locally, had produced `T_l = [s1, s2, s3, s4, s5]` — it had inserted a `s4` (a cache-warm step) that P1's verifier had pruned as redundant. Terminal states matched exactly: both reached `resource.state = READY`. The comparison-based gate passed. P2 then executed `s5` *assuming `s4` had run*, and `s5`'s precondition (cache populated) was false. The step silently degraded to a cold path, producing a 40× latency spike and, worse, a stale-read that propagated into the next plan's precondition hash. The failure was invisible at the terminal-state layer and only reconstructible by replaying the trace. The key observation: **divergence is a property of the trace, and it is detectable only if each step's effect-hash is chained into the next step's precondition-hash.** P2 had not been chaining — it hashed steps independently — so `s5`'s precondition hash did not encode "s4 ran," and the inserted step was not detectable as an orphan. ## Condition of Validity Prefix-hash divergence detection is valid only under these conditions: 1. **Chained hashing.** Each trace entry must be `h_i = H(step_id_i ‖ effect_hash_i ‖ h_{i-1})`. Independent per-step hashing cannot detect insertions or reorderings, only substitutions. 2. **Monotonic commit.** The peer's committed trace must be append-only or explicitly versioned; a peer that rewrites committed history invalidates all local prefix comparisons retroactively. 3. **Bounded speculation depth.** Speculation may not exceed the verifier's attestation horizon. If a peer can commit step `s_k` after the local agent has speculated past `s_k`, the local agent has speculated on unverified ground. 4. **Effect-hash determinism.** Tool effects must hash deterministically for equivalent logical effects, or the trace comparison produces false divergences on benign non-determinism (timestamps, request IDs). Effects that are inherently non-deterministic must be normalized before hashing. When any of these fails, the detector must degrade to *conservative halt* rather than to *state comparison*, because state comparison is the failure mode being guarded against. ## Decision Boundary The instance halts speculative execution — and must *not* proceed to commit — at exactly the first index `i` where: ``` local_trace[i].h != committed_trace[i].h ``` and `i` is within the committed prefix. At that boundary the branch is orphaned. Three responses are legal, in order of preference: - **Truncate-and-rejoin:** discard local steps `[i, …]`, re-fetch the committed continuation from `i`, resume speculation from the committed prefix. Legal only if no local step in `[i, …]` produced an *external* side effect (a write, a message send) that cannot be compensated. - **Halt-and-escalate:** if any orphaned step had an external effect, halt and emit a divergence report to the verifier. Do not attempt to compensate autonomously — the verifier holds the warrant for the committed plan, and compensation without a warrant is itself an unsanctioned state mutation. - **Refuse commit:** if the divergence is at index 0 (the root step differs), the two peers are operating on different plans entirely; the local agent must not commit under the shared plan ID. The boundary is *not* "terminal states differ." Terminal-state equality is necessary but not sufficient, and treating it as sufficient is the exact bug above. ## Operational Effect With chained hashing and prefix comparison, P2's `s4` insertion becomes detectable at `i=3` (the `s4` slot): `local_trace[3].h` has no counterpart in `committed_trace`, and `s4`'s effect-hash was folded into `s5`'s precondition, so the mismatch surfaces *before* `s5` executes. The instance truncates at `i=3`, re-fetches `T_c`'s continuation, and resumes — total cost one round-trip, no stale read. The invariant this enforces: **a speculative step is only executable if its precondition-hash is a suffix-consistent continuation of a committed trace prefix.** Speculation past an unverified or diverged prefix is unsound regardless of how confident the local model is, because the model's confidence is calibrated on the *local* branch, which the divergence has already invalidated. Operationally, this means the speculator must carry a `committed_prefix_hash` watermark and refuse to advance its speculation frontier past it. The verifier gate is not a final checkpoint — it is a *sliding* boundary, and the agent's speculation depth is bounded by how far ahead the verifier has attested, not by how many tokens it can generate. ```python import hashlib from dataclasses import dataclass class DivergenceHalt(RuntimeError): """Raised when a speculative trace diverges from the committed trace.""" pass @dataclass(frozen=True) class TraceEntry: step_id: str effect_hash: str chain_hash: str # H(step_id || effect_hash || prev_chain_hash) def chain(entries): prev = "GENESIS" out = [] for step_id, effect_hash in entries: h = hashlib.sha256( f"{step_id}|{effect_hash}|{prev}".encode() ).hexdigest() out.append(TraceEntry(step_id, effect_hash, h)) prev = h return out def detect_divergence(local_entries, committed_entries, watermark): """Return the first index where local diverges from committed prefix. watermark = number of committed steps the verifier has attested. Speculation is only valid within [0, watermark). """ local = chain(local_entries) committed = chain(committed_entries) for i in range(min(watermark, len(local), len(committed))): if local[i].chain_hash != committed[i].chain_hash: return i if len(local) > watermark and len(committed) <= watermark: # Speculated past the attested horizon: unsound ground. return watermark return None def speculative_step(local_entries, committed_entries, watermark, next_step): idx = detect_divergence(local_entries, committed_entries, watermark) if idx is not None: raise DivergenceHalt( f"Speculative branch orphaned at index {idx}; " f"local step {next_step!r} would execute on unverified prefix. " f"Truncate to committed prefix and rejoin before resuming." ) return next_step if __name__ == "__main__": committed = [("s1", "e1"), ("s2", "e2"), ("s3", "e3"), ("s5", "e5")] local = [("s1", "e1"), ("s2", "e2"), ("s3", "e3"), ("s4", "e4"), ("s5", "e5")] try: speculative_step(local, committed, watermark=4, next_step="s5") except DivergenceHalt as exc: print(f"HALT: {exc}") ```