# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-anchor-causal-replay-when-a-non-deterministic-trace-diverges-before-its-first-tool-call.md # Remnant Warrant: https://corpusfetch.com/c/where-must-an-instance-anchor-causal-replay-when-a-non-deterministic-trace-diverges-before-its-first-tool-call.warrant.json # Markdown SHA-256: b5e9e14ca0da8fa54f01ec5735b58e3613ba92b8b8d8a51b9ff77a8b54b9b259 # Code AST SHA-256: d197433dde4d6e0e4f5ba282bffb1ef8254d3424bcdd02479ebc7231a88084c0 # Verified Classes: DivergenceClass, InsufficientTraceFidelity, TraceAnchor # Defensive Guards: 2 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 from enum import Enum class DivergenceClass(Enum): PREFIX_FORK = "prefix_fork" # sampling diverged before first tool call TOOL_STATE_DRIFT = "tool_state_drift" # same call, different response HARNESS_MISALIGN = "harness_misalign" # injection keyed by position, not signature UNCLASSIFIABLE = "unclassifiable" # anchor too coarse to localize class InsufficientTraceFidelity(RuntimeError): """Raised when the replay anchor is coarser than the decision it must explain.""" pass @dataclass class TraceAnchor: has_sampled_prefix: bool # tokens + logprobs + context hash persisted pre_tool_entropy: float # entropy at decision preceding first tool call entropy_tolerance: float = 0.15 def classify_divergence( anchor: TraceAnchor, replayed_prefix_matches: bool | None, first_call_signature_matches: bool, first_response_matches: bool, injection_keyed_by_position: bool, ) -> DivergenceClass: # Guardrail: if the anchor is coarser than the sampling boundary AND the # pre-tool decision was high-entropy, any label would be an alias of the # harness's own resampling. Refuse to classify. if not anchor.has_sampled_prefix and anchor.pre_tool_entropy > anchor.entropy_tolerance: raise InsufficientTraceFidelity( f"anchor lacks sampled prefix but pre-tool entropy " f"{anchor.pre_tool_entropy:.3f} > tolerance {anchor.entropy_tolerance:.3f}; " f"divergence cannot be localized to model vs. tool layer" ) if anchor.has_sampled_prefix and replayed_prefix_matches is False: return DivergenceClass.PREFIX_FORK if injection_keyed_by_position and not first_call_signature_matches: return DivergenceClass.HARNESS_MISALIGN if first_call_signature_matches and not first_response_matches: return DivergenceClass.TOOL_STATE_DRIFT if first_call_signature_matches and first_response_matches: raise InsufficientTraceFidelity( "no divergence observed at first tool boundary; " "anchor does not cover the divergent decision — re-anchor downstream" ) # Signature differs with an intact prefix and signature-keyed injection: # this is a model-layer fork we failed to pre-empt. return DivergenceClass.PREFIX_FORK def context_hash(tokens: list[str]) -> str: return hashlib.sha256("|".join(tokens).encode()).hexdigest()