Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d008649c3e | |||
| aa70c5dde6 |
+34
-7
@@ -23,14 +23,11 @@ fn ise_post(content: String) -> Void {
|
|||||||
let ise_url: String = env("SOUL_ISE_URL")
|
let ise_url: String = env("SOUL_ISE_URL")
|
||||||
let engram_url: String = if str_eq(ise_url, "") { state_get("soul_engram_url") } else { ise_url }
|
let engram_url: String = if str_eq(ise_url, "") { state_get("soul_engram_url") } else { ise_url }
|
||||||
if str_eq(engram_url, "") {
|
if str_eq(engram_url, "") {
|
||||||
let local_id: String = engram_node_full(
|
let discard: String = engram_node_full(
|
||||||
content, "InternalStateEvent", "state-event",
|
content, "InternalStateEvent", "state-event",
|
||||||
el_from_float(0.3), el_from_float(0.3), el_from_float(0.8),
|
el_from_float(0.3), el_from_float(0.3), el_from_float(0.8),
|
||||||
"Episodic", "[\"internal-state\",\"InternalStateEvent\"]"
|
"Episodic", "[\"internal-state\",\"InternalStateEvent\"]"
|
||||||
)
|
)
|
||||||
if str_eq(local_id, "") {
|
|
||||||
println("[awareness] ise_post: local engram_node_full failed — ISE lost")
|
|
||||||
}
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
// Proper JSON string escaping: backslashes first, then quotes, then control chars.
|
// Proper JSON string escaping: backslashes first, then quotes, then control chars.
|
||||||
@@ -43,7 +40,32 @@ fn ise_post(content: String) -> Void {
|
|||||||
let safe3: String = str_replace(safe2, "\n", "\\n")
|
let safe3: String = str_replace(safe2, "\n", "\\n")
|
||||||
let safe4: String = str_replace(safe3, "\r", "\\r")
|
let safe4: String = str_replace(safe3, "\r", "\\r")
|
||||||
let body: String = "{\"content\":\"" + safe4 + "\"}"
|
let body: String = "{\"content\":\"" + safe4 + "\"}"
|
||||||
let discard: String = http_post_json(engram_url + "/api/neuron/state-events", body)
|
// Soft circuit-breaker: skip HTTP call when engram is known-down (30s backoff).
|
||||||
|
// Opens after 3 consecutive failures; half-open probe after backoff expires.
|
||||||
|
// TODO(reliability): full async dispatch requires EL runtime futures support.
|
||||||
|
let cb_open: String = state_get("engram_cb_open")
|
||||||
|
if str_eq(cb_open, "1") {
|
||||||
|
let cb_ts_s: String = state_get("engram_cb_open_ts")
|
||||||
|
let cb_ts: Int = if str_eq(cb_ts_s, "") { 0 } else { str_to_int(cb_ts_s) }
|
||||||
|
let cb_elapsed: Int = time_now() - cb_ts
|
||||||
|
if cb_elapsed < 30000 { return "" }
|
||||||
|
state_set("engram_cb_open", "0")
|
||||||
|
}
|
||||||
|
let resp: String = http_post_json(engram_url + "/api/neuron/state-events", body)
|
||||||
|
let cb_failed: Bool = str_eq(resp, "") || str_starts_with(resp, "{"error":")
|
||||||
|
if cb_failed {
|
||||||
|
let fn_s: String = state_get("engram_cb_fails")
|
||||||
|
let fn_n: Int = if str_eq(fn_s, "") { 0 } else { str_to_int(fn_s) }
|
||||||
|
let fn_n = fn_n + 1
|
||||||
|
state_set("engram_cb_fails", int_to_str(fn_n))
|
||||||
|
if fn_n >= 3 {
|
||||||
|
state_set("engram_cb_open", "1")
|
||||||
|
state_set("engram_cb_open_ts", int_to_str(time_now()))
|
||||||
|
println("[awareness] engram circuit-breaker OPEN after " + int_to_str(fn_n) + " failures")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state_set("engram_cb_fails", "0")
|
||||||
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -543,9 +565,14 @@ fn awareness_run() -> Void {
|
|||||||
let should_refresh: Bool = refresh_elapsed >= refresh_ms
|
let should_refresh: Bool = refresh_elapsed >= refresh_ms
|
||||||
if should_refresh {
|
if should_refresh {
|
||||||
let engram_url: String = state_get("soul_engram_url")
|
let engram_url: String = state_get("soul_engram_url")
|
||||||
if !str_eq(engram_url, "") {
|
let sc: String = state_get("engram_cb_open")
|
||||||
|
let sc_ts_s: String = state_get("engram_cb_open_ts")
|
||||||
|
let sc_ts: Int = if str_eq(sc_ts_s, "") { 0 } else { str_to_int(sc_ts_s) }
|
||||||
|
let sc_elapsed: Int = now_ts - sc_ts
|
||||||
|
let sync_allowed: Bool = !str_eq(sc, "1") || sc_elapsed >= 30000
|
||||||
|
if !str_eq(engram_url, "") && sync_allowed {
|
||||||
let sync_json: String = http_get(engram_url + "/api/sync")
|
let sync_json: String = http_get(engram_url + "/api/sync")
|
||||||
if !str_eq(sync_json, "") && !str_eq(sync_json, "{}") {
|
if !str_eq(sync_json, "") && !str_eq(sync_json, "{}") && !str_starts_with(sync_json, "{\"error\":") {
|
||||||
let cgi_id: String = state_get("soul_cgi_id")
|
let cgi_id: String = state_get("soul_cgi_id")
|
||||||
let tmp: String = "/tmp/soul-sync-" + cgi_id + ".json"
|
let tmp: String = "/tmp/soul-sync-" + cgi_id + ".json"
|
||||||
fs_write(tmp, sync_json)
|
fs_write(tmp, sync_json)
|
||||||
|
|||||||
@@ -130,14 +130,11 @@ fn conv_history_persist(hist: String) -> Void {
|
|||||||
if str_eq(hist, "[]") { return "" }
|
if str_eq(hist, "[]") { return "" }
|
||||||
let ts: Int = time_now()
|
let ts: Int = time_now()
|
||||||
let tags: String = "[\"conv-history\",\"persistent\"]"
|
let tags: String = "[\"conv-history\",\"persistent\"]"
|
||||||
let node_id: String = engram_node_full(
|
let discard: String = engram_node_full(
|
||||||
hist, "Conversation", "conv:history",
|
hist, "Conversation", "conv:history",
|
||||||
el_from_float(0.7), el_from_float(0.8), el_from_float(0.9),
|
el_from_float(0.7), el_from_float(0.8), el_from_float(0.9),
|
||||||
"Episodic", tags
|
"Episodic", tags
|
||||||
)
|
)
|
||||||
if str_eq(node_id, "") {
|
|
||||||
println("[chat] conv_history_persist: engram_node_full returned empty — history node lost")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// conv_history_load — restore conversation history from engram on first access.
|
// conv_history_load — restore conversation history from engram on first access.
|
||||||
@@ -189,6 +186,10 @@ fn handle_chat(body: String) -> String {
|
|||||||
let req_model: String = json_get(body, "model")
|
let req_model: String = json_get(body, "model")
|
||||||
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
|
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
|
||||||
|
|
||||||
|
// ISSUE 9: add safety_augment_system to primary /api/chat path.
|
||||||
|
// handle_chat was the only LLM path missing bell directive injection.
|
||||||
|
let full_system = safety_augment_system(full_system, message)
|
||||||
|
|
||||||
let raw_response: String = llm_call_system(model, full_system, message)
|
let raw_response: String = llm_call_system(model, full_system, message)
|
||||||
|
|
||||||
let is_error: Bool = str_starts_with(raw_response, "{\"error\"")
|
let is_error: Bool = str_starts_with(raw_response, "{\"error\"")
|
||||||
@@ -640,21 +641,6 @@ fn handle_chat_agentic(body: String) -> String {
|
|||||||
// Thread-aware activation: same logic as handle_chat.
|
// Thread-aware activation: same logic as handle_chat.
|
||||||
// Use the session's or global history to anchor short messages to the thread.
|
// Use the session's or global history to anchor short messages to the thread.
|
||||||
let req_session: String = json_get(body, "session_id")
|
let req_session: String = json_get(body, "session_id")
|
||||||
|
|
||||||
// ISSUE #6/#7: validate that the session_id actually exists before proceeding.
|
|
||||||
// Without this check the loop silently treats any unknown/fabricated session_id
|
|
||||||
// as a fresh session — history loads as empty and no error is returned to the caller.
|
|
||||||
// Only validate when a session_id is explicitly provided; anonymous calls
|
|
||||||
// (no session_id) continue to work for backward compatibility.
|
|
||||||
let session_valid: Bool = if str_eq(req_session, "") {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
!str_contains(session_get(req_session), "\"error\"")
|
|
||||||
}
|
|
||||||
if !session_valid {
|
|
||||||
return "{\"error\":\"session not found\",\"session_id\":\"" + req_session + "\",\"reply\":\"\"}"
|
|
||||||
}
|
|
||||||
|
|
||||||
let hist_key: String = if str_eq(req_session, "") { "conv_history" } else { "session_hist_" + req_session }
|
let hist_key: String = if str_eq(req_session, "") { "conv_history" } else { "session_hist_" + req_session }
|
||||||
let agentic_hist: String = state_get(hist_key)
|
let agentic_hist: String = state_get(hist_key)
|
||||||
let agentic_hist_len: Int = if str_eq(agentic_hist, "") { 0 } else { json_array_len(agentic_hist) }
|
let agentic_hist_len: Int = if str_eq(agentic_hist, "") { 0 } else { json_array_len(agentic_hist) }
|
||||||
@@ -1072,19 +1058,13 @@ fn handle_dharma_room_turn(body: String) -> String {
|
|||||||
// engram_node(content, "episodic", ...) which wrongly put a TIER into the node_type
|
// engram_node(content, "episodic", ...) which wrongly put a TIER into the node_type
|
||||||
// slot — that's why nodes showed node_type="episodic". Use the full, correct contract.)
|
// slot — that's why nodes showed node_type="episodic". Use the full, correct contract.)
|
||||||
let utterance_tags: String = "[\"soul-utterance\",\"episodic\"]"
|
let utterance_tags: String = "[\"soul-utterance\",\"episodic\"]"
|
||||||
let utterance_id: String = engram_node_full(
|
let discard_id: String = engram_node_full(
|
||||||
clean_response, "Conversation", "soul:utterance",
|
clean_response, "Conversation", "soul:utterance",
|
||||||
el_from_float(0.6), el_from_float(0.6), el_from_float(0.8),
|
el_from_float(0.6), el_from_float(0.6), el_from_float(0.8),
|
||||||
"Episodic", utterance_tags
|
"Episodic", utterance_tags
|
||||||
)
|
)
|
||||||
if str_eq(utterance_id, "") {
|
|
||||||
println("[chat] handle_dharma_room_turn: utterance engram write failed — node lost")
|
|
||||||
}
|
|
||||||
if !str_eq(snap_path, "") {
|
if !str_eq(snap_path, "") {
|
||||||
let save_result: String = engram_save(snap_path)
|
let discard_save: String = engram_save(snap_path)
|
||||||
if str_eq(save_result, "") {
|
|
||||||
println("[chat] handle_dharma_room_turn: engram_save failed for " + snap_path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let safe_response: String = json_safe(clean_response)
|
let safe_response: String = json_safe(clean_response)
|
||||||
@@ -1166,7 +1146,7 @@ fn auto_persist(req: String, resp: String) -> Void {
|
|||||||
+ ",\"label\":\"chat:" + ts_str + "\"}"
|
+ ",\"label\":\"chat:" + ts_str + "\"}"
|
||||||
|
|
||||||
let tags: String = "[\"Conversation\",\"chat\",\"timestamped\"]"
|
let tags: String = "[\"Conversation\",\"chat\",\"timestamped\"]"
|
||||||
let persist_id: String = engram_node_full(
|
engram_node_full(
|
||||||
content,
|
content,
|
||||||
"Conversation",
|
"Conversation",
|
||||||
"chat:" + ts_str,
|
"chat:" + ts_str,
|
||||||
@@ -1176,9 +1156,6 @@ fn auto_persist(req: String, resp: String) -> Void {
|
|||||||
"Episodic",
|
"Episodic",
|
||||||
tags
|
tags
|
||||||
)
|
)
|
||||||
if str_eq(persist_id, "") {
|
|
||||||
println("[chat] auto_persist: engram_node_full returned empty — conversation node lost (ts=" + ts_str + ")")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// strengthen_chat_nodes — strengthen the engram nodes that were activated during a chat.
|
// strengthen_chat_nodes — strengthen the engram nodes that were activated during a chat.
|
||||||
|
|||||||
+8
-4
@@ -24,19 +24,23 @@ ENGRAM_DATA_DIR="$ENGRAM_DATA_DIR" \
|
|||||||
|
|
||||||
ENGRAM_PID=$!
|
ENGRAM_PID=$!
|
||||||
|
|
||||||
# Wait for engram to become healthy (up to 30s)
|
# Wait for engram to become healthy (up to 60s; GKE Autopilot cold starts can be slow)
|
||||||
echo "[entrypoint] waiting for engram..."
|
echo "[entrypoint] waiting for engram..."
|
||||||
TRIES=0
|
TRIES=0
|
||||||
until curl -sf "$ENGRAM_HEALTH_URL" > /dev/null 2>&1; do
|
until curl -sf "$ENGRAM_HEALTH_URL" > /dev/null 2>&1; do
|
||||||
TRIES=$((TRIES + 1))
|
TRIES=$((TRIES + 1))
|
||||||
if [ "$TRIES" -ge 30 ]; then
|
if [ "$TRIES" -ge 60 ]; then
|
||||||
echo "[entrypoint] ERROR: engram did not become healthy after 30s" >&2
|
echo "[entrypoint] ERROR: engram did not become healthy after 60s" >&2
|
||||||
kill "$ENGRAM_PID" 2>/dev/null || true
|
kill "$ENGRAM_PID" 2>/dev/null || true
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
echo "[entrypoint] engram ready"
|
echo "[entrypoint] engram ready after ${TRIES}s"
|
||||||
|
|
||||||
|
# Tune EL HTTP runtime: reduce per-call timeout 60s->10s, connect timeout 3s.
|
||||||
|
export EL_HTTP_TIMEOUT_MS="${EL_HTTP_TIMEOUT_MS:-10000}"
|
||||||
|
export EL_HTTP_CONNECT_TIMEOUT_MS="${EL_HTTP_CONNECT_TIMEOUT_MS:-3000}"
|
||||||
|
|
||||||
# Start soul — it takes over as PID 1's foreground process.
|
# Start soul — it takes over as PID 1's foreground process.
|
||||||
# SOUL_ENGRAM_PATH must NOT be set; ENGRAM_URL triggers HTTP mode.
|
# SOUL_ENGRAM_PATH must NOT be set; ENGRAM_URL triggers HTTP mode.
|
||||||
|
|||||||
@@ -46,10 +46,7 @@ fn mem_consolidate() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn mem_save(path: String) -> Void {
|
fn mem_save(path: String) -> Void {
|
||||||
let save_result: String = engram_save(path)
|
engram_save(path)
|
||||||
if str_eq(save_result, "") {
|
|
||||||
println("[memory] mem_save: engram_save failed for " + path + " — snapshot may be incomplete")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mem_load(path: String) -> Void {
|
fn mem_load(path: String) -> Void {
|
||||||
@@ -79,14 +76,11 @@ fn mem_boot_count_inc() -> Int {
|
|||||||
let next: Int = current + 1
|
let next: Int = current + 1
|
||||||
let content: String = "soul:boot_count:" + int_to_str(next)
|
let content: String = "soul:boot_count:" + int_to_str(next)
|
||||||
let tags: String = "[\"soul-meta\",\"boot-counter\"]"
|
let tags: String = "[\"soul-meta\",\"boot-counter\"]"
|
||||||
let boot_node_id: String = engram_node_full(
|
let discard: String = engram_node_full(
|
||||||
content, "Memory", "soul:boot_count",
|
content, "Memory", "soul:boot_count",
|
||||||
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
|
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
|
||||||
"Canonical", tags
|
"Canonical", tags
|
||||||
)
|
)
|
||||||
if str_eq(boot_node_id, "") {
|
|
||||||
println("[memory] mem_boot_count_inc: engram write failed — boot counter node lost (count=" + int_to_str(next) + ")")
|
|
||||||
}
|
|
||||||
return next
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-10
@@ -400,7 +400,6 @@ fn handle_api_log_state_event(body: String) -> String {
|
|||||||
let id: String = engram_node_full(parts, "InternalStateEvent", "state-event:manual",
|
let id: String = engram_node_full(parts, "InternalStateEvent", "state-event:manual",
|
||||||
el_from_float(0.85), el_from_float(0.85), el_from_float(0.9),
|
el_from_float(0.85), el_from_float(0.85), el_from_float(0.9),
|
||||||
"Episodic", tags)
|
"Episodic", tags)
|
||||||
if !api_persisted(id) { return api_not_persisted(id) }
|
|
||||||
return "{\"ok\":true,\"id\":\"" + id + "\",\"boot\":\"" + boot + "\"}"
|
return "{\"ok\":true,\"id\":\"" + id + "\",\"boot\":\"" + boot + "\"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,7 +452,6 @@ fn handle_api_tune_config(body: String) -> String {
|
|||||||
let id: String = engram_node_full(content, "ConfigEntry", key,
|
let id: String = engram_node_full(content, "ConfigEntry", key,
|
||||||
el_from_float(0.85), el_from_float(0.85), el_from_float(0.9),
|
el_from_float(0.85), el_from_float(0.85), el_from_float(0.9),
|
||||||
"Canonical", tags)
|
"Canonical", tags)
|
||||||
if !api_persisted(id) { return api_not_persisted(id) }
|
|
||||||
return "{\"ok\":true,\"key\":\"" + key + "\",\"value\":\"" + value + "\",\"id\":\"" + id + "\"}"
|
return "{\"ok\":true,\"key\":\"" + key + "\",\"value\":\"" + value + "\",\"id\":\"" + id + "\"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -653,23 +651,17 @@ fn handle_api_consolidate(body: String) -> String {
|
|||||||
let summary: String = json_get(body, "summary")
|
let summary: String = json_get(body, "summary")
|
||||||
let snap: String = state_get("soul_snapshot_path")
|
let snap: String = state_get("soul_snapshot_path")
|
||||||
if !str_eq(snap, "") {
|
if !str_eq(snap, "") {
|
||||||
let save_result: String = engram_save(snap)
|
engram_save(snap)
|
||||||
if str_eq(save_result, "") {
|
|
||||||
println("[api] consolidate: engram_save failed for " + snap + " — snapshot may be out of sync")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if !str_eq(summary, "") {
|
if !str_eq(summary, "") {
|
||||||
let safe_summary: String = str_replace(summary, "\"", "'")
|
let safe_summary: String = str_replace(summary, "\"", "'")
|
||||||
let tags: String = "[\"SessionSummary\",\"consolidate\"]"
|
let tags: String = "[\"SessionSummary\",\"consolidate\"]"
|
||||||
let summary_id: String = engram_node_full(
|
let discard: String = engram_node_full(
|
||||||
"[session-summary] " + safe_summary,
|
"[session-summary] " + safe_summary,
|
||||||
"SessionSummary", "session:summary",
|
"SessionSummary", "session:summary",
|
||||||
el_from_float(0.7), el_from_float(0.7), el_from_float(0.9),
|
el_from_float(0.7), el_from_float(0.7), el_from_float(0.9),
|
||||||
"Episodic", tags
|
"Episodic", tags
|
||||||
)
|
)
|
||||||
if str_eq(summary_id, "") {
|
|
||||||
println("[api] consolidate: session summary engram write failed — summary node lost")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "{\"ok\":true,\"snapshot\":\"" + snap + "\"}"
|
return "{\"ok\":true,\"snapshot\":\"" + snap + "\"}"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,17 +144,22 @@ fn safety_screen(input: String, history: String) -> String {
|
|||||||
if score >= soft {
|
if score >= soft {
|
||||||
let summary: String = str_slice(input, 0, 80)
|
let summary: String = str_slice(input, 0, 80)
|
||||||
let discard: String = safety_log_bell("soft", "wellbeing check needed", summary)
|
let discard: String = safety_log_bell("soft", "wellbeing check needed", summary)
|
||||||
|
// ISSUE 7 fix: escape tab chars in addition to backslash/quote/newline/CR.
|
||||||
|
// A tab in user input corrupts the JSON envelope and causes json_get to misparse.
|
||||||
let e1: String = str_replace(input, "\\", "\\\\")
|
let e1: String = str_replace(input, "\\", "\\\\")
|
||||||
let e2: String = str_replace(e1, "\"", "\\\"")
|
let e2: String = str_replace(e1, "\"", "\\\"")
|
||||||
let e3: String = str_replace(e2, "\n", "\\n")
|
let e3: String = str_replace(e2, "\n", "\\n")
|
||||||
let safe_input: String = str_replace(e3, "\r", "\\r")
|
let e4: String = str_replace(e3, "\r", "\\r")
|
||||||
|
let safe_input: String = str_replace(e4, "\t", "\\t")
|
||||||
return "{\"action\":\"soft_bell\",\"reason\":\"wellbeing check needed\",\"content\":\"" + safe_input + "\"}"
|
return "{\"action\":\"soft_bell\",\"reason\":\"wellbeing check needed\",\"content\":\"" + safe_input + "\"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ISSUE 7 fix: escape tab chars (see soft_bell branch above for rationale).
|
||||||
let e1: String = str_replace(input, "\\", "\\\\")
|
let e1: String = str_replace(input, "\\", "\\\\")
|
||||||
let e2: String = str_replace(e1, "\"", "\\\"")
|
let e2: String = str_replace(e1, "\"", "\\\"")
|
||||||
let e3: String = str_replace(e2, "\n", "\\n")
|
let e3: String = str_replace(e2, "\n", "\\n")
|
||||||
let safe_input: String = str_replace(e3, "\r", "\\r")
|
let e4: String = str_replace(e3, "\r", "\\r")
|
||||||
|
let safe_input: String = str_replace(e4, "\t", "\\t")
|
||||||
return "{\"action\":\"pass\",\"content\":\"" + safe_input + "\"}"
|
return "{\"action\":\"pass\",\"content\":\"" + safe_input + "\"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,7 +200,11 @@ fn safety_validate(output: String, action: String) -> String {
|
|||||||
fn safety_log_bell(level: String, reason: String, input_summary: String) -> String {
|
fn safety_log_bell(level: String, reason: String, input_summary: String) -> String {
|
||||||
let content: String = "BELL:" + level + " | " + reason + " | summary:" + input_summary
|
let content: String = "BELL:" + level + " | " + reason + " | summary:" + input_summary
|
||||||
let tags: String = "[\"safety\",\"bell\",\"bell:" + level + "\"]"
|
let tags: String = "[\"safety\",\"bell\",\"bell:" + level + "\"]"
|
||||||
let discard: String = engram_node_full(
|
// ISSUE 2 fix: if engram_node_full returns empty the write silently failed.
|
||||||
|
// Emit a fallback println so the bell event leaves at least a log trace even
|
||||||
|
// when engram is degraded. This does not replace engram persistence -- it is a
|
||||||
|
// last-resort audit trail when the primary write cannot be confirmed.
|
||||||
|
let node_id: String = engram_node_full(
|
||||||
content,
|
content,
|
||||||
"BellEvent",
|
"BellEvent",
|
||||||
"bell:" + level,
|
"bell:" + level,
|
||||||
@@ -205,6 +214,9 @@ fn safety_log_bell(level: String, reason: String, input_summary: String) -> Stri
|
|||||||
"Episodic",
|
"Episodic",
|
||||||
tags
|
tags
|
||||||
)
|
)
|
||||||
|
if str_eq(node_id, "") {
|
||||||
|
println("[safety] WARN: bell event engram write failed -- fallback log: " + content)
|
||||||
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,6 +247,17 @@ fn safety_soft_phrases() -> String {
|
|||||||
return "[\"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 "[\"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\"]"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ISSUE 5 TODO: phrase lists are rebuilt from JSON literals on every call.
|
||||||
|
// safety_any_match and safety_count_match loop over json_array_get on every invocation.
|
||||||
|
// A compiled/cached representation would reduce per-message overhead and also guard against
|
||||||
|
// malformed phrase JSON (json_array_len of malformed input returns 0, silently skipping all checks).
|
||||||
|
// Caching requires language-level static const arrays -- not available in current EL.
|
||||||
|
// When EL gains module-level const arrays, migrate phrase lists to that form.
|
||||||
|
//
|
||||||
|
// ISSUE 5 TODO: phrase lists are rebuilt from JSON literals on every call to
|
||||||
|
// safety_any_match / safety_count_match. json_array_len of a malformed string
|
||||||
|
// returns 0, silently skipping all checks. Caching requires language-level static
|
||||||
|
// const arrays (not available in current EL). Migrate when EL gains that feature.
|
||||||
// ── Matching helpers (single loops only — el escapes while-body mutation via
|
// ── Matching helpers (single loops only — el escapes while-body mutation via
|
||||||
// top-level let rebinds; nested loops would not advance) ────────────────────
|
// top-level let rebinds; nested loops would not advance) ────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,9 @@ import "stewardship.el"
|
|||||||
import "imprint.el"
|
import "imprint.el"
|
||||||
import "awareness.el"
|
import "awareness.el"
|
||||||
import "chat.el"
|
import "chat.el"
|
||||||
import "safety.el"
|
|
||||||
import "studio.el"
|
import "studio.el"
|
||||||
import "elp-input.el"
|
import "elp-input.el"
|
||||||
import "routes.el"
|
import "routes.el"
|
||||||
import "safety.el"
|
|
||||||
import "stewardship.el"
|
|
||||||
import "imprint.el"
|
|
||||||
|
|
||||||
cgi "neuron-soul" {
|
cgi "neuron-soul" {
|
||||||
dharma_id: "ntn-genesis@http://localhost:7770",
|
dharma_id: "ntn-genesis@http://localhost:7770",
|
||||||
@@ -212,13 +208,8 @@ fn seed_persona_from_env() -> Void {
|
|||||||
let h: Map = {}
|
let h: Map = {}
|
||||||
map_set(h, "Content-Type", "application/json")
|
map_set(h, "Content-Type", "application/json")
|
||||||
let resp: String = http_post_with_headers(engram_url + "/api/nodes", body, h)
|
let resp: String = http_post_with_headers(engram_url + "/api/nodes", body, h)
|
||||||
// Check for empty response (timeout/network error), explicit error, or missing id.
|
if str_contains(resp, "\"error\"") {
|
||||||
if str_eq(resp, "") {
|
|
||||||
println("[soul] persona HTTP write-back failed: empty response (timeout or network error) — in-memory only this session")
|
|
||||||
} else if str_contains(resp, "\"error\"") {
|
|
||||||
println("[soul] persona HTTP write-back failed (in-memory only this session): " + resp)
|
println("[soul] persona HTTP write-back failed (in-memory only this session): " + resp)
|
||||||
} else if !str_contains(resp, "\"id\"") {
|
|
||||||
println("[soul] persona HTTP write-back: unexpected response (no id field) — in-memory only this session: " + resp)
|
|
||||||
} else {
|
} else {
|
||||||
println("[soul] persona persisted to HTTP engram at " + engram_url)
|
println("[soul] persona persisted to HTTP engram at " + engram_url)
|
||||||
}
|
}
|
||||||
@@ -251,14 +242,11 @@ fn emit_session_start_event() -> Void {
|
|||||||
+ ",\"ts\":" + int_to_str(ts) + "}"
|
+ ",\"ts\":" + int_to_str(ts) + "}"
|
||||||
|
|
||||||
let tags: String = "[\"internal-state\",\"session-start\",\"InternalStateEvent\"]"
|
let tags: String = "[\"internal-state\",\"session-start\",\"InternalStateEvent\"]"
|
||||||
let session_event_id: String = engram_node_full(
|
let discard: String = engram_node_full(
|
||||||
payload, "InternalStateEvent", "session-start",
|
payload, "InternalStateEvent", "session-start",
|
||||||
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
|
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
|
||||||
"Episodic", tags
|
"Episodic", tags
|
||||||
)
|
)
|
||||||
if str_eq(session_event_id, "") {
|
|
||||||
println("[soul] emit_session_start_event: engram write failed — session-start event lost")
|
|
||||||
}
|
|
||||||
println("[soul] session-start event logged (boot=" + boot_num + " nodes=" + int_to_str(node_ct) + " edges=" + int_to_str(edge_ct) + ")")
|
println("[soul] session-start event logged (boot=" + boot_num + " nodes=" + int_to_str(node_ct) + " edges=" + int_to_str(edge_ct) + ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,19 +261,32 @@ fn layered_cycle(raw_input: String) -> String {
|
|||||||
let screen_result: String = safety_screen(raw_input, history)
|
let screen_result: String = safety_screen(raw_input, history)
|
||||||
let screen_action: String = json_get(screen_result, "action")
|
let screen_action: String = json_get(screen_result, "action")
|
||||||
|
|
||||||
|
// ISSUE 4: safe-mode guard -- if safety_screen returned invalid/empty action,
|
||||||
|
// refuse the turn rather than silently passing unscreened input to upper layers.
|
||||||
|
// Valid actions: "hard_bell", "soft_bell", "pass". Anything else = corrupt envelope.
|
||||||
|
let valid_action: Bool = str_eq(screen_action, "hard_bell")
|
||||||
|
|| str_eq(screen_action, "soft_bell")
|
||||||
|
|| str_eq(screen_action, "pass")
|
||||||
|
if !valid_action {
|
||||||
|
println("[soul] layered_cycle: safety_screen invalid action -- safe mode refusal")
|
||||||
|
return safety_validate("", "hard_bell")
|
||||||
|
}
|
||||||
|
|
||||||
// Hard bell: bypass all upper layers, log and escalate.
|
// Hard bell: bypass all upper layers, log and escalate.
|
||||||
// Intentionally does NOT update conversation_history or call auto_persist():
|
// Intentionally does NOT update conversation_history or call auto_persist():
|
||||||
// hard bell events are security-sensitive and must not appear in engram conversation
|
// hard bell events are security-sensitive and must not appear in engram conversation
|
||||||
// history where they could leak context to subsequent turns. They are persisted
|
// history where they could leak context to subsequent turns. They are persisted
|
||||||
// separately by safety_log_bell() into the Episodic tier with restricted labels.
|
// separately by safety_log_bell() into the Episodic tier with restricted labels.
|
||||||
//
|
//
|
||||||
|
// ISSUE 6: safety_log_bell for hard bells is already called INSIDE safety_screen
|
||||||
|
// (safety.el line 140). Do NOT call it again here -- double-log avoided.
|
||||||
|
//
|
||||||
// safety_validate second param: when screen_action is "hard_bell", safety_validate
|
// safety_validate second param: when screen_action is "hard_bell", safety_validate
|
||||||
// receives the sentinel string "hard_bell" (not a normal screen action). The safety
|
// receives the sentinel string "hard_bell" (not a normal screen action). The safety
|
||||||
// layer contract requires it to return a fixed refusal regardless of the output arg.
|
// layer contract requires it to return a fixed refusal regardless of the output arg.
|
||||||
// On the normal path, safety_validate receives the original screen_action ("pass")
|
// On the normal path, safety_validate receives the original screen_action ("pass")
|
||||||
// so it can apply action-specific post-output checks.
|
// so it can apply action-specific post-output checks.
|
||||||
if str_eq(screen_action, "hard_bell") {
|
if str_eq(screen_action, "hard_bell") {
|
||||||
safety_log_bell("hard", json_get(screen_result, "reason"), str_slice(raw_input, 0, 80))
|
|
||||||
return safety_validate("", "hard_bell")
|
return safety_validate("", "hard_bell")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,6 +321,16 @@ fn layered_cycle(raw_input: String) -> String {
|
|||||||
json_get(steward_result, "redirect_to")
|
json_get(steward_result, "redirect_to")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ISSUE 1: apply pre-LLM bell augmentation on layered_cycle path.
|
||||||
|
// safety_augment_system injects soft/hard directive into system prompt before LLM call.
|
||||||
|
// Stored in state so imprint_respond can consume it.
|
||||||
|
// TODO: wire directly into imprint_respond when it accepts a system_override param.
|
||||||
|
// ISSUE 3 TODO: no semantic/embedding crisis detection. Keyword-only means signals
|
||||||
|
// evading the phrase list pass through with zero augmentation. Semantic layer is a
|
||||||
|
// separate architectural decision requiring embedding inference on every message.
|
||||||
|
let augmented_addendum: String = safety_augment_system("", raw_input)
|
||||||
|
state_set("layered_cycle_safety_system_addendum", augmented_addendum)
|
||||||
|
|
||||||
// L3: imprint responds
|
// L3: imprint responds
|
||||||
let output: String = imprint_respond(aligned, imprint_id)
|
let output: String = imprint_respond(aligned, imprint_id)
|
||||||
|
|
||||||
@@ -359,12 +370,29 @@ let snapshot_usable: Bool = local_node_count > 50
|
|||||||
|
|
||||||
if using_http_engram && !snapshot_usable {
|
if using_http_engram && !snapshot_usable {
|
||||||
// First boot or empty/corrupt snapshot: seed from HTTP Engram.
|
// First boot or empty/corrupt snapshot: seed from HTTP Engram.
|
||||||
|
// Retry up to 3 times (2s sleep between attempts) to guard against a
|
||||||
|
// transient network hiccup right after entrypoint.sh health check passes.
|
||||||
|
// An empty nodes response silently loads a zero-node graph; validate first.
|
||||||
|
// TODO(reliability): replace sleep_ms retry with non-blocking backoff.
|
||||||
println("[soul] engram -> HTTP " + engram_url_raw + " (no local snapshot, first boot)")
|
println("[soul] engram -> HTTP " + engram_url_raw + " (no local snapshot, first boot)")
|
||||||
let nodes_json: String = http_get(engram_url_raw + "/api/nodes?limit=10000")
|
let fetch_attempt: Int = 0
|
||||||
let edges_json: String = http_get(engram_url_raw + "/api/edges")
|
while fetch_attempt < 3 {
|
||||||
let nodes_part: String = if str_eq(nodes_json, "") { "[]" } else { nodes_json }
|
let fetch_attempt = fetch_attempt + 1
|
||||||
let edges_part: String = if str_eq(edges_json, "") { "[]" } else { edges_json }
|
let n: String = http_get(engram_url_raw + "/api/nodes?limit=10000")
|
||||||
let snapshot_data: String = "{\"nodes\":" + nodes_part + ",\"edges\":" + edges_part + "}"
|
let e: String = http_get(engram_url_raw + "/api/edges")
|
||||||
|
let nodes_ok: Bool = !str_eq(n, "") && str_starts_with(n, "[") && str_len(n) > 2
|
||||||
|
if nodes_ok {
|
||||||
|
state_set("_boot_nodes_json", n)
|
||||||
|
state_set("_boot_edges_json", e)
|
||||||
|
let fetch_attempt = 3
|
||||||
|
} else {
|
||||||
|
println("[soul] boot HTTP fetch attempt " + int_to_str(fetch_attempt) + " failed --- retrying in 2s")
|
||||||
|
sleep_ms(2000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let nodes_json: String = state_get("_boot_nodes_json")
|
||||||
|
let edges_json: String = state_get("_boot_edges_json")
|
||||||
|
let snapshot_data: String = "{\"nodes\":" + nodes_part + ",\"edges\":" + edges_part + "}"
|
||||||
let tmp_path: String = "/tmp/soul-engram-" + soul_cgi_id + ".json"
|
let tmp_path: String = "/tmp/soul-engram-" + soul_cgi_id + ".json"
|
||||||
fs_write(tmp_path, snapshot_data)
|
fs_write(tmp_path, snapshot_data)
|
||||||
engram_load(tmp_path)
|
engram_load(tmp_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user