src/lightspeed@ops@large_data_harness.erl

-module(lightspeed@ops@large_data_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/large_data_harness.gleam").
-export([benchmark_signature/1, budget_version_label/0, budget_failures/1, evaluate_budget/2, default_budget/0, run_heavy_benchmarks/0, run_scenario/1, run_matrix/0, scenario_label/1, pass_fail_label/1, signature/1, deterministic/1, scenario/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, benchmark/0, budget/0, budget_result/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 large-data and async-runtime expansion harness for M28.\n").

-type scenario() :: large_list_windowed_incremental |
    large_grid_windowed_incremental |
    large_chart_windowed_incremental |
    async_backpressure_recovery |
    heavy_budget_enforcement.

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

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

-type benchmark() :: {benchmark,
        binary(),
        integer(),
        integer(),
        integer(),
        integer(),
        integer(),
        integer(),
        integer()}.

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

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

-file("src/lightspeed/ops/large_data_harness.gleam", 708).
-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/large_data_harness.gleam", 697).
-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/large_data_harness.gleam", 653).
-spec pass_fail_label_from_bool(boolean()) -> binary().
pass_fail_label_from_bool(Value) ->
    case Value of
        true ->
            <<"pass"/utf8>>;

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

-file("src/lightspeed/ops/large_data_harness.gleam", 645).
-spec budget_result_signature(budget_result()) -> binary().
budget_result_signature(Result) ->
    <<<<<<<<(erlang:element(2, Result))/binary, ":"/utf8>>/binary,
                (pass_fail_label_from_bool(erlang:element(3, Result)))/binary>>/binary,
            ":"/utf8>>/binary,
        (erlang:element(4, Result))/binary>>.

-file("src/lightspeed/ops/large_data_harness.gleam", 740).
-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/large_data_harness.gleam", 243).
?DOC(" Stable benchmark signature.\n").
-spec benchmark_signature(benchmark()) -> binary().
benchmark_signature(Benchmark) ->
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<(erlang:element(2, Benchmark))/binary,
                                                            ":p50="/utf8>>/binary,
                                                        (erlang:integer_to_binary(
                                                            erlang:element(
                                                                3,
                                                                Benchmark
                                                            )
                                                        ))/binary>>/binary,
                                                    ":p95="/utf8>>/binary,
                                                (erlang:integer_to_binary(
                                                    erlang:element(4, Benchmark)
                                                ))/binary>>/binary,
                                            ":avg_payload="/utf8>>/binary,
                                        (erlang:integer_to_binary(
                                            erlang:element(5, Benchmark)
                                        ))/binary>>/binary,
                                    ":avg_ops="/utf8>>/binary,
                                (erlang:integer_to_binary(
                                    erlang:element(6, Benchmark)
                                ))/binary>>/binary,
                            ":peak_mem="/utf8>>/binary,
                        (erlang:integer_to_binary(erlang:element(7, Benchmark)))/binary>>/binary,
                    ":throughput="/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(8, Benchmark)))/binary>>/binary,
            ":root_churn="/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(9, Benchmark)))/binary>>.

-file("src/lightspeed/ops/large_data_harness.gleam", 176).
?DOC(" M28 benchmark budget profile version.\n").
-spec budget_version_label() -> binary().
budget_version_label() ->
    <<"m28.budget.v"/utf8, (erlang:integer_to_binary(1))/binary>>.

-file("src/lightspeed/ops/large_data_harness.gleam", 231).
?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/large_data_harness.gleam", 733).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/ops/large_data_harness.gleam", 631).
-spec find_benchmark(list(benchmark()), binary()) -> gleam@option:option(benchmark()).
find_benchmark(Benchmarks, Workload) ->
    case Benchmarks of
        [] ->
            none;

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

                false ->
                    find_benchmark(Rest, Workload)
            end
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 581).
-spec evaluate_one_budget(list(benchmark()), budget()) -> budget_result().
evaluate_one_budget(Benchmarks, Budget) ->
    case find_benchmark(Benchmarks, erlang:element(2, Budget)) of
        none ->
            {budget_result,
                erlang:element(2, Budget),
                false,
                <<"missing"/utf8>>};

        {some, Benchmark} ->
            P95_ok = erlang:element(4, Benchmark) =< erlang:element(3, Budget),
            Payload_ok = erlang:element(5, Benchmark) =< erlang:element(
                4,
                Budget
            ),
            Patch_ok = erlang:element(6, Benchmark) =< erlang:element(5, Budget),
            Memory_ok = erlang:element(7, Benchmark) =< erlang:element(
                6,
                Budget
            ),
            Throughput_ok = erlang:element(8, Benchmark) >= erlang:element(
                7,
                Budget
            ),
            Churn_ok = erlang:element(9, Benchmark) =< erlang:element(8, Budget),
            Passed = ((((P95_ok andalso Payload_ok) andalso Patch_ok) andalso Memory_ok)
            andalso Throughput_ok)
            andalso Churn_ok,
            {budget_result, erlang:element(2, Budget), Passed, case Passed of
                    true ->
                        <<"within_budget"/utf8>>;

                    false ->
                        <<<<<<<<<<<<<<<<<<<<<<"p95="/utf8,
                                                                    (bool_label(
                                                                        P95_ok
                                                                    ))/binary>>/binary,
                                                                ",payload="/utf8>>/binary,
                                                            (bool_label(
                                                                Payload_ok
                                                            ))/binary>>/binary,
                                                        ",patch="/utf8>>/binary,
                                                    (bool_label(Patch_ok))/binary>>/binary,
                                                ",memory="/utf8>>/binary,
                                            (bool_label(Memory_ok))/binary>>/binary,
                                        ",throughput="/utf8>>/binary,
                                    (bool_label(Throughput_ok))/binary>>/binary,
                                ",churn="/utf8>>/binary,
                            (bool_label(Churn_ok))/binary>>
                end}
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 566).
-spec evaluate_budget_loop(
    list(benchmark()),
    list(budget()),
    list(budget_result())
) -> list(budget_result()).
evaluate_budget_loop(Benchmarks, Budgets, Results_rev) ->
    case Budgets of
        [] ->
            lists:reverse(Results_rev);

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

-file("src/lightspeed/ops/large_data_harness.gleam", 223).
?DOC(" Evaluate M28 benchmark suite against one budget profile.\n").
-spec evaluate_budget(list(benchmark()), list(budget())) -> list(budget_result()).
evaluate_budget(Benchmarks, Budgets) ->
    evaluate_budget_loop(Benchmarks, Budgets, []).

-file("src/lightspeed/ops/large_data_harness.gleam", 181).
?DOC(" Default M28 heavy-workload budgets.\n").
-spec default_budget() -> list(budget()).
default_budget() ->
    [{budget, <<"large_grid"/utf8>>, 95, 240, 6, 520, 65, 0},
        {budget, <<"large_chart"/utf8>>, 120, 260, 7, 540, 55, 0}].

-file("src/lightspeed/ops/large_data_harness.gleam", 719).
-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/large_data_harness.gleam", 726).
-spec max_int(integer(), integer()) -> integer().
max_int(Left, Right) ->
    case Left >= Right of
        true ->
            Left;

        false ->
            Right
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 558).
-spec total_payload_bytes(list(lightspeed@diff:patch()), integer()) -> integer().
total_payload_bytes(Patches, Total) ->
    case Patches of
        [] ->
            Total;

        [Patch | Rest] ->
            total_payload_bytes(
                Rest,
                Total + string:length(lightspeed@diff:encode(Patch))
            )
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 693).
-spec row_payload(binary(), binary()) -> binary().
row_payload(Id, Tag) ->
    <<<<<<<<<<<<"<div data-id=\""/utf8, Id/binary>>/binary,
                        "\" data-tag=\""/utf8>>/binary,
                    Tag/binary>>/binary,
                "\">"/utf8>>/binary,
            Id/binary>>/binary,
        "</div>"/utf8>>.

-file("src/lightspeed/ops/large_data_harness.gleam", 540).
-spec benchmark_updates(
    binary(),
    integer(),
    integer(),
    list(lightspeed@data_plane:update())
) -> list(lightspeed@data_plane:update()).
benchmark_updates(Prefix, Offset, Count, Updates_rev) ->
    case Count =< 0 of
        true ->
            lists:reverse(Updates_rev);

        false ->
            Id = <<<<Prefix/binary, "-"/utf8>>/binary,
                (erlang:integer_to_binary(Offset + Count))/binary>>,
            benchmark_updates(
                Prefix,
                Offset,
                Count - 1,
                [{upsert,
                        lightspeed@data_plane:row(
                            Id,
                            row_payload(Id, <<"bench"/utf8>>)
                        )} |
                    Updates_rev]
            )
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 676).
-spec large_rows(binary(), integer(), list(lightspeed@data_plane:row())) -> list(lightspeed@data_plane:row()).
large_rows(Prefix, Total, Rows_rev) ->
    case Total =< 0 of
        true ->
            lists:reverse(Rows_rev);

        false ->
            Id = <<<<Prefix/binary, "-"/utf8>>/binary,
                (erlang:integer_to_binary(Total))/binary>>,
            large_rows(
                Prefix,
                Total - 1,
                [lightspeed@data_plane:row(Id, row_payload(Id, <<"seed"/utf8>>)) |
                    Rows_rev]
            )
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 501).
-spec simulate_benchmark(
    binary(),
    lightspeed@data_plane:dataset(),
    integer(),
    integer()
) -> benchmark().
simulate_benchmark(Workload, Dataset, Offset, Limit) ->
    Target = <<"#"/utf8, Workload/binary>>,
    Plane = lightspeed@data_plane:new(
        Target,
        Dataset,
        large_rows(Workload, 700, [])
    ),
    Request = lightspeed@data_plane:request(Offset, Limit),
    Updates = benchmark_updates(Workload, Offset, 12, []),
    {_, _, Patches} = lightspeed@data_plane:apply_updates(
        Plane,
        Request,
        Updates
    ),
    Total_payload_bytes = total_payload_bytes(Patches, 0),
    Patch_ops = erlang:length(Patches),
    Avg_payload_bytes = divide_safe(Total_payload_bytes, max_int(1, Patch_ops)),
    Avg_patch_ops = divide_safe(Patch_ops, 3),
    Root_churn_events = case lightspeed@data_plane:contains_full_root_churn(
        Patches
    ) of
        true ->
            1;

        false ->
            0
    end,
    P50_latency = 12 + divide_safe(Total_payload_bytes, 200),
    P95_latency = (P50_latency + 25) + Patch_ops,
    Peak_memory = 340 + (Patch_ops * 6),
    Throughput = max_int(30, 140 - divide_safe(P95_latency, 2)),
    {benchmark,
        Workload,
        P50_latency,
        P95_latency,
        Avg_payload_bytes,
        Avg_patch_ops,
        Peak_memory,
        Throughput,
        Root_churn_events}.

-file("src/lightspeed/ops/large_data_harness.gleam", 205).
?DOC(" Run deterministic benchmark suite for M28.\n").
-spec run_heavy_benchmarks() -> list(benchmark()).
run_heavy_benchmarks() ->
    [simulate_benchmark(<<"large_grid"/utf8>>, {grid_dataset, 8}, 120, 40),
        simulate_benchmark(
            <<"large_chart"/utf8>>,
            {chart_dataset, <<"events"/utf8>>},
            160,
            50
        )].

-file("src/lightspeed/ops/large_data_harness.gleam", 481).
-spec evaluate_heavy_budget() -> {boolean(), binary()}.
evaluate_heavy_budget() ->
    Benchmarks = run_heavy_benchmarks(),
    Budget = default_budget(),
    Results = evaluate_budget(Benchmarks, Budget),
    Failures = budget_failures(Results),
    Passed = Failures =:= 0,
    {Passed,
        <<<<<<<<<<<<<<"budget="/utf8, (budget_version_label())/binary>>/binary,
                                "|benchmarks="/utf8>>/binary,
                            (join_with(
                                <<";"/utf8>>,
                                gleam@list:map(
                                    Benchmarks,
                                    fun benchmark_signature/1
                                )
                            ))/binary>>/binary,
                        "|results="/utf8>>/binary,
                    (join_with(
                        <<";"/utf8>>,
                        gleam@list:map(Results, fun budget_result_signature/1)
                    ))/binary>>/binary,
                "|failures="/utf8>>/binary,
            (erlang:integer_to_binary(Failures))/binary>>}.

-file("src/lightspeed/ops/large_data_harness.gleam", 669).
-spec result_signature(
    {ok, nil} | {error, lightspeed@async@backpressure:runtime_error()}
) -> binary().
result_signature(Result) ->
    case Result of
        {ok, _} ->
            <<"ok"/utf8>>;

        {error, Error} ->
            lightspeed@async@backpressure:error_label(Error)
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 433).
-spec evaluate_async_backpressure() -> {boolean(), binary()}.
evaluate_async_backpressure() ->
    Runtime = lightspeed@async@backpressure:new(
        lightspeed@async@backpressure:boundary(push_pull, 2, 2, 200)
    ),
    {Runtime@1, B} = lightspeed@async@backpressure:enqueue(
        Runtime,
        <<"b"/utf8>>
    ),
    {Runtime@2, A} = lightspeed@async@backpressure:enqueue(
        Runtime@1,
        <<"a"/utf8>>
    ),
    {Runtime@3, Overflow} = lightspeed@async@backpressure:enqueue(
        Runtime@2,
        <<"overflow"/utf8>>
    ),
    {Runtime@4, Started} = lightspeed@async@backpressure:start_available(
        Runtime@3,
        10
    ),
    {Runtime@5, Failed} = lightspeed@async@backpressure:fail(
        Runtime@4,
        <<"a"/utf8>>,
        <<"timeout"/utf8>>
    ),
    {Runtime@6, Cancelled} = lightspeed@async@backpressure:cancel(
        Runtime@5,
        <<"b"/utf8>>,
        <<"backpressure"/utf8>>
    ),
    {Runtime@7, Retry_failed} = lightspeed@async@backpressure:retry(
        Runtime@6,
        <<"a"/utf8>>
    ),
    {Runtime@8, Retry_cancelled} = lightspeed@async@backpressure:retry(
        Runtime@7,
        <<"b"/utf8>>
    ),
    {Runtime@9, Started_again} = lightspeed@async@backpressure:start_available(
        Runtime@8,
        30
    ),
    {Runtime@10, Succeeded_a} = lightspeed@async@backpressure:succeed(
        Runtime@9,
        <<"a"/utf8>>
    ),
    {Runtime@11, Succeeded_b} = lightspeed@async@backpressure:succeed(
        Runtime@10,
        <<"b"/utf8>>
    ),
    Passed = ((((((((((((((lightspeed@async@backpressure:valid(Runtime@11)
    andalso (A =:= {ok, nil}))
    andalso (B =:= {ok, nil}))
    andalso (Overflow =:= {error, {queue_saturated, <<"overflow"/utf8>>, 2}}))
    andalso (Failed =:= {ok, nil}))
    andalso (Cancelled =:= {ok, nil}))
    andalso (Retry_failed =:= {ok, nil}))
    andalso (Retry_cancelled =:= {ok, nil}))
    andalso (Succeeded_a =:= {ok, nil}))
    andalso (Succeeded_b =:= {ok, nil}))
    andalso (erlang:length(Started) =:= 2))
    andalso (erlang:length(Started_again) =:= 2))
    andalso (lightspeed@async@backpressure:in_flight_count(Runtime@11) =:= 0))
    andalso (lightspeed@async@backpressure:queued_count(Runtime@11) =:= 0))
    andalso (lightspeed@async@backpressure:state(Runtime@11, <<"a"/utf8>>) =:= {some,
        {succeeded, 2}}))
    andalso (lightspeed@async@backpressure:state(Runtime@11, <<"b"/utf8>>) =:= {some,
        {succeeded, 1}}),
    {Passed,
        <<<<<<<<<<<<<<"overflow="/utf8, (result_signature(Overflow))/binary>>/binary,
                                "|started="/utf8>>/binary,
                            (erlang:integer_to_binary(erlang:length(Started)))/binary>>/binary,
                        "|started_again="/utf8>>/binary,
                    (erlang:integer_to_binary(erlang:length(Started_again)))/binary>>/binary,
                "|runtime="/utf8>>/binary,
            (lightspeed@async@backpressure:signature(Runtime@11))/binary>>}.

-file("src/lightspeed/ops/large_data_harness.gleam", 660).
-spec window_result_signature(
    {ok, lightspeed@data_plane:window()} |
        {error, lightspeed@data_plane:query_error()}
) -> binary().
window_result_signature(Result) ->
    case Result of
        {ok, Window} ->
            lightspeed@data_plane:window_signature(Window);

        {error, Error} ->
            <<"error:"/utf8,
                (lightspeed@data_plane:query_error_label(Error))/binary>>
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 392).
-spec evaluate_large_chart_window() -> {boolean(), binary()}.
evaluate_large_chart_window() ->
    Plane = lightspeed@data_plane:new(
        <<"#large-chart"/utf8>>,
        {chart_dataset, <<"events"/utf8>>},
        large_rows(<<"point"/utf8>>, 360, [])
    ),
    Request = lightspeed@data_plane:request(250, 50),
    Updates = [{upsert,
            lightspeed@data_plane:row(
                <<"point-255"/utf8>>,
                row_payload(<<"point-255"/utf8>>, <<"v1"/utf8>>)
            )},
        {upsert,
            lightspeed@data_plane:row(
                <<"point-256"/utf8>>,
                row_payload(<<"point-256"/utf8>>, <<"v2"/utf8>>)
            )},
        {delete, <<"point-251"/utf8>>}],
    {_, Next_window, Patches} = lightspeed@data_plane:apply_updates(
        Plane,
        Request,
        Updates
    ),
    Passed = case Next_window of
        {ok, Window} ->
            (((lightspeed@data_plane:valid(Plane) andalso lightspeed@data_plane:steady_state_incremental(
                Patches
            ))
            andalso not lightspeed@data_plane:contains_full_root_churn(Patches))
            andalso (erlang:element(4, Window) =:= 359))
            andalso (erlang:length(erlang:element(5, Window)) =:= 50);

        {error, _} ->
            false
    end,
    {Passed,
        <<<<<<"patch_ops="/utf8,
                    (erlang:integer_to_binary(erlang:length(Patches)))/binary>>/binary,
                "|next="/utf8>>/binary,
            (window_result_signature(Next_window))/binary>>}.

-file("src/lightspeed/ops/large_data_harness.gleam", 356).
-spec evaluate_large_grid_window() -> {boolean(), binary()}.
evaluate_large_grid_window() ->
    Plane = lightspeed@data_plane:new(
        <<"#large-grid"/utf8>>,
        {grid_dataset, 8},
        large_rows(<<"grid"/utf8>>, 420, [])
    ),
    Request = lightspeed@data_plane:request(180, 60),
    Updates = [{upsert,
            lightspeed@data_plane:row(
                <<"grid-200"/utf8>>,
                row_payload(<<"grid-200"/utf8>>, <<"r1"/utf8>>)
            )},
        {upsert,
            lightspeed@data_plane:row(
                <<"grid-219"/utf8>>,
                row_payload(<<"grid-219"/utf8>>, <<"r2"/utf8>>)
            )},
        {delete, <<"grid-181"/utf8>>},
        {upsert,
            lightspeed@data_plane:row(
                <<"grid-451"/utf8>>,
                row_payload(<<"grid-451"/utf8>>, <<"new"/utf8>>)
            )}],
    {_, Next_window, Patches} = lightspeed@data_plane:apply_updates(
        Plane,
        Request,
        Updates
    ),
    Passed = case Next_window of
        {ok, Window} ->
            (((lightspeed@data_plane:valid(Plane) andalso lightspeed@data_plane:steady_state_incremental(
                Patches
            ))
            andalso not lightspeed@data_plane:contains_full_root_churn(Patches))
            andalso (erlang:element(4, Window) =:= 420))
            andalso (erlang:length(erlang:element(5, Window)) =:= 60);

        {error, _} ->
            false
    end,
    {Passed,
        <<<<<<"patch_ops="/utf8,
                    (erlang:integer_to_binary(erlang:length(Patches)))/binary>>/binary,
                "|next="/utf8>>/binary,
            (window_result_signature(Next_window))/binary>>}.

-file("src/lightspeed/ops/large_data_harness.gleam", 313).
-spec evaluate_large_list_window() -> {boolean(), binary()}.
evaluate_large_list_window() ->
    Plane = lightspeed@data_plane:new(
        <<"#large-list"/utf8>>,
        list_dataset,
        large_rows(<<"list"/utf8>>, 300, [])
    ),
    Request = lightspeed@data_plane:request(120, 40),
    Updates = [{upsert,
            lightspeed@data_plane:row(
                <<"list-130"/utf8>>,
                <<"<li>list-130:patched</li>"/utf8>>
            )},
        {delete, <<"list-121"/utf8>>},
        {upsert,
            lightspeed@data_plane:row(
                <<"list-500"/utf8>>,
                <<"<li>list-500:new</li>"/utf8>>
            )}],
    {Next_plane, Next_window, Patches} = lightspeed@data_plane:apply_updates(
        Plane,
        Request,
        Updates
    ),
    Outside_updates = [{upsert,
            lightspeed@data_plane:row(
                <<"list-2"/utf8>>,
                <<"<li>list-2:x</li>"/utf8>>
            )}],
    {_, _, Steady_patches} = lightspeed@data_plane:apply_updates(
        Next_plane,
        Request,
        Outside_updates
    ),
    Passed = case Next_window of
        {ok, Window} ->
            ((((lightspeed@data_plane:valid(Plane) andalso lightspeed@data_plane:steady_state_incremental(
                Patches
            ))
            andalso lightspeed@data_plane:steady_state_incremental(
                Steady_patches
            ))
            andalso not lightspeed@data_plane:contains_full_root_churn(Patches))
            andalso not lightspeed@data_plane:contains_full_root_churn(
                Steady_patches
            ))
            andalso (erlang:element(4, Window) =:= 300);

        {error, _} ->
            false
    end,
    {Passed,
        <<<<<<<<<<"patch_ops="/utf8,
                            (erlang:integer_to_binary(erlang:length(Patches)))/binary>>/binary,
                        "|steady_ops="/utf8>>/binary,
                    (erlang:integer_to_binary(erlang:length(Steady_patches)))/binary>>/binary,
                "|next="/utf8>>/binary,
            (window_result_signature(Next_window))/binary>>}.

-file("src/lightspeed/ops/large_data_harness.gleam", 303).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        large_list_windowed_incremental ->
            evaluate_large_list_window();

        large_grid_windowed_incremental ->
            evaluate_large_grid_window();

        large_chart_windowed_incremental ->
            evaluate_large_chart_window();

        async_backpressure_recovery ->
            evaluate_async_backpressure();

        heavy_budget_enforcement ->
            evaluate_heavy_budget()
    end.

-file("src/lightspeed/ops/large_data_harness.gleam", 95).
?DOC(" Run one scenario twice and require deterministic signature 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/large_data_harness.gleam", 76).
?DOC(" Run all M28 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [large_list_windowed_incremental,
            large_grid_windowed_incremental,
            large_chart_windowed_incremental,
            async_backpressure_recovery,
            heavy_budget_enforcement],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        large_grid_windowed_incremental ->
            <<"large_grid_windowed_incremental"/utf8>>;

        large_chart_windowed_incremental ->
            <<"large_chart_windowed_incremental"/utf8>>;

        async_backpressure_recovery ->
            <<"async_backpressure_recovery"/utf8>>;

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

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

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

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

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

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

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

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

-file("src/lightspeed/ops/large_data_harness.gleam", 270).
?DOC(" Deterministic markdown report for 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Large Data 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\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>>.