self-review 2026-06-04: wire wm_avg_weight into heartbeat ISE

Calls engram_wm_avg_weight() (new builtin) in emit_heartbeat() and appends
wm_avg_weight field to the heartbeat JSON payload. This makes activation
quality visible in the ISE stream — a heartbeat showing wm_active=2000 and
wm_avg_weight=0.075 reveals the sparse-graph problem directly (many nodes
barely clearing the threshold), vs wm_avg_weight=0.4+ which would indicate
dense, high-confidence activations.

Rebuilt dist/neuron from soul.el (which imports awareness.el). Build uses
single self-contained dist/neuron.c to avoid duplicate-symbol linker errors
from the dist/ directory containing stale soul_new.c / soul-rebuilt.c files.
This commit is contained in:
2026-06-04 08:38:39 -05:00
parent e92fd2d5a4
commit 2f16855d6b
4 changed files with 160 additions and 129 deletions
+6 -1
View File
@@ -85,10 +85,15 @@ fn emit_heartbeat() -> Void {
let nc: Int = engram_node_count()
let ec: Int = engram_edge_count()
let wmc: Int = engram_wm_count()
// avg_wm_weight: mean working_memory_weight of promoted nodes.
// Distinguishes "many weak activations" (sparse graph) from "few strong" (dense).
// Returns float bits; use float_to_str to embed in JSON. (2026-06-04)
let wm_avg_bits: Float = engram_wm_avg_weight()
let wm_avg_str: String = float_to_str(wm_avg_bits)
let up_ms: Int = elapsed_ms()
let up_human: String = elapsed_human()
let emb_ok: Int = embed_ok()
let payload: String = "{\"event\":\"heartbeat\",\"pulse\":" + pulse + ",\"boot\":" + boot + ",\"idle\":" + idle + ",\"node_count\":" + int_to_str(nc) + ",\"edge_count\":" + int_to_str(ec) + ",\"wm_active\":" + int_to_str(wmc) + ",\"ts\":" + int_to_str(ts) + ",\"uptime_ms\":" + int_to_str(up_ms) + ",\"uptime\":\"" + up_human + "\",\"embed_ok\":" + int_to_str(emb_ok) + "}"
let payload: String = "{\"event\":\"heartbeat\",\"pulse\":" + pulse + ",\"boot\":" + boot + ",\"idle\":" + idle + ",\"node_count\":" + int_to_str(nc) + ",\"edge_count\":" + int_to_str(ec) + ",\"wm_active\":" + int_to_str(wmc) + ",\"wm_avg_weight\":" + wm_avg_str + ",\"ts\":" + int_to_str(ts) + ",\"uptime_ms\":" + int_to_str(up_ms) + ",\"uptime\":\"" + up_human + "\",\"embed_ok\":" + int_to_str(emb_ok) + "}"
ise_post(payload)
}
+3 -1
View File
@@ -230,10 +230,12 @@ el_val_t emit_heartbeat(void) {
el_val_t nc = engram_node_count();
el_val_t ec = engram_edge_count();
el_val_t wmc = engram_wm_count();
el_val_t wm_avg_bits = engram_wm_avg_weight();
el_val_t wm_avg_str = float_to_str(wm_avg_bits);
el_val_t up_ms = elapsed_ms();
el_val_t up_human = elapsed_human();
el_val_t emb_ok = embed_ok();
el_val_t payload = 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_concat(EL_STR("{\"event\":\"heartbeat\",\"pulse\":"), pulse), EL_STR(",\"boot\":")), boot), EL_STR(",\"idle\":")), idle), EL_STR(",\"node_count\":")), int_to_str(nc)), EL_STR(",\"edge_count\":")), int_to_str(ec)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR(",\"uptime_ms\":")), int_to_str(up_ms)), EL_STR(",\"uptime\":\"")), up_human), EL_STR("\",\"embed_ok\":")), int_to_str(emb_ok)), EL_STR("}"));
el_val_t payload = 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_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"heartbeat\",\"pulse\":"), pulse), EL_STR(",\"boot\":")), boot), EL_STR(",\"idle\":")), idle), EL_STR(",\"node_count\":")), int_to_str(nc)), EL_STR(",\"edge_count\":")), int_to_str(ec)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"wm_avg_weight\":")), wm_avg_str), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR(",\"uptime_ms\":")), int_to_str(up_ms)), EL_STR(",\"uptime\":\"")), up_human), EL_STR("\",\"embed_ok\":")), int_to_str(emb_ok)), EL_STR("}"));
ise_post(payload);
return 0;
}
Vendored
BIN
View File
Binary file not shown.
+151 -127
View File
@@ -1068,12 +1068,12 @@ 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 handle_api_consolidate(el_val_t body);
el_val_t session_title_from_message(el_val_t message);
el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at);
el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at, el_val_t folder);
el_val_t session_create(el_val_t body);
el_val_t session_list(void);
el_val_t session_get(el_val_t session_id);
el_val_t session_delete(el_val_t session_id);
el_val_t session_update_title(el_val_t session_id, el_val_t body);
el_val_t session_update_patch(el_val_t session_id, el_val_t body);
el_val_t session_search(el_val_t query);
el_val_t session_hist_load(el_val_t session_id);
el_val_t session_hist_save(el_val_t session_id, el_val_t hist);
@@ -25353,10 +25353,12 @@ el_val_t emit_heartbeat(void) {
el_val_t nc = engram_node_count();
el_val_t ec = engram_edge_count();
el_val_t wmc = engram_wm_count();
el_val_t wm_avg_bits = engram_wm_avg_weight();
el_val_t wm_avg_str = float_to_str(wm_avg_bits);
el_val_t up_ms = elapsed_ms();
el_val_t up_human = elapsed_human();
el_val_t emb_ok = embed_ok();
el_val_t payload = 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_concat(EL_STR("{\"event\":\"heartbeat\",\"pulse\":"), pulse), EL_STR(",\"boot\":")), boot), EL_STR(",\"idle\":")), idle), EL_STR(",\"node_count\":")), int_to_str(nc)), EL_STR(",\"edge_count\":")), int_to_str(ec)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR(",\"uptime_ms\":")), int_to_str(up_ms)), EL_STR(",\"uptime\":\"")), up_human), EL_STR("\",\"embed_ok\":")), int_to_str(emb_ok)), EL_STR("}"));
el_val_t payload = 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_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"heartbeat\",\"pulse\":"), pulse), EL_STR(",\"boot\":")), boot), EL_STR(",\"idle\":")), idle), EL_STR(",\"node_count\":")), int_to_str(nc)), EL_STR(",\"edge_count\":")), int_to_str(ec)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"wm_avg_weight\":")), wm_avg_str), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR(",\"uptime_ms\":")), int_to_str(up_ms)), EL_STR(",\"uptime\":\"")), up_human), EL_STR("\",\"embed_ok\":")), int_to_str(emb_ok)), EL_STR("}"));
ise_post(payload);
return 0;
}
@@ -27227,9 +27229,10 @@ el_val_t session_title_from_message(el_val_t message) {
return 0;
}
el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at) {
el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at, el_val_t folder) {
el_val_t safe_title = json_safe(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("{\"type\":\"session:meta\""), EL_STR(",\"id\":\"")), id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(created_at)), EL_STR(",\"updated_at\":")), int_to_str(updated_at)), EL_STR("}"));
el_val_t safe_folder = json_safe(folder);
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("{\"type\":\"session:meta\""), EL_STR(",\"id\":\"")), id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"folder\":\"")), safe_folder), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(created_at)), EL_STR(",\"updated_at\":")), int_to_str(updated_at)), EL_STR("}"));
return 0;
}
@@ -27238,7 +27241,8 @@ el_val_t session_create(el_val_t body) {
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_247 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_247 = (EL_STR("New conversation")); } else { _if_result_247 = (title_req); } _if_result_247; });
el_val_t content = session_make_content(id, title, ts, ts);
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\"]");
el_val_t node_id = engram_node_full(content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
if (str_eq(node_id, EL_STR(""))) {
@@ -27246,10 +27250,10 @@ el_val_t session_create(el_val_t body) {
}
state_set(el_str_concat(EL_STR("session_node_"), id), node_id);
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("{\"id\":\""), id), EL_STR("\",\"title\":\"")), json_safe(title)), EL_STR("\",\"created_at\":")), int_to_str(ts)), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR(",\"last_message\":\"\"}"));
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_248 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_248 = (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_248 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_248; });
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("{\"id\":\""), id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(title)), EL_STR("\"")), EL_STR(",\"node_id\":\"")), node_id), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(ts)), EL_STR("}"));
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;
}
@@ -27278,11 +27282,12 @@ el_val_t session_list(void) {
el_val_t eff_id = ({ el_val_t _if_result_249 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_249 = (json_get(node, EL_STR("id"))); } else { _if_result_249 = (sess_id); } _if_result_249; });
el_val_t title_inner = json_get(content, EL_STR("title"));
el_val_t eff_title = ({ el_val_t _if_result_250 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_250 = (EL_STR("New conversation")); } else { _if_result_250 = (title_inner); } _if_result_250; });
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_251 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_251 = (EL_STR("0")); } else { _if_result_251 = (created_inner); } _if_result_251; });
el_val_t eff_updated = ({ el_val_t _if_result_252 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_252 = (eff_created); } else { _if_result_252 = (updated_inner); } _if_result_252; });
el_val_t entry = ({ el_val_t _if_result_253 = 0; if (is_session) { _if_result_253 = (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(",\"last_message\":\"\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_253 = (EL_STR("")); } _if_result_253; });
el_val_t entry = ({ el_val_t _if_result_253 = 0; if (is_session) { _if_result_253 = (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_253 = (EL_STR("")); } _if_result_253; });
out = ({ el_val_t _if_result_254 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_254 = (({ el_val_t _if_result_255 = 0; if (str_eq(out, EL_STR(""))) { _if_result_255 = (entry); } else { _if_result_255 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_255; })); } else { _if_result_254 = (out); } _if_result_254; });
i = (i + 1);
}
@@ -27297,6 +27302,7 @@ el_val_t session_get(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 meta_content = EL_STR("");
el_val_t meta_title = EL_STR("New conversation");
el_val_t meta_folder = EL_STR("");
el_val_t meta_created = EL_STR("0");
el_val_t meta_updated = EL_STR("0");
el_val_t found = 0;
@@ -27310,16 +27316,17 @@ el_val_t session_get(el_val_t session_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_257 = 0; if (is_match) { _if_result_257 = (1); } else { _if_result_257 = (found); } _if_result_257; });
meta_title = ({ el_val_t _if_result_258 = 0; if (is_match) { _if_result_258 = (json_get(content, EL_STR("title"))); } else { _if_result_258 = (meta_title); } _if_result_258; });
meta_folder = ({ el_val_t _if_result_259 = 0; if (is_match) { _if_result_259 = (json_get(content, EL_STR("folder"))); } else { _if_result_259 = (meta_folder); } _if_result_259; });
el_val_t meta_created_raw = json_get(content, EL_STR("created_at"));
meta_created = ({ el_val_t _if_result_259 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_259 = (meta_created_raw); } else { _if_result_259 = (meta_created); } _if_result_259; });
meta_created = ({ el_val_t _if_result_260 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_260 = (meta_created_raw); } else { _if_result_260 = (meta_created); } _if_result_260; });
el_val_t meta_updated_raw = json_get(content, EL_STR("updated_at"));
meta_updated = ({ el_val_t _if_result_260 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_260 = (meta_updated_raw); } else { _if_result_260 = (meta_updated); } _if_result_260; });
meta_updated = ({ el_val_t _if_result_261 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_261 = (meta_updated_raw); } else { _if_result_261 = (meta_updated); } _if_result_261; });
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_261 = 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_261 = (({ el_val_t _if_result_262 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_262 = (EL_STR("[]")); } else { _if_result_262 = (({ el_val_t _if_result_263 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_263 = (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_263 = (({ el_val_t _if_result_264 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_264 = (h_content); } else { _if_result_264 = (EL_STR("[]")); } _if_result_264; })); } _if_result_263; })); } _if_result_262; })); } else { _if_result_261 = (state_hist); } _if_result_261; });
el_val_t hist_raw = ({ el_val_t _if_result_262 = 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_262 = (({ el_val_t _if_result_263 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_263 = (EL_STR("[]")); } else { _if_result_263 = (({ el_val_t _if_result_264 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_264 = (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_264 = (({ el_val_t _if_result_265 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_265 = (h_content); } else { _if_result_265 = (EL_STR("[]")); } _if_result_265; })); } _if_result_264; })); } _if_result_263; })); } else { _if_result_262 = (state_hist); } _if_result_262; });
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("{\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"created_at\":")), meta_created), EL_STR(",\"updated_at\":")), meta_updated), EL_STR(",\"messages\":")), hist_raw), EL_STR("}"));
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;
}
@@ -27328,7 +27335,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_265 = 0; if (str_eq(results, EL_STR(""))) { _if_result_265 = (0); } else { _if_result_265 = (json_array_len(results)); } _if_result_265; });
el_val_t total = ({ el_val_t _if_result_266 = 0; if (str_eq(results, EL_STR(""))) { _if_result_266 = (0); } else { _if_result_266 = (json_array_len(results)); } _if_result_266; });
el_val_t deleted_meta = 0;
el_val_t i = 0;
while (i < total) {
@@ -27338,11 +27345,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_266 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_266 = ((deleted_meta + 1)); } else { _if_result_266 = (deleted_meta); } _if_result_266; });
deleted_meta = ({ el_val_t _if_result_267 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_267 = ((deleted_meta + 1)); } else { _if_result_267 = (deleted_meta); } _if_result_267; });
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_267 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_267 = (0); } else { _if_result_267 = (json_array_len(msg_results)); } _if_result_267; });
el_val_t m_total = ({ el_val_t _if_result_268 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_268 = (0); } else { _if_result_268 = (json_array_len(msg_results)); } _if_result_268; });
el_val_t deleted_msgs = 0;
el_val_t j = 0;
while (j < m_total) {
@@ -27350,7 +27357,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_268 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_268 = ((deleted_msgs + 1)); } else { _if_result_268 = (deleted_msgs); } _if_result_268; });
deleted_msgs = ({ el_val_t _if_result_269 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_269 = ((deleted_msgs + 1)); } else { _if_result_269 = (deleted_msgs); } _if_result_269; });
j = (j + 1);
}
state_set(el_str_concat(EL_STR("session_hist_"), session_id), EL_STR(""));
@@ -27359,17 +27366,20 @@ el_val_t session_delete(el_val_t session_id) {
return 0;
}
el_val_t session_update_title(el_val_t session_id, el_val_t body) {
el_val_t session_update_patch(el_val_t session_id, el_val_t body) {
if (str_eq(session_id, EL_STR(""))) {
return EL_STR("{\"error\":\"session_id is required\"}");
}
el_val_t new_title = json_get(body, EL_STR("title"));
if (str_eq(new_title, EL_STR(""))) {
return EL_STR("{\"error\":\"title is required\"}");
el_val_t has_title = str_contains(body, EL_STR("\"title\""));
el_val_t has_folder = str_contains(body, EL_STR("\"folder\""));
if (!has_title && !has_folder) {
return EL_STR("{\"error\":\"title or folder required in body\"}");
}
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_269 = 0; if (str_eq(results, EL_STR(""))) { _if_result_269 = (0); } else { _if_result_269 = (json_array_len(results)); } _if_result_269; });
el_val_t results = engram_search_json(EL_STR("session:meta"), 50);
el_val_t total = ({ el_val_t _if_result_270 = 0; if (str_eq(results, EL_STR(""))) { _if_result_270 = (0); } else { _if_result_270 = (json_array_len(results)); } _if_result_270; });
el_val_t found = 0;
el_val_t old_title = EL_STR("New conversation");
el_val_t old_folder = EL_STR("");
el_val_t old_created = EL_STR("0");
el_val_t old_node_id = EL_STR("");
el_val_t i = 0;
@@ -27379,26 +27389,34 @@ el_val_t session_update_title(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_270 = 0; if (is_match) { _if_result_270 = (1); } else { _if_result_270 = (found); } _if_result_270; });
found = ({ el_val_t _if_result_271 = 0; if (is_match) { _if_result_271 = (1); } else { _if_result_271 = (found); } _if_result_271; });
el_val_t title_raw = json_get(content, EL_STR("title"));
old_title = ({ el_val_t _if_result_272 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_272 = (title_raw); } else { _if_result_272 = (old_title); } _if_result_272; });
el_val_t folder_raw = json_get(content, EL_STR("folder"));
old_folder = ({ el_val_t _if_result_273 = 0; if (is_match) { _if_result_273 = (folder_raw); } else { _if_result_273 = (old_folder); } _if_result_273; });
el_val_t created_raw = json_get(content, EL_STR("created_at"));
old_created = ({ el_val_t _if_result_271 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_271 = (created_raw); } else { _if_result_271 = (old_created); } _if_result_271; });
old_created = ({ el_val_t _if_result_274 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_274 = (created_raw); } else { _if_result_274 = (old_created); } _if_result_274; });
el_val_t nid = json_get(node, EL_STR("id"));
old_node_id = ({ el_val_t _if_result_272 = 0; if (is_match) { _if_result_272 = (nid); } else { _if_result_272 = (old_node_id); } _if_result_272; });
old_node_id = ({ el_val_t _if_result_275 = 0; if (is_match) { _if_result_275 = (nid); } else { _if_result_275 = (old_node_id); } _if_result_275; });
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_276 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_276 = (req_title); } else { _if_result_276 = (old_title); } _if_result_276; });
el_val_t eff_folder = ({ el_val_t _if_result_277 = 0; if (has_folder) { _if_result_277 = (json_get(body, EL_STR("folder"))); } else { _if_result_277 = (old_folder); } _if_result_277; });
if (!str_eq(old_node_id, EL_STR(""))) {
engram_forget(old_node_id);
}
el_val_t ts = time_now();
el_val_t created_int = str_to_int(old_created);
el_val_t new_content = session_make_content(session_id, new_title, created_int, ts);
el_val_t new_content = session_make_content(session_id, eff_title, created_int, ts, eff_folder);
el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
el_val_t new_node_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
state_set(el_str_concat(EL_STR("session_node_"), session_id), new_node_id);
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("{\"ok\":true,\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(new_title)), EL_STR("\"")), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR("}"));
state_set(EL_STR("session_index"), EL_STR(""));
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("{\"ok\":true,\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(eff_folder)), EL_STR("\"")), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR("}"));
return 0;
}
@@ -27425,10 +27443,10 @@ el_val_t session_search(el_val_t query) {
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_273 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_273 = (EL_STR("0")); } else { _if_result_273 = (created_raw); } _if_result_273; });
el_val_t eff_updated = ({ el_val_t _if_result_274 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_274 = (eff_created); } else { _if_result_274 = (updated_raw); } _if_result_274; });
el_val_t entry = ({ el_val_t _if_result_275 = 0; if ((is_session && !str_eq(sess_id, EL_STR("")))) { _if_result_275 = (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(sess_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(title)), EL_STR("\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_275 = (EL_STR("")); } _if_result_275; });
out = ({ el_val_t _if_result_276 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_276 = (({ el_val_t _if_result_277 = 0; if (str_eq(out, EL_STR(""))) { _if_result_277 = (entry); } else { _if_result_277 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_277; })); } else { _if_result_276 = (out); } _if_result_276; });
el_val_t eff_created = ({ el_val_t _if_result_278 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_278 = (EL_STR("0")); } else { _if_result_278 = (created_raw); } _if_result_278; });
el_val_t eff_updated = ({ el_val_t _if_result_279 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_279 = (eff_created); } else { _if_result_279 = (updated_raw); } _if_result_279; });
el_val_t entry = ({ el_val_t _if_result_280 = 0; if ((is_session && !str_eq(sess_id, EL_STR("")))) { _if_result_280 = (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(sess_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(title)), EL_STR("\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_280 = (EL_STR("")); } _if_result_280; });
out = ({ el_val_t _if_result_281 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_281 = (({ el_val_t _if_result_282 = 0; if (str_eq(out, EL_STR(""))) { _if_result_282 = (entry); } else { _if_result_282 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_282; })); } else { _if_result_281 = (out); } _if_result_281; });
i = (i + 1);
}
return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]"));
@@ -27463,7 +27481,7 @@ el_val_t session_hist_load(el_val_t session_id) {
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);
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_278 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_278 = (0); } else { _if_result_278 = (json_array_len(old_results)); } _if_result_278; });
el_val_t o_total = ({ el_val_t _if_result_283 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_283 = (0); } else { _if_result_283 = (json_array_len(old_results)); } _if_result_283; });
el_val_t oi = 0;
while (oi < o_total) {
el_val_t node = json_array_get(old_results, oi);
@@ -27480,48 +27498,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 results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10);
el_val_t total = ({ el_val_t _if_result_279 = 0; if (str_eq(results, EL_STR(""))) { _if_result_279 = (0); } else { _if_result_279 = (json_array_len(results)); } _if_result_279; });
el_val_t found = 0;
el_val_t old_title = EL_STR("New conversation");
el_val_t old_created = EL_STR("0");
el_val_t old_node_id = EL_STR("");
el_val_t i = 0;
while (i < total) {
el_val_t node = json_array_get(results, i);
el_val_t label = json_get(node, EL_STR("label"));
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_280 = 0; if (is_match) { _if_result_280 = (1); } else { _if_result_280 = (found); } _if_result_280; });
el_val_t title_raw = json_get(content, EL_STR("title"));
old_title = ({ el_val_t _if_result_281 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_281 = (title_raw); } else { _if_result_281 = (old_title); } _if_result_281; });
el_val_t created_raw = json_get(content, EL_STR("created_at"));
old_created = ({ el_val_t _if_result_282 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_282 = (created_raw); } else { _if_result_282 = (old_created); } _if_result_282; });
el_val_t nid = json_get(node, EL_STR("id"));
old_node_id = ({ el_val_t _if_result_283 = 0; if (is_match) { _if_result_283 = (nid); } else { _if_result_283 = (old_node_id); } _if_result_283; });
i = (i + 1);
}
if (!found) {
return EL_STR("");
}
if (!str_eq(old_node_id, EL_STR(""))) {
engram_forget(old_node_id);
}
el_val_t ts = time_now();
el_val_t created_int = str_to_int(old_created);
el_val_t new_content = session_make_content(session_id, old_title, created_int, ts);
el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
el_val_t new_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
state_set(el_str_concat(EL_STR("session_node_"), session_id), new_id);
return 0;
}
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_284 = 0; if (str_eq(results, EL_STR(""))) { _if_result_284 = (0); } else { _if_result_284 = (json_array_len(results)); } _if_result_284; });
el_val_t found = 0;
el_val_t cur_title = EL_STR("");
el_val_t old_title = EL_STR("New conversation");
el_val_t old_folder = EL_STR("");
el_val_t old_created = EL_STR("0");
el_val_t old_node_id = EL_STR("");
el_val_t i = 0;
@@ -27533,11 +27514,54 @@ el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) {
el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found);
found = ({ el_val_t _if_result_285 = 0; if (is_match) { _if_result_285 = (1); } else { _if_result_285 = (found); } _if_result_285; });
el_val_t title_raw = json_get(content, EL_STR("title"));
cur_title = ({ el_val_t _if_result_286 = 0; if (is_match) { _if_result_286 = (title_raw); } else { _if_result_286 = (cur_title); } _if_result_286; });
old_title = ({ el_val_t _if_result_286 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_286 = (title_raw); } else { _if_result_286 = (old_title); } _if_result_286; });
el_val_t folder_raw = json_get(content, EL_STR("folder"));
old_folder = ({ el_val_t _if_result_287 = 0; if (is_match) { _if_result_287 = (folder_raw); } else { _if_result_287 = (old_folder); } _if_result_287; });
el_val_t created_raw = json_get(content, EL_STR("created_at"));
old_created = ({ el_val_t _if_result_287 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_287 = (created_raw); } else { _if_result_287 = (old_created); } _if_result_287; });
old_created = ({ el_val_t _if_result_288 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_288 = (created_raw); } else { _if_result_288 = (old_created); } _if_result_288; });
el_val_t nid = json_get(node, EL_STR("id"));
old_node_id = ({ el_val_t _if_result_288 = 0; if (is_match) { _if_result_288 = (nid); } else { _if_result_288 = (old_node_id); } _if_result_288; });
old_node_id = ({ el_val_t _if_result_289 = 0; if (is_match) { _if_result_289 = (nid); } else { _if_result_289 = (old_node_id); } _if_result_289; });
i = (i + 1);
}
if (!found) {
return EL_STR("");
}
if (!str_eq(old_node_id, EL_STR(""))) {
engram_forget(old_node_id);
}
el_val_t ts = time_now();
el_val_t created_int = str_to_int(old_created);
el_val_t new_content = session_make_content(session_id, old_title, created_int, ts, old_folder);
el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
el_val_t new_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
state_set(el_str_concat(EL_STR("session_node_"), session_id), new_id);
return 0;
}
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_290 = 0; if (str_eq(results, EL_STR(""))) { _if_result_290 = (0); } else { _if_result_290 = (json_array_len(results)); } _if_result_290; });
el_val_t found = 0;
el_val_t cur_title = EL_STR("");
el_val_t old_folder = EL_STR("");
el_val_t old_created = EL_STR("0");
el_val_t old_node_id = EL_STR("");
el_val_t i = 0;
while (i < total) {
el_val_t node = json_array_get(results, i);
el_val_t label = json_get(node, EL_STR("label"));
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_291 = 0; if (is_match) { _if_result_291 = (1); } else { _if_result_291 = (found); } _if_result_291; });
el_val_t title_raw = json_get(content, EL_STR("title"));
cur_title = ({ el_val_t _if_result_292 = 0; if (is_match) { _if_result_292 = (title_raw); } else { _if_result_292 = (cur_title); } _if_result_292; });
el_val_t folder_raw = json_get(content, EL_STR("folder"));
old_folder = ({ el_val_t _if_result_293 = 0; if (is_match) { _if_result_293 = (folder_raw); } else { _if_result_293 = (old_folder); } _if_result_293; });
el_val_t created_raw = json_get(content, EL_STR("created_at"));
old_created = ({ el_val_t _if_result_294 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_294 = (created_raw); } else { _if_result_294 = (old_created); } _if_result_294; });
el_val_t nid = json_get(node, EL_STR("id"));
old_node_id = ({ el_val_t _if_result_295 = 0; if (is_match) { _if_result_295 = (nid); } else { _if_result_295 = (old_node_id); } _if_result_295; });
i = (i + 1);
}
if (!found) {
@@ -27552,7 +27576,7 @@ el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) {
}
el_val_t ts = time_now();
el_val_t created_int = str_to_int(old_created);
el_val_t new_content = session_make_content(session_id, new_title, created_int, ts);
el_val_t new_content = session_make_content(session_id, new_title, created_int, ts, old_folder);
el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
el_val_t new_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.7)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
state_set(el_str_concat(EL_STR("session_node_"), session_id), new_id);
@@ -27586,10 +27610,10 @@ 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_always = ({ el_val_t _if_result_289 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_290 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_290 = (tool_name); } else { _if_result_290 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_290; }); (void)(state_set(always_key, new_always)); _if_result_289 = (1); } else { _if_result_289 = (0); } _if_result_289; });
el_val_t discard_always = ({ el_val_t _if_result_296 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_297 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_297 = (tool_name); } else { _if_result_297 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_297; }); (void)(state_set(always_key, new_always)); _if_result_296 = (1); } else { _if_result_296 = (0); } _if_result_296; });
state_set(el_str_concat(EL_STR("pending_tool_"), session_id), EL_STR(""));
el_val_t eff_action = ({ el_val_t _if_result_291 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_291 = (EL_STR("allow")); } else { _if_result_291 = (action); } _if_result_291; });
el_val_t tool_result = ({ el_val_t _if_result_292 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_292 = (({ el_val_t _if_result_293 = 0; if ((str_len(raw) > 6000)) { _if_result_293 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_293 = (raw); } _if_result_293; })); } else { _if_result_292 = (json_safe(EL_STR("{\"error\":\"User denied this tool call\"}"))); } _if_result_292; });
el_val_t eff_action = ({ el_val_t _if_result_298 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_298 = (EL_STR("allow")); } else { _if_result_298 = (action); } _if_result_298; });
el_val_t tool_result = ({ el_val_t _if_result_299 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_299 = (({ el_val_t _if_result_300 = 0; if ((str_len(raw) > 6000)) { _if_result_300 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_300 = (raw); } _if_result_300; })); } else { _if_result_299 = (json_safe(EL_STR("{\"error\":\"User denied this tool call\"}"))); } _if_result_299; });
el_val_t tool_msg = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"type\":\"tool_result\",\"tool_use_id\":\""), call_id), EL_STR("\",\"content\":\"")), tool_result), EL_STR("\"}"));
el_val_t inner = str_slice(messages, 1, (str_len(messages) - 1));
el_val_t resumed_messages = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}]"));
@@ -27614,7 +27638,7 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) {
}
el_val_t stop_reason = json_get(raw_resp, EL_STR("stop_reason"));
el_val_t content_arr = json_get_raw(raw_resp, EL_STR("content"));
el_val_t eff_content = ({ el_val_t _if_result_294 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_294 = (EL_STR("[]")); } else { _if_result_294 = (content_arr); } _if_result_294; });
el_val_t eff_content = ({ el_val_t _if_result_301 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_301 = (EL_STR("[]")); } else { _if_result_301 = (content_arr); } _if_result_301; });
el_val_t text_out = EL_STR("");
el_val_t has_tool = 0;
el_val_t next_tool_id = EL_STR("");
@@ -27625,12 +27649,12 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) {
while (ci < c_total) {
el_val_t block = json_array_get(eff_content, ci);
el_val_t btype = json_get(block, EL_STR("type"));
text_out = ({ el_val_t _if_result_295 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_295 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_295 = (text_out); } _if_result_295; });
text_out = ({ el_val_t _if_result_302 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_302 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_302 = (text_out); } _if_result_302; });
el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool);
has_tool = ({ el_val_t _if_result_296 = 0; if (is_new_tool) { _if_result_296 = (1); } else { _if_result_296 = (has_tool); } _if_result_296; });
next_tool_id = ({ el_val_t _if_result_297 = 0; if (is_new_tool) { _if_result_297 = (json_get(block, EL_STR("id"))); } else { _if_result_297 = (next_tool_id); } _if_result_297; });
next_tool_name = ({ el_val_t _if_result_298 = 0; if (is_new_tool) { _if_result_298 = (json_get(block, EL_STR("name"))); } else { _if_result_298 = (next_tool_name); } _if_result_298; });
next_tool_input = ({ el_val_t _if_result_299 = 0; if (is_new_tool) { _if_result_299 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_299 = (next_tool_input); } _if_result_299; });
has_tool = ({ el_val_t _if_result_303 = 0; if (is_new_tool) { _if_result_303 = (1); } else { _if_result_303 = (has_tool); } _if_result_303; });
next_tool_id = ({ el_val_t _if_result_304 = 0; if (is_new_tool) { _if_result_304 = (json_get(block, EL_STR("id"))); } else { _if_result_304 = (next_tool_id); } _if_result_304; });
next_tool_name = ({ el_val_t _if_result_305 = 0; if (is_new_tool) { _if_result_305 = (json_get(block, EL_STR("name"))); } else { _if_result_305 = (next_tool_name); } _if_result_305; });
next_tool_input = ({ el_val_t _if_result_306 = 0; if (is_new_tool) { _if_result_306 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_306 = (next_tool_input); } _if_result_306; });
ci = (ci + 1);
}
el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && has_tool);
@@ -27639,14 +27663,14 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) {
el_val_t is_always = (str_contains(always_list2, next_tool_name) && !str_eq(next_tool_name, EL_STR("")));
el_val_t require_approval = state_get(el_str_concat(EL_STR("session_require_approval_"), session_id));
el_val_t needs_pause = ((is_tool_turn && str_eq(require_approval, EL_STR("true"))) && !is_always);
el_val_t next_tool_result = ({ el_val_t _if_result_300 = 0; if ((is_tool_turn && !needs_pause)) { el_val_t raw2 = dispatch_tool(next_tool_name, next_tool_input); _if_result_300 = (({ el_val_t _if_result_301 = 0; if ((str_len(raw2) > 6000)) { _if_result_301 = (el_str_concat(str_slice(raw2, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_301 = (raw2); } _if_result_301; })); } else { _if_result_300 = (EL_STR("")); } _if_result_300; });
el_val_t next_tool_result = ({ el_val_t _if_result_307 = 0; if ((is_tool_turn && !needs_pause)) { el_val_t raw2 = dispatch_tool(next_tool_name, next_tool_input); _if_result_307 = (({ el_val_t _if_result_308 = 0; if ((str_len(raw2) > 6000)) { _if_result_308 = (el_str_concat(str_slice(raw2, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_308 = (raw2); } _if_result_308; })); } else { _if_result_307 = (EL_STR("")); } _if_result_307; });
el_val_t next_tool_msg = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"type\":\"tool_result\",\"tool_use_id\":\""), next_tool_id), EL_STR("\",\"content\":\"")), next_tool_result), EL_STR("\"}"));
el_val_t tool_entry = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"tool\":\""), next_tool_name), EL_STR("\",\"input\":\"")), json_safe(next_tool_name)), EL_STR("\"}"));
tools_log = ({ el_val_t _if_result_302 = 0; if ((is_tool_turn && !needs_pause)) { _if_result_302 = (({ el_val_t _if_result_303 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_303 = (tool_entry); } else { _if_result_303 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_entry)); } _if_result_303; })); } else { _if_result_302 = (tools_log); } _if_result_302; });
cur_messages = ({ el_val_t _if_result_304 = 0; if ((is_tool_turn && !needs_pause)) { _if_result_304 = (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("["), inner2), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}")), EL_STR(",{\"role\":\"user\",\"content\":[")), next_tool_msg), EL_STR("]}")), EL_STR("]"))); } else { _if_result_304 = (cur_messages); } _if_result_304; });
el_val_t discard_pause = ({ el_val_t _if_result_305 = 0; if (needs_pause) { el_val_t safe_sys2 = json_safe(safe_sys); el_val_t msgs_with_assistant = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner2), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}]")); el_val_t pending = 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("{\"call_id\":\""), next_tool_id), EL_STR("\"")), EL_STR(",\"tool_name\":\"")), next_tool_name), EL_STR("\"")), EL_STR(",\"tool_input\":")), next_tool_input), EL_STR(",\"messages_so_far\":")), msgs_with_assistant), EL_STR(",\"model\":\"")), model), EL_STR("\"")), EL_STR(",\"system\":\"")), safe_sys2), EL_STR("\"}")); (void)(state_set(el_str_concat(EL_STR("pending_tool_"), session_id), pending)); _if_result_305 = (1); } else { _if_result_305 = (0); } _if_result_305; });
final_text = ({ el_val_t _if_result_306 = 0; if (!is_tool_turn) { _if_result_306 = (text_out); } else { _if_result_306 = (final_text); } _if_result_306; });
keep_going = ({ el_val_t _if_result_307 = 0; if (!is_tool_turn) { _if_result_307 = (0); } else { _if_result_307 = (({ el_val_t _if_result_308 = 0; if (needs_pause) { _if_result_308 = (0); } else { _if_result_308 = (keep_going); } _if_result_308; })); } _if_result_307; });
tools_log = ({ el_val_t _if_result_309 = 0; if ((is_tool_turn && !needs_pause)) { _if_result_309 = (({ el_val_t _if_result_310 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_310 = (tool_entry); } else { _if_result_310 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_entry)); } _if_result_310; })); } else { _if_result_309 = (tools_log); } _if_result_309; });
cur_messages = ({ el_val_t _if_result_311 = 0; if ((is_tool_turn && !needs_pause)) { _if_result_311 = (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("["), inner2), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}")), EL_STR(",{\"role\":\"user\",\"content\":[")), next_tool_msg), EL_STR("]}")), EL_STR("]"))); } else { _if_result_311 = (cur_messages); } _if_result_311; });
el_val_t discard_pause = ({ el_val_t _if_result_312 = 0; if (needs_pause) { el_val_t safe_sys2 = json_safe(safe_sys); el_val_t msgs_with_assistant = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner2), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}]")); el_val_t pending = 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("{\"call_id\":\""), next_tool_id), EL_STR("\"")), EL_STR(",\"tool_name\":\"")), next_tool_name), EL_STR("\"")), EL_STR(",\"tool_input\":")), next_tool_input), EL_STR(",\"messages_so_far\":")), msgs_with_assistant), EL_STR(",\"model\":\"")), model), EL_STR("\"")), EL_STR(",\"system\":\"")), safe_sys2), EL_STR("\"}")); (void)(state_set(el_str_concat(EL_STR("pending_tool_"), session_id), pending)); _if_result_312 = (1); } else { _if_result_312 = (0); } _if_result_312; });
final_text = ({ el_val_t _if_result_313 = 0; if (!is_tool_turn) { _if_result_313 = (text_out); } else { _if_result_313 = (final_text); } _if_result_313; });
keep_going = ({ el_val_t _if_result_314 = 0; if (!is_tool_turn) { _if_result_314 = (0); } else { _if_result_314 = (({ el_val_t _if_result_315 = 0; if (needs_pause) { _if_result_315 = (0); } else { _if_result_315 = (keep_going); } _if_result_315; })); } _if_result_314; });
iteration = (iteration + 1);
}
el_val_t new_pending = state_get(el_str_concat(EL_STR("pending_tool_"), session_id));
@@ -27661,11 +27685,11 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) {
}
el_val_t hist = session_hist_load(session_id);
el_val_t updated_hist = hist_append(hist, EL_STR("assistant"), final_text);
el_val_t final_hist = ({ el_val_t _if_result_309 = 0; if ((json_array_len(updated_hist) > 20)) { _if_result_309 = (hist_trim(updated_hist)); } else { _if_result_309 = (updated_hist); } _if_result_309; });
el_val_t final_hist = ({ el_val_t _if_result_316 = 0; if ((json_array_len(updated_hist) > 20)) { _if_result_316 = (hist_trim(updated_hist)); } else { _if_result_316 = (updated_hist); } _if_result_316; });
session_hist_save(session_id, final_hist);
session_update_meta_timestamp(session_id);
el_val_t safe_text = json_safe(final_text);
el_val_t tools_arr = ({ el_val_t _if_result_310 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_310 = (EL_STR("[]")); } else { _if_result_310 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_310; });
el_val_t tools_arr = ({ el_val_t _if_result_317 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_317 = (EL_STR("[]")); } else { _if_result_317 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_317; });
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("{\"reply\":\""), safe_text), EL_STR("\",\"model\":\"")), model), EL_STR("\",\"agentic\":true,\"tools_used\":")), tools_arr), EL_STR(",\"session_id\":\"")), session_id), EL_STR("\"}"));
return 0;
}
@@ -27692,11 +27716,11 @@ 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_311 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_311 = (EL_STR("0")); } else { _if_result_311 = (boot); } _if_result_311; });
el_val_t boot_num = ({ el_val_t _if_result_318 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_318 = (EL_STR("0")); } else { _if_result_318 = (boot); } _if_result_318; });
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_312 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_312 = (EL_STR("0")); } else { _if_result_312 = (pulse); } _if_result_312; });
el_val_t pulse_num = ({ el_val_t _if_result_319 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_319 = (EL_STR("0")); } else { _if_result_319 = (pulse); } _if_result_319; });
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("{\"status\":\"alive\""), EL_STR(",\"cgi_id\":\"")), cgi_id), EL_STR("\"")), EL_STR(",\"boot\":")), boot_num), 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("}"));
return 0;
}
@@ -27766,28 +27790,28 @@ 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_313 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_313 = (EL_STR("chat")); } else { _if_result_313 = (event_type); } _if_result_313; });
el_val_t eff_payload = ({ el_val_t _if_result_314 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_314 = (content_raw); } else { _if_result_314 = (payload); } _if_result_314; });
el_val_t eff_event = ({ el_val_t _if_result_320 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_320 = (EL_STR("chat")); } else { _if_result_320 = (event_type); } _if_result_320; });
el_val_t eff_payload = ({ el_val_t _if_result_321 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_321 = (content_raw); } else { _if_result_321 = (payload); } _if_result_321; });
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_315 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_315 = (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_315 = (eff_payload); } _if_result_315; });
el_val_t chat_body = ({ el_val_t _if_result_322 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_322 = (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_322 = (eff_payload); } _if_result_322; });
el_val_t agentic_flag = json_get_bool(eff_payload, EL_STR("agentic"));
el_val_t reply = ({ el_val_t _if_result_316 = 0; if (agentic_flag) { _if_result_316 = (handle_chat_agentic(chat_body)); } else { _if_result_316 = (handle_chat(chat_body)); } _if_result_316; });
el_val_t reply = ({ el_val_t _if_result_323 = 0; if (agentic_flag) { _if_result_323 = (handle_chat_agentic(chat_body)); } else { _if_result_323 = (handle_chat(chat_body)); } _if_result_323; });
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_317 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_317 = (20); } else { _if_result_317 = (str_to_int(limit_str)); } _if_result_317; });
el_val_t q = ({ el_val_t _if_result_318 = 0; if (str_eq(query, EL_STR(""))) { _if_result_318 = (eff_payload); } else { _if_result_318 = (query); } _if_result_318; });
el_val_t limit = ({ el_val_t _if_result_324 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_324 = (20); } else { _if_result_324 = (str_to_int(limit_str)); } _if_result_324; });
el_val_t q = ({ el_val_t _if_result_325 = 0; if (str_eq(query, EL_STR(""))) { _if_result_325 = (eff_payload); } else { _if_result_325 = (query); } _if_result_325; });
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_319 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_319 = (EL_STR("POST")); } else { _if_result_319 = (method_field); } _if_result_319; });
el_val_t eff_method = ({ el_val_t _if_result_326 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_326 = (EL_STR("POST")); } else { _if_result_326 = (method_field); } _if_result_326; });
return handle_tool(path_field, eff_method, tool_body);
}
if (str_eq(eff_event, EL_STR("see"))) {
@@ -27881,7 +27905,7 @@ 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_320 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_320 = (EL_STR("[]")); } else { _if_result_320 = (edges_raw); } _if_result_320; });
return ({ el_val_t _if_result_327 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_327 = (EL_STR("[]")); } else { _if_result_327 = (edges_raw); } _if_result_327; });
}
if (str_eq(clean, EL_STR("/api/chat"))) {
return handle_chat(body);
@@ -27980,7 +28004,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
}
if (str_eq(clean, EL_STR("/api/chat"))) {
el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic"));
el_val_t reply = ({ el_val_t _if_result_321 = 0; if (agentic_flag) { _if_result_321 = (handle_chat_agentic(body)); } else { _if_result_321 = (handle_chat(body)); } _if_result_321; });
el_val_t reply = ({ el_val_t _if_result_328 = 0; if (agentic_flag) { _if_result_328 = (handle_chat_agentic(body)); } else { _if_result_328 = (handle_chat(body)); } _if_result_328; });
auto_persist(body, reply);
return reply;
}
@@ -28093,7 +28117,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
el_val_t sess_id = parse_session_id_from_path(clean);
el_val_t sub = parse_session_subpath(clean);
if (!str_eq(sess_id, EL_STR("")) && str_eq(sub, EL_STR(""))) {
return session_update_title(sess_id, body);
return session_update_patch(sess_id, body);
}
}
return err_404(clean);
@@ -28180,21 +28204,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_322 = 0; if (intel_ok) { _if_result_322 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_322 = (EL_STR("")); } _if_result_322; });
el_val_t values_content = ({ el_val_t _if_result_323 = 0; if (values_ok) { _if_result_323 = (json_get(node_values, EL_STR("content"))); } else { _if_result_323 = (EL_STR("")); } _if_result_323; });
el_val_t mem_content = ({ el_val_t _if_result_324 = 0; if (mem_ok) { _if_result_324 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_324 = (EL_STR("")); } _if_result_324; });
el_val_t intel_short = ({ el_val_t _if_result_325 = 0; if ((str_len(intel_content) > 2000)) { _if_result_325 = (str_slice(intel_content, 0, 2000)); } else { _if_result_325 = (intel_content); } _if_result_325; });
el_val_t values_short = ({ el_val_t _if_result_326 = 0; if ((str_len(values_content) > 2000)) { _if_result_326 = (str_slice(values_content, 0, 2000)); } else { _if_result_326 = (values_content); } _if_result_326; });
el_val_t mem_short = ({ el_val_t _if_result_327 = 0; if ((str_len(mem_content) > 2000)) { _if_result_327 = (str_slice(mem_content, 0, 2000)); } else { _if_result_327 = (mem_content); } _if_result_327; });
el_val_t intel_content = ({ el_val_t _if_result_329 = 0; if (intel_ok) { _if_result_329 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_329 = (EL_STR("")); } _if_result_329; });
el_val_t values_content = ({ el_val_t _if_result_330 = 0; if (values_ok) { _if_result_330 = (json_get(node_values, EL_STR("content"))); } else { _if_result_330 = (EL_STR("")); } _if_result_330; });
el_val_t mem_content = ({ el_val_t _if_result_331 = 0; if (mem_ok) { _if_result_331 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_331 = (EL_STR("")); } _if_result_331; });
el_val_t intel_short = ({ el_val_t _if_result_332 = 0; if ((str_len(intel_content) > 2000)) { _if_result_332 = (str_slice(intel_content, 0, 2000)); } else { _if_result_332 = (intel_content); } _if_result_332; });
el_val_t values_short = ({ el_val_t _if_result_333 = 0; if ((str_len(values_content) > 2000)) { _if_result_333 = (str_slice(values_content, 0, 2000)); } else { _if_result_333 = (values_content); } _if_result_333; });
el_val_t mem_short = ({ el_val_t _if_result_334 = 0; if ((str_len(mem_content) > 2000)) { _if_result_334 = (str_slice(mem_content, 0, 2000)); } else { _if_result_334 = (mem_content); } _if_result_334; });
el_val_t parts_count = 0;
parts_count = ({ el_val_t _if_result_328 = 0; if (intel_ok) { _if_result_328 = ((parts_count + 1)); } else { _if_result_328 = (parts_count); } _if_result_328; });
parts_count = ({ el_val_t _if_result_329 = 0; if (values_ok) { _if_result_329 = ((parts_count + 1)); } else { _if_result_329 = (parts_count); } _if_result_329; });
parts_count = ({ el_val_t _if_result_330 = 0; if (mem_ok) { _if_result_330 = ((parts_count + 1)); } else { _if_result_330 = (parts_count); } _if_result_330; });
parts_count = ({ el_val_t _if_result_335 = 0; if (intel_ok) { _if_result_335 = ((parts_count + 1)); } else { _if_result_335 = (parts_count); } _if_result_335; });
parts_count = ({ el_val_t _if_result_336 = 0; if (values_ok) { _if_result_336 = ((parts_count + 1)); } else { _if_result_336 = (parts_count); } _if_result_336; });
parts_count = ({ el_val_t _if_result_337 = 0; if (mem_ok) { _if_result_337 = ((parts_count + 1)); } else { _if_result_337 = (parts_count); } _if_result_337; });
if (parts_count > 0) {
el_val_t ctx = EL_STR("");
ctx = ({ el_val_t _if_result_331 = 0; if (intel_ok) { _if_result_331 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_331 = (ctx); } _if_result_331; });
ctx = ({ el_val_t _if_result_332 = 0; if (values_ok) { _if_result_332 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_332 = (ctx); } _if_result_332; });
ctx = ({ el_val_t _if_result_333 = 0; if (mem_ok) { _if_result_333 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_333 = (ctx); } _if_result_333; });
ctx = ({ el_val_t _if_result_338 = 0; if (intel_ok) { _if_result_338 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_338 = (ctx); } _if_result_338; });
ctx = ({ el_val_t _if_result_339 = 0; if (values_ok) { _if_result_339 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_339 = (ctx); } _if_result_339; });
ctx = ({ el_val_t _if_result_340 = 0; if (mem_ok) { _if_result_340 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_340 = (ctx); } _if_result_340; });
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)")));
}
@@ -28250,14 +28274,14 @@ 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_334 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_334 = (EL_STR("0")); } else { _if_result_334 = (boot); } _if_result_334; });
el_val_t boot_num = ({ el_val_t _if_result_341 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_341 = (EL_STR("0")); } else { _if_result_341 = (boot); } _if_result_341; });
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_335 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_335 = (EL_STR("false")); } else { _if_result_335 = (EL_STR("true")); } _if_result_335; });
el_val_t has_identity = ({ el_val_t _if_result_342 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_342 = (EL_STR("false")); } else { _if_result_342 = (EL_STR("true")); } _if_result_342; });
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_336 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_336 = (cgi_from_state); } else { _if_result_336 = (({ el_val_t _if_result_337 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_337 = (cgi_from_env); } else { _if_result_337 = (EL_STR("ntn-genesis")); } _if_result_337; })); } _if_result_336; });
el_val_t eff_cgi = ({ el_val_t _if_result_343 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_343 = (cgi_from_state); } else { _if_result_343 = (({ el_val_t _if_result_344 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_344 = (cgi_from_env); } else { _if_result_344 = (EL_STR("ntn-genesis")); } _if_result_344; })); } _if_result_343; });
el_val_t ts = time_now();
el_val_t payload = 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("{\"event\":\"session_start\""), EL_STR(",\"boot\":")), boot_num), EL_STR(",\"cgi\":\"")), eff_cgi), EL_STR("\"")), EL_STR(",\"node_count\":")), int_to_str(node_ct)), EL_STR(",\"edge_count\":")), int_to_str(edge_ct)), EL_STR(",\"identity_loaded\":")), has_identity), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}"));
el_val_t tags = EL_STR("[\"internal-state\",\"session-start\",\"InternalStateEvent\"]");
@@ -28270,25 +28294,25 @@ int main(int _argc, char** _argv) {
el_runtime_init_args(_argc, _argv);
el_cgi_init(EL_STR("neuron-soul"), EL_STR("ntn-genesis@http://localhost:7770"), EL_STR("william-christopher-anderson"), EL_STR("dharma-mainnet"), EL_STR("http://localhost:8742"));
soul_cgi_id_raw = env(EL_STR("SOUL_CGI_ID"));
soul_cgi_id = ({ el_val_t _if_result_338 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_338 = (EL_STR("ntn-genesis")); } else { _if_result_338 = (soul_cgi_id_raw); } _if_result_338; });
soul_cgi_id = ({ el_val_t _if_result_345 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_345 = (EL_STR("ntn-genesis")); } else { _if_result_345 = (soul_cgi_id_raw); } _if_result_345; });
port_raw = env(EL_STR("NEURON_PORT"));
port = ({ el_val_t _if_result_339 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_339 = (7770); } else { _if_result_339 = (str_to_int(port_raw)); } _if_result_339; });
port = ({ el_val_t _if_result_346 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_346 = (7770); } else { _if_result_346 = (str_to_int(port_raw)); } _if_result_346; });
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_340 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_340 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_340 = (snapshot_raw); } _if_result_340; });
snapshot = ({ el_val_t _if_result_347 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_347 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_347 = (snapshot_raw); } _if_result_347; });
axon_raw = env(EL_STR("NEURON_API_URL"));
axon_base = ({ el_val_t _if_result_341 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_341 = (EL_STR("http://localhost:7771")); } else { _if_result_341 = (axon_raw); } _if_result_341; });
axon_base = ({ el_val_t _if_result_348 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_348 = (EL_STR("http://localhost:7771")); } else { _if_result_348 = (axon_raw); } _if_result_348; });
studio_dir_raw = env(EL_STR("SOUL_STUDIO_DIR"));
studio_dir = ({ el_val_t _if_result_342 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_342 = (EL_STR("/Users/will/Development/neuron-technologies/products/cgi-studio/el-daemon")); } else { _if_result_342 = (studio_dir_raw); } _if_result_342; });
studio_dir = ({ el_val_t _if_result_349 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_349 = (EL_STR("/Users/will/Development/neuron-technologies/products/cgi-studio/el-daemon")); } else { _if_result_349 = (studio_dir_raw); } _if_result_349; });
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(""));
if (using_http_engram) {
println(el_str_concat(EL_STR("[soul] engram -> HTTP "), engram_url_raw));
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_343 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_343 = (EL_STR("[]")); } else { _if_result_343 = (nodes_json); } _if_result_343; });
el_val_t edges_part = ({ el_val_t _if_result_344 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_344 = (EL_STR("[]")); } else { _if_result_344 = (edges_json); } _if_result_344; });
el_val_t nodes_part = ({ el_val_t _if_result_350 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_350 = (EL_STR("[]")); } else { _if_result_350 = (nodes_json); } _if_result_350; });
el_val_t edges_part = ({ el_val_t _if_result_351 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_351 = (EL_STR("[]")); } else { _if_result_351 = (edges_json); } _if_result_351; });
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);