diff --git a/chat.el b/chat.el index 0113d1f..681c1a5 100644 --- a/chat.el +++ b/chat.el @@ -73,6 +73,181 @@ fn engram_score_node(node_json: String) -> Int { return salience_100 * importance_100 * recency_100 / 10000 } +// engram_render_node — render a single engram node JSON object as a human-readable +// bullet line for inclusion in the system prompt. Format: - [TYPE age salience] content +// Fixes Issue #1, #4: content extraction from raw JSON nodes. +// Fixes Issue #3: age and salience annotations surface staleness/confidence to LLM. +fn engram_render_node(node_json: String) -> String { + if str_eq(node_json, "") { return "" } + let content: String = json_get(node_json, "content") + if str_eq(content, "") { return "" } + let node_type: String = json_get(node_json, "node_type") + let type_label: String = if str_eq(node_type, "") { "mem" } else { node_type } + let now_ts: Int = time_now() + let created_str: String = json_get(node_json, "created_at") + let updated_str: String = json_get(node_json, "updated_at") + let ts_raw: String = if str_eq(created_str, "") { updated_str } else { created_str } + let age_label: String = if str_eq(ts_raw, "") { "" } else { + let node_ts: Int = str_to_int(ts_raw) + let age_secs: Int = now_ts - node_ts + let age_days: Int = if age_secs < 0 { 0 } else { age_secs / 86400 } + if age_days == 0 { "today" } else { + if age_days > 30 { "old" } else { int_to_str(age_days) + "d ago" } + } + } + let salience_str: String = json_get(node_json, "salience") + let sal_100: Int = if str_eq(salience_str, "") { 0 } else { + let s: Int = str_to_int(str_replace(salience_str, ".", "")) + if s > 100 { 100 } else { if s < 0 { 0 } else { s } } + } + let salience_hint: String = if str_eq(salience_str, "") { "" } else { + if sal_100 >= 80 { "high" } else { if sal_100 >= 50 { "med" } else { "low" } } + } + let ann_inner: String = type_label + let ann_inner = if str_eq(age_label, "") { ann_inner } else { ann_inner + " " + age_label } + let ann_inner = if str_eq(salience_hint, "") { ann_inner } else { ann_inner + " " + salience_hint } + let ann: String = "[" + ann_inner + "]" + let snip: String = if str_len(content) > 200 { str_slice(content, 0, 200) } else { content } + return "- " + ann + " " + snip +} + +// engram_render_nodes — render a JSON array of nodes as newline-joined bullet lines. +fn engram_render_nodes(nodes_json: String) -> String { + if str_eq(nodes_json, "") { return "" } + if str_eq(nodes_json, "[]") { return "" } + let total: Int = json_array_len(nodes_json) + if total == 0 { return "" } + let result: String = "" + let i: Int = 0 + while i < total { + let node: String = json_array_get(nodes_json, i) + let line: String = engram_render_node(node) + let result = if str_eq(line, "") { result } else { + if str_eq(result, "") { line } else { result + "\n" + line } + } + let i = i + 1 + } + return result +} + +// engram_dedup_nodes — deduplicate a merged JSON node array by id / content fingerprint. +// Fixes Issue #2: prevents same node appearing from both activation and search passes. +fn engram_dedup_nodes(nodes_json: String) -> String { + if str_eq(nodes_json, "") { return "" } + if str_eq(nodes_json, "[]") { return "" } + let total: Int = json_array_len(nodes_json) + if total == 0 { return "" } + let seen_keys: String = "" + let result: String = "" + let i: Int = 0 + while i < total { + let node: String = json_array_get(nodes_json, i) + let node_content: String = json_get(node, "content") + let node_id: String = json_get(node, "id") + let dedup_key: String = if str_eq(node_id, "") { + if str_len(node_content) > 80 { str_slice(node_content, 0, 80) } else { node_content } + } else { node_id } + let key_marker: String = "|" + dedup_key + "|" + let already_seen: Bool = str_contains(seen_keys, key_marker) + let seen_keys = if already_seen { seen_keys } else { seen_keys + key_marker } + let result = if already_seen { result } else { + if str_eq(result, "") { node } else { result + "," + node } + } + let i = i + 1 + } + if str_eq(result, "") { return "" } + return "[" + result + "]" +} + +// engram_compile_ranked — build a ranked list of nodes, best-first by score. +// Fix (Issue #11): uses "|N|" index tracking instead of _sel_N JSON mutation, +// which leaked sentinel fields into the node objects passed to the LLM. +fn engram_compile_ranked(nodes_json: String, max_nodes: Int) -> String { + if str_eq(nodes_json, "") { return "" } + if str_eq(nodes_json, "[]") { return "" } + let total: Int = json_array_len(nodes_json) + if total == 0 { return "" } + let selected_indices: String = "" + let selected_nodes: String = "" + let pass: Int = 0 + while pass < max_nodes && pass < total { + let best_idx: Int = -1 + let best_score: Int = -1 + let ci: Int = 0 + 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 + 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 + let best_score = if is_better { score } else { best_score } + let best_idx = if is_better { ci } else { best_idx } + let ci = ci + 1 + } + if best_idx < 0 { + let pass = total // break + } else { + let chosen: String = json_array_get(nodes_json, best_idx) + let sep: String = if str_eq(selected_nodes, "") { "" } else { "," } + let selected_nodes = selected_nodes + sep + chosen + let selected_indices = selected_indices + "|" + int_to_str(best_idx) + "|" + } + let pass = pass + 1 + } + if str_eq(selected_nodes, "") { return "" } + return "[" + selected_nodes + "]" +}ory.el" + +fn chat_default_model() -> String { + let m: String = state_get("soul_model") + if !str_eq(m, "") { + return m + } + let e: String = env("SOUL_LLM_MODEL") + if !str_eq(e, "") { + return e + } + return "claude-sonnet-4-5" +} + +// 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. +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") + + // 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 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 str_eq(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 + 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 +} + // engram_compile_ranked — build a context string from a JSON array of node objects, // ordered best-first by score. Only nodes above threshold=15 are included. // With corrected parsing: sal=0.5 * imp=0.5 at max recency scores 25; threshold 15