From ec7efdeeb735b42bef86adee6cb1fb76e4dd83ee Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Mon, 22 Jun 2026 13:57:33 -0500 Subject: [PATCH] =?UTF-8?q?fix(recall):=20engram=20score=20float=20parsing?= =?UTF-8?q?=20=E2=80=94=20pad=20to=202=20decimals=20before=20strip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat.el | 63 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/chat.el b/chat.el index 681c1a5..a900c91 100644 --- a/chat.el +++ b/chat.el @@ -35,6 +35,41 @@ fn engram_numeric_valid(s: String) -> Bool { return true } +// parse_float_x100 — parse a float string like "0.85", "0.9", "1.0" into an integer +// scaled by 100 (so "0.85" -> 85, "0.9" -> 90, "1.0" -> 100). Uses only integer +// arithmetic because el has no float math. Normalises to exactly 2 decimal digits +// before stripping the dot so 1-decimal values like "0.9" are not misread as 9. +// Returns 70 (a safe mid-range default) for empty or structurally invalid strings. +fn parse_float_x100(s: String) -> Int { + if str_eq(s, "") { return 70 } + if !str_contains(s, ".") { + // Integer input: treat as a whole number * 100 (e.g. "1" -> 100) + let whole: Int = str_to_int(s) + return whole * 100 + } + // Split at the dot. str_slice(s, 0, dot_pos) gives left, rest gives right. + let dot_pos: Int = str_index_of(s, ".") + let left: String = str_slice(s, 0, dot_pos) + let right_raw: String = str_slice(s, dot_pos + 1, str_len(s)) + // Normalise right side to exactly 2 decimal digits. + let right: String = if str_eq(right_raw, "") { + "00" + } else { + if str_len(right_raw) == 1 { + right_raw + "0" + } else { + if str_len(right_raw) >= 3 { + str_slice(right_raw, 0, 2) + } else { + right_raw + } + } + } + let left_val: Int = if str_eq(left, "") { 0 } else { str_to_int(left) } + let right_val: Int = str_to_int(right) + return left_val * 100 + right_val +} + // 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, @@ -50,13 +85,13 @@ fn engram_score_node(node_json: String) -> Int { let tier_str: String = json_get(node_json, "tier") // Q1 fix: validate before str_to_int. Non-numeric values fall back to safe defaults. - // Parse as floats via * 100 integer arithmetic (el has no float math). + // parse_float_x100 handles 1- and 2-decimal floats correctly ("0.9" -> 90, "0.85" -> 85). let salience_100: Int = if !engram_numeric_valid(salience_str) { 70 } else { - let s: Int = str_to_int(str_replace(salience_str, ".", "")) + let s: Int = parse_float_x100(salience_str) if s > 100 { 100 } else { if s < 0 { 0 } else { s } } } let importance_100: Int = if !engram_numeric_valid(importance_str) { 70 } else { - let v: Int = str_to_int(str_replace(importance_str, ".", "")) + let v: Int = parse_float_x100(importance_str) if v > 100 { 100 } else { if v < 0 { 0 } else { v } } } @@ -97,7 +132,7 @@ fn engram_render_node(node_json: String) -> String { } 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, ".", "")) + let s: Int = parse_float_x100(salience_str) if s > 100 { 100 } else { if s < 0 { 0 } else { s } } } let salience_hint: String = if str_eq(salience_str, "") { "" } else { @@ -223,14 +258,14 @@ fn engram_score_node(node_json: String) -> Int { 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) + // 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. 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) + let s: Int = parse_float_x100(salience_str) 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, ".", "")) + let v: Int = parse_float_x100(importance_str) if v > 100 { 100 } else { if v < 0 { 0 } else { v } } } @@ -249,9 +284,10 @@ fn engram_score_node(node_json: String) -> Int { } // 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 -// gives headroom for moderately-relevant older nodes while filtering near-zero noise. +// ordered best-first by score. Only nodes above threshold=25 are included. +// With corrected float parsing: sal=0.5 * imp=0.5 at max recency (100) scores exactly 25, +// so threshold=25 admits all nodes with at least moderate salience and importance while +// cutting near-zero noise. Lower values were masking the bug; 25 is correct post-fix. // Returns at most max_nodes entries. max_nodes must not exceed 20 (sentinel limit). fn engram_compile_ranked(nodes_json: String, max_nodes: Int) -> String { if str_eq(nodes_json, "") { return "" } @@ -268,9 +304,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 lowered from 25 to 15: includes moderately-relevant older nodes. - // A 3-week-old node with salience 0.6 and importance 0.6 scores ~18 — was dropped, now included. - 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 // Check this index wasn't already selected (sentinel: look for idx marker) let idx_marker: String = "\"_sel_" + int_to_str(ci) + "\"" let already_picked: Bool = str_contains(selected, idx_marker)