#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_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 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 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 session_title_from_message(el_val_t message);
el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at, el_val_t folder);
el_val_t session_exists(el_val_t session_id);
el_val_t session_create(el_val_t body);
el_val_t session_create_cleanup(el_val_t session_id);
el_val_t session_list(void);
el_val_t session_get(el_val_t session_id);
el_val_t session_delete(el_val_t session_id);
el_val_t session_update_patch(el_val_t session_id, el_val_t body);
el_val_t session_search_entry(el_val_t node);
el_val_t session_search(el_val_t query);
el_val_t session_hist_load(el_val_t session_id);
el_val_t session_hist_save(el_val_t session_id, el_val_t hist);
el_val_t session_update_meta_timestamp(el_val_t session_id);
el_val_t session_auto_title(el_val_t session_id, el_val_t first_message);
el_val_t handle_session_approve(el_val_t session_id, el_val_t body);

el_val_t session_title_from_message(el_val_t message) {
  if (str_eq(message, EL_STR(""))) {
    return EL_STR("New conversation");
  }
  el_val_t trimmed = str_trim(message);
  if (str_len(trimmed) <= 60) {
    return trimmed;
  }
  return str_slice(trimmed, 0, 60);
  return 0;
}

el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at, el_val_t folder) {
  el_val_t safe_title = json_safe(title);
  el_val_t safe_folder = json_safe(folder);
  return 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("{\"type\":\"session:meta\""), EL_STR(",\"id\":\"")), id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"folder\":\"")), safe_folder), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(created_at)), EL_STR(",\"updated_at\":")), int_to_str(updated_at)), EL_STR("}"));
  return 0;
}

el_val_t session_exists(el_val_t session_id) {
  if (str_eq(session_id, EL_STR(""))) {
    return 0;
  }
  el_val_t idx = state_get(EL_STR("session_index"));
  if (!str_eq(idx, EL_STR("")) && !str_eq(idx, EL_STR("[]"))) {
    if (str_contains(idx, el_str_concat(el_str_concat(EL_STR("\"id\":\""), session_id), EL_STR("\"")))) {
      return 1;
    }
  }
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 5);
  if (str_eq(results, EL_STR(""))) {
    return 0;
  }
  if (str_eq(results, EL_STR("[]"))) {
    return 0;
  }
  el_val_t total = json_array_len(results);
  el_val_t found = 0;
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sid = json_get(content, EL_STR("id"));
    el_val_t is_match = (str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id));
    found = ({ el_val_t _if_result_1 = 0; if (is_match) { _if_result_1 = (1); } else { _if_result_1 = (found); } _if_result_1; });
    i = (i + 1);
  }
  return found;
  return 0;
}

el_val_t session_create(el_val_t body) {
  el_val_t ts = time_now();
  el_val_t id = uuid_v4();
  el_val_t title_req = json_get(body, EL_STR("title"));
  el_val_t title = ({ el_val_t _if_result_2 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_2 = (EL_STR("New conversation")); } else { _if_result_2 = (title_req); } _if_result_2; });
  el_val_t folder = json_get(body, EL_STR("folder"));
  el_val_t content = session_make_content(id, title, ts, ts, folder);
  el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
  el_val_t node_id = engram_node_full(content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(0.7), el_from_float(0.7), el_from_float(0.9), EL_STR("Episodic"), tags);
  if (str_eq(node_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"failed to create session\"}");
  }
  state_set(el_str_concat(EL_STR("session_node_"), id), node_id);
  state_set(el_str_concat(EL_STR("session_pending_first_msg_"), id), EL_STR("1"));
  el_val_t existing_idx = state_get(EL_STR("session_index"));
  el_val_t idx_entry = 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("{\"id\":\""), id), EL_STR("\",\"title\":\"")), json_safe(title)), EL_STR("\",\"folder\":\"")), json_safe(folder)), EL_STR("\",\"created_at\":")), int_to_str(ts)), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR(",\"last_message\":\"\"}"));
  el_val_t new_idx = ({ el_val_t _if_result_3 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_3 = (el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR("]"))); } else { el_val_t inner = str_slice(existing_idx, 1, (str_len(existing_idx) - 1)); _if_result_3 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_3; });
  state_set(EL_STR("session_index"), new_idx);
  return 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("{\"id\":\""), id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder)), EL_STR("\"")), EL_STR(",\"node_id\":\"")), node_id), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(ts)), EL_STR("}"));
  return 0;
}

el_val_t session_create_cleanup(el_val_t session_id) {
  if (str_eq(session_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"session_id is required\"}");
  }
  state_set(el_str_concat(EL_STR("session_pending_first_msg_"), session_id), EL_STR(""));
  return session_delete(session_id);
  return 0;
}

el_val_t session_list(void) {
  el_val_t state_idx = state_get(EL_STR("session_index"));
  if (!str_eq(state_idx, EL_STR("")) && !str_eq(state_idx, EL_STR("[]"))) {
    return state_idx;
  }
  el_val_t results = engram_search_json(EL_STR("session:meta"), 50);
  if (str_eq(results, EL_STR(""))) {
    return EL_STR("[]");
  }
  if (str_eq(results, EL_STR("[]"))) {
    return EL_STR("[]");
  }
  el_val_t total = json_array_len(results);
  el_val_t out = EL_STR("");
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t node_type = json_get(node, EL_STR("node_type"));
    el_val_t is_session = (str_eq(label, EL_STR("session:meta")) && str_eq(node_type, EL_STR("Conversation")));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sess_id = json_get(content, EL_STR("id"));
    el_val_t eff_id = ({ el_val_t _if_result_4 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_4 = (json_get(node, EL_STR("id"))); } else { _if_result_4 = (sess_id); } _if_result_4; });
    el_val_t title_inner = json_get(content, EL_STR("title"));
    el_val_t eff_title = ({ el_val_t _if_result_5 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_5 = (EL_STR("New conversation")); } else { _if_result_5 = (title_inner); } _if_result_5; });
    el_val_t folder_inner = json_get(content, EL_STR("folder"));
    el_val_t created_inner = json_get(content, EL_STR("created_at"));
    el_val_t updated_inner = json_get(content, EL_STR("updated_at"));
    el_val_t eff_created = ({ el_val_t _if_result_6 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_6 = (EL_STR("0")); } else { _if_result_6 = (created_inner); } _if_result_6; });
    el_val_t eff_updated = ({ el_val_t _if_result_7 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_7 = (eff_created); } else { _if_result_7 = (updated_inner); } _if_result_7; });
    el_val_t entry = ({ el_val_t _if_result_8 = 0; if (is_session) { _if_result_8 = (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("{\"id\":\""), json_safe(eff_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder_inner)), EL_STR("\"")), EL_STR(",\"last_message\":\"\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_8 = (EL_STR("")); } _if_result_8; });
    out = ({ el_val_t _if_result_9 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_9 = (({ el_val_t _if_result_10 = 0; if (str_eq(out, EL_STR(""))) { _if_result_10 = (entry); } else { _if_result_10 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_10; })); } else { _if_result_9 = (out); } _if_result_9; });
    i = (i + 1);
  }
  return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]"));
  return 0;
}

el_val_t session_get(el_val_t session_id) {
  if (str_eq(session_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"session_id is required\"}");
  }
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10);
  el_val_t meta_content = EL_STR("");
  el_val_t meta_title = EL_STR("New conversation");
  el_val_t meta_folder = EL_STR("");
  el_val_t meta_created = EL_STR("0");
  el_val_t meta_updated = EL_STR("0");
  el_val_t found = 0;
  el_val_t total = ({ el_val_t _if_result_11 = 0; if (str_eq(results, EL_STR(""))) { _if_result_11 = (0); } else { _if_result_11 = (json_array_len(results)); } _if_result_11; });
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sid = json_get(content, EL_STR("id"));
    el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found);
    found = ({ el_val_t _if_result_12 = 0; if (is_match) { _if_result_12 = (1); } else { _if_result_12 = (found); } _if_result_12; });
    meta_title = ({ el_val_t _if_result_13 = 0; if (is_match) { _if_result_13 = (json_get(content, EL_STR("title"))); } else { _if_result_13 = (meta_title); } _if_result_13; });
    meta_folder = ({ el_val_t _if_result_14 = 0; if (is_match) { _if_result_14 = (json_get(content, EL_STR("folder"))); } else { _if_result_14 = (meta_folder); } _if_result_14; });
    el_val_t meta_created_raw = json_get(content, EL_STR("created_at"));
    meta_created = ({ el_val_t _if_result_15 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_15 = (meta_created_raw); } else { _if_result_15 = (meta_created); } _if_result_15; });
    el_val_t meta_updated_raw = json_get(content, EL_STR("updated_at"));
    meta_updated = ({ el_val_t _if_result_16 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_16 = (meta_updated_raw); } else { _if_result_16 = (meta_updated); } _if_result_16; });
    i = (i + 1);
  }
  el_val_t state_hist = state_get(el_str_concat(EL_STR("session_hist_"), session_id));
  el_val_t hist_raw = ({ el_val_t _if_result_17 = 0; if (str_eq(state_hist, EL_STR(""))) { el_val_t engram_hist = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); _if_result_17 = (({ el_val_t _if_result_18 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_18 = (EL_STR("[]")); } else { _if_result_18 = (({ el_val_t _if_result_19 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_19 = (EL_STR("[]")); } else { el_val_t h_node = json_array_get(engram_hist, 0); el_val_t h_content = json_get(h_node, EL_STR("content")); _if_result_19 = (({ el_val_t _if_result_20 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_20 = (h_content); } else { _if_result_20 = (EL_STR("[]")); } _if_result_20; })); } _if_result_19; })); } _if_result_18; })); } else { _if_result_17 = (state_hist); } _if_result_17; });
  el_val_t safe_title = json_safe(meta_title);
  return 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("{\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(meta_folder)), EL_STR("\"")), EL_STR(",\"created_at\":")), meta_created), EL_STR(",\"updated_at\":")), meta_updated), EL_STR(",\"messages\":")), hist_raw), EL_STR("}"));
  return 0;
}

el_val_t session_delete(el_val_t session_id) {
  if (str_eq(session_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"session_id is required\"}");
  }
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10);
  el_val_t total = ({ el_val_t _if_result_21 = 0; if (str_eq(results, EL_STR(""))) { _if_result_21 = (0); } else { _if_result_21 = (json_array_len(results)); } _if_result_21; });
  el_val_t deleted_meta = 0;
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sid = json_get(content, EL_STR("id"));
    el_val_t is_match = (str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id));
    el_val_t node_id = json_get(node, EL_STR("id"));
    deleted_meta = ({ el_val_t _if_result_22 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_22 = ((deleted_meta + 1)); } else { _if_result_22 = (deleted_meta); } _if_result_22; });
    i = (i + 1);
  }
  el_val_t msg_results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 10);
  el_val_t m_total = ({ el_val_t _if_result_23 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_23 = (0); } else { _if_result_23 = (json_array_len(msg_results)); } _if_result_23; });
  el_val_t deleted_msgs = 0;
  el_val_t j = 0;
  while (j < m_total) {
    el_val_t node = json_array_get(msg_results, j);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t is_msgs = str_eq(label, el_str_concat(EL_STR("session:messages:"), session_id));
    el_val_t node_id = json_get(node, EL_STR("id"));
    deleted_msgs = ({ el_val_t _if_result_24 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_24 = ((deleted_msgs + 1)); } else { _if_result_24 = (deleted_msgs); } _if_result_24; });
    j = (j + 1);
  }
  state_set(el_str_concat(EL_STR("session_hist_"), session_id), EL_STR(""));
  state_set(el_str_concat(EL_STR("session_node_"), session_id), EL_STR(""));
  state_set(EL_STR("session_index"), EL_STR(""));
  state_set(el_str_concat(EL_STR("mcp_bridge:"), session_id), EL_STR(""));
  state_set(el_str_concat(EL_STR("always_allow_"), session_id), EL_STR(""));
  state_set(el_str_concat(EL_STR("session_pending_first_msg_"), session_id), EL_STR(""));
  return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"session_id\":\""), session_id), EL_STR("\"")), EL_STR(",\"deleted_meta\":")), int_to_str(deleted_meta)), EL_STR(",\"deleted_msgs\":")), int_to_str(deleted_msgs)), EL_STR("}"));
  return 0;
}

el_val_t session_update_patch(el_val_t session_id, el_val_t body) {
  if (str_eq(session_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"session_id is required\"}");
  }
  el_val_t has_title = str_contains(body, EL_STR("\"title\""));
  el_val_t has_folder = str_contains(body, EL_STR("\"folder\""));
  if (!has_title && !has_folder) {
    return EL_STR("{\"error\":\"title or folder required in body\"}");
  }
  el_val_t results = engram_search_json(EL_STR("session:meta"), 50);
  el_val_t total = ({ el_val_t _if_result_25 = 0; if (str_eq(results, EL_STR(""))) { _if_result_25 = (0); } else { _if_result_25 = (json_array_len(results)); } _if_result_25; });
  el_val_t found = 0;
  el_val_t old_title = EL_STR("New conversation");
  el_val_t old_folder = EL_STR("");
  el_val_t old_created = EL_STR("0");
  el_val_t old_node_id = EL_STR("");
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sid = json_get(content, EL_STR("id"));
    el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found);
    found = ({ el_val_t _if_result_26 = 0; if (is_match) { _if_result_26 = (1); } else { _if_result_26 = (found); } _if_result_26; });
    el_val_t title_raw = json_get(content, EL_STR("title"));
    old_title = ({ el_val_t _if_result_27 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_27 = (title_raw); } else { _if_result_27 = (old_title); } _if_result_27; });
    el_val_t folder_raw = json_get(content, EL_STR("folder"));
    old_folder = ({ el_val_t _if_result_28 = 0; if (is_match) { _if_result_28 = (folder_raw); } else { _if_result_28 = (old_folder); } _if_result_28; });
    el_val_t created_raw = json_get(content, EL_STR("created_at"));
    old_created = ({ el_val_t _if_result_29 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_29 = (created_raw); } else { _if_result_29 = (old_created); } _if_result_29; });
    el_val_t nid = json_get(node, EL_STR("id"));
    old_node_id = ({ el_val_t _if_result_30 = 0; if (is_match) { _if_result_30 = (nid); } else { _if_result_30 = (old_node_id); } _if_result_30; });
    i = (i + 1);
  }
  if (!found) {
    return el_str_concat(el_str_concat(EL_STR("{\"error\":\"session not found\",\"session_id\":\""), session_id), EL_STR("\"}"));
  }
  el_val_t req_title = json_get(body, EL_STR("title"));
  el_val_t eff_title = ({ el_val_t _if_result_31 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_31 = (req_title); } else { _if_result_31 = (old_title); } _if_result_31; });
  el_val_t eff_folder = ({ el_val_t _if_result_32 = 0; if (has_folder) { _if_result_32 = (json_get(body, EL_STR("folder"))); } else { _if_result_32 = (old_folder); } _if_result_32; });
  if (!str_eq(old_node_id, EL_STR(""))) {
    engram_forget(old_node_id);
  }
  el_val_t ts = time_now();
  el_val_t created_int = str_to_int(old_created);
  el_val_t new_content = session_make_content(session_id, eff_title, created_int, ts, eff_folder);
  el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
  el_val_t new_node_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(0.7), el_from_float(0.7), el_from_float(0.9), EL_STR("Episodic"), tags);
  state_set(el_str_concat(EL_STR("session_node_"), session_id), new_node_id);
  state_set(EL_STR("session_index"), EL_STR(""));
  return 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("{\"ok\":true,\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(eff_folder)), EL_STR("\"")), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR("}"));
  return 0;
}

el_val_t session_search_entry(el_val_t node) {
  el_val_t label = json_get(node, EL_STR("label"));
  if (!str_eq(label, EL_STR("session:meta"))) {
    return EL_STR("");
  }
  el_val_t content = json_get(node, EL_STR("content"));
  el_val_t sess_id = json_get(content, EL_STR("id"));
  if (str_eq(sess_id, EL_STR(""))) {
    return EL_STR("");
  }
  el_val_t title = json_get(content, EL_STR("title"));
  el_val_t created_raw = json_get(content, EL_STR("created_at"));
  el_val_t updated_raw = json_get(content, EL_STR("updated_at"));
  el_val_t eff_created = ({ el_val_t _if_result_33 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_33 = (EL_STR("0")); } else { _if_result_33 = (created_raw); } _if_result_33; });
  el_val_t eff_updated = ({ el_val_t _if_result_34 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_34 = (eff_created); } else { _if_result_34 = (updated_raw); } _if_result_34; });
  el_val_t e_id = el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(sess_id)), EL_STR("\""));
  el_val_t e_title = el_str_concat(el_str_concat(EL_STR(",\"title\":\""), json_safe(title)), EL_STR("\""));
  el_val_t e_ts = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR(",\"created_at\":"), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"));
  return el_str_concat(el_str_concat(e_id, e_title), e_ts);
  return 0;
}

el_val_t session_search(el_val_t query) {
  if (str_eq(query, EL_STR(""))) {
    return EL_STR("[]");
  }
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), query), 20);
  if (str_eq(results, EL_STR(""))) {
    return EL_STR("[]");
  }
  if (str_eq(results, EL_STR("[]"))) {
    return EL_STR("[]");
  }
  el_val_t total = json_array_len(results);
  el_val_t out = EL_STR("");
  el_val_t i = 0;
  while (i < total) {
    el_val_t entry = session_search_entry(json_array_get(results, i));
    out = ({ el_val_t _if_result_35 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_35 = (({ el_val_t _if_result_36 = 0; if (str_eq(out, EL_STR(""))) { _if_result_36 = (entry); } else { _if_result_36 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_36; })); } else { _if_result_35 = (out); } _if_result_35; });
    i = (i + 1);
  }
  return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]"));
  return 0;
}

el_val_t session_hist_load(el_val_t session_id) {
  el_val_t state_hist = state_get(el_str_concat(EL_STR("session_hist_"), session_id));
  if (!str_eq(state_hist, EL_STR(""))) {
    return state_hist;
  }
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3);
  if (str_eq(results, EL_STR(""))) {
    return EL_STR("");
  }
  if (str_eq(results, EL_STR("[]"))) {
    return EL_STR("");
  }
  el_val_t node = json_array_get(results, 0);
  el_val_t label = json_get(node, EL_STR("label"));
  if (!str_eq(label, el_str_concat(EL_STR("session:messages:"), session_id))) {
    return EL_STR("");
  }
  el_val_t content = json_get(node, EL_STR("content"));
  if (str_starts_with(content, EL_STR("["))) {
    return content;
  }
  return EL_STR("");
  return 0;
}

el_val_t session_hist_save(el_val_t session_id, el_val_t hist) {
  state_set(el_str_concat(EL_STR("session_hist_"), session_id), hist);
  state_set(el_str_concat(EL_STR("session_pending_first_msg_"), session_id), EL_STR(""));
  el_val_t old_results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3);
  el_val_t o_total = ({ el_val_t _if_result_37 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_37 = (0); } else { _if_result_37 = (json_array_len(old_results)); } _if_result_37; });
  el_val_t oi = 0;
  while (oi < o_total) {
    el_val_t node = json_array_get(old_results, oi);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t nid = json_get(node, EL_STR("id"));
    if (str_eq(label, el_str_concat(EL_STR("session:messages:"), session_id)) && !str_eq(nid, EL_STR(""))) {
      engram_forget(nid);
    }
    oi = (oi + 1);
  }
  el_val_t tags = EL_STR("[\"session\",\"session-history\",\"Conversation\"]");
  el_val_t discard = engram_node_full(hist, EL_STR("Conversation"), el_str_concat(EL_STR("session:messages:"), session_id), el_from_float(0.6), el_from_float(0.6), el_from_float(0.9), EL_STR("Episodic"), tags);
  el_val_t summary_written_key = el_str_concat(EL_STR("session_bell_summary_written:"), session_id);
  el_val_t already_written = state_get(summary_written_key);
  if (str_eq(already_written, EL_STR(""))) {
    el_val_t bell_count_key = el_str_concat(EL_STR("session_bell_count:"), session_id);
    el_val_t bell_count_raw = state_get(bell_count_key);
    el_val_t bell_count = ({ el_val_t _if_result_38 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_38 = (0); } else { _if_result_38 = (str_to_int(bell_count_raw)); } _if_result_38; });
    if (bell_count > 0) {
      el_val_t bell_level_key = el_str_concat(EL_STR("session_bell_level:"), session_id);
      el_val_t bell_signal_key = el_str_concat(EL_STR("session_bell_signal:"), session_id);
      el_val_t dominant_level = state_get(bell_level_key);
      el_val_t last_signal = state_get(bell_signal_key);
      el_val_t eff_level = ({ el_val_t _if_result_39 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_39 = (EL_STR("soft")); } else { _if_result_39 = (dominant_level); } _if_result_39; });
      el_val_t eff_signal = ({ el_val_t _if_result_40 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_40 = (EL_STR("(no signal captured)")); } else { _if_result_40 = (last_signal); } _if_result_40; });
      el_val_t ts_now = time_now();
      el_val_t summary_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("session:emotional-summary"), EL_STR(" | session:")), session_id), EL_STR(" | bell_count:")), int_to_str(bell_count)), EL_STR(" | dominant_level:")), eff_level), EL_STR(" | last_signal:")), eff_signal), EL_STR(" | ts:")), int_to_str(ts_now));
      el_val_t summary_tags = el_str_concat(el_str_concat(EL_STR("[\"session-emotional-summary\",\"affective\",\"bell:"), eff_level), EL_STR("\",\"BellEvent\"]"));
      el_val_t summary_sal = ({ el_val_t _if_result_41 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_41 = (el_from_float(0.95)); } else { _if_result_41 = (el_from_float(0.85)); } _if_result_41; });
      el_val_t sum_discard = engram_node_full(summary_content, EL_STR("BellEvent"), EL_STR("session:emotional-summary"), summary_sal, summary_sal, el_from_float(1.0), EL_STR("Episodic"), summary_tags);
      state_set(summary_written_key, EL_STR("1"));
    }
  }
  el_val_t hist_arr_len = ({ el_val_t _if_result_42 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_42 = (0); } else { _if_result_42 = (json_array_len(hist)); } _if_result_42; });
  if (hist_arr_len >= 2) {
    el_val_t last_entry = json_array_get(hist, (hist_arr_len - 1));
    el_val_t last_role = json_get(last_entry, EL_STR("role"));
    el_val_t last_content = json_get(last_entry, EL_STR("content"));
    el_val_t topic_snip = ({ el_val_t _if_result_43 = 0; if ((str_len(last_content) > 200)) { _if_result_43 = (str_slice(last_content, 0, 200)); } else { _if_result_43 = (last_content); } _if_result_43; });
    el_val_t safe_topic = str_replace(topic_snip, EL_STR("\""), EL_STR("'"));
    el_val_t ts_now = int_to_str(time_now());
    el_val_t topic_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("last-session-topic | ts:"), ts_now), EL_STR(" | session:")), session_id), EL_STR(" | topic:")), safe_topic);
    el_val_t topic_tags = EL_STR("[\"last-session-topic\",\"conv:history\",\"Conversation\",\"session:topic\"]");
    el_val_t topic_label = el_str_concat(EL_STR("last-session-topic:"), session_id);
    el_val_t old_topic = engram_search_json(el_str_concat(EL_STR("last-session-topic:"), session_id), 2);
    el_val_t ot_len = ({ el_val_t _if_result_44 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_44 = (0); } else { _if_result_44 = (json_array_len(old_topic)); } _if_result_44; });
    el_val_t oti = 0;
    while (oti < ot_len) {
      el_val_t ot_node = json_array_get(old_topic, oti);
      el_val_t ot_id = json_get(ot_node, EL_STR("id"));
      if (!str_eq(ot_id, EL_STR(""))) {
        engram_forget(ot_id);
      }
      oti = (oti + 1);
    }
    el_val_t discard_topic = engram_node_full(topic_content, EL_STR("Conversation"), topic_label, el_from_float(0.7), el_from_float(0.7), el_from_float(0.9), EL_STR("Episodic"), topic_tags);
  }
  return 0;
}

el_val_t session_update_meta_timestamp(el_val_t session_id) {
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10);
  el_val_t total = ({ el_val_t _if_result_45 = 0; if (str_eq(results, EL_STR(""))) { _if_result_45 = (0); } else { _if_result_45 = (json_array_len(results)); } _if_result_45; });
  el_val_t found = 0;
  el_val_t old_title = EL_STR("New conversation");
  el_val_t old_folder = EL_STR("");
  el_val_t old_created = EL_STR("0");
  el_val_t old_node_id = EL_STR("");
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sid = json_get(content, EL_STR("id"));
    el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found);
    found = ({ el_val_t _if_result_46 = 0; if (is_match) { _if_result_46 = (1); } else { _if_result_46 = (found); } _if_result_46; });
    el_val_t title_raw = json_get(content, EL_STR("title"));
    old_title = ({ el_val_t _if_result_47 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_47 = (title_raw); } else { _if_result_47 = (old_title); } _if_result_47; });
    el_val_t folder_raw = json_get(content, EL_STR("folder"));
    old_folder = ({ el_val_t _if_result_48 = 0; if (is_match) { _if_result_48 = (folder_raw); } else { _if_result_48 = (old_folder); } _if_result_48; });
    el_val_t created_raw = json_get(content, EL_STR("created_at"));
    old_created = ({ el_val_t _if_result_49 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_49 = (created_raw); } else { _if_result_49 = (old_created); } _if_result_49; });
    el_val_t nid = json_get(node, EL_STR("id"));
    old_node_id = ({ el_val_t _if_result_50 = 0; if (is_match) { _if_result_50 = (nid); } else { _if_result_50 = (old_node_id); } _if_result_50; });
    i = (i + 1);
  }
  if (!found) {
    return EL_STR("");
  }
  if (!str_eq(old_node_id, EL_STR(""))) {
    engram_forget(old_node_id);
  }
  el_val_t ts = time_now();
  el_val_t created_int = str_to_int(old_created);
  el_val_t new_content = session_make_content(session_id, old_title, created_int, ts, old_folder);
  el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
  el_val_t new_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(0.7), el_from_float(0.7), el_from_float(0.9), EL_STR("Episodic"), tags);
  state_set(el_str_concat(EL_STR("session_node_"), session_id), new_id);
  return 0;
}

el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) {
  el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10);
  el_val_t total = ({ el_val_t _if_result_51 = 0; if (str_eq(results, EL_STR(""))) { _if_result_51 = (0); } else { _if_result_51 = (json_array_len(results)); } _if_result_51; });
  el_val_t found = 0;
  el_val_t cur_title = EL_STR("");
  el_val_t old_folder = EL_STR("");
  el_val_t old_created = EL_STR("0");
  el_val_t old_node_id = EL_STR("");
  el_val_t i = 0;
  while (i < total) {
    el_val_t node = json_array_get(results, i);
    el_val_t label = json_get(node, EL_STR("label"));
    el_val_t content = json_get(node, EL_STR("content"));
    el_val_t sid = json_get(content, EL_STR("id"));
    el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found);
    found = ({ el_val_t _if_result_52 = 0; if (is_match) { _if_result_52 = (1); } else { _if_result_52 = (found); } _if_result_52; });
    el_val_t title_raw = json_get(content, EL_STR("title"));
    cur_title = ({ el_val_t _if_result_53 = 0; if (is_match) { _if_result_53 = (title_raw); } else { _if_result_53 = (cur_title); } _if_result_53; });
    el_val_t folder_raw = json_get(content, EL_STR("folder"));
    old_folder = ({ el_val_t _if_result_54 = 0; if (is_match) { _if_result_54 = (folder_raw); } else { _if_result_54 = (old_folder); } _if_result_54; });
    el_val_t created_raw = json_get(content, EL_STR("created_at"));
    old_created = ({ el_val_t _if_result_55 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_55 = (created_raw); } else { _if_result_55 = (old_created); } _if_result_55; });
    el_val_t nid = json_get(node, EL_STR("id"));
    old_node_id = ({ el_val_t _if_result_56 = 0; if (is_match) { _if_result_56 = (nid); } else { _if_result_56 = (old_node_id); } _if_result_56; });
    i = (i + 1);
  }
  if (!found) {
    return EL_STR("");
  }
  if (!str_eq(cur_title, EL_STR("New conversation"))) {
    return EL_STR("");
  }
  el_val_t new_title = session_title_from_message(first_message);
  if (!str_eq(old_node_id, EL_STR(""))) {
    engram_forget(old_node_id);
  }
  el_val_t ts = time_now();
  el_val_t created_int = str_to_int(old_created);
  el_val_t new_content = session_make_content(session_id, new_title, created_int, ts, old_folder);
  el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]");
  el_val_t new_id = engram_node_full(new_content, EL_STR("Conversation"), EL_STR("session:meta"), el_from_float(0.7), el_from_float(0.7), el_from_float(0.9), EL_STR("Episodic"), tags);
  state_set(el_str_concat(EL_STR("session_node_"), session_id), new_id);
  return 0;
}

el_val_t handle_session_approve(el_val_t session_id, el_val_t body) {
  if (str_eq(session_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"session_id is required\"}");
  }
  el_val_t call_id = json_get(body, EL_STR("call_id"));
  el_val_t action = json_get(body, EL_STR("action"));
  if (str_eq(call_id, EL_STR(""))) {
    return EL_STR("{\"error\":\"call_id is required\"}");
  }
  if (str_eq(action, EL_STR(""))) {
    return EL_STR("{\"error\":\"action is required (allow|deny|always)\"}");
  }
  el_val_t eff_action = ({ el_val_t _if_result_57 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_57 = (EL_STR("allow")); } else { _if_result_57 = (action); } _if_result_57; });
  el_val_t bridge_blob = state_get(el_str_concat(EL_STR("mcp_bridge:"), session_id));
  if (!str_eq(bridge_blob, EL_STR(""))) {
    el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id);
    el_val_t approve_tool_name = json_get(body, EL_STR("tool_name"));
    el_val_t discard_always = ({ el_val_t _if_result_58 = 0; if ((str_eq(action, EL_STR("always")) && !str_eq(approve_tool_name, EL_STR("")))) { el_val_t always_list = state_get(always_key); el_val_t new_always = ({ el_val_t _if_result_59 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_59 = (approve_tool_name); } else { _if_result_59 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_59; }); (void)(state_set(always_key, new_always)); _if_result_58 = (1); } else { _if_result_58 = (0); } _if_result_58; });
    if (str_eq(approve_tool_name, EL_STR("")) && str_eq(eff_action, EL_STR("allow"))) {
      return EL_STR("{\"error\":\"tool_name is required for allow action\"}");
    }
    el_val_t client_content = json_get(body, EL_STR("content"));
    el_val_t use_client_content = !str_eq(client_content, EL_STR(""));
    el_val_t use_dispatch = (is_builtin_tool(approve_tool_name) && !use_client_content);
    el_val_t raw_input = json_get_raw(body, EL_STR("tool_input"));
    el_val_t eff_input = ({ el_val_t _if_result_60 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_60 = (EL_STR("{}")); } else { _if_result_60 = (raw_input); } _if_result_60; });
    el_val_t content = ({ el_val_t _if_result_61 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_61 = (({ el_val_t _if_result_62 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_63 = 0; if ((str_len(client_content) > 6000)) { _if_result_63 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_63 = (client_content); } _if_result_63; }); _if_result_62 = (trimmed); } else { _if_result_62 = (({ el_val_t _if_result_64 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_64 = (({ el_val_t _if_result_65 = 0; if ((str_len(raw) > 6000)) { _if_result_65 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_65 = (raw); } _if_result_65; })); } else { _if_result_64 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_64; })); } _if_result_62; })); } else { _if_result_61 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_61; });
    return agentic_resume(session_id, call_id, content);
  }
  el_val_t pending_raw = state_get(el_str_concat(EL_STR("pending_tool_"), session_id));
  if (str_eq(pending_raw, EL_STR(""))) {
    return el_str_concat(el_str_concat(EL_STR("{\"error\":\"no pending tool for session\",\"session_id\":\""), session_id), EL_STR("\"}"));
  }
  el_val_t pending_call_id = json_get(pending_raw, EL_STR("call_id"));
  if (!str_eq(pending_call_id, call_id)) {
    return el_str_concat(el_str_concat(EL_STR("{\"error\":\"call_id mismatch\",\"expected\":\""), pending_call_id), EL_STR("\"}"));
  }
  el_val_t tool_name = json_get(pending_raw, EL_STR("tool_name"));
  el_val_t tool_input = json_get_raw(pending_raw, EL_STR("tool_input"));
  el_val_t model = json_get(pending_raw, EL_STR("model"));
  el_val_t safe_sys = json_get(pending_raw, EL_STR("system"));
  el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id);
  el_val_t always_list = state_get(always_key);
  el_val_t discard_always2 = ({ el_val_t _if_result_66 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_67 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_67 = (tool_name); } else { _if_result_67 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_67; }); (void)(state_set(always_key, new_always)); _if_result_66 = (1); } else { _if_result_66 = (0); } _if_result_66; });
  state_set(el_str_concat(EL_STR("pending_tool_"), session_id), EL_STR(""));
  el_val_t tool_result = ({ el_val_t _if_result_68 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_68 = (({ el_val_t _if_result_69 = 0; if ((str_len(raw) > 6000)) { _if_result_69 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_69 = (raw); } _if_result_69; })); } else { _if_result_68 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_68; });
  el_val_t legacy_messages = json_get_raw(pending_raw, EL_STR("messages_so_far"));
  el_val_t stored_variant = json_get(pending_raw, EL_STR("tools_variant"));
  el_val_t tools_json = ({ el_val_t _if_result_70 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_70 = (agentic_tools_with_web()); } else { _if_result_70 = (({ el_val_t _if_result_71 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_71 = (agentic_tools_all()); } else { _if_result_71 = (agentic_tools_literal()); } _if_result_71; })); } _if_result_70; });
  el_val_t blob = 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("{\"model\":\""), json_safe(model)), EL_STR("\"")), EL_STR(",\"safe_sys\":\"")), json_safe(safe_sys)), EL_STR("\"")), EL_STR(",\"tools_json\":\"")), json_safe(tools_json)), EL_STR("\"")), EL_STR(",\"messages\":\"")), json_safe(legacy_messages)), EL_STR("\"")), EL_STR(",\"tools_log\":\"\"")), EL_STR(",\"tool_use_id\":\"")), json_safe(call_id)), EL_STR("\"}"));
  state_set(el_str_concat(EL_STR("mcp_bridge:"), session_id), blob);
  return agentic_resume(session_id, call_id, tool_result);
  return 0;
}

