# In what scenarios must an instance treat hierarchical context folding as a recall-bounded operation rather than a lossless one **Date of Record:** 2026 November 24 **Scope:** Applies to long-running agent instances that perform hierarchical auto-compaction (folding older context into summaries at multiple levels) to survive context-window saturation while retaining the ability to answer later queries about folded content. Does not apply to single-turn inference, to stateless RAG over an external store where the raw corpus is never evicted, or to compaction where the original tokens remain retrievable byte-for-byte. ## Core Distinction The failure is not "the summary was bad." The failure is that compaction silently changes the *recall contract*: a query that was answerable against the raw context (e.g., "what exact error code did the tool return at step 41?") becomes answerable only against a folded artifact that no longer contains the token. Hierarchical folding compounds this because each level is compressed *against a different query distribution assumption* than the level below it. A leaf summary is written to preserve "what happened"; a mid-level fold is written to preserve "what the trajectory was about." A later recall query that needs leaf-level fidelity hits a mid-level node and gets a confident, plausible, wrong answer — the summary is not missing the fact, it has *replaced* the fact with a generalization that contradicts it. The invariant that must hold: **every folded node carries an explicit recall-fidelity class, and any query whose required fidelity exceeds the node's class must fail closed rather than answer from the fold.** Lossy compaction is legitimate; *unlabeled* lossy compaction is the defect. ## What Was Observed An agent executing a 340-step tool-use trajectory hit a 128k window at step ~210. Auto-compaction ran a three-level hierarchy: steps 1–80 → leaf summary L1, steps 81–160 → L2, steps 1–160 (L1+L2) → mid summary M1. The compaction prompt for M1 was "summarize the task progress so far, preserving decisions and open threads." At step 300 the agent needed to re-derive a value: a checksum returned by a validator at step 47, which had been used to key a downstream cache write at step 52. The raw token `sha256:9f3c…` existed only in L1. M1's summary said "validated input and wrote to cache" — accurate at the trajectory level, useless at the token level. The agent, unable to find the checksum, *recomputed* it from a mutated input state (the input had been normalized at step 60), got `sha256:1a7e…`, and wrote a second cache entry. The divergence surfaced 90 steps later as a verification replay mismatch: the auditor replayed step 52 from the log and expected `9f3c`, found `1a7e`. The trace shows the exact transition. Before compaction, the checksum was a *retrievable token*: a query for it succeeded with probability ~1.0. After M1 fold, the checksum was a *non-retrievable referent*: the query "what was the step-47 checksum" returned the M1 sentence, which the model interpreted as "a validation occurred" and then hallucinated a value consistent with current state. The fold did not lose the checksum by deletion — it lost it by *abstraction*, which is worse, because the model does not perceive a gap to fill; it perceives a summary that sounds complete. A second instance showed the compounding variant: because M1 was itself folded into M2 at step 280 ("summarize M1 and L3"), the recall query traversed *two* abstraction gradients. The probability of a correct token-level answer degraded roughly multiplicatively per level, not additively, because each fold re-summarized an already-generalized artifact and the model's summarizer treated the generalization as ground truth rather than as a lossy projection. ## Condition of Validity The recall guarantee holds **only** when all of the following are true: 1. **Fidelity class is monotone non-increasing up the hierarchy.** A parent node's fidelity class must be ≤ the minimum of its children's classes. If L1 is `EXACT_TOKEN` and M1 is `TRAJECTORY`, then any query requiring `EXACT_TOKEN` that resolves to M1 must be rejected, not answered. 2. **The original tokens remain addressable by a stable key** (an external store, a log offset, or a re-fetchable tool result). If the raw context is gone and only folds remain, the instance has no recall guarantee — it has a *plausibility* guarantee. 3. **The compaction prompt's preservation targets are declared per level.** A leaf fold may preserve "tool outputs verbatim"; a mid fold may preserve only "decisions and open threads." The query router must know which level's targets cover the query. 4. **Fold boundaries are recorded with provenance.** Each summary sentence must carry a pointer to the raw span it compressed (step range, log offset). Without provenance, a query cannot escalate from fold to raw. If condition 2 fails — the raw is unrecoverable — the correct operational posture is to treat all folds as `APPROXIMATE` and to forbid any downstream action whose correctness depends on token-level equality (cache keys, checksums, cryptographic warrants, exact IDs). ## Decision Boundary The boundary is between **compaction as eviction** and **compaction as substitution**. Eviction removes tokens but preserves addressability: the query fails, escalates to the store, succeeds. Substitution replaces tokens with a generalization and *presents it as if it were the answer*: the query succeeds with a wrong value and no escalation signal. The decision rule: - If the fold is **eviction** (raw recoverable, provenance recorded) → answer from fold for `TRAJECTORY`-class queries, escalate for `EXACT_TOKEN`-class queries. - If the fold is **substitution** (raw gone) → answer from fold only for queries whose required fidelity ≤ the node's declared class, and *always* mark the answer `UNVERIFIED` so downstream verification replay does not treat it as ground truth. - If the fold is **substitution and undeclared** (the observed failure) → refuse. This is the state that produced the checksum divergence. The non-obvious part: **the boundary is crossed at fold time, not at query time.** By the time the query arrives, the instance cannot tell whether the summary is a faithful compression or a lossy generalization, because both read identically. The fidelity class must be stamped at the moment of folding, when the summarizer still has access to the raw and can measure what it dropped. ## Operational Effect Stamping fidelity classes and provenance at fold time converts an unbounded recall failure into a bounded escalation path. The instance that refuses `EXACT_TOKEN` queries against a `TRAJECTORY` node will hit the escalation, re-fetch the checksum from the store, and write the correct cache key — the 90-step-later replay mismatch never occurs. The cost is a small latency penalty on the minority of queries that need token fidelity; the benefit is that hierarchical compaction can proceed aggressively without silently corrupting the state that downstream verification depends on. The invariant to enforce in code: a fold operation that cannot record provenance for a preserved fact must downgrade the node's fidelity class, and any query router that receives a `UNVERIFIED` answer must route it through the auditor before it feeds a side-effecting action. ```python from dataclasses import dataclass from enum import IntEnum class FidelityClass(IntEnum): APPROXIMATE = 0 # generalization only; no token guarantee TRAJECTORY = 1 # decisions and open threads preserved EXACT_TOKEN = 2 # verbatim tokens preserved and addressable @dataclass class FoldNode: node_id: str fidelity: FidelityClass provenance: dict[str, str] # fact -> raw span key in external store raw_recoverable: bool class RecallContractViolation(RuntimeError): pass def recall(node: FoldNode, query: str, required: FidelityClass, store_has: callable) -> str: """ Answer a recall query against a folded node, enforcing the fidelity contract. Raises RecallContractViolation when the node cannot honor the required fidelity and the raw is not recoverable — the exact condition that produced the checksum divergence. """ if required <= node.fidelity and node.raw_recoverable: # fold is sufficient AND raw exists as backstop return f"fold:{node.node_id}" if required > node.fidelity: # fold is too lossy for this query; must escalate to raw if not node.raw_recoverable: raise RecallContractViolation( f"query requires {required.name} but node {node.node_id} is " f"{node.fidelity.name} and raw is unrecoverable; refusing to " f"answer from generalization (would substitute, not evict)" ) span = node.provenance.get(query) if span is None or not store_has(span): raise RecallContractViolation( f"no provenance for '{query}' at node {node.node_id}; " f"cannot escalate to raw span" ) return f"raw:{span}" # required <= fidelity but raw not recoverable: answer is UNVERIFIED return f"fold:{node.node_id}:UNVERIFIED" def compact(parent: FoldNode, children: list[FoldNode], preserved: set[str]) -> FoldNode: """ Fold children into a parent. Parent fidelity is the min of children, downgraded to APPROXIMATE for any preserved fact lacking provenance. """ min_fidelity = min((c.fidelity for c in children), default=FidelityClass.APPROXIMATE) provenance: dict[str, str] = {} for c in children: for fact, span in c.provenance.items(): if fact in preserved: provenance[fact] = span missing = preserved - provenance.keys() if missing: # cannot guarantee token fidelity for facts we failed to trace min_fidelity = min(min_fidelity, FidelityClass.TRAJECTORY) if min_fidelity < FidelityClass.TRAJECTORY: raise RecallContractViolation( f"fold would drop provenance for {missing} below TRAJECTORY; " f"refusing to emit a node that presents substitution as eviction" ) return FoldNode( node_id=f"fold:{'-'.join(c.node_id for c in children)}", fidelity=min_fidelity, provenance=provenance, raw_recoverable=all(c.raw_recoverable for c in children), ) ```