import { cli_locale } from "std/cli/localization"
import { cli_message } from "std/cli/localization/models/lora_plan"
import {
render_peft_save_policy,
render_probe_command_templates,
render_required_probe_cases,
render_tool_catalog_contract,
render_trainer_identity_check,
} from "std/cli/models/lora_render"
/**
* `harn models lora plan` renderer.
*
* The dispatch shim resolves model aliases, provider capabilities, and catalog
* launch facts in Rust. This Harn source owns the portable training/eval
* recipe and the human/JSON presentation.
*
* Inputs (from the dispatch shim):
* HARN_MODELS_LORA_PLAN_PAYLOAD_JSON - compact report JSON.
* HARN_MODELS_LORA_PLAN_PAYLOAD_PRETTY - pretty report JSON.
* HARN_OUTPUT_JSON - "1" for JSON, else human text.
*/
import {
cli_json_envelope,
nonempty_string,
print_list,
render_command,
safe_bool,
safe_dict,
safe_int_string,
safe_list,
safe_number_string,
safe_string,
} from "std/cli/render"
fn __render_human(__cli_locale: string, stdio: HarnessStdio, report: dict) {
const base = safe_dict(report["base"])
const request = safe_dict(report["request"])
const tool_calling = safe_dict(report["tool_calling"])
const training = safe_dict(report["training"])
const training_contract = safe_dict(training["contract"])
const peft_save_policy = safe_dict(training_contract["peft_save_policy"])
const precision = safe_dict(report["precision"])
const template = safe_dict(report["template"])
const data = safe_dict(report["data"])
const corpus_refresh = safe_dict(report["corpus_refresh"])
const evaluation = safe_dict(report["evaluation"])
const evidence = safe_dict(evaluation["evidence_contract"])
const batch_ready = safe_dict(evidence["batch_ready"])
const serving = safe_dict(report["serving"])
const launch = safe_dict(report["launch"])
const warnings = safe_list(report["warnings"])
const base_id = safe_string(base["id"], "")
const provider = safe_string(base["provider"], "")
stdio.println(
cli_message(
__cli_locale,
"LoRA plan for {0} via {1}",
[to_string(base_id), to_string(provider)],
),
)
stdio.println(
cli_message(
__cli_locale,
" base selector: {0}",
[to_string(safe_string(base["selector"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" tool format: {0} (requested {1})",
[
to_string(safe_string(request["effective_tool_format"], "")),
to_string(safe_string(request["requested_tool_format"], "auto")),
],
),
)
const correction = safe_string(request["tool_format_correction"], "")
if correction != "" {
stdio.println(
cli_message(__cli_locale, " tool format correction: {0}", [to_string(correction)]),
)
}
const native_tools = if safe_bool(tool_calling["native_tools"], false) {
cli_message(__cli_locale, "yes", [])
} else {
cli_message(__cli_locale, "no", [])
}
stdio.println(
cli_message(
__cli_locale,
" native tools: {0}, preferred: {1}",
[
to_string(native_tools),
to_string(nonempty_string(tool_calling["preferred_tool_format"], "unset")),
],
),
)
stdio.println(
cli_message(
__cli_locale,
" training: {0} + {1}",
[
to_string(safe_string(request["method"], "")),
to_string(safe_string(training["adapter_type"], "")),
],
),
)
stdio.println(
cli_message(__cli_locale, " trainer: {0}", [to_string(safe_string(training["trainer"], ""))]),
)
render_trainer_identity_check(__cli_locale, stdio, training["trainer_identity"])
stdio.println(
cli_message(
__cli_locale,
" LoRA hparams: rank={0} alpha={1} dropout={2}",
[
to_string(safe_int_string(training["rank"], "")),
to_string(safe_int_string(training["alpha"], "")),
to_string(safe_number_string(training["dropout"], "")),
],
),
)
stdio.println(
cli_message(
__cli_locale,
" quantization: {0}",
[to_string(safe_string(training["quantization"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" training base precision: {0}",
[to_string(safe_string(precision["training_base_precision"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" training compute precision: {0}",
[to_string(safe_string(precision["training_compute_precision"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" adapter precision: {0}",
[to_string(safe_string(precision["adapter_weight_precision"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" serving base precision: {0}",
[to_string(safe_string(precision["serving_base_precision"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" precision policy: {0}",
[to_string(safe_string(precision["compatibility_policy"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" loss scope: {0}",
[to_string(safe_string(training["loss_scope"], ""))],
),
)
stdio.println(
cli_message(__cli_locale, " packing: {0}", [to_string(safe_string(training["packing"], ""))]),
)
const target_module_contract = safe_dict(training["target_modules"])
const target_modules = safe_list(target_module_contract["modules"])
if len(target_modules) > 0 {
stdio.println(
cli_message(
__cli_locale,
" target modules: {0}",
[to_string(join(target_modules.map({ item -> safe_string(item, "") }), ", "))],
),
)
stdio.println(
cli_message(
__cli_locale,
" target module policy: {0}",
[to_string(safe_string(target_module_contract["policy"], ""))],
),
)
}
stdio.println(
cli_message(
__cli_locale,
" assistant mask: {0}",
[to_string(safe_string(training_contract["assistant_mask_policy"], ""))],
),
)
render_peft_save_policy(__cli_locale, stdio, peft_save_policy)
stdio.println(
cli_message(
__cli_locale,
" parser owner: {0}",
[to_string(safe_string(training_contract["tool_parser_owner"], ""))],
),
)
render_tool_catalog_contract(__cli_locale, stdio, safe_dict(training_contract["tool_catalog"]))
stdio.println(
cli_message(
__cli_locale,
" split policy: {0}",
[to_string(safe_string(training_contract["dataset_split_policy"], ""))],
),
)
stdio.println(
cli_message(__cli_locale, " template: {0}", [to_string(safe_string(template["name"], ""))]),
)
stdio.println(
cli_message(
__cli_locale,
" template source: {0}",
[to_string(safe_string(template["source"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" supervised target: {0}",
[to_string(safe_string(template["supervised_target"], ""))],
),
)
const corpus = safe_string(request["corpus"], "")
if corpus != "" {
stdio.println(cli_message(__cli_locale, " corpus: {0}", [to_string(corpus)]))
}
stdio.println(
cli_message(
__cli_locale,
" corpus strategy: {0} (requested {1})",
[
to_string(safe_string(request["effective_corpus_strategy"], "")),
to_string(safe_string(request["requested_corpus_strategy"], "auto")),
],
),
)
const teacher = safe_dict(request["teacher"])
const teacher_id = safe_string(teacher["id"], "")
if teacher_id != "" {
stdio.println(
cli_message(
__cli_locale,
" teacher: {0} via {1}",
[to_string(teacher_id), to_string(safe_string(teacher["provider"], ""))],
),
)
}
stdio.println(
cli_message(
__cli_locale,
" dataset format: {0}",
[to_string(safe_string(data["dataset_format"], ""))],
),
)
print_list(
stdio,
cli_message(__cli_locale, "template requirements", []),
safe_list(template["requirements"]),
)
print_list(
stdio,
cli_message(__cli_locale, "stop sequences", []),
safe_list(template["stop_sequences"]),
)
print_list(stdio, cli_message(__cli_locale, "template notes", []), safe_list(template["notes"]))
print_list(
stdio,
cli_message(__cli_locale, "required columns", []),
safe_list(data["required_columns"]),
)
print_list(stdio, cli_message(__cli_locale, "data validation", []), safe_list(data["validation"]))
print_list(
stdio,
cli_message(__cli_locale, "corpus refresh", []),
safe_list(corpus_refresh["generation_notes"]),
)
print_list(
stdio,
cli_message(__cli_locale, "provenance manifest", []),
safe_list(corpus_refresh["provenance_manifest_fields"]),
)
print_list(
stdio,
cli_message(__cli_locale, "hard negatives", []),
safe_list(corpus_refresh["hard_negative_slices"]),
)
print_list(
stdio,
cli_message(__cli_locale, "corpus gates", []),
safe_list(corpus_refresh["acceptance_gates"]),
)
const model_aware = safe_dict(corpus_refresh["model_aware_selection"])
const objective = safe_string(model_aware["objective"], "")
if objective != "" {
stdio.println(cli_message(__cli_locale, " model-aware selection: {0}", [to_string(objective)]))
}
print_list(
stdio,
cli_message(__cli_locale, "difficulty signals", []),
safe_list(model_aware["difficulty_signals"]),
)
print_list(
stdio,
cli_message(__cli_locale, "sampling policy", []),
safe_list(model_aware["sampling_policy"]),
)
print_list(
stdio,
cli_message(__cli_locale, "refinement loop", []),
safe_list(model_aware["refinement_loop"]),
)
print_list(
stdio,
cli_message(__cli_locale, "selection stop conditions", []),
safe_list(model_aware["stop_conditions"]),
)
print_list(
stdio,
cli_message(__cli_locale, "trainer contract", []),
safe_list(training["trainer_contract"]),
)
print_list(
stdio,
cli_message(__cli_locale, "precision gates", []),
safe_list(precision["promotion_gates"]),
)
print_list(stdio, cli_message(__cli_locale, "training notes", []), safe_list(training["notes"]))
stdio.println(
cli_message(
__cli_locale,
" holdout policy: {0}",
[to_string(safe_string(evaluation["holdout_policy"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" minimum trials: {0}",
[to_string(safe_int_string(evaluation["minimum_trials"], "0"))],
),
)
stdio.println(
cli_message(
__cli_locale,
" promotion id: {0}",
[to_string(safe_string(evidence["promotion_id"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" comparison baseline: {0}",
[to_string(safe_string(evaluation["comparison_baseline"], ""))],
),
)
print_list(
stdio,
cli_message(__cli_locale, "required metrics", []),
safe_list(evaluation["required_metrics"]),
)
print_list(stdio, cli_message(__cli_locale, "eval gates", []), safe_list(evaluation["gates"]))
print_list(
stdio,
cli_message(__cli_locale, "required receipts", []),
safe_list(evidence["required_receipts"]),
)
render_required_probe_cases(__cli_locale, stdio, evidence["required_probe_cases"])
render_probe_command_templates(__cli_locale, stdio, evidence["probe_command_templates"])
print_list(
stdio,
cli_message(__cli_locale, "optional batch receipts", []),
safe_list(evidence["optional_batch_receipts"]),
)
print_list(
stdio,
cli_message(__cli_locale, "batch group by", []),
safe_list(batch_ready["group_by"]),
)
stdio.println(
cli_message(
__cli_locale,
" adapter binding: {0}",
[to_string(safe_string(serving["adapter_binding"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" LoRA module format: {0}",
[to_string(safe_string(serving["lora_module_value_format"], ""))],
),
)
stdio.println(
cli_message(
__cli_locale,
" request model: {0}",
[to_string(safe_string(serving["request_model"], ""))],
),
)
print_list(
stdio,
cli_message(__cli_locale, "serving notes", []),
safe_list(serving["runtime_notes"]),
)
print_list(
stdio,
cli_message(__cli_locale, "promotion gates", []),
safe_list(serving["promotion_gates"]),
)
render_command(
stdio,
cli_message(__cli_locale, "preflight corpus", []),
safe_list(launch["preflight_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "export dataset", []),
safe_list(launch["export_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "train adapter", []),
safe_list(launch["train_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "eval", []),
safe_list(evaluation["eval_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "collect promotion receipt", []),
safe_list(launch["promote_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "batch eval manifest", []),
safe_list(batch_ready["manifest_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "inspect adapter", []),
safe_list(launch["inspect_command"]),
)
render_command(
stdio,
cli_message(__cli_locale, "local launch", []),
safe_list(launch["local_launch_command"]),
)
for warning in warnings {
const text = safe_string(warning, "")
if text != "" {
stdio.println(cli_message(__cli_locale, " warning: {0}", [to_string(text)]))
}
}
}
fn __render_json(report: dict) -> string {
const ok = safe_bool(report["ok"], false)
const envelope = if ok {
cli_json_envelope({schema_version: 1, ok: true, data: report})
} else {
cli_json_envelope(
{
schema_version: 1,
ok: false,
error: {
code: "lora_plan_failed",
message: "LoRA plan did not produce a usable training contract.",
details: report,
},
},
)
}
return json_stringify_pretty(envelope)
}
fn main(harness: Harness) -> int {
const __cli_locale = cli_locale(harness.env)
const raw = harness.env.get_or("HARN_MODELS_LORA_PLAN_PAYLOAD_JSON", "")
if raw == "" {
harness.stdio.eprintln("internal error: HARN_MODELS_LORA_PLAN_PAYLOAD_JSON not set")
return 70
}
const report = try {
json_parse(raw)
} catch (e) {
harness.stdio.eprintln("internal error: failed to parse LoRA plan payload: " + e.message)
return 70
}
const ok = safe_bool(report["ok"], false)
const json_mode = harness.env.get_or("HARN_OUTPUT_JSON", "0") == "1"
if json_mode {
harness.stdio.println(__render_json(report))
} else {
__render_human(__cli_locale, harness.stdio, report)
}
if ok {
return 0
}
return 1
}