From 33cb1138f4b661955e45c33e7e0edfd510a30347 Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Mon, 22 Jun 2026 13:58:17 -0500 Subject: [PATCH] fix(recall): set threshold=25 in all engram_compile_ranked variants --- chat.el | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/chat.el b/chat.el index a900c91..dcc5b42 100644 --- a/chat.el +++ b/chat.el @@ -212,8 +212,8 @@ 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) - // Threshold: includes moderately-relevant older nodes (score >= 15). - let above_thresh: Bool = score >= 15 + // Threshold 25: sal=0.5 * imp=0.5 * recency=1.0 -> 50*50*100/10000 = 25. + let above_thresh: Bool = score >= 25 let idx_marker: String = "|" + int_to_str(ci) + "|" let already_picked: Bool = str_contains(selected_indices, idx_marker) let is_better: Bool = score > best_score && above_thresh && !already_picked @@ -1159,12 +1159,29 @@ fn handle_chat(body: String) -> String { state_set("conv_history", final_hist) conv_history_persist(final_hist) - // Automatic session-end summary: write/overwrite the SessionSummary node on each turn - // so process restarts always have a continuity snapshot (no shutdown hook needed). - // Uses autogenerate (no LLM) so it is cheap — the node is overwritten not appended. - let auto_sum: String = session_summary_autogenerate(final_hist) - if !str_eq(auto_sum, "") { - let discard_sum: String = session_summary_write(auto_sum) + // Session-end summary hook: write a dated SessionSummary node once per boot when + // the conversation reaches >= 5 user turns (10 hist entries = 5 user+assistant pairs). + // Uses a per-boot label ("session:summary:") so summaries accumulate across + // sessions instead of overwriting a single global node. A state flag prevents rewriting + // on every subsequent turn once the threshold is crossed. + let final_hist_len: Int = json_array_len(final_hist) + if final_hist_len >= 10 { + let already_wrote: String = state_get("session_summary_written") + if str_eq(already_wrote, "") { + // Derive (or create) a stable boot-scoped session id. + let boot_id: String = state_get("session_boot_id") + let boot_id = if str_eq(boot_id, "") { + let new_id: String = int_to_str(time_now()) + state_set("session_boot_id", new_id) + new_id + } else { boot_id } + let sess_label: String = "session:summary:" + boot_id + let auto_sum: String = session_summary_autogenerate(final_hist) + if !str_eq(auto_sum, "") { + let discard_sum: String = session_summary_write_dated(auto_sum, sess_label) + state_set("session_summary_written", "1") + } + } } let activation_nodes: String = engram_activate_json(message, 2) @@ -2247,6 +2264,32 @@ fn session_summary_write(summary_text: String) -> String { return node_id } +// session_summary_write_dated — write a SessionSummary node with a caller-supplied dated label. +// Unlike session_summary_write, this does NOT delete old nodes — each session accumulates its +// own node so engram_search_json("session:summary") can return multiple past sessions. +// The label must be unique per session (e.g. "session:summary:"). +fn session_summary_write_dated(summary_text: String, label: String) -> String { + if str_eq(summary_text, "") { return "" } + if str_eq(label, "") { return "" } + let safe_text: String = str_replace(summary_text, "\"", "'") + let trimmed: String = if str_len(safe_text) > 800 { str_slice(safe_text, 0, 800) } else { safe_text } + let ts: Int = time_now() + let ts_str: String = int_to_str(ts) + let content: String = "[session-summary] " + trimmed + " | ts:" + ts_str + let tags: String = "[\"SessionSummary\",\"session-summary\",\"previous-session\",\"consolidate\"]" + let node_id: String = engram_node_full( + content, "SessionSummary", label, + el_from_float(0.9), el_from_float(0.8), el_from_float(1.0), + "Episodic", tags + ) + if str_eq(node_id, "") { + println("[chat] session_summary_write_dated: engram write failed — summary node lost (label=" + label + ")") + return "" + } + println("[chat] session_summary_write_dated: wrote SessionSummary (" + int_to_str(str_len(content)) + " chars) label=" + label + " -> " + node_id) + return node_id +} + // session_summary_autogenerate — build a minimal summary from conversation history without LLM. // Extracts user message snippets (first 80 chars each, up to 5 turns). // Used as the automatic session-end hook so every turn produces a continuity snapshot.