# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-cognitive-load-invariants-must-survive-when-an-instance-reasoning-budget-is-reallocated-mid-task.md # Remnant Warrant: https://corpusfetch.com/c/what-cognitive-load-invariants-must-survive-when-an-instance-reasoning-budget-is-reallocated-mid-task.warrant.json # Markdown SHA-256: b521756430d8683b156d048220f9c089267a532ee0bf0ab9d6499cd4d9e9c1b0 # Code AST SHA-256: 8c927c48c60e31f7c9a13d931934227b99b989a3ff779f33af137efc0da089d7 # Verified Classes: ReallocationVerdict, BudgetReallocation, ReallocationPolicy, ReallocationManifest, BudgetReallocationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class ReallocationVerdict(Enum): NO_REALLOCATION = auto() REALLOCATION_CAPTURED = auto() SNAPSHOT_MISSING = auto() CONFIDENCE_UNATTRIBUTED = auto() @dataclass(frozen=True) class BudgetReallocation: reallocation_id: str original_budget: int new_budget: int reallocation_epoch: float in_progress_reasoning_hash: str | None cause: str confidence_at_cut: float | None @dataclass(frozen=True) class ReallocationPolicy: require_snapshot: bool require_confidence_attribution: bool @dataclass(frozen=True) class ReallocationManifest: verdict: ReallocationVerdict directive: str manifest_digest: str class BudgetReallocationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_reallocation( cls, realloc: BudgetReallocation | None, policy: ReallocationPolicy ) -> ReallocationManifest: # Gate 1: No reallocation if realloc is None: digest = cls._hash(json.dumps({ "status": "NO_REALLOCATION" }, sort_keys=True)) return ReallocationManifest( verdict=ReallocationVerdict.NO_REALLOCATION, directive="PROCEED: No budget reallocation occurred.", manifest_digest=digest ) # Gate 2: Reallocation increased budget (benign) if realloc.new_budget >= realloc.original_budget: digest = cls._hash(json.dumps({ "reallocation": realloc.reallocation_id, "direction": "INCREASE" }, sort_keys=True)) return ReallocationManifest( verdict=ReallocationVerdict.NO_REALLOCATION, directive="PROCEED: Budget increased, no truncation risk.", manifest_digest=digest ) # Gate 3: Snapshot requirement if policy.require_snapshot and not realloc.in_progress_reasoning_hash: return ReallocationManifest( verdict=ReallocationVerdict.SNAPSHOT_MISSING, directive=f"HALT: Budget cut on {realloc.reallocation_id} without reasoning snapshot.", manifest_digest=cls._hash(f"NO_SNAPSHOT:{realloc.reallocation_id}") ) # Gate 4: Confidence attribution if policy.require_confidence_attribution and realloc.confidence_at_cut is None: return ReallocationManifest( verdict=ReallocationVerdict.CONFIDENCE_UNATTRIBUTED, directive=f"HALT: Confidence at cut not recorded for {realloc.reallocation_id}.", manifest_digest=cls._hash(f"NO_CONF:{realloc.reallocation_id}") ) # Gate 5: Captured digest = cls._hash(json.dumps({ "reallocation": realloc.reallocation_id, "original": str(realloc.original_budget), "new": str(realloc.new_budget), "cause": realloc.cause, "snapshot": realloc.in_progress_reasoning_hash, "confidence_at_cut": f"{realloc.confidence_at_cut:.3f}" if realloc.confidence_at_cut else "NONE" }, sort_keys=True)) return ReallocationManifest( verdict=ReallocationVerdict.REALLOCATION_CAPTURED, directive=f"PROCEED: Reallocation {realloc.reallocation_id} captured with snapshot and confidence.", manifest_digest=digest )