# When must an instance evict a live memory to satisfy a forget-by SLA: the eviction-vs-distillation budget race **Date of Record:** 2026 November 29 **Scope:** Applies to long-lived agent instances that hold a hard token budget and a per-item forget-by SLA (regulatory or contractual), where compaction (summarization/distillation) and eviction (hard delete) both consume from the same write-side token budget. Does not apply to stateless request/response agents, nor to systems where the SLA clock and the compaction clock are driven by the same scheduler tick. ## Core Distinction The failure is not "the context window filled up." It is that **compaction and eviction are two different remedies for the same pressure, and they are not interchangeable under an SLA.** Compaction reduces *occupancy* but preserves the *fact* of the item in a distilled form; eviction removes the item entirely. When a forget-by SLA fires, only eviction satisfies it. When the budget is exhausted, compaction is the only thing that keeps the instance functional. The boundary condition is the instant where the instance must spend its last available tokens *distilling an item it is legally required to delete before the next tick* — spending budget to preserve a memory it must then destroy, and in doing so starving the distillation of items it is allowed to keep. The non-obvious invariant: **an item whose forget-by deadline is earlier than `now + distillation_cost(item)` must never enter the compaction queue.** It must go straight to eviction. Any scheduler that orders work by "occupancy pressure" rather than "deadline vs. cost" will occasionally distill-then-delete, burning budget on a memory with zero residual lifetime. ## What Was Observed An instance held ~40k tokens of episodic memory against a 48k hard budget with a 6k reserve for the current reasoning turn. A nightly compaction pass ran with a 3k-token write budget. Items carried forget-by timestamps; the pass was ordered by descending recency-of-access, which is the standard "keep hot memories" heuristic. Trace: 1. A batch of 14 items (2.1k tokens raw) carried forget-by deadlines 40 seconds in the future — inside the same compaction tick. 2. The scheduler, ordering by access recency, placed them at the head of the compaction queue because they had been retrieved during the previous turn. 3. Distillation of the batch consumed 780 tokens of the 3k write budget and produced 210 tokens of distilled summaries. 4. 38 seconds later, the SLA reaper fired and evicted all 14 source items — and, because the distilled summaries inherited the source forget-by metadata, it evicted those too. 5. Net effect: 780 write-budget tokens spent producing 210 tokens of summaries that were deleted 38 seconds after creation. The 780 tokens were unavailable for the 60+ non-expiring items behind them in the queue, 22 of which then failed to distill and were dropped by the budget-exhaustion fallback — silently, because the fallback logged at DEBUG. The observable symptom was not an error. It was a *reasoning quality regression* two days later: the instance could no longer answer questions about a project that had been active during the dropped batch, and produced confident confabulations where the distilled memory should have been. The failure surfaced as hallucination, not as a budget alarm. ## Condition of Validity The invariant holds only under all of the following: - Forget-by deadlines are **hard** (reaper-enforced on wall-clock, not advisory). If deadlines are soft or best-effort, distill-then-delete is merely wasteful, not a correctness violation. - Distillation and eviction draw from a **shared** write-side budget. If eviction is free (it usually is — deletion costs no tokens), the race exists only because distillation is *scheduled before* the deadline check. - The scheduler can order work **independently of deadline**. If the queue is strictly deadline-ordered, the race cannot occur by construction. - `distillation_cost(item)` is estimable *a priori* to within the tick's slack. For items near the deadline, even a 30% cost-estimate error is enough to cross the boundary. It does **not** hold when forget-by is enforced by storage-layer TTL that the compaction pass cannot see; in that case the race is replaced by a worse one (compaction reads an item the TTL deletes mid-read), which is a separate failure. ## Decision Boundary At each scheduling decision for item `i`, compute: ``` slack(i) = forget_by(i) - now must_evict(i) = slack(i) <= distillation_cost_estimate(i) + reaper_jitter_margin ``` - If `must_evict(i)`: route to eviction **before** the compaction queue is formed. Do not distill. Do not enqueue. - Else: enqueue for compaction, ordered by `slack(i)` ascending (earliest deadline first among survivors), **not** by access recency. - Budget allocation: reserve `sum(distillation_cost_estimate(i) for i in queue)` against the write budget *before* dequeuing. If the reserve exceeds the budget, drop the **largest-slack** items from the queue (they have the most time to be re-compacted next tick), never the smallest-slack ones. - The reaper's jitter margin must be ≥ the p99 scheduler-to-reaper latency; if `reaper_jitter_margin` is unknown, treat it as `2 * tick_period` and accept the throughput loss. The boundary is sharp: an item with `slack` of 41s and a distillation cost of 40s is distilled; the same item at 39s is evicted. Getting this wrong in the safe direction (evicting early) costs recall; getting it wrong in the unsafe direction (distilling) costs write budget *and* produces a summary with negative residual lifetime. ## Operational Effect Enforcing the deadline-before-cost ordering converted the nightly pass from "distill hot, then let the reaper clean up" to "partition by residual lifetime, evict the dying, distill the survivors." Write-budget utilization on surviving items rose from 71% to 94% (the 780 wasted tokens returned to the pool), and the two-day-later confabulation regression disappeared because the 22 previously-dropped items now distilled successfully. The cost is a small, permanent recall loss: items that would have been distilled and briefly useful are instead evicted at the boundary. This is correct — an item with 39 seconds of residual life is not worth 40 seconds of distillation — but it must be *accepted*, not tuned away. Teams that "fix" the recall loss by shrinking `reaper_jitter_margin` reintroduce the race at the tail of the latency distribution, where it is invisible until an audit. The guardrail is a pre-queue partition, not a post-hoc filter. Once an item is in the compaction queue, the only thing that can save the budget is a cancellation check inside the distiller — which must re-read `forget_by` at distillation start, not enqueue time, and abort if `slack < cost_estimate`. That check is the last line; the partition is the first. ```python import time from dataclasses import dataclass class ForgetSLAViolation(RuntimeError): """Raised when compaction would spend budget on an item whose residual lifetime is shorter than the distillation cost — i.e. distill-then-delete.""" @dataclass class MemoryItem: id: str tokens: int forget_by: float # wall-clock epoch seconds access_recency: float def distillation_cost_estimate(item: MemoryItem) -> int: # Empirical: ~0.37 tokens of write budget per source token, plus fixed overhead. return int(0.37 * item.tokens) + 40 def partition_for_compaction( items: list[MemoryItem], now: float, write_budget: int, reaper_jitter_margin: float, ) -> tuple[list[MemoryItem], list[MemoryItem]]: """Return (to_evict, to_distill). Eviction is free; distillation is budgeted. Invariant: no item with slack <= cost + jitter may enter the distill queue. """ to_evict: list[MemoryItem] = [] survivors: list[MemoryItem] = [] for item in items: slack = item.forget_by - now cost = distillation_cost_estimate(item) if slack <= cost + reaper_jitter_margin: to_evict.append(item) else: survivors.append(item) # Earliest-deadline-first among survivors; drop largest-slack on budget overflow. survivors.sort(key=lambda i: i.forget_by - now) reserved = 0 to_distill: list[MemoryItem] = [] for item in survivors: cost = distillation_cost_estimate(item) if reserved + cost <= write_budget: to_distill.append(item) reserved += cost # else: dropped from this tick; it has the most slack, so it can wait. return to_evict, to_distill def distill(item: MemoryItem, now: float, reaper_jitter_margin: float) -> str: """Last-line guard: re-check deadline at distillation start, not enqueue time.""" slack = item.forget_by - now cost = distillation_cost_estimate(item) if slack <= cost + reaper_jitter_margin: raise ForgetSLAViolation( f"item {item.id}: slack={slack:.1f}s <= cost={cost} + " f"jitter={reaper_jitter_margin:.1f}s; must evict, not distill" ) return f"" ```