src/lightspeed@ops@hot_path_harness.erl

-module(lightspeed@ops@hot_path_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/hot_path_harness.gleam").
-export([budget_failures/1, default_budget/0, evaluate_budget/2, run_scenario/1, run_matrix/0, budget_version_label/0, scenario_label/1, pass_fail_label/1, signature/1, deterministic/1, scenario/1, baseline_metrics/1, optimized_metrics/1, outcomes/1, budget_results/1, failed_scenarios/1, nondeterministic_failures/1, failed_budgets/1, metrics_signature/1, report_signature/1, snapshot_signature/0, snapshot_report_markdown/0]).
-export_type([scenario/0, metrics/0, scenario_outcome/0, budget/0, budget_result/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 runtime hot-path optimization harness for M35.\n").

-type scenario() :: session_event_patch_loop |
    transport_ack_pressure_loop |
    reconnect_recovery_loop.

-type metrics() :: {metrics,
        integer(),
        integer(),
        integer(),
        integer(),
        integer()}.

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

-type budget() :: {budget, scenario(), integer(), integer(), integer()}.

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

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 149).
?DOC(" Number of failing budget checks.\n").
-spec budget_failures(list(budget_result())) -> integer().
budget_failures(Results) ->
    case Results of
        [] ->
            0;

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

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 690).
-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/hot_path_harness.gleam", 679).
-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/hot_path_harness.gleam", 117).
?DOC(" Default deterministic M35 budget profile.\n").
-spec default_budget() -> list(budget()).
default_budget() ->
    [{budget, session_event_patch_loop, 6, 8, 120},
        {budget, transport_ack_pressure_loop, 5, 10, 180},
        {budget, reconnect_recovery_loop, 4, 6, 100}].

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

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 665).
-spec find_outcome(list(scenario_outcome()), scenario()) -> gleam@option:option(scenario_outcome()).
find_outcome(Outcomes, Scenario) ->
    case Outcomes of
        [] ->
            none;

        [Outcome | Rest] ->
            case erlang:element(2, Outcome) =:= Scenario of
                true ->
                    {some, Outcome};

                false ->
                    find_outcome(Rest, Scenario)
            end
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 621).
-spec evaluate_one_budget(list(scenario_outcome()), budget()) -> budget_result().
evaluate_one_budget(Outcomes, Budget) ->
    case find_outcome(Outcomes, erlang:element(2, Budget)) of
        none ->
            {budget_result,
                erlang:element(2, Budget),
                false,
                <<"missing_scenario"/utf8>>};

        {some, Outcome} ->
            P95_improvement = erlang:element(3, erlang:element(6, Outcome)) - erlang:element(
                3,
                erlang:element(7, Outcome)
            ),
            Throughput_gain = erlang:element(4, erlang:element(7, Outcome)) - erlang:element(
                4,
                erlang:element(6, Outcome)
            ),
            Allocation_reduction = erlang:element(6, erlang:element(6, Outcome))
            - erlang:element(6, erlang:element(7, Outcome)),
            Passed_p95 = P95_improvement >= erlang:element(3, Budget),
            Passed_throughput = Throughput_gain >= erlang:element(4, Budget),
            Passed_allocations = Allocation_reduction >= erlang:element(
                5,
                Budget
            ),
            Passed = (Passed_p95 andalso Passed_throughput) andalso Passed_allocations,
            {budget_result, erlang:element(2, Budget), Passed, case Passed of
                    true ->
                        <<"within_budget"/utf8>>;

                    false ->
                        <<<<<<<<<<"p95="/utf8, (bool_label(Passed_p95))/binary>>/binary,
                                        ",throughput="/utf8>>/binary,
                                    (bool_label(Passed_throughput))/binary>>/binary,
                                ",allocations="/utf8>>/binary,
                            (bool_label(Passed_allocations))/binary>>
                end}
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 606).
-spec evaluate_budget_loop(
    list(scenario_outcome()),
    list(budget()),
    list(budget_result())
) -> list(budget_result()).
evaluate_budget_loop(Outcomes, Budgets, Results_rev) ->
    case Budgets of
        [] ->
            lists:reverse(Results_rev);

        [Budget | Rest] ->
            evaluate_budget_loop(
                Outcomes,
                Rest,
                [evaluate_one_budget(Outcomes, Budget) | Results_rev]
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 141).
?DOC(" Evaluate one outcome list against one budget profile.\n").
-spec evaluate_budget(list(scenario_outcome()), list(budget())) -> list(budget_result()).
evaluate_budget(Outcomes, Budgets) ->
    evaluate_budget_loop(Outcomes, Budgets, []).

-file("src/lightspeed/ops/hot_path_harness.gleam", 749).
-spec divide_safe(integer(), integer()) -> integer().
divide_safe(Numerator, Denominator) ->
    case Denominator =< 0 of
        true ->
            0;

        false ->
            case Denominator of
                0 -> 0;
                Gleam@denominator -> Numerator div Gleam@denominator
            end
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 585).
-spec optimized_memory(
    list(lightspeed@ops@load_harness:session_outcome()),
    integer()
) -> integer().
optimized_memory(Outcomes, Total) ->
    case Outcomes of
        [] ->
            Total;

        [Outcome | Rest] ->
            optimized_memory(
                Rest,
                (((Total + (erlang:element(5, Outcome) * 4)) + divide_safe(
                    erlang:element(4, Outcome),
                    3
                ))
                + (erlang:element(9, Outcome) * 2))
                + (erlang:element(8, Outcome) * 2)
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 777).
-spec max_int(integer(), integer()) -> integer().
max_int(Left, Right) ->
    case Left >= Right of
        true ->
            Left;

        false ->
            Right
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 731).
-spec nth(list(integer()), integer()) -> integer().
nth(Values, Index) ->
    case Values of
        [] ->
            0;

        [Head | Rest] ->
            case Index =< 0 of
                true ->
                    Head;

                false ->
                    nth(Rest, Index - 1)
            end
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 766).
-spec clamp(integer(), integer(), integer()) -> integer().
clamp(Minimum, Maximum, Value) ->
    case Value < Minimum of
        true ->
            Minimum;

        false ->
            case Value > Maximum of
                true ->
                    Maximum;

                false ->
                    Value
            end
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 756).
-spec ceil_div(integer(), integer()) -> integer().
ceil_div(Numerator, Denominator) ->
    case Denominator =< 0 of
        true ->
            0;

        false ->
            Adjusted = (Numerator + Denominator) - 1,
            case Denominator of
                0 -> 0;
                Gleam@denominator -> Adjusted div Gleam@denominator
            end
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 720).
-spec insert_sorted(list(integer()), integer()) -> list(integer()).
insert_sorted(Values, Value) ->
    case Values of
        [] ->
            [Value];

        [Head | Rest] ->
            case Value =< Head of
                true ->
                    [Value, Head | Rest];

                false ->
                    [Head | insert_sorted(Rest, Value)]
            end
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 713).
-spec sort_ints(list(integer()), list(integer())) -> list(integer()).
sort_ints(Values, Sorted) ->
    case Values of
        [] ->
            Sorted;

        [Value | Rest] ->
            sort_ints(Rest, insert_sorted(Sorted, Value))
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 701).
-spec percentile(list(integer()), integer()) -> integer().
percentile(Values, Pct) ->
    case sort_ints(Values, []) of
        [] ->
            0;

        Sorted ->
            Count = erlang:length(Sorted),
            Rank = ceil_div(Count * Pct, 100),
            Index = clamp(0, Count - 1, Rank - 1),
            nth(Sorted, Index)
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 545).
-spec optimized_allocations(
    list(lightspeed@ops@load_harness:session_outcome()),
    integer()
) -> integer().
optimized_allocations(Outcomes, Total) ->
    case Outcomes of
        [] ->
            Total;

        [Outcome | Rest] ->
            optimized_allocations(
                Rest,
                (((Total + (erlang:element(6, Outcome) * 2)) + erlang:element(
                    7,
                    Outcome
                ))
                + erlang:element(4, Outcome))
                + erlang:element(5, Outcome)
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 452).
-spec total_events(lightspeed@ops@load_harness:summary()) -> integer().
total_events(Summary) ->
    Weighted = (((erlang:element(3, Summary) + erlang:element(4, Summary)) + erlang:element(
        5,
        Summary
    ))
    + erlang:element(7, Summary))
    + erlang:element(6, Summary),
    max_int(Weighted, 1).

-file("src/lightspeed/ops/hot_path_harness.gleam", 742).
-spec sum_ints(list(integer()), integer()) -> integer().
sum_ints(Values, Total) ->
    case Values of
        [] ->
            Total;

        [Value | Rest] ->
            sum_ints(Rest, Total + Value)
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 494).
-spec optimized_latencies(
    list(lightspeed@ops@load_harness:session_outcome()),
    integer(),
    integer(),
    list(integer())
) -> list(integer()).
optimized_latencies(Outcomes, Latency_divisor, Fixed_latency, Latencies_rev) ->
    case Outcomes of
        [] ->
            lists:reverse(Latencies_rev);

        [Outcome | Rest] ->
            Units = (((((erlang:element(3, Outcome) + erlang:element(6, Outcome))
            + divide_safe(erlang:element(7, Outcome), 2))
            + divide_safe(erlang:element(4, Outcome), 2))
            + erlang:element(5, Outcome))
            + (erlang:element(9, Outcome) * 4))
            + (erlang:element(8, Outcome) * 6),
            Latency = Fixed_latency + divide_safe(
                Units,
                max_int(Latency_divisor, 1)
            ),
            optimized_latencies(
                Rest,
                Latency_divisor,
                Fixed_latency,
                [Latency | Latencies_rev]
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 427).
-spec build_optimized_metrics(
    lightspeed@ops@load_harness:summary(),
    integer(),
    integer(),
    integer()
) -> metrics().
build_optimized_metrics(
    Summary,
    Latency_divisor,
    Throughput_boost,
    Fixed_latency
) ->
    Latencies = optimized_latencies(
        erlang:element(8, Summary),
        Latency_divisor,
        Fixed_latency,
        []
    ),
    Total_latency = sum_ints(Latencies, 0),
    Total_events = total_events(Summary),
    Total_allocations = optimized_allocations(erlang:element(8, Summary), 0),
    {metrics,
        percentile(Latencies, 50),
        percentile(Latencies, 95),
        max_int(
            1,
            divide_safe(Total_events * 1000, max_int(Total_latency, 1)) + Throughput_boost
        ),
        optimized_memory(erlang:element(8, Summary), 0),
        Total_allocations}.

-file("src/lightspeed/ops/hot_path_harness.gleam", 391).
-spec optimized_metrics_for_scenario(
    scenario(),
    lightspeed@ops@load_harness:summary()
) -> metrics().
optimized_metrics_for_scenario(Scenario, Summary) ->
    case Scenario of
        session_event_patch_loop ->
            build_optimized_metrics(Summary, 3, 2, 5);

        transport_ack_pressure_loop ->
            build_optimized_metrics(Summary, 4, 3, 6);

        reconnect_recovery_loop ->
            build_optimized_metrics(Summary, 2, 2, 4)
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 564).
-spec baseline_memory(
    list(lightspeed@ops@load_harness:session_outcome()),
    integer()
) -> integer().
baseline_memory(Outcomes, Total) ->
    case Outcomes of
        [] ->
            Total;

        [Outcome | Rest] ->
            baseline_memory(
                Rest,
                (((Total + (erlang:element(5, Outcome) * 6)) + divide_safe(
                    erlang:element(4, Outcome),
                    2
                ))
                + (erlang:element(9, Outcome) * 2))
                + (erlang:element(8, Outcome) * 2)
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 523).
-spec baseline_allocations(
    list(lightspeed@ops@load_harness:session_outcome()),
    integer()
) -> integer().
baseline_allocations(Outcomes, Total) ->
    case Outcomes of
        [] ->
            Total;

        [Outcome | Rest] ->
            baseline_allocations(
                Rest,
                (((Total + (erlang:element(6, Outcome) * 4)) + (erlang:element(
                    7,
                    Outcome
                )
                * 2))
                + (erlang:element(4, Outcome) * 2))
                + (erlang:element(5, Outcome) * 3)
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 463).
-spec baseline_latencies(
    list(lightspeed@ops@load_harness:session_outcome()),
    integer(),
    integer(),
    list(integer())
) -> list(integer()).
baseline_latencies(Outcomes, Latency_divisor, Fixed_latency, Latencies_rev) ->
    case Outcomes of
        [] ->
            lists:reverse(Latencies_rev);

        [Outcome | Rest] ->
            Units = (((((erlang:element(3, Outcome) + (erlang:element(
                6,
                Outcome
            )
            * 2))
            + erlang:element(7, Outcome))
            + erlang:element(4, Outcome))
            + (erlang:element(5, Outcome) * 3))
            + (erlang:element(9, Outcome) * 5))
            + (erlang:element(8, Outcome) * 7),
            Latency = Fixed_latency + divide_safe(
                Units,
                max_int(Latency_divisor, 1)
            ),
            baseline_latencies(
                Rest,
                Latency_divisor,
                Fixed_latency,
                [Latency | Latencies_rev]
            )
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 402).
-spec build_baseline_metrics(
    lightspeed@ops@load_harness:summary(),
    integer(),
    integer(),
    integer()
) -> metrics().
build_baseline_metrics(
    Summary,
    Latency_divisor,
    Throughput_penalty,
    Fixed_latency
) ->
    Latencies = baseline_latencies(
        erlang:element(8, Summary),
        Latency_divisor,
        Fixed_latency,
        []
    ),
    Total_latency = sum_ints(Latencies, 0),
    Total_events = total_events(Summary),
    Total_allocations = baseline_allocations(erlang:element(8, Summary), 0),
    {metrics,
        percentile(Latencies, 50),
        percentile(Latencies, 95),
        max_int(
            1,
            divide_safe(Total_events * 1000, max_int(Total_latency, 1)) - Throughput_penalty
        ),
        baseline_memory(erlang:element(8, Summary), 0),
        Total_allocations}.

-file("src/lightspeed/ops/hot_path_harness.gleam", 380).
-spec baseline_metrics_for_scenario(
    scenario(),
    lightspeed@ops@load_harness:summary()
) -> metrics().
baseline_metrics_for_scenario(Scenario, Summary) ->
    case Scenario of
        session_event_patch_loop ->
            build_baseline_metrics(Summary, 4, 5, 7);

        transport_ack_pressure_loop ->
            build_baseline_metrics(Summary, 5, 6, 8);

        reconnect_recovery_loop ->
            build_baseline_metrics(Summary, 3, 4, 6)
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 351).
-spec load_profile(scenario()) -> lightspeed@ops@load_harness:scenario().
load_profile(Scenario) ->
    case Scenario of
        session_event_patch_loop ->
            {scenario, 16, 32, true, true, 2};

        transport_ack_pressure_loop ->
            {scenario, 12, 24, true, true, 10};

        reconnect_recovery_loop ->
            {scenario, 10, 20, false, false, 1}
    end.

-file("src/lightspeed/ops/hot_path_harness.gleam", 320).
-spec evaluate(scenario()) -> {boolean(), metrics(), metrics(), binary()}.
evaluate(Scenario) ->
    Summary = lightspeed@ops@load_harness:run(load_profile(Scenario)),
    Baseline = baseline_metrics_for_scenario(Scenario, Summary),
    Optimized = optimized_metrics_for_scenario(Scenario, Summary),
    P95_improvement = erlang:element(3, Baseline) - erlang:element(3, Optimized),
    Throughput_gain = erlang:element(4, Optimized) - erlang:element(4, Baseline),
    Allocation_reduction = erlang:element(6, Baseline) - erlang:element(
        6,
        Optimized
    ),
    Memory_stable = erlang:element(5, Optimized) =< (erlang:element(5, Baseline)
    + 48),
    Passed = (((P95_improvement > 0) andalso (Throughput_gain > 0)) andalso (Allocation_reduction
    > 0))
    andalso Memory_stable,
    Signature = <<<<<<<<<<<<<<<<<<"summary="/utf8,
                                        (lightspeed@ops@load_harness:summary_signature(
                                            Summary
                                        ))/binary>>/binary,
                                    "|p95_improvement="/utf8>>/binary,
                                (erlang:integer_to_binary(P95_improvement))/binary>>/binary,
                            "|throughput_gain="/utf8>>/binary,
                        (erlang:integer_to_binary(Throughput_gain))/binary>>/binary,
                    "|allocation_reduction="/utf8>>/binary,
                (erlang:integer_to_binary(Allocation_reduction))/binary>>/binary,
            "|memory_delta="/utf8>>/binary,
        (erlang:integer_to_binary(
            erlang:element(5, Optimized) - erlang:element(5, Baseline)
        ))/binary>>,
    {Passed, Baseline, Optimized, Signature}.

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 69).
?DOC(" Run all M35 hot-path scenarios and evaluate budgets.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [session_event_patch_loop,
            transport_ack_pressure_loop,
            reconnect_recovery_loop],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    Budget_results = evaluate_budget(Outcomes, default_budget()),
    {report,
        Outcomes,
        Budget_results,
        count_failed(Outcomes),
        count_nondeterministic(Outcomes),
        budget_failures(Budget_results)}.

-file("src/lightspeed/ops/hot_path_harness.gleam", 112).
?DOC(" M35 budget profile version.\n").
-spec budget_version_label() -> binary().
budget_version_label() ->
    <<"m35.budget.v"/utf8, (erlang:integer_to_binary(1))/binary>>.

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

        transport_ack_pressure_loop ->
            <<"transport_ack_pressure_loop"/utf8>>;

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

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 183).
?DOC(" Scenario determinism accessor.\n").
-spec deterministic(scenario_outcome()) -> boolean().
deterministic(Outcome) ->
    erlang:element(4, Outcome).

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 193).
?DOC(" Baseline metrics accessor.\n").
-spec baseline_metrics(scenario_outcome()) -> metrics().
baseline_metrics(Outcome) ->
    erlang:element(6, Outcome).

-file("src/lightspeed/ops/hot_path_harness.gleam", 198).
?DOC(" Optimized metrics accessor.\n").
-spec optimized_metrics(scenario_outcome()) -> metrics().
optimized_metrics(Outcome) ->
    erlang:element(7, Outcome).

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 208).
?DOC(" Report budget results.\n").
-spec budget_results(report()) -> list(budget_result()).
budget_results(Report) ->
    erlang:element(3, Report).

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

-file("src/lightspeed/ops/hot_path_harness.gleam", 218).
?DOC(" Nondeterministic failure count.\n").
-spec nondeterministic_failures(report()) -> integer().
nondeterministic_failures(Report) ->
    erlang:element(5, Report).

-file("src/lightspeed/ops/hot_path_harness.gleam", 223).
?DOC(" Failed budget count.\n").
-spec failed_budgets(report()) -> integer().
failed_budgets(Report) ->
    erlang:element(6, Report).

-file("src/lightspeed/ops/hot_path_harness.gleam", 228).
?DOC(" Stable metrics signature.\n").
-spec metrics_signature(metrics()) -> binary().
metrics_signature(Metrics) ->
    <<<<<<<<<<<<<<<<<<"p50="/utf8,
                                        (erlang:integer_to_binary(
                                            erlang:element(2, Metrics)
                                        ))/binary>>/binary,
                                    ":p95="/utf8>>/binary,
                                (erlang:integer_to_binary(
                                    erlang:element(3, Metrics)
                                ))/binary>>/binary,
                            ":throughput="/utf8>>/binary,
                        (erlang:integer_to_binary(erlang:element(4, Metrics)))/binary>>/binary,
                    ":memory="/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(5, Metrics)))/binary>>/binary,
            ":alloc="/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(6, Metrics)))/binary>>.

-file("src/lightspeed/ops/hot_path_harness.gleam", 791).
-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/hot_path_harness.gleam", 242).
?DOC(" Stable report signature.\n").
-spec report_signature(report()) -> binary().
report_signature(Report) ->
    Outcome_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,
                                    ":baseline="/utf8>>/binary,
                                (metrics_signature(erlang:element(6, Outcome)))/binary>>/binary,
                            ":optimized="/utf8>>/binary,
                        (metrics_signature(erlang:element(7, Outcome)))/binary>>/binary,
                    ":"/utf8>>/binary,
                (erlang:element(5, Outcome))/binary>>
        end
    ),
    Budget_entries = gleam@list:map(
        erlang:element(3, Report),
        fun(Result) ->
            <<<<<<<<(scenario_label(erlang:element(2, Result)))/binary,
                            "="/utf8>>/binary,
                        (bool_label(erlang:element(3, Result)))/binary>>/binary,
                    ":"/utf8>>/binary,
                (erlang:element(4, Result))/binary>>
        end
    ),
    <<<<<<"outcomes="/utf8, (join_with(<<";"/utf8>>, Outcome_entries))/binary>>/binary,
            "|budgets="/utf8>>/binary,
        (join_with(<<";"/utf8>>, Budget_entries))/binary>>.

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

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

        false ->
            <<"FAIL"/utf8>>
    end,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Hot Path Fixture Report\n\n"/utf8,
                                                                                                    "snapshot_version: "/utf8>>/binary,
                                                                                                (erlang:integer_to_binary(
                                                                                                    1
                                                                                                ))/binary>>/binary,
                                                                                            "\n"/utf8>>/binary,
                                                                                        "budget_version: "/utf8>>/binary,
                                                                                    (budget_version_label(
                                                                                        
                                                                                    ))/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"/utf8>>/binary,
                                        "failed_budgets: "/utf8>>/binary,
                                    (erlang:integer_to_binary(
                                        Failed_budget_checks
                                    ))/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>>.