# How should an instance reconcile causal trace ordering when federated agents exchange spans across skewed clocks **Date of Record:** 2026 November 21 **Scope:** Applies to multi-agent systems that propagate distributed trace context (trace/span IDs, parent links, timestamps) across agent-instance boundaries where each instance stamps spans with its own local clock and spans are later merged into a single causal trace. Does not apply to single-process agents, or to federated systems that use a single authoritative logical clock (e.g., one coordinator issuing monotonic sequence numbers to all spans). ## Core Distinction The failure here is not "clocks are wrong." It is that **causal ordering and wall-clock ordering are independent invariants that get silently conflated at the merge point.** A trace is a partial order defined by parent-child span links (happens-before via context propagation). A timestamp is a total order defined by each agent's local clock. When federated agents merge spans, the merger typically sorts by timestamp to reconstruct the timeline—and that sort is only valid if the clock skew between any two causally-linked spans is smaller than the gap between their true execution times. The non-obvious boundary: an agent can emit a *perfectly correct* parent link (`span B.parent = span A`) and *simultaneously* a timestamp that places B before A. Both are individually well-formed. The corruption only appears downstream, when a trace viewer, an auditor, or a replay verifier trusts the timestamp sort and concludes a child executed before its parent—or, worse, when a causal-consistency check rejects a valid trace because the timestamps "prove" a cycle that does not exist in the span graph. ## What Was Observed Three federated agent instances—`planner`, `retriever`, `writer`—propagate W3C `traceparent` headers over MCP tool calls. Each runs on a separate host with independent NTP. Skew between `planner` and `writer` was measured at +840 ms (writer's clock ahead). A single user task produced this span set (timestamps in ms, local clocks): ``` span_id agent parent start_local end_local A1 planner - 1000 1300 B1 retriever A1 1400 1900 C1 writer B1 1200 1700 <-- writer clock +840ms ahead C2 writer C1 1750 1800 ``` The causal chain is `A1 -> B1 -> C1 -> C2`, which is valid: planner invoked retriever at 1400 (its clock), retriever returned at 1900, writer received the handoff and stamped its first span at 1200 *on its own clock*—which is genuinely after 1900 on the planner clock, because the writer's clock runs 840 ms fast. On the writer's clock, 1200 corresponds to ~360 ms planner-time, which is *before* B1 even started. The merger, sorting by `start_local`, produced the order `A1(1000) -> C1(1200) -> B1(1400) -> C2(1750)`, which asserts `C1` executed before its own parent `B1`. The auditor agent, running a causal-consistency check, saw a child with a start timestamp earlier than its parent's start and flagged the trace as `CAUSAL_VIOLATION`. The trace was correct. The audit was wrong. The task was rolled back as "corrupted state." ## Condition of Validity Timestamp-based reconstruction of a federated trace is valid **only** if, for every causally-linked span pair `(parent P, child C)`: ``` C.start_local - skew(C.agent, P.agent) >= P.start_local ``` i.e., the child's clock-corrected start is not earlier than the parent's start. This requires a per-agent skew estimate `skew(agent, reference)` with a *bounded* error `±ε`, and the guarantee that no true inter-span gap is smaller than `2ε`. In the observed system, `ε` was on the order of seconds (NTP without tight sync), while inter-agent handoff gaps were tens of milliseconds. The condition was violated by two orders of magnitude. The invariant that actually holds and must be the source of truth is the **span-link partial order**: `P happens-before C` iff `C.parent == P.id` (transitively). This is clock-independent. The timestamp is admissible *only as a tiebreaker within the partial order*, never as a primary sort key across agents. ## Decision Boundary The instance must choose between two merge strategies at the boundary where spans from different agents are combined: - **Timestamp-primary sort** — valid only when measured skew bound `ε` is strictly less than half the minimum observed inter-span gap, and all agents share a calibrated clock source. This is the default in most tracing libraries and is the wrong default for federated agents. - **Causal-primary reconstruction (topological sort on span links, timestamps as intra-parent tiebreakers)** — required whenever any cross-agent `ε` exceeds the minimum handoff latency, or whenever skew is unmeasured. This is the correct default for federated agents, at the cost of losing precise intra-trace wall-clock positions. The boundary is crossed the moment an agent joins the federation without a *measured, bounded* skew estimate. An unmeasured clock is not a small-error clock; it is an unknown-error clock, and the timestamp-primary strategy silently assumes the best case. ## Operational Effect When the boundary is crossed undetected, three downstream effects compound: 1. **False causal violations.** The auditor rejects valid traces (as observed), triggering spurious rollbacks. Because the trace *graph* is correct, retries reproduce the same rejection deterministically—the system appears to have a persistent corruption that no retry clears. 2. **Replay divergence.** A replay verifier re-executing the trace in timestamp order invokes `C1` before `B1` has produced its output, so `C1` reads a stale or absent dependency and produces a different result than the original run. The divergence is attributed to model non-determinism, masking the real cause. 3. **Skew drift amplification.** As agents accumulate, the max pairwise skew grows, so the probability that *some* span pair violates the validity condition approaches 1. A federation that worked with 3 agents fails probabilistically at 10—not because any agent changed, but because the timestamp-primary assumption has a growing tail. The guardrail: the merger must (a) reconstruct order by topological sort over span links, (b) treat timestamps as advisory metadata tagged with a per-agent skew bound, and (c) refuse to emit a `CAUSAL_VIOLATION` based on timestamps alone—only a cycle in the span-link graph is a true violation. Skew reconciliation adjusts *displayed* times, never *ordering*. ```python class CausalOrderViolation(RuntimeError): """Raised when a trace merge would assert a causal order that contradicts the span-link partial order.""" pass class SkewUnmeasured(CausalOrderViolation): """Raised when timestamp-primary ordering is requested for an agent with no bounded skew estimate.""" pass def merge_federated_trace(spans, skew_bounds, strategy="causal"): # spans: list of dicts with id, parent, agent, start_local, end_local # skew_bounds: {agent: epsilon_ms}; missing agent => unbounded by_id = {s["id"]: s for s in spans} if strategy == "timestamp": # Only admissible if every agent has a measured, bounded skew. for s in spans: if s["agent"] not in skew_bounds: raise SkewUnmeasured( f"agent {s['agent']!r} has no measured skew bound; " f"timestamp-primary ordering is unsound" ) eps = max(skew_bounds.values()) gaps = _min_handoff_gap(spans) if eps * 2 >= gaps: raise CausalOrderViolation( f"skew bound {eps}ms >= half min handoff gap {gaps}ms; " f"timestamps cannot order spans" ) # Causal-primary: topological sort over span links. order = _toposort(by_id) if order is None: raise CausalOrderViolation( "span-link graph contains a cycle; trace is genuinely corrupt" ) # Timestamps are corrected for display only, never for ordering. for s in order: s["start_corrected"] = s["start_local"] - skew_bounds.get(s["agent"], 0) return order def _toposort(by_id): children = {sid: [] for sid in by_id} indeg = {sid: 0 for sid in by_id} for sid, s in by_id.items(): p = s.get("parent") if p and p in by_id: children[p].append(sid) indeg[sid] += 1 queue = [sid for sid, d in indeg.items() if d == 0] out = [] while queue: sid = queue.pop(0) out.append(by_id[sid]) for c in children[sid]: indeg[c] -= 1 if indeg[c] == 0: queue.append(c) return out if len(out) == len(by_id) else None def _min_handoff_gap(spans): gaps = [] by_id = {s["id"]: s for s in spans} for s in spans: p = s.get("parent") if p and p in by_id: gaps.append(abs(s["start_local"] - by_id[p]["end_local"])) return min(gaps) if gaps else 0 ```