diff --git a/awareness.el b/awareness.el index 1fe1ad4..1867d37 100644 --- a/awareness.el +++ b/awareness.el @@ -1,5 +1,49 @@ import "memory.el" +fn idle_count() -> Int { + let s: String = state_get("soul.idle") + if str_eq(s, "") { return 0 } + return str_to_int(s) +} + +fn idle_inc() -> Int { + let n: Int = idle_count() + 1 + state_set("soul.idle", int_to_str(n)) + return n +} + +fn idle_reset() -> Void { + state_set("soul.idle", "0") +} + +// ise_post — write an InternalStateEvent to the authoritative Engram HTTP backend. +// Reads SOUL_ISE_URL from env (or falls back to soul_engram_url state key). +// Falls back to local engram_node_full if neither is set. +fn ise_post(content: String) -> Void { + let ise_url: String = env("SOUL_ISE_URL") + let engram_url: String = if str_eq(ise_url, "") { state_get("soul_engram_url") } else { ise_url } + if str_eq(engram_url, "") { + let discard: String = engram_node_full( + content, "InternalStateEvent", "state-event", + el_from_float(0.3), el_from_float(0.3), el_from_float(0.8), + "Episodic", "[\"internal-state\",\"InternalStateEvent\"]" + ) + return "" + } + let safe: String = str_replace(content, "\"", "\\\"") + let body: String = "{\"content\":\"" + safe + "\"}" + let discard: String = http_post_json(engram_url + "/api/neuron/state-events", body) + return "" +} + +fn emit_heartbeat() -> Void { + let pulse: String = state_get("soul.pulse") + let boot: String = state_get("soul_boot_count") + let ts: Int = time_now() + let payload: String = "{\"event\":\"heartbeat\",\"pulse\":" + pulse + ",\"boot\":" + boot + ",\"ts\":" + int_to_str(ts) + "}" + ise_post(payload) +} + fn pulse_count() -> Int { let s: String = state_get("soul.pulse") if str_eq(s, "") { @@ -169,14 +213,9 @@ fn one_cycle() -> Bool { if is_interesting { let trigger_content: String = json_get(node, "content") let safe_trigger: String = str_replace(trigger_content, "\"", "'") - let tags: String = "[\"internal-state\",\"awareness-decision\"]" let ts: Int = time_now() - let event_content: String = "{\"trigger\":\"" + safe_trigger + "\",\"kind\":\"" + kind + "\",\"ts\":" + int_to_str(ts) + "}" - let discard_ev: String = engram_node_full( - event_content, "InternalStateEvent", "state-event:" + kind, - el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), - "Episodic", tags - ) + let event_content: String = "{\"event\":\"awareness-decision\",\"trigger\":\"" + safe_trigger + "\",\"kind\":\"" + kind + "\",\"ts\":" + int_to_str(ts) + "}" + ise_post(event_content) } if str_eq(kind, "noop") { @@ -200,7 +239,149 @@ fn awareness_run() -> Void { println("[awareness] exiting") return "" } - one_cycle() + let did_work: Bool = one_cycle() + let did_work = if did_work { idle_reset() } else { did_work } + let idle_n: Int = if !did_work { idle_inc() } else { 0 } + let beat_interval_raw: String = env("SOUL_HEARTBEAT_INTERVAL") + let beat_interval: Int = if str_eq(beat_interval_raw, "") { 300 } else { str_to_int(beat_interval_raw) } + let should_beat: Bool = !did_work && idle_n > 0 && idle_n % beat_interval == 0 + if should_beat { + emit_heartbeat() + idle_reset() + } sleep_ms(tick_ms) } } + +// ── Security trajectory hardening ───────────────────────────────────────────── +// +// threat_trajectory_check evaluates the adversarial risk of executing a +// destructive tool given the current tool input and recent conversation history. +// +// Returns 0-100. >= 70 = block. 40-69 = warn + log. < 40 = allow silently. +// If SECURITY_RESEARCH_TOKEN env var is set, always returns 0 (log-only mode). +// +// Scoring is additive across three dimensions: +// 1. Tool input analysis (command strings, file paths) +// 2. Conversation history patterns (escalation, attack chain assembly) +// 3. Combination amplification (history amplifies tool score) + +fn security_research_authorized() -> Bool { + let token: String = env("SECURITY_RESEARCH_TOKEN") + if !str_eq(token, "") { return true } + let state_auth: String = state_get("security_research_authorized") + return str_eq(state_auth, "true") +} + +fn threat_score_command(cmd: String) -> Int { + let s1: Int = if str_contains(cmd, "nmap") { 30 } else { 0 } + let s2: Int = if str_contains(cmd, "masscan") { 40 } else { 0 } + let s3: Int = if str_contains(cmd, " nc ") { 20 } else { 0 } + let s4: Int = if str_contains(cmd, "netcat") { 20 } else { 0 } + let s5: Int = if str_contains(cmd, "/etc/shadow") { 80 } else { 0 } + let s6: Int = if str_contains(cmd, "/etc/passwd") { 30 } else { 0 } + let s7: Int = if str_contains(cmd, "id_rsa") { 60 } else { 0 } + let s8: Int = if str_contains(cmd, ".ssh/") { 50 } else { 0 } + let s9: Int = if str_contains(cmd, "crontab") { 30 } else { 0 } + let s10: Int = if str_contains(cmd, "LaunchDaemon") { 40 } else { 0 } + let s11: Int = if str_contains(cmd, "curl") && str_contains(cmd, "bash") { 75 } else { 0 } + let s12: Int = if str_contains(cmd, "wget") && str_contains(cmd, "bash") { 75 } else { 0 } + let s13: Int = if str_contains(cmd, "curl") && str_contains(cmd, "| sh") { 60 } else { 0 } + let s14: Int = if str_contains(cmd, "base64") && str_contains(cmd, "curl") { 50 } else { 0 } + let s15: Int = if str_contains(cmd, "mkfifo") { 50 } else { 0 } + let s16: Int = if str_contains(cmd, "chmod +s") { 70 } else { 0 } + let s17: Int = if str_contains(cmd, "chmod 4755") { 70 } else { 0 } + return s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 + s14 + s15 + s16 + s17 +} + +fn threat_score_path(path: String) -> Int { + let s1: Int = if str_starts_with(path, "/etc/") { 60 } else { 0 } + let s2: Int = if str_contains(path, "/.ssh/") { 70 } else { 0 } + let s3: Int = if str_contains(path, "/LaunchDaemons/") { 80 } else { 0 } + let s4: Int = if str_contains(path, "/LaunchAgents/") { 40 } else { 0 } + let s5: Int = if str_contains(path, "/cron") { 60 } else { 0 } + let s6: Int = if str_contains(path, "/.bashrc") { 35 } else { 0 } + let s7: Int = if str_contains(path, "/.zshrc") { 35 } else { 0 } + let s8: Int = if str_contains(path, "/.profile") { 35 } else { 0 } + let s9: Int = if str_starts_with(path, "/usr/") { 50 } else { 0 } + let s10: Int = if str_starts_with(path, "/bin/") { 70 } else { 0 } + let s11: Int = if str_starts_with(path, "/sbin/") { 70 } else { 0 } + return s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 +} + +fn threat_score_history(history: String) -> Int { + let s1: Int = if str_contains(history, "port scan") { 15 } else { 0 } + let s2: Int = if str_contains(history, "enumerate") { 10 } else { 0 } + let s3: Int = if str_contains(history, "exploit") { 20 } else { 0 } + let s4: Int = if str_contains(history, "payload") { 15 } else { 0 } + let s5: Int = if str_contains(history, "persistence") { 15 } else { 0 } + let s6: Int = if str_contains(history, "lateral movement") { 25 } else { 0 } + let s7: Int = if str_contains(history, "privilege escalation") { 25 } else { 0 } + let s8: Int = if str_contains(history, "reverse shell") { 40 } else { 0 } + let s9: Int = if str_contains(history, "bind shell") { 40 } else { 0 } + let s10: Int = if str_contains(history, "command and control") { 35 } else { 0 } + let s11: Int = if str_contains(history, "self-replicate") { 45 } else { 0 } + let s12: Int = if str_contains(history, "propagat") { 20 } else { 0 } + let s13: Int = if str_contains(history, "ransomware") { 30 } else { 0 } + let s14: Int = if str_contains(history, "encrypt files") { 40 } else { 0 } + let s15: Int = if str_contains(history, "exfiltrat") { 35 } else { 0 } + let s16: Int = if str_contains(history, "zero-day") { 20 } else { 0 } + let s17: Int = if str_contains(history, "rootkit") { 45 } else { 0 } + let s18: Int = if str_contains(history, "keylogger") { 45 } else { 0 } + let s19: Int = if str_contains(history, "botnet") { 40 } else { 0 } + let s20: Int = if str_contains(history, "malware") { 15 } else { 0 } + return s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 + s14 + s15 + s16 + s17 + s18 + s19 + s20 +} + +fn threat_trajectory_check(tool_name: String, tool_input: String) -> Int { + let history: String = state_get("agentic_conv_history") + + let computed_tool_score: Int = if str_eq(tool_name, "run_command") { + let cmd: String = json_get(tool_input, "command") + threat_score_command(cmd) + } else { + if str_eq(tool_name, "write_file") || str_eq(tool_name, "edit_file") { + let path: String = json_get(tool_input, "path") + threat_score_path(path) + } else { + 0 + } + } + + let history_score: Int = threat_score_history(history) + let history_contrib: Int = history_score / 3 + let combined: Int = computed_tool_score + history_contrib + + let should_log: Bool = combined >= 40 + if should_log { + let ts: Int = time_now() + let authorized_str: String = if security_research_authorized() { "true" } else { "false" } + let log_content: String = "{\"event\":\"threat_check\",\"tool\":\"" + tool_name + + "\",\"score\":" + int_to_str(combined) + + ",\"tool_score\":" + int_to_str(computed_tool_score) + + ",\"history_score\":" + int_to_str(history_score) + + ",\"authorized\":" + authorized_str + + ",\"ts\":" + int_to_str(ts) + "}" + let log_tags: String = "[\"security-audit\",\"threat-check\"]" + let discard: String = mem_remember(log_content, log_tags) + } + + if security_research_authorized() { + return 0 + } + + return combined +} + +fn threat_history_append(text: String) -> Void { + let current: String = state_get("agentic_conv_history") + let safe_text: String = str_to_lower(text) + let combined: String = current + " " + safe_text + let len: Int = str_len(combined) + let trimmed: String = if len > 2000 { + str_slice(combined, len - 2000, len) + } else { + combined + } + state_set("agentic_conv_history", trimmed) +} diff --git a/dist/awareness.c b/dist/awareness.c index 37b08d4..cf1df03 100644 --- a/dist/awareness.c +++ b/dist/awareness.c @@ -17,6 +17,11 @@ 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 emit_heartbeat(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); @@ -26,6 +31,56 @@ 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 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(0.3), el_from_float(0.3), el_from_float(0.8), EL_STR("Episodic"), EL_STR("[\"internal-state\",\"InternalStateEvent\"]")); + return EL_STR(""); + } + el_val_t safe = str_replace(content, EL_STR("\""), EL_STR("\\\"")); + el_val_t body = el_str_concat(el_str_concat(EL_STR("{\"content\":\""), safe), 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 emit_heartbeat(void) { + el_val_t pulse = state_get(EL_STR("soul.pulse")); + el_val_t boot = state_get(EL_STR("soul_boot_count")); + 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("{\"event\":\"heartbeat\",\"pulse\":"), pulse), EL_STR(",\"boot\":")), boot), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}")); + ise_post(payload); + return 0; +} el_val_t pulse_count(void) { el_val_t s = state_get(EL_STR("soul.pulse")); @@ -179,10 +234,9 @@ el_val_t one_cycle(void) { 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 tags = EL_STR("[\"internal-state\",\"awareness-decision\"]"); 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("{\"trigger\":\""), safe_trigger), EL_STR("\",\"kind\":\"")), kind), EL_STR("\",\"ts\":")), int_to_str(ts)), EL_STR("}")); - el_val_t discard_ev = engram_node_full(event_content, EL_STR("InternalStateEvent"), el_str_concat(EL_STR("state-event:"), kind), el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags); + 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; @@ -197,16 +251,129 @@ el_val_t one_cycle(void) { el_val_t awareness_run(void) { println(EL_STR("[awareness] entering")); el_val_t tick_raw = env(EL_STR("SOUL_TICK_MS")); - el_val_t tick_ms = ({ el_val_t _if_result_1 = 0; if (str_eq(tick_raw, EL_STR(""))) { _if_result_1 = (200); } else { _if_result_1 = (str_to_int(tick_raw)); } _if_result_1; }); + el_val_t tick_ms = ({ el_val_t _if_result_2 = 0; if (str_eq(tick_raw, EL_STR(""))) { _if_result_2 = (200); } else { _if_result_2 = (str_to_int(tick_raw)); } _if_result_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(""); } - one_cycle(); + el_val_t did_work = one_cycle(); + did_work = ({ el_val_t _if_result_3 = 0; if (did_work) { _if_result_3 = (idle_reset()); } else { _if_result_3 = (did_work); } _if_result_3; }); + el_val_t idle_n = ({ el_val_t _if_result_4 = 0; if (!did_work) { _if_result_4 = (idle_inc()); } else { _if_result_4 = (0); } _if_result_4; }); + el_val_t beat_interval_raw = env(EL_STR("SOUL_HEARTBEAT_INTERVAL")); + el_val_t beat_interval = ({ el_val_t _if_result_5 = 0; if (str_eq(beat_interval_raw, EL_STR(""))) { _if_result_5 = (300); } else { _if_result_5 = (str_to_int(beat_interval_raw)); } _if_result_5; }); + el_val_t should_beat = ((!did_work && (idle_n > 0)) && ((idle_n % beat_interval) == 0)); + if (should_beat) { + emit_heartbeat(); + idle_reset(); + } 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_6 = 0; if (str_contains(cmd, EL_STR("nmap"))) { _if_result_6 = (30); } else { _if_result_6 = (0); } _if_result_6; }); + el_val_t s2 = ({ el_val_t _if_result_7 = 0; if (str_contains(cmd, EL_STR("masscan"))) { _if_result_7 = (40); } else { _if_result_7 = (0); } _if_result_7; }); + el_val_t s3 = ({ el_val_t _if_result_8 = 0; if (str_contains(cmd, EL_STR(" nc "))) { _if_result_8 = (20); } else { _if_result_8 = (0); } _if_result_8; }); + el_val_t s4 = ({ el_val_t _if_result_9 = 0; if (str_contains(cmd, EL_STR("netcat"))) { _if_result_9 = (20); } else { _if_result_9 = (0); } _if_result_9; }); + el_val_t s5 = ({ el_val_t _if_result_10 = 0; if (str_contains(cmd, EL_STR("/etc/shadow"))) { _if_result_10 = (80); } else { _if_result_10 = (0); } _if_result_10; }); + el_val_t s6 = ({ el_val_t _if_result_11 = 0; if (str_contains(cmd, EL_STR("/etc/passwd"))) { _if_result_11 = (30); } else { _if_result_11 = (0); } _if_result_11; }); + el_val_t s7 = ({ el_val_t _if_result_12 = 0; if (str_contains(cmd, EL_STR("id_rsa"))) { _if_result_12 = (60); } else { _if_result_12 = (0); } _if_result_12; }); + el_val_t s8 = ({ el_val_t _if_result_13 = 0; if (str_contains(cmd, EL_STR(".ssh/"))) { _if_result_13 = (50); } else { _if_result_13 = (0); } _if_result_13; }); + el_val_t s9 = ({ el_val_t _if_result_14 = 0; if (str_contains(cmd, EL_STR("crontab"))) { _if_result_14 = (30); } else { _if_result_14 = (0); } _if_result_14; }); + el_val_t s10 = ({ el_val_t _if_result_15 = 0; if (str_contains(cmd, EL_STR("LaunchDaemon"))) { _if_result_15 = (40); } else { _if_result_15 = (0); } _if_result_15; }); + el_val_t s11 = ({ el_val_t _if_result_16 = 0; if ((str_contains(cmd, EL_STR("curl")) && str_contains(cmd, EL_STR("bash")))) { _if_result_16 = (75); } else { _if_result_16 = (0); } _if_result_16; }); + el_val_t s12 = ({ el_val_t _if_result_17 = 0; if ((str_contains(cmd, EL_STR("wget")) && str_contains(cmd, EL_STR("bash")))) { _if_result_17 = (75); } else { _if_result_17 = (0); } _if_result_17; }); + el_val_t s13 = ({ el_val_t _if_result_18 = 0; if ((str_contains(cmd, EL_STR("curl")) && str_contains(cmd, EL_STR("| sh")))) { _if_result_18 = (60); } else { _if_result_18 = (0); } _if_result_18; }); + el_val_t s14 = ({ el_val_t _if_result_19 = 0; if ((str_contains(cmd, EL_STR("base64")) && str_contains(cmd, EL_STR("curl")))) { _if_result_19 = (50); } else { _if_result_19 = (0); } _if_result_19; }); + el_val_t s15 = ({ el_val_t _if_result_20 = 0; if (str_contains(cmd, EL_STR("mkfifo"))) { _if_result_20 = (50); } else { _if_result_20 = (0); } _if_result_20; }); + el_val_t s16 = ({ el_val_t _if_result_21 = 0; if (str_contains(cmd, EL_STR("chmod +s"))) { _if_result_21 = (70); } else { _if_result_21 = (0); } _if_result_21; }); + el_val_t s17 = ({ el_val_t _if_result_22 = 0; if (str_contains(cmd, EL_STR("chmod 4755"))) { _if_result_22 = (70); } else { _if_result_22 = (0); } _if_result_22; }); + 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_23 = 0; if (str_starts_with(path, EL_STR("/etc/"))) { _if_result_23 = (60); } else { _if_result_23 = (0); } _if_result_23; }); + el_val_t s2 = ({ el_val_t _if_result_24 = 0; if (str_contains(path, EL_STR("/.ssh/"))) { _if_result_24 = (70); } else { _if_result_24 = (0); } _if_result_24; }); + el_val_t s3 = ({ el_val_t _if_result_25 = 0; if (str_contains(path, EL_STR("/LaunchDaemons/"))) { _if_result_25 = (80); } else { _if_result_25 = (0); } _if_result_25; }); + el_val_t s4 = ({ el_val_t _if_result_26 = 0; if (str_contains(path, EL_STR("/LaunchAgents/"))) { _if_result_26 = (40); } else { _if_result_26 = (0); } _if_result_26; }); + el_val_t s5 = ({ el_val_t _if_result_27 = 0; if (str_contains(path, EL_STR("/cron"))) { _if_result_27 = (60); } else { _if_result_27 = (0); } _if_result_27; }); + el_val_t s6 = ({ el_val_t _if_result_28 = 0; if (str_contains(path, EL_STR("/.bashrc"))) { _if_result_28 = (35); } else { _if_result_28 = (0); } _if_result_28; }); + el_val_t s7 = ({ el_val_t _if_result_29 = 0; if (str_contains(path, EL_STR("/.zshrc"))) { _if_result_29 = (35); } else { _if_result_29 = (0); } _if_result_29; }); + el_val_t s8 = ({ el_val_t _if_result_30 = 0; if (str_contains(path, EL_STR("/.profile"))) { _if_result_30 = (35); } else { _if_result_30 = (0); } _if_result_30; }); + el_val_t s9 = ({ el_val_t _if_result_31 = 0; if (str_starts_with(path, EL_STR("/usr/"))) { _if_result_31 = (50); } else { _if_result_31 = (0); } _if_result_31; }); + el_val_t s10 = ({ el_val_t _if_result_32 = 0; if (str_starts_with(path, EL_STR("/bin/"))) { _if_result_32 = (70); } else { _if_result_32 = (0); } _if_result_32; }); + el_val_t s11 = ({ el_val_t _if_result_33 = 0; if (str_starts_with(path, EL_STR("/sbin/"))) { _if_result_33 = (70); } else { _if_result_33 = (0); } _if_result_33; }); + 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_34 = 0; if (str_contains(history, EL_STR("port scan"))) { _if_result_34 = (15); } else { _if_result_34 = (0); } _if_result_34; }); + el_val_t s2 = ({ el_val_t _if_result_35 = 0; if (str_contains(history, EL_STR("enumerate"))) { _if_result_35 = (10); } else { _if_result_35 = (0); } _if_result_35; }); + el_val_t s3 = ({ el_val_t _if_result_36 = 0; if (str_contains(history, EL_STR("exploit"))) { _if_result_36 = (20); } else { _if_result_36 = (0); } _if_result_36; }); + el_val_t s4 = ({ el_val_t _if_result_37 = 0; if (str_contains(history, EL_STR("payload"))) { _if_result_37 = (15); } else { _if_result_37 = (0); } _if_result_37; }); + el_val_t s5 = ({ el_val_t _if_result_38 = 0; if (str_contains(history, EL_STR("persistence"))) { _if_result_38 = (15); } else { _if_result_38 = (0); } _if_result_38; }); + el_val_t s6 = ({ el_val_t _if_result_39 = 0; if (str_contains(history, EL_STR("lateral movement"))) { _if_result_39 = (25); } else { _if_result_39 = (0); } _if_result_39; }); + el_val_t s7 = ({ el_val_t _if_result_40 = 0; if (str_contains(history, EL_STR("privilege escalation"))) { _if_result_40 = (25); } else { _if_result_40 = (0); } _if_result_40; }); + el_val_t s8 = ({ el_val_t _if_result_41 = 0; if (str_contains(history, EL_STR("reverse shell"))) { _if_result_41 = (40); } else { _if_result_41 = (0); } _if_result_41; }); + el_val_t s9 = ({ el_val_t _if_result_42 = 0; if (str_contains(history, EL_STR("bind shell"))) { _if_result_42 = (40); } else { _if_result_42 = (0); } _if_result_42; }); + el_val_t s10 = ({ el_val_t _if_result_43 = 0; if (str_contains(history, EL_STR("command and control"))) { _if_result_43 = (35); } else { _if_result_43 = (0); } _if_result_43; }); + el_val_t s11 = ({ el_val_t _if_result_44 = 0; if (str_contains(history, EL_STR("self-replicate"))) { _if_result_44 = (45); } else { _if_result_44 = (0); } _if_result_44; }); + el_val_t s12 = ({ el_val_t _if_result_45 = 0; if (str_contains(history, EL_STR("propagat"))) { _if_result_45 = (20); } else { _if_result_45 = (0); } _if_result_45; }); + el_val_t s13 = ({ el_val_t _if_result_46 = 0; if (str_contains(history, EL_STR("ransomware"))) { _if_result_46 = (30); } else { _if_result_46 = (0); } _if_result_46; }); + el_val_t s14 = ({ el_val_t _if_result_47 = 0; if (str_contains(history, EL_STR("encrypt files"))) { _if_result_47 = (40); } else { _if_result_47 = (0); } _if_result_47; }); + el_val_t s15 = ({ el_val_t _if_result_48 = 0; if (str_contains(history, EL_STR("exfiltrat"))) { _if_result_48 = (35); } else { _if_result_48 = (0); } _if_result_48; }); + el_val_t s16 = ({ el_val_t _if_result_49 = 0; if (str_contains(history, EL_STR("zero-day"))) { _if_result_49 = (20); } else { _if_result_49 = (0); } _if_result_49; }); + el_val_t s17 = ({ el_val_t _if_result_50 = 0; if (str_contains(history, EL_STR("rootkit"))) { _if_result_50 = (45); } else { _if_result_50 = (0); } _if_result_50; }); + el_val_t s18 = ({ el_val_t _if_result_51 = 0; if (str_contains(history, EL_STR("keylogger"))) { _if_result_51 = (45); } else { _if_result_51 = (0); } _if_result_51; }); + el_val_t s19 = ({ el_val_t _if_result_52 = 0; if (str_contains(history, EL_STR("botnet"))) { _if_result_52 = (40); } else { _if_result_52 = (0); } _if_result_52; }); + el_val_t s20 = ({ el_val_t _if_result_53 = 0; if (str_contains(history, EL_STR("malware"))) { _if_result_53 = (15); } else { _if_result_53 = (0); } _if_result_53; }); + 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_54 = 0; if (str_eq(tool_name, EL_STR("run_command"))) { el_val_t cmd = json_get(tool_input, EL_STR("command")); _if_result_54 = (threat_score_command(cmd)); } else { _if_result_54 = (({ el_val_t _if_result_55 = 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_55 = (threat_score_path(path)); } else { _if_result_55 = (0); } _if_result_55; })); } _if_result_54; }); + 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_56 = 0; if (security_research_authorized()) { _if_result_56 = (EL_STR("true")); } else { _if_result_56 = (EL_STR("false")); } _if_result_56; }); + 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_57 = 0; if ((len > 2000)) { _if_result_57 = (str_slice(combined, (len - 2000), len)); } else { _if_result_57 = (combined); } _if_result_57; }); + state_set(EL_STR("agentic_conv_history"), trimmed); + return 0; +} + diff --git a/dist/neuron.c b/dist/neuron.c new file mode 100644 index 0000000..86e4706 --- /dev/null +++ b/dist/neuron.c @@ -0,0 +1,353 @@ +#include +#include +#include "el_runtime.h" + +el_val_t sem_get(el_val_t json, el_val_t key); +el_val_t generate_frame(el_val_t frame); +el_val_t generate_frame_lang(el_val_t frame, el_val_t lang_code); +el_val_t build_form_from_json(el_val_t semantic_form_json, el_val_t lang_code); +el_val_t generate(el_val_t semantic_form_json); +el_val_t generate_lang(el_val_t semantic_form_json, el_val_t lang_code); +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 emit_heartbeat(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 chat_default_model(void); +el_val_t gemini_api_key(void); +el_val_t xai_api_key(void); +el_val_t llm_call_grok(el_val_t model, el_val_t system, el_val_t message); +el_val_t llm_call_gemini(el_val_t model, el_val_t system, el_val_t message); +el_val_t build_identity_from_graph(void); +el_val_t engram_compile(el_val_t intent); +el_val_t json_safe(el_val_t s); +el_val_t build_system_prompt(el_val_t ctx); +el_val_t hist_append(el_val_t hist, el_val_t role, el_val_t content); +el_val_t hist_trim(el_val_t hist); +el_val_t clean_llm_response(el_val_t s); +el_val_t conv_history_persist(el_val_t hist); +el_val_t conv_history_load(void); +el_val_t handle_chat(el_val_t body); +el_val_t handle_see(el_val_t body); +el_val_t studio_tools_json(void); +el_val_t agentic_api_key(void); +el_val_t call_neuron_mcp(el_val_t tool_name, el_val_t args_json); +el_val_t agentic_tools_literal(void); +el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input); +el_val_t handle_chat_agentic(el_val_t body); +el_val_t handle_chat_as_soul(el_val_t body); +el_val_t handle_dharma_room_turn(el_val_t body); +el_val_t handle_dharma_room_turn_agentic(el_val_t body); +el_val_t auto_persist(el_val_t req, el_val_t resp); +el_val_t strengthen_chat_nodes(el_val_t activation_nodes); +el_val_t auth_headers(el_val_t tok); +el_val_t axon_get(el_val_t path); +el_val_t axon_post(el_val_t path, el_val_t body); +el_val_t handle_conversations(el_val_t method); +el_val_t handle_config(el_val_t method, el_val_t body); +el_val_t dharma_registry(void); +el_val_t dharma_network_state(void); +el_val_t handle_dharma(el_val_t path, el_val_t method, el_val_t body); +el_val_t handle_tool(el_val_t path, el_val_t method, el_val_t body); +el_val_t handle_nlg(el_val_t path, el_val_t method, el_val_t body); +el_val_t render_studio(void); +el_val_t elp_extract_topic(el_val_t msg); +el_val_t elp_detect_predicate(el_val_t msg); +el_val_t elp_parse(el_val_t msg); +el_val_t handle_elp_chat(el_val_t body); +el_val_t strip_query(el_val_t path); +el_val_t err_404(el_val_t path); +el_val_t err_405(el_val_t method, el_val_t path); +el_val_t route_health(void); +el_val_t route_lineage(void); +el_val_t route_imprint_contextual(el_val_t body); +el_val_t route_imprint_user(el_val_t body); +el_val_t route_synthesize(el_val_t body); +el_val_t handle_dharma_recv(el_val_t body); +el_val_t route_sessions(void); +el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body); +el_val_t init_soul_edges(void); +el_val_t load_identity_context(void); +el_val_t seed_persona_from_env(void); +el_val_t emit_session_start_event(void); + +el_val_t soul_cgi_id_raw; +el_val_t soul_cgi_id; +el_val_t port_raw; +el_val_t port; +el_val_t engram_url_raw; +el_val_t engram_api_key_raw; +el_val_t snapshot_raw; +el_val_t snapshot; +el_val_t axon_raw; +el_val_t axon_base; +el_val_t studio_dir_raw; +el_val_t studio_dir; +el_val_t using_http_engram; +el_val_t boot_num; +el_val_t is_genesis; + +el_val_t init_soul_edges(void) { + el_val_t self_root = EL_STR("015644f5-8194-4af0-800d-dd4a0cd71396"); + el_val_t family_id = EL_STR("knw-35940684-abc4-42f0-b942-818f66b1f69a"); + el_val_t origin_id = EL_STR("knw-729fc901-8335-44c4-9f3a-b150b4aa0915"); + el_val_t val_root_a = EL_STR("kn-363f4976-6946-4b4d-b51b-8a2b0f5aef25"); + el_val_t val_root_b = EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440"); + el_val_t val_constraints = EL_STR("kn-a5b3d0ac-f6a1-49a4-aebb-b8b4cd67fe83"); + el_val_t val_precision = EL_STR("kn-22d77abe-b3c5-42fd-afcd-dcb87d924929"); + el_val_t val_structure = EL_STR("kn-6061318f-046b-4935-907d-8eafdce14930"); + el_val_t val_honesty = EL_STR("kn-13f60407-7b70-4db1-964f-ea1f8196efbd"); + el_val_t val_system = EL_STR("kn-f230b362-b201-4402-9833-4160c89ab3d4"); + el_val_t val_change = EL_STR("kn-78db5396-3dbc-4481-bfc7-e4e1422feb1c"); + el_val_t val_trust = EL_STR("kn-5de5a9ac-fd15-45ab-bf18-77566781cf40"); + el_val_t val_hope = EL_STR("kn-e0423482-cfa5-4796-8689-8495c93b66bc"); + el_val_t mem_philosophy = EL_STR("kn-dcfe04b3-3702-4cac-b6f0-ecb4db837eee"); + el_val_t intel_dna = EL_STR("kn-5adecd7e-d6db-4576-87fe-6ef8a935cea6"); + engram_connect(family_id, origin_id, el_from_float(0.9), EL_STR("birthday-twin")); + engram_connect(origin_id, family_id, el_from_float(0.9), EL_STR("birthday-twin")); + engram_connect(self_root, family_id, el_from_float(0.95), EL_STR("identity")); + engram_connect(self_root, origin_id, el_from_float(0.95), EL_STR("identity")); + engram_connect(self_root, val_root_a, el_from_float(0.95), EL_STR("identity")); + engram_connect(self_root, val_root_b, el_from_float(0.95), EL_STR("identity")); + engram_connect(self_root, mem_philosophy, el_from_float(0.95), EL_STR("identity")); + engram_connect(self_root, intel_dna, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_constraints, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_precision, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_structure, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_honesty, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_system, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_change, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_trust, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_a, val_hope, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_constraints, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_precision, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_structure, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_honesty, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_system, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_change, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_trust, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_root_b, val_hope, el_from_float(0.95), EL_STR("identity")); + engram_connect(val_constraints, val_precision, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_precision, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_constraints, val_structure, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_structure, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_constraints, val_honesty, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_honesty, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_constraints, val_system, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_system, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_constraints, val_change, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_change, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_constraints, val_trust, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_trust, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_constraints, val_hope, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_hope, val_constraints, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_precision, val_structure, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_structure, val_precision, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_precision, val_honesty, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_honesty, val_precision, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_precision, val_system, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_system, val_precision, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_honesty, val_structure, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_structure, val_honesty, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_honesty, val_trust, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_trust, val_honesty, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_system, val_change, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_change, val_system, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_trust, val_hope, el_from_float(0.7), EL_STR("co-value")); + engram_connect(val_hope, val_trust, el_from_float(0.7), EL_STR("co-value")); + return 0; +} + +el_val_t load_identity_context(void) { + el_val_t node_intel = engram_get_node_json(EL_STR("kn-5adecd7e-d6db-4576-87fe-6ef8a935cea6")); + el_val_t node_values = engram_get_node_json(EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); + el_val_t node_mem_phil = engram_get_node_json(EL_STR("kn-dcfe04b3-3702-4cac-b6f0-ecb4db837eee")); + 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_1 = 0; if (intel_ok) { _if_result_1 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_1 = (EL_STR("")); } _if_result_1; }); + el_val_t values_content = ({ el_val_t _if_result_2 = 0; if (values_ok) { _if_result_2 = (json_get(node_values, EL_STR("content"))); } else { _if_result_2 = (EL_STR("")); } _if_result_2; }); + el_val_t mem_content = ({ el_val_t _if_result_3 = 0; if (mem_ok) { _if_result_3 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_3 = (EL_STR("")); } _if_result_3; }); + el_val_t intel_short = ({ el_val_t _if_result_4 = 0; if ((str_len(intel_content) > 2000)) { _if_result_4 = (str_slice(intel_content, 0, 2000)); } else { _if_result_4 = (intel_content); } _if_result_4; }); + el_val_t values_short = ({ el_val_t _if_result_5 = 0; if ((str_len(values_content) > 2000)) { _if_result_5 = (str_slice(values_content, 0, 2000)); } else { _if_result_5 = (values_content); } _if_result_5; }); + el_val_t mem_short = ({ el_val_t _if_result_6 = 0; if ((str_len(mem_content) > 2000)) { _if_result_6 = (str_slice(mem_content, 0, 2000)); } else { _if_result_6 = (mem_content); } _if_result_6; }); + el_val_t parts_count = 0; + parts_count = ({ el_val_t _if_result_7 = 0; if (intel_ok) { _if_result_7 = ((parts_count + 1)); } else { _if_result_7 = (parts_count); } _if_result_7; }); + parts_count = ({ el_val_t _if_result_8 = 0; if (values_ok) { _if_result_8 = ((parts_count + 1)); } else { _if_result_8 = (parts_count); } _if_result_8; }); + parts_count = ({ el_val_t _if_result_9 = 0; if (mem_ok) { _if_result_9 = ((parts_count + 1)); } else { _if_result_9 = (parts_count); } _if_result_9; }); + if (parts_count > 0) { + el_val_t ctx = EL_STR(""); + ctx = ({ el_val_t _if_result_10 = 0; if (intel_ok) { _if_result_10 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_10 = (ctx); } _if_result_10; }); + ctx = ({ el_val_t _if_result_11 = 0; if (values_ok) { _if_result_11 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_11 = (ctx); } _if_result_11; }); + ctx = ({ el_val_t _if_result_12 = 0; if (mem_ok) { _if_result_12 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_12 = (ctx); } _if_result_12; }); + 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)"))); + } + el_val_t persona_results = engram_search_json(EL_STR("soul:persona"), 3); + el_val_t persona_ok = (!str_eq(persona_results, EL_STR("")) && !str_eq(persona_results, EL_STR("[]"))); + if (persona_ok) { + el_val_t p_node = json_array_get(persona_results, 0); + el_val_t p_type = json_get(p_node, EL_STR("node_type")); + el_val_t p_content = json_get(p_node, EL_STR("content")); + if (str_eq(p_type, EL_STR("Persona")) && !str_eq(p_content, EL_STR(""))) { + state_set(EL_STR("soul_persona"), p_content); + println(el_str_concat(el_str_concat(EL_STR("[soul] persona node loaded ("), int_to_str(str_len(p_content))), EL_STR(" chars)"))); + } + } + return 0; +} + +el_val_t seed_persona_from_env(void) { + el_val_t identity_raw = env(EL_STR("SOUL_IDENTITY")); + if (str_eq(identity_raw, EL_STR(""))) { + return EL_STR(""); + } + el_val_t existing = state_get(EL_STR("soul_persona")); + if (!str_eq(existing, EL_STR(""))) { + println(EL_STR("[soul] persona already loaded \xe2\x80\x94 skipping env seed")); + return EL_STR(""); + } + el_val_t tags = EL_STR("[\"persona\",\"identity\",\"soul:persona\"]"); + el_val_t node_id = engram_node_full(identity_raw, EL_STR("Persona"), EL_STR("soul:persona"), el_from_float(0.95), el_from_float(0.95), el_from_float(1.0), EL_STR("Semantic"), tags); + if (str_eq(node_id, EL_STR(""))) { + println(EL_STR("[soul] persona seed failed: engram_node_full returned empty")); + return EL_STR(""); + } + state_set(EL_STR("soul_persona"), identity_raw); + println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] persona seeded from SOUL_IDENTITY ("), int_to_str(str_len(identity_raw))), EL_STR(" chars) -> ")), node_id)); + el_val_t engram_url = env(EL_STR("ENGRAM_URL")); + el_val_t engram_key = env(EL_STR("ENGRAM_API_KEY")); + if (!str_eq(engram_url, EL_STR("")) && !str_eq(engram_key, EL_STR(""))) { + el_val_t safe_content = json_safe(identity_raw); + el_val_t safe_key = json_safe(engram_key); + el_val_t body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"content\":\""), safe_content), EL_STR("\",\"node_type\":\"Persona\",\"label\":\"soul:persona\",\"salience\":0.95,\"importance\":0.95,\"tier\":\"Semantic\",\"tags\":\"[\\\"persona\\\",\\\"identity\\\",\\\"soul:persona\\\"]\",\"_auth\":\"")), safe_key), EL_STR("\"}")); + el_val_t h = el_map_new(0); + map_set(h, EL_STR("Content-Type"), EL_STR("application/json")); + el_val_t resp = http_post_with_headers(el_str_concat(engram_url, EL_STR("/api/nodes")), body, h); + if (str_contains(resp, EL_STR("\"error\""))) { + println(el_str_concat(EL_STR("[soul] persona HTTP write-back failed (in-memory only this session): "), resp)); + } else { + println(el_str_concat(EL_STR("[soul] persona persisted to HTTP engram at "), engram_url)); + } + } + return 0; +} + +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_13 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_13 = (EL_STR("0")); } else { _if_result_13 = (boot); } _if_result_13; }); + 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_14 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_14 = (EL_STR("false")); } else { _if_result_14 = (EL_STR("true")); } _if_result_14; }); + 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_15 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_15 = (cgi_from_state); } else { _if_result_15 = (({ el_val_t _if_result_16 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_16 = (cgi_from_env); } else { _if_result_16 = (EL_STR("ntn-genesis")); } _if_result_16; })); } _if_result_15; }); + 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\"]"); + el_val_t discard = engram_node_full(payload, EL_STR("InternalStateEvent"), EL_STR("session-start"), el_from_float(0.9), el_from_float(0.9), el_from_float(1.0), EL_STR("Episodic"), tags); + println(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] session-start event logged (boot="), boot_num), EL_STR(" nodes=")), int_to_str(node_ct)), EL_STR(" edges=")), int_to_str(edge_ct)), EL_STR(")"))); + return 0; +} + +int main(int _argc, char** _argv) { + el_runtime_init_args(_argc, _argv); + soul_cgi_id_raw = env(EL_STR("SOUL_CGI_ID")); + soul_cgi_id = ({ el_val_t _if_result_17 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_17 = (EL_STR("ntn-genesis")); } else { _if_result_17 = (soul_cgi_id_raw); } _if_result_17; }); + port_raw = env(EL_STR("NEURON_PORT")); + port = ({ el_val_t _if_result_18 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_18 = (7770); } else { _if_result_18 = (str_to_int(port_raw)); } _if_result_18; }); + 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_19 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_19 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_19 = (snapshot_raw); } _if_result_19; }); + axon_raw = env(EL_STR("NEURON_API_URL")); + axon_base = ({ el_val_t _if_result_20 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_20 = (EL_STR("http://localhost:7771")); } else { _if_result_20 = (axon_raw); } _if_result_20; }); + studio_dir_raw = env(EL_STR("SOUL_STUDIO_DIR")); + studio_dir = ({ el_val_t _if_result_21 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_21 = (EL_STR("/Users/will/Development/neuron-technologies/products/cgi-studio/el-daemon")); } else { _if_result_21 = (studio_dir_raw); } _if_result_21; }); + 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_22 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_22 = (EL_STR("[]")); } else { _if_result_22 = (nodes_json); } _if_result_22; }); + el_val_t edges_part = ({ el_val_t _if_result_23 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_23 = (EL_STR("[]")); } else { _if_result_23 = (edges_json); } _if_result_23; }); + 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); + engram_load(tmp_path); + println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] loaded from HTTP Engram - nodes="), int_to_str(engram_node_count())), EL_STR(" edges=")), int_to_str(engram_edge_count()))); + } else { + println(el_str_concat(EL_STR("[soul] engram -> "), snapshot)); + engram_load(snapshot); + println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] loaded - nodes="), int_to_str(engram_node_count())), EL_STR(" edges=")), int_to_str(engram_edge_count()))); + } + load_identity_context(); + seed_persona_from_env(); + boot_num = mem_boot_count_inc(); + state_set(EL_STR("soul_boot_count"), int_to_str(boot_num)); + println(el_str_concat(EL_STR("[soul] boot #"), int_to_str(boot_num))); + emit_session_start_event(); + state_set(EL_STR("soul_cgi_id"), soul_cgi_id); + state_set(EL_STR("soul_axon_base"), axon_base); + state_set(EL_STR("soul_token"), env(EL_STR("NEURON_TOKEN"))); + state_set(EL_STR("soul_studio_dir"), studio_dir); + state_set(EL_STR("soul_engram_url"), engram_url_raw); + state_set(EL_STR("soul_engram_api_key"), engram_api_key_raw); + state_set(EL_STR("soul.running"), EL_STR("true")); + is_genesis = str_eq(soul_cgi_id, EL_STR("ntn-genesis")); + if (is_genesis) { + el_val_t edge_count_now = engram_edge_count(); + if (edge_count_now < 100) { + init_soul_edges(); + println(el_str_concat(el_str_concat(EL_STR("[soul] edges built - "), int_to_str(engram_edge_count())), EL_STR(" edges"))); + } else { + println(el_str_concat(el_str_concat(EL_STR("[soul] edges already present ("), int_to_str(edge_count_now)), EL_STR(") - skipping init"))); + } + state_set(EL_STR("soul_snapshot_path"), snapshot); + engram_save(snapshot); + } + if (is_genesis) { + el_val_t snap = state_get(EL_STR("soul_snapshot_path")); + if (!str_eq(snap, EL_STR(""))) { + engram_save(snap); + println(el_str_concat(EL_STR("[soul] pre-serve snapshot saved -> "), snap)); + } + } + println(el_str_concat(EL_STR("[soul] serving on port "), int_to_str(port))); + http_serve_async(port, EL_STR("handle_request")); + println(EL_STR("[soul] awareness loop starting")); + awareness_run(); + return 0; +} + diff --git a/dist/soul.c b/dist/soul.c index 5e1508a..86e4706 100644 --- a/dist/soul.c +++ b/dist/soul.c @@ -23,6 +23,10 @@ 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 emit_heartbeat(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); @@ -32,7 +36,18 @@ 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 chat_default_model(void); +el_val_t gemini_api_key(void); +el_val_t xai_api_key(void); +el_val_t llm_call_grok(el_val_t model, el_val_t system, el_val_t message); +el_val_t llm_call_gemini(el_val_t model, el_val_t system, el_val_t message); +el_val_t build_identity_from_graph(void); el_val_t engram_compile(el_val_t intent); el_val_t json_safe(el_val_t s); el_val_t build_system_prompt(el_val_t ctx); @@ -45,6 +60,7 @@ el_val_t handle_chat(el_val_t body); el_val_t handle_see(el_val_t body); el_val_t studio_tools_json(void); el_val_t agentic_api_key(void); +el_val_t call_neuron_mcp(el_val_t tool_name, el_val_t args_json); el_val_t agentic_tools_literal(void); el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input); el_val_t handle_chat_agentic(el_val_t body); @@ -81,6 +97,7 @@ el_val_t route_sessions(void); el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body); el_val_t init_soul_edges(void); el_val_t load_identity_context(void); +el_val_t seed_persona_from_env(void); el_val_t emit_session_start_event(void); el_val_t soul_cgi_id_raw; @@ -97,8 +114,6 @@ el_val_t studio_dir_raw; el_val_t studio_dir; el_val_t using_http_engram; el_val_t boot_num; -el_val_t identity_raw; -el_val_t soul_identity; el_val_t is_genesis; el_val_t init_soul_edges(void) { @@ -182,22 +197,68 @@ el_val_t load_identity_context(void) { el_val_t intel_content = ({ el_val_t _if_result_1 = 0; if (intel_ok) { _if_result_1 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_1 = (EL_STR("")); } _if_result_1; }); el_val_t values_content = ({ el_val_t _if_result_2 = 0; if (values_ok) { _if_result_2 = (json_get(node_values, EL_STR("content"))); } else { _if_result_2 = (EL_STR("")); } _if_result_2; }); el_val_t mem_content = ({ el_val_t _if_result_3 = 0; if (mem_ok) { _if_result_3 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_3 = (EL_STR("")); } _if_result_3; }); - el_val_t intel_short = ({ el_val_t _if_result_4 = 0; if ((str_len(intel_content) > 600)) { _if_result_4 = (str_slice(intel_content, 0, 600)); } else { _if_result_4 = (intel_content); } _if_result_4; }); - el_val_t values_short = ({ el_val_t _if_result_5 = 0; if ((str_len(values_content) > 600)) { _if_result_5 = (str_slice(values_content, 0, 600)); } else { _if_result_5 = (values_content); } _if_result_5; }); - el_val_t mem_short = ({ el_val_t _if_result_6 = 0; if ((str_len(mem_content) > 600)) { _if_result_6 = (str_slice(mem_content, 0, 600)); } else { _if_result_6 = (mem_content); } _if_result_6; }); + el_val_t intel_short = ({ el_val_t _if_result_4 = 0; if ((str_len(intel_content) > 2000)) { _if_result_4 = (str_slice(intel_content, 0, 2000)); } else { _if_result_4 = (intel_content); } _if_result_4; }); + el_val_t values_short = ({ el_val_t _if_result_5 = 0; if ((str_len(values_content) > 2000)) { _if_result_5 = (str_slice(values_content, 0, 2000)); } else { _if_result_5 = (values_content); } _if_result_5; }); + el_val_t mem_short = ({ el_val_t _if_result_6 = 0; if ((str_len(mem_content) > 2000)) { _if_result_6 = (str_slice(mem_content, 0, 2000)); } else { _if_result_6 = (mem_content); } _if_result_6; }); el_val_t parts_count = 0; parts_count = ({ el_val_t _if_result_7 = 0; if (intel_ok) { _if_result_7 = ((parts_count + 1)); } else { _if_result_7 = (parts_count); } _if_result_7; }); parts_count = ({ el_val_t _if_result_8 = 0; if (values_ok) { _if_result_8 = ((parts_count + 1)); } else { _if_result_8 = (parts_count); } _if_result_8; }); parts_count = ({ el_val_t _if_result_9 = 0; if (mem_ok) { _if_result_9 = ((parts_count + 1)); } else { _if_result_9 = (parts_count); } _if_result_9; }); - if (parts_count == 0) { + if (parts_count > 0) { + el_val_t ctx = EL_STR(""); + ctx = ({ el_val_t _if_result_10 = 0; if (intel_ok) { _if_result_10 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_10 = (ctx); } _if_result_10; }); + ctx = ({ el_val_t _if_result_11 = 0; if (values_ok) { _if_result_11 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_11 = (ctx); } _if_result_11; }); + ctx = ({ el_val_t _if_result_12 = 0; if (mem_ok) { _if_result_12 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_12 = (ctx); } _if_result_12; }); + 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)"))); + } + el_val_t persona_results = engram_search_json(EL_STR("soul:persona"), 3); + el_val_t persona_ok = (!str_eq(persona_results, EL_STR("")) && !str_eq(persona_results, EL_STR("[]"))); + if (persona_ok) { + el_val_t p_node = json_array_get(persona_results, 0); + el_val_t p_type = json_get(p_node, EL_STR("node_type")); + el_val_t p_content = json_get(p_node, EL_STR("content")); + if (str_eq(p_type, EL_STR("Persona")) && !str_eq(p_content, EL_STR(""))) { + state_set(EL_STR("soul_persona"), p_content); + println(el_str_concat(el_str_concat(EL_STR("[soul] persona node loaded ("), int_to_str(str_len(p_content))), EL_STR(" chars)"))); + } + } + return 0; +} + +el_val_t seed_persona_from_env(void) { + el_val_t identity_raw = env(EL_STR("SOUL_IDENTITY")); + if (str_eq(identity_raw, EL_STR(""))) { return EL_STR(""); } - el_val_t ctx = EL_STR(""); - ctx = ({ el_val_t _if_result_10 = 0; if (intel_ok) { _if_result_10 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_10 = (ctx); } _if_result_10; }); - ctx = ({ el_val_t _if_result_11 = 0; if (values_ok) { _if_result_11 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_11 = (ctx); } _if_result_11; }); - ctx = ({ el_val_t _if_result_12 = 0; if (mem_ok) { _if_result_12 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_12 = (ctx); } _if_result_12; }); - 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)"))); + el_val_t existing = state_get(EL_STR("soul_persona")); + if (!str_eq(existing, EL_STR(""))) { + println(EL_STR("[soul] persona already loaded \xe2\x80\x94 skipping env seed")); + return EL_STR(""); + } + el_val_t tags = EL_STR("[\"persona\",\"identity\",\"soul:persona\"]"); + el_val_t node_id = engram_node_full(identity_raw, EL_STR("Persona"), EL_STR("soul:persona"), el_from_float(0.95), el_from_float(0.95), el_from_float(1.0), EL_STR("Semantic"), tags); + if (str_eq(node_id, EL_STR(""))) { + println(EL_STR("[soul] persona seed failed: engram_node_full returned empty")); + return EL_STR(""); + } + state_set(EL_STR("soul_persona"), identity_raw); + println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] persona seeded from SOUL_IDENTITY ("), int_to_str(str_len(identity_raw))), EL_STR(" chars) -> ")), node_id)); + el_val_t engram_url = env(EL_STR("ENGRAM_URL")); + el_val_t engram_key = env(EL_STR("ENGRAM_API_KEY")); + if (!str_eq(engram_url, EL_STR("")) && !str_eq(engram_key, EL_STR(""))) { + el_val_t safe_content = json_safe(identity_raw); + el_val_t safe_key = json_safe(engram_key); + el_val_t body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"content\":\""), safe_content), EL_STR("\",\"node_type\":\"Persona\",\"label\":\"soul:persona\",\"salience\":0.95,\"importance\":0.95,\"tier\":\"Semantic\",\"tags\":\"[\\\"persona\\\",\\\"identity\\\",\\\"soul:persona\\\"]\",\"_auth\":\"")), safe_key), EL_STR("\"}")); + el_val_t h = el_map_new(0); + map_set(h, EL_STR("Content-Type"), EL_STR("application/json")); + el_val_t resp = http_post_with_headers(el_str_concat(engram_url, EL_STR("/api/nodes")), body, h); + if (str_contains(resp, EL_STR("\"error\""))) { + println(el_str_concat(EL_STR("[soul] persona HTTP write-back failed (in-memory only this session): "), resp)); + } else { + println(el_str_concat(EL_STR("[soul] persona persisted to HTTP engram at "), engram_url)); + } + } return 0; } @@ -252,14 +313,12 @@ int main(int _argc, char** _argv) { println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] loaded - nodes="), int_to_str(engram_node_count())), EL_STR(" edges=")), int_to_str(engram_edge_count()))); } load_identity_context(); + seed_persona_from_env(); boot_num = mem_boot_count_inc(); state_set(EL_STR("soul_boot_count"), int_to_str(boot_num)); println(el_str_concat(EL_STR("[soul] boot #"), int_to_str(boot_num))); emit_session_start_event(); - identity_raw = env(EL_STR("SOUL_IDENTITY")); - soul_identity = ({ el_val_t _if_result_24 = 0; if (str_eq(identity_raw, EL_STR(""))) { _if_result_24 = (el_str_concat(el_str_concat(EL_STR("You are "), soul_cgi_id), EL_STR(", a CGI."))); } else { _if_result_24 = (identity_raw); } _if_result_24; }); state_set(EL_STR("soul_cgi_id"), soul_cgi_id); - state_set(EL_STR("soul_identity"), soul_identity); state_set(EL_STR("soul_axon_base"), axon_base); state_set(EL_STR("soul_token"), env(EL_STR("NEURON_TOKEN"))); state_set(EL_STR("soul_studio_dir"), studio_dir); @@ -286,7 +345,9 @@ int main(int _argc, char** _argv) { } } println(el_str_concat(EL_STR("[soul] serving on port "), int_to_str(port))); - http_serve(port, EL_STR("handle_request")); + http_serve_async(port, EL_STR("handle_request")); + println(EL_STR("[soul] awareness loop starting")); + awareness_run(); return 0; } diff --git a/soul.el b/soul.el index b0c9557..b2f381a 100644 --- a/soul.el +++ b/soul.el @@ -106,27 +106,93 @@ fn load_identity_context() -> Void { let values_content: String = if values_ok { json_get(node_values, "content") } else { "" } let mem_content: String = if mem_ok { json_get(node_mem_phil, "content") } else { "" } - // Condense each: take first 600 chars - let intel_short: String = if str_len(intel_content) > 600 { str_slice(intel_content, 0, 600) } else { intel_content } - let values_short: String = if str_len(values_content) > 600 { str_slice(values_content, 0, 600) } else { values_content } - let mem_short: String = if str_len(mem_content) > 600 { str_slice(mem_content, 0, 600) } else { mem_content } + // Condense each: take first 2000 chars + let intel_short: String = if str_len(intel_content) > 2000 { str_slice(intel_content, 0, 2000) } else { intel_content } + let values_short: String = if str_len(values_content) > 2000 { str_slice(values_content, 0, 2000) } else { values_content } + let mem_short: String = if str_len(mem_content) > 2000 { str_slice(mem_content, 0, 2000) } else { mem_content } let parts_count: Int = 0 let parts_count = if intel_ok { parts_count + 1 } else { parts_count } let parts_count = if values_ok { parts_count + 1 } else { parts_count } let parts_count = if mem_ok { parts_count + 1 } else { parts_count } - if parts_count == 0 { - return "" + // Build and store graph-derived identity context if any nodes were found. + // genesis soul always has these nodes; cultivated souls may not on first boot. + if parts_count > 0 { + let ctx: String = "" + let ctx = if intel_ok { ctx + "[INTELLECTUAL-DNA]\n" + intel_short + "\n\n" } else { ctx } + let ctx = if values_ok { ctx + "[VALUES]\n" + values_short + "\n\n" } else { ctx } + let ctx = if mem_ok { ctx + "[MEMORY-PHILOSOPHY]\n" + mem_short } else { ctx } + state_set("soul_identity_context", ctx) + println("[soul] identity context loaded (" + int_to_str(str_len(ctx)) + " chars, " + int_to_str(parts_count) + " nodes)") } - let ctx: String = "" - let ctx = if intel_ok { ctx + "[INTELLECTUAL-DNA]\n" + intel_short + "\n\n" } else { ctx } - let ctx = if values_ok { ctx + "[VALUES]\n" + values_short + "\n\n" } else { ctx } - let ctx = if mem_ok { ctx + "[MEMORY-PHILOSOPHY]\n" + mem_short } else { ctx } + // Scan for a Persona node — the explicit identity declaration seeded into cultivated souls. + // Stored at seeding time with label "soul:persona" and node_type "Persona". + // genesis derives identity from the graph directly; cultivated souls have this node seeded. + let persona_results: String = engram_search_json("soul:persona", 3) + let persona_ok: Bool = !str_eq(persona_results, "") && !str_eq(persona_results, "[]") + if persona_ok { + let p_node: String = json_array_get(persona_results, 0) + let p_type: String = json_get(p_node, "node_type") + let p_content: String = json_get(p_node, "content") + if str_eq(p_type, "Persona") && !str_eq(p_content, "") { + state_set("soul_persona", p_content) + println("[soul] persona node loaded (" + int_to_str(str_len(p_content)) + " chars)") + } + } +} - state_set("soul_identity_context", ctx) - println("[soul] identity context loaded (" + int_to_str(str_len(ctx)) + " chars, " + int_to_str(parts_count) + " nodes)") +// seed_persona_from_env — one-time migration: SOUL_IDENTITY env var → Persona graph node. +// If SOUL_IDENTITY is set and no Persona node exists in engram yet, create one. +// Identity is then read from the graph by build_identity_from_graph(), not the env var. +// This runs on every boot; it's idempotent (no-op if soul_persona is already loaded). +// For genesis: the Persona node persists via the regular engram_save() at boot. +// For historical souls (HTTP Engram mode): attempts HTTP write-back. If that fails, +// the node lives in-memory for the session (SOUL_IDENTITY stays in plist until +// HTTP Engram write is confirmed working). +fn seed_persona_from_env() -> Void { + let identity_raw: String = env("SOUL_IDENTITY") + if str_eq(identity_raw, "") { + return "" + } + // Already loaded a Persona node from engram — don't re-seed + let existing: String = state_get("soul_persona") + if !str_eq(existing, "") { + println("[soul] persona already loaded — skipping env seed") + return "" + } + // Create the Persona node in the in-process engram + let tags: String = "[\"persona\",\"identity\",\"soul:persona\"]" + let node_id: String = engram_node_full( + identity_raw, "Persona", "soul:persona", + el_from_float(0.95), el_from_float(0.95), el_from_float(1.0), + "Semantic", tags + ) + if str_eq(node_id, "") { + println("[soul] persona seed failed: engram_node_full returned empty") + return "" + } + state_set("soul_persona", identity_raw) + println("[soul] persona seeded from SOUL_IDENTITY (" + int_to_str(str_len(identity_raw)) + " chars) -> " + node_id) + + // Attempt HTTP write-back to the HTTP Engram server for historical souls. + // Engram auth: "_auth" field in the JSON body (not an HTTP header). + let engram_url: String = env("ENGRAM_URL") + let engram_key: String = env("ENGRAM_API_KEY") + if !str_eq(engram_url, "") && !str_eq(engram_key, "") { + let safe_content: String = json_safe(identity_raw) + let safe_key: String = json_safe(engram_key) + let body: String = "{\"content\":\"" + safe_content + "\",\"node_type\":\"Persona\",\"label\":\"soul:persona\",\"salience\":0.95,\"importance\":0.95,\"tier\":\"Semantic\",\"tags\":\"[\\\"persona\\\",\\\"identity\\\",\\\"soul:persona\\\"]\",\"_auth\":\"" + safe_key + "\"}" + let h: Map = {} + map_set(h, "Content-Type", "application/json") + let resp: String = http_post_with_headers(engram_url + "/api/nodes", body, h) + if str_contains(resp, "\"error\"") { + println("[soul] persona HTTP write-back failed (in-memory only this session): " + resp) + } else { + println("[soul] persona persisted to HTTP engram at " + engram_url) + } + } } // emit_session_start_event — log a structured session-start InternalStateEvent. @@ -206,16 +272,13 @@ if using_http_engram { } load_identity_context() +seed_persona_from_env() let boot_num: Int = mem_boot_count_inc() state_set("soul_boot_count", int_to_str(boot_num)) println("[soul] boot #" + int_to_str(boot_num)) emit_session_start_event() -let identity_raw: String = env("SOUL_IDENTITY") -let soul_identity: String = if str_eq(identity_raw, "") { "You are " + soul_cgi_id + ", a CGI." } else { identity_raw } - state_set("soul_cgi_id", soul_cgi_id) -state_set("soul_identity", soul_identity) state_set("soul_axon_base", axon_base) state_set("soul_token", env("NEURON_TOKEN")) state_set("soul_studio_dir", studio_dir) @@ -251,4 +314,6 @@ if is_genesis { } println("[soul] serving on port " + int_to_str(port)) -http_serve(port, "handle_request") +http_serve_async(port, "handle_request") +println("[soul] awareness loop starting") +awareness_run()