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
+37 -34
View File
@@ -641,22 +641,15 @@ static void inference_tick(unsigned place, v4_mesh_physics *p)
#if OPCODE_PHYSICS
opcodes_inference(p);
#endif
/* Loop #7, the adaptive heartrate: stable, 25% slower; else 20% faster -- within 1/4 to 4 times the nominal */
{
uint64_t cur = p->tick_target_ns, n;
if (p->inference.early_exited) { n = (cur * 125u) / 100u; if (n > HEARTBEAT_TICK_NS * 4u) n = HEARTBEAT_TICK_NS * 4u; }
else { n = (cur * 80u) / 100u; if (n < HEARTBEAT_TICK_NS / 4u) n = HEARTBEAT_TICK_NS / 4u; }
p->tick_target_ns = n;
if (place == the->hera_place && the->heartrate) /* Hera's re-arms the kernel's (10 ms base) */
the->heartrate(the->self, (p->tick_target_ns * 10000000ULL) / HEARTBEAT_TICK_NS);
}
/* No Loop #7 here: the one adaptive heartbeat paces itself, on its own RWOT and slope provider, from the
* Stadium's resident heat alone (kernel/src/heartbeat.c; Captain Bob, 2026-11-17). A node paces nothing. */
}
/* a node's heartbeat cycle, in v3's order (vm_heartbeat_run_cycle): its tick (and inference, every 1000);
* background decay; the window's service; L8's update; the snapshot */
static void heartbeat_cycle(unsigned place, v4_mesh_physics *p)
{
p->tick++;
p->tick = the->tick; /* the one heartbeat's: every node the same */
if (p->tick - p->last_inference_tick >= V4_MESH_INFERENCE_EVERY) inference_tick(place, p);
background_decay(place, p);
window_service(&p->win);
@@ -684,14 +677,6 @@ static void on_call(void *ctx, v4_node *n, v4_cell target)
v4_mesh_account *a;
const v4_image *im = the->im;
int from_code, dispatched;
if (the->physics) { /* the tokens INTERPRET has taken since: each counts, as v3's */
v4_mesh_physics *pp = &the->physics[place];
v4_cell t = n->mem[im->tokens];
if (t != pp->tokens_seen) {
pp->check_counter += (uint32_t)(t - pp->tokens_seen);
pp->tokens_seen = t;
}
}
/* a word run, as v3 counts it: called from code defined at run time, or by INTERPRET or EXECUTE -- not the
* system's own calls among its words (v3's are C) */
from_code = (ret >= im->dict_lo && ret < im->dict_hi) || ret == im->exec + 1;
@@ -717,14 +702,7 @@ static void on_call(void *ctx, v4_node *n, v4_cell target)
if (p->l1_cache && a->heat > V4_MESH_HOT_HEAT && !hot_has(place, target)) promote(place, target);
#endif
p->prev_id = (uint32_t)id;
/* the heartbeat, by execution (v3: every HEARTBEAT_CHECK_FREQUENCY executions -- its interpreter's tokens and
* the words its definitions run; Captain Bob, 2026-11-13: "By execution, as v3"); a word INTERPRET dispatched
* was counted as its token */
if (from_code) p->check_counter++;
while (p->check_counter >= p->cycle_every) {
p->check_counter -= p->cycle_every;
heartbeat_cycle(place, p);
}
/* the tick is not counted here: it is the one heartbeat's (Captain Bob, 2026-11-17) */
}
a->calls++;
a->since++;
@@ -734,6 +712,7 @@ void v4_mesh_set_tick(v4_mesh *ms, uint64_t tick)
{
unsigned k;
if (!ms->physics) return;
ms->tick = tick;
for (k = 0; k < ms->capacity; k++) ms->physics[k].tick = tick;
}
@@ -774,13 +753,12 @@ static void words_attach(unsigned place, v4_fabric_node *x)
}
if (the->physics) { /* the node's physics, as v3's VM starts */
v4_mesh_physics *p = &the->physics[place];
uint64_t tick = 0; /* its own count of cycles, from none */
uint64_t tick = the->tick; /* the one heartbeat's, as every other node's */
unsigned char *z = (unsigned char *)p;
unsigned long i;
for (i = 0; i < sizeof *p; i++) z[i] = 0;
p->tick = tick;
p->cycle_every = V4_MESH_CHECK_FREQUENCY;
p->tokens_seen = x->n.mem[the->im->tokens];
p->ticking = 1; /* from now it runs its cycle on every tick */
p->decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48;
p->win.effective_window_size = V4_MESH_WINDOW;
p->opwin.effective_window_size = V4_MESH_WINDOW;
@@ -791,7 +769,6 @@ static void words_attach(unsigned place, v4_fabric_node *x)
* applies its card */
l8_seed(&p->l8);
p->last_inference_tick = 0; /* as v3's */
p->tick_target_ns = HEARTBEAT_TICK_NS;
}
x->es.on_call = on_call;
x->es.on_call_ctx = &the->kernel[place];
@@ -1074,6 +1051,7 @@ static void gone_cb(void *self, unsigned place, v4_fabric_node *was)
{
(void)self;
if (place < MAX_PLACES) { the->reading_nucleus[place] = 0; reads[place].p = reads[place].end = 0; }
if (the->physics && place < the->capacity) the->physics[place].ticking = 0; /* it marches no more */
if (the->accounts && place < the->capacity) { /* its words' records go with it */
unsigned k;
for (k = 0; k < WORD_BITS_BYTES; k++) bits_of(place)[k] = 0;
@@ -1137,12 +1115,36 @@ static int takes(v4_mesh *ms, v4_cell *ended_from, v4_cell *ended_how, unsigned
return took;
}
/* ONE ADAPTIVE HEARTBEAT (Captain Bob, 2026-11-17). At every fabric step the owner's clock is read; each tick
* of the heartbeat not yet delivered is delivered, one by one: every node present whose physics is set up runs its
* heartbeat cycle on it, in place order -- active, idle or blocked alike: lockstep. */
static void heart(v4_mesh *ms)
{
uint64_t t;
unsigned k;
if (!ms->clock) return;
t = ms->clock(ms->self);
while (ms->tick < t) {
ms->tick++;
if (ms->physics)
for (k = 0; k < ms->capacity; k++)
if (v4_fabric_node_at(&ms->f, k) && ms->physics[k].ticking) heartbeat_cycle(k, &ms->physics[k]);
}
}
static unsigned fabric_step(v4_mesh *ms)
{
unsigned did = v4_fabric_step(&ms->f);
heart(ms);
return did;
}
unsigned v4_mesh_step(v4_mesh *ms)
{
v4_cell f = -1, h = -1;
unsigned e = 0, did;
the = ms;
did = v4_fabric_step(&ms->f);
did = fabric_step(ms);
(void)takes(ms, &f, &h, &e);
return did;
}
@@ -1163,7 +1165,7 @@ int v4_mesh_tell(v4_mesh *ms, v4_cell node, const char *text, unsigned len)
if (v4_fabric_device_put(&ms->f, ms->hera_place, V4_MESH_CONSOLE, msg.word, msg.word + V4_MSG_HEADER) != V4_WIRE_DONE)
return V4_BOOT_LINE_REFUSED;
while (steps++ < STEP_LIMIT) {
unsigned did = v4_fabric_step(&ms->f);
unsigned did = fabric_step(ms);
int took = takes(ms, &ended_from, &ended_how, &ended);
int running;
if (h && h->n.stopped) return V4_BOOT_LINE_STOPPED;
@@ -1265,7 +1267,8 @@ static int boot(v4_mesh *ms)
(void)v4_capsule_write(&ms->nucleus->n, ms->im->idle, put_cell, ms);
if (ms->cell_count == 0 || ms->cell_count >= V4_MESH_NUCLEUS_CELLS) { say("V4: the nucleus did not fit\n"); return 0; }
/* 2. the fabric, and Hera born empty at place 0: the kernel on port 0, the console on port 1 */
/* 2. the fabric, and Hera born empty at place 0: the kernel on port 0, the console on port 1; no tick yet */
ms->tick = 0;
v4_fabric_init(&ms->f, ms->places, ms->capacity);
v4_fabric_queues(&ms->f, ms->queues, ms->queue_count);
v4_fabric_gone_error(&ms->f, V4_ERROR_NO_ONE);
@@ -1280,7 +1283,7 @@ static int boot(v4_mesh *ms)
return 0;
}
while (!(h->n.doing && h->n.do_op == 8 && ms->given == ms->cell_count)) { /* she takes it in, and sleeps */
(void)v4_fabric_step(&ms->f);
(void)fabric_step(ms);
if (h->n.stopped || ++steps > STEP_LIMIT) { say("V4: Hera did not take in the nucleus\n"); return 0; }
}