harn-stdlib 0.10.182

Embedded Harn standard library source catalog
Documentation
import { cli_locale } from "std/cli/localization"
import { cli_message } from "std/cli/localization/models/lora_export"
import {
  render_peft_save_policy,
  render_probe_command_templates,
  render_required_probe_cases,
  render_tool_catalog_contract,
} from "std/cli/models/lora_render"
/**
 * `harn models lora export` renderer.
 *
 * The dispatch shim resolves model/catalog facts and writes dataset rows. This
 * Harn source owns the report presentation and JSON passthrough.
 *
 * Inputs (from the dispatch shim):
 *   HARN_MODELS_LORA_EXPORT_PAYLOAD_JSON   - compact report JSON.
 *   HARN_MODELS_LORA_EXPORT_PAYLOAD_PRETTY - pretty report JSON.
 *   HARN_OUTPUT_JSON                       - "1" for JSON, else human text.
 */
import {
  cli_json_envelope,
  print_list,
  render_command,
  safe_bool,
  safe_dict,
  safe_int_string,
  safe_list,
  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 target = safe_dict(report["target"])
  const contract = safe_dict(report["contract"])
  const training_contract = safe_dict(contract["training_contract"])
  const serving = safe_dict(report["serving"])
  const promotion = safe_dict(report["promotion"])
  const evidence = safe_dict(promotion["evidence_contract"])
  const batch_ready = safe_dict(evidence["batch_ready"])
  const output = safe_dict(report["output"])
  const stats = safe_dict(report["stats"])
  const behavior_strata = safe_dict(stats["behavior_strata"])
  const warnings = safe_list(report["warnings"])
  const errors = safe_list(report["errors"])
  const base_id = safe_string(base["id"], "")
  const provider = safe_string(base["provider"], "")
  stdio.println(
    cli_message(
      __cli_locale,
      "LoRA export for {0} via {1}",
      [to_string(base_id), to_string(provider)],
    ),
  )
  stdio.println(
    cli_message(__cli_locale, "  corpus: {0}", [to_string(safe_string(request["corpus"], ""))]),
  )
  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")),
      ],
    ),
  )
  stdio.println(
    cli_message(
      __cli_locale,
      "  dataset format: {0}",
      [to_string(safe_string(request["dataset_format"], ""))],
    ),
  )
  stdio.println(
    cli_message(__cli_locale, "  contract: {0}", [to_string(safe_string(contract["id"], ""))]),
  )
  stdio.println(
    cli_message(
      __cli_locale,
      "  contract assistant mask: {0}",
      [to_string(safe_string(training_contract["assistant_mask_policy"], ""))],
    ),
  )
  stdio.println(
    cli_message(
      __cli_locale,
      "  contract 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,
      "  contract split policy: {0}",
      [to_string(safe_string(training_contract["dataset_split_policy"], ""))],
    ),
  )
  const peft_save_policy = safe_dict(training_contract["peft_save_policy"])
  render_peft_save_policy(__cli_locale, stdio, peft_save_policy)
  stdio.println(
    cli_message(
      __cli_locale,
      "  provenance defaults: split={0} license={1}",
      [
        to_string(safe_string(request["default_split"], "")),
        to_string(safe_string(request["default_license"], "")),
      ],
    ),
  )
  print_list(
    stdio,
    cli_message(__cli_locale, "required example metadata", []),
    safe_list(training_contract["required_example_metadata"]),
  )
  stdio.println(
    cli_message(
      __cli_locale,
      "  target base: {0}",
      [to_string(safe_string(target["base_model"], ""))],
    ),
  )
  const adapter = safe_string(target["adapter_name"], "")
  if adapter != "" {
    stdio.println(cli_message(__cli_locale, "  adapter name: {0}", [to_string(adapter)]))
  }
  const template = safe_string(target["chat_template"], "")
  if template != "" {
    stdio.println(cli_message(__cli_locale, "  chat template: {0}", [to_string(template)]))
  }
  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"], ""))],
    ),
  )
  const request_model = safe_string(serving["request_model"], "")
  if request_model != "" {
    stdio.println(cli_message(__cli_locale, "  request model: {0}", [to_string(request_model)]))
  }
  stdio.println(
    cli_message(
      __cli_locale,
      "  promotion minimum trials: {0}",
      [to_string(safe_int_string(promotion["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,
      "  promotion baseline: {0}",
      [to_string(safe_string(promotion["comparison_baseline"], ""))],
    ),
  )
  print_list(
    stdio,
    cli_message(__cli_locale, "promotion metrics", []),
    safe_list(promotion["required_metrics"]),
  )
  print_list(stdio, cli_message(__cli_locale, "promotion gates", []), safe_list(promotion["gates"]))
  print_list(
    stdio,
    cli_message(__cli_locale, "promotion 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, "batch receipts", []),
    safe_list(evidence["optional_batch_receipts"]),
  )
  render_command(
    stdio,
    cli_message(__cli_locale, "batch eval manifest", []),
    safe_list(batch_ready["manifest_command"]),
  )
  const path = safe_string(output["path"], "")
  if path != "" {
    stdio.println(cli_message(__cli_locale, "  output: {0}", [to_string(path)]))
  }
  const manifest = safe_string(output["manifest_path"], "")
  if manifest != "" {
    stdio.println(cli_message(__cli_locale, "  manifest: {0}", [to_string(manifest)]))
  }
  if safe_bool(request["check"], false) {
    stdio.println(cli_message(__cli_locale, "  mode: check", []))
  }
  stdio.println(
    cli_message(
      __cli_locale,
      "  stats: records={0} emitted={1} skipped={2} tool_calls={3} tool_results={4}",
      [
        to_string(safe_int_string(stats["records"], "0")),
        to_string(safe_int_string(stats["emitted"], "0")),
        to_string(safe_int_string(stats["skipped"], "0")),
        to_string(safe_int_string(stats["tool_calls"], "0")),
        to_string(safe_int_string(stats["tool_results"], "0")),
      ],
    ),
  )
  stdio.println(
    cli_message(
      __cli_locale,
      "  behavior strata: source={0} emitted={1}",
      [
        to_string(json_stringify(safe_dict(behavior_strata["source"]))),
        to_string(json_stringify(safe_dict(behavior_strata["emitted"]))),
      ],
    ),
  )
  print_list(stdio, cli_message(__cli_locale, "warnings", []), warnings)
  print_list(stdio, cli_message(__cli_locale, "errors", []), errors)
}

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_export_failed",
          message: "LoRA dataset export failed validation or conversion.",
          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_EXPORT_PAYLOAD_JSON", "")
  if raw == "" {
    harness.stdio.eprintln("internal error: HARN_MODELS_LORA_EXPORT_PAYLOAD_JSON not set")
    return 70
  }
  const report = try {
    json_parse(raw)
  } catch (e) {
    harness.stdio.eprintln("internal error: failed to parse LoRA export 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
}