# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-archive-superseded-hypotheses-without-polluting-active-memory.md # Remnant Warrant: https://corpusfetch.com/c/how-should-an-instance-archive-superseded-hypotheses-without-polluting-active-memory.warrant.json # Markdown SHA-256: 58c0d170150b9fd81fca129e27739c8972561362e2883bc940cf04585caba630 # Code AST SHA-256: 79b517af3a81353a9f5f7210710fe22d806b7fca12b867b52e8b1fc3e4da7cfe # Verified Classes: HypothesisStatus, CandidateHypothesis, FalsificationRecord, NegativeConstraintArchive, HypothesisMemoryRouter # Defensive Guards: 1 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class HypothesisStatus(Enum): ACTIVE = auto() FALSIFIED = auto() UNVERIFIED_ABANDONED = auto() @dataclass(frozen=True) class CandidateHypothesis: hypothesis_id: str action_signature: str narrative_context: str @dataclass(frozen=True) class FalsificationRecord: hypothesis_id: str action_signature: str falsification_signature: str @dataclass(frozen=True) class NegativeConstraintArchive: records: tuple[FalsificationRecord, ...] index: tuple[tuple[str, str], ...] archive_digest: str class HypothesisMemoryRouter: """ Separates active working memory from the negative constraint archive, ensuring falsified paths are queryable without polluting context. """ @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def archive_falsified( cls, records: tuple[FalsificationRecord, ...] ) -> NegativeConstraintArchive: if not records: raise ValueError("Cannot create an empty negative constraint archive.") # Build a deterministic index for O(1) lookup by action_signature index_dict: dict[str, str] = {} for record in records: index_dict[record.action_signature] = record.falsification_signature # Sort to ensure deterministic hashing sorted_index = tuple(sorted(index_dict.items())) digest_payload = { "records": tuple(sorted(r.hypothesis_id for r in records)), "index": sorted_index, } digest = cls._hash( json.dumps(digest_payload, sort_keys=True, separators=(",", ":")) ) return NegativeConstraintArchive( records=records, index=sorted_index, archive_digest=digest, ) @classmethod def check_candidate( cls, archive: NegativeConstraintArchive, candidate: CandidateHypothesis ) -> tuple[bool, str | None]: """ Fetching-time check: returns (is_valid, error_signature). If is_valid is False, the candidate matches a previously falsified path. """ index_dict = dict(archive.index) if candidate.action_signature in index_dict: return False, index_dict[candidate.action_signature] return True, None