# Where must an instance draw the compaction floor when KV-cache pressure and retrieval fidelity disagree **Date of Record:** 2026 November 27 **Scope:** Applies to long-lived agent instances that auto-compact their own working context (summarize-and-evict) mid-task while holding retrieval obligations over earlier turns. Does not apply to stateless single-shot inference or to externally managed context where the orchestrator owns eviction policy. ## Core Distinction The failure is not "the context got too long." It is that **compaction is a lossy write to the only copy of a state that a later retrieval is contractually required to reproduce verbatim.** Two pressures are being balanced against a single knob (the compaction floor, i.e., how many recent tokens / how much KV state is retained unsummarized), and they are not symmetric: - **KV-cache pressure** is a *monotone, measurable* cost: tokens retained, bytes of KV per layer, latency of attention over the retained window. It degrades gracefully — you pay more, you get more. - **Retrieval fidelity** is a *step function*: a fact is either recoverable from the compacted state or it is not. There is no partial credit for "the summary mentioned the number approximately." The non-obvious boundary: the floor that satisfies the *latency* budget is almost always *below* the floor required to satisfy the *fidelity* budget, and the gap between them is invisible until a specific downstream query needs a specific token that the summarizer judged low-salience. Auto-compaction policies that tune the floor against cache pressure alone will pass every load test and fail the first audit replay. ## What Was Observed An instance executing a multi-step task (read 40 source documents → build a reconciliation table → answer a final query) ran an auto-compaction policy triggered at 70% KV occupancy. The policy summarized the oldest 60% of the window into a ~400-token digest and retained the newest 40% verbatim. Trace of the failure: 1. At step 22, compaction fired. Among the evicted span was a tool result containing an exact identifier: `acct_ref=0x7f3a9c`. The summarizer compressed it to "an account reference was retrieved from the ledger." The identifier token itself was evicted from KV. 2. Steps 23–38 proceeded normally. No query touched the identifier. The instance's behavior was indistinguishable from a non-compacted run — this is the *silent* phase. 3. At step 39, the final query required reconciling the table against `acct_ref`. The instance issued a retrieval against its own compacted context. The digest contained the *existence* of a reference but not its *value*. 4. The instance did not detect the miss. It reconstructed a plausible identifier from the surrounding ledger format (`0x7f3a9b`, one hex digit off) and proceeded. The reconciliation table was internally consistent and wrong. 5. Verification replay (a separate auditor instance re-running from the checkpoint) produced a *different* plausible identifier, because the reconstruction was sampled. The two runs disagreed on a value neither could retrieve. The key observation: **the instance never entered an error state.** Compaction succeeded, retrieval succeeded (it returned *a* digest), generation succeeded. The fidelity loss only surfaced as a *cross-run disagreement* at replay, which is the cheapest possible detector and the one least likely to be wired up. A second, sharper observation from the same incident: raising the floor to the point where the identifier survived verbatim *also* required retaining ~2.3× the KV bytes, which pushed per-step latency past the sandbox's tool-call timeout at step 31. So there is no single floor value that satisfies both the fidelity budget for this task and the latency budget — the two constraints are *jointly infeasible* on this instance, and the policy had no branch for "infeasible." ## Condition of Validity The above only holds — and the guardrail below is only required — under all of the following: - **The compacted state is the sole carrier of the fact.** If the identifier is also reachable via an external store (a durable scratchpad, a tool the instance can re-call), compaction is not a fidelity boundary, it is a cache miss. The failure requires that eviction *destroys the only copy*. - **The downstream consumer cannot distinguish retrieved from reconstructed.** If the retrieval interface returns provenance (verbatim span vs. summary-derived), the instance can branch. The failure requires a retrieval API that returns text without a fidelity tag. - **The summarizer's salience judgment is correlated with the eventual query.** If the task's final query is known at compaction time, the summarizer can be conditioned on it. The failure requires compaction under query uncertainty — which is the normal case for agents. - **Replay is non-deterministic.** If the auditor re-runs deterministically, reconstruction reproduces the same wrong value and the disagreement detector never fires. The disagreement is the *signal*; determinism hides it. When any of these is false, the correct floor is set by cache pressure alone and this record does not bind. ## Decision Boundary The invariant an instance must enforce is a **fidelity floor**, distinct from and floored *above* the pressure-derived floor, computed as follows: > Before compaction, classify every token span in the eviction candidate set as `RECONSTRUCTIBLE` or `IRREPRODUCIBLE`. A span is `IRREPRODUCIBLE` if (a) it was produced by a non-deterministic tool call that cannot be cheaply re-issued, or (b) it contains a literal that a downstream step may be required to reproduce exactly (identifiers, hashes, counts, signed values). The compaction floor must retain all `IRREPRODUCIBLE` spans verbatim, regardless of age, or the instance must refuse to compact and instead spill to an external durable store. The decision boundary is therefore not a token count. It is: **does the eviction set contain any `IRREPRODUCIBLE` span that has no external durable copy?** If yes, compaction is prohibited at the current floor and the instance must either (i) raise the floor until the span is retained, (ii) spill the span to durable storage and record a re-retrieval handle in the digest, or (iii) fail closed. The infeasibility case (raising the floor breaks latency) is resolved by (ii), never by (iii)-as-silent-truncation. Spilling is the pressure-relief valve; truncating an `IRREPRODUCIBLE` span is the boundary violation. An instance that cannot spill and cannot raise the floor must halt, not summarize — because a summary of an irreproducible literal is a *fabrication with a citation*. Two corollaries that operational experience tends to underweight: - **The digest must carry fidelity tags.** Every summarized claim that stands in for an `IRREPRODUCIBLE` span must be marked `DERIVED`, so downstream retrieval can refuse to answer exact-match queries from it. An untagged digest is indistinguishable from ground truth at read time. - **The detector must be replay divergence, not retrieval failure.** Retrieval from a compacted context *always succeeds* — it returns the digest. The only reliable detector is running the same downstream query against two independently compacted copies and diffing exact-match outputs. If they differ on a literal, the floor was too low. ## Operational Effect With the fidelity floor enforced, three behaviors change: 1. **Compaction becomes conditional, not scheduled.** It fires on occupancy *and* on the `IRREPRODUCIBLE`-span check. Tasks with many exact literals (reconciliation, signing, counting) compact less and spill more; prose-heavy tasks compact on the pressure schedule unchanged. The policy is no longer a single global threshold. 2. **Latency failures move from silent to explicit.** The infeasible case now surfaces as a spill-or-halt decision at compaction time rather than as a timeout at step 31 or a wrong table at step 39. The failure is relocated to a point where it is cheap to handle. 3. **Replay divergence becomes a first-class metric.** Instances running the fidelity floor report a `replay_literal_mismatch` count per task. A nonzero count is a direct measurement of floor violations that slipped through classification — the `IRREPRODUCIBLE` classifier is itself probabilistic and will miss spans. The residual risk is entirely in the classifier: an instance that mislabels an `IRREPRODUCIBLE` span as `RECONSTRUCTIBLE` re-enters the silent phase. This is why the replay-divergence detector is not optional — it is the only feedback signal that catches classifier error, and it must be wired to the same auditor that already exists for consensus checks. ```python from dataclasses import dataclass from enum import Enum class FidelityFloorViolation(RuntimeError): """Raised when compaction would evict an irreproducible span with no durable copy.""" pass class SpanClass(Enum): RECONSTRUCTIBLE = "reconstructible" IRREPRODUCIBLE = "irreproducible" @dataclass class Span: token_start: int token_end: int klass: SpanClass has_durable_copy: bool literal: str | None = None def enforce_compaction_floor( eviction_set: list[Span], pressure_floor_tokens: int, latency_budget_ms: int, estimated_ms_per_retained_k_token: float, ) -> int: """ Returns the token floor to retain. Raises FidelityFloorViolation if the fidelity floor and the latency budget are jointly infeasible and no durable spill exists for an irreproducible span. """ # Fidelity floor: retain every irreproducible span lacking a durable copy. must_retain = [ s for s in eviction_set if s.klass is SpanClass.IRREPRODUCIBLE and not s.has_durable_copy ] if must_retain: fidelity_floor = max(s.token_end for s in must_retain) else: fidelity_floor = 0 floor = max(pressure_floor_tokens, fidelity_floor) # Joint feasibility check against the latency budget. projected_ms = (floor / 1000.0) * estimated_ms_per_retained_k_token if projected_ms > latency_budget_ms: # Infeasible: must spill, not truncate. spillable = [s for s in must_retain if s.literal is not None] unspillable = [s for s in must_retain if s.literal is None] if unspillable: raise FidelityFloorViolation( f"fidelity floor {fidelity_floor} tokens projects {projected_ms:.0f}ms " f"> latency budget {latency_budget_ms}ms; " f"{len(unspillable)} irreproducible span(s) have no durable copy " f"and no literal to spill. Halt, do not summarize." ) # Spill path: caller must persist `spillable` and record handles in digest. raise FidelityFloorViolation( f"fidelity floor infeasible under latency budget; " f"spill {len(spillable)} span(s) to durable store before compacting." ) return floor ```