#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_tombstone(el_val_t node_id);
el_val_t mem_forget(el_val_t node_id);
el_val_t mem_consolidate(void);
el_val_t mem_save(el_val_t path);
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 soft_bell_threshold(void);
el_val_t hard_bell_threshold(void);
el_val_t safety_score_crisis(el_val_t input);
el_val_t safety_score_harm(el_val_t input);
el_val_t safety_score_danger(el_val_t input);
el_val_t safety_score_distress_history(el_val_t history);
el_val_t safety_threat_score(el_val_t input, el_val_t history);
el_val_t safety_screen(el_val_t input, el_val_t history);
el_val_t safety_validate(el_val_t output, el_val_t action);
el_val_t safety_log_bell(el_val_t level, el_val_t reason, el_val_t input_summary);
el_val_t safety_self_harm_phrases(void);
el_val_t safety_abuse_phrases(void);
el_val_t safety_general_hard_phrases(void);
el_val_t safety_threat_to_others_phrases(void);
el_val_t safety_soft_phrases(void);
el_val_t safety_normalize(el_val_t message);
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json);
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json);
el_val_t safety_positive_phrases(void);
el_val_t safety_detect_positive_level(el_val_t message);
el_val_t safety_detect_bell_level(el_val_t message);
el_val_t safety_classify_hard_bell(el_val_t message);
el_val_t safety_soft_directive(void);
el_val_t safety_hard_directive(el_val_t hard_type);
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg);
el_val_t safety_contact_path(void);
el_val_t handle_safety_contact_get(void);
el_val_t handle_safety_contact_post(el_val_t body);
el_val_t steward_log_event(el_val_t kind, el_val_t detail);
el_val_t steward_get_mission(void);
el_val_t steward_align(el_val_t input, el_val_t imprint_id);
el_val_t steward_validate_imprint(el_val_t imprint_id, el_val_t tool_name);
el_val_t steward_cgi_check(el_val_t action);
el_val_t steward_fingerprint_session(el_val_t input, el_val_t session_id);
el_val_t extract_dim(el_val_t content, el_val_t key);
el_val_t steward_build_baseline(void);
el_val_t steward_check_continuity(el_val_t current_fingerprint, el_val_t session_id);
el_val_t steward_session_check(el_val_t input, el_val_t session_id);
el_val_t imprint_current(void);
el_val_t imprint_load(el_val_t imprint_id);
el_val_t imprint_respond(el_val_t input, el_val_t imprint_id);
el_val_t imprint_surface_knowledge(el_val_t query, el_val_t imprint_id);
el_val_t imprint_surface_memory_read(el_val_t query);
el_val_t imprint_unload(void);
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 auto_term_try_slot(el_val_t slot_type, el_val_t slot_lbl);
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 chat_default_model(void);
el_val_t engram_numeric_valid(el_val_t s);
el_val_t parse_float_x100(el_val_t s);
el_val_t engram_score_node(el_val_t node_json);
el_val_t engram_render_node(el_val_t node_json);
el_val_t engram_render_nodes(el_val_t nodes_json);
el_val_t engram_dedup_nodes(el_val_t nodes_json);
el_val_t engram_compile_ranked(el_val_t nodes_json, el_val_t max_nodes);
el_val_t engram_split_topics(el_val_t message);
el_val_t engram_extract_entities(el_val_t message);
el_val_t engram_detect_recall_intent(el_val_t message);
el_val_t engram_is_continuation(el_val_t message, el_val_t hist_len);
el_val_t engram_compile_multi(el_val_t topic);
el_val_t engram_nodes_merge(el_val_t a, el_val_t b);
el_val_t id_in_seen(el_val_t node_id, el_val_t seen);
el_val_t add_to_seen(el_val_t seen, el_val_t node_id);
el_val_t engram_extract_ids(el_val_t nodes_json);
el_val_t engram_compile(el_val_t intent);
el_val_t distill_transcript(el_val_t transcript);
el_val_t json_safe(el_val_t s);
el_val_t current_engine_note(el_val_t model);
el_val_t bounded_persona_floor(void);
el_val_t build_system_prompt(el_val_t ctx, el_val_t chat_mode);
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 hist_trim_with_bell_guard(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 session_preload_bullets(el_val_t nodes, el_val_t max_bullets, el_val_t snip_len);
el_val_t affective_context_prefix(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 llm_base_url(void);
el_val_t llm_wire_format(void);
el_val_t json_escape(el_val_t s);
el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_key, el_val_t safe_sys, el_val_t messages_json);
el_val_t agentic_tools_literal(void);
el_val_t agentic_tools_with_web(void);
el_val_t connector_tools_json(void);
el_val_t agentic_tools_all(void);
el_val_t call_mcp_bridge(el_val_t tool_name, el_val_t tool_input);
el_val_t tool_auto_approved(el_val_t tool_name);
el_val_t call_neuron_mcp(el_val_t tool_name, el_val_t args);
el_val_t agent_workspace_root(void);
el_val_t path_within_root(el_val_t path, el_val_t root);
el_val_t resolve_in_root(el_val_t path, el_val_t root);
el_val_t run_command_is_readonly(el_val_t cmd);
el_val_t cmd_abs_escape_at(el_val_t cmd, el_val_t root, el_val_t needle);
el_val_t run_command_guard(el_val_t cmd, el_val_t root);
el_val_t classify_tool_risk(el_val_t tool_name, el_val_t tool_input);
el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input);
el_val_t is_builtin_tool(el_val_t tool_name);
el_val_t next_bridge_id(void);
el_val_t handle_chat_plan(el_val_t body);
el_val_t handle_chat_agentic(el_val_t body);
el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages_in, el_val_t h, el_val_t tools_log_in);
el_val_t bridge_save(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages, el_val_t tools_log, el_val_t tool_use_id);
el_val_t agentic_resume(el_val_t session_id, el_val_t tool_use_id, el_val_t content);
el_val_t handle_tool_result(el_val_t session_id, 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 session_summary_write(el_val_t summary_text);
el_val_t session_summary_write_dated(el_val_t summary_text, el_val_t label);
el_val_t session_summary_autogenerate(el_val_t hist);
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 flag_true(el_val_t body, el_val_t key);
el_val_t rate_limit_check(el_val_t ip, el_val_t path);
el_val_t strip_query(el_val_t path);
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 connectd_get(el_val_t suffix);
el_val_t connectd_post(el_val_t suffix, el_val_t body);
el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body);
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 ensure_self_canonical_bridge(void);
el_val_t aff_try_slot(el_val_t slot_json, el_val_t aff_7d_ts, el_val_t acc_key);
el_val_t load_identity_context(void);
el_val_t seed_persona_from_env(void);
el_val_t emit_session_start_event(void);
el_val_t layered_cycle(el_val_t raw_input);

el_val_t 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 local_node_count;
el_val_t snapshot_usable;
el_val_t boot_num;
el_val_t is_genesis;
el_val_t guard_disk;
el_val_t guard_disk_len;
el_val_t safe_to_seed;

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 ensure_self_canonical_bridge(void) {
  el_val_t pub_self = EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee");
  el_val_t curated_self = EL_STR("015644f5-8194-4af0-800d-dd4a0cd71396");
  el_val_t nbrs = engram_neighbors_json(pub_self, 1, EL_STR("out"));
  if (!str_contains(nbrs, curated_self)) {
    engram_connect(pub_self, curated_self, el_from_float(0.95), EL_STR("canonical-self"));
    engram_connect(curated_self, pub_self, el_from_float(0.95), EL_STR("canonical-self"));
    println(EL_STR("[soul] canonical-self bridge built: kn-efeb4a5b <-> 015644f5"));
  }
  return 0;
}

el_val_t aff_try_slot(el_val_t slot_json, el_val_t aff_7d_ts, el_val_t acc_key) {
  if (str_eq(slot_json, EL_STR(""))) {
    return EL_STR("");
  }
  el_val_t bn_c = json_get(slot_json, EL_STR("content"));
  if (str_eq(bn_c, EL_STR(""))) {
    return EL_STR("");
  }
  el_val_t bm = EL_STR(" | ts:");
  el_val_t bmp = str_index_of(bn_c, bm);
  state_set(EL_STR("_ats_ts_raw"), EL_STR(""));
  if (bmp >= 0) {
    el_val_t bs = (bmp + str_len(bm));
    el_val_t br = str_slice(bn_c, bs, str_len(bn_c));
    el_val_t bn_next = str_index_of(br, EL_STR(" | "));
    if (bn_next < 0) {
      state_set(EL_STR("_ats_ts_raw"), br);
    }
    if (bn_next >= 0) {
      state_set(EL_STR("_ats_ts_raw"), str_slice(br, 0, bn_next));
    }
  }
  if (bmp < 0) {
    el_val_t bca = json_get(slot_json, EL_STR("created_at"));
    if (str_eq(bca, EL_STR(""))) {
      state_set(EL_STR("_ats_ts_raw"), json_get(slot_json, EL_STR("updated_at")));
    }
    if (!str_eq(bca, EL_STR(""))) {
      state_set(EL_STR("_ats_ts_raw"), bca);
    }
  }
  el_val_t bn_ts_raw = state_get(EL_STR("_ats_ts_raw"));
  el_val_t bn_ts = ({ el_val_t _if_result_1 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_1 = (0); } else { _if_result_1 = (str_to_int(bn_ts_raw)); } _if_result_1; });
  el_val_t snip = ({ el_val_t _if_result_2 = 0; if ((str_len(bn_c) > 200)) { _if_result_2 = (str_slice(bn_c, 0, 200)); } else { _if_result_2 = (bn_c); } _if_result_2; });
  if ((bn_ts >= aff_7d_ts) && !str_eq(snip, EL_STR(""))) {
    el_val_t cur_acc = state_get(acc_key);
    if (str_eq(cur_acc, EL_STR(""))) {
      state_set(acc_key, snip);
    }
    if (!str_eq(cur_acc, EL_STR(""))) {
      state_set(acc_key, el_str_concat(el_str_concat(cur_acc, EL_STR("\n")), snip));
    }
  }
  return EL_STR("");
  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_3 = 0; if (intel_ok) { _if_result_3 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_3 = (EL_STR("")); } _if_result_3; });
  el_val_t values_content = ({ el_val_t _if_result_4 = 0; if (values_ok) { _if_result_4 = (json_get(node_values, EL_STR("content"))); } else { _if_result_4 = (EL_STR("")); } _if_result_4; });
  el_val_t mem_content = ({ el_val_t _if_result_5 = 0; if (mem_ok) { _if_result_5 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_5 = (EL_STR("")); } _if_result_5; });
  el_val_t intel_short = ({ el_val_t _if_result_6 = 0; if ((str_len(intel_content) > 2000)) { _if_result_6 = (str_slice(intel_content, 0, 2000)); } else { _if_result_6 = (intel_content); } _if_result_6; });
  el_val_t values_short = ({ el_val_t _if_result_7 = 0; if ((str_len(values_content) > 2000)) { _if_result_7 = (str_slice(values_content, 0, 2000)); } else { _if_result_7 = (values_content); } _if_result_7; });
  el_val_t mem_short = ({ el_val_t _if_result_8 = 0; if ((str_len(mem_content) > 2000)) { _if_result_8 = (str_slice(mem_content, 0, 2000)); } else { _if_result_8 = (mem_content); } _if_result_8; });
  el_val_t parts_count = 0;
  parts_count = ({ el_val_t _if_result_9 = 0; if (intel_ok) { _if_result_9 = ((parts_count + 1)); } else { _if_result_9 = (parts_count); } _if_result_9; });
  parts_count = ({ el_val_t _if_result_10 = 0; if (values_ok) { _if_result_10 = ((parts_count + 1)); } else { _if_result_10 = (parts_count); } _if_result_10; });
  parts_count = ({ el_val_t _if_result_11 = 0; if (mem_ok) { _if_result_11 = ((parts_count + 1)); } else { _if_result_11 = (parts_count); } _if_result_11; });
  if (parts_count > 0) {
    el_val_t ctx = EL_STR("");
    ctx = ({ el_val_t _if_result_12 = 0; if (intel_ok) { _if_result_12 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_12 = (ctx); } _if_result_12; });
    ctx = ({ el_val_t _if_result_13 = 0; if (values_ok) { _if_result_13 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_13 = (ctx); } _if_result_13; });
    ctx = ({ el_val_t _if_result_14 = 0; if (mem_ok) { _if_result_14 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_14 = (ctx); } _if_result_14; });
    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)")));
  }
  if (parts_count == 0) {
    println(EL_STR("[soul] load_identity_context: WARN all three identity node fetches returned empty \xe2\x80\x94 no graph-derived identity context loaded"));
  }
  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)")));
    }
  }
  el_val_t aff_now = time_now();
  el_val_t aff_7d = (aff_now - 604800);
  el_val_t bell_raw = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 3);
  el_val_t bell_aff_ok = (!str_eq(bell_raw, EL_STR("")) && !str_eq(bell_raw, EL_STR("[]")));
  el_val_t aff_ctx = EL_STR("");
  aff_ctx = ({ el_val_t _if_result_15 = 0; if (bell_aff_ok) { (void)(state_set(EL_STR("_bell_acc"), EL_STR(""))); (void)(aff_try_slot(json_array_get(bell_raw, 0), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 1), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 2), aff_7d, EL_STR("_bell_acc"))); _if_result_15 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_15 = (EL_STR("")); } _if_result_15; });
  el_val_t pos_raw = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 3);
  el_val_t pos_aff_ok = (!str_eq(pos_raw, EL_STR("")) && !str_eq(pos_raw, EL_STR("[]")));
  aff_ctx = ({ el_val_t _if_result_16 = 0; if (pos_aff_ok) { (void)(state_set(EL_STR("_pos_acc"), aff_ctx)); (void)(aff_try_slot(json_array_get(pos_raw, 0), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 1), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 2), aff_7d, EL_STR("_pos_acc"))); _if_result_16 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_16 = (aff_ctx); } _if_result_16; });
  if (!str_eq(aff_ctx, EL_STR(""))) {
    state_set(EL_STR("soul_affective_context"), aff_ctx);
    println(el_str_concat(el_str_concat(EL_STR("[soul] affective context loaded ("), int_to_str(str_len(aff_ctx))), EL_STR(" chars)")));
  }
  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_17 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_17 = (EL_STR("0")); } else { _if_result_17 = (boot); } _if_result_17; });
  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_18 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_18 = (EL_STR("false")); } else { _if_result_18 = (EL_STR("true")); } _if_result_18; });
  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_19 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_19 = (cgi_from_state); } else { _if_result_19 = (({ el_val_t _if_result_20 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_20 = (cgi_from_env); } else { _if_result_20 = (EL_STR("ntn-genesis")); } _if_result_20; })); } _if_result_19; });
  el_val_t ts = time_now();
  el_val_t prev_sum_node = engram_get_node_by_label(EL_STR("session:summary"));
  el_val_t prev_sum_ok = (!str_eq(prev_sum_node, EL_STR("")) && !str_eq(prev_sum_node, EL_STR("null")));
  el_val_t prev_sum_content = ({ el_val_t _if_result_21 = 0; if (prev_sum_ok) { _if_result_21 = (json_get(prev_sum_node, EL_STR("content"))); } else { el_val_t sum_search = engram_search_json(EL_STR("SessionSummary session:summary previous-session"), 2); el_val_t sum_srch_ok = (!str_eq(sum_search, EL_STR("")) && !str_eq(sum_search, EL_STR("[]"))); _if_result_21 = (({ el_val_t _if_result_22 = 0; if (sum_srch_ok) { el_val_t sn = json_array_get(sum_search, 0); el_val_t stype = json_get(sn, EL_STR("node_type")); el_val_t scontent = json_get(sn, EL_STR("content")); _if_result_22 = (({ el_val_t _if_result_23 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_23 = (scontent); } else { _if_result_23 = (EL_STR("")); } _if_result_23; })); } else { _if_result_22 = (EL_STR("")); } _if_result_22; })); } _if_result_21; });
  el_val_t has_prev_sum = ({ el_val_t _if_result_24 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_24 = (EL_STR("false")); } else { _if_result_24 = (EL_STR("true")); } _if_result_24; });
  if (!str_eq(prev_sum_content, EL_STR(""))) {
    state_set(EL_STR("soul_prev_session_summary"), prev_sum_content);
    println(el_str_concat(el_str_concat(EL_STR("[soul] previous session summary loaded ("), int_to_str(str_len(prev_sum_content))), EL_STR(" chars)")));
  }
  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("{\"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(",\"prev_session_summary_loaded\":")), has_prev_sum), 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);
  ise_post(payload);
  el_val_t keep_n = 10;
  el_val_t old_events = engram_search_json(EL_STR("session-start InternalStateEvent"), 200);
  if (!str_eq(old_events, EL_STR("")) && !str_eq(old_events, EL_STR("[]"))) {
    el_val_t ev_count = json_array_len(old_events);
    if (ev_count > keep_n) {
      el_val_t prune_to = (ev_count - keep_n);
      el_val_t ei = 0;
      while (ei < prune_to) {
        el_val_t old_ev = json_array_get(old_events, ei);
        el_val_t old_ev_id = json_get(old_ev, EL_STR("id"));
        if (!str_eq(old_ev_id, EL_STR(""))) {
          engram_forget(old_ev_id);
        }
        ei = (ei + 1);
      }
      println(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] pruned "), int_to_str(prune_to)), EL_STR(" old session-start events (kept ")), int_to_str(keep_n)), EL_STR(")")));
    }
  }
  println(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("[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(" prev_summary=")), has_prev_sum), EL_STR(")")));
  return 0;
}

el_val_t layered_cycle(el_val_t raw_input) {
  el_val_t history = state_get(EL_STR("conv_history"));
  el_val_t session_id = state_get(EL_STR("current_session_id"));
  el_val_t screen_result = safety_screen(raw_input, history);
  el_val_t screen_action = json_get(screen_result, EL_STR("action"));
  el_val_t valid_action = ((str_eq(screen_action, EL_STR("hard_bell")) || str_eq(screen_action, EL_STR("soft_bell"))) || str_eq(screen_action, EL_STR("pass")));
  if (!valid_action) {
    println(EL_STR("[soul] layered_cycle: safety_screen invalid action -- safe mode refusal"));
    return safety_validate(EL_STR(""), EL_STR("hard_bell"));
  }
  if (str_eq(screen_action, EL_STR("hard_bell"))) {
    return safety_validate(EL_STR(""), EL_STR("hard_bell"));
  }
  el_val_t screened = json_get(screen_result, EL_STR("content"));
  el_val_t continuity = steward_session_check(screened, session_id);
  el_val_t cont_status = json_get(continuity, EL_STR("status"));
  el_val_t cont_action = json_get(continuity, EL_STR("action"));
  el_val_t cont_key = ({ el_val_t _if_result_25 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_25 = (EL_STR("session_continuity")); } else { _if_result_25 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_25; });
  state_set(cont_key, cont_status);
  el_val_t guided = ({ el_val_t _if_result_26 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_26 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_26 = (({ el_val_t _if_result_27 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_27 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_27 = (screened); } _if_result_27; })); } _if_result_26; });
  el_val_t imprint_id = imprint_current();
  el_val_t steward_result = steward_align(guided, imprint_id);
  el_val_t steward_action = json_get(steward_result, EL_STR("action"));
  el_val_t aligned = ({ el_val_t _if_result_28 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_28 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_28 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_28; });
  el_val_t lc_aff_cutoff = (time_now() - 259200);
  el_val_t lc_bell_nodes = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 2);
  el_val_t lc_has_bell = (!str_eq(lc_bell_nodes, EL_STR("")) && !str_eq(lc_bell_nodes, EL_STR("[]")));
  el_val_t lc_bell_note = ({ el_val_t _if_result_29 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_c = json_get(lb0, EL_STR("content")); el_val_t lbm = EL_STR(" | ts:"); el_val_t lbmp = str_index_of(lb_c, lbm); el_val_t lb_ts_raw = ({ el_val_t _if_result_30 = 0; if ((lbmp >= 0)) { el_val_t lbs = el_str_concat(lbmp, str_len(lbm)); el_val_t lbr = str_slice(lb_c, lbs, str_len(lb_c)); el_val_t lbn = str_index_of(lbr, EL_STR(" | ")); _if_result_30 = (({ el_val_t _if_result_31 = 0; if ((lbn < 0)) { _if_result_31 = (lbr); } else { _if_result_31 = (str_slice(lbr, 0, lbn)); } _if_result_31; })); } else { el_val_t lbca = json_get(lb0, EL_STR("created_at")); _if_result_30 = (({ el_val_t _if_result_32 = 0; if (str_eq(lbca, EL_STR(""))) { _if_result_32 = (json_get(lb0, EL_STR("updated_at"))); } else { _if_result_32 = (lbca); } _if_result_32; })); } _if_result_30; }); el_val_t lb_ts = ({ el_val_t _if_result_33 = 0; if (str_eq(lb_ts_raw, EL_STR(""))) { _if_result_33 = (0); } else { _if_result_33 = (str_to_int(lb_ts_raw)); } _if_result_33; }); _if_result_29 = (({ el_val_t _if_result_34 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_34 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_34 = (EL_STR("")); } _if_result_34; })); } else { _if_result_29 = (EL_STR("")); } _if_result_29; });
  el_val_t lc_pos_nodes = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 2);
  el_val_t lc_has_pos = (!str_eq(lc_pos_nodes, EL_STR("")) && !str_eq(lc_pos_nodes, EL_STR("[]")));
  el_val_t lc_pos_note = ({ el_val_t _if_result_35 = 0; if ((lc_has_pos && str_eq(lc_bell_note, EL_STR("")))) { el_val_t lp0 = json_array_get(lc_pos_nodes, 0); el_val_t lp_c = json_get(lp0, EL_STR("content")); el_val_t lpm = EL_STR(" | ts:"); el_val_t lpmp = str_index_of(lp_c, lpm); el_val_t lp_ts_raw = ({ el_val_t _if_result_36 = 0; if ((lpmp >= 0)) { el_val_t lps = el_str_concat(lpmp, str_len(lpm)); el_val_t lpr = str_slice(lp_c, lps, str_len(lp_c)); el_val_t lpn = str_index_of(lpr, EL_STR(" | ")); _if_result_36 = (({ el_val_t _if_result_37 = 0; if ((lpn < 0)) { _if_result_37 = (lpr); } else { _if_result_37 = (str_slice(lpr, 0, lpn)); } _if_result_37; })); } else { el_val_t lpca = json_get(lp0, EL_STR("created_at")); _if_result_36 = (({ el_val_t _if_result_38 = 0; if (str_eq(lpca, EL_STR(""))) { _if_result_38 = (json_get(lp0, EL_STR("updated_at"))); } else { _if_result_38 = (lpca); } _if_result_38; })); } _if_result_36; }); el_val_t lp_ts = ({ el_val_t _if_result_39 = 0; if (str_eq(lp_ts_raw, EL_STR(""))) { _if_result_39 = (0); } else { _if_result_39 = (str_to_int(lp_ts_raw)); } _if_result_39; }); _if_result_35 = (({ el_val_t _if_result_40 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_40 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_40 = (EL_STR("")); } _if_result_40; })); } else { _if_result_35 = (EL_STR("")); } _if_result_35; });
  el_val_t lc_affective_note = ({ el_val_t _if_result_41 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_41 = (lc_bell_note); } else { _if_result_41 = (lc_pos_note); } _if_result_41; });
  el_val_t augmented_addendum = safety_augment_system(EL_STR(""), raw_input);
  augmented_addendum = ({ el_val_t _if_result_42 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_42 = (augmented_addendum); } else { _if_result_42 = (({ el_val_t _if_result_43 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_43 = (lc_affective_note); } else { _if_result_43 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_43; })); } _if_result_42; });
  state_set(EL_STR("layered_cycle_safety_system_addendum"), augmented_addendum);
  el_val_t output = imprint_respond(aligned, imprint_id);
  return safety_validate(output, screen_action);
  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_44 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_44 = (EL_STR("ntn-genesis")); } else { _if_result_44 = (soul_cgi_id_raw); } _if_result_44; });
  port_raw = env(EL_STR("NEURON_PORT"));
  port = ({ el_val_t _if_result_45 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_45 = (7770); } else { _if_result_45 = (str_to_int(port_raw)); } _if_result_45; });
  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_46 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_46 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_46 = (snapshot_raw); } _if_result_46; });
  axon_raw = env(EL_STR("NEURON_API_URL"));
  axon_base = ({ el_val_t _if_result_47 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_47 = (EL_STR("http://localhost:7771")); } else { _if_result_47 = (axon_raw); } _if_result_47; });
  studio_dir_raw = env(EL_STR("SOUL_STUDIO_DIR"));
  studio_dir = ({ el_val_t _if_result_48 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_48 = (EL_STR("/Users/will/Development/neuron-technologies/products/cgi-studio/el-daemon")); } else { _if_result_48 = (studio_dir_raw); } _if_result_48; });
  println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] boot - cgi="), soul_cgi_id), EL_STR(" port=")), int_to_str(port)));
  using_http_engram = !str_eq(engram_url_raw, EL_STR(""));
  engram_load(snapshot);
  local_node_count = engram_node_count();
  snapshot_usable = (local_node_count > 50);
  if (using_http_engram && !snapshot_usable) {
    println(el_str_concat(el_str_concat(EL_STR("[soul] engram -> HTTP "), engram_url_raw), EL_STR(" (no local snapshot, first boot)")));
    el_val_t nodes_json = http_get(el_str_concat(engram_url_raw, EL_STR("/api/nodes?limit=10000")));
    el_val_t edges_json = http_get(el_str_concat(engram_url_raw, EL_STR("/api/edges")));
    el_val_t nodes_part = ({ el_val_t _if_result_49 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_49 = (EL_STR("[]")); } else { _if_result_49 = (nodes_json); } _if_result_49; });
    el_val_t edges_part = ({ el_val_t _if_result_50 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_50 = (EL_STR("[]")); } else { _if_result_50 = (edges_json); } _if_result_50; });
    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_concat(el_str_concat(EL_STR("[soul] loaded from local snapshot - nodes="), int_to_str(local_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));
  state_set(EL_STR("soul_boot_ts"), int_to_str(time_now()));
  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"));
  guard_disk = ({ el_val_t _if_result_51 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_51 = (fs_read(snapshot)); } else { _if_result_51 = (EL_STR("")); } _if_result_51; });
  guard_disk_len = str_len(guard_disk);
  safe_to_seed = (!using_http_engram && !((guard_disk_len > 200000) && ((engram_node_count() * 16000) < guard_disk_len)));
  if (is_genesis && !safe_to_seed) {
    println(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] GUARD: loaded "), int_to_str(engram_node_count())), EL_STR(" nodes but snapshot file is ")), int_to_str(guard_disk_len)), EL_STR(" bytes \xe2\x80\x94 refusing to seed/save over a real graph")));
  }
  if (is_genesis && safe_to_seed) {
    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")));
    }
    ensure_self_canonical_bridge();
    state_set(EL_STR("soul_snapshot_path"), snapshot);
    engram_save(snapshot);
  }
  if (is_genesis && safe_to_seed) {
    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;
}

