|
|
|
@@ -12,66 +12,34 @@ fn chat_default_model() -> String {
|
|
|
|
|
return "claude-sonnet-4-5"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// engram_numeric_valid — guard for str_to_int: returns true only when s is a valid
|
|
|
|
|
// decimal number (integer or single-decimal-point float, optional leading minus).
|
|
|
|
|
// Q1 fix: rejects "", "null", "N/A", multi-dot strings ("1.2.3"), pure-letter strings.
|
|
|
|
|
// Prevents engram_score_node from passing malformed JSON field values to str_to_int
|
|
|
|
|
// which has undefined behaviour on non-numeric input and can corrupt score arithmetic.
|
|
|
|
|
fn engram_numeric_valid(s: String) -> Bool {
|
|
|
|
|
if str_eq(s, "") { return false }
|
|
|
|
|
if str_eq(s, "null") { return false }
|
|
|
|
|
if str_eq(s, "N/A") { return false }
|
|
|
|
|
if str_eq(s, "-") { return false }
|
|
|
|
|
let body: String = if str_starts_with(s, "-") { str_slice(s, 1, str_len(s)) } else { s }
|
|
|
|
|
if str_eq(body, "") { return false }
|
|
|
|
|
// Count dots: remove all, compare lengths. Allow at most one dot (float).
|
|
|
|
|
let no_dot: String = str_replace(body, ".", "")
|
|
|
|
|
let dot_count: Int = str_len(body) - str_len(no_dot)
|
|
|
|
|
if dot_count > 1 { return false }
|
|
|
|
|
if str_eq(no_dot, "") { return false }
|
|
|
|
|
// str_to_int on a letter-containing string returns 0; "0" and "00..." (e.g. from "0.0")
|
|
|
|
|
// are valid zeros. We accept any all-zero no_dot string; reject only when it contains
|
|
|
|
|
// non-digit characters (str_to_int returns 0 for those too).
|
|
|
|
|
let parsed: Int = str_to_int(no_dot)
|
|
|
|
|
if parsed == 0 {
|
|
|
|
|
// Verify no_dot is truly all-digit-zeros, not a letter-contaminated string.
|
|
|
|
|
// Strip all '0' characters; if anything remains the string is non-numeric.
|
|
|
|
|
let stripped_zeros: String = str_replace(no_dot, "0", "")
|
|
|
|
|
if !str_eq(stripped_zeros, "") { return false }
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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_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.
|
|
|
|
|
// Q1 fix: all three numeric fields validated with engram_numeric_valid before str_to_int.
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
// Q1 fix: validate before str_to_int. Non-numeric values fall back to safe defaults.
|
|
|
|
|
// Parse as floats via * 100 integer arithmetic (el has no float math).
|
|
|
|
|
let salience_100: Int = if !engram_numeric_valid(salience_str) { 70 } else {
|
|
|
|
|
// 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 !engram_numeric_valid(importance_str) { 70 } else {
|
|
|
|
|
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 !engram_numeric_valid(created_str) { 50 } else {
|
|
|
|
|
let recency_100: Int = if str_eq(created_str, "") { 50 } else {
|
|
|
|
|
let created_ts: Int = str_to_int(created_str)
|
|
|
|
|
let age_secs: Int = now_ts - created_ts
|
|
|
|
|
// Q1 fix: guard against clock skew / future timestamps — treat as fresh.
|
|
|
|
|
let age_days: Int = if age_secs < 0 { 0 } else { age_secs / 86400 }
|
|
|
|
|
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 }
|
|
|
|
|
}
|
|
|
|
@@ -105,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)
|
|
|
|
@@ -145,83 +114,289 @@ 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,", "")
|
|
|
|
|
return c14
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Q4 note: engram_compile has no cache or circuit-breaker at the EL layer.
|
|
|
|
|
// Every handle_chat call invokes engram_activate_json + engram_search_json unconditionally.
|
|
|
|
|
// If the engram backend is repeatedly unreachable (e.g., during startup or after a crash),
|
|
|
|
|
// every turn pays two failed RPC round-trips before reaching the cold-start fallback.
|
|
|
|
|
// A proper cache/circuit-breaker requires C runtime support (e.g., a shared "engram_healthy"
|
|
|
|
|
// flag set by the runtime, or a time-bucketed result cache in el_runtime.c). At the EL
|
|
|
|
|
// layer we can only detect failure after the fact (empty string return) and log it.
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
// Q6/Q7 fix: track raw "" (engram down) vs "[]" (empty graph) to surface different warnings.
|
|
|
|
|
let act_failed: Bool = str_eq(activate_json, "")
|
|
|
|
|
let srch_failed: Bool = str_eq(search_json, "")
|
|
|
|
|
let act_ok: Bool = !act_failed && !str_eq(activate_json, "[]")
|
|
|
|
|
let srch_ok: Bool = !srch_failed && !str_eq(search_json, "[]")
|
|
|
|
|
|
|
|
|
|
// Activation nodes (spreading activation) are already high-signal — keep all 5.
|
|
|
|
|
let act_part: String = if act_ok { activate_json } else { "" }
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
// Q2 fix: soul-agnostic cold-start fallback. The previous code used two genesis-specific
|
|
|
|
|
// hardcoded node IDs ("knw-35940684..." and "knw-729fc901..."). Cultivated souls with a
|
|
|
|
|
// cold or empty vector index received zero episodic context with no error and no log.
|
|
|
|
|
// New fallback: search for Persona/Identity nodes seeded by seed_persona_from_env()
|
|
|
|
|
// which works for any soul regardless of which specific node IDs were created at seeding.
|
|
|
|
|
// Q6 fix: log a warning so the empty-recall path is visible in operator logs.
|
|
|
|
|
let scan_part: String = if !act_ok && !srch_ok {
|
|
|
|
|
let engram_down: Bool = act_failed && srch_failed
|
|
|
|
|
if engram_down {
|
|
|
|
|
println("[chat] engram_compile: WARN engram_down — all calls returned empty string for intent=" + str_slice(intent, 0, 60))
|
|
|
|
|
} else {
|
|
|
|
|
println("[chat] engram_compile: WARN cold-index — activation and search returned no results for intent=" + str_slice(intent, 0, 60))
|
|
|
|
|
// 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 { "" }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Soul-agnostic fallback: fetch the Persona node by label — immune to cold vector index.
|
|
|
|
|
// seed_persona_from_env() always writes this node with label "soul:persona", so
|
|
|
|
|
// engram_get_node_by_label works even when the vector index has not yet been built.
|
|
|
|
|
// Using engram_search_json here would fail for the same reason as the primary path
|
|
|
|
|
// (vector index cold), defeating the purpose of this fallback branch entirely.
|
|
|
|
|
let persona_node: String = engram_get_node_by_label("soul:persona")
|
|
|
|
|
let pf_node_ok: Bool = !str_eq(persona_node, "") && !str_eq(persona_node, "null")
|
|
|
|
|
let persona_arr: String = if pf_node_ok { "[" + persona_node + "]" } else { "" }
|
|
|
|
|
let pf_ok: Bool = pf_node_ok
|
|
|
|
|
let combined: String = if pf_ok { engram_compile_ranked(persona_arr, 1) } else { "" }
|
|
|
|
|
if str_eq(combined, "") {
|
|
|
|
|
println("[chat] engram_compile: WARN cold-start fallback also empty — LLM has no episodic context")
|
|
|
|
|
}
|
|
|
|
|
combined
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
}
|
|
|
|
|
let scan_ok: Bool = !str_eq(scan_part, "")
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Affective context: always include the most recent high-emotion memory if one
|
|
|
|
|
// exists within 72 hours. This ensures continuity of care across turns — when
|
|
|
|
|
// the user was in distress earlier in the session (or recently), that context
|
|
|
|
|
// travels into every subsequent LLM call so the response register stays aware.
|
|
|
|
|
// We search for BellEvent nodes specifically; these are written by auto_persist
|
|
|
|
|
// when safety_detect_bell_level fires. The 72h window (259200 seconds) is wide
|
|
|
|
|
// enough to span a multi-session day without pulling ancient history.
|
|
|
|
|
// 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 {
|
|
|
|
|
let entity_count = entity_count + 1
|
|
|
|
|
if str_eq(entities, "") { word } else { entities + "\n" + word }
|
|
|
|
|
} else { entities }
|
|
|
|
|
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 — semantic continuation detection replacing the brittle 50-char
|
|
|
|
|
// threshold. Returns true when message starts with a pronoun, continuation opener, or is
|
|
|
|
|
// < 80 chars (raised from 50 to catch "Can you remind me what Prism's architecture
|
|
|
|
|
// looks like?" at 57 chars which is clearly a continuation in an active thread).
|
|
|
|
|
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 }
|
|
|
|
|
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 ")
|
|
|
|
|
|| str_starts_with(message, "What about") || str_starts_with(message, "what about")
|
|
|
|
|
|| str_starts_with(message, "Why ") || str_starts_with(message, "why ")
|
|
|
|
|
|| str_starts_with(message, "How ") || str_starts_with(message, "how ")
|
|
|
|
|
|| str_starts_with(message, "When ") || str_starts_with(message, "when ")
|
|
|
|
|
if is_cont_opener { return true }
|
|
|
|
|
if str_len(message) < 80 { 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 + "]")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 + ",")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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_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
|
|
|
|
|
}
|
|
|
|
|
return ids
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn engram_compile(intent: String) -> String {
|
|
|
|
|
// 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")
|
|
|
|
|
|
|
|
|
|
// Issue 4: detect explicit recall intent and run boosted search.
|
|
|
|
|
let is_recall_intent: Bool = engram_detect_recall_intent(intent)
|
|
|
|
|
|
|
|
|
|
// 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, "")
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
|
|
// 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 { "" }
|
|
|
|
|
|
|
|
|
|
// 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 { "" }
|
|
|
|
|
|
|
|
|
|
// 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, 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 - 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)
|
|
|
|
@@ -234,39 +409,48 @@ fn engram_compile(intent: String) -> String {
|
|
|
|
|
let ca: String = json_get(bn0, "created_at")
|
|
|
|
|
if str_eq(ca, "") { json_get(bn0, "updated_at") } else { ca }
|
|
|
|
|
}
|
|
|
|
|
// Q1 fix: validate bell timestamp before str_to_int.
|
|
|
|
|
let bn_ts: Int = if !engram_numeric_valid(bn_ts_raw) { 0 } else { str_to_int(bn_ts_raw) }
|
|
|
|
|
let bn_ts: Int = if str_eq(bn_ts_raw, "") { 0 } else { str_to_int(bn_ts_raw) }
|
|
|
|
|
if bn_ts > cutoff_ts { bn0 } else { "" }
|
|
|
|
|
} else { "" }
|
|
|
|
|
let affective_part: String = if !str_eq(recent_bell, "") { recent_bell } else { "" }
|
|
|
|
|
let affective_ok: Bool = !str_eq(affective_part, "")
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
// Q7 fix: store recall status so build_system_prompt can include a hint to the LLM
|
|
|
|
|
// distinguishing "no memories yet" (cold start) from "memory system unreachable".
|
|
|
|
|
// Values: "ok" | "empty" | "unavailable"
|
|
|
|
|
let any_ok: Bool = act_ok || srch_ok || scan_ok || affective_ok
|
|
|
|
|
let all_failed: Bool = act_failed && srch_failed
|
|
|
|
|
let recall_status: String = if any_ok { "ok" } else { if all_failed { "unavailable" } else { "empty" } }
|
|
|
|
|
state_set("engram_recall_status", recall_status)
|
|
|
|
|
// 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, "") {
|
|
|
|
|
// Q6 fix: log when ctx is empty after all recall paths so cold-start is visible.
|
|
|
|
|
println("[chat] engram_compile: all paths empty — recall_status=" + recall_status + " intent=" + str_slice(intent, 0, 60))
|
|
|
|
|
return ""
|
|
|
|
|
if str_eq(ctx, "") { return "" }
|
|
|
|
|
|
|
|
|
|
// Issue 7 fix: safe JSON truncation — find last closing brace before budget cap.
|
|
|
|
|
// Budget raised from 6000 to 8000 for the larger multi-topic pool.
|
|
|
|
|
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 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Raise the cap slightly to match the ranked (higher-signal) output.
|
|
|
|
|
if str_len(ctx) > 6000 {
|
|
|
|
|
return str_slice(ctx, 0, 6000)
|
|
|
|
|
}
|
|
|
|
|
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, "[") { return truncated + "]" }
|
|
|
|
|
return truncated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn json_safe(s: String) -> String {
|
|
|
|
|
let s1: String = str_replace(s, "\\", "\\\\")
|
|
|
|
|
let s2: String = str_replace(s1, "\"", "\\\"")
|
|
|
|
@@ -297,33 +481,12 @@ fn build_system_prompt(ctx: String) -> String {
|
|
|
|
|
"\n\n[IDENTITY GRAPH — who you are, loaded from your engram]\n" + id_ctx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Q7 fix: if recall produced no results, include a hint so the LLM can respond
|
|
|
|
|
// authentically ("I seem to be starting fresh" vs "memory system may be down")
|
|
|
|
|
// rather than silently acting as if it has context it doesn't have.
|
|
|
|
|
// Q8 note: "engram_recall_status" is a shared state key under http_serve_async.
|
|
|
|
|
// Concurrent requests can overwrite each other's status. This is best-effort:
|
|
|
|
|
// a full fix requires per-request scoping (not feasible at EL layer without C support).
|
|
|
|
|
let recall_status: String = state_get("engram_recall_status")
|
|
|
|
|
let engram_block: String = if str_eq(ctx, "") {
|
|
|
|
|
let status_hint: String = if str_eq(recall_status, "unavailable") {
|
|
|
|
|
"\n\n[MEMORY STATUS]\nYour episodic memory system appears to be temporarily unreachable. You may not have access to memories from previous sessions. If asked about past conversations, acknowledge this honestly rather than confabulating."
|
|
|
|
|
} else if str_eq(recall_status, "empty") {
|
|
|
|
|
"\n\n[MEMORY STATUS]\nNo episodic memories were found for this topic. This may be a new soul or a new area of conversation. Respond naturally from your identity without fabricating memories."
|
|
|
|
|
} else {
|
|
|
|
|
""
|
|
|
|
|
}
|
|
|
|
|
status_hint
|
|
|
|
|
""
|
|
|
|
|
} else {
|
|
|
|
|
"\n\n[ENGRAM CONTEXT — compiled from your graph]\n" + ctx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Q8 note: layered_cycle_safety_system_addendum is a shared mutable state key.
|
|
|
|
|
// Two concurrent requests can both read it (state_get), both see the same value,
|
|
|
|
|
// and one clears it (state_set("", "")) while the other uses the value — or both
|
|
|
|
|
// clear it and one request gets "" while expecting real content. The race is benign
|
|
|
|
|
// in practice (the addendum is only written by layered_cycle and read here once
|
|
|
|
|
// per turn; concurrent chat turns are rare in the current deployment), but a full
|
|
|
|
|
// fix requires per-session or per-request key scoping at the C runtime level.
|
|
|
|
|
let safety_addendum: String = state_get("layered_cycle_safety_system_addendum")
|
|
|
|
|
let safety_block: String = if str_eq(safety_addendum, "") {
|
|
|
|
|
""
|
|
|
|
@@ -438,92 +601,51 @@ fn clean_llm_response(s: String) -> String {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// conv_history_persist — save conversation history to engram for cross-restart continuity.
|
|
|
|
|
// Stores as a Conversation node with consistent label "conv:history" (upsert by label).
|
|
|
|
|
// Q3/Q6 fix: added partial-write guard and failure logging.
|
|
|
|
|
// Stores as a Conversation node. Overwrites by using consistent label "conv:history".
|
|
|
|
|
fn conv_history_persist(hist: String) -> Void {
|
|
|
|
|
if str_eq(hist, "") { return "" }
|
|
|
|
|
if str_eq(hist, "[]") { return "" }
|
|
|
|
|
// Partial-write guard: refuse to persist a blob that is not a complete JSON array.
|
|
|
|
|
// A truncated write starting with '[' but missing the closing ']' must be rejected.
|
|
|
|
|
// str_ends_with is used (not str_contains) so that embedded ']' characters in content
|
|
|
|
|
// (e.g. "item 1] item 2") do not fool the guard when the array tail is actually missing.
|
|
|
|
|
if !str_starts_with(hist, "[") { return "" }
|
|
|
|
|
if !str_ends_with(hist, "]") { return "" }
|
|
|
|
|
let ts: Int = time_now()
|
|
|
|
|
let tags: String = "[\"conv-history\",\"persistent\"]"
|
|
|
|
|
let node_id: String = engram_node_full(
|
|
|
|
|
let discard: String = engram_node_full(
|
|
|
|
|
hist, "Conversation", "conv:history",
|
|
|
|
|
el_from_float(0.7), el_from_float(0.8), el_from_float(0.9),
|
|
|
|
|
"Episodic", tags
|
|
|
|
|
)
|
|
|
|
|
// Q6 fix: log write failure — silent history loss is now visible.
|
|
|
|
|
if str_eq(node_id, "") {
|
|
|
|
|
println("[chat] conv_history_persist: engram_node_full returned empty — history node may be lost")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// conv_history_load — restore conversation history from engram on first access.
|
|
|
|
|
// Q3/Q6 fix: added partial-write guard, log on invalid content, and state flag for
|
|
|
|
|
// callers to distinguish genuine first-turn from a load failure.
|
|
|
|
|
// Returns the most recent "conv:history" node content, or "" if none found.
|
|
|
|
|
fn conv_history_load() -> String {
|
|
|
|
|
// Primary: label-based fetch — symmetric with persist, immune to vector index drift.
|
|
|
|
|
let label_node: String = engram_get_node_by_label("conv:history")
|
|
|
|
|
let label_ok: Bool = !str_eq(label_node, "") && !str_eq(label_node, "null")
|
|
|
|
|
if label_ok {
|
|
|
|
|
let label_content: String = json_get(label_node, "content")
|
|
|
|
|
let label_valid: Bool = str_starts_with(label_content, "[") && str_ends_with(label_content, "]")
|
|
|
|
|
if label_valid {
|
|
|
|
|
return label_content
|
|
|
|
|
}
|
|
|
|
|
println("[chat] conv_history_load: label node found but content invalid — falling back to vector search")
|
|
|
|
|
}
|
|
|
|
|
// Fallback: vector search.
|
|
|
|
|
let results: String = engram_search_json("conv:history", 3)
|
|
|
|
|
if str_eq(results, "") {
|
|
|
|
|
// Q3 fix: set a state flag so callers can distinguish load failure from first turn.
|
|
|
|
|
state_set("conv_history_load_failed", "1")
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
if str_eq(results, "") { return "" }
|
|
|
|
|
if str_eq(results, "[]") { return "" }
|
|
|
|
|
let node: String = json_array_get(results, 0)
|
|
|
|
|
let content: String = json_get(node, "content")
|
|
|
|
|
// Partial-write guard: require both '[' prefix AND closing ']' at the tail.
|
|
|
|
|
// str_ends_with guards against embedded ']' in content fooling the check.
|
|
|
|
|
if !str_starts_with(content, "[") || !str_ends_with(content, "]") {
|
|
|
|
|
println("[chat] conv_history_load: vector search result content invalid — treating as first turn")
|
|
|
|
|
state_set("conv_history_load_failed", "1")
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
// Validate it looks like a JSON array
|
|
|
|
|
if !str_starts_with(content, "[") { return "" }
|
|
|
|
|
return content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_chat(body: String) -> String {
|
|
|
|
|
let message: String = json_get(body, "message")
|
|
|
|
|
if str_eq(message, "") {
|
|
|
|
|
return "{\"__status__\":400,\"error\":\"message is required\",\"response\":\"\"}"
|
|
|
|
|
return "{\"error\":\"message is required\",\"response\":\"\"}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load history BEFORE compiling context so we can anchor activation to the thread.
|
|
|
|
|
// Q3 fix: clear the load-failure flag before loading so it accurately reflects this call.
|
|
|
|
|
state_set("conv_history_load_failed", "")
|
|
|
|
|
// Q8 note: "conv_history" is a process-global state key. Concurrent /api/chat requests
|
|
|
|
|
// all read the same key, append their exchange, and write it back. Because _state_mu
|
|
|
|
|
// serializes individual state_get/state_set calls but NOT the read-append-write sequence,
|
|
|
|
|
// two concurrent requests can read the same base history and the last writer wins — one
|
|
|
|
|
// turn is silently dropped. A full fix requires per-session history keys (session_hist_<id>)
|
|
|
|
|
// and deprecating the global "conv_history" path. Callers using session_id are not affected.
|
|
|
|
|
// TODO(reliability #3 — conv_history global race): process-global key; concurrent
|
|
|
|
|
// /api/chat requests without session_id race on this read-append-write.
|
|
|
|
|
let state_hist: String = state_get("conv_history")
|
|
|
|
|
let stored_hist: String = if str_eq(state_hist, "") { conv_history_load() } else { state_hist }
|
|
|
|
|
let hist_load_failed: Bool = str_eq(state_get("conv_history_load_failed"), "1")
|
|
|
|
|
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 {
|
|
|
|
@@ -550,73 +672,114 @@ fn handle_chat(body: String) -> String {
|
|
|
|
|
} 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)
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
// Dedup fix: skip any node whose ID already appeared in engram_compile's output.
|
|
|
|
|
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 n0_id: String = json_get(n0, "id")
|
|
|
|
|
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, "") || 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 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, "") || 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 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, "") || id_in_seen(n2_id, seen_ids) { 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 w0_id: String = json_get(w0, "id")
|
|
|
|
|
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, "") || 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 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, "") || id_in_seen(w1_id, seen_ids) { 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 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
|
|
|
|
|
} 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 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 { "" }
|
|
|
|
@@ -663,13 +826,7 @@ fn handle_chat(body: String) -> String {
|
|
|
|
|
let act_out: String = if act_ok { activation_nodes } else { "[]" }
|
|
|
|
|
strengthen_chat_nodes(act_out)
|
|
|
|
|
|
|
|
|
|
// Q3 fix: surface history load failure in the response envelope so callers can
|
|
|
|
|
// show a "starting fresh — could not load previous conversation" indicator.
|
|
|
|
|
let hist_warning: String = if hist_load_failed {
|
|
|
|
|
",\"history_load_failed\":true"
|
|
|
|
|
} else { "" }
|
|
|
|
|
|
|
|
|
|
return "{\"response\":\"" + safe_response + "\",\"model\":\"" + model + "\",\"activation_nodes\":" + act_out + hist_warning + "}"
|
|
|
|
|
return "{\"response\":\"" + safe_response + "\",\"model\":\"" + model + "\",\"activation_nodes\":" + act_out + "}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_see(body: String) -> String {
|
|
|
|
@@ -1076,15 +1233,18 @@ fn is_builtin_tool(tool_name: String) -> Bool {
|
|
|
|
|
|| str_starts_with(tool_name, "neuron_")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// next_bridge_id — monotonic correlation id for a suspended agentic turn.
|
|
|
|
|
// Combines boot-relative time with a per-process counter so two unknown-tool
|
|
|
|
|
// suspensions in the same second still get distinct ids.
|
|
|
|
|
// next_bridge_id — unique correlation id for a suspended agentic turn.
|
|
|
|
|
// Uses uuid_v4() as the primary uniqueness guarantee — concurrent calls cannot collide.
|
|
|
|
|
//
|
|
|
|
|
// TODO(reliability #6): mcp_bridge_seq RMW is non-atomic. Now benign because
|
|
|
|
|
// uuid_v4() provides collision-free uniqueness. Counter is kept for readability only.
|
|
|
|
|
fn next_bridge_id() -> String {
|
|
|
|
|
let prev: String = state_get("mcp_bridge_seq")
|
|
|
|
|
let n: Int = if str_eq(prev, "") { 0 } else { str_to_int(prev) }
|
|
|
|
|
let next: Int = n + 1
|
|
|
|
|
state_set("mcp_bridge_seq", int_to_str(next))
|
|
|
|
|
return "br-" + int_to_str(time_now()) + "-" + int_to_str(next)
|
|
|
|
|
let uid: String = uuid_v4()
|
|
|
|
|
return "br-" + uid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_chat_agentic(body: String) -> String {
|
|
|
|
@@ -1112,7 +1272,7 @@ fn handle_chat_agentic(body: String) -> String {
|
|
|
|
|
if str_eq(screen_action, "hard_bell") {
|
|
|
|
|
safety_log_bell("hard", json_get(screen_result, "reason"), str_slice(message, 0, 80))
|
|
|
|
|
return "{\"reply\":\"" + json_safe(safety_validate("", "hard_bell")) + "\",\"model\":\"\",\"agentic\":true,\"tools_used\":[]}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let req_model: String = json_get(body, "model")
|
|
|
|
|
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
|
|
|
|
@@ -1138,7 +1298,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 }
|
|
|
|
@@ -1674,13 +1835,6 @@ fn auto_persist(req: String, resp: String) -> Void {
|
|
|
|
|
"Episodic",
|
|
|
|
|
tags
|
|
|
|
|
)
|
|
|
|
|
// CRITICAL BUG fix: log conv_node_id failure OUTSIDE the is_bell block.
|
|
|
|
|
// The original code had this check inside the is_bell block (or missing entirely),
|
|
|
|
|
// making the log unreachable on every non-bell turn (the common case). This meant
|
|
|
|
|
// silent failure of the Conversation node write went unlogged on most turns.
|
|
|
|
|
if str_eq(conv_node_id, "") {
|
|
|
|
|
println("[chat] auto_persist: engram_node_full returned empty — conversation node lost (ts=" + ts_str + ")")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When a bell fires, write a dedicated BellEvent node in addition to the
|
|
|
|
|
// Conversation node. This makes distress moments directly findable by label
|
|
|
|
|