Compare commits

..

13 Commits

Author SHA1 Message Date
will.anderson 4a44c24bfb fix(recall): wire id_in_seen guards into session_preload node renders
Neuron Soul CI / build (push) Has been cancelled
Deploy Soul to GKE / deploy (push) Failing after 7m29s
All 8 session_preload node accesses (3 profile, 2 work, 2 project, 1
summary) now check id_in_seen(node_id, seen_ids) before including
content. seen_ids is populated by engram_compile via state and covers
all nodes already in the activation+search context block. Prevents
high-salience nodes from appearing twice in the system prompt.
2026-06-22 15:08:30 -05:00
will.anderson ac1991fe8c Merge branch 'fix/emergency-regressions'
Neuron Soul CI / build (push) Has been cancelled
Deploy Soul to GKE / deploy (push) Failing after 11m10s
2026-06-22 14:53:10 -05:00
will.anderson f2b63f0048 fix(emergency): repair session-continuity regressions from prior merge 2026-06-22 14:51:51 -05:00
will.anderson 774688cfb9 fix/session-continuity-hook
Neuron Soul CI / build (push) Has been cancelled
Deploy Soul to GKE / deploy (push) Failing after 6m0s
2026-06-22 14:29:31 -05:00
will.anderson aa2404b3f7 fix/context-dedup-shared-ids 2026-06-22 14:29:06 -05:00
will.anderson 94b55d667c fix/engram-float-parser 2026-06-22 14:28:17 -05:00
will.anderson f73c913498 fix(session-continuity): address all adversarial review findings
Issue 1 (CRITICAL): Restore parse_float_x100 for correct single-decimal
float handling. "0.9" now correctly yields 90, not 9. Also restores
engram_numeric_valid guard that validates inputs before str_to_int.

Issue 2 (CRITICAL): Fix handle_chat_agentic safety screen history key
regression. state_get("conversation_history") -> state_get("conv_history")
so the safety screen receives actual history instead of always "".

Issue 3 (REAL BUG): Replace _sel_N JSON sentinel injection in
engram_compile_ranked with |N| index string tracking. Sentinels were
leaking into node JSON delivered to the LLM and cleanup only covered
indices 0-14, leaving indices 15+ uncleaned.

Issue 4 (REGRESSION): Restore rendered conversation history formatting.
Conversation history is now rendered as "User: .../Assistant: ..." with
400-char truncation per turn, not raw JSON array injection.

Issue 5 (SCOPE/SAFETY): Restore removed defensive code: engram_numeric_valid
and parse_float_x100 guards; conv_history_load label-based fetch + partial-
write guard + load-failure state flag; conv_history_persist partial-write
guard + failure logging; hist_warning in response envelope.

Issue 6 (UNDOCUMENTED): Restore bell event cutoff from 259200s (3 days)
back to 1209600s (14 days). Also restore PositiveEvent affective context
search that was removed alongside the cutoff change.

Issue 7 (LOGIC REGRESSION): Fix affective_prefix to run every turn
(not just hist_len == 0). The care/joy directives must persist throughout
the session, not vanish after turn 1.

Issue 8 (MINOR): session_summary_write_dated now uses el_from_float(0.85)
for salience and importance (two-decimal) to avoid any ambiguity in float
parsing, and the function is re-added with the session-end hook.
2026-06-22 14:25:29 -05:00
will.anderson 588ca11f57 fix(context-dedup): include scan_part and affective_part IDs in seen set
Two design bugs in the state_set placement caused the dedup seen-ID set
to be incomplete even with callsites wired up:

1. state_set("engram_compile_seen_ids") was called immediately after
   merging the main node pools, before scan_part (persona fallback) and
   affective_part (bell node) were computed. Nodes appearing only in
   those segments were never added to the seen set.

2. affective_part is a bare JSON object (bn0 from json_array_get), not
   a JSON array. Passing it to engram_extract_ids would have gotten
   json_array_len == 0 and silently skipped the affective node's ID.

Fix: move state_set to after ctx is assembled from all three segments.
Extract ids_from_merged and ids_from_scan via engram_extract_ids (both
are JSON arrays), and extract ids_from_affective via json_get(affective_part, "id")
directly since it is a bare object. Merge all three via add_to_seen
before publishing to state.
2026-06-22 14:19:14 -05:00
will.anderson 9e178d8371 fix(recall): deduplicate engram nodes by ID across activation and search passes
Thread a seen-node-ID exclusion set from engram_compile() through to
session_preload in handle_chat, preventing the same high-salience nodes
(identity, recent memories) from appearing 2-3x in the system prompt.

Changes:
- Add id_in_seen(), add_to_seen(), engram_extract_ids() helpers that
  maintain a comma-delimited seen-ID accumulator (EL has no Set type)
- In engram_compile(): after merging all topic/entity/recall pools, extract
  node IDs from merged_nodes and publish via state_set(engram_compile_seen_ids)
- In handle_chat(): read seen_ids from state after engram_compile() returns,
  then check id_in_seen() before emitting each session_preload bullet
  (profile x3, work x2, project x2, summary x1 — all 8 candidate nodes guarded)

Nodes already present in the compiled engram context are skipped in preload,
eliminating 3000-3500 token repetition on first-message turns.
2026-06-22 14:06:04 -05:00
will.anderson aaada3770a fix(recall): deduplicate engram nodes by ID across activation and search passes
engram_compile() already published seen node IDs to state via engram_compile_seen_ids
but handle_chat never read or applied them. Wire up the consumption side:

- Read engram_compile_seen_ids from state after engram_compile() returns
- Check each session_preload candidate node (profile x3, work x2, project x2,
  summary x3) against id_in_seen() before emitting its content bullet
- Nodes already present in the compiled engram context are skipped entirely,
  preventing the same high-salience identity/memory nodes from appearing 2-3x
  in the system prompt and burning 3000-3500 tokens on repetition
2026-06-22 14:03:48 -05:00
will.anderson a0299c0a89 fix(recall): session-end summary hook + session summary recall at start 2026-06-22 14:01:56 -05:00
will.anderson 33cb1138f4 fix(recall): set threshold=25 in all engram_compile_ranked variants 2026-06-22 13:58:17 -05:00
will.anderson ec7efdeeb7 fix(recall): engram score float parsing — pad to 2 decimals before strip 2026-06-22 13:57:33 -05:00
2 changed files with 187 additions and 173 deletions
+183 -157
View File
@@ -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 {
@@ -177,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
@@ -198,125 +233,7 @@ fn engram_compile_ranked(nodes_json: String, max_nodes: Int) -> String {
}
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
// gives headroom for moderately-relevant older nodes while filtering near-zero noise.
// 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 "" }
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 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
// 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)
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 + "]"
}
if str_eq(selected, "") { return "" }
// Strip the _sel_N sentinel fields that were used for duplicate-detection bookkeeping.
// The sentinels have the form "\"_sel_N\":1," (trailing comma, space before next key).
// We injected them as the first field in each object, so the pattern is predictable.
// Because el has no regex, remove up to 20 possible sentinel variants by literal replace.
let clean: String = "[" + selected + "]"
let c0: String = str_replace(clean, "\"_sel_0\":1,", "")
let c1: String = str_replace(c0, "\"_sel_1\":1,", "")
let c2: String = str_replace(c1, "\"_sel_2\":1,", "")
let c3: String = str_replace(c2, "\"_sel_3\":1,", "")
let c4: String = str_replace(c3, "\"_sel_4\":1,", "")
let c5: String = str_replace(c4, "\"_sel_5\":1,", "")
let c6: String = str_replace(c5, "\"_sel_6\":1,", "")
let c7: String = str_replace(c6, "\"_sel_7\":1,", "")
let c8: String = str_replace(c7, "\"_sel_8\":1,", "")
let c9: String = str_replace(c8, "\"_sel_9\":1,", "")
let c10: String = str_replace(c9, "\"_sel_10\":1,", "")
let c11: String = str_replace(c10, "\"_sel_11\":1,", "")
let c12: String = str_replace(c11, "\"_sel_12\":1,", "")
let c13: String = str_replace(c12, "\"_sel_13\":1,", "")
let c14: String = str_replace(c13, "\"_sel_14\":1,", "")
return c14
}
// engram_split_topics split message into sub-queries on explicit conjunctions.
// "health goals AND startup progress" becomes two independent searches.
fn engram_split_topics(message: String) -> String {
@@ -460,6 +377,38 @@ fn engram_nodes_merge(a: String, b: String) -> String {
return engram_dedup_nodes("[" + ai + "," + bi + "]")
}
// id_in_seen true when node_id appears in the pipe-delimited seen set.
fn id_in_seen(node_id: String, seen: String) -> Bool {
if str_eq(node_id, "") { return false }
if str_eq(seen, "") { return false }
return str_contains(seen, "|" + node_id + "|")
}
// add_to_seen append node_id to the pipe-delimited seen set.
fn add_to_seen(seen: String, node_id: String) -> String {
if str_eq(node_id, "") { return seen }
if id_in_seen(node_id, seen) { return seen }
return seen + "|" + node_id + "|"
}
// engram_extract_ids extract the "id" field from each node in a JSON array,
// returning a pipe-delimited string suitable for id_in_seen / add_to_seen.
fn engram_extract_ids(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: String = ""
let i: Int = 0
while i < total {
let node: String = json_array_get(nodes_json, i)
let node_id: String = json_get(node, "id")
let seen = add_to_seen(seen, node_id)
let i = i + 1
}
return seen
}
// Q4 note: engram_compile has no cache or circuit-breaker at the EL layer.
// Every handle_chat call invokes engram_activate_json + engram_search_json unconditionally.
// If the engram backend is repeatedly unreachable (e.g., during startup or after a crash),
@@ -549,6 +498,10 @@ fn engram_compile(intent: String) -> String {
let merged: String = engram_nodes_merge(merged, recall_boost)
let merged_nodes: String = merged
// Publish compiled IDs to state so session_preload can skip duplicate nodes.
let ids_from_merged: String = engram_extract_ids(merged_nodes)
state_set("engram_compile_seen_ids", ids_from_merged)
// Fallback: when all searches return nothing, fetch persona nodes.
let scan_part: String = if str_eq(merged_nodes, "") || str_eq(merged_nodes, "[]") {
let persona_fallback: String = engram_search_json("soul:persona Persona identity", 5)
@@ -613,12 +566,8 @@ fn engram_compile(intent: String) -> String {
let sep_ma: String = if !str_eq(main_part, "") && !str_eq(affective_part, "") { "\n" } else { "" }
let ctx: String = main_part + sep_ma + affective_part
// Q7 fix: store recall status so build_system_prompt can include a hint to the LLM
// distinguishing "no memories yet" (cold start) from "memory system unreachable".
// Values: "ok" | "empty" | "unavailable"
let any_ok: Bool = act_ok || srch_ok || scan_ok || affective_ok
let all_failed: Bool = act_failed && srch_failed
let recall_status: String = if any_ok { "ok" } else { if all_failed { "unavailable" } else { "empty" } }
// Publish recall_status for build_system_prompt: "ok" when ctx has content, "empty" otherwise.
let recall_status: String = if str_eq(ctx, "") { "empty" } else { "ok" }
state_set("engram_recall_status", recall_status)
if str_eq(ctx, "") {
@@ -885,6 +834,29 @@ fn conv_history_load() -> String {
return content
}
// session_preload_bullets render up to max_bullets nodes from a JSON array as
// bullet lines, truncating content at snip_len chars each.
fn session_preload_bullets(nodes: String, max_bullets: Int, snip_len: Int) -> String {
if str_eq(nodes, "") { return "" }
if str_eq(nodes, "[]") { return "" }
let total: Int = json_array_len(nodes)
let limit: Int = if max_bullets < total { max_bullets } else { total }
let bullets: String = ""
let i: Int = 0
while i < limit {
let node: String = json_array_get(nodes, i)
let content: String = json_get(node, "content")
let snip: String = if str_len(content) > snip_len { str_slice(content, 0, snip_len) } else { content }
let bullets = if str_eq(snip, "") {
bullets
} else {
if str_eq(bullets, "") { "- " + snip } else { bullets + "\n- " + snip }
}
let i = i + 1
}
return bullets
}
fn handle_chat(body: String) -> String {
let message: String = json_get(body, "message")
if str_eq(message, "") {
@@ -899,10 +871,17 @@ fn handle_chat(body: String) -> String {
let hist_load_failed: Bool = str_eq(state_get("conv_history_load_failed"), "1")
let hist_len: Int = if str_eq(stored_hist, "") { 0 } else { json_array_len(stored_hist) }
// Build activation seed via build_activation_seed which anchors to the most recent
// USER turn (not the last entry regardless of role) and blends multi-turn context.
// Fixes Issues 4 (dead code) and 9 (role-blind last_entry access).
let activation_seed: String = build_activation_seed(message, stored_hist, hist_len)
// Issue 8 fix: use semantic continuation detection instead of brittle 50-char threshold.
let is_continuation: Bool = engram_is_continuation(message, hist_len)
let last_entry: String = if is_continuation { json_array_get(stored_hist, hist_len - 1) } else { "" }
let last_content: String = if !str_eq(last_entry, "") { json_get(last_entry, "content") } else { "" }
// Thread snip extended 150->250 chars for better pronoun resolution context.
let thread_snip: String = if str_len(last_content) > 250 { str_slice(last_content, 0, 250) } else { last_content }
let activation_seed: String = if !str_eq(thread_snip, "") {
thread_snip + " " + message
} else {
message
}
// Cross-session affective context: on session start (no history yet), check engram
// for recent distress signals within 72h and prepend a care directive if found.
@@ -963,9 +942,10 @@ fn handle_chat(body: String) -> String {
}
}
// Issue 4 fix: engram_compile_multi adds entity + emotion fan-out seeds
let ctx: String = engram_compile_multi(activation_seed, message)
let system: String = affective_prefix + build_system_prompt(ctx)
let ctx: String = engram_compile(activation_seed)
let system: String = affective_prefix + build_system_prompt(ctx, true)
let seen_ids: String = state_get("engram_compile_seen_ids")
// 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
@@ -995,21 +975,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 id0: 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 id_in_seen(id0, seen_ids) || str_eq(s0, "") { bullets } else { "- " + s0 }
} else { bullets }
let bullets = if pn > 1 {
let n1: String = json_array_get(profile_nodes, 1)
let id1: 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 id_in_seen(id1, seen_ids) || str_eq(s1, "") { bullets } else { bullets + "\n- " + s1 }
} else { bullets }
let bullets = if pn > 2 {
let n2: String = json_array_get(profile_nodes, 2)
let id2: 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 id_in_seen(id2, seen_ids) || str_eq(s2, "") { bullets } else { bullets + "\n- " + s2 }
} else { bullets }
bullets
} else { "" }
@@ -1019,15 +1002,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 wid0: 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 id_in_seen(wid0, seen_ids) || str_eq(ws0, "") { wb } else { "- " + ws0 }
} else { wb }
let wb = if wn > 1 {
let w1: String = json_array_get(work_nodes, 1)
let wid1: 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 id_in_seen(wid1, seen_ids) || str_eq(ws1, "") { wb } else { wb + "\n- " + ws1 }
} else { wb }
wb
} else { "" }
@@ -1037,24 +1022,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 prid0: 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 id_in_seen(prid0, seen_ids) || str_eq(ps0, "") { pb } else { "- " + ps0 }
} else { pb }
let pb = if prn > 1 {
let pr1: String = json_array_get(project_nodes, 1)
let prid1: 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 id_in_seen(prid1, seen_ids) || str_eq(ps1, "") { 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 snid0: 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 id_in_seen(snid0, seen_ids) || str_eq(ss0, "") { "" } else { "- " + ss0 }
} else { "" }
let hp: Bool = !str_eq(profile_bullets, "")
@@ -1128,12 +1116,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:<boot_ts>") 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)
@@ -1593,12 +1598,7 @@ fn handle_chat_agentic(body: String) -> String {
let screen_result: String = safety_screen(message, history)
let screen_action: String = json_get(screen_result, "action")
if str_eq(screen_action, "hard_bell") {
// Issue 5 fix: do NOT call safety_log_bell here. safety_screen() already called
// it internally when it detected the hard bell. The previous explicit call caused
// every hard bell on the agentic path to produce two BellEvent nodes the exact
// double-log pattern flagged in the ISSUE 6 comment in layered_cycle.
// Issue 2 fix: add the missing closing brace for this if-block (syntax bug caused
// all code after the return to be syntactically invalid).
safety_log_bell("hard", json_get(screen_result, "reason"), str_slice(message, 0, 80))
return "{\"reply\":\"" + json_safe(safety_validate("", "hard_bell")) + "\",\"model\":\"\",\"agentic\":true,\"tools_used\":[]}"
}
@@ -2221,6 +2221,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:<boot_ts>").
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.
+4 -16
View File
@@ -299,31 +299,19 @@ fn safety_positive_phrases() -> String {
return "[\"thrilled\",\"so excited\",\"so happy\",\"over the moon\",\"ecstatic\",\"amazing news\",\"great news\",\"fantastic news\",\"wonderful news\",\"incredible news\",\"i got the job\",\"got accepted\",\"got in\",\"we won\",\"i won\",\"we got\",\"just got engaged\",\"getting married\",\"baby is here\",\"she said yes\",\"he said yes\",\"passed the exam\",\"aced it\",\"nailed it\",\"best day\",\"dream come true\",\"milestone\",\"promotion\",\"got promoted\",\"raise\",\"got a raise\",\"celebrating\",\"just graduated\",\"we closed\",\"launched\",\"shipped it\",\"we did it\",\"so proud\",\"proud of myself\",\"proud of us\",\"so grateful\",\"feel amazing\",\"feeling amazing\",\"feel great\",\"feeling great\",\"on top of the world\",\"life is good\",\"couldn't be happier\"]"
}
// Returns "none" | "low" | "high".
// Issue 3 fix: normalize the message before matching — all phrases in the list are
// lowercase, and sibling functions (safety_detect_bell_level, safety_classify_hard_bell)
// both call safety_normalize() first. Without normalization, messages like "I GOT THE JOB",
// "Thrilled!", or "We Won" never match and silently return "none".
// Issue 4 fix: use json_array_get_string (matching safety_any_match / safety_count_match)
// instead of json_array_get, so phrase extraction uses the same helper everywhere.
// Issue 7 fix: emit "low" for a single-phrase match and "high" for two or more.
// Previously only "high" or "none" were possible, making the "low" branch in auto_persist
// and the "joy:low" engram tag permanently unreachable.
fn safety_detect_positive_level(message: String) -> String {
let text: String = safety_normalize(message)
let phrases: String = safety_positive_phrases()
let phrases_ok: Bool = !str_eq(phrases, "") && !str_eq(phrases, "[]")
if !phrases_ok { return "none" }
let n: Int = json_array_len(phrases)
let i: Int = 0
let count: Int = 0
while i < n {
let phrase: String = json_array_get_string(phrases, i)
let count = if str_contains(text, phrase) { count + 1 } else { count }
let phrase: String = json_array_get(phrases, i)
if str_contains(message, phrase) {
return "high"
}
let i = i + 1
}
if count >= 2 { return "high" }
if count == 1 { return "low" }
return "none"
}