Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 774688cfb9 | |||
| aa2404b3f7 | |||
| 94b55d667c | |||
| f73c913498 | |||
| 588ca11f57 | |||
| 9e178d8371 | |||
| aaada3770a | |||
| a0299c0a89 |
@@ -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()
|
||||
@@ -1001,6 +1003,7 @@ fn handle_chat(body: String) -> String {
|
||||
// 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)
|
||||
@@ -1026,21 +1029,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 { "" }
|
||||
@@ -1050,15 +1056,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 { "" }
|
||||
@@ -1068,24 +1076,27 @@ 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 { "" }
|
||||
|
||||
let summary_bullet: String = if summary_ok {
|
||||
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, "") { "" } else { "- " + ss0 }
|
||||
if str_eq(ss0, "") || id_in_seen(sn0_id, seen_ids) { "" } else { "- " + ss0 }
|
||||
} else { "" }
|
||||
|
||||
let hp: Bool = !str_eq(profile_bullets, "")
|
||||
@@ -2477,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:<boot_ts>").
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user