feat(v4): Phase 7 Task 3 -- the inference engine and Loop #7 in each node's heartbeat cycle (#189)

v4/system/inference.c: v3's inference_engine.c and its Q48.16 arithmetic ported to the bit, no C library --
the variance, the window width (Levene), the decay slope (log-linear regression), the run with its 5%
early exit. v4/tests/test_inference.c links v3's own and compares them: POST's fixed cases, 20000 random
multiply/log pairs, 400 random trajectories (make -C v4 inference; in make -C v4 test). In each node's
cycle every 1000 ticks (HEARTBEAT_INFERENCE_FREQUENCY), once the window is warm: the trajectory from the
window's words' heat, the width and slope applied unless it exits early, then Loop #7 -- the heartrate
slower when stable, faster when not, within 1/4..4x; Hera's re-arms the kernel's heartbeat
(heartbeat_set_adaptive_period_ns), as v3's Hera. L8 reads its outcome. Test first: test_mesh_boot.c 72
checks, 80 on. amd64 PASS, 163 s. Not pushed (held to the end of Phase 7).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-13 23:59:18 +00:00
parent d09eee9a08
commit 6d954ac279
13 changed files with 434 additions and 14 deletions
+81
View File
@@ -0,0 +1,81 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_inference.c -- v4's inference engine (v4/inference.h) against v3's own, to the bit (#189, Phase 7,
* Task 3; Captain Bob, 2026-11-13: "Rewrite for v4", v3 the specification). v3's inference_engine.c and its
* Q48.16 code are linked in and run on the same trajectories: fixed ones, and many made at random. */
#include "v4/inference.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef uint64_t q48_16_t;
q48_16_t compute_variance_q48(const uint64_t *heat_data, uint64_t length);
uint32_t find_variance_inflection(const uint64_t *heat_data, uint64_t trajectory_length, q48_16_t full_variance);
uint64_t infer_decay_slope_q48(const uint64_t *heat_data, uint64_t length);
q48_16_t q48_log_approx(uint64_t x);
q48_16_t q48_mul(q48_16_t a, q48_16_t b);
/* what v3's engine reaches only through extract_heat_trajectory, which this test does not run */
void *vm_dictionary_lookup_by_word_id(void *vm, uint32_t id);
void *vm_dictionary_lookup_by_word_id(void *vm, uint32_t id) { (void)vm; (void)id; return 0; }
uint64_t rolling_window_export_execution_history(const void *w, uint32_t *out, uint64_t max);
uint64_t rolling_window_export_execution_history(const void *w, uint32_t *out, uint64_t max) { (void)w; (void)out; (void)max; return 0; }
void sf_mutex_lock(void *m);
void sf_mutex_lock(void *m) { (void)m; }
void sf_mutex_unlock(void *m);
void sf_mutex_unlock(void *m) { (void)m; }
static int failures = 0, checks = 0;
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
static uint32_t rng = 0x2545F491u;
static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 17; rng ^= rng << 5; return rng; }
static uint64_t h[4096];
static int same(uint64_t n)
{
return v4_inference_variance(h, n) == compute_variance_q48(h, n)
&& v4_inference_window_width(h, n) == find_variance_inflection(h, n, 0)
&& v4_inference_decay_slope(h, n) == infer_decay_slope_q48(h, n);
}
int main(void)
{
unsigned k, bad = 0;
uint64_t x;
printf("v4 inference engine against v3's\n");
/* POST's own: 100 200 300, and 300 200 100 */
h[0] = 100; h[1] = 200; h[2] = 300;
CHECK(v4_inference_variance(h, 3) == compute_variance_q48(h, 3), "the variance of 100 200 300: %llu, v3 %llu",
(unsigned long long)v4_inference_variance(h, 3), (unsigned long long)compute_variance_q48(h, 3));
CHECK(v4_inference_window_width(h, 3) == find_variance_inflection(h, 3, 0), "its window width: %u, v3 %u",
v4_inference_window_width(h, 3), find_variance_inflection(h, 3, 0));
h[0] = 300; h[1] = 200; h[2] = 100;
CHECK(v4_inference_decay_slope(h, 3) == infer_decay_slope_q48(h, 3), "the decay slope of 300 200 100: %llu, v3 %llu",
(unsigned long long)v4_inference_decay_slope(h, 3), (unsigned long long)infer_decay_slope_q48(h, 3));
/* the arithmetic underneath */
for (k = 0; k < 20000; k++) {
uint64_t a = ((uint64_t)rnd() << 32 | rnd()) >> (rnd() % 40), b = ((uint64_t)rnd() << 32 | rnd()) >> (rnd() % 40);
if (v4_q48_mul(a, b) != q48_mul(a, b) || v4_q48_log(a) != q48_log_approx(a)) bad++;
}
CHECK(bad == 0, "Q48.16 multiply and log agree on 20000 random pairs (%u differ)", bad);
for (x = 0; x < 300000; x += 7) if (v4_q48_log(x) != q48_log_approx(x)) { bad++; break; }
CHECK(bad == 0, "and the log on every 7th value to 300000");
/* trajectories: short and long, flat, ramps, noise, bursts, zeros */
bad = 0;
for (k = 0; k < 400; k++) {
uint64_t n = 2 + rnd() % 4095, i, shape = k % 5, top = 1 + rnd() % (k % 3 == 0 ? 60u : 100000u);
for (i = 0; i < n; i++)
h[i] = shape == 0 ? top
: shape == 1 ? (n - i) * top / n
: shape == 2 ? rnd() % (top + 1)
: shape == 3 ? ((i / 64) % 2 ? top : rnd() % 3)
: (rnd() % 4 ? 0 : rnd() % (top + 1));
if (!same(n)) { bad++; if (bad < 4) printf(" differ: shape %u, n %lu\n", (unsigned)shape, (unsigned long)n); }
}
CHECK(bad == 0, "variance, window width and decay slope agree on 400 trajectories (%u differ)", bad);
printf(" %d checks, %d failures\n", checks, failures);
return failures ? 1 : 0;
}
+29 -4
View File
@@ -36,7 +36,9 @@ static v4_mesh ms;
static v4_mesh_account accounts[PLACES * V4_MESH_WORD_IDS]; /* the words' records (#189, Phase 2) */
static unsigned char is_word[PLACES * (V4_NODE_WORDS / 8u)];
static v4_mesh_physics physics[PLACES];
static unsigned forgotten;
static unsigned forgotten, heartrates;
static uint64_t heartrate_ns;
static void heartrate(void *self, uint64_t ns) { (void)self; heartrates++; heartrate_ns = ns; }
static v4_cell forgotten_id = -1;
static void forgot(void *self, unsigned place, v4_cell id) { (void)self; (void)place; forgotten++; forgotten_id = id; }
static v4_uheat_t charged[V4_MESH_WORD_IDS];
@@ -110,7 +112,7 @@ int main(void)
ms.out = out; ms.key = key; ms.words = words; ms.word_count = 1; ms.serve = serve;
ms.capsules = capsules; ms.capsule_count = 2; ms.im = &im; ms.self = 0;
ms.accounts = accounts; ms.is_word = is_word; ms.forgot = forgot;
ms.physics = physics; ms.alloc = alloc; ms.release = release;
ms.physics = physics; ms.alloc = alloc; ms.release = release; ms.heartrate = heartrate;
/* ---- the boot ---- */
CHECK(v4_mesh_boot(&ms) == 1, "the boot: %s", said);
@@ -216,6 +218,23 @@ int main(void)
r = line(V4_MESH_HERA, "FORGET L8T", &how);
}
/* ---- inference every 1000 ticks, and Loop #7 (#189 Phase 7, Task 3) ---- */
{
v4_mesh_physics *p = &ms.physics[0];
uint64_t t0 = p->tick, runs0 = p->inference_runs;
r = line(V4_MESH_HERA, ": IW ; : LIW 30 0 DO 10000 0 DO IW LOOP LOOP ; LIW", &how); /* 300000 calls: over 1000 ticks */
CHECK(how == V4_TEXT_COMPLETED && p->tick - t0 > 1000u && p->is_warm, "a long run: over 1000 ticks (%lu), the window warm",
(unsigned long)(p->tick - t0));
CHECK(p->inference_runs > runs0, "inference ran (%lu times)", (unsigned long)p->inference_runs);
CHECK(p->tick_target_ns != HEARTBEAT_TICK_NS && p->tick_target_ns >= HEARTBEAT_TICK_NS / 4u
&& p->tick_target_ns <= HEARTBEAT_TICK_NS * 4u, "Loop #7 moved the heartrate, within its bounds: %lu ns",
(unsigned long)p->tick_target_ns);
CHECK(heartrates > 0 && heartrate_ns == (p->tick_target_ns * 10000000ULL) / HEARTBEAT_TICK_NS,
"and on Hera re-armed the kernel's heartbeat: %lu ns", (unsigned long)heartrate_ns);
CHECK(!p->inference.early_exited ? p->last_inference_tick > 0 : 1, "a full run marks its tick");
r = line(V4_MESH_HERA, "FORGET IW", &how);
}
/* ---- the words' accounts (#189, Phase 2; Captain Bob, 2026-11-13: the word ID in the header, charged
* per heartbeat tick) ---- */
{
@@ -229,8 +248,9 @@ int main(void)
/* Phase 7, Task 1: v3's physics on the record (DictPhysics; v3/include/vm.h) */
CHECK(a && a->heat == 0 && a->temperature_q8 == 0 && a->state_flags == 0,
"a new word's physics: no heat, no temperature, no state flags (background decay may have touched it, as v3's)");
CHECK(ms.physics[0].decay_slope_q48 == 21845u && ms.physics[0].l3_decay == 1,
"each node has v3's initial decay slope (Q48.16, 1/3) and Loop #3 on");
CHECK(ms.physics[0].decay_slope_q48 == 21845u || ms.physics[0].decay_slope_q48 == ms.physics[0].inference.adaptive_decay_slope,
"each node's decay slope: v3's initial (Q48.16, 1/3), or the one inference has set since (%lu)",
(unsigned long)ms.physics[0].decay_slope_q48);
r = line(V4_MESH_HERA, ": IMM ; IMMEDIATE ' IMM .", &how);
{
v4_cell ix = (v4_cell)strtol(r, 0, 10);
@@ -246,6 +266,8 @@ int main(void)
const v4_mesh_account *a;
uint64_t total0;
uint32_t every = p->cycle_every;
uint64_t slope = p->decay_slope_q48;
p->decay_slope_q48 = 21845u; /* v3's initial, for decay's arithmetic seen exactly */
r = line(V4_MESH_HERA, ": P7 DUP * ; ' P7 .", &how);
a = v4_mesh_account_at(&ms, 0, (v4_cell)strtol(r, 0, 10));
/* the tick, by execution (Captain Bob, 2026-11-13: "By execution, as v3"): every 256 word calls a
@@ -299,6 +321,8 @@ int main(void)
v4_cell q7;
const v4_mesh_account *qa;
uint64_t c0 = p->decay_cycles;
p->last_inference_tick = p->tick; /* no inference in this check's 1000 ticks: v3's slope stays */
p->decay_slope_q48 = 21845u;
r = line(V4_MESH_HERA, ": Q7 ; : LQ7 100 0 DO Q7 LOOP ; LQ7 ' Q7 .", &how);
q7 = (v4_cell)strtol(r, 0, 10);
qa = v4_mesh_account_at(&ms, 0, q7);
@@ -314,6 +338,7 @@ int main(void)
r = line(V4_MESH_HERA, "FORGET Q7", &how);
}
v4_mesh_l8_force(&ms, 0, ms.physics[0].l8.current_card, 0); /* L8 chooses again */
p->decay_slope_q48 = slope;
r = line(V4_MESH_HERA, "FORGET P7", &how);
}
r = line(V4_MESH_HERA, "3 SQ .", &how);