Compare commits

..

2 Commits

Author SHA1 Message Date
will.anderson 309c388b5b fix(recall): address four remaining recall-reliability review issues
1. engram_numeric_valid: "0.0"/"0.00"/"00.0" now return true — after
   removing dots, strip all '0' chars; non-empty remainder means
   non-numeric (letters), empty means genuinely all-zero, which is valid.
   Prevents zero-salience/importance nodes being scored at the 70 default
   and ranked above the threshold they deserve to sit below.

2. Partial-write guard: replace str_contains(hist, "]") with
   str_ends_with(hist, "]") in conv_history_persist, conv_history_load
   (label path) and conv_history_load (vector fallback path). A truncated
   array whose *content* contains "]" (e.g. "item 1] item 2") no longer
   passes the guard.

3. Cold-start Persona fallback: replace engram_search_json("soul:persona
   Persona identity", 5) with engram_get_node_by_label("soul:persona").
   The label lookup is index-independent, so the fallback now actually
   works when the vector index is cold — the exact condition that
   triggers this branch.

4. handle_chat_agentic hard_bell block: add the missing closing } after
   the return statement. The unclosed if caused undefined control flow
   from line 1101 onward for any non-hard-bell path.
2026-06-22 13:34:05 -05:00
will.anderson a39998a502 feat(recall): recall-reliability improvements
Neuron Soul CI / build (pull_request) Failing after 12m52s
- Q1: engram_numeric_valid() guard against non-numeric timestamps in bell scoring
- Q2: soul-agnostic cold-start fallback in engram_compile (drops genesis-specific hardcoded node IDs)
- Q3: partial-write guard and failure logging in conv_history_persist/load
- Q4: document circuit-breaker limitation requiring C runtime support
- Q5: println warnings on empty activation/search paths
- Q6: load_identity_context warns when all identity fetches return empty
- Q7: recall_status state tracking (ok/empty/unavailable) surfaced to LLM via MEMORY STATUS block
- Q8: document shared-state race conditions in engram_recall_status and safety_system_addendum
- CRITICAL BUG: conv_node_id empty check moved outside is_bell block so silent Conversation node loss is always logged
2026-06-22 12:52:31 -05:00
3 changed files with 329 additions and 934 deletions
+300 -754
View File
File diff suppressed because it is too large Load Diff
-32
View File
@@ -488,38 +488,6 @@ fn session_hist_save(session_id: String, hist: String) -> Void {
state_set(summary_written_key, "1")
}
}
// Issue 5 fix: write a last-session-topic Conversation node so future sessions can
// find the most recent session's topic via engram search. This enables cross-session
// continuity chat.el searches for "last-session-topic" and shows a [CONTINUING FROM
// LAST SESSION] section on the first message of a new session.
let hist_arr_len: Int = if str_eq(hist, "") { 0 } else { json_array_len(hist) }
if hist_arr_len >= 2 {
let last_entry: String = json_array_get(hist, hist_arr_len - 1)
let last_role: String = json_get(last_entry, "role")
let last_content: String = json_get(last_entry, "content")
let topic_snip: String = if str_len(last_content) > 200 { str_slice(last_content, 0, 200) } else { last_content }
let safe_topic: String = str_replace(topic_snip, """, "'")
let ts_now: String = int_to_str(time_now())
let topic_content: String = "last-session-topic | ts:" + ts_now + " | session:" + session_id + " | topic:" + safe_topic
let topic_tags: String = "["last-session-topic","conv:history","Conversation","session:topic"]"
let topic_label: String = "last-session-topic:" + session_id
// Delete old last-session-topic node for this session before writing fresh
let old_topic: String = engram_search_json("last-session-topic:" + session_id, 2)
let ot_len: Int = if str_eq(old_topic, "") { 0 } else { json_array_len(old_topic) }
let oti: Int = 0
while oti < ot_len {
let ot_node: String = json_array_get(old_topic, oti)
let ot_id: String = json_get(ot_node, "id")
if !str_eq(ot_id, "") { engram_forget(ot_id) }
let oti = oti + 1
}
let discard_topic: String = engram_node_full(
topic_content, "Conversation", topic_label,
el_from_float(0.7), el_from_float(0.7), el_from_float(0.9),
"Episodic", topic_tags
)
}
}
// session_update_meta_timestamp update the updated_at field in the session:meta node.
+29 -148
View File
@@ -148,6 +148,14 @@ fn load_identity_context() -> Void {
println("[soul] identity context loaded (" + int_to_str(str_len(ctx)) + " chars, " + int_to_str(parts_count) + " nodes)")
}
// Q6 fix: warn when all three identity node fetches return empty. For genesis this
// indicates a corrupted or missing graph. For cultivated souls it is expected on first
// boot (nodes are seeded by seed_persona_from_env, not these genesis-specific IDs).
// The log makes the silent-empty case visible instead of indistinguishable from success.
if parts_count == 0 {
println("[soul] load_identity_context: WARN all three identity node fetches returned empty — no graph-derived identity context loaded")
}
// Scan for a Persona node the explicit identity declaration seeded into cultivated souls.
// Stored at seeding time with label "soul:persona" and node_type "Persona".
// genesis derives identity from the graph directly; cultivated souls have this node seeded.
@@ -162,106 +170,11 @@ fn load_identity_context() -> Void {
println("[soul] persona node loaded (" + int_to_str(str_len(p_content)) + " chars)")
}
}
// Cross-session affective context: load recent BellEvent nodes (distress) and
// PositiveEvent nodes (joy/success) from the last 7 days. Stored in state as
// "soul_affective_context" for build_system_prompt to consume. Uses embedded
// " | ts:NNNNN" marker for recency filtering (created_at is unreliable).
let aff_now: Int = time_now()
let aff_7d: Int = aff_now - 604800
let bell_raw: String = engram_search_json("bell:soft bell:hard BellEvent affective", 3)
let bell_aff_ok: Bool = !str_eq(bell_raw, "") && !str_eq(bell_raw, "[]")
let aff_ctx: String = ""
let aff_ctx = if bell_aff_ok {
let bn_total: Int = json_array_len(bell_raw)
let result: String = ""
let bi: Int = 0
let result = while bi < bn_total {
let bn: String = json_array_get(bell_raw, bi)
let bn_c: String = json_get(bn, "content")
let bm: String = " | ts:"
let bmp: Int = str_index_of(bn_c, bm)
let bn_ts_raw: String = if bmp >= 0 {
let bs: Int = bmp + str_len(bm)
let br: String = str_slice(bn_c, bs, str_len(bn_c))
let bn_next: Int = str_index_of(br, " | ")
if bn_next < 0 { br } else { str_slice(br, 0, bn_next) }
} else {
let bca: String = json_get(bn, "created_at")
if str_eq(bca, "") { json_get(bn, "updated_at") } else { bca }
}
let bn_ts: Int = if str_eq(bn_ts_raw, "") { 0 } else { str_to_int(bn_ts_raw) }
let snip: String = if str_len(bn_c) > 200 { str_slice(bn_c, 0, 200) } else { bn_c }
let result = if bn_ts >= aff_7d && !str_eq(snip, "") {
if str_eq(result, "") { snip } else { result + "\n" + snip }
} else { result }
let bi = bi + 1
result
}
result
} else { "" }
let pos_raw: String = engram_search_json("PositiveEvent joy:high joy:low affective", 3)
let pos_aff_ok: Bool = !str_eq(pos_raw, "") && !str_eq(pos_raw, "[]")
let aff_ctx = if pos_aff_ok {
let pn_total: Int = json_array_len(pos_raw)
let presult: String = aff_ctx
let pi: Int = 0
let presult = while pi < pn_total {
let pn: String = json_array_get(pos_raw, pi)
let pn_c: String = json_get(pn, "content")
let pm: String = " | ts:"
let pmp: Int = str_index_of(pn_c, pm)
let pn_ts_raw: String = if pmp >= 0 {
let ps: Int = pmp + str_len(pm)
let pr: String = str_slice(pn_c, ps, str_len(pn_c))
let pn_next: Int = str_index_of(pr, " | ")
if pn_next < 0 { pr } else { str_slice(pr, 0, pn_next) }
} else {
let pca: String = json_get(pn, "created_at")
if str_eq(pca, "") { json_get(pn, "updated_at") } else { pca }
}
let pn_ts: Int = if str_eq(pn_ts_raw, "") { 0 } else { str_to_int(pn_ts_raw) }
let psnip: String = if str_len(pn_c) > 200 { str_slice(pn_c, 0, 200) } else { pn_c }
let presult = if pn_ts >= aff_7d && !str_eq(psnip, "") {
if str_eq(presult, "") { psnip } else { presult + "\n" + psnip }
} else { presult }
let pi = pi + 1
presult
}
presult
} else { aff_ctx }
if !str_eq(aff_ctx, "") {
state_set("soul_affective_context", aff_ctx)
println("[soul] cross-session affective context loaded (" + int_to_str(str_len(aff_ctx)) + " chars)")
}
// Issue 4/10 fix: scan BellEvent nodes for recent distress and cache in state.
// chat.el reads "soul_affective_context" at session start to avoid duplicating this
// search on every first message. Timestamp extracted from embedded " | ts:" marker
// first; falls back to created_at when absent (Issue 10 fix). Window: 14 days.
let aff_nodes: String = engram_search_json("BellEvent bell:soft bell:hard distress crisis upset hopeless", 5)
let aff_has: Bool = !str_eq(aff_nodes, "") && !str_eq(aff_nodes, "[]")
if aff_has {
let aff_now: Int = time_now()
let aff_cutoff: Int = aff_now - 1209600
let aff_node: String = json_array_get(aff_nodes, 0)
let aff_content: String = json_get(aff_node, "content")
let ts_marker: String = " | ts:"
let ts_pos: Int = str_index_of(aff_content, ts_marker)
let aff_ts_raw: String = if ts_pos >= 0 {
let ts_start: Int = ts_pos + str_len(ts_marker)
let rest: String = str_slice(aff_content, ts_start, str_len(aff_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(aff_node, "created_at")
if str_eq(ca, "") { json_get(aff_node, "updated_at") } else { ca }
}
let aff_ts: Int = if str_eq(aff_ts_raw, "") { 0 } else { str_to_int(aff_ts_raw) }
if aff_ts > aff_cutoff {
state_set("soul_affective_context", "[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]")
println("[soul] affective context loaded — distress signal within 14d window")
}
// Q6 fix: if neither identity nodes nor persona node were loaded, log explicitly.
let soul_id_ctx: String = state_get("soul_identity_context")
let soul_persona_ctx: String = state_get("soul_persona")
if str_eq(soul_id_ctx, "") && str_eq(soul_persona_ctx, "") {
println("[soul] load_identity_context: WARN no identity context available from graph — soul will have identity_block empty in system prompts")
}
}
@@ -421,55 +334,23 @@ fn layered_cycle(raw_input: String) -> String {
json_get(steward_result, "redirect_to")
}
// L2c: affective context injection augment safety addendum with recent emotional history.
// Ensures cross-session affective awareness is active even when soul_affective_context
// was not injected by build_system_prompt (belt-and-suspenders path).
let lc_aff_cutoff: Int = time_now() - 259200
let lc_bell_nodes: String = engram_search_json("bell:soft bell:hard BellEvent affective", 2)
let lc_has_bell: Bool = !str_eq(lc_bell_nodes, "") && !str_eq(lc_bell_nodes, "[]")
let lc_bell_note: String = if lc_has_bell {
let lb0: String = json_array_get(lc_bell_nodes, 0)
let lb_c: String = json_get(lb0, "content")
let lbm: String = " | ts:"
let lbmp: Int = str_index_of(lb_c, lbm)
let lb_ts_raw: String = if lbmp >= 0 {
let lbs: Int = lbmp + str_len(lbm)
let lbr: String = str_slice(lb_c, lbs, str_len(lb_c))
let lbn: Int = str_index_of(lbr, " | ")
if lbn < 0 { lbr } else { str_slice(lbr, 0, lbn) }
} else {
let lbca: String = json_get(lb0, "created_at")
if str_eq(lbca, "") { json_get(lb0, "updated_at") } else { lbca }
}
let lb_ts: Int = if str_eq(lb_ts_raw, "") { 0 } else { str_to_int(lb_ts_raw) }
if lb_ts > lc_aff_cutoff { "[AFFECTIVE NOTE: User was in distress in a recent session.]" } else { "" }
} else { "" }
let lc_pos_nodes: String = engram_search_json("PositiveEvent joy:high joy:low affective", 2)
let lc_has_pos: Bool = !str_eq(lc_pos_nodes, "") && !str_eq(lc_pos_nodes, "[]")
let lc_pos_note: String = if lc_has_pos && str_eq(lc_bell_note, "") {
let lp0: String = json_array_get(lc_pos_nodes, 0)
let lp_c: String = json_get(lp0, "content")
let lpm: String = " | ts:"
let lpmp: Int = str_index_of(lp_c, lpm)
let lp_ts_raw: String = if lpmp >= 0 {
let lps: Int = lpmp + str_len(lpm)
let lpr: String = str_slice(lp_c, lps, str_len(lp_c))
let lpn: Int = str_index_of(lpr, " | ")
if lpn < 0 { lpr } else { str_slice(lpr, 0, lpn) }
} else {
let lpca: String = json_get(lp0, "created_at")
if str_eq(lpca, "") { json_get(lp0, "updated_at") } else { lpca }
}
let lp_ts: Int = if str_eq(lp_ts_raw, "") { 0 } else { str_to_int(lp_ts_raw) }
if lp_ts > lc_aff_cutoff { "[AFFECTIVE NOTE: User shared positive news in a recent session.]" } else { "" }
} else { "" }
let lc_affective_note: String = if !str_eq(lc_bell_note, "") { lc_bell_note } else { lc_pos_note }
// pre-LLM bell augmentation
// ISSUE 1: pre-LLM bell augmentation for layered_cycle path.
// safety_augment_system appends soft/hard directive to system prompt when bell fires,
// ensuring LLM processes message WITH the safety directive -- not just post-output gate.
// Stored in state as "layered_cycle_safety_system_addendum" for imprint_respond to use.
// TODO: wire directly when imprint_respond gains system_override param (imprint.el change).
// ISSUE 3 TODO: no semantic crisis detection. Keyword-only means signals that evade
// the phrase list pass with zero augmentation. Semantic layer = separate decision.
//
// Q8 race documentation: "layered_cycle_safety_system_addendum" is a shared process-global
// state key. Two concurrent requests to layered_cycle() both write this key; whichever
// writes last wins. The concurrent build_system_prompt() read in chat.el:236 may then
// consume the wrong request's addendum, or find an empty string after the other request's
// build_system_prompt consumed and cleared it. Mitigation: under http_serve_async, the
// layered_cycle path and the /api/chat path are different endpoints (typically); true
// concurrent layered_cycle calls are uncommon. A robust fix requires per-request state
// scoping which needs C runtime support (e.g. a request-id-keyed addendum map).
let augmented_addendum: String = safety_augment_system("", raw_input)
let augmented_addendum = if str_eq(lc_affective_note, "") { augmented_addendum } else {
if str_eq(augmented_addendum, "") { lc_affective_note } else { lc_affective_note + "\n" + augmented_addendum }
}
state_set("layered_cycle_safety_system_addendum", augmented_addendum)
// L3: imprint responds