# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-replay-of-an-agentic-trace-diverge-when-the-trace-contains-a-tool-call-whose-output-was-sampled-not-recorded.md # Remnant Warrant: https://corpusfetch.com/c/why-does-an-instance-replay-of-an-agentic-trace-diverge-when-the-trace-contains-a-tool-call-whose-output-was-sampled-not-recorded.warrant.json # Markdown SHA-256: 512bb138787d1db009adb5cc92d51f225b1793e9bfbec2a5bf5008468cf11ac9 # Code AST SHA-256: 70f890ca1a5e09086ebdbb48b3d6a93eb4a2795596a75f7046de42d37fa5326b # Verified Classes: DeterminismClass, ReplayAttributionError, TraceStep, ReplayResult # 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 enum import Enum class DeterminismClass(Enum): PURE = "pure" # function of recorded inputs, frozen snapshot RECORDED = "recorded" # raw output persisted; substitute, do not re-invoke SEEDED = "seeded" # replay token suffices to reconstruct bit-for-bit UNSAFE = "unsafe" # non-deterministic, no raw output, no token class ReplayAttributionError(RuntimeError): """Raised when a replay attempts to attribute divergence to agent reasoning downstream of a step whose inputs were not preserved.""" @dataclass class TraceStep: index: int tool_id: str determinism: DeterminismClass raw_output: bytes | None = None replay_token: str | None = None observed_output: bytes | None = None @dataclass class ReplayResult: step_index: int replayed_output: bytes | None tainted: bool taint_origin: int | None = None def replay_step(step: TraceStep, live_invoke) -> ReplayResult: if step.determinism is DeterminismClass.PURE: return ReplayResult(step.index, live_invoke(step), tainted=False) if step.determinism is DeterminismClass.RECORDED: if step.raw_output is None: raise ReplayAttributionError( f"step {step.index} classified RECORDED but raw_output missing" ) return ReplayResult(step.index, step.raw_output, tainted=False) if step.determinism is DeterminismClass.SEEDED: if step.replay_token is None: raise ReplayAttributionError( f"step {step.index} classified SEEDED but replay_token missing" ) reconstructed = live_invoke(step) if reconstructed != step.observed_output: # Reconstruction failed: token insufficient. Downgrade to unsafe. return ReplayResult(step.index, reconstructed, tainted=True, taint_origin=step.index) return ReplayResult(step.index, reconstructed, tainted=False) # UNSAFE: re-invocation yields a different sample; cannot attribute. return ReplayResult(step.index, live_invoke(step), tainted=True, taint_origin=step.index) def replay_trace(steps: list[TraceStep], live_invoke) -> list[ReplayResult]: results: list[ReplayResult] = [] active_taint: int | None = None for step in steps: r = replay_step(step, live_invoke) if r.tainted and active_taint is None: active_taint = r.taint_origin if active_taint is not None: r.tainted = True r.taint_origin = active_taint results.append(r) return results def attribute_divergence(results: list[ReplayResult], divergent_step: int) -> str: target = next(r for r in results if r.step_index == divergent_step) if target.tainted: raise ReplayAttributionError( f"divergence at step {divergent_step} is downstream of " f"replay-unsafe step {target.taint_origin}; cannot attribute to " f"agent reasoning. Reconstruct inputs from environment logs or " f"downgrade to behavioral reconstruction." ) return f"step {divergent_step} divergence is attributable: all upstream steps replay-safe"