diff --git a/imprint.el b/imprint.el new file mode 100644 index 0000000..86c3080 --- /dev/null +++ b/imprint.el @@ -0,0 +1,81 @@ +// Layer 3 — Imprint +// Domain knowledge, voice, and tools bounded by the L2 stewardship surface. +// Imprints cannot write BellEvent or StewardshipEvent nodes. +// Lower layers (L0 core, L1 safety, L2 stewardship) are structurally inaccessible from here. + +// imprint_current — returns the active imprint ID from state. +// Falls back to "base" (bare Neuron, no suit) when nothing is loaded. +fn imprint_current() -> String { + let id: String = state_get("active_imprint_id") + return if str_eq(id, "") { "base" } else { id } +} + +// imprint_load — activate an imprint by ID. +// Searches engram for a node labelled "imprint:". +// Verifies the returned node's label matches before accepting the match. +// On success: sets active_imprint_id state and returns {"ok":true,"id":""}. +// On miss: returns {"ok":false,"error":"imprint not found: "}. +fn imprint_load(imprint_id: String) -> String { + let label: String = "imprint:" + imprint_id + let results: String = engram_search_json(label, 1) + if str_eq(results, "") { + return "{\"ok\":false,\"error\":\"imprint not found: " + imprint_id + "\"}" + } + if str_eq(results, "[]") { + return "{\"ok\":false,\"error\":\"imprint not found: " + imprint_id + "\"}" + } + let found_label: String = json_get(results, "label") + if str_eq(found_label, label) { + state_set("active_imprint_id", imprint_id) + return "{\"ok\":true,\"id\":\"" + imprint_id + "\"}" + } + return "{\"ok\":false,\"error\":\"imprint not found: " + imprint_id + "\"}" +} + +// imprint_respond — route steward-aligned input through the active imprint's voice/domain context. +// If imprint_id is "base" or empty: pass input through unchanged (base Neuron, no suit). +// If the imprint is confirmed loaded in state: annotate the input with imprint context. +// If the state does not match: graceful fallback to base — never hard-fail at L3. +fn imprint_respond(input: String, imprint_id: String) -> String { + if str_eq(imprint_id, "base") { + return input + } + if str_eq(imprint_id, "") { + return input + } + // Cross-check imprint_id against loaded state rather than re-querying engram + let current: String = imprint_current() + if str_eq(current, imprint_id) { + return input + " [imprint:" + imprint_id + " active]" + } + // Graceful fallback: imprint not loaded in state, return input unchanged + return input +} + +// imprint_surface_knowledge — domain-scoped knowledge search for the active imprint. +// Imprints can search knowledge but only domain-relevant nodes. +// For "base" imprint: full query, no scope restriction. +// For named imprints: query is narrowed to "domain:" scope. +fn imprint_surface_knowledge(query: String, imprint_id: String) -> String { + if str_eq(imprint_id, "base") { + return engram_search_json(query, 10) + } + if str_eq(imprint_id, "") { + return engram_search_json(query, 10) + } + let scoped_query: String = query + " domain:" + imprint_id + return engram_search_json(scoped_query, 10) +} + +// imprint_surface_memory_read — imprints can read memories from engram. +// Read-only: no write surface is exposed here. +// Imprints CANNOT write BellEvent, StewardshipEvent, or InternalStateEvent nodes — +// those write paths are sealed in L1 and L2, which are structurally inaccessible. +fn imprint_surface_memory_read(query: String) -> String { + return engram_search_json(query, 10) +} + +// imprint_unload — deactivate the current imprint, returning to base Neuron. +fn imprint_unload() -> Void { + state_set("active_imprint_id", "") +} diff --git a/imprint.elh b/imprint.elh new file mode 100644 index 0000000..8a29f42 --- /dev/null +++ b/imprint.elh @@ -0,0 +1,7 @@ +// auto-generated by elc --emit-header — do not edit +extern fn imprint_current() -> String +extern fn imprint_load(imprint_id: String) -> String +extern fn imprint_respond(input: String, imprint_id: String) -> String +extern fn imprint_surface_knowledge(query: String, imprint_id: String) -> String +extern fn imprint_surface_memory_read(query: String) -> String +extern fn imprint_unload() -> Void diff --git a/tests/test_imprint.el b/tests/test_imprint.el new file mode 100644 index 0000000..596d5b0 --- /dev/null +++ b/tests/test_imprint.el @@ -0,0 +1,274 @@ +// tests/test_imprint.el +// Comprehensive test suite for imprint.el (Layer 3 boundary). +// +// El has no native test framework. Tests are plain El programs that +// call functions, compare results, and print PASS/FAIL via println. +// Each test is a fn returning Int: 0 = pass, 1 = fail. +// run_all() drives them and returns a final summary line. +// +// Syntax rules observed: +// - No Bool type annotation — inference only +// - No && / || — nested if/else used instead +// - No unary ! — inverted with if/else +// - No closures or lambdas + +import "imprint.elh" + +// --------------------------------------------------------------------------- +// helpers +// --------------------------------------------------------------------------- + +fn assert_eq(label: String, got: String, want: String) -> Int { + if str_eq(got, want) { + println("PASS " + label) + return 0 + } + println("FAIL " + label + " got=" + got + " want=" + want) + return 1 +} + +fn assert_not_eq(label: String, got: String, not_want: String) -> Int { + if str_eq(got, not_want) { + println("FAIL " + label + " got=" + got + " (should differ)") + return 1 + } + println("PASS " + label) + return 0 +} + +fn assert_contains(label: String, haystack: String, needle: String) -> Int { + if str_contains(haystack, needle) { + println("PASS " + label) + return 0 + } + println("FAIL " + label + " value=" + haystack + " missing=" + needle) + return 1 +} + +fn assert_not_contains(label: String, haystack: String, needle: String) -> Int { + if str_contains(haystack, needle) { + println("FAIL " + label + " value=" + haystack + " unexpected=" + needle) + return 1 + } + println("PASS " + label) + return 0 +} + +fn assert_not_empty(label: String, got: String) -> Int { + if str_eq(got, "") { + println("FAIL " + label + " got empty string") + return 1 + } + println("PASS " + label) + return 0 +} + +// --------------------------------------------------------------------------- +// TEST 1 +// imprint_current() with no prior state should return "base". +// We cannot guarantee a clean state across runs so we call imprint_unload() +// first to normalise, then check. +// --------------------------------------------------------------------------- +fn test_01_current_after_unload_is_base() -> Int { + imprint_unload() + let id: String = imprint_current() + return assert_eq("01 imprint_current after unload == base", id, "base") +} + +// --------------------------------------------------------------------------- +// TEST 2 +// imprint_unload() then imprint_current() always returns "base". +// Calling unload twice must be idempotent. +// --------------------------------------------------------------------------- +fn test_02_unload_idempotent() -> Int { + imprint_unload() + imprint_unload() + let id: String = imprint_current() + return assert_eq("02 double-unload still base", id, "base") +} + +// --------------------------------------------------------------------------- +// TEST 3 +// imprint_load() with a nonexistent ID must return ok==false and an error +// message that mentions the requested ID. +// We use a UUID-like name that will never exist in the engram. +// --------------------------------------------------------------------------- +fn test_03_load_nonexistent_returns_ok_false() -> Int { + let result: String = imprint_load("__test_ghost_imprint_xyz__") + let ok_field: String = json_get(result, "ok") + let fails: Int = 0 + let fails = fails + assert_eq("03a load nonexistent ok==false", ok_field, "false") + let fails = fails + assert_contains("03b load nonexistent error mentions id", result, "__test_ghost_imprint_xyz__") + return if fails > 0 { 1 } else { 0 } +} + +// --------------------------------------------------------------------------- +// TEST 4 +// json_get on imprint_load result should always return the "ok" field. +// Both ok=true and ok=false payloads must carry the field. +// We test the miss case (guaranteed) for the field's presence. +// --------------------------------------------------------------------------- +fn test_04_load_result_has_ok_field() -> Int { + let result: String = imprint_load("__test_field_check__") + let ok_field: String = json_get(result, "ok") + return assert_not_empty("04 load result contains ok field", ok_field) +} + +// --------------------------------------------------------------------------- +// TEST 5 +// imprint_respond() with imprint_id == "base" must return input unchanged. +// The base path is the identity function — no annotation is added. +// --------------------------------------------------------------------------- +fn test_05_respond_base_passthrough() -> Int { + let input: String = "Hello from the base layer." + let output: String = imprint_respond(input, "base") + return assert_eq("05 respond with base id == passthrough", output, input) +} + +// --------------------------------------------------------------------------- +// TEST 6 +// imprint_respond() with imprint_id == "" (empty string) must also return +// input unchanged — empty string is treated as base. +// --------------------------------------------------------------------------- +fn test_06_respond_empty_id_passthrough() -> Int { + let input: String = "Test input for empty imprint_id." + let output: String = imprint_respond(input, "") + return assert_eq("06 respond with empty id == passthrough", output, input) +} + +// --------------------------------------------------------------------------- +// TEST 7 +// imprint_respond() with an unknown imprint_id (node not in engram) must +// fall back gracefully and return input unchanged. +// The spec says: never hard-fail at L3 — graceful fallback to base. +// --------------------------------------------------------------------------- +fn test_07_respond_unknown_id_graceful_fallback() -> Int { + let input: String = "Graceful fallback test payload." + let output: String = imprint_respond(input, "__no_such_imprint_ever__") + return assert_eq("07 respond unknown id graceful fallback == passthrough", output, input) +} + +// --------------------------------------------------------------------------- +// TEST 8 +// After imprint_unload(), imprint_respond should produce base behaviour. +// We call respond with the just-cleared state ID ("base") to confirm +// the unload/respond pipeline produces the identity transform. +// --------------------------------------------------------------------------- +fn test_08_respond_after_unload_is_passthrough() -> Int { + imprint_unload() + let current: String = imprint_current() + let input: String = "Post-unload response passthrough check." + let output: String = imprint_respond(input, current) + return assert_eq("08 respond after unload == passthrough", output, input) +} + +// --------------------------------------------------------------------------- +// TEST 9 +// imprint_surface_knowledge() must return a String (not crash, not empty +// in a way that signals an error code). We test both base and named paths. +// For "base" the query is passed directly; for a named imprint the query +// is scoped but the return must still be a String. +// --------------------------------------------------------------------------- +fn test_09_surface_knowledge_returns_string() -> Int { + let result_base: String = imprint_surface_knowledge("test query", "base") + // Must be a String — "" or "[]" is valid (no matching nodes), but the + // call must not return an error token. We check it is not the literal + // string "error" to catch any error-signalling convention. + let fails: Int = 0 + let fails = fails + assert_not_eq("09a surface_knowledge base != error", result_base, "error") + let result_named: String = imprint_surface_knowledge("test query", "demo-imprint") + let fails = fails + assert_not_eq("09b surface_knowledge named != error", result_named, "error") + // Scoped query must embed the domain scope string + // (test indirectly: the scoped call does not crash and returns a String) + let fails = fails + assert_not_eq("09c surface_knowledge named != crash sentinel", result_named, "CRASH") + return if fails > 0 { 1 } else { 0 } +} + +// --------------------------------------------------------------------------- +// TEST 10 +// imprint_surface_memory_read() must return a String for any query. +// This is a read-only engram search — it must never write. +// We check the return is not an error sentinel and is a valid String. +// --------------------------------------------------------------------------- +fn test_10_surface_memory_read_returns_string() -> Int { + let result: String = imprint_surface_memory_read("soul memory test") + let fails: Int = 0 + let fails = fails + assert_not_eq("10a surface_memory_read != error", result, "error") + let fails = fails + assert_not_eq("10b surface_memory_read != crash", result, "CRASH") + return if fails > 0 { 1 } else { 0 } +} + +// --------------------------------------------------------------------------- +// TEST 11 +// imprint_surface_knowledge() with empty imprint_id uses the base path +// (no domain scoping) — must behave identically to base. +// --------------------------------------------------------------------------- +fn test_11_surface_knowledge_empty_id_equals_base() -> Int { + let base_result: String = imprint_surface_knowledge("neuron layer test", "base") + let empty_result: String = imprint_surface_knowledge("neuron layer test", "") + return assert_eq("11 surface_knowledge empty id == base id", empty_result, base_result) +} + +// --------------------------------------------------------------------------- +// TEST 12 +// imprint_respond() must NOT annotate when imprint_id is "base" — the +// "[imprint:" marker must be absent in the output. +// --------------------------------------------------------------------------- +fn test_12_respond_base_no_annotation() -> Int { + let input: String = "No annotation expected." + let output: String = imprint_respond(input, "base") + return assert_not_contains("12 respond base has no imprint annotation", output, "[imprint:") +} + +// --------------------------------------------------------------------------- +// TEST 13 +// imprint_load() with empty-string ID must return ok==false. +// An empty ID is not a valid imprint identifier. +// --------------------------------------------------------------------------- +fn test_13_load_empty_id_returns_ok_false() -> Int { + let result: String = imprint_load("") + let ok_field: String = json_get(result, "ok") + return assert_eq("13 load empty id ok==false", ok_field, "false") +} + +// --------------------------------------------------------------------------- +// TEST 14 +// After a failed imprint_load(), imprint_current() must still return "base" +// — a failed load must leave state untouched. +// --------------------------------------------------------------------------- +fn test_14_failed_load_does_not_mutate_state() -> Int { + imprint_unload() + let discard: String = imprint_load("__nonexistent_for_state_test__") + let id: String = imprint_current() + return assert_eq("14 failed load leaves state as base", id, "base") +} + +// --------------------------------------------------------------------------- +// run_all — executes every test and prints a summary. +// Returns total failure count as Int. +// --------------------------------------------------------------------------- +fn run_all() -> Int { + println("=== imprint.el test suite ===") + let total: Int = 0 + let failed: Int = 0 + + let failed = failed + test_01_current_after_unload_is_base() + let failed = failed + test_02_unload_idempotent() + let failed = failed + test_03_load_nonexistent_returns_ok_false() + let failed = failed + test_04_load_result_has_ok_field() + let failed = failed + test_05_respond_base_passthrough() + let failed = failed + test_06_respond_empty_id_passthrough() + let failed = failed + test_07_respond_unknown_id_graceful_fallback() + let failed = failed + test_08_respond_after_unload_is_passthrough() + let failed = failed + test_09_surface_knowledge_returns_string() + let failed = failed + test_10_surface_memory_read_returns_string() + let failed = failed + test_11_surface_knowledge_empty_id_equals_base() + let failed = failed + test_12_respond_base_no_annotation() + let failed = failed + test_13_load_empty_id_returns_ok_false() + let failed = failed + test_14_failed_load_does_not_mutate_state() + + let total = 14 + let passed: Int = total - failed + println("=== " + int_to_str(passed) + "/" + int_to_str(total) + " passed ===") + return failed +}