From 5f6ce5ca1fd209dd315ae51d471172ac87d60d21 Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Wed, 12 Aug 2026 23:36:24 -0500 Subject: [PATCH] M-INTEROCEPTION P1: two-threshold consolidation layer (ENGRAM_CONSOLIDATION, default OFF) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The co-activation accrual already works (hebb is an EWMA over co-firing); what was missing is the promotion layer from design §9 that turns accrual into durable structure. This adds it on top, entirely behind ENGRAM_CONSOLIDATION so the OFF path is byte-identical to trunk. CONNECTION threshold ("connection IS consolidation"): when a strongly-firing InternalStateEvent is created (salience >= ENGRAM_CONSOL_CONN_MIN, default 0.6), wire hebbian-associate edges from it to the top-K working-memory nodes active at that instant (ENGRAM_CONSOL_WM_TOPK, default 5), provenance-tagged "consolidated-from-ISE" and dedup-guarded. A sub-threshold ISE forms nothing (a shower thought) and drifts out at the existing 48h prune. PERMANENCE threshold (rare): engram_consolidate_permanence(node) marks a node whose rehearsed ACT-R base-level clears ENGRAM_CONSOL_PERM_MIN durable via a reversible metadata marker; engram_prune_telemetry then exempts it (gated, so no trunk node is ever affected). Idempotent, no double-promote. Thresholds are env-tunable for A/B without a rebuild. MEASURED on a copy (throwaway HOME): - Headline accrual curve (flag OFF, pure trunk) over N co-activations of a wired pair — hebb_max: N=1 -> 1e-4 (=ETA), N=100 -> 0.010, N=1625 -> 0.150 (LINK_MIN, where an unwired pair consolidates), N=3000 -> 0.259; tracks the analytic EWMA 1-0.9999^N within measurement noise (co-activation P~1). - Strong ISE wired 2 edges to exactly the wm_top nodes (hebb-a, hebb-b); weak ISE formed 0; edges carry the reversible provenance marker. - Promoted node survived the 48h prune (2->1 nodes); ephemeral ISE swept. - Flag OFF: ISE creation added 0 edges, permanence returned 0 (no-op). ASan+UBSan clean. --- engram/test/run_interoception_p1.sh | 147 +++++++++++++++++++++ engram/test/test_interoception_p1_consol.c | 124 +++++++++++++++++ lang/runtime/el_runtime.c | 113 ++++++++++++++++ lang/runtime/el_runtime.h | 1 + lang/runtime/el_seed.c | 4 + 5 files changed, 389 insertions(+) create mode 100755 engram/test/run_interoception_p1.sh create mode 100644 engram/test/test_interoception_p1_consol.c diff --git a/engram/test/run_interoception_p1.sh b/engram/test/run_interoception_p1.sh new file mode 100755 index 0000000..41e5dbe --- /dev/null +++ b/engram/test/run_interoception_p1.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# M-INTEROCEPTION P1 gate: two-threshold consolidation (ENGRAM_CONSOLIDATION). +# Throwaway HOME + /tmp only. Never touches ~/.neuron or :8742. +set -u +HERE="$(cd "$(dirname "$0")" && pwd)" +RT="$HERE/../../lang/runtime/el_runtime.c" +ST="$HERE/../../lang/runtime/engram_store.c" +GEO="$HERE/../../lang/runtime/engram_geometry.c" +VIDX="$HERE/../../lang/runtime/engram_vindex.c" +INC="$HERE/../../lang/runtime" +WORK="$(mktemp -d /tmp/engram-p1-XXXXXX)" +export HOME="$WORK/home"; mkdir -p "$HOME" +unset ENGRAM_STORE ENGRAM_CONSOLIDATION ENGRAM_CONSOL_CONN_MIN ENGRAM_CONSOL_PERM_MIN ENGRAM_CONSOL_WM_TOPK +fail=0 + +echo "== compile ==" +gcc -O1 -std=c11 -I "$INC" "$HERE/test_interoception_p1_consol.c" "$RT" "$ST" "$GEO" "$VIDX" \ + -lcurl -lm -o "$WORK/p1" 2>"$WORK/cc.log" || { echo "COMPILE FAILED"; cat "$WORK/cc.log"; rm -rf "$WORK"; exit 1; } + +echo +echo "== (a) HEADLINE: hebb accrual curve over N co-activations (flag OFF, pure trunk) ==" +D="$WORK/a"; mkdir -p "$D" +( unset ENGRAM_CONSOLIDATION; "$WORK/p1" accrual "$D" ) >"$WORK/accrual.txt" 2>&1 || { echo "FAIL accrual run"; fail=1; } +python3 - "$WORK/accrual.txt" <<'PY' +import json,sys,re +rows=[] +for line in open(sys.argv[1]): + m=re.match(r'SAMPLE (\d+) (\{.*\})',line.strip()) + if not m: continue + n=int(m.group(1)); j=json.loads(m.group(2)) + hm=j.get("hebb_max",0.0); hc=j.get("hebb_cand_max",0.0) + rows.append((n,hm,hc)) +print(" N hebb_max 1-0.9999^N (predicted EWMA)") +rc=0 +for n,hm,hc in rows: + pred=1-0.9999**n + print(f" {n:<7} {hm:<12.6g} {pred:.6g}") +# assertions: monotonic rise, starts near ETA, tracks EWMA prediction +first=rows[0]; last=rows[-1] +def check(c,m): + global rc; print((" PASS: " if c else " FAIL: ")+m); + if not c: rc=1 +check(abs(first[1]-0.0001)<5e-5, f"first sample hebb ~= ETA 0.0001 (got {first[1]:.6g})") +check(all(rows[i][1] <= rows[i+1][1]+1e-9 for i in range(len(rows)-1)), "hebb_max is monotonically non-decreasing over N") +check(last[1] > first[1]*50, f"hebb accrues substantially by N={last[0]} (got {last[1]:.4g} vs {first[1]:.4g})") +# EWMA fit: measured should be within 25% of 1-0.9999^N at the mid samples +mid=[r for r in rows if 100<=r[0]<=2000] +ok=all(abs(hm-(1-0.9999**n))/(1-0.9999**n) < 0.25 for n,hm,hc in mid) +check(ok, "measured curve tracks the 1-0.9999^N EWMA prediction within 25% (co-activation P~1)") +sys.exit(rc) +PY +[ $? -ne 0 ] && fail=1 + +echo +echo "== (b) CONNECTION threshold: strong ISE wires to wm_top, weak ISE wires nothing (flag ON) ==" +D="$WORK/b"; mkdir -p "$D" +( export ENGRAM_CONSOLIDATION=1; "$WORK/p1" connect "$D" ) >"$WORK/connect.txt" 2>&1 || { echo "FAIL connect run"; fail=1; } +cat "$WORK/connect.txt" | sed 's/^/ /' +python3 - "$WORK/connect.txt" "$D/connect.json" <<'PY' +import json,sys,re +txt=open(sys.argv[1]).read() +g=json.load(open(sys.argv[2])) +def field(k): + m=re.search(rf'{k} (\S+)',txt); return m.group(1) if m else None +sid=field("ISE_STRONG_ID"); wid=field("ISE_WEAK_ID") +m=re.search(r'EDGES before=(\d+) after_strong=(\d+) after_weak=(\d+)',txt) +before,aftS,aftW=int(m.group(1)),int(m.group(2)),int(m.group(3)) +rc=0 +def check(c,mm): + global rc; print((" PASS: " if c else " FAIL: ")+mm) + if not c: rc=1 +strong_edges=[e for e in g["edges"] if e["from_id"]==sid and e["relation"]=="hebbian-associate"] +weak_edges=[e for e in g["edges"] if e["from_id"]==wid] +check(aftS>before, f"strong ISE formed connection edges ({before} -> {aftS})") +check(aftW==aftS, f"weak ISE formed NO edges ({aftS} -> {aftW})") +check(len(strong_edges)>=1, f"strong ISE has {len(strong_edges)} hebbian-associate edge(s) to wm_top") +check(all('consolidated-from-ISE' in (e.get('metadata') or '') for e in strong_edges), + "connection edges are provenance-tagged consolidated-from-ISE (reversible)") +check(len(weak_edges)==0, "weak ISE (below connection bar) has zero outgoing edges") +# targets must be the WM-top nodes (hebb-a / hebb-b), not distractors +tgt_labels=set() +byid={n["id"]:n for n in g["nodes"]} +for e in strong_edges: + t=byid.get(e["to_id"]); + if t: tgt_labels.add(t.get("label")) +print(f" connection targets: {sorted(tgt_labels)}") +check(tgt_labels.issubset({"hebb-a","hebb-b"}) and len(tgt_labels)>=1, + f"connections point at the wm_top nodes {sorted(tgt_labels)}") +sys.exit(rc) +PY +[ $? -ne 0 ] && fail=1 + +echo +echo "== (c) PERMANENCE threshold: promoted node survives 48h prune, ephemeral is swept (flag ON) ==" +D="$WORK/c"; mkdir -p "$D" +( export ENGRAM_CONSOLIDATION=1 ENGRAM_CONSOL_PERM_MIN=-1000; "$WORK/p1" perm "$D" ) >"$WORK/perm.txt" 2>&1 || { echo "FAIL perm run"; fail=1; } +cat "$WORK/perm.txt" | sed 's/^/ /' +python3 - "$WORK/perm.txt" <<'PY' +import sys,re,json +txt=open(sys.argv[1]).read() +rc=0 +def check(c,m): + global rc; print((" PASS: " if c else " FAIL: ")+m) + if not c: rc=1 +prom=int(re.search(r'PROMOTED (\d+)',txt).group(1)) +m=re.search(r'NODES before=(\d+) after=(\d+) removed=(\d+)',txt) +before,after,removed=int(m.group(1)),int(m.group(2)),int(m.group(3)) +dur=re.search(r'DURABLE_NODE (\{.*\})',txt).group(1) +eph=re.search(r'EPHEMERAL_NODE (\{.*\})',txt).group(1) +durj=json.loads(dur); ephj=json.loads(eph) +check(prom==1, "engram_consolidate_permanence promoted the node (returned 1)") +check(before==2 and after==1 and removed==1, f"exactly one node pruned ({before}->{after}, removed={removed})") +check(durj.get("id")=="ise-durable", "durable node SURVIVED the 48h telemetry prune") +check('consolidated-from-ISE' in (durj.get("metadata") or ''), "durable node carries reversible provenance marker") +check(ephj=={} or not ephj.get("id"), "ephemeral (non-permanent) ISE was swept") +sys.exit(rc) +PY +[ $? -ne 0 ] && fail=1 + +echo +echo "== (d) OFF path byte-identical: ISE creation forms no edges, permanence is a no-op ==" +D="$WORK/d"; mkdir -p "$D" +( unset ENGRAM_CONSOLIDATION; "$WORK/p1" offcheck "$D" ) >"$WORK/off.txt" 2>&1 +rcoff=$? +cat "$WORK/off.txt" | sed 's/^/ /' +[ $rcoff -eq 0 ] && echo " PASS: flag OFF — ISE creation added 0 edges and permanence returned 0" \ + || { echo " FAIL: OFF path changed behavior"; fail=1; } + +echo +echo "== ASan+UBSan (connect + perm + accrual-short) ==" +gcc -O1 -g -std=c11 -fsanitize=address,undefined -fno-sanitize-recover=undefined \ + -I "$INC" "$HERE/test_interoception_p1_consol.c" "$RT" "$ST" "$GEO" "$VIDX" \ + -lcurl -lm -o "$WORK/p1.san" 2>"$WORK/san_cc.log" || { echo "SAN COMPILE FAILED"; tail -25 "$WORK/san_cc.log"; fail=1; } +if [ -x "$WORK/p1.san" ]; then + export ASAN_OPTIONS=detect_leaks=0 + DS="$WORK/san"; mkdir -p "$DS" + ( export ENGRAM_CONSOLIDATION=1 ENGRAM_CONSOL_PERM_MIN=-1000; "$WORK/p1.san" connect "$DS" ) >/dev/null 2>"$WORK/san_run.log" + ( export ENGRAM_CONSOLIDATION=1 ENGRAM_CONSOL_PERM_MIN=-1000; "$WORK/p1.san" perm "$DS" ) >/dev/null 2>>"$WORK/san_run.log" + if grep -qiE 'runtime error|AddressSanitizer|Sanitizer|ERROR: ' "$WORK/san_run.log"; then + echo " FAIL: sanitizer findings:"; grep -iE 'runtime error|Sanitizer|ERROR' "$WORK/san_run.log" | head; fail=1 + else echo " ok: ASan+UBSan clean"; fi +fi + +echo +if [ "$fail" -eq 0 ]; then echo "====== P1 CONSOLIDATION GATE: PASS ======"; else echo "====== P1 CONSOLIDATION GATE: FAIL ======"; fi +rm -rf "$WORK" +exit $fail diff --git a/engram/test/test_interoception_p1_consol.c b/engram/test/test_interoception_p1_consol.c new file mode 100644 index 0000000..a5bc312 --- /dev/null +++ b/engram/test/test_interoception_p1_consol.c @@ -0,0 +1,124 @@ +/* test_interoception_p1_consol.c — M-INTEROCEPTION Priority 1. + * Two-threshold consolidation (ENGRAM_CONSOLIDATION, default OFF). + * + * Modes: + * accrual — flag OFF (pure trunk). Drive N co-activations of a WIRED pair and + * print act-stats at sampled N so the run script can plot the + * hebb accrual curve (headline measurement). No consolidation code + * runs; this measures the EXISTING EWMA accrual. + * connect — flag ON. Seed, activate to populate WM, then create a STRONG ISE + * (connects to wm_top) and a WEAK ISE (below the bar → nothing). + * Exports the graph so edges from each ISE can be counted. + * perm — flag ON. Load two OLD InternalStateEvent nodes; promote one to + * permanence; prune telemetry; export so the durable one is shown + * to survive while the ephemeral one is swept. + * offcheck — flag OFF. Prove creating an ISE forms NO edges and + * engram_consolidate_permanence is a no-op (byte-identical OFF path). + */ +#include "el_runtime.h" +#include +#include +#include + +static el_val_t S(const char* s){ return EL_STR(s); } +static el_val_t F(double d){ return el_from_float(d); } + +static void build_seed(void){ + el_val_t a = engram_node_full(S("hebbian potentiation strengthens co-active memory links"), + S("Concept"), S("hebb-a"), F(0.9), F(0.85), F(1.0), S("Semantic"), + S("hebbian,memory,activation")); + el_val_t b = engram_node_full(S("co-active memory links accrue hebbian associative weight"), + S("Concept"), S("hebb-b"), F(0.9), F(0.85), F(1.0), S("Semantic"), + S("hebbian,memory,weight")); + el_val_t c = engram_node_full(S("unrelated culinary recipe for sourdough bread"), + S("Fact"), S("distractor-1"), F(0.4), F(0.4), F(1.0), S("Semantic"), S("food")); + el_val_t d = engram_node_full(S("the weather forecast predicts rain tomorrow afternoon"), + S("Fact"), S("distractor-2"), F(0.4), F(0.4), F(1.0), S("Semantic"), S("weather")); + engram_connect(a, b, F(0.8), S("associate")); + engram_connect(a, c, F(0.3), S("associate")); + engram_connect(b, d, F(0.3), S("associate")); +} +static const char* QUERY = + "hebbian potentiation co-active memory links associative weight"; + +int main(int argc, char** argv){ + if (argc < 3){ fprintf(stderr,"usage: %s \n",argv[0]); return 2; } + const char* mode = argv[1]; + const char* dir = argv[2]; + char p[1024]; + + if (!strcmp(mode,"accrual")){ + build_seed(); + int samples[] = {1,10,50,100,250,500,1000,1625,2000,2500,3000}; + int ns = (int)(sizeof samples/sizeof samples[0]); + int NMAX = samples[ns-1]; + int si = 0; + for (int n=1; n<=NMAX; n++){ + engram_activate_json(S(QUERY), (el_val_t)3); + if (si=0.0&&v<=1.0))v=0.6; return v; } +static double eg_consol_perm_min(void){ const char* s=getenv("ENGRAM_CONSOL_PERM_MIN"); return s?atof(s):0.9; } +static int eg_consol_wm_topk(void){ const char* s=getenv("ENGRAM_CONSOL_WM_TOPK"); int v=s?atoi(s):5; if(v<0)v=0; if(v>64)v=64; return v; } +/* Durable marker is carried in node metadata (persisted, reversible) rather than + * a new struct field, so it survives the store round-trip with no schema change. */ +static int eg_node_is_durable(const EngramNode* n){ + return n && n->metadata && strstr(n->metadata, "consolidated-from-ISE") != NULL; +} +static void eg_consolidate_ise_connect(EngramNode* ise); + el_val_t engram_node_full(el_val_t content, el_val_t node_type, el_val_t label, el_val_t salience, el_val_t importance, el_val_t confidence, el_val_t tier, el_val_t tags) { @@ -7956,6 +7986,8 @@ el_val_t engram_node_full(el_val_t content, el_val_t node_type, el_val_t label, engram_idmap_put(g, n->id, new_idx_full); engram_adj_on_node_added(g); if (engram_store_enabled()) eg_store_put_node(n); + /* P1 CONNECTION threshold: self-gated (inert unless ENGRAM_CONSOLIDATION). */ + eg_consolidate_ise_connect(n); return el_wrap_str(el_strdup(n->id)); } @@ -8266,6 +8298,10 @@ el_val_t engram_prune_telemetry(el_val_t older_than_ms) { n->created_at < cutoff && !(n->label && strcmp(n->label, "session-start") == 0) && !(n->content && strstr(n->content, "self_review")); + /* P1 PERMANENCE: a node promoted past the permanence bar is durable and + * must not be swept by the 48h telemetry prune. Gated so the OFF path is + * byte-identical (no trunk node ever carries the marker). */ + if (eg_consolidation_on() && prunable && eg_node_is_durable(n)) prunable = 0; if (prunable && removed < cap) { removed_ids[removed++] = n->id; /* keep id for edge sweep */ free(n->content); free(n->node_type); free(n->label); @@ -8767,6 +8803,83 @@ static int eg_edge_exists_between(EngramStore* g, const char* a, const char* b) return 0; } +/* P1 CONNECTION threshold (design §9). When a strongly-firing ISE is created, + * wire hebbian-associate edges from it to the wm_top nodes active at that + * moment. Self-gated: returns immediately unless ENGRAM_CONSOLIDATION is set, + * the node is an InternalStateEvent, and its salience clears the connection bar + * — so a sub-threshold ISE forms nothing (a shower thought), which then drifts + * out at the 48h prune. Edges are provenance-tagged "consolidated-from-ISE" + * (reversible: deletable by that marker) and dedup-guarded via + * eg_edge_exists_between. They accrue/decay through the normal hebb machinery + * and are swept with their ISE at the 48h prune unless the ISE is promoted to + * permanence. O(node_count) per qualifying ISE — same order as the + * engram_prune_telemetry already run on ISE insert. */ +static void eg_consolidate_ise_connect(EngramNode* ise){ + if(!eg_consolidation_on()) return; + if(!ise || !ise->node_type || strcmp(ise->node_type,"InternalStateEvent")!=0) return; + if(ise->salience < eg_consol_conn_min()) return; /* below connection bar */ + int K = eg_consol_wm_topk(); + if(K<=0) return; + if(K>64) K=64; + EngramStore* g = engram_get(); + /* Select the top-K working-memory members by weight (excluding ISEs and + * self). Bounded selection, no allocation. */ + int64_t best_idx[64]; double best_w[64]; int nb=0; + for(int64_t i=0;inode_count;i++){ + EngramNode* t=&g->nodes[i]; + if(t==ise) continue; + double w=t->working_memory_weight; + if(w<=0.0) continue; + if(t->node_type && strcmp(t->node_type,"InternalStateEvent")==0) continue; + if(nbbest_w[m]){ best_w[m]=w; best_idx[m]=i; } } + } + int64_t now = engram_now_ms(); + for(int b=0;bnodes[best_idx[b]]; + if(eg_edge_exists_between(g, ise->id, t->id)) continue; /* dedup guard */ + engram_grow_edges(); + EngramEdge* ne=&g->edges[g->edge_count]; + memset(ne,0,sizeof(*ne)); + ne->id = engram_new_id(); + ne->from_id = el_strdup_persist(ise->id); + ne->to_id = el_strdup_persist(t->id); + ne->relation = el_strdup_persist("hebbian-associate"); + ne->metadata = el_strdup_persist("{\"origin\":\"consolidated-from-ISE\"}"); + ne->weight = ENGRAM_HEBB_LINK_W0; + ne->hebb = ENGRAM_HEBB_ETA; /* nonzero so the trace can rehearse */ + ne->confidence= 1.0; + ne->created_at= now; ne->updated_at= now; ne->last_fired= now; + ne->layer_id = ENGRAM_LAYER_DEFAULT; + g->edge_count++; + engram_adj_on_edge_added(g, g->edge_count-1); + eg_hebb_wb_push(ne->from_id, ne->to_id, ne->weight, ne->hebb); + } +} + +/* P1 PERMANENCE threshold (design §9). Promote a node past the higher bar: + * mark it durable (metadata provenance "consolidated-from-ISE") so the 48h + * telemetry prune no longer sweeps it, and mirror to the durable store. The + * caller (soul) decides WHEN to attempt promotion (rehearsal / re-activation); + * this primitive enforces the bar on the node's ACT-R base-level and is + * idempotent (no double-promote). Returns 1 if the node is durable after the + * call, else 0. Reversible: clear the marker to demote. Inert unless + * ENGRAM_CONSOLIDATION is set. */ +el_val_t engram_consolidate_permanence(el_val_t node_id){ + if(!eg_consolidation_on()) return (el_val_t)(int64_t)0; + EngramNode* n = engram_find_node(EL_CSTR(node_id)); + if(!n) return (el_val_t)(int64_t)0; + if(eg_node_is_durable(n)) return (el_val_t)(int64_t)1; /* already durable */ + double bl = engram_bll_base_level(n, engram_now_ms()); + if(bl < eg_consol_perm_min()) return (el_val_t)(int64_t)0; /* below permanence bar */ + free(n->metadata); + n->metadata = el_strdup_persist("{\"origin\":\"consolidated-from-ISE\",\"durable\":true}"); + n->updated_at = engram_now_ms(); + if(engram_store_enabled()) eg_store_put_node(n); + return (el_val_t)(int64_t)1; +} + /* engram_temporal_decay — recency shaping on the activation path. * * MEASURED FAILURE (2026-08-05 self-review). Census of the live graph under diff --git a/lang/runtime/el_runtime.h b/lang/runtime/el_runtime.h index 1b6b55b..efff522 100644 --- a/lang/runtime/el_runtime.h +++ b/lang/runtime/el_runtime.h @@ -622,6 +622,7 @@ el_val_t engram_search_json(el_val_t query, el_val_t limit); el_val_t engram_scan_nodes_json(el_val_t limit, el_val_t offset); el_val_t engram_scan_nodes_by_type_json(el_val_t node_type, el_val_t limit, el_val_t offset); el_val_t engram_scan_nodes_emb_json(el_val_t limit, el_val_t offset); +el_val_t engram_consolidate_permanence(el_val_t node_id); el_val_t engram_neighbors_json(el_val_t node_id, el_val_t max_depth, el_val_t direction); el_val_t engram_activate_json(el_val_t query, el_val_t depth); el_val_t engram_stats_json(void); diff --git a/lang/runtime/el_seed.c b/lang/runtime/el_seed.c index e9809e8..6b65ec6 100644 --- a/lang/runtime/el_seed.c +++ b/lang/runtime/el_seed.c @@ -1090,6 +1090,10 @@ el_val_t __engram_scan_nodes_emb_json(el_val_t limit, el_val_t offset) { return engram_scan_nodes_emb_json(limit, offset); } +el_val_t __engram_consolidate_permanence(el_val_t node_id) { + return engram_consolidate_permanence(node_id); +} + el_val_t __engram_neighbors_json(el_val_t node_id, el_val_t max_depth, el_val_t direction) { return engram_neighbors_json(node_id, max_depth, direction); }