src/lightspeed@ops@operational_autopilot_harness.erl

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

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

?MODULEDOC(" Deterministic operational autopilot expansion harness (M54).\n").

-type scenario() :: cross_surface_slo_control_loops |
    policy_bounded_override_semantics |
    diagnostics_correlation_and_recovery_timing |
    deterministic_autopilot_drill_certification |
    regression_gate_determinism.

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

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

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 363).
-spec count_nondeterministic(list(scenario_outcome())) -> integer().
count_nondeterministic(Outcomes) ->
    _pipe = Outcomes,
    _pipe@1 = gleam@list:filter(
        _pipe,
        fun(Outcome) -> not erlang:element(4, Outcome) end
    ),
    erlang:length(_pipe@1).

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 357).
-spec count_failed(list(scenario_outcome())) -> integer().
count_failed(Outcomes) ->
    _pipe = Outcomes,
    _pipe@1 = gleam@list:filter(
        _pipe,
        fun(Outcome) -> not erlang:element(3, Outcome) end
    ),
    erlang:length(_pipe@1).

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 94).
?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/operational_autopilot_harness.gleam", 376).
-spec join_with(binary(), list(binary())) -> binary().
join_with(Separator, Values) ->
    case Values of
        [] ->
            <<""/utf8>>;

        [First | Rest] ->
            gleam@list:fold(
                Rest,
                First,
                fun(Accumulator, Value) ->
                    <<<<Accumulator/binary, Separator/binary>>/binary,
                        Value/binary>>
                end
            )
    end.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 351).
-spec join_drill_signatures(
    list(lightspeed@ops@operational_autopilot:drill_result())
) -> binary().
join_drill_signatures(Drills) ->
    join_with(
        <<"|"/utf8>>,
        gleam@list:map(
            Drills,
            fun lightspeed@ops@operational_autopilot:drill_result_signature/1
        )
    ).

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 302).
-spec evaluate_deterministic_autopilot_drill_certification() -> {boolean(),
    binary()}.
evaluate_deterministic_autopilot_drill_certification() ->
    Policy = lightspeed@ops@operational_autopilot:default_policy(),
    First = lightspeed@ops@operational_autopilot:run_default_drills(),
    Second = lightspeed@ops@operational_autopilot:run_default_drills(),
    First_signature = join_drill_signatures(First),
    Second_signature = join_drill_signatures(Second),
    Passed = ((First_signature =:= Second_signature) andalso lightspeed@ops@operational_autopilot:all_drills_valid(
        First,
        Policy
    ))
    andalso lightspeed@ops@operational_autopilot:all_drills_valid(
        Second,
        Policy
    ),
    {Passed, First_signature}.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 263).
-spec evaluate_diagnostics_correlation_and_recovery_timing() -> {boolean(),
    binary()}.
evaluate_diagnostics_correlation_and_recovery_timing() ->
    Runtime = lightspeed@ops@operational_autopilot:run_drill(
        runtime_latency_spike,
        lightspeed@ops@operational_autopilot:default_policy()
    ),
    Tenant = lightspeed@ops@operational_autopilot:run_drill(
        tenant_hotspot_isolation,
        lightspeed@ops@operational_autopilot:default_policy()
    ),
    Runtime_correlation = begin
        _pipe = Runtime,
        _pipe@1 = lightspeed@ops@operational_autopilot:correlation(_pipe),
        lightspeed@ops@operational_autopilot:correlation_label(_pipe@1)
    end,
    Tenant_correlation = begin
        _pipe@2 = Tenant,
        _pipe@3 = lightspeed@ops@operational_autopilot:correlation(_pipe@2),
        lightspeed@ops@operational_autopilot:correlation_label(_pipe@3)
    end,
    Signature = <<<<<<<<<<<<<<"runtime="/utf8, Runtime_correlation/binary>>/binary,
                            ":recovery_ms="/utf8>>/binary,
                        (erlang:integer_to_binary(
                            lightspeed@ops@operational_autopilot:recovery_latency_ms(
                                Runtime
                            )
                        ))/binary>>/binary,
                    "|tenant="/utf8>>/binary,
                Tenant_correlation/binary>>/binary,
            ":recovery_ms="/utf8>>/binary,
        (erlang:integer_to_binary(
            lightspeed@ops@operational_autopilot:recovery_latency_ms(Tenant)
        ))/binary>>,
    Passed = ((((gleam_stdlib:contains_string(
        Runtime_correlation,
        <<"incident=m54-runtime-latency"/utf8>>
    )
    andalso gleam_stdlib:contains_string(
        Tenant_correlation,
        <<"incident=m54-tenant-hotspot"/utf8>>
    ))
    andalso lightspeed@ops@operational_autopilot:latency_reduced(Runtime))
    andalso lightspeed@ops@operational_autopilot:latency_reduced(Tenant))
    andalso (lightspeed@ops@operational_autopilot:recovery_latency_ms(Runtime) > 0))
    andalso (lightspeed@ops@operational_autopilot:recovery_latency_ms(Tenant) > 0),
    {Passed, Signature}.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 217).
-spec evaluate_policy_bounded_override_semantics() -> {boolean(), binary()}.
evaluate_policy_bounded_override_semantics() ->
    Strict = lightspeed@ops@operational_autopilot:strict_policy(),
    Blocked = lightspeed@ops@operational_autopilot:blocked_policy(),
    Tenant = lightspeed@ops@operational_autopilot:run_drill(
        tenant_hotspot_isolation,
        Strict
    ),
    Etl = lightspeed@ops@operational_autopilot:run_drill(etl_replay_lag, Strict),
    Blocked_tenant = lightspeed@ops@operational_autopilot:run_drill(
        tenant_hotspot_isolation,
        Blocked
    ),
    Signature = <<<<<<<<<<<<(lightspeed@ops@operational_autopilot:policy_label(
                                Strict
                            ))/binary,
                            "|tenant="/utf8>>/binary,
                        (lightspeed@ops@operational_autopilot:drill_result_signature(
                            Tenant
                        ))/binary>>/binary,
                    "|etl="/utf8>>/binary,
                (lightspeed@ops@operational_autopilot:drill_result_signature(
                    Etl
                ))/binary>>/binary,
            "|blocked="/utf8>>/binary,
        (lightspeed@ops@operational_autopilot:drill_result_signature(
            Blocked_tenant
        ))/binary>>,
    Tenant_action_requires_override = begin
        _pipe = Tenant,
        _pipe@1 = lightspeed@ops@operational_autopilot:actions(_pipe),
        gleam@list:any(
            _pipe@1,
            fun lightspeed@ops@operational_autopilot:requires_override/1
        )
    end,
    Etl_action_requires_override = begin
        _pipe@2 = Etl,
        _pipe@3 = lightspeed@ops@operational_autopilot:actions(_pipe@2),
        gleam@list:any(
            _pipe@3,
            fun lightspeed@ops@operational_autopilot:requires_override/1
        )
    end,
    Blocked_action_has_policy_bound = begin
        _pipe@4 = Blocked_tenant,
        _pipe@5 = lightspeed@ops@operational_autopilot:actions(_pipe@4),
        gleam@list:any(
            _pipe@5,
            fun lightspeed@ops@operational_autopilot:blocked_by_policy/1
        )
    end,
    Passed = ((((lightspeed@ops@operational_autopilot:drill_result_valid(
        Tenant,
        Strict
    )
    andalso lightspeed@ops@operational_autopilot:drill_result_valid(Etl, Strict))
    andalso Tenant_action_requires_override)
    andalso Etl_action_requires_override)
    andalso Blocked_action_has_policy_bound)
    andalso lightspeed@ops@operational_autopilot:actions_bounded(
        Blocked_tenant,
        Blocked
    ),
    {Passed, Signature}.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 202).
-spec evaluate_cross_surface_slo_control_loops() -> {boolean(), binary()}.
evaluate_cross_surface_slo_control_loops() ->
    Policy = lightspeed@ops@operational_autopilot:default_policy(),
    Drills = lightspeed@ops@operational_autopilot:run_default_drills(),
    Signature = join_drill_signatures(Drills),
    Passed = (((((erlang:length(Drills) =:= 4) andalso lightspeed@ops@operational_autopilot:all_drills_valid(
        Drills,
        Policy
    ))
    andalso gleam_stdlib:contains_string(Signature, <<"surface=runtime"/utf8>>))
    andalso gleam_stdlib:contains_string(
        Signature,
        <<"surface=transport"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(Signature, <<"surface=tenant"/utf8>>))
    andalso gleam_stdlib:contains_string(Signature, <<"surface=etl"/utf8>>),
    {Passed, Signature}.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 316).
-spec evaluate_regression_gate_determinism() -> {boolean(), binary()}.
evaluate_regression_gate_determinism() ->
    Cross = run_scenario(cross_surface_slo_control_loops),
    Policy = run_scenario(policy_bounded_override_semantics),
    Diagnostics = run_scenario(diagnostics_correlation_and_recovery_timing),
    Drills = run_scenario(deterministic_autopilot_drill_certification),
    Signature = <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"cross="/utf8,
                                                                (pass_fail_label(
                                                                    Cross
                                                                ))/binary>>/binary,
                                                            ":"/utf8>>/binary,
                                                        (erlang:element(
                                                            5,
                                                            Cross
                                                        ))/binary>>/binary,
                                                    "|policy="/utf8>>/binary,
                                                (pass_fail_label(Policy))/binary>>/binary,
                                            ":"/utf8>>/binary,
                                        (erlang:element(5, Policy))/binary>>/binary,
                                    "|diagnostics="/utf8>>/binary,
                                (pass_fail_label(Diagnostics))/binary>>/binary,
                            ":"/utf8>>/binary,
                        (erlang:element(5, Diagnostics))/binary>>/binary,
                    "|drills="/utf8>>/binary,
                (pass_fail_label(Drills))/binary>>/binary,
            ":"/utf8>>/binary,
        (erlang:element(5, Drills))/binary>>,
    Passed = ((((((erlang:element(3, Cross) andalso erlang:element(3, Policy))
    andalso erlang:element(3, Diagnostics))
    andalso erlang:element(3, Drills))
    andalso erlang:element(4, Cross))
    andalso erlang:element(4, Policy))
    andalso erlang:element(4, Diagnostics))
    andalso erlang:element(4, Drills),
    {Passed, Signature}.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 189).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        cross_surface_slo_control_loops ->
            evaluate_cross_surface_slo_control_loops();

        policy_bounded_override_semantics ->
            evaluate_policy_bounded_override_semantics();

        diagnostics_correlation_and_recovery_timing ->
            evaluate_diagnostics_correlation_and_recovery_timing();

        deterministic_autopilot_drill_certification ->
            evaluate_deterministic_autopilot_drill_certification();

        regression_gate_determinism ->
            evaluate_regression_gate_determinism()
    end.

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

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 41).
?DOC(" Run all M54 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [cross_surface_slo_control_loops,
            policy_bounded_override_semantics,
            diagnostics_correlation_and_recovery_timing,
            deterministic_autopilot_drill_certification,
            regression_gate_determinism],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 76).
?DOC(" M54 policy version label.\n").
-spec policy_version_label() -> binary().
policy_version_label() ->
    <<"m54.policy.v"/utf8, (erlang:integer_to_binary(1))/binary>>.

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

        policy_bounded_override_semantics ->
            <<"policy_bounded_override_semantics"/utf8>>;

        diagnostics_correlation_and_recovery_timing ->
            <<"diagnostics_correlation_and_recovery_timing"/utf8>>;

        deterministic_autopilot_drill_certification ->
            <<"deterministic_autopilot_drill_certification"/utf8>>;

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

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

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

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

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

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

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

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

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

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

-file("src/lightspeed/ops/operational_autopilot_harness.gleam", 156).
?DOC(" Deterministic markdown report for M54 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Operational Autopilot Fixture Report\n\n"/utf8,
                                                                                        "snapshot_version: "/utf8>>/binary,
                                                                                    (erlang:integer_to_binary(
                                                                                        1
                                                                                    ))/binary>>/binary,
                                                                                "\n"/utf8>>/binary,
                                                                            "policy_version: "/utf8>>/binary,
                                                                        (policy_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>>.