From aaada3770ad99cceb1a63b8404d45ca81b13786f Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Mon, 22 Jun 2026 14:03:48 -0500 Subject: [PATCH] fix(recall): deduplicate engram nodes by ID across activation and search passes engram_compile() already published seen node IDs to state via engram_compile_seen_ids but handle_chat never read or applied them. Wire up the consumption side: - Read engram_compile_seen_ids from state after engram_compile() returns - Check each session_preload candidate node (profile x3, work x2, project x2, summary x3) against id_in_seen() before emitting its content bullet - Nodes already present in the compiled engram context are skipped entirely, preventing the same high-salience identity/memory nodes from appearing 2-3x in the system prompt and burning 3000-3500 tokens on repetition --- chat.el | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/chat.el b/chat.el index 87b1714..5fef8e6 100644 --- a/chat.el +++ b/chat.el @@ -665,11 +665,15 @@ 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) // 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 work", 5) @@ -686,21 +690,24 @@ fn handle_chat(body: String) -> String { let bullets: String = "" let bullets = if pn > 0 { let n0: String = json_array_get(profile_nodes, 0) + let n0_id: String = json_get(n0, "id") let c0: String = json_get(n0, "content") let s0: String = if str_len(c0) > 120 { str_slice(c0, 0, 120) } else { c0 } - if str_eq(s0, "") { bullets } else { "- " + s0 } + if str_eq(s0, "") || id_in_seen(n0_id, seen_ids) { bullets } else { "- " + s0 } } else { bullets } let bullets = if pn > 1 { let n1: String = json_array_get(profile_nodes, 1) + let n1_id: String = json_get(n1, "id") let c1: String = json_get(n1, "content") let s1: String = if str_len(c1) > 120 { str_slice(c1, 0, 120) } else { c1 } - if str_eq(s1, "") { bullets } else { bullets + "\n- " + s1 } + if str_eq(s1, "") || id_in_seen(n1_id, seen_ids) { bullets } else { bullets + "\n- " + s1 } } else { bullets } let bullets = if pn > 2 { let n2: String = json_array_get(profile_nodes, 2) + let n2_id: String = json_get(n2, "id") let c2: String = json_get(n2, "content") let s2: String = if str_len(c2) > 120 { str_slice(c2, 0, 120) } else { c2 } - if str_eq(s2, "") { bullets } else { bullets + "\n- " + s2 } + if str_eq(s2, "") || id_in_seen(n2_id, seen_ids) { bullets } else { bullets + "\n- " + s2 } } else { bullets } bullets } else { "" } @@ -710,15 +717,17 @@ fn handle_chat(body: String) -> String { let wb: String = "" let wb = if wn > 0 { let w0: String = json_array_get(work_nodes, 0) + let w0_id: String = json_get(w0, "id") let wc0: String = json_get(w0, "content") let ws0: String = if str_len(wc0) > 120 { str_slice(wc0, 0, 120) } else { wc0 } - if str_eq(ws0, "") { wb } else { "- " + ws0 } + if str_eq(ws0, "") || id_in_seen(w0_id, seen_ids) { wb } else { "- " + ws0 } } else { wb } let wb = if wn > 1 { let w1: String = json_array_get(work_nodes, 1) + let w1_id: String = json_get(w1, "id") let wc1: String = json_get(w1, "content") let ws1: String = if str_len(wc1) > 120 { str_slice(wc1, 0, 120) } else { wc1 } - if str_eq(ws1, "") { wb } else { wb + "\n- " + ws1 } + if str_eq(ws1, "") || id_in_seen(w1_id, seen_ids) { wb } else { wb + "\n- " + ws1 } } else { wb } wb } else { "" } @@ -728,15 +737,17 @@ fn handle_chat(body: String) -> String { 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, "") { pb } else { "- " + ps0 } + 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, "") { pb } else { pb + "\n- " + ps1 } + if str_eq(ps1, "") || id_in_seen(pr1_id, seen_ids) { pb } else { pb + "\n- " + ps1 } } else { pb } pb } else { "" } @@ -748,21 +759,24 @@ fn handle_chat(body: String) -> String { let sb: String = "" let sb = if sn_total > 0 { 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, "") { sb } else { "- " + ss0 } + if str_eq(ss0, "") || id_in_seen(sn0_id, seen_ids) { sb } else { "- " + ss0 } } else { sb } let sb = if sn_total > 1 { let sn1: String = json_array_get(summary_nodes, 1) + let sn1_id: String = json_get(sn1, "id") let sc1: String = json_get(sn1, "content") let ss1: String = if str_len(sc1) > 200 { str_slice(sc1, 0, 200) } else { sc1 } - if str_eq(ss1, "") { sb } else { sb + "\n- " + ss1 } + if str_eq(ss1, "") || id_in_seen(sn1_id, seen_ids) { sb } else { sb + "\n- " + ss1 } } else { sb } let sb = if sn_total > 2 { let sn2: String = json_array_get(summary_nodes, 2) + let sn2_id: String = json_get(sn2, "id") let sc2: String = json_get(sn2, "content") let ss2: String = if str_len(sc2) > 200 { str_slice(sc2, 0, 200) } else { sc2 } - if str_eq(ss2, "") { sb } else { sb + "\n- " + ss2 } + if str_eq(ss2, "") || id_in_seen(sn2_id, seen_ids) { sb } else { sb + "\n- " + ss2 } } else { sb } sb } else { "" }