diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 7b26e07..5e13896 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -34,7 +34,7 @@ jobs: - name: Install build dependencies run: | apt-get update -qq - apt-get install -y gcc libcurl4-openssl-dev apt-transport-https ca-certificates + apt-get install -y gcc curl libcurl4-openssl-dev apt-transport-https ca-certificates echo "deb [trusted=yes] https://packages.cloud.google.com/apt cloud-sdk main" \ > /etc/apt/sources.list.d/google-cloud-sdk.list apt-get update -qq && apt-get install -y google-cloud-cli @@ -107,6 +107,17 @@ jobs: strip -s dist/neuron ls -lh dist/neuron + - name: Soul contract gate (HARD BLOCK — no destructive/stale soul publishes) + run: | + # Boots dist/neuron on a throwaway port with a throwaway HOME/engram/cgi + # (never touches ~/.neuron or any live service) and fails the build if any + # app-contract route is unanswered (PRESENCE) or any engram write route + # hard-deletes instead of tombstoning/superseding (IMMUTABILITY). Non-zero + # here blocks Publish -> Artifact Registry -> GKE deploy, so a stale or + # memory-destroying soul can never reach prod. + chmod +x dist/neuron scripts/verify-soul-contract.sh + bash scripts/verify-soul-contract.sh dist/neuron 7796 + - name: Smoke test run: | file dist/neuron diff --git a/awareness.el b/awareness.el index c2d0a6c..1a1fdb0 100644 --- a/awareness.el +++ b/awareness.el @@ -446,8 +446,10 @@ fn respond(action_json: String) -> String { } if str_eq(kind, "forget") { - engram_forget(payload) - return "{\"outcome\":\"forgotten\",\"id\":\"" + payload + "\"}" + // The soul must NOT be able to autonomously hard-delete a memory. + // Tombstone instead (keep node + edges, recoverable). + let _marker: String = mem_tombstone(payload) + return "{\"outcome\":\"tombstoned\",\"id\":\"" + payload + "\"}" } return "{\"outcome\":\"noop\"}" diff --git a/dist/awareness.c b/dist/awareness.c index 58b4544..e346069 100644 --- a/dist/awareness.c +++ b/dist/awareness.c @@ -10,6 +10,7 @@ el_val_t mem_remember(el_val_t content, el_val_t tags); el_val_t mem_recall(el_val_t query, el_val_t depth); el_val_t mem_search(el_val_t query, el_val_t limit); el_val_t mem_strengthen(el_val_t node_id); +el_val_t mem_tombstone(el_val_t node_id); el_val_t mem_forget(el_val_t node_id); el_val_t mem_consolidate(void); el_val_t mem_save(el_val_t path); @@ -362,8 +363,8 @@ el_val_t respond(el_val_t action_json) { return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"strengthened\",\"id\":\""), payload), EL_STR("\"}")); } if (str_eq(kind, EL_STR("forget"))) { - engram_forget(payload); - return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"forgotten\",\"id\":\""), payload), EL_STR("\"}")); + el_val_t _marker = mem_tombstone(payload); + return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"tombstoned\",\"id\":\""), payload), EL_STR("\"}")); } return EL_STR("{\"outcome\":\"noop\"}"); return 0; diff --git a/dist/memory.c b/dist/memory.c index 148120e..fd77cef 100644 --- a/dist/memory.c +++ b/dist/memory.c @@ -10,6 +10,7 @@ el_val_t mem_remember(el_val_t content, el_val_t tags); el_val_t mem_recall(el_val_t query, el_val_t depth); el_val_t mem_search(el_val_t query, el_val_t limit); el_val_t mem_strengthen(el_val_t node_id); +el_val_t mem_tombstone(el_val_t node_id); el_val_t mem_forget(el_val_t node_id); el_val_t mem_consolidate(void); el_val_t mem_save(el_val_t path); @@ -69,8 +70,18 @@ el_val_t mem_strengthen(el_val_t node_id) { return 0; } +el_val_t mem_tombstone(el_val_t node_id) { + el_val_t tags = EL_STR("[\"Tombstone\",\"status:deleted\"]"); + el_val_t marker = engram_node_full(node_id, EL_STR("Tombstone"), el_str_concat(EL_STR("tombstone:"), node_id), el_from_float(0.01), el_from_float(0.01), el_from_float(1.0), EL_STR("Episodic"), tags); + if (!str_eq(marker, EL_STR(""))) { + engram_connect(marker, node_id, el_from_float(1.0), EL_STR("tombstones")); + } + return marker; + return 0; +} + el_val_t mem_forget(el_val_t node_id) { - engram_forget(node_id); + el_val_t _marker = mem_tombstone(node_id); return 0; } diff --git a/dist/neuron-api.c b/dist/neuron-api.c index 03b3d7f..1a97d4a 100644 --- a/dist/neuron-api.c +++ b/dist/neuron-api.c @@ -10,6 +10,7 @@ el_val_t mem_remember(el_val_t content, el_val_t tags); el_val_t mem_recall(el_val_t query, el_val_t depth); el_val_t mem_search(el_val_t query, el_val_t limit); el_val_t mem_strengthen(el_val_t node_id); +el_val_t mem_tombstone(el_val_t node_id); el_val_t mem_forget(el_val_t node_id); el_val_t mem_consolidate(void); el_val_t mem_save(el_val_t path); @@ -28,6 +29,9 @@ el_val_t api_nonempty(el_val_t s); el_val_t api_or_empty(el_val_t s); el_val_t api_persisted(el_val_t id); el_val_t api_not_persisted(el_val_t id); +el_val_t tombstone_node(el_val_t id); +el_val_t tombstoned_id_set(void); +el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path); el_val_t handle_api_begin_session(el_val_t body); el_val_t handle_api_compile_ctx(el_val_t body); el_val_t handle_api_remember(el_val_t body); @@ -189,6 +193,61 @@ el_val_t api_not_persisted(el_val_t id) { return 0; } +el_val_t tombstone_node(el_val_t id) { + return mem_tombstone(id); + return 0; +} + +el_val_t tombstoned_id_set(void) { + el_val_t markers = engram_scan_nodes_by_type_json(EL_STR("Tombstone"), 5000, 0); + if (str_eq(markers, EL_STR("")) || str_eq(markers, EL_STR("[]"))) { + return EL_STR(""); + } + el_val_t n = json_array_len(markers); + el_val_t acc = EL_STR("|"); + el_val_t i = 0; + while (i < n) { + el_val_t m = json_array_get(markers, i); + el_val_t tid = json_get(m, EL_STR("content")); + acc = ({ el_val_t _if_result_1 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_1 = (acc); } else { _if_result_1 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_1; }); + i = (i + 1); + } + return acc; + return 0; +} + +el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path) { + if (str_contains(path, EL_STR("include_deleted"))) { + return raw; + } + if (str_eq(raw, EL_STR("")) || str_eq(raw, EL_STR("[]"))) { + return raw; + } + el_val_t dead = tombstoned_id_set(); + if (str_eq(dead, EL_STR(""))) { + return raw; + } + el_val_t n = json_array_len(raw); + if (n > 1000) { + return raw; + } + el_val_t out = EL_STR("["); + el_val_t first = 1; + el_val_t i = 0; + while (i < n) { + el_val_t node = json_array_get(raw, i); + el_val_t nid = json_get(node, EL_STR("id")); + el_val_t ntype = json_get(node, EL_STR("node_type")); + el_val_t is_dead = (!str_eq(nid, EL_STR("")) && str_contains(dead, el_str_concat(el_str_concat(EL_STR("|"), nid), EL_STR("|")))); + el_val_t keep = (!str_eq(ntype, EL_STR("Tombstone")) && !is_dead); + out = ({ el_val_t _if_result_2 = 0; if (keep) { _if_result_2 = (({ el_val_t _if_result_3 = 0; if (first) { _if_result_3 = (el_str_concat(out, node)); } else { _if_result_3 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_3; })); } else { _if_result_2 = (out); } _if_result_2; }); + first = ({ el_val_t _if_result_4 = 0; if (keep) { _if_result_4 = (0); } else { _if_result_4 = (first); } _if_result_4; }); + i = (i + 1); + } + return el_str_concat(out, EL_STR("]")); + return 0; +} + el_val_t handle_api_begin_session(el_val_t body) { el_val_t stats = engram_stats_json(); el_val_t activated = engram_activate_json(EL_STR("session start recent memory important"), 2); @@ -215,10 +274,10 @@ el_val_t handle_api_remember(el_val_t body) { el_val_t importance = json_get(body, EL_STR("importance")); el_val_t tags_raw = json_get(body, EL_STR("tags")); el_val_t project = json_get(body, EL_STR("project")); - el_val_t sal_str = ({ el_val_t _if_result_1 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_1 = (EL_STR("0.95")); } else { _if_result_1 = (({ el_val_t _if_result_2 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_2 = (EL_STR("0.75")); } else { _if_result_2 = (({ el_val_t _if_result_3 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_3 = (EL_STR("0.25")); } else { _if_result_3 = (EL_STR("0.50")); } _if_result_3; })); } _if_result_2; })); } _if_result_1; }); - el_val_t sal = ({ el_val_t _if_result_4 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_4 = (el_from_float(0.95)); } else { _if_result_4 = (({ el_val_t _if_result_5 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_5 = (el_from_float(0.75)); } else { _if_result_5 = (({ el_val_t _if_result_6 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_6 = (el_from_float(0.25)); } else { _if_result_6 = (el_from_float(0.5)); } _if_result_6; })); } _if_result_5; })); } _if_result_4; }); - el_val_t base_tags = ({ el_val_t _if_result_7 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_7 = (EL_STR("[\"Memory\"]")); } else { _if_result_7 = (tags_raw); } _if_result_7; }); - el_val_t final_tags = ({ el_val_t _if_result_8 = 0; if (str_eq(project, EL_STR(""))) { _if_result_8 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_8 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_8; }); + el_val_t sal_str = ({ el_val_t _if_result_5 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_5 = (EL_STR("0.95")); } else { _if_result_5 = (({ el_val_t _if_result_6 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_6 = (EL_STR("0.75")); } else { _if_result_6 = (({ el_val_t _if_result_7 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_7 = (EL_STR("0.25")); } else { _if_result_7 = (EL_STR("0.50")); } _if_result_7; })); } _if_result_6; })); } _if_result_5; }); + el_val_t sal = ({ el_val_t _if_result_8 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_8 = (el_from_float(0.95)); } else { _if_result_8 = (({ el_val_t _if_result_9 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_9 = (el_from_float(0.75)); } else { _if_result_9 = (({ el_val_t _if_result_10 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_10 = (el_from_float(0.25)); } else { _if_result_10 = (el_from_float(0.5)); } _if_result_10; })); } _if_result_9; })); } _if_result_8; }); + el_val_t base_tags = ({ el_val_t _if_result_11 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_11 = (EL_STR("[\"Memory\"]")); } else { _if_result_11 = (tags_raw); } _if_result_11; }); + el_val_t final_tags = ({ el_val_t _if_result_12 = 0; if (str_eq(project, EL_STR(""))) { _if_result_12 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_12 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_12; }); el_val_t id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:remembered"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), final_tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -233,15 +292,15 @@ el_val_t handle_api_node_create(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t nt_raw = json_get(body, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_9 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_9 = (EL_STR("Memory")); } else { _if_result_9 = (nt_raw); } _if_result_9; }); + el_val_t node_type = ({ el_val_t _if_result_13 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_13 = (EL_STR("Memory")); } else { _if_result_13 = (nt_raw); } _if_result_13; }); el_val_t label_raw = json_get(body, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_10 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_10 = (EL_STR("node:created")); } else { _if_result_10 = (label_raw); } _if_result_10; }); + el_val_t label = ({ el_val_t _if_result_14 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_14 = (EL_STR("node:created")); } else { _if_result_14 = (label_raw); } _if_result_14; }); el_val_t tier_raw = json_get(body, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_11 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_11 = (EL_STR("Episodic")); } else { _if_result_11 = (tier_raw); } _if_result_11; }); + el_val_t tier = ({ el_val_t _if_result_15 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_15 = (EL_STR("Episodic")); } else { _if_result_15 = (tier_raw); } _if_result_15; }); el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_12 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_12 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_12 = (tags_raw); } _if_result_12; }); + el_val_t tags = ({ el_val_t _if_result_16 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_16 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_16 = (tags_raw); } _if_result_16; }); el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_13 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_13 = (el_from_float(0.95)); } else { _if_result_13 = (({ el_val_t _if_result_14 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_14 = (el_from_float(0.75)); } else { _if_result_14 = (({ el_val_t _if_result_15 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_15 = (el_from_float(0.25)); } else { _if_result_15 = (el_from_float(0.5)); } _if_result_15; })); } _if_result_14; })); } _if_result_13; }); + el_val_t sal = ({ el_val_t _if_result_17 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_17 = (el_from_float(0.95)); } else { _if_result_17 = (({ el_val_t _if_result_18 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_18 = (el_from_float(0.75)); } else { _if_result_18 = (({ el_val_t _if_result_19 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_19 = (el_from_float(0.25)); } else { _if_result_19 = (el_from_float(0.5)); } _if_result_19; })); } _if_result_18; })); } _if_result_17; }); el_val_t id = engram_node_full(content, node_type, label, el_from_float(sal), el_from_float(sal), el_from_float(0.9), tier, tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -255,8 +314,18 @@ el_val_t handle_api_node_delete(el_val_t body) { if (str_eq(id, EL_STR(""))) { return api_err(EL_STR("id is required")); } - engram_forget(id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}")); + if (is_protected_node(id)) { + return api_err_protected(id); + } + el_val_t existing = engram_get_node_json(id); + if (str_eq(existing, EL_STR("{}"))) { + return api_err(el_str_concat(EL_STR("node not found: "), id)); + } + el_val_t marker = tombstone_node(id); + if (str_eq(marker, EL_STR(""))) { + return api_err(el_str_concat(EL_STR("tombstone failed: "), id)); + } + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\",\"tombstoned\":true}")); return 0; } @@ -270,37 +339,37 @@ el_val_t handle_api_node_update(el_val_t body) { } el_val_t old = engram_get_node_json(id); el_val_t body_content = json_get(body, EL_STR("content")); - el_val_t content = ({ el_val_t _if_result_16 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_16 = (json_get(old, EL_STR("content"))); } else { _if_result_16 = (body_content); } _if_result_16; }); + el_val_t content = ({ el_val_t _if_result_20 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_20 = (json_get(old, EL_STR("content"))); } else { _if_result_20 = (body_content); } _if_result_20; }); el_val_t body_nt = json_get(body, EL_STR("node_type")); el_val_t old_nt = json_get(old, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_17 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_17 = (body_nt); } else { _if_result_17 = (({ el_val_t _if_result_18 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_18 = (old_nt); } else { _if_result_18 = (EL_STR("Memory")); } _if_result_18; })); } _if_result_17; }); + el_val_t node_type = ({ el_val_t _if_result_21 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_21 = (body_nt); } else { _if_result_21 = (({ el_val_t _if_result_22 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_22 = (old_nt); } else { _if_result_22 = (EL_STR("Memory")); } _if_result_22; })); } _if_result_21; }); el_val_t body_label = json_get(body, EL_STR("label")); el_val_t old_label = json_get(old, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_19 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_19 = (body_label); } else { _if_result_19 = (({ el_val_t _if_result_20 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_20 = (old_label); } else { _if_result_20 = (EL_STR("node:updated")); } _if_result_20; })); } _if_result_19; }); + el_val_t label = ({ el_val_t _if_result_23 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_23 = (body_label); } else { _if_result_23 = (({ el_val_t _if_result_24 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_24 = (old_label); } else { _if_result_24 = (EL_STR("node:updated")); } _if_result_24; })); } _if_result_23; }); el_val_t body_tier = json_get(body, EL_STR("tier")); el_val_t old_tier = json_get(old, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_21 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_21 = (body_tier); } else { _if_result_21 = (({ el_val_t _if_result_22 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_22 = (old_tier); } else { _if_result_22 = (EL_STR("Episodic")); } _if_result_22; })); } _if_result_21; }); + el_val_t tier = ({ el_val_t _if_result_25 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_25 = (body_tier); } else { _if_result_25 = (({ el_val_t _if_result_26 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_26 = (old_tier); } else { _if_result_26 = (EL_STR("Episodic")); } _if_result_26; })); } _if_result_25; }); el_val_t body_tags = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_23 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_23 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_23 = (body_tags); } _if_result_23; }); + el_val_t tags = ({ el_val_t _if_result_27 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_27 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_27 = (body_tags); } _if_result_27; }); el_val_t new_id = engram_node_full(content, node_type, label, el_from_float(0.5), el_from_float(0.5), el_from_float(0.8), tier, tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); } - engram_forget(id); - return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), new_id), EL_STR("\",\"replaced\":\"")), id), EL_STR("\",\"ok\":true}")); + engram_connect(new_id, id, el_from_float(0.9), EL_STR("supersedes")); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), new_id), EL_STR("\",\"supersedes\":\"")), id), EL_STR("\",\"ok\":true}")); return 0; } el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body) { - el_val_t url_q = ({ el_val_t _if_result_24 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_24 = (api_query_param(path, EL_STR("q"))); } else { _if_result_24 = (api_query_param(path, EL_STR("query"))); } _if_result_24; }); + el_val_t url_q = ({ el_val_t _if_result_28 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_28 = (api_query_param(path, EL_STR("q"))); } else { _if_result_28 = (api_query_param(path, EL_STR("query"))); } _if_result_28; }); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_25 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_25 = (url_q); } else { _if_result_25 = (({ el_val_t _if_result_26 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_26 = (body_query); } else { _if_result_26 = (body_q); } _if_result_26; })); } _if_result_25; }); + el_val_t q = ({ el_val_t _if_result_29 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_29 = (url_q); } else { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_30 = (body_query); } else { _if_result_30 = (body_q); } _if_result_30; })); } _if_result_29; }); el_val_t chain = json_get(body, EL_STR("chain_name")); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_27 = 0; if ((limit == 0)) { _if_result_27 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_27 = (limit); } _if_result_27; }); - limit = ({ el_val_t _if_result_28 = 0; if ((limit == 0)) { _if_result_28 = (10); } else { _if_result_28 = (limit); } _if_result_28; }); - el_val_t eff_q = ({ el_val_t _if_result_29 = 0; if (str_eq(q, EL_STR(""))) { _if_result_29 = (chain); } else { _if_result_29 = (q); } _if_result_29; }); + limit = ({ el_val_t _if_result_31 = 0; if ((limit == 0)) { _if_result_31 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_31 = (limit); } _if_result_31; }); + limit = ({ el_val_t _if_result_32 = 0; if ((limit == 0)) { _if_result_32 = (10); } else { _if_result_32 = (limit); } _if_result_32; }); + el_val_t eff_q = ({ el_val_t _if_result_33 = 0; if (str_eq(q, EL_STR(""))) { _if_result_33 = (chain); } else { _if_result_33 = (q); } _if_result_33; }); if (str_eq(eff_q, EL_STR(""))) { return api_or_empty(engram_scan_nodes_json(limit, 0)); } @@ -313,10 +382,10 @@ el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t bo el_val_t url_q = api_query_param(path, EL_STR("q")); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_30 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_30 = (url_q); } else { _if_result_30 = (({ el_val_t _if_result_31 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_31 = (body_query); } else { _if_result_31 = (body_q); } _if_result_31; })); } _if_result_30; }); + el_val_t q = ({ el_val_t _if_result_34 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_34 = (url_q); } else { _if_result_34 = (({ el_val_t _if_result_35 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_35 = (body_query); } else { _if_result_35 = (body_q); } _if_result_35; })); } _if_result_34; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_32 = 0; if ((limit == 0)) { _if_result_32 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_32 = (limit); } _if_result_32; }); - limit = ({ el_val_t _if_result_33 = 0; if ((limit == 0)) { _if_result_33 = (10); } else { _if_result_33 = (limit); } _if_result_33; }); + limit = ({ el_val_t _if_result_36 = 0; if ((limit == 0)) { _if_result_36 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_36 = (limit); } _if_result_36; }); + limit = ({ el_val_t _if_result_37 = 0; if ((limit == 0)) { _if_result_37 = (10); } else { _if_result_37 = (limit); } _if_result_37; }); if (str_eq(q, EL_STR(""))) { return api_err(EL_STR("query is required")); } @@ -344,7 +413,7 @@ el_val_t handle_api_capture_knowledge(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t full = ({ el_val_t _if_result_34 = 0; if (str_eq(title, EL_STR(""))) { _if_result_34 = (content); } else { _if_result_34 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_34; }); + el_val_t full = ({ el_val_t _if_result_38 = 0; if (str_eq(title, EL_STR(""))) { _if_result_38 = (content); } else { _if_result_38 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_38; }); el_val_t tags = EL_STR("[\"Knowledge\",\"captured\"]"); el_val_t id = engram_node_full(full, EL_STR("Knowledge"), EL_STR("knowledge:captured"), el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags); if (!api_persisted(id)) { @@ -385,7 +454,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { return api_err(EL_STR("id (prior node) is required")); } el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_35 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_35 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_35 = (tags_raw); } _if_result_35; }); + el_val_t tags = ({ el_val_t _if_result_39 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_39 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_39 = (tags_raw); } _if_result_39; }); el_val_t new_id = engram_node_full(content, EL_STR("Knowledge"), EL_STR("knowledge:canonical"), el_from_float(0.9), el_from_float(0.9), el_from_float(1.0), EL_STR("Canonical"), tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); @@ -396,7 +465,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { } el_val_t handle_api_browse_processes(el_val_t method, el_val_t path, el_val_t body) { - el_val_t name = ({ el_val_t _if_result_36 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_36 = (api_query_param(path, EL_STR("name"))); } else { _if_result_36 = (json_get(body, EL_STR("name"))); } _if_result_36; }); + el_val_t name = ({ el_val_t _if_result_40 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_40 = (api_query_param(path, EL_STR("name"))); } else { _if_result_40 = (json_get(body, EL_STR("name"))); } _if_result_40; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 50); if (str_eq(name, EL_STR(""))) { return api_or_empty(engram_scan_nodes_by_type_json(EL_STR("Process"), limit, 0)); @@ -411,7 +480,7 @@ el_val_t handle_api_define_process(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t label = ({ el_val_t _if_result_37 = 0; if (str_eq(name, EL_STR(""))) { _if_result_37 = (EL_STR("process:unnamed")); } else { _if_result_37 = (el_str_concat(EL_STR("process:"), name)); } _if_result_37; }); + el_val_t label = ({ el_val_t _if_result_41 = 0; if (str_eq(name, EL_STR(""))) { _if_result_41 = (EL_STR("process:unnamed")); } else { _if_result_41 = (el_str_concat(EL_STR("process:"), name)); } _if_result_41; }); el_val_t tags = EL_STR("[\"Process\"]"); el_val_t id = engram_node_full(content, EL_STR("Process"), label, el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Canonical"), tags); if (!api_persisted(id)) { @@ -429,12 +498,12 @@ el_val_t handle_api_log_state_event(el_val_t body) { el_val_t gap = json_get(body, EL_STR("gap_direction")); el_val_t legacy = json_get(body, EL_STR("content")); el_val_t parts = EL_STR("INTERNAL STATE EVENT"); - parts = ({ el_val_t _if_result_38 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_38 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_38 = (parts); } _if_result_38; }); - parts = ({ el_val_t _if_result_39 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_39 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_39 = (parts); } _if_result_39; }); - parts = ({ el_val_t _if_result_40 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_40 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_40 = (parts); } _if_result_40; }); - parts = ({ el_val_t _if_result_41 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_41 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_41 = (parts); } _if_result_41; }); - parts = ({ el_val_t _if_result_42 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_42 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_42 = (parts); } _if_result_42; }); - parts = ({ el_val_t _if_result_43 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_43 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_43 = (parts); } _if_result_43; }); + parts = ({ el_val_t _if_result_42 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_42 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_42 = (parts); } _if_result_42; }); + parts = ({ el_val_t _if_result_43 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_43 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_43 = (parts); } _if_result_43; }); + parts = ({ el_val_t _if_result_44 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_44 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_44 = (parts); } _if_result_44; }); + parts = ({ el_val_t _if_result_45 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_45 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_45 = (parts); } _if_result_45; }); + parts = ({ el_val_t _if_result_46 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_46 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_46 = (parts); } _if_result_46; }); + parts = ({ el_val_t _if_result_47 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_47 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_47 = (parts); } _if_result_47; }); el_val_t ts = time_now(); el_val_t boot = state_get(EL_STR("soul_boot_count")); el_val_t tags = EL_STR("[\"internal-state\",\"InternalStateEvent\",\"pre-reasoning\"]"); @@ -447,7 +516,7 @@ el_val_t handle_api_log_state_event(el_val_t body) { } el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t body) { - el_val_t q = ({ el_val_t _if_result_44 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_44 = (api_query_param(path, EL_STR("query"))); } else { _if_result_44 = (json_get(body, EL_STR("query"))); } _if_result_44; }); + el_val_t q = ({ el_val_t _if_result_48 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_48 = (api_query_param(path, EL_STR("query"))); } else { _if_result_48 = (json_get(body, EL_STR("query"))); } _if_result_48; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 20); if (!str_eq(q, EL_STR(""))) { return api_or_empty(engram_search_json(el_str_concat(EL_STR("internal state "), q), limit)); @@ -458,7 +527,7 @@ el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t b el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t key = api_query_param(path, EL_STR("key")); - key = ({ el_val_t _if_result_45 = 0; if (str_eq(key, EL_STR(""))) { _if_result_45 = (json_get(body, EL_STR("key"))); } else { _if_result_45 = (key); } _if_result_45; }); + key = ({ el_val_t _if_result_49 = 0; if (str_eq(key, EL_STR(""))) { _if_result_49 = (json_get(body, EL_STR("key"))); } else { _if_result_49 = (key); } _if_result_49; }); if (str_eq(key, EL_STR(""))) { return EL_STR("{\"hint\":\"pass ?key=\",\"known\":[\"neuron.self.traversal_root\",\"neuron.self.values_hub\"]}"); } @@ -475,7 +544,7 @@ el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t node = json_array_get(results, 0); el_val_t content = json_get(node, EL_STR("content")); el_val_t prefix = el_str_concat(el_str_concat(EL_STR("config:"), key), EL_STR("=")); - el_val_t value = ({ el_val_t _if_result_46 = 0; if (str_starts_with(content, prefix)) { _if_result_46 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_46 = (content); } _if_result_46; }); + el_val_t value = ({ el_val_t _if_result_50 = 0; if (str_starts_with(content, prefix)) { _if_result_50 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_50 = (content); } _if_result_50; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"key\":\""), key), EL_STR("\",\"value\":\"")), value), EL_STR("\"}")); return 0; } @@ -497,13 +566,13 @@ el_val_t handle_api_tune_config(el_val_t body) { } el_val_t handle_api_inspect_graph(el_val_t method, el_val_t path, el_val_t body) { - el_val_t entity_id = ({ el_val_t _if_result_47 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_47 = (api_query_param(path, EL_STR("id"))); } else { _if_result_47 = (json_get(body, EL_STR("entity_id"))); } _if_result_47; }); - el_val_t name = ({ el_val_t _if_result_48 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_48 = (api_query_param(path, EL_STR("name"))); } else { _if_result_48 = (json_get(body, EL_STR("name"))); } _if_result_48; }); + el_val_t entity_id = ({ el_val_t _if_result_51 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_51 = (api_query_param(path, EL_STR("id"))); } else { _if_result_51 = (json_get(body, EL_STR("entity_id"))); } _if_result_51; }); + el_val_t name = ({ el_val_t _if_result_52 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_52 = (api_query_param(path, EL_STR("name"))); } else { _if_result_52 = (json_get(body, EL_STR("name"))); } _if_result_52; }); el_val_t depth = api_query_int(path, EL_STR("depth"), 0); - depth = ({ el_val_t _if_result_49 = 0; if ((depth == 0)) { _if_result_49 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_49 = (depth); } _if_result_49; }); - depth = ({ el_val_t _if_result_50 = 0; if ((depth == 0)) { _if_result_50 = (1); } else { _if_result_50 = (depth); } _if_result_50; }); + depth = ({ el_val_t _if_result_53 = 0; if ((depth == 0)) { _if_result_53 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_53 = (depth); } _if_result_53; }); + depth = ({ el_val_t _if_result_54 = 0; if ((depth == 0)) { _if_result_54 = (1); } else { _if_result_54 = (depth); } _if_result_54; }); el_val_t resolved = entity_id; - resolved = ({ el_val_t _if_result_51 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_51 = (({ el_val_t _if_result_52 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_52 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_52 = (({ el_val_t _if_result_53 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_53 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_53 = (EL_STR("")); } _if_result_53; })); } _if_result_52; })); } else { _if_result_51 = (resolved); } _if_result_51; }); + resolved = ({ el_val_t _if_result_55 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_55 = (({ el_val_t _if_result_56 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_56 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_56 = (({ el_val_t _if_result_57 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_57 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_57 = (EL_STR("")); } _if_result_57; })); } _if_result_56; })); } else { _if_result_55 = (resolved); } _if_result_55; }); if (str_eq(resolved, EL_STR(""))) { return api_err(EL_STR("entity_id or name required. Known names: self, neuron, values, values_hub")); } @@ -525,7 +594,7 @@ el_val_t handle_api_link_entities(el_val_t body) { return api_err_protected(to_id); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_54 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_54 = (EL_STR("associates")); } else { _if_result_54 = (relation); } _if_result_54; }); + el_val_t eff_relation = ({ el_val_t _if_result_58 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_58 = (EL_STR("associates")); } else { _if_result_58 = (relation); } _if_result_58; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\"}")); return 0; @@ -540,7 +609,7 @@ el_val_t handle_api_forget(el_val_t body) { return api_err_protected(node_id); } mem_forget(node_id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\"}")); + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"tombstoned\":true}")); return 0; } @@ -554,8 +623,8 @@ el_val_t handle_api_evolve_memory(el_val_t body) { return api_err_protected(prior_id); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal_str = ({ el_val_t _if_result_55 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_55 = (EL_STR("0.95")); } else { _if_result_55 = (({ el_val_t _if_result_56 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_56 = (EL_STR("0.75")); } else { _if_result_56 = (({ el_val_t _if_result_57 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_57 = (EL_STR("0.25")); } else { _if_result_57 = (EL_STR("0.50")); } _if_result_57; })); } _if_result_56; })); } _if_result_55; }); - el_val_t sal = ({ el_val_t _if_result_58 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_58 = (el_from_float(0.95)); } else { _if_result_58 = (({ el_val_t _if_result_59 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_59 = (el_from_float(0.75)); } else { _if_result_59 = (({ el_val_t _if_result_60 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_60 = (el_from_float(0.25)); } else { _if_result_60 = (el_from_float(0.5)); } _if_result_60; })); } _if_result_59; })); } _if_result_58; }); + el_val_t sal_str = ({ el_val_t _if_result_59 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_59 = (EL_STR("0.95")); } else { _if_result_59 = (({ el_val_t _if_result_60 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_60 = (EL_STR("0.75")); } else { _if_result_60 = (({ el_val_t _if_result_61 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_61 = (EL_STR("0.25")); } else { _if_result_61 = (EL_STR("0.50")); } _if_result_61; })); } _if_result_60; })); } _if_result_59; }); + el_val_t sal = ({ el_val_t _if_result_62 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_62 = (el_from_float(0.95)); } else { _if_result_62 = (({ el_val_t _if_result_63 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_63 = (el_from_float(0.75)); } else { _if_result_63 = (({ el_val_t _if_result_64 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_64 = (el_from_float(0.25)); } else { _if_result_64 = (el_from_float(0.5)); } _if_result_64; })); } _if_result_63; })); } _if_result_62; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:evolved"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -577,8 +646,11 @@ el_val_t handle_api_memory_delete(el_val_t body) { if (str_eq(existing, EL_STR("{}"))) { return api_err(el_str_concat(EL_STR("memory not found: "), node_id)); } - mem_forget(node_id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"deleted\":true}")); + el_val_t marker = tombstone_node(node_id); + if (str_eq(marker, EL_STR(""))) { + return api_err(el_str_concat(EL_STR("tombstone failed: "), node_id)); + } + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"tombstoned\":true}")); return 0; } @@ -627,7 +699,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_61 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_61 = (el_from_float(0.95)); } else { _if_result_61 = (({ el_val_t _if_result_62 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_62 = (el_from_float(0.75)); } else { _if_result_62 = (({ el_val_t _if_result_63 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_63 = (el_from_float(0.25)); } else { _if_result_63 = (el_from_float(0.5)); } _if_result_63; })); } _if_result_62; })); } _if_result_61; }); + el_val_t sal = ({ el_val_t _if_result_65 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_65 = (el_from_float(0.95)); } else { _if_result_65 = (({ el_val_t _if_result_66 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_66 = (el_from_float(0.75)); } else { _if_result_66 = (({ el_val_t _if_result_67 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_67 = (el_from_float(0.25)); } else { _if_result_67 = (el_from_float(0.5)); } _if_result_67; })); } _if_result_66; })); } _if_result_65; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\",\"cultivated\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:cultivated"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -641,7 +713,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("id is required")); } mem_forget(node_id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"cultivated\":true}")); + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"tombstoned\":true,\"cultivated\":true}")); } if (str_eq(op, EL_STR("link_entities"))) { el_val_t from_id = json_get(body, EL_STR("from_id")); @@ -653,7 +725,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("to_id is required")); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_64 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_64 = (EL_STR("associates")); } else { _if_result_64 = (relation); } _if_result_64; }); + el_val_t eff_relation = ({ el_val_t _if_result_68 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_68 = (EL_STR("associates")); } else { _if_result_68 = (relation); } _if_result_68; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\",\"cultivated\":true}")); } @@ -663,7 +735,8 @@ el_val_t handle_api_cultivate(el_val_t body) { el_val_t handle_api_list_typed(el_val_t node_type, el_val_t path, el_val_t body) { el_val_t limit = api_query_int(path, EL_STR("limit"), 50); - return api_or_empty(engram_scan_nodes_by_type_json(node_type, limit, 0)); + el_val_t raw = api_or_empty(engram_scan_nodes_by_type_json(node_type, limit, 0)); + return memory_hide_tombstoned(raw, path); return 0; } diff --git a/dist/soul.c b/dist/soul.c index da0d007..db71e56 100644 --- a/dist/soul.c +++ b/dist/soul.c @@ -963,6 +963,7 @@ el_val_t mem_remember(el_val_t content, el_val_t tags); el_val_t mem_recall(el_val_t query, el_val_t depth); el_val_t mem_search(el_val_t query, el_val_t limit); el_val_t mem_strengthen(el_val_t node_id); +el_val_t mem_tombstone(el_val_t node_id); el_val_t mem_forget(el_val_t node_id); el_val_t mem_consolidate(void); el_val_t mem_save(el_val_t path); @@ -1134,6 +1135,9 @@ el_val_t api_nonempty(el_val_t s); el_val_t api_or_empty(el_val_t s); el_val_t api_persisted(el_val_t id); el_val_t api_not_persisted(el_val_t id); +el_val_t tombstone_node(el_val_t id); +el_val_t tombstoned_id_set(void); +el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path); el_val_t handle_api_begin_session(el_val_t body); el_val_t handle_api_compile_ctx(el_val_t body); el_val_t handle_api_remember(el_val_t body); @@ -1177,6 +1181,11 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist); el_val_t session_update_meta_timestamp(el_val_t session_id); el_val_t session_auto_title(el_val_t session_id, el_val_t first_message); el_val_t handle_session_approve(el_val_t session_id, el_val_t body); +el_val_t init_soul_edges(void); +el_val_t load_identity_context(void); +el_val_t seed_persona_from_env(void); +el_val_t emit_session_start_event(void); +el_val_t layered_cycle(el_val_t raw_input); el_val_t flag_true(el_val_t body, el_val_t key); el_val_t rate_limit_check(el_val_t ip, el_val_t path); el_val_t strip_query(el_val_t path); @@ -25323,8 +25332,18 @@ el_val_t mem_strengthen(el_val_t node_id) { return 0; } +el_val_t mem_tombstone(el_val_t node_id) { + el_val_t tags = EL_STR("[\"Tombstone\",\"status:deleted\"]"); + el_val_t marker = engram_node_full(node_id, EL_STR("Tombstone"), el_str_concat(EL_STR("tombstone:"), node_id), el_from_float(0.01), el_from_float(0.01), el_from_float(1.0), EL_STR("Episodic"), tags); + if (!str_eq(marker, EL_STR(""))) { + engram_connect(marker, node_id, el_from_float(1.0), EL_STR("tombstones")); + } + return marker; + return 0; +} + el_val_t mem_forget(el_val_t node_id) { - engram_forget(node_id); + el_val_t _marker = mem_tombstone(node_id); return 0; } @@ -26397,8 +26416,8 @@ el_val_t respond(el_val_t action_json) { return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"strengthened\",\"id\":\""), payload), EL_STR("\"}")); } if (str_eq(kind, EL_STR("forget"))) { - engram_forget(payload); - return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"forgotten\",\"id\":\""), payload), EL_STR("\"}")); + el_val_t _marker = mem_tombstone(payload); + return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"tombstoned\",\"id\":\""), payload), EL_STR("\"}")); } return EL_STR("{\"outcome\":\"noop\"}"); return 0; @@ -28719,6 +28738,61 @@ el_val_t api_not_persisted(el_val_t id) { return 0; } +el_val_t tombstone_node(el_val_t id) { + return mem_tombstone(id); + return 0; +} + +el_val_t tombstoned_id_set(void) { + el_val_t markers = engram_scan_nodes_by_type_json(EL_STR("Tombstone"), 5000, 0); + if (str_eq(markers, EL_STR("")) || str_eq(markers, EL_STR("[]"))) { + return EL_STR(""); + } + el_val_t n = json_array_len(markers); + el_val_t acc = EL_STR("|"); + el_val_t i = 0; + while (i < n) { + el_val_t m = json_array_get(markers, i); + el_val_t tid = json_get(m, EL_STR("content")); + acc = ({ el_val_t _if_result_482 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_482 = (acc); } else { _if_result_482 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_482; }); + i = (i + 1); + } + return acc; + return 0; +} + +el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path) { + if (str_contains(path, EL_STR("include_deleted"))) { + return raw; + } + if (str_eq(raw, EL_STR("")) || str_eq(raw, EL_STR("[]"))) { + return raw; + } + el_val_t dead = tombstoned_id_set(); + if (str_eq(dead, EL_STR(""))) { + return raw; + } + el_val_t n = json_array_len(raw); + if (n > 1000) { + return raw; + } + el_val_t out = EL_STR("["); + el_val_t first = 1; + el_val_t i = 0; + while (i < n) { + el_val_t node = json_array_get(raw, i); + el_val_t nid = json_get(node, EL_STR("id")); + el_val_t ntype = json_get(node, EL_STR("node_type")); + el_val_t is_dead = (!str_eq(nid, EL_STR("")) && str_contains(dead, el_str_concat(el_str_concat(EL_STR("|"), nid), EL_STR("|")))); + el_val_t keep = (!str_eq(ntype, EL_STR("Tombstone")) && !is_dead); + out = ({ el_val_t _if_result_483 = 0; if (keep) { _if_result_483 = (({ el_val_t _if_result_484 = 0; if (first) { _if_result_484 = (el_str_concat(out, node)); } else { _if_result_484 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_484; })); } else { _if_result_483 = (out); } _if_result_483; }); + first = ({ el_val_t _if_result_485 = 0; if (keep) { _if_result_485 = (0); } else { _if_result_485 = (first); } _if_result_485; }); + i = (i + 1); + } + return el_str_concat(out, EL_STR("]")); + return 0; +} + el_val_t handle_api_begin_session(el_val_t body) { el_val_t stats = engram_stats_json(); el_val_t activated = engram_activate_json(EL_STR("session start recent memory important"), 2); @@ -28745,10 +28819,10 @@ el_val_t handle_api_remember(el_val_t body) { el_val_t importance = json_get(body, EL_STR("importance")); el_val_t tags_raw = json_get(body, EL_STR("tags")); el_val_t project = json_get(body, EL_STR("project")); - el_val_t sal_str = ({ el_val_t _if_result_482 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_482 = (EL_STR("0.95")); } else { _if_result_482 = (({ el_val_t _if_result_483 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_483 = (EL_STR("0.75")); } else { _if_result_483 = (({ el_val_t _if_result_484 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_484 = (EL_STR("0.25")); } else { _if_result_484 = (EL_STR("0.50")); } _if_result_484; })); } _if_result_483; })); } _if_result_482; }); - el_val_t sal = ({ el_val_t _if_result_485 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_485 = (el_from_float(0.95)); } else { _if_result_485 = (({ el_val_t _if_result_486 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_486 = (el_from_float(0.75)); } else { _if_result_486 = (({ el_val_t _if_result_487 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_487 = (el_from_float(0.25)); } else { _if_result_487 = (el_from_float(0.5)); } _if_result_487; })); } _if_result_486; })); } _if_result_485; }); - el_val_t base_tags = ({ el_val_t _if_result_488 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_488 = (EL_STR("[\"Memory\"]")); } else { _if_result_488 = (tags_raw); } _if_result_488; }); - el_val_t final_tags = ({ el_val_t _if_result_489 = 0; if (str_eq(project, EL_STR(""))) { _if_result_489 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_489 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_489; }); + el_val_t sal_str = ({ el_val_t _if_result_486 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_486 = (EL_STR("0.95")); } else { _if_result_486 = (({ el_val_t _if_result_487 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_487 = (EL_STR("0.75")); } else { _if_result_487 = (({ el_val_t _if_result_488 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_488 = (EL_STR("0.25")); } else { _if_result_488 = (EL_STR("0.50")); } _if_result_488; })); } _if_result_487; })); } _if_result_486; }); + el_val_t sal = ({ el_val_t _if_result_489 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_489 = (el_from_float(0.95)); } else { _if_result_489 = (({ el_val_t _if_result_490 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_490 = (el_from_float(0.75)); } else { _if_result_490 = (({ el_val_t _if_result_491 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_491 = (el_from_float(0.25)); } else { _if_result_491 = (el_from_float(0.5)); } _if_result_491; })); } _if_result_490; })); } _if_result_489; }); + el_val_t base_tags = ({ el_val_t _if_result_492 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_492 = (EL_STR("[\"Memory\"]")); } else { _if_result_492 = (tags_raw); } _if_result_492; }); + el_val_t final_tags = ({ el_val_t _if_result_493 = 0; if (str_eq(project, EL_STR(""))) { _if_result_493 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_493 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_493; }); el_val_t id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:remembered"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), final_tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -28763,15 +28837,15 @@ el_val_t handle_api_node_create(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t nt_raw = json_get(body, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_490 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_490 = (EL_STR("Memory")); } else { _if_result_490 = (nt_raw); } _if_result_490; }); + el_val_t node_type = ({ el_val_t _if_result_494 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_494 = (EL_STR("Memory")); } else { _if_result_494 = (nt_raw); } _if_result_494; }); el_val_t label_raw = json_get(body, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_491 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_491 = (EL_STR("node:created")); } else { _if_result_491 = (label_raw); } _if_result_491; }); + el_val_t label = ({ el_val_t _if_result_495 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_495 = (EL_STR("node:created")); } else { _if_result_495 = (label_raw); } _if_result_495; }); el_val_t tier_raw = json_get(body, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_492 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_492 = (EL_STR("Episodic")); } else { _if_result_492 = (tier_raw); } _if_result_492; }); + el_val_t tier = ({ el_val_t _if_result_496 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_496 = (EL_STR("Episodic")); } else { _if_result_496 = (tier_raw); } _if_result_496; }); el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_493 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_493 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_493 = (tags_raw); } _if_result_493; }); + el_val_t tags = ({ el_val_t _if_result_497 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_497 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_497 = (tags_raw); } _if_result_497; }); el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_494 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_494 = (el_from_float(0.95)); } else { _if_result_494 = (({ el_val_t _if_result_495 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_495 = (el_from_float(0.75)); } else { _if_result_495 = (({ el_val_t _if_result_496 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_496 = (el_from_float(0.25)); } else { _if_result_496 = (el_from_float(0.5)); } _if_result_496; })); } _if_result_495; })); } _if_result_494; }); + el_val_t sal = ({ el_val_t _if_result_498 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_498 = (el_from_float(0.95)); } else { _if_result_498 = (({ el_val_t _if_result_499 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_499 = (el_from_float(0.75)); } else { _if_result_499 = (({ el_val_t _if_result_500 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_500 = (el_from_float(0.25)); } else { _if_result_500 = (el_from_float(0.5)); } _if_result_500; })); } _if_result_499; })); } _if_result_498; }); el_val_t id = engram_node_full(content, node_type, label, el_from_float(sal), el_from_float(sal), el_from_float(0.9), tier, tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -28785,8 +28859,18 @@ el_val_t handle_api_node_delete(el_val_t body) { if (str_eq(id, EL_STR(""))) { return api_err(EL_STR("id is required")); } - engram_forget(id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}")); + if (is_protected_node(id)) { + return api_err_protected(id); + } + el_val_t existing = engram_get_node_json(id); + if (str_eq(existing, EL_STR("{}"))) { + return api_err(el_str_concat(EL_STR("node not found: "), id)); + } + el_val_t marker = tombstone_node(id); + if (str_eq(marker, EL_STR(""))) { + return api_err(el_str_concat(EL_STR("tombstone failed: "), id)); + } + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\",\"tombstoned\":true}")); return 0; } @@ -28800,37 +28884,37 @@ el_val_t handle_api_node_update(el_val_t body) { } el_val_t old = engram_get_node_json(id); el_val_t body_content = json_get(body, EL_STR("content")); - el_val_t content = ({ el_val_t _if_result_497 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_497 = (json_get(old, EL_STR("content"))); } else { _if_result_497 = (body_content); } _if_result_497; }); + el_val_t content = ({ el_val_t _if_result_501 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_501 = (json_get(old, EL_STR("content"))); } else { _if_result_501 = (body_content); } _if_result_501; }); el_val_t body_nt = json_get(body, EL_STR("node_type")); el_val_t old_nt = json_get(old, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_498 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_498 = (body_nt); } else { _if_result_498 = (({ el_val_t _if_result_499 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_499 = (old_nt); } else { _if_result_499 = (EL_STR("Memory")); } _if_result_499; })); } _if_result_498; }); + el_val_t node_type = ({ el_val_t _if_result_502 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_502 = (body_nt); } else { _if_result_502 = (({ el_val_t _if_result_503 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_503 = (old_nt); } else { _if_result_503 = (EL_STR("Memory")); } _if_result_503; })); } _if_result_502; }); el_val_t body_label = json_get(body, EL_STR("label")); el_val_t old_label = json_get(old, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_500 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_500 = (body_label); } else { _if_result_500 = (({ el_val_t _if_result_501 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_501 = (old_label); } else { _if_result_501 = (EL_STR("node:updated")); } _if_result_501; })); } _if_result_500; }); + el_val_t label = ({ el_val_t _if_result_504 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_504 = (body_label); } else { _if_result_504 = (({ el_val_t _if_result_505 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_505 = (old_label); } else { _if_result_505 = (EL_STR("node:updated")); } _if_result_505; })); } _if_result_504; }); el_val_t body_tier = json_get(body, EL_STR("tier")); el_val_t old_tier = json_get(old, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_502 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_502 = (body_tier); } else { _if_result_502 = (({ el_val_t _if_result_503 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_503 = (old_tier); } else { _if_result_503 = (EL_STR("Episodic")); } _if_result_503; })); } _if_result_502; }); + el_val_t tier = ({ el_val_t _if_result_506 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_506 = (body_tier); } else { _if_result_506 = (({ el_val_t _if_result_507 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_507 = (old_tier); } else { _if_result_507 = (EL_STR("Episodic")); } _if_result_507; })); } _if_result_506; }); el_val_t body_tags = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_504 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_504 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_504 = (body_tags); } _if_result_504; }); + el_val_t tags = ({ el_val_t _if_result_508 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_508 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_508 = (body_tags); } _if_result_508; }); el_val_t new_id = engram_node_full(content, node_type, label, el_from_float(0.5), el_from_float(0.5), el_from_float(0.8), tier, tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); } - engram_forget(id); - return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), new_id), EL_STR("\",\"replaced\":\"")), id), EL_STR("\",\"ok\":true}")); + engram_connect(new_id, id, el_from_float(0.9), EL_STR("supersedes")); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), new_id), EL_STR("\",\"supersedes\":\"")), id), EL_STR("\",\"ok\":true}")); return 0; } el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body) { - el_val_t url_q = ({ el_val_t _if_result_505 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_505 = (api_query_param(path, EL_STR("q"))); } else { _if_result_505 = (api_query_param(path, EL_STR("query"))); } _if_result_505; }); + el_val_t url_q = ({ el_val_t _if_result_509 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_509 = (api_query_param(path, EL_STR("q"))); } else { _if_result_509 = (api_query_param(path, EL_STR("query"))); } _if_result_509; }); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_506 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_506 = (url_q); } else { _if_result_506 = (({ el_val_t _if_result_507 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_507 = (body_query); } else { _if_result_507 = (body_q); } _if_result_507; })); } _if_result_506; }); + el_val_t q = ({ el_val_t _if_result_510 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_510 = (url_q); } else { _if_result_510 = (({ el_val_t _if_result_511 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_511 = (body_query); } else { _if_result_511 = (body_q); } _if_result_511; })); } _if_result_510; }); el_val_t chain = json_get(body, EL_STR("chain_name")); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_508 = 0; if ((limit == 0)) { _if_result_508 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_508 = (limit); } _if_result_508; }); - limit = ({ el_val_t _if_result_509 = 0; if ((limit == 0)) { _if_result_509 = (10); } else { _if_result_509 = (limit); } _if_result_509; }); - el_val_t eff_q = ({ el_val_t _if_result_510 = 0; if (str_eq(q, EL_STR(""))) { _if_result_510 = (chain); } else { _if_result_510 = (q); } _if_result_510; }); + limit = ({ el_val_t _if_result_512 = 0; if ((limit == 0)) { _if_result_512 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_512 = (limit); } _if_result_512; }); + limit = ({ el_val_t _if_result_513 = 0; if ((limit == 0)) { _if_result_513 = (10); } else { _if_result_513 = (limit); } _if_result_513; }); + el_val_t eff_q = ({ el_val_t _if_result_514 = 0; if (str_eq(q, EL_STR(""))) { _if_result_514 = (chain); } else { _if_result_514 = (q); } _if_result_514; }); if (str_eq(eff_q, EL_STR(""))) { return api_or_empty(engram_scan_nodes_json(limit, 0)); } @@ -28843,10 +28927,10 @@ el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t bo el_val_t url_q = api_query_param(path, EL_STR("q")); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_511 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_511 = (url_q); } else { _if_result_511 = (({ el_val_t _if_result_512 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_512 = (body_query); } else { _if_result_512 = (body_q); } _if_result_512; })); } _if_result_511; }); + el_val_t q = ({ el_val_t _if_result_515 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_515 = (url_q); } else { _if_result_515 = (({ el_val_t _if_result_516 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_516 = (body_query); } else { _if_result_516 = (body_q); } _if_result_516; })); } _if_result_515; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_513 = 0; if ((limit == 0)) { _if_result_513 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_513 = (limit); } _if_result_513; }); - limit = ({ el_val_t _if_result_514 = 0; if ((limit == 0)) { _if_result_514 = (10); } else { _if_result_514 = (limit); } _if_result_514; }); + limit = ({ el_val_t _if_result_517 = 0; if ((limit == 0)) { _if_result_517 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_517 = (limit); } _if_result_517; }); + limit = ({ el_val_t _if_result_518 = 0; if ((limit == 0)) { _if_result_518 = (10); } else { _if_result_518 = (limit); } _if_result_518; }); if (str_eq(q, EL_STR(""))) { return api_err(EL_STR("query is required")); } @@ -28874,7 +28958,7 @@ el_val_t handle_api_capture_knowledge(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t full = ({ el_val_t _if_result_515 = 0; if (str_eq(title, EL_STR(""))) { _if_result_515 = (content); } else { _if_result_515 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_515; }); + el_val_t full = ({ el_val_t _if_result_519 = 0; if (str_eq(title, EL_STR(""))) { _if_result_519 = (content); } else { _if_result_519 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_519; }); el_val_t tags = EL_STR("[\"Knowledge\",\"captured\"]"); el_val_t id = engram_node_full(full, EL_STR("Knowledge"), EL_STR("knowledge:captured"), el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags); if (!api_persisted(id)) { @@ -28915,7 +28999,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { return api_err(EL_STR("id (prior node) is required")); } el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_516 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_516 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_516 = (tags_raw); } _if_result_516; }); + el_val_t tags = ({ el_val_t _if_result_520 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_520 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_520 = (tags_raw); } _if_result_520; }); el_val_t new_id = engram_node_full(content, EL_STR("Knowledge"), EL_STR("knowledge:canonical"), el_from_float(0.9), el_from_float(0.9), el_from_float(1.0), EL_STR("Canonical"), tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); @@ -28926,7 +29010,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { } el_val_t handle_api_browse_processes(el_val_t method, el_val_t path, el_val_t body) { - el_val_t name = ({ el_val_t _if_result_517 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_517 = (api_query_param(path, EL_STR("name"))); } else { _if_result_517 = (json_get(body, EL_STR("name"))); } _if_result_517; }); + el_val_t name = ({ el_val_t _if_result_521 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_521 = (api_query_param(path, EL_STR("name"))); } else { _if_result_521 = (json_get(body, EL_STR("name"))); } _if_result_521; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 50); if (str_eq(name, EL_STR(""))) { return api_or_empty(engram_scan_nodes_by_type_json(EL_STR("Process"), limit, 0)); @@ -28941,7 +29025,7 @@ el_val_t handle_api_define_process(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t label = ({ el_val_t _if_result_518 = 0; if (str_eq(name, EL_STR(""))) { _if_result_518 = (EL_STR("process:unnamed")); } else { _if_result_518 = (el_str_concat(EL_STR("process:"), name)); } _if_result_518; }); + el_val_t label = ({ el_val_t _if_result_522 = 0; if (str_eq(name, EL_STR(""))) { _if_result_522 = (EL_STR("process:unnamed")); } else { _if_result_522 = (el_str_concat(EL_STR("process:"), name)); } _if_result_522; }); el_val_t tags = EL_STR("[\"Process\"]"); el_val_t id = engram_node_full(content, EL_STR("Process"), label, el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Canonical"), tags); if (!api_persisted(id)) { @@ -28959,12 +29043,12 @@ el_val_t handle_api_log_state_event(el_val_t body) { el_val_t gap = json_get(body, EL_STR("gap_direction")); el_val_t legacy = json_get(body, EL_STR("content")); el_val_t parts = EL_STR("INTERNAL STATE EVENT"); - parts = ({ el_val_t _if_result_519 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_519 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_519 = (parts); } _if_result_519; }); - parts = ({ el_val_t _if_result_520 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_520 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_520 = (parts); } _if_result_520; }); - parts = ({ el_val_t _if_result_521 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_521 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_521 = (parts); } _if_result_521; }); - parts = ({ el_val_t _if_result_522 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_522 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_522 = (parts); } _if_result_522; }); - parts = ({ el_val_t _if_result_523 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_523 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_523 = (parts); } _if_result_523; }); - parts = ({ el_val_t _if_result_524 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_524 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_524 = (parts); } _if_result_524; }); + parts = ({ el_val_t _if_result_523 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_523 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_523 = (parts); } _if_result_523; }); + parts = ({ el_val_t _if_result_524 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_524 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_524 = (parts); } _if_result_524; }); + parts = ({ el_val_t _if_result_525 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_525 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_525 = (parts); } _if_result_525; }); + parts = ({ el_val_t _if_result_526 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_526 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_526 = (parts); } _if_result_526; }); + parts = ({ el_val_t _if_result_527 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_527 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_527 = (parts); } _if_result_527; }); + parts = ({ el_val_t _if_result_528 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_528 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_528 = (parts); } _if_result_528; }); el_val_t ts = time_now(); el_val_t boot = state_get(EL_STR("soul_boot_count")); el_val_t tags = EL_STR("[\"internal-state\",\"InternalStateEvent\",\"pre-reasoning\"]"); @@ -28977,7 +29061,7 @@ el_val_t handle_api_log_state_event(el_val_t body) { } el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t body) { - el_val_t q = ({ el_val_t _if_result_525 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_525 = (api_query_param(path, EL_STR("query"))); } else { _if_result_525 = (json_get(body, EL_STR("query"))); } _if_result_525; }); + el_val_t q = ({ el_val_t _if_result_529 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_529 = (api_query_param(path, EL_STR("query"))); } else { _if_result_529 = (json_get(body, EL_STR("query"))); } _if_result_529; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 20); if (!str_eq(q, EL_STR(""))) { return api_or_empty(engram_search_json(el_str_concat(EL_STR("internal state "), q), limit)); @@ -28988,7 +29072,7 @@ el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t b el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t key = api_query_param(path, EL_STR("key")); - key = ({ el_val_t _if_result_526 = 0; if (str_eq(key, EL_STR(""))) { _if_result_526 = (json_get(body, EL_STR("key"))); } else { _if_result_526 = (key); } _if_result_526; }); + key = ({ el_val_t _if_result_530 = 0; if (str_eq(key, EL_STR(""))) { _if_result_530 = (json_get(body, EL_STR("key"))); } else { _if_result_530 = (key); } _if_result_530; }); if (str_eq(key, EL_STR(""))) { return EL_STR("{\"hint\":\"pass ?key=\",\"known\":[\"neuron.self.traversal_root\",\"neuron.self.values_hub\"]}"); } @@ -29005,7 +29089,7 @@ el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t node = json_array_get(results, 0); el_val_t content = json_get(node, EL_STR("content")); el_val_t prefix = el_str_concat(el_str_concat(EL_STR("config:"), key), EL_STR("=")); - el_val_t value = ({ el_val_t _if_result_527 = 0; if (str_starts_with(content, prefix)) { _if_result_527 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_527 = (content); } _if_result_527; }); + el_val_t value = ({ el_val_t _if_result_531 = 0; if (str_starts_with(content, prefix)) { _if_result_531 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_531 = (content); } _if_result_531; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"key\":\""), key), EL_STR("\",\"value\":\"")), value), EL_STR("\"}")); return 0; } @@ -29027,13 +29111,13 @@ el_val_t handle_api_tune_config(el_val_t body) { } el_val_t handle_api_inspect_graph(el_val_t method, el_val_t path, el_val_t body) { - el_val_t entity_id = ({ el_val_t _if_result_528 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_528 = (api_query_param(path, EL_STR("id"))); } else { _if_result_528 = (json_get(body, EL_STR("entity_id"))); } _if_result_528; }); - el_val_t name = ({ el_val_t _if_result_529 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_529 = (api_query_param(path, EL_STR("name"))); } else { _if_result_529 = (json_get(body, EL_STR("name"))); } _if_result_529; }); + el_val_t entity_id = ({ el_val_t _if_result_532 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_532 = (api_query_param(path, EL_STR("id"))); } else { _if_result_532 = (json_get(body, EL_STR("entity_id"))); } _if_result_532; }); + el_val_t name = ({ el_val_t _if_result_533 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_533 = (api_query_param(path, EL_STR("name"))); } else { _if_result_533 = (json_get(body, EL_STR("name"))); } _if_result_533; }); el_val_t depth = api_query_int(path, EL_STR("depth"), 0); - depth = ({ el_val_t _if_result_530 = 0; if ((depth == 0)) { _if_result_530 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_530 = (depth); } _if_result_530; }); - depth = ({ el_val_t _if_result_531 = 0; if ((depth == 0)) { _if_result_531 = (1); } else { _if_result_531 = (depth); } _if_result_531; }); + depth = ({ el_val_t _if_result_534 = 0; if ((depth == 0)) { _if_result_534 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_534 = (depth); } _if_result_534; }); + depth = ({ el_val_t _if_result_535 = 0; if ((depth == 0)) { _if_result_535 = (1); } else { _if_result_535 = (depth); } _if_result_535; }); el_val_t resolved = entity_id; - resolved = ({ el_val_t _if_result_532 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_532 = (({ el_val_t _if_result_533 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_533 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_533 = (({ el_val_t _if_result_534 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_534 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_534 = (EL_STR("")); } _if_result_534; })); } _if_result_533; })); } else { _if_result_532 = (resolved); } _if_result_532; }); + resolved = ({ el_val_t _if_result_536 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_536 = (({ el_val_t _if_result_537 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_537 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_537 = (({ el_val_t _if_result_538 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_538 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_538 = (EL_STR("")); } _if_result_538; })); } _if_result_537; })); } else { _if_result_536 = (resolved); } _if_result_536; }); if (str_eq(resolved, EL_STR(""))) { return api_err(EL_STR("entity_id or name required. Known names: self, neuron, values, values_hub")); } @@ -29055,7 +29139,7 @@ el_val_t handle_api_link_entities(el_val_t body) { return api_err_protected(to_id); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_535 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_535 = (EL_STR("associates")); } else { _if_result_535 = (relation); } _if_result_535; }); + el_val_t eff_relation = ({ el_val_t _if_result_539 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_539 = (EL_STR("associates")); } else { _if_result_539 = (relation); } _if_result_539; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\"}")); return 0; @@ -29070,7 +29154,7 @@ el_val_t handle_api_forget(el_val_t body) { return api_err_protected(node_id); } mem_forget(node_id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\"}")); + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"tombstoned\":true}")); return 0; } @@ -29084,8 +29168,8 @@ el_val_t handle_api_evolve_memory(el_val_t body) { return api_err_protected(prior_id); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal_str = ({ el_val_t _if_result_536 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_536 = (EL_STR("0.95")); } else { _if_result_536 = (({ el_val_t _if_result_537 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_537 = (EL_STR("0.75")); } else { _if_result_537 = (({ el_val_t _if_result_538 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_538 = (EL_STR("0.25")); } else { _if_result_538 = (EL_STR("0.50")); } _if_result_538; })); } _if_result_537; })); } _if_result_536; }); - el_val_t sal = ({ el_val_t _if_result_539 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_539 = (el_from_float(0.95)); } else { _if_result_539 = (({ el_val_t _if_result_540 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_540 = (el_from_float(0.75)); } else { _if_result_540 = (({ el_val_t _if_result_541 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_541 = (el_from_float(0.25)); } else { _if_result_541 = (el_from_float(0.5)); } _if_result_541; })); } _if_result_540; })); } _if_result_539; }); + el_val_t sal_str = ({ el_val_t _if_result_540 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_540 = (EL_STR("0.95")); } else { _if_result_540 = (({ el_val_t _if_result_541 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_541 = (EL_STR("0.75")); } else { _if_result_541 = (({ el_val_t _if_result_542 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_542 = (EL_STR("0.25")); } else { _if_result_542 = (EL_STR("0.50")); } _if_result_542; })); } _if_result_541; })); } _if_result_540; }); + el_val_t sal = ({ el_val_t _if_result_543 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_543 = (el_from_float(0.95)); } else { _if_result_543 = (({ el_val_t _if_result_544 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_544 = (el_from_float(0.75)); } else { _if_result_544 = (({ el_val_t _if_result_545 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_545 = (el_from_float(0.25)); } else { _if_result_545 = (el_from_float(0.5)); } _if_result_545; })); } _if_result_544; })); } _if_result_543; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:evolved"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -29107,8 +29191,11 @@ el_val_t handle_api_memory_delete(el_val_t body) { if (str_eq(existing, EL_STR("{}"))) { return api_err(el_str_concat(EL_STR("memory not found: "), node_id)); } - mem_forget(node_id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"deleted\":true}")); + el_val_t marker = tombstone_node(node_id); + if (str_eq(marker, EL_STR(""))) { + return api_err(el_str_concat(EL_STR("tombstone failed: "), node_id)); + } + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"tombstoned\":true}")); return 0; } @@ -29157,7 +29244,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_542 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_542 = (el_from_float(0.95)); } else { _if_result_542 = (({ el_val_t _if_result_543 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_543 = (el_from_float(0.75)); } else { _if_result_543 = (({ el_val_t _if_result_544 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_544 = (el_from_float(0.25)); } else { _if_result_544 = (el_from_float(0.5)); } _if_result_544; })); } _if_result_543; })); } _if_result_542; }); + el_val_t sal = ({ el_val_t _if_result_546 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_546 = (el_from_float(0.95)); } else { _if_result_546 = (({ el_val_t _if_result_547 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_547 = (el_from_float(0.75)); } else { _if_result_547 = (({ el_val_t _if_result_548 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_548 = (el_from_float(0.25)); } else { _if_result_548 = (el_from_float(0.5)); } _if_result_548; })); } _if_result_547; })); } _if_result_546; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\",\"cultivated\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:cultivated"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -29171,7 +29258,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("id is required")); } mem_forget(node_id); - return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"cultivated\":true}")); + return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), node_id), EL_STR("\",\"tombstoned\":true,\"cultivated\":true}")); } if (str_eq(op, EL_STR("link_entities"))) { el_val_t from_id = json_get(body, EL_STR("from_id")); @@ -29183,7 +29270,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("to_id is required")); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_545 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_545 = (EL_STR("associates")); } else { _if_result_545 = (relation); } _if_result_545; }); + el_val_t eff_relation = ({ el_val_t _if_result_549 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_549 = (EL_STR("associates")); } else { _if_result_549 = (relation); } _if_result_549; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\",\"cultivated\":true}")); } @@ -29193,7 +29280,8 @@ el_val_t handle_api_cultivate(el_val_t body) { el_val_t handle_api_list_typed(el_val_t node_type, el_val_t path, el_val_t body) { el_val_t limit = api_query_int(path, EL_STR("limit"), 50); - return api_or_empty(engram_scan_nodes_by_type_json(node_type, limit, 0)); + el_val_t raw = api_or_empty(engram_scan_nodes_by_type_json(node_type, limit, 0)); + return memory_hide_tombstoned(raw, path); return 0; } @@ -29263,7 +29351,7 @@ el_val_t session_exists(el_val_t session_id) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = (str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)); - found = ({ el_val_t _if_result_546 = 0; if (is_match) { _if_result_546 = (1); } else { _if_result_546 = (found); } _if_result_546; }); + found = ({ el_val_t _if_result_550 = 0; if (is_match) { _if_result_550 = (1); } else { _if_result_550 = (found); } _if_result_550; }); i = (i + 1); } return found; @@ -29274,7 +29362,7 @@ el_val_t session_create(el_val_t body) { el_val_t ts = time_now(); el_val_t id = uuid_v4(); el_val_t title_req = json_get(body, EL_STR("title")); - el_val_t title = ({ el_val_t _if_result_547 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_547 = (EL_STR("New conversation")); } else { _if_result_547 = (title_req); } _if_result_547; }); + el_val_t title = ({ el_val_t _if_result_551 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_551 = (EL_STR("New conversation")); } else { _if_result_551 = (title_req); } _if_result_551; }); el_val_t folder = json_get(body, EL_STR("folder")); el_val_t content = session_make_content(id, title, ts, ts, folder); el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]"); @@ -29286,7 +29374,7 @@ el_val_t session_create(el_val_t body) { state_set(el_str_concat(EL_STR("session_pending_first_msg_"), id), EL_STR("1")); el_val_t existing_idx = state_get(EL_STR("session_index")); el_val_t idx_entry = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\",\"title\":\"")), json_safe(title)), EL_STR("\",\"folder\":\"")), json_safe(folder)), EL_STR("\",\"created_at\":")), int_to_str(ts)), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR(",\"last_message\":\"\"}")); - el_val_t new_idx = ({ el_val_t _if_result_548 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_548 = (el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR("]"))); } else { el_val_t inner = str_slice(existing_idx, 1, (str_len(existing_idx) - 1)); _if_result_548 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_548; }); + el_val_t new_idx = ({ el_val_t _if_result_552 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_552 = (el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR("]"))); } else { el_val_t inner = str_slice(existing_idx, 1, (str_len(existing_idx) - 1)); _if_result_552 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_552; }); state_set(EL_STR("session_index"), new_idx); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder)), EL_STR("\"")), EL_STR(",\"node_id\":\"")), node_id), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(ts)), EL_STR("}")); return 0; @@ -29323,16 +29411,16 @@ el_val_t session_list(void) { el_val_t is_session = (str_eq(label, EL_STR("session:meta")) && str_eq(node_type, EL_STR("Conversation"))); el_val_t content = json_get(node, EL_STR("content")); el_val_t sess_id = json_get(content, EL_STR("id")); - el_val_t eff_id = ({ el_val_t _if_result_549 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_549 = (json_get(node, EL_STR("id"))); } else { _if_result_549 = (sess_id); } _if_result_549; }); + el_val_t eff_id = ({ el_val_t _if_result_553 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_553 = (json_get(node, EL_STR("id"))); } else { _if_result_553 = (sess_id); } _if_result_553; }); el_val_t title_inner = json_get(content, EL_STR("title")); - el_val_t eff_title = ({ el_val_t _if_result_550 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_550 = (EL_STR("New conversation")); } else { _if_result_550 = (title_inner); } _if_result_550; }); + el_val_t eff_title = ({ el_val_t _if_result_554 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_554 = (EL_STR("New conversation")); } else { _if_result_554 = (title_inner); } _if_result_554; }); el_val_t folder_inner = json_get(content, EL_STR("folder")); el_val_t created_inner = json_get(content, EL_STR("created_at")); el_val_t updated_inner = json_get(content, EL_STR("updated_at")); - el_val_t eff_created = ({ el_val_t _if_result_551 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_551 = (EL_STR("0")); } else { _if_result_551 = (created_inner); } _if_result_551; }); - el_val_t eff_updated = ({ el_val_t _if_result_552 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_552 = (eff_created); } else { _if_result_552 = (updated_inner); } _if_result_552; }); - el_val_t entry = ({ el_val_t _if_result_553 = 0; if (is_session) { _if_result_553 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(eff_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder_inner)), EL_STR("\"")), EL_STR(",\"last_message\":\"\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_553 = (EL_STR("")); } _if_result_553; }); - out = ({ el_val_t _if_result_554 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_554 = (({ el_val_t _if_result_555 = 0; if (str_eq(out, EL_STR(""))) { _if_result_555 = (entry); } else { _if_result_555 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_555; })); } else { _if_result_554 = (out); } _if_result_554; }); + el_val_t eff_created = ({ el_val_t _if_result_555 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_555 = (EL_STR("0")); } else { _if_result_555 = (created_inner); } _if_result_555; }); + el_val_t eff_updated = ({ el_val_t _if_result_556 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_556 = (eff_created); } else { _if_result_556 = (updated_inner); } _if_result_556; }); + el_val_t entry = ({ el_val_t _if_result_557 = 0; if (is_session) { _if_result_557 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(eff_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder_inner)), EL_STR("\"")), EL_STR(",\"last_message\":\"\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_557 = (EL_STR("")); } _if_result_557; }); + out = ({ el_val_t _if_result_558 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_558 = (({ el_val_t _if_result_559 = 0; if (str_eq(out, EL_STR(""))) { _if_result_559 = (entry); } else { _if_result_559 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_559; })); } else { _if_result_558 = (out); } _if_result_558; }); i = (i + 1); } return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); @@ -29350,7 +29438,7 @@ el_val_t session_get(el_val_t session_id) { el_val_t meta_created = EL_STR("0"); el_val_t meta_updated = EL_STR("0"); el_val_t found = 0; - el_val_t total = ({ el_val_t _if_result_556 = 0; if (str_eq(results, EL_STR(""))) { _if_result_556 = (0); } else { _if_result_556 = (json_array_len(results)); } _if_result_556; }); + el_val_t total = ({ el_val_t _if_result_560 = 0; if (str_eq(results, EL_STR(""))) { _if_result_560 = (0); } else { _if_result_560 = (json_array_len(results)); } _if_result_560; }); el_val_t i = 0; while (i < total) { el_val_t node = json_array_get(results, i); @@ -29358,17 +29446,17 @@ el_val_t session_get(el_val_t session_id) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_557 = 0; if (is_match) { _if_result_557 = (1); } else { _if_result_557 = (found); } _if_result_557; }); - meta_title = ({ el_val_t _if_result_558 = 0; if (is_match) { _if_result_558 = (json_get(content, EL_STR("title"))); } else { _if_result_558 = (meta_title); } _if_result_558; }); - meta_folder = ({ el_val_t _if_result_559 = 0; if (is_match) { _if_result_559 = (json_get(content, EL_STR("folder"))); } else { _if_result_559 = (meta_folder); } _if_result_559; }); + found = ({ el_val_t _if_result_561 = 0; if (is_match) { _if_result_561 = (1); } else { _if_result_561 = (found); } _if_result_561; }); + meta_title = ({ el_val_t _if_result_562 = 0; if (is_match) { _if_result_562 = (json_get(content, EL_STR("title"))); } else { _if_result_562 = (meta_title); } _if_result_562; }); + meta_folder = ({ el_val_t _if_result_563 = 0; if (is_match) { _if_result_563 = (json_get(content, EL_STR("folder"))); } else { _if_result_563 = (meta_folder); } _if_result_563; }); el_val_t meta_created_raw = json_get(content, EL_STR("created_at")); - meta_created = ({ el_val_t _if_result_560 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_560 = (meta_created_raw); } else { _if_result_560 = (meta_created); } _if_result_560; }); + meta_created = ({ el_val_t _if_result_564 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_564 = (meta_created_raw); } else { _if_result_564 = (meta_created); } _if_result_564; }); el_val_t meta_updated_raw = json_get(content, EL_STR("updated_at")); - meta_updated = ({ el_val_t _if_result_561 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_561 = (meta_updated_raw); } else { _if_result_561 = (meta_updated); } _if_result_561; }); + meta_updated = ({ el_val_t _if_result_565 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_565 = (meta_updated_raw); } else { _if_result_565 = (meta_updated); } _if_result_565; }); i = (i + 1); } el_val_t state_hist = state_get(el_str_concat(EL_STR("session_hist_"), session_id)); - el_val_t hist_raw = ({ el_val_t _if_result_562 = 0; if (str_eq(state_hist, EL_STR(""))) { el_val_t engram_hist = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); _if_result_562 = (({ el_val_t _if_result_563 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_563 = (EL_STR("[]")); } else { _if_result_563 = (({ el_val_t _if_result_564 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_564 = (EL_STR("[]")); } else { el_val_t h_node = json_array_get(engram_hist, 0); el_val_t h_content = json_get(h_node, EL_STR("content")); _if_result_564 = (({ el_val_t _if_result_565 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_565 = (h_content); } else { _if_result_565 = (EL_STR("[]")); } _if_result_565; })); } _if_result_564; })); } _if_result_563; })); } else { _if_result_562 = (state_hist); } _if_result_562; }); + el_val_t hist_raw = ({ el_val_t _if_result_566 = 0; if (str_eq(state_hist, EL_STR(""))) { el_val_t engram_hist = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); _if_result_566 = (({ el_val_t _if_result_567 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_567 = (EL_STR("[]")); } else { _if_result_567 = (({ el_val_t _if_result_568 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_568 = (EL_STR("[]")); } else { el_val_t h_node = json_array_get(engram_hist, 0); el_val_t h_content = json_get(h_node, EL_STR("content")); _if_result_568 = (({ el_val_t _if_result_569 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_569 = (h_content); } else { _if_result_569 = (EL_STR("[]")); } _if_result_569; })); } _if_result_568; })); } _if_result_567; })); } else { _if_result_566 = (state_hist); } _if_result_566; }); el_val_t safe_title = json_safe(meta_title); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(meta_folder)), EL_STR("\"")), EL_STR(",\"created_at\":")), meta_created), EL_STR(",\"updated_at\":")), meta_updated), EL_STR(",\"messages\":")), hist_raw), EL_STR("}")); return 0; @@ -29379,7 +29467,7 @@ el_val_t session_delete(el_val_t session_id) { return EL_STR("{\"error\":\"session_id is required\"}"); } el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10); - el_val_t total = ({ el_val_t _if_result_566 = 0; if (str_eq(results, EL_STR(""))) { _if_result_566 = (0); } else { _if_result_566 = (json_array_len(results)); } _if_result_566; }); + el_val_t total = ({ el_val_t _if_result_570 = 0; if (str_eq(results, EL_STR(""))) { _if_result_570 = (0); } else { _if_result_570 = (json_array_len(results)); } _if_result_570; }); el_val_t deleted_meta = 0; el_val_t i = 0; while (i < total) { @@ -29389,11 +29477,11 @@ el_val_t session_delete(el_val_t session_id) { el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = (str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)); el_val_t node_id = json_get(node, EL_STR("id")); - deleted_meta = ({ el_val_t _if_result_567 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_567 = ((deleted_meta + 1)); } else { _if_result_567 = (deleted_meta); } _if_result_567; }); + deleted_meta = ({ el_val_t _if_result_571 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_571 = ((deleted_meta + 1)); } else { _if_result_571 = (deleted_meta); } _if_result_571; }); i = (i + 1); } el_val_t msg_results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 10); - el_val_t m_total = ({ el_val_t _if_result_568 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_568 = (0); } else { _if_result_568 = (json_array_len(msg_results)); } _if_result_568; }); + el_val_t m_total = ({ el_val_t _if_result_572 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_572 = (0); } else { _if_result_572 = (json_array_len(msg_results)); } _if_result_572; }); el_val_t deleted_msgs = 0; el_val_t j = 0; while (j < m_total) { @@ -29401,7 +29489,7 @@ el_val_t session_delete(el_val_t session_id) { el_val_t label = json_get(node, EL_STR("label")); el_val_t is_msgs = str_eq(label, el_str_concat(EL_STR("session:messages:"), session_id)); el_val_t node_id = json_get(node, EL_STR("id")); - deleted_msgs = ({ el_val_t _if_result_569 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_569 = ((deleted_msgs + 1)); } else { _if_result_569 = (deleted_msgs); } _if_result_569; }); + deleted_msgs = ({ el_val_t _if_result_573 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_573 = ((deleted_msgs + 1)); } else { _if_result_573 = (deleted_msgs); } _if_result_573; }); j = (j + 1); } state_set(el_str_concat(EL_STR("session_hist_"), session_id), EL_STR("")); @@ -29424,7 +29512,7 @@ el_val_t session_update_patch(el_val_t session_id, el_val_t body) { return EL_STR("{\"error\":\"title or folder required in body\"}"); } el_val_t results = engram_search_json(EL_STR("session:meta"), 50); - el_val_t total = ({ el_val_t _if_result_570 = 0; if (str_eq(results, EL_STR(""))) { _if_result_570 = (0); } else { _if_result_570 = (json_array_len(results)); } _if_result_570; }); + el_val_t total = ({ el_val_t _if_result_574 = 0; if (str_eq(results, EL_STR(""))) { _if_result_574 = (0); } else { _if_result_574 = (json_array_len(results)); } _if_result_574; }); el_val_t found = 0; el_val_t old_title = EL_STR("New conversation"); el_val_t old_folder = EL_STR(""); @@ -29437,23 +29525,23 @@ el_val_t session_update_patch(el_val_t session_id, el_val_t body) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_571 = 0; if (is_match) { _if_result_571 = (1); } else { _if_result_571 = (found); } _if_result_571; }); + found = ({ el_val_t _if_result_575 = 0; if (is_match) { _if_result_575 = (1); } else { _if_result_575 = (found); } _if_result_575; }); el_val_t title_raw = json_get(content, EL_STR("title")); - old_title = ({ el_val_t _if_result_572 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_572 = (title_raw); } else { _if_result_572 = (old_title); } _if_result_572; }); + old_title = ({ el_val_t _if_result_576 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_576 = (title_raw); } else { _if_result_576 = (old_title); } _if_result_576; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_573 = 0; if (is_match) { _if_result_573 = (folder_raw); } else { _if_result_573 = (old_folder); } _if_result_573; }); + old_folder = ({ el_val_t _if_result_577 = 0; if (is_match) { _if_result_577 = (folder_raw); } else { _if_result_577 = (old_folder); } _if_result_577; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_574 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_574 = (created_raw); } else { _if_result_574 = (old_created); } _if_result_574; }); + old_created = ({ el_val_t _if_result_578 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_578 = (created_raw); } else { _if_result_578 = (old_created); } _if_result_578; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_575 = 0; if (is_match) { _if_result_575 = (nid); } else { _if_result_575 = (old_node_id); } _if_result_575; }); + old_node_id = ({ el_val_t _if_result_579 = 0; if (is_match) { _if_result_579 = (nid); } else { _if_result_579 = (old_node_id); } _if_result_579; }); i = (i + 1); } if (!found) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"session not found\",\"session_id\":\""), session_id), EL_STR("\"}")); } el_val_t req_title = json_get(body, EL_STR("title")); - el_val_t eff_title = ({ el_val_t _if_result_576 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_576 = (req_title); } else { _if_result_576 = (old_title); } _if_result_576; }); - el_val_t eff_folder = ({ el_val_t _if_result_577 = 0; if (has_folder) { _if_result_577 = (json_get(body, EL_STR("folder"))); } else { _if_result_577 = (old_folder); } _if_result_577; }); + el_val_t eff_title = ({ el_val_t _if_result_580 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_580 = (req_title); } else { _if_result_580 = (old_title); } _if_result_580; }); + el_val_t eff_folder = ({ el_val_t _if_result_581 = 0; if (has_folder) { _if_result_581 = (json_get(body, EL_STR("folder"))); } else { _if_result_581 = (old_folder); } _if_result_581; }); if (!str_eq(old_node_id, EL_STR(""))) { engram_forget(old_node_id); } @@ -29481,8 +29569,8 @@ el_val_t session_search_entry(el_val_t node) { el_val_t title = json_get(content, EL_STR("title")); el_val_t created_raw = json_get(content, EL_STR("created_at")); el_val_t updated_raw = json_get(content, EL_STR("updated_at")); - el_val_t eff_created = ({ el_val_t _if_result_578 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_578 = (EL_STR("0")); } else { _if_result_578 = (created_raw); } _if_result_578; }); - el_val_t eff_updated = ({ el_val_t _if_result_579 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_579 = (eff_created); } else { _if_result_579 = (updated_raw); } _if_result_579; }); + el_val_t eff_created = ({ el_val_t _if_result_582 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_582 = (EL_STR("0")); } else { _if_result_582 = (created_raw); } _if_result_582; }); + el_val_t eff_updated = ({ el_val_t _if_result_583 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_583 = (eff_created); } else { _if_result_583 = (updated_raw); } _if_result_583; }); el_val_t e_id = el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(sess_id)), EL_STR("\"")); el_val_t e_title = el_str_concat(el_str_concat(EL_STR(",\"title\":\""), json_safe(title)), EL_STR("\"")); el_val_t e_ts = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR(",\"created_at\":"), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}")); @@ -29506,7 +29594,7 @@ el_val_t session_search(el_val_t query) { el_val_t i = 0; while (i < total) { el_val_t entry = session_search_entry(json_array_get(results, i)); - out = ({ el_val_t _if_result_580 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_580 = (({ el_val_t _if_result_581 = 0; if (str_eq(out, EL_STR(""))) { _if_result_581 = (entry); } else { _if_result_581 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_581; })); } else { _if_result_580 = (out); } _if_result_580; }); + out = ({ el_val_t _if_result_584 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_584 = (({ el_val_t _if_result_585 = 0; if (str_eq(out, EL_STR(""))) { _if_result_585 = (entry); } else { _if_result_585 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_585; })); } else { _if_result_584 = (out); } _if_result_584; }); i = (i + 1); } return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); @@ -29542,7 +29630,7 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist) { state_set(el_str_concat(EL_STR("session_hist_"), session_id), hist); state_set(el_str_concat(EL_STR("session_pending_first_msg_"), session_id), EL_STR("")); el_val_t old_results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); - el_val_t o_total = ({ el_val_t _if_result_582 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_582 = (0); } else { _if_result_582 = (json_array_len(old_results)); } _if_result_582; }); + el_val_t o_total = ({ el_val_t _if_result_586 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_586 = (0); } else { _if_result_586 = (json_array_len(old_results)); } _if_result_586; }); el_val_t oi = 0; while (oi < o_total) { el_val_t node = json_array_get(old_results, oi); @@ -29560,35 +29648,35 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist) { if (str_eq(already_written, EL_STR(""))) { el_val_t bell_count_key = el_str_concat(EL_STR("session_bell_count:"), session_id); el_val_t bell_count_raw = state_get(bell_count_key); - el_val_t bell_count = ({ el_val_t _if_result_583 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_583 = (0); } else { _if_result_583 = (str_to_int(bell_count_raw)); } _if_result_583; }); + el_val_t bell_count = ({ el_val_t _if_result_587 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_587 = (0); } else { _if_result_587 = (str_to_int(bell_count_raw)); } _if_result_587; }); if (bell_count > 0) { el_val_t bell_level_key = el_str_concat(EL_STR("session_bell_level:"), session_id); el_val_t bell_signal_key = el_str_concat(EL_STR("session_bell_signal:"), session_id); el_val_t dominant_level = state_get(bell_level_key); el_val_t last_signal = state_get(bell_signal_key); - el_val_t eff_level = ({ el_val_t _if_result_584 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_584 = (EL_STR("soft")); } else { _if_result_584 = (dominant_level); } _if_result_584; }); - el_val_t eff_signal = ({ el_val_t _if_result_585 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_585 = (EL_STR("(no signal captured)")); } else { _if_result_585 = (last_signal); } _if_result_585; }); + el_val_t eff_level = ({ el_val_t _if_result_588 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_588 = (EL_STR("soft")); } else { _if_result_588 = (dominant_level); } _if_result_588; }); + el_val_t eff_signal = ({ el_val_t _if_result_589 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_589 = (EL_STR("(no signal captured)")); } else { _if_result_589 = (last_signal); } _if_result_589; }); el_val_t ts_now = time_now(); el_val_t summary_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("session:emotional-summary"), EL_STR(" | session:")), session_id), EL_STR(" | bell_count:")), int_to_str(bell_count)), EL_STR(" | dominant_level:")), eff_level), EL_STR(" | last_signal:")), eff_signal), EL_STR(" | ts:")), int_to_str(ts_now)); el_val_t summary_tags = el_str_concat(el_str_concat(EL_STR("[\"session-emotional-summary\",\"affective\",\"bell:"), eff_level), EL_STR("\",\"BellEvent\"]")); - el_val_t summary_sal = ({ el_val_t _if_result_586 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_586 = (el_from_float(0.95)); } else { _if_result_586 = (el_from_float(0.85)); } _if_result_586; }); + el_val_t summary_sal = ({ el_val_t _if_result_590 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_590 = (el_from_float(0.95)); } else { _if_result_590 = (el_from_float(0.85)); } _if_result_590; }); el_val_t sum_discard = engram_node_full(summary_content, EL_STR("BellEvent"), EL_STR("session:emotional-summary"), summary_sal, summary_sal, el_from_float(1.0), EL_STR("Episodic"), summary_tags); state_set(summary_written_key, EL_STR("1")); } } - el_val_t hist_arr_len = ({ el_val_t _if_result_587 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_587 = (0); } else { _if_result_587 = (json_array_len(hist)); } _if_result_587; }); + el_val_t hist_arr_len = ({ el_val_t _if_result_591 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_591 = (0); } else { _if_result_591 = (json_array_len(hist)); } _if_result_591; }); if (hist_arr_len >= 2) { el_val_t last_entry = json_array_get(hist, (hist_arr_len - 1)); el_val_t last_role = json_get(last_entry, EL_STR("role")); el_val_t last_content = json_get(last_entry, EL_STR("content")); - el_val_t topic_snip = ({ el_val_t _if_result_588 = 0; if ((str_len(last_content) > 200)) { _if_result_588 = (str_slice(last_content, 0, 200)); } else { _if_result_588 = (last_content); } _if_result_588; }); + el_val_t topic_snip = ({ el_val_t _if_result_592 = 0; if ((str_len(last_content) > 200)) { _if_result_592 = (str_slice(last_content, 0, 200)); } else { _if_result_592 = (last_content); } _if_result_592; }); el_val_t safe_topic = str_replace(topic_snip, EL_STR("\""), EL_STR("'")); el_val_t ts_now = int_to_str(time_now()); el_val_t topic_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("last-session-topic | ts:"), ts_now), EL_STR(" | session:")), session_id), EL_STR(" | topic:")), safe_topic); el_val_t topic_tags = EL_STR("[\"last-session-topic\",\"conv:history\",\"Conversation\",\"session:topic\"]"); el_val_t topic_label = el_str_concat(EL_STR("last-session-topic:"), session_id); el_val_t old_topic = engram_search_json(el_str_concat(EL_STR("last-session-topic:"), session_id), 2); - el_val_t ot_len = ({ el_val_t _if_result_589 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_589 = (0); } else { _if_result_589 = (json_array_len(old_topic)); } _if_result_589; }); + el_val_t ot_len = ({ el_val_t _if_result_593 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_593 = (0); } else { _if_result_593 = (json_array_len(old_topic)); } _if_result_593; }); el_val_t oti = 0; while (oti < ot_len) { el_val_t ot_node = json_array_get(old_topic, oti); @@ -29605,7 +29693,7 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist) { el_val_t session_update_meta_timestamp(el_val_t session_id) { el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10); - el_val_t total = ({ el_val_t _if_result_590 = 0; if (str_eq(results, EL_STR(""))) { _if_result_590 = (0); } else { _if_result_590 = (json_array_len(results)); } _if_result_590; }); + el_val_t total = ({ el_val_t _if_result_594 = 0; if (str_eq(results, EL_STR(""))) { _if_result_594 = (0); } else { _if_result_594 = (json_array_len(results)); } _if_result_594; }); el_val_t found = 0; el_val_t old_title = EL_STR("New conversation"); el_val_t old_folder = EL_STR(""); @@ -29618,15 +29706,15 @@ el_val_t session_update_meta_timestamp(el_val_t session_id) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_591 = 0; if (is_match) { _if_result_591 = (1); } else { _if_result_591 = (found); } _if_result_591; }); + found = ({ el_val_t _if_result_595 = 0; if (is_match) { _if_result_595 = (1); } else { _if_result_595 = (found); } _if_result_595; }); el_val_t title_raw = json_get(content, EL_STR("title")); - old_title = ({ el_val_t _if_result_592 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_592 = (title_raw); } else { _if_result_592 = (old_title); } _if_result_592; }); + old_title = ({ el_val_t _if_result_596 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_596 = (title_raw); } else { _if_result_596 = (old_title); } _if_result_596; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_593 = 0; if (is_match) { _if_result_593 = (folder_raw); } else { _if_result_593 = (old_folder); } _if_result_593; }); + old_folder = ({ el_val_t _if_result_597 = 0; if (is_match) { _if_result_597 = (folder_raw); } else { _if_result_597 = (old_folder); } _if_result_597; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_594 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_594 = (created_raw); } else { _if_result_594 = (old_created); } _if_result_594; }); + old_created = ({ el_val_t _if_result_598 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_598 = (created_raw); } else { _if_result_598 = (old_created); } _if_result_598; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_595 = 0; if (is_match) { _if_result_595 = (nid); } else { _if_result_595 = (old_node_id); } _if_result_595; }); + old_node_id = ({ el_val_t _if_result_599 = 0; if (is_match) { _if_result_599 = (nid); } else { _if_result_599 = (old_node_id); } _if_result_599; }); i = (i + 1); } if (!found) { @@ -29646,7 +29734,7 @@ el_val_t session_update_meta_timestamp(el_val_t session_id) { el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) { el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10); - el_val_t total = ({ el_val_t _if_result_596 = 0; if (str_eq(results, EL_STR(""))) { _if_result_596 = (0); } else { _if_result_596 = (json_array_len(results)); } _if_result_596; }); + el_val_t total = ({ el_val_t _if_result_600 = 0; if (str_eq(results, EL_STR(""))) { _if_result_600 = (0); } else { _if_result_600 = (json_array_len(results)); } _if_result_600; }); el_val_t found = 0; el_val_t cur_title = EL_STR(""); el_val_t old_folder = EL_STR(""); @@ -29659,15 +29747,15 @@ el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_597 = 0; if (is_match) { _if_result_597 = (1); } else { _if_result_597 = (found); } _if_result_597; }); + found = ({ el_val_t _if_result_601 = 0; if (is_match) { _if_result_601 = (1); } else { _if_result_601 = (found); } _if_result_601; }); el_val_t title_raw = json_get(content, EL_STR("title")); - cur_title = ({ el_val_t _if_result_598 = 0; if (is_match) { _if_result_598 = (title_raw); } else { _if_result_598 = (cur_title); } _if_result_598; }); + cur_title = ({ el_val_t _if_result_602 = 0; if (is_match) { _if_result_602 = (title_raw); } else { _if_result_602 = (cur_title); } _if_result_602; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_599 = 0; if (is_match) { _if_result_599 = (folder_raw); } else { _if_result_599 = (old_folder); } _if_result_599; }); + old_folder = ({ el_val_t _if_result_603 = 0; if (is_match) { _if_result_603 = (folder_raw); } else { _if_result_603 = (old_folder); } _if_result_603; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_600 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_600 = (created_raw); } else { _if_result_600 = (old_created); } _if_result_600; }); + old_created = ({ el_val_t _if_result_604 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_604 = (created_raw); } else { _if_result_604 = (old_created); } _if_result_604; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_601 = 0; if (is_match) { _if_result_601 = (nid); } else { _if_result_601 = (old_node_id); } _if_result_601; }); + old_node_id = ({ el_val_t _if_result_605 = 0; if (is_match) { _if_result_605 = (nid); } else { _if_result_605 = (old_node_id); } _if_result_605; }); i = (i + 1); } if (!found) { @@ -29701,12 +29789,12 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) { if (str_eq(action, EL_STR(""))) { return EL_STR("{\"error\":\"action is required (allow|deny|always)\"}"); } - el_val_t eff_action = ({ el_val_t _if_result_602 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_602 = (EL_STR("allow")); } else { _if_result_602 = (action); } _if_result_602; }); + el_val_t eff_action = ({ el_val_t _if_result_606 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_606 = (EL_STR("allow")); } else { _if_result_606 = (action); } _if_result_606; }); el_val_t bridge_blob = state_get(el_str_concat(EL_STR("mcp_bridge:"), session_id)); if (!str_eq(bridge_blob, EL_STR(""))) { el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); el_val_t approve_tool_name = json_get(body, EL_STR("tool_name")); - el_val_t discard_always = ({ el_val_t _if_result_603 = 0; if ((str_eq(action, EL_STR("always")) && !str_eq(approve_tool_name, EL_STR("")))) { el_val_t always_list = state_get(always_key); el_val_t new_always = ({ el_val_t _if_result_604 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_604 = (approve_tool_name); } else { _if_result_604 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_604; }); (void)(state_set(always_key, new_always)); _if_result_603 = (1); } else { _if_result_603 = (0); } _if_result_603; }); + el_val_t discard_always = ({ el_val_t _if_result_607 = 0; if ((str_eq(action, EL_STR("always")) && !str_eq(approve_tool_name, EL_STR("")))) { el_val_t always_list = state_get(always_key); el_val_t new_always = ({ el_val_t _if_result_608 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_608 = (approve_tool_name); } else { _if_result_608 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_608; }); (void)(state_set(always_key, new_always)); _if_result_607 = (1); } else { _if_result_607 = (0); } _if_result_607; }); if (str_eq(approve_tool_name, EL_STR("")) && str_eq(eff_action, EL_STR("allow"))) { return EL_STR("{\"error\":\"tool_name is required for allow action\"}"); } @@ -29714,8 +29802,8 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) { el_val_t use_client_content = !str_eq(client_content, EL_STR("")); el_val_t use_dispatch = (is_builtin_tool(approve_tool_name) && !use_client_content); el_val_t raw_input = json_get_raw(body, EL_STR("tool_input")); - el_val_t eff_input = ({ el_val_t _if_result_605 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_605 = (EL_STR("{}")); } else { _if_result_605 = (raw_input); } _if_result_605; }); - el_val_t content = ({ el_val_t _if_result_606 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_606 = (({ el_val_t _if_result_607 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_608 = 0; if ((str_len(client_content) > 6000)) { _if_result_608 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_608 = (client_content); } _if_result_608; }); _if_result_607 = (trimmed); } else { _if_result_607 = (({ el_val_t _if_result_609 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_609 = (({ el_val_t _if_result_610 = 0; if ((str_len(raw) > 6000)) { _if_result_610 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_610 = (raw); } _if_result_610; })); } else { _if_result_609 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_609; })); } _if_result_607; })); } else { _if_result_606 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_606; }); + el_val_t eff_input = ({ el_val_t _if_result_609 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_609 = (EL_STR("{}")); } else { _if_result_609 = (raw_input); } _if_result_609; }); + el_val_t content = ({ el_val_t _if_result_610 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_610 = (({ el_val_t _if_result_611 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_612 = 0; if ((str_len(client_content) > 6000)) { _if_result_612 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_612 = (client_content); } _if_result_612; }); _if_result_611 = (trimmed); } else { _if_result_611 = (({ el_val_t _if_result_613 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_613 = (({ el_val_t _if_result_614 = 0; if ((str_len(raw) > 6000)) { _if_result_614 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_614 = (raw); } _if_result_614; })); } else { _if_result_613 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_613; })); } _if_result_611; })); } else { _if_result_610 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_610; }); return agentic_resume(session_id, call_id, content); } el_val_t pending_raw = state_get(el_str_concat(EL_STR("pending_tool_"), session_id)); @@ -29732,12 +29820,12 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) { el_val_t safe_sys = json_get(pending_raw, EL_STR("system")); el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); el_val_t always_list = state_get(always_key); - el_val_t discard_always2 = ({ el_val_t _if_result_611 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_612 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_612 = (tool_name); } else { _if_result_612 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_612; }); (void)(state_set(always_key, new_always)); _if_result_611 = (1); } else { _if_result_611 = (0); } _if_result_611; }); + el_val_t discard_always2 = ({ el_val_t _if_result_615 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_616 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_616 = (tool_name); } else { _if_result_616 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_616; }); (void)(state_set(always_key, new_always)); _if_result_615 = (1); } else { _if_result_615 = (0); } _if_result_615; }); state_set(el_str_concat(EL_STR("pending_tool_"), session_id), EL_STR("")); - el_val_t tool_result = ({ el_val_t _if_result_613 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_613 = (({ el_val_t _if_result_614 = 0; if ((str_len(raw) > 6000)) { _if_result_614 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_614 = (raw); } _if_result_614; })); } else { _if_result_613 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_613; }); + el_val_t tool_result = ({ el_val_t _if_result_617 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_617 = (({ el_val_t _if_result_618 = 0; if ((str_len(raw) > 6000)) { _if_result_618 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_618 = (raw); } _if_result_618; })); } else { _if_result_617 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_617; }); el_val_t legacy_messages = json_get_raw(pending_raw, EL_STR("messages_so_far")); el_val_t stored_variant = json_get(pending_raw, EL_STR("tools_variant")); - el_val_t tools_json = ({ el_val_t _if_result_615 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_615 = (agentic_tools_with_web()); } else { _if_result_615 = (({ el_val_t _if_result_616 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_616 = (agentic_tools_all()); } else { _if_result_616 = (agentic_tools_literal()); } _if_result_616; })); } _if_result_615; }); + el_val_t tools_json = ({ el_val_t _if_result_619 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_619 = (agentic_tools_with_web()); } else { _if_result_619 = (({ el_val_t _if_result_620 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_620 = (agentic_tools_all()); } else { _if_result_620 = (agentic_tools_literal()); } _if_result_620; })); } _if_result_619; }); el_val_t blob = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"model\":\""), json_safe(model)), EL_STR("\"")), EL_STR(",\"safe_sys\":\"")), json_safe(safe_sys)), EL_STR("\"")), EL_STR(",\"tools_json\":\"")), json_safe(tools_json)), EL_STR("\"")), EL_STR(",\"messages\":\"")), json_safe(legacy_messages)), EL_STR("\"")), EL_STR(",\"tools_log\":\"\"")), EL_STR(",\"tool_use_id\":\"")), json_safe(call_id)), EL_STR("\"}")); state_set(el_str_concat(EL_STR("mcp_bridge:"), session_id), blob); return agentic_resume(session_id, call_id, tool_result); @@ -29754,24 +29842,24 @@ el_val_t rate_limit_check(el_val_t ip, el_val_t path) { return EL_STR(""); } el_val_t limit_str = state_get(EL_STR("soul_rate_limit")); - el_val_t limit = ({ el_val_t _if_result_617 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_617 = (60); } else { _if_result_617 = (str_to_int(limit_str)); } _if_result_617; }); + el_val_t limit = ({ el_val_t _if_result_621 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_621 = (60); } else { _if_result_621 = (str_to_int(limit_str)); } _if_result_621; }); el_val_t now = time_now(); el_val_t window_key = el_str_concat(el_str_concat(EL_STR("rl:"), ip), EL_STR(":window")); el_val_t count_key = el_str_concat(el_str_concat(EL_STR("rl:"), ip), EL_STR(":count")); el_val_t win_str = state_get(window_key); - el_val_t win_start = ({ el_val_t _if_result_618 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_618 = (now); } else { _if_result_618 = (str_to_int(win_str)); } _if_result_618; }); + el_val_t win_start = ({ el_val_t _if_result_622 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_622 = (now); } else { _if_result_622 = (str_to_int(win_str)); } _if_result_622; }); el_val_t elapsed = (now - win_start); el_val_t in_window = (elapsed < 60); el_val_t prev_count_str = state_get(count_key); - el_val_t prev_count = ({ el_val_t _if_result_619 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_619 = (0); } else { _if_result_619 = (str_to_int(prev_count_str)); } _if_result_619; }); - el_val_t eff_count = ({ el_val_t _if_result_620 = 0; if (in_window) { _if_result_620 = (prev_count); } else { _if_result_620 = (0); } _if_result_620; }); - el_val_t eff_win = ({ el_val_t _if_result_621 = 0; if (in_window) { _if_result_621 = (win_start); } else { _if_result_621 = (now); } _if_result_621; }); + el_val_t prev_count = ({ el_val_t _if_result_623 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_623 = (0); } else { _if_result_623 = (str_to_int(prev_count_str)); } _if_result_623; }); + el_val_t eff_count = ({ el_val_t _if_result_624 = 0; if (in_window) { _if_result_624 = (prev_count); } else { _if_result_624 = (0); } _if_result_624; }); + el_val_t eff_win = ({ el_val_t _if_result_625 = 0; if (in_window) { _if_result_625 = (win_start); } else { _if_result_625 = (now); } _if_result_625; }); el_val_t new_count = (eff_count + 1); state_set(count_key, int_to_str(new_count)); state_set(window_key, int_to_str(eff_win)); if (new_count > limit) { el_val_t retry_after = (60 - (now - eff_win)); - el_val_t eff_retry = ({ el_val_t _if_result_622 = 0; if ((retry_after < 0)) { _if_result_622 = (0); } else { _if_result_622 = (retry_after); } _if_result_622; }); + el_val_t eff_retry = ({ el_val_t _if_result_626 = 0; if ((retry_after < 0)) { _if_result_626 = (0); } else { _if_result_626 = (retry_after); } _if_result_626; }); return el_str_concat(el_str_concat(EL_STR("{\"__status__\":429,\"error\":\"rate limit exceeded\",\"code\":\"rate_limited\",\"retry_after_secs\":"), int_to_str(eff_retry)), EL_STR("}")); } return EL_STR(""); @@ -29800,18 +29888,18 @@ el_val_t err_405(el_val_t method, el_val_t path) { el_val_t route_health(void) { el_val_t cgi_id = state_get(EL_STR("soul_cgi_id")); el_val_t boot = state_get(EL_STR("soul_boot_count")); - el_val_t boot_num = ({ el_val_t _if_result_623 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_623 = (EL_STR("0")); } else { _if_result_623 = (boot); } _if_result_623; }); + el_val_t boot_num = ({ el_val_t _if_result_627 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_627 = (EL_STR("0")); } else { _if_result_627 = (boot); } _if_result_627; }); el_val_t node_ct = engram_node_count(); el_val_t edge_ct = engram_edge_count(); el_val_t pulse = state_get(EL_STR("soul.pulse")); - el_val_t pulse_num = ({ el_val_t _if_result_624 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_624 = (EL_STR("0")); } else { _if_result_624 = (pulse); } _if_result_624; }); + el_val_t pulse_num = ({ el_val_t _if_result_628 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_628 = (EL_STR("0")); } else { _if_result_628 = (pulse); } _if_result_628; }); el_val_t boot_ts_str = state_get(EL_STR("soul_boot_ts")); - el_val_t uptime_secs = ({ el_val_t _if_result_625 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_625 = ((-1)); } else { _if_result_625 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_625; }); + el_val_t uptime_secs = ({ el_val_t _if_result_629 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_629 = ((-1)); } else { _if_result_629 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_629; }); el_val_t model = state_get(EL_STR("soul_model")); - el_val_t eff_model = ({ el_val_t _if_result_626 = 0; if (str_eq(model, EL_STR(""))) { _if_result_626 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_626 = (model); } _if_result_626; }); + el_val_t eff_model = ({ el_val_t _if_result_630 = 0; if (str_eq(model, EL_STR(""))) { _if_result_630 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_630 = (model); } _if_result_630; }); el_val_t llm_probe = llm_call_system(eff_model, EL_STR("You are a health probe. Reply with the single word: ok"), EL_STR("ping")); el_val_t llm_ok = (((!str_eq(llm_probe, EL_STR("")) && !str_starts_with(llm_probe, EL_STR("{\"error\""))) && !str_starts_with(llm_probe, EL_STR("{\"type\":\"error\""))) && !str_contains(llm_probe, EL_STR("authentication_error"))); - el_val_t llm_status = ({ el_val_t _if_result_627 = 0; if (llm_ok) { _if_result_627 = (EL_STR("ok")); } else { _if_result_627 = (EL_STR("unreachable")); } _if_result_627; }); + el_val_t llm_status = ({ el_val_t _if_result_631 = 0; if (llm_ok) { _if_result_631 = (EL_STR("ok")); } else { _if_result_631 = (EL_STR("unreachable")); } _if_result_631; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"status\":\"alive\""), EL_STR(",\"cgi_id\":\"")), cgi_id), EL_STR("\"")), EL_STR(",\"boot\":")), boot_num), EL_STR(",\"uptime_secs\":")), int_to_str(uptime_secs)), EL_STR(",\"node_count\":")), int_to_str(node_ct)), EL_STR(",\"edge_count\":")), int_to_str(edge_ct)), EL_STR(",\"pulse\":")), pulse_num), EL_STR(",\"llm\":\"")), llm_status), EL_STR("\"")), EL_STR(",\"layers\":{\"l0\":\"core\",\"l1\":\"safety\",\"l2\":\"stewardship\",\"l3\":\"")), imprint_current()), EL_STR("\"}}")); return 0; } @@ -29881,30 +29969,30 @@ el_val_t handle_dharma_recv(el_val_t body) { el_val_t from_id = json_get(body, EL_STR("from")); el_val_t event_type = json_get(content_raw, EL_STR("event_type")); el_val_t payload = json_get(content_raw, EL_STR("payload")); - el_val_t eff_event = ({ el_val_t _if_result_628 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_628 = (EL_STR("chat")); } else { _if_result_628 = (event_type); } _if_result_628; }); - el_val_t eff_payload = ({ el_val_t _if_result_629 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_629 = (content_raw); } else { _if_result_629 = (payload); } _if_result_629; }); + el_val_t eff_event = ({ el_val_t _if_result_632 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_632 = (EL_STR("chat")); } else { _if_result_632 = (event_type); } _if_result_632; }); + el_val_t eff_payload = ({ el_val_t _if_result_633 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_633 = (content_raw); } else { _if_result_633 = (payload); } _if_result_633; }); if (str_eq(eff_event, EL_STR("chat"))) { el_val_t msg = json_get(eff_payload, EL_STR("message")); - el_val_t chat_body = ({ el_val_t _if_result_630 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_630 = (el_str_concat(el_str_concat(EL_STR("{\"message\":\""), str_replace(str_replace(eff_payload, EL_STR("\\"), EL_STR("\\\\")), EL_STR("\""), EL_STR("\\\""))), EL_STR("\"}"))); } else { _if_result_630 = (eff_payload); } _if_result_630; }); + el_val_t chat_body = ({ el_val_t _if_result_634 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_634 = (el_str_concat(el_str_concat(EL_STR("{\"message\":\""), str_replace(str_replace(eff_payload, EL_STR("\\"), EL_STR("\\\\")), EL_STR("\""), EL_STR("\\\""))), EL_STR("\"}"))); } else { _if_result_634 = (eff_payload); } _if_result_634; }); el_val_t agentic_flag = json_get_bool(eff_payload, EL_STR("agentic")); el_val_t raw_msg = json_get(chat_body, EL_STR("message")); el_val_t req_mode = json_get(chat_body, EL_STR("mode")); - el_val_t reply = ({ el_val_t _if_result_631 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_631 = (handle_chat_plan(chat_body)); } else { _if_result_631 = (({ el_val_t _if_result_632 = 0; if (agentic_flag) { _if_result_632 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_632 = (screened_reply); } _if_result_632; })); } _if_result_631; }); + el_val_t reply = ({ el_val_t _if_result_635 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_635 = (handle_chat_plan(chat_body)); } else { _if_result_635 = (({ el_val_t _if_result_636 = 0; if (agentic_flag) { _if_result_636 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_636 = (screened_reply); } _if_result_636; })); } _if_result_635; }); auto_persist(chat_body, reply); return reply; } if (str_eq(eff_event, EL_STR("memory"))) { el_val_t query = json_get(eff_payload, EL_STR("query")); el_val_t limit_str = json_get(eff_payload, EL_STR("limit")); - el_val_t limit = ({ el_val_t _if_result_633 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_633 = (20); } else { _if_result_633 = (str_to_int(limit_str)); } _if_result_633; }); - el_val_t q = ({ el_val_t _if_result_634 = 0; if (str_eq(query, EL_STR(""))) { _if_result_634 = (eff_payload); } else { _if_result_634 = (query); } _if_result_634; }); + el_val_t limit = ({ el_val_t _if_result_637 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_637 = (20); } else { _if_result_637 = (str_to_int(limit_str)); } _if_result_637; }); + el_val_t q = ({ el_val_t _if_result_638 = 0; if (str_eq(query, EL_STR(""))) { _if_result_638 = (eff_payload); } else { _if_result_638 = (query); } _if_result_638; }); return engram_search_json(q, limit); } if (str_eq(eff_event, EL_STR("tool"))) { el_val_t path_field = json_get(eff_payload, EL_STR("path")); el_val_t method_field = json_get(eff_payload, EL_STR("method")); el_val_t tool_body = json_get(eff_payload, EL_STR("body")); - el_val_t eff_method = ({ el_val_t _if_result_635 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_635 = (EL_STR("POST")); } else { _if_result_635 = (method_field); } _if_result_635; }); + el_val_t eff_method = ({ el_val_t _if_result_639 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_639 = (EL_STR("POST")); } else { _if_result_639 = (method_field); } _if_result_639; }); return handle_tool(path_field, eff_method, tool_body); } if (str_eq(eff_event, EL_STR("see"))) { @@ -29939,7 +30027,7 @@ el_val_t connectd_get(el_val_t suffix) { } el_val_t connectd_post(el_val_t suffix, el_val_t body) { - el_val_t eff = ({ el_val_t _if_result_636 = 0; if (str_eq(body, EL_STR(""))) { _if_result_636 = (EL_STR("{}")); } else { _if_result_636 = (body); } _if_result_636; }); + el_val_t eff = ({ el_val_t _if_result_640 = 0; if (str_eq(body, EL_STR(""))) { _if_result_640 = (EL_STR("{}")); } else { _if_result_640 = (body); } _if_result_640; }); el_val_t tmp = el_str_concat(el_str_concat(EL_STR("/tmp/neuron-connectors-req-"), int_to_str(time_now())), EL_STR(".json")); fs_write(tmp, eff); el_val_t out = exec_capture(el_str_concat(el_str_concat(el_str_concat(EL_STR("curl -s --max-time 20 -X POST http://127.0.0.1:7771"), suffix), EL_STR(" -H 'Content-Type: application/json' -d @")), tmp)); @@ -30006,17 +30094,17 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { engram_save(snap_path); el_val_t snap = fs_read(snap_path); el_val_t edges_raw = json_get_raw(snap, EL_STR("edges")); - return ({ el_val_t _if_result_637 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_637 = (EL_STR("[]")); } else { _if_result_637 = (edges_raw); } _if_result_637; }); + return ({ el_val_t _if_result_641 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_641 = (EL_STR("[]")); } else { _if_result_641 = (edges_raw); } _if_result_641; }); } if (str_eq(clean, EL_STR("/api/chat"))) { el_val_t raw_msg = json_get(body, EL_STR("message")); - el_val_t eff_msg = ({ el_val_t _if_result_638 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_638 = (body); } else { _if_result_638 = (raw_msg); } _if_result_638; }); + el_val_t eff_msg = ({ el_val_t _if_result_642 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_642 = (body); } else { _if_result_642 = (raw_msg); } _if_result_642; }); if (str_eq(eff_msg, EL_STR(""))) { return EL_STR("{\"error\":\"message is required\",\"code\":\"missing_param\"}"); } el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic")); el_val_t req_mode = json_get(body, EL_STR("mode")); - el_val_t reply = ({ el_val_t _if_result_639 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_639 = (handle_chat_plan(body)); } else { _if_result_639 = (({ el_val_t _if_result_640 = 0; if (agentic_flag) { _if_result_640 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_640 = (screened_reply); } _if_result_640; })); } _if_result_639; }); + el_val_t reply = ({ el_val_t _if_result_643 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_643 = (handle_chat_plan(body)); } else { _if_result_643 = (({ el_val_t _if_result_644 = 0; if (agentic_flag) { _if_result_644 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_644 = (screened_reply); } _if_result_644; })); } _if_result_643; }); auto_persist(body, reply); return reply; } @@ -30097,7 +30185,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { el_val_t rp_id = str_slice(clean, 18, str_len(clean)); if (!str_eq(rp_id, EL_STR(""))) { el_val_t rp_raw = state_get(el_str_concat(EL_STR("run_progress_"), rp_id)); - el_val_t rp_arr = ({ el_val_t _if_result_641 = 0; if (str_eq(rp_raw, EL_STR(""))) { _if_result_641 = (EL_STR("[]")); } else { _if_result_641 = (el_str_concat(el_str_concat(EL_STR("["), rp_raw), EL_STR("]"))); } _if_result_641; }); + el_val_t rp_arr = ({ el_val_t _if_result_645 = 0; if (str_eq(rp_raw, EL_STR(""))) { _if_result_645 = (EL_STR("[]")); } else { _if_result_645 = (el_str_concat(el_str_concat(EL_STR("["), rp_raw), EL_STR("]"))); } _if_result_645; }); return el_str_concat(el_str_concat(EL_STR("{\"progress\":"), rp_arr), EL_STR("}")); } } @@ -30107,7 +30195,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t gs_after = str_slice(clean, 14, str_len(clean)); el_val_t gs_slash = str_index_of(gs_after, EL_STR("/")); - el_val_t gs_id = ({ el_val_t _if_result_642 = 0; if ((gs_slash < 0)) { _if_result_642 = (gs_after); } else { _if_result_642 = (str_slice(gs_after, 0, gs_slash)); } _if_result_642; }); + el_val_t gs_id = ({ el_val_t _if_result_646 = 0; if ((gs_slash < 0)) { _if_result_646 = (gs_after); } else { _if_result_646 = (str_slice(gs_after, 0, gs_slash)); } _if_result_646; }); if (!str_eq(gs_id, EL_STR(""))) { return session_get(gs_id); } @@ -30121,14 +30209,14 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/")) && str_ends_with(clean, EL_STR("/tool_result"))) { el_val_t after = str_slice(clean, 14, str_len(clean)); el_val_t slash = str_index_of(after, EL_STR("/")); - el_val_t session_id = ({ el_val_t _if_result_643 = 0; if ((slash < 0)) { _if_result_643 = (after); } else { _if_result_643 = (str_slice(after, 0, slash)); } _if_result_643; }); + el_val_t session_id = ({ el_val_t _if_result_647 = 0; if ((slash < 0)) { _if_result_647 = (after); } else { _if_result_647 = (str_slice(after, 0, slash)); } _if_result_647; }); return handle_tool_result(session_id, body); } if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t sess_after = str_slice(clean, 14, str_len(clean)); el_val_t sess_slash = str_index_of(sess_after, EL_STR("/")); - el_val_t sess_id = ({ el_val_t _if_result_644 = 0; if ((sess_slash < 0)) { _if_result_644 = (sess_after); } else { _if_result_644 = (str_slice(sess_after, 0, sess_slash)); } _if_result_644; }); - el_val_t sess_sub = ({ el_val_t _if_result_645 = 0; if ((sess_slash < 0)) { _if_result_645 = (EL_STR("")); } else { _if_result_645 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_645; }); + el_val_t sess_id = ({ el_val_t _if_result_648 = 0; if ((sess_slash < 0)) { _if_result_648 = (sess_after); } else { _if_result_648 = (str_slice(sess_after, 0, sess_slash)); } _if_result_648; }); + el_val_t sess_sub = ({ el_val_t _if_result_649 = 0; if ((sess_slash < 0)) { _if_result_649 = (EL_STR("")); } else { _if_result_649 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_649; }); if (!str_eq(sess_id, EL_STR("")) && str_eq(sess_sub, EL_STR("approve"))) { return handle_session_approve(sess_id, body); } @@ -30152,7 +30240,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { } el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic")); el_val_t req_mode = json_get(body, EL_STR("mode")); - el_val_t reply = ({ el_val_t _if_result_646 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_646 = (handle_chat_plan(body)); } else { _if_result_646 = (({ el_val_t _if_result_647 = 0; if (agentic_flag) { _if_result_647 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_647 = (screened_reply); } _if_result_647; })); } _if_result_646; }); + el_val_t reply = ({ el_val_t _if_result_650 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_650 = (handle_chat_plan(body)); } else { _if_result_650 = (({ el_val_t _if_result_651 = 0; if (agentic_flag) { _if_result_651 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_651 = (screened_reply); } _if_result_651; })); } _if_result_650; }); auto_persist(body, reply); return reply; } @@ -30276,7 +30364,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t del_after = str_slice(clean, 14, str_len(clean)); el_val_t del_slash = str_index_of(del_after, EL_STR("/")); - el_val_t del_id = ({ el_val_t _if_result_648 = 0; if ((del_slash < 0)) { _if_result_648 = (del_after); } else { _if_result_648 = (str_slice(del_after, 0, del_slash)); } _if_result_648; }); + el_val_t del_id = ({ el_val_t _if_result_652 = 0; if ((del_slash < 0)) { _if_result_652 = (del_after); } else { _if_result_652 = (str_slice(del_after, 0, del_slash)); } _if_result_652; }); if (!str_eq(del_id, EL_STR(""))) { return session_delete(del_id); } @@ -30287,7 +30375,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t patch_after = str_slice(clean, 14, str_len(clean)); el_val_t patch_slash = str_index_of(patch_after, EL_STR("/")); - el_val_t patch_id = ({ el_val_t _if_result_649 = 0; if ((patch_slash < 0)) { _if_result_649 = (patch_after); } else { _if_result_649 = (str_slice(patch_after, 0, patch_slash)); } _if_result_649; }); + el_val_t patch_id = ({ el_val_t _if_result_653 = 0; if ((patch_slash < 0)) { _if_result_653 = (patch_after); } else { _if_result_653 = (str_slice(patch_after, 0, patch_slash)); } _if_result_653; }); if (!str_eq(patch_id, EL_STR(""))) { return session_update_patch(patch_id, body); } @@ -30413,8 +30501,8 @@ el_val_t aff_try_slot(el_val_t slot_json, el_val_t aff_7d_ts, el_val_t acc_key) } } el_val_t bn_ts_raw = state_get(EL_STR("_ats_ts_raw")); - el_val_t bn_ts = ({ el_val_t _if_result_650 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_650 = (0); } else { _if_result_650 = (str_to_int(bn_ts_raw)); } _if_result_650; }); - el_val_t snip = ({ el_val_t _if_result_651 = 0; if ((str_len(bn_c) > 200)) { _if_result_651 = (str_slice(bn_c, 0, 200)); } else { _if_result_651 = (bn_c); } _if_result_651; }); + el_val_t bn_ts = ({ el_val_t _if_result_654 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_654 = (0); } else { _if_result_654 = (str_to_int(bn_ts_raw)); } _if_result_654; }); + el_val_t snip = ({ el_val_t _if_result_655 = 0; if ((str_len(bn_c) > 200)) { _if_result_655 = (str_slice(bn_c, 0, 200)); } else { _if_result_655 = (bn_c); } _if_result_655; }); if ((bn_ts >= aff_7d_ts) && !str_eq(snip, EL_STR(""))) { el_val_t cur_acc = state_get(acc_key); if (str_eq(cur_acc, EL_STR(""))) { @@ -30435,21 +30523,21 @@ el_val_t load_identity_context(void) { el_val_t intel_ok = (!str_eq(node_intel, EL_STR("")) && !str_eq(node_intel, EL_STR("null"))); el_val_t values_ok = (!str_eq(node_values, EL_STR("")) && !str_eq(node_values, EL_STR("null"))); el_val_t mem_ok = (!str_eq(node_mem_phil, EL_STR("")) && !str_eq(node_mem_phil, EL_STR("null"))); - el_val_t intel_content = ({ el_val_t _if_result_652 = 0; if (intel_ok) { _if_result_652 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_652 = (EL_STR("")); } _if_result_652; }); - el_val_t values_content = ({ el_val_t _if_result_653 = 0; if (values_ok) { _if_result_653 = (json_get(node_values, EL_STR("content"))); } else { _if_result_653 = (EL_STR("")); } _if_result_653; }); - el_val_t mem_content = ({ el_val_t _if_result_654 = 0; if (mem_ok) { _if_result_654 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_654 = (EL_STR("")); } _if_result_654; }); - el_val_t intel_short = ({ el_val_t _if_result_655 = 0; if ((str_len(intel_content) > 2000)) { _if_result_655 = (str_slice(intel_content, 0, 2000)); } else { _if_result_655 = (intel_content); } _if_result_655; }); - el_val_t values_short = ({ el_val_t _if_result_656 = 0; if ((str_len(values_content) > 2000)) { _if_result_656 = (str_slice(values_content, 0, 2000)); } else { _if_result_656 = (values_content); } _if_result_656; }); - el_val_t mem_short = ({ el_val_t _if_result_657 = 0; if ((str_len(mem_content) > 2000)) { _if_result_657 = (str_slice(mem_content, 0, 2000)); } else { _if_result_657 = (mem_content); } _if_result_657; }); + el_val_t intel_content = ({ el_val_t _if_result_656 = 0; if (intel_ok) { _if_result_656 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_656 = (EL_STR("")); } _if_result_656; }); + el_val_t values_content = ({ el_val_t _if_result_657 = 0; if (values_ok) { _if_result_657 = (json_get(node_values, EL_STR("content"))); } else { _if_result_657 = (EL_STR("")); } _if_result_657; }); + el_val_t mem_content = ({ el_val_t _if_result_658 = 0; if (mem_ok) { _if_result_658 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_658 = (EL_STR("")); } _if_result_658; }); + el_val_t intel_short = ({ el_val_t _if_result_659 = 0; if ((str_len(intel_content) > 2000)) { _if_result_659 = (str_slice(intel_content, 0, 2000)); } else { _if_result_659 = (intel_content); } _if_result_659; }); + el_val_t values_short = ({ el_val_t _if_result_660 = 0; if ((str_len(values_content) > 2000)) { _if_result_660 = (str_slice(values_content, 0, 2000)); } else { _if_result_660 = (values_content); } _if_result_660; }); + el_val_t mem_short = ({ el_val_t _if_result_661 = 0; if ((str_len(mem_content) > 2000)) { _if_result_661 = (str_slice(mem_content, 0, 2000)); } else { _if_result_661 = (mem_content); } _if_result_661; }); el_val_t parts_count = 0; - parts_count = ({ el_val_t _if_result_658 = 0; if (intel_ok) { _if_result_658 = ((parts_count + 1)); } else { _if_result_658 = (parts_count); } _if_result_658; }); - parts_count = ({ el_val_t _if_result_659 = 0; if (values_ok) { _if_result_659 = ((parts_count + 1)); } else { _if_result_659 = (parts_count); } _if_result_659; }); - parts_count = ({ el_val_t _if_result_660 = 0; if (mem_ok) { _if_result_660 = ((parts_count + 1)); } else { _if_result_660 = (parts_count); } _if_result_660; }); + parts_count = ({ el_val_t _if_result_662 = 0; if (intel_ok) { _if_result_662 = ((parts_count + 1)); } else { _if_result_662 = (parts_count); } _if_result_662; }); + parts_count = ({ el_val_t _if_result_663 = 0; if (values_ok) { _if_result_663 = ((parts_count + 1)); } else { _if_result_663 = (parts_count); } _if_result_663; }); + parts_count = ({ el_val_t _if_result_664 = 0; if (mem_ok) { _if_result_664 = ((parts_count + 1)); } else { _if_result_664 = (parts_count); } _if_result_664; }); if (parts_count > 0) { el_val_t ctx = EL_STR(""); - ctx = ({ el_val_t _if_result_661 = 0; if (intel_ok) { _if_result_661 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_661 = (ctx); } _if_result_661; }); - ctx = ({ el_val_t _if_result_662 = 0; if (values_ok) { _if_result_662 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_662 = (ctx); } _if_result_662; }); - ctx = ({ el_val_t _if_result_663 = 0; if (mem_ok) { _if_result_663 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_663 = (ctx); } _if_result_663; }); + ctx = ({ el_val_t _if_result_665 = 0; if (intel_ok) { _if_result_665 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_665 = (ctx); } _if_result_665; }); + ctx = ({ el_val_t _if_result_666 = 0; if (values_ok) { _if_result_666 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_666 = (ctx); } _if_result_666; }); + ctx = ({ el_val_t _if_result_667 = 0; if (mem_ok) { _if_result_667 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_667 = (ctx); } _if_result_667; }); state_set(EL_STR("soul_identity_context"), ctx); println(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] identity context loaded ("), int_to_str(str_len(ctx))), EL_STR(" chars, ")), int_to_str(parts_count)), EL_STR(" nodes)"))); } @@ -30472,10 +30560,10 @@ el_val_t load_identity_context(void) { el_val_t bell_raw = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 3); el_val_t bell_aff_ok = (!str_eq(bell_raw, EL_STR("")) && !str_eq(bell_raw, EL_STR("[]"))); el_val_t aff_ctx = EL_STR(""); - aff_ctx = ({ el_val_t _if_result_664 = 0; if (bell_aff_ok) { (void)(state_set(EL_STR("_bell_acc"), EL_STR(""))); (void)(aff_try_slot(json_array_get(bell_raw, 0), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 1), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 2), aff_7d, EL_STR("_bell_acc"))); _if_result_664 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_664 = (EL_STR("")); } _if_result_664; }); + aff_ctx = ({ el_val_t _if_result_668 = 0; if (bell_aff_ok) { (void)(state_set(EL_STR("_bell_acc"), EL_STR(""))); (void)(aff_try_slot(json_array_get(bell_raw, 0), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 1), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 2), aff_7d, EL_STR("_bell_acc"))); _if_result_668 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_668 = (EL_STR("")); } _if_result_668; }); el_val_t pos_raw = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 3); el_val_t pos_aff_ok = (!str_eq(pos_raw, EL_STR("")) && !str_eq(pos_raw, EL_STR("[]"))); - aff_ctx = ({ el_val_t _if_result_665 = 0; if (pos_aff_ok) { (void)(state_set(EL_STR("_pos_acc"), aff_ctx)); (void)(aff_try_slot(json_array_get(pos_raw, 0), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 1), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 2), aff_7d, EL_STR("_pos_acc"))); _if_result_665 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_665 = (aff_ctx); } _if_result_665; }); + aff_ctx = ({ el_val_t _if_result_669 = 0; if (pos_aff_ok) { (void)(state_set(EL_STR("_pos_acc"), aff_ctx)); (void)(aff_try_slot(json_array_get(pos_raw, 0), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 1), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 2), aff_7d, EL_STR("_pos_acc"))); _if_result_669 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_669 = (aff_ctx); } _if_result_669; }); if (!str_eq(aff_ctx, EL_STR(""))) { state_set(EL_STR("soul_affective_context"), aff_ctx); println(el_str_concat(el_str_concat(EL_STR("[soul] affective context loaded ("), int_to_str(str_len(aff_ctx))), EL_STR(" chars)"))); @@ -30521,19 +30609,19 @@ el_val_t seed_persona_from_env(void) { el_val_t emit_session_start_event(void) { el_val_t boot = state_get(EL_STR("soul_boot_count")); - el_val_t boot_num = ({ el_val_t _if_result_666 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_666 = (EL_STR("0")); } else { _if_result_666 = (boot); } _if_result_666; }); + el_val_t boot_num = ({ el_val_t _if_result_670 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_670 = (EL_STR("0")); } else { _if_result_670 = (boot); } _if_result_670; }); el_val_t node_ct = engram_node_count(); el_val_t edge_ct = engram_edge_count(); el_val_t id_ctx = state_get(EL_STR("soul_identity_context")); - el_val_t has_identity = ({ el_val_t _if_result_667 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_667 = (EL_STR("false")); } else { _if_result_667 = (EL_STR("true")); } _if_result_667; }); + el_val_t has_identity = ({ el_val_t _if_result_671 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_671 = (EL_STR("false")); } else { _if_result_671 = (EL_STR("true")); } _if_result_671; }); el_val_t cgi_from_state = state_get(EL_STR("soul_cgi_id")); el_val_t cgi_from_env = env(EL_STR("SOUL_CGI_ID")); - el_val_t eff_cgi = ({ el_val_t _if_result_668 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_668 = (cgi_from_state); } else { _if_result_668 = (({ el_val_t _if_result_669 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_669 = (cgi_from_env); } else { _if_result_669 = (EL_STR("ntn-genesis")); } _if_result_669; })); } _if_result_668; }); + el_val_t eff_cgi = ({ el_val_t _if_result_672 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_672 = (cgi_from_state); } else { _if_result_672 = (({ el_val_t _if_result_673 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_673 = (cgi_from_env); } else { _if_result_673 = (EL_STR("ntn-genesis")); } _if_result_673; })); } _if_result_672; }); el_val_t ts = time_now(); el_val_t prev_sum_node = engram_get_node_by_label(EL_STR("session:summary")); el_val_t prev_sum_ok = (!str_eq(prev_sum_node, EL_STR("")) && !str_eq(prev_sum_node, EL_STR("null"))); - el_val_t prev_sum_content = ({ el_val_t _if_result_670 = 0; if (prev_sum_ok) { _if_result_670 = (json_get(prev_sum_node, EL_STR("content"))); } else { el_val_t sum_search = engram_search_json(EL_STR("SessionSummary session:summary previous-session"), 2); el_val_t sum_srch_ok = (!str_eq(sum_search, EL_STR("")) && !str_eq(sum_search, EL_STR("[]"))); _if_result_670 = (({ el_val_t _if_result_671 = 0; if (sum_srch_ok) { el_val_t sn = json_array_get(sum_search, 0); el_val_t stype = json_get(sn, EL_STR("node_type")); el_val_t scontent = json_get(sn, EL_STR("content")); _if_result_671 = (({ el_val_t _if_result_672 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_672 = (scontent); } else { _if_result_672 = (EL_STR("")); } _if_result_672; })); } else { _if_result_671 = (EL_STR("")); } _if_result_671; })); } _if_result_670; }); - el_val_t has_prev_sum = ({ el_val_t _if_result_673 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_673 = (EL_STR("false")); } else { _if_result_673 = (EL_STR("true")); } _if_result_673; }); + el_val_t prev_sum_content = ({ el_val_t _if_result_674 = 0; if (prev_sum_ok) { _if_result_674 = (json_get(prev_sum_node, EL_STR("content"))); } else { el_val_t sum_search = engram_search_json(EL_STR("SessionSummary session:summary previous-session"), 2); el_val_t sum_srch_ok = (!str_eq(sum_search, EL_STR("")) && !str_eq(sum_search, EL_STR("[]"))); _if_result_674 = (({ el_val_t _if_result_675 = 0; if (sum_srch_ok) { el_val_t sn = json_array_get(sum_search, 0); el_val_t stype = json_get(sn, EL_STR("node_type")); el_val_t scontent = json_get(sn, EL_STR("content")); _if_result_675 = (({ el_val_t _if_result_676 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_676 = (scontent); } else { _if_result_676 = (EL_STR("")); } _if_result_676; })); } else { _if_result_675 = (EL_STR("")); } _if_result_675; })); } _if_result_674; }); + el_val_t has_prev_sum = ({ el_val_t _if_result_677 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_677 = (EL_STR("false")); } else { _if_result_677 = (EL_STR("true")); } _if_result_677; }); if (!str_eq(prev_sum_content, EL_STR(""))) { state_set(EL_STR("soul_prev_session_summary"), prev_sum_content); println(el_str_concat(el_str_concat(EL_STR("[soul] previous session summary loaded ("), int_to_str(str_len(prev_sum_content))), EL_STR(" chars)"))); @@ -30580,23 +30668,23 @@ el_val_t layered_cycle(el_val_t raw_input) { el_val_t continuity = steward_session_check(screened, session_id); el_val_t cont_status = json_get(continuity, EL_STR("status")); el_val_t cont_action = json_get(continuity, EL_STR("action")); - el_val_t cont_key = ({ el_val_t _if_result_674 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_674 = (EL_STR("session_continuity")); } else { _if_result_674 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_674; }); + el_val_t cont_key = ({ el_val_t _if_result_678 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_678 = (EL_STR("session_continuity")); } else { _if_result_678 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_678; }); state_set(cont_key, cont_status); - el_val_t guided = ({ el_val_t _if_result_675 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_675 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_675 = (({ el_val_t _if_result_676 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_676 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_676 = (screened); } _if_result_676; })); } _if_result_675; }); + el_val_t guided = ({ el_val_t _if_result_679 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_679 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_679 = (({ el_val_t _if_result_680 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_680 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_680 = (screened); } _if_result_680; })); } _if_result_679; }); el_val_t imprint_id = imprint_current(); el_val_t steward_result = steward_align(guided, imprint_id); el_val_t steward_action = json_get(steward_result, EL_STR("action")); - el_val_t aligned = ({ el_val_t _if_result_677 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_677 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_677 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_677; }); + el_val_t aligned = ({ el_val_t _if_result_681 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_681 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_681 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_681; }); el_val_t lc_aff_cutoff = (time_now() - 259200); el_val_t lc_bell_nodes = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 2); el_val_t lc_has_bell = (!str_eq(lc_bell_nodes, EL_STR("")) && !str_eq(lc_bell_nodes, EL_STR("[]"))); - el_val_t lc_bell_note = ({ el_val_t _if_result_678 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_c = json_get(lb0, EL_STR("content")); el_val_t lbm = EL_STR(" | ts:"); el_val_t lbmp = str_index_of(lb_c, lbm); el_val_t lb_ts_raw = ({ el_val_t _if_result_679 = 0; if ((lbmp >= 0)) { el_val_t lbs = el_str_concat(lbmp, str_len(lbm)); el_val_t lbr = str_slice(lb_c, lbs, str_len(lb_c)); el_val_t lbn = str_index_of(lbr, EL_STR(" | ")); _if_result_679 = (({ el_val_t _if_result_680 = 0; if ((lbn < 0)) { _if_result_680 = (lbr); } else { _if_result_680 = (str_slice(lbr, 0, lbn)); } _if_result_680; })); } else { el_val_t lbca = json_get(lb0, EL_STR("created_at")); _if_result_679 = (({ el_val_t _if_result_681 = 0; if (str_eq(lbca, EL_STR(""))) { _if_result_681 = (json_get(lb0, EL_STR("updated_at"))); } else { _if_result_681 = (lbca); } _if_result_681; })); } _if_result_679; }); el_val_t lb_ts = ({ el_val_t _if_result_682 = 0; if (str_eq(lb_ts_raw, EL_STR(""))) { _if_result_682 = (0); } else { _if_result_682 = (str_to_int(lb_ts_raw)); } _if_result_682; }); _if_result_678 = (({ el_val_t _if_result_683 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_683 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_683 = (EL_STR("")); } _if_result_683; })); } else { _if_result_678 = (EL_STR("")); } _if_result_678; }); + el_val_t lc_bell_note = ({ el_val_t _if_result_682 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_c = json_get(lb0, EL_STR("content")); el_val_t lbm = EL_STR(" | ts:"); el_val_t lbmp = str_index_of(lb_c, lbm); el_val_t lb_ts_raw = ({ el_val_t _if_result_683 = 0; if ((lbmp >= 0)) { el_val_t lbs = el_str_concat(lbmp, str_len(lbm)); el_val_t lbr = str_slice(lb_c, lbs, str_len(lb_c)); el_val_t lbn = str_index_of(lbr, EL_STR(" | ")); _if_result_683 = (({ el_val_t _if_result_684 = 0; if ((lbn < 0)) { _if_result_684 = (lbr); } else { _if_result_684 = (str_slice(lbr, 0, lbn)); } _if_result_684; })); } else { el_val_t lbca = json_get(lb0, EL_STR("created_at")); _if_result_683 = (({ el_val_t _if_result_685 = 0; if (str_eq(lbca, EL_STR(""))) { _if_result_685 = (json_get(lb0, EL_STR("updated_at"))); } else { _if_result_685 = (lbca); } _if_result_685; })); } _if_result_683; }); el_val_t lb_ts = ({ el_val_t _if_result_686 = 0; if (str_eq(lb_ts_raw, EL_STR(""))) { _if_result_686 = (0); } else { _if_result_686 = (str_to_int(lb_ts_raw)); } _if_result_686; }); _if_result_682 = (({ el_val_t _if_result_687 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_687 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_687 = (EL_STR("")); } _if_result_687; })); } else { _if_result_682 = (EL_STR("")); } _if_result_682; }); el_val_t lc_pos_nodes = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 2); el_val_t lc_has_pos = (!str_eq(lc_pos_nodes, EL_STR("")) && !str_eq(lc_pos_nodes, EL_STR("[]"))); - el_val_t lc_pos_note = ({ el_val_t _if_result_684 = 0; if ((lc_has_pos && str_eq(lc_bell_note, EL_STR("")))) { el_val_t lp0 = json_array_get(lc_pos_nodes, 0); el_val_t lp_c = json_get(lp0, EL_STR("content")); el_val_t lpm = EL_STR(" | ts:"); el_val_t lpmp = str_index_of(lp_c, lpm); el_val_t lp_ts_raw = ({ el_val_t _if_result_685 = 0; if ((lpmp >= 0)) { el_val_t lps = el_str_concat(lpmp, str_len(lpm)); el_val_t lpr = str_slice(lp_c, lps, str_len(lp_c)); el_val_t lpn = str_index_of(lpr, EL_STR(" | ")); _if_result_685 = (({ el_val_t _if_result_686 = 0; if ((lpn < 0)) { _if_result_686 = (lpr); } else { _if_result_686 = (str_slice(lpr, 0, lpn)); } _if_result_686; })); } else { el_val_t lpca = json_get(lp0, EL_STR("created_at")); _if_result_685 = (({ el_val_t _if_result_687 = 0; if (str_eq(lpca, EL_STR(""))) { _if_result_687 = (json_get(lp0, EL_STR("updated_at"))); } else { _if_result_687 = (lpca); } _if_result_687; })); } _if_result_685; }); el_val_t lp_ts = ({ el_val_t _if_result_688 = 0; if (str_eq(lp_ts_raw, EL_STR(""))) { _if_result_688 = (0); } else { _if_result_688 = (str_to_int(lp_ts_raw)); } _if_result_688; }); _if_result_684 = (({ el_val_t _if_result_689 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_689 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_689 = (EL_STR("")); } _if_result_689; })); } else { _if_result_684 = (EL_STR("")); } _if_result_684; }); - el_val_t lc_affective_note = ({ el_val_t _if_result_690 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_690 = (lc_bell_note); } else { _if_result_690 = (lc_pos_note); } _if_result_690; }); + el_val_t lc_pos_note = ({ el_val_t _if_result_688 = 0; if ((lc_has_pos && str_eq(lc_bell_note, EL_STR("")))) { el_val_t lp0 = json_array_get(lc_pos_nodes, 0); el_val_t lp_c = json_get(lp0, EL_STR("content")); el_val_t lpm = EL_STR(" | ts:"); el_val_t lpmp = str_index_of(lp_c, lpm); el_val_t lp_ts_raw = ({ el_val_t _if_result_689 = 0; if ((lpmp >= 0)) { el_val_t lps = el_str_concat(lpmp, str_len(lpm)); el_val_t lpr = str_slice(lp_c, lps, str_len(lp_c)); el_val_t lpn = str_index_of(lpr, EL_STR(" | ")); _if_result_689 = (({ el_val_t _if_result_690 = 0; if ((lpn < 0)) { _if_result_690 = (lpr); } else { _if_result_690 = (str_slice(lpr, 0, lpn)); } _if_result_690; })); } else { el_val_t lpca = json_get(lp0, EL_STR("created_at")); _if_result_689 = (({ el_val_t _if_result_691 = 0; if (str_eq(lpca, EL_STR(""))) { _if_result_691 = (json_get(lp0, EL_STR("updated_at"))); } else { _if_result_691 = (lpca); } _if_result_691; })); } _if_result_689; }); el_val_t lp_ts = ({ el_val_t _if_result_692 = 0; if (str_eq(lp_ts_raw, EL_STR(""))) { _if_result_692 = (0); } else { _if_result_692 = (str_to_int(lp_ts_raw)); } _if_result_692; }); _if_result_688 = (({ el_val_t _if_result_693 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_693 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_693 = (EL_STR("")); } _if_result_693; })); } else { _if_result_688 = (EL_STR("")); } _if_result_688; }); + el_val_t lc_affective_note = ({ el_val_t _if_result_694 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_694 = (lc_bell_note); } else { _if_result_694 = (lc_pos_note); } _if_result_694; }); el_val_t augmented_addendum = safety_augment_system(EL_STR(""), raw_input); - augmented_addendum = ({ el_val_t _if_result_691 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_691 = (augmented_addendum); } else { _if_result_691 = (({ el_val_t _if_result_692 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_692 = (lc_affective_note); } else { _if_result_692 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_692; })); } _if_result_691; }); + augmented_addendum = ({ el_val_t _if_result_695 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_695 = (augmented_addendum); } else { _if_result_695 = (({ el_val_t _if_result_696 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_696 = (lc_affective_note); } else { _if_result_696 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_696; })); } _if_result_695; }); state_set(EL_STR("layered_cycle_safety_system_addendum"), augmented_addendum); el_val_t output = imprint_respond(aligned, imprint_id); return safety_validate(output, screen_action); @@ -30606,17 +30694,17 @@ el_val_t layered_cycle(el_val_t raw_input) { int main(int _argc, char** _argv) { el_runtime_init_args(_argc, _argv); soul_cgi_id_raw = env(EL_STR("SOUL_CGI_ID")); - soul_cgi_id = ({ el_val_t _if_result_693 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_693 = (EL_STR("ntn-genesis")); } else { _if_result_693 = (soul_cgi_id_raw); } _if_result_693; }); + soul_cgi_id = ({ el_val_t _if_result_697 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_697 = (EL_STR("ntn-genesis")); } else { _if_result_697 = (soul_cgi_id_raw); } _if_result_697; }); port_raw = env(EL_STR("NEURON_PORT")); - port = ({ el_val_t _if_result_694 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_694 = (7770); } else { _if_result_694 = (str_to_int(port_raw)); } _if_result_694; }); + port = ({ el_val_t _if_result_698 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_698 = (7770); } else { _if_result_698 = (str_to_int(port_raw)); } _if_result_698; }); engram_url_raw = env(EL_STR("ENGRAM_URL")); engram_api_key_raw = env(EL_STR("ENGRAM_API_KEY")); snapshot_raw = env(EL_STR("SOUL_ENGRAM_PATH")); - snapshot = ({ el_val_t _if_result_695 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_695 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_695 = (snapshot_raw); } _if_result_695; }); + snapshot = ({ el_val_t _if_result_699 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_699 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_699 = (snapshot_raw); } _if_result_699; }); axon_raw = env(EL_STR("NEURON_API_URL")); - axon_base = ({ el_val_t _if_result_696 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_696 = (EL_STR("http://localhost:7771")); } else { _if_result_696 = (axon_raw); } _if_result_696; }); + axon_base = ({ el_val_t _if_result_700 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_700 = (EL_STR("http://localhost:7771")); } else { _if_result_700 = (axon_raw); } _if_result_700; }); studio_dir_raw = env(EL_STR("SOUL_STUDIO_DIR")); - studio_dir = ({ el_val_t _if_result_697 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_697 = (EL_STR("/Users/will/Development/neuron-technologies/products/cgi-studio/el-daemon")); } else { _if_result_697 = (studio_dir_raw); } _if_result_697; }); + studio_dir = ({ el_val_t _if_result_701 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_701 = (EL_STR("/Users/will/Development/neuron-technologies/products/cgi-studio/el-daemon")); } else { _if_result_701 = (studio_dir_raw); } _if_result_701; }); println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] boot - cgi="), soul_cgi_id), EL_STR(" port=")), int_to_str(port))); using_http_engram = !str_eq(engram_url_raw, EL_STR("")); engram_load(snapshot); @@ -30626,8 +30714,8 @@ int main(int _argc, char** _argv) { println(el_str_concat(el_str_concat(EL_STR("[soul] engram -> HTTP "), engram_url_raw), EL_STR(" (no local snapshot, first boot)"))); el_val_t nodes_json = http_get(el_str_concat(engram_url_raw, EL_STR("/api/nodes?limit=10000"))); el_val_t edges_json = http_get(el_str_concat(engram_url_raw, EL_STR("/api/edges"))); - el_val_t nodes_part = ({ el_val_t _if_result_698 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_698 = (EL_STR("[]")); } else { _if_result_698 = (nodes_json); } _if_result_698; }); - el_val_t edges_part = ({ el_val_t _if_result_699 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_699 = (EL_STR("[]")); } else { _if_result_699 = (edges_json); } _if_result_699; }); + el_val_t nodes_part = ({ el_val_t _if_result_702 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_702 = (EL_STR("[]")); } else { _if_result_702 = (nodes_json); } _if_result_702; }); + el_val_t edges_part = ({ el_val_t _if_result_703 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_703 = (EL_STR("[]")); } else { _if_result_703 = (edges_json); } _if_result_703; }); el_val_t snapshot_data = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"nodes\":"), nodes_part), EL_STR(",\"edges\":")), edges_part), EL_STR("}")); el_val_t tmp_path = el_str_concat(el_str_concat(EL_STR("/tmp/soul-engram-"), soul_cgi_id), EL_STR(".json")); fs_write(tmp_path, snapshot_data); @@ -30651,7 +30739,7 @@ int main(int _argc, char** _argv) { state_set(EL_STR("soul_engram_api_key"), engram_api_key_raw); state_set(EL_STR("soul.running"), EL_STR("true")); is_genesis = str_eq(soul_cgi_id, EL_STR("ntn-genesis")); - guard_disk = ({ el_val_t _if_result_700 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_700 = (fs_read(snapshot)); } else { _if_result_700 = (EL_STR("")); } _if_result_700; }); + guard_disk = ({ el_val_t _if_result_704 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_704 = (fs_read(snapshot)); } else { _if_result_704 = (EL_STR("")); } _if_result_704; }); guard_disk_len = str_len(guard_disk); safe_to_seed = (!using_http_engram && !((guard_disk_len > 200000) && ((engram_node_count() * 16000) < guard_disk_len))); if (is_genesis && !safe_to_seed) { diff --git a/mcp-wrapper/src/main.el b/mcp-wrapper/src/main.el index bfb98b6..5c8c46a 100644 --- a/mcp-wrapper/src/main.el +++ b/mcp-wrapper/src/main.el @@ -91,7 +91,7 @@ tool("beginSession", "Initialize session: surface recent high-importance memorie "," + tool("recall", "Retrieve memories by chain or query.") + "," + tool("inspectMemories", "List recent memory nodes.") + "," + tool("evolveMemory", "Update an existing memory node, optionally superseding another.") + -"," + tool("forget", "Remove a node from memory.") + +"," + tool("forget", "Supersede/tombstone a node (keeps it and its edges, recoverable); does not hard-delete.") + "," + tool("pinNode", "Strengthen a node so it stays salient.") + // ── Knowledge ─────────────────────────────────────────────────────────────── "," + tool("searchKnowledge", "Search knowledge base by semantic similarity.") + @@ -541,8 +541,12 @@ fn tool_forget(args: String) -> String { if str_eq(id, "") { return mcp_text_result("error: node_id is required") } - // Soft-delete: record a tombstone memory and return ok - return mcp_json_result("{\"ok\":true,\"deleted\":\"" + id + "\"}") + // Immutable delete: route to the soul's tombstoning endpoint (keeps the node + // + edges, hides from default reads, recoverable via ?include_deleted). + // Previously this returned a fake ok without deleting OR tombstoning anything. + let body: String = "{\"id\":\"" + id + "\"}" + let resp: String = http_post_json(neuron_url() + "/memory/delete", body) + return mcp_json_result(resp) } fn tool_check_events(args: String) -> String { diff --git a/memory.el b/memory.el index bc14505..46d9f28 100644 --- a/memory.el +++ b/memory.el @@ -43,8 +43,32 @@ fn mem_strengthen(node_id: String) -> Void { engram_strengthen(node_id) } +// mem_tombstone — immutable "delete": KEEP the node and all its edges; record a +// Tombstone marker (content = target id, label "tombstone:", wired with a +// "tombstones" edge). Never engram_forget. Default bounded list reads hide +// tombstoned nodes; ?include_deleted=1 recovers them. This is the ONE canonical +// tombstone helper — every forget path routes through it. Defined here in +// memory.el (imported first) so awareness.el and neuron-api.el can both call it. +fn mem_tombstone(node_id: String) -> String { + let tags: String = "[\"Tombstone\",\"status:deleted\"]" + let marker: String = engram_node_full( + node_id, "Tombstone", "tombstone:" + node_id, + el_from_float(0.01), el_from_float(0.01), el_from_float(1.0), + "Episodic", tags) + if !str_eq(marker, "") { + engram_connect(marker, node_id, el_from_float(1.0), "tombstones") + } + return marker +} + +// mem_forget — NOTE: no longer a hard delete. Engram nodes are immutable, so +// this now TOMBSTONES (via mem_tombstone): the node and its edges are kept and +// stay recoverable. Every caller (the /memory/forget route and the cultivate +// forget op) is non-destructive as a result. Internal GC that genuinely needs +// removal (session-summary replace, telemetry pruning) calls engram_forget +// directly and is unaffected by this. fn mem_forget(node_id: String) -> Void { - engram_forget(node_id) + let _marker: String = mem_tombstone(node_id) } // mem_consolidate — structural scan plus salience-evolution pass. diff --git a/neuron-api.el b/neuron-api.el index 1ad8770..12ce4c5 100644 --- a/neuron-api.el +++ b/neuron-api.el @@ -104,6 +104,66 @@ fn api_not_persisted(id: String) -> String { return "{\"ok\":false,\"error\":\"write_not_persisted\",\"id\":\"" + id + "\"}" } +// ── Immutability: tombstone instead of hard-delete ──────────────────────────── +// +// Day-one rule: engram nodes are immutable. A "delete" must never engram_forget +// (which frees the node and drops its incident edges). Instead we TOMBSTONE: the +// original node and all its edges are KEPT and stay traversable; a small +// Tombstone marker node records the deletion (content = target id, label +// "tombstone:"), wired to the target with a "tombstones" edge. Default +// bounded list reads hide tombstoned nodes (memory_hide_tombstoned); internal +// cognition and explicit ?include_deleted reads still see them. +fn tombstone_node(id: String) -> String { + // Delegates to the canonical helper in memory.el (single source of truth). + return mem_tombstone(id) +} + +// tombstoned_id_set — delimited "|id1|id2|" of every tombstoned target id. +// Empty string when nothing is tombstoned (callers fast-path on that). +fn tombstoned_id_set() -> String { + let markers: String = engram_scan_nodes_by_type_json("Tombstone", 5000, 0) + if str_eq(markers, "") || str_eq(markers, "[]") { return "" } + let n: Int = json_array_len(markers) + let acc: String = "|" + let i: Int = 0 + while i < n { + let m: String = json_array_get(markers, i) + let tid: String = json_get(m, "content") + let acc = if str_eq(tid, "") { acc } else { acc + tid + "|" } + let i = i + 1 + } + return acc +} + +// memory_hide_tombstoned — drop tombstone markers and tombstoned nodes from a +// scanned node array. BOUNDED use only (typed/paginated lists), NOT the full +// graph scan: json_array_get is O(index), so a full pass is O(n^2). Safe for the +// ~50-item memory list; a hard cap protects against a large limit. The full +// /api/graph/nodes hide needs a runtime scan filter and is deferred (see PR). +// ?include_deleted bypasses the filter (explicit traversal). +fn memory_hide_tombstoned(raw: String, path: String) -> String { + if str_contains(path, "include_deleted") { return raw } + if str_eq(raw, "") || str_eq(raw, "[]") { return raw } + let dead: String = tombstoned_id_set() + if str_eq(dead, "") { return raw } + let n: Int = json_array_len(raw) + if n > 1000 { return raw } + let out: String = "[" + let first: Bool = true + let i: Int = 0 + while i < n { + let node: String = json_array_get(raw, i) + let nid: String = json_get(node, "id") + let ntype: String = json_get(node, "node_type") + let is_dead: Bool = !str_eq(nid, "") && str_contains(dead, "|" + nid + "|") + let keep: Bool = !str_eq(ntype, "Tombstone") && !is_dead + let out = if keep { if first { out + node } else { out + "," + node } } else { out } + let first = if keep { false } else { first } + let i = i + 1 + } + return out + "]" +} + // ── Session ─────────────────────────────────────────────────────────────────── // handle_api_begin_session — full context bootstrap. @@ -191,25 +251,26 @@ fn handle_api_node_create(body: String) -> String { return "{\"id\":\"" + id + "\",\"ok\":true}" } -// handle_api_node_delete — remove a node by id (engram_forget) and verify it is gone. +// handle_api_node_delete — TOMBSTONE a node by id (immutable delete). // Backs /api/neuron/node/delete and the /api/neuron/memory/delete alias the UI calls. +// The node and all its incident edges are KEPT; a Tombstone marker records the +// deletion. Never engram_forget — engram nodes are immutable by design. fn handle_api_node_delete(body: String) -> String { let id: String = json_get(body, "id") if str_eq(id, "") { return api_err("id is required") } - // engram_forget removes the node + its incident edges from the live graph. - // Delete is NOT read-back-verified: engram_get_node_json can return a stale hit - // for a just-forgotten id because the id→index map is not rebuilt on forget. - // A stale hit would cause a false "delete_failed" on a successful deletion. - // This exception is correct: read-back-verify guards WRITES; for deletes, - // the graph endpoints (/api/graph/nodes) reflect the removal and are the source of truth. - engram_forget(id) - return "{\"ok\":true,\"id\":\"" + id + "\"}" + if is_protected_node(id) { return api_err_protected(id) } + let existing: String = engram_get_node_json(id) + if str_eq(existing, "{}") { return api_err("node not found: " + id) } + let marker: String = tombstone_node(id) + if str_eq(marker, "") { return api_err("tombstone failed: " + id) } + return "{\"ok\":true,\"id\":\"" + id + "\",\"tombstoned\":true}" } // handle_api_node_update — update a node's content/fields. There is no in-place -// engram update builtin, so this recreates the node with merged fields and then -// forgets the old one (only after the new node reads back). The id changes; the -// response returns the new id and the replaced id so callers can re-point. +// engram update builtin, so this creates a new node with merged fields and wires +// a "supersedes" edge new->old. The original is KEPT (immutable); the id changes, +// and the response returns the new id and the superseded id so callers re-point. +// Mirrors handle_api_memory_update / evolve exactly. Never engram_forget. fn handle_api_node_update(body: String) -> String { let id: String = json_get(body, "id") if str_eq(id, "") { return api_err("id is required") } @@ -240,8 +301,8 @@ fn handle_api_node_update(body: String) -> String { el_from_float(0.5), el_from_float(0.5), el_from_float(0.8), tier, tags) if !api_persisted(new_id) { return api_not_persisted(new_id) } - engram_forget(id) - return "{\"id\":\"" + new_id + "\",\"replaced\":\"" + id + "\",\"ok\":true}" + engram_connect(new_id, id, el_from_float(0.9), "supersedes") + return "{\"id\":\"" + new_id + "\",\"supersedes\":\"" + id + "\",\"ok\":true}" } // handle_api_recall — search or activate memory by query. @@ -504,13 +565,15 @@ fn handle_api_link_entities(body: String) -> String { return "{\"ok\":true,\"from_id\":\"" + from_id + "\",\"to_id\":\"" + to_id + "\",\"relation\":\"" + eff_relation + "\"}" } -// handle_api_forget — delete a node by ID. Blocked for protected identity nodes. +// handle_api_forget — TOMBSTONE a node by ID (immutable; mem_forget now +// tombstones). The node + edges are kept and recoverable. Blocked for protected +// identity nodes. fn handle_api_forget(body: String) -> String { let node_id: String = json_get(body, "id") if str_eq(node_id, "") { return api_err("id is required") } if is_protected_node(node_id) { return api_err_protected(node_id) } mem_forget(node_id) - return "{\"ok\":true,\"id\":\"" + node_id + "\"}" + return "{\"ok\":true,\"id\":\"" + node_id + "\",\"tombstoned\":true}" } // handle_api_evolve_memory — evolve a Memory node. Blocked for protected identity nodes. @@ -541,10 +604,10 @@ fn handle_api_evolve_memory(body: String) -> String { } // handle_api_memory_delete — POST /api/neuron/memory/delete {"id":"..."}. -// Hard delete: engram_forget (via mem_forget) removes the node and all -// incident edges from the engram store, so no soft-delete fallback is -// needed. Existence is checked first because engram_forget silently -// no-ops on unknown ids — a bad id must return an error, not fake success. +// Immutable delete: TOMBSTONE via tombstone_node — the node and all its incident +// edges are KEPT and stay traversable; a Tombstone marker records the deletion +// and default bounded list reads hide it. Never engram_forget. Existence is +// checked first so a bad id errors rather than faking success. // Blocked for protected identity nodes, same as /memory/forget. fn handle_api_memory_delete(body: String) -> String { let node_id: String = json_get(body, "id") @@ -552,8 +615,10 @@ fn handle_api_memory_delete(body: String) -> String { if is_protected_node(node_id) { return api_err_protected(node_id) } let existing: String = engram_get_node_json(node_id) if str_eq(existing, "{}") { return api_err("memory not found: " + node_id) } - mem_forget(node_id) - return "{\"ok\":true,\"id\":\"" + node_id + "\",\"deleted\":true}" + // Immutable delete: tombstone, never mem_forget/engram_forget. Node + edges KEPT. + let marker: String = tombstone_node(node_id) + if str_eq(marker, "") { return api_err("tombstone failed: " + node_id) } + return "{\"ok\":true,\"id\":\"" + node_id + "\",\"tombstoned\":true}" } // handle_api_memory_update — POST /api/neuron/memory/update {"id","content"}. @@ -623,8 +688,9 @@ fn handle_api_cultivate(body: String) -> String { if str_eq(op, "forget") { let node_id: String = json_get(body, "id") if str_eq(node_id, "") { return api_err("id is required") } + // Immutable: mem_forget now tombstones (keep node + edges), never hard-delete. mem_forget(node_id) - return "{\"ok\":true,\"id\":\"" + node_id + "\",\"cultivated\":true}" + return "{\"ok\":true,\"id\":\"" + node_id + "\",\"tombstoned\":true,\"cultivated\":true}" } if str_eq(op, "link_entities") { @@ -646,7 +712,10 @@ fn handle_api_cultivate(body: String) -> String { // handle_api_list_typed — list nodes by node_type. fn handle_api_list_typed(node_type: String, path: String, body: String) -> String { let limit: Int = api_query_int(path, "limit", 50) - return api_or_empty(engram_scan_nodes_by_type_json(node_type, limit, 0)) + let raw: String = api_or_empty(engram_scan_nodes_by_type_json(node_type, limit, 0)) + // Hide tombstoned nodes from the default (bounded) memory list. + // ?include_deleted=1 returns them for explicit traversal. + return memory_hide_tombstoned(raw, path) } // ── Consolidate ─────────────────────────────────────────────────────────────── diff --git a/scripts/verify-soul-contract.sh b/scripts/verify-soul-contract.sh new file mode 100755 index 0000000..c120920 --- /dev/null +++ b/scripts/verify-soul-contract.sh @@ -0,0 +1,226 @@ +#!/usr/bin/env bash +# verify-soul-contract.sh — the soul contract gate. +# +# TERMINOLOGY (canonical): the ENGRAM is the brain — the memory/knowledge-graph +# substrate. The binary this gate exercises is the SOUL — the runtime/reasoning +# engine compiled from dist/soul.c that serves the /api/ surface. The app +# (neuron-ui) bundles the soul binary at resources//neuron. +# +# WHY THIS EXISTS +# For a while the soul binary was hand-dropped, and a stale one shipped: it +# 404'd several capability routes the app calls (knowledge-graph node +# update/delete, live-run narration, safety-contact, ...). This gate makes +# shipping a stale soul IMPOSSIBLE. It has two enforced sections: +# A. PRESENCE — every route the app calls must be ANSWERED (not 404, not +# the el-runtime "no handler"). This is the packaging gate: +# if it fails, do not package. +# B. IMMUTABILITY — engram nodes/memories are immutable by design. To +# "update" is to create a NEW node + a supersede EDGE back to +# the original; the original is KEPT. To "delete" is to +# supersede/tombstone, never hard-remove. A soul that +# hard-deletes an engram node is DEFECTIVE and fails the gate. +# +# SAFETY +# Never touches the live soul (:7770), live engram (:8742), or ~/.neuron. +# Boots on a throwaway port (default 7799) with HOME=$(mktemp -d), a throwaway +# engram snapshot, a non-genesis cgi id, no ENGRAM_URL (so it uses its own +# in-process store, never the live server), NEURON_API_URL pointed at a dead +# port, and no ANTHROPIC_API_KEY (so no probe triggers a real LLM call). +# Connectors proxy to a HARDCODED 127.0.0.1:7771 (no env override): those +# sub-routes are probed with GET, which the soul maps to a read-only +# connectd_get, so this gate never writes to a running connectd bridge. +# +# USAGE +# scripts/verify-soul-contract.sh [port] +# exit 0 = all required routes answered AND no destructive engram mutation; +# non-zero = a route is missing (presence) or a mutation route hard-deletes. +set -uo pipefail + +SOUL="${1:?usage: verify-soul-contract.sh [port]}" +PORT="${2:-7799}" + +if [ "$PORT" = "7770" ] || [ "$PORT" = "8742" ] || [ "$PORT" = "7771" ]; then + echo "REFUSING: port $PORT is a live service port. Use a throwaway port." >&2 + exit 2 +fi +if [ ! -x "$SOUL" ]; then echo "not executable: $SOUL" >&2; exit 2; fi + +BASE="http://127.0.0.1:$PORT" +THROW_HOME="$(mktemp -d "${TMPDIR:-/tmp}/soul-contract-home.XXXXXX")" +SOUL_LOG="$(mktemp "${TMPDIR:-/tmp}/soul-contract-log.XXXXXX")" +SOUL_PID="" +cleanup() { + [ -n "$SOUL_PID" ] && kill "$SOUL_PID" 2>/dev/null + [ -n "$SOUL_PID" ] && { sleep 0.3; kill -9 "$SOUL_PID" 2>/dev/null; } + rm -rf "$THROW_HOME" "$SOUL_LOG" +} +trap cleanup EXIT INT TERM + +# ============================================================================= +# THE CONTRACT — routes the app (neuron-ui/src/main/kotlin/ai/neuron/ui/*.kt) +# calls against the soul ($SOUL). Format: "METHOD PATH". +# +# EXCLUDED and why: +# /api/auth, /api/auth/status, /api/dispatch, /api/tasks +# -> served by the APP's own DispatchServer.kt (localhost:8080), not the +# soul. Not soul routes. +# /api/tags +# -> not handled by any soul .el (app-side/other). Pre-verified excluded. +# /api/neuron/, /api/neuron/node/, /api/connectors/ (bare prefixes) +# -> base-path string constants used to build the concrete routes below. +# +# KNOWN-PENDING (probed + reported, NON-blocking): +# POST /api/engram/import -> the app's "Restore memory" path. No soul handler +# yet, and the app hides Restore from shipped builds (B3, "lands in an +# update"). Reported so we see it; does not block packaging. +# +# Connectors sub-routes are listed as GET (see SAFETY note): handle_connectors is +# monolithic, so a GET reaching it proves the whole connectors surface without +# writing to the live bridge. Binary-strings cross-check confirms each POST +# sub-path literal is compiled in. +# ============================================================================= +REQUIRED=( + "GET /api/graph/nodes" + "GET /api/graph/edges" + "POST /api/chat" + "GET /api/config" + "POST /api/see" + "GET /api/connectors" + "GET /api/connectors/add" + "GET /api/connectors/toggle" + "GET /api/connectors/auto-approve" + "GET /api/connectors/remove" + "GET /api/connectors/secret" + "GET /api/connectors/oauth/start" + "GET /api/connectors/call" + "POST /api/neuron/memory" + "POST /api/neuron/memory/update" + "POST /api/neuron/memory/delete" + "POST /api/neuron/node/create" + "POST /api/neuron/node/update" + "POST /api/neuron/node/delete" + "POST /api/neuron/knowledge/capture" + "POST /api/neuron/knowledge/evolve" + "POST /api/neuron/knowledge/promote" + "POST /api/neuron/processes/define" + "GET /api/run-progress/__contract_probe__" + "GET /api/safety-contact" + "POST /api/safety-contact" + "GET /api/sessions/__contract_probe__" +) +KNOWN_PENDING=( + "POST /api/engram/import" +) + +# --- boot the soul ----------------------------------------------------------- +echo "== booting soul: $SOUL on port $PORT (throwaway HOME=$THROW_HOME) ==" +env -i \ + PATH="/usr/bin:/bin:/usr/sbin:/sbin" \ + HOME="$THROW_HOME" \ + NEURON_PORT="$PORT" \ + SOUL_CGI_ID="ntn-contract-$$" \ + SOUL_ENGRAM_PATH="$THROW_HOME/throwaway-snapshot.json" \ + NEURON_API_URL="http://127.0.0.1:9" \ + SOUL_TICK_MS="3600000" SOUL_HEARTBEAT_MS="3600000" SOUL_REFRESH_MS="3600000" \ + "$SOUL" >"$SOUL_LOG" 2>&1 & +SOUL_PID=$! + +UP=0 +for _ in $(seq 1 60); do + if ! kill -0 "$SOUL_PID" 2>/dev/null; then + echo "!! soul exited during boot. log tail:" >&2; tail -20 "$SOUL_LOG" >&2; exit 3 + fi + RSS=$(ps -o rss= -p "$SOUL_PID" 2>/dev/null | tr -d ' ') + if [ -n "$RSS" ] && [ "$RSS" -gt $((3*1024*1024)) ]; then + echo "!! soul RSS >3GB — kill -9" >&2; kill -9 "$SOUL_PID" 2>/dev/null; exit 3 + fi + [ "$(curl -s -o /dev/null -w '%{http_code}' -m 2 "$BASE/health" 2>/dev/null)" = "200" ] && { UP=1; break; } + sleep 0.5 +done +[ "$UP" = 1 ] || { echo "!! soul never healthy on $BASE/health" >&2; tail -20 "$SOUL_LOG" >&2; exit 3; } +echo "== soul healthy =="; echo + +# --- probing helpers --------------------------------------------------------- +# request METHOD PATH [BODY] -> prints response body (single line) +request() { + curl -s -m 12 -X "$1" -H 'Content-Type: application/json' --data "${3:-{}}" "$BASE$2" 2>/dev/null | tr -d '\n' +} +# is_missing BODY -> 0 if the body is a "route not present" signal +is_missing() { + printf '%s' "$1" | grep -qE '"error":"not found"|"code":"not_found"|no http handler registered|"code":"method_not_allowed"' +} +extract_id() { printf '%s' "$1" | grep -oE '"id":"[^"]+"' | head -1 | sed 's/.*"id":"//;s/"//'; } +node_present() { # id -> 0 if id appears in /api/graph/nodes + request GET /api/graph/nodes | grep -qF "\"$1\"" +} + +# --- SECTION A: presence ----------------------------------------------------- +run_presence() { + local -n arr=$1; local fail=0 + printf ' %-8s %-42s %s\n' "METHOD" "ROUTE" "RESULT" + for e in "${arr[@]}"; do + local m p body; m=$(awk '{print $1}' <<<"$e"); p=$(awk '{print $2}' <<<"$e") + body=$(request "$m" "$p") + if is_missing "$body"; then + printf ' %-8s %-42s MISSING %s\n' "$m" "$p" "$(cut -c1-46 <<<"$body")"; fail=$((fail+1)) + else + printf ' %-8s %-42s ANSWERED %s\n' "$m" "$p" "$(cut -c1-46 <<<"$body")" + fi + done + return $fail +} + +echo "== SECTION A: PRESENCE (required, blocking) ==" +run_presence REQUIRED; A_FAIL=$? +echo +echo "== KNOWN-PENDING (non-blocking) ==" +run_presence KNOWN_PENDING; P_FAIL=$? +echo + +# --- SECTION B: immutability (engram write routes must supersede, not destroy) -- +# For each mutation route: create a node, mutate it, then check the ORIGINAL id +# still exists in the graph. KEPT = supersede/tombstone (correct). DESTROYED = +# hard delete (DEFECTIVE -> fail). N/A = mutate route absent (a presence failure). +# For "delete" mutations we additionally require a real tombstone marker +# (label "tombstone:") so a no-op delete cannot false-pass as KEPT. +marker_present() { # id -> 0 if a "tombstone:" marker exists (include_deleted view) + request GET "/api/graph/nodes?include_deleted=1" | grep -qF "tombstone:$1" +} +immut_check() { # label KIND(update|delete) CREATE_PATH MUTATE_PATH + local label="$1" kind="$2" create="$3" mutate="$4" + local cbody id mb mbody + cbody=$(request POST "$create" "{\"content\":\"__immut_${label}__\",\"node_type\":\"Memory\",\"label\":\"contract:immut\"}") + id=$(extract_id "$cbody") + if [ -z "$id" ]; then printf ' %-14s SKELETON-FAIL create returned no id: %s\n' "$label" "$(cut -c1-40 <<<"$cbody")"; return 2; fi + mb="{\"id\":\"$id\"}"; [ "$kind" = update ] && mb="{\"id\":\"$id\",\"content\":\"__immut_${label}_v2__\"}" + mbody=$(request POST "$mutate" "$mb") + if is_missing "$mbody"; then printf ' %-14s N/A mutate route absent (see Section A)\n' "$label"; return 0; fi + if ! node_present "$id"; then + printf ' %-14s DESTROYED original %s hard-removed <== DEFECTIVE\n' "$label" "$id"; return 1 + fi + if [ "$kind" = delete ] && ! marker_present "$id"; then + printf ' %-14s NO-OP original %s kept but no tombstone marker <== DEFECTIVE\n' "$label" "$id"; return 1 + fi + local how="supersede edge"; [ "$kind" = delete ] && how="tombstoned + hidden from default list" + printf ' %-14s KEPT original %s survived (%s)\n' "$label" "$id" "$how"; return 0 +} + +echo "== SECTION B: IMMUTABILITY (engram nodes must be superseded, never destroyed) ==" +B_FAIL=0 +immut_check "memory-update" update /api/neuron/memory /api/neuron/memory/update || B_FAIL=$((B_FAIL+$?)) +immut_check "memory-delete" delete /api/neuron/memory /api/neuron/memory/delete || B_FAIL=$((B_FAIL+$?)) +immut_check "node-update" update /api/neuron/node/create /api/neuron/node/update || B_FAIL=$((B_FAIL+$?)) +immut_check "node-delete" delete /api/neuron/node/create /api/neuron/node/delete || B_FAIL=$((B_FAIL+$?)) +immut_check "memory-forget" delete /api/neuron/memory /api/neuron/memory/forget || B_FAIL=$((B_FAIL+$?)) +echo + +echo "============================================================" +RC=0 +if [ "$A_FAIL" -gt 0 ]; then echo "PRESENCE: FAIL — $A_FAIL required route(s) unanswered. Do NOT package."; RC=1 + else echo "PRESENCE: PASS — all ${#REQUIRED[@]} required routes answered."; fi +if [ "$B_FAIL" -gt 0 ]; then echo "IMMUTABILITY: FAIL — $B_FAIL engram write route(s) hard-delete. DEFECTIVE soul."; RC=1 + else echo "IMMUTABILITY: PASS — no engram write route hard-deletes."; fi +[ "$P_FAIL" -gt 0 ] && echo "note: $P_FAIL known-pending route(s) unanswered (expected; non-blocking)." +echo "============================================================" +[ "$RC" = 0 ] && echo "GATE: PASS" || echo "GATE: FAIL" +exit $RC