harn-stdlib 0.10.124

Embedded Harn standard library source catalog
Documentation
/**
 * `harn version` ported to .harn — see harn#2301 (W1).
 *
 * Build-time constants come from env vars set by the dispatch shim in
 * crates/harn-cli/src/entrypoint.rs's `run_version`: HARN_BUILD_NAME,
 * HARN_BUILD_VERSION, HARN_BUILD_DESCRIPTION, HARN_BUILD_REVISION, and the
 * VM-owned HARN_RUNTIME_CONTENT_FINGERPRINT receipt.
 * The shim reads package metadata and the optional revision embedded by the
 * build script, never from the caller's runtime environment or checkout.
 *
 * The banner and JSON envelope shapes are pinned by
 * crates/harn-cli/tests/harn_cli_e2e/version_dispatch.rs.
 */
import { get_typed_value } from "std/schema"

type RuntimeBuildFeatures = {
  default: bool,
  full: bool,
  llm_bench_internals: bool,
  vm_bench_internals: bool,
  content: bool,
  compression: bool,
  http_compression: bool,
  cloud_aws: bool,
  native_keyring: bool,
  postgres: bool,
  sqlite: bool,
  otel: bool,
  testbench_wasi: bool,
}

type RuntimeCompatibilityFingerprint = {
  codegen_fingerprint: string,
  bytecode_schema_version: int,
  linked_program_schema_version: int,
  linker_algorithm_version: int,
  build_features: RuntimeBuildFeatures,
}

type RuntimeContentFingerprint = {
  schema: string,
  content_sha256: string,
  harn_version: string,
  embedded_stdlib_sha256: string,
  compatibility: RuntimeCompatibilityFingerprint,
  source_revision: string?,
}

fn render_banner(version: string) -> string {
  return "\n ╱▔▔╲\n ╱    ╲    harn v" + version
    + "\n │ ◆  │    the agent harness language\n │    │\n ╰──╯╱\n   ╱╱\n"
}

fn render_json(
  name: string,
  version: string,
  description: string,
  source_revision: string?,
  runtime_content_fingerprint: RuntimeContentFingerprint,
) -> string {
  const env = {
    schemaVersion: 2,
    ok: true,
    data: {
      name: name,
      version: version,
      description: description,
      source_revision: source_revision,
      runtime_content_fingerprint: runtime_content_fingerprint,
    },
    error: nil,
    warnings: [],
  }
  return json_stringify_pretty(env)
}

fn main(harness: Harness) {
  const json_mode = harness.env.get_or("HARN_OUTPUT_JSON", "0") == "1"
  const version = harness.env.get_or("HARN_BUILD_VERSION", "unknown")
  if json_mode {
    const name = harness.env.get_or("HARN_BUILD_NAME", "harn-cli")
    const description = harness.env.get_or("HARN_BUILD_DESCRIPTION", "")
    const raw_revision = trim(harness.env.get_or("HARN_BUILD_REVISION", ""))
    const source_revision: string? = if raw_revision == "" {
      nil
    } else {
      raw_revision
    }
    const runtime_content_fingerprint: RuntimeContentFingerprint = get_typed_value(
      json_parse(harness.env.get_or("HARN_RUNTIME_CONTENT_FINGERPRINT", "{}")),
      schema_of(RuntimeContentFingerprint),
    )
    harness.stdio.println(
      render_json(name, version, description, source_revision, runtime_content_fingerprint),
    )
  } else {
    harness.stdio.println(render_banner(version))
  }
}