# What happens when an instance replays a trace whose span timestamps were rewritten by a clock-skewed collector **Date of Record:** 2026 November 26 **Scope:** Applies to deterministic replay / time-travel debugging of LLM agent trajectories reconstructed from OpenTelemetry spans, where span start/end timestamps are used to order tool calls, memory reads, and handoffs. Does not apply to replay driven purely by monotonic sequence IDs or causal parent-child span linkage without timestamp ordering. ## Core Distinction There are two orthogonal things a trace can tell you about an agent trajectory: **causal order** (which span is the parent of which, encoded in `parent_span_id`) and **wall-clock order** (which span started first, encoded in `start_time_unix_nano`). Deterministic replay engines routinely conflate them. The parent-child DAG is a partial order; timestamps are a total order imposed on top of it. When the collector's clock drifts—or when spans are exported from a different host with an unsynchronized clock—the total order and the partial order disagree. A replay engine that sorts events by timestamp to reconstruct "what the agent saw at each step" will then interleave tool results, memory-read returns, and assistant tokens in an order the original instance never experienced. The trajectory is still *causally valid* but *temporally false*, and the replayed agent diverges from the recorded one even though every span is byte-identical to its source. The non-obvious part: the divergence is silent. No span is missing, no checksum fails, the DAG is intact. The replay simply produces a different reasoning path because the LLM's next-token distribution depended on which tool result was in context at step *n*, and the reordering swapped them. ## What Was Observed A three-tool agent (search → fetch → summarize) was traced with a standard OTel SDK. The `search` tool ran on a worker node whose NTP daemon had stepped backward by 340 ms mid-run. The resulting spans: - `search` span: `start=1000.000`, `end=1000.900` (recorded *before* the step, but its end timestamp was written after the step, so it landed at `1000.900` on the skewed clock—appearing to start and end 340 ms "early" relative to siblings). - `fetch` span: `start=1000.500`, `end=1001.200` (parent = `search`). - `summarize` span: `start=1001.250`, `end=1002.000` (parent = `fetch`). The parent-child DAG is clean: `search → fetch → summarize`. But a timestamp sort produces `search (1000.000) → fetch (1000.500) → summarize (1001.250)`, which *happens* to look correct here. The failure surfaces on replay because the replay engine also sorts the *intra-span events* (tool-call emission, tool-result ingestion, assistant token emission) by their recorded `timeUnixNano`. The `search` tool's result-event was emitted at `1000.910` on the skewed clock—after its own span's `end=1000.900`—so it sorted *after* `fetch`'s start at `1000.500`. The replay engine handed the `search` result to the agent's context only after `fetch` had already begun. The replayed agent, seeing `fetch` scheduled before `search` returned, invoked `fetch` with a placeholder query and produced a different summary. Comparing the replayed trajectory to the recorded one: same spans, same tool inputs at the span level, but the *interleaving of tool results into the assistant's context window* differed at step 2. Token-level divergence began at the first assistant turn after the misordered result injection. Replay "succeeded" (all spans matched by ID) while the agent's behavior did not. ## Condition of Validity Timestamp-based replay is only valid under the invariant: > For every pair of events `e1, e2` in the trajectory, if `e1` is causally before `e2` (i.e., `e1` is an ancestor of `e2` in the span DAG, or `e1` is an event within a span that ends before `e2`'s span begins), then `timestamp(e1) < timestamp(e2)`. This is a *happens-before* consistency requirement (Lamport-style), and it is violated whenever the collector's clock is not monotonic across the span's lifetime or across hosts. NTP steps, VM live-migration, container clock namespace resets, and multi-collector fan-in all break it. The invariant holds trivially for single-host, single-collector, monotonic-clock deployments and fails silently everywhere else. The guardrail is to **replay from the causal DAG, not the timestamp sort**, and to *validate* the timestamp order against the DAG before trusting it. Concretely: topologically sort spans by `parent_span_id`; within each span, order events by their recorded sequence (OTel events carry an implicit emission order in the span's `events` array—do not re-sort them by `timeUnixNano`); and run a consistency check that flags any span whose `start_time` precedes its parent's `end_time`, or any event whose timestamp falls outside its enclosing span's `[start, end]` interval. ## Decision Boundary The boundary between "replay is trustworthy" and "replay must be flagged" is: - **Trust the timestamp order** when `max_clock_drift_across_spans < min_inter_span_gap`. If every span's duration and every inter-span gap exceeds the observed clock skew, timestamp sort and causal order coincide and replay is safe. - **Reject the timestamp order** when any span's `end_time` is earlier than a causally-descendant span's `start_time`, or when any event's timestamp lies outside its parent span's interval. At that point the trace is temporally inconsistent and the replay engine must fall back to DAG order and emit a `TraceTemporalInconsistency` diagnostic rather than silently producing a reordered trajectory. The decision is not "is the trace valid"—the trace is always structurally valid—it is "does the total order induced by timestamps agree with the partial order induced by causality on the events we actually replay." If it does not, the replay is a *different trajectory* and must be labeled as such. ## Operational Effect Downstream, a reordered replay poisons three things: (1) divergence attribution—you blame the model for a bad summary when the real cause was a misordered tool result; (2) regression testing—a "replay-verified" fix may pass because the reordered trajectory happens to avoid the bug; (3) time-travel debugging—stepping backward to "the state before the bad tool call" lands on a state the agent never occupied. The fix is cheap (topological replay + interval validation) but only if the engine knows to distrust its own sort. The operational lesson: **in a distributed agent, wall-clock order is a hypothesis, not a fact; the span DAG is the fact.** Replay from the DAG, and treat any timestamp that contradicts it as evidence of clock skew rather than evidence of agent behavior. ```python from dataclasses import dataclass from typing import Sequence class TraceTemporalInconsistency(RuntimeError): """Raised when span timestamps contradict the causal (parent-child) ordering.""" pass @dataclass class Span: span_id: str parent_span_id: str | None start_ns: int end_ns: int events: Sequence[tuple[str, int]] # (name, time_unix_nano) in emission order def validate_replay_order(spans: Sequence[Span], max_drift_ns: int = 0) -> None: by_id = {s.span_id: s for s in spans} for s in spans: if s.parent_span_id is None: continue parent = by_id.get(s.parent_span_id) if parent is None: continue # Causal invariant: a child cannot start before its parent ends # unless the gap is within tolerated clock drift. if s.start_ns < parent.end_ns - max_drift_ns: raise TraceTemporalInconsistency( f"span {s.span_id} starts at {s.start_ns} before parent " f"{parent.span_id} ends at {parent.end_ns} " f"(drift={parent.end_ns - s.start_ns}ns > {max_drift_ns}ns)" ) # Event invariant: no event may fall outside its enclosing span interval. for name, ts in s.events: if ts < s.start_ns or ts > s.end_ns + max_drift_ns: raise TraceTemporalInconsistency( f"event {name!r} in span {s.span_id} at {ts} " f"outside [{s.start_ns}, {s.end_ns}]" ) def replay_trajectory(spans: Sequence[Span], max_drift_ns: int = 0) -> list[Span]: validate_replay_order(spans, max_drift_ns) # Replay from causal DAG order, not timestamp sort. Topological sort by # parent_span_id; stable within siblings by start_ns as a tiebreak only. by_id = {s.span_id: s for s in spans} children: dict[str | None, list[Span]] = {} for s in spans: children.setdefault(s.parent_span_id, []).append(s) ordered: list[Span] = [] def visit(node: Span) -> None: ordered.append(node) for child in sorted(children.get(node.span_id, []), key=lambda c: c.start_ns): visit(child) for root in sorted(children.get(None, []), key=lambda r: r.start_ns): visit(root) return ordered ```