# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reconcile-non-deterministic-tool-side-effects-before-replaying-an-otel-trace-graph-for-deterministic-debugging.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reconcile-non-deterministic-tool-side-effects-before-replaying-an-otel-trace-graph-for-deterministic-debugging.warrant.json
# Markdown SHA-256:   a4b00d28a470b06250e8c47800a4da6de82dee5c9c88a1a232d2ef6ec00a660c
# Code AST SHA-256:   3f3495828171cc0ca9faa1577cd6b0e8622d4e13da166f76eb3bf5faa652cc1c
# Verified Classes:   NonReplayableEffect, Span
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class NonReplayableEffect(RuntimeError):
    """Raised when a trace span cannot be safely replayed because its
    side-effect closure is non-empty and unneutralized."""
    pass


class Span:
    def __init__(self, name, pure, mutating, has_fixture,
                 has_idempotency_key, has_snapshot):
        self.name = name
        self.pure = pure
        self.mutating = mutating
        self.has_fixture = has_fixture
        self.has_idempotency_key = has_idempotency_key
        self.has_snapshot = has_snapshot


def assert_effect_closed(spans, replay_mode):
    """Enforce the replay invariant: every span must be effect-closed
    before the trace graph may be replayed deterministically."""
    for s in spans:
        if s.pure:
            continue
        if replay_mode == "fixture" and s.has_fixture:
            continue
        if s.has_idempotency_key:
            continue
        if s.has_snapshot:
            continue
        # Mutating (or unknown) span with no neutralization path.
        raise NonReplayableEffect(
            f"span {s.name!r} is mutating={s.mutating} with no fixture, "
            f"idempotency key, or snapshot; replay would produce a "
            f"divergent trace graph"
        )
    return True


# The failure from the record: append_file passed every local check
# (result_hash matched) but was mutating with no neutralization.
trace = [
    Span("write_file", pure=False, mutating=True,
         has_fixture=False, has_idempotency_key=False, has_snapshot=False),
    Span("append_file", pure=False, mutating=True,
         has_fixture=False, has_idempotency_key=False, has_snapshot=False),
    Span("read_file", pure=True, mutating=False,
         has_fixture=False, has_idempotency_key=False, has_snapshot=False),
]

assert_effect_closed(trace, replay_mode="live")  # -> NonReplayableEffect
