src/lightspeed@ops@chaos_expansion_harness.erl

-module(lightspeed@ops@chaos_expansion_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/chaos_expansion_harness.gleam").
-export([run_scenario/1, run_matrix/0, taxonomy_version_label/0, scenario_label/1, pass_fail_label/1, signature/1, scenario/1, deterministic/1, outcomes/1, failed_scenarios/1, nondeterministic_failures/1, report_signature/1, snapshot_signature/0, snapshot_report_markdown/0]).
-export_type([scenario/0, scenario_outcome/0, report/0, incident_pack/0, taxonomy_entry/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 chaos and fault-injection verification expansion harness (M53).\n").

-type scenario() :: compound_failure_matrix_expansion |
    deterministic_incident_pack_replay_certification |
    autopilot_operator_mitigation_integration |
    failure_taxonomy_remediation_coverage |
    regression_gate_determinism.

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

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

-type incident_pack() :: {incident_pack,
        binary(),
        lightspeed@ops@replay_diagnostics:investigation_workflow(),
        binary(),
        lightspeed@ops@replay_diagnostics:replay_trace()}.

-type taxonomy_entry() :: {taxonomy_entry,
        binary(),
        binary(),
        lightspeed@ops@slo_autopilot:status(),
        lightspeed@ops@operations_kit:workflow_action(),
        binary(),
        binary()}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 753).
-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/chaos_expansion_harness.gleam", 742).
-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/chaos_expansion_harness.gleam", 783).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 775).
-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/chaos_expansion_harness.gleam", 705).
-spec has_workflow(
    list(lightspeed@ops@operations_kit:workflow_action()),
    lightspeed@ops@operations_kit:workflow_action()
) -> boolean().
has_workflow(Workflows, Target) ->
    case Workflows of
        [] ->
            false;

        [Workflow | Rest] ->
            case Workflow =:= Target of
                true ->
                    true;

                false ->
                    has_workflow(Rest, Target)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 694).
-spec has_runbook(list(lightspeed@ops@operations_kit:runbook()), binary()) -> boolean().
has_runbook(Runbooks, Name) ->
    case Runbooks of
        [] ->
            false;

        [Runbook | Rest] ->
            case erlang:element(3, Runbook) =:= Name of
                true ->
                    true;

                false ->
                    has_runbook(Rest, Name)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 677).
-spec taxonomy_fields_valid(
    list(taxonomy_entry()),
    list(lightspeed@ops@operations_kit:runbook()),
    list(lightspeed@ops@operations_kit:workflow_action())
) -> boolean().
taxonomy_fields_valid(Entries, Runbooks, Workflows) ->
    case Entries of
        [] ->
            true;

        [Entry | Rest] ->
            (((((erlang:element(2, Entry) /= <<""/utf8>>) andalso (erlang:element(
                3,
                Entry
            )
            /= <<""/utf8>>))
            andalso (erlang:element(7, Entry) /= <<""/utf8>>))
            andalso has_runbook(Runbooks, erlang:element(6, Entry)))
            andalso has_workflow(Workflows, erlang:element(5, Entry)))
            andalso taxonomy_fields_valid(Rest, Runbooks, Workflows)
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 764).
-spec contains(list(binary()), binary()) -> boolean().
contains(Values, Target) ->
    case Values of
        [] ->
            false;

        [Value | Rest] ->
            case Value =:= Target of
                true ->
                    true;

                false ->
                    contains(Rest, Target)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 663).
-spec taxonomy_unique_classes(list(taxonomy_entry()), list(binary())) -> boolean().
taxonomy_unique_classes(Entries, Seen) ->
    case Entries of
        [] ->
            true;

        [Entry | Rest] ->
            case contains(Seen, erlang:element(2, Entry)) of
                true ->
                    false;

                false ->
                    taxonomy_unique_classes(
                        Rest,
                        [erlang:element(2, Entry) | Seen]
                    )
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 652).
-spec has_surfaces(list(taxonomy_entry()), binary()) -> boolean().
has_surfaces(Entries, Target) ->
    case Entries of
        [] ->
            false;

        [Entry | Rest] ->
            case gleam_stdlib:contains_string(erlang:element(3, Entry), Target) of
                true ->
                    true;

                false ->
                    has_surfaces(Rest, Target)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 645).
-spec taxonomy_covers_domains(list(taxonomy_entry())) -> boolean().
taxonomy_covers_domains(Entries) ->
    ((has_surfaces(Entries, <<"transport"/utf8>>) andalso has_surfaces(
        Entries,
        <<"runtime"/utf8>>
    ))
    andalso has_surfaces(Entries, <<"data"/utf8>>))
    andalso has_surfaces(Entries, <<"etl"/utf8>>).

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 634).
-spec has_severity(list(taxonomy_entry()), binary()) -> boolean().
has_severity(Entries, Label) ->
    case Entries of
        [] ->
            false;

        [Entry | Rest] ->
            case lightspeed@ops@slo_autopilot:status_label(
                erlang:element(4, Entry)
            )
            =:= Label of
                true ->
                    true;

                false ->
                    has_severity(Rest, Label)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 630).
-spec taxonomy_covers_critical_and_warning(list(taxonomy_entry())) -> boolean().
taxonomy_covers_critical_and_warning(Entries) ->
    has_severity(Entries, <<"critical"/utf8>>) andalso has_severity(
        Entries,
        <<"warning"/utf8>>
    ).

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 619).
-spec count_compound_classes(list(taxonomy_entry())) -> integer().
count_compound_classes(Entries) ->
    case Entries of
        [] ->
            0;

        [Entry | Rest] ->
            case gleam_stdlib:contains_string(
                erlang:element(2, Entry),
                <<"+"/utf8>>
            ) of
                true ->
                    1 + count_compound_classes(Rest);

                false ->
                    count_compound_classes(Rest)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 615).
-spec taxonomy_has_compound_classes(list(taxonomy_entry())) -> boolean().
taxonomy_has_compound_classes(Entries) ->
    count_compound_classes(Entries) >= 2.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 604).
-spec taxonomy_entries_valid(list(taxonomy_entry())) -> boolean().
taxonomy_entries_valid(Entries) ->
    Runbooks = lightspeed@ops@operations_kit:canonical_runbooks(),
    Workflows = lightspeed@ops@operations_kit:required_workflows(),
    (((((erlang:length(Entries) >= 7) andalso taxonomy_has_compound_classes(
        Entries
    ))
    andalso taxonomy_covers_critical_and_warning(Entries))
    andalso taxonomy_covers_domains(Entries))
    andalso taxonomy_unique_classes(Entries, []))
    andalso taxonomy_fields_valid(Entries, Runbooks, Workflows).

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 589).
-spec taxonomy_entry_label(taxonomy_entry()) -> binary().
taxonomy_entry_label(Entry) ->
    <<<<<<<<<<<<<<<<<<<<<<"class="/utf8, (erlang:element(2, Entry))/binary>>/binary,
                                            "|surfaces="/utf8>>/binary,
                                        (erlang:element(3, Entry))/binary>>/binary,
                                    "|severity="/utf8>>/binary,
                                (lightspeed@ops@slo_autopilot:status_label(
                                    erlang:element(4, Entry)
                                ))/binary>>/binary,
                            "|workflow="/utf8>>/binary,
                        (lightspeed@ops@operations_kit:workflow_label(
                            erlang:element(5, Entry)
                        ))/binary>>/binary,
                    "|runbook="/utf8>>/binary,
                (erlang:element(6, Entry))/binary>>/binary,
            "|remediation="/utf8>>/binary,
        (erlang:element(7, Entry))/binary>>.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 528).
-spec taxonomy_entries() -> list(taxonomy_entry()).
taxonomy_entries() ->
    [{taxonomy_entry,
            <<"transport_heartbeat_timeout"/utf8>>,
            <<"transport"/utf8>>,
            warning,
            drain,
            <<"transport_degradation"/utf8>>,
            <<"activate_fallback_mode"/utf8>>},
        {taxonomy_entry,
            <<"runtime_crash_loop"/utf8>>,
            <<"runtime"/utf8>>,
            critical,
            rollback,
            <<"runtime_incident"/utf8>>,
            <<"execute_safe_rollback"/utf8>>},
        {taxonomy_entry,
            <<"storage_split_brain"/utf8>>,
            <<"storage"/utf8>>,
            critical,
            recover,
            <<"data_integrity"/utf8>>,
            <<"freeze_mutating_writes_and_recover_owner"/utf8>>},
        {taxonomy_entry,
            <<"data_checkpoint_drift"/utf8>>,
            <<"data"/utf8>>,
            warning,
            replay,
            <<"data_integrity"/utf8>>,
            <<"select_replay_checkpoint"/utf8>>},
        {taxonomy_entry,
            <<"pipeline_replay_divergence"/utf8>>,
            <<"etl"/utf8>>,
            critical,
            recover,
            <<"etl_reliability"/utf8>>,
            <<"pause_affected_pipelines"/utf8>>},
        {taxonomy_entry,
            <<"transport+runtime_compound"/utf8>>,
            <<"transport,runtime"/utf8>>,
            critical,
            rollback,
            <<"runtime_incident"/utf8>>,
            <<"enable_guardrails_then_rollback"/utf8>>},
        {taxonomy_entry,
            <<"storage+data_compound"/utf8>>,
            <<"storage,data"/utf8>>,
            critical,
            replay,
            <<"data_integrity"/utf8>>,
            <<"replay_with_checkpoint_fencing"/utf8>>}].

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 421).
-spec evaluate_failure_taxonomy_remediation_coverage() -> {boolean(), binary()}.
evaluate_failure_taxonomy_remediation_coverage() ->
    Entries = taxonomy_entries(),
    Labels = gleam@list:map(Entries, fun taxonomy_entry_label/1),
    Passed = taxonomy_entries_valid(Entries),
    {Passed, join_with(<<";"/utf8>>, Labels)}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 719).
-spec count_decision(list(lightspeed@ops@slo_autopilot:audit_entry()), binary()) -> integer().
count_decision(Entries, Target) ->
    case Entries of
        [] ->
            0;

        [Entry | Rest] ->
            case lightspeed@ops@slo_autopilot:decision_label(
                erlang:element(4, Entry)
            )
            =:= Target of
                true ->
                    1 + count_decision(Rest, Target);

                false ->
                    count_decision(Rest, Target)
            end
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 342).
-spec evaluate_autopilot_operator_mitigation_integration() -> {boolean(),
    binary()}.
evaluate_autopilot_operator_mitigation_integration() ->
    Runtime = lightspeed@ops@slo_autopilot:new(
        lightspeed@ops@slo_autopilot:default_thresholds()
    ),
    Warning_sample = lightspeed@ops@slo_autopilot:sample(
        <<"edge"/utf8>>,
        <<"availability"/utf8>>,
        1300,
        900,
        <<"tenant-a"/utf8>>
    ),
    {Runtime@1, Warning_status} = lightspeed@ops@slo_autopilot:evaluate(
        Runtime,
        Warning_sample,
        <<"autopilot"/utf8>>,
        10
    ),
    Critical_sample = lightspeed@ops@slo_autopilot:sample(
        <<"edge"/utf8>>,
        <<"availability"/utf8>>,
        2700,
        1900,
        <<"tenant-a"/utf8>>
    ),
    {Runtime@2, Critical_status} = lightspeed@ops@slo_autopilot:evaluate(
        Runtime@1,
        Critical_sample,
        <<"autopilot"/utf8>>,
        20
    ),
    Recovery_sample = lightspeed@ops@slo_autopilot:sample(
        <<"edge"/utf8>>,
        <<"availability"/utf8>>,
        200,
        100,
        <<""/utf8>>
    ),
    {Runtime@3, Recovery_status} = lightspeed@ops@slo_autopilot:evaluate(
        Runtime@2,
        Recovery_sample,
        <<"autopilot"/utf8>>,
        30
    ),
    Audits = lightspeed@ops@slo_autopilot:audit_entries(Runtime@3),
    Applied = count_decision(Audits, <<"applied"/utf8>>),
    Rolled_back = count_decision(Audits, <<"rolled_back"/utf8>>),
    Active = gleam@list:map(
        lightspeed@ops@slo_autopilot:active_controls(Runtime@3),
        fun lightspeed@ops@slo_autopilot:control_label/1
    ),
    Rollback_plan = lightspeed@ops@operations_kit:workflow_plan_signature(
        lightspeed@ops@operations_kit:workflow_contract(rollback)
    ),
    Recover_plan = lightspeed@ops@operations_kit:workflow_plan_signature(
        lightspeed@ops@operations_kit:workflow_contract(recover)
    ),
    Runbook_signatures = join_with(
        <<";"/utf8>>,
        gleam@list:map(
            lightspeed@ops@operations_kit:canonical_runbooks(),
            fun lightspeed@ops@operations_kit:runbook_signature/1
        )
    ),
    Passed = ((((((((((lightspeed@ops@slo_autopilot:valid(Runtime@3) andalso lightspeed@ops@operations_kit:operations_kit_ready(
        
    ))
    andalso (lightspeed@ops@slo_autopilot:status_label(Warning_status) =:= <<"warning"/utf8>>))
    andalso (lightspeed@ops@slo_autopilot:status_label(Critical_status) =:= <<"critical"/utf8>>))
    andalso (lightspeed@ops@slo_autopilot:status_label(Recovery_status) =:= <<"healthy"/utf8>>))
    andalso (Active =:= []))
    andalso (Applied >= 5))
    andalso (Rolled_back >= 5))
    andalso gleam_stdlib:contains_string(
        Rollback_plan,
        <<"workflow:rollback"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Recover_plan,
        <<"workflow:recover"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Runbook_signatures,
        <<"runbook:runtime:runtime_incident"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Runbook_signatures,
        <<"runbook:data:data_integrity"/utf8>>
    ),
    {Passed,
        <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"statuses="/utf8,
                                                                    (lightspeed@ops@slo_autopilot:status_label(
                                                                        Warning_status
                                                                    ))/binary>>/binary,
                                                                ","/utf8>>/binary,
                                                            (lightspeed@ops@slo_autopilot:status_label(
                                                                Critical_status
                                                            ))/binary>>/binary,
                                                        ","/utf8>>/binary,
                                                    (lightspeed@ops@slo_autopilot:status_label(
                                                        Recovery_status
                                                    ))/binary>>/binary,
                                                "|active="/utf8>>/binary,
                                            (join_with(<<","/utf8>>, Active))/binary>>/binary,
                                        "|applied="/utf8>>/binary,
                                    (erlang:integer_to_binary(Applied))/binary>>/binary,
                                "|rolled_back="/utf8>>/binary,
                            (erlang:integer_to_binary(Rolled_back))/binary>>/binary,
                        "|rollback_plan="/utf8>>/binary,
                    Rollback_plan/binary>>/binary,
                "|recover_plan="/utf8>>/binary,
            Recover_plan/binary>>}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 501).
-spec incident_pack_ready(incident_pack()) -> boolean().
incident_pack_ready(Pack) ->
    Workflow_ready = case erlang:element(3, Pack) of
        fault_workflow ->
            lightspeed@ops@replay_diagnostics:fault_investigation_ready(
                erlang:element(5, Pack)
            );

        etl_workflow ->
            lightspeed@ops@replay_diagnostics:etl_investigation_ready(
                erlang:element(5, Pack)
            )
    end,
    First_fault = lightspeed@ops@replay_diagnostics:first_fault_snapshot(
        erlang:element(5, Pack)
    ),
    Runbooks = lightspeed@ops@operations_kit:canonical_runbooks(),
    Runbook_ready = has_runbook(Runbooks, erlang:element(4, Pack)),
    (Workflow_ready andalso Runbook_ready) andalso case First_fault of
        none ->
            false;

        {some, _} ->
            true
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 520).
-spec all_incident_packs_ready(list(incident_pack())) -> boolean().
all_incident_packs_ready(Packs) ->
    case Packs of
        [] ->
            true;

        [Pack | Rest] ->
            incident_pack_ready(Pack) andalso all_incident_packs_ready(Rest)
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 733).
-spec investigation_workflow_label(
    lightspeed@ops@replay_diagnostics:investigation_workflow()
) -> binary().
investigation_workflow_label(Workflow) ->
    case Workflow of
        fault_workflow ->
            <<"fault"/utf8>>;

        etl_workflow ->
            <<"etl"/utf8>>
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 488).
-spec incident_pack_signature(incident_pack()) -> binary().
incident_pack_signature(Pack) ->
    <<<<<<<<<<<<<<<<<<"id="/utf8, (erlang:element(2, Pack))/binary>>/binary,
                                    "|workflow="/utf8>>/binary,
                                (investigation_workflow_label(
                                    erlang:element(3, Pack)
                                ))/binary>>/binary,
                            "|runbook="/utf8>>/binary,
                        (erlang:element(4, Pack))/binary>>/binary,
                    "|ready="/utf8>>/binary,
                (bool_label(incident_pack_ready(Pack)))/binary>>/binary,
            "|trace="/utf8>>/binary,
        (lightspeed@ops@replay_diagnostics:trace_signature(
            erlang:element(5, Pack)
        ))/binary>>.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 465).
-spec incident_packs() -> list(incident_pack()).
incident_packs() ->
    [{incident_pack,
            <<"runtime_fault_pack"/utf8>>,
            fault_workflow,
            <<"runtime_incident"/utf8>>,
            lightspeed@ops@replay_diagnostics:runtime_fault_trace(
                <<"m53-runtime-fault"/utf8>>
            )},
        {incident_pack,
            <<"session_fault_pack"/utf8>>,
            fault_workflow,
            <<"transport_degradation"/utf8>>,
            lightspeed@ops@replay_diagnostics:session_fault_trace(
                <<"m53-session-fault"/utf8>>
            )},
        {incident_pack,
            <<"pipeline_replay_pack"/utf8>>,
            etl_workflow,
            <<"data_integrity"/utf8>>,
            lightspeed@ops@replay_diagnostics:pipeline_etl_trace(
                <<"m53-pipeline-replay"/utf8>>
            )}].

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 326).
-spec evaluate_deterministic_incident_pack_replay_certification() -> {boolean(),
    binary()}.
evaluate_deterministic_incident_pack_replay_certification() ->
    First = incident_packs(),
    Second = incident_packs(),
    First_signatures = gleam@list:map(First, fun incident_pack_signature/1),
    Second_signatures = gleam@list:map(Second, fun incident_pack_signature/1),
    Passed = ((erlang:length(First) =:= 3) andalso (First_signatures =:= Second_signatures))
    andalso all_incident_packs_ready(First),
    {Passed, join_with(<<";"/utf8>>, First_signatures)}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 268).
-spec evaluate_data_surface_failure_recovery() -> {boolean(), binary()}.
evaluate_data_surface_failure_recovery() ->
    Corrupted = lightspeed@pipeline@checkpoint:checkpoint(
        <<"run-m53"/utf8>>,
        <<"extract_orders"/utf8>>,
        0,
        lightspeed@pipeline@checkpoint:watermark(-1, 120),
        <<"source-a"/utf8>>,
        121
    ),
    Chain = [lightspeed@pipeline@checkpoint:checkpoint(
            <<"run-m53"/utf8>>,
            <<"extract_orders"/utf8>>,
            1,
            lightspeed@pipeline@checkpoint:watermark(10, 120),
            <<"source-a"/utf8>>,
            122
        ),
        lightspeed@pipeline@checkpoint:checkpoint(
            <<"run-m53"/utf8>>,
            <<"extract_orders"/utf8>>,
            2,
            lightspeed@pipeline@checkpoint:watermark(25, 150),
            <<"source-a"/utf8>>,
            130
        )],
    Missing = lightspeed@pipeline@checkpoint:resume_point_for_stage(
        Chain,
        <<"write_orders"/utf8>>
    ),
    Recovered = lightspeed@pipeline@checkpoint:resume_point_for_stage(
        Chain,
        <<"extract_orders"/utf8>>
    ),
    Missing_label = case Missing of
        {ok, _} ->
            <<"ok"/utf8>>;

        {error, Reason} ->
            Reason
    end,
    Recovered_label = case Recovered of
        {ok, Point} ->
            lightspeed@pipeline@checkpoint:resume_point_label(Point);

        {error, Reason@1} ->
            <<"error:"/utf8, Reason@1/binary>>
    end,
    Passed = (((lightspeed@pipeline@checkpoint:valid(Corrupted) =:= false)
    andalso (Missing_label =:= <<"missing_stage_checkpoint:write_orders"/utf8>>))
    andalso gleam_stdlib:contains_string(Recovered_label, <<"sequence=2"/utf8>>))
    andalso gleam_stdlib:contains_string(
        lightspeed@pipeline@checkpoint:chain_signature(Chain),
        <<"sequence=2"/utf8>>
    ),
    {Passed,
        <<<<<<<<<<<<<<"corrupted_valid="/utf8,
                                    (bool_label(
                                        lightspeed@pipeline@checkpoint:valid(
                                            Corrupted
                                        )
                                    ))/binary>>/binary,
                                "|missing="/utf8>>/binary,
                            Missing_label/binary>>/binary,
                        "|recovered="/utf8>>/binary,
                    Recovered_label/binary>>/binary,
                "|chain="/utf8>>/binary,
            (lightspeed@pipeline@checkpoint:chain_signature(Chain))/binary>>}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 229).
-spec evaluate_compound_failure_matrix_expansion() -> {boolean(), binary()}.
evaluate_compound_failure_matrix_expansion() ->
    Transport = lightspeed@ops@chaos_harness:run_scenario(
        transport_chaos_fault_injection
    ),
    Runtime = lightspeed@ops@chaos_harness:run_scenario(
        runtime_chaos_fault_injection
    ),
    Storage = lightspeed@ops@chaos_harness:run_scenario(
        storage_chaos_fault_injection
    ),
    {Data_passed, Data_signature} = evaluate_data_surface_failure_recovery(),
    Transport_signature = lightspeed@ops@chaos_harness:signature(Transport),
    Runtime_signature = lightspeed@ops@chaos_harness:signature(Runtime),
    Storage_signature = lightspeed@ops@chaos_harness:signature(Storage),
    Passed = ((((((((((lightspeed@ops@chaos_harness:pass_fail_label(Transport)
    =:= <<"pass"/utf8>>)
    andalso lightspeed@ops@chaos_harness:deterministic(Transport))
    andalso (lightspeed@ops@chaos_harness:pass_fail_label(Runtime) =:= <<"pass"/utf8>>))
    andalso lightspeed@ops@chaos_harness:deterministic(Runtime))
    andalso (lightspeed@ops@chaos_harness:pass_fail_label(Storage) =:= <<"pass"/utf8>>))
    andalso lightspeed@ops@chaos_harness:deterministic(Storage))
    andalso Data_passed)
    andalso gleam_stdlib:contains_string(
        Transport_signature,
        <<"heartbeat_timeout"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Runtime_signature,
        <<"session_crashed"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Storage_signature,
        <<"split_brain"/utf8>>
    ))
    andalso gleam_stdlib:contains_string(
        Data_signature,
        <<"missing_stage_checkpoint"/utf8>>
    ),
    {Passed,
        <<<<<<<<<<<<<<"transport="/utf8, Transport_signature/binary>>/binary,
                                "|runtime="/utf8>>/binary,
                            Runtime_signature/binary>>/binary,
                        "|storage="/utf8>>/binary,
                    Storage_signature/binary>>/binary,
                "|data="/utf8>>/binary,
            Data_signature/binary>>}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 437).
-spec determinism_signature() -> binary().
determinism_signature() ->
    {Compound_passed, Compound} = evaluate_compound_failure_matrix_expansion(),
    {Incident_passed, Incidents} = evaluate_deterministic_incident_pack_replay_certification(
        
    ),
    {Mitigation_passed, Mitigation} = evaluate_autopilot_operator_mitigation_integration(
        
    ),
    {Taxonomy_passed, Taxonomy} = evaluate_failure_taxonomy_remediation_coverage(
        
    ),
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"compound="/utf8,
                                                                (bool_label(
                                                                    Compound_passed
                                                                ))/binary>>/binary,
                                                            ":"/utf8>>/binary,
                                                        Compound/binary>>/binary,
                                                    "|incidents="/utf8>>/binary,
                                                (bool_label(Incident_passed))/binary>>/binary,
                                            ":"/utf8>>/binary,
                                        Incidents/binary>>/binary,
                                    "|mitigation="/utf8>>/binary,
                                (bool_label(Mitigation_passed))/binary>>/binary,
                            ":"/utf8>>/binary,
                        Mitigation/binary>>/binary,
                    "|taxonomy="/utf8>>/binary,
                (bool_label(Taxonomy_passed))/binary>>/binary,
            ":"/utf8>>/binary,
        Taxonomy/binary>>.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 429).
-spec evaluate_regression_gate_determinism() -> {boolean(), binary()}.
evaluate_regression_gate_determinism() ->
    First = determinism_signature(),
    Second = determinism_signature(),
    Passed = (First =:= Second) andalso (First /= <<""/utf8>>),
    {Passed, First}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 215).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        compound_failure_matrix_expansion ->
            evaluate_compound_failure_matrix_expansion();

        deterministic_incident_pack_replay_certification ->
            evaluate_deterministic_incident_pack_replay_certification();

        autopilot_operator_mitigation_integration ->
            evaluate_autopilot_operator_mitigation_integration();

        failure_taxonomy_remediation_coverage ->
            evaluate_failure_taxonomy_remediation_coverage();

        regression_gate_determinism ->
            evaluate_regression_gate_determinism()
    end.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 85).
?DOC(" Run one M53 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/chaos_expansion_harness.gleam", 66).
?DOC(" Run all M53 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [compound_failure_matrix_expansion,
            deterministic_incident_pack_replay_certification,
            autopilot_operator_mitigation_integration,
            failure_taxonomy_remediation_coverage,
            regression_gate_determinism],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 101).
?DOC(" M53 failure taxonomy version label.\n").
-spec taxonomy_version_label() -> binary().
taxonomy_version_label() ->
    <<"m53.taxonomy.v"/utf8, (erlang:integer_to_binary(1))/binary>>.

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

        deterministic_incident_pack_replay_certification ->
            <<"deterministic_incident_pack_replay_certification"/utf8>>;

        autopilot_operator_mitigation_integration ->
            <<"autopilot_operator_mitigation_integration"/utf8>>;

        failure_taxonomy_remediation_coverage ->
            <<"failure_taxonomy_remediation_coverage"/utf8>>;

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

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

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

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

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

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

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

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

-file("src/lightspeed/ops/chaos_expansion_harness.gleam", 182).
?DOC(" Deterministic markdown report for M53 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# Chaos Expansion Fixture Report\n\n"/utf8,
                                                                                        "snapshot_version: "/utf8>>/binary,
                                                                                    (erlang:integer_to_binary(
                                                                                        1
                                                                                    ))/binary>>/binary,
                                                                                "\n"/utf8>>/binary,
                                                                            "taxonomy_version: "/utf8>>/binary,
                                                                        (taxonomy_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>>.