feat(v4, kernel): ONE adaptive heartbeat -- it paces itself on the Stadium's resident heat; every VM ticks on it, in lockstep

Captain Bob, 2026-11-17: "The adaptive heartbeat is its own thing ... It has its own independent RWOT and slope
provider. Only one output from the entire stadium is used as a load measurement for heart rate pacing. ... ONE
AS IN THE NUMBER 1 heartbeat (the adaptive one) runs the entire system." Ruled one at a time: the kernel's
heartbeat; resident heat its one input; held in POST; the Stadium charged as before today
(docs/v4.0.0/plans/2026-11-17-one-adaptive-heartbeat.md, its first version kept as superseded).

- kernel/src/heartbeat.c, timer.h: heartbeat_pace_from(load) -- its own window of samples, its own slope
  provider (v4's inference arithmetic) every HEARTBEAT_PACE_EVERY samples, Loop #7 setting its own period
  (not stable 20% faster, stable 25% slower, 1/4..4x the 10 ms base). No source (v3's path): nothing runs.
- kernel/src/vm/stadium.c: stadium_resident_heat_total(), read only -- the one output it paces by.
- v4/system/mesh.c, mesh.h: the owner's clock read at every fabric step; each tick delivered, one by one, to
  every node present -- its heartbeat cycle run, active, idle or blocked alike. The tick by execution, the
  nodes' own Loop #7 and the heartrate callback are gone.
- kernel/src/v4/sk_v4.c: the clock gives the heartbeat's bottom half its turn; 0 until POST has ended.
- Tests first, each seen to fail (does not build): kernel/tests/test_heartbeat.c (11 checks),
  test_stadium_node.c (16), test_mesh_boot.c's lockstep checks. Seven Phase 7 checks that assumed the tick by
  execution now drive the test's heartbeat (ruled). POST 990/990 with v3's heat cases as written;
  mesh-boot 103, -on 112, -noop 100, san-mesh-boot 103, every v4 program at both widths, every kernel host
  test: 0 failures. The full `sanitize` target not run (it outlasts the foreground limit).

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-17 20:55:14 +00:00
parent 4a5c9bfd42
commit 00be537503
12 changed files with 462 additions and 88 deletions
+58 -41
View File
@@ -37,10 +37,13 @@ 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, heartrates;
static uint64_t heartrate_ns;
static void heartrate(void *self, uint64_t ns) { (void)self; heartrates++; heartrate_ns = ns; }
static unsigned forgotten;
static v4_cell forgotten_id = -1;
/* the one adaptive heartbeat's tick, as the owner reads it to the mesh (the kernel's heartbeat_ticks() after POST;
* here the test's own): Captain Bob, 2026-11-17 -- every VM ticks on it, active or not */
static uint64_t heart;
static uint64_t clock_now(void *self) { (void)self; return heart; }
static void set_tick(uint64_t t);
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];
static void charge(void *ctx, v4_cell id, v4_uheat_t calls) { (void)ctx; charged[id] += calls; }
@@ -96,6 +99,9 @@ static const char *line(v4_cell node, const char *text, int *how)
return said;
}
/* the tick set for a check (v4_mesh_set_tick), and the heartbeat's clock with it, so no catch-up follows */
static void set_tick(uint64_t t) { v4_mesh_set_tick(&ms, t); heart = t; }
int main(void)
{
int how;
@@ -113,7 +119,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 = 3; 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.heartrate = heartrate;
ms.physics = physics; ms.alloc = alloc; ms.release = release; ms.clock = clock_now;
/* ---- the boot ---- */
CHECK(v4_mesh_boot(&ms) == 1, "the boot: %s", said);
@@ -150,6 +156,30 @@ int main(void)
CHECK(v4_mesh_node(&ms, 11) != 0, "node 11 is in the fabric");
r = line(11, "6 7 * .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "42 ") == 0, "node 11 answers a line of its own: \"%s\" (%d)", r, how);
/* ---- ONE ADAPTIVE HEARTBEAT (Captain Bob, 2026-11-17; plan 2026-11-17-one-adaptive-heartbeat): every node's
* tick is the heartbeat's -- the owner's clock -- and on each tick every node present runs its cycle, active or
* idle, in lockstep. Nothing in a VM paces it. ---- */
{
unsigned k, p11 = PLACES;
uint64_t c0, c11;
for (k = 0; k < PLACES; k++) if (v4_fabric_node_at(&ms.f, k) == v4_mesh_node(&ms, 11)) p11 = k;
CHECK(p11 < PLACES, "node 11 has a place");
CHECK(ms.tick == 0 && ms.physics[0].tick == 0 && p11 < PLACES && ms.physics[p11].tick == 0,
"held: through the boot and POST the heartbeat gave no tick, and no node ticked (%lu)", (unsigned long)ms.tick);
c0 = ms.physics[0].decay_cycles; c11 = p11 < PLACES ? ms.physics[p11].decay_cycles : 0;
heart = 5;
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(ms.tick == 5 && ms.physics[0].tick == 5 && p11 < PLACES && ms.physics[p11].tick == 5,
"five ticks: Hera and node 11, idle, both at tick 5 -- one clock (%lu)", (unsigned long)ms.tick);
CHECK(ms.physics[0].decay_cycles - c0 == 5 && p11 < PLACES && ms.physics[p11].decay_cycles - c11 == 5,
"each tick delivered, one by one, to every node: five cycles each, idle node 11's too");
CHECK(p11 < PLACES && ms.physics[p11].snapshot.tick == 5, "node 11's last cycle was tick 5");
heart = 8;
(void)v4_mesh_step(&ms);
CHECK(ms.tick == 8 && p11 < PLACES && ms.physics[p11].tick == 8 && ms.physics[p11].decay_cycles - c11 == 8,
"between lines too: three more ticks, three more cycles, none lost (%lu)", (unsigned long)ms.tick);
}
r = line(V4_MESH_HERA, "S\" 6 7 * .\" 11 SEND 11 AWAIT .", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "42 ") && strstr(r, "1 "),
"and through Hera, as the prompt reaches it: she sends node 11 a line and awaits it: \"%s\" (%d)", r, how);
@@ -228,22 +258,14 @@ int main(void)
r = line(V4_MESH_HERA, "FORGET L8T", &how);
}
/* ---- the tick as v3's (Captain Bob, 2026-11-14, "v3"; "we can always change it late"): one a token the
* interpreter takes, one a word run from code defined at run time; the system's own internal calls, none ---- */
/* ---- the words run, as v3 counts them on their records: one a word run from code defined at run time, or
* dispatched by INTERPRET or EXECUTE; the system's own internal calls, none. (The tick no longer counts them:
* it is the one heartbeat's, Captain Bob 2026-11-17 -- the "tick as v3's" checks of 11-14 are withdrawn.) ---- */
{
v4_mesh_physics *p = &ms.physics[0];
uint64_t c0, c1;
const v4_mesh_account *wd;
c0 = p->tick * p->cycle_every + p->check_counter;
r = line(V4_MESH_HERA, "3 4 + .", &how);
c1 = p->tick * p->cycle_every + p->check_counter;
CHECK(strcmp(r, "7 ") == 0 && c1 - c0 == 4u, "3 4 + . counts 4, as v3: its four tokens (%lu)", (unsigned long)(c1 - c0));
r = line(V4_MESH_HERA, ": TSQ DUP * ; : TQ TSQ TSQ ;", &how);
c0 = p->tick * p->cycle_every + p->check_counter;
r = line(V4_MESH_HERA, "2 TQ .", &how);
c1 = p->tick * p->cycle_every + p->check_counter;
CHECK(strcmp(r, "16 ") == 0 && c1 - c0 >= 5u && c1 - c0 <= 9u,
"2 TQ . counts its 3 tokens and the words TQ runs, as v3 (%lu)", (unsigned long)(c1 - c0));
CHECK(strcmp(r, "16 ") == 0, "2 TQ . runs: %s", r);
r = line(V4_MESH_HERA, "' TSQ .", &how);
wd = v4_mesh_account_at(&ms, 0, (v4_cell)strtol(r, 0, 10));
CHECK(wd && wd->calls == 2, "TSQ, called twice from TQ, has 2 calls on its record (%lu)", wd ? (unsigned long)wd->calls : 0ul);
@@ -349,19 +371,16 @@ int main(void)
r = line(V4_MESH_HERA, "FORGET OPS", &how);
}
/* ---- inference every 1000 ticks, and Loop #7 (#189 Phase 7, Task 3) ---- */
/* ---- inference every 1000 ticks (#189 Phase 7, Task 3) -- on the one heartbeat's ticks (2026-11-17) ---- */
{
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 */
r = line(V4_MESH_HERA, ": IW ; : LIW 30 0 DO 10000 0 DO IW LOOP LOOP ; LIW", &how); /* 300000 calls */
heart += 1100u; (void)v4_mesh_step(&ms); /* and the one heartbeat ticks 1100 times (its ticks, not the calls') */
CHECK(how == V4_TEXT_COMPLETED && p->tick - t0 > 1000u && p->win.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);
/* Loop #7 is the heartbeat's own now (kernel/tests/test_heartbeat.c): a node paces nothing */
CHECK(!p->inference.early_exited ? p->last_inference_tick > 0 : 1, "a full run marks its tick");
CHECK(p->snapshot.tick == p->tick && p->snapshot.window_width == p->win.effective_window_size
&& p->snapshot.decay_slope_q48 == p->decay_slope_q48 && p->snapshot.fill == V4_MESH_WINDOW
@@ -399,20 +418,16 @@ int main(void)
v4_mesh_physics *p = &ms.physics[0];
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
* node's heartbeat cycle runs, and its tick is the count of them -- held still here, so decay's
* arithmetic is seen exactly, and seen moving below */
CHECK(every == 256u, "a node's heartbeat cycle comes every 256 word calls, as v3's HEARTBEAT_CHECK_FREQUENCY");
p->cycle_every = 0xFFFFFFFFu;
/* the tick is the one heartbeat's (Captain Bob, 2026-11-17): set by hand here, and the clock with it,
* so decay's arithmetic is seen exactly */
v4_mesh_l8_force(&ms, 0, 24u, 1); /* L8 held on card 24: L2 and L3 (and L7) -- the window and decay */
CHECK(p->l2_window && p->l3_decay && !p->l4_pipelining && !p->l1_cache && p->l7_adaptive_heartrate,
"card 24's gates: the window, decay, and L7, which is always on");
v4_mesh_set_tick(&ms, 100);
set_tick(100);
total0 = p->win.total_executions;
r = line(V4_MESH_HERA, "2 P7 3 P7 4 P7 + + .", &how);
CHECK(strcmp(r, "29 ") == 0 && a && a->heat == 3 && a->last_decay_tick == 100,
@@ -420,33 +435,33 @@ int main(void)
CHECK(p->win.total_executions >= total0 + 3 && p->win.history[(p->win.window_pos + V4_MESH_WINDOW - 1u) % V4_MESH_WINDOW]
!= (uint32_t)-1, "the rolling window took them (%lu)", (unsigned long)(p->win.total_executions - total0));
CHECK(a && a->temperature_q8 > 0, "and the temperature rose: %u", a ? a->temperature_q8 : 0u);
v4_mesh_set_tick(&ms, 106); /* six ticks at 21845 (1/3 a tick, Q48.16): 1 heat */
set_tick(106); /* six ticks at 21845 (1/3 a tick, Q48.16): 1 heat */
r = line(V4_MESH_HERA, "2 P7 .", &how);
CHECK(a && a->heat == 3 && a->last_decay_tick == 106, "six ticks on: one heat decayed, one added: %lu",
a ? (unsigned long)a->heat : 0ul);
v4_mesh_set_tick(&ms, 109); /* three ticks: 65535 >> 16 = 0, as v3 */
set_tick(109); /* three ticks: 65535 >> 16 = 0, as v3 */
r = line(V4_MESH_HERA, "2 P7 .", &how);
CHECK(a && a->heat == 4, "three ticks decay nothing, as v3's arithmetic: %lu", a ? (unsigned long)a->heat : 0ul);
((v4_mesh_account *)a)->state_flags |= V4_MESH_STATE_FROZEN;
v4_mesh_set_tick(&ms, 409);
set_tick(409);
r = line(V4_MESH_HERA, "2 P7 .", &how);
CHECK(a && a->heat == 5, "a frozen word does not decay: %lu", a ? (unsigned long)a->heat : 0ul);
((v4_mesh_account *)a)->state_flags &= (uint8_t)~V4_MESH_STATE_FROZEN;
p->l3_decay = 0;
v4_mesh_set_tick(&ms, 709);
set_tick(709);
r = line(V4_MESH_HERA, "2 P7 .", &how);
CHECK(a && a->heat == 6, "nor any word while L8 has Loop #3 off: %lu", a ? (unsigned long)a->heat : 0ul);
p->l3_decay = 1;
v4_mesh_set_tick(&ms, 1009);
set_tick(1009);
r = line(V4_MESH_HERA, "2 P7 .", &how);
CHECK(a && a->heat == 1, "and with it on, 300 ticks take all six, then one: %lu", a ? (unsigned long)a->heat : 0ul);
v4_mesh_set_tick(&ms, 0);
p->cycle_every = every;
set_tick(0);
{
uint64_t t0 = p->tick;
heart += 2u; /* the heartbeat ticks twice while the line runs */
r = line(V4_MESH_HERA, ": LP7 300 0 DO 2 P7 DROP LOOP ; LP7", &how);
CHECK(p->tick >= t0 + 2,
"300 calls of P7, and the many the line's compiling makes: the tick moves, a cycle per 256 (%lu)",
CHECK(p->tick >= t0 + 2 && p->tick == ms.tick,
"300 calls of P7 while the heartbeat ticks twice: Hera's tick is the heartbeat's (%lu)",
(unsigned long)(p->tick - t0));
}
/* the cycle's loops (Task 3): background decay, 64 words a cycle from a cursor down the dictionary,
@@ -458,14 +473,16 @@ int main(void)
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 300 0 DO Q7 LOOP ; LQ7 ' Q7 .", &how); /* over 256: a cycle at least */
heart += 1u; /* a tick of the heartbeat, at least, while it runs */
r = line(V4_MESH_HERA, ": Q7 ; : LQ7 300 0 DO Q7 LOOP ; LQ7 ' Q7 .", &how);
q7 = (v4_cell)strtol(r, 0, 10);
qa = v4_mesh_account_at(&ms, 0, q7);
#define Q7_HOT 90u /* under card 24 (no L4): no speculation to set its heat to 51 */
CHECK(qa && qa->heat >= Q7_HOT && p->decay_cycles > c0, "Q7 hot after 300 calls (%lu), and the cycles decay in the background",
qa ? (unsigned long)qa->heat : 0ul);
q7_heat = qa ? qa->heat : 0;
r = line(V4_MESH_HERA, ": NOP ; : LZ 20000 0 DO NOP LOOP ; LZ", &how); /* many cycles of calls, none to Q7 */
heart += 300u; /* many ticks of the heartbeat, no call to Q7 */
r = line(V4_MESH_HERA, ": NOP ; : LZ 20000 0 DO NOP LOOP ; LZ", &how);
CHECK(qa && qa->heat < q7_heat && qa->last_decay_tick > 0, "Q7, never called again, has cooled in the background: %lu",
qa ? (unsigned long)qa->heat : 0ul);
CHECK(p->win.diversity_checks > 0 && p->win.effective_window_size >= 256u && p->win.effective_window_size <= V4_MESH_WINDOW,