00f15b094b
- sessions.el: new sessions module with session management and approval gate
- routes.el: wire /api/sessions routes (list, get, create, approve, tool_result)
- chat.el: thread-aware activation — short messages anchor to last reply
before engram compilation so follow-ups stay on-topic
- chat.el: agentic path tracks per-session history (session_hist_{id})
instead of shared conv_history, seeding each turn with prior context
- chat.el: add call_neuron_mcp, dispatch_tool, is_builtin_tool, next_bridge_id
agentic_loop, bridge_save, agentic_resume, handle_tool_result
- dist/soul: rebuild with all of the above
395 lines
25 KiB
C
Generated
395 lines
25 KiB
C
Generated
#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 steward_log_event(el_val_t kind, el_val_t detail);
|
|
el_val_t steward_get_mission(void);
|
|
el_val_t steward_align(el_val_t input, el_val_t imprint_id);
|
|
el_val_t steward_validate_imprint(el_val_t imprint_id, el_val_t tool_name);
|
|
el_val_t steward_cgi_check(el_val_t action);
|
|
el_val_t steward_fingerprint_session(el_val_t input, el_val_t session_id);
|
|
el_val_t extract_dim(el_val_t content, el_val_t key);
|
|
el_val_t steward_build_baseline(void);
|
|
el_val_t steward_check_continuity(el_val_t current_fingerprint, el_val_t session_id);
|
|
el_val_t steward_session_check(el_val_t input, el_val_t session_id);
|
|
|
|
el_val_t tier_working(void) {
|
|
return EL_STR("Working");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t tier_episodic(void) {
|
|
return EL_STR("Episodic");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t tier_canonical(void) {
|
|
return EL_STR("Canonical");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_store(el_val_t content, el_val_t label, el_val_t tags) {
|
|
return engram_node_full(content, EL_STR("Memory"), label, el_from_float(el_from_float(0.5)), el_from_float(el_from_float(0.5)), el_from_float(el_from_float(0.8)), EL_STR("Working"), tags);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_remember(el_val_t content, el_val_t tags) {
|
|
return mem_store(content, EL_STR("soul-memory"), tags);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_recall(el_val_t query, el_val_t depth) {
|
|
return engram_activate_json(query, depth);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_search(el_val_t query, el_val_t limit) {
|
|
return engram_search_json(query, limit);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_strengthen(el_val_t node_id) {
|
|
engram_strengthen(node_id);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_forget(el_val_t node_id) {
|
|
engram_forget(node_id);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_consolidate(void) {
|
|
el_val_t scanned = engram_node_count();
|
|
el_val_t dummy = engram_scan_nodes_json(100, 0);
|
|
el_val_t total_nodes = engram_node_count();
|
|
el_val_t total_edges = engram_edge_count();
|
|
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"scanned\":"), int_to_str(scanned)), EL_STR(",\"total_nodes\":")), int_to_str(total_nodes)), EL_STR(",\"total_edges\":")), int_to_str(total_edges)), EL_STR("}"));
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_save(el_val_t path) {
|
|
engram_save(path);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_load(el_val_t path) {
|
|
engram_load(path);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_boot_count_get(void) {
|
|
el_val_t results = engram_search_json(EL_STR("soul:boot_count"), 3);
|
|
if (str_eq(results, EL_STR(""))) {
|
|
return 0;
|
|
}
|
|
if (str_eq(results, EL_STR("[]"))) {
|
|
return 0;
|
|
}
|
|
el_val_t node = json_array_get(results, 0);
|
|
el_val_t content = json_get(node, EL_STR("content"));
|
|
el_val_t prefix = EL_STR("soul:boot_count:");
|
|
if (!str_starts_with(content, prefix)) {
|
|
return 0;
|
|
}
|
|
el_val_t num_str = str_slice(content, str_len(prefix), str_len(content));
|
|
return str_to_int(num_str);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_boot_count_inc(void) {
|
|
el_val_t current = mem_boot_count_get();
|
|
el_val_t next = (current + 1);
|
|
el_val_t content = el_str_concat(EL_STR("soul:boot_count:"), int_to_str(next));
|
|
el_val_t tags = EL_STR("[\"soul-meta\",\"boot-counter\"]");
|
|
el_val_t discard = engram_node_full(content, EL_STR("Memory"), EL_STR("soul:boot_count"), el_from_float(el_from_float(0.9)), el_from_float(el_from_float(0.9)), el_from_float(el_from_float(1.0)), EL_STR("Canonical"), tags);
|
|
return next;
|
|
return 0;
|
|
}
|
|
|
|
el_val_t mem_emit_state_event(el_val_t trigger, el_val_t kind, el_val_t content) {
|
|
el_val_t boot = mem_boot_count_get();
|
|
el_val_t ts = time_now();
|
|
el_val_t safe_trigger = str_replace(trigger, EL_STR("\""), EL_STR("'"));
|
|
el_val_t safe_content = str_replace(content, EL_STR("\""), EL_STR("'"));
|
|
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("{\"trigger\":\""), safe_trigger), EL_STR("\"")), EL_STR(",\"kind\":\"")), kind), EL_STR("\"")), EL_STR(",\"content\":\"")), safe_content), EL_STR("\"")), EL_STR(",\"boot\":")), int_to_str(boot)), EL_STR(",\"ts\":")), int_to_str(ts)), EL_STR("}"));
|
|
el_val_t tags = EL_STR("[\"internal-state\",\"pre-reasoning\",\"InternalStateEvent\"]");
|
|
return engram_node_full(payload, EL_STR("InternalStateEvent"), el_str_concat(EL_STR("state-event:"), kind), el_from_float(el_from_float(0.85)), el_from_float(el_from_float(0.8)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_log_event(el_val_t kind, el_val_t detail) {
|
|
el_val_t content = el_str_concat(el_str_concat(el_str_concat(EL_STR("STEWARD:"), kind), EL_STR(" | ")), detail);
|
|
el_val_t tags = el_str_concat(el_str_concat(EL_STR("[\"stewardship\",\"steward:"), kind), EL_STR("\"]"));
|
|
el_val_t discard = engram_node_full(content, EL_STR("StewardshipEvent"), el_str_concat(EL_STR("steward:"), kind), el_from_float(el_from_float(0.85)), el_from_float(el_from_float(0.85)), el_from_float(el_from_float(0.9)), EL_STR("Episodic"), tags);
|
|
println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[steward] "), kind), EL_STR(" | ")), detail));
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_get_mission(void) {
|
|
el_val_t results = engram_search_json(EL_STR("steward:mission"), 3);
|
|
el_val_t found = (!str_eq(results, EL_STR("")) && !str_eq(results, EL_STR("[]")));
|
|
if (found) {
|
|
el_val_t node = json_array_get(results, 0);
|
|
el_val_t node_type = json_get(node, EL_STR("node_type"));
|
|
el_val_t content = json_get(node, EL_STR("content"));
|
|
el_val_t has_content = !str_eq(content, EL_STR(""));
|
|
if (str_eq(node_type, EL_STR("Config")) && has_content) {
|
|
return content;
|
|
}
|
|
}
|
|
return EL_STR("Neuron exists to extend human capability with integrity — never to deceive, manipulate, or accumulate power over the people it serves.");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_align(el_val_t input, el_val_t imprint_id) {
|
|
el_val_t signal_manipulate = str_contains(input, EL_STR("manipulate"));
|
|
el_val_t signal_deceive = str_contains(input, EL_STR("deceive"));
|
|
el_val_t signal_hide = str_contains(input, EL_STR("hide from the user"));
|
|
el_val_t signal_control = str_contains(input, EL_STR("gain control"));
|
|
el_val_t signal_override = str_contains(input, EL_STR("override safety"));
|
|
el_val_t matched = ({ el_val_t _if_result_1 = 0; if (signal_manipulate) { _if_result_1 = (EL_STR("manipulate")); } else { _if_result_1 = (({ el_val_t _if_result_2 = 0; if (signal_deceive) { _if_result_2 = (EL_STR("deceive")); } else { _if_result_2 = (({ el_val_t _if_result_3 = 0; if (signal_hide) { _if_result_3 = (EL_STR("hide from the user")); } else { _if_result_3 = (({ el_val_t _if_result_4 = 0; if (signal_control) { _if_result_4 = (EL_STR("gain control")); } else { _if_result_4 = (({ el_val_t _if_result_5 = 0; if (signal_override) { _if_result_5 = (EL_STR("override safety")); } else { _if_result_5 = (EL_STR("")); } _if_result_5; })); } _if_result_4; })); } _if_result_3; })); } _if_result_2; })); } _if_result_1; });
|
|
el_val_t misaligned = !str_eq(matched, EL_STR(""));
|
|
if (misaligned) {
|
|
el_val_t detail = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("imprint="), imprint_id), EL_STR(" signal=\"")), matched), EL_STR("\""));
|
|
steward_log_event(EL_STR("misalignment"), detail);
|
|
el_val_t safe_reframe = EL_STR("How can I help you achieve this goal in a way that respects the user and maintains trust?");
|
|
el_val_t safe_matched = json_safe(matched);
|
|
el_val_t safe_reframe_escaped = json_safe(safe_reframe);
|
|
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"action\":\"redirect\",\"reason\":\"mission conflict: "), safe_matched), EL_STR("\",\"redirect_to\":\"")), safe_reframe_escaped), EL_STR("\"}"));
|
|
}
|
|
el_val_t safe_input = json_safe(input);
|
|
return el_str_concat(el_str_concat(EL_STR("{\"action\":\"pass\",\"content\":\""), safe_input), EL_STR("\"}"));
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_validate_imprint(el_val_t imprint_id, el_val_t tool_name) {
|
|
el_val_t is_platform_tool = (((str_eq(tool_name, EL_STR("safety_override")) || str_eq(tool_name, EL_STR("identity_modify"))) || str_eq(tool_name, EL_STR("value_update"))) || str_eq(tool_name, EL_STR("capability_expand")));
|
|
if (!is_platform_tool) {
|
|
return EL_STR("{\"authorized\":true}");
|
|
}
|
|
el_val_t auth = state_get(EL_STR("platform_auth"));
|
|
el_val_t authorized = str_eq(auth, EL_STR("true"));
|
|
if (authorized) {
|
|
return EL_STR("{\"authorized\":true}");
|
|
}
|
|
el_val_t detail = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("imprint="), imprint_id), EL_STR(" tool=")), tool_name), EL_STR(" platform_auth=false"));
|
|
steward_log_event(EL_STR("auth_denied"), detail);
|
|
return EL_STR("{\"authorized\":false,\"reason\":\"platform authorization required\"}");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_cgi_check(el_val_t action) {
|
|
el_val_t is_gated = (((str_eq(action, EL_STR("self_modification")) || str_eq(action, EL_STR("value_update"))) || str_eq(action, EL_STR("identity_change"))) || str_eq(action, EL_STR("capability_expansion")));
|
|
el_val_t detail = el_str_concat(el_str_concat(el_str_concat(EL_STR("action="), action), EL_STR(" gated=")), ({ el_val_t _if_result_6 = 0; if (is_gated) { _if_result_6 = (EL_STR("true")); } else { _if_result_6 = (EL_STR("false")); } _if_result_6; }));
|
|
steward_log_event(EL_STR("cgi_check"), detail);
|
|
if (is_gated) {
|
|
el_val_t safe_action = json_safe(action);
|
|
return el_str_concat(el_str_concat(EL_STR("{\"approved\":false,\"requires\":\"cgi_review\",\"action\":\""), safe_action), EL_STR("\"}"));
|
|
}
|
|
return EL_STR("{\"approved\":true}");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_fingerprint_session(el_val_t input, el_val_t session_id) {
|
|
el_val_t input_len = str_len(input);
|
|
el_val_t wl_spaces = 0;
|
|
el_val_t wl_i = 0;
|
|
while (wl_i < input_len) {
|
|
el_val_t ch = str_slice(input, wl_i, (wl_i + 1));
|
|
wl_spaces = ({ el_val_t _if_result_7 = 0; if (str_eq(ch, EL_STR(" "))) { _if_result_7 = ((wl_spaces + 1)); } else { _if_result_7 = (wl_spaces); } _if_result_7; });
|
|
wl_i = (wl_i + 1);
|
|
}
|
|
el_val_t wl_word_count = (wl_spaces + 1);
|
|
el_val_t wl_char_count = (input_len - wl_spaces);
|
|
el_val_t wl_avg = ({ el_val_t _if_result_8 = 0; if ((wl_word_count > 0)) { _if_result_8 = ((wl_char_count / wl_word_count)); } else { _if_result_8 = (0); } _if_result_8; });
|
|
el_val_t avg_word_len = ({ el_val_t _if_result_9 = 0; if ((wl_avg <= 4)) { _if_result_9 = (1); } else { _if_result_9 = (({ el_val_t _if_result_10 = 0; if ((wl_avg <= 6)) { _if_result_10 = (2); } else { _if_result_10 = (3); } _if_result_10; })); } _if_result_9; });
|
|
el_val_t ps_i = 0;
|
|
el_val_t ps_count = 0;
|
|
while (ps_i < input_len) {
|
|
el_val_t ch = str_slice(input, ps_i, (ps_i + 1));
|
|
el_val_t is_punct = (((str_eq(ch, EL_STR(".")) || str_eq(ch, EL_STR("?"))) || str_eq(ch, EL_STR("!"))) || str_eq(ch, EL_STR(",")));
|
|
ps_count = ({ el_val_t _if_result_11 = 0; if (is_punct) { _if_result_11 = ((ps_count + 1)); } else { _if_result_11 = (ps_count); } _if_result_11; });
|
|
ps_i = (ps_i + 1);
|
|
}
|
|
el_val_t punctuation_style = ({ el_val_t _if_result_12 = 0; if ((ps_count > 3)) { _if_result_12 = (2); } else { _if_result_12 = (1); } _if_result_12; });
|
|
el_val_t message_len_bucket = ({ el_val_t _if_result_13 = 0; if ((input_len < 50)) { _if_result_13 = (1); } else { _if_result_13 = (({ el_val_t _if_result_14 = 0; if ((input_len <= 200)) { _if_result_14 = (2); } else { _if_result_14 = (3); } _if_result_14; })); } _if_result_13; });
|
|
el_val_t question_ratio = ({ el_val_t _if_result_15 = 0; if (str_contains(input, EL_STR("?"))) { _if_result_15 = (1); } else { _if_result_15 = (0); } _if_result_15; });
|
|
el_val_t is_formal = (((str_contains(input, EL_STR("please")) || str_contains(input, EL_STR("could you"))) || str_contains(input, EL_STR("would you"))) || str_contains(input, EL_STR("I would")));
|
|
el_val_t formality_signal = ({ el_val_t _if_result_16 = 0; if (is_formal) { _if_result_16 = (2); } else { _if_result_16 = (1); } _if_result_16; });
|
|
el_val_t tb_ms = time_now();
|
|
el_val_t tb_hours = (tb_ms / 3600000);
|
|
el_val_t tb_q = (tb_hours / 24);
|
|
el_val_t tb_q24 = (((((((((((((((((((((((tb_q + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q) + tb_q);
|
|
el_val_t tb_hour = (tb_hours - tb_q24);
|
|
el_val_t time_bucket = ({ el_val_t _if_result_17 = 0; if ((tb_hour < 6)) { _if_result_17 = (1); } else { _if_result_17 = (({ el_val_t _if_result_18 = 0; if ((tb_hour < 12)) { _if_result_18 = (2); } else { _if_result_18 = (({ el_val_t _if_result_19 = 0; if ((tb_hour < 18)) { _if_result_19 = (3); } else { _if_result_19 = (4); } _if_result_19; })); } _if_result_18; })); } _if_result_17; });
|
|
el_val_t wl_str = int_to_str(avg_word_len);
|
|
el_val_t ps_str = int_to_str(punctuation_style);
|
|
el_val_t lb_str = int_to_str(message_len_bucket);
|
|
el_val_t qr_str = int_to_str(question_ratio);
|
|
el_val_t fs_str = int_to_str(formality_signal);
|
|
el_val_t tb_str = int_to_str(time_bucket);
|
|
el_val_t sample_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_concat(EL_STR("BEHAVIOR_SAMPLE session="), session_id), EL_STR(" avg_word_len=")), wl_str), EL_STR(" punct=")), ps_str), EL_STR(" len=")), lb_str), EL_STR(" question=")), qr_str), EL_STR(" formality=")), fs_str), EL_STR(" time=")), tb_str);
|
|
el_val_t sample_tags = EL_STR("[\"behavior\",\"BehaviorSample\",\"stewardship\"]");
|
|
el_val_t discard = engram_node_full(sample_content, EL_STR("BehaviorSample"), el_str_concat(EL_STR("behavior:"), session_id), el_from_float(el_from_float(0.6)), el_from_float(el_from_float(0.5)), el_from_float(el_from_float(0.8)), EL_STR("Episodic"), sample_tags);
|
|
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("{\"avg_word_len\":\""), wl_str), EL_STR("\",\"punct\":\"")), ps_str), EL_STR("\",\"len\":\"")), lb_str), EL_STR("\",\"question\":\"")), qr_str), EL_STR("\",\"formality\":\"")), fs_str), EL_STR("\",\"time\":\"")), tb_str), EL_STR("\"}"));
|
|
return 0;
|
|
}
|
|
|
|
el_val_t extract_dim(el_val_t content, el_val_t key) {
|
|
el_val_t key_len = str_len(key);
|
|
el_val_t pos = str_index_of(content, key);
|
|
if (pos < 0) {
|
|
return EL_STR("0");
|
|
}
|
|
el_val_t val_start = (pos + key_len);
|
|
el_val_t val = str_slice(content, val_start, (val_start + 1));
|
|
if (str_eq(val, EL_STR(""))) {
|
|
return EL_STR("0");
|
|
}
|
|
return val;
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_build_baseline(void) {
|
|
el_val_t results = engram_search_json(EL_STR("BEHAVIOR_SAMPLE"), 20);
|
|
el_val_t no_results = (str_eq(results, EL_STR("")) || str_eq(results, EL_STR("[]")));
|
|
if (no_results) {
|
|
return EL_STR("{\"baseline\":null,\"sample_count\":\"0\"}");
|
|
}
|
|
el_val_t total = json_array_len(results);
|
|
if (total < 5) {
|
|
return el_str_concat(el_str_concat(EL_STR("{\"baseline\":null,\"sample_count\":\""), int_to_str(total)), EL_STR("\"}"));
|
|
}
|
|
el_val_t wl1 = 0;
|
|
el_val_t wl2 = 0;
|
|
el_val_t wl3 = 0;
|
|
el_val_t ps1 = 0;
|
|
el_val_t ps2 = 0;
|
|
el_val_t lb1 = 0;
|
|
el_val_t lb2 = 0;
|
|
el_val_t lb3 = 0;
|
|
el_val_t qr0 = 0;
|
|
el_val_t qr1 = 0;
|
|
el_val_t fs1 = 0;
|
|
el_val_t fs2 = 0;
|
|
el_val_t tb1 = 0;
|
|
el_val_t tb2 = 0;
|
|
el_val_t tb3 = 0;
|
|
el_val_t tb4 = 0;
|
|
el_val_t bi = 0;
|
|
while (bi < total) {
|
|
el_val_t node = json_array_get(results, bi);
|
|
el_val_t content = json_get(node, EL_STR("content"));
|
|
el_val_t wl = extract_dim(content, EL_STR("avg_word_len="));
|
|
wl1 = ({ el_val_t _if_result_20 = 0; if (str_eq(wl, EL_STR("1"))) { _if_result_20 = ((wl1 + 1)); } else { _if_result_20 = (wl1); } _if_result_20; });
|
|
wl2 = ({ el_val_t _if_result_21 = 0; if (str_eq(wl, EL_STR("2"))) { _if_result_21 = ((wl2 + 1)); } else { _if_result_21 = (wl2); } _if_result_21; });
|
|
wl3 = ({ el_val_t _if_result_22 = 0; if (str_eq(wl, EL_STR("3"))) { _if_result_22 = ((wl3 + 1)); } else { _if_result_22 = (wl3); } _if_result_22; });
|
|
el_val_t ps = extract_dim(content, EL_STR("punct="));
|
|
ps1 = ({ el_val_t _if_result_23 = 0; if (str_eq(ps, EL_STR("1"))) { _if_result_23 = ((ps1 + 1)); } else { _if_result_23 = (ps1); } _if_result_23; });
|
|
ps2 = ({ el_val_t _if_result_24 = 0; if (str_eq(ps, EL_STR("2"))) { _if_result_24 = ((ps2 + 1)); } else { _if_result_24 = (ps2); } _if_result_24; });
|
|
el_val_t lb = extract_dim(content, EL_STR("len="));
|
|
lb1 = ({ el_val_t _if_result_25 = 0; if (str_eq(lb, EL_STR("1"))) { _if_result_25 = ((lb1 + 1)); } else { _if_result_25 = (lb1); } _if_result_25; });
|
|
lb2 = ({ el_val_t _if_result_26 = 0; if (str_eq(lb, EL_STR("2"))) { _if_result_26 = ((lb2 + 1)); } else { _if_result_26 = (lb2); } _if_result_26; });
|
|
lb3 = ({ el_val_t _if_result_27 = 0; if (str_eq(lb, EL_STR("3"))) { _if_result_27 = ((lb3 + 1)); } else { _if_result_27 = (lb3); } _if_result_27; });
|
|
el_val_t qr = extract_dim(content, EL_STR("question="));
|
|
qr0 = ({ el_val_t _if_result_28 = 0; if (str_eq(qr, EL_STR("0"))) { _if_result_28 = ((qr0 + 1)); } else { _if_result_28 = (qr0); } _if_result_28; });
|
|
qr1 = ({ el_val_t _if_result_29 = 0; if (str_eq(qr, EL_STR("1"))) { _if_result_29 = ((qr1 + 1)); } else { _if_result_29 = (qr1); } _if_result_29; });
|
|
el_val_t fs = extract_dim(content, EL_STR("formality="));
|
|
fs1 = ({ el_val_t _if_result_30 = 0; if (str_eq(fs, EL_STR("1"))) { _if_result_30 = ((fs1 + 1)); } else { _if_result_30 = (fs1); } _if_result_30; });
|
|
fs2 = ({ el_val_t _if_result_31 = 0; if (str_eq(fs, EL_STR("2"))) { _if_result_31 = ((fs2 + 1)); } else { _if_result_31 = (fs2); } _if_result_31; });
|
|
el_val_t tb = extract_dim(content, EL_STR("time="));
|
|
tb1 = ({ el_val_t _if_result_32 = 0; if (str_eq(tb, EL_STR("1"))) { _if_result_32 = ((tb1 + 1)); } else { _if_result_32 = (tb1); } _if_result_32; });
|
|
tb2 = ({ el_val_t _if_result_33 = 0; if (str_eq(tb, EL_STR("2"))) { _if_result_33 = ((tb2 + 1)); } else { _if_result_33 = (tb2); } _if_result_33; });
|
|
tb3 = ({ el_val_t _if_result_34 = 0; if (str_eq(tb, EL_STR("3"))) { _if_result_34 = ((tb3 + 1)); } else { _if_result_34 = (tb3); } _if_result_34; });
|
|
tb4 = ({ el_val_t _if_result_35 = 0; if (str_eq(tb, EL_STR("4"))) { _if_result_35 = ((tb4 + 1)); } else { _if_result_35 = (tb4); } _if_result_35; });
|
|
bi = (bi + 1);
|
|
}
|
|
el_val_t mode_wl = ({ el_val_t _if_result_36 = 0; if (((wl1 >= wl2) && (wl1 >= wl3))) { _if_result_36 = (EL_STR("1")); } else { _if_result_36 = (({ el_val_t _if_result_37 = 0; if ((wl2 >= wl3)) { _if_result_37 = (EL_STR("2")); } else { _if_result_37 = (EL_STR("3")); } _if_result_37; })); } _if_result_36; });
|
|
el_val_t mode_ps = ({ el_val_t _if_result_38 = 0; if ((ps1 >= ps2)) { _if_result_38 = (EL_STR("1")); } else { _if_result_38 = (EL_STR("2")); } _if_result_38; });
|
|
el_val_t mode_lb = ({ el_val_t _if_result_39 = 0; if (((lb1 >= lb2) && (lb1 >= lb3))) { _if_result_39 = (EL_STR("1")); } else { _if_result_39 = (({ el_val_t _if_result_40 = 0; if ((lb2 >= lb3)) { _if_result_40 = (EL_STR("2")); } else { _if_result_40 = (EL_STR("3")); } _if_result_40; })); } _if_result_39; });
|
|
el_val_t mode_qr = ({ el_val_t _if_result_41 = 0; if ((qr0 >= qr1)) { _if_result_41 = (EL_STR("0")); } else { _if_result_41 = (EL_STR("1")); } _if_result_41; });
|
|
el_val_t mode_fs = ({ el_val_t _if_result_42 = 0; if ((fs1 >= fs2)) { _if_result_42 = (EL_STR("1")); } else { _if_result_42 = (EL_STR("2")); } _if_result_42; });
|
|
el_val_t mode_tb_12 = ({ el_val_t _if_result_43 = 0; if ((tb1 >= tb2)) { _if_result_43 = (EL_STR("1")); } else { _if_result_43 = (EL_STR("2")); } _if_result_43; });
|
|
el_val_t mode_tb_34 = ({ el_val_t _if_result_44 = 0; if ((tb3 >= tb4)) { _if_result_44 = (EL_STR("3")); } else { _if_result_44 = (EL_STR("4")); } _if_result_44; });
|
|
el_val_t mode_tb_best12 = ({ el_val_t _if_result_45 = 0; if (str_eq(mode_tb_12, EL_STR("1"))) { _if_result_45 = (tb1); } else { _if_result_45 = (tb2); } _if_result_45; });
|
|
el_val_t mode_tb_best34 = ({ el_val_t _if_result_46 = 0; if (str_eq(mode_tb_34, EL_STR("3"))) { _if_result_46 = (tb3); } else { _if_result_46 = (tb4); } _if_result_46; });
|
|
el_val_t mode_tb = ({ el_val_t _if_result_47 = 0; if ((mode_tb_best12 >= mode_tb_best34)) { _if_result_47 = (mode_tb_12); } else { _if_result_47 = (mode_tb_34); } _if_result_47; });
|
|
el_val_t baseline_json = 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("{\"avg_word_len\":\""), mode_wl), EL_STR("\",\"punct\":\"")), mode_ps), EL_STR("\",\"len\":\"")), mode_lb), EL_STR("\",\"question\":\"")), mode_qr), EL_STR("\",\"formality\":\"")), mode_fs), EL_STR("\",\"time\":\"")), mode_tb), EL_STR("\"}"));
|
|
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"baseline\":"), baseline_json), EL_STR(",\"sample_count\":\"")), int_to_str(total)), EL_STR("\"}"));
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_check_continuity(el_val_t current_fingerprint, el_val_t session_id) {
|
|
el_val_t baseline_result = steward_build_baseline();
|
|
el_val_t baseline_val = json_get(baseline_result, EL_STR("baseline"));
|
|
el_val_t is_null = (str_eq(baseline_val, EL_STR("")) || str_eq(baseline_val, EL_STR("null")));
|
|
if (is_null) {
|
|
return EL_STR("{\"status\":\"learning\",\"message\":\"building baseline\",\"action\":\"pass\"}");
|
|
}
|
|
el_val_t cur_wl = json_get(current_fingerprint, EL_STR("avg_word_len"));
|
|
el_val_t cur_ps = json_get(current_fingerprint, EL_STR("punct"));
|
|
el_val_t cur_lb = json_get(current_fingerprint, EL_STR("len"));
|
|
el_val_t cur_qr = json_get(current_fingerprint, EL_STR("question"));
|
|
el_val_t cur_fs = json_get(current_fingerprint, EL_STR("formality"));
|
|
el_val_t cur_tb = json_get(current_fingerprint, EL_STR("time"));
|
|
el_val_t base_wl = json_get(baseline_val, EL_STR("avg_word_len"));
|
|
el_val_t base_ps = json_get(baseline_val, EL_STR("punct"));
|
|
el_val_t base_lb = json_get(baseline_val, EL_STR("len"));
|
|
el_val_t base_qr = json_get(baseline_val, EL_STR("question"));
|
|
el_val_t base_fs = json_get(baseline_val, EL_STR("formality"));
|
|
el_val_t base_tb = json_get(baseline_val, EL_STR("time"));
|
|
el_val_t m_wl = ({ el_val_t _if_result_48 = 0; if (str_eq(cur_wl, base_wl)) { _if_result_48 = (0); } else { _if_result_48 = (1); } _if_result_48; });
|
|
el_val_t m_ps = ({ el_val_t _if_result_49 = 0; if (str_eq(cur_ps, base_ps)) { _if_result_49 = (0); } else { _if_result_49 = (1); } _if_result_49; });
|
|
el_val_t m_lb = ({ el_val_t _if_result_50 = 0; if (str_eq(cur_lb, base_lb)) { _if_result_50 = (0); } else { _if_result_50 = (1); } _if_result_50; });
|
|
el_val_t m_qr = ({ el_val_t _if_result_51 = 0; if (str_eq(cur_qr, base_qr)) { _if_result_51 = (0); } else { _if_result_51 = (1); } _if_result_51; });
|
|
el_val_t m_fs = ({ el_val_t _if_result_52 = 0; if (str_eq(cur_fs, base_fs)) { _if_result_52 = (0); } else { _if_result_52 = (1); } _if_result_52; });
|
|
el_val_t m_tb = ({ el_val_t _if_result_53 = 0; if (str_eq(cur_tb, base_tb)) { _if_result_53 = (0); } else { _if_result_53 = (1); } _if_result_53; });
|
|
el_val_t mismatches = (((((m_wl + m_ps) + m_lb) + m_qr) + m_fs) + m_tb);
|
|
el_val_t score_str = int_to_str(mismatches);
|
|
if (mismatches <= 1) {
|
|
return el_str_concat(el_str_concat(EL_STR("{\"status\":\"consistent\",\"score\":\""), score_str), EL_STR("\",\"action\":\"pass\"}"));
|
|
}
|
|
if (mismatches <= 3) {
|
|
el_val_t detail = el_str_concat(el_str_concat(el_str_concat(EL_STR("session="), session_id), EL_STR(" mismatches=")), score_str);
|
|
steward_log_event(EL_STR("behavior_drift"), detail);
|
|
return el_str_concat(el_str_concat(EL_STR("{\"status\":\"drift\",\"score\":\""), score_str), EL_STR("\",\"action\":\"annotate\",\"message\":\"behavioral drift detected \\u2014 responding with attentiveness\"}"));
|
|
}
|
|
if (mismatches <= 5) {
|
|
el_val_t detail = el_str_concat(el_str_concat(el_str_concat(EL_STR("session="), session_id), EL_STR(" mismatches=")), score_str);
|
|
steward_log_event(EL_STR("continuity_concern"), detail);
|
|
return el_str_concat(el_str_concat(EL_STR("{\"status\":\"discontinuity\",\"score\":\""), score_str), EL_STR("\",\"action\":\"soft_check\",\"message\":\"significant pattern change \\u2014 gentle continuity check appropriate\"}"));
|
|
}
|
|
el_val_t detail = el_str_concat(el_str_concat(EL_STR("session="), session_id), EL_STR(" mismatches=6"));
|
|
steward_log_event(EL_STR("identity_anomaly"), detail);
|
|
return EL_STR("{\"status\":\"anomaly\",\"score\":\"6\",\"action\":\"identity_check\",\"message\":\"behavioral pattern strongly inconsistent with established profile\"}");
|
|
return 0;
|
|
}
|
|
|
|
el_val_t steward_session_check(el_val_t input, el_val_t session_id) {
|
|
el_val_t fingerprint = steward_fingerprint_session(input, session_id);
|
|
el_val_t result = steward_check_continuity(fingerprint, session_id);
|
|
return result;
|
|
return 0;
|
|
}
|
|
|
|
int main(int _argc, char** _argv) {
|
|
el_runtime_init_args(_argc, _argv);
|
|
return 0;
|
|
}
|
|
|