; cn library (pure functions, testable)
;
; Discipline: no filesystem, git, process exec, HTTP, or LLM code in
; this library. Pure types, validators, parsers, and lookups only.
; See docs/alpha/agent-runtime/CORE-REFACTOR.md §7 Move 2.
(library
 (name cn_lib)
 (wrapped false)
 (modules cn_lib cn_json cn_sha256 cn_package cn_contract cn_workflow_ir cn_frontmatter cn_build_info cn_version cn_repo_info)
 (libraries inbox_lib))

(rule
 (target cn_build_info.ml)
 (deps (universe))
 (action (bash "echo 'let cnos_commit = \"'$(git rev-parse --short HEAD 2>/dev/null || echo unknown)'\"' > %{target}")))

(rule
 (target cn_version.ml)
 (deps ../../VERSION)
 (action (bash "echo 'let version = \"'$(cat ../../VERSION | tr -d '\\n')'\"' > %{target}")))

(rule
 (target cn_repo_info.ml)
 (deps ../../cn.json)
 ; Extract the first URL inside the `repo_urls` array specifically (rather than
 ; grepping any github.com URL in the file, which would also match maintainer
 ; hub URLs like usurobor/cn-sigma). awk scopes the match to lines between
 ; `"repo_urls"` and the closing `]`.
 (action (bash "repo=$(awk '/\"repo_urls\"/{flag=1;next} /\\]/{flag=0} flag' ../../cn.json | sed -n 's|.*github.com/\\([^\"]*\\)\".*|\\1|p' | head -1 | sed 's/\\.git$//'); if [ -z \"$repo\" ]; then echo 'Error: no GitHub repo URL found in cn.json repo_urls' >&2; exit 1; fi; echo \"let repo = \\\"${repo}\\\"\" > %{target}")))
