From 0af39df16f2bb328ea57f7cd231802ef91e928c3 Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Wed, 12 Aug 2026 23:41:09 -0500 Subject: [PATCH] =?UTF-8?q?M-INTEROCEPTION=20P2:=20chronoception=20?= =?UTF-8?q?=E2=80=94=20age=20the=20activation=20field=20by=20measured=20wa?= =?UTF-8?q?ll-clock=20delta=20(ENGRAM=5FCHRONOCEPTION,=20default=20OFF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The felt passage of time is the cooling of the activation field, not a tick count and not an elapsed-seconds readout. engram_age_field(delta_ms) cools the field (working_memory_weight + background_activation) by the caller's MEASURED wall-clock delta with a pure exponential exp(-dt/TC) — no per-call floor — so it is exactly scale-invariant: N ticks summing to the same elapsed time produce the same total cooling. It returns the cooling MAGNITUDE (1-exp(-dt/TC), a bounded [0,1) drift signal), never elapsed seconds. Reboot = anesthesia: a global last-tick wall-clock stamp is persisted to a sidecar (chrono_last_tick) in the data dir. engram_age_field_catchup() reads it on boot, applies ONE cooling for the whole unconscious gap, refreshes the stamp, and reports the magnitude — timestamps are bookkeeping to COMPUTE the drift, never the felt signal. TC env-tunable via ENGRAM_CHRONO_TC (default 3600s). All inert unless ENGRAM_CHRONOCEPTION is set → OFF path byte-identical. MEASURED on a copy (throwaway HOME, TC=3600s): - Cooling scales with dt, matching 1-exp(-dt/TC) to 1e-6: dt=600s->0.1535, 1800s->0.3935, 3600s->0.6321, 7200s->0.8647. - Scale-invariance EXACT: age(dt) once vs age(dt/N) N times gives identical field sum (|delta|=0.0) for N=2, 10, 100. - Reboot catch-up over a 1h gap cooled the field 0.60->0.4415 in one shot, magnitude 0.6321, bounded in [0,1). - Flag OFF: age & catchup return 0, field untouched (sum 1.2). ASan+UBSan clean. --- engram/test/run_interoception_p2.sh | 96 ++++++++++++++++++++++ engram/test/test_interoception_p2_chrono.c | 95 +++++++++++++++++++++ lang/runtime/el_runtime.c | 69 ++++++++++++++++ lang/runtime/el_runtime.h | 3 + lang/runtime/el_seed.c | 4 + 5 files changed, 267 insertions(+) create mode 100755 engram/test/run_interoception_p2.sh create mode 100644 engram/test/test_interoception_p2_chrono.c diff --git a/engram/test/run_interoception_p2.sh b/engram/test/run_interoception_p2.sh new file mode 100755 index 0000000..3e209b5 --- /dev/null +++ b/engram/test/run_interoception_p2.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# M-INTEROCEPTION P2 gate: chronoception (ENGRAM_CHRONOCEPTION). +# Throwaway HOME + /tmp only. TC defaults to 3600s; we pin it for the math. +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-p2-XXXXXX)" +export HOME="$WORK/home"; mkdir -p "$HOME" +export ENGRAM_CHRONO_TC=3600 # pin cooling time-constant for the math +unset ENGRAM_STORE +fail=0 + +echo "== compile ==" +gcc -O1 -std=c11 -I "$INC" "$HERE/test_interoception_p2_chrono.c" "$RT" "$ST" "$GEO" "$VIDX" \ + -lcurl -lm -o "$WORK/p2" 2>"$WORK/cc.log" || { echo "COMPILE FAILED"; cat "$WORK/cc.log"; rm -rf "$WORK"; exit 1; } + +sum_wm(){ python3 -c "import json,sys; g=json.load(open('$1')); print(sum(n.get('working_memory_weight',0) for n in g['nodes']))"; } + +echo +echo "== (a) cooling scales with dt (flag ON) ==" +for DT in 600000 1800000 3600000 7200000; do # 600s,1800s,3600s,7200s at TC=3600 + D="$WORK/dt$DT"; mkdir -p "$D" + ( export ENGRAM_CHRONOCEPTION=1; "$WORK/p2" once "$D" "$DT" ) >"$D/out.txt" 2>&1 + MAG=$(grep MAGNITUDE "$D/out.txt" | awk '{print $2}') + WM=$(sum_wm "$D/field.json") + PRED=$(python3 -c "import math; print(round(1-math.exp(-$DT/1000/3600),6))") + echo " dt=${DT}ms magnitude=$MAG predicted 1-exp(-dt/TC)=$PRED field_wm_sum=$WM" + python3 -c "import sys; m=float('$MAG'); p=float('$PRED'); sys.exit(0 if abs(m-p)<1e-4 else 1)" \ + && echo " PASS: magnitude matches exp cooling" || { echo " FAIL"; fail=1; } +done + +echo +echo "== (b) SCALE-INVARIANCE: age(dt) once == age(dt/N) N times (field within float tol) ==" +DT=3600000 +for N in 2 10 100; do + DA="$WORK/inv_once_$N"; DB="$WORK/inv_split_$N"; mkdir -p "$DA" "$DB" + ( export ENGRAM_CHRONOCEPTION=1; "$WORK/p2" once "$DA" "$DT" ) >/dev/null 2>&1 + ( export ENGRAM_CHRONOCEPTION=1; "$WORK/p2" split "$DB" "$DT" "$N" ) >/dev/null 2>&1 + WA=$(sum_wm "$DA/field.json"); WB=$(sum_wm "$DB/field.json") + echo " N=$N once_wm=$WA split_wm=$WB |delta|=$(python3 -c "print(abs($WA-$WB))")" + python3 -c "import sys; sys.exit(0 if abs($WA-$WB)<1e-9 else 1)" \ + && echo " PASS: scale-invariant within 1e-9" || { echo " FAIL: not scale-invariant"; fail=1; } +done + +echo +echo "== (c) REBOOT catch-up: one-shot cooling from persisted last-tick, reports MAGNITUDE not seconds ==" +D="$WORK/catch"; mkdir -p "$D" +GAP=3600000 # 1h unconscious +( export ENGRAM_CHRONOCEPTION=1 ENGRAM_DATA_DIR="$D"; "$WORK/p2" catchup "$D" "$GAP" ) >"$D/out.txt" 2>&1 +CMAG=$(grep CATCHUP_MAGNITUDE "$D/out.txt" | awk '{print $2}') +CWM=$(sum_wm "$D/field.json") +PRED=$(python3 -c "import math; print(round(1-math.exp(-$GAP/1000/3600),4))") +echo " gap=${GAP}ms catchup_magnitude=$CMAG predicted=$PRED field_wm_sum=$CWM (was 0.6)" +python3 -c "import sys; sys.exit(0 if abs(float('$CMAG')-float('$PRED'))<1e-2 else 1)" \ + && echo " PASS: one-shot catch-up cooled by the elapsed gap, surfaced as a magnitude" \ + || { echo " FAIL"; fail=1; } +# honesty rail: magnitude is bounded [0,1), NOT an elapsed-seconds number +python3 -c "import sys; m=float('$CMAG'); sys.exit(0 if 0<=m<1 else 1)" \ + && echo " PASS: magnitude is a bounded drift signal in [0,1), never elapsed seconds" \ + || { echo " FAIL: magnitude out of [0,1)"; fail=1; } + +echo +echo "== (d) OFF path: flag unset -> age & catchup return 0, field untouched ==" +D="$WORK/off"; mkdir -p "$D" +( unset ENGRAM_CHRONOCEPTION; export ENGRAM_DATA_DIR="$D"; "$WORK/p2" offcheck "$D" 3600000 ) >"$D/out.txt" 2>&1 +cat "$D/out.txt" | sed 's/^/ /' +OFFWM=$(sum_wm "$D/field.json") +# loaded field wm sum = (1.0+0.8+0.6)*0.5 halving = 1.2 ; must be UNCHANGED +echo " field_wm_sum=$OFFWM (expected 1.2, unchanged)" +python3 -c "import sys; sys.exit(0 if abs($OFFWM-1.2)<1e-9 else 1)" \ + && echo " PASS: OFF path leaves the field byte-identical (no aging)" \ + || { echo " FAIL: OFF path modified the field"; fail=1; } + +echo +echo "== ASan+UBSan ==" +gcc -O1 -g -std=c11 -fsanitize=address,undefined -fno-sanitize-recover=undefined \ + -I "$INC" "$HERE/test_interoception_p2_chrono.c" "$RT" "$ST" "$GEO" "$VIDX" \ + -lcurl -lm -o "$WORK/p2.san" 2>"$WORK/san_cc.log" || { echo "SAN COMPILE FAILED"; tail -25 "$WORK/san_cc.log"; fail=1; } +if [ -x "$WORK/p2.san" ]; then + export ASAN_OPTIONS=detect_leaks=0 + DS="$WORK/san"; mkdir -p "$DS" + ( export ENGRAM_CHRONOCEPTION=1 ENGRAM_DATA_DIR="$DS"; "$WORK/p2.san" once "$DS" 3600000 ) >/dev/null 2>"$WORK/san.log" + ( export ENGRAM_CHRONOCEPTION=1 ENGRAM_DATA_DIR="$DS"; "$WORK/p2.san" catchup "$DS" 3600000 ) >/dev/null 2>>"$WORK/san.log" + if grep -qiE 'runtime error|AddressSanitizer|Sanitizer|ERROR: ' "$WORK/san.log"; then + echo " FAIL: sanitizer findings:"; grep -iE 'runtime error|Sanitizer|ERROR' "$WORK/san.log" | head; fail=1 + else echo " ok: ASan+UBSan clean"; fi +fi + +echo +if [ "$fail" -eq 0 ]; then echo "====== P2 CHRONOCEPTION GATE: PASS ======"; else echo "====== P2 CHRONOCEPTION GATE: FAIL ======"; fi +rm -rf "$WORK" +exit $fail diff --git a/engram/test/test_interoception_p2_chrono.c b/engram/test/test_interoception_p2_chrono.c new file mode 100644 index 0000000..f800527 --- /dev/null +++ b/engram/test/test_interoception_p2_chrono.c @@ -0,0 +1,95 @@ +/* test_interoception_p2_chrono.c — M-INTEROCEPTION Priority 2. + * Chronoception: engram_age_field(delta_ms) + reboot catch-up + * (ENGRAM_CHRONOCEPTION, default OFF). + * + * Uses loaded snapshots with KNOWN working_memory_weight / background_activation + * so the field is deterministic without depending on activation. (engram_load + * halves WM on boot — the laundering step — so snapshot wm 1.0 -> 0.5 resident.) + * + * Modes: + * once — age the field once by dt; save field.json. + * split — age by dt/N, N times; save field.json. + * (once vs split must match: scale-invariance.) + * catchup — write a last-tick gap_ms in the past, then + * engram_age_field_catchup(); print MAGNITUDE. + * offcheck — flag OFF: age returns 0 and field is untouched. + */ +#include "el_runtime.h" +#include +#include +#include +#include + +static el_val_t S(const char* s){ return EL_STR(s); } + +static void write_seed(const char* dir){ + char p[1024]; snprintf(p,sizeof p,"%s/seed.json",dir); + FILE* f=fopen(p,"w"); + fprintf(f,"{\"nodes\":[" + "{\"id\":\"f1\",\"content\":\"field node 1\",\"node_type\":\"Concept\",\"label\":\"f1\"," + "\"salience\":0.9,\"confidence\":1.0,\"working_memory_weight\":1.0,\"background_activation\":0.5}," + "{\"id\":\"f2\",\"content\":\"field node 2\",\"node_type\":\"Concept\",\"label\":\"f2\"," + "\"salience\":0.8,\"confidence\":1.0,\"working_memory_weight\":0.8,\"background_activation\":0.4}," + "{\"id\":\"f3\",\"content\":\"field node 3\",\"node_type\":\"Concept\",\"label\":\"f3\"," + "\"salience\":0.7,\"confidence\":1.0,\"working_memory_weight\":0.6,\"background_activation\":0.3}" + "],\"edges\":[]}"); + fclose(f); +} +static void load_seed(const char* dir){ + char p[1024]; snprintf(p,sizeof p,"%s/seed.json",dir); + write_seed(dir); + if(!engram_load(S(p))){ fprintf(stderr,"load failed\n"); exit(2); } +} +static void save_field(const char* dir){ + char p[1024]; snprintf(p,sizeof p,"%s/field.json",dir); + engram_save(S(p)); +} + +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]; + + if(!strcmp(mode,"once")){ + double dt=atof(argv[3]); + load_seed(dir); + el_val_t mag=engram_age_field((el_val_t)(int64_t)dt); + printf("MAGNITUDE %.10f\n", el_to_float(mag)); + save_field(dir); + return 0; + } + if(!strcmp(mode,"split")){ + double dt=atof(argv[3]); int N=atoi(argv[4]); if(N<1)N=1; + load_seed(dir); + double sub=dt/(double)N; + for(int i=0;i/chrono_last_tick. */ + struct timeval tv; gettimeofday(&tv,NULL); + long long now_ms=(long long)tv.tv_sec*1000+tv.tv_usec/1000; + long long last=now_ms-(long long)gap; + char p[1200]; snprintf(p,sizeof p,"%s/chrono_last_tick",dir); + FILE* f=fopen(p,"w"); fprintf(f,"%lld\n",last); fclose(f); + el_val_t mag=engram_age_field_catchup(); + printf("CATCHUP_MAGNITUDE %.10f\n", el_to_float(mag)); + save_field(dir); + return 0; + } + if(!strcmp(mode,"offcheck")){ + double dt=atof(argv[3]); + load_seed(dir); + el_val_t mag=engram_age_field((el_val_t)(int64_t)dt); + el_val_t magc=engram_age_field_catchup(); + printf("OFF age_mag=%.10f catchup_mag=%.10f\n", el_to_float(mag), el_to_float(magc)); + save_field(dir); + return 0; + } + fprintf(stderr,"unknown mode %s\n",mode); return 2; +} diff --git a/lang/runtime/el_runtime.c b/lang/runtime/el_runtime.c index a97b3a6..4a47add 100644 --- a/lang/runtime/el_runtime.c +++ b/lang/runtime/el_runtime.c @@ -11836,6 +11836,75 @@ el_val_t engram_resolve_data_dir(void) { return el_wrap_str(el_strdup(engramdir)); } +/* ── M-INTEROCEPTION P2: chronoception (ENGRAM_CHRONOCEPTION, default OFF) ──── + * Design §9. The felt passage of time is the COOLING of the activation field, + * not a tick count and not an elapsed-seconds readout. engram_age_field cools + * the field (working_memory_weight + background_activation) by the MEASURED + * wall-clock delta the caller supplies, using a pure exponential exp(-dt/TC). + * + * SCALE-INVARIANCE is the whole point and is structural: the cooling is a pure + * multiply with NO per-call floor, so age(dt) applied once is bit-close to + * age(dt/N) applied N times — N ticks that sum to the same elapsed time produce + * the same total cooling. (A per-call snap-to-floor would break this, which is + * why there isn't one here.) + * + * Reboot = anesthesia: a global last-tick wall-clock stamp is persisted to a + * sidecar in the data dir. On boot, engram_age_field_catchup reads it, applies + * ONE cooling for the whole unconscious gap, and reports the COOLING MAGNITUDE — + * how far the field drifted — NEVER the elapsed seconds (timestamps are + * bookkeeping to COMPUTE the drift, never the felt signal). + * + * All inert unless ENGRAM_CHRONOCEPTION is set → OFF path byte-identical. */ +static int eg_chronoception_on(void){ + static int cached=-1; + if(cached<0){ const char* s=getenv("ENGRAM_CHRONOCEPTION"); cached=(s&&s[0]&&s[0]!='0')?1:0; } + return cached; +} +/* Field cooling time-constant in seconds; default = conversational span. */ +static double eg_chrono_tc(void){ const char* s=getenv("ENGRAM_CHRONO_TC"); double v=s?atof(s):ENGRAM_CARRY_TC; if(!(v>0.0))v=ENGRAM_CARRY_TC; return v; } + +el_val_t engram_age_field(el_val_t delta_ms){ + if(!eg_chronoception_on()) return el_from_float(0.0); + double dt_ms=(double)(int64_t)delta_ms; + if(dt_ms<=0.0) return el_from_float(0.0); + double factor=exp(-(dt_ms/1000.0)/eg_chrono_tc()); /* pure exponential, no floor */ + EngramStore* g=engram_get(); + for(int64_t i=0;inode_count;i++){ + g->nodes[i].working_memory_weight *= factor; + g->nodes[i].background_activation *= factor; + } + return el_from_float(1.0-factor); /* cooling magnitude in [0,1), NOT seconds */ +} + +static void eg_chrono_tick_path(char* out, size_t cap){ + el_val_t dd=engram_resolve_data_dir(); + snprintf(out, cap, "%s/chrono_last_tick", EL_CSTR(dd)); +} +/* Persist the global last-tick stamp (call each tick / before shutdown). */ +el_val_t engram_chrono_persist_tick(void){ + if(!eg_chronoception_on()) return (el_val_t)(int64_t)0; + char path[1200]; eg_chrono_tick_path(path,sizeof path); + FILE* f=fopen(path,"w"); if(!f) return (el_val_t)(int64_t)0; + fprintf(f,"%lld\n",(long long)engram_now_ms()); fclose(f); + return (el_val_t)(int64_t)1; +} +/* Boot-time one-shot catch-up. Reads the persisted last-tick, cools the field + * for the whole elapsed gap in a single application, refreshes the tick, and + * returns the COOLING MAGNITUDE (never elapsed seconds). 0 if no prior tick. */ +el_val_t engram_age_field_catchup(void){ + if(!eg_chronoception_on()) return el_from_float(0.0); + char path[1200]; eg_chrono_tick_path(path,sizeof path); + FILE* f=fopen(path,"r"); if(!f) return el_from_float(0.0); + long long last=0; int ok=fscanf(f,"%lld",&last); fclose(f); + if(ok!=1 || last<=0) return el_from_float(0.0); + int64_t now=engram_now_ms(); + double dt_ms=(double)(now-last); + if(dt_ms<=0.0) return el_from_float(0.0); + el_val_t mag=engram_age_field((el_val_t)(int64_t)dt_ms); + f=fopen(path,"w"); if(f){ fprintf(f,"%lld\n",(long long)now); fclose(f); } + return mag; +} + /* ── Integrity: store-level write-protection (§18.1, §18.3) ────────────────── * The protected set is DERIVED from the self-graph at call time, not hardcoded: * the self root and the values hub, plus every node adjacent to either (in diff --git a/lang/runtime/el_runtime.h b/lang/runtime/el_runtime.h index efff522..03702e7 100644 --- a/lang/runtime/el_runtime.h +++ b/lang/runtime/el_runtime.h @@ -623,6 +623,9 @@ 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_age_field(el_val_t delta_ms); +el_val_t engram_age_field_catchup(void); +el_val_t engram_chrono_persist_tick(void); 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 6b65ec6..a5260a4 100644 --- a/lang/runtime/el_seed.c +++ b/lang/runtime/el_seed.c @@ -1094,6 +1094,10 @@ el_val_t __engram_consolidate_permanence(el_val_t node_id) { return engram_consolidate_permanence(node_id); } +el_val_t __engram_age_field(el_val_t delta_ms) { return engram_age_field(delta_ms); } +el_val_t __engram_age_field_catchup(void) { return engram_age_field_catchup(); } +el_val_t __engram_chrono_persist_tick(void) { return engram_chrono_persist_tick(); } + 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); }