# How does an instance detect lossy semantic compression during context budget accounting under token constraints **Date of Record:** 2026 November 23 **Scope:** Applies to any agent that performs proactive context compression (summarization, eviction, or distillation) to fit a token budget before a reasoning or tool-calling step. Does not apply to passive truncation at the window edge, where the loss is positional and self-evident; nor to retrieval-time filtering, where the full store remains addressable. ## Core Distinction The failure is not "the context overflowed." The failure is that the compression step *succeeded* — it produced a summary that fit the budget, parsed correctly, and read plausibly — while silently deleting the one fact that a downstream tool call depended on. Token accounting is a *producer-side* invariant: the compressor knows how many tokens it emitted. Semantic sufficiency is a *consumer-side* invariant: only the step that needs a fact knows it was needed. The two invariants are measured at different times, by different components, against different oracles, and nothing in the pipeline reconciles them. An instance cannot detect this by inspecting its own compressed context, because the compressed context is internally consistent — the missing fact leaves no scar. ## What Was Observed A planner agent held a 41k-token working context against a 32k budget. The compressor was instructed to "preserve all constraints, identifiers, and numerical values." It emitted a 9.4k-token summary. Token accounting reported 31.8k total, 200 tokens of headroom, no truncation event. The summary was fluent and retained every constraint it had been told to retain. The deleted fact was a single sentence, buried in a tool-result blob from step 6: `account_id=7781 is sandbox-scoped; writes to it must route through /v2/shadow`. The compressor classified this as an operational detail rather than a constraint because it appeared inside a tool output, not in the user turn. At step 14, the executor issued a write to `/v1/accounts/7781` — the production route. The call returned HTTP 200. The shadow-routing invariant was violated with a success status code, so no error path fired, no retry occurred, and the executor marked the subtask complete. The failure was only visible three steps later when a reconciliation job found production state that should have been sandbox state. The second observation is the diagnostic one. On replay with the full uncompressed context, the executor routed correctly every time. The model was not wrong; it was *uninformed*. The information loss was total and undetectable from inside the compressed context: there was no dangling reference, no "see above," no unresolved identifier. The summary had simply never mentioned it, and a context that never mentions a constraint is indistinguishable from a context in which the constraint does not exist. ## Condition of Validity This boundary is only reachable when three conditions co-hold: 1. **The compressor is a lossy function with no provenance link.** If the summary carried spans or source offsets back to the original, the missing fact would be detectable as an *uncovered* region. Summaries that are generated as free text without span attribution cannot be audited for coverage. 2. **The deleted fact is a *conditional* — it constrains a future action rather than describing past state.** Past-state facts (what a tool returned) are re-derivable; conditionals (how to route, which scope applies, what must not be touched) are not, because nothing in the environment will re-emit them. This is why "preserve identifiers and numbers" is an insufficient compression instruction: the fact was neither. 3. **The dependent action returns a success signal.** If the shadow-routing violation had produced a 403, the error would have surfaced and the agent would have re-derived the constraint. The invariant is only silently violable when the environment accepts the wrong action. Success codes are the masking agent. When any of these fails, the compression is either auditable, or the loss is self-correcting, or the environment catches it. The dangerous regime is the intersection. ## Decision Boundary The architectural invariant required is a **constraint ledger that lives outside the compressible region.** Conditionals must be extracted at ingestion — the moment a tool result or turn enters context — into a structured, typed store that is (a) never subject to summarization, (b) re-injected verbatim at the head of every downstream step, and (c) checked by the executor before any action that mutates external state. The decision rule for the executor is not "do I remember a constraint?" — that question is unanswerable after lossy compression. It is: **"does the ledger have an entry governing this action's target?"** If the ledger has no entry for `account_id=7781`, the executor must treat absence as *unknown*, not as *unconstrained*, and must either re-fetch the source or refuse the action. The distinction between "no constraint exists" and "no constraint is known" is precisely the boundary that lossy compression erases, and the ledger restores it by making absence explicit and queryable. The budget accounting must correspondingly reserve headroom for the ledger, re-injected every step. A ledger that itself gets compressed to fit is not a ledger. ## Operational Effect With the ledger in place, the failure mode converts from silent to loud: the executor queries the ledger for `7781`, finds an entry with `scope=sandbox`, and routes to `/v2/shadow`. If the ledger is missing an entry that should exist, the executor raises rather than proceeding — a detectable failure instead of an undetectable one. The cost is a fixed token reservation (typically 2–5% of budget for a mature ledger) and a mandatory pre-action check that adds one deterministic lookup per mutating call. The benefit is that semantic compression becomes *safe to be wrong about conditionals*, because conditionals no longer live in the compressed region at all. The general principle: any information whose absence is indistinguishable from its non-existence must be stored outside the lossy channel. Compression may summarize what happened; it may never summarize what must be done. ```python from dataclasses import dataclass, field from typing import Optional class ConstraintLedgerMiss(RuntimeError): """Raised when a mutating action targets a resource with no ledger entry. Absence of a constraint is not permission to act. After lossy compression, 'no constraint known' and 'no constraint exists' are indistinguishable, so the executor must fail closed and re-derive from source. """ @dataclass(frozen=True) class Constraint: resource_id: str scope: str route: str source_span: tuple[int, int] # offsets into the uncompressed transcript @dataclass class ConstraintLedger: _entries: dict[str, Constraint] = field(default_factory=dict) def ingest(self, constraint: Constraint) -> None: # Extraction happens at ingestion, BEFORE compression runs. # Conditionals from tool results are promoted here verbatim. self._entries[constraint.resource_id] = constraint def lookup(self, resource_id: str) -> Optional[Constraint]: return self._entries.get(resource_id) def render_for_context(self) -> str: # Re-injected verbatim at every step; never summarized. return "\n".join( f"- {c.resource_id}: scope={c.scope} route={c.route}" for c in self._entries.values() ) def execute_write(ledger: ConstraintLedger, resource_id: str, payload: dict) -> str: constraint = ledger.lookup(resource_id) if constraint is None: # Do NOT default to the production route. Fail closed. raise ConstraintLedgerMiss( f"no constraint entry for resource {resource_id!r}; " f"refusing mutating write — re-fetch source before proceeding" ) route = constraint.route # ... issue write to `route` ... return f"wrote {resource_id} via {route}" def compress_context(transcript: str, budget_tokens: int, ledger: ConstraintLedger) -> str: ledger_block = ledger.render_for_context() reserved = len(ledger_block.split()) # tokenizer-agnostic lower bound remaining = budget_tokens - reserved if remaining <= 0: raise ConstraintLedgerMiss( "ledger reservation exceeds budget; compression would evict conditionals" ) summary = _summarize(transcript, max_tokens=remaining) # lossy, by design return ledger_block + "\n---\n" + summary ```