src/lightspeed@ops@replay_diagnostics_harness.erl

-module(lightspeed@ops@replay_diagnostics_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/replay_diagnostics_harness.gleam").
-export([run_scenario/1, run_matrix/0, scenario_label/1, pass_fail_label/1, signature/1, scenario/1, deterministic/1, outcomes/1, failed_scenarios/1, nondeterministic_failures/1, report_signature/1, snapshot_signature/0, snapshot_report_markdown/0]).
-export_type([scenario/0, scenario_outcome/0, report/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(" Deterministic replay and time-travel diagnostics harness for M42.\n").

-type scenario() :: certified_failure_replay_path |
    time_indexed_snapshot_inspection |
    fault_and_etl_workflow_integration |
    ci_debug_replay_determinism.

-type scenario_outcome() :: {scenario_outcome,
        scenario(),
        boolean(),
        boolean(),
        binary()}.

-type report() :: {report, list(scenario_outcome()), integer(), integer()}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 328).
-spec count_nondeterministic(list(scenario_outcome())) -> integer().
count_nondeterministic(Outcomes) ->
    case Outcomes of
        [] ->
            0;

        [Outcome | Rest] ->
            case erlang:element(4, Outcome) of
                true ->
                    count_nondeterministic(Rest);

                false ->
                    1 + count_nondeterministic(Rest)
            end
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 317).
-spec count_failed(list(scenario_outcome())) -> integer().
count_failed(Outcomes) ->
    case Outcomes of
        [] ->
            0;

        [Outcome | Rest] ->
            case erlang:element(3, Outcome) of
                true ->
                    count_failed(Rest);

                false ->
                    1 + count_failed(Rest)
            end
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 339).
-spec join_with(binary(), list(binary())) -> binary().
join_with(Separator, Values) ->
    case Values of
        [] ->
            <<""/utf8>>;

        [Value] ->
            Value;

        [Value@1 | Rest] ->
            <<<<Value@1/binary, Separator/binary>>/binary,
                (join_with(Separator, Rest))/binary>>
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 284).
-spec evaluate_ci_debug_replay_determinism() -> {boolean(), binary()}.
evaluate_ci_debug_replay_determinism() ->
    Runtime = lightspeed@ops@replay_diagnostics:runtime_fault_trace(
        <<"m42_ci_runtime"/utf8>>
    ),
    Session = lightspeed@ops@replay_diagnostics:session_fault_trace(
        <<"m42_ci_session"/utf8>>
    ),
    Pipeline = lightspeed@ops@replay_diagnostics:pipeline_etl_trace(
        <<"m42_ci_pipeline"/utf8>>
    ),
    First = join_with(
        <<"|"/utf8>>,
        [lightspeed@ops@replay_diagnostics:trace_signature(Runtime),
            lightspeed@ops@replay_diagnostics:trace_signature(Session),
            lightspeed@ops@replay_diagnostics:trace_signature(Pipeline)]
    ),
    Second = join_with(
        <<"|"/utf8>>,
        [lightspeed@ops@replay_diagnostics:trace_signature(
                lightspeed@ops@replay_diagnostics:runtime_fault_trace(
                    <<"m42_ci_runtime"/utf8>>
                )
            ),
            lightspeed@ops@replay_diagnostics:trace_signature(
                lightspeed@ops@replay_diagnostics:session_fault_trace(
                    <<"m42_ci_session"/utf8>>
                )
            ),
            lightspeed@ops@replay_diagnostics:trace_signature(
                lightspeed@ops@replay_diagnostics:pipeline_etl_trace(
                    <<"m42_ci_pipeline"/utf8>>
                )
            )]
    ),
    Passed = (((First =:= Second) andalso gleam_stdlib:contains_string(
        First,
        <<"trace_id=runtime_fault_trace"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        First,
        <<"trace_id=session_fault_trace"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        First,
        <<"trace_id=pipeline_etl_trace"/utf8>>
    ),
    {Passed, First}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 254).
-spec evaluate_fault_and_etl_workflow_integration() -> {boolean(), binary()}.
evaluate_fault_and_etl_workflow_integration() ->
    Runtime = lightspeed@ops@replay_diagnostics:runtime_fault_trace(
        <<"m42_fault"/utf8>>
    ),
    Session = lightspeed@ops@replay_diagnostics:session_fault_trace(
        <<"m42_fault_session"/utf8>>
    ),
    Etl = lightspeed@ops@replay_diagnostics:pipeline_etl_trace(
        <<"m42_etl"/utf8>>
    ),
    Runtime_fault = lightspeed@ops@replay_diagnostics:investigation_signature(
        fault_workflow,
        Runtime
    ),
    Session_fault = lightspeed@ops@replay_diagnostics:investigation_signature(
        fault_workflow,
        Session
    ),
    Etl_workflow = lightspeed@ops@replay_diagnostics:investigation_signature(
        etl_workflow,
        Etl
    ),
    Passed = ((lightspeed@ops@replay_diagnostics:fault_investigation_ready(
        Runtime
    )
    andalso lightspeed@ops@replay_diagnostics:fault_investigation_ready(Session))
    andalso lightspeed@ops@replay_diagnostics:etl_investigation_ready(Etl))
    andalso gleam_stdlib:contains_string(
        Etl_workflow,
        <<"etl_workflow:ready=true"/utf8>>
    ),
    {Passed,
        <<<<<<<<Runtime_fault/binary, "|"/utf8>>/binary, Session_fault/binary>>/binary,
                "|"/utf8>>/binary,
            Etl_workflow/binary>>}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 209).
-spec evaluate_time_indexed_snapshot_inspection() -> {boolean(), binary()}.
evaluate_time_indexed_snapshot_inspection() ->
    Session = lightspeed@ops@replay_diagnostics:session_fault_trace(
        <<"m42_session"/utf8>>
    ),
    Pipeline = lightspeed@ops@replay_diagnostics:pipeline_etl_trace(
        <<"m42_pipeline"/utf8>>
    ),
    Session_mid = lightspeed@ops@replay_diagnostics:snapshot_at(Session, 3),
    Session_latest = lightspeed@ops@replay_diagnostics:latest_snapshot(Session),
    Pipeline_mid = lightspeed@ops@replay_diagnostics:snapshot_at(Pipeline, 4),
    Pipeline_latest = lightspeed@ops@replay_diagnostics:latest_snapshot(
        Pipeline
    ),
    Session_mid_label = case Session_mid of
        {ok, Snapshot} ->
            lightspeed@ops@replay_diagnostics:snapshot_label(Snapshot);

        {error, Reason} ->
            <<"error:"/utf8, Reason/binary>>
    end,
    Session_latest_label = case Session_latest of
        {some, Snapshot@1} ->
            lightspeed@ops@replay_diagnostics:snapshot_label(Snapshot@1);

        none ->
            <<"none"/utf8>>
    end,
    Pipeline_mid_label = case Pipeline_mid of
        {ok, Snapshot@2} ->
            lightspeed@ops@replay_diagnostics:snapshot_label(Snapshot@2);

        {error, Reason@1} ->
            <<"error:"/utf8, Reason@1/binary>>
    end,
    Pipeline_latest_label = case Pipeline_latest of
        {some, Snapshot@3} ->
            lightspeed@ops@replay_diagnostics:snapshot_label(Snapshot@3);

        none ->
            <<"none"/utf8>>
    end,
    Passed = ((gleam_stdlib:contains_string(
        Session_mid_label,
        <<"flow=session"/utf8>>
    )
    andalso gleam_stdlib:contains_string(
        Session_latest_label,
        <<"index=6"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Pipeline_mid_label,
        <<"flow=pipeline"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Pipeline_latest_label,
        <<"index=7"/utf8>>
    ),
    {Passed,
        <<<<<<<<<<<<<<"session_mid="/utf8, Session_mid_label/binary>>/binary,
                                "|session_latest="/utf8>>/binary,
                            Session_latest_label/binary>>/binary,
                        "|pipeline_mid="/utf8>>/binary,
                    Pipeline_mid_label/binary>>/binary,
                "|pipeline_latest="/utf8>>/binary,
            Pipeline_latest_label/binary>>}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 185).
-spec evaluate_certified_failure_replay_path() -> {boolean(), binary()}.
evaluate_certified_failure_replay_path() ->
    Runtime = lightspeed@ops@replay_diagnostics:runtime_fault_trace(
        <<"m42_runtime"/utf8>>
    ),
    Replay = lightspeed@ops@replay_diagnostics:runtime_fault_trace(
        <<"m42_runtime"/utf8>>
    ),
    First_fault = lightspeed@ops@replay_diagnostics:first_fault_snapshot(
        Runtime
    ),
    First_fault_label = case First_fault of
        none ->
            <<"none"/utf8>>;

        {some, Snapshot} ->
            lightspeed@ops@replay_diagnostics:snapshot_label(Snapshot)
    end,
    Passed = ((lightspeed@ops@replay_diagnostics:trace_signature(Runtime) =:= lightspeed@ops@replay_diagnostics:trace_signature(
        Replay
    ))
    andalso lightspeed@ops@replay_diagnostics:fault_investigation_ready(Runtime))
    andalso gleam_stdlib:contains_string(
        First_fault_label,
        <<"flow=runtime"/utf8>>
    ),
    {Passed,
        <<<<<<"runtime="/utf8,
                    (lightspeed@ops@replay_diagnostics:trace_signature(Runtime))/binary>>/binary,
                "|first_fault="/utf8>>/binary,
            First_fault_label/binary>>}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 175).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        certified_failure_replay_path ->
            evaluate_certified_failure_replay_path();

        time_indexed_snapshot_inspection ->
            evaluate_time_indexed_snapshot_inspection();

        fault_and_etl_workflow_integration ->
            evaluate_fault_and_etl_workflow_integration();

        ci_debug_replay_determinism ->
            evaluate_ci_debug_replay_determinism()
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 57).
?DOC(" Run one M42 scenario twice and require deterministic parity.\n").
-spec run_scenario(scenario()) -> scenario_outcome().
run_scenario(Scenario) ->
    {First_passed, First_signature} = evaluate(Scenario),
    {Second_passed, Second_signature} = evaluate(Scenario),
    Deterministic = (First_passed =:= Second_passed) andalso (First_signature
    =:= Second_signature),
    Passed = (First_passed andalso Second_passed) andalso Deterministic,
    {scenario_outcome, Scenario, Passed, Deterministic, First_signature}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 39).
?DOC(" Run all M42 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [certified_failure_replay_path,
            time_indexed_snapshot_inspection,
            fault_and_etl_workflow_integration,
            ci_debug_replay_determinism],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 73).
?DOC(" Scenario label.\n").
-spec scenario_label(scenario()) -> binary().
scenario_label(Scenario) ->
    case Scenario of
        certified_failure_replay_path ->
            <<"certified_failure_replay_path"/utf8>>;

        time_indexed_snapshot_inspection ->
            <<"time_indexed_snapshot_inspection"/utf8>>;

        fault_and_etl_workflow_integration ->
            <<"fault_and_etl_workflow_integration"/utf8>>;

        ci_debug_replay_determinism ->
            <<"ci_debug_replay_determinism"/utf8>>
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 83).
?DOC(" Stable pass/fail label.\n").
-spec pass_fail_label(scenario_outcome()) -> binary().
pass_fail_label(Outcome) ->
    case erlang:element(3, Outcome) of
        true ->
            <<"pass"/utf8>>;

        false ->
            <<"fail"/utf8>>
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 91).
?DOC(" Scenario signature accessor.\n").
-spec signature(scenario_outcome()) -> binary().
signature(Outcome) ->
    erlang:element(5, Outcome).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 96).
?DOC(" Scenario accessor.\n").
-spec scenario(scenario_outcome()) -> scenario().
scenario(Outcome) ->
    erlang:element(2, Outcome).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 101).
?DOC(" Determinism accessor.\n").
-spec deterministic(scenario_outcome()) -> boolean().
deterministic(Outcome) ->
    erlang:element(4, Outcome).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 106).
?DOC(" Report outcomes accessor.\n").
-spec outcomes(report()) -> list(scenario_outcome()).
outcomes(Report) ->
    erlang:element(2, Report).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 111).
?DOC(" Failed scenario count.\n").
-spec failed_scenarios(report()) -> integer().
failed_scenarios(Report) ->
    erlang:element(3, Report).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 116).
?DOC(" Nondeterministic scenario count.\n").
-spec nondeterministic_failures(report()) -> integer().
nondeterministic_failures(Report) ->
    erlang:element(4, Report).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 347).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

        false ->
            <<"false"/utf8>>
    end.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 121).
?DOC(" Stable report signature.\n").
-spec report_signature(report()) -> binary().
report_signature(Report) ->
    Entries = gleam@list:map(
        erlang:element(2, Report),
        fun(Outcome) ->
            <<<<<<<<<<<<(scenario_label(erlang:element(2, Outcome)))/binary,
                                    "="/utf8>>/binary,
                                (pass_fail_label(Outcome))/binary>>/binary,
                            ":deterministic="/utf8>>/binary,
                        (bool_label(erlang:element(4, Outcome)))/binary>>/binary,
                    ":"/utf8>>/binary,
                (erlang:element(5, Outcome))/binary>>
        end
    ),
    join_with(<<";"/utf8>>, Entries).

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 137).
?DOC(" Deterministic snapshot signature for M42 fixture drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    <<<<<<"m42.snapshot.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (report_signature(run_matrix()))/binary>>.

-file("src/lightspeed/ops/replay_diagnostics_harness.gleam", 145).
?DOC(" Deterministic markdown report for M42 fixture scripts.\n").
-spec snapshot_report_markdown() -> binary().
snapshot_report_markdown() ->
    Report = run_matrix(),
    Failed = failed_scenarios(Report),
    Nondeterministic = nondeterministic_failures(Report),
    Status = case (Failed =:= 0) andalso (Nondeterministic =:= 0) of
        true ->
            <<"OK"/utf8>>;

        false ->
            <<"FAIL"/utf8>>
    end,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Replay Diagnostics Fixture Report\n\n"/utf8,
                                                                            "snapshot_version: "/utf8>>/binary,
                                                                        (erlang:integer_to_binary(
                                                                            1
                                                                        ))/binary>>/binary,
                                                                    "\n"/utf8>>/binary,
                                                                "status: "/utf8>>/binary,
                                                            Status/binary>>/binary,
                                                        "\n"/utf8>>/binary,
                                                    "failed_scenarios: "/utf8>>/binary,
                                                (erlang:integer_to_binary(
                                                    Failed
                                                ))/binary>>/binary,
                                            "\n"/utf8>>/binary,
                                        "nondeterministic_failures: "/utf8>>/binary,
                                    (erlang:integer_to_binary(Nondeterministic))/binary>>/binary,
                                "\n\n"/utf8>>/binary,
                            "snapshot_signature: "/utf8>>/binary,
                        (snapshot_signature())/binary>>/binary,
                    "\n\n"/utf8>>/binary,
                "report_signature: "/utf8>>/binary,
            (report_signature(Report))/binary>>/binary,
        "\n"/utf8>>.