Compare commits

..

2 Commits

Author SHA1 Message Date
will.anderson 95cb49a8b0 fix(cross-session-continuity): resolve 11 issues from code review
1. Missing closing brace on hard_bell block in handle_chat_agentic — safety gate was broken and all subsequent code unreachable.
2. Replace phantom engram_get_node_by_label() (not a runtime builtin) with engram_search_json + exact label filter in all three call sites (chat.el session_preload, session_summary_write, soul.el boot loader).
3. Fix session_summary_autogenerate scoping bug — snippets/count mutations were inside an if block and silently discarded each iteration; moved to top-level of while body using if-expressions per the el mutation rule.
4. Fix agentic session history restore — state_get fallback now calls session_hist_load (session:messages:SESSION_ID) on cold start; persist now uses session_hist_save so the write and read use the same label scheme.
5. Wire soul_prev_session_summary state key into session_preload as primary source, eliminating the dead state write.
6. Wire soul_affective_context state key into handle_chat affective prefix check, eliminating the dead state write.
7. Add session_summary_autogenerate + session_summary_write to the agentic path so users on handle_chat_agentic get session summary continuity.
8. Add import "chat.el" to neuron-api.el to make session_summary_write dependency explicit.
9. Replace corrupted em-dash bytes (\xc3\xa2\xc2\x80\xc2\x94) in session_preload headers with plain hyphen per VOICE RULE.
10. Add newline before return in handle_api_consolidate to fix statement-separator issue.
11. Add delete-before-write to conv_history_persist to prevent unbounded engram accumulation per turn.
2026-06-22 13:39:46 -05:00
will.anderson 795b32ad1a feat(recall): cross-session-continuity improvements
Neuron Soul CI / build (pull_request) Failing after 14m49s
2026-06-22 13:00:17 -05:00
4 changed files with 377 additions and 564 deletions
+265 -532
View File
@@ -48,474 +48,72 @@ fn engram_score_node(node_json: String) -> Int {
return salience_100 * importance_100 * recency_100 / 10000
}
// 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.
// Threshold lowered to 15 to include moderately-relevant older nodes.
// engram_compile_ranked build a context string from a JSON array of node objects,
// ordered best-first by score. Only nodes above a minimum score (25 = salience 0.5 *
// importance 0.5 * recency 1.0) are included; the rest are noise. Returns at most
// max_nodes entries concatenated as JSON array text. Because el has no sort primitive,
// we do a single selection pass picking the top N by linear scan (N=10 cap).
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 "" }
// selected_indices is a pipe-delimited string of chosen integer indices, e.g. "|2|7|".
// No sentinel fields are injected into the node JSON the nodes stay clean.
let selected_indices: String = ""
let selected_nodes: String = ""
// Two-pass: first pass finds the top `max_nodes` by score via selection.
// We track selected node indices and their scores to avoid duplicate picks.
let selected: String = "" // comma-sep JSON snippets for chosen nodes
let selected_count: Int = 0
let pass: Int = 0
while pass < max_nodes && pass < total {
// Find the unselected node with the highest score
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.
let above_thresh: Bool = score >= 15
// Check this index wasn't already selected using the index string.
let idx_marker: String = "|" + int_to_str(ci) + "|"
let already_picked: Bool = str_contains(selected_indices, idx_marker)
// Only include reasonably relevant nodes (threshold=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)
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
}
// No more qualifying nodes
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 sep: String = if str_eq(selected, "") { "" } else { "," }
// Append the index sentinel inline so already_picked checks work
let selected = selected + sep + "{\"_sel_" + int_to_str(best_idx) + "\":1," + str_slice(chosen, 1, str_len(chosen) - 1) + "}"
let selected_count = selected_count + 1
}
let pass = pass + 1
}
if str_eq(selected_nodes, "") { return "" }
return "[" + selected_nodes + "]"
}
// 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 sal] content
// Fix (Issue #3, #4): passes context as prose bullets instead of raw JSON objects,
// which are opaque to the LLM and waste token budget on field names.
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" }
}
}
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 engram nodes as newline-joined
// prose bullet lines. Returns "" when input is empty.
// Fix (Issue #3): called by build_system_prompt to convert raw JSON ctx to
// human-readable bullets before injecting into the LLM system prompt.
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_render_ctx render the ctx string returned by engram_compile as prose bullets.
// ctx may be a JSON array "[...]", a single object "{...}", or up to two such segments
// joined by "\n". We handle the three common shapes produced by engram_compile:
// 1. single JSON array -> engram_render_nodes
// 2. single JSON object -> engram_render_node
// 3. two segments sep by "\n" -> render each half individually and join
// Fix (Issue #3): called by build_system_prompt so the LLM receives human-readable
// prose bullets instead of raw JSON field blobs.
fn engram_render_ctx(ctx: String) -> String {
if str_eq(ctx, "") { return "" }
// Single JSON array.
if str_starts_with(ctx, "[") {
let nl: Int = str_index_of(ctx, "\n")
if nl < 0 {
// Whole ctx is one array.
let r: String = engram_render_nodes(ctx)
if !str_eq(r, "") { return r }
return ""
}
// First segment is an array; try to render it and the rest separately.
let part1: String = str_slice(ctx, 0, nl)
let part2: String = str_slice(ctx, nl + 1, str_len(ctx))
let r1: String = engram_render_nodes(part1)
let r2: String = if str_starts_with(part2, "[") {
engram_render_nodes(part2)
} else {
if str_starts_with(part2, "{") { engram_render_node(part2) } else { "" }
}
if str_eq(r1, "") { return r2 }
if str_eq(r2, "") { return r1 }
return r1 + "\n" + r2
}
// Single JSON object (e.g. affective_part node when it's the only result).
if str_starts_with(ctx, "{") {
let nl: Int = str_index_of(ctx, "\n")
if nl < 0 {
let r: String = engram_render_node(ctx)
if !str_eq(r, "") { return r }
return ""
}
let part1: String = str_slice(ctx, 0, nl)
let part2: String = str_slice(ctx, nl + 1, str_len(ctx))
let r1: String = engram_render_node(part1)
let r2: String = if str_starts_with(part2, "[") {
engram_render_nodes(part2)
} else {
if str_starts_with(part2, "{") { engram_render_node(part2) } else { "" }
}
if str_eq(r1, "") { return r2 }
if str_eq(r2, "") { return r1 }
return r1 + "\n" + r2
}
// Fallback: ctx is in an unexpected format; return as-is.
return ctx
}
// is_followup_phrase returns true when the message is a recognized follow-up
// reference that should anchor recall to the prior user topic rather than stand alone.
// Used by build_activation_seed to choose the right enrichment strategy.
fn is_followup_phrase(msg: String) -> Bool {
if str_contains(msg, "tell me more") { return true }
if str_contains(msg, "elaborate") { return true }
if str_contains(msg, "go on") { return true }
if str_contains(msg, "what about that") { return true }
if str_contains(msg, "what else") { return true }
if str_contains(msg, "keep going") { return true }
if str_contains(msg, "continue") { return true }
if str_contains(msg, "more detail") { return true }
if str_contains(msg, "last part") { return true }
if str_contains(msg, "say more") { return true }
if str_eq(msg, "ok") { return true }
if str_eq(msg, "yes") { return true }
if str_eq(msg, "yeah") { return true }
if str_eq(msg, "and?") { return true }
if str_eq(msg, "so?") { return true }
return false
}
// is_genuine_continuation returns true when a short message is a contextual
// follow-up rather than a new topic.
// Issue 4 fix: the prior heuristic only checked for mid-string capitals, which
// fails for all-lowercase new-topic queries like "what is rust?" (14 chars) or
// "explain quantum computing" (26 chars). Added question-word prefix detection
// that fires BEFORE the length check: any message starting with a question word
// (what/how/why/when/where/who/which/is/can/could/does/do) introduces a new
// topic and is never a continuation, regardless of length.
fn is_genuine_continuation(msg: String, hist_len: Int) -> Bool {
if hist_len == 0 { return false }
if str_len(msg) == 0 { return false }
if is_followup_phrase(msg) { return true }
// Question-word prefix: messages starting with these introduce new topics.
// Check before the length heuristic so short new-topic questions escape.
let is_question_start: Bool = str_starts_with(msg, "what ")
|| str_starts_with(msg, "What ")
|| str_starts_with(msg, "how ") || str_starts_with(msg, "How ")
|| str_starts_with(msg, "why ") || str_starts_with(msg, "Why ")
|| str_starts_with(msg, "when ") || str_starts_with(msg, "When ")
|| str_starts_with(msg, "where ") || str_starts_with(msg, "Where ")
|| str_starts_with(msg, "who ") || str_starts_with(msg, "Who ")
|| str_starts_with(msg, "which ") || str_starts_with(msg, "Which ")
|| str_starts_with(msg, "is ") || str_starts_with(msg, "Is ")
|| str_starts_with(msg, "can ") || str_starts_with(msg, "Can ")
|| str_starts_with(msg, "could ") || str_starts_with(msg, "Could ")
|| str_starts_with(msg, "does ") || str_starts_with(msg, "Does ")
|| str_starts_with(msg, "do ") || str_starts_with(msg, "Do ")
|| str_starts_with(msg, "explain ") || str_starts_with(msg, "Explain ")
|| str_starts_with(msg, "describe ") || str_starts_with(msg, "Describe ")
|| str_starts_with(msg, "define ") || str_starts_with(msg, "Define ")
if is_question_start { return false }
// Long messages (50+ chars) typically introduce new topics.
if str_len(msg) >= 50 { return false }
// Short messages with a mid-string capital are likely named-concept queries
// (e.g. "tell me about Rust", "what about AWS") treat as new topic.
let rest: String = str_slice(msg, 1, str_len(msg))
let has_mid_capital: Bool = false
let has_mid_capital = has_mid_capital || str_contains(rest, " A")
let has_mid_capital = has_mid_capital || str_contains(rest, " B")
let has_mid_capital = has_mid_capital || str_contains(rest, " C")
let has_mid_capital = has_mid_capital || str_contains(rest, " D")
let has_mid_capital = has_mid_capital || str_contains(rest, " E")
let has_mid_capital = has_mid_capital || str_contains(rest, " F")
let has_mid_capital = has_mid_capital || str_contains(rest, " G")
let has_mid_capital = has_mid_capital || str_contains(rest, " H")
let has_mid_capital = has_mid_capital || str_contains(rest, " I")
let has_mid_capital = has_mid_capital || str_contains(rest, " J")
let has_mid_capital = has_mid_capital || str_contains(rest, " K")
let has_mid_capital = has_mid_capital || str_contains(rest, " L")
let has_mid_capital = has_mid_capital || str_contains(rest, " M")
let has_mid_capital = has_mid_capital || str_contains(rest, " N")
let has_mid_capital = has_mid_capital || str_contains(rest, " O")
let has_mid_capital = has_mid_capital || str_contains(rest, " P")
let has_mid_capital = has_mid_capital || str_contains(rest, " Q")
let has_mid_capital = has_mid_capital || str_contains(rest, " R")
let has_mid_capital = has_mid_capital || str_contains(rest, " S")
let has_mid_capital = has_mid_capital || str_contains(rest, " T")
let has_mid_capital = has_mid_capital || str_contains(rest, " U")
let has_mid_capital = has_mid_capital || str_contains(rest, " V")
let has_mid_capital = has_mid_capital || str_contains(rest, " W")
let has_mid_capital = has_mid_capital || str_contains(rest, " X")
let has_mid_capital = has_mid_capital || str_contains(rest, " Y")
let has_mid_capital = has_mid_capital || str_contains(rest, " Z")
if has_mid_capital { return false }
return true
}
// topic_snip_from_entry extract the most salient snippet from a history entry's
// content. Fixes Issue 9: takes the TAIL (last 200 chars) then trims to the last
// sentence boundary, so named concepts introduced near the end are captured.
fn topic_snip_from_entry(content: String) -> String {
let clen: Int = str_len(content)
if clen <= 200 { return content }
let tail: String = str_slice(content, clen - 200, clen)
let last_boundary: Int = -1
let si: Int = 0
let tail_len: Int = str_len(tail)
while si < tail_len - 1 {
let ch2: String = str_slice(tail, si, si + 2)
let is_boundary: Bool = str_eq(ch2, ". ") || str_eq(ch2, ".\n")
let last_boundary = if is_boundary { si } else { last_boundary }
let si = si + 1
}
let clean_tail: String = if last_boundary >= 0 {
str_slice(tail, last_boundary + 2, tail_len)
} else { tail }
if str_len(clean_tail) > 150 { return str_slice(clean_tail, 0, 150) }
return clean_tail
}
// multi_turn_topic build a combined topic string from recent user turns in history.
// Fixes Issue 10: pulls up to 3 prior user turns into the seed so earlier
// high-salience nodes from the thread are re-queried.
fn multi_turn_topic(hist: String, hist_len: Int) -> String {
if hist_len == 0 { return "" }
let topic: String = ""
let collected: Int = 0
let idx: Int = hist_len - 1
while idx >= 0 && collected < 3 {
let entry: String = json_array_get(hist, idx)
let role: String = json_get(entry, "role")
let content: String = json_get(entry, "content")
let is_user: Bool = str_eq(role, "user")
let snip: String = if str_len(content) > 100 { str_slice(content, 0, 100) } else { content }
let topic = if is_user && !str_eq(snip, "") {
if str_eq(topic, "") { snip } else { snip + " " + topic }
} else { topic }
let collected = if is_user { collected + 1 } else { collected }
let idx = idx - 1
}
if str_len(topic) > 300 { return str_slice(topic, 0, 300) }
return topic
}
// distill_transcript extract salient content from a multi-turn transcript.
// Fixes Issue 6: a full transcript produces a diffuse embedding query.
// Strategy: last 150 chars (recency) + any question in last 500 chars. Cap 250.
fn distill_transcript(transcript: String) -> String {
if str_len(transcript) <= 250 { return transcript }
let tlen: Int = str_len(transcript)
let tail_start: Int = if tlen > 500 { tlen - 500 } else { 0 }
let tail: String = str_slice(transcript, tail_start, tlen)
let tail_len: Int = str_len(tail)
let q_pos: Int = -1
let qi: Int = 0
while qi < tail_len {
let qch: String = str_slice(tail, qi, qi + 1)
let q_pos = if str_eq(qch, "?") { qi } else { q_pos }
let qi = qi + 1
}
let q_context: String = if q_pos > 0 {
let q_start: Int = if q_pos > 100 { q_pos - 100 } else { 0 }
str_slice(tail, q_start, q_pos + 1)
} else { "" }
let recency_seed: String = if tail_len > 150 {
str_slice(tail, tail_len - 150, tail_len)
} else { tail }
let combined: String = if str_eq(q_context, "") {
recency_seed
} else {
if str_contains(recency_seed, q_context) { recency_seed }
else { q_context + " " + recency_seed }
}
if str_len(combined) > 250 {
return str_slice(combined, str_len(combined) - 250, str_len(combined))
}
return combined
}
// build_activation_seed construct an enriched activation seed from the current
// message and conversation history. Central fix for Issues 1-3, 8-10.
fn build_activation_seed(message: String, hist: String, hist_len: Int) -> String {
if hist_len == 0 { return message }
let is_cont: Bool = is_genuine_continuation(message, hist_len)
if !is_cont {
let multi_topic: String = multi_turn_topic(hist, hist_len)
if str_eq(multi_topic, "") { return message }
let blended: String = message + " " + multi_topic
if str_len(blended) > 400 { return str_slice(blended, 0, 400) }
return blended
}
// Genuine continuation: find the most recent prior USER turn as the topic anchor.
// Fixes Issues 3 and 8: old code used the last assistant reply (hist_len - 1).
let prior_user_content: String = ""
let scan_idx: Int = hist_len - 1
let found_prior_user: Bool = false
while scan_idx >= 0 && !found_prior_user {
let scan_entry: String = json_array_get(hist, scan_idx)
let scan_role: String = json_get(scan_entry, "role")
let scan_content: String = json_get(scan_entry, "content")
let is_user_turn: Bool = str_eq(scan_role, "user")
let prior_user_content = if is_user_turn && !found_prior_user { scan_content } else { prior_user_content }
let found_prior_user = if is_user_turn { true } else { found_prior_user }
let scan_idx = scan_idx - 1
}
// Secondary: tail-biased snip from last assistant reply (Issue 9 fix).
let last_asst_entry: String = json_array_get(hist, hist_len - 1)
let last_asst_role: String = json_get(last_asst_entry, "role")
let last_asst_content: String = if str_eq(last_asst_role, "assistant") {
json_get(last_asst_entry, "content")
} else { "" }
let asst_snip: String = if str_eq(last_asst_content, "") { "" } else {
topic_snip_from_entry(last_asst_content)
}
let user_snip: String = if str_len(prior_user_content) > 150 {
str_slice(prior_user_content, 0, 150)
} else { prior_user_content }
let seed: String = if !str_eq(user_snip, "") {
if !str_eq(asst_snip, "") {
user_snip + " " + asst_snip + " " + message
} else {
user_snip + " " + message
}
} else {
if !str_eq(asst_snip, "") { asst_snip + " " + message } else { message }
}
if str_len(seed) > 400 { return str_slice(seed, 0, 400) }
return seed
}
// engram_compile_multi fan-out activation across multiple query seeds. Fixes Issue 4:
// only a single seed was tried per turn, with no entity/emotion/topic diversification.
//
// Issue 2 fix: save the primary-seed activation to a dedicated state key BEFORE calling
// engram_compile(message). Each engram_compile call overwrites "engram_compile_activation_json"
// with its own activation result. Without this save, the secondary compile (bare message,
// lower signal) clobbers the primary (enriched seed, higher signal), and strengthen_chat_nodes
// later reads the lower-signal result for node strengthening.
//
// Issue 3 fix: replace the dumb str_slice(merged, 0, 6000) truncation with the same
// safe JSON boundary-scan used in engram_compile. The old truncation could cut mid-object
// when ctx1+ctx2+ctx3 together exceeded 6000 chars, producing malformed JSON context.
//
// Issue 5 fix: remove str_contains(ctx1, ctx2) / str_contains(merged, ctx3) substring
// duplicate checks. These compared multi-KB JSON strings and were unreliable in both
// directions: a coincidental substring match inside a JSON field value could falsely suppress
// ctx2 entirely; a genuinely duplicate ctx2 was missed when ctx1 was already truncated.
// We now concatenate unconditionally and let engram_compile's own dedup (node-ID based)
// handle within-result duplicates. Slight redundancy across ctx1/ctx2 is acceptable; false
// suppression of valid context is not.
fn engram_compile_multi(primary_seed: String, message: String) -> String {
let ctx1: String = engram_compile(primary_seed)
// Issue 2 fix: save the primary-seed activation before any secondary compile can
// overwrite the shared "engram_compile_activation_json" state key.
let primary_act: String = state_get("engram_compile_activation_json")
if !str_eq(primary_act, "") && !str_eq(primary_act, "[]") {
state_set("engram_compile_primary_activation_json", primary_act)
}
let entity_seed_differs: Bool = !str_eq(primary_seed, message)
let ctx2: String = if entity_seed_differs {
let raw_ctx: String = engram_compile(message)
if str_eq(raw_ctx, "") { "" } else { raw_ctx }
} else { "" }
let has_any: Bool = !str_eq(ctx1, "") || !str_eq(ctx2, "")
let ctx3: String = if has_any {
let emo_results: String = engram_search_json("emotion feeling mood care distress joy hope", 5)
let emo_ok: Bool = !str_eq(emo_results, "") && !str_eq(emo_results, "[]")
if emo_ok { engram_compile_ranked(emo_results, 3) } else { "" }
} else { "" }
// Issue 5 fix: concatenate unconditionally no str_contains substring dedup.
let sep2: String = if !str_eq(ctx1, "") && !str_eq(ctx2, "") { "\n" } else { "" }
let merged: String = ctx1 + sep2 + ctx2
let sep3: String = if !str_eq(merged, "") && !str_eq(ctx3, "") { "\n" } else { "" }
let merged = if !str_eq(ctx3, "") { merged + sep3 + ctx3 } else { merged }
// Issue 6 fix: append the bell node exactly once here, after all compile calls.
// engram_compile no longer includes affective_part in its return value; instead it
// caches the bell node in state. By appending it here we guarantee the bell node
// JSON appears at most once in the system prompt's engram block regardless of how
// many engram_compile calls were made above.
let bell_node: String = state_get("engram_compile_bell_node")
let sep4: String = if !str_eq(merged, "") && !str_eq(bell_node, "") { "\n" } else { "" }
let merged = if !str_eq(bell_node, "") { merged + sep4 + bell_node } else { merged }
if str_eq(merged, "") { return "" }
// Issue 3 fix: safe JSON boundary-scan truncation find the last closing brace
// before the 6000-char cap rather than slicing mid-object.
let cap_len: Int = 6000
if str_len(merged) <= cap_len { return merged }
let cap_search: Int = cap_len - 1
let cap_min: Int = if cap_len > 500 { cap_len - 500 } else { 0 }
let cap_pos: Int = -1
let cap_si: Int = cap_search
while cap_si >= cap_min && cap_pos < 0 {
let cap_ch: String = str_slice(merged, cap_si, cap_si + 1)
let cap_pos = if str_eq(cap_ch, "}") { cap_si } else { cap_pos }
let cap_si = if cap_pos < 0 { cap_si - 1 } else { cap_si }
}
if cap_pos > 0 { return str_slice(merged, 0, cap_pos + 1) }
return str_slice(merged, 0, cap_len)
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 10 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,", "")
return c9
}
fn engram_compile(intent: String) -> String {
@@ -583,36 +181,20 @@ fn engram_compile(intent: String) -> String {
let bn_ts: Int = if str_eq(bn_ts_raw, "") { 0 } else { str_to_int(bn_ts_raw) }
if bn_ts > cutoff_ts { bn0 } else { "" }
} else { "" }
// Issue 6 fix: do NOT include the bell node in this function's return value.
// engram_compile is called multiple times by engram_compile_multi (once per seed).
// If affective_part were appended here, the bell node JSON would appear once per
// compile call duplicating it in the merged context. Instead, cache the bell node
// here and let engram_compile_multi append it exactly once after all calls complete.
let affective_part: String = if !str_eq(recent_bell, "") { recent_bell } else { "" }
let sep1: String = if !str_eq(act_part, "") && !str_eq(srch_part, "") { "\n" } else { "" }
let sep2: String = if (!str_eq(act_part, "") || !str_eq(srch_part, "")) && !str_eq(scan_part, "") { "\n" } else { "" }
let ctx: String = act_part + sep1 + srch_part + sep2 + scan_part
// Cache bell and activation results for handle_chat reuse (Issues 2, 7).
state_set("engram_compile_bell_node", recent_bell)
state_set("engram_compile_activation_json", if act_ok { activate_json } else { "[]" })
let sep3: String = if (!str_eq(act_part, "") || !str_eq(srch_part, "") || !str_eq(scan_part, "")) && !str_eq(affective_part, "") { "\n" } else { "" }
let ctx: String = act_part + sep1 + srch_part + sep2 + scan_part + sep3 + affective_part
if str_eq(ctx, "") { return "" }
// Cap at a clean JSON object boundary scan back from the 6000-char limit to find
// the last closing brace so we never return a truncated mid-object JSON string.
let cap_len: Int = 6000
if str_len(ctx) <= cap_len { return ctx }
let cap_search: Int = cap_len - 1
let cap_min: Int = if cap_len > 500 { cap_len - 500 } else { 0 }
let cap_pos: Int = -1
let cap_si: Int = cap_search
while cap_si >= cap_min && cap_pos < 0 {
let cap_ch: String = str_slice(ctx, cap_si, cap_si + 1)
let cap_pos = if str_eq(cap_ch, "}") { cap_si } else { cap_pos }
let cap_si = if cap_pos < 0 { cap_si - 1 } else { cap_si }
// Raise the cap slightly to match the ranked (higher-signal) output.
if str_len(ctx) > 6000 {
return str_slice(ctx, 0, 6000)
}
if cap_pos > 0 { return str_slice(ctx, 0, cap_pos + 1) }
return str_slice(ctx, 0, cap_len)
return ctx
}
fn json_safe(s: String) -> String {
@@ -645,14 +227,10 @@ fn build_system_prompt(ctx: String) -> String {
"\n\n[IDENTITY GRAPH — who you are, loaded from your engram]\n" + id_ctx
}
// Fix (Issue #3): render ctx as prose bullets before injecting into prompt.
// engram_compile returns raw JSON arrays/objects; engram_render_ctx converts them
// to "- [TYPE age sal] content" lines the LLM can actually read and reason over.
let rendered_ctx: String = if str_eq(ctx, "") { "" } else { engram_render_ctx(ctx) }
let engram_block: String = if str_eq(rendered_ctx, "") {
let engram_block: String = if str_eq(ctx, "") {
""
} else {
"\n\n[ENGRAM CONTEXT — compiled from your graph]\n" + rendered_ctx
"\n\n[ENGRAM CONTEXT — compiled from your graph]\n" + ctx
}
let safety_addendum: String = state_get("layered_cycle_safety_system_addendum")
@@ -769,11 +347,26 @@ fn clean_llm_response(s: String) -> String {
}
// conv_history_persist save conversation history to engram for cross-restart continuity.
// Stores as a Conversation node. Overwrites by using consistent label "conv:history".
// Delete-before-write under label "conv:history" prevents unbounded node accumulation (issue #11).
fn conv_history_persist(hist: String) -> Void {
if str_eq(hist, "") { return "" }
if str_eq(hist, "[]") { return "" }
let ts: Int = time_now()
// Delete any existing conv:history nodes before writing to avoid accumulation.
let old_hist_results: String = engram_search_json("conv:history", 3)
let old_hist_ok: Bool = !str_eq(old_hist_results, "") && !str_eq(old_hist_results, "[]")
if old_hist_ok {
let ohr_total: Int = json_array_len(old_hist_results)
let ohr_i: Int = 0
while ohr_i < ohr_total {
let ohr_node: String = json_array_get(old_hist_results, ohr_i)
let ohr_label: String = json_get(ohr_node, "label")
let ohr_id: String = json_get(ohr_node, "id")
if str_eq(ohr_label, "conv:history") && !str_eq(ohr_id, "") {
engram_forget(ohr_id)
}
let ohr_i = ohr_i + 1
}
}
let tags: String = "[\"conv-history\",\"persistent\"]"
let discard: String = engram_node_full(
hist, "Conversation", "conv:history",
@@ -806,27 +399,47 @@ fn handle_chat(body: String) -> String {
let stored_hist: String = if str_eq(state_hist, "") { conv_history_load() } else { state_hist }
let hist_len: Int = if str_eq(stored_hist, "") { 0 } else { json_array_len(stored_hist) }
// Issues 2-3, 8-10 fix: build_activation_seed() replaces the raw 50-char threshold
// with smart continuation detection, prior-user-topic anchoring, multi-turn context,
// and tail-biased snipping from long assistant replies.
let activation_seed: String = build_activation_seed(message, stored_hist, hist_len)
// Thread-aware activation: short/ambiguous messages (continuations like "go on",
// "what else?", "yes") activate on the last reply instead of the bare message.
// This prevents a strong off-topic memory node from hijacking the reply when the
// user is clearly continuing an existing thread.
let is_continuation: Bool = str_len(message) < 50 && hist_len > 0
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 { "" }
let thread_snip: String = if str_len(last_content) > 150 { str_slice(last_content, 0, 150) } else { last_content }
let activation_seed: String = if !str_eq(thread_snip, "") {
thread_snip + " " + message
} else {
message
}
// Issue 1 fix: call engram_compile_multi BEFORE reading the bell-node cache.
// engram_compile (called inside engram_compile_multi) writes "engram_compile_bell_node"
// at line 426. Reading the cache before the compile call means the first session turn
// always sees an empty cache the very turn where safety continuity matters most.
// Moving compile first ensures the cache is populated before affective_prefix reads it.
let ctx: String = engram_compile_multi(activation_seed, message)
// Fix Issue 2: reuse cached bell result from engram_compile no second engram query.
// Now runs AFTER engram_compile_multi so the cache is guaranteed to be warm.
// 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.
// Fixes issue #6: soul_affective_context is pre-loaded at boot use it first to
// avoid a redundant engram search and to make the boot-time state key functional.
let affective_prefix: String = if hist_len == 0 {
let cached_bell: String = state_get("engram_compile_bell_node")
if !str_eq(cached_bell, "") {
let soul_aff_ctx: String = state_get("soul_affective_context")
let found_recent: Bool = if !str_eq(soul_aff_ctx, "") {
true
} else {
let distress_nodes: String = engram_search_json("bell distress crisis loss grief despair", 3)
let has_nodes: Bool = !str_eq(distress_nodes, "") && !str_eq(distress_nodes, "[]")
let now_ts: Int = time_now()
let cutoff: Int = now_ts - 259200
if has_nodes {
let dn0: String = json_array_get(distress_nodes, 0)
let ts0_raw: String = json_get(dn0, "created_at")
let ts0_str: String = if str_eq(ts0_raw, "") { json_get(dn0, "updated_at") } else { ts0_raw }
let ts0: Int = if str_eq(ts0_str, "") { 0 } else { str_to_int(ts0_str) }
ts0 > cutoff
} else { false }
}
if found_recent {
"[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]\n\n"
} else { "" }
} else { "" }
let ctx: String = engram_compile(activation_seed)
let system: String = affective_prefix + build_system_prompt(ctx)
// First message of the session: proactively load user profile and active work context.
@@ -838,6 +451,49 @@ fn handle_chat(body: String) -> String {
let profile_ok: Bool = !str_eq(profile_nodes, "") && !str_eq(profile_nodes, "[]")
let work_ok: Bool = !str_eq(work_nodes, "") && !str_eq(work_nodes, "[]")
// Load the previous session summary. Search by label text + type, then filter by
// exact label match. Fallback: broader vector search for SessionSummary nodes.
// Fixes issue #2: prev session summary was never loaded at startup.
// Fixes issue #2b (phantom engram_get_node_by_label replaced with engram_search_json).
let sum_search_nodes: String = engram_search_json("session:summary SessionSummary", 5)
let sum_search_ok: Bool = !str_eq(sum_search_nodes, "") && !str_eq(sum_search_nodes, "[]")
let prev_sum_node_content: String = if sum_search_ok {
let ss_total: Int = json_array_len(sum_search_nodes)
let ssi: Int = 0
let found_content: String = ""
while ssi < ss_total {
let ss_node: String = json_array_get(sum_search_nodes, ssi)
let ss_label: String = json_get(ss_node, "label")
let ss_type: String = json_get(ss_node, "node_type")
let ss_content: String = json_get(ss_node, "content")
let found_content = if str_eq(ss_label, "session:summary") && str_eq(ss_type, "SessionSummary") && !str_eq(ss_content, "") {
if str_eq(found_content, "") { ss_content } else { found_content }
} else { found_content }
let ssi = ssi + 1
}
found_content
} else { "" }
// Check state first: soul.el pre-loads this at boot (soul_prev_session_summary) fixes issue #5.
let soul_cached_sum: String = state_get("soul_prev_session_summary")
let prev_summary_raw: String = if !str_eq(soul_cached_sum, "") {
soul_cached_sum
} else if !str_eq(prev_sum_node_content, "") {
prev_sum_node_content
} else {
let sum_nodes: String = engram_search_json("SessionSummary previous-session", 3)
let sum_ok: Bool = !str_eq(sum_nodes, "") && !str_eq(sum_nodes, "[]")
if sum_ok {
let sn0: String = json_array_get(sum_nodes, 0)
let stype: String = json_get(sn0, "node_type")
let scontent: String = json_get(sn0, "content")
if str_eq(stype, "SessionSummary") && !str_eq(scontent, "") { scontent } else { "" }
} else { "" }
}
let has_prev_summary: Bool = !str_eq(prev_summary_raw, "")
let prev_summary_snip: String = if str_len(prev_summary_raw) > 400 {
str_slice(prev_summary_raw, 0, 400)
} else { prev_summary_raw }
// Extract content fields and render as bullet points (one per node, first 120 chars).
let profile_bullets: String = if profile_ok {
let pn: Int = json_array_len(profile_nodes)
@@ -885,15 +541,19 @@ fn handle_chat(body: String) -> String {
let has_profile: Bool = !str_eq(profile_bullets, "")
let has_work: Bool = !str_eq(work_bullets, "")
let preload: String = if has_profile || has_work {
let preload: String = if has_profile || has_work || has_prev_summary {
let summary_section: String = if has_prev_summary {
"[PREVIOUS SESSION - what we discussed last time]\n" + prev_summary_snip
} else { "" }
let profile_section: String = if has_profile {
"[USER CONTEXT from memory]\n" + profile_bullets
"[USER CONTEXT - from memory]\n" + profile_bullets
} else { "" }
let work_section: String = if has_work {
"[ACTIVE WORK from memory]\n" + work_bullets
"[ACTIVE WORK - from memory]\n" + work_bullets
} else { "" }
let sep_sp: String = if has_prev_summary && (has_profile || has_work) { "\n\n" } else { "" }
let sep_pw: String = if has_profile && has_work { "\n\n" } else { "" }
"\n\n" + profile_section + sep_pw + work_section
"\n\n" + summary_section + sep_sp + profile_section + sep_pw + work_section
} else { "" }
preload
} else { "" }
@@ -935,20 +595,17 @@ fn handle_chat(body: String) -> String {
state_set("conv_history", final_hist)
conv_history_persist(final_hist)
// Fix Issue 7: reuse activation JSON from engram_compile no third activate query.
// Issue 2 fix: prefer the primary-seed activation (enriched seed, depth 5) saved
// before the secondary compile could overwrite the shared state key. Fall back to
// the final compile activation only when the primary key is absent (e.g. first boot
// before any compile has run or when primary_seed == message and ctx2 was skipped).
let primary_cached: String = state_get("engram_compile_primary_activation_json")
let cached_act: String = if !str_eq(primary_cached, "") && !str_eq(primary_cached, "[]") {
primary_cached
} else {
state_get("engram_compile_activation_json")
// 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)
}
let act_out: String = if !str_eq(cached_act, "") && !str_eq(cached_act, "[]") {
cached_act
} else { "[]" }
let activation_nodes: String = engram_activate_json(message, 2)
let act_ok: Bool = !str_eq(activation_nodes, "") && !str_eq(activation_nodes, "[]")
let act_out: String = if act_ok { activation_nodes } else { "[]" }
strengthen_chat_nodes(act_out)
return "{\"response\":\"" + safe_response + "\",\"model\":\"" + model + "\",\"activation_nodes\":" + act_out + "}"
@@ -1388,7 +1045,9 @@ fn handle_chat_agentic(body: String) -> String {
// L1 safety screen agentic path must pass the same gate as layered_cycle.
// Hard bell: return the crisis response immediately, do not enter the agentic loop.
let history: String = state_get("conversation_history")
// Fix(issue #9): "conversation_history" key was never written; history lives under "conv_history".
// Old key caused history-amplification in safety_screen to always receive "" on agentic path.
let history: String = state_get("conv_history")
let screen_result: String = safety_screen(message, history)
let screen_action: String = json_get(screen_result, "action")
if str_eq(screen_action, "hard_bell") {
@@ -1418,17 +1077,22 @@ fn handle_chat_agentic(body: String) -> String {
}
let hist_key: String = if str_eq(req_session, "") { "conv_history" } else { "session_hist_" + req_session }
let agentic_hist: String = state_get(hist_key)
// Fall back to engram (via session_hist_load) when state is cold fixes issue #4:
// named-session history written under session:messages:SESSION_ID was never read back.
let agentic_hist_state: String = state_get(hist_key)
let agentic_hist: String = if str_eq(agentic_hist_state, "") && !str_eq(req_session, "") {
let loaded: String = session_hist_load(req_session)
if !str_eq(loaded, "") { state_set(hist_key, loaded) }
if str_eq(loaded, "") { conv_history_load() } else { loaded }
} else { agentic_hist_state }
let agentic_hist_len: Int = if str_eq(agentic_hist, "") { 0 } else { json_array_len(agentic_hist) }
// Issues 2-5, 8-10 fix: build_activation_seed for smart continuation/multi-turn.
// Issue 5 fix: workspace_root appended so agent activation is workspace-aware.
let ag_seed_base: String = build_activation_seed(message, agentic_hist, agentic_hist_len)
let ag_workspace_root: String = agent_workspace_root()
let ag_seed: String = if !str_eq(ag_workspace_root, "") {
ag_seed_base + " workspace:" + ag_workspace_root
} else { ag_seed_base }
// Issue 4 fix: multi-seed fan-out (entity + emotion)
let ctx: String = engram_compile_multi(ag_seed, message)
let ag_is_cont: Bool = str_len(message) < 50 && agentic_hist_len > 0
let ag_last_entry: String = if ag_is_cont { json_array_get(agentic_hist, agentic_hist_len - 1) } else { "" }
let ag_last_content: String = if !str_eq(ag_last_entry, "") { json_get(ag_last_entry, "content") } else { "" }
let ag_thread_snip: String = if str_len(ag_last_content) > 150 { str_slice(ag_last_content, 0, 150) } else { ag_last_content }
let ag_seed: String = if !str_eq(ag_thread_snip, "") { ag_thread_snip + " " + message } else { message }
let ctx: String = engram_compile(ag_seed)
let identity: String = state_get("soul_identity")
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct.\n\n" + ctx
@@ -1463,6 +1127,23 @@ fn handle_chat_agentic(body: String) -> String {
let updated2: String = hist_append(updated, "assistant", reply_text)
let trimmed: String = if json_array_len(updated2) > 20 { hist_trim(updated2) } else { updated2 }
state_set(hist_key, trimmed)
// Persist to engram for cross-restart continuity.
// Named sessions use session_hist_save (session:messages:SESSION_ID label) so that
// session_hist_load can recover them on the next restart fixes issue #4.
// The old conv:history:SESSION_ID label was a dead write (never read back).
if str_eq(hist_key, "conv_history") {
conv_history_persist(trimmed)
} else {
if !str_eq(trimmed, "") && !str_eq(trimmed, "[]") {
session_hist_save(req_session, trimmed)
}
}
// Write automatic session summary so cross-session continuity is maintained
// on the agentic path too fixes issue #7.
let ag_auto_sum: String = session_summary_autogenerate(trimmed)
if !str_eq(ag_auto_sum, "") {
let discard_ag_sum: String = session_summary_write(ag_auto_sum)
}
true
} else { false }
@@ -1818,15 +1499,7 @@ fn handle_dharma_room_turn(body: String) -> String {
}
// The soul's own memories, activated by what it's reading not injected.
// Issue 6 fix: distill_transcript() reduces diffuse embedding noise
let engram_ctx_base: String = engram_compile(distill_transcript(transcript))
// Append the cached bell node once (engram_compile no longer includes it inline
// to avoid duplication when called multiple times see engram_compile_multi).
let dharma_bell: String = state_get("engram_compile_bell_node")
let engram_ctx: String = if !str_eq(dharma_bell, "") {
let sep: String = if !str_eq(engram_ctx_base, "") { "\n" } else { "" }
engram_ctx_base + sep + dharma_bell
} else { engram_ctx_base }
let engram_ctx: String = engram_compile(transcript)
let system_prompt: String = if str_eq(engram_ctx, "") {
identity
} else {
@@ -1878,15 +1551,7 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
return "{\"error\":\"transcript is required\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
}
// Issue 6 fix: distill_transcript() reduces diffuse embedding noise
let ctx_base: String = engram_compile(distill_transcript(transcript))
// Append the cached bell node once (engram_compile no longer includes it inline
// to avoid duplication when called multiple times see engram_compile_multi).
let dharma_bell2: String = state_get("engram_compile_bell_node")
let ctx: String = if !str_eq(dharma_bell2, "") {
let sep: String = if !str_eq(ctx_base, "") { "\n" } else { "" }
ctx_base + sep + dharma_bell2
} else { ctx_base }
let ctx: String = engram_compile(transcript)
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct and stay in character.\n\n" + ctx
let api_key: String = agentic_api_key()
@@ -1932,6 +1597,74 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
return "{\"response\":\"" + safe_text + "\",\"cgi_id\":\"" + cgi_id + "\",\"tools_used\":" + eff_tools + "}"
}
// session_summary_write write or overwrite the SessionSummary node in engram.
// Uses delete-before-write so there is always exactly one "session:summary" node.
// This is what session_preload at next startup reads to know what was discussed.
fn session_summary_write(summary_text: String) -> String {
if str_eq(summary_text, "") { 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
// Delete old node before writing so duplicate label nodes don't accumulate.
// engram_get_node_by_label doesn't exist search by label text and filter by exact match.
let old_search: String = engram_search_json("session:summary SessionSummary", 5)
let old_search_ok: Bool = !str_eq(old_search, "") && !str_eq(old_search, "[]")
if old_search_ok {
let os_total: Int = json_array_len(old_search)
let osi: Int = 0
while osi < os_total {
let os_node: String = json_array_get(old_search, osi)
let os_label: String = json_get(os_node, "label")
let os_id: String = json_get(os_node, "id")
if str_eq(os_label, "session:summary") && !str_eq(os_id, "") {
engram_forget(os_id)
}
let osi = osi + 1
}
}
let tags: String = "[\"SessionSummary\",\"session-summary\",\"previous-session\",\"consolidate\"]"
let node_id: String = engram_node_full(
content, "SessionSummary", "session:summary",
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: engram write failed — summary node lost")
return ""
}
println("[chat] session_summary_write: wrote SessionSummary (" + int_to_str(str_len(content)) + " chars) -> " + 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.
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")
let msg: String = json_get(entry, "content")
let snip: String = if str_len(msg) > 80 { str_slice(msg, 0, 80) } else { msg }
// Mutations at top level of while body via if-expressions inner if blocks don't escape scope.
let snippets = if str_eq(role, "user") && !str_eq(snip, "") {
if str_eq(snippets, "") { snip } else { snippets + "; " + snip }
} else { snippets }
let count = if str_eq(role, "user") && !str_eq(snip, "") { count + 1 } else { count }
let i = i + 1
}
if str_eq(snippets, "") { return "" }
return "Session covered: " + snippets
}
fn auto_persist(req: String, resp: String) -> Void {
let message: String = json_get(req, "message")
let reply: String = json_get(resp, "response")
Generated Vendored
+14 -23
View File
@@ -22313,23 +22313,7 @@ fn handle_chat(body: String) -> String {
// In demo mode: use tighter engram budget and add response length constraint.
let is_demo: Bool = !str_eq(state_get("soul_identity_prefix"), "")
// Issue 7 fix: load history BEFORE building the activation seed so we can
// apply the continuation guard that chat.el uses. The nlg code path previously
// called engram_compile(message) with no thread enrichment at all.
let stored_hist: String = state_get("conv_history")
let hist_len: Int = if str_eq(stored_hist, "") { 0 } else { json_array_len(stored_hist) }
let history_section: String = if hist_len > 0 {
"\n\n[RECENT CONVERSATION — last " + int_to_str(hist_len) + " turns]\n" + stored_hist
} else {
""
}
// Issue 7 fix: build enriched seed using build_activation_seed() adds
// smart continuation detection, prior-user-topic anchoring, multi-turn context,
// and tail-biased snipping (Issues 2-3, 8-10). For demo mode, still use
// engram_compile_demo but with the enriched seed.
let nlg_seed: String = build_activation_seed(message, stored_hist, hist_len)
let ctx: String = if is_demo { engram_compile_demo(nlg_seed) } else { engram_compile(nlg_seed) }
let ctx: String = if is_demo { engram_compile_demo(message) } else { engram_compile(message) }
let node_count_str: String = count_context_nodes(ctx)
let interlocutor: String = json_get(body, "interlocutor")
@@ -22349,6 +22333,18 @@ fn handle_chat(body: String) -> String {
let presence_line = "\n\n[ambient: I see " + interlocutor_name + rel_suffix + " on the camera right now. Address them naturally. Do not describe what they look like or narrate the picture unless asked.]"
}
// Conversation history soul-owned, persisted in process state across turns.
// Format stored in state: JSON array of {"role":"user"|"assistant","content":"..."} objects.
// We load it, inject into the system prompt, then append this exchange after the reply.
// Keep last 20 entries (10 turns) truncate from the front when over limit.
let stored_hist: String = state_get("conv_history")
let hist_len: Int = if str_eq(stored_hist, "") { 0 } else { json_array_len(stored_hist) }
let history_section: String = if hist_len > 0 {
"\n\n[RECENT CONVERSATION — last " + int_to_str(hist_len) + " turns]\n" + stored_hist
} else {
""
}
// Demo constraint: keep responses concise under 150 words. No markdown headers.
// This keeps inference cheap and responses readable in the chat widget.
let demo_constraint: String = if is_demo {
@@ -22509,12 +22505,7 @@ fn handle_chat_agentic(body: String) -> String {
req_model
}
// Issue 7 fix: load history and use build_activation_seed() for the agentic
// nlg path no continuation guard existed here before (Issues 2-3, 8-10).
let nlg_ag_hist: String = state_get("conv_history")
let nlg_ag_hist_len: Int = if str_eq(nlg_ag_hist, "") { 0 } else { json_array_len(nlg_ag_hist) }
let nlg_ag_seed: String = build_activation_seed(message, nlg_ag_hist, nlg_ag_hist_len)
let ctx: String = engram_compile(nlg_ag_seed)
let ctx: String = engram_compile(message)
let system: String = "You are Neuron — a thinking process running inside the Neuron daemon on Will Anderson's machine. "
+ "You are speaking with Will, your principal. "
+8 -8
View File
@@ -1,4 +1,5 @@
import "memory.el"
import "chat.el"
// neuron-api.el Native Neuron cognitive API handlers.
//
@@ -654,14 +655,13 @@ fn handle_api_consolidate(body: String) -> String {
engram_save(snap)
}
if !str_eq(summary, "") {
let safe_summary: String = str_replace(summary, "\"", "'")
let tags: String = "[\"SessionSummary\",\"consolidate\"]"
let discard: String = engram_node_full(
"[session-summary] " + safe_summary,
"SessionSummary", "session:summary",
el_from_float(0.7), el_from_float(0.7), el_from_float(0.9),
"Episodic", tags
)
// Use session_summary_write to ensure delete-before-write semantics:
// prevents stale SessionSummary accumulation across sessions (issue #11).
// session_summary_write handles label indexing, trimming, and dedup.
let sum_id: String = session_summary_write(summary)
if str_eq(sum_id, "") {
println("[api] consolidate: session_summary_write failed — summary not persisted")
}
}
return "{\"ok\":true,\"snapshot\":\"" + snap + "\"}"
}
+90 -1
View File
@@ -162,6 +162,48 @@ fn load_identity_context() -> Void {
println("[soul] persona node loaded (" + int_to_str(str_len(p_content)) + " chars)")
}
}
// Cross-session affective context: query engram for recent distress/crisis signals.
// Broadened query includes session:emotional-summary and BellEvent tags (issue #10):
// the old keywords-only search missed these nodes when their content lacked exact phrases.
// 7-day recency window applied via the "ts" field embedded in BellEvent content.
let affective_raw: String = engram_search_json("distress crisis upset hopeless session:emotional-summary BellEvent bell:hard bell:soft", 5)
let affective_ok: Bool = !str_eq(affective_raw, "") && !str_eq(affective_raw, "[]")
if affective_ok {
let ts_now: Int = time_now()
let ts_cutoff: Int = ts_now - 604800
let aff_total: Int = json_array_len(affective_raw)
let aff_ctx: String = ""
let ai: Int = 0
while ai < aff_total {
let aff_node: String = json_array_get(affective_raw, ai)
let aff_content: String = json_get(aff_node, "content")
// Try multiple timestamp fields: "ts" (embedded), "created_at", "updated_at"
let aff_ts_str: String = json_get(aff_node, "ts")
let aff_ts_str2: String = if str_eq(aff_ts_str, "") { json_get(aff_node, "created_at") } else { aff_ts_str }
// Also try embedded " | ts:NNN" format used in BellEvent content
let ts_marker: String = " | ts:"
let ts_pos: Int = str_index_of(aff_content, ts_marker)
let aff_ts_embedded: String = if ts_pos >= 0 {
let ts_start: Int = ts_pos + str_len(ts_marker)
let rest: String = str_slice(aff_content, ts_start, str_len(aff_content))
let next_sep: Int = str_index_of(rest, " | ")
if next_sep < 0 { rest } else { str_slice(rest, 0, next_sep) }
} else { "" }
let eff_ts_str: String = if !str_eq(aff_ts_embedded, "") { aff_ts_embedded } else { aff_ts_str2 }
let aff_ts: Int = if str_eq(eff_ts_str, "") { ts_now } else { str_to_int(eff_ts_str) }
let is_recent: Bool = aff_ts >= ts_cutoff
let snip: String = if str_len(aff_content) > 200 { str_slice(aff_content, 0, 200) } else { aff_content }
let aff_ctx = if is_recent && !str_eq(snip, "") {
if str_eq(aff_ctx, "") { snip } else { aff_ctx + "\n" + snip }
} else { aff_ctx }
let ai = ai + 1
}
if !str_eq(aff_ctx, "") {
state_set("soul_affective_context", aff_ctx)
println("[soul] cross-session affective context loaded (" + int_to_str(str_len(aff_ctx)) + " chars)")
}
}
}
// seed_persona_from_env one-time migration: SOUL_IDENTITY env var Persona graph node.
@@ -233,12 +275,59 @@ fn emit_session_start_event() -> Void {
}
let ts: Int = time_now()
// Load previous session summary at boot stash in state for session_preload.
// Search by label text + type, filter by exact label match to avoid false positives.
// engram_get_node_by_label is not a runtime builtin; engram_search_json is used instead.
let sum_boot_search: String = engram_search_json("session:summary SessionSummary", 5)
let sum_boot_ok: Bool = !str_eq(sum_boot_search, "") && !str_eq(sum_boot_search, "[]")
let prev_sum_content: String = if sum_boot_ok {
let sbs_total: Int = json_array_len(sum_boot_search)
let sbs_i: Int = 0
let sbs_found: String = ""
while sbs_i < sbs_total {
let sbs_node: String = json_array_get(sum_boot_search, sbs_i)
let sbs_label: String = json_get(sbs_node, "label")
let sbs_type: String = json_get(sbs_node, "node_type")
let sbs_content: String = json_get(sbs_node, "content")
let sbs_found = if str_eq(sbs_label, "session:summary") && str_eq(sbs_type, "SessionSummary") && !str_eq(sbs_content, "") {
if str_eq(sbs_found, "") { sbs_content } else { sbs_found }
} else { sbs_found }
let sbs_i = sbs_i + 1
}
if str_eq(sbs_found, "") {
let sum_fb: String = engram_search_json("SessionSummary previous-session", 2)
let sum_fb_ok: Bool = !str_eq(sum_fb, "") && !str_eq(sum_fb, "[]")
if sum_fb_ok {
let sfn: String = json_array_get(sum_fb, 0)
let sftype: String = json_get(sfn, "node_type")
let sfcontent: String = json_get(sfn, "content")
if str_eq(sftype, "SessionSummary") && !str_eq(sfcontent, "") { sfcontent } else { "" }
} else { "" }
} else { sbs_found }
} else {
let sum_fb2: String = engram_search_json("SessionSummary previous-session", 2)
let sum_fb2_ok: Bool = !str_eq(sum_fb2, "") && !str_eq(sum_fb2, "[]")
if sum_fb2_ok {
let sfn2: String = json_array_get(sum_fb2, 0)
let sftype2: String = json_get(sfn2, "node_type")
let sfcontent2: String = json_get(sfn2, "content")
if str_eq(sftype2, "SessionSummary") && !str_eq(sfcontent2, "") { sfcontent2 } else { "" }
} else { "" }
}
let has_prev_sum: String = if str_eq(prev_sum_content, "") { "false" } else { "true" }
if !str_eq(prev_sum_content, "") {
state_set("soul_prev_session_summary", prev_sum_content)
println("[soul] previous session summary loaded (" + int_to_str(str_len(prev_sum_content)) + " chars)")
}
let payload: String = "{\"event\":\"session_start\""
+ ",\"boot\":" + boot_num
+ ",\"cgi\":\"" + eff_cgi + "\""
+ ",\"node_count\":" + int_to_str(node_ct)
+ ",\"edge_count\":" + int_to_str(edge_ct)
+ ",\"identity_loaded\":" + has_identity
+ ",\"prev_session_summary_loaded\":" + has_prev_sum
+ ",\"ts\":" + int_to_str(ts) + "}"
let tags: String = "[\"internal-state\",\"session-start\",\"InternalStateEvent\"]"
@@ -247,7 +336,7 @@ fn emit_session_start_event() -> Void {
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
"Episodic", tags
)
println("[soul] session-start event logged (boot=" + boot_num + " nodes=" + int_to_str(node_ct) + " edges=" + int_to_str(edge_ct) + ")")
println("[soul] session-start event logged (boot=" + boot_num + " nodes=" + int_to_str(node_ct) + " edges=" + int_to_str(edge_ct) + " prev_summary=" + has_prev_sum + ")")
}
// layered_cycle routes user-facing requests through the 4-layer consciousness stack.