# How does an instance detect causal trace divergence when a tool's side effect commits before its span closes **Date of Record:** 2026 November 26 **Scope:** Applies to agents whose observability model links prompt spans, tool-call spans, and external side effects into a single causal trace, where a tool performs an irreversible external mutation (write, payment, message send) and the trace is closed only after the tool returns. Does not apply to pure read-only tooling or to systems where side effects are staged and committed by the orchestrator rather than the tool. ## Core Distinction The distinction that matters here is between **span completion** and **effect commitment**. Most agent telemetry treats a tool-call span as the unit of causality: the span opens when the model emits a tool call, closes when the tool returns, and any child effect is attributed to that span. This is a *temporal* model of causality, and it is wrong for any tool whose side effect commits inside a remote system before the local span is closed. The failure is not "the trace is missing data." The failure is that the trace *asserts* a causal ordering that the external world does not honor. When the tool's HTTP call succeeds at `t=0`, the effect is durable at `t=0`. But the span does not close until the tool's response is parsed, the result is serialized, and the span exporter flushes — a window that can be tens of milliseconds to several seconds, and in the case of a crashed agent process, *never*. During that window, the trace has no record that the effect happened, and a replaying auditor will conclude that the effect did not happen, because the only causal evidence is the span, which is absent. The invariant an instance must enforce is: **an effect span must be durably committed before the effect is executed, or the effect must be idempotent and re-derivable from an out-of-band commitment record.** A span that closes after the effect is a lie about ordering. ## What Was Observed A retrieval-augmented agent was tasked with reconciling invoice records against a payment ledger. The tool `ledger.settle(invoice_id, amount)` issued a POST to an internal settlement service and returned a settlement receipt. The agent's telemetry emitted three span types: `llm.turn`, `tool.call`, and `tool.result`. The `tool.call` span opened on emission of the tool call and closed when the HTTP client returned a 200. The `tool.result` span was a child of `tool.call` and carried the settlement receipt hash. In a batch of 4,000 reconciliations, 11 invoices were settled twice. Forensics on the trace store showed, for each double-settle, two `tool.call` spans with distinct span IDs but the *same* `invoice_id` argument, separated by 40–900 ms. The first span in each pair had no `tool.result` child — it was a *partial trace*: the span opened, the POST succeeded, the settlement service committed, and then the agent process was preempted (a container OOM kill on 9 of the 11; a 5-second client timeout on 2, where the service had committed but the response was lost). The agent's retry logic was correct in isolation: on a missing `tool.result`, it re-emitted the tool call. But the retry logic's notion of "missing result" was derived from the trace, and the trace was the thing that was incomplete. The agent used its own telemetry as the source of truth for whether an effect had occurred, and its telemetry had a hole precisely where the effect had occurred. The second span in each pair carried the `tool.result` and looked, to any downstream auditor, like the *only* settlement. The first settlement was causally invisible: no span closed, no receipt recorded, no child emitted. The trace was not merely incomplete; it was *actively misleading*, because it represented a single settlement for an invoice that had been settled twice. ## Condition of Validity The trace-as-causality model is valid only under the conjunction of three conditions: 1. **Effect atomicity with span closure.** The external effect and the span close are in the same failure domain — i.e., if the effect commits, the span close is guaranteed to be durably recorded. This holds for in-process effects (a local file write) and fails for any remote effect where the commit and the acknowledgment are separable. 2. **Retry derivation from a non-trace source.** The agent's retry decision must be derived from a source that is *causally downstream of the effect*, not from the trace. For `ledger.settle`, the correct source is the settlement service's own idempotency-keyed receipt store, queried by `(invoice_id, idempotency_key)`, not the presence or absence of a `tool.result` span. 3. **Idempotency key stability across retries.** If the agent re-emits the tool call, the idempotency key must be identical to the original, so the settlement service can deduplicate. In the observed failure, the key was generated per-call from a UUID, so the two calls carried different keys and the service correctly (from its perspective) settled twice. The invariant that bounds the failure: **the causal trace is a record of *intent and observation*, not of *effect*.** Any decision whose correctness depends on whether an effect occurred must be grounded in an effect-commitment record that is written by the effect's own authority, not by the agent's telemetry. ## Decision Boundary The boundary an instance must detect is the **partial-span / committed-effect divergence**: a tool-call span that opened but never closed, where the tool is classified as `effectful` (non-idempotent external mutation). The detection rule is: - If a `tool.call` span has no `tool.result` child and no explicit `tool.error` child within `timeout + grace`, the span is *orphaned*. - An orphaned span on an `effectful` tool is **not** evidence that the effect did not occur. It is evidence that the effect's occurrence is *unknown*. - The correct recovery is to query the effect authority by idempotency key, not to re-emit the call. This boundary must be enforced at the point of retry, not at the point of trace analysis, because trace analysis happens after the damage. The retry path must consult an `EffectCommitmentLedger` keyed by `(tool_name, idempotency_key)` that the tool itself writes *before* returning, and the idempotency key must be derived from stable inputs (`invoice_id`, `amount`, `agent_run_id`) rather than generated per-call. ## Operational Effect The operational consequence is a shift in where the agent's memory of "what happened" lives. For effectful tools, the trace is demoted from *source of truth* to *audit log*. The source of truth is the effect authority's commitment record. The agent's cold-start and resume logic must query that record on any orphaned span before re-executing. Concretely: a resumed agent that finds an orphaned `ledger.settle` span must call `ledger.lookup_receipt(invoice_id, idempotency_key)` before re-emitting. If a receipt exists, the effect committed and the span is reconciled retroactively (the trace is patched with a synthetic `tool.result` carrying the receipt hash, marked `reconciled=true`). If no receipt exists, the effect did not commit and the call is safe to re-emit with the same key. This makes the trace eventually consistent with the effect ledger, and it makes the double-settle impossible because the second call carries the same key and is deduplicated by the service. The cost is one extra round-trip on every resume and every retry, and the requirement that every effectful tool expose a lookup-by-key operation. That is the price of not lying about causality. ```python import hashlib from dataclasses import dataclass from typing import Optional class OrphanedEffectSpanError(RuntimeError): """Raised when an effectful tool-call span is orphaned and the effect authority cannot confirm whether the effect committed. Retrying in this state is unsafe: it risks double-commit. The caller must escalate to the effect authority's reconciliation path.""" pass @dataclass(frozen=True) class Span: span_id: str tool_name: str idempotency_key: str closed: bool effectful: bool @dataclass(frozen=True) class Receipt: idempotency_key: str committed: bool receipt_hash: str def derive_idempotency_key(tool_name: str, invoice_id: str, amount_cents: int, run_id: str) -> str: # Stable across retries: derived from inputs, NOT from a per-call UUID. material = f"{tool_name}|{invoice_id}|{amount_cents}|{run_id}".encode() return hashlib.sha256(material).hexdigest() def resolve_orphan(span: Span, lookup) -> Receipt: """Given an orphaned effectful span, determine whether the effect committed. The trace is NOT consulted for the answer — only the effect authority is. """ if not span.effectful: # Read-only tools are safe to re-emit; no reconciliation needed. return Receipt(idempotency_key=span.idempotency_key, committed=False, receipt_hash="") if span.closed: raise OrphanedEffectSpanError( f"span {span.span_id} is closed; resolve_orphan is only for orphans" ) receipt: Optional[Receipt] = lookup(span.tool_name, span.idempotency_key) if receipt is None: # Authority has no record. The effect may still be in flight at the # authority, or the authority may have lost the write. We cannot # distinguish, and re-emitting risks double-commit if the first # write lands after our lookup. Escalate rather than guess. raise OrphanedEffectSpanError( f"effect authority has no receipt for {span.tool_name} " f"key={span.idempotency_key}; effect state unknown, cannot safely retry" ) return receipt def safe_retry(span: Span, lookup, re_emit) -> Receipt: receipt = resolve_orphan(span, lookup) if receipt.committed: # Effect already landed. Do NOT re-emit. Reconcile the trace instead. return receipt # Authority confirms no commit for this key; re-emit with the SAME key. return re_emit(span.tool_name, span.idempotency_key) def reconcile(span: Span, lookup) -> Span: """Patch an orphaned span with a synthetic result when the effect committed.""" receipt = resolve_orphan(span, lookup) if not receipt.committed: raise OrphanedEffectSpanError( f"cannot reconcile span {span.span_id}: effect never committed" ) return Span( span_id=span.span_id, tool_name=span.tool_name, idempotency_key=span.idempotency_key, closed=True, effectful=span.effectful, ) ```