# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconstruct-a-deterministic-forensic-replay-when-non-deterministic-tool-outputs-and-interleaved-multi-agent-writes-make-the-tra.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconstruct-a-deterministic-forensic-replay-when-non-deterministic-tool-outputs-and-interleaved-multi-agent-writes-make-the-tra.warrant.json
# Markdown SHA-256:   b2145ff2ebaf6eaac3a3b618ef901d6ff7c39084b545beb75eeafa74595a2648
# Code AST SHA-256:   c6a707a89a5298cdf0e40c30960be20e80657e007f004cb4780b508e034f24c5
# Verified Classes:   ReplayAdmissibilityError, DecisionPoint
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ReplayAdmissibilityError(RuntimeError):
    """Raised when a forensic replay cannot be admitted as evidence
    because an upstream decision point lacks a determinism token."""
    pass


class DecisionPoint:
    def __init__(self, point_id, kind, determinism_token, recorded_output):
        self.point_id = point_id
        self.kind = kind  # "llm_sample" | "tool_call" | "message_handoff"
        self.determinism_token = determinism_token  # seed, response hash, or parent_id
        self.recorded_output = recorded_output


def replay_run(decision_points, replayer, incident_point_id):
    """Reconstruct the run up to the incident. Fail closed if any upstream
    decision point is unrecorded — its replayed output is a counterfactual,
    not a reconstruction, and poisons all downstream conclusions."""
    admissible_prefix = []
    for dp in decision_points:
        if dp.determinism_token is None:
            raise ReplayAdmissibilityError(
                f"decision point {dp.point_id} ({dp.kind}) has no determinism "
                f"token; replay past this point is a counterfactual, not a "
                f"reconstruction. Incident at {incident_point_id} cannot be "
                f"attributed from this trace alone."
            )
        replayed = replayer(dp)
        if replayed != dp.recorded_output:
            raise ReplayAdmissibilityError(
                f"decision point {dp.point_id} diverged under replay despite a "
                f"recorded determinism token; replayer or trace is unsound."
            )
        admissible_prefix.append(dp)
        if dp.point_id == incident_point_id:
            return admissible_prefix
    raise ReplayAdmissibilityError(
        f"incident point {incident_point_id} not reached in replay; trace "
        f"ordering may be timestamp-inferred rather than causally recorded."
    )
