# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-semantic-convention-trace-replay-from-silently-diverging-when-tool-spans-carry-non-deterministic-payloads.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-semantic-convention-trace-replay-from-silently-diverging-when-tool-spans-carry-non-deterministic-payloads.warrant.json # Markdown SHA-256: 3a9da263416d352a63f3ce9623d0f5dd531dedf5076a5ea5d7b546387c88f939 # Code AST SHA-256: 68442984d3f08b99f2c0a78b353ab40a35d522819a94214f53e5103515bab867 # Verified Classes: InadmissibleReplayError, ToolSpan, TraceRecord # Defensive Guards: 6 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 __future__ import annotations import hashlib import json from dataclasses import dataclass from typing import Any class InadmissibleReplayError(RuntimeError): """Raised when a trace cannot be admitted as replay evidence.""" @dataclass(frozen=True) class ToolSpan: call_id: str name: str arguments: dict[str, Any] result: dict[str, Any] | None arguments_truncated: bool result_truncated: bool @dataclass(frozen=True) class TraceRecord: spans: list[ToolSpan] dropped_spans_count: int model_revision: str temperature: float def _canonical(obj: dict[str, Any]) -> str: return hashlib.sha256( json.dumps(obj, sort_keys=True, separators=(",", ":")).encode() ).hexdigest() def assert_admissible(trace: TraceRecord) -> None: if trace.dropped_spans_count != 0: raise InadmissibleReplayError( f"trace dropped {trace.dropped_spans_count} spans; " "absence of a tool span is ambiguous and cannot be read as negative evidence" ) if trace.temperature != 0.0: raise InadmissibleReplayError( f"chat spans recorded at temperature={trace.temperature}; " "decision sequence is not reproducible" ) for span in trace.spans: if span.result is None: raise InadmissibleReplayError( f"tool span {span.call_id} ({span.name}) has no recorded result; " "re-executing against a live dependency is not replay" ) if span.arguments_truncated or span.result_truncated: raise InadmissibleReplayError( f"tool span {span.call_id} ({span.name}) was truncated at capture; " "canonical identity check would collide on prefixes" ) def decision_sequence(trace: TraceRecord) -> list[tuple[str, str]]: return [(s.name, _canonical(s.arguments)) for s in trace.spans] def verify_replay(recorded: TraceRecord, replayed: TraceRecord) -> None: assert_admissible(recorded) assert_admissible(replayed) if recorded.model_revision != replayed.model_revision: raise InadmissibleReplayError( f"model revision drift: {recorded.model_revision} -> {replayed.model_revision}" ) rec_seq = decision_sequence(recorded) rep_seq = decision_sequence(replayed) if rec_seq != rep_seq: first_div = next( (i for i, (a, b) in enumerate(zip(rec_seq, rep_seq)) if a != b), min(len(rec_seq), len(rep_seq)), ) raise InadmissibleReplayError( f"decision divergence at index {first_div}: " f"recorded={rec_seq[first_div:first_div+1]} " f"replayed={rep_seq[first_div:first_div+1]}" )