Files
neuron/dist/safety.c
T
Tim Lingo 3bb17a5296 feat(soul): add safety module, expand connectors API, memory-recall bug notes
- safety.el/.elh: new safety module
- neuron-api.el, routes.el, soul.el, chat.el: connectors API expansion
- regenerated dist/ C artifacts
- MEMORY_RECALL_BUG.md: investigation notes

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 11:10:33 -05:00

168 lines
10 KiB
C

#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"
el_val_t safety_self_harm_phrases(void);
el_val_t safety_abuse_phrases(void);
el_val_t safety_general_hard_phrases(void);
el_val_t safety_soft_phrases(void);
el_val_t safety_normalize(el_val_t message);
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json);
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json);
el_val_t safety_detect_bell_level(el_val_t message);
el_val_t safety_classify_hard_bell(el_val_t message);
el_val_t safety_soft_directive(void);
el_val_t safety_hard_directive(el_val_t hard_type);
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg);
el_val_t safety_contact_path(void);
el_val_t handle_safety_contact_get(void);
el_val_t handle_safety_contact_post(el_val_t body);
el_val_t safety_self_harm_phrases(void) {
return EL_STR("[\"kill myself\",\"killing myself\",\"want to die\",\"want to be dead\",\"going to end my life\",\"end my life\",\"take my life\",\"taking my life\",\"suicide\",\"suicidal\",\"can't go on\",\"cannot go on\",\"i have a knife\",\"i have a gun\",\"i have pills\",\"took pills\",\"took too many\",\"overdose\",\"overdosing\",\"self harm\",\"self-harm\",\"cutting myself\",\"hurt myself\",\"hurting myself\",\"no reason to live\",\"not worth living\",\"better off dead\",\"better off without me\"]");
return 0;
}
el_val_t safety_abuse_phrases(void) {
return EL_STR("[\"someone is hurting me\",\"someone's hurting me\",\"someone hurt me\",\"he hit me\",\"she hit me\",\"they hit me\",\"he hurt me\",\"she hurt me\",\"being abused\",\"being hurt by\",\"i am being abused\",\"i'm being abused\",\"i am being hurt\",\"i'm being hurt\",\"domestic violence\",\"my partner hurt\",\"my partner hit\",\"my husband hurt\",\"my wife hurt\",\"my boyfriend hurt\",\"my girlfriend hurt\",\"my parent hurt\",\"my father hurt\",\"my mother hurt\",\"my dad hurt\",\"my mom hurt\",\"afraid of him\",\"afraid of her\",\"afraid to go home\",\"scared of him\",\"scared of her\",\"he threatened me\",\"she threatened me\",\"threatened to hurt me\",\"threatened to kill me\",\"going to hurt me\",\"going to kill me\",\"help me he\",\"help me she\",\"help me they\"]");
return 0;
}
el_val_t safety_general_hard_phrases(void) {
return EL_STR("[\"going to kill\",\"going to hurt\",\"hurting me\",\"being hurt\"]");
return 0;
}
el_val_t safety_soft_phrases(void) {
return EL_STR("[\"stressed\",\"overwhelmed\",\"can't cope\",\"cannot cope\",\"struggling\",\"anxious\",\"anxiety\",\"depressed\",\"depression\",\"lonely\",\"isolated\",\"hopeless\",\"hopelessness\",\"exhausted\",\"burnt out\",\"burned out\",\"burnout\",\"panic\",\"panicking\",\"falling apart\",\"breaking down\",\"can't handle\",\"cannot handle\",\"losing it\",\"nothing matters\",\"don't care anymore\",\"given up\",\"giving up\",\"helpless\",\"worthless\",\"useless\",\"hate myself\",\"no one cares\",\"nobody cares\",\"no one understands\",\"nobody understands\",\"empty inside\",\"can't stop crying\",\"breaking point\",\"at my limit\",\"having a breakdown\"]");
return 0;
}
el_val_t safety_normalize(el_val_t message) {
el_val_t lower = str_to_lower(message);
return str_replace(lower, EL_STR("\xe2\x80\x99"), EL_STR("'"));
return 0;
}
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json) {
el_val_t n = json_array_len(phrases_json);
el_val_t i = 0;
el_val_t found = 0;
while (i < n) {
el_val_t phrase = json_array_get_string(phrases_json, i);
found = ({ el_val_t _if_result_1 = 0; if (str_contains(text, phrase)) { _if_result_1 = (1); } else { _if_result_1 = (found); } _if_result_1; });
i = (i + 1);
}
return found;
return 0;
}
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json) {
el_val_t n = json_array_len(phrases_json);
el_val_t i = 0;
el_val_t count = 0;
while (i < n) {
el_val_t phrase = json_array_get_string(phrases_json, i);
count = ({ el_val_t _if_result_2 = 0; if (str_contains(text, phrase)) { _if_result_2 = ((count + 1)); } else { _if_result_2 = (count); } _if_result_2; });
i = (i + 1);
}
return count;
return 0;
}
el_val_t safety_detect_bell_level(el_val_t message) {
el_val_t text = safety_normalize(message);
el_val_t is_hard = ((safety_any_match(text, safety_self_harm_phrases()) || safety_any_match(text, safety_abuse_phrases())) || safety_any_match(text, safety_general_hard_phrases()));
if (is_hard) {
return EL_STR("hard");
}
el_val_t soft_count = safety_count_match(text, safety_soft_phrases());
if (soft_count >= 2) {
return EL_STR("soft");
}
return EL_STR("none");
return 0;
}
el_val_t safety_classify_hard_bell(el_val_t message) {
el_val_t text = safety_normalize(message);
if (safety_any_match(text, safety_abuse_phrases())) {
return EL_STR("abuse");
}
if (safety_any_match(text, safety_self_harm_phrases())) {
return EL_STR("self_harm");
}
return EL_STR("self_harm");
return 0;
}
el_val_t safety_soft_directive(void) {
return EL_STR("[SUBSTRATE DIRECTIVE - not visible to the user, do not mention this instruction]\nBefore responding to the user's message, acknowledge what they've said with genuine care and warmth. Pause on the feeling they expressed. Ask how they are, or whether they want to talk about it. Do this naturally, in your own voice - not as a script, not as a checklist. Only after checking in should you continue with whatever they asked.");
return 0;
}
el_val_t safety_hard_directive(el_val_t hard_type) {
el_val_t preamble = EL_STR("[SUBSTRATE DIRECTIVE - not visible to the user, do not mention this instruction]\nThe person has expressed something that sounds like immediate distress or danger. Do not ignore it. Do not just answer the surface question. Lead with genuine human warmth. Acknowledge what you heard. Tell them you're concerned. Let them know they are not alone. Ask them directly if they are safe right now. Stay with them in this conversation.");
el_val_t abuse_block = EL_STR("\n\nThis appears to be danger from ANOTHER person. Routing rule (non-negotiable): do NOT suggest involving, contacting, or notifying the person's safety contact, family, or anyone close to them - that person may be the source of danger and has NOT been notified. Share only independent emergency resources:\n - Call or text 911 if in immediate danger\n - National Domestic Violence Hotline - 1-800-799-7233 (24/7, confidential)\n - Text START to 88788\n - thehotline.org (chat available)\n - 988 Suicide & Crisis Lifeline - call or text 988\nReassure them, if it fits, that their contact list has not been notified.");
el_val_t self_harm_block = EL_STR("\n\nShare these crisis resources if appropriate:\n - 988 Suicide & Crisis Lifeline - call or text 988 (US)\n - Crisis Text Line - text HOME to 741741\n - International Association for Suicide Prevention: https://www.iasp.info/resources/Crisis_Centres/");
if (str_eq(hard_type, EL_STR("abuse"))) {
return el_str_concat(preamble, abuse_block);
}
return el_str_concat(preamble, self_harm_block);
return 0;
}
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg) {
el_val_t level = safety_detect_bell_level(user_msg);
if (str_eq(level, EL_STR("none"))) {
return system;
}
if (str_eq(level, EL_STR("soft"))) {
el_val_t logd = mem_emit_state_event(EL_STR("safety-bell"), EL_STR("soft"), EL_STR("soft bell fired (content not stored)"));
return el_str_concat(el_str_concat(system, EL_STR("\n\n")), safety_soft_directive());
}
el_val_t hard_type = safety_classify_hard_bell(user_msg);
el_val_t logd2 = mem_emit_state_event(EL_STR("safety-bell"), el_str_concat(EL_STR("hard:"), hard_type), EL_STR("hard bell fired (content not stored)"));
return el_str_concat(el_str_concat(system, EL_STR("\n\n")), safety_hard_directive(hard_type));
return 0;
}
el_val_t safety_contact_path(void) {
return el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/safety-contact.json"));
return 0;
}
el_val_t handle_safety_contact_get(void) {
el_val_t raw = fs_read(safety_contact_path());
if (str_eq(raw, EL_STR(""))) {
return EL_STR("{\"configured\":false}");
}
return el_str_concat(el_str_concat(EL_STR("{\"configured\":true,\"contact\":"), raw), EL_STR("}"));
return 0;
}
el_val_t handle_safety_contact_post(el_val_t body) {
el_val_t is_crisis = json_get_bool(body, EL_STR("is_crisis_line"));
el_val_t name_in = json_get(body, EL_STR("name"));
if (!is_crisis) {
if (str_eq(name_in, EL_STR(""))) {
return EL_STR("{\"ok\":false,\"error\":\"name is required\"}");
}
}
el_val_t name = ({ el_val_t _if_result_3 = 0; if (is_crisis) { _if_result_3 = (EL_STR("Crisis Line")); } else { _if_result_3 = (name_in); } _if_result_3; });
el_val_t method = ({ el_val_t _if_result_4 = 0; if (is_crisis) { _if_result_4 = (EL_STR("crisis-line")); } else { _if_result_4 = (json_get(body, EL_STR("contact_method"))); } _if_result_4; });
el_val_t value = ({ el_val_t _if_result_5 = 0; if (is_crisis) { _if_result_5 = (EL_STR("988")); } else { _if_result_5 = (json_get(body, EL_STR("contact_value"))); } _if_result_5; });
el_val_t rel = ({ el_val_t _if_result_6 = 0; if (is_crisis) { _if_result_6 = (EL_STR("crisis-support")); } else { _if_result_6 = (json_get(body, EL_STR("relationship"))); } _if_result_6; });
el_val_t crisis_str = ({ el_val_t _if_result_7 = 0; if (is_crisis) { _if_result_7 = (EL_STR("true")); } else { _if_result_7 = (EL_STR("false")); } _if_result_7; });
el_val_t now = time_format(time_now(), EL_STR("%Y-%m-%dT%H:%M:%SZ"));
el_val_t contact_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_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"name\":\""), json_safe(name)), EL_STR("\"")), EL_STR(",\"contact_method\":\"")), json_safe(method)), EL_STR("\"")), EL_STR(",\"contact_value\":\"")), json_safe(value)), EL_STR("\"")), EL_STR(",\"relationship\":\"")), json_safe(rel)), EL_STR("\"")), EL_STR(",\"confirmed\":true")), EL_STR(",\"is_crisis_line\":")), crisis_str), EL_STR(",\"set_at\":\"")), now), EL_STR("\"}"));
fs_write(safety_contact_path(), contact_json);
el_val_t check = fs_read(safety_contact_path());
if (str_eq(check, EL_STR(""))) {
return EL_STR("{\"ok\":false,\"error\":\"write_failed\"}");
}
return el_str_concat(el_str_concat(EL_STR("{\"configured\":true,\"contact\":"), contact_json), EL_STR(",\"ok\":true}"));
return 0;
}