Compare commits

..

2 Commits

Author SHA1 Message Date
will.anderson b7fd8901d4 fix(routes): fix handle_request ABI, 429 status code, soul_boot_ts write
Neuron Soul CI / build (pull_request) Has been cancelled
2026-06-22 11:53:09 -05:00
will.anderson d92b8c279a fix(routes): error handling, health diagnostics, request validation, rate limiting
Neuron Soul CI / build (pull_request) Has been cancelled
- Add per-IP in-memory rate limiter (60 req/min default, configurable via
  soul_rate_limit state key; /health exempt; loopback callers skipped)
- Extend /health with uptime_secs (from soul_boot_ts) and live LLM probe
- Add missing_param 400 guard on POST /api/chat before passing to LLM
- Standardise error envelopes: add "code" field to err_404/err_405 and all
  missing-param returns; route_synthesize now errors clearly instead of
  returning the misleading {"mechanism":"did not engage"} on bad input
- Document streaming gap in /api/chat (SSE not implemented, note added)
- handle_request gains ip param; rate_limit_check wired at entry point
2026-06-22 11:21:18 -05:00
4 changed files with 112 additions and 232 deletions
+4 -183
View File
@@ -40,43 +40,9 @@ fn engram_compile(intent: String) -> String {
""
}
// Affective context: always include the most recent high-emotion memory if one
// exists within 72 hours. This ensures continuity of care across turns when
// the user was in distress earlier in the session (or recently), that context
// travels into every subsequent LLM call so the response register stays aware.
// We search for BellEvent nodes specifically; these are written by auto_persist
// when safety_detect_bell_level fires. The 72h window (259200 seconds) is wide
// enough to span a multi-session day without pulling ancient history.
let bell_nodes: String = engram_search_json("bell:soft bell:hard BellEvent", 3)
let bell_ok: Bool = !str_eq(bell_nodes, "") && !str_eq(bell_nodes, "[]")
let now_ts: Int = time_now()
let cutoff_ts: Int = now_ts - 259200
let recent_bell: String = if bell_ok {
let bn0: String = json_array_get(bell_nodes, 0)
// created_at is not present in engram node JSON for BellEvent nodes.
// Extract the timestamp embedded in the content string as " | ts:NNNNN".
// Fall back to created_at / updated_at JSON fields if the marker is absent.
let bn_content: String = json_get(bn0, "content")
let ts_marker: String = " | ts:"
let ts_pos: Int = str_index_of(bn_content, ts_marker)
let bn_ts_raw: String = if ts_pos >= 0 {
let ts_start: Int = ts_pos + str_len(ts_marker)
let rest: String = str_slice(bn_content, ts_start, str_len(bn_content))
let next_sep: Int = str_index_of(rest, " | ")
if next_sep < 0 { rest } else { str_slice(rest, 0, next_sep) }
} else {
let ca: String = json_get(bn0, "created_at")
if str_eq(ca, "") { json_get(bn0, "updated_at") } else { ca }
}
let bn_ts: Int = if str_eq(bn_ts_raw, "") { 0 } else { str_to_int(bn_ts_raw) }
if bn_ts > cutoff_ts { bn0 } else { "" }
} else { "" }
let affective_part: String = if !str_eq(recent_bell, "") { recent_bell } else { "" }
let sep1: String = if !str_eq(act_part, "") && !str_eq(srch_part, "") { "\n" } else { "" }
let sep2: String = if (!str_eq(act_part, "") || !str_eq(srch_part, "")) && !str_eq(scan_part, "") { "\n" } else { "" }
let sep3: String = if (!str_eq(act_part, "") || !str_eq(srch_part, "") || !str_eq(scan_part, "")) && !str_eq(affective_part, "") { "\n" } else { "" }
let ctx: String = act_part + sep1 + srch_part + sep2 + scan_part + sep3 + affective_part
let ctx: String = act_part + sep1 + srch_part + sep2 + scan_part
if str_eq(ctx, "") { return "" }
@@ -142,69 +108,6 @@ fn hist_trim(hist: String) -> String {
return hist
}
// hist_trim_with_bell_guard trim the history window exactly as hist_trim does, but
// before dropping the oldest user/assistant pair check whether the user turn triggered
// a bell event. If it did, write a preservation node to engram so the distress exchange
// survives the 20-turn window. The LLM window drops it; engram retains it permanently
// and engram_compile will surface it again via the affective context path.
fn hist_trim_with_bell_guard(hist: String) -> String {
// Extract the first turn (should be a user message) to inspect it.
let inner: String = str_slice(hist, 1, str_len(hist) - 1)
let marker: String = "{\"role\":"
let i1: Int = str_index_of(inner, marker)
// i1 is the start of the first entry within inner.
// Find where the second entry begins to delimit the first entry's JSON.
let tail1: String = str_slice(inner, i1 + 1, str_len(inner))
let i2: Int = str_index_of(tail1, marker)
// The first entry spans from i1 to (i1 + 1 + i2 - 1) within inner.
let first_entry_raw: String = if i2 > 0 {
str_slice(inner, i1, i1 + 1 + i2 - 1)
} else {
str_slice(inner, i1, str_len(inner))
}
let first_role: String = json_get(first_entry_raw, "role")
let first_content: String = json_get(first_entry_raw, "content")
// Only inspect user turns assistant content doesn't carry bell signals.
let bell_level: String = if str_eq(first_role, "user") {
safety_detect_bell_level(first_content)
} else {
"none"
}
// If the turn being evicted triggered a bell, preserve it to engram.
// This is distinct from the BellEvent written by auto_persist: that node
// carries a short summary. This node carries the full exchange content so
// it is recoverable for clinical/continuity review.
if !str_eq(bell_level, "none") {
let ts: Int = time_now()
let ts_str: String = int_to_str(ts)
let safe_content: String = str_replace(first_content, "\"", "'")
let preserve_content: String = "PRESERVED_BELL:" + bell_level
+ " | evicted_at:" + ts_str
+ " | message:" + safe_content
let preserve_tags: String = "[\"bell-history\",\"bell:" + bell_level + "\",\"evicted\",\"affective\",\"BellEvent\"]"
let discard: String = engram_node_full(
preserve_content,
"BellEvent",
"bell:" + bell_level + ":preserved",
el_from_float(0.9),
el_from_float(0.9),
el_from_float(1.0),
"Episodic",
preserve_tags
)
}
// Now perform the standard trim (drop oldest 2 entries = 1 user + 1 assistant pair).
let tail2: String = str_slice(tail1, i2 + 1, str_len(tail1))
let i3: Int = str_index_of(tail2, marker)
if i3 >= 0 {
return "[" + str_slice(tail2, i3, str_len(tail2)) + "]"
}
return hist
}
// clean_llm_response strips GPT-2 BPE byte-to-unicode artifacts that vLLM
// emits when the tokenizer hasn't decoded back to raw bytes.
//
@@ -297,10 +200,8 @@ fn handle_chat(body: String) -> String {
let updated_hist: String = hist_append(stored_hist, "user", message)
let updated_hist2: String = hist_append(updated_hist, "assistant", raw_response)
// Use bell-guarded trim: if the evicted turn triggered a bell event, it is
// preserved to engram before being dropped from the in-memory window.
let final_hist: String = if json_array_len(updated_hist2) > 20 {
hist_trim_with_bell_guard(updated_hist2)
hist_trim(updated_hist2)
} else {
updated_hist2
}
@@ -1234,28 +1135,14 @@ fn auto_persist(req: String, resp: String) -> Void {
let safe_msg: String = str_replace(message, "\"", "'")
let safe_reply: String = str_replace(reply2, "\"", "'")
// Detect emotional salience before persisting. safety_detect_bell_level uses the
// same phrase lists as the safety layer (safety.el), so the classification is
// consistent with what safety_screen already evaluated for this turn.
let bell_level: String = safety_detect_bell_level(message)
let is_bell: Bool = !str_eq(bell_level, "none")
// Tag the Conversation node with bell metadata when distress is present so
// subsequent affective queries (e.g. engram_compile) can find this exchange.
let tags: String = if is_bell {
"[\"Conversation\",\"chat\",\"timestamped\",\"bell:" + bell_level + "\",\"affective\"]"
} else {
"[\"Conversation\",\"chat\",\"timestamped\"]"
}
let content: String = "{\"q\":\"" + safe_msg + "\""
+ ",\"a\":\"" + safe_reply + "\""
+ ",\"created_at\":" + ts_str
+ ",\"source\":\"chat\""
+ ",\"bell\":\"" + bell_level + "\""
+ ",\"label\":\"chat:" + ts_str + "\"}"
let conv_node_id: String = engram_node_full(
let tags: String = "[\"Conversation\",\"chat\",\"timestamped\"]"
engram_node_full(
content,
"Conversation",
"chat:" + ts_str,
@@ -1265,72 +1152,6 @@ fn auto_persist(req: String, resp: String) -> Void {
"Episodic",
tags
)
// When a bell fires, write a dedicated BellEvent node in addition to the
// Conversation node. This makes distress moments directly findable by label
// ("bell:soft" / "bell:hard") without having to scan all Conversation nodes.
// The BellEvent carries higher salience so engram_compile pulls it into context.
// The message content is truncated to 120 chars enough signal, not a full dump.
if is_bell {
let summary: String = if str_len(message) > 120 { str_slice(message, 0, 120) } else { message }
let safe_summary: String = str_replace(summary, "\"", "'")
let bell_content: String = "BELL:" + bell_level
+ " | ts:" + ts_str
+ " | summary:" + safe_summary
// bell:hard gets peak salience; bell:soft is slightly lower.
let sal_a: String = if str_eq(bell_level, "hard") { el_from_float(0.98) } else { el_from_float(0.88) }
let sal_b: String = if str_eq(bell_level, "hard") { el_from_float(0.98) } else { el_from_float(0.88) }
let sal_c: String = if str_eq(bell_level, "hard") { el_from_float(1.0) } else { el_from_float(0.95) }
let bell_tags: String = "[\"safety\",\"bell\",\"bell:" + bell_level + "\",\"affective\",\"BellEvent\"]"
let bell_ts_str: String = int_to_str(time_now())
let bell_label: String = "bell:" + bell_level + ":" + bell_ts_str
let bell_node_id: String = engram_node_full(
bell_content,
"BellEvent",
bell_label,
sal_a,
sal_b,
sal_c,
"Episodic",
bell_tags
)
// Increment session-level bell counter so session_hist_save knows whether
// any bell fired during this session when writing a boundary summary.
let sess_id: String = json_get(req, "session_id")
let bell_key: String = if str_eq(sess_id, "") {
"session_bell_count"
} else {
"session_bell_count:" + sess_id
}
let prior_count: String = state_get(bell_key)
let prior_n: Int = if str_eq(prior_count, "") { 0 } else { str_to_int(prior_count) }
state_set(bell_key, int_to_str(prior_n + 1))
// Also record the highest bell level seen this session so the boundary
// summary can classify the session correctly (hard takes precedence).
let level_key: String = if str_eq(sess_id, "") {
"session_bell_level"
} else {
"session_bell_level:" + sess_id
}
let prior_level: String = state_get(level_key)
let new_level: String = if str_eq(bell_level, "hard") { "hard" } else {
if str_eq(prior_level, "hard") { "hard" } else { "soft" }
}
state_set(level_key, new_level)
// Stash a short signal summary for the boundary node (last bell wins for
// the one-liner; the full history is in per-bell BellEvent nodes).
let signal_key: String = if str_eq(sess_id, "") {
"session_bell_signal"
} else {
"session_bell_signal:" + sess_id
}
state_set(signal_key, safe_summary)
}
}
// strengthen_chat_nodes strengthen the engram nodes that were activated during a chat.
+107 -7
View File
@@ -7,6 +7,65 @@ import "neuron-api.el"
import "sessions.el"
import "soul.elh"
// ---------------------------------------------------------------------------
// Rate limiting simple in-memory per-IP sliding window counter.
//
// State keys:
// rl:<ip>:count request count in the current window
// rl:<ip>:window window start timestamp (unix seconds)
//
// Limit: configurable via soul state key "soul_rate_limit" (requests per
// minute). Falls back to 60 req/min if not set. The /health endpoint is
// exempt so monitoring does not consume quota.
//
// State growth: each unique source IP accumulates exactly 2 state keys
// (count + window) for the lifetime of the process. Per-IP storage is
// bounded and constant; values reset on window expiry. In aggregate, state
// grows linearly with distinct IPs typical for a trusted-client service.
// EL has no state_delete builtin, so keys from inactive IPs persist.
// TODO: add state_delete sweep when the EL runtime exposes that primitive.
//
// Returns "" when the request is allowed, or a 429 JSON body when rejected.
// ---------------------------------------------------------------------------
fn rate_limit_check(ip: String, path: String) -> String {
// Health checks are exempt they must never be blocked.
if str_eq(path, "/health") {
return ""
}
let limit_str: String = state_get("soul_rate_limit")
let limit: Int = if str_eq(limit_str, "") { 60 } else { str_to_int(limit_str) }
let now: Int = time_now()
let window_key: String = "rl:" + ip + ":window"
let count_key: String = "rl:" + ip + ":count"
let win_str: String = state_get(window_key)
let win_start: Int = if str_eq(win_str, "") { now } else { str_to_int(win_str) }
// New window every 60 seconds.
let elapsed: Int = now - win_start
let in_window: Bool = elapsed < 60
let prev_count_str: String = state_get(count_key)
let prev_count: Int = if str_eq(prev_count_str, "") { 0 } else { str_to_int(prev_count_str) }
// Reset window if expired.
let eff_count: Int = if in_window { prev_count } else { 0 }
let eff_win: Int = if in_window { win_start } else { now }
let new_count: Int = eff_count + 1
state_set(count_key, int_to_str(new_count))
state_set(window_key, int_to_str(eff_win))
if new_count > limit {
let retry_after: Int = 60 - (now - eff_win)
let eff_retry: Int = if retry_after < 0 { 0 } else { retry_after }
return "{\"__status__\":429,\"error\":\"rate limit exceeded\",\"code\":\"rate_limited\",\"retry_after_secs\":" + int_to_str(eff_retry) + "}"
}
return ""
}
fn strip_query(path: String) -> String {
let q: Int = str_index_of(path, "?")
if q < 0 {
@@ -16,11 +75,11 @@ fn strip_query(path: String) -> String {
}
fn err_404(path: String) -> String {
return "{\"error\":\"not found\",\"path\":\"" + path + "\"}"
return "{\"error\":\"not found\",\"code\":\"not_found\",\"path\":\"" + path + "\"}"
}
fn err_405(method: String, path: String) -> String {
return "{\"error\":\"method not allowed\",\"method\":\"" + method + "\",\"path\":\"" + path + "\"}"
return "{\"error\":\"method not allowed\",\"code\":\"method_not_allowed\",\"method\":\"" + method + "\",\"path\":\"" + path + "\"}"
}
fn route_health() -> String {
@@ -31,12 +90,35 @@ fn route_health() -> String {
let edge_ct: Int = engram_edge_count()
let pulse: String = state_get("soul.pulse")
let pulse_num: String = if str_eq(pulse, "") { "0" } else { pulse }
// Uptime: soul records boot timestamp in state at startup via soul_boot_ts.
// Compute elapsed seconds; fall back to -1 if not yet set.
let boot_ts_str: String = state_get("soul_boot_ts")
let uptime_secs: Int = if str_eq(boot_ts_str, "") {
-1
} else {
time_now() - str_to_int(boot_ts_str)
}
// LLM connectivity: probe with a minimal call. Any non-error reply = ok.
// Use a short, fixed prompt so this never counts against conversation history.
let model: String = state_get("soul_model")
let eff_model: String = if str_eq(model, "") { "claude-sonnet-4-5" } else { model }
let llm_probe: String = llm_call_system(eff_model, "You are a health probe. Reply with the single word: ok", "ping")
let llm_ok: Bool = !str_eq(llm_probe, "")
&& !str_starts_with(llm_probe, "{\"error\"")
&& !str_starts_with(llm_probe, "{\"type\":\"error\"")
&& !str_contains(llm_probe, "authentication_error")
let llm_status: String = if llm_ok { "ok" } else { "unreachable" }
return "{\"status\":\"alive\""
+ ",\"cgi_id\":\"" + cgi_id + "\""
+ ",\"boot\":" + boot_num
+ ",\"uptime_secs\":" + int_to_str(uptime_secs)
+ ",\"node_count\":" + int_to_str(node_ct)
+ ",\"edge_count\":" + int_to_str(edge_ct)
+ ",\"pulse\":" + pulse_num
+ ",\"llm\":\"" + llm_status + "\""
+ ",\"layers\":{\"l0\":\"core\",\"l1\":\"safety\",\"l2\":\"stewardship\",\"l3\":\"" + imprint_current() + "\"}}"
}
@@ -103,15 +185,15 @@ fn route_imprint_user(body: String) -> String {
fn route_synthesize(body: String) -> String {
if str_eq(body, "") {
return "{\"mechanism\":\"did not engage\"}"
return "{\"error\":\"body is required\",\"code\":\"missing_param\"}"
}
let parent_a: String = json_get(body, "parent_a")
let parent_b: String = json_get(body, "parent_b")
if str_eq(parent_a, "") {
return "{\"mechanism\":\"did not engage\"}"
return "{\"error\":\"parent_a is required\",\"code\":\"missing_param\"}"
}
if str_eq(parent_b, "") {
return "{\"mechanism\":\"did not engage\"}"
return "{\"error\":\"parent_b is required\",\"code\":\"missing_param\"}"
}
let req: String = "synthesize " + parent_a + " " + parent_b
let tags: String = "[\"soul-inbox-pending\",\"synthesis-request\"]"
@@ -259,6 +341,17 @@ fn handle_connectors(method: String, clean: String, body: String) -> String {
fn handle_request(method: String, path: String, body: String) -> String {
let clean: String = strip_query(path)
// Rate limit check. Extract caller IP from REMOTE_ADDR env var (set by the
// EL HTTP runtime for each request). Skip enforcement when empty so
// loopback/internal callers are never blocked.
let ip: String = env("REMOTE_ADDR")
if !str_eq(ip, "") {
let rl_result: String = rate_limit_check(ip, clean)
if !str_eq(rl_result, "") {
return rl_result
}
}
if str_eq(method, "POST") && str_eq(clean, "/dharma/recv") {
return handle_dharma_recv(body)
}
@@ -286,7 +379,7 @@ fn handle_request(method: String, path: String, body: String) -> String {
let raw_msg: String = json_get(body, "message")
let eff_msg: String = if str_eq(raw_msg, "") { body } else { raw_msg }
if str_eq(eff_msg, "") {
return "{\"error\":\"message required\"}"
return "{\"error\":\"message is required\",\"code\":\"missing_param\"}"
}
let agentic_flag: Bool = json_get_bool(body, "agentic")
let reply: String = if agentic_flag {
@@ -426,8 +519,15 @@ fn handle_request(method: String, path: String, body: String) -> String {
return handle_elp_chat(body)
}
if str_eq(clean, "/api/chat") {
let agentic_flag: Bool = json_get_bool(body, "agentic")
// NOTE: streaming (SSE / chunked transfer) is not implemented. All chat
// responses are buffered and returned as a single JSON object. Streaming
// would require runtime-level SSE support in el_runtime.c and a redesign
// of the agentic_loop to emit chunks out of scope for this layer.
let raw_msg: String = json_get(body, "message")
if str_eq(raw_msg, "") {
return "{\"error\":\"message is required\",\"code\":\"missing_param\"}"
}
let agentic_flag: Bool = json_get_bool(body, "agentic")
let reply: String = if agentic_flag {
handle_chat_agentic(body)
} else {
-42
View File
@@ -368,48 +368,6 @@ fn session_hist_save(session_id: String, hist: String) -> Void {
el_from_float(0.6), el_from_float(0.6), el_from_float(0.9),
"Episodic", tags
)
// Session boundary emotional summary written once per session the first time
// a bell event has fired. The summary node is findable by future sessions via
// broad affective queries ("session:emotional-summary" or "bell distress session").
// It is NOT rewritten on every save the state flag prevents duplicate nodes.
let summary_written_key: String = "session_bell_summary_written:" + session_id
let already_written: String = state_get(summary_written_key)
if str_eq(already_written, "") {
let bell_count_key: String = "session_bell_count:" + session_id
let bell_count_raw: String = state_get(bell_count_key)
let bell_count: Int = if str_eq(bell_count_raw, "") { 0 } else { str_to_int(bell_count_raw) }
if bell_count > 0 {
let bell_level_key: String = "session_bell_level:" + session_id
let bell_signal_key: String = "session_bell_signal:" + session_id
let dominant_level: String = state_get(bell_level_key)
let last_signal: String = state_get(bell_signal_key)
let eff_level: String = if str_eq(dominant_level, "") { "soft" } else { dominant_level }
let eff_signal: String = if str_eq(last_signal, "") { "(no signal captured)" } else { last_signal }
let ts_now: Int = time_now()
let summary_content: String = "session:emotional-summary"
+ " | session:" + session_id
+ " | bell_count:" + int_to_str(bell_count)
+ " | dominant_level:" + eff_level
+ " | last_signal:" + eff_signal
+ " | ts:" + int_to_str(ts_now)
let summary_tags: String = "[\"session-emotional-summary\",\"affective\",\"bell:" + eff_level + "\",\"BellEvent\"]"
let summary_sal: String = if str_eq(eff_level, "hard") { el_from_float(0.95) } else { el_from_float(0.85) }
let sum_discard: String = engram_node_full(
summary_content,
"BellEvent",
"session:emotional-summary",
summary_sal,
summary_sal,
el_from_float(1.0),
"Episodic",
summary_tags
)
// Mark written so we do not create duplicate summary nodes as the
// session continues accumulating more turns.
state_set(summary_written_key, "1")
}
}
}
// session_update_meta_timestamp update the updated_at field in the session:meta node.
+1
View File
@@ -369,6 +369,7 @@ load_identity_context()
seed_persona_from_env()
let boot_num: Int = mem_boot_count_inc()
state_set("soul_boot_count", int_to_str(boot_num))
state_set("soul_boot_ts", int_to_str(time_now()))
println("[soul] boot #" + int_to_str(boot_num))
emit_session_start_event()