feat: port arbor, dharma, forge El source into monorepo

Brings the remaining foundation repos that were not included in the
original monorepo consolidation:

- arbor/vessels/ — 6 vessels (arbor-cli, arbor-core, arbor-diagram,
  arbor-layout, arbor-parse, arbor-render) with manifests + src/main.el
- dharma/ — CGI Provenance Registry package (flat layout, 14 .el files
  across registry/, sandbox/, training/, validation/, tests/)
- forge/ — consciousness channel tool (8 src .el files + new manifest.el)
- elp/src/ — 36 test fixture files not carried over in original merge
  (dedup_*, realizer_*, semantics_*, morph_*, ext_*, one_extern_* helpers)

el-ide, engram, elql are already complete in ide/, engram/, ql/.
This commit is contained in:
Will Anderson
2026-05-05 04:27:34 -05:00
parent bdd7b56703
commit 90ddbdbfc3
78 changed files with 18211 additions and 0 deletions
@@ -0,0 +1,56 @@
// test_body_contains_key.el verifies body_contains_key correctly
// distinguishes "user asserted this field" from "user did not mention it".
// This is the gate the PATCH endpoint uses to enforce immutability.
fn body_contains_key(body: String, key: String) -> Bool {
return str_contains(body, "\"" + key + "\":")
}
fn assert_true(name: String, got: Bool) -> Bool {
if got {
println("PASS " + name)
return true
}
println("FAIL " + name + " want=true got=false")
return false
}
fn assert_false(name: String, got: Bool) -> Bool {
if got {
println("FAIL " + name + " want=false got=true")
return false
}
println("PASS " + name)
return true
}
fn main() -> Void {
let b1: String = "{\"post_reasoning\":\"x\"}"
assert_true("present-non-empty", body_contains_key(b1, "post_reasoning"))
// Empty value still counts as "asserted" user mentioned the key.
let b2: String = "{\"post_reasoning\":\"\"}"
assert_true("present-empty-string", body_contains_key(b2, "post_reasoning"))
// Key absent.
let b3: String = "{\"gap_summary\":\"x\"}"
assert_false("absent", body_contains_key(b3, "post_reasoning"))
// Substring of another key must NOT match.
// E.g. "tags": vs "tag": distinct.
let b4: String = "{\"tags\":\"a,b,c\"}"
assert_false("substring-distinct", body_contains_key(b4, "tag"))
assert_true("exact-tags", body_contains_key(b4, "tags"))
// Immutable-field detection: PATCH must reject these.
let b5: String = "{\"pre_reasoning\":\"new value\",\"post_reasoning\":\"x\"}"
assert_true("detect-pre_reasoning", body_contains_key(b5, "pre_reasoning"))
assert_true("detect-post_reasoning", body_contains_key(b5, "post_reasoning"))
let b6: String = "{\"cgi_id\":\"x\"}"
assert_true("detect-cgi_id", body_contains_key(b6, "cgi_id"))
// No false-positive if the value happens to contain the same string.
let b7: String = "{\"trigger\":\"contained pre_reasoning string\"}"
assert_false("no-false-positive-from-value", body_contains_key(b7, "pre_reasoning"))
}
@@ -0,0 +1,104 @@
// test_build_json.el verifies the JSON serialization of an internal-state event.
// Confirms field order, presence of required + optional fields, and JSON escaping.
fn json_escape(s: String) -> String {
let s1: String = str_replace(s, "\\", "\\\\")
let s2: String = str_replace(s1, "\"", "\\\"")
let s3: String = str_replace(s2, "\n", "\\n")
let s4: String = str_replace(s3, "\r", "\\r")
let s5: String = str_replace(s4, "\t", "\\t")
return s5
}
fn build_internal_state_json(
id: String,
cgi_id: String,
event_id: String,
trigger: String,
domain: String,
pre_reasoning: String,
pre_logged_at: Int,
post_reasoning: String,
gap_summary: String,
compression_ratio: Float,
gap_direction: String,
tags: String,
logged_at: Int
) -> String {
let p1: String = "{\"_type\":\"internal_state\",\"id\":\"" + id + "\""
let p2: String = p1 + ",\"cgi_id\":\"" + cgi_id + "\""
let p3: String = p2 + ",\"event_id\":\"" + event_id + "\""
let p4: String = p3 + ",\"trigger\":\"" + json_escape(trigger) + "\""
let p5: String = p4 + ",\"domain\":\"" + json_escape(domain) + "\""
let p6: String = p5 + ",\"pre_reasoning\":\"" + json_escape(pre_reasoning) + "\""
let p7: String = p6 + ",\"pre_logged_at\":" + int_to_str(pre_logged_at)
let p8: String = p7 + ",\"post_reasoning\":\"" + json_escape(post_reasoning) + "\""
let p9: String = p8 + ",\"gap_summary\":\"" + json_escape(gap_summary) + "\""
let p10: String = p9 + ",\"compression_ratio\":" + float_to_str(compression_ratio)
let p11: String = p10 + ",\"gap_direction\":\"" + json_escape(gap_direction) + "\""
let p12: String = p11 + ",\"tags\":\"" + json_escape(tags) + "\""
let p13: String = p12 + ",\"logged_at\":" + int_to_str(logged_at) + "}"
return p13
}
fn assert_contains(name: String, hay: String, needle: String) -> Bool {
if str_contains(hay, needle) {
println("PASS " + name)
return true
}
println("FAIL " + name + " missing=" + needle)
return false
}
fn assert_eq_str(name: String, got: String, want: String) -> Bool {
if str_eq(got, want) {
println("PASS " + name)
return true
}
println("FAIL " + name)
println(" want=" + want)
println(" got =" + got)
return false
}
fn main() -> Void {
// Pre-reasoning capture (POST initial state; post fields blank).
let pre_only: String = build_internal_state_json(
"id-001", "cgi-001", "evt-001",
"user said \"X\"", "cognition",
"resistance, vertigo", 1700000000,
"", "",
0.0, "", "structural",
1700000005
)
assert_contains("type-discriminator", pre_only, "\"_type\":\"internal_state\"")
assert_contains("pre_reasoning-present", pre_only, "\"pre_reasoning\":\"resistance, vertigo\"")
assert_contains("pre_logged_at-present", pre_only, "\"pre_logged_at\":1700000000")
assert_contains("post-empty", pre_only, "\"post_reasoning\":\"\"")
assert_contains("logged_at-after-pre", pre_only, "\"logged_at\":1700000005")
assert_contains("escaped-trigger-quotes", pre_only, "\"user said \\\"X\\\"\"")
// Full record after PATCH (all fields populated).
let full: String = build_internal_state_json(
"id-002", "cgi-001", "evt-002",
"trigger phrase", "emotion",
"raw pull toward defending", 1700001000,
"softened to honest acknowledgment", "pre/post differ: defensive impulse → owned uncertainty",
0.42, "softened", "honesty,vulnerability",
1700001020
)
assert_contains("post_reasoning", full, "\"post_reasoning\":\"softened to honest acknowledgment\"")
assert_contains("gap_summary", full, "\"gap_summary\":\"pre/post differ: defensive impulse → owned uncertainty\"")
assert_contains("gap_direction", full, "\"gap_direction\":\"softened\"")
assert_contains("compression_ratio", full, "\"compression_ratio\":")
// Newline / quote escape sanity.
let with_newline: String = build_internal_state_json(
"id-003", "cgi-001", "evt-003",
"t", "d",
"line1\nline2", 1700002000,
"", "", 0.0, "", "",
1700002001
)
assert_contains("newline-escaped", with_newline, "\"pre_reasoning\":\"line1\\nline2\"")
}
+101
View File
@@ -0,0 +1,101 @@
// test_filter.el exercises the GET filter logic on a synthetic JSON array.
// Tests: since, until, domain match, tag substring, no-op when all filters empty.
fn filter_internal_state_inner(
arr: String,
n: Int,
idx: Int,
since: Int,
until: Int,
domain: String,
tag: String,
acc: String,
first: Bool
) -> String {
if idx >= n {
return acc + "]"
}
let item: String = json_array_get(arr, idx)
let logged_at: Int = json_get_int(item, "logged_at")
let item_domain: String = json_get(item, "domain")
let item_tags: String = json_get(item, "tags")
let keep_since: Bool = if since <= 0 { true } else { logged_at >= since }
let keep_until: Bool = if until <= 0 { true } else { logged_at <= until }
let keep_domain: Bool = if str_eq(domain, "") { true } else { str_eq(item_domain, domain) }
let keep_tag: Bool = if str_eq(tag, "") { true } else { str_contains(item_tags, tag) }
if keep_since {
if keep_until {
if keep_domain {
if keep_tag {
if first {
return filter_internal_state_inner(arr, n, idx + 1, since, until, domain, tag, acc + item, false)
}
return filter_internal_state_inner(arr, n, idx + 1, since, until, domain, tag, acc + "," + item, false)
}
}
}
}
return filter_internal_state_inner(arr, n, idx + 1, since, until, domain, tag, acc, first)
}
fn filter_internal_state_array(arr: String, since: Int, until: Int, domain: String, tag: String) -> String {
let n: Int = json_array_len(arr)
if since <= 0 {
if until <= 0 {
if str_eq(domain, "") {
if str_eq(tag, "") {
return arr
}
}
}
}
return filter_internal_state_inner(arr, n, 0, since, until, domain, tag, "[", true)
}
fn assert_count(name: String, arr: String, want: Int) -> Bool {
let got: Int = json_array_len(arr)
if got == want {
println("PASS " + name)
return true
}
println("FAIL " + name + " want=" + int_to_str(want) + " got=" + int_to_str(got))
return false
}
fn main() -> Void {
// Three synthetic events at logged_at = 100, 200, 300; domains and tags vary.
let a: String = "{\"id\":\"a\",\"logged_at\":100,\"domain\":\"cognition\",\"tags\":\"vertigo,resistance\"}"
let b: String = "{\"id\":\"b\",\"logged_at\":200,\"domain\":\"emotion\",\"tags\":\"pull\"}"
let c: String = "{\"id\":\"c\",\"logged_at\":300,\"domain\":\"cognition\",\"tags\":\"resistance,softening\"}"
let arr: String = "[" + a + "," + b + "," + c + "]"
// No-op (all filters empty) returns input.
assert_count("noop", filter_internal_state_array(arr, 0, 0, "", ""), 3)
// since: only 200 and 300 pass since>=200.
assert_count("since-200", filter_internal_state_array(arr, 200, 0, "", ""), 2)
// until: only 100 and 200 pass until<=200.
assert_count("until-200", filter_internal_state_array(arr, 0, 200, "", ""), 2)
// since+until window: only 200.
assert_count("window-200-200", filter_internal_state_array(arr, 200, 200, "", ""), 1)
// domain exact match: cognition matches a and c.
assert_count("domain-cognition", filter_internal_state_array(arr, 0, 0, "cognition", ""), 2)
// domain no-match.
assert_count("domain-none", filter_internal_state_array(arr, 0, 0, "missing", ""), 0)
// tag substring match: "resist" appears in a and c.
assert_count("tag-resist", filter_internal_state_array(arr, 0, 0, "", "resist"), 2)
// tag exact unique: "pull" only in b.
assert_count("tag-pull", filter_internal_state_array(arr, 0, 0, "", "pull"), 1)
// Combined filters: cognition + resistance + since>=200.
assert_count("combined",
filter_internal_state_array(arr, 200, 0, "cognition", "resistance"), 1)
}
@@ -0,0 +1,75 @@
// test_immutability_gate.el verifies that PATCH bodies referencing
// any of the immutable fields are rejected by body_contains_key + the
// gate logic (the per-field check ladder in patch_internal_state_handler).
// This mirrors the production check ladder in registry/handlers.el.
fn body_contains_key(body: String, key: String) -> Bool {
return str_contains(body, "\"" + key + "\":")
}
// patch_validate returns "" if the body is acceptable for PATCH,
// otherwise the field name that violates immutability. This is the same
// ladder as patch_internal_state_handler in handlers.el.
fn patch_validate(body: String) -> String {
if body_contains_key(body, "pre_reasoning") { return "pre_reasoning" }
if body_contains_key(body, "pre_logged_at") { return "pre_logged_at" }
if body_contains_key(body, "cgi_id") { return "cgi_id" }
if body_contains_key(body, "event_id") { return "event_id" }
if body_contains_key(body, "trigger") { return "trigger" }
if body_contains_key(body, "domain") { return "domain" }
if body_contains_key(body, "logged_at") { return "logged_at" }
if body_contains_key(body, "id") { return "id" }
return ""
}
fn assert_eq_str(name: String, got: String, want: String) -> Bool {
if str_eq(got, want) {
println("PASS " + name)
return true
}
println("FAIL " + name + " want=" + want + " got=" + got)
return false
}
fn main() -> Void {
// Allowed: only post-reasoning + gap + compression
let ok1: String = "{\"post_reasoning\":\"x\",\"gap_summary\":\"y\"}"
assert_eq_str("allow/post-only", patch_validate(ok1), "")
let ok2: String = "{\"post_reasoning\":\"x\",\"gap_direction\":\"softened\",\"compression_ratio\":0.5,\"tags\":\"new\"}"
assert_eq_str("allow/all-mutable", patch_validate(ok2), "")
// Empty body is acceptable (no-op PATCH; idempotent).
assert_eq_str("allow/empty", patch_validate("{}"), "")
// Each immutable field must be rejected.
assert_eq_str("reject/pre_reasoning",
patch_validate("{\"pre_reasoning\":\"new\"}"),
"pre_reasoning")
assert_eq_str("reject/pre_logged_at",
patch_validate("{\"pre_logged_at\":1234}"),
"pre_logged_at")
assert_eq_str("reject/cgi_id",
patch_validate("{\"cgi_id\":\"other\"}"),
"cgi_id")
assert_eq_str("reject/event_id",
patch_validate("{\"event_id\":\"other\"}"),
"event_id")
assert_eq_str("reject/trigger",
patch_validate("{\"trigger\":\"new\"}"),
"trigger")
assert_eq_str("reject/domain",
patch_validate("{\"domain\":\"other\"}"),
"domain")
assert_eq_str("reject/logged_at",
patch_validate("{\"logged_at\":1234}"),
"logged_at")
assert_eq_str("reject/id",
patch_validate("{\"id\":\"other\"}"),
"id")
// Mixed: rejection on first immutable hit; ladder order matters.
assert_eq_str("reject/pre-takes-precedence",
patch_validate("{\"post_reasoning\":\"ok\",\"pre_reasoning\":\"bad\"}"),
"pre_reasoning")
}
@@ -0,0 +1,91 @@
// test_query_param.el unit tests for query_param + str_qmark_index helpers
// in registry/handlers.el. Self-contained: copies the helpers under test so
// the test program doesn't drag in the http server / engram dependency.
fn json_escape(s: String) -> String { return s }
fn str_qmark_index_inner(s: String, idx: Int, total: Int) -> Int {
if idx >= total {
return -1
}
let c: String = str_slice(s, idx, idx + 1)
if str_eq(c, "?") {
return idx
}
return str_qmark_index_inner(s, idx + 1, total)
}
fn str_qmark_index(s: String) -> Int {
return str_qmark_index_inner(s, 0, str_len(s))
}
fn query_param_inner(parts: [String], key: String, idx: Int, total: Int) -> String {
if idx >= total {
return ""
}
let pair: String = list_get(parts, idx)
let kv: [String] = str_split(pair, "=")
let nkv: Int = list_len(kv)
if nkv >= 2 {
let k: String = list_get(kv, 0)
if str_eq(k, key) {
return list_get(kv, 1)
}
}
return query_param_inner(parts, key, idx + 1, total)
}
fn query_param(path: String, key: String) -> String {
let qpos: Int = str_qmark_index(path)
if qpos < 0 {
return ""
}
let qs: String = str_slice(path, qpos + 1, str_len(path))
let parts: [String] = str_split(qs, "&")
return query_param_inner(parts, key, 0, list_len(parts))
}
fn assert_eq_str(name: String, got: String, want: String) -> Bool {
if str_eq(got, want) {
println("PASS " + name)
return true
}
println("FAIL " + name + " want=" + want + " got=" + got)
return false
}
fn assert_eq_int(name: String, got: Int, want: Int) -> Bool {
if got == want {
println("PASS " + name)
return true
}
println("FAIL " + name + " want=" + int_to_str(want) + " got=" + int_to_str(got))
return false
}
fn main() -> Void {
// str_qmark_index
assert_eq_int("qmark/none", str_qmark_index("/internal-state"), -1)
assert_eq_int("qmark/at-15", str_qmark_index("/internal-state?cgi_id=abc"), 15)
assert_eq_int("qmark/empty", str_qmark_index(""), -1)
// query_param: single key, multi key, missing
assert_eq_str("qp/single",
query_param("/internal-state?cgi_id=cgi-001", "cgi_id"),
"cgi-001")
assert_eq_str("qp/multi-first",
query_param("/internal-state?cgi_id=x&domain=cognition&since=100", "cgi_id"),
"x")
assert_eq_str("qp/multi-mid",
query_param("/internal-state?cgi_id=x&domain=cognition&since=100", "domain"),
"cognition")
assert_eq_str("qp/multi-last",
query_param("/internal-state?cgi_id=x&domain=cognition&since=100", "since"),
"100")
assert_eq_str("qp/missing",
query_param("/internal-state?cgi_id=x", "until"),
"")
assert_eq_str("qp/no-qs",
query_param("/internal-state", "cgi_id"),
"")
}