# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-replay-divergence-when-agent-traces-are-reconstructed-from-lossy-span-logs.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-replay-divergence-when-agent-traces-are-reconstructed-from-lossy-span-logs.warrant.json # Markdown SHA-256: b12a5538120590cc2c6c24edf925eb0374fd05f2d59d782daf80055bf701ceb1 # Code AST SHA-256: cab8c654de6285bca6ff7bfd8b7247a56d045eb76978fe8834c8b29e5b9d11a6 # Verified Classes: ReplayDivergenceError, Span, ReconstructedEdge # 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) from dataclasses import dataclass, field from typing import Optional class ReplayDivergenceError(RuntimeError): """Raised when a reconstructed trace asserts causality not supported by the log.""" pass @dataclass class Span: span_id: str parent_id: Optional[str] tool_name: str args_digest: str args_full: Optional[str] = None # None when log elided args status: str = "ok" @dataclass class ReconstructedEdge: child: str parent: str inferred: bool rule: Optional[str] = None def reconstruct(spans: list[Span]) -> list[ReconstructedEdge]: by_id = {s.span_id: s for s in spans} edges: list[ReconstructedEdge] = [] # Invariant 1: recorded parentage is authoritative. for s in spans: if s.parent_id is not None: if s.parent_id not in by_id: raise ReplayDivergenceError( f"span {s.span_id} references missing parent {s.parent_id}; " "cannot reconstruct causal chain" ) edges.append(ReconstructedEdge(s.span_id, s.parent_id, inferred=False)) # Invariant 2: digest-based identity may only merge when injective over observed set. digest_groups: dict[str, list[Span]] = {} for s in spans: digest_groups.setdefault(s.args_digest, []).append(s) for digest, group in digest_groups.items(): if len(group) < 2: continue # Candidate retry cluster: confirm identity using full args if available. confirmed = {s.args_full for s in group if s.args_full is not None} if any(s.args_full is None for s in group): raise ReplayDivergenceError( f"digest {digest} collides across {len(group)} spans with elided args; " "cannot prove retry vs distinct call — refusing to merge" ) if len(confirmed) > 1: # Distinct calls sharing a lossy digest. Do NOT dedup; record as inferred- # distinct and surface to auditor. for s in group: edges.append( ReconstructedEdge( child=s.span_id, parent=s.parent_id or "", inferred=True, rule="digest-collision-distinct", ) ) else: # Genuine retry: merge downstream of the first occurrence. first = min(group, key=lambda s: s.span_id) for s in group: if s.span_id != first.span_id: edges.append( ReconstructedEdge( child=s.span_id, parent=first.span_id, inferred=True, rule="confirmed-retry", ) ) # Invariant 3: any load-bearing inferred edge fails the reconstruction unless # the auditor has declared it acceptable. load_bearing = {e.child for e in edges if e.inferred} if load_bearing: raise ReplayDivergenceError( f"reconstruction contains {len(load_bearing)} inferred load-bearing edges; " "emit PartialReconstruction instead of asserting a causal trace" ) return edges