src/lightspeed@ops@async_engine_harness.erl

-module(lightspeed@ops@async_engine_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/async_engine_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 async data engine and enterprise driver harness for M48.\n").

-type scenario() :: async_driver_contracts_consistency |
    enterprise_driver_conformance |
    timeout_retry_telemetry_parity |
    heavy_data_mixed_latency_benchmarks |
    capability_matrix_determinism.

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

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

-file("src/lightspeed/ops/async_engine_harness.gleam", 275).
-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/async_engine_harness.gleam", 264).
-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/async_engine_harness.gleam", 239).
-spec evaluate_capability_matrix_determinism() -> {boolean(), binary()}.
evaluate_capability_matrix_determinism() ->
    First = lightspeed@integration@async_driver:capability_matrix_signature(),
    Second = lightspeed@integration@async_driver:capability_matrix_signature(),
    Passed = First =:= Second,
    {Passed, First}.

-file("src/lightspeed/ops/async_engine_harness.gleam", 293).
-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/async_engine_harness.gleam", 225).
-spec evaluate_heavy_data_mixed_latency_benchmarks() -> {boolean(), binary()}.
evaluate_heavy_data_mixed_latency_benchmarks() ->
    Benchmarks = lightspeed@integration@async_driver:run_enterprise_benchmarks(),
    Budgets = lightspeed@integration@async_driver:default_budgets(),
    Results = lightspeed@integration@async_driver:evaluate_budgets(
        Benchmarks,
        Budgets
    ),
    Passed = lightspeed@integration@async_driver:budget_failures(Results) =:= 0,
    Signature = <<<<<<"benchmarks="/utf8,
                (join_with(
                    <<";"/utf8>>,
                    gleam@list:map(
                        Benchmarks,
                        fun lightspeed@integration@async_driver:benchmark_signature/1
                    )
                ))/binary>>/binary,
            "|budget_results="/utf8>>/binary,
        (join_with(
            <<";"/utf8>>,
            gleam@list:map(
                Results,
                fun lightspeed@integration@async_driver:budget_result_signature/1
            )
        ))/binary>>,
    {Passed, Signature}.

-file("src/lightspeed/ops/async_engine_harness.gleam", 247).
-spec telemetry_signature(
    lightspeed@integration@async_driver:telemetry_profile()
) -> binary().
telemetry_signature(Profile) ->
    <<<<<<<<<<<<(erlang:element(2, Profile))/binary, ","/utf8>>/binary,
                        (erlang:element(3, Profile))/binary>>/binary,
                    ","/utf8>>/binary,
                (erlang:element(4, Profile))/binary>>/binary,
            ","/utf8>>/binary,
        (erlang:element(5, Profile))/binary>>.

-file("src/lightspeed/ops/async_engine_harness.gleam", 209).
-spec evaluate_timeout_retry_telemetry_parity() -> {boolean(), binary()}.
evaluate_timeout_retry_telemetry_parity() ->
    Drivers = lightspeed@integration@async_driver:supported_drivers(),
    Passed = lightspeed@integration@async_driver:timeout_retry_telemetry_parity(
        
    ),
    Signature = join_with(
        <<";"/utf8>>,
        gleam@list:map(
            Drivers,
            fun(Driver) ->
                <<<<(lightspeed@integration@async_driver:name(Driver))/binary,
                        "|telemetry="/utf8>>/binary,
                    (telemetry_signature(
                        lightspeed@integration@async_driver:telemetry(Driver)
                    ))/binary>>
            end
        )
    ),
    {Passed, Signature}.

-file("src/lightspeed/ops/async_engine_harness.gleam", 257).
-spec all_drivers_valid(
    list(lightspeed@integration@async_driver:driver_contract())
) -> boolean().
all_drivers_valid(Drivers) ->
    case Drivers of
        [] ->
            true;

        [Driver | Rest] ->
            lightspeed@integration@async_driver:valid(Driver) andalso all_drivers_valid(
                Rest
            )
    end.

-file("src/lightspeed/ops/async_engine_harness.gleam", 200).
-spec evaluate_enterprise_driver_conformance() -> {boolean(), binary()}.
evaluate_enterprise_driver_conformance() ->
    Drivers = lightspeed@integration@async_driver:supported_drivers(),
    Passed = all_drivers_valid(Drivers),
    Signature = join_with(
        <<";"/utf8>>,
        gleam@list:map(
            Drivers,
            fun lightspeed@integration@async_driver:driver_signature/1
        )
    ),
    {Passed, Signature}.

-file("src/lightspeed/ops/async_engine_harness.gleam", 188).
-spec evaluate_async_driver_contracts_consistency() -> {boolean(), binary()}.
evaluate_async_driver_contracts_consistency() ->
    Drivers = lightspeed@integration@async_driver:supported_drivers(),
    Passed = lightspeed@integration@async_driver:contracts_consistent() andalso (erlang:length(
        Drivers
    )
    =:= 4),
    Signature = <<<<<<"count="/utf8,
                (erlang:integer_to_binary(erlang:length(Drivers)))/binary>>/binary,
            "|matrix="/utf8>>/binary,
        (lightspeed@integration@async_driver:capability_matrix_signature())/binary>>,
    {Passed, Signature}.

-file("src/lightspeed/ops/async_engine_harness.gleam", 176).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        async_driver_contracts_consistency ->
            evaluate_async_driver_contracts_consistency();

        enterprise_driver_conformance ->
            evaluate_enterprise_driver_conformance();

        timeout_retry_telemetry_parity ->
            evaluate_timeout_retry_telemetry_parity();

        heavy_data_mixed_latency_benchmarks ->
            evaluate_heavy_data_mixed_latency_benchmarks();

        capability_matrix_determinism ->
            evaluate_capability_matrix_determinism()
    end.

-file("src/lightspeed/ops/async_engine_harness.gleam", 57).
?DOC(" Run one M48 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/async_engine_harness.gleam", 38).
?DOC(" Run all M48 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [async_driver_contracts_consistency,
            enterprise_driver_conformance,
            timeout_retry_telemetry_parity,
            heavy_data_mixed_latency_benchmarks,
            capability_matrix_determinism],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        enterprise_driver_conformance ->
            <<"enterprise_driver_conformance"/utf8>>;

        timeout_retry_telemetry_parity ->
            <<"timeout_retry_telemetry_parity"/utf8>>;

        heavy_data_mixed_latency_benchmarks ->
            <<"heavy_data_mixed_latency_benchmarks"/utf8>>;

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

-file("src/lightspeed/ops/async_engine_harness.gleam", 84).
?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/async_engine_harness.gleam", 92).
?DOC(" Scenario signature accessor.\n").
-spec signature(scenario_outcome()) -> binary().
signature(Outcome) ->
    erlang:element(5, Outcome).

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

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

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

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

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

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

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

-file("src/lightspeed/ops/async_engine_harness.gleam", 122).
?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/async_engine_harness.gleam", 138).
?DOC(" Deterministic snapshot signature for M48 fixture drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    <<<<<<"m48.snapshot.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
            "|"/utf8>>/binary,
        (report_signature(run_matrix()))/binary>>.

-file("src/lightspeed/ops/async_engine_harness.gleam", 146).
?DOC(" Deterministic markdown report for M48 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Async Engine 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>>.