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
+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);