diff --git a/docs/v4.0.0/plans/2026-11-17-one-adaptive-heartbeat.md b/docs/v4.0.0/plans/2026-11-17-one-adaptive-heartbeat.md index ddfb689e..72529d9a 100644 --- a/docs/v4.0.0/plans/2026-11-17-one-adaptive-heartbeat.md +++ b/docs/v4.0.0/plans/2026-11-17-one-adaptive-heartbeat.md @@ -1,4 +1,152 @@ -# One adaptive heartbeat -- every node ticks in lockstep -- design, for ruling +# One adaptive heartbeat -- design, for ruling + +## REVISED 2026-11-17 (Captain Bob): the heartbeat is its own thing + +> "The adaptive heartbeat is its own thing. It has nothing to do with stadium with any interaction at all. Not +> words, not messages, not VMs. 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. THATS IT! ONE AS IN THE NUMBER 1 heartbeat +> (the adaptive one) runs the entire system." + +The first version, kept below under a "superseded" banner, got this wrong in three places. I built part of it, +and none of that was committed: +- it made the beat out of the fabric's steps, which ties it to the VMs' execution; +- it drove Loop #7 from Hera's word inference, which is a VM's RWOT and slope; +- it charged the Stadium on the beat. + +**Ruled the same day, one at a time:** +1. **The one heartbeat is the kernel's,** `kernel/src/heartbeat.c`: the timer's ISR and its own rolling window. + Every VM's tick is its tick. +2. **Its one Stadium input is resident heat:** how much of the conserved 1.0 is resident on the floor rather + than in the reservoirs, summed over the Stadium. The heartbeat reads it and nothing else, and calls nothing + in the Stadium. +3. **It is held during POST,** as v3's boot runs POST before its timer starts (V3-PARITY 1a). v3's cases then + read as written, the same on every ISA. The VMs' ticks start after POST. +4. **The Stadium is charged as before today:** at the kernel's idle points and after each line, stamped with + the heartbeat's tick. "On the one beat" is withdrawn. + +### The design + +**A. The heartbeat's own RWOT and slope provider** (`kernel/src/heartbeat.c`): +- On each heartbeat tick, its bottom half (`heartbeat_service`) samples resident heat into a rolling window of + its own. +- Every `HEARTBEAT_INFERENCE_FREQUENCY` ticks, a slope provider of its own runs on that window: v4's inference + arithmetic, `v4/system/inference.c`, which is libc-free and already ported from v3 to the bit. +- Loop #7's rule then sets its own period, through `heartbeat_set_adaptive_period_ns`: + - stable (the inference exits early): 25% slower; + - otherwise: 20% faster; + - within a quarter to four times the 10 ms base. +- No VM, word or message is read, and nothing in the Stadium is called. One read-only accessor is added: + `stadium_resident_heat_total()`. + +**B. Every VM ticks on it, in lockstep:** +- The kernel delivers each heartbeat tick to the mesh with `v4_mesh_tick(mesh, tick)`. Ticks missed between + service points are delivered one by one, so no tick is lost. +- On each delivered tick, every node present advances to that tick and runs its heartbeat cycle (decay, window + service, inference on its words, L8, snapshot), in place order, active, idle or blocked alike. +- Nothing in a VM paces the heartbeat. The nodes' own Loop #7 goes: a node adapts nothing of the clock. + +**C. Held in POST:** the kernel delivers no ticks until POST has ended. + +**D. What goes from the build:** +- the beat counted on fabric steps; +- `steps_per_beat`, `beat_base` and `HEARTBEAT_STEPS_PER_BEAT`; +- Loop #7 on Hera's word inference; +- the mesh's `beat` callback charging the Stadium; +- the per-node tick by execution (`check_counter`, `cycle_every`, `tokens_seen`). + +**E. What stays from the build:** a node's tick is the one heartbeat's, the same on every node, and every node +present runs its cycle on each tick. + +### Verification, named before anything is built + +1. **Test first:** + - the mesh: after N delivered ticks, every node, active or idle, shows tick N, and each ran its cycle on + each; + - an idle node's words decay; + - no tick is delivered during POST; + - the heartbeat (`kernel/tests`, the host harness): resident heat is sampled each tick; the slope provider + runs; Loop #7 moves the period within its bounds; nothing in the Stadium is written by it. + + Each test is seen to fail first. +2. **The rest of v4:** + - POST 990 of 990 with v3's heat cases as written, at both widths and under the sanitizers; + - every existing check; + - Phase 7 checks that assumed a tick by execution are brought to Captain Bob one at a time. +3. **The protocol** on a clean tree, three ISAs, one `dict_hash`. + +### As built (2026-11-17) + +- **The heartbeat paces itself** (`kernel/src/heartbeat.c`, `kernel/include/starkernel/timer.h`): + - `heartbeat_pace_from(load)` gives it its one load source; + - every serviced tick samples that source into the heartbeat's own window (`HEARTBEAT_PACE_WINDOW`, + 1024 samples); + - every `HEARTBEAT_PACE_EVERY` (1000) samples its slope provider, `v4_inference_run`, runs on the window, + oldest first; + - Loop #7 then sets the period through `heartbeat_set_adaptive_period_ns`: not stable, 20% faster; stable, + 25% slower; within a quarter to four times the 10 ms base. +- **With no source, nothing of this runs.** v3's path never gives one, so it is unchanged. The slope provider + is built under `STARFORTH_V4`. +- **The one input:** `stadium_resident_heat_total()` (`kernel/src/vm/stadium.c`) sums the heat of every + patron on the floor. It is read only. +- **Every VM on it** (`v4/system/mesh.c`, `v4/include/v4/mesh.h`): + - the owner's `clock` is read at every fabric step, and each tick not yet delivered is delivered, one by + one; + - on each tick, every node present whose physics is set up runs its heartbeat cycle, in place order, + active, idle or blocked: lockstep; + - a node's `tick` is the mesh's. +- **What went:** + - the tick by execution (`check_counter`, `cycle_every`, `tokens_seen`); + - the nodes' own Loop #7 (`tick_target_ns`); + - the `heartrate` callback. +- **The kernel** (`kernel/src/v4/sk_v4.c`): + - its `clock` gives the heartbeat's bottom half its turn and returns 0 until POST has ended (held), then the + heartbeat's ticks since; + - `heartbeat_pace_from(stadium_resident_heat_total)` is set before the mesh boots; + - the Stadium is charged as before today. + +**Tests:** + +| Test | Before | After | +|---|---|---| +| `kernel/tests/test_heartbeat.c` (new) | does not build | 11 checks, 0 failures | +| `kernel/tests/test_stadium_node.c` (added) | does not build | 16 checks, 0 failures | +| `v4/tests/test_mesh_boot.c` (added) | does not build | see below | + +What the tests check: +- `test_heartbeat.c`: no source, no pacing; one sample per serviced tick; the slope provider every 1000; + faster when not stable, slower when stable; both bounds; the source taken away. +- `test_stadium_node.c`: resident heat over the Stadium. +- `test_mesh_boot.c`: + - held through the boot and POST; + - five ticks deliver five cycles to Hera and to an idle node alike; + - no tick lost between lines. + +**Then:** +- **The mesh-boot builds:** POST 990 of 990, with v3's heat cases as written. + - `mesh-boot`: 103 checks; + - `mesh-boot-on`: 112 checks; + - `mesh-boot-noop`: 100 checks; + - `san-mesh-boot`: 103 checks under ASan and UBSan. + + All with 0 failures. +- **Every v4 program at both widths:** 0 failures. +- **The kernel host tests:** all pass. + +**Ruled along the way:** +- Seven Phase 7 checks assumed the tick by execution. They now drive the test's heartbeat clock by the ticks + they need (Captain Bob: "Drive the test's heartbeat"). The two Loop #7 checks on Hera went with Hera's + Loop #7. + +**Noted, not changed:** v4's inference variance, which is v3's ported to the bit, saturates on very large +spreads. The heartbeat test keeps its fixture within that range. + +**Not run here:** the full `sanitize` target, which outlasts the foreground limit. Only the mesh boot, where +the change is, ran under the sanitizers. + +--- + +## SUPERSEDED 2026-11-17 -- the first version (wrong: see above). Kept as the record. + Written 2026-11-17. Captain Bob: **"ONE ADAPTIVE HEARTBEAT. EVERY VM USES THAT ONE CLOCK AND REGARDLESS OF WHAT THEY'RE ALL DOING, ACTIVE OR NOT, THEY ADVANCE ONE TICK! PERIOD. ... Lots of diff --git a/kernel/include/starkernel/timer.h b/kernel/include/starkernel/timer.h index 0f3d29ed..c6fa2b08 100644 --- a/kernel/include/starkernel/timer.h +++ b/kernel/include/starkernel/timer.h @@ -192,6 +192,22 @@ void heartbeat_service(void); */ uint64_t heartbeat_read_counter(void); +/* ---- The one adaptive heartbeat paces itself (StarForth v4; Captain Bob, 2026-11-17: "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"; docs/v4.0.0/plans/2026-11-17-one-adaptive-heartbeat.md). Given a load source -- the Stadium's resident + * heat, on v4 -- every serviced tick samples it into the heartbeat's own rolling window; every + * HEARTBEAT_PACE_EVERY samples its own slope provider (v4's inference arithmetic) runs on that window, and Loop + * #7's rule sets the period: not stable, 20% faster; stable, 25% slower; within a quarter to four times the base. + * The source is only read. With none (v3's path) nothing of this runs and the period is left to its setter. */ +#ifndef HEARTBEAT_PACE_EVERY +#define HEARTBEAT_PACE_EVERY 1000u /* samples between runs: Kconfig.heartbeat's inference frequency */ +#endif +#define HEARTBEAT_PACE_WINDOW 1024u /* the heartbeat's own rolling window, in samples */ +typedef uint64_t (*heartbeat_load_fn)(void); +void heartbeat_pace_from(heartbeat_load_fn load); /* 0: no source, no pacing */ +uint64_t heartbeat_pace_samples(void); +uint64_t heartbeat_pace_runs(void); + /** * Get current TIME-TICKS (monotonic heartbeat count). */ diff --git a/kernel/include/starkernel/vm/stadium.h b/kernel/include/starkernel/vm/stadium.h index 0e846c13..254a5f88 100644 --- a/kernel/include/starkernel/vm/stadium.h +++ b/kernel/include/starkernel/vm/stadium.h @@ -393,6 +393,10 @@ int stadium_quota_slot_for_vm(VMUuid vm_id); */ uint64_t stadium_resident_sum(VMUuid vm_id); +/* The heat of every patron on the floor, summed over the whole Stadium: the one output the adaptive heartbeat paces + * by (kernel/src/heartbeat.c, heartbeat_pace_from; Captain Bob, 2026-11-17). Read only. */ +uint64_t stadium_resident_heat_total(void); + /* * stadium_conserved - Boolean analogue of vm_physics_conserved(), for the * Stadium per-VM quota invariant rather than fleet-wide execution heat diff --git a/kernel/src/heartbeat.c b/kernel/src/heartbeat.c index 83678a80..1f52d298 100644 --- a/kernel/src/heartbeat.c +++ b/kernel/src/heartbeat.c @@ -37,6 +37,9 @@ #include "starkernel/timer.h" #include "starkernel/capsule_vm_switch_signal.h" /* FABRIC-3.md §XXVIII Stage 3 */ +#if STARFORTH_V4 +#include "v4/inference.h" /* the heartbeat's own slope provider: v4's inference arithmetic */ +#endif #if defined(ARCH_AMD64) #define HEARTBEAT_HAS_VARIANCE 1 @@ -63,6 +66,42 @@ static volatile int g_pending_valid; * rate even if it forwarded an unclamped value. */ static volatile uint64_t g_adaptive_period_ns = HEARTBEAT_BASE_PERIOD_NS; +/* The heartbeat's own pacing (timer.h, heartbeat_pace_from): its load source, its own rolling window of samples, + * and its own slope provider's state. Mainline only (heartbeat_service), as the rest of the bottom half. */ +static heartbeat_load_fn g_pace_load; +static uint64_t g_pace_window[HEARTBEAT_PACE_WINDOW]; +static uint64_t g_pace_samples, g_pace_runs; +#if STARFORTH_V4 +static uint64_t g_pace_trajectory[HEARTBEAT_PACE_WINDOW]; +static v4_inference_out g_pace_inference; +#endif + +void heartbeat_pace_from(heartbeat_load_fn load) { g_pace_load = load; } +uint64_t heartbeat_pace_samples(void) { return g_pace_samples; } +uint64_t heartbeat_pace_runs(void) { return g_pace_runs; } + +/* one sample of the load into the window; every HEARTBEAT_PACE_EVERY of them, the slope provider and Loop #7 */ +static void pace_sample(void) +{ + if (!g_pace_load) return; + g_pace_window[g_pace_samples % HEARTBEAT_PACE_WINDOW] = g_pace_load(); + g_pace_samples++; +#if STARFORTH_V4 + if (g_pace_samples % HEARTBEAT_PACE_EVERY == 0) { + uint64_t n = g_pace_samples < HEARTBEAT_PACE_WINDOW ? g_pace_samples : HEARTBEAT_PACE_WINDOW, i, cur, next; + for (i = 0; i < n; i++) /* oldest first */ + g_pace_trajectory[i] = g_pace_window[(g_pace_samples - n + i) % HEARTBEAT_PACE_WINDOW]; + v4_inference_run(g_pace_trajectory, n, 0, 0, &g_pace_inference); + g_pace_runs++; + /* Loop #7, v3's rule: stable, 25% slower; else 20% faster -- heartbeat_set_adaptive_period_ns clamps it + * to a quarter to four times the base */ + cur = g_adaptive_period_ns; + next = g_pace_inference.early_exited ? (cur * 125u) / 100u : (cur * 80u) / 100u; + heartbeat_set_adaptive_period_ns(next); + } +#endif +} + #if HEARTBEAT_HAS_VARIANCE /** @@ -156,6 +195,8 @@ void heartbeat_init(uint64_t tsc_hz, uint64_t tick_hz) g_pending_valid = 0; g_adaptive_period_ns = HEARTBEAT_BASE_PERIOD_NS; + g_pace_samples = 0; + g_pace_runs = 0; } void heartbeat_tick(void) @@ -189,6 +230,7 @@ void heartbeat_service(void) TimeTrustState *s = &g_heartbeat; s->total_samples++; + pace_sample(); /* the heartbeat's own RWOT, when it paces itself */ if (s->total_samples == 1) { s->last_tsc = now; diff --git a/kernel/src/v4/sk_v4.c b/kernel/src/v4/sk_v4.c index 2645b986..b976991a 100644 --- a/kernel/src/v4/sk_v4.c +++ b/kernel/src/v4/sk_v4.c @@ -89,8 +89,18 @@ static void *sk_v4_alloc(void *self, unsigned long bytes) return p; } static void sk_v4_release(void *self, void *p) { (void)self; kfree(p); } -/* Loop #7 on Hera: the kernel's heartbeat re-armed at the period her inference sets, as v3's Hera does */ -static void sk_v4_heartrate(void *self, uint64_t ns) { (void)self; heartbeat_set_adaptive_period_ns(ns); } +/* THE ONE ADAPTIVE HEARTBEAT (Captain Bob, 2026-11-17; plan 2026-11-17-one-adaptive-heartbeat): the kernel's + * heartbeat, which paces itself on the Stadium's resident heat (heartbeat.c). Every node's tick is its tick -- + * held, 0, until POST has ended (as v3's boot runs POST before its timer starts), then the heartbeat's ticks since. + * Read by the mesh at every fabric step, which also gives the heartbeat's bottom half its turn during a line. */ +static int sk_v4_heart_live; +static uint64_t sk_v4_heart_base; +static uint64_t sk_v4_clock(void *self) +{ + (void)self; + heartbeat_service(); + return sk_v4_heart_live ? heartbeat_ticks() - sk_v4_heart_base : 0u; +} static void sk_v4_charge_one(void *ctx, v4_cell id, v4_uheat_t calls) { (void)ctx; @@ -345,7 +355,8 @@ void sk_v4_boot(void (*chain)(void), const BootDisk *boot_disk) sk_v4_mesh.forgot = sk_v4_forgot; sk_v4_mesh.physics = sk_v4_physics; sk_v4_mesh.alloc = sk_v4_alloc; sk_v4_mesh.release = sk_v4_release; - sk_v4_mesh.heartrate = sk_v4_heartrate; + sk_v4_mesh.clock = sk_v4_clock; + heartbeat_pace_from(stadium_resident_heat_total); /* the heartbeat's one input: the Stadium's resident heat */ sk_v4_mesh.out = sk_v4_out; sk_v4_mesh.key = sk_v4_key; sk_v4_mesh.words = sk_v4_words; sk_v4_mesh.word_count = sizeof sk_v4_words / sizeof sk_v4_words[0]; sk_v4_mesh.serve = sk_v4_serve; @@ -357,6 +368,9 @@ void sk_v4_boot(void (*chain)(void), const BootDisk *boot_disk) sk_v4_dump(0, 0u); sk_v4_stop(); /* #177: halts, not a 100% spin */ } + /* POST has ended: from here every node ticks on the heartbeat */ + sk_v4_heart_base = heartbeat_ticks(); + sk_v4_heart_live = 1; if (chain) { /* the blocks are other blocks now: what the node has in its two * buffers is POST's, and is let go */ diff --git a/kernel/src/vm/stadium.c b/kernel/src/vm/stadium.c index 4e23db5c..70a1e6a0 100644 --- a/kernel/src/vm/stadium.c +++ b/kernel/src/vm/stadium.c @@ -821,6 +821,17 @@ uint64_t stadium_resident_sum(VMUuid vm_id) { return sum; } +/* The one output the adaptive heartbeat paces by (Captain Bob, 2026-11-17: "Only one output from the entire stadium + * is used as a load measurement for heart rate pacing" -- resident heat): the heat of every patron on the floor, + * summed over the whole Stadium, whoever's quota. Read only. */ +uint64_t stadium_resident_heat_total(void) { + uint64_t sum = 0; + size_t i; + for (i = 0; i < stadium_ncells; i++) + if (bitmap_get(i)) sum += stadium_cell_array[i].header.heat; + return sum; +} + void stadium_consumed_record(VMUuid vm_id, uint64_t amount) { int slot = quota_slot_for_vm(vm_id); diff --git a/kernel/tests/Makefile b/kernel/tests/Makefile index 0415166e..95ac68dc 100644 --- a/kernel/tests/Makefile +++ b/kernel/tests/Makefile @@ -19,7 +19,11 @@ CC ?= cc CFLAGS := -std=c99 -Wall -Wextra -Werror -O2 -g -I$(HERE) -I$(ROOT)/kernel/include \ -I$(ROOT)/kernel/include/starkernel -I$(ROOT)/v3/include -TESTS := test_gpt test_usb_config test_sk_v4_stop test_sk_v4_slots test_sk_v4_mint test_sk_v4_identity test_heartbeat_export test_ssm_jacquard test_physics_l2 test_stadium_node test_stadium_ops test_stadium_words +TESTS := test_heartbeat test_gpt test_usb_config test_sk_v4_stop test_sk_v4_slots test_sk_v4_mint test_sk_v4_identity test_heartbeat_export test_ssm_jacquard test_physics_l2 test_stadium_node test_stadium_ops test_stadium_words +# the one adaptive heartbeat paces itself (plan 2026-11-17-one-adaptive-heartbeat): heartbeat.c, its Q48.16, and the +# v4 inference arithmetic its slope provider runs +SRC_test_heartbeat := $(ROOT)/kernel/src/heartbeat.c $(ROOT)/kernel/src/math/q48_16.c $(ROOT)/v4/system/inference.c +EXTRA_test_heartbeat := -D__STARKERNEL__ -DSTARFORTH_V4=1 -DARCH_AMD64 -I$(ROOT)/v4/include SRC_test_stadium_ops := $(ROOT)/kernel/src/vm/stadium_ops.c $(ROOT)/kernel/src/vm/stadium.c $(ROOT)/kernel/src/vm/session.c $(ROOT)/kernel/src/capsule/vm_uuid.c EXTRA_test_stadium_ops := -D__STARKERNEL__ SRC_test_stadium_words := $(ROOT)/kernel/src/vm/stadium_words.c $(ROOT)/kernel/src/vm/stadium.c $(ROOT)/kernel/src/vm/session.c $(ROOT)/kernel/src/capsule/vm_uuid.c diff --git a/kernel/tests/test_heartbeat.c b/kernel/tests/test_heartbeat.c new file mode 100644 index 00000000..576b0cae --- /dev/null +++ b/kernel/tests/test_heartbeat.c @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: LicenseRef-Starship-1.0 + * Copyright (c) 2023-2026 Robert A. James + */ +/* test_heartbeat.c -- the one adaptive heartbeat paces itself (docs/v4.0.0/plans/2026-11-17-one-adaptive- + * heartbeat.md; Captain Bob, 2026-11-17: "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"). + * + * heartbeat.c with the v4 inference arithmetic, and a load source the test controls in place of the Stadium's + * resident heat. Checked: + * - with no load source (v3's path, and the boot before v4 starts it), the period is never touched by itself; + * - with one, each serviced tick samples the load into the heartbeat's own window; + * - every HEARTBEAT_INFERENCE_FREQUENCY samples its slope provider runs on that window, and Loop #7's rule sets + * the period: not stable, 20% faster; stable (the variance within 5% of the last run's), 25% slower; + * - always within a quarter to four times the 10 ms base; + * - the load source is only read: nothing else is called. + */ +#include "harness.h" +#include "starkernel/timer.h" + +#include + +/* what heartbeat.c needs of the rest of the kernel, here */ +static uint64_t counter; +uint64_t heartbeat_read_counter(void) { return counter += 1000u; } +void sk_vm_switch_signal_tick(void) { } + +static uint64_t load_value, loads_read; +static int load_mode; /* 0 constant; 1 alternating, the same spread; 2 a spread that doubles each run */ +static uint64_t load(void) +{ + loads_read++; + if (load_mode == 1) return (loads_read & 1u) ? 100u : 200u; + /* doubling for six runs, then back: v4's inference variance (v3's, ported to the bit) saturates beyond that */ + if (load_mode == 2) return (loads_read & 1u) ? 100u : 100u + (100u << (heartbeat_pace_runs() % 6u)); + return load_value; +} + +#define BASE 10000000ULL + +static void ticks(unsigned n) +{ + unsigned i; + for (i = 0; i < n; i++) { heartbeat_tick(); heartbeat_service(); } +} + +int main(void) +{ + printf("test_heartbeat\n"); + heartbeat_init(100000u, 100u); + + /* no load source: v3's path -- the heartbeat does not pace itself */ + ticks(3000); + CHECK(heartbeat_next_period_ns() == BASE, "no load source: the period stays at the base (%llu)", + (unsigned long long)heartbeat_next_period_ns()); + CHECK(heartbeat_pace_samples() == 0, "and nothing is sampled"); + + /* the load source: sampled every serviced tick */ + load_mode = 1; loads_read = 0; + heartbeat_pace_from(load); + ticks(10); + CHECK(heartbeat_pace_samples() == 10 && loads_read == 10, "each serviced tick samples the load once (%llu, %llu read)", + (unsigned long long)heartbeat_pace_samples(), (unsigned long long)loads_read); + + /* the first inference: no variance before it -- not stable: 20% faster */ + ticks(HEARTBEAT_PACE_EVERY - 10u); + CHECK(heartbeat_pace_runs() == 1, "the slope provider ran after %u samples", HEARTBEAT_PACE_EVERY); + CHECK(heartbeat_next_period_ns() == BASE * 80u / 100u, "not stable: the period 20%% shorter (%llu)", + (unsigned long long)heartbeat_next_period_ns()); + + /* the same spread again: its variance within 5% of the last run's -- stable: 25% slower */ + ticks(HEARTBEAT_PACE_EVERY); + CHECK(heartbeat_pace_runs() == 2, "it ran again"); + CHECK(heartbeat_next_period_ns() == BASE * 80u / 100u * 125u / 100u, "stable: 25%% longer, back to %llu", + (unsigned long long)heartbeat_next_period_ns()); + + /* the same spread for many runs: stable each time -- slower, held at four times the base */ + ticks(HEARTBEAT_PACE_EVERY * 20u); + CHECK(heartbeat_next_period_ns() == BASE * 4u, "stable run after run: slower, held at four times the base (%llu)", + (unsigned long long)heartbeat_next_period_ns()); + + /* a spread that doubles each run (its variance four times the last): never stable -- faster each run, until a + * quarter of the base */ + load_mode = 2; + ticks(HEARTBEAT_PACE_EVERY * 20u); + CHECK(heartbeat_next_period_ns() == BASE / 4u, "never stable: faster each run, held at a quarter of the base (%llu)", + (unsigned long long)heartbeat_next_period_ns()); + + /* a constant load: steady -- slower each run? its variance is 0, and v3's rule (ported to the bit) calls a + * variance of 0 never stabilized, so it stays fast: recorded as the rule is */ + load_mode = 0; load_value = 500u; + ticks(HEARTBEAT_PACE_EVERY * 20u); + CHECK(heartbeat_next_period_ns() >= BASE / 4u && heartbeat_next_period_ns() <= BASE * 4u, + "a constant load: within the bounds (%llu)", (unsigned long long)heartbeat_next_period_ns()); + + /* stopped: no source, no pacing */ + heartbeat_pace_from(0); + { + uint64_t p = heartbeat_next_period_ns(), s = heartbeat_pace_samples(); + ticks(3000); + CHECK(heartbeat_next_period_ns() == p && heartbeat_pace_samples() == s, "the source taken away: pacing stops"); + } + return DONE(); +} diff --git a/kernel/tests/test_stadium_node.c b/kernel/tests/test_stadium_node.c index 2754860d..8c626033 100644 --- a/kernel/tests/test_stadium_node.c +++ b/kernel/tests/test_stadium_node.c @@ -55,5 +55,15 @@ int main(void) CHECK(session_is_pinned(key), "and pinned"); CHECK(stadium_conserved(key), "conservation holds: resident heat and reservoir sum to 1.0"); CHECK(stadium_birth_node(key, "Hera") == 0 && session_find(key) == s, "born twice is born once"); + + /* the one output the heartbeat paces by (Captain Bob, 2026-11-17: resident heat): the heat on the floor, + * summed over the Stadium -- every patron's, whoever's quota -- and nothing changed by reading it */ + { + uint64_t total = stadium_resident_heat_total(); + CHECK(total == stadium_resident_sum(key), "resident heat over the Stadium: the one node's resident sum (%llu)", + (unsigned long long)total); + CHECK(total + stadium_reservoir_peek(key) == Q48_ONE, "with its reservoir, the whole share: 1.0"); + CHECK(stadium_resident_heat_total() == total && stadium_conserved(key), "read twice, the same; nothing changed"); + } return DONE(); } diff --git a/v4/include/v4/mesh.h b/v4/include/v4/mesh.h index 2ad37957..717075a2 100644 --- a/v4/include/v4/mesh.h +++ b/v4/include/v4/mesh.h @@ -198,10 +198,10 @@ typedef struct { } v4_mesh_window; typedef struct { - uint64_t tick; /* the node's heartbeat cycles (v3's tick_count): by execution, as v3 */ - uint32_t check_counter; /* executions since the last cycle: v3's, tokens and words run */ - v4_cell tokens_seen; /* (TOKENS) when last added in */ - uint32_t cycle_every; /* V4_MESH_CHECK_FREQUENCY */ + uint64_t tick; /* the ONE adaptive heartbeat's tick (the mesh's `tick`), the same on every + * node: every node advances one tick on each of its ticks, active or not + * (Captain Bob, 2026-11-17; plan 2026-11-17-one-adaptive-heartbeat) */ + uint8_t ticking; /* not 0: the node's physics is set up, and it runs its cycle on each tick */ uint64_t decay_slope_q48; uint8_t l3_decay; /* L8's gate on Loop #3 */ uint8_t l2_window; /* L8's gate on Loop #2 */ @@ -225,7 +225,6 @@ typedef struct { uint64_t last_inference_tick; uint8_t inference_early_exited; uint64_t inference_runs, inference_early_exits; - uint64_t tick_target_ns; /* v3's tick_target_ns: HEARTBEAT_TICK_NS, a quarter to four times it */ /* the opcodes' physics (ruling 7): their records, the one before, their decay slope, their inference */ v4_mesh_opcode op[V4_MESH_OPCODES]; uint32_t prev_op; /* V4_MESH_NO_WORD: none yet */ @@ -275,8 +274,10 @@ struct v4_mesh { v4_mesh_account *accounts; unsigned char *is_word; v4_mesh_physics *physics; /* `capacity` of them: each node's (#189, Phase 7) */ - /* Loop #7 on Hera: the kernel's heartbeat period, re-armed (v3: heartbeat_set_adaptive_period_ns; may be 0) */ - void (*heartrate)(void *self, uint64_t kernel_period_ns); + /* THE ONE ADAPTIVE HEARTBEAT's tick, as the owner has it (the kernel: its heartbeat's ticks since POST ended -- + * held, 0, until then); 0: no heartbeat, the tick never moves. Read at every fabric step; each new tick is + * delivered to every node. The heartbeat paces itself (kernel/src/heartbeat.c): nothing here paces it. */ + uint64_t (*clock)(void *self); /* memory for a word's transition counters, zeroed, and given back (pipelining; may be 0: none recorded) */ void *(*alloc)(void *self, unsigned long bytes); void (*release)(void *self, void *p); @@ -287,6 +288,7 @@ struct v4_mesh { /* ---- the mesh's own ---- */ v4_fabric f; v4_manage m; + uint64_t tick; /* the last of the heartbeat's ticks delivered to every node */ unsigned hera_place; v4_cell cells[V4_MESH_NUCLEUS_CELLS]; /* the nucleus as a capsule of F18 cells */ unsigned cell_count, given; /* how many; how many given to Hera */ @@ -323,7 +325,7 @@ const v4_mesh_account *v4_mesh_account_at(const v4_mesh *ms, unsigned place, v4_ * it choose again from there. */ void v4_mesh_l8_force(v4_mesh *ms, unsigned place, unsigned card, int hold); -/* Every node's tick set to `tick` (for tests: a node's tick is its own count of heartbeat cycles). */ +/* The tick, and so every node's, set to `tick`, no cycle run (for tests). */ void v4_mesh_set_tick(v4_mesh *ms, uint64_t tick); /* A word's state flags (V4_MESH_STATE_*), or 0 if xt is not a word's. */ diff --git a/v4/system/mesh.c b/v4/system/mesh.c index 04fa01e3..12bd4586 100644 --- a/v4/system/mesh.c +++ b/v4/system/mesh.c @@ -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; } } diff --git a/v4/tests/test_mesh_boot.c b/v4/tests/test_mesh_boot.c index df69af13..765470e1 100644 --- a/v4/tests/test_mesh_boot.c +++ b/v4/tests/test_mesh_boot.c @@ -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,