# In what scenarios must an instance refuse to emit a parent span link from a compaction event to a memory-write span whose semantic convention version predates the compaction **Date of Record:** 2026 November 23 **Scope:** Applies to agents that emit OpenTelemetry spans under GenAI semantic conventions (`gen_ai.*`) for memory read/write/compaction operations and link compaction spans back to the memory spans they summarize. Does not cover trace propagation across transport (W3C `traceparent`), nor span linking for tool-call or retrieval spans where no compaction boundary exists. ## Core Distinction A compaction event is not a memory write. It is a *derivation*: N episodic memory-write spans (`gen_ai.memory.operation = "write"`) are folded into a single semantic record, and the compaction span carries `gen_ai.memory.compaction.source_span_ids` plus a `Link` set pointing at each source. The subtle boundary is that span links are *assertions of provenance*, and provenance is only valid if the source span's attribute schema is interpretable by the linker's schema. When a memory-write span was emitted under an older GenAI semantic-convention revision — one whose `gen_ai.memory.entry.kind` enumeration did not include `"semantic"`, or whose `gen_ai.memory.entry.ttl_seconds` was absent — the compaction span cannot faithfully claim "I distilled these episodes into a semantic record," because the source did not carry the field that distinguishes episodic from semantic. Emitting the link anyway produces a trace that *looks* like valid provenance but is semantically incoherent: downstream auditors replaying the compaction cannot reconstruct which inputs were episodic and which were already semantic, so distillation-loss analysis silently double-counts. The invariant: **a compaction span may only link to a source memory span if the source's `gen_ai.schema.version` is ≥ the linker's minimum-compatible version for the fields the compaction asserts.** Below that threshold, the source must be linked as *opaque* (attribute-only reference, no provenance claim) or excluded with a recorded gap. ## What Was Observed A long-running research agent performed rolling compaction every 40 memory writes. During a schema rollout, half the fleet was upgraded to GenAI semconv `1.29.0` (which introduced `gen_ai.memory.entry.kind` as a required string enum) while the memory-write emitters on two replica workers remained on `1.27.0`. The upgraded compactor read 40 recent writes, found 17 of them lacking `gen_ai.memory.entry.kind`, and — because the OTel SDK's `add_link()` accepts any `SpanContext` without validating attributes — emitted 40 links regardless. Failure trace: 1. Compaction span `C` created with `gen_ai.memory.compaction.source_count = 40`. 2. For each source span `S_i`, `C.add_link(S_i.context, {"gen_ai.link.role": "distilled_from"})`. 3. For the 17 legacy spans, `S_i.attributes["gen_ai.memory.entry.kind"]` is `None`; the link is emitted anyway. 4. Downstream distillation-loss auditor computes `semantic_out / episodic_in`. It classifies inputs by reading `entry.kind` off the linked source. The 17 legacy spans fall into an `unknown` bucket that the auditor's denominator silently drops. 5. Reported distillation ratio inflates from 0.61 to 0.94 — a 54% overstatement — because the denominator shrank, not because distillation improved. The overstatement persisted for 6 hours of compaction cycles before a human noticed the ratio exceeded the theoretical ceiling implied by the token-budget ledger. No exception, no error span, no dropped link: the trace was structurally valid and semantically false. ## Condition of Validity The link-with-provenance claim holds if and only if, for every linked source span `S`: - `S.attributes["gen_ai.schema.version"]` parses and is ≥ `compactor.min_compatible_semconv`; - every attribute the compaction span asserts about its sources (here `gen_ai.memory.entry.kind`, `gen_ai.memory.entry.ttl_seconds`) is present on `S` with a value in the linker's enumeration; - the source span's `SpanContext` is not remote-only (a remote context without local attributes cannot be validated — the compactor must have the attribute set in-process or via a resource lookup that returned a hit). If any condition fails, the correct emission is a link with `gen_ai.link.role = "opaque_reference"` and *no* distillation claim, plus a span event `gen_ai.memory.compaction.schema_gap` recording the source span ID and the missing field. The compaction span's `source_count` must then be split into `distilled_count` and `opaque_count` so that downstream ratios have an honest denominator. This does **not** apply when the compactor and all emitters share a pinned schema version (the common case in a single-replica deployment) — there the version check is trivially satisfied and the guardrail is inert. It also does not apply to *retrieval* spans linked to writes, since retrieval provenance does not assert distillation. ## Decision Boundary The decision is not "is the source span old?" but "does the compaction span assert a field the source cannot supply?" A legacy span that happens to carry `entry.kind` (e.g., emitted by a backfilled 1.27.0 writer with the field set manually) is linkable; a 1.29.0 span missing the field due to a writer bug is not. Version is a *proxy* for field presence, never a substitute — the guardrail must check field presence directly and use version only to explain the gap in the event. The boundary is also asymmetric across compaction *direction*. Episodic→semantic compaction asserts the most fields and is the strictest. Semantic→semantic deduplication asserts only `entry.kind` equality and may link across a wider version window. A single global `min_compatible_semconv` const is therefore the wrong shape; the threshold must be per-compaction-kind. ## Operational Effect Enforcing the guardrail converts a silent denominator corruption into an explicit, replayable gap event. Auditors reading `distilled_count` / `opaque_count` can reconstruct the true distillation ratio for the window and flag the opaque sources for backfill. The cost is that compaction spans become slightly larger (one event per gap) and that the compactor must hold source attributes in-process rather than relying on the SDK's permissive `add_link`. In the observed incident, adding the guardrail would have kept the reported ratio at 0.61 ± 0.02 and surfaced the 17 legacy spans as a backfill worklist instead of a 54% phantom improvement. ```python from dataclasses import dataclass class SchemaGapLinkError(RuntimeError): """Raised when a compaction span attempts to assert provenance over a source memory span whose attributes cannot support the claim.""" pass REQUIRED_FIELDS_BY_KIND = { "episodic_to_semantic": ("gen_ai.memory.entry.kind", "gen_ai.memory.entry.ttl_seconds"), "semantic_dedup": ("gen_ai.memory.entry.kind",), } @dataclass class SourceSpan: span_id: str attributes: dict def classify_link(compaction_kind: str, source: SourceSpan, strict: bool = True) -> str: required = REQUIRED_FIELDS_BY_KIND[compaction_kind] missing = [f for f in required if source.attributes.get(f) is None] if not missing: return "distilled_from" if strict: # Provenance claim cannot be honored: refuse the link-with-claim. raise SchemaGapLinkError( f"source span {source.span_id} missing {missing} for " f"compaction kind {compaction_kind}; emit opaque_reference + " f"gen_ai.memory.compaction.schema_gap event instead" ) return "opaque_reference" def build_compaction_links(compaction_kind: str, sources: list[SourceSpan]) -> dict: distilled, opaque, gaps = [], [], [] for s in sources: try: role = classify_link(compaction_kind, s, strict=True) distilled.append((s.span_id, role)) except SchemaGapLinkError as exc: opaque.append((s.span_id, "opaque_reference")) gaps.append({"span_id": s.span_id, "reason": str(exc)}) # Honest denominator: callers must report both counts, not just source_count. return { "gen_ai.memory.compaction.distilled_count": len(distilled), "gen_ai.memory.compaction.opaque_count": len(opaque), "links": distilled + opaque, "gap_events": gaps, } ```