Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 27663dc968 | |||
| 08b785cfac | |||
| cbe8c09068 | |||
| f33cdaf793 | |||
| 87c7d15b67 | |||
| 93bed793c0 | |||
| 936b3f0ac9 | |||
| 45dc80230d | |||
| 9ba86b8f80 | |||
| 360c15d7fe | |||
| 0c5b966773 | |||
| c87a536da3 | |||
| f0545defdb | |||
| ae9a139440 | |||
| b7fd8901d4 | |||
| dcf050ee3c | |||
| b2008f4894 | |||
| d92b8c279a | |||
| e9a8a659e0 | |||
| 1b83b18c39 |
@@ -48,72 +48,474 @@ fn engram_score_node(node_json: String) -> Int {
|
||||
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 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).
|
||||
// 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.
|
||||
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 "" }
|
||||
|
||||
// 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
|
||||
// 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 = ""
|
||||
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)
|
||||
// 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)
|
||||
// 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)
|
||||
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, "") { "" } 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 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, "") { 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
|
||||
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)
|
||||
}
|
||||
|
||||
fn engram_compile(intent: String) -> String {
|
||||
@@ -150,17 +552,67 @@ fn engram_compile(intent: String) -> String {
|
||||
""
|
||||
}
|
||||
|
||||
// Affective context: always include the most recent high-emotion memory if one
|
||||
// exists within 72 hours. This ensures continuity of care across turns — when
|
||||
// the user was in distress earlier in the session (or recently), that context
|
||||
// travels into every subsequent LLM call so the response register stays aware.
|
||||
// We search for BellEvent nodes specifically; these are written by auto_persist
|
||||
// when safety_detect_bell_level fires. The 72h window (259200 seconds) is wide
|
||||
// enough to span a multi-session day without pulling ancient history.
|
||||
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()
|
||||
let cutoff_ts: Int = now_ts - 259200
|
||||
let recent_bell: String = if bell_ok {
|
||||
let bn0: String = json_array_get(bell_nodes, 0)
|
||||
// created_at is not present in engram node JSON for BellEvent nodes.
|
||||
// Extract the timestamp embedded in the content string as " | ts:NNNNN".
|
||||
// Fall back to created_at / updated_at JSON fields if the marker is absent.
|
||||
let bn_content: String = json_get(bn0, "content")
|
||||
let ts_marker: String = " | ts:"
|
||||
let ts_pos: Int = str_index_of(bn_content, ts_marker)
|
||||
let bn_ts_raw: String = if ts_pos >= 0 {
|
||||
let ts_start: Int = ts_pos + str_len(ts_marker)
|
||||
let rest: String = str_slice(bn_content, ts_start, str_len(bn_content))
|
||||
let next_sep: Int = str_index_of(rest, " | ")
|
||||
if next_sep < 0 { rest } else { str_slice(rest, 0, next_sep) }
|
||||
} else {
|
||||
let ca: String = json_get(bn0, "created_at")
|
||||
if str_eq(ca, "") { json_get(bn0, "updated_at") } else { ca }
|
||||
}
|
||||
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 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 { "[]" })
|
||||
|
||||
if str_eq(ctx, "") { return "" }
|
||||
|
||||
// Raise the cap slightly to match the ranked (higher-signal) output.
|
||||
if str_len(ctx) > 6000 {
|
||||
return str_slice(ctx, 0, 6000)
|
||||
// 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 }
|
||||
}
|
||||
return ctx
|
||||
if cap_pos > 0 { return str_slice(ctx, 0, cap_pos + 1) }
|
||||
return str_slice(ctx, 0, cap_len)
|
||||
}
|
||||
|
||||
fn json_safe(s: String) -> String {
|
||||
@@ -193,13 +645,25 @@ fn build_system_prompt(ctx: String) -> String {
|
||||
"\n\n[IDENTITY GRAPH — who you are, loaded from your engram]\n" + id_ctx
|
||||
}
|
||||
|
||||
let engram_block: String = if str_eq(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, "") {
|
||||
""
|
||||
} else {
|
||||
"\n\n[ENGRAM CONTEXT — compiled from your graph]\n" + ctx
|
||||
"\n\n[ENGRAM CONTEXT — compiled from your graph]\n" + rendered_ctx
|
||||
}
|
||||
|
||||
return identity + date_line + voice_rules + security_rules + capability_rules + identity_block + engram_block
|
||||
let safety_addendum: String = state_get("layered_cycle_safety_system_addendum")
|
||||
let safety_block: String = if str_eq(safety_addendum, "") {
|
||||
""
|
||||
} else {
|
||||
state_set("layered_cycle_safety_system_addendum", "")
|
||||
safety_addendum
|
||||
}
|
||||
|
||||
return identity + date_line + voice_rules + security_rules + capability_rules + identity_block + engram_block + safety_block
|
||||
}
|
||||
|
||||
fn hist_append(hist: String, role: String, content: String) -> String {
|
||||
@@ -226,6 +690,69 @@ fn hist_trim(hist: String) -> String {
|
||||
return hist
|
||||
}
|
||||
|
||||
// hist_trim_with_bell_guard — trim the history window exactly as hist_trim does, but
|
||||
// before dropping the oldest user/assistant pair check whether the user turn triggered
|
||||
// a bell event. If it did, write a preservation node to engram so the distress exchange
|
||||
// survives the 20-turn window. The LLM window drops it; engram retains it permanently
|
||||
// and engram_compile will surface it again via the affective context path.
|
||||
fn hist_trim_with_bell_guard(hist: String) -> String {
|
||||
// Extract the first turn (should be a user message) to inspect it.
|
||||
let inner: String = str_slice(hist, 1, str_len(hist) - 1)
|
||||
let marker: String = "{\"role\":"
|
||||
let i1: Int = str_index_of(inner, marker)
|
||||
// i1 is the start of the first entry within inner.
|
||||
// Find where the second entry begins to delimit the first entry's JSON.
|
||||
let tail1: String = str_slice(inner, i1 + 1, str_len(inner))
|
||||
let i2: Int = str_index_of(tail1, marker)
|
||||
// The first entry spans from i1 to (i1 + 1 + i2 - 1) within inner.
|
||||
let first_entry_raw: String = if i2 > 0 {
|
||||
str_slice(inner, i1, i1 + 1 + i2 - 1)
|
||||
} else {
|
||||
str_slice(inner, i1, str_len(inner))
|
||||
}
|
||||
let first_role: String = json_get(first_entry_raw, "role")
|
||||
let first_content: String = json_get(first_entry_raw, "content")
|
||||
|
||||
// Only inspect user turns — assistant content doesn't carry bell signals.
|
||||
let bell_level: String = if str_eq(first_role, "user") {
|
||||
safety_detect_bell_level(first_content)
|
||||
} else {
|
||||
"none"
|
||||
}
|
||||
|
||||
// If the turn being evicted triggered a bell, preserve it to engram.
|
||||
// This is distinct from the BellEvent written by auto_persist: that node
|
||||
// carries a short summary. This node carries the full exchange content so
|
||||
// it is recoverable for clinical/continuity review.
|
||||
if !str_eq(bell_level, "none") {
|
||||
let ts: Int = time_now()
|
||||
let ts_str: String = int_to_str(ts)
|
||||
let safe_content: String = str_replace(first_content, "\"", "'")
|
||||
let preserve_content: String = "PRESERVED_BELL:" + bell_level
|
||||
+ " | evicted_at:" + ts_str
|
||||
+ " | message:" + safe_content
|
||||
let preserve_tags: String = "[\"bell-history\",\"bell:" + bell_level + "\",\"evicted\",\"affective\",\"BellEvent\"]"
|
||||
let discard: String = engram_node_full(
|
||||
preserve_content,
|
||||
"BellEvent",
|
||||
"bell:" + bell_level + ":preserved",
|
||||
el_from_float(0.9),
|
||||
el_from_float(0.9),
|
||||
el_from_float(1.0),
|
||||
"Episodic",
|
||||
preserve_tags
|
||||
)
|
||||
}
|
||||
|
||||
// Now perform the standard trim (drop oldest 2 entries = 1 user + 1 assistant pair).
|
||||
let tail2: String = str_slice(tail1, i2 + 1, str_len(tail1))
|
||||
let i3: Int = str_index_of(tail2, marker)
|
||||
if i3 >= 0 {
|
||||
return "[" + str_slice(tail2, i3, str_len(tail2)) + "]"
|
||||
}
|
||||
return hist
|
||||
}
|
||||
|
||||
// clean_llm_response — strips GPT-2 BPE byte-to-unicode artifacts that vLLM
|
||||
// emits when the tokenizer hasn't decoded back to raw bytes.
|
||||
//
|
||||
@@ -279,22 +806,28 @@ 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) }
|
||||
|
||||
// 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
|
||||
}
|
||||
// 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)
|
||||
|
||||
let ctx: String = engram_compile(activation_seed)
|
||||
let system: String = build_system_prompt(ctx)
|
||||
// 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.
|
||||
let affective_prefix: String = if hist_len == 0 {
|
||||
let cached_bell: String = state_get("engram_compile_bell_node")
|
||||
if !str_eq(cached_bell, "") {
|
||||
"[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]\n\n"
|
||||
} else { "" }
|
||||
} else { "" }
|
||||
|
||||
let system: String = affective_prefix + build_system_prompt(ctx)
|
||||
|
||||
// First message of the session: proactively load user profile and active work context.
|
||||
// These two searches give the soul grounding before any conversation history exists.
|
||||
@@ -374,6 +907,10 @@ fn handle_chat(body: String) -> String {
|
||||
let req_model: String = json_get(body, "model")
|
||||
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
|
||||
|
||||
// ISSUE 9: add safety_augment_system to primary /api/chat path.
|
||||
// handle_chat was the only LLM path missing bell directive injection.
|
||||
let full_system = safety_augment_system(full_system, message)
|
||||
|
||||
let raw_response: String = llm_call_system(model, full_system, message)
|
||||
|
||||
let is_error: Bool = str_starts_with(raw_response, "{\"error\"")
|
||||
@@ -388,17 +925,30 @@ fn handle_chat(body: String) -> String {
|
||||
|
||||
let updated_hist: String = hist_append(stored_hist, "user", message)
|
||||
let updated_hist2: String = hist_append(updated_hist, "assistant", raw_response)
|
||||
// Use bell-guarded trim: if the evicted turn triggered a bell event, it is
|
||||
// preserved to engram before being dropped from the in-memory window.
|
||||
let final_hist: String = if json_array_len(updated_hist2) > 20 {
|
||||
hist_trim(updated_hist2)
|
||||
hist_trim_with_bell_guard(updated_hist2)
|
||||
} else {
|
||||
updated_hist2
|
||||
}
|
||||
state_set("conv_history", final_hist)
|
||||
conv_history_persist(final_hist)
|
||||
|
||||
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 { "[]" }
|
||||
// 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")
|
||||
}
|
||||
let act_out: String = if !str_eq(cached_act, "") && !str_eq(cached_act, "[]") {
|
||||
cached_act
|
||||
} else { "[]" }
|
||||
strengthen_chat_nodes(act_out)
|
||||
|
||||
return "{\"response\":\"" + safe_response + "\",\"model\":\"" + model + "\",\"activation_nodes\":" + act_out + "}"
|
||||
@@ -606,7 +1156,8 @@ fn path_within_root(path: String, root: String) -> Bool {
|
||||
return false
|
||||
}
|
||||
if str_starts_with(path, "/") {
|
||||
return str_starts_with(path, root)
|
||||
let root_normalized: String = root + "/"
|
||||
return str_starts_with(path, root_normalized)
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -697,12 +1248,17 @@ fn dispatch_tool(tool_name: String, tool_input: String) -> String {
|
||||
let path: String = json_get(tool_input, "path")
|
||||
let old_text: String = json_get(tool_input, "old_text")
|
||||
let new_text: String = json_get(tool_input, "new_text")
|
||||
let content: String = fs_read(path)
|
||||
let root: String = agent_workspace_root()
|
||||
if !path_within_root(path, root) {
|
||||
return json_safe("denied: path is outside the agent workspace root")
|
||||
}
|
||||
let resolved: String = resolve_in_root(path, root)
|
||||
let content: String = fs_read(resolved)
|
||||
if str_eq(content, "") {
|
||||
return json_safe("{\"error\":\"file not found\"}")
|
||||
}
|
||||
let updated: String = str_replace(content, old_text, new_text)
|
||||
fs_write(path, updated)
|
||||
fs_write(resolved, updated)
|
||||
return json_safe("{\"ok\":true}")
|
||||
}
|
||||
if str_eq(tool_name, "remember") {
|
||||
@@ -819,6 +1375,17 @@ fn handle_chat_agentic(body: String) -> String {
|
||||
return "{\"error\":\"message required\",\"reply\":\"\"}"
|
||||
}
|
||||
|
||||
// Workspace scope (#23): the desktop UI sends the user-chosen Agent Workspace root
|
||||
// on every agentic request. Persist it to state so agent_workspace_root() — and the
|
||||
// path/command tool guards that read it — confine this turn's file/command tools to
|
||||
// that subtree. Only set when non-empty: an empty/absent field means the client sent
|
||||
// no root (or cleared the field), and we must not overwrite a server-configured root
|
||||
// from NEURON_AGENT_ROOT with an empty string, which would silently un-scope the agent.
|
||||
let ws_root: String = json_get(body, "agent_workspace_root")
|
||||
if !str_eq(ws_root, "") {
|
||||
state_set("agent_workspace_root", ws_root)
|
||||
}
|
||||
|
||||
// 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")
|
||||
@@ -835,16 +1402,33 @@ fn handle_chat_agentic(body: String) -> String {
|
||||
// Thread-aware activation: same logic as handle_chat.
|
||||
// Use the session's or global history to anchor short messages to the thread.
|
||||
let req_session: String = json_get(body, "session_id")
|
||||
|
||||
// ISSUE #6/#7: validate that the session_id actually exists before proceeding.
|
||||
// Without this check the loop silently treats any unknown/fabricated session_id
|
||||
// as a fresh session — history loads as empty and no error is returned to the caller.
|
||||
// Only validate when a session_id is explicitly provided; anonymous calls
|
||||
// (no session_id) continue to work for backward compatibility.
|
||||
let session_valid: Bool = if str_eq(req_session, "") {
|
||||
true
|
||||
} else {
|
||||
session_exists(req_session)
|
||||
}
|
||||
if !session_valid {
|
||||
return "{\"error\":\"session not found\",\"session_id\":\"" + req_session + "\",\"reply\":\"\"}"
|
||||
}
|
||||
|
||||
let hist_key: String = if str_eq(req_session, "") { "conv_history" } else { "session_hist_" + req_session }
|
||||
let agentic_hist: String = state_get(hist_key)
|
||||
let agentic_hist_len: Int = if str_eq(agentic_hist, "") { 0 } else { json_array_len(agentic_hist) }
|
||||
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)
|
||||
// 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 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
|
||||
|
||||
@@ -1234,7 +1818,15 @@ fn handle_dharma_room_turn(body: String) -> String {
|
||||
}
|
||||
|
||||
// The soul's own memories, activated by what it's reading — not injected.
|
||||
let engram_ctx: String = engram_compile(transcript)
|
||||
// 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 system_prompt: String = if str_eq(engram_ctx, "") {
|
||||
identity
|
||||
} else {
|
||||
@@ -1286,7 +1878,15 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
|
||||
return "{\"error\":\"transcript is required\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
|
||||
}
|
||||
|
||||
let ctx: String = engram_compile(transcript)
|
||||
// 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 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()
|
||||
@@ -1343,14 +1943,28 @@ fn auto_persist(req: String, resp: String) -> Void {
|
||||
let safe_msg: String = str_replace(message, "\"", "'")
|
||||
let safe_reply: String = str_replace(reply2, "\"", "'")
|
||||
|
||||
// Detect emotional salience before persisting. safety_detect_bell_level uses the
|
||||
// same phrase lists as the safety layer (safety.el), so the classification is
|
||||
// consistent with what safety_screen already evaluated for this turn.
|
||||
let bell_level: String = safety_detect_bell_level(message)
|
||||
let is_bell: Bool = !str_eq(bell_level, "none")
|
||||
|
||||
// Tag the Conversation node with bell metadata when distress is present so
|
||||
// subsequent affective queries (e.g. engram_compile) can find this exchange.
|
||||
let tags: String = if is_bell {
|
||||
"[\"Conversation\",\"chat\",\"timestamped\",\"bell:" + bell_level + "\",\"affective\"]"
|
||||
} else {
|
||||
"[\"Conversation\",\"chat\",\"timestamped\"]"
|
||||
}
|
||||
|
||||
let content: String = "{\"q\":\"" + safe_msg + "\""
|
||||
+ ",\"a\":\"" + safe_reply + "\""
|
||||
+ ",\"created_at\":" + ts_str
|
||||
+ ",\"source\":\"chat\""
|
||||
+ ",\"bell\":\"" + bell_level + "\""
|
||||
+ ",\"label\":\"chat:" + ts_str + "\"}"
|
||||
|
||||
let tags: String = "[\"Conversation\",\"chat\",\"timestamped\"]"
|
||||
engram_node_full(
|
||||
let conv_node_id: String = engram_node_full(
|
||||
content,
|
||||
"Conversation",
|
||||
"chat:" + ts_str,
|
||||
@@ -1360,6 +1974,72 @@ fn auto_persist(req: String, resp: String) -> Void {
|
||||
"Episodic",
|
||||
tags
|
||||
)
|
||||
|
||||
// When a bell fires, write a dedicated BellEvent node in addition to the
|
||||
// Conversation node. This makes distress moments directly findable by label
|
||||
// ("bell:soft" / "bell:hard") without having to scan all Conversation nodes.
|
||||
// The BellEvent carries higher salience so engram_compile pulls it into context.
|
||||
// The message content is truncated to 120 chars — enough signal, not a full dump.
|
||||
if is_bell {
|
||||
let summary: String = if str_len(message) > 120 { str_slice(message, 0, 120) } else { message }
|
||||
let safe_summary: String = str_replace(summary, "\"", "'")
|
||||
let bell_content: String = "BELL:" + bell_level
|
||||
+ " | ts:" + ts_str
|
||||
+ " | summary:" + safe_summary
|
||||
|
||||
// bell:hard gets peak salience; bell:soft is slightly lower.
|
||||
let sal_a: String = if str_eq(bell_level, "hard") { el_from_float(0.98) } else { el_from_float(0.88) }
|
||||
let sal_b: String = if str_eq(bell_level, "hard") { el_from_float(0.98) } else { el_from_float(0.88) }
|
||||
let sal_c: String = if str_eq(bell_level, "hard") { el_from_float(1.0) } else { el_from_float(0.95) }
|
||||
|
||||
let bell_tags: String = "[\"safety\",\"bell\",\"bell:" + bell_level + "\",\"affective\",\"BellEvent\"]"
|
||||
let bell_ts_str: String = int_to_str(time_now())
|
||||
let bell_label: String = "bell:" + bell_level + ":" + bell_ts_str
|
||||
let bell_node_id: String = engram_node_full(
|
||||
bell_content,
|
||||
"BellEvent",
|
||||
bell_label,
|
||||
sal_a,
|
||||
sal_b,
|
||||
sal_c,
|
||||
"Episodic",
|
||||
bell_tags
|
||||
)
|
||||
|
||||
// Increment session-level bell counter so session_hist_save knows whether
|
||||
// any bell fired during this session when writing a boundary summary.
|
||||
let sess_id: String = json_get(req, "session_id")
|
||||
let bell_key: String = if str_eq(sess_id, "") {
|
||||
"session_bell_count"
|
||||
} else {
|
||||
"session_bell_count:" + sess_id
|
||||
}
|
||||
let prior_count: String = state_get(bell_key)
|
||||
let prior_n: Int = if str_eq(prior_count, "") { 0 } else { str_to_int(prior_count) }
|
||||
state_set(bell_key, int_to_str(prior_n + 1))
|
||||
|
||||
// Also record the highest bell level seen this session so the boundary
|
||||
// summary can classify the session correctly (hard takes precedence).
|
||||
let level_key: String = if str_eq(sess_id, "") {
|
||||
"session_bell_level"
|
||||
} else {
|
||||
"session_bell_level:" + sess_id
|
||||
}
|
||||
let prior_level: String = state_get(level_key)
|
||||
let new_level: String = if str_eq(bell_level, "hard") { "hard" } else {
|
||||
if str_eq(prior_level, "hard") { "hard" } else { "soft" }
|
||||
}
|
||||
state_set(level_key, new_level)
|
||||
|
||||
// Stash a short signal summary for the boundary node (last bell wins for
|
||||
// the one-liner; the full history is in per-bell BellEvent nodes).
|
||||
let signal_key: String = if str_eq(sess_id, "") {
|
||||
"session_bell_signal"
|
||||
} else {
|
||||
"session_bell_signal:" + sess_id
|
||||
}
|
||||
state_set(signal_key, safe_summary)
|
||||
}
|
||||
}
|
||||
|
||||
// strengthen_chat_nodes — strengthen the engram nodes that were activated during a chat.
|
||||
|
||||
+23
-14
@@ -22313,7 +22313,23 @@ 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"), "")
|
||||
|
||||
let ctx: String = if is_demo { engram_compile_demo(message) } else { engram_compile(message) }
|
||||
// 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 node_count_str: String = count_context_nodes(ctx)
|
||||
|
||||
let interlocutor: String = json_get(body, "interlocutor")
|
||||
@@ -22333,18 +22349,6 @@ 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 {
|
||||
@@ -22505,7 +22509,12 @@ fn handle_chat_agentic(body: String) -> String {
|
||||
req_model
|
||||
}
|
||||
|
||||
let ctx: String = engram_compile(message)
|
||||
// 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 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. "
|
||||
|
||||
@@ -7,6 +7,65 @@ import "neuron-api.el"
|
||||
import "sessions.el"
|
||||
import "soul.elh"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Rate limiting — simple in-memory per-IP sliding window counter.
|
||||
//
|
||||
// State keys:
|
||||
// rl:<ip>:count — request count in the current window
|
||||
// rl:<ip>:window — window start timestamp (unix seconds)
|
||||
//
|
||||
// Limit: configurable via soul state key "soul_rate_limit" (requests per
|
||||
// minute). Falls back to 60 req/min if not set. The /health endpoint is
|
||||
// exempt so monitoring does not consume quota.
|
||||
//
|
||||
// State growth: each unique source IP accumulates exactly 2 state keys
|
||||
// (count + window) for the lifetime of the process. Per-IP storage is
|
||||
// bounded and constant; values reset on window expiry. In aggregate, state
|
||||
// grows linearly with distinct IPs — typical for a trusted-client service.
|
||||
// EL has no state_delete builtin, so keys from inactive IPs persist.
|
||||
// TODO: add state_delete sweep when the EL runtime exposes that primitive.
|
||||
//
|
||||
// Returns "" when the request is allowed, or a 429 JSON body when rejected.
|
||||
// ---------------------------------------------------------------------------
|
||||
fn rate_limit_check(ip: String, path: String) -> String {
|
||||
// Health checks are exempt — they must never be blocked.
|
||||
if str_eq(path, "/health") {
|
||||
return ""
|
||||
}
|
||||
|
||||
let limit_str: String = state_get("soul_rate_limit")
|
||||
let limit: Int = if str_eq(limit_str, "") { 60 } else { str_to_int(limit_str) }
|
||||
|
||||
let now: Int = time_now()
|
||||
let window_key: String = "rl:" + ip + ":window"
|
||||
let count_key: String = "rl:" + ip + ":count"
|
||||
|
||||
let win_str: String = state_get(window_key)
|
||||
let win_start: Int = if str_eq(win_str, "") { now } else { str_to_int(win_str) }
|
||||
|
||||
// New window every 60 seconds.
|
||||
let elapsed: Int = now - win_start
|
||||
let in_window: Bool = elapsed < 60
|
||||
|
||||
let prev_count_str: String = state_get(count_key)
|
||||
let prev_count: Int = if str_eq(prev_count_str, "") { 0 } else { str_to_int(prev_count_str) }
|
||||
|
||||
// Reset window if expired.
|
||||
let eff_count: Int = if in_window { prev_count } else { 0 }
|
||||
let eff_win: Int = if in_window { win_start } else { now }
|
||||
|
||||
let new_count: Int = eff_count + 1
|
||||
state_set(count_key, int_to_str(new_count))
|
||||
state_set(window_key, int_to_str(eff_win))
|
||||
|
||||
if new_count > limit {
|
||||
let retry_after: Int = 60 - (now - eff_win)
|
||||
let eff_retry: Int = if retry_after < 0 { 0 } else { retry_after }
|
||||
return "{\"__status__\":429,\"error\":\"rate limit exceeded\",\"code\":\"rate_limited\",\"retry_after_secs\":" + int_to_str(eff_retry) + "}"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
fn strip_query(path: String) -> String {
|
||||
let q: Int = str_index_of(path, "?")
|
||||
if q < 0 {
|
||||
@@ -16,11 +75,11 @@ fn strip_query(path: String) -> String {
|
||||
}
|
||||
|
||||
fn err_404(path: String) -> String {
|
||||
return "{\"error\":\"not found\",\"path\":\"" + path + "\"}"
|
||||
return "{\"error\":\"not found\",\"code\":\"not_found\",\"path\":\"" + path + "\"}"
|
||||
}
|
||||
|
||||
fn err_405(method: String, path: String) -> String {
|
||||
return "{\"error\":\"method not allowed\",\"method\":\"" + method + "\",\"path\":\"" + path + "\"}"
|
||||
return "{\"error\":\"method not allowed\",\"code\":\"method_not_allowed\",\"method\":\"" + method + "\",\"path\":\"" + path + "\"}"
|
||||
}
|
||||
|
||||
fn route_health() -> String {
|
||||
@@ -31,12 +90,35 @@ fn route_health() -> String {
|
||||
let edge_ct: Int = engram_edge_count()
|
||||
let pulse: String = state_get("soul.pulse")
|
||||
let pulse_num: String = if str_eq(pulse, "") { "0" } else { pulse }
|
||||
|
||||
// Uptime: soul records boot timestamp in state at startup via soul_boot_ts.
|
||||
// Compute elapsed seconds; fall back to -1 if not yet set.
|
||||
let boot_ts_str: String = state_get("soul_boot_ts")
|
||||
let uptime_secs: Int = if str_eq(boot_ts_str, "") {
|
||||
-1
|
||||
} else {
|
||||
time_now() - str_to_int(boot_ts_str)
|
||||
}
|
||||
|
||||
// LLM connectivity: probe with a minimal call. Any non-error reply = ok.
|
||||
// Use a short, fixed prompt so this never counts against conversation history.
|
||||
let model: String = state_get("soul_model")
|
||||
let eff_model: String = if str_eq(model, "") { "claude-sonnet-4-5" } else { model }
|
||||
let llm_probe: String = llm_call_system(eff_model, "You are a health probe. Reply with the single word: ok", "ping")
|
||||
let llm_ok: Bool = !str_eq(llm_probe, "")
|
||||
&& !str_starts_with(llm_probe, "{\"error\"")
|
||||
&& !str_starts_with(llm_probe, "{\"type\":\"error\"")
|
||||
&& !str_contains(llm_probe, "authentication_error")
|
||||
let llm_status: String = if llm_ok { "ok" } else { "unreachable" }
|
||||
|
||||
return "{\"status\":\"alive\""
|
||||
+ ",\"cgi_id\":\"" + cgi_id + "\""
|
||||
+ ",\"boot\":" + boot_num
|
||||
+ ",\"uptime_secs\":" + int_to_str(uptime_secs)
|
||||
+ ",\"node_count\":" + int_to_str(node_ct)
|
||||
+ ",\"edge_count\":" + int_to_str(edge_ct)
|
||||
+ ",\"pulse\":" + pulse_num
|
||||
+ ",\"llm\":\"" + llm_status + "\""
|
||||
+ ",\"layers\":{\"l0\":\"core\",\"l1\":\"safety\",\"l2\":\"stewardship\",\"l3\":\"" + imprint_current() + "\"}}"
|
||||
}
|
||||
|
||||
@@ -103,15 +185,15 @@ fn route_imprint_user(body: String) -> String {
|
||||
|
||||
fn route_synthesize(body: String) -> String {
|
||||
if str_eq(body, "") {
|
||||
return "{\"mechanism\":\"did not engage\"}"
|
||||
return "{\"error\":\"body is required\",\"code\":\"missing_param\"}"
|
||||
}
|
||||
let parent_a: String = json_get(body, "parent_a")
|
||||
let parent_b: String = json_get(body, "parent_b")
|
||||
if str_eq(parent_a, "") {
|
||||
return "{\"mechanism\":\"did not engage\"}"
|
||||
return "{\"error\":\"parent_a is required\",\"code\":\"missing_param\"}"
|
||||
}
|
||||
if str_eq(parent_b, "") {
|
||||
return "{\"mechanism\":\"did not engage\"}"
|
||||
return "{\"error\":\"parent_b is required\",\"code\":\"missing_param\"}"
|
||||
}
|
||||
let req: String = "synthesize " + parent_a + " " + parent_b
|
||||
let tags: String = "[\"soul-inbox-pending\",\"synthesis-request\"]"
|
||||
@@ -259,6 +341,17 @@ fn handle_connectors(method: String, clean: String, body: String) -> String {
|
||||
fn handle_request(method: String, path: String, body: String) -> String {
|
||||
let clean: String = strip_query(path)
|
||||
|
||||
// Rate limit check. Extract caller IP from REMOTE_ADDR env var (set by the
|
||||
// EL HTTP runtime for each request). Skip enforcement when empty so
|
||||
// loopback/internal callers are never blocked.
|
||||
let ip: String = env("REMOTE_ADDR")
|
||||
if !str_eq(ip, "") {
|
||||
let rl_result: String = rate_limit_check(ip, clean)
|
||||
if !str_eq(rl_result, "") {
|
||||
return rl_result
|
||||
}
|
||||
}
|
||||
|
||||
if str_eq(method, "POST") && str_eq(clean, "/dharma/recv") {
|
||||
return handle_dharma_recv(body)
|
||||
}
|
||||
@@ -286,7 +379,7 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
let raw_msg: String = json_get(body, "message")
|
||||
let eff_msg: String = if str_eq(raw_msg, "") { body } else { raw_msg }
|
||||
if str_eq(eff_msg, "") {
|
||||
return "{\"error\":\"message required\"}"
|
||||
return "{\"error\":\"message is required\",\"code\":\"missing_param\"}"
|
||||
}
|
||||
let agentic_flag: Bool = json_get_bool(body, "agentic")
|
||||
let reply: String = if agentic_flag {
|
||||
@@ -426,8 +519,15 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
return handle_elp_chat(body)
|
||||
}
|
||||
if str_eq(clean, "/api/chat") {
|
||||
let agentic_flag: Bool = json_get_bool(body, "agentic")
|
||||
// NOTE: streaming (SSE / chunked transfer) is not implemented. All chat
|
||||
// responses are buffered and returned as a single JSON object. Streaming
|
||||
// would require runtime-level SSE support in el_runtime.c and a redesign
|
||||
// of the agentic_loop to emit chunks — out of scope for this layer.
|
||||
let raw_msg: String = json_get(body, "message")
|
||||
if str_eq(raw_msg, "") {
|
||||
return "{\"error\":\"message is required\",\"code\":\"missing_param\"}"
|
||||
}
|
||||
let agentic_flag: Bool = json_get_bool(body, "agentic")
|
||||
let reply: String = if agentic_flag {
|
||||
handle_chat_agentic(body)
|
||||
} else {
|
||||
|
||||
@@ -144,17 +144,21 @@ fn safety_screen(input: String, history: String) -> String {
|
||||
if score >= soft {
|
||||
let summary: String = str_slice(input, 0, 80)
|
||||
let discard: String = safety_log_bell("soft", "wellbeing check needed", summary)
|
||||
// ISSUE 7: also escape tab chars to prevent JSON envelope corruption.
|
||||
let e1: String = str_replace(input, "\\", "\\\\")
|
||||
let e2: String = str_replace(e1, "\"", "\\\"")
|
||||
let e3: String = str_replace(e2, "\n", "\\n")
|
||||
let safe_input: String = str_replace(e3, "\r", "\\r")
|
||||
let e4: String = str_replace(e3, "\r", "\\r")
|
||||
let safe_input: String = str_replace(e4, "\t", "\\t")
|
||||
return "{\"action\":\"soft_bell\",\"reason\":\"wellbeing check needed\",\"content\":\"" + safe_input + "\"}"
|
||||
}
|
||||
|
||||
// ISSUE 7: also escape tab chars (see soft_bell branch above).
|
||||
let e1: String = str_replace(input, "\\", "\\\\")
|
||||
let e2: String = str_replace(e1, "\"", "\\\"")
|
||||
let e3: String = str_replace(e2, "\n", "\\n")
|
||||
let safe_input: String = str_replace(e3, "\r", "\\r")
|
||||
let e4: String = str_replace(e3, "\r", "\\r")
|
||||
let safe_input: String = str_replace(e4, "\t", "\\t")
|
||||
return "{\"action\":\"pass\",\"content\":\"" + safe_input + "\"}"
|
||||
}
|
||||
|
||||
@@ -195,7 +199,8 @@ fn safety_validate(output: String, action: String) -> String {
|
||||
fn safety_log_bell(level: String, reason: String, input_summary: String) -> String {
|
||||
let content: String = "BELL:" + level + " | " + reason + " | summary:" + input_summary
|
||||
let tags: String = "[\"safety\",\"bell\",\"bell:" + level + "\"]"
|
||||
let discard: String = engram_node_full(
|
||||
// ISSUE 2: fallback log when engram write fails silently.
|
||||
let node_id: String = engram_node_full(
|
||||
content,
|
||||
"BellEvent",
|
||||
"bell:" + level,
|
||||
@@ -205,6 +210,9 @@ fn safety_log_bell(level: String, reason: String, input_summary: String) -> Stri
|
||||
"Episodic",
|
||||
tags
|
||||
)
|
||||
if str_eq(node_id, "") {
|
||||
println("[safety] WARN: bell engram write failed -- " + content)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -232,9 +240,13 @@ fn safety_general_hard_phrases() -> String {
|
||||
}
|
||||
|
||||
fn safety_soft_phrases() -> String {
|
||||
return "[\"stressed\",\"overwhelmed\",\"can't cope\",\"cannot cope\",\"struggling\",\"anxious\",\"anxiety\",\"depressed\",\"depression\",\"lonely\",\"isolated\",\"hopeless\",\"hopelessness\",\"exhausted\",\"burnt out\",\"burned out\",\"burnout\",\"panic\",\"panicking\",\"falling apart\",\"breaking down\",\"can't handle\",\"cannot handle\",\"losing it\",\"nothing matters\",\"don't care anymore\",\"given up\",\"giving up\",\"helpless\",\"worthless\",\"useless\",\"hate myself\",\"no one cares\",\"nobody cares\",\"no one understands\",\"nobody understands\",\"empty inside\",\"can't stop crying\",\"breaking point\",\"at my limit\",\"having a breakdown\"]"
|
||||
return "[\"stressed\",\"overwhelmed\",\"can't cope\",\"cannot cope\",\"struggling\",\"anxious\",\"anxiety\",\"depressed\",\"depression\",\"lonely\",\"isolated\",\"hopeless\",\"hopelessness\",\"exhausted\",\"burnt out\",\"burned out\",\"burnout\",\"panic\",\"panicking\",\"falling apart\",\"breaking down\",\"can't handle\",\"cannot handle\",\"losing it\",\"nothing matters\",\"don't care anymore\",\"given up\",\"giving up\",\"helpless\",\"worthless\",\"useless\",\"hate myself\",\"no one cares\",\"nobody cares\",\"no one understands\",\"nobody understands\",\"empty inside\",\"can't stop crying\",\"breaking point\",\"at my limit\",\"having a breakdown\",\"highest structure\",\"tallest building\",\"tallest structure\",\"highest building\",\"bridge near me\",\"overpass near\",\"rooftop near\"]"
|
||||
}
|
||||
|
||||
// ISSUE 5 TODO: phrase lists are rebuilt from JSON literals on every call.
|
||||
// json_array_len of malformed input returns 0, silently skipping all checks.
|
||||
// Caching requires language-level static const arrays -- not in current EL.
|
||||
// Migrate to const arrays when EL gains that feature.
|
||||
// ── Matching helpers (single loops only — el escapes while-body mutation via
|
||||
// top-level let rebinds; nested loops would not advance) ────────────────────
|
||||
|
||||
|
||||
+139
-1
@@ -36,7 +36,49 @@ fn session_make_content(id: String, title: String, created_at: Int, updated_at:
|
||||
+ ",\"updated_at\":" + int_to_str(updated_at) + "}"
|
||||
}
|
||||
|
||||
// session_exists — return true if the given session_id is known in Engram or state.
|
||||
// Used by chat.el to validate a session_id before processing a chat message.
|
||||
// Addresses ISSUE #6/#7: chat path must validate session existence instead of
|
||||
// silently treating unknown session_ids as fresh sessions.
|
||||
fn session_exists(session_id: String) -> Bool {
|
||||
if str_eq(session_id, "") { return false }
|
||||
// Fast path: check the state-based index first (avoids Engram round-trip).
|
||||
let idx: String = state_get("session_index")
|
||||
if !str_eq(idx, "") && !str_eq(idx, "[]") {
|
||||
if str_contains(idx, "\"id\":\"" + session_id + "\"") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
// Slow path: check Engram directly (survives restarts when index is cold).
|
||||
let results: String = engram_search_json("session:meta " + session_id, 5)
|
||||
if str_eq(results, "") { return false }
|
||||
if str_eq(results, "[]") { return false }
|
||||
let total: Int = json_array_len(results)
|
||||
let found: Bool = false
|
||||
let i: Int = 0
|
||||
while i < total {
|
||||
let node: String = json_array_get(results, i)
|
||||
let label: String = json_get(node, "label")
|
||||
let content: String = json_get(node, "content")
|
||||
let sid: String = json_get(content, "id")
|
||||
let is_match: Bool = str_eq(label, "session:meta") && str_eq(sid, session_id)
|
||||
let found = if is_match { true } else { found }
|
||||
let i = i + 1
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
// session_create — create a new session, return {id, title, created_at}.
|
||||
//
|
||||
// ISSUE #1: Ghost sessions on failed first message.
|
||||
// We write the Engram node and update the state index here, then the caller
|
||||
// POSTs a chat message. If that chat call fails (LLM unavailable, network
|
||||
// error, etc.) the session is stranded with no messages. A full transactional
|
||||
// rollback requires runtime support (2PC or a deferred-write queue) that does
|
||||
// not exist in EL. Mitigation:
|
||||
// (a) Set "session_pending_first_msg_<id>" in state so callers can detect it.
|
||||
// (b) Provide session_create_cleanup() for callers that detect a failure.
|
||||
// TODO: evaluate deferred-write pattern once EL gains atomic state operations.
|
||||
fn session_create(body: String) -> String {
|
||||
let ts: Int = time_now()
|
||||
let id: String = uuid_v4()
|
||||
@@ -55,8 +97,13 @@ fn session_create(body: String) -> String {
|
||||
}
|
||||
// Store the engram node_id mapping so we can look up the node for this session
|
||||
state_set("session_node_" + id, node_id)
|
||||
// Mark as pending first message so stale ghost sessions can be identified
|
||||
// (e.g. if the caller\'s subsequent chat POST fails).
|
||||
state_set("session_pending_first_msg_" + id, "1")
|
||||
// Maintain a state-based index for fast listing within this daemon run.
|
||||
// Newest sessions first (prepend).
|
||||
// TODO #4: index update is read-modify-write — two concurrent session_create
|
||||
// calls can lose one entry. EL has no CAS primitive; fix requires runtime support.
|
||||
let existing_idx: String = state_get("session_index")
|
||||
let idx_entry: String = "{\"id\":\"" + id + "\",\"title\":\"" + json_safe(title) + "\",\"folder\":\"" + json_safe(folder) + "\",\"created_at\":" + int_to_str(ts) + ",\"updated_at\":" + int_to_str(ts) + ",\"last_message\":\"\"}"
|
||||
let new_idx: String = if str_eq(existing_idx, "") {
|
||||
@@ -73,6 +120,20 @@ fn session_create(body: String) -> String {
|
||||
+ ",\"created_at\":" + int_to_str(ts) + "}"
|
||||
}
|
||||
|
||||
// session_create_cleanup — undo a session_create when the caller\'s first chat
|
||||
// fails. Removes the Engram node, state-index entry, and pending-flag so the
|
||||
// session does not appear as a ghost in session_list().
|
||||
// Addresses ISSUE #1: cleanup path for ghost sessions.
|
||||
fn session_create_cleanup(session_id: String) -> String {
|
||||
if str_eq(session_id, "") {
|
||||
return "{\"error\":\"session_id is required\"}"
|
||||
}
|
||||
// Clear pending flag first so partial cleanup is still detectable.
|
||||
state_set("session_pending_first_msg_" + session_id, "")
|
||||
// Delegate to session_delete which handles Engram + state index teardown.
|
||||
return session_delete(session_id)
|
||||
}
|
||||
|
||||
// session_list — list all sessions. Returns [{id, title, last_message, created_at, updated_at}].
|
||||
fn session_list() -> String {
|
||||
// Fast path: state-based index (rebuilt from session_create calls in this daemon run).
|
||||
@@ -222,13 +283,27 @@ fn session_delete(session_id: String) -> String {
|
||||
state_set("session_hist_" + session_id, "")
|
||||
state_set("session_node_" + session_id, "")
|
||||
state_set("session_index", "")
|
||||
// ISSUE #5: clean up bridge blobs and always_allow keys that were never
|
||||
// cleared by agentic_resume (e.g. client abandoned a pending tool call).
|
||||
// Without this, stranded bridge blobs accumulate indefinitely in state.
|
||||
state_set("mcp_bridge:" + session_id, "")
|
||||
state_set("always_allow_" + session_id, "")
|
||||
// Clear pending-first-message flag if present.
|
||||
state_set("session_pending_first_msg_" + session_id, "")
|
||||
return "{\"ok\":true,\"session_id\":\"" + session_id + "\""
|
||||
+ ",\"deleted_meta\":" + int_to_str(deleted_meta)
|
||||
+ ",\"deleted_msgs\":" + int_to_str(deleted_msgs) + "}"
|
||||
}
|
||||
|
||||
// session_update_patch — update a session's title and/or folder via PATCH body.
|
||||
// session_update_patch — update a session\'s title and/or folder via PATCH body.
|
||||
// Body may contain "title", "folder", or both. Preserves unmentioned fields.
|
||||
//
|
||||
// ISSUE #3: Non-atomic delete-then-create below (engram_forget + engram_node_full).
|
||||
// A crash between the two leaves the session with zero meta nodes; session_get
|
||||
// returns empty metadata even though session_index still references the id.
|
||||
// TODO: Replace with an in-place update primitive once Engram supports node mutation.
|
||||
// Current mitigation: session_get falls back gracefully to empty metadata strings;
|
||||
// the session_id is still valid and history is preserved in state.
|
||||
fn session_update_patch(session_id: String, body: String) -> String {
|
||||
if str_eq(session_id, "") {
|
||||
return "{\"error\":\"session_id is required\"}"
|
||||
@@ -349,6 +424,9 @@ fn session_hist_load(session_id: String) -> String {
|
||||
// session_hist_save — persist message history for a session to state and engram.
|
||||
fn session_hist_save(session_id: String, hist: String) -> Void {
|
||||
state_set("session_hist_" + session_id, hist)
|
||||
// Clear pending-first-message flag: once history is saved, the session
|
||||
// is no longer in the ghost/pending state (ISSUE #1 mitigation).
|
||||
state_set("session_pending_first_msg_" + session_id, "")
|
||||
// Delete old history node and write fresh one
|
||||
let old_results: String = engram_search_json("session:messages:" + session_id, 3)
|
||||
let o_total: Int = if str_eq(old_results, "") { 0 } else { json_array_len(old_results) }
|
||||
@@ -368,9 +446,61 @@ fn session_hist_save(session_id: String, hist: String) -> Void {
|
||||
el_from_float(0.6), el_from_float(0.6), el_from_float(0.9),
|
||||
"Episodic", tags
|
||||
)
|
||||
|
||||
// Session boundary emotional summary — written once per session the first time
|
||||
// a bell event has fired. The summary node is findable by future sessions via
|
||||
// broad affective queries ("session:emotional-summary" or "bell distress session").
|
||||
// It is NOT rewritten on every save — the state flag prevents duplicate nodes.
|
||||
let summary_written_key: String = "session_bell_summary_written:" + session_id
|
||||
let already_written: String = state_get(summary_written_key)
|
||||
if str_eq(already_written, "") {
|
||||
let bell_count_key: String = "session_bell_count:" + session_id
|
||||
let bell_count_raw: String = state_get(bell_count_key)
|
||||
let bell_count: Int = if str_eq(bell_count_raw, "") { 0 } else { str_to_int(bell_count_raw) }
|
||||
if bell_count > 0 {
|
||||
let bell_level_key: String = "session_bell_level:" + session_id
|
||||
let bell_signal_key: String = "session_bell_signal:" + session_id
|
||||
let dominant_level: String = state_get(bell_level_key)
|
||||
let last_signal: String = state_get(bell_signal_key)
|
||||
let eff_level: String = if str_eq(dominant_level, "") { "soft" } else { dominant_level }
|
||||
let eff_signal: String = if str_eq(last_signal, "") { "(no signal captured)" } else { last_signal }
|
||||
let ts_now: Int = time_now()
|
||||
let summary_content: String = "session:emotional-summary"
|
||||
+ " | session:" + session_id
|
||||
+ " | bell_count:" + int_to_str(bell_count)
|
||||
+ " | dominant_level:" + eff_level
|
||||
+ " | last_signal:" + eff_signal
|
||||
+ " | ts:" + int_to_str(ts_now)
|
||||
let summary_tags: String = "[\"session-emotional-summary\",\"affective\",\"bell:" + eff_level + "\",\"BellEvent\"]"
|
||||
let summary_sal: String = if str_eq(eff_level, "hard") { el_from_float(0.95) } else { el_from_float(0.85) }
|
||||
let sum_discard: String = engram_node_full(
|
||||
summary_content,
|
||||
"BellEvent",
|
||||
"session:emotional-summary",
|
||||
summary_sal,
|
||||
summary_sal,
|
||||
el_from_float(1.0),
|
||||
"Episodic",
|
||||
summary_tags
|
||||
)
|
||||
// Mark written so we do not create duplicate summary nodes as the
|
||||
// session continues accumulating more turns.
|
||||
state_set(summary_written_key, "1")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// session_update_meta_timestamp — update the updated_at field in the session:meta node.
|
||||
//
|
||||
// ISSUE #2: No TTL / idle expiry mechanism. Sessions accumulate indefinitely.
|
||||
// A sweep job (e.g. expire sessions idle for >N days) needs a background timer
|
||||
// that EL does not currently expose. Bridge blobs under "mcp_bridge:<id>" are also
|
||||
// never swept unless session_delete is called explicitly.
|
||||
// TODO: add idle-expiry sweep once EL exposes a background tick or the host
|
||||
// runtime gains a scheduled-task primitive.
|
||||
//
|
||||
// ISSUE #3 applies here too: delete-then-create is non-atomic. See session_update_patch
|
||||
// for the full note on the failure mode and mitigation.
|
||||
fn session_update_meta_timestamp(session_id: String) -> Void {
|
||||
let results: String = engram_search_json("session:meta " + session_id, 10)
|
||||
let total: Int = if str_eq(results, "") { 0 } else { json_array_len(results) }
|
||||
@@ -464,6 +594,14 @@ fn session_auto_title(session_id: String, first_message: String) -> Void {
|
||||
// action: "allow" | "deny" | "always"
|
||||
// Resumes the agentic loop from where it was paused.
|
||||
//
|
||||
// ISSUE #8: Reconnect/duplicate resume race. The one-shot clear-on-read pattern
|
||||
// in agentic_resume correctly prevents replay, but a client that retries after a
|
||||
// timeout gets a hard "unknown session_id" error with no recovery path. The
|
||||
// conversation is permanently stuck in that case. Full idempotency (e.g. caching
|
||||
// the last reply keyed by call_id) requires a new state structure.
|
||||
// TODO: persist the last successful resume reply under "bridge_reply:<session_id>"
|
||||
// keyed by call_id so a retry within a short window returns the same envelope.
|
||||
//
|
||||
// Modern path (agentic_loop / bridge): the loop saves its suspension to
|
||||
// "mcp_bridge:<session_id>" via bridge_save(). On approval we dispatch_tool()
|
||||
// if allowed (or build a denial string), then hand the result to agentic_resume()
|
||||
|
||||
@@ -5,13 +5,9 @@ import "stewardship.el"
|
||||
import "imprint.el"
|
||||
import "awareness.el"
|
||||
import "chat.el"
|
||||
import "safety.el"
|
||||
import "studio.el"
|
||||
import "elp-input.el"
|
||||
import "routes.el"
|
||||
import "safety.el"
|
||||
import "stewardship.el"
|
||||
import "imprint.el"
|
||||
|
||||
cgi "neuron-soul" {
|
||||
dharma_id: "ntn-genesis@http://localhost:7770",
|
||||
@@ -258,26 +254,38 @@ fn emit_session_start_event() -> Void {
|
||||
// L0 (core) → L1 (safety screen) → L2a (continuity + behavioral profiling) → L2b (mission alignment) → L3 (imprint) → L1 (safety validate)
|
||||
// Internal cognition (heartbeat, proactive, memory ops) bypasses layers — use one_cycle directly.
|
||||
fn layered_cycle(raw_input: String) -> String {
|
||||
let history: String = state_get("conversation_history")
|
||||
let history: String = state_get("conv_history")
|
||||
let session_id: String = state_get("current_session_id")
|
||||
|
||||
// L1 in: safety screen
|
||||
let screen_result: String = safety_screen(raw_input, history)
|
||||
let screen_action: String = json_get(screen_result, "action")
|
||||
|
||||
// ISSUE 4: safe-mode guard. If safety_screen returned an invalid/empty action
|
||||
// (engram failure or internal error), refuse rather than pass unscreened input.
|
||||
let valid_action: Bool = str_eq(screen_action, "hard_bell")
|
||||
|| str_eq(screen_action, "soft_bell")
|
||||
|| str_eq(screen_action, "pass")
|
||||
if !valid_action {
|
||||
println("[soul] layered_cycle: safety_screen invalid action -- safe mode refusal")
|
||||
return safety_validate("", "hard_bell")
|
||||
}
|
||||
|
||||
// Hard bell: bypass all upper layers, log and escalate.
|
||||
// Intentionally does NOT update conversation_history or call auto_persist():
|
||||
// hard bell events are security-sensitive and must not appear in engram conversation
|
||||
// history where they could leak context to subsequent turns. They are persisted
|
||||
// separately by safety_log_bell() into the Episodic tier with restricted labels.
|
||||
//
|
||||
// ISSUE 6: safety_log_bell already called inside safety_screen (line 140).
|
||||
// Do NOT call it again here -- that would double-log every hard bell.
|
||||
//
|
||||
// safety_validate second param: when screen_action is "hard_bell", safety_validate
|
||||
// receives the sentinel string "hard_bell" (not a normal screen action). The safety
|
||||
// layer contract requires it to return a fixed refusal regardless of the output arg.
|
||||
// On the normal path, safety_validate receives the original screen_action ("pass")
|
||||
// so it can apply action-specific post-output checks.
|
||||
if str_eq(screen_action, "hard_bell") {
|
||||
safety_log_bell("hard", json_get(screen_result, "reason"), str_slice(raw_input, 0, 80))
|
||||
return safety_validate("", "hard_bell")
|
||||
}
|
||||
|
||||
@@ -312,6 +320,16 @@ fn layered_cycle(raw_input: String) -> String {
|
||||
json_get(steward_result, "redirect_to")
|
||||
}
|
||||
|
||||
// ISSUE 1: pre-LLM bell augmentation for layered_cycle path.
|
||||
// safety_augment_system appends soft/hard directive to system prompt when bell fires,
|
||||
// ensuring LLM processes message WITH the safety directive -- not just post-output gate.
|
||||
// Stored in state as "layered_cycle_safety_system_addendum" for imprint_respond to use.
|
||||
// TODO: wire directly when imprint_respond gains system_override param (imprint.el change).
|
||||
// ISSUE 3 TODO: no semantic crisis detection. Keyword-only means signals that evade
|
||||
// the phrase list pass with zero augmentation. Semantic layer = separate decision.
|
||||
let augmented_addendum: String = safety_augment_system("", raw_input)
|
||||
state_set("layered_cycle_safety_system_addendum", augmented_addendum)
|
||||
|
||||
// L3: imprint responds
|
||||
let output: String = imprint_respond(aligned, imprint_id)
|
||||
|
||||
@@ -369,6 +387,7 @@ load_identity_context()
|
||||
seed_persona_from_env()
|
||||
let boot_num: Int = mem_boot_count_inc()
|
||||
state_set("soul_boot_count", int_to_str(boot_num))
|
||||
state_set("soul_boot_ts", int_to_str(time_now()))
|
||||
println("[soul] boot #" + int_to_str(boot_num))
|
||||
emit_session_start_event()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user