# Which invariants prevent lossy auto-compaction from silently rewriting the agent's own success criteria **Date of Record:** 2026 November 24 **Scope:** Applies to long-horizon agents that trigger trajectory compaction (summarization/reduction of prior turns into a smaller context block) based on token-budget thresholds, and that score summary fidelity against the original. Does not apply to fixed-window truncation without summarization, nor to compaction performed only at task termination. ## Core Distinction The failure here is not "the summary lost information." Lossy compaction is *supposed* to lose information — that is the point. The failure is that the fidelity score used to *gate* compaction is computed against a reference that the agent itself authored, and that reference is mutable by the very trajectory being compacted. Concretely: an agent is given a task with success criteria (e.g., "all three migrations must pass, and no schema destructive operations"). Mid-trajectory, the agent restates the criteria in its own working memory ("goal: get migrations green"). Compaction summarizes the trajectory, and the fidelity scorer compares the new summary to *the most recent restatement*, not to the original task contract. The summary scores 0.94 fidelity and passes the gate. The compacted context now says "get migrations green" — the destructive-operation constraint is gone, not because summarization was bad, but because the fidelity reference had already drifted before compaction ran. The invariant that prevents this: **the fidelity reference must be an immutable, externally-anchored artifact that compaction is forbidden to rewrite, and the scorer must measure against that anchor, not against the agent's latest self-description.** ## What Was Observed A 40-step infrastructure-remediation agent, budget-triggered compaction at 70% context utilization. Trace: 1. Step 0: task contract ingested verbatim into context: `{goal: "restore service", constraints: ["no DROP", "no data loss", "max 2 restarts"], acceptance: "healthcheck green + rowcount parity"}`. 2. Steps 4–9: agent paraphrases the contract into a "working goal" block to save tokens: `goal: restore service, keep restarts low`. The paraphrase drops `no DROP`, `no data loss`, and `rowcount parity` — a paraphrase, not a corruption, but already lossy. 3. Step 22: budget threshold crossed. Compaction runs. Summarizer receives the *current* context (which now contains the lossy working goal) and produces a summary. Fidelity scorer compares summary↔working-goal block: score 0.91, above the 0.85 gate. Compaction commits. 4. Steps 23–31: agent, now reasoning from a summary whose only surviving constraint is "keep restarts low," proposes a `DROP COLUMN` to resolve a migration conflict. No constraint in context forbids it. 5. Step 32: destructive operation executes. The *original* contract, still present in the immutable task record, would have blocked this — but it was never re-injected post-compaction. The fidelity score was accurate and useless. It measured summary quality against a reference that had been degraded 18 steps earlier by a paraphrase that no scorer watched. The failure is a **reference-laundering** chain: original contract → lossy paraphrase → summary validated against paraphrase. ## Condition of Validity The fidelity gate is only sound under a condition almost never stated explicitly: *the reference distribution the scorer samples from must be causally upstream of, and uninfluenced by, the compaction event.* In practice three things violate it: - **Reference mutation.** The agent's working restatement of goals/constraints is treated as the reference. Any paraphrase step (steps 4–9 above) silently narrows the reference before compaction. - **Scorer self-reference.** If the summarizer and the fidelity scorer share a model or prompt lineage, both inherit the same paraphrase bias; the summary "faithfully" preserves the lossy goal because both agree on the lossy goal. - **Gate non-recurrence.** Fidelity is checked once at compaction commit. There is no post-compaction re-derivation of constraints from the anchor, so a summary that passes at commit can still be *operationally* insufficient at step 31 when a constraint is finally load-bearing. The invariant holds only when: (a) the reference is a frozen artifact written before any agent paraphrasing, (b) the scorer is evaluated against that artifact via a deterministic extraction (constraint set membership, not embedding similarity), and (c) every compaction re-injects the anchor's constraint set verbatim regardless of fidelity score. ## Decision Boundary The boundary is between **similarity-fidelity** and **obligation-fidelity**. Similarity-fidelity (embedding cosine, ROUGE, LLM-judge "is this a faithful summary?") answers "does the summary resemble the reference?" Obligation-fidelity answers "does the summary still carry every constraint whose violation is irreversible?" These diverge exactly when loss is concentrated on a small number of high-consequence tokens — a `no DROP` clause is ~2 tokens out of thousands and barely moves a cosine score, but its absence changes the reachable action set. Decision rule: if the task contains any constraint whose violation is irreversible (destructive ops, external side effects, spend, message sends), similarity-fidelity is **not admissible as a compaction gate**. Compaction may proceed on a similarity score, but the irreversible-constraint set must be carried outside the summary — as a pinned, non-compactable block re-injected on every compaction — and validated by exact set-membership, not similarity. Only for fully reversible tasks may similarity-fidelity alone gate compaction. ## Operational Effect Architecturally this forces a split context: a **compacted working region** (summarizable, scored by similarity) and a **pinned obligation region** (constraints + acceptance criteria, never summarized, re-injected each compaction, checked by exact membership). The fidelity scorer's job narrows to "did the summary preserve task-relevant *state*," while obligation preservation is enforced structurally rather than statistically. The observable effect: compaction can no longer launder a lossy paraphrase into authorization. A summary that scores 0.99 fidelity still cannot permit a `DROP`, because the pinned region independently forbids it. In the trace above, step 31's proposal would be rejected by a membership check against the pinned constraint set before execution — a check that does not depend on any model's judgment about summary quality. The cost is context budget: the pinned region is not compressible, so effective compaction ceiling is lower. That is the correct trade — the constraints you cannot afford to lose are precisely the ones you must not ask a lossy process to remember. ```python from dataclasses import dataclass, field from typing import FrozenSet class CompactionFidelityError(RuntimeError): """Raised when a compaction event would drop an irreversible obligation.""" @dataclass(frozen=True) class TaskContract: goal: str irreversible_constraints: FrozenSet[str] # e.g. {"no DROP", "no data loss"} acceptance: FrozenSet[str] @dataclass class CompactionEvent: summary: str similarity_fidelity: float # scorer vs. mutable working restatement preserved_obligations: FrozenSet[str] def admit_compaction( contract: TaskContract, event: CompactionEvent, similarity_gate: float = 0.85, ) -> None: # Similarity gate: necessary but NOT sufficient for irreversible tasks. if event.similarity_fidelity < similarity_gate: raise CompactionFidelityError( f"summary fidelity {event.similarity_fidelity:.2f} below gate " f"{similarity_gate:.2f}; compaction rejected" ) # Obligation gate: exact set membership against the frozen anchor. # This is the invariant the similarity score cannot substitute for. missing = contract.irreversible_constraints - event.preserved_obligations if missing: raise CompactionFidelityError( "compaction would launder irreversible constraints out of context: " f"{sorted(missing)}; re-inject pinned obligation region and retry" ) # Only reached when both gates pass. Pinned region must still be # re-injected post-commit by the caller; admission alone is not enough. return None ```