Files
neuron/dist/neuron.c
T

354 lines
23 KiB
C

#include <stdint.h>
#include <stdlib.h>
#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;
}