Compare commits

..

1 Commits

Author SHA1 Message Date
will.anderson 3f53b6b1b6 feat(recall): session-start-recall improvements
Neuron Soul CI / build (pull_request) Has been cancelled
10 targeted fixes for session-start memory recall quality:

Issue 1: typed engram queries (Persona, WorkItem) replace generic keyword bags
Issue 2: bullet truncation raised from 120 to 350 chars
Issue 3: bullet caps raised to 8/6 with while-loop (no hardcoded unrolling)
Issue 4: read pre-computed soul_affective_context state key instead of duplicating boot-time search
Issue 5: last-session-topic node written per session; continuity section added to session_preload
Issue 6: greeting detection injects SESSION START orientation directive when continuity found
Issue 7: pinned identity node fallback when all engram searches return empty
Issue 8: session_preload always fires on first message (greeting detection controls directive only)
Issue 9: agentic path gets matching session_preload block (was missing entirely)
Issue 10: BellEvent recency reads created_at / embedded ts marker, not the never-written "ts" field
2026-06-22 13:06:55 -05:00
10 changed files with 654 additions and 225 deletions
-2
View File
@@ -678,8 +678,6 @@ fn threat_trajectory_check(tool_name: String, tool_input: String) -> Int {
return combined
}
// TODO(reliability #10): agentic_conv_history is process-global; awareness loop
// and HTTP workers race on this key. Impact: noisy threat score only, not content.
fn threat_history_append(text: String) -> Void {
let current: String = state_get("agentic_conv_history")
let safe_text: String = str_to_lower(text)
+456 -156
View File
@@ -265,37 +265,172 @@ fn engram_nodes_merge(a: String, b: String) -> String {
return engram_dedup_nodes("[" + ai + "," + bi + "]")
}
// id_in_seen check if node_id appears in the comma-delimited seen accumulator.
// Pads both sides with commas to avoid false substring matches.
fn id_in_seen(node_id: String, seen: String) -> Bool {
if str_eq(node_id, "") { return false }
if str_eq(seen, "") { return false }
return str_contains("," + seen + ",", "," + node_id + ",")
// is_followup_phrase recognize explicit follow-up references that are too short
// to carry meaningful topic signal on their own. These should activate on the
// prior thread context rather than the bare message. Fixes Issues 2/8.
fn is_followup_phrase(msg: String) -> Bool {
if str_contains(msg, "tell me more") { return true }
if str_contains(msg, "elaborate") { return true }
if str_contains(msg, "go on") { return true }
if str_contains(msg, "what about that") { return true }
if str_contains(msg, "what else") { return true }
if str_contains(msg, "keep going") { return true }
if str_contains(msg, "more detail") { return true }
if str_contains(msg, "last part") { return true }
if str_contains(msg, "say more") { return true }
if str_eq(msg, "ok") { return true }
if str_eq(msg, "yes") { return true }
if str_eq(msg, "yeah") { return true }
return false
}
// add_to_seen append node_id to the comma-delimited seen accumulator.
fn add_to_seen(seen: String, node_id: String) -> String {
if str_eq(node_id, "") { return seen }
if str_eq(seen, "") { return node_id }
return seen + "," + node_id
// engram_is_continuation semantic continuation detection for recall activation.
// Fixes Issue 2: the old 50-char threshold was brittle short messages that
// introduce new topics (e.g. "sleep" or "AWS") were treated as continuations.
// Strategy: combine length heuristic with follow-up phrase detection and
// mid-sentence capitalization check (new sentence, probably new topic).
fn engram_is_continuation(msg: String, hist_len: Int) -> Bool {
if hist_len == 0 { return false }
let mlen: Int = str_len(msg)
if mlen > 80 { return false }
if is_followup_phrase(msg) { return true }
// Single-word or very short messages without capitalized new topic
if mlen < 20 { return true }
// Treat as new topic if message starts with a capital (new sentence) and is > 30 chars
let first_char: String = str_slice(msg, 0, 1)
let starts_capital: Bool = str_eq(first_char, str_replace(first_char, "abcdefghijklmnopqrstuvwxyz", ""))
if starts_capital && mlen > 30 { return false }
return true
}
// engram_extract_ids extract all non-empty "id" fields from a JSON node array
// into a comma-delimited string for use with id_in_seen / add_to_seen.
fn engram_extract_ids(nodes_json: String) -> String {
if str_eq(nodes_json, "") { return "" }
if str_eq(nodes_json, "[]") { return "" }
let total: Int = json_array_len(nodes_json)
if total == 0 { return "" }
let ids: String = ""
let i: Int = 0
while i < total {
let node: String = json_array_get(nodes_json, i)
let nid: String = json_get(node, "id")
let ids = if str_eq(nid, "") { ids } else { add_to_seen(ids, nid) }
let i = i + 1
// topic_snip_from_entry extract the most salient snippet from a history entry.
// Fixes Issue 9: the old code sliced from position 0, capturing preamble instead
// of the concepts discussed near the end. This takes the TAIL of a long reply
// and trims to the last sentence boundary for cleaner semantic anchoring.
fn topic_snip_from_entry(content: String) -> String {
let clen: Int = str_len(content)
if clen <= 200 { return content }
let tail: String = str_slice(content, clen - 200, clen)
let last_boundary: Int = -1
let si: Int = 0
let tail_len: Int = str_len(tail)
while si < tail_len - 1 {
let ch2: String = str_slice(tail, si, si + 2)
let is_boundary: Bool = str_eq(ch2, ". ") || str_eq(ch2, ".\n")
let last_boundary = if is_boundary { si } else { last_boundary }
let si = si + 1
}
return ids
let clean_tail: String = if last_boundary >= 0 {
str_slice(tail, last_boundary + 2, tail_len)
} else { tail }
if str_len(clean_tail) > 150 { return str_slice(clean_tail, 0, 150) }
return clean_tail
}
// multi_turn_topic build a combined topic string from recent user turns.
// Fixes Issue 10: a single prior turn in the seed loses earlier high-salience
// nodes from multi-turn discussions. This pulls up to 3 prior user turns so
// thread continuity survives longer conversations.
fn multi_turn_topic(hist: String, hist_len: Int) -> String {
if hist_len == 0 { return "" }
let topic: String = ""
let collected: Int = 0
let idx: Int = hist_len - 1
while idx >= 0 && collected < 3 {
let entry: String = json_array_get(hist, idx)
let role: String = json_get(entry, "role")
let content: String = json_get(entry, "content")
let is_user: Bool = str_eq(role, "user")
let snip: String = if str_len(content) > 100 { str_slice(content, 0, 100) } else { content }
let topic = if is_user && !str_eq(snip, "") {
if str_eq(topic, "") { snip } else { snip + " " + topic }
} else { topic }
let collected = if is_user { collected + 1 } else { collected }
let idx = idx - 1
}
if str_len(topic) > 300 { return str_slice(topic, 0, 300) }
return topic
}
// distill_transcript extract salient content from a long dharma-room transcript.
// Fixes Issue 6: passing the entire transcript produces a diffuse embedding query
// where topic signal drowns in context noise. Strategy: last 150 chars (recency)
// combined with any question found in the last 500 chars (intent anchoring).
fn distill_transcript(transcript: String) -> String {
if str_len(transcript) <= 250 { return transcript }
let tlen: Int = str_len(transcript)
let tail_start: Int = if tlen > 500 { tlen - 500 } else { 0 }
let tail: String = str_slice(transcript, tail_start, tlen)
let tail_len: Int = str_len(tail)
let q_pos: Int = -1
let qi: Int = 0
while qi < tail_len {
let qch: String = str_slice(tail, qi, qi + 1)
let q_pos = if str_eq(qch, "?") { qi } else { q_pos }
let qi = qi + 1
}
let q_context: String = if q_pos > 0 {
let q_start: Int = if q_pos > 100 { q_pos - 100 } else { 0 }
str_slice(tail, q_start, q_pos + 1)
} else { "" }
let recency_seed: String = if tail_len > 150 {
str_slice(tail, tail_len - 150, tail_len)
} else { tail }
let combined: String = if str_eq(q_context, "") {
recency_seed
} else {
if str_contains(recency_seed, q_context) { recency_seed }
else { q_context + " " + recency_seed }
}
if str_len(combined) > 250 {
return str_slice(combined, str_len(combined) - 250, str_len(combined))
}
return combined
}
// build_activation_seed construct an enriched activation seed from the current
// message and conversation history. Central fix for Issues 1-3, 8-10.
// For genuine continuations: anchors to the PRIOR USER TURN (Issues 3/8) and
// adds a tail-biased snip from the last assistant reply (Issue 9).
// For new topics: blends up to 3 prior user turns for thread continuity (Issue 10).
fn build_activation_seed(message: String, hist: String, hist_len: Int) -> String {
if hist_len == 0 { return message }
let is_cont: Bool = engram_is_continuation(message, hist_len)
if is_cont {
// Scan back to find the most recent USER turn as topic anchor (Issues 3/8 fix)
let prior_user_content: String = ""
let scan_idx: Int = hist_len - 1
let found_prior: Bool = false
while scan_idx >= 0 && !found_prior {
let se: String = json_array_get(hist, scan_idx)
let se_role: String = json_get(se, "role")
let se_content: String = json_get(se, "content")
let prior_user_content = if str_eq(se_role, "user") && !found_prior { se_content } else { prior_user_content }
let found_prior = if str_eq(se_role, "user") { true } else { found_prior }
let scan_idx = scan_idx - 1
}
// Tail-biased snip from last assistant reply (Issue 9 fix)
let last_asst: String = json_array_get(hist, hist_len - 1)
let last_asst_role: String = json_get(last_asst, "role")
let last_asst_content: String = if str_eq(last_asst_role, "assistant") { json_get(last_asst, "content") } else { "" }
let asst_snip: String = if str_eq(last_asst_content, "") { "" } else { topic_snip_from_entry(last_asst_content) }
let user_snip: String = if str_len(prior_user_content) > 150 { str_slice(prior_user_content, 0, 150) } else { prior_user_content }
// Seed: prior user topic (primary anchor) + assistant tail (context) + current message
let s: String = if !str_eq(user_snip, "") {
if !str_eq(asst_snip, "") { user_snip + " " + asst_snip + " " + message }
else { user_snip + " " + message }
} else {
if !str_eq(asst_snip, "") { asst_snip + " " + message } else { message }
}
if str_len(s) > 400 { return str_slice(s, 0, 400) }
return s
}
// Not a continuation: blend with multi-turn user topics for richer seed (Issue 10)
let mt: String = multi_turn_topic(hist, hist_len)
if str_eq(mt, "") { return message }
let b: String = message + " " + mt
if str_len(b) > 400 { return str_slice(b, 0, 400) }
return b
}
fn engram_compile(intent: String) -> String {
@@ -419,18 +554,6 @@ fn engram_compile(intent: String) -> String {
let sep_ma: String = if !str_eq(main_part, "") && !str_eq(affective_part, "") { "\n" } else { "" }
let ctx: String = main_part + sep_ma + affective_part
// Dedup fix: publish seen node IDs so downstream callers (session_preload) can skip
// nodes already present in the compiled context. Must be computed after scan_part and
// affective_part are resolved so all three segments are represented in the seen set.
// EL has no tuple returns so we use state as an out-param.
// scan_part is a JSON array extract with engram_extract_ids.
// affective_part is a bare JSON object (bn0), not an array extract its id directly.
let ids_from_merged: String = engram_extract_ids(merged_nodes)
let ids_from_scan: String = engram_extract_ids(scan_part)
let ids_from_affective: String = json_get(affective_part, "id")
let compile_seen_ids: String = add_to_seen(add_to_seen(ids_from_merged, ids_from_scan), ids_from_affective)
state_set("engram_compile_seen_ids", compile_seen_ids)
if str_eq(ctx, "") { return "" }
// Issue 7 fix: safe JSON truncation find last closing brace before budget cap.
@@ -481,6 +604,16 @@ fn build_system_prompt(ctx: String) -> String {
"\n\n[IDENTITY GRAPH — who you are, loaded from your engram]\n" + id_ctx
}
// soul_affective_context is loaded at boot by load_identity_context() with BellEvent/
// PositiveEvent nodes from last 7 days. Surfaced here so the LLM sees historical
// emotional patterns from prior sessions at every turn.
let boot_aff_ctx: String = state_get("soul_affective_context")
let affective_boot_block: String = if str_eq(boot_aff_ctx, "") {
""
} else {
"\n\n[CROSS-SESSION EMOTIONAL CONTEXT — from prior sessions]\n" + boot_aff_ctx
}
let engram_block: String = if str_eq(ctx, "") {
""
} else {
@@ -495,7 +628,7 @@ fn build_system_prompt(ctx: String) -> String {
safety_addendum
}
return identity + date_line + voice_rules + security_rules + capability_rules + identity_block + engram_block + safety_block
return identity + 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 {
@@ -652,134 +785,225 @@ fn handle_chat(body: String) -> String {
message
}
// Cross-session affective context: on session start (no history yet), check engram
// for recent distress signals within 72h and prepend a care directive if found.
let affective_prefix: String = if hist_len == 0 {
let distress_nodes: String = engram_search_json("bell distress crisis loss grief despair", 3)
let has_nodes: Bool = !str_eq(distress_nodes, "") && !str_eq(distress_nodes, "[]")
let now_ts: Int = time_now()
let cutoff: Int = now_ts - 259200
let found_recent: Bool = if has_nodes {
let dn0: String = json_array_get(distress_nodes, 0)
let ts0_raw: String = json_get(dn0, "created_at")
let ts0_str: String = if str_eq(ts0_raw, "") { json_get(dn0, "updated_at") } else { ts0_raw }
let ts0: Int = if str_eq(ts0_str, "") { 0 } else { str_to_int(ts0_str) }
ts0 > cutoff
// Cross-session affective context: fix Issues 4 and 10.
// Issue 4: soul_affective_context was computed at boot (soul.el:load_identity_context)
// but never consumed here this block duplicated the search unnecessarily.
// Now we read the pre-computed state key and only fall back to a live search when empty.
// Issue 10: the live fallback reads created_at (not the never-written "ts" field),
// so BellEvent recency filtering now works correctly.
let affective_prefix: String = {
// Runs every turn. Uses correct BellEvent/PositiveEvent query tags.
// Timestamps extracted from embedded ts marker, not created_at.
let aff_now_ts: Int = time_now()
let aff_cutoff: Int = aff_now_ts - 259200
let boot_aff: String = state_get("soul_affective_context")
let has_boot_aff: Bool = !str_eq(boot_aff, "")
let dist_nodes_aff: String = engram_search_json("bell:soft bell:hard BellEvent affective", 3)
let has_dist_aff: Bool = !str_eq(dist_nodes_aff, "") && !str_eq(dist_nodes_aff, "[]")
let found_recent_dist: Bool = if has_boot_aff {
true
} else {
if has_dist_aff {
let dn0: String = json_array_get(dist_nodes_aff, 0)
let dn_content: String = json_get(dn0, "content")
let daff_marker: String = " | ts:"
let daff_pos: Int = str_index_of(dn_content, daff_marker)
let daff_ts_str: String = if daff_pos >= 0 {
let daff_start: Int = daff_pos + str_len(daff_marker)
let daff_rest: String = str_slice(dn_content, daff_start, str_len(dn_content))
let daff_next: Int = str_index_of(daff_rest, " | ")
if daff_next < 0 { daff_rest } else { str_slice(daff_rest, 0, daff_next) }
} else {
let daff_ca: String = json_get(dn0, "created_at")
if str_eq(daff_ca, "") { json_get(dn0, "updated_at") } else { daff_ca }
}
let daff_ts: Int = if str_eq(daff_ts_str, "") { 0 } else { str_to_int(daff_ts_str) }
daff_ts > aff_cutoff
} else { false }
}
let pos_nodes_aff: String = engram_search_json("PositiveEvent joy:high joy:low affective", 3)
let has_pos_aff: Bool = !str_eq(pos_nodes_aff, "") && !str_eq(pos_nodes_aff, "[]")
let found_recent_pos: Bool = if has_pos_aff && !found_recent_dist {
let pn0: String = json_array_get(pos_nodes_aff, 0)
let pn_content: String = json_get(pn0, "content")
let paff_marker: String = " | ts:"
let paff_pos: Int = str_index_of(pn_content, paff_marker)
let paff_ts_str: String = if paff_pos >= 0 {
let paff_start: Int = paff_pos + str_len(paff_marker)
let paff_rest: String = str_slice(pn_content, paff_start, str_len(pn_content))
let paff_next: Int = str_index_of(paff_rest, " | ")
if paff_next < 0 { paff_rest } else { str_slice(paff_rest, 0, paff_next) }
} else {
let paff_ca: String = json_get(pn0, "created_at")
if str_eq(paff_ca, "") { json_get(pn0, "updated_at") } else { paff_ca }
}
let paff_ts: Int = if str_eq(paff_ts_str, "") { 0 } else { str_to_int(paff_ts_str) }
paff_ts > aff_cutoff
} else { false }
if found_recent {
"[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]\n\n"
} else { "" }
} else { "" }
if found_recent_dist {
"[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]
"
} else {
if found_recent_pos {
"[RECENT CONTEXT: User recently shared exciting or joyful news. Acknowledge and celebrate with them when relevant.]
"
} else { "" }
}
}
let ctx: String = engram_compile(activation_seed)
// Read IDs published by engram_compile so session_preload can skip duplicate nodes.
// EL has no multiple return values; engram_compile writes its seen set to state.
let seen_ids: String = state_get("engram_compile_seen_ids")
let system: String = affective_prefix + build_system_prompt(ctx)
// Issue 9 fix: add project-specific and session-summary searches to session preload.
// Old hardcoded "user profile" and "in_progress active project" miss project-specific
// nodes stored under names like "Prism" unless those exact words appear in content.
// Dedup fix: skip any node whose ID already appeared in engram_compile's output.
// session_preload_bullets render up to max_bullets "- <content>" lines from a node array.
// Fix Issue 2: snip_len parameter replaces hardcoded 120 (caller uses 350).
// Fix Issue 3: max_bullets parameter replaces hardcoded 3/2 caps; loop-driven not unrolled.
fn session_preload_bullets(nodes: String, max_bullets: Int, snip_len: Int) -> String {
if str_eq(nodes, "") { return "" }
if str_eq(nodes, "[]") { return "" }
let total: Int = json_array_len(nodes)
let limit: Int = if max_bullets < total { max_bullets } else { total }
let bullets: String = ""
let i: Int = 0
while i < limit {
let node: String = json_array_get(nodes, i)
let content: String = json_get(node, "content")
let snip: String = if str_len(content) > snip_len { str_slice(content, 0, snip_len) } else { content }
let bullets = if str_eq(snip, "") {
bullets
} else {
if str_eq(bullets, "") { "- " + snip } else { bullets + "
- " + snip }
}
let i = i + 1
}
return bullets
}
// First message of the session: proactively load user profile, active work, and
// cross-session continuity context so the soul greets the user with real grounding.
// Fix Issue 1: type-targeted queries (Persona, WorkItem) first; broad fallback only
// when typed query returns empty. Avoids generic keyword bags that miss typed nodes.
// Fix Issue 2: truncation raised from 120 to 350 chars per bullet.
// Fix Issue 3: caps raised to 8 profile / 6 work, loop-driven (no hardcoded unrolling).
// Fix Issue 5: add continuity search (last session topic via session:emotional-summary).
// Fix Issue 6: detect low-info greeting and inject a first-message orientation directive.
// Fix Issue 7: when all searches return empty, fall back to pinned identity nodes and log.
// Fix Issue 8: preload always fires on first message; greeting detection controls the
// orientation directive only (substantive openers still get grounding).
let session_preload: String = if hist_len == 0 {
let profile_nodes: String = engram_search_json("user profile identity preferences", 5)
let work_nodes: String = engram_search_json("in_progress active project work", 5)
let project_nodes: String = engram_search_json("project status current ongoing active", 5)
let summary_nodes: String = engram_search_json("SessionSummary session:summary previous-session recent", 3)
// Issue 6/8: detect greeting vs. substantive opener.
let is_greeting: Bool = str_len(message) <= 20
|| str_starts_with(message, "hi")
|| str_starts_with(message, "hello")
|| str_starts_with(message, "hey")
// Issue 1: typed profile query Persona node_type + soul:persona label first.
let profile_nodes_typed: String = engram_search_json("Persona soul:persona identity principal", 8)
let profile_ok_typed: Bool = !str_eq(profile_nodes_typed, "") && !str_eq(profile_nodes_typed, "[]")
let profile_nodes: String = if profile_ok_typed {
profile_nodes_typed
} else {
engram_search_json("user profile preferences name", 8)
}
let profile_ok: Bool = !str_eq(profile_nodes, "") && !str_eq(profile_nodes, "[]")
// Issue 1: typed work query WorkItem with in_progress label first.
let work_nodes_typed: String = engram_search_json("WorkItem status:in_progress active work", 6)
let work_ok_typed: Bool = !str_eq(work_nodes_typed, "") && !str_eq(work_nodes_typed, "[]")
let work_nodes: String = if work_ok_typed {
work_nodes_typed
} else {
engram_search_json("active project task current in_progress", 6)
}
let work_ok: Bool = !str_eq(work_nodes, "") && !str_eq(work_nodes, "[]")
let project_ok: Bool = !str_eq(project_nodes, "") && !str_eq(project_nodes, "[]")
let summary_ok: Bool = !str_eq(summary_nodes, "") && !str_eq(summary_nodes, "[]")
let profile_bullets: String = if profile_ok {
let pn: Int = json_array_len(profile_nodes)
let bullets: String = ""
let bullets = if pn > 0 {
let n0: String = json_array_get(profile_nodes, 0)
let n0_id: String = json_get(n0, "id")
let c0: String = json_get(n0, "content")
let s0: String = if str_len(c0) > 120 { str_slice(c0, 0, 120) } else { c0 }
if str_eq(s0, "") || id_in_seen(n0_id, seen_ids) { bullets } else { "- " + s0 }
} else { bullets }
let bullets = if pn > 1 {
let n1: String = json_array_get(profile_nodes, 1)
let n1_id: String = json_get(n1, "id")
let c1: String = json_get(n1, "content")
let s1: String = if str_len(c1) > 120 { str_slice(c1, 0, 120) } else { c1 }
if str_eq(s1, "") || id_in_seen(n1_id, seen_ids) { bullets } else { bullets + "\n- " + s1 }
} else { bullets }
let bullets = if pn > 2 {
let n2: String = json_array_get(profile_nodes, 2)
let n2_id: String = json_get(n2, "id")
let c2: String = json_get(n2, "content")
let s2: String = if str_len(c2) > 120 { str_slice(c2, 0, 120) } else { c2 }
if str_eq(s2, "") || id_in_seen(n2_id, seen_ids) { bullets } else { bullets + "\n- " + s2 }
} else { bullets }
bullets
// Issue 5: cross-session continuity last session emotional summary or last-session-topic.
let continuity_nodes: String = engram_search_json("last-session-topic session:emotional-summary conv:history last session", 3)
let continuity_ok: Bool = !str_eq(continuity_nodes, "") && !str_eq(continuity_nodes, "[]")
let continuity_snip: String = if continuity_ok {
let cn0: String = json_array_get(continuity_nodes, 0)
let cc: String = json_get(cn0, "content")
if str_len(cc) > 350 { str_slice(cc, 0, 350) } else { cc }
} else { "" }
let work_bullets: String = if work_ok {
let wn: Int = json_array_len(work_nodes)
let wb: String = ""
let wb = if wn > 0 {
let w0: String = json_array_get(work_nodes, 0)
let w0_id: String = json_get(w0, "id")
let wc0: String = json_get(w0, "content")
let ws0: String = if str_len(wc0) > 120 { str_slice(wc0, 0, 120) } else { wc0 }
if str_eq(ws0, "") || id_in_seen(w0_id, seen_ids) { wb } else { "- " + ws0 }
} else { wb }
let wb = if wn > 1 {
let w1: String = json_array_get(work_nodes, 1)
let w1_id: String = json_get(w1, "id")
let wc1: String = json_get(w1, "content")
let ws1: String = if str_len(wc1) > 120 { str_slice(wc1, 0, 120) } else { wc1 }
if str_eq(ws1, "") || id_in_seen(w1_id, seen_ids) { wb } else { wb + "\n- " + ws1 }
} else { wb }
wb
// Issue 7: fallback to pinned identity nodes when all searches return empty.
let all_empty: Bool = !profile_ok && !work_ok && !continuity_ok
let fallback_identity: String = if all_empty {
let family_node: String = engram_get_node_json("knw-35940684-abc4-42f0-b942-818f66b1f69a")
let origin_node: String = engram_get_node_json("knw-729fc901-8335-44c4-9f3a-b150b4aa0915")
let fam_ok: Bool = !str_eq(family_node, "") && !str_eq(family_node, "null")
let orig_ok: Bool = !str_eq(origin_node, "") && !str_eq(origin_node, "null")
let fam_content: String = if fam_ok { json_get(family_node, "content") } else { "" }
let orig_content: String = if orig_ok { json_get(origin_node, "content") } else { "" }
let fam_snip: String = if str_len(fam_content) > 350 { str_slice(fam_content, 0, 350) } else { fam_content }
let orig_snip: String = if str_len(orig_content) > 350 { str_slice(orig_content, 0, 350) } else { orig_content }
let fb: String = if fam_ok {
if orig_ok { "- " + fam_snip + "
- " + orig_snip } else { "- " + fam_snip }
} else {
if orig_ok { "- " + orig_snip } else { "" }
}
if str_eq(fb, "") {
println("[chat] session_preload: all engram searches empty and pinned nodes missing — grounding context unavailable")
} else {
println("[chat] session_preload: all typed/broad searches empty — using pinned identity nodes as fallback")
}
fb
} else { "" }
let project_bullets: String = if project_ok {
let prn: Int = json_array_len(project_nodes)
let pb: String = ""
let pb = if prn > 0 {
let pr0: String = json_array_get(project_nodes, 0)
let pr0_id: String = json_get(pr0, "id")
let prc0: String = json_get(pr0, "content")
let ps0: String = if str_len(prc0) > 120 { str_slice(prc0, 0, 120) } else { prc0 }
if str_eq(ps0, "") || id_in_seen(pr0_id, seen_ids) { pb } else { "- " + ps0 }
} else { pb }
let pb = if prn > 1 {
let pr1: String = json_array_get(project_nodes, 1)
let pr1_id: String = json_get(pr1, "id")
let prc1: String = json_get(pr1, "content")
let ps1: String = if str_len(prc1) > 120 { str_slice(prc1, 0, 120) } else { prc1 }
if str_eq(ps1, "") || id_in_seen(pr1_id, seen_ids) { pb } else { pb + "\n- " + ps1 }
} else { pb }
pb
// Issue 2 + 3: render bullets with raised caps and 350-char snip.
let profile_bullets: String = session_preload_bullets(profile_nodes, 8, 350)
let work_bullets: String = session_preload_bullets(work_nodes, 6, 350)
let has_profile: Bool = !str_eq(profile_bullets, "")
let has_work: Bool = !str_eq(work_bullets, "")
let has_continuity: Bool = !str_eq(continuity_snip, "")
let has_fallback: Bool = !str_eq(fallback_identity, "")
// Issue 6: orient the soul on greeting openers to ask a check-in question first.
let continuity_directive: String = if is_greeting && has_continuity {
"[SESSION START — FIRST TURN] New session. The user sent a short greeting. Orient yourself: acknowledge you are present and ask what they would like to work on or continue. Do not recite the context below — use it only for orientation.
"
} else { "" }
let summary_bullet: String = if summary_ok {
let sn0: String = json_array_get(summary_nodes, 0)
let sn0_id: String = json_get(sn0, "id")
let sc0: String = json_get(sn0, "content")
let ss0: String = if str_len(sc0) > 200 { str_slice(sc0, 0, 200) } else { sc0 }
if str_eq(ss0, "") || id_in_seen(sn0_id, seen_ids) { "" } else { "- " + ss0 }
} else { "" }
let preload: String = if has_profile || has_work || has_continuity || has_fallback {
let directive_part: String = continuity_directive
let profile_part: String = if has_profile {
"[USER CONTEXT — from memory]
" + profile_bullets + "
let hp: Bool = !str_eq(profile_bullets, "")
let hw: Bool = !str_eq(work_bullets, "")
let hpr: Bool = !str_eq(project_bullets, "")
let hs: Bool = !str_eq(summary_bullet, "")
let preload: String = if hp || hw || hpr || hs {
let sec_p: String = if hp { "[USER CONTEXT — from memory]\n" + profile_bullets } else { "" }
let sec_w: String = if hw { "[ACTIVE WORK — from memory]\n" + work_bullets } else { "" }
let sec_pr: String = if hpr { "[PROJECTS — from memory]\n" + project_bullets } else { "" }
let sec_s: String = if hs { "[PREVIOUS SESSION — from memory]\n" + summary_bullet } else { "" }
let sep1: String = if hp && (hw || hpr || hs) { "\n\n" } else { "" }
let sep2: String = if hw && (hpr || hs) { "\n\n" } else { "" }
let sep3: String = if hpr && hs { "\n\n" } else { "" }
"\n\n" + sec_p + sep1 + sec_w + sep2 + sec_pr + sep3 + sec_s
"
} else { "" }
let work_part: String = if has_work {
"[ACTIVE WORK — from memory]
" + work_bullets + "
"
} else { "" }
let continuity_part: String = if has_continuity {
"[CONTINUING FROM LAST SESSION]
" + continuity_snip + "
"
} else { "" }
let fallback_part: String = if has_fallback && !has_profile && !has_work {
"[IDENTITY CONTEXT — from memory]
" + fallback_identity + "
"
} else { "" }
let body: String = directive_part + profile_part + work_part + continuity_part + fallback_part
let body_len: Int = str_len(body)
let trimmed_body: String = if body_len > 2 && str_eq(str_slice(body, body_len - 2, body_len), "
") {
str_slice(body, 0, body_len - 2)
} else { body }
"
" + trimmed_body
} else { "" }
preload
} else { "" }
@@ -1307,7 +1531,53 @@ fn handle_chat_agentic(body: String) -> String {
let ctx: String = engram_compile(ag_seed)
let identity: String = state_get("soul_identity")
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct.\n\n" + ctx
// Issue 9: agentic first-message session preload mirrors handle_chat grounding.
let ag_session_preload: String = if agentic_hist_len == 0 {
let ag_profile_nodes: String = engram_search_json("Persona soul:persona identity principal", 8)
let ag_profile_ok: Bool = !str_eq(ag_profile_nodes, "") && !str_eq(ag_profile_nodes, "[]")
let ag_profile_nodes2: String = if ag_profile_ok { ag_profile_nodes } else {
engram_search_json("user profile preferences name", 8)
}
let ag_work_nodes: String = engram_search_json("WorkItem status:in_progress active work", 6)
let ag_work_ok: Bool = !str_eq(ag_work_nodes, "") && !str_eq(ag_work_nodes, "[]")
let ag_work_nodes2: String = if ag_work_ok { ag_work_nodes } else {
engram_search_json("active project task current in_progress", 6)
}
let ag_continuity_nodes: String = engram_search_json("last-session-topic session:emotional-summary conv:history last session", 3)
let ag_continuity_ok: Bool = !str_eq(ag_continuity_nodes, "") && !str_eq(ag_continuity_nodes, "[]")
let ag_continuity_snip: String = if ag_continuity_ok {
let acn0: String = json_array_get(ag_continuity_nodes, 0)
let acc: String = json_get(acn0, "content")
if str_len(acc) > 350 { str_slice(acc, 0, 350) } else { acc }
} else { "" }
let ag_profile_bullets: String = session_preload_bullets(ag_profile_nodes2, 8, 350)
let ag_work_bullets: String = session_preload_bullets(ag_work_nodes2, 6, 350)
let ag_has_profile: Bool = !str_eq(ag_profile_bullets, "")
let ag_has_work: Bool = !str_eq(ag_work_bullets, "")
let ag_has_cont: Bool = !str_eq(ag_continuity_snip, "")
if ag_has_profile || ag_has_work || ag_has_cont {
let p: String = if ag_has_profile { "[USER CONTEXT — from memory]
" + ag_profile_bullets + "
" } else { "" }
let w: String = if ag_has_work { "[ACTIVE WORK — from memory]
" + ag_work_bullets + "
" } else { "" }
let c: String = if ag_has_cont { "[CONTINUING FROM LAST SESSION]
" + ag_continuity_snip + "
" } else { "" }
"
" + p + w + c
} else { "" }
} else { "" }
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct.
" + ctx + ag_session_preload
let api_key: String = agentic_api_key()
let tools_json: String = agentic_tools_all()
@@ -1695,7 +1965,8 @@ fn handle_dharma_room_turn(body: String) -> String {
}
// The soul's own memories, activated by what it's reading not injected.
let engram_ctx: String = engram_compile(transcript)
// Issue 6 fix: distill_transcript() extracts salient tail+question from full transcript
let engram_ctx: String = engram_compile(distill_transcript(transcript))
let system_prompt: String = if str_eq(engram_ctx, "") {
identity
} else {
@@ -1747,7 +2018,8 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
return "{\"error\":\"transcript is required\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
}
let ctx: String = engram_compile(transcript)
// Issue 6 fix: distill_transcript() extracts salient tail+question from full transcript
let ctx: String = engram_compile(distill_transcript(transcript))
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct and stay in character.\n\n" + ctx
let api_key: String = agentic_api_key()
@@ -1809,13 +2081,19 @@ fn auto_persist(req: String, resp: String) -> Void {
// 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")
// Positive emotion detection mirrors distress detection.
let positive_level: String = safety_detect_positive_level(message)
let is_positive: Bool = !str_eq(positive_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.
// Tag the Conversation node with affective metadata when emotion is detected.
let tags: String = if is_bell {
"[\"Conversation\",\"chat\",\"timestamped\",\"bell:" + bell_level + "\",\"affective\"]"
} else {
"[\"Conversation\",\"chat\",\"timestamped\"]"
if is_positive {
"[\"Conversation\",\"chat\",\"timestamped\",\"joy:" + positive_level + "\",\"affective\"]"
} else {
"[\"Conversation\",\"chat\",\"timestamped\"]"
}
}
let content: String = "{\"q\":\"" + safe_msg + "\""
@@ -1901,6 +2179,28 @@ fn auto_persist(req: String, resp: String) -> Void {
}
state_set(signal_key, safe_summary)
}
// Dedicated PositiveEvent node for joy/pride/success moments.
if is_positive {
let pos_summary: String = if str_len(message) > 120 { str_slice(message, 0, 120) } else { message }
let safe_pos_sum: String = str_replace(pos_summary, "\"", "'")
let pos_content: String = "POSITIVE:" + positive_level
+ " | ts:" + ts_str
+ " | summary:" + safe_pos_sum
let pos_sal_a: String = if str_eq(positive_level, "high") { el_from_float(0.88) } else { el_from_float(0.75) }
let pos_sal_b: String = if str_eq(positive_level, "high") { el_from_float(0.88) } else { el_from_float(0.75) }
let pos_sal_c: String = if str_eq(positive_level, "high") { el_from_float(0.95) } else { el_from_float(0.85) }
let pos_tags: String = "[\"joy\",\"positive\",\"joy:" + positive_level + "\",\"affective\",\"PositiveEvent\"]"
let pos_ts_label: String = int_to_str(time_now())
let pos_label: String = "joy:" + positive_level + ":" + pos_ts_label
let pos_node_id: String = engram_node_full(
pos_content, "PositiveEvent", pos_label,
pos_sal_a, pos_sal_b, pos_sal_c, "Episodic", pos_tags
)
if str_eq(pos_node_id, "") {
println("[chat] auto_persist: PositiveEvent write failed (ts=" + ts_str + ")")
}
}
}
// strengthen_chat_nodes strengthen the engram nodes that were activated during a chat.
+4 -8
View File
@@ -24,23 +24,19 @@ ENGRAM_DATA_DIR="$ENGRAM_DATA_DIR" \
ENGRAM_PID=$!
# Wait for engram to become healthy (up to 60s; GKE Autopilot cold starts can be slow)
# Wait for engram to become healthy (up to 30s)
echo "[entrypoint] waiting for engram..."
TRIES=0
until curl -sf "$ENGRAM_HEALTH_URL" > /dev/null 2>&1; do
TRIES=$((TRIES + 1))
if [ "$TRIES" -ge 60 ]; then
echo "[entrypoint] ERROR: engram did not become healthy after 60s" >&2
if [ "$TRIES" -ge 30 ]; then
echo "[entrypoint] ERROR: engram did not become healthy after 30s" >&2
kill "$ENGRAM_PID" 2>/dev/null || true
exit 1
fi
sleep 1
done
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}"
echo "[entrypoint] engram ready"
# Start soul — it takes over as PID 1's foreground process.
# SOUL_ENGRAM_PATH must NOT be set; ENGRAM_URL triggers HTTP mode.
-4
View File
@@ -5,10 +5,6 @@
// imprint_current returns the active imprint ID from state.
// Falls back to "base" (bare Neuron, no suit) when nothing is loaded.
//
// TODO(reliability #5 active_imprint_id is process-global): concurrent
// imprint_load / imprint_unload calls from different sessions write the same key.
// Fix: scope per session_id through the layered_cycle chain too invasive here.
fn imprint_current() -> String {
let id: String = state_get("active_imprint_id")
return if str_eq(id, "") { "base" } else { id }
+2 -8
View File
@@ -46,10 +46,7 @@ fn mem_consolidate() -> String {
}
fn mem_save(path: String) -> Void {
let save_result: String = engram_save(path)
if str_eq(save_result, "") {
println("[memory] mem_save: engram_save failed for " + path + " — snapshot may be incomplete")
}
engram_save(path)
}
fn mem_load(path: String) -> Void {
@@ -79,14 +76,11 @@ fn mem_boot_count_inc() -> Int {
let next: Int = current + 1
let content: String = "soul:boot_count:" + int_to_str(next)
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",
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
"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
}
+2 -10
View File
@@ -400,7 +400,6 @@ fn handle_api_log_state_event(body: String) -> String {
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),
"Episodic", tags)
if !api_persisted(id) { return api_not_persisted(id) }
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,
el_from_float(0.85), el_from_float(0.85), el_from_float(0.9),
"Canonical", tags)
if !api_persisted(id) { return api_not_persisted(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 snap: String = state_get("soul_snapshot_path")
if !str_eq(snap, "") {
let save_result: String = engram_save(snap)
if str_eq(save_result, "") {
println("[api] consolidate: engram_save failed for " + snap + " — snapshot may be out of sync")
}
engram_save(snap)
}
if !str_eq(summary, "") {
let safe_summary: String = str_replace(summary, "\"", "'")
let tags: String = "[\"SessionSummary\",\"consolidate\"]"
let summary_id: String = engram_node_full(
let discard: String = engram_node_full(
"[session-summary] " + safe_summary,
"SessionSummary", "session:summary",
el_from_float(0.7), el_from_float(0.7), el_from_float(0.9),
"Episodic", tags
)
if str_eq(summary_id, "") {
println("[api] consolidate: session summary engram write failed — summary node lost")
}
}
return "{\"ok\":true,\"snapshot\":\"" + snap + "\"}"
}
-3
View File
@@ -367,9 +367,6 @@ fn handle_request(method: String, path: String, body: String) -> String {
return engram_scan_nodes_json(9999, 0)
}
if str_eq(clean, "/api/graph/edges") {
// TODO(reliability #8): engram_save races with awareness loop mem_save().
// Both now use atomic write-to-temp+rename (el_runtime.c). Serialised
// by engram_global_mu. Future: add engram_edges_json() builtin.
let snap_path: String = env("HOME") + "/.neuron/engram/snapshot.json"
engram_save(snap_path)
let snap: String = fs_read(snap_path)
+7 -18
View File
@@ -144,8 +144,7 @@ fn safety_screen(input: String, history: String) -> String {
if score >= soft {
let summary: String = str_slice(input, 0, 80)
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.
// ISSUE 7: also escape tab chars to prevent JSON envelope corruption.
let e1: String = str_replace(input, "\\", "\\\\")
let e2: String = str_replace(e1, "\"", "\\\"")
let e3: String = str_replace(e2, "\n", "\\n")
@@ -154,7 +153,7 @@ fn safety_screen(input: String, history: String) -> String {
return "{\"action\":\"soft_bell\",\"reason\":\"wellbeing check needed\",\"content\":\"" + safe_input + "\"}"
}
// ISSUE 7 fix: escape tab chars (see soft_bell branch above for rationale).
// ISSUE 7: also escape tab chars (see soft_bell branch above).
let e1: String = str_replace(input, "\\", "\\\\")
let e2: String = str_replace(e1, "\"", "\\\"")
let e3: String = str_replace(e2, "\n", "\\n")
@@ -200,10 +199,7 @@ fn safety_validate(output: String, action: String) -> String {
fn safety_log_bell(level: String, reason: String, input_summary: String) -> String {
let content: String = "BELL:" + level + " | " + reason + " | summary:" + input_summary
let tags: String = "[\"safety\",\"bell\",\"bell:" + level + "\"]"
// 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.
// ISSUE 2: fallback log when engram write fails silently.
let node_id: String = engram_node_full(
content,
"BellEvent",
@@ -215,7 +211,7 @@ fn safety_log_bell(level: String, reason: String, input_summary: String) -> Stri
tags
)
if str_eq(node_id, "") {
println("[safety] WARN: bell event engram write failed -- fallback log: " + content)
println("[safety] WARN: bell engram write failed -- " + content)
}
return ""
}
@@ -248,16 +244,9 @@ fn safety_soft_phrases() -> String {
}
// 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.
// json_array_len of malformed input returns 0, silently skipping all checks.
// Caching requires language-level static const arrays -- not in current EL.
// Migrate to const arrays when EL gains that feature.
// Matching helpers (single loops only el escapes while-body mutation via
// top-level let rebinds; nested loops would not advance) ────────────────────
+32 -4
View File
@@ -104,8 +104,6 @@ fn session_create(body: String) -> String {
// Newest sessions first (prepend).
// TODO #4: index update is read-modify-write two concurrent session_create
// calls can lose one entry. EL has no CAS primitive; fix requires runtime support.
// TODO(reliability #2): session_index RMW is non-atomic. Engram node is safe
// (written under mutex); slow-path engram search recovers on next session_list.
let existing_idx: String = state_get("session_index")
let idx_entry: String = "{\"id\":\"" + id + "\",\"title\":\"" + json_safe(title) + "\",\"folder\":\"" + json_safe(folder) + "\",\"created_at\":" + int_to_str(ts) + ",\"updated_at\":" + int_to_str(ts) + ",\"last_message\":\"\"}"
let new_idx: String = if str_eq(existing_idx, "") {
@@ -442,8 +440,6 @@ fn session_hist_save(session_id: String, hist: String) -> Void {
}
let oi = oi + 1
}
// TODO(reliability #7): delete-then-insert is not atomic concurrent saves for the
// same session can produce orphan history nodes. State is primary truth; engram fallback.
let tags: String = "[\"session\",\"session-history\",\"Conversation\"]"
let discard: String = engram_node_full(
hist, "Conversation", "session:messages:" + session_id,
@@ -492,6 +488,38 @@ 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.
+151 -12
View File
@@ -162,6 +162,107 @@ 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")
}
}
}
// seed_persona_from_env one-time migration: SOUL_IDENTITY env var Persona graph node.
@@ -296,11 +397,8 @@ fn layered_cycle(raw_input: String) -> String {
let cont_status: String = json_get(continuity, "status")
let cont_action: String = json_get(continuity, "action")
// Store continuity status so imprint can adjust its response register.
// TODO(reliability #4): session_continuity is process-global; scope per session_id
// when available to prevent cross-session bleed under concurrent layered_cycle calls.
let cont_key: String = if str_eq(session_id, "") { "session_continuity" } else { "session_continuity:" + session_id }
state_set(cont_key, cont_status)
// Store continuity status so imprint can adjust its response register
state_set("session_continuity", cont_status)
// Identity anomaly: add a gentle verification cue to the input before imprint
let guided: String = if str_eq(cont_action, "identity_check") {
@@ -323,14 +421,55 @@ fn layered_cycle(raw_input: String) -> String {
json_get(steward_result, "redirect_to")
}
// 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.
// 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
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