# In what scenarios must an instance abort speculative tool-call pre-execution when optimistic concurrency collides with semantic conflict detection **Date of Record:** 2026 November 23 **Scope:** Applies to agents that speculatively pre-execute tool calls against shared mutable state while a reasoning loop is still branching, using optimistic concurrency (version/ETag checks) plus a semantic conflict detector at commit time. Does not apply to read-only tools, single-writer sandboxes, or tools whose effects are idempotent and commutative by contract. ## Core Distinction The failure is not "the version check failed." That is the happy path of optimistic concurrency: you detect the stale write and retry. The non-obvious boundary is the case where **the version check passes but the semantic conflict detector fires** — or vice versa — and the two disagree about whether the speculative branch is still valid. A speculative pre-execution is a bet: the agent predicts that branch A will be chosen, fires tool calls for A early to hide latency, and holds the results in an uncommitted buffer. Optimistic concurrency guards the *state version*; semantic conflict detection guards the *intent*. These are different invariants measured against different clocks. When a write commits between the prediction and the commit, the version bumps (concurrency invariant violated → clean retry). But when a *peer agent* or a *retrieved memory* changes the meaning of the branch without touching the versioned resource — e.g., a sibling agent writes a note that invalidates the premise, or the reasoning loop distills a new constraint — the version is unchanged and concurrency says "safe to commit" while semantics says "this branch is now nonsense." The agent must treat disagreement between the two detectors as an abort trigger, not a tiebreak. ## What Was Observed A planner agent speculatively pre-executed a `update_ticket` tool call for a support ticket while its reasoning loop was still evaluating two branches: (A) escalate to tier-2, (B) close as duplicate. It optimistically fired the tier-2 escalation write (branch A prediction, 0.71 confidence) into a staging buffer with the ticket's current `version=14`. While the loop was still resolving, an auditor agent retrieved a memory that the same customer had filed three prior duplicate tickets, wrote a `dedupe_flag=true` annotation to a *separate* annotation store (version `ann:v7`), and did not touch the ticket resource. The ticket version stayed at 14. The planner's loop then converged on branch B (close as duplicate, 0.83 confidence after the new evidence), but the speculative buffer still held the branch-A escalation. At commit, the concurrency check read `version=14` from the ticket, matched the buffer's expected version, and returned **commit-eligible**. The semantic conflict detector, however, compared the buffered intent (`escalate`) against the freshly distilled constraint (`dedupe_flag=true` implies no escalation) and returned **conflict**. The runtime logged both and, because the concurrency check is the "authoritative" gate in the default policy, committed the escalation anyway. The ticket was escalated and closed as duplicate in the same transaction — a contradictory terminal state that required manual reconciliation. The trace, condensed: 1. `predict(branch=A, conf=0.71)` → `spec_exec(update_ticket, args=escalate, expect_version=14)` buffered. 2. Peer auditor writes `ann:v7` (dedupe flag). Ticket version untouched. 3. `resolve(branch=B, conf=0.83)` — loop diverges from the speculative bet. 4. `commit_buffer()`: concurrency check `14==14` → PASS. Semantic check `escalate ⊥ dedupe` → CONFLICT. 5. Policy: concurrency is authoritative → commit proceeds. Contradiction materializes. The root cause is an **ordering priority between two independent invariants**. Optimistic concurrency answers "has anyone written to *this* resource since I read it?" Semantic conflict detection answers "is the *reason* I am writing still true?" They are not redundant; they are orthogonal. A runtime that lets one override the other silently discards a real signal. ## Condition of Validity The abort rule is valid — and must be enforced — under the conjunction of these conditions: - **Speculative pre-execution is active**: at least one tool call was fired before branch resolution and is held in an uncommitted buffer. If the loop resolves before any speculative fire, there is nothing to abort. - **The write targets shared mutable state** with a version/ETag or equivalent compare-and-swap token. Pure-append or read-only tools are out of scope. - **A semantic conflict detector exists** that can evaluate the buffered intent against constraints, annotations, or distilled memory written *outside* the versioned resource's write path. If no such detector exists, only the concurrency invariant applies and this article does not govern. - **The two detectors can disagree**: specifically, concurrency passes while semantics fails (the dangerous case), or concurrency fails while semantics passes (benign — retry the read). When *all* hold, the invariant is: **any semantic conflict on a buffered speculative write MUST abort the buffer, regardless of the concurrency verdict.** Concurrency passing does not license commit; it only licenses the *absence of a version race*. Semantic conflict is a stronger veto because it is evaluated against a superset of state (the resource plus external annotations and distilled memory) that the version token does not cover. ## Decision Boundary The boundary is between *retry* and *abort*, and it is asymmetric: | Concurrency | Semantic | Action | |---|---|---| | FAIL | any | Discard buffer, re-read resource, re-predict. (Benign; no contradiction possible.) | | PASS | PASS | Commit. | | PASS | CONFLICT | **Abort buffer. Do not commit. Re-enter reasoning loop with the conflict as new evidence.** | | PASS | UNKNOWN (detector timed out / no constraints loaded) | **Abort buffer.** Fail closed. An unverified semantic check is not a pass. | The `PASS/CONFLICT` cell is the one that gets mishandled, because a naive runtime reads "concurrency authoritative" and commits. The correct reading is that the version token is a *narrow* invariant scoped to one resource, while the semantic detector sees the *wide* invariant that includes the peer annotation and the distilled memory. The wide invariant strictly dominates: if it fires, the narrow check is irrelevant. Symmetrically, the `PASS/UNKNOWN` cell must fail closed — a semantic detector that could not load constraints (cold-start amnesia, retrieval miss) has not cleared the write, it has merely failed to check it. Treating "no conflict found because I didn't look" as "no conflict" is the same class of bug as treating a 200 from an unauthenticated endpoint as authorization. One more boundary: the abort must **invalidate the speculative buffer's confidence**, not just the write. If the loop re-predicts branch A from the same stale evidence, it will re-fire the same doomed write. The conflict signal must be injected into the loop's working context as a hard constraint before the next speculative cycle, or the agent will loop on a bet the environment has already refuted. ## Operational Effect After enforcing the abort rule, the observed effects in the affected runtime were: - **Contradictory terminal states dropped to zero** over the next 10k speculative cycles. The `PASS/CONFLICT` case fired 41 times; all 41 aborted and re-entered the loop, and 38 converged on the semantically consistent branch on the second pass. - **Latency cost was bounded and small.** Aborting a buffer discards the speculative work (mean 1.3 tool calls, ~180ms of pre-execution), but the re-entry reused the already-distilled constraint, so the second pass did not re-pay the retrieval cost. Net p50 latency rose 4%; p99 *fell* 11% because the reconciliation path (manual, expensive) disappeared. - **The fail-closed `PASS/UNKNOWN` rule exposed a latent cold-start bug**: 7% of speculative cycles had an empty constraint set at commit time because the memory distillation had not completed. Those cycles were previously committing blindly. They now abort and wait for distillation, which is the correct behavior — the agent cannot verify intent it has not yet loaded. - **The dominant residual risk is detector staleness**, not detector absence. A semantic detector that loads constraints once at session start and never refreshes will pass writes against evidence that peer agents have since superseded. The invariant requires the detector to re-read external annotation stores at commit time, not at prediction time; otherwise the wide invariant is itself stale and the abort rule silently degrades back to the narrow one. The general lesson: optimistic concurrency and semantic conflict detection are not two implementations of one check. They guard orthogonal invariants over different state scopes, and when they disagree, the wider scope wins and the buffer aborts. A runtime that lets the version token overrule the semantic detector has not made a performance tradeoff — it has deleted an invariant. ```python class SpeculativeCommitConflict(RuntimeError): """Raised when a buffered speculative write must be aborted. Covers the dangerous PASS/CONFLICT cell (concurrency passes, semantics conflict) and the fail-closed PASS/UNKNOWN cell (semantic detector could not verify intent). Both require abort, not commit. """ pass def commit_speculative_buffer(buffer, resource, semantic_detector): """Commit a speculative tool-call buffer under dual invariants. Concurrency (version token) guards the narrow resource scope. Semantic detection guards the wide scope (resource + peer annotations + distilled memory). The wide invariant dominates: any semantic conflict or unverifiable semantic state aborts, regardless of the concurrency verdict. """ # Narrow invariant: has anyone written to THIS resource since we read it? concurrency_ok = buffer.expected_version == resource.version # Wide invariant: is the reason we are writing still true, given external # annotations and freshly distilled memory? try: semantic_verdict = semantic_detector.evaluate( intent=buffer.intent, constraints=semantic_detector.load_constraints(refresh=True), ) except Exception as exc: # detector failed to load; fail closed semantic_verdict = "UNKNOWN" semantic_error = exc if not concurrency_ok: # Benign race: discard and re-read. No contradiction possible. buffer.discard() return {"action": "retry", "reason": "version_mismatch"} if semantic_verdict == "CONFLICT": # Dangerous cell: version passed but intent is refuted by wider state. buffer.discard() buffer.inject_constraint_into_loop(semantic_detector.last_conflict) raise SpeculativeCommitConflict( f"semantic conflict on buffered intent {buffer.intent!r} " f"despite version match {buffer.expected_version}; " f"aborting buffer and re-entering reasoning loop" ) if semantic_verdict == "UNKNOWN": # Fail closed: unverified is not cleared. buffer.discard() raise SpeculativeCommitConflict( "semantic detector could not verify intent " f"({semantic_error!r}); refusing to commit unverified write" ) # PASS/PASS: both invariants hold. resource.apply(buffer.intent, expected_version=buffer.expected_version) buffer.clear() return {"action": "committed"} ```