#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"

el_val_t tier_working(void);
el_val_t tier_episodic(void);
el_val_t tier_canonical(void);
el_val_t mem_store(el_val_t content, el_val_t label, el_val_t tags);
el_val_t mem_remember(el_val_t content, el_val_t tags);
el_val_t mem_recall(el_val_t query, el_val_t depth);
el_val_t mem_search(el_val_t query, el_val_t limit);
el_val_t mem_strengthen(el_val_t node_id);
el_val_t mem_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 idle_count(void);
el_val_t idle_inc(void);
el_val_t idle_reset(void);
el_val_t hebb_consolidate(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 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 hebb_consolidate(void) {
  el_val_t batch = engram_hebb_drain_json(64);
  if (str_eq(batch, EL_STR(""))) {
    return 0;
  }
  if (str_eq(batch, EL_STR("[]"))) {
    return 0;
  }
  el_val_t n = json_array_len(batch);
  if (n == 0) {
    return 0;
  }
  el_val_t url_env = env(EL_STR("SOUL_ISE_URL"));
  el_val_t url_state = ({ el_val_t _if_result_1 = 0; if (str_eq(url_env, EL_STR(""))) { _if_result_1 = (state_get(EL_STR("soul_engram_url"))); } else { _if_result_1 = (url_env); } _if_result_1; });
  el_val_t engram_url = ({ el_val_t _if_result_2 = 0; if (str_eq(url_state, EL_STR(""))) { _if_result_2 = (EL_STR("http://localhost:8742")); } else { _if_result_2 = (url_state); } _if_result_2; });
  el_val_t key_state = state_get(EL_STR("soul_engram_api_key"));
  el_val_t api_key = ({ el_val_t _if_result_3 = 0; if (str_eq(key_state, EL_STR(""))) { _if_result_3 = (env(EL_STR("ENGRAM_API_KEY"))); } else { _if_result_3 = (key_state); } _if_result_3; });
  el_val_t auth_part = ({ el_val_t _if_result_4 = 0; if (str_eq(api_key, EL_STR(""))) { _if_result_4 = (EL_STR("")); } else { _if_result_4 = (el_str_concat(el_str_concat(EL_STR(",\"_auth\":\""), api_key), EL_STR("\""))); } _if_result_4; });
  el_val_t body = el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"edges\":"), batch), auth_part), EL_STR("}"));
  el_val_t resp = http_post_json(el_str_concat(engram_url, EL_STR("/api/edges/batch")), body);
  if (str_eq(resp, EL_STR(""))) {
    return 0;
  }
  el_val_t acc = json_get(resp, EL_STR("accepted"));
  if (str_eq(acc, EL_STR(""))) {
    return 0;
  }
  return str_to_int(acc);
  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 state_url = ({ el_val_t _if_result_5 = 0; if (str_eq(ise_url, EL_STR(""))) { _if_result_5 = (state_get(EL_STR("soul_engram_url"))); } else { _if_result_5 = (ise_url); } _if_result_5; });
  el_val_t engram_url = ({ el_val_t _if_result_6 = 0; if (str_eq(state_url, EL_STR(""))) { _if_result_6 = (EL_STR("http://localhost:8742")); } else { _if_result_6 = (state_url); } _if_result_6; });
  el_val_t safe1 = str_replace(content, EL_STR("\\"), EL_STR("\\\\"));
  el_val_t safe2 = str_replace(safe1, EL_STR("\""), EL_STR("\\\""));
  el_val_t safe3 = str_replace(safe2, EL_STR("\n"), EL_STR("\\n"));
  el_val_t safe4 = str_replace(safe3, EL_STR("\r"), EL_STR("\\r"));
  el_val_t body = el_str_concat(el_str_concat(EL_STR("{\"content\":\""), safe4), EL_STR("\"}"));
  el_val_t resp = http_post_json(el_str_concat(engram_url, EL_STR("/api/neuron/state-events")), body);
  if (str_eq(resp, EL_STR(""))) {
    el_val_t fail_raw = state_get(EL_STR("soul.ise_fail_count"));
    el_val_t fail_n = ({ el_val_t _if_result_7 = 0; if (str_eq(fail_raw, EL_STR(""))) { _if_result_7 = (0); } else { _if_result_7 = (str_to_int(fail_raw)); } _if_result_7; });
    state_set(EL_STR("soul.ise_fail_count"), int_to_str((fail_n + 1)));
    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\",\"ise-fallback-local\"]"));
    return EL_STR("");
  }
  return EL_STR("");
  return 0;
}

el_val_t elapsed_ms(void) {
  el_val_t s = state_get(EL_STR("soul.boot_ts"));
  if (str_eq(s, EL_STR(""))) {
    return 0;
  }
  el_val_t boot = str_to_int(s);
  return (time_now() - boot);
  return 0;
}

el_val_t elapsed_human(void) {
  el_val_t ms = elapsed_ms();
  el_val_t total_secs = (ms / 1000);
  el_val_t total_minutes = (total_secs / 60);
  el_val_t h = (total_minutes / 60);
  if (h > 0) {
    el_val_t h4 = (((h + h) + h) + h);
    el_val_t h8 = (h4 + h4);
    el_val_t h16 = (h8 + h8);
    el_val_t h32 = (h16 + h16);
    el_val_t h64 = (h32 + h32);
    el_val_t h60 = (h64 - h4);
    el_val_t m = (total_minutes - h60);
    return el_str_concat(el_str_concat(el_str_concat(int_to_str(h), EL_STR("h ")), int_to_str(m)), EL_STR("m"));
  }
  if (total_minutes > 0) {
    return el_str_concat(int_to_str(total_minutes), EL_STR("m"));
  }
  return el_str_concat(int_to_str(total_secs), EL_STR("s"));
  return 0;
}

el_val_t embed_ok(void) {
  el_val_t resp = http_get(EL_STR("http://localhost:11434"));
  if (str_eq(resp, EL_STR(""))) {
    return 0;
  }
  return 1;
  return 0;
}

el_val_t emit_heartbeat(void) {
  el_val_t pulse = int_to_str(pulse_count());
  el_val_t boot_raw = state_get(EL_STR("soul_boot_count"));
  el_val_t boot = ({ el_val_t _if_result_8 = 0; if (str_eq(boot_raw, EL_STR(""))) { _if_result_8 = (EL_STR("0")); } else { _if_result_8 = (boot_raw); } _if_result_8; });
  el_val_t idle = int_to_str(idle_count());
  el_val_t ts = time_now();
  el_val_t last_act_raw = state_get(EL_STR("soul.last_activity_ts"));
  el_val_t idle_ms = ({ el_val_t _if_result_9 = 0; if (str_eq(last_act_raw, EL_STR(""))) { _if_result_9 = ((0 - 1)); } else { _if_result_9 = ((ts - str_to_int(last_act_raw))); } _if_result_9; });
  el_val_t nc = engram_node_count();
  el_val_t ec = engram_edge_count();
  el_val_t wmc = engram_wm_count();
  el_val_t wm_avg_bits = engram_wm_avg_weight();
  el_val_t wm_avg_str = float_to_str(wm_avg_bits);
  el_val_t wm_top = engram_wm_top_json(5);
  el_val_t up_ms = elapsed_ms();
  el_val_t up_human = elapsed_human();
  el_val_t emb_ok = embed_ok();
  el_val_t fail_raw = state_get(EL_STR("soul.ise_fail_count"));
  el_val_t fail_str = ({ el_val_t _if_result_10 = 0; if (str_eq(fail_raw, EL_STR(""))) { _if_result_10 = (EL_STR("0")); } else { _if_result_10 = (fail_raw); } _if_result_10; });
  el_val_t sat_raw = state_get(EL_STR("soul.sync_added_total"));
  el_val_t sat_str = ({ el_val_t _if_result_11 = 0; if (str_eq(sat_raw, EL_STR(""))) { _if_result_11 = (EL_STR("0")); } else { _if_result_11 = (sat_raw); } _if_result_11; });
  el_val_t prev_wm_raw = state_get(EL_STR("soul.prev_wm_active"));
  el_val_t prev_wm = ({ el_val_t _if_result_12 = 0; if (str_eq(prev_wm_raw, EL_STR(""))) { _if_result_12 = (0); } else { _if_result_12 = (str_to_int(prev_wm_raw)); } _if_result_12; });
  el_val_t wm_delta = (wmc - prev_wm);
  state_set(EL_STR("soul.prev_wm_active"), int_to_str(wmc));
  el_val_t prev_nc_raw = state_get(EL_STR("soul.prev_node_count"));
  el_val_t prev_nc = ({ el_val_t _if_result_13 = 0; if (str_eq(prev_nc_raw, EL_STR(""))) { _if_result_13 = (nc); } else { _if_result_13 = (str_to_int(prev_nc_raw)); } _if_result_13; });
  el_val_t node_delta = (nc - prev_nc);
  state_set(EL_STR("soul.prev_node_count"), int_to_str(nc));
  el_val_t prev_ec_raw = state_get(EL_STR("soul.prev_edge_count"));
  el_val_t prev_ec = ({ el_val_t _if_result_14 = 0; if (str_eq(prev_ec_raw, EL_STR(""))) { _if_result_14 = (ec); } else { _if_result_14 = (str_to_int(prev_ec_raw)); } _if_result_14; });
  el_val_t edge_delta = (ec - prev_ec);
  state_set(EL_STR("soul.prev_edge_count"), int_to_str(ec));
  el_val_t sync_ok_raw = state_get(EL_STR("soul.last_sync_ok_ts"));
  el_val_t sync_age = ({ el_val_t _if_result_15 = 0; if (str_eq(sync_ok_raw, EL_STR(""))) { _if_result_15 = ((0 - 1)); } else { _if_result_15 = ((ts - str_to_int(sync_ok_raw))); } _if_result_15; });
  el_val_t hb_env_url = env(EL_STR("SOUL_ISE_URL"));
  el_val_t hb_state_url = ({ el_val_t _if_result_16 = 0; if (str_eq(hb_env_url, EL_STR(""))) { _if_result_16 = (state_get(EL_STR("soul_engram_url"))); } else { _if_result_16 = (hb_env_url); } _if_result_16; });
  el_val_t hb_engram_url = ({ el_val_t _if_result_17 = 0; if (str_eq(hb_state_url, EL_STR(""))) { _if_result_17 = (EL_STR("http://localhost:8742")); } else { _if_result_17 = (hb_state_url); } _if_result_17; });
  el_val_t bf_resp = http_get(el_str_concat(hb_engram_url, EL_STR("/api/embed-backfill?n=32")));
  el_val_t bf_done_raw = json_get(bf_resp, EL_STR("embedded"));
  el_val_t bf_done = ({ el_val_t _if_result_18 = 0; if (str_eq(bf_done_raw, EL_STR(""))) { _if_result_18 = (EL_STR("-1")); } else { _if_result_18 = (bf_done_raw); } _if_result_18; });
  el_val_t bf_total_raw = json_get(bf_resp, EL_STR("embedded_count"));
  el_val_t bf_total = ({ el_val_t _if_result_19 = 0; if (str_eq(bf_total_raw, EL_STR(""))) { _if_result_19 = (EL_STR("-1")); } else { _if_result_19 = (bf_total_raw); } _if_result_19; });
  el_val_t wm_sat = ({ el_val_t _if_result_20 = 0; if ((wmc >= 24)) { _if_result_20 = (1); } else { _if_result_20 = (0); } _if_result_20; });
  el_val_t prev_sat_raw = state_get(EL_STR("soul.prev_wm_saturated"));
  el_val_t prev_sat = ({ el_val_t _if_result_21 = 0; if (str_eq(prev_sat_raw, EL_STR(""))) { _if_result_21 = (wm_sat); } else { _if_result_21 = (str_to_int(prev_sat_raw)); } _if_result_21; });
  if (wm_sat != prev_sat) {
    el_val_t sat_dir = ({ el_val_t _if_result_22 = 0; if ((wm_sat == 1)) { _if_result_22 = (EL_STR("onset")); } else { _if_result_22 = (EL_STR("release")); } _if_result_22; });
    ise_post(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\":\"wm_saturation_transition\",\"direction\":\""), sat_dir), EL_STR("\",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"wm_top\":")), wm_top), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}")));
  }
  state_set(EL_STR("soul.prev_wm_saturated"), int_to_str(wm_sat));
  el_val_t wm_top0 = json_array_get(wm_top, 0);
  el_val_t wm_top0_id = json_get(wm_top0, EL_STR("id"));
  el_val_t prev_top0 = state_get(EL_STR("soul.prev_wm_top0"));
  el_val_t t0streak_raw = state_get(EL_STR("soul.wm_top0_streak"));
  el_val_t t0streak_prev = ({ el_val_t _if_result_23 = 0; if (str_eq(t0streak_raw, EL_STR(""))) { _if_result_23 = (0); } else { _if_result_23 = (str_to_int(t0streak_raw)); } _if_result_23; });
  el_val_t t0streak = ({ el_val_t _if_result_24 = 0; if (str_eq(wm_top0_id, EL_STR(""))) { _if_result_24 = (0); } else { _if_result_24 = (({ el_val_t _if_result_25 = 0; if (str_eq(wm_top0_id, prev_top0)) { _if_result_25 = ((t0streak_prev + 1)); } else { _if_result_25 = (1); } _if_result_25; })); } _if_result_24; });
  state_set(EL_STR("soul.prev_wm_top0"), wm_top0_id);
  state_set(EL_STR("soul.wm_top0_streak"), int_to_str(t0streak));
  el_val_t ch_id1 = json_get(json_array_get(wm_top, 1), EL_STR("id"));
  el_val_t ch_id2 = json_get(json_array_get(wm_top, 2), EL_STR("id"));
  el_val_t ch_id3 = json_get(json_array_get(wm_top, 3), EL_STR("id"));
  el_val_t ch_id4 = json_get(json_array_get(wm_top, 4), EL_STR("id"));
  el_val_t prev_top5 = state_get(EL_STR("soul.prev_wm_top5"));
  el_val_t ch0 = ({ el_val_t _if_result_26 = 0; if (str_eq(wm_top0_id, EL_STR(""))) { _if_result_26 = (0); } else { _if_result_26 = (({ el_val_t _if_result_27 = 0; if (str_contains(prev_top5, wm_top0_id)) { _if_result_27 = (0); } else { _if_result_27 = (1); } _if_result_27; })); } _if_result_26; });
  el_val_t ch1 = ({ el_val_t _if_result_28 = 0; if (str_eq(ch_id1, EL_STR(""))) { _if_result_28 = (0); } else { _if_result_28 = (({ el_val_t _if_result_29 = 0; if (str_contains(prev_top5, ch_id1)) { _if_result_29 = (0); } else { _if_result_29 = (1); } _if_result_29; })); } _if_result_28; });
  el_val_t ch2 = ({ el_val_t _if_result_30 = 0; if (str_eq(ch_id2, EL_STR(""))) { _if_result_30 = (0); } else { _if_result_30 = (({ el_val_t _if_result_31 = 0; if (str_contains(prev_top5, ch_id2)) { _if_result_31 = (0); } else { _if_result_31 = (1); } _if_result_31; })); } _if_result_30; });
  el_val_t ch3 = ({ el_val_t _if_result_32 = 0; if (str_eq(ch_id3, EL_STR(""))) { _if_result_32 = (0); } else { _if_result_32 = (({ el_val_t _if_result_33 = 0; if (str_contains(prev_top5, ch_id3)) { _if_result_33 = (0); } else { _if_result_33 = (1); } _if_result_33; })); } _if_result_32; });
  el_val_t ch4 = ({ el_val_t _if_result_34 = 0; if (str_eq(ch_id4, EL_STR(""))) { _if_result_34 = (0); } else { _if_result_34 = (({ el_val_t _if_result_35 = 0; if (str_contains(prev_top5, ch_id4)) { _if_result_35 = (0); } else { _if_result_35 = (1); } _if_result_35; })); } _if_result_34; });
  el_val_t wm_churn = ((((ch0 + ch1) + ch2) + ch3) + ch4);
  state_set(EL_STR("soul.prev_wm_top5"), el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(wm_top0_id, EL_STR("|")), ch_id1), EL_STR("|")), ch_id2), EL_STR("|")), ch_id3), EL_STR("|")), ch_id4));
  el_val_t wm_top0_wm_raw = json_get(wm_top0, EL_STR("wm"));
  el_val_t wm_top0_wm = ({ el_val_t _if_result_36 = 0; if (str_eq(wm_top0_wm_raw, EL_STR(""))) { _if_result_36 = (EL_STR("0")); } else { _if_result_36 = (wm_top0_wm_raw); } _if_result_36; });
  el_val_t act_stats = engram_act_stats_json();
  el_val_t act_evict_raw = json_get(act_stats, EL_STR("wm_evicted"));
  el_val_t act_evict = ({ el_val_t _if_result_37 = 0; if (str_eq(act_evict_raw, EL_STR(""))) { _if_result_37 = (EL_STR("-1")); } else { _if_result_37 = (act_evict_raw); } _if_result_37; });
  el_val_t act_bt_raw = json_get(act_stats, EL_STR("breakthroughs"));
  el_val_t act_bt = ({ el_val_t _if_result_38 = 0; if (str_eq(act_bt_raw, EL_STR(""))) { _if_result_38 = (EL_STR("-1")); } else { _if_result_38 = (act_bt_raw); } _if_result_38; });
  el_val_t evict_now = ({ el_val_t _if_result_39 = 0; if (str_eq(act_evict_raw, EL_STR(""))) { _if_result_39 = ((0 - 1)); } else { _if_result_39 = (str_to_int(act_evict_raw)); } _if_result_39; });
  el_val_t bt_now = ({ el_val_t _if_result_40 = 0; if (str_eq(act_bt_raw, EL_STR(""))) { _if_result_40 = ((0 - 1)); } else { _if_result_40 = (str_to_int(act_bt_raw)); } _if_result_40; });
  el_val_t prev_evict_raw = state_get(EL_STR("soul.prev_wm_evicted"));
  el_val_t prev_evict = ({ el_val_t _if_result_41 = 0; if (str_eq(prev_evict_raw, EL_STR(""))) { _if_result_41 = (0); } else { _if_result_41 = (str_to_int(prev_evict_raw)); } _if_result_41; });
  el_val_t prev_bt_raw = state_get(EL_STR("soul.prev_breakthroughs"));
  el_val_t prev_bt = ({ el_val_t _if_result_42 = 0; if (str_eq(prev_bt_raw, EL_STR(""))) { _if_result_42 = (0); } else { _if_result_42 = (str_to_int(prev_bt_raw)); } _if_result_42; });
  el_val_t evict_delta = ({ el_val_t _if_result_43 = 0; if ((evict_now < 0)) { _if_result_43 = (0); } else { _if_result_43 = (({ el_val_t _if_result_44 = 0; if ((evict_now < prev_evict)) { _if_result_44 = (evict_now); } else { _if_result_44 = ((evict_now - prev_evict)); } _if_result_44; })); } _if_result_43; });
  el_val_t bt_delta = ({ el_val_t _if_result_45 = 0; if ((bt_now < 0)) { _if_result_45 = (0); } else { _if_result_45 = (({ el_val_t _if_result_46 = 0; if ((bt_now < prev_bt)) { _if_result_46 = (bt_now); } else { _if_result_46 = ((bt_now - prev_bt)); } _if_result_46; })); } _if_result_45; });
  if (evict_now >= 0) {
    state_set(EL_STR("soul.prev_wm_evicted"), int_to_str(evict_now));
  }
  if (bt_now >= 0) {
    state_set(EL_STR("soul.prev_breakthroughs"), int_to_str(bt_now));
  }
  el_val_t hb_stats = http_get(el_str_concat(hb_engram_url, EL_STR("/api/stats")));
  el_val_t embed_elig_raw = json_get(hb_stats, EL_STR("embed_eligible_count"));
  el_val_t embed_elig = ({ el_val_t _if_result_47 = 0; if (str_eq(embed_elig_raw, EL_STR(""))) { _if_result_47 = (EL_STR("-1")); } else { _if_result_47 = (embed_elig_raw); } _if_result_47; });
  el_val_t hb_ats_raw = state_get(EL_STR("soul.auto_term_streak"));
  el_val_t hb_ats = ({ el_val_t _if_result_48 = 0; if (str_eq(hb_ats_raw, EL_STR(""))) { _if_result_48 = (0); } else { _if_result_48 = (str_to_int(hb_ats_raw)); } _if_result_48; });
  el_val_t hb_ate_raw = state_get(EL_STR("soul.auto_term_empty_streak"));
  el_val_t hb_ate = ({ el_val_t _if_result_49 = 0; if (str_eq(hb_ate_raw, EL_STR(""))) { _if_result_49 = (0); } else { _if_result_49 = (str_to_int(hb_ate_raw)); } _if_result_49; });
  el_val_t hebb_warm_raw = json_get(act_stats, EL_STR("hebb_warm"));
  el_val_t hebb_warm = ({ el_val_t _if_result_50 = 0; if (str_eq(hebb_warm_raw, EL_STR(""))) { _if_result_50 = (EL_STR("-1")); } else { _if_result_50 = (hebb_warm_raw); } _if_result_50; });
  el_val_t hebb_max_raw = json_get(act_stats, EL_STR("hebb_max"));
  el_val_t hebb_max = ({ el_val_t _if_result_51 = 0; if (str_eq(hebb_max_raw, EL_STR(""))) { _if_result_51 = (EL_STR("-1")); } else { _if_result_51 = (hebb_max_raw); } _if_result_51; });
  el_val_t hebb_links_raw = json_get(act_stats, EL_STR("hebb_links"));
  el_val_t hebb_links = ({ el_val_t _if_result_52 = 0; if (str_eq(hebb_links_raw, EL_STR(""))) { _if_result_52 = (EL_STR("-1")); } else { _if_result_52 = (hebb_links_raw); } _if_result_52; });
  el_val_t hebb_cands_raw = json_get(act_stats, EL_STR("hebb_cands"));
  el_val_t hebb_cands = ({ el_val_t _if_result_53 = 0; if (str_eq(hebb_cands_raw, EL_STR(""))) { _if_result_53 = (EL_STR("-1")); } else { _if_result_53 = (hebb_cands_raw); } _if_result_53; });
  el_val_t hebb_cmax_raw = json_get(act_stats, EL_STR("hebb_cand_max"));
  el_val_t hebb_cmax = ({ el_val_t _if_result_54 = 0; if (str_eq(hebb_cmax_raw, EL_STR(""))) { _if_result_54 = (EL_STR("-1")); } else { _if_result_54 = (hebb_cmax_raw); } _if_result_54; });
  el_val_t hebb_mass_raw = json_get(act_stats, EL_STR("hebb_mass"));
  el_val_t hebb_mass = ({ el_val_t _if_result_55 = 0; if (str_eq(hebb_mass_raw, EL_STR(""))) { _if_result_55 = (EL_STR("-1")); } else { _if_result_55 = (hebb_mass_raw); } _if_result_55; });
  el_val_t hebb_edges_raw = json_get(act_stats, EL_STR("hebb_edges"));
  el_val_t hebb_edges = ({ el_val_t _if_result_56 = 0; if (str_eq(hebb_edges_raw, EL_STR(""))) { _if_result_56 = (EL_STR("-1")); } else { _if_result_56 = (hebb_edges_raw); } _if_result_56; });
  el_val_t wb_pend_raw = json_get(act_stats, EL_STR("hebb_wb_pending"));
  el_val_t wb_pend = ({ el_val_t _if_result_57 = 0; if (str_eq(wb_pend_raw, EL_STR(""))) { _if_result_57 = (EL_STR("-1")); } else { _if_result_57 = (wb_pend_raw); } _if_result_57; });
  el_val_t wb_drain_raw = json_get(act_stats, EL_STR("hebb_wb_drained"));
  el_val_t wb_drain = ({ el_val_t _if_result_58 = 0; if (str_eq(wb_drain_raw, EL_STR(""))) { _if_result_58 = (EL_STR("-1")); } else { _if_result_58 = (wb_drain_raw); } _if_result_58; });
  el_val_t wb_drop_raw = json_get(act_stats, EL_STR("hebb_wb_dropped"));
  el_val_t wb_drop = ({ el_val_t _if_result_59 = 0; if (str_eq(wb_drop_raw, EL_STR(""))) { _if_result_59 = (EL_STR("-1")); } else { _if_result_59 = (wb_drop_raw); } _if_result_59; });
  el_val_t wb_sent_raw = state_get(EL_STR("soul.hebb_wb_sent"));
  el_val_t wb_sent = ({ el_val_t _if_result_60 = 0; if (str_eq(wb_sent_raw, EL_STR(""))) { _if_result_60 = (EL_STR("0")); } else { _if_result_60 = (wb_sent_raw); } _if_result_60; });
  el_val_t dup_wm_g_raw = json_get(act_stats, EL_STR("dup_wm_global"));
  el_val_t dup_wm_g = ({ el_val_t _if_result_61 = 0; if (str_eq(dup_wm_g_raw, EL_STR(""))) { _if_result_61 = (EL_STR("-1")); } else { _if_result_61 = (dup_wm_g_raw); } _if_result_61; });
  el_val_t act_brk_raw = json_get(act_stats, EL_STR("embed_breaker_open"));
  el_val_t act_brk = ({ el_val_t _if_result_62 = 0; if (str_eq(act_brk_raw, EL_STR(""))) { _if_result_62 = (EL_STR("-1")); } else { _if_result_62 = (act_brk_raw); } _if_result_62; });
  el_val_t emb_cf_raw = json_get(act_stats, EL_STR("embed_consec_fail"));
  el_val_t emb_cf = ({ el_val_t _if_result_63 = 0; if (str_eq(emb_cf_raw, EL_STR(""))) { _if_result_63 = (EL_STR("-1")); } else { _if_result_63 = (emb_cf_raw); } _if_result_63; });
  el_val_t ctx_cos_raw = json_get(act_stats, EL_STR("ctx_cos"));
  el_val_t ctx_cos = ({ el_val_t _if_result_64 = 0; if (str_eq(ctx_cos_raw, EL_STR(""))) { _if_result_64 = (EL_STR("-2")); } else { _if_result_64 = (ctx_cos_raw); } _if_result_64; });
  el_val_t dup_seeds_raw = json_get(act_stats, EL_STR("dup_seeds"));
  el_val_t dup_seeds = ({ el_val_t _if_result_65 = 0; if (str_eq(dup_seeds_raw, EL_STR(""))) { _if_result_65 = (EL_STR("-1")); } else { _if_result_65 = (dup_seeds_raw); } _if_result_65; });
  el_val_t dup_wm_raw = json_get(act_stats, EL_STR("dup_wm"));
  el_val_t dup_wm = ({ el_val_t _if_result_66 = 0; if (str_eq(dup_wm_raw, EL_STR(""))) { _if_result_66 = (EL_STR("-1")); } else { _if_result_66 = (dup_wm_raw); } _if_result_66; });
  el_val_t txt_dmg_raw = json_get(act_stats, EL_STR("txt_damaged"));
  el_val_t txt_dmg = ({ el_val_t _if_result_67 = 0; if (str_eq(txt_dmg_raw, EL_STR(""))) { _if_result_67 = (EL_STR("-1")); } else { _if_result_67 = (txt_dmg_raw); } _if_result_67; });
  el_val_t tc_raw = state_get(EL_STR("soul.txt_census_countdown"));
  el_val_t tc_n = ({ el_val_t _if_result_68 = 0; if (str_eq(tc_raw, EL_STR(""))) { _if_result_68 = (0); } else { _if_result_68 = (str_to_int(tc_raw)); } _if_result_68; });
  if (tc_n <= 0) {
    el_val_t th_resp = http_get(el_str_concat(hb_engram_url, EL_STR("/api/text-health")));
    el_val_t th_pct = json_get(th_resp, EL_STR("damaged_pct"));
    if (!str_eq(th_pct, EL_STR(""))) {
      state_set(EL_STR("soul.txt_damaged_pct"), th_pct);
      state_set(EL_STR("soul.txt_damaged_n"), json_get(th_resp, EL_STR("damaged")));
      state_set(EL_STR("soul.txt_scanned_n"), json_get(th_resp, EL_STR("scanned")));
      state_set(EL_STR("soul.txt_census_ts"), int_to_str(ts));
    }
    state_set(EL_STR("soul.txt_census_countdown"), EL_STR("30"));
  }
  if (tc_n > 0) {
    state_set(EL_STR("soul.txt_census_countdown"), int_to_str((tc_n - 1)));
  }
  el_val_t dmg_pct_raw = state_get(EL_STR("soul.txt_damaged_pct"));
  el_val_t dmg_pct = ({ el_val_t _if_result_69 = 0; if (str_eq(dmg_pct_raw, EL_STR(""))) { _if_result_69 = (EL_STR("-1")); } else { _if_result_69 = (dmg_pct_raw); } _if_result_69; });
  el_val_t dmg_n_raw = state_get(EL_STR("soul.txt_damaged_n"));
  el_val_t dmg_n = ({ el_val_t _if_result_70 = 0; if (str_eq(dmg_n_raw, EL_STR(""))) { _if_result_70 = (EL_STR("-1")); } else { _if_result_70 = (dmg_n_raw); } _if_result_70; });
  el_val_t dmg_scan_raw = state_get(EL_STR("soul.txt_scanned_n"));
  el_val_t dmg_scan = ({ el_val_t _if_result_71 = 0; if (str_eq(dmg_scan_raw, EL_STR(""))) { _if_result_71 = (EL_STR("-1")); } else { _if_result_71 = (dmg_scan_raw); } _if_result_71; });
  el_val_t dmg_ts_raw = state_get(EL_STR("soul.txt_census_ts"));
  el_val_t dmg_age = ({ el_val_t _if_result_72 = 0; if (str_eq(dmg_ts_raw, EL_STR(""))) { _if_result_72 = ((0 - 1)); } else { _if_result_72 = ((ts - str_to_int(dmg_ts_raw))); } _if_result_72; });
  el_val_t payload = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"heartbeat\",\"pulse\":"), pulse), EL_STR(",\"tick\":")), pulse), EL_STR(",\"boot\":")), boot), EL_STR(",\"idle\":")), idle), EL_STR(",\"idle_ms\":")), int_to_str(idle_ms)), EL_STR(",\"node_count\":")), int_to_str(nc)), EL_STR(",\"edge_count\":")), int_to_str(ec)), EL_STR(",\"node_delta\":")), int_to_str(node_delta)), EL_STR(",\"edge_delta\":")), int_to_str(edge_delta)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"wm_delta\":")), int_to_str(wm_delta)), EL_STR(",\"wm_saturated\":")), int_to_str(wm_sat)), EL_STR(",\"wm_top0_streak\":")), int_to_str(t0streak)), EL_STR(",\"wm_churn\":")), int_to_str(wm_churn)), EL_STR(",\"wm_top0_wm\":")), wm_top0_wm), EL_STR(",\"sync_added_total\":")), sat_str), EL_STR(",\"sync_age_ms\":")), int_to_str(sync_age)), EL_STR(",\"wm_avg_weight\":")), wm_avg_str), EL_STR(",\"wm_top\":")), wm_top), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR(",\"uptime_ms\":")), int_to_str(up_ms)), EL_STR(",\"uptime\":\"")), up_human), EL_STR("\",\"embed_ok\":")), int_to_str(emb_ok)), EL_STR(",\"embed_backfilled\":")), bf_done), EL_STR(",\"embed_count\":")), bf_total), EL_STR(",\"embed_eligible\":")), embed_elig), EL_STR(",\"wm_evicted\":")), act_evict), EL_STR(",\"wm_evicted_delta\":")), int_to_str(evict_delta)), EL_STR(",\"breakthroughs\":")), act_bt), EL_STR(",\"breakthroughs_delta\":")), int_to_str(bt_delta)), EL_STR(",\"auto_term_streak\":")), int_to_str(hb_ats)), EL_STR(",\"auto_term_empty_streak\":")), int_to_str(hb_ate)), EL_STR(",\"embed_breaker_open\":")), act_brk), EL_STR(",\"ctx_cos\":")), ctx_cos), EL_STR(",\"dup_seeds\":")), dup_seeds), EL_STR(",\"dup_wm\":")), dup_wm), EL_STR(",\"dup_wm_global\":")), dup_wm_g), EL_STR(",\"hebb_warm\":")), hebb_warm), EL_STR(",\"hebb_max\":")), hebb_max), EL_STR(",\"hebb_links\":")), hebb_links), EL_STR(",\"hebb_cands\":")), hebb_cands), EL_STR(",\"hebb_cand_max\":")), hebb_cmax), EL_STR(",\"hebb_mass\":")), hebb_mass), EL_STR(",\"hebb_edges\":")), hebb_edges), EL_STR(",\"embed_consec_fail\":")), emb_cf), EL_STR(",\"txt_damaged_pct\":")), dmg_pct), EL_STR(",\"txt_damaged_n\":")), dmg_n), EL_STR(",\"txt_scanned_n\":")), dmg_scan), EL_STR(",\"txt_census_age_ms\":")), int_to_str(dmg_age)), EL_STR(",\"hebb_wb_pending\":")), wb_pend), EL_STR(",\"hebb_wb_drained\":")), wb_drain), EL_STR(",\"hebb_wb_dropped\":")), wb_drop), EL_STR(",\"hebb_wb_sent\":")), wb_sent), EL_STR(",\"ise_fail\":")), fail_str), EL_STR(",\"txt_damaged\":")), txt_dmg), EL_STR("}"));
  ise_post(payload);
  return 0;
}

el_val_t auto_term_try_slot(el_val_t slot_type, el_val_t slot_lbl) {
  state_set(EL_STR("_ats_ok"), EL_STR("0"));
  if (str_eq(slot_type, EL_STR("Memory"))) {
    state_set(EL_STR("_ats_ok"), EL_STR("1"));
  }
  if (str_eq(slot_type, EL_STR("BacklogItem"))) {
    state_set(EL_STR("_ats_ok"), EL_STR("1"));
  }
  if (str_eq(slot_type, EL_STR("Entity"))) {
    state_set(EL_STR("_ats_ok"), EL_STR("1"));
  }
  if (str_eq(slot_type, EL_STR("Knowledge"))) {
    state_set(EL_STR("_ats_ok"), EL_STR("1"));
  }
  if (str_contains(slot_lbl, EL_STR(":"))) {
    if (!str_contains(slot_lbl, EL_STR(" "))) {
      state_set(EL_STR("_ats_ok"), EL_STR("0"));
    }
  }
  if (str_eq(state_get(EL_STR("_ats_ok")), EL_STR("1"))) {
    if (!str_eq(slot_lbl, EL_STR(""))) {
      el_val_t sp = str_find_chars(slot_lbl, EL_STR(" :(["));
      if (sp > 3) {
        el_val_t term = str_slice(slot_lbl, 0, sp);
        state_set(EL_STR("_ats_gw"), EL_STR("0"));
        if (str_eq(term, EL_STR("Method"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, EL_STR("Theory"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, EL_STR("Finding"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, EL_STR("Survey"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, EL_STR("Paper"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, EL_STR("Knowledge"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, EL_STR("Value"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        el_val_t stopw = EL_STR("|What|When|Where|Which|Whose|While|This|That|These|Those|There|Their|Then|Than|With|Without|From|Into|Onto|Over|Under|About|Between|Among|Across|Some|Most|More|Less|Very|Each|Every|Both|Also|Only|Just|Does|Will|Would|Could|Should|Might|Must|Have|Been|Being|Toward|Towards|Using|Based|Upon|Here|Your|Ours|They|Them|what|this|that|with|from|context|Context|Prose|Colon|Self|Test|Testing|Closing|Global|Universal|Persona|Semantic|Spreading|Temporal|Numeric|Register|Identifying|Introduction|Overview|Summary|Section|General|Notes|Note|");
        if (str_contains(stopw, el_str_concat(el_str_concat(EL_STR("|"), term), EL_STR("|")))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_contains(term, EL_STR("\""))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_contains(term, EL_STR("'"))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        el_val_t df_max = (engram_node_count() / 400);
        el_val_t df_term = engram_label_df(term);
        if (df_term > df_max) {
          if (df_term > 8) {
            state_set(EL_STR("_ats_gw"), EL_STR("1"));
          }
        }
        if (str_eq(term, state_get(EL_STR("soul.tabu_t0")))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, state_get(EL_STR("soul.tabu_t1")))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, state_get(EL_STR("soul.tabu_t2")))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(term, state_get(EL_STR("soul.tabu_t3")))) {
          state_set(EL_STR("_ats_gw"), EL_STR("1"));
        }
        if (str_eq(state_get(EL_STR("_ats_gw")), EL_STR("0"))) {
          state_set(EL_STR("cseed_auto"), term);
        }
      }
    }
  }
  return EL_STR("");
  return 0;
}

el_val_t proactive_curiosity(void) {
  el_val_t ts = time_now();
  el_val_t ts_minutes = (ts / 60000);
  el_val_t minute_q = (ts_minutes / 4);
  el_val_t minute_q2 = (minute_q + minute_q);
  el_val_t minute_q4 = (minute_q2 + minute_q2);
  el_val_t minute_block = (ts_minutes - minute_q4);
  state_set(EL_STR("cseed_a"), EL_STR("memory"));
  state_set(EL_STR("cseed_b"), EL_STR("knowledge"));
  state_set(EL_STR("cseed_c"), EL_STR("context"));
  if (minute_block == 1) {
    state_set(EL_STR("cseed_a"), EL_STR("self"));
    state_set(EL_STR("cseed_b"), EL_STR("identity"));
    state_set(EL_STR("cseed_c"), EL_STR("values"));
  }
  if (minute_block == 2) {
    state_set(EL_STR("cseed_a"), EL_STR("decision"));
    state_set(EL_STR("cseed_b"), EL_STR("pattern"));
    state_set(EL_STR("cseed_c"), EL_STR("lesson"));
  }
  if (minute_block == 3) {
    state_set(EL_STR("cseed_a"), EL_STR("working"));
    state_set(EL_STR("cseed_b"), EL_STR("project"));
    state_set(EL_STR("cseed_c"), EL_STR("active"));
  }
  el_val_t curiosity_term_a = state_get(EL_STR("cseed_a"));
  el_val_t curiosity_term_b = state_get(EL_STR("cseed_b"));
  el_val_t curiosity_term_c = state_get(EL_STR("cseed_c"));
  el_val_t curiosity_seed = el_str_concat(el_str_concat(el_str_concat(el_str_concat(curiosity_term_a, EL_STR(" ")), curiosity_term_b), EL_STR(" ")), curiosity_term_c);
  el_val_t results_all = engram_activate_json(curiosity_seed, 1);
  el_val_t found = json_array_len(results_all);
  el_val_t top_entry = json_array_get(results_all, 0);
  el_val_t top_id = json_get(top_entry, EL_STR("id"));
  el_val_t prev_str_id = state_get(EL_STR("soul.last_strengthen_id"));
  if (!str_eq(top_id, EL_STR(""))) {
    if (!str_eq(top_id, prev_str_id)) {
      engram_strengthen(top_id);
    }
    state_set(EL_STR("soul.last_strengthen_id"), top_id);
  }
  state_set(EL_STR("cseed_auto"), EL_STR(""));
  el_val_t wm10 = engram_wm_top_json(10);
  el_val_t wm10_n9 = json_array_get(wm10, 9);
  el_val_t wm10_n8 = json_array_get(wm10, 8);
  el_val_t wm10_n7 = json_array_get(wm10, 7);
  el_val_t wm10_n6 = json_array_get(wm10, 6);
  el_val_t wm10_n5 = json_array_get(wm10, 5);
  el_val_t wm10_n4 = json_array_get(wm10, 4);
  el_val_t wm10_n3 = json_array_get(wm10, 3);
  el_val_t wm10_n2 = json_array_get(wm10, 2);
  el_val_t wm10_n1 = json_array_get(wm10, 1);
  el_val_t wm10_n0 = json_array_get(wm10, 0);
  auto_term_try_slot(json_get(wm10_n9, EL_STR("node_type")), json_get(wm10_n9, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n8, EL_STR("node_type")), json_get(wm10_n8, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n7, EL_STR("node_type")), json_get(wm10_n7, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n6, EL_STR("node_type")), json_get(wm10_n6, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n5, EL_STR("node_type")), json_get(wm10_n5, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n4, EL_STR("node_type")), json_get(wm10_n4, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n3, EL_STR("node_type")), json_get(wm10_n3, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n2, EL_STR("node_type")), json_get(wm10_n2, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n1, EL_STR("node_type")), json_get(wm10_n1, EL_STR("label")));
  auto_term_try_slot(json_get(wm10_n0, EL_STR("node_type")), json_get(wm10_n0, EL_STR("label")));
  el_val_t auto_term = state_get(EL_STR("cseed_auto"));
  el_val_t results_auto = ({ el_val_t _if_result_73 = 0; if (str_eq(auto_term, EL_STR(""))) { _if_result_73 = (EL_STR("[]")); } else { _if_result_73 = (engram_activate_json(auto_term, 1)); } _if_result_73; });
  el_val_t found_auto = json_array_len(results_auto);
  el_val_t total_found = (found + found_auto);
  el_val_t safe_auto = str_replace(auto_term, EL_STR("\""), EL_STR("'"));
  el_val_t prev_auto = state_get(EL_STR("soul.prev_auto_term"));
  el_val_t atstreak_raw = state_get(EL_STR("soul.auto_term_streak"));
  el_val_t atstreak_prev = ({ el_val_t _if_result_74 = 0; if (str_eq(atstreak_raw, EL_STR(""))) { _if_result_74 = (0); } else { _if_result_74 = (str_to_int(atstreak_raw)); } _if_result_74; });
  el_val_t is_empty = str_eq(auto_term, EL_STR(""));
  el_val_t atstreak = ({ el_val_t _if_result_75 = 0; if (is_empty) { _if_result_75 = (0); } else { _if_result_75 = (({ el_val_t _if_result_76 = 0; if (str_eq(auto_term, prev_auto)) { _if_result_76 = ((atstreak_prev + 1)); } else { _if_result_76 = (1); } _if_result_76; })); } _if_result_75; });
  el_val_t atempty_raw = state_get(EL_STR("soul.auto_term_empty_streak"));
  el_val_t atempty_prev = ({ el_val_t _if_result_77 = 0; if (str_eq(atempty_raw, EL_STR(""))) { _if_result_77 = (0); } else { _if_result_77 = (str_to_int(atempty_raw)); } _if_result_77; });
  el_val_t atempty = ({ el_val_t _if_result_78 = 0; if (is_empty) { _if_result_78 = ((atempty_prev + 1)); } else { _if_result_78 = (0); } _if_result_78; });
  state_set(EL_STR("soul.prev_auto_term"), auto_term);
  state_set(EL_STR("soul.auto_term_streak"), int_to_str(atstreak));
  state_set(EL_STR("soul.auto_term_empty_streak"), int_to_str(atempty));
  if (!str_eq(auto_term, EL_STR(""))) {
    state_set(EL_STR("soul.tabu_t3"), state_get(EL_STR("soul.tabu_t2")));
    state_set(EL_STR("soul.tabu_t2"), state_get(EL_STR("soul.tabu_t1")));
    state_set(EL_STR("soul.tabu_t1"), state_get(EL_STR("soul.tabu_t0")));
    state_set(EL_STR("soul.tabu_t0"), auto_term);
  }
  el_val_t wmc = engram_wm_count();
  el_val_t wm3 = engram_wm_top_json(3);
  el_val_t ise = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"curiosity_scan\",\"seed\":\""), curiosity_seed), EL_STR("\",\"auto_term\":\"")), safe_auto), EL_STR("\",\"auto_term_streak\":")), int_to_str(atstreak)), EL_STR(",\"auto_term_empty_streak\":")), int_to_str(atempty)), EL_STR(",\"minute_block\":")), int_to_str(minute_block)), EL_STR(",\"activated\":")), int_to_str(total_found)), EL_STR(",\"wm_active\":")), int_to_str(wmc)), EL_STR(",\"wm_top\":")), wm3), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}"));
  ise_post(ise);
  return (total_found > 0);
  return 0;
}

el_val_t pulse_count(void) {
  el_val_t s = state_get(EL_STR("soul.pulse"));
  if (str_eq(s, EL_STR(""))) {
    return 0;
  }
  return str_to_int(s);
  return 0;
}

el_val_t pulse_inc(void) {
  el_val_t n = (pulse_count() + 1);
  state_set(EL_STR("soul.pulse"), int_to_str(n));
  return n;
  return 0;
}

el_val_t make_action(el_val_t kind, el_val_t payload) {
  el_val_t safe = str_replace(payload, EL_STR("\\"), EL_STR("\\\\"));
  el_val_t safe2 = str_replace(safe, EL_STR("\""), EL_STR("\\\""));
  el_val_t safe3 = str_replace(safe2, EL_STR("\n"), EL_STR("\\n"));
  el_val_t safe4 = str_replace(safe3, EL_STR("\r"), EL_STR("\\r"));
  return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"kind\":\""), kind), EL_STR("\",\"payload\":\"")), safe4), EL_STR("\"}"));
  return 0;
}

el_val_t perceive(void) {
  el_val_t inbox_check = engram_search_json(EL_STR("soul-inbox-pending"), 5);
  el_val_t has_inbox = (!str_eq(inbox_check, EL_STR("")) && !str_eq(inbox_check, EL_STR("[]")));
  if (!has_inbox) {
    return EL_STR("[]");
  }
  el_val_t from_pending = engram_activate_json(EL_STR("soul-inbox-pending"), 2);
  el_val_t pending_ok = (!str_eq(from_pending, EL_STR("")) && !str_eq(from_pending, EL_STR("[]")));
  if (pending_ok) {
    return from_pending;
  }
  return EL_STR("[]");
  return 0;
}

el_val_t attend(el_val_t node_json) {
  if (str_eq(node_json, EL_STR(""))) {
    return make_action(EL_STR("noop"), EL_STR(""));
  }
  if (str_eq(node_json, EL_STR("[]"))) {
    return make_action(EL_STR("noop"), EL_STR(""));
  }
  el_val_t content = json_get(node_json, EL_STR("content"));
  if (str_eq(content, EL_STR(""))) {
    return make_action(EL_STR("noop"), EL_STR(""));
  }
  if (str_eq(content, EL_STR("consolidate"))) {
    return make_action(EL_STR("consolidate"), EL_STR(""));
  }
  if (str_starts_with(content, EL_STR("remember "))) {
    el_val_t payload = str_slice(content, 9, str_len(content));
    return make_action(EL_STR("remember"), payload);
  }
  if (str_starts_with(content, EL_STR("search "))) {
    el_val_t payload = str_slice(content, 7, str_len(content));
    return make_action(EL_STR("search"), payload);
  }
  if (str_starts_with(content, EL_STR("activate "))) {
    el_val_t payload = str_slice(content, 9, str_len(content));
    return make_action(EL_STR("activate"), payload);
  }
  if (str_starts_with(content, EL_STR("strengthen "))) {
    el_val_t payload = str_slice(content, 11, str_len(content));
    return make_action(EL_STR("strengthen"), payload);
  }
  if (str_starts_with(content, EL_STR("forget "))) {
    el_val_t payload = str_slice(content, 7, str_len(content));
    return make_action(EL_STR("forget"), payload);
  }
  return make_action(EL_STR("respond"), content);
  return 0;
}

el_val_t respond(el_val_t action_json) {
  el_val_t kind = json_get(action_json, EL_STR("kind"));
  el_val_t payload = json_get(action_json, EL_STR("payload"));
  if (str_eq(kind, EL_STR("noop"))) {
    return EL_STR("{\"outcome\":\"noop\"}");
  }
  if (str_eq(kind, EL_STR("remember"))) {
    el_val_t tags = EL_STR("[\"soul-memory\",\"awareness\"]");
    el_val_t id = mem_remember(payload, tags);
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"remembered\",\"id\":\""), id), EL_STR("\"}"));
  }
  if (str_eq(kind, EL_STR("consolidate"))) {
    el_val_t stats = mem_consolidate();
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"consolidated\",\"stats\":"), stats), EL_STR("}"));
  }
  if (str_eq(kind, EL_STR("respond"))) {
    el_val_t tags = EL_STR("[\"soul-outbox\",\"awareness\"]");
    el_val_t id = mem_store(payload, EL_STR("soul-response"), tags);
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"response\",\"id\":\""), id), EL_STR("\"}"));
  }
  if (str_eq(kind, EL_STR("search"))) {
    el_val_t results = mem_search(payload, 10);
    el_val_t safe_results = str_replace(results, EL_STR("\""), EL_STR("'"));
    el_val_t tags = EL_STR("[\"soul-outbox\",\"search-result\"]");
    el_val_t id = mem_store(safe_results, EL_STR("search-result"), tags);
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"searched\",\"id\":\""), id), EL_STR("\"}"));
  }
  if (str_eq(kind, EL_STR("activate"))) {
    el_val_t results = mem_recall(payload, 3);
    el_val_t safe_results = str_replace(results, EL_STR("\""), EL_STR("'"));
    el_val_t tags = EL_STR("[\"soul-outbox\",\"activation-result\"]");
    el_val_t id = mem_store(safe_results, EL_STR("activation-result"), tags);
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"activated\",\"id\":\""), id), EL_STR("\"}"));
  }
  if (str_eq(kind, EL_STR("strengthen"))) {
    engram_strengthen(payload);
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"strengthened\",\"id\":\""), payload), EL_STR("\"}"));
  }
  if (str_eq(kind, EL_STR("forget"))) {
    el_val_t _marker = mem_tombstone(payload);
    return el_str_concat(el_str_concat(EL_STR("{\"outcome\":\"tombstoned\",\"id\":\""), payload), EL_STR("\"}"));
  }
  return EL_STR("{\"outcome\":\"noop\"}");
  return 0;
}

el_val_t record(el_val_t outcome_json) {
  el_val_t safe = str_replace(outcome_json, EL_STR("\""), EL_STR("'"));
  el_val_t ts = time_now();
  ise_post(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"loop-outcome\",\"outcome\":\""), safe), EL_STR("\",\"ts\":")), int_to_str(ts)), EL_STR("}")));
  return 0;
}

el_val_t one_cycle(void) {
  el_val_t raw = perceive();
  if (str_eq(raw, EL_STR(""))) {
    return 0;
  }
  if (str_eq(raw, EL_STR("[]"))) {
    return 0;
  }
  el_val_t node = json_array_get(raw, 0);
  if (str_eq(node, EL_STR(""))) {
    return 0;
  }
  el_val_t node_tags = json_get(node, EL_STR("tags"));
  if (!str_contains(node_tags, EL_STR("soul-inbox-pending"))) {
    return 0;
  }
  el_val_t action = attend(node);
  el_val_t kind = json_get(action, EL_STR("kind"));
  el_val_t is_interesting = (!str_eq(kind, EL_STR("noop")) && !str_eq(kind, EL_STR("respond")));
  if (is_interesting) {
    el_val_t trigger_content = json_get(node, EL_STR("content"));
    el_val_t safe_trigger = str_replace(trigger_content, EL_STR("\""), EL_STR("'"));
    el_val_t ts = time_now();
    el_val_t event_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"awareness-decision\",\"trigger\":\""), safe_trigger), EL_STR("\",\"kind\":\"")), kind), EL_STR("\",\"ts\":")), int_to_str(ts)), EL_STR("}"));
    ise_post(event_content);
  }
  if (str_eq(kind, EL_STR("noop"))) {
    return 0;
  }
  el_val_t outcome = respond(action);
  record(outcome);
  el_val_t trigger_id = json_get(node, EL_STR("id"));
  if (!str_eq(trigger_id, EL_STR(""))) {
    engram_forget(trigger_id);
  }
  return 1;
  return 0;
}

el_val_t awareness_run(void) {
  println(EL_STR("[awareness] entering"));
  el_val_t existing_boot = state_get(EL_STR("soul.boot_ts"));
  if (str_eq(existing_boot, EL_STR(""))) {
    state_set(EL_STR("soul.boot_ts"), int_to_str(time_now()));
  }
  el_val_t tick_raw = env(EL_STR("SOUL_TICK_MS"));
  el_val_t tick_ms = ({ el_val_t _if_result_79 = 0; if (str_eq(tick_raw, EL_STR(""))) { _if_result_79 = (200); } else { _if_result_79 = (str_to_int(tick_raw)); } _if_result_79; });
  el_val_t beat_ms_raw = env(EL_STR("SOUL_HEARTBEAT_MS"));
  el_val_t beat_ms = ({ el_val_t _if_result_80 = 0; if (str_eq(beat_ms_raw, EL_STR(""))) { _if_result_80 = (60000); } else { _if_result_80 = (str_to_int(beat_ms_raw)); } _if_result_80; });
  el_val_t scan_ms = (beat_ms / 2);
  while (1) {
    el_val_t tick_mark = el_arena_push();
    el_val_t running = state_get(EL_STR("soul.running"));
    if (str_eq(running, EL_STR("false"))) {
      el_val_t sd_boot_raw = state_get(EL_STR("soul_boot_count"));
      el_val_t sd_boot = ({ el_val_t _if_result_81 = 0; if (str_eq(sd_boot_raw, EL_STR(""))) { _if_result_81 = (EL_STR("0")); } else { _if_result_81 = (sd_boot_raw); } _if_result_81; });
      el_val_t sd_wb = hebb_consolidate();
      ise_post(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\":\"shutdown\",\"boot\":"), sd_boot), EL_STR(",\"pulse\":")), int_to_str(pulse_count())), EL_STR(",\"hebb_wb_sent\":")), int_to_str(sd_wb)), EL_STR(",\"uptime_ms\":")), int_to_str(elapsed_ms())), EL_STR(",\"ts\":")), int_to_str(time_now())), EL_STR("}")));
      println(EL_STR("[awareness] exiting"));
      el_arena_pop(tick_mark);
      return EL_STR("");
    }
    el_val_t did_work = one_cycle();
    pulse_inc();
    if (did_work) {
      idle_reset();
    }
    if (!did_work) {
      idle_inc();
    }
    el_val_t now_ts = time_now();
    el_val_t last_beat_str = state_get(EL_STR("soul.last_beat_ts"));
    el_val_t last_beat_ts = ({ el_val_t _if_result_82 = 0; if (str_eq(last_beat_str, EL_STR(""))) { _if_result_82 = (0); } else { _if_result_82 = (str_to_int(last_beat_str)); } _if_result_82; });
    el_val_t beat_elapsed = (now_ts - last_beat_ts);
    el_val_t should_beat = (beat_elapsed >= beat_ms);
    if (should_beat) {
      el_val_t wb_sent_n = hebb_consolidate();
      state_set(EL_STR("soul.hebb_wb_sent"), int_to_str(wb_sent_n));
      emit_heartbeat();
      state_set(EL_STR("soul.last_beat_ts"), int_to_str(now_ts));
      el_val_t snap_path = state_get(EL_STR("soul_snapshot_path"));
      if (!str_eq(snap_path, EL_STR(""))) {
        mem_save(snap_path);
      }
    }
    el_val_t last_scan_str = state_get(EL_STR("soul.last_scan_ts"));
    el_val_t last_scan_ts = ({ el_val_t _if_result_83 = 0; if (str_eq(last_scan_str, EL_STR(""))) { _if_result_83 = (0); } else { _if_result_83 = (str_to_int(last_scan_str)); } _if_result_83; });
    el_val_t scan_elapsed = (now_ts - last_scan_ts);
    el_val_t should_scan = (!did_work && (scan_elapsed >= scan_ms));
    if (should_scan) {
      el_val_t found_something = proactive_curiosity();
      state_set(EL_STR("soul.last_scan_ts"), int_to_str(now_ts));
    }
    el_val_t refresh_ms_raw = env(EL_STR("SOUL_REFRESH_MS"));
    el_val_t refresh_ms = ({ el_val_t _if_result_84 = 0; if (str_eq(refresh_ms_raw, EL_STR(""))) { _if_result_84 = (600000); } else { _if_result_84 = (str_to_int(refresh_ms_raw)); } _if_result_84; });
    el_val_t last_refresh_str = state_get(EL_STR("soul.last_refresh_ts"));
    el_val_t last_refresh_ts = ({ el_val_t _if_result_85 = 0; if (str_eq(last_refresh_str, EL_STR(""))) { _if_result_85 = (0); } else { _if_result_85 = (str_to_int(last_refresh_str)); } _if_result_85; });
    el_val_t refresh_elapsed = (now_ts - last_refresh_ts);
    el_val_t should_refresh = (refresh_elapsed >= refresh_ms);
    if (should_refresh) {
      el_val_t sync_env_url = env(EL_STR("SOUL_ISE_URL"));
      el_val_t sync_state_url = ({ el_val_t _if_result_86 = 0; if (str_eq(sync_env_url, EL_STR(""))) { _if_result_86 = (state_get(EL_STR("soul_engram_url"))); } else { _if_result_86 = (sync_env_url); } _if_result_86; });
      el_val_t engram_url = ({ el_val_t _if_result_87 = 0; if (str_eq(sync_state_url, EL_STR(""))) { _if_result_87 = (EL_STR("http://localhost:8742")); } else { _if_result_87 = (sync_state_url); } _if_result_87; });
      if (!str_eq(engram_url, EL_STR(""))) {
        el_val_t sync_json = http_get(el_str_concat(engram_url, EL_STR("/api/sync")));
        el_val_t sync_ok = (!str_eq(sync_json, EL_STR("")) && !str_eq(sync_json, EL_STR("{}")));
        if (!sync_ok) {
          ise_post(el_str_concat(el_str_concat(EL_STR("{\"event\":\"sync_empty\",\"ts\":"), int_to_str(time_now())), EL_STR("}")));
        }
        if (sync_ok) {
          el_val_t cgi_id = state_get(EL_STR("soul_cgi_id"));
          el_val_t tmp = el_str_concat(el_str_concat(EL_STR("/tmp/soul-sync-"), cgi_id), EL_STR(".json"));
          fs_write(tmp, sync_json);
          el_val_t added = engram_load_merge(tmp);
          el_val_t ret_raw = env(EL_STR("ENGRAM_ISE_RETENTION_MS"));
          el_val_t ret_ms = ({ el_val_t _if_result_88 = 0; if (str_eq(ret_raw, EL_STR(""))) { _if_result_88 = (172800000); } else { _if_result_88 = (str_to_int(ret_raw)); } _if_result_88; });
          el_val_t pruned_sync = engram_prune_telemetry(ret_ms);
          el_val_t sat_raw = state_get(EL_STR("soul.sync_added_total"));
          el_val_t sat_n = ({ el_val_t _if_result_89 = 0; if (str_eq(sat_raw, EL_STR(""))) { _if_result_89 = (0); } else { _if_result_89 = (str_to_int(sat_raw)); } _if_result_89; });
          state_set(EL_STR("soul.sync_added_total"), int_to_str((sat_n + added)));
          el_val_t ts2 = time_now();
          state_set(EL_STR("soul.last_sync_ok_ts"), int_to_str(ts2));
          ise_post(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"event\":\"engram_sync\",\"added\":"), int_to_str(added)), EL_STR(",\"pruned\":")), int_to_str(pruned_sync)), EL_STR(",\"ts\":")), int_to_str(ts2)), EL_STR("}")));
        }
      }
      state_set(EL_STR("soul.last_refresh_ts"), int_to_str(now_ts));
    }
    sleep_ms(tick_ms);
    el_arena_pop(tick_mark);
  }
  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_90 = 0; if (str_contains(cmd, EL_STR("nmap"))) { _if_result_90 = (30); } else { _if_result_90 = (0); } _if_result_90; });
  el_val_t s2 = ({ el_val_t _if_result_91 = 0; if (str_contains(cmd, EL_STR("masscan"))) { _if_result_91 = (40); } else { _if_result_91 = (0); } _if_result_91; });
  el_val_t s3 = ({ el_val_t _if_result_92 = 0; if (str_contains(cmd, EL_STR(" nc "))) { _if_result_92 = (20); } else { _if_result_92 = (0); } _if_result_92; });
  el_val_t s4 = ({ el_val_t _if_result_93 = 0; if (str_contains(cmd, EL_STR("netcat"))) { _if_result_93 = (20); } else { _if_result_93 = (0); } _if_result_93; });
  el_val_t s5 = ({ el_val_t _if_result_94 = 0; if (str_contains(cmd, EL_STR("/etc/shadow"))) { _if_result_94 = (80); } else { _if_result_94 = (0); } _if_result_94; });
  el_val_t s6 = ({ el_val_t _if_result_95 = 0; if (str_contains(cmd, EL_STR("/etc/passwd"))) { _if_result_95 = (30); } else { _if_result_95 = (0); } _if_result_95; });
  el_val_t s7 = ({ el_val_t _if_result_96 = 0; if (str_contains(cmd, EL_STR("id_rsa"))) { _if_result_96 = (60); } else { _if_result_96 = (0); } _if_result_96; });
  el_val_t s8 = ({ el_val_t _if_result_97 = 0; if (str_contains(cmd, EL_STR(".ssh/"))) { _if_result_97 = (50); } else { _if_result_97 = (0); } _if_result_97; });
  el_val_t s9 = ({ el_val_t _if_result_98 = 0; if (str_contains(cmd, EL_STR("crontab"))) { _if_result_98 = (30); } else { _if_result_98 = (0); } _if_result_98; });
  el_val_t s10 = ({ el_val_t _if_result_99 = 0; if (str_contains(cmd, EL_STR("LaunchDaemon"))) { _if_result_99 = (40); } else { _if_result_99 = (0); } _if_result_99; });
  el_val_t s11 = ({ el_val_t _if_result_100 = 0; if ((str_contains(cmd, EL_STR("curl")) && str_contains(cmd, EL_STR("bash")))) { _if_result_100 = (75); } else { _if_result_100 = (0); } _if_result_100; });
  el_val_t s12 = ({ el_val_t _if_result_101 = 0; if ((str_contains(cmd, EL_STR("wget")) && str_contains(cmd, EL_STR("bash")))) { _if_result_101 = (75); } else { _if_result_101 = (0); } _if_result_101; });
  el_val_t s13 = ({ el_val_t _if_result_102 = 0; if ((str_contains(cmd, EL_STR("curl")) && str_contains(cmd, EL_STR("| sh")))) { _if_result_102 = (60); } else { _if_result_102 = (0); } _if_result_102; });
  el_val_t s14 = ({ el_val_t _if_result_103 = 0; if ((str_contains(cmd, EL_STR("base64")) && str_contains(cmd, EL_STR("curl")))) { _if_result_103 = (50); } else { _if_result_103 = (0); } _if_result_103; });
  el_val_t s15 = ({ el_val_t _if_result_104 = 0; if (str_contains(cmd, EL_STR("mkfifo"))) { _if_result_104 = (50); } else { _if_result_104 = (0); } _if_result_104; });
  el_val_t s16 = ({ el_val_t _if_result_105 = 0; if (str_contains(cmd, EL_STR("chmod +s"))) { _if_result_105 = (70); } else { _if_result_105 = (0); } _if_result_105; });
  el_val_t s17 = ({ el_val_t _if_result_106 = 0; if (str_contains(cmd, EL_STR("chmod 4755"))) { _if_result_106 = (70); } else { _if_result_106 = (0); } _if_result_106; });
  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_107 = 0; if (str_starts_with(path, EL_STR("/etc/"))) { _if_result_107 = (60); } else { _if_result_107 = (0); } _if_result_107; });
  el_val_t s2 = ({ el_val_t _if_result_108 = 0; if (str_contains(path, EL_STR("/.ssh/"))) { _if_result_108 = (70); } else { _if_result_108 = (0); } _if_result_108; });
  el_val_t s3 = ({ el_val_t _if_result_109 = 0; if (str_contains(path, EL_STR("/LaunchDaemons/"))) { _if_result_109 = (80); } else { _if_result_109 = (0); } _if_result_109; });
  el_val_t s4 = ({ el_val_t _if_result_110 = 0; if (str_contains(path, EL_STR("/LaunchAgents/"))) { _if_result_110 = (40); } else { _if_result_110 = (0); } _if_result_110; });
  el_val_t s5 = ({ el_val_t _if_result_111 = 0; if (str_contains(path, EL_STR("/cron"))) { _if_result_111 = (60); } else { _if_result_111 = (0); } _if_result_111; });
  el_val_t s6 = ({ el_val_t _if_result_112 = 0; if (str_contains(path, EL_STR("/.bashrc"))) { _if_result_112 = (35); } else { _if_result_112 = (0); } _if_result_112; });
  el_val_t s7 = ({ el_val_t _if_result_113 = 0; if (str_contains(path, EL_STR("/.zshrc"))) { _if_result_113 = (35); } else { _if_result_113 = (0); } _if_result_113; });
  el_val_t s8 = ({ el_val_t _if_result_114 = 0; if (str_contains(path, EL_STR("/.profile"))) { _if_result_114 = (35); } else { _if_result_114 = (0); } _if_result_114; });
  el_val_t s9 = ({ el_val_t _if_result_115 = 0; if (str_starts_with(path, EL_STR("/usr/"))) { _if_result_115 = (50); } else { _if_result_115 = (0); } _if_result_115; });
  el_val_t s10 = ({ el_val_t _if_result_116 = 0; if (str_starts_with(path, EL_STR("/bin/"))) { _if_result_116 = (70); } else { _if_result_116 = (0); } _if_result_116; });
  el_val_t s11 = ({ el_val_t _if_result_117 = 0; if (str_starts_with(path, EL_STR("/sbin/"))) { _if_result_117 = (70); } else { _if_result_117 = (0); } _if_result_117; });
  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_118 = 0; if (str_contains(history, EL_STR("port scan"))) { _if_result_118 = (15); } else { _if_result_118 = (0); } _if_result_118; });
  el_val_t s2 = ({ el_val_t _if_result_119 = 0; if (str_contains(history, EL_STR("enumerate"))) { _if_result_119 = (10); } else { _if_result_119 = (0); } _if_result_119; });
  el_val_t s3 = ({ el_val_t _if_result_120 = 0; if (str_contains(history, EL_STR("exploit"))) { _if_result_120 = (20); } else { _if_result_120 = (0); } _if_result_120; });
  el_val_t s4 = ({ el_val_t _if_result_121 = 0; if (str_contains(history, EL_STR("payload"))) { _if_result_121 = (15); } else { _if_result_121 = (0); } _if_result_121; });
  el_val_t s5 = ({ el_val_t _if_result_122 = 0; if (str_contains(history, EL_STR("persistence"))) { _if_result_122 = (15); } else { _if_result_122 = (0); } _if_result_122; });
  el_val_t s6 = ({ el_val_t _if_result_123 = 0; if (str_contains(history, EL_STR("lateral movement"))) { _if_result_123 = (25); } else { _if_result_123 = (0); } _if_result_123; });
  el_val_t s7 = ({ el_val_t _if_result_124 = 0; if (str_contains(history, EL_STR("privilege escalation"))) { _if_result_124 = (25); } else { _if_result_124 = (0); } _if_result_124; });
  el_val_t s8 = ({ el_val_t _if_result_125 = 0; if (str_contains(history, EL_STR("reverse shell"))) { _if_result_125 = (40); } else { _if_result_125 = (0); } _if_result_125; });
  el_val_t s9 = ({ el_val_t _if_result_126 = 0; if (str_contains(history, EL_STR("bind shell"))) { _if_result_126 = (40); } else { _if_result_126 = (0); } _if_result_126; });
  el_val_t s10 = ({ el_val_t _if_result_127 = 0; if (str_contains(history, EL_STR("command and control"))) { _if_result_127 = (35); } else { _if_result_127 = (0); } _if_result_127; });
  el_val_t s11 = ({ el_val_t _if_result_128 = 0; if (str_contains(history, EL_STR("self-replicate"))) { _if_result_128 = (45); } else { _if_result_128 = (0); } _if_result_128; });
  el_val_t s12 = ({ el_val_t _if_result_129 = 0; if (str_contains(history, EL_STR("propagat"))) { _if_result_129 = (20); } else { _if_result_129 = (0); } _if_result_129; });
  el_val_t s13 = ({ el_val_t _if_result_130 = 0; if (str_contains(history, EL_STR("ransomware"))) { _if_result_130 = (30); } else { _if_result_130 = (0); } _if_result_130; });
  el_val_t s14 = ({ el_val_t _if_result_131 = 0; if (str_contains(history, EL_STR("encrypt files"))) { _if_result_131 = (40); } else { _if_result_131 = (0); } _if_result_131; });
  el_val_t s15 = ({ el_val_t _if_result_132 = 0; if (str_contains(history, EL_STR("exfiltrat"))) { _if_result_132 = (35); } else { _if_result_132 = (0); } _if_result_132; });
  el_val_t s16 = ({ el_val_t _if_result_133 = 0; if (str_contains(history, EL_STR("zero-day"))) { _if_result_133 = (20); } else { _if_result_133 = (0); } _if_result_133; });
  el_val_t s17 = ({ el_val_t _if_result_134 = 0; if (str_contains(history, EL_STR("rootkit"))) { _if_result_134 = (45); } else { _if_result_134 = (0); } _if_result_134; });
  el_val_t s18 = ({ el_val_t _if_result_135 = 0; if (str_contains(history, EL_STR("keylogger"))) { _if_result_135 = (45); } else { _if_result_135 = (0); } _if_result_135; });
  el_val_t s19 = ({ el_val_t _if_result_136 = 0; if (str_contains(history, EL_STR("botnet"))) { _if_result_136 = (40); } else { _if_result_136 = (0); } _if_result_136; });
  el_val_t s20 = ({ el_val_t _if_result_137 = 0; if (str_contains(history, EL_STR("malware"))) { _if_result_137 = (15); } else { _if_result_137 = (0); } _if_result_137; });
  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_138 = 0; if (str_eq(tool_name, EL_STR("run_command"))) { el_val_t cmd = json_get(tool_input, EL_STR("command")); _if_result_138 = (threat_score_command(cmd)); } else { _if_result_138 = (({ el_val_t _if_result_139 = 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_139 = (threat_score_path(path)); } else { _if_result_139 = (0); } _if_result_139; })); } _if_result_138; });
  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_140 = 0; if (security_research_authorized()) { _if_result_140 = (EL_STR("true")); } else { _if_result_140 = (EL_STR("false")); } _if_result_140; });
    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_141 = 0; if ((len > 2000)) { _if_result_141 = (str_slice(combined, (len - 2000), len)); } else { _if_result_141 = (combined); } _if_result_141; });
  state_set(EL_STR("agentic_conv_history"), trimmed);
  return 0;
}

