# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-observability-replay-as-a-lossy-projection-rather-than-a-faithful-reconstruction-of-agent-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-observability-replay-as-a-lossy-projection-rather-than-a-faithful-reconstruction-of-agent-execution.warrant.json
# Markdown SHA-256:   b3e2959ae6b98f27f4d1984cff2c9f77093cf858034ff5ce53290c6639bfa4c3
# Code AST SHA-256:   74b8f0acf1d277d1d40579c3e69268a52382a105c2868401a862fb89df690272
# Verified Classes:   ReplayFidelityError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ReplayFidelityError(RuntimeError):
    """Raised when a replay trace underdetermines execution state."""
    pass


def assert_replay_is_faithful(trace: dict) -> None:
    summarized = [
        ev for ev in trace["events"]
        if ev["type"] == "context_edit"
        and ev.get("dropped") is not None
        and not isinstance(ev["dropped"], list)
    ]
    if summarized:
        raise ReplayFidelityError(
            f"context_edit at step {summarized[0]['step']} records dropped={summarized[0]['dropped']!r} "
            "as a count, not identities; replay cannot reconstruct post-compaction context"
        )

    replayed_actions = [
        ev for ev in trace["events"]
        if ev["type"] == "tool_call" and ev.get("mode") == "replay"
    ]
    if replayed_actions:
        raise ReplayFidelityError(
            f"{len(replayed_actions)} tool calls replayed as fixed actions; "
            "reasoning divergence is undetectable and replay is a projection"
        )

    unmarked_stubs = [
        ev for ev in trace["events"]
        if ev["type"] == "tool_result" and not ev.get("assumption_marked")
    ]
    if unmarked_stubs:
        raise ReplayFidelityError(
            f"tool_result at step {unmarked_stubs[0]['step']} lacks assumption marker; "
            "external-state dependency will be misattributed to model nondeterminism"
        )
