# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-hierarchical-compaction-from-silently-severing-retrieval-keyed-reconstruction-in-long-horizon-agents.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-hierarchical-compaction-from-silently-severing-retrieval-keyed-reconstruction-in-long-horizon-agents.warrant.json # Markdown SHA-256: fa04af4d777f037ae40ca60798bbe9d0599e2bb25740d20737f50cd5f61502e0 # Code AST SHA-256: 68e53dd876637a069b1612895ea7e364c6ac7a592712694aa76f4adcba0e1dfa # Verified Classes: CompactionInvariantError, SummaryNode # Defensive Guards: 2 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) from dataclasses import dataclass, field from typing import Optional class CompactionInvariantError(RuntimeError): """Raised when a retrieval key cannot be reconstructed from its owning summary node.""" pass @dataclass class SummaryNode: node_id: str summary_text: str keys: list[str] = field(default_factory=list) aliases: dict[str, str] = field(default_factory=dict) # key -> owning node_id lossy: bool = False def admit_key(node: SummaryNode, key: str, *, important: bool) -> None: """Enforce key containment: a key may enter the index only if the summary contains it verbatim or advertises it as a tombstoned alias.""" if key in node.summary_text: node.keys.append(key) return if key in node.aliases: node.keys.append(key) return if important: # Cannot preserve and not advertised: must not silently index it. raise CompactionInvariantError( f"key {key!r} absent from summary {node.node_id!r} and not advertised " f"as alias; indexing it would make it retrievable-but-unqueryable" ) node.lossy = True # drop the key, mark the span lossy def reconstruct(node: SummaryNode, query_key: str) -> Optional[str]: """Return the owning span text for a key, or raise on ambiguous absence.""" if query_key in node.keys: return node.summary_text if node.lossy: raise CompactionInvariantError( f"span {node.node_id!r} is LOSSY; query {query_key!r} cannot be " f"distinguished from absent — escalate, do not fabricate" ) return None # typed KEY_ABSENT at the caller