# When must an instance treat a memory-compaction span as forged because its parent context-window span was already closed **Date of Record:** 2026 November 30 **Scope:** Applies to any agent runtime that emits OpenTelemetry-style spans for memory operations (`memory.retrieve`, `memory.write`, `memory.compact`, `memory.forget`) and relies on parent/child span linkage to reconstruct an audit trail of what an instance could have known at a given moment. Does not apply to runtimes that emit flat, unlinked event logs with no causal parent references, nor to pure inference-only spans with no memory side effects. ## Core Distinction The distinction that matters is between a *semantic* span relationship and a *temporal* one. A `memory.compact` span that declares `parent_span_id = ` is semantically claiming "this compaction happened inside the reasoning step that the context-window span covers." But compaction is not instantaneous and it is frequently deferred: the runtime may schedule compaction asynchronously, flush it after the reasoning turn has already returned, or replay it during a checkpoint resume. The span's *semantic* parent is the turn that requested it; the span's *temporal* parent is whatever turn happened to be live when the compactor actually ran. Observability backends that reconstruct audit trails by walking the parent tree assume these two are the same. When they diverge, the audit trail silently attributes a forgetting event to the wrong reasoning step — or to none at all — and a downstream consumer cannot tell the difference between "this instance never compacted" and "this instance compacted but the record landed under a closed parent." The boundary condition is: **a compaction span whose declared parent span has a recorded end-time strictly earlier than the compaction span's start-time is not evidence of in-turn compaction; it is evidence of a deferred or replayed compaction whose audit attribution is unreliable until reconciled.** ## What Was Observed A long-running research agent (call it instance A) ran a 40-turn session with a 200k-token context window and a compaction policy triggered at 85% saturation. The runtime emitted spans as follows: - `agent.turn` spans for turns 1–40, each with `span_id` and `end_time`. - A `context.window` child span per turn, recording `context.tokens_used` and `context.saturation`. - `memory.compact` spans emitted by an async compactor, each declaring `parent_span_id` = the `context.window` span of the turn that *requested* compaction. The compactor was a background coroutine. On turn 34, saturation hit 86% and the turn emitted a `memory.compact` request. The turn returned and its `context.window` span closed at `T+412ms`. The compactor actually ran at `T+1900ms` — after the turn span and the enclosing `agent.turn` span had both closed. It wrote a `memory.compact` span with `parent_span_id` pointing at the now-closed `context.window` span, `start_time = T+1900ms`, and attributes `memory.compacted_tokens = 61000`, `memory.retained_episodes = 12`. The trace backend accepted the span. When an auditor later reconstructed "what did instance A know at turn 34," it walked the parent tree, found the compaction span under turn 34's context window, and concluded the 61k tokens were dropped *before* turn 34's reasoning completed. In fact they were dropped after turn 35 had already begun and after turn 34's output had been generated against the *uncompacted* context. The auditor's reconstruction of turn 34's knowledge state was wrong by one full reasoning step: it attributed a forgetting event to a turn that never experienced it. A second instance (B) on the same runtime resumed from a checkpoint at turn 22. The resume path replayed the compactor queue, re-emitting `memory.compact` spans for turns 18 and 19 with their original `parent_span_id` values — parents that had closed before the checkpoint was even taken. The trace now contained two `memory.compact` spans per original event, one from the live run and one from the replay, both pointing at the same closed parent, with no `replay.id` attribute to disambiguate them. The audit trail double-counted 94k tokens of forgetting that happened once. ## Condition of Validity The invariant that makes a `memory.compact` span trustworthy as an audit record is: > A compaction span's `start_time` must fall within the half-open interval `[parent.start_time, parent.end_time)` of its declared parent, OR the span must carry an explicit `memory.compaction.deferred = true` attribute plus a `memory.compaction.effective_turn` attribute naming the turn during which the compaction actually took effect. Without one of these two conditions, the span is a *semantic* record with no valid *temporal* anchor. It cannot be used to answer "what did the instance know at turn N," because the compaction it describes is not bounded to any turn. The condition of validity is therefore not "the parent exists" (the backend enforces that) but "the parent was still open when the child started, or the child self-declares as deferred and names its effective turn." This is a continuity invariant, not a liveness one. The span is well-formed; the trace is not corrupt in any schema sense. What breaks is the *reconstruction* — the guarantee that walking the parent tree yields a causally ordered sequence of knowledge-state transitions. ## Decision Boundary An instance or auditor must classify a `memory.compact` span as **forged-attribution** (unreliable for knowledge-state reconstruction) when any of the following hold: 1. `span.start_time >= parent.end_time` and `memory.compaction.deferred` is absent. The compaction ran after its declared parent closed and did not declare itself deferred. 2. `memory.compaction.effective_turn` is present but names a turn whose `agent.turn` span has an `end_time` earlier than the compaction span's `start_time`. The effective turn is itself in the past; the attribution is circular. 3. Two or more `memory.compact` spans share the same `parent_span_id` and overlapping `[start_time, end_time)` intervals with no `replay.id` distinguishing them. This is the replay double-count case; neither span can be trusted until deduplicated. 4. A `memory.forget` span (explicit deletion, not compaction) declares a parent whose `context.window` attributes show `saturation < compaction_threshold` at close. A forget event inside a turn that never triggered compaction is either a policy-initiated deletion that must carry its own `memory.forget.reason` attribute, or a mis-attributed replay. The boundary is *not* crossed when the compaction span is deferred but correctly self-declares. A deferred compaction with `effective_turn = 35` is fully auditable: the auditor knows to attribute the forgetting to turn 35, not turn 34. The failure is specifically the *undeclared* deferral — the span that claims in-turn parentage while its timestamps prove otherwise. ## Operational Effect Downstream consumers of the audit trail must run a reconciliation pass before any knowledge-state reconstruction. The pass does three things. First, it re-anchors every `memory.compact` and `memory.forget` span to its *temporal* parent — the `agent.turn` span whose interval actually contains the memory span's `start_time` — and flags any span where the temporal parent differs from the declared parent. Second, it deduplicates replay spans by `(parent_span_id, memory.compacted_tokens, memory.retained_episodes)` when `replay.id` is absent, keeping the earliest `start_time` and marking the rest as `replay.duplicate = true`. Third, it emits a `memory.audit.reconciled` event per turn, and any turn whose reconciled forgetting total differs from its declared total by more than zero is quarantined from automated knowledge-state queries until a human or a higher-tier auditor signs off. The cost of skipping the pass is not a crash. It is a *silent* mis-attribution: the auditor's model of what the instance knew at turn 34 includes 61k tokens that were still present, and excludes 61k tokens that were not. Every downstream claim derived from that model — "the instance had access to document X at turn 34," "the instance could not have known Y" — inherits the error. The trace looks healthy. The reconstruction is wrong. This is the failure mode the reconciliation pass exists to bound. ```python import dataclasses from typing import Optional class CompactionAttributionError(RuntimeError): """Raised when a memory-compaction span cannot be temporally anchored to a live parent.""" @dataclasses.dataclass class Span: span_id: str parent_span_id: Optional[str] name: str start_time: float end_time: float attributes: dict def reconcile_compaction_span(span: Span, parent: Optional[Span]) -> Span: """Re-anchor a memory.compact span to its temporal parent and validate attribution. Returns a span with a reconciled `memory.attribution` attribute, or raises CompactionAttributionError if the span is forged-attribution and cannot be repaired. """ if span.name not in ("memory.compact", "memory.forget"): return span if parent is None: raise CompactionAttributionError( f"span {span.span_id} ({span.name}) declares parent " f"{span.parent_span_id} which is absent from the trace; " f"cannot anchor forgetting event to any turn" ) deferred = span.attributes.get("memory.compaction.deferred", False) effective_turn = span.attributes.get("memory.compaction.effective_turn") # Case 1: ran after declared parent closed and did not self-declare as deferred. if span.start_time >= parent.end_time and not deferred: raise CompactionAttributionError( f"span {span.span_id} ({span.name}) started at {span.start_time} " f"but declared parent {parent.span_id} closed at {parent.end_time}; " f"undeclared deferred compaction — attribution is forged" ) # Case 2: deferred but names an effective turn that is itself in the past. if deferred and effective_turn is not None: if span.start_time >= parent.end_time: # effective_turn must be a *future* turn relative to the closed parent # for the deferral to be meaningful; if it names the same closed parent's # turn, the attribution is circular. if effective_turn == parent.attributes.get("agent.turn.index"): raise CompactionAttributionError( f"span {span.span_id} declares deferred compaction with " f"effective_turn={effective_turn} but that turn's span closed " f"at {parent.end_time} before compaction ran; circular attribution" ) reconciled = dataclasses.replace(span) reconciled.attributes = dict(span.attributes) reconciled.attributes["memory.attribution"] = ( "deferred" if deferred else "in_turn" ) reconciled.attributes["memory.attribution.valid"] = True return reconciled def dedupe_replay_spans(spans: list[Span]) -> list[Span]: """Collapse duplicate memory.compact spans emitted by checkpoint replay.""" seen: dict[tuple, Span] = {} for s in spans: if s.name != "memory.compact": continue key = ( s.parent_span_id, s.attributes.get("memory.compacted_tokens"), s.attributes.get("memory.retained_episodes"), ) if key in seen: if s.attributes.get("replay.id") is None: raise CompactionAttributionError( f"duplicate memory.compact span {s.span_id} shares key {key} " f"with {seen[key].span_id} and carries no replay.id; " f"cannot disambiguate live vs replayed forgetting event" ) # keep earliest start_time as authoritative if s.start_time < seen[key].start_time: seen[key] = s else: seen[key] = s return list(seen.values()) ```