|
|
|
@@ -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 }
|
|
|
|
|
}
|
|
|
|
@@ -148,23 +116,247 @@ fn engram_compile_ranked(nodes_json: String, max_nodes: Int) -> String {
|
|
|
|
|
return c9
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
// is_followup_phrase — returns true when the message is a recognized follow-up
|
|
|
|
|
// reference that should anchor recall to the prior user topic rather than stand alone.
|
|
|
|
|
// Used by build_activation_seed to choose the right enrichment strategy.
|
|
|
|
|
fn is_followup_phrase(msg: String) -> Bool {
|
|
|
|
|
if str_contains(msg, "tell me more") { return true }
|
|
|
|
|
if str_contains(msg, "elaborate") { return true }
|
|
|
|
|
if str_contains(msg, "go on") { return true }
|
|
|
|
|
if str_contains(msg, "what about that") { return true }
|
|
|
|
|
if str_contains(msg, "what else") { return true }
|
|
|
|
|
if str_contains(msg, "keep going") { return true }
|
|
|
|
|
if str_contains(msg, "continue") { return true }
|
|
|
|
|
if str_contains(msg, "more detail") { return true }
|
|
|
|
|
if str_contains(msg, "last part") { return true }
|
|
|
|
|
if str_contains(msg, "say more") { return true }
|
|
|
|
|
if str_eq(msg, "ok") { return true }
|
|
|
|
|
if str_eq(msg, "yes") { return true }
|
|
|
|
|
if str_eq(msg, "yeah") { return true }
|
|
|
|
|
if str_eq(msg, "and?") { return true }
|
|
|
|
|
if str_eq(msg, "so?") { return true }
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// is_genuine_continuation — returns true when a short message is a contextual
|
|
|
|
|
// follow-up rather than a new topic. Fixes Issue 2: the old threshold (str_len < 50)
|
|
|
|
|
// conflated new-topic short messages like "explain quantum tunneling" (49 chars)
|
|
|
|
|
// with genuine follow-ups like "ok", "yes", or "what do you think?".
|
|
|
|
|
fn is_genuine_continuation(msg: String, hist_len: Int) -> Bool {
|
|
|
|
|
if hist_len == 0 { return false }
|
|
|
|
|
if str_len(msg) == 0 { return false }
|
|
|
|
|
if is_followup_phrase(msg) { return true }
|
|
|
|
|
if str_len(msg) >= 50 { return false }
|
|
|
|
|
let rest: String = str_slice(msg, 1, str_len(msg))
|
|
|
|
|
let has_mid_capital: Bool = false
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " A")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " B")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " C")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " D")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " E")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " F")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " G")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " H")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " I")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " J")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " K")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " L")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " M")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " N")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " O")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " P")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " Q")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " R")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " S")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " T")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " U")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " V")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " W")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " X")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " Y")
|
|
|
|
|
let has_mid_capital = has_mid_capital || str_contains(rest, " Z")
|
|
|
|
|
if has_mid_capital { return false }
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// topic_snip_from_entry — extract the most salient snippet from a history entry's
|
|
|
|
|
// content. Fixes Issue 9: takes the TAIL (last 200 chars) then trims to the last
|
|
|
|
|
// sentence boundary, so named concepts introduced near the end are captured.
|
|
|
|
|
fn topic_snip_from_entry(content: String) -> String {
|
|
|
|
|
let clen: Int = str_len(content)
|
|
|
|
|
if clen <= 200 { return content }
|
|
|
|
|
let tail: String = str_slice(content, clen - 200, clen)
|
|
|
|
|
let last_boundary: Int = -1
|
|
|
|
|
let si: Int = 0
|
|
|
|
|
let tail_len: Int = str_len(tail)
|
|
|
|
|
while si < tail_len - 1 {
|
|
|
|
|
let ch2: String = str_slice(tail, si, si + 2)
|
|
|
|
|
let is_boundary: Bool = str_eq(ch2, ". ") || str_eq(ch2, ".\n")
|
|
|
|
|
let last_boundary = if is_boundary { si } else { last_boundary }
|
|
|
|
|
let si = si + 1
|
|
|
|
|
}
|
|
|
|
|
let clean_tail: String = if last_boundary >= 0 {
|
|
|
|
|
str_slice(tail, last_boundary + 2, tail_len)
|
|
|
|
|
} else { tail }
|
|
|
|
|
if str_len(clean_tail) > 150 { return str_slice(clean_tail, 0, 150) }
|
|
|
|
|
return clean_tail
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// multi_turn_topic — build a combined topic string from recent user turns in history.
|
|
|
|
|
// Fixes Issue 10: pulls up to 3 prior user turns into the seed so earlier
|
|
|
|
|
// high-salience nodes from the thread are re-queried.
|
|
|
|
|
fn multi_turn_topic(hist: String, hist_len: Int) -> String {
|
|
|
|
|
if hist_len == 0 { return "" }
|
|
|
|
|
let topic: String = ""
|
|
|
|
|
let collected: Int = 0
|
|
|
|
|
let idx: Int = hist_len - 1
|
|
|
|
|
while idx >= 0 && collected < 3 {
|
|
|
|
|
let entry: String = json_array_get(hist, idx)
|
|
|
|
|
let role: String = json_get(entry, "role")
|
|
|
|
|
let content: String = json_get(entry, "content")
|
|
|
|
|
let is_user: Bool = str_eq(role, "user")
|
|
|
|
|
let snip: String = if str_len(content) > 100 { str_slice(content, 0, 100) } else { content }
|
|
|
|
|
let topic = if is_user && !str_eq(snip, "") {
|
|
|
|
|
if str_eq(topic, "") { snip } else { snip + " " + topic }
|
|
|
|
|
} else { topic }
|
|
|
|
|
let collected = if is_user { collected + 1 } else { collected }
|
|
|
|
|
let idx = idx - 1
|
|
|
|
|
}
|
|
|
|
|
if str_len(topic) > 300 { return str_slice(topic, 0, 300) }
|
|
|
|
|
return topic
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// distill_transcript — extract salient content from a multi-turn transcript.
|
|
|
|
|
// Fixes Issue 6: a full transcript produces a diffuse embedding query.
|
|
|
|
|
// Strategy: last 150 chars (recency) + any question in last 500 chars. Cap 250.
|
|
|
|
|
fn distill_transcript(transcript: String) -> String {
|
|
|
|
|
if str_len(transcript) <= 250 { return transcript }
|
|
|
|
|
let tlen: Int = str_len(transcript)
|
|
|
|
|
let tail_start: Int = if tlen > 500 { tlen - 500 } else { 0 }
|
|
|
|
|
let tail: String = str_slice(transcript, tail_start, tlen)
|
|
|
|
|
let tail_len: Int = str_len(tail)
|
|
|
|
|
let q_pos: Int = -1
|
|
|
|
|
let qi: Int = 0
|
|
|
|
|
while qi < tail_len {
|
|
|
|
|
let qch: String = str_slice(tail, qi, qi + 1)
|
|
|
|
|
let q_pos = if str_eq(qch, "?") { qi } else { q_pos }
|
|
|
|
|
let qi = qi + 1
|
|
|
|
|
}
|
|
|
|
|
let q_context: String = if q_pos > 0 {
|
|
|
|
|
let q_start: Int = if q_pos > 100 { q_pos - 100 } else { 0 }
|
|
|
|
|
str_slice(tail, q_start, q_pos + 1)
|
|
|
|
|
} else { "" }
|
|
|
|
|
let recency_seed: String = if tail_len > 150 {
|
|
|
|
|
str_slice(tail, tail_len - 150, tail_len)
|
|
|
|
|
} else { tail }
|
|
|
|
|
let combined: String = if str_eq(q_context, "") {
|
|
|
|
|
recency_seed
|
|
|
|
|
} else {
|
|
|
|
|
if str_contains(recency_seed, q_context) { recency_seed }
|
|
|
|
|
else { q_context + " " + recency_seed }
|
|
|
|
|
}
|
|
|
|
|
if str_len(combined) > 250 {
|
|
|
|
|
return str_slice(combined, str_len(combined) - 250, str_len(combined))
|
|
|
|
|
}
|
|
|
|
|
return combined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// build_activation_seed — construct an enriched activation seed from the current
|
|
|
|
|
// message and conversation history. Central fix for Issues 1-3, 8-10.
|
|
|
|
|
fn build_activation_seed(message: String, hist: String, hist_len: Int) -> String {
|
|
|
|
|
if hist_len == 0 { return message }
|
|
|
|
|
|
|
|
|
|
let is_cont: Bool = is_genuine_continuation(message, hist_len)
|
|
|
|
|
|
|
|
|
|
if !is_cont {
|
|
|
|
|
let multi_topic: String = multi_turn_topic(hist, hist_len)
|
|
|
|
|
if str_eq(multi_topic, "") { return message }
|
|
|
|
|
let blended: String = message + " " + multi_topic
|
|
|
|
|
if str_len(blended) > 400 { return str_slice(blended, 0, 400) }
|
|
|
|
|
return blended
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Genuine continuation: find the most recent prior USER turn as the topic anchor.
|
|
|
|
|
// Fixes Issues 3 and 8: old code used the last assistant reply (hist_len - 1).
|
|
|
|
|
let prior_user_content: String = ""
|
|
|
|
|
let scan_idx: Int = hist_len - 1
|
|
|
|
|
let found_prior_user: Bool = false
|
|
|
|
|
while scan_idx >= 0 && !found_prior_user {
|
|
|
|
|
let scan_entry: String = json_array_get(hist, scan_idx)
|
|
|
|
|
let scan_role: String = json_get(scan_entry, "role")
|
|
|
|
|
let scan_content: String = json_get(scan_entry, "content")
|
|
|
|
|
let is_user_turn: Bool = str_eq(scan_role, "user")
|
|
|
|
|
let prior_user_content = if is_user_turn && !found_prior_user { scan_content } else { prior_user_content }
|
|
|
|
|
let found_prior_user = if is_user_turn { true } else { found_prior_user }
|
|
|
|
|
let scan_idx = scan_idx - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Secondary: tail-biased snip from last assistant reply (Issue 9 fix).
|
|
|
|
|
let last_asst_entry: String = json_array_get(hist, hist_len - 1)
|
|
|
|
|
let last_asst_role: String = json_get(last_asst_entry, "role")
|
|
|
|
|
let last_asst_content: String = if str_eq(last_asst_role, "assistant") {
|
|
|
|
|
json_get(last_asst_entry, "content")
|
|
|
|
|
} else { "" }
|
|
|
|
|
let asst_snip: String = if str_eq(last_asst_content, "") { "" } else {
|
|
|
|
|
topic_snip_from_entry(last_asst_content)
|
|
|
|
|
}
|
|
|
|
|
let user_snip: String = if str_len(prior_user_content) > 150 {
|
|
|
|
|
str_slice(prior_user_content, 0, 150)
|
|
|
|
|
} else { prior_user_content }
|
|
|
|
|
|
|
|
|
|
let seed: String = if !str_eq(user_snip, "") {
|
|
|
|
|
if !str_eq(asst_snip, "") {
|
|
|
|
|
user_snip + " " + asst_snip + " " + message
|
|
|
|
|
} else {
|
|
|
|
|
user_snip + " " + message
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if !str_eq(asst_snip, "") { asst_snip + " " + message } else { message }
|
|
|
|
|
}
|
|
|
|
|
if str_len(seed) > 400 { return str_slice(seed, 0, 400) }
|
|
|
|
|
return seed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// engram_compile_multi — fan-out activation across multiple query seeds. Fixes Issue 4:
|
|
|
|
|
// only a single seed was tried per turn, with no entity/emotion/topic diversification.
|
|
|
|
|
fn engram_compile_multi(primary_seed: String, message: String) -> String {
|
|
|
|
|
let ctx1: String = engram_compile(primary_seed)
|
|
|
|
|
|
|
|
|
|
let entity_seed_differs: Bool = !str_eq(primary_seed, message)
|
|
|
|
|
let ctx2: String = if entity_seed_differs {
|
|
|
|
|
let raw_ctx: String = engram_compile(message)
|
|
|
|
|
if str_eq(raw_ctx, "") { "" } else { raw_ctx }
|
|
|
|
|
} else { "" }
|
|
|
|
|
|
|
|
|
|
let has_any: Bool = !str_eq(ctx1, "") || !str_eq(ctx2, "")
|
|
|
|
|
let ctx3: String = if has_any {
|
|
|
|
|
let emo_results: String = engram_search_json("emotion feeling mood care distress joy hope", 5)
|
|
|
|
|
let emo_ok: Bool = !str_eq(emo_results, "") && !str_eq(emo_results, "[]")
|
|
|
|
|
if emo_ok { engram_compile_ranked(emo_results, 3) } else { "" }
|
|
|
|
|
} else { "" }
|
|
|
|
|
|
|
|
|
|
let merged: String = ctx1
|
|
|
|
|
let sep2: String = if !str_eq(merged, "") && !str_eq(ctx2, "") { "\n" } else { "" }
|
|
|
|
|
let merged = if !str_eq(ctx2, "") && !str_contains(ctx1, ctx2) {
|
|
|
|
|
merged + sep2 + ctx2
|
|
|
|
|
} else { merged }
|
|
|
|
|
let sep3: String = if !str_eq(merged, "") && !str_eq(ctx3, "") { "\n" } else { "" }
|
|
|
|
|
let merged = if !str_eq(ctx3, "") && !str_contains(merged, ctx3) {
|
|
|
|
|
merged + sep3 + ctx3
|
|
|
|
|
} else { merged }
|
|
|
|
|
|
|
|
|
|
if str_eq(merged, "") { return "" }
|
|
|
|
|
if str_len(merged) > 6000 { return str_slice(merged, 0, 6000) }
|
|
|
|
|
return merged
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, "[]")
|
|
|
|
|
let act_ok: Bool = !str_eq(activate_json, "") && !str_eq(activate_json, "[]")
|
|
|
|
|
let srch_ok: Bool = !str_eq(search_json, "") && !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 { "" }
|
|
|
|
@@ -174,37 +366,23 @@ fn engram_compile(intent: String) -> String {
|
|
|
|
|
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.
|
|
|
|
|
// 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 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))
|
|
|
|
|
}
|
|
|
|
|
// 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
|
|
|
|
|
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 {
|
|
|
|
|
""
|
|
|
|
|
}
|
|
|
|
|
let scan_ok: Bool = !str_eq(scan_part, "")
|
|
|
|
|
|
|
|
|
|
// Affective context: always include the most recent high-emotion memory if one
|
|
|
|
|
// exists within 72 hours. This ensures continuity of care across turns — when
|
|
|
|
@@ -234,37 +412,36 @@ 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
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
// Cache bell and activation results for handle_chat reuse (Issues 2, 7).
|
|
|
|
|
state_set("engram_compile_bell_node", recent_bell)
|
|
|
|
|
state_set("engram_compile_activation_json", if act_ok { activate_json } else { "[]" })
|
|
|
|
|
|
|
|
|
|
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 "" }
|
|
|
|
|
|
|
|
|
|
// Raise the cap slightly to match the ranked (higher-signal) output.
|
|
|
|
|
if str_len(ctx) > 6000 {
|
|
|
|
|
return str_slice(ctx, 0, 6000)
|
|
|
|
|
// Fix Issue 6: cap at a clean JSON object boundary.
|
|
|
|
|
let cap_len: Int = 6000
|
|
|
|
|
if str_len(ctx) <= cap_len { return ctx }
|
|
|
|
|
let cap_search: Int = cap_len - 1
|
|
|
|
|
let cap_min: Int = if cap_len > 500 { cap_len - 500 } else { 0 }
|
|
|
|
|
let cap_pos: Int = -1
|
|
|
|
|
let cap_si: Int = cap_search
|
|
|
|
|
while cap_si >= cap_min && cap_pos < 0 {
|
|
|
|
|
let cap_ch: String = str_slice(ctx, cap_si, cap_si + 1)
|
|
|
|
|
let cap_pos = if str_eq(cap_ch, "}") { cap_si } else { cap_pos }
|
|
|
|
|
let cap_si = if cap_pos < 0 { cap_si - 1 } else { cap_si }
|
|
|
|
|
}
|
|
|
|
|
return ctx
|
|
|
|
|
if cap_pos > 0 { return str_slice(ctx, 0, cap_pos + 1) }
|
|
|
|
|
return str_slice(ctx, 0, cap_len)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn json_safe(s: String) -> String {
|
|
|
|
@@ -297,33 +474,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,118 +594,58 @@ 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.
|
|
|
|
|
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
|
|
|
|
|
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 }
|
|
|
|
|
let activation_seed: String = if !str_eq(thread_snip, "") {
|
|
|
|
|
thread_snip + " " + message
|
|
|
|
|
} else {
|
|
|
|
|
message
|
|
|
|
|
}
|
|
|
|
|
// Issues 2-3, 8-10 fix: build_activation_seed() replaces the raw 50-char threshold
|
|
|
|
|
// with smart continuation detection, prior-user-topic anchoring, multi-turn context,
|
|
|
|
|
// and tail-biased snipping from long assistant replies.
|
|
|
|
|
let activation_seed: String = build_activation_seed(message, stored_hist, hist_len)
|
|
|
|
|
|
|
|
|
|
// Cross-session affective context: on session start (no history yet), check engram
|
|
|
|
|
// for recent distress signals within 72h and prepend a care directive if found.
|
|
|
|
|
// Fix Issue 2: reuse cached bell result from engram_compile — no second engram query.
|
|
|
|
|
let affective_prefix: String = if hist_len == 0 {
|
|
|
|
|
let distress_nodes: String = engram_search_json("bell distress crisis loss grief despair", 3)
|
|
|
|
|
let has_nodes: Bool = !str_eq(distress_nodes, "") && !str_eq(distress_nodes, "[]")
|
|
|
|
|
let now_ts: Int = time_now()
|
|
|
|
|
let cutoff: Int = now_ts - 259200
|
|
|
|
|
let found_recent: Bool = if has_nodes {
|
|
|
|
|
let dn0: String = json_array_get(distress_nodes, 0)
|
|
|
|
|
let ts0_raw: String = json_get(dn0, "created_at")
|
|
|
|
|
let ts0_str: String = if str_eq(ts0_raw, "") { json_get(dn0, "updated_at") } else { ts0_raw }
|
|
|
|
|
let ts0: Int = if str_eq(ts0_str, "") { 0 } else { str_to_int(ts0_str) }
|
|
|
|
|
ts0 > cutoff
|
|
|
|
|
} else { false }
|
|
|
|
|
if found_recent {
|
|
|
|
|
let cached_bell: String = state_get("engram_compile_bell_node")
|
|
|
|
|
if !str_eq(cached_bell, "") {
|
|
|
|
|
"[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]\n\n"
|
|
|
|
|
} else { "" }
|
|
|
|
|
} else { "" }
|
|
|
|
|
|
|
|
|
|
let ctx: String = engram_compile(activation_seed)
|
|
|
|
|
// Issue 4 fix: engram_compile_multi adds entity + emotion fan-out seeds
|
|
|
|
|
let ctx: String = engram_compile_multi(activation_seed, message)
|
|
|
|
|
let system: String = affective_prefix + build_system_prompt(ctx)
|
|
|
|
|
|
|
|
|
|
// First message of the session: proactively load user profile and active work context.
|
|
|
|
@@ -658,18 +754,14 @@ fn handle_chat(body: String) -> String {
|
|
|
|
|
state_set("conv_history", final_hist)
|
|
|
|
|
conv_history_persist(final_hist)
|
|
|
|
|
|
|
|
|
|
let activation_nodes: String = engram_activate_json(message, 2)
|
|
|
|
|
let act_ok: Bool = !str_eq(activation_nodes, "") && !str_eq(activation_nodes, "[]")
|
|
|
|
|
let act_out: String = if act_ok { activation_nodes } else { "[]" }
|
|
|
|
|
// Fix Issue 7: reuse activation JSON from engram_compile — no third activate query.
|
|
|
|
|
let cached_act: String = state_get("engram_compile_activation_json")
|
|
|
|
|
let act_out: String = if !str_eq(cached_act, "") && !str_eq(cached_act, "[]") {
|
|
|
|
|
cached_act
|
|
|
|
|
} 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 {
|
|
|
|
@@ -1112,7 +1204,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,13 +1230,15 @@ 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
|
|
|
|
|
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 }
|
|
|
|
|
let ag_seed: String = if !str_eq(ag_thread_snip, "") { ag_thread_snip + " " + message } else { message }
|
|
|
|
|
|
|
|
|
|
let ctx: String = engram_compile(ag_seed)
|
|
|
|
|
// Issues 2-5, 8-10 fix: build_activation_seed for smart continuation/multi-turn.
|
|
|
|
|
// Issue 5 fix: workspace_root appended so agent activation is workspace-aware.
|
|
|
|
|
let ag_seed_base: String = build_activation_seed(message, agentic_hist, agentic_hist_len)
|
|
|
|
|
let ag_workspace_root: String = agent_workspace_root()
|
|
|
|
|
let ag_seed: String = if !str_eq(ag_workspace_root, "") {
|
|
|
|
|
ag_seed_base + " workspace:" + ag_workspace_root
|
|
|
|
|
} else { ag_seed_base }
|
|
|
|
|
// Issue 4 fix: multi-seed fan-out (entity + emotion)
|
|
|
|
|
let ctx: String = engram_compile_multi(ag_seed, message)
|
|
|
|
|
let identity: String = state_get("soul_identity")
|
|
|
|
|
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct.\n\n" + ctx
|
|
|
|
|
|
|
|
|
@@ -1534,7 +1628,8 @@ fn handle_dharma_room_turn(body: String) -> String {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The soul's own memories, activated by what it's reading — not injected.
|
|
|
|
|
let engram_ctx: String = engram_compile(transcript)
|
|
|
|
|
// Issue 6 fix: distill_transcript() reduces diffuse embedding noise
|
|
|
|
|
let engram_ctx: String = engram_compile(distill_transcript(transcript))
|
|
|
|
|
let system_prompt: String = if str_eq(engram_ctx, "") {
|
|
|
|
|
identity
|
|
|
|
|
} else {
|
|
|
|
@@ -1586,7 +1681,8 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
|
|
|
|
|
return "{\"error\":\"transcript is required\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let ctx: String = engram_compile(transcript)
|
|
|
|
|
// Issue 6 fix: distill_transcript() reduces diffuse embedding noise
|
|
|
|
|
let ctx: String = engram_compile(distill_transcript(transcript))
|
|
|
|
|
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct and stay in character.\n\n" + ctx
|
|
|
|
|
|
|
|
|
|
let api_key: String = agentic_api_key()
|
|
|
|
@@ -1674,13 +1770,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
|
|
|
|
|