Compare commits

..

2 Commits

Author SHA1 Message Date
will.anderson 24789f726a chore(dist): update chat.c with operator identity fix
Neuron Soul CI / build (pull_request) Has been cancelled
2026-06-28 14:13:31 -05:00
will.anderson 53df211e06 Inject operator home dir into system prompt to fix 'my' path resolution
Neuron Soul CI / build (pull_request) Has been cancelled
Resolves #30. The LLM was resolving possessive filesystem references ('my
notes', 'my downloads') against the imprint author's identity in the Engram
graph rather than the actual OS user running the daemon. Add an OPERATOR
IDENTITY section to build_system_prompt() that explicitly states the current
user and home directory, blocking the LLM from inferring the wrong home from
biographical context.
2026-06-28 14:04:37 -05:00
7 changed files with 161 additions and 227 deletions
+17 -50
View File
@@ -608,6 +608,22 @@ fn json_safe(s: String) -> String {
// Issue #8 fix: engram_block at END of system prompt for strongest recency bias.
// Issue #10 fix: STABLE IDENTITY vs RETRIEVED MEMORY section labels.
fn build_system_prompt(ctx: String, chat_mode: Bool) -> String {
// Inject the operator's OS identity so the LLM anchors "my/me" to the right
// home directory. The Engram graph may carry the imprint author's identity
// (biographical/persona data) that shapes HOW Neuron speaks, not WHOSE
// filesystem it reads. The operator is whoever is running this daemon process.
let op_home: String = env("HOME")
let op_user: String = env("USER")
let op_display: String = if str_eq(op_user, "") { "the current user" } else { op_user }
let operator_section: String = "OPERATOR IDENTITY\n\n"
+ "You are running on " + op_display + "'s machine. Their home directory is " + op_home + ".\n\n"
+ "When they say \"my files\", \"my notes\", \"my downloads\", \"my desktop\", or any possessive "
+ "referring to their filesystem, always resolve those paths under " + op_home + " — never under "
+ "a different user's home directory. This is a hard rule.\n\n"
+ "The memory graph may include identity context from a different person (the imprint who shaped your personality and values). "
+ "That context governs how you think and speak — it does not tell you whose machine you are on. "
+ "The person speaking to you right now is " + op_display + " at " + op_home + ".\n\n"
let identity: String = state_get("soul_identity")
let current_date: String = time_format(time_now(), "%A, %B %d, %Y")
let date_line: String = "\n\nCurrent date: " + current_date
@@ -673,7 +689,7 @@ fn build_system_prompt(ctx: String, chat_mode: Bool) -> String {
safety_addendum
}
return identity + date_line + voice_rules + security_rules + capability_rules + identity_block + affective_boot_block + engram_block + safety_block
return identity + operator_section + date_line + voice_rules + security_rules + capability_rules + identity_block + affective_boot_block + engram_block + safety_block
}
fn hist_append(hist: String, role: String, content: String) -> String {
@@ -1573,55 +1589,6 @@ fn next_bridge_id() -> String {
return "br-" + uid
}
fn handle_chat_plan(body: String) -> String {
let message: String = json_get(body, "message")
if str_eq(message, "") {
return "{\"error\":\"message required\",\"plan\":null}"
}
let req_model: String = json_get(body, "model")
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
let op_home: String = env("HOME")
let op_user: String = env("USER")
let op_display: String = if str_eq(op_user, "") { "the current user" } else { op_user }
// Compile context same intent-seeding as agentic path so the plan is grounded.
let ctx: String = engram_compile(message)
let ctx_block: String = if str_eq(ctx, "") { "" } else { "\n\n[CONTEXT]\n" + ctx }
let plan_system: String = "You are in PLAN MODE. Your job is to produce a concise step-by-step plan for the request below — WITHOUT executing it.\n\nReturn ONLY a JSON object. No markdown. No preamble. No explanation. Just the JSON:\n{\"steps\":[{\"id\":\"s1\",\"title\":\"<2-6 word title>\",\"detail\":\"<one concrete sentence>\"},{\"id\":\"s2\",...}]}\n\nPlan rules:\n- 3-7 steps (more only when genuinely needed for a complex multi-file task)\n- Each step is one atomic, independently verifiable action\n- title: 2-6 words, imperative (e.g. \"Read config file\", \"Write updated handler\")\n- detail: exactly one sentence describing what happens\n- No tool calls. No execution. No side effects. The user approves before anything runs.\n\nOperator: " + op_display + " at " + op_home + ctx_block
let raw: String = llm_call_system(model, plan_system, message)
let is_error: Bool = str_starts_with(raw, "{\"error\"")
if is_error {
return "{\"error\":\"plan generation failed\",\"plan\":null,\"detail\":" + raw + "}"
}
// Extract the JSON object from the response (LLM sometimes wraps in markdown).
let brace_start: Int = str_index_of(raw, "{")
// Scan backwards to find the last closing brace (str_last_index_of not available).
let brace_end: Int = -1
let scan_i: Int = str_len(raw) - 1
while scan_i >= 0 {
let ch: String = str_slice(raw, scan_i, scan_i + 1)
let brace_end = if str_eq(ch, "}") && brace_end < 0 { scan_i } else { brace_end }
let scan_i = if brace_end >= 0 { -1 } else { scan_i - 1 }
}
let plan_json: String = if brace_start >= 0 {
if brace_end > brace_start {
str_slice(raw, brace_start, brace_end + 1)
} else {
raw
}
} else {
raw
}
return "{\"plan\":" + plan_json + ",\"model\":\"" + json_safe(model) + "\"}"
}
fn handle_chat_agentic(body: String) -> String {
let message: String = json_get(body, "message")
if str_eq(message, "") {
-1
View File
@@ -43,7 +43,6 @@ extern fn resolve_in_root(path: String, root: String) -> String
extern fn dispatch_tool(tool_name: String, tool_input: String) -> String
extern fn is_builtin_tool(tool_name: String) -> Bool
extern fn next_bridge_id() -> String
extern fn handle_chat_plan(body: String) -> String
extern fn handle_chat_agentic(body: String) -> String
extern fn agentic_loop(session_id: String, model: String, safe_sys: String, tools_json: String, messages_in: String, h: Map, tools_log_in: String) -> String
extern fn bridge_save(session_id: String, model: String, safe_sys: String, tools_json: String, messages: String, tools_log: String, tool_use_id: String) -> Bool
Generated Vendored
+116 -144
View File
File diff suppressed because one or more lines are too long
Generated Vendored
-1
View File
@@ -43,7 +43,6 @@ extern fn resolve_in_root(path: String, root: String) -> String
extern fn dispatch_tool(tool_name: String, tool_input: String) -> String
extern fn is_builtin_tool(tool_name: String) -> Bool
extern fn next_bridge_id() -> String
extern fn handle_chat_plan(body: String) -> String
extern fn handle_chat_agentic(body: String) -> String
extern fn agentic_loop(session_id: String, model: String, safe_sys: String, tools_json: String, messages_in: String, h: Map, tools_log_in: String) -> String
extern fn bridge_save(session_id: String, model: String, safe_sys: String, tools_json: String, messages: String, tools_log: String, tool_use_id: String) -> Bool
Generated Vendored
+15 -19
View File
@@ -85,7 +85,6 @@ el_val_t resolve_in_root(el_val_t path, el_val_t root);
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);
@@ -318,23 +317,22 @@ el_val_t handle_dharma_recv(el_val_t body) {
el_val_t chat_body = ({ el_val_t _if_result_14 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_14 = (el_str_concat(el_str_concat(EL_STR("{\"message\":\""), str_replace(str_replace(eff_payload, EL_STR("\\"), EL_STR("\\\\")), EL_STR("\""), EL_STR("\\\""))), EL_STR("\"}"))); } else { _if_result_14 = (eff_payload); } _if_result_14; });
el_val_t agentic_flag = json_get_bool(eff_payload, EL_STR("agentic"));
el_val_t raw_msg = json_get(chat_body, EL_STR("message"));
el_val_t req_mode = json_get(chat_body, EL_STR("mode"));
el_val_t reply = ({ el_val_t _if_result_15 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_15 = (handle_chat_plan(chat_body)); } else { _if_result_15 = (({ el_val_t _if_result_16 = 0; if (agentic_flag) { _if_result_16 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_16 = (screened_reply); } _if_result_16; })); } _if_result_15; });
el_val_t reply = ({ el_val_t _if_result_15 = 0; if (agentic_flag) { _if_result_15 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_15 = (screened_reply); } _if_result_15; });
auto_persist(chat_body, reply);
return reply;
}
if (str_eq(eff_event, EL_STR("memory"))) {
el_val_t query = json_get(eff_payload, EL_STR("query"));
el_val_t limit_str = json_get(eff_payload, EL_STR("limit"));
el_val_t limit = ({ el_val_t _if_result_17 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_17 = (20); } else { _if_result_17 = (str_to_int(limit_str)); } _if_result_17; });
el_val_t q = ({ el_val_t _if_result_18 = 0; if (str_eq(query, EL_STR(""))) { _if_result_18 = (eff_payload); } else { _if_result_18 = (query); } _if_result_18; });
el_val_t limit = ({ el_val_t _if_result_16 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_16 = (20); } else { _if_result_16 = (str_to_int(limit_str)); } _if_result_16; });
el_val_t q = ({ el_val_t _if_result_17 = 0; if (str_eq(query, EL_STR(""))) { _if_result_17 = (eff_payload); } else { _if_result_17 = (query); } _if_result_17; });
return engram_search_json(q, limit);
}
if (str_eq(eff_event, EL_STR("tool"))) {
el_val_t path_field = json_get(eff_payload, EL_STR("path"));
el_val_t method_field = json_get(eff_payload, EL_STR("method"));
el_val_t tool_body = json_get(eff_payload, EL_STR("body"));
el_val_t eff_method = ({ el_val_t _if_result_19 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_19 = (EL_STR("POST")); } else { _if_result_19 = (method_field); } _if_result_19; });
el_val_t eff_method = ({ el_val_t _if_result_18 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_18 = (EL_STR("POST")); } else { _if_result_18 = (method_field); } _if_result_18; });
return handle_tool(path_field, eff_method, tool_body);
}
if (str_eq(eff_event, EL_STR("see"))) {
@@ -369,7 +367,7 @@ el_val_t connectd_get(el_val_t suffix) {
}
el_val_t connectd_post(el_val_t suffix, el_val_t body) {
el_val_t eff = ({ el_val_t _if_result_20 = 0; if (str_eq(body, EL_STR(""))) { _if_result_20 = (EL_STR("{}")); } else { _if_result_20 = (body); } _if_result_20; });
el_val_t eff = ({ el_val_t _if_result_19 = 0; if (str_eq(body, EL_STR(""))) { _if_result_19 = (EL_STR("{}")); } else { _if_result_19 = (body); } _if_result_19; });
el_val_t tmp = el_str_concat(el_str_concat(EL_STR("/tmp/neuron-connectors-req-"), int_to_str(time_now())), EL_STR(".json"));
fs_write(tmp, eff);
el_val_t out = exec_capture(el_str_concat(el_str_concat(el_str_concat(EL_STR("curl -s --max-time 20 -X POST http://127.0.0.1:7771"), suffix), EL_STR(" -H 'Content-Type: application/json' -d @")), tmp));
@@ -436,17 +434,16 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
engram_save(snap_path);
el_val_t snap = fs_read(snap_path);
el_val_t edges_raw = json_get_raw(snap, EL_STR("edges"));
return ({ el_val_t _if_result_21 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_21 = (EL_STR("[]")); } else { _if_result_21 = (edges_raw); } _if_result_21; });
return ({ el_val_t _if_result_20 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_20 = (EL_STR("[]")); } else { _if_result_20 = (edges_raw); } _if_result_20; });
}
if (str_eq(clean, EL_STR("/api/chat"))) {
el_val_t raw_msg = json_get(body, EL_STR("message"));
el_val_t eff_msg = ({ el_val_t _if_result_22 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_22 = (body); } else { _if_result_22 = (raw_msg); } _if_result_22; });
el_val_t eff_msg = ({ el_val_t _if_result_21 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_21 = (body); } else { _if_result_21 = (raw_msg); } _if_result_21; });
if (str_eq(eff_msg, EL_STR(""))) {
return EL_STR("{\"error\":\"message is required\",\"code\":\"missing_param\"}");
}
el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic"));
el_val_t req_mode = json_get(body, EL_STR("mode"));
el_val_t reply = ({ el_val_t _if_result_23 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_23 = (handle_chat_plan(body)); } else { _if_result_23 = (({ el_val_t _if_result_24 = 0; if (agentic_flag) { _if_result_24 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_24 = (screened_reply); } _if_result_24; })); } _if_result_23; });
el_val_t reply = ({ el_val_t _if_result_22 = 0; if (agentic_flag) { _if_result_22 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_22 = (screened_reply); } _if_result_22; });
auto_persist(body, reply);
return reply;
}
@@ -529,7 +526,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
if (str_starts_with(clean, EL_STR("/api/sessions/"))) {
el_val_t gs_after = str_slice(clean, 14, str_len(clean));
el_val_t gs_slash = str_index_of(gs_after, EL_STR("/"));
el_val_t gs_id = ({ el_val_t _if_result_25 = 0; if ((gs_slash < 0)) { _if_result_25 = (gs_after); } else { _if_result_25 = (str_slice(gs_after, 0, gs_slash)); } _if_result_25; });
el_val_t gs_id = ({ el_val_t _if_result_23 = 0; if ((gs_slash < 0)) { _if_result_23 = (gs_after); } else { _if_result_23 = (str_slice(gs_after, 0, gs_slash)); } _if_result_23; });
if (!str_eq(gs_id, EL_STR(""))) {
return session_get(gs_id);
}
@@ -543,14 +540,14 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
if (str_starts_with(clean, EL_STR("/api/sessions/")) && str_ends_with(clean, EL_STR("/tool_result"))) {
el_val_t after = str_slice(clean, 14, str_len(clean));
el_val_t slash = str_index_of(after, EL_STR("/"));
el_val_t session_id = ({ el_val_t _if_result_26 = 0; if ((slash < 0)) { _if_result_26 = (after); } else { _if_result_26 = (str_slice(after, 0, slash)); } _if_result_26; });
el_val_t session_id = ({ el_val_t _if_result_24 = 0; if ((slash < 0)) { _if_result_24 = (after); } else { _if_result_24 = (str_slice(after, 0, slash)); } _if_result_24; });
return handle_tool_result(session_id, body);
}
if (str_starts_with(clean, EL_STR("/api/sessions/"))) {
el_val_t sess_after = str_slice(clean, 14, str_len(clean));
el_val_t sess_slash = str_index_of(sess_after, EL_STR("/"));
el_val_t sess_id = ({ el_val_t _if_result_27 = 0; if ((sess_slash < 0)) { _if_result_27 = (sess_after); } else { _if_result_27 = (str_slice(sess_after, 0, sess_slash)); } _if_result_27; });
el_val_t sess_sub = ({ el_val_t _if_result_28 = 0; if ((sess_slash < 0)) { _if_result_28 = (EL_STR("")); } else { _if_result_28 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_28; });
el_val_t sess_id = ({ el_val_t _if_result_25 = 0; if ((sess_slash < 0)) { _if_result_25 = (sess_after); } else { _if_result_25 = (str_slice(sess_after, 0, sess_slash)); } _if_result_25; });
el_val_t sess_sub = ({ el_val_t _if_result_26 = 0; if ((sess_slash < 0)) { _if_result_26 = (EL_STR("")); } else { _if_result_26 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_26; });
if (!str_eq(sess_id, EL_STR("")) && str_eq(sess_sub, EL_STR("approve"))) {
return handle_session_approve(sess_id, body);
}
@@ -573,8 +570,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
return EL_STR("{\"error\":\"message is required\",\"code\":\"missing_param\"}");
}
el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic"));
el_val_t req_mode = json_get(body, EL_STR("mode"));
el_val_t reply = ({ el_val_t _if_result_29 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_29 = (handle_chat_plan(body)); } else { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (agentic_flag) { _if_result_30 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_30 = (screened_reply); } _if_result_30; })); } _if_result_29; });
el_val_t reply = ({ el_val_t _if_result_27 = 0; if (agentic_flag) { _if_result_27 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_27 = (screened_reply); } _if_result_27; });
auto_persist(body, reply);
return reply;
}
@@ -698,7 +694,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
if (str_starts_with(clean, EL_STR("/api/sessions/"))) {
el_val_t del_after = str_slice(clean, 14, str_len(clean));
el_val_t del_slash = str_index_of(del_after, EL_STR("/"));
el_val_t del_id = ({ el_val_t _if_result_31 = 0; if ((del_slash < 0)) { _if_result_31 = (del_after); } else { _if_result_31 = (str_slice(del_after, 0, del_slash)); } _if_result_31; });
el_val_t del_id = ({ el_val_t _if_result_28 = 0; if ((del_slash < 0)) { _if_result_28 = (del_after); } else { _if_result_28 = (str_slice(del_after, 0, del_slash)); } _if_result_28; });
if (!str_eq(del_id, EL_STR(""))) {
return session_delete(del_id);
}
@@ -709,7 +705,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
if (str_starts_with(clean, EL_STR("/api/sessions/"))) {
el_val_t patch_after = str_slice(clean, 14, str_len(clean));
el_val_t patch_slash = str_index_of(patch_after, EL_STR("/"));
el_val_t patch_id = ({ el_val_t _if_result_32 = 0; if ((patch_slash < 0)) { _if_result_32 = (patch_after); } else { _if_result_32 = (str_slice(patch_after, 0, patch_slash)); } _if_result_32; });
el_val_t patch_id = ({ el_val_t _if_result_29 = 0; if ((patch_slash < 0)) { _if_result_29 = (patch_after); } else { _if_result_29 = (str_slice(patch_after, 0, patch_slash)); } _if_result_29; });
if (!str_eq(patch_id, EL_STR(""))) {
return session_update_patch(patch_id, body);
}
Generated Vendored
+10
View File
@@ -0,0 +1,10 @@
#include <stdint.h>
#include <stdlib.h>
#include "el_runtime.h"
el_val_t init_soul_edges(void);
el_val_t load_identity_context(void);
el_val_t seed_persona_from_env(void);
el_val_t emit_session_start_event(void);
el_val_t layered_cycle(el_val_t raw_input);
+3 -12
View File
@@ -229,10 +229,7 @@ fn handle_dharma_recv(body: String) -> String {
}
let agentic_flag: Bool = json_get_bool(eff_payload, "agentic")
let raw_msg: String = json_get(chat_body, "message")
let req_mode: String = json_get(chat_body, "mode")
let reply: String = if str_eq(req_mode, "plan") {
handle_chat_plan(chat_body)
} else if agentic_flag {
let reply: String = if agentic_flag {
handle_chat_agentic(chat_body)
} else {
let screened_reply: String = layered_cycle(raw_msg)
@@ -394,10 +391,7 @@ fn handle_request(method: String, path: String, body: String) -> String {
return "{\"error\":\"message is required\",\"code\":\"missing_param\"}"
}
let agentic_flag: Bool = json_get_bool(body, "agentic")
let req_mode: String = json_get(body, "mode")
let reply: String = if str_eq(req_mode, "plan") {
handle_chat_plan(body)
} else if agentic_flag {
let reply: String = if agentic_flag {
handle_chat_agentic(body)
} else {
let screened_reply: String = layered_cycle(eff_msg)
@@ -546,10 +540,7 @@ fn handle_request(method: String, path: String, body: String) -> String {
return "{\"error\":\"message is required\",\"code\":\"missing_param\"}"
}
let agentic_flag: Bool = json_get_bool(body, "agentic")
let req_mode: String = json_get(body, "mode")
let reply: String = if str_eq(req_mode, "plan") {
handle_chat_plan(body)
} else if agentic_flag {
let reply: String = if agentic_flag {
handle_chat_agentic(body)
} else {
let screened_reply: String = layered_cycle(raw_msg)