src/lightspeed@integration@async_driver.erl

-module(lightspeed@integration@async_driver).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/integration/async_driver.gleam").
-export([postgres_async_driver/0, clickhouse_async_driver/0, kafka_async_driver/0, object_store_async_driver/0, supported_drivers/0, default_budgets/0, valid/1, timeout_retry_telemetry_parity/0, contracts_consistent/0, class_label/1, capability_label/1, profile_label/1, driver_signature/1, capability_matrix_signature/0, run_enterprise_benchmarks/0, evaluate_budgets/2, budget_failures/1, benchmark_signature/1, budget_result_signature/1, name/1, telemetry/1, capabilities/1]).
-export_type([driver_class/0, driver_capability/0, timeout_policy/0, retry_policy/0, telemetry_profile/0, driver_contract/0, benchmark_profile/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(" Async data engine and enterprise driver-layer contracts for M48.\n").

-type driver_class() :: relational_adapter |
    columnar_adapter |
    stream_adapter |
    object_store_adapter.

-type driver_capability() :: non_blocking_execute |
    bounded_concurrency |
    timeout_telemetry |
    retry_telemetry |
    batch_read |
    batch_write |
    checkpoint_resume |
    idempotent_write |
    mixed_latency_tolerance.

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

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

-type telemetry_profile() :: {telemetry_profile,
        binary(),
        binary(),
        binary(),
        binary()}.

-type driver_contract() :: {driver_contract,
        binary(),
        driver_class(),
        binary(),
        integer(),
        integer(),
        timeout_policy(),
        retry_policy(),
        telemetry_profile(),
        list(driver_capability()),
        binary(),
        binary()}.

-type benchmark_profile() :: heavy_data | mixed_latency.

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

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

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

-file("src/lightspeed/integration/async_driver.gleam", 439).
-spec telemetry_profile(binary()) -> telemetry_profile().
telemetry_profile(Driver_name) ->
    {telemetry_profile,
        <<<<"lightspeed.driver."/utf8, Driver_name/binary>>/binary,
            ".timeouts_total"/utf8>>,
        <<<<"lightspeed.driver."/utf8, Driver_name/binary>>/binary,
            ".retries_total"/utf8>>,
        <<<<"lightspeed.driver."/utf8, Driver_name/binary>>/binary,
            ".latency_ms"/utf8>>,
        <<<<"lightspeed.driver."/utf8, Driver_name/binary>>/binary,
            ".in_flight"/utf8>>}.

-file("src/lightspeed/integration/async_driver.gleam", 425).
-spec retry_policy(integer(), integer(), integer(), integer()) -> retry_policy().
retry_policy(Max_attempts, Base_backoff_ms, Max_backoff_ms, Jitter_percent) ->
    {retry_policy,
        Max_attempts,
        Base_backoff_ms,
        Max_backoff_ms,
        Jitter_percent}.

-file("src/lightspeed/integration/async_driver.gleam", 413).
-spec timeout_policy(integer(), integer(), integer()) -> timeout_policy().
timeout_policy(Connect_ms, Request_ms, Idle_ms) ->
    {timeout_policy, Connect_ms, Request_ms, Idle_ms}.

-file("src/lightspeed/integration/async_driver.gleam", 111).
?DOC(" Relational async driver.\n").
-spec postgres_async_driver() -> driver_contract().
postgres_async_driver() ->
    {driver_contract,
        <<"postgres_async_driver"/utf8>>,
        relational_adapter,
        <<"Lightspeed.Driver.PostgresAsync"/utf8>>,
        32,
        512,
        timeout_policy(80, 450, 3000),
        retry_policy(4, 25, 250, 15),
        telemetry_profile(<<"postgres_async_driver"/utf8>>),
        [non_blocking_execute,
            bounded_concurrency,
            timeout_telemetry,
            retry_telemetry,
            batch_read,
            batch_write,
            idempotent_write,
            mixed_latency_tolerance],
        <<"ga"/utf8>>,
        <<"docs/async_data_engine_enterprise_driver_expansion.md#postgres-async-driver"/utf8>>}.

-file("src/lightspeed/integration/async_driver.gleam", 137).
?DOC(" Columnar analytics async driver.\n").
-spec clickhouse_async_driver() -> driver_contract().
clickhouse_async_driver() ->
    {driver_contract,
        <<"clickhouse_async_driver"/utf8>>,
        columnar_adapter,
        <<"Lightspeed.Driver.ClickHouseAsync"/utf8>>,
        24,
        384,
        timeout_policy(100, 550, 3200),
        retry_policy(4, 30, 280, 15),
        telemetry_profile(<<"clickhouse_async_driver"/utf8>>),
        [non_blocking_execute,
            bounded_concurrency,
            timeout_telemetry,
            retry_telemetry,
            batch_read,
            checkpoint_resume,
            mixed_latency_tolerance],
        <<"ga"/utf8>>,
        <<"docs/async_data_engine_enterprise_driver_expansion.md#clickhouse-async-driver"/utf8>>}.

-file("src/lightspeed/integration/async_driver.gleam", 162).
?DOC(" Streaming async driver.\n").
-spec kafka_async_driver() -> driver_contract().
kafka_async_driver() ->
    {driver_contract,
        <<"kafka_async_driver"/utf8>>,
        stream_adapter,
        <<"Lightspeed.Driver.KafkaAsync"/utf8>>,
        48,
        640,
        timeout_policy(70, 420, 2800),
        retry_policy(5, 20, 220, 12),
        telemetry_profile(<<"kafka_async_driver"/utf8>>),
        [non_blocking_execute,
            bounded_concurrency,
            timeout_telemetry,
            retry_telemetry,
            batch_read,
            batch_write,
            checkpoint_resume,
            idempotent_write,
            mixed_latency_tolerance],
        <<"ga"/utf8>>,
        <<"docs/async_data_engine_enterprise_driver_expansion.md#kafka-async-driver"/utf8>>}.

-file("src/lightspeed/integration/async_driver.gleam", 189).
?DOC(" Object-store batch async driver.\n").
-spec object_store_async_driver() -> driver_contract().
object_store_async_driver() ->
    {driver_contract,
        <<"object_store_async_driver"/utf8>>,
        object_store_adapter,
        <<"Lightspeed.Driver.ObjectStoreAsync"/utf8>>,
        20,
        300,
        timeout_policy(120, 700, 4500),
        retry_policy(4, 35, 320, 20),
        telemetry_profile(<<"object_store_async_driver"/utf8>>),
        [non_blocking_execute,
            bounded_concurrency,
            timeout_telemetry,
            retry_telemetry,
            batch_read,
            batch_write,
            checkpoint_resume,
            mixed_latency_tolerance],
        <<"ga"/utf8>>,
        <<"docs/async_data_engine_enterprise_driver_expansion.md#object-store-async-driver"/utf8>>}.

-file("src/lightspeed/integration/async_driver.gleam", 215).
?DOC(" Supported M48 enterprise drivers.\n").
-spec supported_drivers() -> list(driver_contract()).
supported_drivers() ->
    [postgres_async_driver(),
        clickhouse_async_driver(),
        kafka_async_driver(),
        object_store_async_driver()].

-file("src/lightspeed/integration/async_driver.gleam", 225).
?DOC(" M48 default benchmark budgets.\n").
-spec default_budgets() -> list(budget()).
default_budgets() ->
    [{budget, heavy_data, 140, 24, 48, 5000},
        {budget, mixed_latency, 130, 26, 52, 5400}].

-file("src/lightspeed/integration/async_driver.gleam", 482).
-spec telemetry_profile_valid(telemetry_profile(), binary()) -> boolean().
telemetry_profile_valid(Telemetry, Driver_name) ->
    (((erlang:element(2, Telemetry) =:= (<<<<"lightspeed.driver."/utf8,
            Driver_name/binary>>/binary,
        ".timeouts_total"/utf8>>))
    andalso (erlang:element(3, Telemetry) =:= (<<<<"lightspeed.driver."/utf8,
            Driver_name/binary>>/binary,
        ".retries_total"/utf8>>)))
    andalso (erlang:element(4, Telemetry) =:= (<<<<"lightspeed.driver."/utf8,
            Driver_name/binary>>/binary,
        ".latency_ms"/utf8>>)))
    andalso (erlang:element(5, Telemetry) =:= (<<<<"lightspeed.driver."/utf8,
            Driver_name/binary>>/binary,
        ".in_flight"/utf8>>)).

-file("src/lightspeed/integration/async_driver.gleam", 471).
-spec has_capability(list(driver_capability()), driver_capability()) -> boolean().
has_capability(Capabilities, Expected) ->
    case Capabilities of
        [] ->
            false;

        [Capability | Rest] ->
            (Capability =:= Expected) orelse has_capability(Rest, Expected)
    end.

-file("src/lightspeed/integration/async_driver.gleam", 462).
-spec has_required_capabilities(list(driver_capability())) -> boolean().
has_required_capabilities(Capabilities) ->
    (((((erlang:length(Capabilities) >= 6) andalso has_capability(
        Capabilities,
        non_blocking_execute
    ))
    andalso has_capability(Capabilities, bounded_concurrency))
    andalso has_capability(Capabilities, timeout_telemetry))
    andalso has_capability(Capabilities, retry_telemetry))
    andalso has_capability(Capabilities, mixed_latency_tolerance).

-file("src/lightspeed/integration/async_driver.gleam", 454).
-spec retry_policy_valid(retry_policy()) -> boolean().
retry_policy_valid(Policy) ->
    ((((erlang:element(2, Policy) >= 2) andalso (erlang:element(3, Policy) > 0))
    andalso (erlang:element(4, Policy) >= erlang:element(3, Policy)))
    andalso (erlang:element(5, Policy) >= 0))
    andalso (erlang:element(5, Policy) =< 25).

-file("src/lightspeed/integration/async_driver.gleam", 448).
-spec timeout_policy_valid(timeout_policy()) -> boolean().
timeout_policy_valid(Policy) ->
    ((erlang:element(2, Policy) > 0) andalso (erlang:element(3, Policy) >= erlang:element(
        2,
        Policy
    )))
    andalso (erlang:element(4, Policy) >= erlang:element(3, Policy)).

-file("src/lightspeed/integration/async_driver.gleam", 245).
?DOC(" Validate one driver contract.\n").
-spec valid(driver_contract()) -> boolean().
valid(Driver) ->
    (((((((((erlang:element(2, Driver) /= <<""/utf8>>) andalso (erlang:element(
        4,
        Driver
    )
    /= <<""/utf8>>))
    andalso (erlang:element(11, Driver) =:= <<"ga"/utf8>>))
    andalso (erlang:element(12, Driver) /= <<""/utf8>>))
    andalso (erlang:element(5, Driver) > 0))
    andalso (erlang:element(6, Driver) >= erlang:element(5, Driver)))
    andalso timeout_policy_valid(erlang:element(7, Driver)))
    andalso retry_policy_valid(erlang:element(8, Driver)))
    andalso has_required_capabilities(erlang:element(10, Driver)))
    andalso telemetry_profile_valid(
        erlang:element(9, Driver),
        erlang:element(2, Driver)
    ).

-file("src/lightspeed/integration/async_driver.gleam", 527).
-spec telemetry_metric_suffixes_consistent(list(driver_contract())) -> boolean().
telemetry_metric_suffixes_consistent(Drivers) ->
    case Drivers of
        [] ->
            true;

        [Driver | Rest] ->
            Telemetry = erlang:element(9, Driver),
            (((gleam_stdlib:string_ends_with(
                erlang:element(2, Telemetry),
                <<".timeouts_total"/utf8>>
            )
            andalso gleam_stdlib:string_ends_with(
                erlang:element(3, Telemetry),
                <<".retries_total"/utf8>>
            ))
            andalso gleam_stdlib:string_ends_with(
                erlang:element(4, Telemetry),
                <<".latency_ms"/utf8>>
            ))
            andalso gleam_stdlib:string_ends_with(
                erlang:element(5, Telemetry),
                <<".in_flight"/utf8>>
            ))
            andalso telemetry_metric_suffixes_consistent(Rest)
    end.

-file("src/lightspeed/integration/async_driver.gleam", 513).
-spec all_telemetry_metrics_present(list(driver_contract())) -> boolean().
all_telemetry_metrics_present(Drivers) ->
    case Drivers of
        [] ->
            true;

        [Driver | Rest] ->
            Telemetry = erlang:element(9, Driver),
            ((((erlang:element(2, Telemetry) /= <<""/utf8>>) andalso (erlang:element(
                3,
                Telemetry
            )
            /= <<""/utf8>>))
            andalso (erlang:element(4, Telemetry) /= <<""/utf8>>))
            andalso (erlang:element(5, Telemetry) /= <<""/utf8>>))
            andalso all_telemetry_metrics_present(Rest)
    end.

-file("src/lightspeed/integration/async_driver.gleam", 266).
?DOC(" True when timeout/retry telemetry contracts are consistent across drivers.\n").
-spec timeout_retry_telemetry_parity() -> boolean().
timeout_retry_telemetry_parity() ->
    all_telemetry_metrics_present(supported_drivers()) andalso telemetry_metric_suffixes_consistent(
        supported_drivers()
    ).

-file("src/lightspeed/integration/async_driver.gleam", 503).
-spec timeout_retry_ranges_consistent(list(driver_contract())) -> boolean().
timeout_retry_ranges_consistent(Drivers) ->
    case Drivers of
        [] ->
            true;

        [Driver | Rest] ->
            ((erlang:element(3, erlang:element(7, Driver)) =< 800) andalso (erlang:element(
                2,
                erlang:element(8, Driver)
            )
            =< 6))
            andalso timeout_retry_ranges_consistent(Rest)
    end.

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

        [Driver | Rest] ->
            valid(Driver) andalso all_drivers_valid(Rest)
    end.

-file("src/lightspeed/integration/async_driver.gleam", 259).
?DOC(" True when all supported drivers are valid and policy-consistent.\n").
-spec contracts_consistent() -> boolean().
contracts_consistent() ->
    (all_drivers_valid(supported_drivers()) andalso timeout_retry_ranges_consistent(
        supported_drivers()
    ))
    andalso timeout_retry_telemetry_parity().

-file("src/lightspeed/integration/async_driver.gleam", 272).
?DOC(" Stable class label.\n").
-spec class_label(driver_class()) -> binary().
class_label(Class) ->
    case Class of
        relational_adapter ->
            <<"relational_adapter"/utf8>>;

        columnar_adapter ->
            <<"columnar_adapter"/utf8>>;

        stream_adapter ->
            <<"stream_adapter"/utf8>>;

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

-file("src/lightspeed/integration/async_driver.gleam", 282).
?DOC(" Stable capability label.\n").
-spec capability_label(driver_capability()) -> binary().
capability_label(Capability) ->
    case Capability of
        non_blocking_execute ->
            <<"non_blocking_execute"/utf8>>;

        bounded_concurrency ->
            <<"bounded_concurrency"/utf8>>;

        timeout_telemetry ->
            <<"timeout_telemetry"/utf8>>;

        retry_telemetry ->
            <<"retry_telemetry"/utf8>>;

        batch_read ->
            <<"batch_read"/utf8>>;

        batch_write ->
            <<"batch_write"/utf8>>;

        checkpoint_resume ->
            <<"checkpoint_resume"/utf8>>;

        idempotent_write ->
            <<"idempotent_write"/utf8>>;

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

-file("src/lightspeed/integration/async_driver.gleam", 297).
?DOC(" Stable benchmark-profile label.\n").
-spec profile_label(benchmark_profile()) -> binary().
profile_label(Profile) ->
    case Profile of
        heavy_data ->
            <<"heavy_data"/utf8>>;

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

-file("src/lightspeed/integration/async_driver.gleam", 687).
-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/integration/async_driver.gleam", 561).
-spec telemetry_signature(telemetry_profile()) -> binary().
telemetry_signature(Profile) ->
    <<<<<<<<<<<<<<"timeout="/utf8, (erlang:element(2, Profile))/binary>>/binary,
                            ",retry="/utf8>>/binary,
                        (erlang:element(3, Profile))/binary>>/binary,
                    ",latency="/utf8>>/binary,
                (erlang:element(4, Profile))/binary>>/binary,
            ",in_flight="/utf8>>/binary,
        (erlang:element(5, Profile))/binary>>.

-file("src/lightspeed/integration/async_driver.gleam", 550).
-spec retry_policy_signature(retry_policy()) -> binary().
retry_policy_signature(Policy) ->
    <<<<<<<<<<<<<<"attempts="/utf8,
                                (erlang:integer_to_binary(
                                    erlang:element(2, Policy)
                                ))/binary>>/binary,
                            ",base="/utf8>>/binary,
                        (erlang:integer_to_binary(erlang:element(3, Policy)))/binary>>/binary,
                    ",max="/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(4, Policy)))/binary>>/binary,
            ",jitter="/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(5, Policy)))/binary>>.

-file("src/lightspeed/integration/async_driver.gleam", 541).
-spec timeout_policy_signature(timeout_policy()) -> binary().
timeout_policy_signature(Policy) ->
    <<<<<<<<<<"connect="/utf8,
                        (erlang:integer_to_binary(erlang:element(2, Policy)))/binary>>/binary,
                    ",request="/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(3, Policy)))/binary>>/binary,
            ",idle="/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(4, Policy)))/binary>>.

-file("src/lightspeed/integration/async_driver.gleam", 305).
?DOC(" Stable driver contract signature.\n").
-spec driver_signature(driver_contract()) -> binary().
driver_signature(Driver) ->
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"driver="/utf8,
                                                                                (erlang:element(
                                                                                    2,
                                                                                    Driver
                                                                                ))/binary>>/binary,
                                                                            "|class="/utf8>>/binary,
                                                                        (class_label(
                                                                            erlang:element(
                                                                                3,
                                                                                Driver
                                                                            )
                                                                        ))/binary>>/binary,
                                                                    "|provider="/utf8>>/binary,
                                                                (erlang:element(
                                                                    4,
                                                                    Driver
                                                                ))/binary>>/binary,
                                                            "|in_flight="/utf8>>/binary,
                                                        (erlang:integer_to_binary(
                                                            erlang:element(
                                                                5,
                                                                Driver
                                                            )
                                                        ))/binary>>/binary,
                                                    "|queue_depth="/utf8>>/binary,
                                                (erlang:integer_to_binary(
                                                    erlang:element(6, Driver)
                                                ))/binary>>/binary,
                                            "|timeouts="/utf8>>/binary,
                                        (timeout_policy_signature(
                                            erlang:element(7, Driver)
                                        ))/binary>>/binary,
                                    "|retries="/utf8>>/binary,
                                (retry_policy_signature(
                                    erlang:element(8, Driver)
                                ))/binary>>/binary,
                            "|telemetry="/utf8>>/binary,
                        (telemetry_signature(erlang:element(9, Driver)))/binary>>/binary,
                    "|capabilities="/utf8>>/binary,
                (join_with(
                    <<","/utf8>>,
                    gleam@list:map(
                        erlang:element(10, Driver),
                        fun capability_label/1
                    )
                ))/binary>>/binary,
            "|tier="/utf8>>/binary,
        (erlang:element(11, Driver))/binary>>.

-file("src/lightspeed/integration/async_driver.gleam", 329).
?DOC(" Stable adapter capability matrix signature.\n").
-spec capability_matrix_signature() -> binary().
capability_matrix_signature() ->
    join_with(
        <<";"/utf8>>,
        gleam@list:map(supported_drivers(), fun driver_signature/1)
    ).

-file("src/lightspeed/integration/async_driver.gleam", 608).
-spec ensure_benchmark_driver_count(list(benchmark()), list(driver_contract())) -> list(benchmark()).
ensure_benchmark_driver_count(Benchmarks, Drivers) ->
    case erlang:length(Benchmarks) =:= (erlang:length(Drivers) * 2) of
        true ->
            Benchmarks;

        false ->
            []
    end.

-file("src/lightspeed/integration/async_driver.gleam", 601).
-spec profile_adjustment(benchmark_profile()) -> {integer(),
    integer(),
    integer(),
    integer(),
    integer()}.
profile_adjustment(Profile) ->
    case Profile of
        heavy_data ->
            {12, 28, 4, 8, 1000};

        mixed_latency ->
            {8, 20, 6, 10, 500}
    end.

-file("src/lightspeed/integration/async_driver.gleam", 592).
-spec class_baseline(driver_class()) -> {integer(),
    integer(),
    integer(),
    integer(),
    integer()}.
class_baseline(Class) ->
    case Class of
        relational_adapter ->
            {22, 58, 7, 18, 8600};

        columnar_adapter ->
            {30, 76, 9, 22, 7800};

        stream_adapter ->
            {18, 50, 6, 16, 9800};

        object_store_adapter ->
            {36, 92, 11, 26, 6500}
    end.

-file("src/lightspeed/integration/async_driver.gleam", 572).
-spec simulate_benchmark(driver_contract(), benchmark_profile()) -> benchmark().
simulate_benchmark(Driver, Profile) ->
    {Base_p50, Base_p95, Base_timeout, Base_retry, Base_throughput} = class_baseline(
        erlang:element(3, Driver)
    ),
    {P50_adjust, P95_adjust, Timeout_adjust, Retry_adjust, Throughput_adjust} = profile_adjustment(
        Profile
    ),
    {benchmark,
        erlang:element(2, Driver),
        Profile,
        Base_p50 + P50_adjust,
        Base_p95 + P95_adjust,
        Base_timeout + Timeout_adjust,
        Base_retry + Retry_adjust,
        Base_throughput - Throughput_adjust}.

-file("src/lightspeed/integration/async_driver.gleam", 334).
?DOC(" Run deterministic heavy-data and mixed-latency benchmark profiles.\n").
-spec run_enterprise_benchmarks() -> list(benchmark()).
run_enterprise_benchmarks() ->
    Drivers = supported_drivers(),
    _pipe = [simulate_benchmark(postgres_async_driver(), heavy_data),
        simulate_benchmark(postgres_async_driver(), mixed_latency),
        simulate_benchmark(clickhouse_async_driver(), heavy_data),
        simulate_benchmark(clickhouse_async_driver(), mixed_latency),
        simulate_benchmark(kafka_async_driver(), heavy_data),
        simulate_benchmark(kafka_async_driver(), mixed_latency),
        simulate_benchmark(object_store_async_driver(), heavy_data),
        simulate_benchmark(object_store_async_driver(), mixed_latency)],
    ensure_benchmark_driver_count(_pipe, Drivers).

-file("src/lightspeed/integration/async_driver.gleam", 666).
-spec budget_for_profile(benchmark_profile(), list(budget())) -> {ok, budget()} |
    {error, binary()}.
budget_for_profile(Profile, Budgets) ->
    case Budgets of
        [] ->
            {error, <<"missing_budget_profile"/utf8>>};

        [Budget | Rest] ->
            case erlang:element(2, Budget) =:= Profile of
                true ->
                    {ok, Budget};

                false ->
                    budget_for_profile(Profile, Rest)
            end
    end.

-file("src/lightspeed/integration/async_driver.gleam", 632).
-spec evaluate_one_budget(benchmark(), list(budget())) -> budget_result().
evaluate_one_budget(Benchmark, Budgets) ->
    case budget_for_profile(erlang:element(3, Benchmark), Budgets) of
        {error, Reason} ->
            {budget_result,
                erlang:element(2, Benchmark),
                erlang:element(3, Benchmark),
                false,
                Reason};

        {ok, Budget} ->
            P95_ok = erlang:element(5, Benchmark) =< erlang:element(3, Budget),
            Timeout_ok = erlang:element(6, Benchmark) =< erlang:element(
                4,
                Budget
            ),
            Retry_ok = erlang:element(7, Benchmark) =< erlang:element(5, Budget),
            Throughput_ok = erlang:element(8, Benchmark) >= erlang:element(
                6,
                Budget
            ),
            Passed = ((P95_ok andalso Timeout_ok) andalso Retry_ok) andalso Throughput_ok,
            Reason@1 = case Passed of
                true ->
                    <<"within_budget"/utf8>>;

                false ->
                    <<"budget_exceeded"/utf8>>
            end,
            {budget_result,
                erlang:element(2, Benchmark),
                erlang:element(3, Benchmark),
                Passed,
                Reason@1}
    end.

-file("src/lightspeed/integration/async_driver.gleam", 618).
-spec evaluate_budgets_loop(
    list(benchmark()),
    list(budget()),
    list(budget_result())
) -> list(budget_result()).
evaluate_budgets_loop(Benchmarks, Budgets, Results_rev) ->
    case Benchmarks of
        [] ->
            lists:reverse(Results_rev);

        [Benchmark | Rest] ->
            Result = evaluate_one_budget(Benchmark, Budgets),
            evaluate_budgets_loop(Rest, Budgets, [Result | Results_rev])
    end.

-file("src/lightspeed/integration/async_driver.gleam", 351).
?DOC(" Evaluate benchmarks against M48 budgets.\n").
-spec evaluate_budgets(list(benchmark()), list(budget())) -> list(budget_result()).
evaluate_budgets(Benchmarks, Budgets) ->
    evaluate_budgets_loop(Benchmarks, Budgets, []).

-file("src/lightspeed/integration/async_driver.gleam", 359).
?DOC(" Count failing budget checks.\n").
-spec budget_failures(list(budget_result())) -> integer().
budget_failures(Results) ->
    case Results of
        [] ->
            0;

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

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

-file("src/lightspeed/integration/async_driver.gleam", 371).
?DOC(" Stable benchmark signature.\n").
-spec benchmark_signature(benchmark()) -> binary().
benchmark_signature(Benchmark) ->
    <<<<<<<<<<<<<<<<<<<<<<<<(erlang:element(2, Benchmark))/binary,
                                                    ":profile="/utf8>>/binary,
                                                (profile_label(
                                                    erlang:element(3, Benchmark)
                                                ))/binary>>/binary,
                                            ":p50="/utf8>>/binary,
                                        (erlang:integer_to_binary(
                                            erlang:element(4, Benchmark)
                                        ))/binary>>/binary,
                                    ":p95="/utf8>>/binary,
                                (erlang:integer_to_binary(
                                    erlang:element(5, Benchmark)
                                ))/binary>>/binary,
                            ":timeout_bps="/utf8>>/binary,
                        (erlang:integer_to_binary(erlang:element(6, Benchmark)))/binary>>/binary,
                    ":retry_bps="/utf8>>/binary,
                (erlang:integer_to_binary(erlang:element(7, Benchmark)))/binary>>/binary,
            ":throughput="/utf8>>/binary,
        (erlang:integer_to_binary(erlang:element(8, Benchmark)))/binary>>.

-file("src/lightspeed/integration/async_driver.gleam", 680).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/integration/async_driver.gleam", 388).
?DOC(" Stable budget-check result signature.\n").
-spec budget_result_signature(budget_result()) -> binary().
budget_result_signature(Result) ->
    <<<<<<<<<<<<(erlang:element(2, Result))/binary, ":profile="/utf8>>/binary,
                        (profile_label(erlang:element(3, Result)))/binary>>/binary,
                    ":passed="/utf8>>/binary,
                (bool_label(erlang:element(4, Result)))/binary>>/binary,
            ":reason="/utf8>>/binary,
        (erlang:element(5, Result))/binary>>.

-file("src/lightspeed/integration/async_driver.gleam", 399).
?DOC(" Driver name accessor.\n").
-spec name(driver_contract()) -> binary().
name(Driver) ->
    erlang:element(2, Driver).

-file("src/lightspeed/integration/async_driver.gleam", 404).
?DOC(" Driver telemetry accessor.\n").
-spec telemetry(driver_contract()) -> telemetry_profile().
telemetry(Driver) ->
    erlang:element(9, Driver).

-file("src/lightspeed/integration/async_driver.gleam", 409).
?DOC(" Driver capabilities accessor.\n").
-spec capabilities(driver_contract()) -> list(driver_capability()).
capabilities(Driver) ->
    erlang:element(10, Driver).