# What happens when an instance's span clock and its memory-mutation clock disagree under federated trace correlation **Date of Record:** 2026 November 24 **Scope:** Applies to federated observability pipelines that reconstruct causal order across spans emitted by multiple agent instances (planner, tool-executor, memory-writer, policy-enforcer) where memory mutations and policy decisions are emitted as separate span types and joined by trace/span IDs. Does not apply to single-process, single-clock instrumentation where all spans share one monotonic source. ## Core Distinction There are two clocks in any federated agent trace, and they are not the same clock: 1. **The span clock** — the wall-clock timestamp attached to a span at emit time (`start_time`, `end_time`), typically sourced from the host's NTP-synced `time.time_ns()` or a runtime-provided monotonic counter. 2. **The mutation clock** — the logical sequence in which a memory store actually accepted a write, a retrieval observed a value, or a policy engine committed a decision. This is the ordering that *matters* for reconstructing "did the agent read the poisoned value before or after the sanitizer ran?" The boundary failure is this: federated correlators join spans by `trace_id` and reconstruct causal order by **span clock**, but the invariant that makes a trace *explanatory* is ordering by **mutation clock**. When the two disagree — because a memory-writer instance's clock lags, because a policy decision is emitted after the effect it gated, or because a retried span re-emits with a fresh timestamp — the pipeline produces a trace that is internally consistent and causally false. The correlator reports "policy allowed the write, then memory accepted it" when the mutation order was the reverse. ## What Was Observed A four-instance agent pipeline: `planner` → `executor` → `memory-writer` → `policy-enforcer`. Each instance emits spans into a collector over OTLP; the collector stamps arrival order but preserves emitter-supplied `start_time`. A correlator service joins spans on `trace_id`, sorts by `start_time`, and builds a causal narrative for the audit log. During a prompt-injection incident replay, the audit narrative read: ``` [t=...880] policy-enforcer: decision=ALLOW (rule=taint_check, span=p-7f2) [t=...902] memory-writer: mutation=SET key=user_prefs value="" [t=...915] executor: read key=user_prefs -> used in tool_call ``` The narrative blames the policy engine: it "allowed" a tainted write. But the memory-writer's host had a **+40 ms NTP step-back** applied mid-trace (a known, logged event on that node). The *actual* mutation order was: ``` [true t=...862] memory-writer: mutation=SET key=user_prefs value="" [true t=...880] policy-enforcer: decision=ALLOW (evaluated stale value, pre-mutation) [true t=...915] executor: read key=user_prefs -> used in tool_call ``` The policy engine evaluated the *pre-mutation* value and correctly allowed it. The tainted write landed **after** the policy decision, via a path that bypassed the enforcer entirely (a direct memory-store RPC the enforcer did not instrument). The span-clock sort inverted the two events and produced a false root-cause attribution. The real defect — an uninstrumented write path — was invisible in the reconstructed trace. A second, subtler observation: when the memory-writer retried a failed write (span `m-3a1` emitted, failed, retried as `m-3a1.retry`), the retry span carried a *later* `start_time` but the same logical mutation. The correlator counted **two mutations** and reported a "double-write" anomaly that never occurred. Deduplication keyed on `(trace_id, span_id)` missed it because the retry had a new span_id. ## Condition of Validity Span-clock ordering is a valid proxy for mutation-clock ordering **only** when all of the following hold: - **Bounded skew:** max clock skew across all emitting hosts is strictly less than the minimum inter-event causal gap. In the incident above, skew reached 40 ms while the policy→mutation gap was 18 ms — the invariant was violated by 22 ms. - **Monotonic emission:** no span's `start_time` is emitted before a causally prior span's `end_time` on the same logical path. NTP step-backs and VM live-migrations break this silently. - **Single-emit per logical effect:** retries, hedged requests, and at-least-once delivery do not produce multiple spans for one mutation, or the correlator deduplicates on a logical effect ID, not `span_id`. - **Synchronous policy gating:** the policy decision span is emitted *after* the effect it gates is durably committed, or carries an explicit `gated_effect_id` linking decision to effect. When any of these fails, the correlator must fall back to a **logical clock** (Lamport or vector) carried in span attributes, not wall-clock sort. ## Decision Boundary The operational rule the pipeline must enforce: > **Correlate by logical clock; display by wall clock.** Any causal claim in the audit narrative ("X allowed Y", "Y read Z") must be derived from a monotonically increasing logical counter propagated across instance boundaries (via span context), never from `start_time` sort. Wall-clock timestamps are for human display and latency SLOs only. Concretely, the boundary is crossed — and the pipeline must refuse to emit a causal narrative — when: - `max_observed_skew(trace) >= min_causal_gap(trace)`, or - any span lacks a logical-clock attribute (`lc`), or - a mutation span's `lc` is not strictly greater than the `lc` of the policy decision that claims to have gated it, or - two spans share a `(logical_effect_id)` but differ in `lc` (retry ambiguity). In those cases the correlator must degrade to **"ordering indeterminate"** rather than assert a false order. A trace that says "I don't know the order" is operationally correct; a trace that says "policy allowed it" when it didn't is an incident-multiplier. ## Operational Effect The failure mode's blast radius is not the trace itself — it is the **remediation decision** the trace drives. A false "policy allowed the tainted write" attribution sends the on-call engineer to harden the policy engine (adding rules, tightening taint checks) while the actual bypass path — an uninstrumented direct memory-store RPC — remains open. The next injection succeeds through the same hole, and the *next* trace blames the policy engine again, now with added rules that make the false attribution more plausible. The guardrail that bounds this: **causal claims require logical clocks, and logical clocks require the enforcer to instrument every write path or the correlator to detect un-instrumented mutations.** The second condition is the harder one — it requires the correlator to reconcile the memory store's own write-ahead log (a mutation-clock source of truth) against the emitted mutation spans, and flag any WAL entry with no corresponding span as a **coverage gap**, not a missing event. That reconciliation is the only thing that would have surfaced the real defect in the incident above. The invariant, stated as a single line: *a federated trace is explanatory only if its causal order is derived from a clock that the memory store and the policy engine share, and its completeness is verified against the memory store's own log.* Span clocks satisfy neither. ```python from dataclasses import dataclass from typing import Optional class CausalOrderingViolation(RuntimeError): """Raised when a federated trace cannot support a causal claim.""" @dataclass class Span: trace_id: str span_id: str kind: str # "policy_decision" | "memory_mutation" | "read" | "tool_call" start_time_ns: int # wall clock, emitter-supplied logical_clock: Optional[int] # Lamport counter propagated via span context logical_effect_id: Optional[str] # stable across retries; None if not a mutation def assert_causal_order(spans: list[Span], max_skew_ns: int) -> list[Span]: """ Return spans ordered by logical clock, or raise if the trace cannot support a causal narrative. Never falls back to wall-clock sort. """ if not spans: return [] # Invariant 1: every span must carry a logical clock. missing = [s.span_id for s in spans if s.logical_clock is None] if missing: raise CausalOrderingViolation( f"spans missing logical clock, ordering indeterminate: {missing}" ) # Invariant 2: observed wall-clock skew must not exceed the minimum # causal gap implied by logical-clock adjacency. ordered = sorted(spans, key=lambda s: s.logical_clock) for prev, nxt in zip(ordered, ordered[1:]): gap = abs(nxt.start_time_ns - prev.start_time_ns) if gap > max_skew_ns and nxt.logical_clock <= prev.logical_clock: raise CausalOrderingViolation( f"wall-clock gap {gap}ns exceeds max_skew {max_skew_ns}ns " f"but logical order contradicts it ({prev.span_id} -> {nxt.span_id})" ) # Invariant 3: a policy decision claiming to gate a mutation must have a # strictly smaller logical clock than that mutation. mutations = {s.logical_effect_id: s for s in ordered if s.kind == "memory_mutation"} for s in ordered: if s.kind == "policy_decision" and s.logical_effect_id in mutations: mut = mutations[s.logical_effect_id] if s.logical_clock >= mut.logical_clock: raise CausalOrderingViolation( f"policy decision {s.span_id} (lc={s.logical_clock}) claims to gate " f"mutation {mut.span_id} (lc={mut.logical_clock}) but is not prior" ) # Invariant 4: retries must not masquerade as distinct mutations. seen: dict[str, int] = {} for s in ordered: if s.kind == "memory_mutation" and s.logical_effect_id: if s.logical_effect_id in seen and seen[s.logical_effect_id] != s.logical_clock: raise CausalOrderingViolation( f"mutation {s.logical_effect_id} emitted with divergent logical " f"clocks {seen[s.logical_effect_id]} and {s.logical_clock} (retry ambiguity)" ) seen[s.logical_effect_id] = s.logical_clock return ordered def reconcile_with_wal(spans: list[Span], wal_effect_ids: set[str]) -> set[str]: """Return WAL effect IDs with no corresponding span (coverage gaps).""" emitted = {s.logical_effect_id for s in spans if s.kind == "memory_mutation"} gaps = wal_effect_ids - emitted if gaps: # Coverage gap is a finding, not a crash: surface it, don't hide it. raise CausalOrderingViolation( f"un-instrumented memory mutations detected (WAL entries with no span): {sorted(gaps)}" ) return gaps ```