src/lightspeed@tooling@documentation_system.erl

-module(lightspeed@tooling@documentation_system).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/tooling/documentation_system.gleam").
-export([bundle/9, m40_bundle/0, m41_bundle/0, m42_bundle/0, m43_bundle/0, m44_bundle/0, m45_bundle/0, m46_bundle/0, reference_bundles/0, milestone/1, module_refs/1, fixture_report/1, fixture_gate/1, module_reference_signature/1, fixture_reference_signature/1, upgrade_troubleshooting_signature/1, signature/1, valid_bundle/1, coherent_system/0, drift_check_contracts/0, versioned_upgrade_paths/0, versioned_troubleshooting_paths/0, evidence_requirements/0, evidence_requirement_signature/0, snapshot_signature/0]).
-export_type([documentation_bundle/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(" Documentation system contracts and evidence model for M46.\n").

-type documentation_bundle() :: {documentation_bundle,
        binary(),
        binary(),
        binary(),
        binary(),
        binary(),
        binary(),
        list(binary()),
        binary(),
        binary()}.

-file("src/lightspeed/tooling/documentation_system.gleam", 25).
?DOC(" Build one documentation bundle.\n").
-spec bundle(
    binary(),
    binary(),
    binary(),
    binary(),
    binary(),
    binary(),
    list(binary()),
    binary(),
    binary()
) -> documentation_bundle().
bundle(
    Milestone,
    Architecture_doc,
    Operations_doc,
    Testing_doc,
    Upgrade_doc,
    Troubleshooting_doc,
    Module_refs,
    Fixture_report,
    Fixture_gate
) ->
    {documentation_bundle,
        Milestone,
        Architecture_doc,
        Operations_doc,
        Testing_doc,
        Upgrade_doc,
        Troubleshooting_doc,
        Module_refs,
        Fixture_report,
        Fixture_gate}.

-file("src/lightspeed/tooling/documentation_system.gleam", 50).
?DOC(" M40 bundle.\n").
-spec m40_bundle() -> documentation_bundle().
m40_bundle() ->
    bundle(
        <<"m40"/utf8>>,
        <<"docs/production_app_operations_kit.md"/utf8>>,
        <<"docs/operator_runbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_runbook.md"/utf8>>,
        [<<"lightspeed/ops/operations_kit"/utf8>>,
            <<"lightspeed/ops/operations_kit_harness"/utf8>>],
        <<"docs/reports/operations_kit_fixture_latest.md"/utf8>>,
        <<"make operations-kit-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 68).
?DOC(" M41 bundle.\n").
-spec m41_bundle() -> documentation_bundle().
m41_bundle() ->
    bundle(
        <<"m41"/utf8>>,
        <<"docs/ecosystem_adapter_sdk.md"/utf8>>,
        <<"docs/operator_adapter_sdk_playbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_adapter_sdk_playbook.md"/utf8>>,
        [<<"lightspeed/integration/adapter_sdk"/utf8>>,
            <<"lightspeed/ops/adapter_sdk_harness"/utf8>>],
        <<"docs/reports/adapter_sdk_fixture_latest.md"/utf8>>,
        <<"make adapter-sdk-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 86).
?DOC(" M42 bundle.\n").
-spec m42_bundle() -> documentation_bundle().
m42_bundle() ->
    bundle(
        <<"m42"/utf8>>,
        <<"docs/replay_time_travel_diagnostics.md"/utf8>>,
        <<"docs/operator_replay_diagnostics_playbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_replay_diagnostics_playbook.md"/utf8>>,
        [<<"lightspeed/ops/replay_diagnostics"/utf8>>,
            <<"lightspeed/ops/replay_diagnostics_harness"/utf8>>],
        <<"docs/reports/replay_diagnostics_fixture_latest.md"/utf8>>,
        <<"make replay-diagnostics-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 104).
?DOC(" M43 bundle.\n").
-spec m43_bundle() -> documentation_bundle().
m43_bundle() ->
    bundle(
        <<"m43"/utf8>>,
        <<"docs/protocol_contract_fuzz_harness.md"/utf8>>,
        <<"docs/operator_protocol_fuzz_playbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_protocol_fuzz_playbook.md"/utf8>>,
        [<<"lightspeed/ops/protocol_contract_harness"/utf8>>,
            <<"lightspeed/protocol"/utf8>>],
        <<"docs/reports/protocol_contract_fixture_latest.md"/utf8>>,
        <<"make protocol-contract-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 122).
?DOC(" M44 bundle.\n").
-spec m44_bundle() -> documentation_bundle().
m44_bundle() ->
    bundle(
        <<"m44"/utf8>>,
        <<"docs/ai_native_application_workflow.md"/utf8>>,
        <<"docs/operator_ai_workflow_playbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_ai_workflow_playbook.md"/utf8>>,
        [<<"lightspeed/tooling/ai_workflow"/utf8>>,
            <<"lightspeed/ops/ai_workflow_harness"/utf8>>],
        <<"docs/reports/ai_workflow_fixture_latest.md"/utf8>>,
        <<"make ai-workflow-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 140).
?DOC(" M45 bundle.\n").
-spec m45_bundle() -> documentation_bundle().
m45_bundle() ->
    bundle(
        <<"m45"/utf8>>,
        <<"docs/convention_over_configuration_core_profile.md"/utf8>>,
        <<"docs/operator_convention_profile_playbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_convention_profile_playbook.md"/utf8>>,
        [<<"lightspeed/platform/convention_profile"/utf8>>,
            <<"lightspeed/ops/convention_profile_harness"/utf8>>],
        <<"docs/reports/convention_profile_fixture_latest.md"/utf8>>,
        <<"make convention-profile-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 158).
?DOC(" M46 bundle.\n").
-spec m46_bundle() -> documentation_bundle().
m46_bundle() ->
    bundle(
        <<"m46"/utf8>>,
        <<"docs/documentation_system_parity_excellence.md"/utf8>>,
        <<"docs/operator_documentation_system_playbook.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"docs/operator_documentation_system_playbook.md"/utf8>>,
        [<<"lightspeed/tooling/documentation_system"/utf8>>,
            <<"lightspeed/ops/documentation_system_harness"/utf8>>],
        <<"docs/reports/documentation_system_fixture_latest.md"/utf8>>,
        <<"make documentation-system-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 176).
?DOC(" All reference bundles for M40–M46.\n").
-spec reference_bundles() -> list(documentation_bundle()).
reference_bundles() ->
    [m40_bundle(),
        m41_bundle(),
        m42_bundle(),
        m43_bundle(),
        m44_bundle(),
        m45_bundle(),
        m46_bundle()].

-file("src/lightspeed/tooling/documentation_system.gleam", 189).
?DOC(" Bundle milestone accessor.\n").
-spec milestone(documentation_bundle()) -> binary().
milestone(Bundle) ->
    erlang:element(2, Bundle).

-file("src/lightspeed/tooling/documentation_system.gleam", 194).
?DOC(" Bundle module refs accessor.\n").
-spec module_refs(documentation_bundle()) -> list(binary()).
module_refs(Bundle) ->
    erlang:element(8, Bundle).

-file("src/lightspeed/tooling/documentation_system.gleam", 199).
?DOC(" Bundle fixture report accessor.\n").
-spec fixture_report(documentation_bundle()) -> binary().
fixture_report(Bundle) ->
    erlang:element(9, Bundle).

-file("src/lightspeed/tooling/documentation_system.gleam", 204).
?DOC(" Bundle fixture gate accessor.\n").
-spec fixture_gate(documentation_bundle()) -> binary().
fixture_gate(Bundle) ->
    erlang:element(10, Bundle).

-file("src/lightspeed/tooling/documentation_system.gleam", 444).
-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/tooling/documentation_system.gleam", 209).
?DOC(" Stable module-reference signature.\n").
-spec module_reference_signature(documentation_bundle()) -> binary().
module_reference_signature(Bundle) ->
    <<<<(erlang:element(2, Bundle))/binary, ":"/utf8>>/binary,
        (join_with(<<","/utf8>>, erlang:element(8, Bundle)))/binary>>.

-file("src/lightspeed/tooling/documentation_system.gleam", 214).
?DOC(" Stable fixture-reference signature.\n").
-spec fixture_reference_signature(documentation_bundle()) -> binary().
fixture_reference_signature(Bundle) ->
    <<<<<<<<(erlang:element(2, Bundle))/binary, ":"/utf8>>/binary,
                (erlang:element(9, Bundle))/binary>>/binary,
            ":"/utf8>>/binary,
        (erlang:element(10, Bundle))/binary>>.

-file("src/lightspeed/tooling/documentation_system.gleam", 219).
?DOC(" Stable upgrade/troubleshooting signature.\n").
-spec upgrade_troubleshooting_signature(documentation_bundle()) -> binary().
upgrade_troubleshooting_signature(Bundle) ->
    <<<<<<<<(erlang:element(2, Bundle))/binary, ":upgrade="/utf8>>/binary,
                (erlang:element(6, Bundle))/binary>>/binary,
            ":troubleshooting="/utf8>>/binary,
        (erlang:element(7, Bundle))/binary>>.

-file("src/lightspeed/tooling/documentation_system.gleam", 230).
?DOC(" Stable bundle signature.\n").
-spec signature(documentation_bundle()) -> binary().
signature(Bundle) ->
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"milestone="/utf8,
                                                                        (erlang:element(
                                                                            2,
                                                                            Bundle
                                                                        ))/binary>>/binary,
                                                                    "|architecture="/utf8>>/binary,
                                                                (erlang:element(
                                                                    3,
                                                                    Bundle
                                                                ))/binary>>/binary,
                                                            "|operations="/utf8>>/binary,
                                                        (erlang:element(
                                                            4,
                                                            Bundle
                                                        ))/binary>>/binary,
                                                    "|testing="/utf8>>/binary,
                                                (erlang:element(5, Bundle))/binary>>/binary,
                                            "|upgrade="/utf8>>/binary,
                                        (erlang:element(6, Bundle))/binary>>/binary,
                                    "|troubleshooting="/utf8>>/binary,
                                (erlang:element(7, Bundle))/binary>>/binary,
                            "|module_refs="/utf8>>/binary,
                        (join_with(<<","/utf8>>, erlang:element(8, Bundle)))/binary>>/binary,
                    "|fixture_report="/utf8>>/binary,
                (erlang:element(9, Bundle))/binary>>/binary,
            "|fixture_gate="/utf8>>/binary,
        (erlang:element(10, Bundle))/binary>>.

-file("src/lightspeed/tooling/documentation_system.gleam", 343).
-spec gate_command_valid(binary()) -> boolean().
gate_command_valid(Command) ->
    gleam_stdlib:string_starts_with(Command, <<"make "/utf8>>) andalso gleam_stdlib:string_ends_with(
        Command,
        <<"-fixture-gate"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 339).
-spec report_path_valid(binary()) -> boolean().
report_path_valid(Path) ->
    gleam_stdlib:string_starts_with(Path, <<"docs/reports/"/utf8>>) andalso gleam_stdlib:string_ends_with(
        Path,
        <<".md"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 364).
-spec module_ref_valid(binary()) -> boolean().
module_ref_valid(Value) ->
    gleam_stdlib:string_starts_with(Value, <<"lightspeed/"/utf8>>).

-file("src/lightspeed/tooling/documentation_system.gleam", 356).
-spec module_refs_valid_continue(list(binary())) -> boolean().
module_refs_valid_continue(Values) ->
    case Values of
        [] ->
            true;

        [Value | Rest] ->
            module_ref_valid(Value) andalso module_refs_valid_continue(Rest)
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 348).
-spec module_refs_valid(list(binary())) -> boolean().
module_refs_valid(Values) ->
    case Values of
        [] ->
            false;

        [Value | Rest] ->
            module_ref_valid(Value) andalso module_refs_valid_continue(Rest)
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 335).
-spec doc_path_valid(binary()) -> boolean().
doc_path_valid(Path) ->
    gleam_stdlib:string_starts_with(Path, <<"docs/"/utf8>>) andalso gleam_stdlib:string_ends_with(
        Path,
        <<".md"/utf8>>
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 331).
-spec milestone_valid(binary()) -> boolean().
milestone_valid(Value) ->
    gleam_stdlib:string_starts_with(Value, <<"m"/utf8>>).

-file("src/lightspeed/tooling/documentation_system.gleam", 252).
?DOC(" Check one bundle is coherent.\n").
-spec valid_bundle(documentation_bundle()) -> boolean().
valid_bundle(Bundle) ->
    (((((((milestone_valid(erlang:element(2, Bundle)) andalso doc_path_valid(
        erlang:element(3, Bundle)
    ))
    andalso doc_path_valid(erlang:element(4, Bundle)))
    andalso (erlang:element(5, Bundle) =:= <<"docs/testing.md"/utf8>>))
    andalso (erlang:element(6, Bundle) =:= <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>))
    andalso doc_path_valid(erlang:element(7, Bundle)))
    andalso module_refs_valid(erlang:element(8, Bundle)))
    andalso report_path_valid(erlang:element(9, Bundle)))
    andalso gate_command_valid(erlang:element(10, Bundle)).

-file("src/lightspeed/tooling/documentation_system.gleam", 398).
-spec contains(list(binary()), binary()) -> boolean().
contains(Values, Expected) ->
    case Values of
        [] ->
            false;

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

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

-file("src/lightspeed/tooling/documentation_system.gleam", 387).
-spec unique_string_list(list(binary()), list(binary())) -> boolean().
unique_string_list(Values, Seen) ->
    case Values of
        [] ->
            true;

        [Value | Rest] ->
            case contains(Seen, Value) of
                true ->
                    false;

                false ->
                    unique_string_list(Rest, [Value | Seen])
            end
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 383).
-spec unique_gates(list(documentation_bundle())) -> boolean().
unique_gates(Values) ->
    unique_string_list(gleam@list:map(Values, fun fixture_gate/1), []).

-file("src/lightspeed/tooling/documentation_system.gleam", 379).
-spec unique_reports(list(documentation_bundle())) -> boolean().
unique_reports(Values) ->
    unique_string_list(gleam@list:map(Values, fun fixture_report/1), []).

-file("src/lightspeed/tooling/documentation_system.gleam", 375).
-spec milestones(list(documentation_bundle())) -> list(binary()).
milestones(Values) ->
    gleam@list:map(Values, fun milestone/1).

-file("src/lightspeed/tooling/documentation_system.gleam", 368).
-spec bundles_valid(list(documentation_bundle())) -> boolean().
bundles_valid(Values) ->
    case Values of
        [] ->
            true;

        [Value | Rest] ->
            valid_bundle(Value) andalso bundles_valid(Rest)
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 265).
?DOC(" Check all bundles are coherent and ordered.\n").
-spec coherent_system() -> boolean().
coherent_system() ->
    ((bundles_valid(reference_bundles()) andalso (milestones(
        reference_bundles()
    )
    =:= [<<"m40"/utf8>>,
        <<"m41"/utf8>>,
        <<"m42"/utf8>>,
        <<"m43"/utf8>>,
        <<"m44"/utf8>>,
        <<"m45"/utf8>>,
        <<"m46"/utf8>>]))
    andalso unique_reports(reference_bundles()))
    andalso unique_gates(reference_bundles()).

-file("src/lightspeed/tooling/documentation_system.gleam", 436).
-spec bundles_have_fixture_gate(list(documentation_bundle())) -> boolean().
bundles_have_fixture_gate(Values) ->
    case Values of
        [] ->
            true;

        [Value | Rest] ->
            gate_command_valid(erlang:element(10, Value)) andalso bundles_have_fixture_gate(
                Rest
            )
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 428).
-spec bundles_use_testing_doc(list(documentation_bundle())) -> boolean().
bundles_use_testing_doc(Values) ->
    case Values of
        [] ->
            true;

        [Value | Rest] ->
            (erlang:element(5, Value) =:= <<"docs/testing.md"/utf8>>) andalso bundles_use_testing_doc(
                Rest
            )
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 417).
-spec has_harness_module(list(binary())) -> boolean().
has_harness_module(Values) ->
    case Values of
        [] ->
            false;

        [Value | Rest] ->
            case gleam_stdlib:contains_string(Value, <<"_harness"/utf8>>) of
                true ->
                    true;

                false ->
                    has_harness_module(Rest)
            end
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 409).
-spec bundles_have_harness_module(list(documentation_bundle())) -> boolean().
bundles_have_harness_module(Values) ->
    case Values of
        [] ->
            true;

        [Value | Rest] ->
            has_harness_module(erlang:element(8, Value)) andalso bundles_have_harness_module(
                Rest
            )
    end.

-file("src/lightspeed/tooling/documentation_system.gleam", 274).
?DOC(" True when all bundles expose stable module and fixture drift-check surfaces.\n").
-spec drift_check_contracts() -> boolean().
drift_check_contracts() ->
    (bundles_have_harness_module(reference_bundles()) andalso bundles_use_testing_doc(
        reference_bundles()
    ))
    andalso bundles_have_fixture_gate(reference_bundles()).

-file("src/lightspeed/tooling/documentation_system.gleam", 281).
?DOC(" Versioned milestone upgrade paths.\n").
-spec versioned_upgrade_paths() -> list(binary()).
versioned_upgrade_paths() ->
    gleam@list:map(
        reference_bundles(),
        fun(Bundle) ->
            <<<<(erlang:element(2, Bundle))/binary, ":upgrade_doc="/utf8>>/binary,
                (erlang:element(6, Bundle))/binary>>
        end
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 288).
?DOC(" Versioned milestone troubleshooting paths.\n").
-spec versioned_troubleshooting_paths() -> list(binary()).
versioned_troubleshooting_paths() ->
    gleam@list:map(
        reference_bundles(),
        fun(Bundle) ->
            <<<<(erlang:element(2, Bundle))/binary,
                    ":troubleshooting_doc="/utf8>>/binary,
                (erlang:element(7, Bundle))/binary>>
        end
    ).

-file("src/lightspeed/tooling/documentation_system.gleam", 295).
?DOC(" Required documentation evidence when milestone contracts change.\n").
-spec evidence_requirements() -> list(binary()).
evidence_requirements() ->
    [<<"ROADMAP.md"/utf8>>,
        <<"MILESTONES.md"/utf8>>,
        <<"CHANGELOG.md"/utf8>>,
        <<"docs/testing.md"/utf8>>,
        <<"docs/operator_runbook.md"/utf8>>,
        <<"docs/milestone_upgrade_troubleshooting.md"/utf8>>,
        <<"rfcs/"/utf8>>,
        <<"adrs/"/utf8>>].

-file("src/lightspeed/tooling/documentation_system.gleam", 309).
?DOC(" Stable evidence-requirement signature.\n").
-spec evidence_requirement_signature() -> binary().
evidence_requirement_signature() ->
    join_with(<<";"/utf8>>, evidence_requirements()).

-file("src/lightspeed/tooling/documentation_system.gleam", 314).
?DOC(" Deterministic snapshot signature for M46 fixture drift gates.\n").
-spec snapshot_signature() -> binary().
snapshot_signature() ->
    Bundle_signatures = gleam@list:map(reference_bundles(), fun signature/1),
    Upgrades = versioned_upgrade_paths(),
    Troubleshooting = versioned_troubleshooting_paths(),
    <<<<<<<<<<<<<<<<<<"m46.docs.v"/utf8, (erlang:integer_to_binary(1))/binary>>/binary,
                                    "|bundles="/utf8>>/binary,
                                (join_with(<<";"/utf8>>, Bundle_signatures))/binary>>/binary,
                            "|upgrades="/utf8>>/binary,
                        (join_with(<<";"/utf8>>, Upgrades))/binary>>/binary,
                    "|troubleshooting="/utf8>>/binary,
                (join_with(<<";"/utf8>>, Troubleshooting))/binary>>/binary,
            "|evidence="/utf8>>/binary,
        (evidence_requirement_signature())/binary>>.