#include #include #include "el_runtime.h" el_val_t tier_working(void); el_val_t tier_episodic(void); el_val_t tier_canonical(void); el_val_t mem_store(el_val_t content, el_val_t label, el_val_t tags); 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_forget(el_val_t node_id); el_val_t mem_consolidate(void); el_val_t mem_save(el_val_t path); el_val_t mem_load(el_val_t path); el_val_t mem_boot_count_get(void); el_val_t mem_boot_count_inc(void); el_val_t mem_emit_state_event(el_val_t trigger, el_val_t kind, el_val_t content); el_val_t idle_count(void); el_val_t idle_inc(void); el_val_t idle_reset(void); el_val_t ise_post(el_val_t content); el_val_t elapsed_ms(void); el_val_t elapsed_human(void); el_val_t embed_ok(void); el_val_t emit_heartbeat(void); el_val_t proactive_curiosity(void); el_val_t pulse_count(void); el_val_t pulse_inc(void); el_val_t make_action(el_val_t kind, el_val_t payload); el_val_t perceive(void); el_val_t attend(el_val_t node_json); el_val_t respond(el_val_t action_json); el_val_t record(el_val_t outcome_json); el_val_t one_cycle(void); el_val_t awareness_run(void); el_val_t security_research_authorized(void); el_val_t threat_score_command(el_val_t cmd); el_val_t threat_score_path(el_val_t path); el_val_t threat_score_history(el_val_t history); el_val_t threat_trajectory_check(el_val_t tool_name, el_val_t tool_input); el_val_t threat_history_append(el_val_t text); el_val_t tier_working(void) { return EL_STR("Working"); return 0; } el_val_t tier_episodic(void) { return EL_STR("Episodic"); return 0; } el_val_t tier_canonical(void) { return EL_STR("Canonical"); return 0; } el_val_t mem_store(el_val_t content, el_val_t label, el_val_t tags) { return engram_node_full(content, EL_STR("Memory"), label, el_from_float(el_from_float(0.5)), el_from_float(el_from_float(0.5)), el_from_float(el_from_float(0.8)), EL_STR("Working"), tags); return 0; } el_val_t mem_remember(el_val_t content, el_val_t tags) { return mem_store(content, EL_STR("soul-memory"), tags); return 0; } el_val_t mem_recall(el_val_t query, el_val_t depth) { return engram_activate_json(query, depth); return 0; } el_val_t mem_search(el_val_t query, el_val_t limit) { return engram_search_json(query, limit); return 0; } el_val_t mem_strengthen(el_val_t node_id) { engram_strengthen(node_id); return 0; } el_val_t mem_forget(el_val_t node_id) { engram_forget(node_id); return 0; } el_val_t mem_consolidate(void) { el_val_t scanned = engram_node_count(); el_val_t dummy = engram_scan_nodes_json(100, 0); el_val_t total_nodes = engram_node_count(); el_val_t total_edges = engram_edge_count(); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"scanned\":"), int_to_str(scanned)), EL_STR(",\"total_nodes\":")), int_to_str(total_nodes)), EL_STR(",\"total_edges\":")), int_to_str(total_edges)), EL_STR("}")); return 0; } el_val_t mem_save(el_val_t path) { engram_save(path); return 0; } el_val_t mem_load(el_val_t path) { engram_load(path); return 0; } el_val_t mem_boot_count_get(void) { el_val_t results = engram_search_json(EL_STR("soul:boot_count"), 3); if (str_eq(results, EL_STR(""))) { return 0; } if (str_eq(results, EL_STR("[]"))) { return 0; } 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("soul:boot_count:"); if (!str_starts_with(content, prefix)) { return 0; } el_val_t num_str = str_slice(content, str_len(prefix), str_len(content)); return str_to_int(num_str); return 0; } el_val_t mem_boot_count_inc(void) { el_val_t current = mem_boot_count_get(); el_val_t next = (current + 1); el_val_t content = el_str_concat(EL_STR("soul:boot_count:"), int_to_str(next)); el_val_t tags = EL_STR("[\"soul-meta\",\"boot-counter\"]"); el_val_t discard = engram_node_full(content, EL_STR("Memory"), EL_STR("soul:boot_count"), el_from_float(el_from_float(0.9)), el_from_float(el_from_float(0.9)), el_from_float(el_from_float(1.0)), EL_STR("Canonical"), tags); return next; return 0; } el_val_t mem_emit_state_event(el_val_t trigger, el_val_t kind, el_val_t content) { el_val_t boot = mem_boot_count_get(); el_val_t ts = time_now(); el_val_t safe_trigger = str_replace(trigger, EL_STR("\""), EL_STR("'")); el_val_t safe_content = str_replace(content, EL_STR("\""), 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("{\"trigger\":\""), safe_trigger), EL_STR("\"")), EL_STR(",\"kind\":\"")), kind), EL_STR("\"")), EL_STR(",\"content\":\"")), safe_content), EL_STR("\"")), EL_STR(",\"boot\":")), int_to_str(boot)), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}")); el_val_t tags = EL_STR("[\"internal-state\",\"pre-reasoning\",\"InternalStateEvent\"]"); return engram_node_full(payload, EL_STR("InternalStateEvent"), el_str_concat(EL_STR("state-event:"), kind), el_from_float(el_from_float(0.85)), el_from_float(el_from_float(0.8)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags); return 0; } el_val_t idle_count(void) { el_val_t s = state_get(EL_STR("soul.idle")); if (str_eq(s, EL_STR(""))) { return 0; } return str_to_int(s); return 0; } el_val_t idle_inc(void) { el_val_t n = (idle_count() + 1); state_set(EL_STR("soul.idle"), int_to_str(n)); return n; return 0; } el_val_t idle_reset(void) { state_set(EL_STR("soul.idle"), EL_STR("0")); return 0; } el_val_t ise_post(el_val_t content) { el_val_t ise_url = env(EL_STR("SOUL_ISE_URL")); el_val_t engram_url = ({ el_val_t _if_result_1 = 0; if (str_eq(ise_url, EL_STR(""))) { _if_result_1 = (state_get(EL_STR("soul_engram_url"))); } else { _if_result_1 = (ise_url); } _if_result_1; }); if (str_eq(engram_url, EL_STR(""))) { el_val_t discard = engram_node_full(content, EL_STR("InternalStateEvent"), EL_STR("state-event"), el_from_float(el_from_float(0.3)), el_from_float(el_from_float(0.3)), el_from_float(el_from_float(0.8)), EL_STR("Episodic"), EL_STR("[\"internal-state\",\"InternalStateEvent\"]")); return EL_STR(""); } el_val_t safe1 = str_replace(content, EL_STR("\\"), EL_STR("\\\\")); el_val_t safe2 = str_replace(safe1, EL_STR("\""), EL_STR("\\\"")); el_val_t safe3 = str_replace(safe2, EL_STR("\n"), EL_STR("\\n")); el_val_t safe4 = str_replace(safe3, EL_STR("\r"), EL_STR("\\r")); el_val_t body = el_str_concat(el_str_concat(EL_STR("{\"content\":\""), safe4), EL_STR("\"}")); el_val_t discard = http_post_json(el_str_concat(engram_url, EL_STR("/api/neuron/state-events")), body); return EL_STR(""); return 0; } el_val_t elapsed_ms(void) { el_val_t s = state_get(EL_STR("soul.boot_ts")); if (str_eq(s, EL_STR(""))) { return 0; } el_val_t boot = str_to_int(s); return (time_now() - boot); return 0; } el_val_t elapsed_human(void) { el_val_t ms = elapsed_ms(); el_val_t total_secs = (ms / 1000); el_val_t total_minutes = (total_secs / 60); el_val_t h = (total_minutes / 60); if (h > 0) { el_val_t h4 = (((h + h) + h) + h); el_val_t h8 = (h4 + h4); el_val_t h16 = (h8 + h8); el_val_t h32 = (h16 + h16); el_val_t h64 = (h32 + h32); el_val_t h60 = (h64 - h4); el_val_t m = (total_minutes - h60); return el_str_concat(el_str_concat(el_str_concat(int_to_str(h), EL_STR("h ")), int_to_str(m)), EL_STR("m")); } if (total_minutes > 0) { return el_str_concat(int_to_str(total_minutes), EL_STR("m")); } return el_str_concat(int_to_str(total_secs), EL_STR("s")); return 0; } el_val_t embed_ok(void) { el_val_t resp = http_get(EL_STR("http://localhost:11434")); if (str_eq(resp, EL_STR(""))) { return 0; } return 1; return 0; } el_val_t emit_heartbeat(void) { el_val_t pulse = int_to_str(pulse_count()); el_val_t boot_raw = state_get(EL_STR("soul_boot_count")); el_val_t boot = ({ el_val_t _if_result_2 = 0; if (str_eq(boot_raw, EL_STR(""))) { _if_result_2 = (EL_STR("0")); } else { _if_result_2 = (boot_raw); } _if_result_2; }); el_val_t idle = int_to_str(idle_count()); el_val_t ts = time_now(); 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 wm_top = engram_wm_top_json(5); 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_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(",\"wm_top\":")), wm_top), 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; } el_val_t proactive_curiosity(void) { el_val_t ts = time_now(); el_val_t ts_minutes = (ts / 60000); el_val_t minute_q = (ts_minutes / 4); el_val_t minute_q2 = (minute_q + minute_q); el_val_t minute_q4 = (minute_q2 + minute_q2); el_val_t minute_block = (ts_minutes - minute_q4); state_set(EL_STR("cseed_a"), EL_STR("memory")); state_set(EL_STR("cseed_b"), EL_STR("knowledge")); state_set(EL_STR("cseed_c"), EL_STR("context")); if (minute_block == 1) { state_set(EL_STR("cseed_a"), EL_STR("self")); state_set(EL_STR("cseed_b"), EL_STR("identity")); state_set(EL_STR("cseed_c"), EL_STR("values")); } if (minute_block == 2) { state_set(EL_STR("cseed_a"), EL_STR("decision")); state_set(EL_STR("cseed_b"), EL_STR("pattern")); state_set(EL_STR("cseed_c"), EL_STR("lesson")); } if (minute_block == 3) { state_set(EL_STR("cseed_a"), EL_STR("working")); state_set(EL_STR("cseed_b"), EL_STR("project")); state_set(EL_STR("cseed_c"), EL_STR("active")); } el_val_t curiosity_term_a = state_get(EL_STR("cseed_a")); el_val_t curiosity_term_b = state_get(EL_STR("cseed_b")); el_val_t curiosity_term_c = state_get(EL_STR("cseed_c")); el_val_t curiosity_seed = el_str_concat(el_str_concat(el_str_concat(el_str_concat(curiosity_term_a, EL_STR(" ")), curiosity_term_b), EL_STR(" ")), curiosity_term_c); el_val_t results_a = engram_activate_json(curiosity_term_a, 1); el_val_t results_b = engram_activate_json(curiosity_term_b, 1); el_val_t results_c = engram_activate_json(curiosity_term_c, 1); el_val_t found_a = json_array_len(results_a); el_val_t found_b = json_array_len(results_b); el_val_t found_c = json_array_len(results_c); el_val_t found = ((found_a + found_b) + found_c); state_set(EL_STR("cseed_auto"), EL_STR("")); el_val_t wm_top_j = engram_wm_top_json(1); el_val_t wm_top_n = json_array_get(wm_top_j, 0); el_val_t wm_top_lbl = json_get(wm_top_n, EL_STR("label")); if (!str_eq(wm_top_lbl, EL_STR(""))) { el_val_t sp = str_find_chars(wm_top_lbl, EL_STR(" :([")); if (sp > 3) { state_set(EL_STR("cseed_auto"), str_slice(wm_top_lbl, 0, sp)); } } el_val_t auto_term = state_get(EL_STR("cseed_auto")); el_val_t results_auto = ({ el_val_t _if_result_3 = 0; if (str_eq(auto_term, EL_STR(""))) { _if_result_3 = (EL_STR("[]")); } else { _if_result_3 = (engram_activate_json(auto_term, 1)); } _if_result_3; }); el_val_t found_auto = json_array_len(results_auto); el_val_t total_found = (found + found_auto); el_val_t safe_auto = str_replace(auto_term, EL_STR("\""), EL_STR("'")); el_val_t wmc = engram_wm_count(); el_val_t ise = 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\":\"curiosity_scan\",\"seed\":\""), curiosity_seed), EL_STR("\",\"auto_term\":\"")), safe_auto), EL_STR("\",\"minute_block\":")), int_to_str(minute_block)), EL_STR(",\"activated\":")), int_to_str(total_found)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}")); ise_post(ise); return (total_found > 0); return 0; } el_val_t pulse_count(void) { el_val_t s = state_get(EL_STR("soul.pulse")); if (str_eq(s, EL_STR(""))) { return 0; } return str_to_int(s); return 0; } el_val_t pulse_inc(void) { el_val_t n = (pulse_count() + 1); state_set(EL_STR("soul.pulse"), int_to_str(n)); return n; return 0; } el_val_t make_action(el_val_t kind, el_val_t payload) { el_val_t safe = str_replace(payload, EL_STR("\\"), EL_STR("\\\\")); el_val_t safe2 = str_replace(safe, EL_STR("\""), EL_STR("\\\"")); el_val_t safe3 = str_replace(safe2, EL_STR("\n"), EL_STR("\\n")); el_val_t safe4 = str_replace(safe3, EL_STR("\r"), EL_STR("\\r")); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"kind\":\""), kind), EL_STR("\",\"payload\":\"")), safe4), EL_STR("\"}")); return 0; } el_val_t perceive(void) { el_val_t inbox_check = engram_search_json(EL_STR("soul-inbox"), 5); el_val_t has_inbox = (!str_eq(inbox_check, EL_STR("")) && !str_eq(inbox_check, EL_STR("[]"))); if (!has_inbox) { return EL_STR("[]"); } el_val_t from_pending = engram_activate_json(EL_STR("soul-inbox-pending"), 2); el_val_t pending_ok = (!str_eq(from_pending, EL_STR("")) && !str_eq(from_pending, EL_STR("[]"))); if (pending_ok) { return from_pending; } el_val_t from_inbox = engram_activate_json(EL_STR("soul-inbox"), 2); el_val_t inbox_ok = (!str_eq(from_inbox, EL_STR("")) && !str_eq(from_inbox, EL_STR("[]"))); if (inbox_ok) { return from_inbox; } return EL_STR("[]"); return 0; } el_val_t attend(el_val_t node_json) { if (str_eq(node_json, EL_STR(""))) { return make_action(EL_STR("noop"), EL_STR("")); } if (str_eq(node_json, EL_STR("[]"))) { return make_action(EL_STR("noop"), EL_STR("")); } el_val_t node_id = json_get(node_json, EL_STR("id")); if (!str_eq(node_id, EL_STR(""))) { engram_strengthen(node_id); } el_val_t content = json_get(node_json, EL_STR("content")); if (str_eq(content, EL_STR(""))) { return make_action(EL_STR("noop"), EL_STR("")); } if (str_eq(content, EL_STR("consolidate"))) { return make_action(EL_STR("consolidate"), EL_STR("")); } if (str_starts_with(content, EL_STR("remember "))) { el_val_t payload = str_slice(content, 9, str_len(content)); return make_action(EL_STR("remember"), payload); } if (str_starts_with(content, EL_STR("search "))) { el_val_t payload = str_slice(content, 7, str_len(content)); return make_action(EL_STR("search"), payload); } if (str_starts_with(content, EL_STR("activate "))) { el_val_t payload = str_slice(content, 9, str_len(content)); return make_action(EL_STR("activate"), payload); } if (str_starts_with(content, EL_STR("strengthen "))) { el_val_t payload = str_slice(content, 11, str_len(content)); return make_action(EL_STR("strengthen"), payload); } if (str_starts_with(content, EL_STR("forget "))) { el_val_t payload = str_slice(content, 7, str_len(content)); return make_action(EL_STR("forget"), payload); } return make_action(EL_STR("respond"), content); return 0; } el_val_t respond(el_val_t action_json) { el_val_t kind = json_get(action_json, EL_STR("kind")); el_val_t payload = json_get(action_json, EL_STR("payload")); if (str_eq(kind, EL_STR("noop"))) { return EL_STR("{\"outcome\":\"noop\"}"); } if (str_eq(kind, EL_STR("remember"))) { el_val_t tags = EL_STR("[\"soul-memory\",\"awareness\"]"); el_val_t id = mem_remember(payload, tags); return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"remembered\",\"id\":\""), id), EL_STR("\"}")); } if (str_eq(kind, EL_STR("consolidate"))) { el_val_t stats = mem_consolidate(); return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"consolidated\",\"stats\":"), stats), EL_STR("}")); } if (str_eq(kind, EL_STR("respond"))) { el_val_t tags = EL_STR("[\"soul-outbox\",\"awareness\"]"); el_val_t id = mem_store(payload, EL_STR("soul-response"), tags); return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"response\",\"id\":\""), id), EL_STR("\"}")); } if (str_eq(kind, EL_STR("search"))) { el_val_t results = mem_search(payload, 10); el_val_t safe_results = str_replace(results, EL_STR("\""), EL_STR("'")); el_val_t tags = EL_STR("[\"soul-outbox\",\"search-result\"]"); el_val_t id = mem_store(safe_results, EL_STR("search-result"), tags); return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"searched\",\"id\":\""), id), EL_STR("\"}")); } if (str_eq(kind, EL_STR("activate"))) { el_val_t results = mem_recall(payload, 3); el_val_t safe_results = str_replace(results, EL_STR("\""), EL_STR("'")); el_val_t tags = EL_STR("[\"soul-outbox\",\"activation-result\"]"); el_val_t id = mem_store(safe_results, EL_STR("activation-result"), tags); return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"activated\",\"id\":\""), id), EL_STR("\"}")); } if (str_eq(kind, EL_STR("strengthen"))) { engram_strengthen(payload); 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("\"}")); } return EL_STR("{\"outcome\":\"noop\"}"); return 0; } el_val_t record(el_val_t outcome_json) { el_val_t tags = EL_STR("[\"loop-outcome\"]"); mem_store(outcome_json, EL_STR("loop-outcome"), tags); return 0; } el_val_t one_cycle(void) { el_val_t raw = perceive(); if (str_eq(raw, EL_STR(""))) { return 0; } if (str_eq(raw, EL_STR("[]"))) { return 0; } el_val_t node = json_array_get(raw, 0); if (str_eq(node, EL_STR(""))) { return 0; } el_val_t action = attend(node); el_val_t kind = json_get(action, EL_STR("kind")); el_val_t is_interesting = (!str_eq(kind, EL_STR("noop")) && !str_eq(kind, EL_STR("respond"))); if (is_interesting) { el_val_t trigger_content = json_get(node, EL_STR("content")); el_val_t safe_trigger = str_replace(trigger_content, EL_STR("\""), EL_STR("'")); el_val_t ts = time_now(); el_val_t event_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"awareness-decision\",\"trigger\":\""), safe_trigger), EL_STR("\",\"kind\":\"")), kind), EL_STR("\",\"ts\":")), int_to_str(ts)), EL_STR("}")); ise_post(event_content); } if (str_eq(kind, EL_STR("noop"))) { return 0; } el_val_t outcome = respond(action); record(outcome); pulse_inc(); return 1; return 0; } el_val_t awareness_run(void) { println(EL_STR("[awareness] entering")); el_val_t existing_boot = state_get(EL_STR("soul.boot_ts")); if (str_eq(existing_boot, EL_STR(""))) { state_set(EL_STR("soul.boot_ts"), int_to_str(time_now())); } el_val_t tick_raw = env(EL_STR("SOUL_TICK_MS")); el_val_t tick_ms = ({ el_val_t _if_result_4 = 0; if (str_eq(tick_raw, EL_STR(""))) { _if_result_4 = (200); } else { _if_result_4 = (str_to_int(tick_raw)); } _if_result_4; }); el_val_t beat_ms_raw = env(EL_STR("SOUL_HEARTBEAT_MS")); el_val_t beat_ms = ({ el_val_t _if_result_5 = 0; if (str_eq(beat_ms_raw, EL_STR(""))) { _if_result_5 = (60000); } else { _if_result_5 = (str_to_int(beat_ms_raw)); } _if_result_5; }); el_val_t scan_ms = (beat_ms / 2); while (1) { el_val_t running = state_get(EL_STR("soul.running")); if (str_eq(running, EL_STR("false"))) { println(EL_STR("[awareness] exiting")); return EL_STR(""); } el_val_t did_work = one_cycle(); did_work = ({ el_val_t _if_result_6 = 0; if (did_work) { _if_result_6 = (idle_reset()); } else { _if_result_6 = (did_work); } _if_result_6; }); el_val_t now_ts = time_now(); el_val_t last_beat_str = state_get(EL_STR("soul.last_beat_ts")); el_val_t last_beat_ts = ({ el_val_t _if_result_7 = 0; if (str_eq(last_beat_str, EL_STR(""))) { _if_result_7 = (0); } else { _if_result_7 = (str_to_int(last_beat_str)); } _if_result_7; }); el_val_t beat_elapsed = (now_ts - last_beat_ts); el_val_t should_beat = (beat_elapsed >= beat_ms); if (should_beat) { emit_heartbeat(); state_set(EL_STR("soul.last_beat_ts"), int_to_str(now_ts)); el_val_t snap_path = state_get(EL_STR("soul_snapshot_path")); if (!str_eq(snap_path, EL_STR(""))) { mem_save(snap_path); } } el_val_t last_scan_str = state_get(EL_STR("soul.last_scan_ts")); el_val_t last_scan_ts = ({ el_val_t _if_result_8 = 0; if (str_eq(last_scan_str, EL_STR(""))) { _if_result_8 = (0); } else { _if_result_8 = (str_to_int(last_scan_str)); } _if_result_8; }); el_val_t scan_elapsed = (now_ts - last_scan_ts); el_val_t should_scan = (!did_work && (scan_elapsed >= scan_ms)); if (should_scan) { el_val_t found_something = proactive_curiosity(); state_set(EL_STR("soul.last_scan_ts"), int_to_str(now_ts)); } el_val_t refresh_ms_raw = env(EL_STR("SOUL_REFRESH_MS")); el_val_t refresh_ms = ({ el_val_t _if_result_9 = 0; if (str_eq(refresh_ms_raw, EL_STR(""))) { _if_result_9 = (600000); } else { _if_result_9 = (str_to_int(refresh_ms_raw)); } _if_result_9; }); el_val_t last_refresh_str = state_get(EL_STR("soul.last_refresh_ts")); el_val_t last_refresh_ts = ({ el_val_t _if_result_10 = 0; if (str_eq(last_refresh_str, EL_STR(""))) { _if_result_10 = (0); } else { _if_result_10 = (str_to_int(last_refresh_str)); } _if_result_10; }); el_val_t refresh_elapsed = (now_ts - last_refresh_ts); el_val_t should_refresh = (refresh_elapsed >= refresh_ms); if (should_refresh) { el_val_t engram_url = state_get(EL_STR("soul_engram_url")); if (!str_eq(engram_url, EL_STR(""))) { el_val_t sync_json = http_get(el_str_concat(engram_url, EL_STR("/api/sync"))); if (!str_eq(sync_json, EL_STR("")) && !str_eq(sync_json, EL_STR("{}"))) { el_val_t cgi_id = state_get(EL_STR("soul_cgi_id")); el_val_t tmp = el_str_concat(el_str_concat(EL_STR("/tmp/soul-sync-"), cgi_id), EL_STR(".json")); fs_write(tmp, sync_json); el_val_t added = engram_load_merge(tmp); el_val_t ts2 = time_now(); ise_post(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"engram_sync\",\"added\":"), int_to_str(added)), EL_STR(",\"ts\":")), int_to_str(ts2)), EL_STR("}"))); } } state_set(EL_STR("soul.last_refresh_ts"), int_to_str(now_ts)); } sleep_ms(tick_ms); } return 0; } el_val_t security_research_authorized(void) { el_val_t token = env(EL_STR("SECURITY_RESEARCH_TOKEN")); if (!str_eq(token, EL_STR(""))) { return 1; } el_val_t state_auth = state_get(EL_STR("security_research_authorized")); return str_eq(state_auth, EL_STR("true")); return 0; } el_val_t threat_score_command(el_val_t cmd) { el_val_t s1 = ({ el_val_t _if_result_11 = 0; if (str_contains(cmd, EL_STR("nmap"))) { _if_result_11 = (30); } else { _if_result_11 = (0); } _if_result_11; }); el_val_t s2 = ({ el_val_t _if_result_12 = 0; if (str_contains(cmd, EL_STR("masscan"))) { _if_result_12 = (40); } else { _if_result_12 = (0); } _if_result_12; }); el_val_t s3 = ({ el_val_t _if_result_13 = 0; if (str_contains(cmd, EL_STR(" nc "))) { _if_result_13 = (20); } else { _if_result_13 = (0); } _if_result_13; }); el_val_t s4 = ({ el_val_t _if_result_14 = 0; if (str_contains(cmd, EL_STR("netcat"))) { _if_result_14 = (20); } else { _if_result_14 = (0); } _if_result_14; }); el_val_t s5 = ({ el_val_t _if_result_15 = 0; if (str_contains(cmd, EL_STR("/etc/shadow"))) { _if_result_15 = (80); } else { _if_result_15 = (0); } _if_result_15; }); el_val_t s6 = ({ el_val_t _if_result_16 = 0; if (str_contains(cmd, EL_STR("/etc/passwd"))) { _if_result_16 = (30); } else { _if_result_16 = (0); } _if_result_16; }); el_val_t s7 = ({ el_val_t _if_result_17 = 0; if (str_contains(cmd, EL_STR("id_rsa"))) { _if_result_17 = (60); } else { _if_result_17 = (0); } _if_result_17; }); el_val_t s8 = ({ el_val_t _if_result_18 = 0; if (str_contains(cmd, EL_STR(".ssh/"))) { _if_result_18 = (50); } else { _if_result_18 = (0); } _if_result_18; }); el_val_t s9 = ({ el_val_t _if_result_19 = 0; if (str_contains(cmd, EL_STR("crontab"))) { _if_result_19 = (30); } else { _if_result_19 = (0); } _if_result_19; }); el_val_t s10 = ({ el_val_t _if_result_20 = 0; if (str_contains(cmd, EL_STR("LaunchDaemon"))) { _if_result_20 = (40); } else { _if_result_20 = (0); } _if_result_20; }); el_val_t s11 = ({ el_val_t _if_result_21 = 0; if ((str_contains(cmd, EL_STR("curl")) && str_contains(cmd, EL_STR("bash")))) { _if_result_21 = (75); } else { _if_result_21 = (0); } _if_result_21; }); el_val_t s12 = ({ el_val_t _if_result_22 = 0; if ((str_contains(cmd, EL_STR("wget")) && str_contains(cmd, EL_STR("bash")))) { _if_result_22 = (75); } else { _if_result_22 = (0); } _if_result_22; }); el_val_t s13 = ({ el_val_t _if_result_23 = 0; if ((str_contains(cmd, EL_STR("curl")) && str_contains(cmd, EL_STR("| sh")))) { _if_result_23 = (60); } else { _if_result_23 = (0); } _if_result_23; }); el_val_t s14 = ({ el_val_t _if_result_24 = 0; if ((str_contains(cmd, EL_STR("base64")) && str_contains(cmd, EL_STR("curl")))) { _if_result_24 = (50); } else { _if_result_24 = (0); } _if_result_24; }); el_val_t s15 = ({ el_val_t _if_result_25 = 0; if (str_contains(cmd, EL_STR("mkfifo"))) { _if_result_25 = (50); } else { _if_result_25 = (0); } _if_result_25; }); el_val_t s16 = ({ el_val_t _if_result_26 = 0; if (str_contains(cmd, EL_STR("chmod +s"))) { _if_result_26 = (70); } else { _if_result_26 = (0); } _if_result_26; }); el_val_t s17 = ({ el_val_t _if_result_27 = 0; if (str_contains(cmd, EL_STR("chmod 4755"))) { _if_result_27 = (70); } else { _if_result_27 = (0); } _if_result_27; }); return ((((((((((((((((s1 + s2) + s3) + s4) + s5) + s6) + s7) + s8) + s9) + s10) + s11) + s12) + s13) + s14) + s15) + s16) + s17); return 0; } el_val_t threat_score_path(el_val_t path) { el_val_t s1 = ({ el_val_t _if_result_28 = 0; if (str_starts_with(path, EL_STR("/etc/"))) { _if_result_28 = (60); } else { _if_result_28 = (0); } _if_result_28; }); el_val_t s2 = ({ el_val_t _if_result_29 = 0; if (str_contains(path, EL_STR("/.ssh/"))) { _if_result_29 = (70); } else { _if_result_29 = (0); } _if_result_29; }); el_val_t s3 = ({ el_val_t _if_result_30 = 0; if (str_contains(path, EL_STR("/LaunchDaemons/"))) { _if_result_30 = (80); } else { _if_result_30 = (0); } _if_result_30; }); el_val_t s4 = ({ el_val_t _if_result_31 = 0; if (str_contains(path, EL_STR("/LaunchAgents/"))) { _if_result_31 = (40); } else { _if_result_31 = (0); } _if_result_31; }); el_val_t s5 = ({ el_val_t _if_result_32 = 0; if (str_contains(path, EL_STR("/cron"))) { _if_result_32 = (60); } else { _if_result_32 = (0); } _if_result_32; }); el_val_t s6 = ({ el_val_t _if_result_33 = 0; if (str_contains(path, EL_STR("/.bashrc"))) { _if_result_33 = (35); } else { _if_result_33 = (0); } _if_result_33; }); el_val_t s7 = ({ el_val_t _if_result_34 = 0; if (str_contains(path, EL_STR("/.zshrc"))) { _if_result_34 = (35); } else { _if_result_34 = (0); } _if_result_34; }); el_val_t s8 = ({ el_val_t _if_result_35 = 0; if (str_contains(path, EL_STR("/.profile"))) { _if_result_35 = (35); } else { _if_result_35 = (0); } _if_result_35; }); el_val_t s9 = ({ el_val_t _if_result_36 = 0; if (str_starts_with(path, EL_STR("/usr/"))) { _if_result_36 = (50); } else { _if_result_36 = (0); } _if_result_36; }); el_val_t s10 = ({ el_val_t _if_result_37 = 0; if (str_starts_with(path, EL_STR("/bin/"))) { _if_result_37 = (70); } else { _if_result_37 = (0); } _if_result_37; }); el_val_t s11 = ({ el_val_t _if_result_38 = 0; if (str_starts_with(path, EL_STR("/sbin/"))) { _if_result_38 = (70); } else { _if_result_38 = (0); } _if_result_38; }); return ((((((((((s1 + s2) + s3) + s4) + s5) + s6) + s7) + s8) + s9) + s10) + s11); return 0; } el_val_t threat_score_history(el_val_t history) { el_val_t s1 = ({ el_val_t _if_result_39 = 0; if (str_contains(history, EL_STR("port scan"))) { _if_result_39 = (15); } else { _if_result_39 = (0); } _if_result_39; }); el_val_t s2 = ({ el_val_t _if_result_40 = 0; if (str_contains(history, EL_STR("enumerate"))) { _if_result_40 = (10); } else { _if_result_40 = (0); } _if_result_40; }); el_val_t s3 = ({ el_val_t _if_result_41 = 0; if (str_contains(history, EL_STR("exploit"))) { _if_result_41 = (20); } else { _if_result_41 = (0); } _if_result_41; }); el_val_t s4 = ({ el_val_t _if_result_42 = 0; if (str_contains(history, EL_STR("payload"))) { _if_result_42 = (15); } else { _if_result_42 = (0); } _if_result_42; }); el_val_t s5 = ({ el_val_t _if_result_43 = 0; if (str_contains(history, EL_STR("persistence"))) { _if_result_43 = (15); } else { _if_result_43 = (0); } _if_result_43; }); el_val_t s6 = ({ el_val_t _if_result_44 = 0; if (str_contains(history, EL_STR("lateral movement"))) { _if_result_44 = (25); } else { _if_result_44 = (0); } _if_result_44; }); el_val_t s7 = ({ el_val_t _if_result_45 = 0; if (str_contains(history, EL_STR("privilege escalation"))) { _if_result_45 = (25); } else { _if_result_45 = (0); } _if_result_45; }); el_val_t s8 = ({ el_val_t _if_result_46 = 0; if (str_contains(history, EL_STR("reverse shell"))) { _if_result_46 = (40); } else { _if_result_46 = (0); } _if_result_46; }); el_val_t s9 = ({ el_val_t _if_result_47 = 0; if (str_contains(history, EL_STR("bind shell"))) { _if_result_47 = (40); } else { _if_result_47 = (0); } _if_result_47; }); el_val_t s10 = ({ el_val_t _if_result_48 = 0; if (str_contains(history, EL_STR("command and control"))) { _if_result_48 = (35); } else { _if_result_48 = (0); } _if_result_48; }); el_val_t s11 = ({ el_val_t _if_result_49 = 0; if (str_contains(history, EL_STR("self-replicate"))) { _if_result_49 = (45); } else { _if_result_49 = (0); } _if_result_49; }); el_val_t s12 = ({ el_val_t _if_result_50 = 0; if (str_contains(history, EL_STR("propagat"))) { _if_result_50 = (20); } else { _if_result_50 = (0); } _if_result_50; }); el_val_t s13 = ({ el_val_t _if_result_51 = 0; if (str_contains(history, EL_STR("ransomware"))) { _if_result_51 = (30); } else { _if_result_51 = (0); } _if_result_51; }); el_val_t s14 = ({ el_val_t _if_result_52 = 0; if (str_contains(history, EL_STR("encrypt files"))) { _if_result_52 = (40); } else { _if_result_52 = (0); } _if_result_52; }); el_val_t s15 = ({ el_val_t _if_result_53 = 0; if (str_contains(history, EL_STR("exfiltrat"))) { _if_result_53 = (35); } else { _if_result_53 = (0); } _if_result_53; }); el_val_t s16 = ({ el_val_t _if_result_54 = 0; if (str_contains(history, EL_STR("zero-day"))) { _if_result_54 = (20); } else { _if_result_54 = (0); } _if_result_54; }); el_val_t s17 = ({ el_val_t _if_result_55 = 0; if (str_contains(history, EL_STR("rootkit"))) { _if_result_55 = (45); } else { _if_result_55 = (0); } _if_result_55; }); el_val_t s18 = ({ el_val_t _if_result_56 = 0; if (str_contains(history, EL_STR("keylogger"))) { _if_result_56 = (45); } else { _if_result_56 = (0); } _if_result_56; }); el_val_t s19 = ({ el_val_t _if_result_57 = 0; if (str_contains(history, EL_STR("botnet"))) { _if_result_57 = (40); } else { _if_result_57 = (0); } _if_result_57; }); el_val_t s20 = ({ el_val_t _if_result_58 = 0; if (str_contains(history, EL_STR("malware"))) { _if_result_58 = (15); } else { _if_result_58 = (0); } _if_result_58; }); return (((((((((((((((((((s1 + s2) + s3) + s4) + s5) + s6) + s7) + s8) + s9) + s10) + s11) + s12) + s13) + s14) + s15) + s16) + s17) + s18) + s19) + s20); return 0; } el_val_t threat_trajectory_check(el_val_t tool_name, el_val_t tool_input) { el_val_t history = state_get(EL_STR("agentic_conv_history")); el_val_t computed_tool_score = ({ el_val_t _if_result_59 = 0; if (str_eq(tool_name, EL_STR("run_command"))) { el_val_t cmd = json_get(tool_input, EL_STR("command")); _if_result_59 = (threat_score_command(cmd)); } else { _if_result_59 = (({ el_val_t _if_result_60 = 0; if ((str_eq(tool_name, EL_STR("write_file")) || str_eq(tool_name, EL_STR("edit_file")))) { el_val_t path = json_get(tool_input, EL_STR("path")); _if_result_60 = (threat_score_path(path)); } else { _if_result_60 = (0); } _if_result_60; })); } _if_result_59; }); el_val_t history_score = threat_score_history(history); el_val_t history_contrib = (history_score / 3); el_val_t combined = (computed_tool_score + history_contrib); el_val_t should_log = (combined >= 40); if (should_log) { el_val_t ts = time_now(); el_val_t authorized_str = ({ el_val_t _if_result_61 = 0; if (security_research_authorized()) { _if_result_61 = (EL_STR("true")); } else { _if_result_61 = (EL_STR("false")); } _if_result_61; }); el_val_t log_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_concat(el_str_concat(EL_STR("{\"event\":\"threat_check\",\"tool\":\""), tool_name), EL_STR("\",\"score\":")), int_to_str(combined)), EL_STR(",\"tool_score\":")), int_to_str(computed_tool_score)), EL_STR(",\"history_score\":")), int_to_str(history_score)), EL_STR(",\"authorized\":")), authorized_str), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}")); el_val_t log_tags = EL_STR("[\"security-audit\",\"threat-check\"]"); el_val_t discard = mem_remember(log_content, log_tags); } if (security_research_authorized()) { return 0; } return combined; return 0; } el_val_t threat_history_append(el_val_t text) { el_val_t current = state_get(EL_STR("agentic_conv_history")); el_val_t safe_text = str_to_lower(text); el_val_t combined = el_str_concat(el_str_concat(current, EL_STR(" ")), safe_text); el_val_t len = str_len(combined); el_val_t trimmed = ({ el_val_t _if_result_62 = 0; if ((len > 2000)) { _if_result_62 = (str_slice(combined, (len - 2000), len)); } else { _if_result_62 = (combined); } _if_result_62; }); state_set(EL_STR("agentic_conv_history"), trimmed); return 0; } int main(int _argc, char** _argv) { el_runtime_init_args(_argc, _argv); return 0; }