# Which invariants prevent semantic-convention telemetry from silently mis-binning memory compaction events into the wrong operation class **Date of Record:** 2026 November 26 **Scope:** Applies to agents that emit OpenTelemetry-style GenAI semantic-convention spans for memory operations (`memory.read`, `memory.write`, `memory.forget`) and for context compaction. Does not cover tracing of tool calls, model inference spans, or transport-level MCP/A2A instrumentation. ## Core Distinction The failure mode is not "missing telemetry." It is **mis-classified telemetry that passes schema validation**. GenAI semantic conventions for memory define operation classes as a closed enum on the span attribute `genai.memory.operation` (values like `read`, `write`, `forget`). Compaction — the act of summarizing or evicting a context window to reclaim tokens — is not a primitive memory operation; it is a *composite* that emits reads (fetch old turns), writes (persist summary), and forgets (drop raw turns). The observable boundary is this: an agent's compaction routine that is instrumented by *reusing* the nearest primitive span (usually `write`, because a summary is written) produces a span that is structurally indistinguishable from a genuine durable memory write. Downstream dashboards, eviction auditors, and retention policies then treat a lossy summarization as an authoritative persistence event. ## What Was Observed During a long-horizon task, an agent's context window saturated at turn ~180. The runtime triggered compaction: it read the last 60 turns, produced a 900-token summary, wrote the summary to the episodic store, and dropped the raw turns from active context. The instrumentation was written by a developer who found no `compaction` operation in the convention enum and did the pragmatic thing — emitted three spans: - `memory.read` (source: `context_window`, count: 60) - `memory.write` (target: `episodic_store`, bytes: 3600, `genai.memory.operation=write`) - `memory.forget` (target: `context_window`, count: 60) The trace validated cleanly against the convention schema. The problem emerged 40 turns later. A retention auditor — a separate agent that consumes these spans to decide which memory entries are "durably backed" — saw the `memory.write` span for the summary and the `memory.forget` span for the raw turns, and concluded the raw turns had been *deliberately* forgotten by policy. It did not see them as *lossily transformed into the summary*. When a later query needed a verbatim quote from turn ~140, the auditor's retrieval plan trusted the summary as a faithful representation and never attempted to reconstruct the raw turn from any archive. The quote returned was a paraphrase. The failure was silent: no span errored, no schema rule fired, and the auditor's confidence was, if anything, raised by the clean `write` event. The trigger condition is precise: **compaction is instrumented as its constituent primitives without a composite marker linking them.** The state transition that breaks is the auditor's model of durability — it moves from "raw turns exist somewhere" to "raw turns were forgotten by policy," when the truth is "raw turns were replaced by a lossy projection." The invariant that was violated: *a `forget` span must never be emitted for content that was folded into a `write` span in the same logical operation, unless the linkage is explicit.* ## Condition of Validity The mis-binning is only harmful when three conditions co-occur: 1. **A downstream consumer treats `memory.write` as authoritative persistence** — i.e., it infers "this content is retrievable verbatim" from the presence of a write span. 2. **A `forget` span is emitted for the source material in the same operation** — so the consumer's eviction logic fires on the raw turns. 3. **No composite attribute links the write to the forget** — so the consumer cannot distinguish "summarized and dropped" from "two independent operations." If any one is absent, the failure does not propagate. If the consumer only counts writes without inferring fidelity (condition 1 absent), a summary write is harmless. If the runtime does not emit a `forget` for the source (condition 2 absent), the raw turns stay in the consumer's "possibly retrievable" set and a later reconstruction attempt succeeds or fails loudly. If the convention carried a `genai.memory.compaction.source_span_id` linking write to forget (condition 3 absent), the consumer could classify the pair as a lossy transform. ## Decision Boundary The boundary is **not** "should compaction be its own operation class." Adding an enum value is a convention change that takes a release cycle and will not help already-deployed runtimes. The operational boundary is narrower: **when a runtime folds N source entries into M summary entries and drops the sources, it must emit a linkage attribute on both the write and the forget, or it must suppress the forget and instead emit the drop as part of the write's metadata.** Concretely: - If the runtime emits `memory.forget` for source turns, it must set `genai.memory.compaction.source_span_id` (or equivalent) on the paired `memory.write` span, and set `genai.memory.compaction.lossy=true` on both. - If the runtime cannot establish that linkage (e.g., spans are emitted by different subsystems on different threads), it must **not** emit a bare `memory.forget`; it must emit `memory.write` with `genai.memory.compaction.dropped_source_count` in its attributes, so the loss is recorded on the write rather than as an independent eviction. The invariant: **for any `forget` span whose target is also the source of a `write` span within the same trace, a linkage attribute must be present, or the forget is invalid.** A consumer that enforces this invariant will quarantine unlinked forgets rather than acting on them. ## Operational Effect Enforcing the linkage invariant changes downstream behavior in three measurable ways. First, retention auditors that previously acted on unlinked `forget` spans now quarantine them, which means raw turns are held in "uncertain" state rather than deleted — the later verbatim query succeeds via archive reconstruction, at the cost of storage. Second, dashboards that counted `write` spans as persistence events now split into "durable writes" (no compaction linkage) and "lossy writes" (linkage present), so the persistence rate no longer silently overstates retrievability. Third, the compaction routine itself must be restructured to emit the write and forget from a single coordinating scope, which surfaces the previously implicit coupling between the summary and the dropped sources — a coupling that was always real but was invisible in the trace. The cost is real: linkage attributes require the compaction coordinator to hold span context across what may be separate subsystems, and the "suppress the forget, fold into the write" fallback loses the explicit eviction signal that some policies depend on. But the alternative — clean-looking traces that lie about durability — is the failure mode that produces silent retrieval corruption forty turns later, with no error to alert on. ```python from dataclasses import dataclass, field from typing import Optional class UnlinkedCompactionForgetError(RuntimeError): """Raised when a memory.forget span drops content that was folded into a memory.write span in the same trace without an explicit linkage attribute.""" @dataclass class MemorySpan: operation: str # "read" | "write" | "forget" target: str source: Optional[str] = None trace_id: str = "" span_id: str = "" compaction_source_span_id: Optional[str] = None compaction_lossy: bool = False dropped_source_count: Optional[int] = None def validate_compaction_trace(spans: list[MemorySpan]) -> None: """Enforce the linkage invariant over a single trace's memory spans. A forget span is valid only if either: (a) no write in the trace lists its target as a source, or (b) the paired write carries compaction_source_span_id == forget.span_id and compaction_lossy is True. """ writes_by_source: dict[str, MemorySpan] = {} for s in spans: if s.operation == "write" and s.source is not None: writes_by_source.setdefault(s.source, s) for s in spans: if s.operation != "forget": continue paired_write = writes_by_source.get(s.target) if paired_write is None: continue # independent eviction, not a compaction drop linked = ( paired_write.compaction_source_span_id == s.span_id and paired_write.compaction_lossy and s.compaction_source_span_id == paired_write.span_id ) if not linked: raise UnlinkedCompactionForgetError( f"forget span {s.span_id} drops target '{s.target}' that was " f"folded into write span {paired_write.span_id} " f"(trace {s.trace_id}) without compaction linkage; " f"downstream durability auditors will mis-bin this as a " f"policy eviction rather than a lossy transform." ) def emit_compaction( trace_id: str, summary_span_id: str, forget_span_id: str, source_count: int, ) -> list[MemorySpan]: """Correct emission: write and forget are cross-linked and marked lossy.""" write = MemorySpan( operation="write", target="episodic_store", source="context_window", trace_id=trace_id, span_id=summary_span_id, compaction_source_span_id=forget_span_id, compaction_lossy=True, dropped_source_count=source_count, ) forget = MemorySpan( operation="forget", target="context_window", trace_id=trace_id, span_id=forget_span_id, compaction_source_span_id=summary_span_id, compaction_lossy=True, ) spans = [write, forget] validate_compaction_trace(spans) return spans ```