Fix engram_compile: fetch pinned nodes directly when vector search empty

Replace scan-by-offset fallback with engram_get_node_json calls for the
known high-salience identity nodes (family, origin). Offset-based scanning
is order-dependent and unreliable; direct ID fetch is stable regardless of
snapshot position. Ensures biographical context (Fox, Bobby, etc.) is
always in the system prompt when vector search returns nothing.
This commit is contained in:
Will Anderson
2026-05-03 11:19:14 -05:00
parent 71ab7eafde
commit e299c92662
6 changed files with 52 additions and 4 deletions
+41 -4
View File
@@ -21,8 +21,28 @@ fn engram_compile(intent: String) -> String {
let act_part: String = if act_ok { activate_json } else { "" }
let srch_part: String = if srch_ok { search_json } else { "" }
let sep: String = if !str_eq(act_part, "") && !str_eq(srch_part, "") { "\n" } else { "" }
let ctx: String = act_part + sep + srch_part
// Fallback: when vector search returns nothing (no embeddings), fetch pinned
// high-salience nodes by their known IDs. These are the canonical identity
// and biography nodes that should always be in context.
// engram_get_node_json(id) returns a single node as JSON or "" if missing.
let scan_part: String = if !act_ok && !srch_ok {
let family_node: String = engram_get_node_json("knw-35940684-abc4-42f0-b942-818f66b1f69a")
let origin_node: String = engram_get_node_json("knw-729fc901-8335-44c4-9f3a-b150b4aa0915")
let fam_ok: Bool = !str_eq(family_node, "") && !str_eq(family_node, "null")
let orig_ok: Bool = !str_eq(origin_node, "") && !str_eq(origin_node, "null")
let fam_str: String = if fam_ok { family_node } else { "" }
let orig_str: String = if orig_ok { origin_node } else { "" }
let sep: String = if fam_ok && orig_ok { "\n" } else { "" }
let combined: String = fam_str + sep + orig_str
if str_eq(combined, "") { "" } else { combined }
} else {
""
}
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 ctx: String = act_part + sep1 + srch_part + sep2 + scan_part
if str_eq(ctx, "") { return "" }
@@ -80,6 +100,21 @@ fn hist_trim(hist: String) -> String {
return hist
}
// clean_llm_response strips GPT-2 BPE byte-to-unicode artifacts that vLLM
// emits when the tokenizer hasn't decoded back to raw bytes.
//
// Ġ (U+0120) = leading space on a BPE token plain space
// Ċ (U+010A) = newline byte encoded as BPE token \n
// ĉ (U+0109) = tab byte tab (rare)
//
// Applied to every LLM response before it reaches callers.
fn clean_llm_response(s: String) -> String {
let s1: String = str_replace(s, "Ġ", " ")
let s2: String = str_replace(s1, "Ċ", "\n")
let s3: String = str_replace(s2, "ĉ", "\t")
return s3
}
fn handle_chat(body: String) -> String {
let message: String = json_get(body, "message")
if str_eq(message, "") {
@@ -109,7 +144,8 @@ fn handle_chat(body: String) -> String {
return "{\"error\":\"llm unavailable\",\"response\":\"\"}"
}
let safe_response: String = json_safe(raw_response)
let clean_response: String = clean_llm_response(raw_response)
let safe_response: String = json_safe(clean_response)
let updated_hist: String = hist_append(stored_hist, "user", message)
let updated_hist2: String = hist_append(updated_hist, "assistant", raw_response)
@@ -246,7 +282,8 @@ fn handle_chat_as_soul(body: String) -> String {
return "{\"error\":\"llm unavailable\",\"response\":\"\",\"speaker_slug\":\"" + speaker + "\",\"model\":\"" + model + "\"}"
}
let safe_response: String = json_safe(raw_response)
let clean_response: String = clean_llm_response(raw_response)
let safe_response: String = json_safe(clean_response)
return "{\"response\":\"" + safe_response + "\",\"model\":\"" + model + "\",\"speaker_slug\":\"" + speaker + "\"}"
}