harn-stdlib 0.10.5

Embedded Harn standard library source catalog
Documentation
/** Shared render helpers for `harn models lora *` commands. */
import { print_list, render_command, safe_dict, safe_list, safe_string } from "std/cli/render"

/**
 * Render the PEFT save policy carried by a LoRA training contract.
 *
 * @effects: [stdio.write]
 * @errors: []
 */
pub fn render_peft_save_policy(harness: Harness, policy: dict) {
  const modules_to_save = safe_list(policy["modules_to_save"])
  if len(modules_to_save) > 0 {
    harness.stdio
      .println(
      "  modules to save: " + join(modules_to_save.map({ item -> safe_string(item, "") }), ", "),
    )
  } else {
    harness.stdio.println("  modules to save: none")
  }
  harness.stdio
    .println("  embedding save policy: " + safe_string(policy["save_embedding_layers"], ""))
  harness.stdio
    .println("  weight tying policy: " + safe_string(policy["tied_embedding_policy"], ""))
  print_list(harness, "PEFT save notes", safe_list(policy["notes"]))
}

fn __probe_case_line(value: unknown) -> string {
  const probe = safe_dict(value)
  const id = safe_string(probe["id"], "")
  if id == "" {
    return ""
  }
  const requirement = safe_string(probe["requirement"], "")
  const expected = safe_string(probe["expected"], "")
  const receipt = safe_string(probe["receipt"], "")
  const rationale = safe_string(probe["rationale"], "")
  let line = id
  if requirement != "" {
    line = line + " [" + requirement + "]"
  }
  if expected != "" {
    line = line + ": " + expected
  }
  if receipt != "" {
    line = line + " (receipt " + receipt + ")"
  }
  if rationale != "" {
    line = line + " - " + rationale
  }
  return line
}

/**
 * Render the behavioral probe matrix required before a LoRA promotion.
 *
 * @effects: [stdio.write]
 * @errors: []
 */
pub fn render_required_probe_cases(harness: Harness, value: unknown) {
  const probe_cases = safe_list(value)
  if len(probe_cases) == 0 {
    return
  }
  harness.stdio.println("  required probe cases:")
  for item in probe_cases {
    const line = __probe_case_line(item)
    if line != "" {
      harness.stdio.println("    - " + line)
    }
  }
}

/**
 * Render the per-case promotion probe commands and expected output receipts.
 *
 * @effects: [stdio.write]
 * @errors: []
 */
pub fn render_probe_command_templates(harness: Harness, value: unknown) {
  const command_templates = safe_list(value)
  if len(command_templates) == 0 {
    return
  }
  harness.stdio.println("  probe command templates:")
  for item in command_templates {
    const template = safe_dict(item)
    const case_id = safe_string(template["case_id"], "")
    if case_id == "" {
      continue
    }
    const receipt = safe_string(template["receipt"], "")
    const summary_path = safe_string(template["summary_path"], "")
    let line = case_id
    if receipt != "" {
      line = line + " -> " + receipt
    }
    if summary_path != "" {
      line = line + " (" + summary_path + ")"
    }
    harness.stdio.println("    - " + line)
    render_command(harness, "probe " + case_id, safe_list(template["command"]))
    print_list(harness, "probe " + case_id + " notes", safe_list(template["notes"]))
  }
}