Compare commits

...

2 Commits

Author SHA1 Message Date
will.anderson 27663dc968 fix(recall): resolve session-start-recall code review issues
- Fix Issue 6 (affective duplication): engram_compile no longer appends
  the bell node JSON to its return value; it only caches it via state.
  engram_compile_multi now appends the cached bell node exactly once after
  all compile calls complete, preventing N copies when multiple seeds are
  used. Dharma room handlers updated to read and append the cached bell node
  explicitly after their single engram_compile call.

- Fix engram_compile_ranked: replace _sel_N JSON sentinel injection with a
  clean |N| pipe-delimited index string. The old approach mutated node JSON
  objects with bookkeeping fields that leaked into the LLM context; the new
  approach tracks selected indices externally and leaves node data untouched.
  Score threshold lowered from 25 to 15 to include moderately-relevant nodes.

- Add engram_render_node / engram_render_nodes / engram_render_ctx: convert
  raw engram JSON arrays/objects into human-readable "- [TYPE age sal] content"
  bullet lines before injecting into the system prompt. build_system_prompt
  now calls engram_render_ctx so the LLM receives prose rather than opaque
  JSON field blobs.

- Fix missing closing brace in handle_chat_agentic hard_bell early-return
  block that left subsequent code dangling outside the conditional.
2026-06-22 13:48:00 -05:00
will.anderson 08b785cfac fix(recall): address all five code-review issues in context-dedup
Issue 1 — cache read-before-write: move engram_compile_multi call to
before the affective_prefix block in handle_chat. engram_compile writes
"engram_compile_bell_node" to state; the previous ordering meant the
first-turn affective prefix always read an empty cache even when a recent
bell node existed.

Issue 2 — double-write clobber: engram_compile_multi now saves the
primary-seed activation ("engram_compile_primary_activation_json") after
the first engram_compile call, before the secondary call can overwrite
the shared "engram_compile_activation_json" key. strengthen_chat_nodes
now prefers the primary key, falling back only when absent.

Issue 3 — mid-object truncation in engram_compile_multi: replace the
dumb str_slice(merged, 0, 6000) with the same safe JSON boundary-scan
(last closing brace before cap) already used in engram_compile, so
ctx1+ctx2+ctx3 over 6000 chars never produces a torn JSON object.

Issue 4 — heuristic regression in is_genuine_continuation: add explicit
question-word prefix detection (what/how/why/when/where/who/which/is/
can/could/does/do/explain/describe/define) that fires before the 50-char
length gate. A message starting with a question word is always a new
topic, regardless of length, so "what is rust?" (14 chars, all-lowercase,
no mid-capitals) correctly returns false instead of true.

Issue 5 — unreliable dedup via str_contains: remove the substring
duplicate checks in engram_compile_multi. str_contains across multi-KB
JSON strings is not a reliable deduplication mechanism — coincidental
field-value matches suppress valid context, and truncated ctx1 misses
genuine duplicates. We now concatenate ctx1+ctx2+ctx3 unconditionally
and accept minor node redundancy in exchange for correctness.
2026-06-22 13:42:33 -05:00
+267 -63
View File
@@ -48,72 +48,170 @@ 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
@@ -139,14 +237,40 @@ fn is_followup_phrase(msg: String) -> Bool {
}
// is_genuine_continuation returns true when a short message is a contextual
// follow-up rather than a new topic. Fixes Issue 2: the old threshold (str_len < 50)
// conflated new-topic short messages like "explain quantum tunneling" (49 chars)
// with genuine follow-ups like "ok", "yes", or "what do you think?".
// 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")
@@ -319,9 +443,34 @@ fn build_activation_seed(message: String, hist: String, hist_len: Int) -> String
// 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)
@@ -335,19 +484,38 @@ fn engram_compile_multi(primary_seed: String, message: String) -> String {
if emo_ok { engram_compile_ranked(emo_results, 3) } else { "" }
} else { "" }
let merged: String = ctx1
let sep2: String = if !str_eq(merged, "") && !str_eq(ctx2, "") { "\n" } else { "" }
let merged = if !str_eq(ctx2, "") && !str_contains(ctx1, ctx2) {
merged + sep2 + ctx2
} else { merged }
// 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, "") && !str_contains(merged, ctx3) {
merged + sep3 + ctx3
} else { merged }
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 "" }
if str_len(merged) > 6000 { return str_slice(merged, 0, 6000) }
return merged
// 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 {
@@ -415,12 +583,14 @@ fn engram_compile(intent: String) -> String {
let bn_ts: Int = if str_eq(bn_ts_raw, "") { 0 } else { str_to_int(bn_ts_raw) }
if bn_ts > cutoff_ts { bn0 } else { "" }
} else { "" }
let affective_part: String = if !str_eq(recent_bell, "") { recent_bell } 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 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
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)
@@ -428,7 +598,8 @@ fn engram_compile(intent: String) -> String {
if str_eq(ctx, "") { return "" }
// Fix Issue 6: cap at a clean JSON object boundary.
// 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
@@ -474,10 +645,14 @@ 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
}
let safety_addendum: String = state_get("layered_cycle_safety_system_addendum")
@@ -636,7 +811,15 @@ fn handle_chat(body: String) -> String {
// and tail-biased snipping from long assistant replies.
let activation_seed: String = build_activation_seed(message, stored_hist, hist_len)
// 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, "") {
@@ -644,8 +827,6 @@ fn handle_chat(body: String) -> String {
} else { "" }
} else { "" }
// Issue 4 fix: engram_compile_multi adds entity + emotion fan-out seeds
let ctx: String = engram_compile_multi(activation_seed, message)
let system: String = affective_prefix + build_system_prompt(ctx)
// First message of the session: proactively load user profile and active work context.
@@ -755,7 +936,16 @@ fn handle_chat(body: String) -> String {
conv_history_persist(final_hist)
// Fix Issue 7: reuse activation JSON from engram_compile no third activate query.
let cached_act: String = state_get("engram_compile_activation_json")
// 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 { "[]" }
@@ -1204,7 +1394,7 @@ fn handle_chat_agentic(body: String) -> String {
if str_eq(screen_action, "hard_bell") {
safety_log_bell("hard", json_get(screen_result, "reason"), str_slice(message, 0, 80))
return "{\"reply\":\"" + json_safe(safety_validate("", "hard_bell")) + "\",\"model\":\"\",\"agentic\":true,\"tools_used\":[]}"
}
let req_model: String = json_get(body, "model")
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
@@ -1629,7 +1819,14 @@ fn handle_dharma_room_turn(body: String) -> String {
// The soul's own memories, activated by what it's reading not injected.
// Issue 6 fix: distill_transcript() reduces diffuse embedding noise
let engram_ctx: String = engram_compile(distill_transcript(transcript))
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 {
@@ -1682,7 +1879,14 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
}
// Issue 6 fix: distill_transcript() reduces diffuse embedding noise
let ctx: String = engram_compile(distill_transcript(transcript))
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()