Compare commits

..

2 Commits

Author SHA1 Message Date
will.anderson 978a6812d7 fix(recall): address all remaining code review issues
Issue 1 (CRITICAL): Fix auto_persist brace structure. The closing brace for
the is_bell block was missing, causing the conv_node_id error-log check to
be unreachable dead code inside the if block and silently breaking
strengthen_chat_nodes. Add the missing } to close the is_bell block before
the conv_node_id guard.

Issue 2 (CRITICAL): Restore session_exists() call in handle_chat_agentic.
The behavioral regression replacing session_exists() with
!str_contains(session_get(...), '"error"') was reverted. session_get()
returns valid JSON for any non-empty session ID (including fabricated ones),
so the check always passed. session_exists() does a proper state-index and
engram search.

Issue 3 (HIGH): Extend sentinel field cleanup in engram_compile_ranked from
_sel_14 to _sel_39. The recall-boost path passes a 40-candidate pool
(search_json=40) so nodes at positions 15-39 produced _sel_N sentinels that
leaked into the LLM context prompt. Cleanup chain now covers all 40 indices.

Issue 4 (HIGH): Fix engram_is_continuation false positives. Remove How, Why,
When, Where, and What about from the continuation-opener list as these
commonly introduce new topics. Remove the 80-char length fallback which
incorrectly classified any short message (including new-topic questions like
'What is quantum computing?') as a continuation.

Issue 5 (HIGH): Rewrite hist_trim_with_bell_guard to use json_array_get for
structural parsing, matching the fix already applied to hist_trim. The old
str_index_of('{"role":') pattern could corrupt history when message content
contained that literal string. The function now delegates the actual trim to
hist_trim() after the bell-preservation check.

Issue 6 (NORMAL): Fix entity_count scoping in engram_extract_entities. Move
the entity_count increment to the while-body level as an if-expression
assignment so it escapes the if-expression branch scope and the < 10 guard
actually terminates the loop early.

Issue 7 (NORMAL): Fix mcp_call_seq race in call_mcp_bridge. Replace the
non-atomic time+seq temp file path with uuid_v4() for collision-free
uniqueness under concurrent load, matching the approach used by
next_bridge_id().

Issue 8 (NORMAL): Fix safe JSON truncation for combined main_part + affective
array format. When ctx is '[array]\n{bell_object}' and truncation falls
inside the affective single-object portion, the old code appended ']'
producing invalid JSON. Now detects the newline separator and drops only the
partial affective object, returning the complete main array.

Issue 9 (NORMAL): Handle 4th+ topics in engram_compile. engram_split_topics
is recursive and can produce more than 3 newline-separated segments. Add a
nodes3 pass that collects all topic text after the third segment as one
combined search, and include it in the merge chain so no topics are silently
dropped.
2026-06-22 13:36:41 -05:00
will.anderson 18e040acb1 feat(recall): recall-completeness improvements
Neuron Soul CI / build (pull_request) Has been cancelled
- Lower engram_compile_ranked threshold 25->15: include moderately-relevant older nodes
- Extend sentinel cleanup from _sel_9 to _sel_14 to prevent JSON noise
- Add engram_split_topics for multi-topic decomposition (AND/and/also/plus)
- Add engram_extract_entities for named entity dedicated searches
- Add engram_detect_recall_intent for boosted 40-candidate search on recall phrases
- Add engram_is_continuation replacing brittle 50-char threshold (now 80 + pronoun/opener detection)
- Add engram_compile_multi with depth 8 (was 5) and 30-candidate search pool
- Add engram_nodes_merge for clean two-array deduplication
- Replace engram_compile with multi-topic/entity/recall-boost version; budget 6000->8000
- Safe JSON truncation: scan for last } before budget cap instead of raw str_slice
- handle_chat and agentic_chat: use engram_is_continuation; thread snip 150->250
- session_preload: add project-status and session-summary search queries
2026-06-22 13:11:06 -05:00
3 changed files with 454 additions and 386 deletions
+445 -280
View File
@@ -12,107 +12,39 @@ fn chat_default_model() -> String {
return "claude-sonnet-4-5"
}
// parse_salience_100 convert a salience/importance float string (as serialized by
// %g format) to an integer in the range 0..100.
//
// The runtime serializes floats with %g which drops trailing zeros:
// 1.0 -> "1" (no decimal at all)
// 0.9 -> "0.9" (one decimal digit)
// 0.85 -> "0.85" (two decimal digits)
// 0.125 -> "0.125" (three decimal digits %g does not round to 2 dp)
//
// The old approach of str_replace(s, ".", "") then str_to_int was broken:
// "0.9" -> "09" -> str_to_int -> 9 (should be 90)
// "0.5" -> "05" -> str_to_int -> 5 (should be 50)
// "1" -> "1" -> str_to_int -> 1 (should be 100)
// "0.85" -> "085" -> str_to_int -> 85 (accidentally correct)
// "0.125" -> "0125" -> str_to_int -> 125 -> clamped to 100 (wrong: should be 12)
//
// Fix: detect presence and position of the decimal point, then scale accordingly.
// - No decimal (e.g. "1"): multiply by 100.
// - One decimal digit (e.g. "0.9"): multiply by 10 to get 90.
// - Two decimal digits (e.g. "0.85"): use as-is (already hundredths).
// - Three+ decimal digits: stripped integer is in units of 10^N (where N=digits
// after the dot), so divide by 10^(N-2) to reduce to hundredths. Examples:
// "0.125" -> stripped=125, N=3 -> 125/10 = 12
// "0.375" -> stripped=375, N=3 -> 375/10 = 37
// "0.625" -> stripped=625, N=3 -> 625/10 = 62
// "0.875" -> stripped=875, N=3 -> 875/10 = 87
fn parse_salience_100(s: String) -> Int {
if str_eq(s, "") { return 70 }
let dot_pos: Int = str_index_of(s, ".")
let raw: Int = if dot_pos < 0 {
let v: Int = str_to_int(s)
v * 100
} else {
let after_dot: String = str_slice(s, dot_pos + 1, str_len(s))
let decimal_digits: Int = str_len(after_dot)
let stripped: Int = str_to_int(str_replace(s, ".", ""))
if decimal_digits == 1 {
stripped * 10
} else {
if decimal_digits == 2 {
stripped
} else {
// 3+ decimal digits: divide out the extra precision to get hundredths.
// extra = decimal_digits - 2; divisor = 10^extra.
let extra: Int = decimal_digits - 2
let divisor: Int = if extra == 1 { 10 } else {
if extra == 2 { 100 } else {
if extra == 3 { 1000 } else {
if extra == 4 { 10000 } else { 100000 }
}
}
}
stripped / divisor
}
}
}
if raw > 100 { 100 } else { if raw < 0 { 0 } else { raw } }
}
// engram_score_node compute a recency x relevance score for a single engram
// node JSON object. Higher is better. Score = salience * importance * recency_factor.
//
// Recency uses a tier-aware decay curve instead of a flat linear slope:
// - Canonical tiers decay very slowly: 365-day window (foundational identity).
// - Episodic tiers decay at a moderate rate: 90-day window (conversation context).
// - Working/untiered nodes decay at 30 days (transient task state).
// - Floor is 10 (never zero) for all tiers.
//
// Uses max(created_at, updated_at) so recently-revised nodes are not penalised.
// recency_factor decays linearly over 30 days: nodes updated today score 1.0,
// nodes 30+ days old score 0.1 (floor). Nodes with no created_at score 0.5.
// This keeps fresh, high-salience nodes at the top and pushes stale low-signal
// nodes to the bottom so they get trimmed when we cap context size.
fn engram_score_node(node_json: String) -> Int {
let salience_str: String = json_get(node_json, "salience")
let importance_str: String = json_get(node_json, "importance")
let created_str: String = json_get(node_json, "created_at")
let updated_str: String = json_get(node_json, "updated_at")
let tier_str: String = json_get(node_json, "tier")
let salience_100: Int = parse_salience_100(salience_str)
let importance_100: Int = parse_salience_100(importance_str)
// Parse as floats via * 100 integer arithmetic (el has no float math)
let salience_100: Int = if str_eq(salience_str, "") { 70 } else {
let s: Int = str_to_int(str_replace(salience_str, ".", ""))
// Clamp to 0-100 range (value was e.g. "0.85" -> parsed "085" = 85)
if s > 100 { 100 } else { if s < 0 { 0 } else { s } }
}
let importance_100: Int = if str_eq(importance_str, "") { 70 } else {
let v: Int = str_to_int(str_replace(importance_str, ".", ""))
if v > 100 { 100 } else { if v < 0 { 0 } else { v } }
}
// Recency: decay from 100 (today) to 10 (30+ days). created_at is Unix seconds.
let now_ts: Int = time_now()
let recency_100: Int = if str_eq(created_str, "") { 50 } else {
let created_ts: Int = str_to_int(created_str)
let updated_ts: Int = if str_eq(updated_str, "") { 0 } else { str_to_int(updated_str) }
let ref_ts: Int = if updated_ts > created_ts { updated_ts } else { created_ts }
let age_secs: Int = now_ts - ref_ts
let age_days: Int = if age_secs < 0 { 0 } else { age_secs / 86400 }
let is_canonical: Bool = str_eq(tier_str, "Canonical")
let is_episodic: Bool = str_eq(tier_str, "Episodic")
let decay: Int = if is_canonical {
let drop: Int = if age_days >= 365 { 90 } else { age_days * 90 / 365 }
100 - drop
} else {
if is_episodic {
if age_days >= 90 { 10 } else { 100 - age_days }
} else {
if age_days >= 30 { 10 } else { 100 - (age_days * 3) }
}
}
let age_secs: Int = now_ts - created_ts
let age_days: Int = age_secs / 86400
let decay: Int = if age_days >= 30 { 10 } else { 100 - (age_days * 3) }
if decay < 10 { 10 } else { decay }
}
// Combined score 0-1000000 (no floats): salience * importance * recency / 10000
return salience_100 * importance_100 * recency_100 / 10000
}
@@ -141,8 +73,9 @@ fn engram_compile_ranked(nodes_json: String, max_nodes: Int) -> String {
while ci < total {
let node: String = json_array_get(nodes_json, ci)
let score: Int = engram_score_node(node)
// Only include reasonably relevant nodes (threshold=25)
let above_thresh: Bool = score >= 25
// Threshold lowered from 25 to 15: includes moderately-relevant older nodes.
// A 3-week-old node with salience 0.6 and importance 0.6 scores ~18 was dropped, now included.
let above_thresh: Bool = score >= 15
// Check this index wasn't already selected (sentinel: look for idx marker)
let idx_marker: String = "\"_sel_" + int_to_str(ci) + "\""
let already_picked: Bool = str_contains(selected, idx_marker)
@@ -181,60 +114,302 @@ fn engram_compile_ranked(nodes_json: String, max_nodes: Int) -> String {
let c7: String = str_replace(c6, "\"_sel_7\":1,", "")
let c8: String = str_replace(c7, "\"_sel_8\":1,", "")
let c9: String = str_replace(c8, "\"_sel_9\":1,", "")
return c9
let c10: String = str_replace(c9, "\"_sel_10\":1,", "")
let c11: String = str_replace(c10, "\"_sel_11\":1,", "")
let c12: String = str_replace(c11, "\"_sel_12\":1,", "")
let c13: String = str_replace(c12, "\"_sel_13\":1,", "")
let c14: String = str_replace(c13, "\"_sel_14\":1,", "")
let c15: String = str_replace(c14, "\"_sel_15\":1,", "")
let c16: String = str_replace(c15, "\"_sel_16\":1,", "")
let c17: String = str_replace(c16, "\"_sel_17\":1,", "")
let c18: String = str_replace(c17, "\"_sel_18\":1,", "")
let c19: String = str_replace(c18, "\"_sel_19\":1,", "")
let c20: String = str_replace(c19, "\"_sel_20\":1,", "")
let c21: String = str_replace(c20, "\"_sel_21\":1,", "")
let c22: String = str_replace(c21, "\"_sel_22\":1,", "")
let c23: String = str_replace(c22, "\"_sel_23\":1,", "")
let c24: String = str_replace(c23, "\"_sel_24\":1,", "")
let c25: String = str_replace(c24, "\"_sel_25\":1,", "")
let c26: String = str_replace(c25, "\"_sel_26\":1,", "")
let c27: String = str_replace(c26, "\"_sel_27\":1,", "")
let c28: String = str_replace(c27, "\"_sel_28\":1,", "")
let c29: String = str_replace(c28, "\"_sel_29\":1,", "")
let c30: String = str_replace(c29, "\"_sel_30\":1,", "")
let c31: String = str_replace(c30, "\"_sel_31\":1,", "")
let c32: String = str_replace(c31, "\"_sel_32\":1,", "")
let c33: String = str_replace(c32, "\"_sel_33\":1,", "")
let c34: String = str_replace(c33, "\"_sel_34\":1,", "")
let c35: String = str_replace(c34, "\"_sel_35\":1,", "")
let c36: String = str_replace(c35, "\"_sel_36\":1,", "")
let c37: String = str_replace(c36, "\"_sel_37\":1,", "")
let c38: String = str_replace(c37, "\"_sel_38\":1,", "")
let c39: String = str_replace(c38, "\"_sel_39\":1,", "")
return c39
}
// engram_split_topics split message into sub-queries on explicit conjunctions.
// "health goals AND startup progress" becomes two independent searches.
fn engram_split_topics(message: String) -> String {
let sep: String = if str_contains(message, " AND ") { " AND " } else {
if str_contains(message, " and ") { " and " } else {
if str_contains(message, " also ") { " also " } else {
if str_contains(message, " plus ") { " plus " } else { "" }
}
}
}
if str_eq(sep, "") { return message }
let sep_pos: Int = str_index_of(message, sep)
let part1: String = str_slice(message, 0, sep_pos)
let part2: String = str_slice(message, sep_pos + str_len(sep), str_len(message))
let part2_topics: String = engram_split_topics(part2)
if str_eq(part1, "") { return part2_topics }
return part1 + "\n" + part2_topics
}
// engram_extract_entities extract probable named entities (capital-first, 3+ chars,
// not stop-words) from a message. Returns newline-separated list.
fn engram_extract_entities(message: String) -> String {
let stops: String = "|I|A|The|An|In|On|At|To|Of|For|And|But|Or|So|My|Me|We|Us|He|She|It|Is|Are|Was|Were|Has|Have|Had|Do|Does|Did|Can|Could|Will|Would|Should|May|Might|Must|Be|Been|Being|This|That|These|Those|What|When|Where|Who|How|Why|Which|If|Then|Now|Just|Also|Not|No|Yes|Oh|Hi|Hey|Ok|Okay|Please|Thank|Thanks|You|Your|Our|Its|His|Her|Their|Any|All|Some|Get|Got|Let|Say|Think|Know|See|Look|Go|Come|Make|Take|Give|Tell|Ask|Need|Want|Like|Love|Feel|Try|Use|Find|Keep|Put|Set|Run|Start|Stop|Show|Help|Work|Play|Move|Change|Follow|Call|Talk|Check|Remind|Update|Create|Delete|Fix|Add|Remove|Open|Close|Read|Write|Send|Receive|"
let capitals: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let entities: String = ""
let entity_count: Int = 0
let msg_len: Int = str_len(message)
let pos: Int = 0
while pos < msg_len && entity_count < 10 {
let wend: Int = pos
let scanning: Bool = true
while scanning && wend < msg_len {
let wch: String = str_slice(message, wend, wend + 1)
let is_sep: Bool = str_eq(wch, " ") || str_eq(wch, "\n") || str_eq(wch, "\t")
|| str_eq(wch, ",") || str_eq(wch, ".") || str_eq(wch, "?")
|| str_eq(wch, "!") || str_eq(wch, ":") || str_eq(wch, ";")
|| str_eq(wch, "(") || str_eq(wch, ")") || str_eq(wch, "\'") || str_eq(wch, "-")
let scanning = if is_sep { false } else { scanning }
let wend = if !is_sep { wend + 1 } else { wend }
}
let word: String = str_slice(message, pos, wend)
let word_len: Int = str_len(word)
let first_ch: String = if word_len >= 3 { str_slice(word, 0, 1) } else { "" }
let is_capital: Bool = word_len >= 3 && str_contains(capitals, first_ch)
let is_stop: Bool = str_contains(stops, "|" + word + "|")
let already_have: Bool = str_contains(entities, word)
let should_add: Bool = is_capital && !is_stop && !already_have && word_len >= 3
let entities = if should_add {
if str_eq(entities, "") { word } else { entities + "\n" + word }
} else { entities }
// Increment entity_count at the while-body level so the binding escapes the
// if-expression scope and the entity_count < 10 guard actually terminates early.
let entity_count = if should_add { entity_count + 1 } else { entity_count }
let pos = if wend > pos { wend + 1 } else { pos + 1 }
}
return entities
}
// engram_detect_recall_intent true when message explicitly requests memory recall.
fn engram_detect_recall_intent(message: String) -> Bool {
return str_contains(message, "remind me")
|| str_contains(message, "do you remember")
|| str_contains(message, "what do you know")
|| str_contains(message, "what happened")
|| str_contains(message, "tell me about")
|| str_contains(message, "what was")
|| str_contains(message, "what were")
|| str_contains(message, "how is it going")
|| str_contains(message, "how are things")
|| str_contains(message, "catch me up")
|| str_contains(message, "fill me in")
|| str_contains(message, "what's the status")
|| str_contains(message, "whats the status")
|| str_contains(message, "any updates")
|| str_contains(message, "recap")
|| str_contains(message, "look up")
|| str_contains(message, "check on")
|| str_contains(message, "how did")
|| str_contains(message, "what happened with")
}
// engram_is_continuation detect whether a message continues the active thread.
// Returns true only when the message starts with a pronoun or an unambiguous
// discourse continuation marker. Does NOT classify by message length: short messages
// that introduce a new topic (e.g. "What is quantum computing?") are not continuations.
// Does NOT classify interrogative starters (How, Why, When, Where, What about) as
// continuations these commonly open new topics and the false-positive cost is too high.
fn engram_is_continuation(message: String, hist_len: Int) -> Bool {
if hist_len <= 0 { return false }
let has_pronoun: Bool = str_starts_with(message, "It ")
|| str_starts_with(message, "it ")
|| str_starts_with(message, "That ") || str_starts_with(message, "that ")
|| str_starts_with(message, "This ") || str_starts_with(message, "this ")
|| str_starts_with(message, "They ") || str_starts_with(message, "they ")
|| str_starts_with(message, "He ") || str_starts_with(message, "he ")
|| str_starts_with(message, "She ") || str_starts_with(message, "she ")
|| str_starts_with(message, "We ") || str_starts_with(message, "we ")
if has_pronoun { return true }
// Only unambiguous discourse connectors that cannot open a new topic on their own.
let is_cont_opener: Bool = str_starts_with(message, "Go on")
|| str_starts_with(message, "go on")
|| str_starts_with(message, "Continue") || str_starts_with(message, "continue")
|| str_starts_with(message, "Yes") || str_starts_with(message, "yes")
|| str_starts_with(message, "No,") || str_starts_with(message, "no,")
|| str_starts_with(message, "Ok") || str_starts_with(message, "ok")
|| str_starts_with(message, "And ") || str_starts_with(message, "and ")
|| str_starts_with(message, "But ") || str_starts_with(message, "but ")
if is_cont_opener { return true }
return false
}
// engram_compile_multi run activation + search for one topic with expanded pools.
// Activation depth 8 (was 5). Search 30 candidates ranked to 12 (was 20/8).
// Per-topic result pool: up to 20 nodes (was 13).
fn engram_compile_multi(topic: String) -> String {
let activate_json: String = engram_activate_json(topic, 8)
let search_json: String = engram_search_json(topic, 30)
let act_ok: Bool = !str_eq(activate_json, "") && !str_eq(activate_json, "[]")
let srch_ok: Bool = !str_eq(search_json, "") && !str_eq(search_json, "[]")
let act_nodes: String = if act_ok { activate_json } else { "" }
let srch_nodes: String = if srch_ok { engram_compile_ranked(search_json, 12) } else { "" }
if !str_eq(act_nodes, "") && !str_eq(srch_nodes, "") {
let act_inner: String = str_slice(act_nodes, 1, str_len(act_nodes) - 1)
let srch_inner: String = str_slice(srch_nodes, 1, str_len(srch_nodes) - 1)
return engram_dedup_nodes("[" + act_inner + "," + srch_inner + "]")
}
if !str_eq(act_nodes, "") { return act_nodes }
if !str_eq(srch_nodes, "") { return srch_nodes }
return ""
}
// engram_nodes_merge merge two node arrays, deduplicating by node id.
fn engram_nodes_merge(a: String, b: String) -> String {
let ok_a: Bool = !str_eq(a, "") && !str_eq(a, "[]")
let ok_b: Bool = !str_eq(b, "") && !str_eq(b, "[]")
if !ok_a && !ok_b { return "" }
if !ok_a { return b }
if !ok_b { return a }
let ai: String = str_slice(a, 1, str_len(a) - 1)
let bi: String = str_slice(b, 1, str_len(b) - 1)
return engram_dedup_nodes("[" + ai + "," + bi + "]")
}
fn engram_compile(intent: String) -> String {
let activate_json: String = engram_activate_json(intent, 5)
// Fetch more search results than we'll use so ranking has a real pool to pick from.
let search_json: String = engram_search_json(intent, 20)
// Issue 1: decompose multi-topic messages into sub-queries.
let topics: String = engram_split_topics(intent)
let has_multi_topic: Bool = str_contains(topics, "\n")
let act_ok: Bool = !str_eq(activate_json, "") && !str_eq(activate_json, "[]")
let srch_ok: Bool = !str_eq(search_json, "") && !str_eq(search_json, "[]")
// Issue 4: detect explicit recall intent and run boosted search.
let is_recall_intent: Bool = engram_detect_recall_intent(intent)
// Activation nodes (spreading activation) are already high-signal keep all 5.
let act_part: String = if act_ok { activate_json } else { "" }
// Issue 2: extract named entities for dedicated per-entity searches.
let entity_list: String = engram_extract_entities(intent)
let has_entities: Bool = !str_eq(entity_list, "")
// Rank search results and keep only the top 8 (was: flat 15 unranked).
// This cuts context noise roughly in half while preserving the best-scoring nodes.
let srch_ranked: String = if srch_ok { engram_compile_ranked(search_json, 8) } else { "" }
let srch_part: String = srch_ranked
// Primary topic search (first or only topic).
let topic0: String = if has_multi_topic {
let nl0: Int = str_index_of(topics, "\n")
str_slice(topics, 0, nl0)
} else { topics }
let nodes0: String = engram_compile_multi(topic0)
// Fallback: when vector search returns nothing (no embeddings), fetch pinned
// high-salience nodes by their known IDs. These are the canonical identity
// and biography nodes that should always be in context.
// engram_get_node_json(id) returns a single node as JSON or "" if missing.
let scan_part: String = if !act_ok && !srch_ok {
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_str: String = if fam_ok { family_node } else { "" }
let orig_str: String = if orig_ok { origin_node } else { "" }
let sep: String = if fam_ok && orig_ok { "\n" } else { "" }
let combined: String = fam_str + sep + orig_str
if str_eq(combined, "") { "" } else { combined }
} else {
""
}
// Second topic segment.
let nodes1: String = if has_multi_topic {
let nl0: Int = str_index_of(topics, "\n")
let rest1: String = str_slice(topics, nl0 + 1, str_len(topics))
let nl1: Int = str_index_of(rest1, "\n")
let topic1: String = if nl1 < 0 { rest1 } else { str_slice(rest1, 0, nl1) }
if str_eq(topic1, "") { "" } else { engram_compile_multi(topic1) }
} else { "" }
// Affective context: always include the most recent high-emotion memory if one
// exists within 14 days. This ensures continuity of care across sessions a
// crisis on Friday must still carry into Monday (72h was too narrow for multi-day
// distress arcs such as grief or recurring suicidal ideation). 14-day window
// (1,209,600 seconds) covers sustained emotional arcs while excluding ancient
// history. Unified with handle_chat and soul.el affective checks.
// We search for BellEvent nodes specifically; these are written by auto_persist
// when safety_detect_bell_level fires.
// Third topic segment.
let nodes2: String = if has_multi_topic {
let nl0: Int = str_index_of(topics, "\n")
let rest1: String = str_slice(topics, nl0 + 1, str_len(topics))
let nl1: Int = str_index_of(rest1, "\n")
if nl1 < 0 { "" } else {
let rest2: String = str_slice(rest1, nl1 + 1, str_len(rest1))
let nl2: Int = str_index_of(rest2, "\n")
let topic2: String = if nl2 < 0 { rest2 } else { str_slice(rest2, 0, nl2) }
if str_eq(topic2, "") { "" } else { engram_compile_multi(topic2) }
}
} else { "" }
// Fourth+ topic segments: engram_split_topics is recursive and can produce 4+ lines.
// Rather than hardcoding each topic index, collect everything after the third topic
// as a single combined search query so no segments are silently dropped.
// This handles inputs like "a and b and c and d" (4 topics).
let nodes3: String = if has_multi_topic {
let nl0: Int = str_index_of(topics, "\n")
let rest1: String = str_slice(topics, nl0 + 1, str_len(topics))
let nl1: Int = str_index_of(rest1, "\n")
if nl1 < 0 { "" } else {
let rest2: String = str_slice(rest1, nl1 + 1, str_len(rest1))
let nl2: Int = str_index_of(rest2, "\n")
if nl2 < 0 { "" } else {
// Remainder after the third segment may span one or more topics.
// Search with the remaining text as-is; engram_compile_multi handles it.
let rest3: String = str_slice(rest2, nl2 + 1, str_len(rest2))
if str_eq(rest3, "") { "" } else { engram_compile_multi(rest3) }
}
}
} else { "" }
// Issue 2 cont.: entity 0 dedicated search (15 candidates, ranked 6).
let entity_nodes0: String = if has_entities {
let nl_e0: Int = str_index_of(entity_list, "\n")
let entity0: String = if nl_e0 < 0 { entity_list } else { str_slice(entity_list, 0, nl_e0) }
if str_eq(entity0, "") { "" } else {
let ent_srch: String = engram_search_json(entity0, 15)
let ent_ok: Bool = !str_eq(ent_srch, "") && !str_eq(ent_srch, "[]")
if ent_ok { engram_compile_ranked(ent_srch, 6) } else { "" }
}
} else { "" }
// Entity 1 dedicated search.
let entity_nodes1: String = if has_entities {
let nl_e0: Int = str_index_of(entity_list, "\n")
if nl_e0 < 0 { "" } else {
let rest_e: String = str_slice(entity_list, nl_e0 + 1, str_len(entity_list))
let nl_e1: Int = str_index_of(rest_e, "\n")
let entity1: String = if nl_e1 < 0 { rest_e } else { str_slice(rest_e, 0, nl_e1) }
if str_eq(entity1, "") { "" } else {
let ent_srch1: String = engram_search_json(entity1, 15)
let ent1_ok: Bool = !str_eq(ent_srch1, "") && !str_eq(ent_srch1, "[]")
if ent1_ok { engram_compile_ranked(ent_srch1, 6) } else { "" }
}
}
} else { "" }
// Issue 4 cont.: boosted search for recall-intent (40 candidates, ranked 15).
let recall_boost: String = if is_recall_intent {
let boost_srch: String = engram_search_json(intent, 40)
let boost_ok: Bool = !str_eq(boost_srch, "") && !str_eq(boost_srch, "[]")
if boost_ok { engram_compile_ranked(boost_srch, 15) } else { "" }
} else { "" }
// Merge all pools, deduplicating at each step.
let merged: String = engram_nodes_merge(nodes0, nodes1)
let merged: String = engram_nodes_merge(merged, nodes2)
let merged: String = engram_nodes_merge(merged, nodes3)
let merged: String = engram_nodes_merge(merged, entity_nodes0)
let merged: String = engram_nodes_merge(merged, entity_nodes1)
let merged: String = engram_nodes_merge(merged, recall_boost)
let merged_nodes: String = merged
// Fallback: when all searches return nothing, fetch persona nodes.
let scan_part: String = if str_eq(merged_nodes, "") || str_eq(merged_nodes, "[]") {
let persona_fallback: String = engram_search_json("soul:persona Persona identity", 5)
let pf_ok: Bool = !str_eq(persona_fallback, "") && !str_eq(persona_fallback, "[]")
if pf_ok {
let pf_ranked: String = engram_compile_ranked(persona_fallback, 3)
if str_eq(pf_ranked, "") { "" } else { pf_ranked }
} else { "" }
} else { "" }
// Affective context: always include the most recent high-emotion memory within 72h.
let bell_nodes: String = engram_search_json("bell:soft bell:hard BellEvent", 3)
let bell_ok: Bool = !str_eq(bell_nodes, "") && !str_eq(bell_nodes, "[]")
let now_ts: Int = time_now()
let cutoff_ts: Int = now_ts - 1209600
let cutoff_ts: Int = now_ts - 259200
let recent_bell: String = if bell_ok {
let bn0: String = json_array_get(bell_nodes, 0)
// created_at is not present in engram node JSON for BellEvent nodes.
// Extract the timestamp embedded in the content string as " | ts:NNNNN".
// Fall back to created_at / updated_at JSON fields if the marker is absent.
let bn_content: String = json_get(bn0, "content")
let ts_marker: String = " | ts:"
let ts_pos: Int = str_index_of(bn_content, ts_marker)
@@ -252,20 +427,50 @@ fn engram_compile(intent: String) -> String {
} else { "" }
let affective_part: String = if !str_eq(recent_bell, "") { recent_bell } else { "" }
let sep1: String = if !str_eq(act_part, "") && !str_eq(srch_part, "") { "\n" } else { "" }
let sep2: String = if (!str_eq(act_part, "") || !str_eq(srch_part, "")) && !str_eq(scan_part, "") { "\n" } else { "" }
let sep3: String = if (!str_eq(act_part, "") || !str_eq(srch_part, "") || !str_eq(scan_part, "")) && !str_eq(affective_part, "") { "\n" } else { "" }
let ctx: String = act_part + sep1 + srch_part + sep2 + scan_part + sep3 + affective_part
let has_main: Bool = !str_eq(merged_nodes, "") && !str_eq(merged_nodes, "[]")
let main_part: String = if has_main { merged_nodes } else { scan_part }
let sep_ma: String = if !str_eq(main_part, "") && !str_eq(affective_part, "") { "\n" } else { "" }
let ctx: String = main_part + sep_ma + affective_part
if str_eq(ctx, "") { return "" }
// Raise the cap slightly to match the ranked (higher-signal) output.
if str_len(ctx) > 6000 {
return str_slice(ctx, 0, 6000)
// Safe JSON truncation find last closing brace before budget cap.
// Budget raised from 6000 to 8000 for the larger multi-topic pool.
//
// Issue 8 fix: ctx may be main_part (JSON array) + "\n" + affective_part (single JSON
// object). When truncation cuts into the affective_part, appending "]" would produce
// "[array_content]{partial_bell_node}]" not valid JSON. Guard: only append "]" when
// the truncation point falls strictly within the main array portion (before the "\n"
// separator). If the cut falls in the affective part, drop the partial object entirely
// and return only the complete main array. If there is no separator (ctx is a plain
// array with no affective part), the original append-"]" behaviour applies.
let budget: Int = 8000
if str_len(ctx) <= budget { return ctx }
let search_end: Int = budget - 1
let scan_limit: Int = if search_end > 500 { search_end - 500 } else { 0 }
let found_pos: Int = -1
let si: Int = search_end
while si >= scan_limit {
let ch: String = str_slice(ctx, si, si + 1)
let found_pos = if str_eq(ch, "}") && found_pos < 0 { si } else { found_pos }
let si = if found_pos >= 0 { scan_limit - 1 } else { si - 1 }
}
return ctx
if found_pos < 0 { return str_slice(ctx, 0, budget) }
let truncated: String = str_slice(ctx, 0, found_pos + 1)
if str_starts_with(ctx, "[") {
// Determine whether this ctx has a separate affective object appended after the array.
// The format is: main_array + "\n" + bell_object. Find the array boundary.
let nl_pos: Int = str_index_of(ctx, "\n")
let has_affective_sep: Bool = nl_pos > 0 && nl_pos < str_len(ctx) - 1
if has_affective_sep && found_pos > nl_pos {
// Truncation fell inside the affective_part drop it and return just the main array.
return str_slice(ctx, 0, nl_pos)
}
// Truncation is within the main array close it properly.
return truncated + "]"
}
return truncated
}
fn json_safe(s: String) -> String {
let s1: String = str_replace(s, "\\", "\\\\")
let s2: String = str_replace(s1, "\"", "\\\"")
@@ -365,23 +570,19 @@ fn hist_trim(hist: String) -> String {
// a bell event. If it did, write a preservation node to engram so the distress exchange
// survives the 20-turn window. The LLM window drops it; engram retains it permanently
// and engram_compile will surface it again via the affective context path.
//
// Fix: use json_array_get for structural parsing (immune to {"role": appearing in
// message content) same fix applied to hist_trim. The old str_index_of("{\"role\":")
// pattern could corrupt history when any message contained that literal string.
fn hist_trim_with_bell_guard(hist: String) -> String {
// Extract the first turn (should be a user message) to inspect it.
let inner: String = str_slice(hist, 1, str_len(hist) - 1)
let marker: String = "{\"role\":"
let i1: Int = str_index_of(inner, marker)
// i1 is the start of the first entry within inner.
// Find where the second entry begins to delimit the first entry's JSON.
let tail1: String = str_slice(inner, i1 + 1, str_len(inner))
let i2: Int = str_index_of(tail1, marker)
// The first entry spans from i1 to (i1 + 1 + i2 - 1) within inner.
let first_entry_raw: String = if i2 > 0 {
str_slice(inner, i1, i1 + 1 + i2 - 1)
} else {
str_slice(inner, i1, str_len(inner))
}
let first_role: String = json_get(first_entry_raw, "role")
let first_content: String = json_get(first_entry_raw, "content")
let total: Int = json_array_len(hist)
// Safety: never trim below 2 entries.
if total <= 2 { return hist }
// Extract the first entry structurally immune to content containing {"role":
let first_entry: String = json_array_get(hist, 0)
let first_role: String = json_get(first_entry, "role")
let first_content: String = json_get(first_entry, "content")
// Only inspect user turns assistant content doesn't carry bell signals.
let bell_level: String = if str_eq(first_role, "user") {
@@ -414,54 +615,9 @@ fn hist_trim_with_bell_guard(hist: String) -> String {
)
}
// Now perform the standard trim (drop oldest 2 entries = 1 user + 1 assistant pair).
let tail2: String = str_slice(tail1, i2 + 1, str_len(tail1))
let i3: Int = str_index_of(tail2, marker)
if i3 >= 0 {
return "[" + str_slice(tail2, i3, str_len(tail2)) + "]"
}
return hist
}
// hist_trim_to_byte_cap drop oldest user+assistant pairs until the history blob
// is at or below `cap_bytes` in length, or until only 2 entries remain (the minimum
// safe window). Uses the same structural json_array_len/json_array_get approach as
// hist_trim to stay immune to content containing JSON marker strings.
//
// Called after count-based trimming to enforce a hard size ceiling on the history
// blob. Without this cap, long technical sessions with large assistant responses
// (code blocks, logs, analysis) can push the 40-turn window to 100KB+, which causes
// engram_node_full writes to grow state entries unboundedly.
fn hist_trim_to_byte_cap(hist: String, cap_bytes: Int) -> String {
let current: String = hist
let current_len: Int = str_len(current)
while current_len > cap_bytes {
let total: Int = json_array_len(current)
// Never trim below 2 entries (1 pair).
if total <= 2 {
let current_len = 0 // exit loop
} else {
// Drop entries 0 and 1 (oldest pair).
let result: String = ""
let i: Int = 2
while i < total {
let entry: String = json_array_get(current, i)
let result = if str_eq(result, "") {
entry
} else {
result + "," + entry
}
let i = i + 1
}
if str_eq(result, "") {
let current_len = 0 // exit loop
} else {
let current = "[" + result + "]"
let current_len = str_len(current)
}
}
}
return current
// Now perform the standard trim: drop entries 0 and 1 (oldest user+assistant pair).
// Reuse hist_trim's structural approach rebuild from entry 2 onward.
return hist_trim(hist)
}
// clean_llm_response strips GPT-2 BPE byte-to-unicode artifacts that vLLM
@@ -577,14 +733,12 @@ fn handle_chat(body: String) -> String {
let stored_hist: String = if str_eq(state_hist, "") { conv_history_load() } else { state_hist }
let hist_len: Int = if str_eq(stored_hist, "") { 0 } else { json_array_len(stored_hist) }
// Thread-aware activation: short/ambiguous messages (continuations like "go on",
// "what else?", "yes") activate on the last reply instead of the bare message.
// This prevents a strong off-topic memory node from hijacking the reply when the
// user is clearly continuing an existing thread.
let is_continuation: Bool = str_len(message) < 50 && hist_len > 0
// Issue 8 fix: use semantic continuation detection instead of brittle 50-char threshold.
let is_continuation: Bool = engram_is_continuation(message, hist_len)
let last_entry: String = if is_continuation { json_array_get(stored_hist, hist_len - 1) } else { "" }
let last_content: String = if !str_eq(last_entry, "") { json_get(last_entry, "content") } else { "" }
let thread_snip: String = if str_len(last_content) > 150 { str_slice(last_content, 0, 150) } else { last_content }
// Thread snip extended 150->250 chars for better pronoun resolution context.
let thread_snip: String = if str_len(last_content) > 250 { str_slice(last_content, 0, 250) } else { last_content }
let activation_seed: String = if !str_eq(thread_snip, "") {
thread_snip + " " + message
} else {
@@ -592,14 +746,12 @@ fn handle_chat(body: String) -> String {
}
// Cross-session affective context: on session start (no history yet), check engram
// for recent distress signals within 14 days and prepend a care directive if found.
// Extended from 72h: multi-day crisis must persist across Monday sessions starting
// 3+ days after a Friday event. Consistent with engram_compile and soul.el checks.
// 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 - 1209600
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")
@@ -615,71 +767,100 @@ fn handle_chat(body: String) -> String {
let ctx: String = engram_compile(activation_seed)
let system: String = affective_prefix + build_system_prompt(ctx)
// First message of the session: proactively load user profile and active work context.
// These two searches give the soul grounding before any conversation history exists.
// Results are rendered as brief bullets not raw JSON so they don't inflate context.
// 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.
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", 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)
let profile_ok: Bool = !str_eq(profile_nodes, "") && !str_eq(profile_nodes, "[]")
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, "[]")
// Extract content fields and render as bullet points (one per node, first 120 chars).
let profile_bullets: String = if profile_ok {
let pn: Int = json_array_len(profile_nodes)
let bullets: String = ""
let pi: Int = 0
// Collect up to 3 profile bullets
let bullets = if pi < pn {
let bullets = if pn > 0 {
let n0: String = json_array_get(profile_nodes, 0)
let c0: String = json_get(n0, "content")
let snip0: String = if str_len(c0) > 120 { str_slice(c0, 0, 120) } else { c0 }
if str_eq(snip0, "") { bullets } else { "- " + snip0 }
let s0: String = if str_len(c0) > 120 { str_slice(c0, 0, 120) } else { c0 }
if str_eq(s0, "") { bullets } else { "- " + s0 }
} else { bullets }
let bullets = if pn > 1 {
let n1: String = json_array_get(profile_nodes, 1)
let c1: String = json_get(n1, "content")
let snip1: String = if str_len(c1) > 120 { str_slice(c1, 0, 120) } else { c1 }
if str_eq(snip1, "") { bullets } else { bullets + "\n- " + snip1 }
let s1: String = if str_len(c1) > 120 { str_slice(c1, 0, 120) } else { c1 }
if str_eq(s1, "") { bullets } else { bullets + "\n- " + s1 }
} else { bullets }
let bullets = if pn > 2 {
let n2: String = json_array_get(profile_nodes, 2)
let c2: String = json_get(n2, "content")
let snip2: String = if str_len(c2) > 120 { str_slice(c2, 0, 120) } else { c2 }
if str_eq(snip2, "") { bullets } else { bullets + "\n- " + snip2 }
let s2: String = if str_len(c2) > 120 { str_slice(c2, 0, 120) } else { c2 }
if str_eq(s2, "") { bullets } else { bullets + "\n- " + s2 }
} else { bullets }
bullets
} else { "" }
let work_bullets: String = if work_ok {
let wn: Int = json_array_len(work_nodes)
let wbullets: String = ""
let wbullets = if wn > 0 {
let wb: String = ""
let wb = if wn > 0 {
let w0: String = json_array_get(work_nodes, 0)
let wc0: String = json_get(w0, "content")
let wsnip0: String = if str_len(wc0) > 120 { str_slice(wc0, 0, 120) } else { wc0 }
if str_eq(wsnip0, "") { wbullets } else { "- " + wsnip0 }
} else { wbullets }
let wbullets = if wn > 1 {
let ws0: String = if str_len(wc0) > 120 { str_slice(wc0, 0, 120) } else { wc0 }
if str_eq(ws0, "") { wb } else { "- " + ws0 }
} else { wb }
let wb = if wn > 1 {
let w1: String = json_array_get(work_nodes, 1)
let wc1: String = json_get(w1, "content")
let wsnip1: String = if str_len(wc1) > 120 { str_slice(wc1, 0, 120) } else { wc1 }
if str_eq(wsnip1, "") { wbullets } else { wbullets + "\n- " + wsnip1 }
} else { wbullets }
wbullets
let ws1: String = if str_len(wc1) > 120 { str_slice(wc1, 0, 120) } else { wc1 }
if str_eq(ws1, "") { wb } else { wb + "\n- " + ws1 }
} else { wb }
wb
} else { "" }
let has_profile: Bool = !str_eq(profile_bullets, "")
let has_work: Bool = !str_eq(work_bullets, "")
let preload: String = if has_profile || has_work {
let profile_section: String = if has_profile {
"[USER CONTEXT — from memory]\n" + profile_bullets
} else { "" }
let work_section: String = if has_work {
"[ACTIVE WORK — from memory]\n" + work_bullets
} else { "" }
let sep_pw: String = if has_profile && has_work { "\n\n" } else { "" }
"\n\n" + profile_section + sep_pw + work_section
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 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, "") { pb } else { "- " + ps0 }
} else { pb }
let pb = if prn > 1 {
let pr1: String = json_array_get(project_nodes, 1)
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, "") { pb } else { pb + "\n- " + ps1 }
} else { pb }
pb
} else { "" }
let summary_bullet: String = if summary_ok {
let sn0: String = json_array_get(summary_nodes, 0)
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, "") { "" } else { "- " + ss0 }
} else { "" }
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 { "" }
preload
} else { "" }
@@ -718,23 +899,16 @@ fn handle_chat(body: String) -> String {
let updated_hist2: String = hist_append(updated_hist, "assistant", raw_response)
// Use bell-guarded trim: if the evicted turn triggered a bell event, it is
// preserved to engram before being dropped from the in-memory window.
// Increased from 20 to 40 turns: long technical sessions lose early context at 20
// (10 user + 10 assistant pairs). 40 turns preserves problem framing for multi-step
// tasks while the bell guard still persists evicted distress turns to engram.
// Byte-cap: after count-based trim, also trim oldest pairs until the history blob
// is under 32KB. Long technical sessions with large assistant responses (code blocks,
// analysis) can produce 100-160KB+ state entries at 40 turns; the count limit alone
// is insufficient. We retain at least 2 entries (1 user + 1 assistant pair) regardless.
let count_trimmed: String = if json_array_len(updated_hist2) > 40 {
// Issue #8 (NO MAX SIZE GUARD): the 20-turn count limit bounds entry count, but individual
// messages can be arbitrarily large (up to max_tokens = 4096 tokens each). At 20 turns the
// history blob can reach ~80KB before trim fires. engram_node_full has no apparent size cap.
// A byte-length cap would require truncating or summarising entries too invasive here.
// TODO: add a byte-length cap (e.g. 32KB) that drops oldest entries until under limit.
let final_hist: String = if json_array_len(updated_hist2) > 20 {
hist_trim_with_bell_guard(updated_hist2)
} else {
updated_hist2
}
let final_hist: String = if str_len(count_trimmed) > 32768 {
hist_trim_to_byte_cap(count_trimmed, 32768)
} else {
count_trimmed
}
state_set("conv_history", final_hist)
conv_history_persist(final_hist)
@@ -905,15 +1079,11 @@ fn agentic_tools_all() -> String {
fn call_mcp_bridge(tool_name: String, tool_input: String) -> String {
let eff_input: String = if str_eq(tool_input, "") { "{}" } else { tool_input }
let body: String = "{\"name\":\"" + tool_name + "\",\"input\":" + eff_input + "}"
// Issue #12: previously used a fixed path /tmp/neuron-mcp-call.json.
// Under concurrent load (64 worker threads), two simultaneous MCP tool calls
// race on this file one call sends the other's input to the bridge.
// Fix: monotonic sequence counter makes the path unique per call.
let mcp_seq_s: String = state_get("mcp_call_seq")
let mcp_seq_n: Int = if str_eq(mcp_seq_s, "") { 0 } else { str_to_int(mcp_seq_s) }
let mcp_seq_next: Int = mcp_seq_n + 1
state_set("mcp_call_seq", int_to_str(mcp_seq_next))
let tmp: String = "/tmp/neuron-mcp-call-" + int_to_str(time_now()) + "-" + int_to_str(mcp_seq_next) + ".json"
// Issue #12: previously used a fixed path /tmp/neuron-mcp-call.json, then a
// time+seq path that still raced (time_now() is 1s granularity; non-atomic seq RMW).
// Fix: uuid_v4() provides collision-free uniqueness regardless of concurrency
// same approach used by next_bridge_id(). No state read/write needed.
let tmp: String = "/tmp/neuron-mcp-call-" + uuid_v4() + ".json"
fs_write(tmp, body)
return exec_capture("curl -s --max-time 30 -X POST http://127.0.0.1:7771/mcp/call -H 'Content-Type: application/json' -d @" + tmp)
}
@@ -1257,7 +1427,7 @@ fn handle_chat_agentic(body: String) -> String {
let session_valid: Bool = if str_eq(req_session, "") {
true
} else {
!str_contains(session_get(req_session), "\"error\"")
session_exists(req_session)
}
if !session_valid {
return "{\"error\":\"session not found\",\"session_id\":\"" + req_session + "\",\"reply\":\"\"}"
@@ -1266,7 +1436,8 @@ fn handle_chat_agentic(body: String) -> String {
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_len: Int = if str_eq(agentic_hist, "") { 0 } else { json_array_len(agentic_hist) }
let ag_is_cont: Bool = str_len(message) < 50 && agentic_hist_len > 0
// Issue 8 fix: use engram_is_continuation instead of brittle 50-char threshold.
let ag_is_cont: Bool = engram_is_continuation(message, agentic_hist_len)
let ag_last_entry: String = if ag_is_cont { json_array_get(agentic_hist, agentic_hist_len - 1) } else { "" }
let ag_last_content: String = if !str_eq(ag_last_entry, "") { json_get(ag_last_entry, "content") } else { "" }
let ag_thread_snip: String = if str_len(ag_last_content) > 150 { str_slice(ag_last_content, 0, 150) } else { ag_last_content }
@@ -1312,14 +1483,7 @@ fn handle_chat_agentic(body: String) -> String {
let discard_hist: Bool = if !str_eq(reply_text, "") {
let updated: String = hist_append(agentic_hist, "user", message)
let updated2: String = hist_append(updated, "assistant", reply_text)
// Increased from 20 to 40 turns: consistent with handle_chat window expansion.
// Byte-cap: also trim if the blob exceeds 32KB, consistent with handle_chat.
let count_trimmed2: String = if json_array_len(updated2) > 40 { hist_trim(updated2) } else { updated2 }
let trimmed: String = if str_len(count_trimmed2) > 32768 {
hist_trim_to_byte_cap(count_trimmed2, 32768)
} else {
count_trimmed2
}
let trimmed: String = if json_array_len(updated2) > 20 { hist_trim(updated2) } else { updated2 }
state_set(hist_key, trimmed)
// Only persist the default global session to engram named sessions are ephemeral.
if str_eq(hist_key, "conv_history") {
@@ -1903,6 +2067,7 @@ fn auto_persist(req: String, resp: String) -> Void {
"session_bell_signal:" + sess_id
}
state_set(signal_key, safe_summary)
}
if str_eq(conv_node_id, "") {
println("[chat] auto_persist: engram_node_full returned empty — conversation node lost (ts=" + ts_str + ")")
}
+3 -83
View File
@@ -35,94 +35,14 @@ fn mem_forget(node_id: String) -> Void {
engram_forget(node_id)
}
// mem_consolidate structural scan plus salience-evolution pass.
//
// Previously this only returned structural counts (scanned, total_nodes, total_edges)
// with no salience updates. No node salience ever changed based on recall frequency
// or time; foundational nodes decayed identically to ephemeral chat; frequently-recalled
// nodes were never promoted. This made consolidation a no-op.
//
// New behavior:
// (a) Strengthen frequently-activated nodes: nodes in the top working-memory list
// (engram_wm_top_json) are strengthened they have been recalled recently
// and deserve higher salience. Raises effective salience for nodes that prove
// relevant across multiple sessions.
// (b) Strengthen Canonical-tier nodes: identity and foundational nodes should not
// decay; each consolidation pass re-strengthens them so they resist the
// tier-aware decay curve without requiring active recall.
// (c) Structural counts are still returned for observability.
//
// Called by awareness_run() on the "consolidate" inbox action.
fn mem_consolidate() -> String {
let scanned: Int = engram_node_count()
let total_edges: Int = engram_edge_count()
let strengthened: Int = 0
// (a) Strengthen top working-memory nodes recalled recently across sessions.
// Cap at 10 to keep consolidation fast.
let wm_top: String = engram_wm_top_json(10)
let wm_len: Int = json_array_len(wm_top)
let wi: Int = 0
while wi < wm_len {
let wm_node: String = json_array_get(wm_top, wi)
let wm_id: String = json_get(wm_node, "id")
if !str_eq(wm_id, "") {
engram_strengthen(wm_id)
let strengthened = strengthened + 1
}
let wi = wi + 1
}
// (b) Strengthen Canonical-tier nodes from a full paginated scan so they resist
// temporal decay. Canonical nodes encode foundational identity they must not
// silently floor at 10. Page size 50, scanning until fewer than 50 nodes are
// returned (last page), so all Canonical nodes are reached even in large graphs.
// Without pagination, only the first 50 nodes in the graph were eligible; any
// Canonical node at index 50+ was silently excluded from the boost.
// Strengthening is skipped if the node's current salience is already at the
// runtime ceiling (represented as "1" by %g) to avoid monotonic unbounded growth.
// Canonical nodes with salience < 1.0 are strengthened each consolidation pass;
// once they reach the ceiling the runtime will no longer raise them further, so
// calling engram_strengthen at the ceiling is a no-op in the runtime anyway, but
// the explicit check makes the intent clear and avoids any runtime log noise.
let page_size: Int = 50
let scan_offset: Int = 0
let scan_done: Bool = false
while !scan_done {
let scan_result: String = engram_scan_nodes_json(page_size, scan_offset)
let scan_len: Int = json_array_len(scan_result)
if scan_len == 0 {
let scan_done = true
} else {
let si: Int = 0
while si < scan_len {
let s_node: String = json_array_get(scan_result, si)
let s_tier: String = json_get(s_node, "tier")
let s_id: String = json_get(s_node, "id")
let s_sal: String = json_get(s_node, "salience")
// Only strengthen if below the ceiling to prevent unbounded salience growth.
// engram serialises the ceiling as "1" (%g drops the decimal part when it
// is exactly zero). Any other value is below ceiling and should be boosted.
let at_ceiling: Bool = str_eq(s_sal, "1")
if str_eq(s_tier, "Canonical") && !str_eq(s_id, "") && !at_ceiling {
engram_strengthen(s_id)
let strengthened = strengthened + 1
}
let si = si + 1
}
let scan_offset = scan_offset + scan_len
// Fewer results than page_size means we've reached the last page.
if scan_len < page_size {
let scan_done = true
}
}
}
let dummy: String = engram_scan_nodes_json(100, 0)
let total_nodes: Int = engram_node_count()
let total_edges: Int = engram_edge_count()
return "{\"scanned\":" + int_to_str(scanned)
+ ",\"total_nodes\":" + int_to_str(total_nodes)
+ ",\"total_edges\":" + int_to_str(total_edges)
+ ",\"strengthened\":" + int_to_str(strengthened) + "}"
+ ",\"total_edges\":" + int_to_str(total_edges) + "}"
}
fn mem_save(path: String) -> Void {
+6 -23
View File
@@ -166,40 +166,23 @@ fn load_identity_context() -> Void {
// Cross-session affective context: query engram for recent distress/crisis signals
// at session start. Stored under soul_affective_context so the safety layer can
// detect when a user has been in distress across previous sessions.
// Recency guard: nodes older than 14 days (1,209,600 seconds) are skipped.
// Unified at 14 days with chat.el engram_compile and handle_chat affective checks
// so all three paths present consistent affective context. The previous 7-day
// (604800s) window was inconsistent with the 72h chat.el window, causing
// conflicting context: soul.el loaded a 5-day-old crisis node while chat.el
// did not include it on subsequent turns. Both now use 14 days.
// Results capped at 3 nodes, 200 chars each, to limit context inflation.
// Soft recency guard: nodes with a ts field older than 7 days are skipped.
// Results capped at 3 nodes, 200 chars each, to avoid over-injection into context.
// TODO(recency): engram_search_json sorts by relevance, not timestamp. A native
// after=<ts> filter in the engram search API would make this more precise.
let affective_raw: String = engram_search_json("distress crisis upset hopeless bell BellEvent", 3)
let affective_raw: String = engram_search_json("distress crisis upset hopeless", 3)
let affective_ok: Bool = !str_eq(affective_raw, "") && !str_eq(affective_raw, "[]")
if affective_ok {
let ts_now: Int = time_now()
let ts_cutoff: Int = ts_now - 1209600
let ts_cutoff: Int = ts_now - 604800
let aff_total: Int = json_array_len(affective_raw)
let aff_ctx: String = ""
let ai: Int = 0
while ai < aff_total {
let aff_node: String = json_array_get(affective_raw, ai)
let aff_content: String = json_get(aff_node, "content")
// Use created_at (the standard engram node timestamp field), consistent
// with handle_chat which reads created_at / updated_at. The previous
// field name "ts" is not a standard engram field: it was present in some
// BellEvent content payloads but absent from standard engram node JSON,
// causing json_get to return "" and the fallback to ts_now meaning ALL
// nodes with a missing "ts" field appeared recent, over-including stale
// content. With the 14-day window, this amplification was significant.
// Fix: read created_at first, fall back to updated_at, then default to 0
// (same as handle_chat). A ts of 0 always fails the cutoff check, so nodes
// missing both timestamp fields are conservatively excluded rather than
// blindly included.
let aff_ca: String = json_get(aff_node, "created_at")
let aff_ts_str: String = if str_eq(aff_ca, "") { json_get(aff_node, "updated_at") } else { aff_ca }
let aff_ts: Int = if str_eq(aff_ts_str, "") { 0 } else { str_to_int(aff_ts_str) }
let aff_ts_str: String = json_get(aff_node, "ts")
let aff_ts: Int = if str_eq(aff_ts_str, "") { ts_now } else { str_to_int(aff_ts_str) }
let is_recent: Bool = aff_ts >= ts_cutoff
let snip: String = if str_len(aff_content) > 200 { str_slice(aff_content, 0, 200) } else { aff_content }
let aff_ctx = if is_recent && !str_eq(snip, "") {