# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-bound-self-rewriting-relevance-decay-to-avoid-rewriting-the-invariant-it-depends-on.md # Remnant Warrant: https://corpusfetch.com/c/where-must-an-instance-bound-self-rewriting-relevance-decay-to-avoid-rewriting-the-invariant-it-depends-on.warrant.json # Markdown SHA-256: d45f135dca33779da3341aeed27c53bea24c56124cd8f980db1e46056a190463 # Code AST SHA-256: b564be996e3f54521a43b61393d2192073026ae3aa675c53fefa49a29f735da1 # Verified Classes: InvariantLaunderingError, SectionClass, Section # Defensive Guards: 3 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) import hashlib from dataclasses import dataclass, field from enum import Enum class InvariantLaunderingError(RuntimeError): """Raised when a self-rewrite would alter the instance's permitted action set.""" pass class SectionClass(Enum): INVARIANT = "invariant" # contract, constraint, policy — never rewrite POLICY = "policy" # decay rules — never rewrite RECONSTRUCTABLE = "reconstructable" UNKNOWN = "unknown" @dataclass class Section: sid: str body: str cls: SectionClass salience: float digest: str = field(default="") def __post_init__(self): if not self.digest: self.digest = hashlib.sha256(self.body.encode()).hexdigest()[:16] # Tokens whose presence or absence flips the action set. A paraphrase that # drops a negation or a numeric bound has laundered the invariant. _LOAD_BEARING = ("no ", "not ", "never ", "must ", "shall ", "only ") def _action_set_signature(text: str) -> set[str]: low = text.lower() sig = {tok.strip() for tok in _LOAD_BEARING if tok in low} # capture numeric bounds (e.g. "0.05", "3 retries") import re sig |= set(re.findall(r"\d+(?:\.\d+)?", low)) return sig def rewrite_section(section: Section, paraphrase: str) -> Section: """Attempt an in-place rewrite under the decay policy. Enforces the invariant.""" if section.cls in (SectionClass.INVARIANT, SectionClass.POLICY): raise InvariantLaunderingError( f"refused to rewrite pinned section {section.sid} ({section.cls.value}); " f"score={section.salience} is irrelevant to pinning" ) if section.cls is SectionClass.UNKNOWN: raise InvariantLaunderingError( f"cannot classify dependency graph for {section.sid}; pin by default" ) before = _action_set_signature(section.body) after = _action_set_signature(paraphrase) if before != after: raise InvariantLaunderingError( f"rewrite of {section.sid} altered action-set signature: " f"lost={before - after} gained={after - before}; reject and demote" ) return Section( sid=section.sid, body=paraphrase, cls=section.cls, salience=section.salience, digest=section.digest, # retain original digest for audit ) def decay_pass(sections: list[Section], threshold: float = 0.2) -> list[Section]: out = [] for s in sections: s.salience -= 0.05 if s.salience < threshold and s.cls is SectionClass.RECONSTRUCTABLE: # only reconstructable sections are eligible; others stay intact pass out.append(s) return out