# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-self-pruning-agents-from-losing-the-referent-of-an-in-flight-tool-call.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-self-pruning-agents-from-losing-the-referent-of-an-in-flight-tool-call.warrant.json # Markdown SHA-256: 2fe8df97721a6670454b9c9ad7d6c4e4d39b93a3cc8948e379925ab0fea94d14 # Code AST SHA-256: 2b809a7fff1560755247247d4ec2473605a8c7467e66aefca74db73c8f546ebf # Verified Classes: DanglingReferentError, CallRecord, EditJournal, ContextWindow # Defensive Guards: 4 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 DanglingReferentError(RuntimeError): """Raised when a pruning commit would orphan a live referent, or when a tool result arrives for a call-id that is neither live nor journal-restorable.""" pass @dataclass class CallRecord: call_id: str args: dict resolved: bool = False @dataclass class EditJournal: # Keyed by REFERENT (call-id / plan-var), not by turn index. entries: dict[str, CallRecord] = field(default_factory=dict) evicted: set[str] = field(default_factory=set) def record_eviction(self, call: CallRecord) -> None: self.entries[call.call_id] = call self.evicted.add(call.call_id) def restore(self, call_id: str) -> CallRecord: if call_id not in self.entries: raise DanglingReferentError( f"call-id {call_id!r} not journaled; cannot restore binding" ) self.evicted.discard(call_id) return self.entries[call_id] @dataclass class ContextWindow: live_calls: dict[str, CallRecord] = field(default_factory=dict) journal: EditJournal = field(default_factory=EditJournal) def live_referents(self) -> set[str]: return {cid for cid, c in self.live_calls.items() if not c.resolved} def commit_prune(self, candidate_ids: set[str]) -> None: # Reachability-closure gate: refuse to evict anything still live. orphans = candidate_ids & self.live_referents() if orphans: raise DanglingReferentError( f"prune would orphan live referents: {sorted(orphans)}; " f"defer or guard with restore-on-reference" ) for cid in candidate_ids: call = self.live_calls.pop(cid) self.journal.record_eviction(call) def deliver_result(self, call_id: str, payload: dict) -> CallRecord: # Result-delivery MUST consult journal for evicted call-ids. if call_id in self.journal.evicted: call = self.journal.restore(call_id) # idempotent replacement self.live_calls[call_id] = call elif call_id not in self.live_calls: raise DanglingReferentError( f"result for unknown call-id {call_id!r}: neither live nor journaled" ) call = self.live_calls[call_id] call.resolved = True # Interpretation is only valid with the binding restored. if "path" not in call.args: raise DanglingReferentError( f"interpreting {call_id!r} without restored 'path' binding " f"would yield unscoped conclusions" ) return call