Compare commits

..

4 Commits

Author SHA1 Message Date
will.anderson 7eca248f1d Fix all 7 remaining code review issues on activation-seed
Issue 1 (CRITICAL): Restore missing closing brace for `if is_bell` block in
auto_persist. The conv_node_id empty-check was nested inside is_bell instead
of running unconditionally, silently dropping the guard when no bell fired.

Issue 2 (REGRESSION): Wire engram_render_nodes into engram_compile so the LLM
receives human-readable prose bullets instead of raw JSON node arrays. Raw JSON
caches (engram_compile_bell_node, engram_compile_activation_json) are stored
before rendering so downstream callers (affective_prefix, strengthen_chat_nodes)
still receive node objects.

Issue 3 (BUG): Fix salience parsing in engram_render_node. The old
str_replace(".", "") approach produced 8 for "0.8" (not 80). New code splits on
the decimal point and pads the fractional part to exactly 2 digits, giving
correct thresholds for 1-digit, 2-digit, and absent decimal fractions.

Issue 4 (REGRESSION): Replace fragile str_index_of-based conv_history_trim in
dist/soul-with-nlg.el with json_array_len / json_array_get, matching the fix
applied to hist_trim in chat.el. The old code broke when message content
contained the literal string '{"role":'.

Issue 5 (LOGIC BUG): Fix `q_pos > 0` → `q_pos >= 0` in distill_transcript.
The old condition silently dropped a question mark at tail offset 0.

Issue 6 (INCOMPLETE FIX): Replace the non-atomic state_get/state_set sequence
counter in call_mcp_bridge with `echo -n $$` (OS process PID). Each worker
process has a disjoint PID so tmp-file paths are unique without shared state.

Issue 7 (INCONSISTENCY): Update soul-with-nlg.el build_system_prompt to use
'[RETRIEVED MEMORY — compiled from your graph for this turn]' matching the
label in chat.el.
2026-06-22 13:36:47 -05:00
will.anderson be02fcd960 feat(recall): thread-aware activation seed for nlg soul path [issue 7]
Neuron Soul CI / build (pull_request) Successful in 4m37s
2026-06-22 13:17:04 -05:00
will.anderson dfa2a33926 feat(recall): context-dedup improvements
- Cache bell node result in engram_compile state (engram_compile_bell_node)
  so handle_chat affective_prefix reads the cached value instead of firing
  a duplicate engram query for distress signals (Issue 2)

- Cache primary activation result in engram_compile state
  (engram_compile_activation_json) using nodes0 from engram_compile_multi

- Replace redundant engram_activate_json(message, 2) in strengthen_chat_nodes
  with state_get(engram_compile_activation_json) - eliminates a third
  activation query per turn (Issue 7)

- engram_compile already has object-boundary truncation and cross-set
  dedup via engram_nodes_merge/engram_dedup_nodes (Issues 1, 6, 9)
2026-06-22 13:12:08 -05:00
will.anderson a60b1967df feat(recall): recall-completeness improvements
- Multi-query decomposition: split on AND/also/plus for multi-topic messages
- Named entity extraction: dedicated per-entity searches for project names
- Recall intent detection: boosted search pool for explicit recall requests
- Expanded pools: activation depth 8 (was 5), search 30->12 ranked (was 20->8)
- Threshold 25->15: retain moderately-relevant older nodes
- Sentinel cleanup extended to c14 for larger node pools
- Safe JSON truncation: find last closing brace before budget cap (8000 chars)
- Semantic continuation: engram_is_continuation replaces brittle 50-char threshold
- Thread snip: 150->250 chars for better pronoun resolution context
- Session preload: add project-specific and session-summary searches
2026-06-22 12:54:36 -05:00
4 changed files with 723 additions and 468 deletions
+682 -347
View File
File diff suppressed because it is too large Load Diff
Generated Vendored
+32 -15
View File
@@ -22186,10 +22186,10 @@ fn build_system_prompt(ctx: String) -> String {
let engram_block: String = if str_eq(ctx, "") {
""
} else {
"\n\n[ENGRAM CONTEXT — compiled from your graph]\n" + ctx
"\n\n[RETRIEVED MEMORY — compiled from your graph for this turn]\n" + ctx
}
// Safety first. Engram fills in. Identity is the base. Voice rules always present.
// Safety first. Memory fills in. Identity is the base. Voice rules always present.
return identity + date_line + voice_rules + safety_block + engram_block
}
@@ -22211,19 +22211,28 @@ fn count_context_nodes(ctx: String) -> String {
// conv_history_trim drop the oldest turn (2 entries) from a JSON history array
// when it exceeds 20 entries. Returns the trimmed array string.
// Locates the 3rd {"role": object boundary and slices from there.
//
// Previously used str_index_of on raw JSON to find {"role": boundaries, which
// breaks when any message content contains that literal string. Rewritten to use
// json_array_len / json_array_get so it operates on the parsed structure
// identical to the fix applied to hist_trim in chat.el.
fn conv_history_trim(hist: String) -> String {
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)) + "]"
let total: Int = json_array_len(hist)
// Never trim below 2 entries.
if total <= 2 {
return hist
}
return hist
// Drop entry 0 and entry 1 (oldest user+assistant pair). Rebuild from entry 2.
let result: String = ""
let i: Int = 2
while i < total {
let entry: String = json_array_get(hist, i)
let sep: String = if str_eq(result, "") { "" } else { "," }
let result = result + sep + entry
let i = i + 1
}
if str_eq(result, "") { return hist }
return "[" + result + "]"
}
fn handle_chat(body: String) -> String {
@@ -22313,7 +22322,11 @@ 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: thread-aware activation seed for nlg path (Issues 2-3,8-10).
let nlg_stored_hist: String = state_get("conv_history")
let nlg_hist_len: Int = if str_eq(nlg_stored_hist, "") { 0 } else { json_array_len(nlg_stored_hist) }
let nlg_seed: String = build_activation_seed(message, nlg_stored_hist, nlg_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")
@@ -22505,7 +22518,11 @@ fn handle_chat_agentic(body: String) -> String {
req_model
}
let ctx: String = engram_compile(message)
// Issue 7 fix: thread-aware seed for agentic nlg path.
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. "
+3 -83
View File
@@ -35,94 +35,14 @@ fn mem_forget(node_id: String) -> Void {
engram_forget(node_id)
}
// mem_consolidate structural scan plus salience-evolution pass.
//
// Previously this only returned structural counts (scanned, total_nodes, total_edges)
// with no salience updates. No node salience ever changed based on recall frequency
// or time; foundational nodes decayed identically to ephemeral chat; frequently-recalled
// nodes were never promoted. This made consolidation a no-op.
//
// New behavior:
// (a) Strengthen frequently-activated nodes: nodes in the top working-memory list
// (engram_wm_top_json) are strengthened they have been recalled recently
// and deserve higher salience. Raises effective salience for nodes that prove
// relevant across multiple sessions.
// (b) Strengthen Canonical-tier nodes: identity and foundational nodes should not
// decay; each consolidation pass re-strengthens them so they resist the
// tier-aware decay curve without requiring active recall.
// (c) Structural counts are still returned for observability.
//
// Called by awareness_run() on the "consolidate" inbox action.
fn mem_consolidate() -> String {
let scanned: Int = engram_node_count()
let total_edges: Int = engram_edge_count()
let strengthened: Int = 0
// (a) Strengthen top working-memory nodes recalled recently across sessions.
// Cap at 10 to keep consolidation fast.
let wm_top: String = engram_wm_top_json(10)
let wm_len: Int = json_array_len(wm_top)
let wi: Int = 0
while wi < wm_len {
let wm_node: String = json_array_get(wm_top, wi)
let wm_id: String = json_get(wm_node, "id")
if !str_eq(wm_id, "") {
engram_strengthen(wm_id)
let strengthened = strengthened + 1
}
let wi = wi + 1
}
// (b) Strengthen Canonical-tier nodes from a full paginated scan so they resist
// temporal decay. Canonical nodes encode foundational identity they must not
// silently floor at 10. Page size 50, scanning until fewer than 50 nodes are
// returned (last page), so all Canonical nodes are reached even in large graphs.
// Without pagination, only the first 50 nodes in the graph were eligible; any
// Canonical node at index 50+ was silently excluded from the boost.
// Strengthening is skipped if the node's current salience is already at the
// runtime ceiling (represented as "1" by %g) to avoid monotonic unbounded growth.
// Canonical nodes with salience < 1.0 are strengthened each consolidation pass;
// once they reach the ceiling the runtime will no longer raise them further, so
// calling engram_strengthen at the ceiling is a no-op in the runtime anyway, but
// the explicit check makes the intent clear and avoids any runtime log noise.
let page_size: Int = 50
let scan_offset: Int = 0
let scan_done: Bool = false
while !scan_done {
let scan_result: String = engram_scan_nodes_json(page_size, scan_offset)
let scan_len: Int = json_array_len(scan_result)
if scan_len == 0 {
let scan_done = true
} else {
let si: Int = 0
while si < scan_len {
let s_node: String = json_array_get(scan_result, si)
let s_tier: String = json_get(s_node, "tier")
let s_id: String = json_get(s_node, "id")
let s_sal: String = json_get(s_node, "salience")
// Only strengthen if below the ceiling to prevent unbounded salience growth.
// engram serialises the ceiling as "1" (%g drops the decimal part when it
// is exactly zero). Any other value is below ceiling and should be boosted.
let at_ceiling: Bool = str_eq(s_sal, "1")
if str_eq(s_tier, "Canonical") && !str_eq(s_id, "") && !at_ceiling {
engram_strengthen(s_id)
let strengthened = strengthened + 1
}
let si = si + 1
}
let scan_offset = scan_offset + scan_len
// Fewer results than page_size means we've reached the last page.
if scan_len < page_size {
let scan_done = true
}
}
}
let dummy: String = engram_scan_nodes_json(100, 0)
let total_nodes: Int = engram_node_count()
let total_edges: Int = engram_edge_count()
return "{\"scanned\":" + int_to_str(scanned)
+ ",\"total_nodes\":" + int_to_str(total_nodes)
+ ",\"total_edges\":" + int_to_str(total_edges)
+ ",\"strengthened\":" + int_to_str(strengthened) + "}"
+ ",\"total_edges\":" + int_to_str(total_edges) + "}"
}
fn mem_save(path: String) -> Void {
+6 -23
View File
@@ -166,40 +166,23 @@ fn load_identity_context() -> Void {
// Cross-session affective context: query engram for recent distress/crisis signals
// at session start. Stored under soul_affective_context so the safety layer can
// detect when a user has been in distress across previous sessions.
// Recency guard: nodes older than 14 days (1,209,600 seconds) are skipped.
// Unified at 14 days with chat.el engram_compile and handle_chat affective checks
// so all three paths present consistent affective context. The previous 7-day
// (604800s) window was inconsistent with the 72h chat.el window, causing
// conflicting context: soul.el loaded a 5-day-old crisis node while chat.el
// did not include it on subsequent turns. Both now use 14 days.
// Results capped at 3 nodes, 200 chars each, to limit context inflation.
// Soft recency guard: nodes with a ts field older than 7 days are skipped.
// Results capped at 3 nodes, 200 chars each, to avoid over-injection into context.
// TODO(recency): engram_search_json sorts by relevance, not timestamp. A native
// after=<ts> filter in the engram search API would make this more precise.
let affective_raw: String = engram_search_json("distress crisis upset hopeless bell BellEvent", 3)
let affective_raw: String = engram_search_json("distress crisis upset hopeless", 3)
let affective_ok: Bool = !str_eq(affective_raw, "") && !str_eq(affective_raw, "[]")
if affective_ok {
let ts_now: Int = time_now()
let ts_cutoff: Int = ts_now - 1209600
let ts_cutoff: Int = ts_now - 604800
let aff_total: Int = json_array_len(affective_raw)
let aff_ctx: String = ""
let ai: Int = 0
while ai < aff_total {
let aff_node: String = json_array_get(affective_raw, ai)
let aff_content: String = json_get(aff_node, "content")
// Use created_at (the standard engram node timestamp field), consistent
// with handle_chat which reads created_at / updated_at. The previous
// field name "ts" is not a standard engram field: it was present in some
// BellEvent content payloads but absent from standard engram node JSON,
// causing json_get to return "" and the fallback to ts_now meaning ALL
// nodes with a missing "ts" field appeared recent, over-including stale
// content. With the 14-day window, this amplification was significant.
// Fix: read created_at first, fall back to updated_at, then default to 0
// (same as handle_chat). A ts of 0 always fails the cutoff check, so nodes
// missing both timestamp fields are conservatively excluded rather than
// blindly included.
let aff_ca: String = json_get(aff_node, "created_at")
let aff_ts_str: String = if str_eq(aff_ca, "") { json_get(aff_node, "updated_at") } else { aff_ca }
let aff_ts: Int = if str_eq(aff_ts_str, "") { 0 } else { str_to_int(aff_ts_str) }
let aff_ts_str: String = json_get(aff_node, "ts")
let aff_ts: Int = if str_eq(aff_ts_str, "") { ts_now } else { str_to_int(aff_ts_str) }
let is_recent: Bool = aff_ts >= ts_cutoff
let snip: String = if str_len(aff_content) > 200 { str_slice(aff_content, 0, 200) } else { aff_content }
let aff_ctx = if is_recent && !str_eq(snip, "") {