diff --git a/chat.el b/chat.el index e7df32c..b28de2d 100644 --- a/chat.el +++ b/chat.el @@ -253,10 +253,13 @@ fn chat_default_model() -> String { // 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") + let updated_str: String = json_get(node_json, "updated_at") + let tier_str: String = json_get(node_json, "tier") // parse_float_x100 handles 1- and 2-decimal floats correctly ("0.9" -> 90, "0.85" -> 85). // Default 70 when field is absent; clamp to 0-100 range. @@ -269,17 +272,16 @@ fn engram_score_node(node_json: String) -> Int { 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 str_eq(created_str, "") { 50 } else { + let recency_100: Int = if !engram_numeric_valid(created_str) { 50 } else { let created_ts: Int = str_to_int(created_str) let age_secs: Int = now_ts - created_ts - let age_days: Int = age_secs / 86400 + // 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 decay: Int = if age_days >= 30 { 10 } else { 100 - (age_days * 3) } if decay < 10 { 10 } else { decay } } - // Combined score 0-1000000 (no floats): salience * importance * recency / 10000 return salience_100 * importance_100 * recency_100 / 10000 } @@ -594,7 +596,7 @@ fn engram_compile(intent: String) -> String { } else { "" } } else { "" } - // Affective context: always include the most recent high-emotion memory within 72h. + // Affective context: always include the most recent high-emotion memory within 14 days. 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() @@ -2486,3 +2488,56 @@ fn strengthen_chat_nodes(activation_nodes: String) -> Void { let i = i + 1 } } + +// session_summary_autogenerate — build a minimal summary from conversation history without LLM. +// Extracts user message snippets (first 80 chars each, up to 5 turns). +// Called by the session-end hook when >= 5 complete turns have occurred. +fn session_summary_autogenerate(hist: String) -> String { + if str_eq(hist, "") { return "" } + if str_eq(hist, "[]") { return "" } + let total: Int = json_array_len(hist) + if total == 0 { return "" } + let snippets: String = "" + let count: Int = 0 + let i: Int = 0 + while i < total && count < 5 { + let entry: String = json_array_get(hist, i) + let role: String = json_get(entry, "role") + if str_eq(role, "user") { + let msg: String = json_get(entry, "content") + let snip: String = if str_len(msg) > 80 { str_slice(msg, 0, 80) } else { msg } + let snippets = if str_eq(snippets, "") { snip } else { snippets + "; " + snip } + let count = count + 1 + } + let i = i + 1 + } + if str_eq(snippets, "") { return "" } + return "Session covered: " + snippets +} + +// session_summary_write_dated — write a SessionSummary node with a caller-supplied dated label. +// Unlike a global-label 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:"). +// Uses salience 0.85/importance 0.85 (two-decimal) to avoid the single-decimal parse bug. +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.85), el_from_float(0.85), 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 +}