|
|
|
@@ -40,9 +40,43 @@ 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 { "" }
|
|
|
|
|
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
|
|
|
|
|
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 "" }
|
|
|
|
|
|
|
|
|
@@ -94,39 +128,81 @@ fn hist_append(hist: String, role: String, content: String) -> String {
|
|
|
|
|
return "[" + inner + "," + entry + "]"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hist_trim — drop the oldest two entries from a history JSON array.
|
|
|
|
|
//
|
|
|
|
|
// Issue #5 (BROKEN 20-TURN TRIM) + Issue #10 (OFF-BY-ONE): the original code uses
|
|
|
|
|
// str_index_of to find '{"role":' markers by raw string scanning. If any message content
|
|
|
|
|
// contains the literal string '{"role":' (e.g. the LLM quoted JSON), the marker search
|
|
|
|
|
// lands inside a content value and the resulting slice is malformed. Additionally, the
|
|
|
|
|
// function had no minimum-retained-count guard.
|
|
|
|
|
//
|
|
|
|
|
// Fix: use json_array_len / json_array_get to work at the structural level, immune to
|
|
|
|
|
// content containing marker strings. Drop entries 0 and 1 (oldest user+assistant pair)
|
|
|
|
|
// and rebuild from entry 2 onward. Minimum retained count: 2 entries (never over-trim).
|
|
|
|
|
fn hist_trim(hist: String) -> String {
|
|
|
|
|
let total: Int = json_array_len(hist)
|
|
|
|
|
// Safety: never trim below 2 entries. If already at or below the minimum, return unchanged.
|
|
|
|
|
if total <= 2 {
|
|
|
|
|
return hist
|
|
|
|
|
let inner: String = str_slice(hist, 1, str_len(hist) - 1)
|
|
|
|
|
let marker: String = "{\"role\":"
|
|
|
|
|
let i1: Int = str_index_of(inner, marker)
|
|
|
|
|
let tail1: String = str_slice(inner, i1 + 1, str_len(inner))
|
|
|
|
|
let i2: Int = str_index_of(tail1, marker)
|
|
|
|
|
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)) + "]"
|
|
|
|
|
}
|
|
|
|
|
// Drop entry 0 and entry 1 (oldest user+assistant pair). Rebuild from entry 2 onward.
|
|
|
|
|
let result: String = ""
|
|
|
|
|
let i: Int = 2
|
|
|
|
|
while i < total {
|
|
|
|
|
let entry: String = json_array_get(hist, i)
|
|
|
|
|
let result = if str_eq(result, "") {
|
|
|
|
|
entry
|
|
|
|
|
} else {
|
|
|
|
|
result + "," + entry
|
|
|
|
|
}
|
|
|
|
|
let i = i + 1
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
if str_eq(result, "") {
|
|
|
|
|
return hist
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
return "[" + result + "]"
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
@@ -145,72 +221,29 @@ fn clean_llm_response(s: String) -> String {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// conv_history_persist — save conversation history to engram for cross-restart continuity.
|
|
|
|
|
// Stores as a Conversation node with label "conv:history".
|
|
|
|
|
//
|
|
|
|
|
// Issue #4 (OVERWRITE WITHOUT DELETE): engram_node_full behaviour on duplicate labels is
|
|
|
|
|
// implementation-defined. If it appends rather than upserts, stale older nodes accumulate.
|
|
|
|
|
// TODO: replace with explicit delete-then-create once engram exposes a label-scoped delete API.
|
|
|
|
|
//
|
|
|
|
|
// Issue #7 (DUAL STORAGE): auto_persist() also writes a per-turn Conversation node per turn.
|
|
|
|
|
// Both run every turn for different purposes (rolling array vs. Q&A snapshot). Documented here.
|
|
|
|
|
// Stores as a Conversation node. Overwrites by using consistent label "conv:history".
|
|
|
|
|
fn conv_history_persist(hist: String) -> Void {
|
|
|
|
|
if str_eq(hist, "") { return "" }
|
|
|
|
|
if str_eq(hist, "[]") { return "" }
|
|
|
|
|
// Issue #6 (PARTIAL-WRITE GUARD): refuse to persist a blob that is not a complete JSON
|
|
|
|
|
// array. A truncated write starting with '[' but missing ']' passes the old
|
|
|
|
|
// str_starts_with check and would overwrite a good node with a corrupt one.
|
|
|
|
|
if !str_starts_with(hist, "[") { return "" }
|
|
|
|
|
if !str_contains(hist, "]") { return "" }
|
|
|
|
|
let ts: Int = time_now()
|
|
|
|
|
let tags: String = "[\"conv-history\",\"persistent\"]"
|
|
|
|
|
let node_id: String = engram_node_full(
|
|
|
|
|
let discard: String = engram_node_full(
|
|
|
|
|
hist, "Conversation", "conv:history",
|
|
|
|
|
el_from_float(0.7), el_from_float(0.8), el_from_float(0.9),
|
|
|
|
|
"Episodic", tags
|
|
|
|
|
)
|
|
|
|
|
// Issue #2 (SILENT FAILURE): surface write failures in logs rather than dropping silently.
|
|
|
|
|
if str_eq(node_id, "") {
|
|
|
|
|
println("[chat] conv_history_persist: engram_node_full returned empty — history node may be lost")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// conv_history_load — restore conversation history from engram on first access.
|
|
|
|
|
//
|
|
|
|
|
// Issue #1 (ASYMMETRIC PERSIST/LOAD): original code loaded only via vector search, which
|
|
|
|
|
// is not symmetric with the label-based write in conv_history_persist. A cold or corrupt
|
|
|
|
|
// vector index returns [] even when the node exists on disk. Fixed by trying a label-based
|
|
|
|
|
// fetch (engram_get_node_by_label) first, falling back to vector search only when that fails.
|
|
|
|
|
//
|
|
|
|
|
// Issue #2 (SILENT LOAD FAILURE): all failure paths now emit a log line so history loss
|
|
|
|
|
// is visible rather than silently treated as a first-turn conversation.
|
|
|
|
|
//
|
|
|
|
|
// Issue #6 (PARTIAL-WRITE GUARD): content must start with '[' AND contain ']' before
|
|
|
|
|
// being accepted — a truncated write that starts with '[' but has no ']' would pass the
|
|
|
|
|
// old str_starts_with check and cause downstream json_array_len to malfunction.
|
|
|
|
|
// Returns the most recent "conv:history" node content, or "" if none found.
|
|
|
|
|
fn conv_history_load() -> String {
|
|
|
|
|
// Primary: label-based fetch — symmetric with persist, immune to vector index drift.
|
|
|
|
|
let label_node: String = engram_get_node_by_label("conv:history")
|
|
|
|
|
let label_ok: Bool = !str_eq(label_node, "") && !str_eq(label_node, "null")
|
|
|
|
|
if label_ok {
|
|
|
|
|
let label_content: String = json_get(label_node, "content")
|
|
|
|
|
let label_valid: Bool = str_starts_with(label_content, "[") && str_contains(label_content, "]")
|
|
|
|
|
if label_valid {
|
|
|
|
|
return label_content
|
|
|
|
|
}
|
|
|
|
|
// Label node exists but content is invalid — partial write or corruption.
|
|
|
|
|
println("[chat] conv_history_load: label node found but content invalid — falling back to vector search")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback: vector search — covers nodes indexed before this fix, or on cold index.
|
|
|
|
|
let results: String = engram_search_json("conv:history", 3)
|
|
|
|
|
if str_eq(results, "") { return "" }
|
|
|
|
|
if str_eq(results, "[]") { return "" }
|
|
|
|
|
let node: String = json_array_get(results, 0)
|
|
|
|
|
let content: String = json_get(node, "content")
|
|
|
|
|
// Issue #6: full partial-write guard — require both '[' prefix AND ']' presence.
|
|
|
|
|
if !str_starts_with(content, "[") || !str_contains(content, "]") {
|
|
|
|
|
println("[chat] conv_history_load: vector search result content invalid — treating as first turn")
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
// Validate it looks like a JSON array
|
|
|
|
|
if !str_starts_with(content, "[") { return "" }
|
|
|
|
|
return content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -221,13 +254,6 @@ fn handle_chat(body: String) -> String {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load history BEFORE compiling context so we can anchor activation to the thread.
|
|
|
|
|
// Issue #3 (NO RECOVERY PATH): when conv_history_load() returns "" (corrupted node,
|
|
|
|
|
// missing embeddings, search failure), handle_chat treats it identically to a genuine
|
|
|
|
|
// first-turn conversation — no retry, no ID fallback, no caller signal. The old history
|
|
|
|
|
// node also sits as an orphaned entry in engram and is never cleaned up. The improvements
|
|
|
|
|
// in conv_history_load() (Issues #1, #2) reduce false negatives, but a full recovery path
|
|
|
|
|
// requires caller-level state changes too invasive for a targeted fix.
|
|
|
|
|
// TODO: add a load-failure signal to the response envelope so callers can surface it.
|
|
|
|
|
let state_hist: String = state_get("conv_history")
|
|
|
|
|
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) }
|
|
|
|
@@ -257,13 +283,6 @@ 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 }
|
|
|
|
|
|
|
|
|
|
// Safety augmentation on the main chat path. Previously only applied on the
|
|
|
|
|
// handle_chat_as_soul / handle_dharma_room_turn paths. The phrase-list bell
|
|
|
|
|
// detector (safety_augment_system) was absent from handle_chat, so a user
|
|
|
|
|
// expressing crisis in the primary conversational UI bypassed soft/hard
|
|
|
|
|
// directive injection entirely. Applying it here before every llm_call_system.
|
|
|
|
|
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\"")
|
|
|
|
@@ -278,13 +297,10 @@ 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)
|
|
|
|
|
// Issue #8 (NO MAX SIZE GUARD): the 20-turn count limit bounds entry count, but individual
|
|
|
|
|
// messages can be arbitrarily large (up to max_tokens = 4096 tokens each). At 20 turns the
|
|
|
|
|
// history blob can reach ~80KB before trim fires. engram_node_full has no apparent size cap.
|
|
|
|
|
// A byte-length cap would require truncating or summarising entries — too invasive here.
|
|
|
|
|
// TODO: add a byte-length cap (e.g. 32KB) that drops oldest entries until under limit.
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
@@ -592,17 +608,12 @@ 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 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)
|
|
|
|
|
let content: String = fs_read(path)
|
|
|
|
|
if str_eq(content, "") {
|
|
|
|
|
return json_safe("{\"error\":\"file not found\"}")
|
|
|
|
|
}
|
|
|
|
|
let updated: String = str_replace(content, old_text, new_text)
|
|
|
|
|
fs_write(resolved, updated)
|
|
|
|
|
fs_write(path, updated)
|
|
|
|
|
return json_safe("{\"ok\":true}")
|
|
|
|
|
}
|
|
|
|
|
if str_eq(tool_name, "remember") {
|
|
|
|
@@ -763,23 +774,12 @@ fn handle_chat_agentic(body: String) -> String {
|
|
|
|
|
|
|
|
|
|
// Persist the exchange to session/global history for thread continuity on next turn.
|
|
|
|
|
// Only save when the loop completed (reply present), not when tool_pending.
|
|
|
|
|
//
|
|
|
|
|
// Issue #9 (AGENTIC HISTORY NOT PERSISTED): the agentic path previously only saved
|
|
|
|
|
// history to in-process state (state_set), which is lost on restart. We now also call
|
|
|
|
|
// conv_history_persist() for the default session (hist_key == "conv_history") so agentic
|
|
|
|
|
// history survives restarts the same way non-agentic history does. Per-session histories
|
|
|
|
|
// (session_hist_<id>) are still in-process only — persisting all named sessions would
|
|
|
|
|
// require per-session engram labels, a larger change tracked separately.
|
|
|
|
|
let reply_text: String = json_get(result, "reply")
|
|
|
|
|
let discard_hist: Bool = if !str_eq(reply_text, "") {
|
|
|
|
|
let updated: String = hist_append(agentic_hist, "user", message)
|
|
|
|
|
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)
|
|
|
|
|
// Only persist the default global session to engram — named sessions are ephemeral.
|
|
|
|
|
if str_eq(hist_key, "conv_history") {
|
|
|
|
|
conv_history_persist(trimmed)
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
} else { false }
|
|
|
|
|
|
|
|
|
@@ -1153,19 +1153,13 @@ fn handle_dharma_room_turn(body: String) -> String {
|
|
|
|
|
// engram_node(content, "episodic", ...) which wrongly put a TIER into the node_type
|
|
|
|
|
// slot — that's why nodes showed node_type="episodic". Use the full, correct contract.)
|
|
|
|
|
let utterance_tags: String = "[\"soul-utterance\",\"episodic\"]"
|
|
|
|
|
let utterance_id: String = engram_node_full(
|
|
|
|
|
let discard_id: String = engram_node_full(
|
|
|
|
|
clean_response, "Conversation", "soul:utterance",
|
|
|
|
|
el_from_float(0.6), el_from_float(0.6), el_from_float(0.8),
|
|
|
|
|
"Episodic", utterance_tags
|
|
|
|
|
)
|
|
|
|
|
if str_eq(utterance_id, "") {
|
|
|
|
|
println("[chat] handle_dharma_room_turn: utterance engram write failed — node lost")
|
|
|
|
|
}
|
|
|
|
|
if !str_eq(snap_path, "") {
|
|
|
|
|
let save_result: String = engram_save(snap_path)
|
|
|
|
|
if str_eq(save_result, "") {
|
|
|
|
|
println("[chat] handle_dharma_room_turn: engram_save failed for " + snap_path)
|
|
|
|
|
}
|
|
|
|
|
let discard_save: String = engram_save(snap_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let safe_response: String = json_safe(clean_response)
|
|
|
|
@@ -1240,14 +1234,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,
|
|
|
|
@@ -1257,6 +1265,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.
|
|
|
|
|