harn-stdlib 0.10.120

Embedded Harn standard library source catalog
Documentation
import { safe_dict, safe_int, safe_list, safe_string } from "std/cli/render"

/**
 * `harn provider cache-probe` render layer — see harn#3927.
 *
 * The classifier (support resolution + per-run normalization + verdict) stays
 * in Rust (`crates/harn-vm/src/llm/cache_conformance.rs`) because it is the
 * stable contract local dogfood (#3532) and Harn Cloud receipts (#1106)
 * consume. The Rust shim classifies the saved usage fixture and hands the JSON
 * `CacheConformanceReport` here for rendering only.
 *
 * Inputs (from the dispatch shim):
 *   HARN_PROVIDER_CACHE_PROBE_PAYLOAD_JSON — JSON envelope matching the
 *     `CacheConformanceReport` shape.
 *   HARN_PROVIDER_CACHE_PROBE_PAYLOAD_PRETTY — pretty-printed companion,
 *     forwarded verbatim in JSON mode to preserve serde float fidelity.
 *   HARN_OUTPUT_JSON — "1" for the JSON envelope, else the human summary.
 */
fn __render_human(report: dict) -> string {
  const provider = safe_string(report["provider"], "")
  const model = safe_string(report["model"], "")
  const support = safe_dict(report["support"])
  const profile = safe_dict(support["profile"])
  const verdict = safe_string(report["verdict"], "")
  const counts = safe_dict(report["bucket_counts"])
  let out = provider + " " + model
    + " support="
    + safe_string(support["status"], "")
    + " breakpoint="
    + safe_string(profile["cache_breakpoint_style"], "none")
    + " verdict="
    + verdict
    + " dogfood_failure="
    + to_string(report["dogfood_failure"])
    + "\n"
  out = out + "  buckets:"
    + " cache_effective="
    + to_string(safe_int(counts["cache_effective"], 0))
    + " cache_supported_miss="
    + to_string(safe_int(counts["cache_supported_miss"], 0))
    + " unsupported_zero="
    + to_string(safe_int(counts["unsupported_zero"], 0))
    + " support_unknown_zero="
    + to_string(safe_int(counts["support_unknown_zero"], 0))
    + " no_prompt_tokens="
    + to_string(safe_int(counts["no_prompt_tokens"], 0))
    + " provider_field_inconsistent="
    + to_string(safe_int(counts["provider_field_inconsistent"], 0))
    + "\n"
  const runs = safe_list(report["runs"])
  for run in runs {
    if type_of(run) != "dict" {
      continue
    }
    const usage = safe_dict(run["usage"])
    out = out + "  run " + to_string(safe_int(run["run_index"], 0)) + ": "
      + safe_string(run["classification"], "")
      + " input="
      + to_string(safe_int(usage["input_tokens"], 0))
      + " fresh="
      + to_string(safe_int(usage["fresh_input_tokens"], 0))
      + " cache_read="
      + to_string(safe_int(usage["cache_read_tokens"], 0))
      + " cache_write="
      + to_string(safe_int(usage["cache_write_tokens"], 0))
      + "\n"
  }
  return out
}

fn main(harness: Harness) -> int {
  const raw = harness.env.get_or("HARN_PROVIDER_CACHE_PROBE_PAYLOAD_JSON", "")
  if raw == "" {
    harness.stdio.eprintln(
      "internal error: HARN_PROVIDER_CACHE_PROBE_PAYLOAD_JSON not set by dispatch shim",
    )
    return 70
  }
  const json_mode = harness.env.get_or("HARN_OUTPUT_JSON", "0") == "1"
  if json_mode {
    const pretty = harness.env.get_or("HARN_PROVIDER_CACHE_PROBE_PAYLOAD_PRETTY", raw)
    harness.stdio.println(pretty)
  } else {
    const report = try {
      json_parse(raw)
    } catch (e) {
      harness.stdio.eprintln(
        "internal error: failed to parse provider-cache-probe payload: " + e.message,
      )
      return 70
    }
    const text = __render_human(report)
    const trimmed = if len(text) > 0 && text[len(text) - 1] == "\n" {
      text[0:len(text) - 1]
    } else {
      text
    }
    harness.stdio.println(trimmed)
  }
  return 0
}