import { cli_locale } from "std/cli/localization"
import { cli_message } from "std/cli/localization/providers/cache_probe"
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(__cli_locale: string, 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 = cli_message(
__cli_locale,
"{0} {1} support={2} breakpoint={3} verdict={4} dogfood_failure={5}\n",
[
to_string(provider),
to_string(model),
to_string(safe_string(support["status"], "")),
to_string(safe_string(profile["cache_breakpoint_style"], "none")),
to_string(verdict),
to_string(to_string(report["dogfood_failure"])),
],
)
out = cli_message(
__cli_locale,
"{0} buckets: cache_effective={1} cache_supported_miss={2} unsupported_zero={3} support_unknown_zero={4} no_prompt_tokens={5} provider_field_inconsistent={6}\n",
[
to_string(out),
to_string(to_string(safe_int(counts["cache_effective"], 0))),
to_string(to_string(safe_int(counts["cache_supported_miss"], 0))),
to_string(to_string(safe_int(counts["unsupported_zero"], 0))),
to_string(to_string(safe_int(counts["support_unknown_zero"], 0))),
to_string(to_string(safe_int(counts["no_prompt_tokens"], 0))),
to_string(to_string(safe_int(counts["provider_field_inconsistent"], 0))),
],
)
const runs = safe_list(report["runs"])
for run in runs {
if type_of(run) != "dict" {
continue
}
const usage = safe_dict(run["usage"])
out = cli_message(
__cli_locale,
"{0} run {1}: {2} input={3} fresh={4} cache_read={5} cache_write={6}\n",
[
to_string(out),
to_string(to_string(safe_int(run["run_index"], 0))),
to_string(safe_string(run["classification"], "")),
to_string(to_string(safe_int(usage["input_tokens"], 0))),
to_string(to_string(safe_int(usage["fresh_input_tokens"], 0))),
to_string(to_string(safe_int(usage["cache_read_tokens"], 0))),
to_string(to_string(safe_int(usage["cache_write_tokens"], 0))),
],
)
}
return out
}
fn main(harness: Harness) -> int {
const __cli_locale = cli_locale(harness.env)
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(__cli_locale, 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
}