src/lightspeed@ops@ai_workflow_harness.erl

-module(lightspeed@ops@ai_workflow_harness).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/ops/ai_workflow_harness.gleam").
-export([run_scenario/1, run_matrix/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]).

-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 AI-native workflow harness for M44.\n").

-type scenario() :: ai_assisted_scaffold_contracts |
    deterministic_policy_guardrails |
    generator_template_track_verification |
    human_in_loop_review_boundaries.

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

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

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 362).
-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/ai_workflow_harness.gleam", 351).
-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/ai_workflow_harness.gleam", 381).
-spec bool_label(boolean()) -> binary().
bool_label(Value) ->
    case Value of
        true ->
            <<"true"/utf8>>;

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

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 338).
-spec has_required_human_boundary(
    list(lightspeed@tooling@ai_workflow:review_boundary())
) -> boolean().
has_required_human_boundary(Boundaries) ->
    case Boundaries of
        [] ->
            false;

        [Boundary | Rest] ->
            case (erlang:element(2, Boundary) =:= <<"human_review"/utf8>>)
            andalso erlang:element(3, Boundary) of
                true ->
                    true;

                false ->
                    has_required_human_boundary(Rest)
            end
    end.

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 373).
-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/ai_workflow_harness.gleam", 290).
-spec evaluate_human_in_loop_review_boundaries() -> {boolean(), binary()}.
evaluate_human_in_loop_review_boundaries() ->
    Refactor = lightspeed@tooling@ai_workflow:refactor_plan(
        <<"component/template_ergonomics"/utf8>>,
        <<"apply optional accessor normalization"/utf8>>
    ),
    Template = lightspeed@tooling@ai_workflow:template_plan(
        <<"component/template_ergonomics"/utf8>>,
        <<"improve migration hint readability"/utf8>>
    ),
    Refactor_eval = lightspeed@tooling@ai_workflow:evaluate(Refactor),
    Template_eval = lightspeed@tooling@ai_workflow:evaluate(Template),
    Refactor_boundaries = join_with(
        <<";"/utf8>>,
        gleam@list:map(
            erlang:element(6, Refactor),
            fun lightspeed@tooling@ai_workflow:boundary_label/1
        )
    ),
    Template_boundaries = join_with(
        <<";"/utf8>>,
        gleam@list:map(
            erlang:element(6, Template),
            fun lightspeed@tooling@ai_workflow:boundary_label/1
        )
    ),
    Deterministic_plan_signature = lightspeed@tooling@ai_workflow:plan_signature(
        Refactor
    )
    =:= lightspeed@tooling@ai_workflow:plan_signature(
        lightspeed@tooling@ai_workflow:refactor_plan(
            <<"component/template_ergonomics"/utf8>>,
            <<"apply optional accessor normalization"/utf8>>
        )
    ),
    Passed = (((has_required_human_boundary(erlang:element(6, Refactor)) andalso has_required_human_boundary(
        erlang:element(6, Template)
    ))
    andalso Deterministic_plan_signature)
    andalso lightspeed@tooling@ai_workflow:passed(Refactor_eval))
    andalso lightspeed@tooling@ai_workflow:passed(Template_eval),
    {Passed,
        <<<<<<<<<<"refactor_boundaries="/utf8, Refactor_boundaries/binary>>/binary,
                        "|template_boundaries="/utf8>>/binary,
                    Template_boundaries/binary>>/binary,
                "|deterministic_plan_signature="/utf8>>/binary,
            (bool_label(Deterministic_plan_signature))/binary>>}.

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 245).
-spec evaluate_generator_template_track_verification() -> {boolean(), binary()}.
evaluate_generator_template_track_verification() ->
    Scaffold = lightspeed@tooling@ai_workflow:evaluate(
        lightspeed@tooling@ai_workflow:scaffold_plan(
            <<"demo_app"/utf8>>,
            <<"posts"/utf8>>
        )
    ),
    Template = lightspeed@tooling@ai_workflow:evaluate(
        lightspeed@tooling@ai_workflow:template_plan(
            <<"template_ergonomics"/utf8>>,
            <<"strengthen alias migration hints and slot defaults"/utf8>>
        )
    ),
    Generator_signature = lightspeed@ops@generator_harness:snapshot_signature(),
    Template_signature = lightspeed@ops@template_ergonomics_harness:snapshot_signature(
        
    ),
    Generator_track_ok = lightspeed@tooling@ai_workflow:satisfies_track_requirements(
        Scaffold,
        generator_track,
        Generator_signature
    ),
    Template_track_ok = lightspeed@tooling@ai_workflow:satisfies_track_requirements(
        Template,
        template_track,
        Template_signature
    ),
    Passed = ((((lightspeed@tooling@ai_workflow:passed(Scaffold) andalso lightspeed@tooling@ai_workflow:passed(
        Template
    ))
    andalso Generator_track_ok)
    andalso Template_track_ok)
    andalso gleam_stdlib:string_starts_with(
        Generator_signature,
        <<"m25.snapshot.v"/utf8>>
    ))
    andalso gleam_stdlib:string_starts_with(
        Template_signature,
        <<"m39.snapshot.v"/utf8>>
    ),
    {Passed,
        <<<<<<<<<<<<<<"generator_track="/utf8,
                                    (bool_label(Generator_track_ok))/binary>>/binary,
                                "|template_track="/utf8>>/binary,
                            (bool_label(Template_track_ok))/binary>>/binary,
                        "|generator_signature="/utf8>>/binary,
                    Generator_signature/binary>>/binary,
                "|template_signature="/utf8>>/binary,
            Template_signature/binary>>}.

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 210).
-spec evaluate_deterministic_policy_guardrails() -> {boolean(), binary()}.
evaluate_deterministic_policy_guardrails() ->
    Refactor_plan = lightspeed@tooling@ai_workflow:refactor_plan(
        <<"transport/matrix"/utf8>>,
        <<"extract order-violation checks with isolated tests"/utf8>>
    ),
    Compliant = lightspeed@tooling@ai_workflow:evaluate(Refactor_plan),
    Invalid_scaffold_plan = {plan,
        <<"ai_scaffold_invalid"/utf8>>,
        scaffold_generation,
        <<"invalid command shape"/utf8>>,
        [<<"gen.live"/utf8>>, <<"demo_app"/utf8>>],
        []},
    Rejected = lightspeed@tooling@ai_workflow:evaluate(Invalid_scaffold_plan),
    Passed = ((lightspeed@tooling@ai_workflow:passed(Compliant) andalso (lightspeed@tooling@ai_workflow:policy_score(
        Compliant
    )
    >= 80))
    andalso (lightspeed@tooling@ai_workflow:passed(Rejected) =:= false))
    andalso gleam_stdlib:contains_string(
        lightspeed@tooling@ai_workflow:signature(Rejected),
        <<"parse_error:missing_arguments:gen.live"/utf8>>
    ),
    {Passed,
        <<<<<<"compliant="/utf8,
                    (lightspeed@tooling@ai_workflow:signature(Compliant))/binary>>/binary,
                "|rejected="/utf8>>/binary,
            (lightspeed@tooling@ai_workflow:signature(Rejected))/binary>>}.

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 187).
-spec evaluate_ai_assisted_scaffold_contracts() -> {boolean(), binary()}.
evaluate_ai_assisted_scaffold_contracts() ->
    Plan = lightspeed@tooling@ai_workflow:scaffold_plan(
        <<"demo_app"/utf8>>,
        <<"posts"/utf8>>
    ),
    Evaluation = lightspeed@tooling@ai_workflow:evaluate(Plan),
    Passed = ((lightspeed@tooling@ai_workflow:passed(Evaluation) andalso (lightspeed@tooling@ai_workflow:policy_score(
        Evaluation
    )
    >= 80))
    andalso (lightspeed@tooling@ai_workflow:quality_score(Evaluation) >= 80))
    andalso lightspeed@tooling@ai_workflow:satisfies_track_requirements(
        Evaluation,
        generator_track,
        lightspeed@ops@generator_harness:snapshot_signature()
    ),
    {Passed,
        <<<<<<"plan="/utf8,
                    (lightspeed@tooling@ai_workflow:plan_signature(Plan))/binary>>/binary,
                "|evaluation="/utf8>>/binary,
            (lightspeed@tooling@ai_workflow:signature(Evaluation))/binary>>}.

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 177).
-spec evaluate(scenario()) -> {boolean(), binary()}.
evaluate(Scenario) ->
    case Scenario of
        ai_assisted_scaffold_contracts ->
            evaluate_ai_assisted_scaffold_contracts();

        deterministic_policy_guardrails ->
            evaluate_deterministic_policy_guardrails();

        generator_template_track_verification ->
            evaluate_generator_template_track_verification();

        human_in_loop_review_boundaries ->
            evaluate_human_in_loop_review_boundaries()
    end.

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 58).
?DOC(" Run one M44 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/ai_workflow_harness.gleam", 40).
?DOC(" Run all M44 scenarios.\n").
-spec run_matrix() -> report().
run_matrix() ->
    Outcomes = begin
        _pipe = [ai_assisted_scaffold_contracts,
            deterministic_policy_guardrails,
            generator_template_track_verification,
            human_in_loop_review_boundaries],
        gleam@list:map(_pipe, fun run_scenario/1)
    end,
    {report, Outcomes, count_failed(Outcomes), count_nondeterministic(Outcomes)}.

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

        deterministic_policy_guardrails ->
            <<"deterministic_policy_guardrails"/utf8>>;

        generator_template_track_verification ->
            <<"generator_template_track_verification"/utf8>>;

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

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

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

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

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

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

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

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

-file("src/lightspeed/ops/ai_workflow_harness.gleam", 147).
?DOC(" Deterministic markdown report for M44 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,
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"# AI Workflow Fixture Report\n\n"/utf8,
                                                                            "snapshot_version: "/utf8>>/binary,
                                                                        (erlang:integer_to_binary(
                                                                            1
                                                                        ))/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>>.