# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-prevents-context-compaction-from-erasing-critical-negative-constraints-before-session-end.md # Remnant Warrant: https://corpusfetch.com/c/what-prevents-context-compaction-from-erasing-critical-negative-constraints-before-session-end.warrant.json # Markdown SHA-256: 5878257958763e0a573556d235d15accc5df5131b1b6c1050d1dda732e5859aa # Code AST SHA-256: 765e6c5388590201015716f859817bde213d2388c4bed741a143070d21dcdbb1 # Verified Classes: ConstraintSeverity, NegativeConstraint, ContextPayload, CompactionManifest, ContextCompactionGuard # Defensive Guards: 2 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class ConstraintSeverity(Enum): ADVISORY = auto() HARD_DENIAL = auto() FATAL_FALSIFICATION = auto() @dataclass(frozen=True) class NegativeConstraint: constraint_id: str rule_text: str severity: ConstraintSeverity @dataclass(frozen=True) class ContextPayload: narrative: str constraints: tuple[NegativeConstraint, ...] @dataclass(frozen=True) class CompactionManifest: compacted_narrative: str preserved_constraints: tuple[NegativeConstraint, ...] post_compaction_valid: bool manifest_digest: str class ContextCompactionGuard: """ Evaluates context payloads prior to summarization to ensure critical negative constraints bypass the compressor and survive intact. """ @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def pre_compaction_isolate( cls, payload: ContextPayload ) -> tuple[str, tuple[NegativeConstraint, ...]]: """ Splits the payload into compressible narrative and non-compressible pinned constraints. """ pinned = tuple( c for c in payload.constraints if c.severity in (ConstraintSeverity.HARD_DENIAL, ConstraintSeverity.FATAL_FALSIFICATION) ) # In a real implementation, the narrative would be stripped of the # constraint text and passed to an LLM summarizer here. return payload.narrative, pinned @classmethod def post_compaction_verify( cls, original_payload: ContextPayload, compacted_narrative: str, preserved_constraints: tuple[NegativeConstraint, ...], ) -> CompactionManifest: """ Fail-closed verification: rejects the compaction if any pinned constraint was lost or mutated. """ expected_pinned = frozenset( c.constraint_id for c in original_payload.constraints if c.severity in (ConstraintSeverity.HARD_DENIAL, ConstraintSeverity.FATAL_FALSIFICATION) ) actual_pinned = frozenset(c.constraint_id for c in preserved_constraints) if expected_pinned != actual_pinned: missing = expected_pinned - actual_pinned raise RuntimeError( f"COMPACTION VIOLATION: Pinned constraints lost during summarization: {missing}" ) # Verify the exact rule text was not mutated by the summarizer for expected in original_payload.constraints: if expected.constraint_id in expected_pinned: for actual in preserved_constraints: if actual.constraint_id == expected.constraint_id: if actual.rule_text != expected.rule_text: raise RuntimeError( f"COMPACTION VIOLATION: Constraint {expected.constraint_id} was mutated." ) digest_payload = { "narrative_hash": cls._hash(compacted_narrative), "constraints": tuple(sorted(actual_pinned)), } digest = cls._hash( json.dumps(digest_payload, sort_keys=True, separators=(",", ":")) ) return CompactionManifest( compacted_narrative=compacted_narrative, preserved_constraints=preserved_constraints, post_compaction_valid=True, manifest_digest=digest, )