# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-replay-a-memory-mutation-whose-causal-antecedent-it-can-no-longer-see.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-replay-a-memory-mutation-whose-causal-antecedent-it-can-no-longer-see.warrant.json
# Markdown SHA-256:   6f0ff0c420039dced68553509916230e58410364be2cad807ea392f970c6e2fe
# Code AST SHA-256:   e892793c217a6ef47856c8621ce892a4f9c034b18277c57f1944fe032c50d74e
# Verified Classes:   UnresolvableAntecedentError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UnresolvableAntecedentError(RuntimeError):
    """Raised when causal replay cannot satisfy an event's antecedent set from the log."""
    pass


def replay_event(event, reconstructed_state, log_index, compaction_boundaries):
    # Hard precondition: antecedent closure. No best-effort resolution.
    unsatisfied = []
    for dep_id in event.get("depends_on", []):
        if dep_id in log_index:
            if dep_id not in reconstructed_state["applied"]:
                unsatisfied.append(dep_id)
        elif dep_id not in compaction_boundaries:
            unsatisfied.append(dep_id)

    if unsatisfied:
        raise UnresolvableAntecedentError(
            f"event {event['id']} ({event['op']}) has unsatisfiable antecedents "
            f"{unsatisfied}: not in log and not a declared compaction boundary. "
            f"Refusing to replay — a no-op resolution would invent causal history."
        )

    # Eviction must carry a companion pointer; absence is a graph hole.
    if event["op"] == "evict" and "preserved_via" not in event:
        raise UnresolvableAntecedentError(
            f"evict event {event['id']} removed range {event.get('range')} "
            f"without a preserved_via companion; replay closure is broken."
        )

    reconstructed_state["applied"].add(event["id"])
    reconstructed_state["context"] = apply_delta(reconstructed_state["context"], event)
    return reconstructed_state
