Files
LithosAnanke/v4/tests/test_mesh_boot.c
T
Claude b5d99ba98b feat(v4): Phase 7 Task 3 -- the heartbeat snapshot; the loops complete (#189)
Each cycle publishes v3's HeartbeatSnapshot: the tick, the window's width and fill, the decay slope, and the
dictionary's heat as inference last counted it (words over 50, cooling, the heat in all). One copy, not
v3's two, which are for its heartbeat thread. With it every loop v3 runs before its prompt runs on v4's
word records on every node. Test first: test_mesh_boot.c 73 checks, 81 on. Not pushed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
2026-11-14 00:10:46 +00:00

452 lines
28 KiB
C

/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_mesh_boot.c -- the kernel's boot of the fabric (v4/mesh.h), on the host, with this program as
* its owner, as the kernel will be (#189, ENGINE step 4 Phase 1, Task 5b; Captain Bob, 2026-11-13:
* "Build it to proper spec"). No scheduler: every node steps together.
*
* The boot: the nucleus assembled by the ROM, Hera born empty and given it, FORTH-79, POST, the
* kernel's capsules -- and the same parity lines v4_boot_run prints. Then: a line; NODE-ME; a node
* born (11 2 2 BIRTH), given the nucleus and FORTH-79 by Hera, answering a line of its own; and
* killed, its room given back.
*
* Linked as the hosted product is (v4/Makefile), with mesh.c, manage.c and a mesh node's ROM.
*/
#include "v4/mesh.h"
#include "v4/message.h"
#include "block_subsystem.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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)
#define PLACES 8u
static v4_place places[PLACES];
static v4_wire_queue queues[32];
static v4_manage_entry reg[PLACES];
static v4_blocks blocks[PLACES];
static v4_device kernel[PLACES];
static v4_fabric_node nucleus, hera, pool[PLACES];
static int used[PLACES];
static unsigned roomed, given_back, borns, gones, parities;
static v4_image im;
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 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];
static void charge(void *ctx, v4_cell id, v4_uheat_t calls) { (void)ctx; charged[id] += calls; }
#define TABLES 1024u /* every word with a successor has one, as on the kernel's heap */
static uint64_t tables[TABLES][V4_MESH_WORD_IDS]; /* transition counters, given out in turn */
static int table_used[TABLES];
static void *alloc(void *self, unsigned long bytes)
{
unsigned k; (void)self;
if (bytes > sizeof tables[0]) return 0;
for (k = 0; k < TABLES; k++) if (!table_used[k]) { table_used[k] = 1; memset(tables[k], 0, sizeof tables[k]); return tables[k]; }
return 0;
}
static void release(void *self, void *p) { (void)self; table_used[(uint64_t (*)[V4_MESH_WORD_IDS])p - tables] = 0; }
#if ENABLE_HOTWORDS_CACHE
static int hot_holds(v4_cell xt)
{
v4_fabric_node *h = v4_mesh_node(&ms, V4_MESH_HERA);
v4_cell i;
for (i = 0; i < h->n.mem[im.hot]; i++) if (h->n.mem[im.hot + 3 + i] == xt) return 1;
return 0;
}
#endif
static int no_room; /* the owner has no room to give */
static v4_fabric_node *room(void *self) { unsigned k; (void)self; if (no_room) return 0; for (k = 0; k < PLACES; k++) if (!used[k]) { used[k] = 1; roomed++; return &pool[k]; } return 0; }
static void unroom(void *self, v4_fabric_node *x) { (void)self; used[x - pool] = 0; given_back++; }
static void born(void *self, unsigned place) { (void)self; (void)place; borns++; }
static void gone(void *self, unsigned place) { (void)self; (void)place; gones++; }
static void parity(void *self, unsigned place, v4_cell number, const v4_node *n) { (void)self; (void)place; (void)number; (void)n; parities++; }
static char said[1u << 16];
static unsigned said_len;
static void out(const char *text, unsigned len)
{
if (said_len + len + 1 < sizeof said) { memcpy(said + said_len, text, len); said_len += len; said[said_len] = 0; }
}
static int key(void) { return V4_BOOT_KEY_END; }
static const char *const words[] = { "BYE" };
static unsigned served;
static void serve(v4_node *n, unsigned request) { (void)n; if (request == 1) served++; }
static const char *const capsules[] = { "v4:centre.4th", "v4:hera.4th" };
static unsigned char ram[(size_t)BLK_RAM_BLOCKS * BLK_FORTH_SIZE];
extern void sf_time_init(void);
extern void log_set_level(int level);
/* what a line printed, and how it ended */
static const char *line(v4_cell node, const char *text, int *how)
{
said_len = 0; said[0] = 0;
*how = v4_mesh_tell(&ms, node, text, (unsigned)strlen(text));
return said;
}
int main(void)
{
int how;
const char *r;
printf("v4 mesh boot: V4_CELL_BITS=%d\n", V4_CELL_BITS);
sf_time_init();
log_set_level(-1);
CHECK(blk_subsys_init(ram, sizeof ram) == BLK_OK, "the kernel's blocks");
im = v4_map_image;
ms.places = places; ms.capacity = PLACES; ms.queues = queues; ms.queue_count = sizeof queues / sizeof queues[0];
ms.reg = reg; ms.blocks = blocks; ms.nucleus = &nucleus; ms.hera = &hera; ms.kernel = kernel;
ms.room = room; ms.unroom = unroom; ms.born = born; ms.gone = gone; ms.parity = parity;
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.heartrate = heartrate;
/* ---- the boot ---- */
CHECK(v4_mesh_boot(&ms) == 1, "the boot: %s", said);
CHECK(strstr(said, "PARITY:V4_ROM ") != 0, "the ROM assembled the nucleus, and said so");
CHECK(strstr(said, "PARITY:V4_CAPSULE name=v4:forth79.4th ") && strstr(said, "PARITY:V4_CAPSULE name=v4:centre.4th ")
&& strstr(said, "PARITY:V4_CAPSULE name=v4:hera.4th "), "FORTH-79 and the kernel's capsules, each with its parity line");
CHECK(strstr(said, "PARITY:V4_POST tests=539 pass=539 fail=0") != 0, "POST passes on Hera, born in the fabric");
CHECK(strstr(said, "PARITY:V4_SYSTEM word_count=") && strstr(said, "PARITY:OK\nPOST: PASSED\n"), "the system line, and POST: PASSED");
if (failures) { printf(" %d checks, %d failures\n", checks, failures); return 1; }
/* ---- lines, as messages ---- */
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "7 ") == 0, "a line to Hera: \"%s\" (%d)", r, how);
r = line(V4_MESH_HERA, "NODE-ME .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "0 ") == 0, "NODE-ME: Hera is at place 0: \"%s\"", r);
r = line(V4_MESH_HERA, "NOSUCH", &how);
CHECK(how == V4_TEXT_ERROR && strstr(r, "UNKNOWN WORD") != 0, "an unknown word ends in an error: \"%s\"", r);
r = line(V4_MESH_HERA, "BYE", &how);
CHECK(served == 1, "a kernel word is served by the owner");
/* ---- a birth, and a kill ---- */
r = line(V4_MESH_HERA, "11 2 2 BIRTH .", &how);
CHECK(how == V4_TEXT_COMPLETED && roomed == 1 && borns == 1 && parities == 1,
"11 2 2 BIRTH: a node is born in the owner's room, given the system, and its parity recorded: \"%s\" (%d)", r, how);
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);
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);
r = line(V4_MESH_HERA, "11 KILL", &how);
CHECK(how == V4_TEXT_COMPLETED && v4_mesh_node(&ms, 11) == 0 && given_back == 1 && gones == 1,
"11 KILL: the node is gone, and its room given back: \"%s\" (%d)", r, how);
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "7 ") == 0, "and Hera goes on: \"%s\"", r);
/* ---- the edges (Captain Bob, 2026-11-13: "Check for the edge cases") ---- */
r = line(V4_MESH_HERA, "11 2 2 BIRTH .", &how);
CHECK(how == V4_TEXT_COMPLETED && v4_mesh_node(&ms, 11) != 0, "after a kill, 11 is born again, in the room given back (%d)", how);
r = line(V4_MESH_HERA, "11 3 2 BIRTH .", &how);
CHECK(how == V4_TEXT_ERROR, "a number that is live is not born again: an error (%d) \"%s\"", how, r);
r = line(11, "1 0 /", &how);
CHECK(how == V4_TEXT_ERROR, "a fault on node 11 ends its line in an error (%d) \"%s\"", how, r);
r = line(11, "2 3 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "5 ") == 0, "and node 11 goes on: \"%s\"", r);
r = line(V4_MESH_HERA, "99 KILL", &how);
CHECK(how == V4_TEXT_ERROR && strstr(r, "no such node"), "KILL of a node there is not: an error, \"%s\"", r);
r = line(V4_MESH_HERA, "10 KILL", &how);
CHECK(how == V4_TEXT_ERROR && v4_mesh_node(&ms, 10) != 0, "Hera does not kill herself: \"%s\"", r);
no_room = 1;
r = line(V4_MESH_HERA, "12 3 2 BIRTH .", &how);
CHECK(how == V4_TEXT_ERROR && v4_mesh_node(&ms, 12) == 0, "no room: the birth fails in an error, \"%s\"", r);
no_room = 0;
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "7 ") == 0, "and Hera goes on: \"%s\"", r);
{
char big[V4_MSG_MAX_CHARS + 8];
memset(big, ' ', sizeof big - 1); big[sizeof big - 1] = 0;
r = line(V4_MESH_HERA, big, &how);
CHECK(how == V4_BOOT_LINE_TOO_LONG, "a line too long for a message is not run (%d)", how);
}
/* a node set going for ever does not hold the console: Hera's line ends, and node 11 goes on
* running between lines -- every node steps together, not only while a line runs */
r = line(V4_MESH_HERA, "S\" VARIABLE N1 : SPIN BEGIN 1 N1 +! AGAIN ;\" 11 SEND 11 AWAIT .", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "1 "), "node 11 is given SPIN, which runs for ever (%d) \"%s\"", how, r);
r = line(V4_MESH_HERA, "S\" SPIN\" 11 SEND", &how);
CHECK(how == V4_TEXT_COMPLETED, "Hera's line ends, though node 11 now runs for ever (%d) \"%s\"", how, r);
{
v4_fabric_node *x = v4_mesh_node(&ms, 11);
v4_uheat_t before = x ? x->es.anticlock : 0;
unsigned k;
for (k = 0; k < 1000; k++) v4_mesh_step(&ms); /* the host's idle: the fabric steps on */
CHECK(x && x->es.anticlock > before + 1000u, "between lines node 11 runs on: every node steps together");
}
r = line(V4_MESH_HERA, "11 KILL", &how);
CHECK(how == V4_TEXT_COMPLETED && v4_mesh_node(&ms, 11) == 0, "and a node running for ever is killed (%d) \"%s\"", how, r);
/* a wait no one will answer: the next line breaks it */
r = line(V4_MESH_HERA, "11 2 2 BIRTH DROP 12 AWAIT", &how);
CHECK(how == V4_BOOT_LINE_WAITING, "AWAIT on a node that will never say: the line waits (%d)", how);
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "Interrupted") && strstr(r, "7 ") > strstr(r, "Interrupted"),
"and the next line ends the wait \"Interrupted\", and is then run (MESH.md 7b.6): \"%s\" (%d)", r, how);
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "7 ") == 0 && ms.owed == 0, "and nothing more is owed: \"%s\"", r);
/* ---- L8 (#189 Phase 7, Task 3): v3's Jacquard selector, its 64 cards, applied correctly (#196) ---- */
{
v4_mesh_physics *p = &ms.physics[0];
unsigned k, regimes = 0;
uint8_t b = (uint8_t)(((p->l8.current_card & 0x3Fu) << 1) | 1u);
for (k = 0; k < V4_L8_REGIMES; k++) regimes += p->l8.total_regime_ticks[k] ? 1u : 0u;
CHECK(p->tick > 0 && regimes > 0, "after the boot and POST, L8 has scored its cards over %lu ticks", (unsigned long)p->tick);
CHECK(p->l2_window == ((b & 0x20u) != 0) && p->l3_decay == ((b & 0x10u) != 0)
&& p->l5_window_inference == ((b & 0x04u) != 0) && p->l6_decay_inference == ((b & 0x02u) != 0)
&& p->l7_adaptive_heartrate == 1u, "the gates are the current card's (%u), through card_bits", p->l8.current_card);
CHECK(p->l1_cache == (ENABLE_HOTWORDS_CACHE && (b & 0x40u)) && p->l4_pipelining == (ENABLE_PIPELINING && (b & 0x08u)),
"and L1 and L4 within the build's ceilings");
v4_mesh_l8_force(&ms, 0, 0u, 1);
r = line(V4_MESH_HERA, ": L8T 300 0 DO 3 DUP * DROP LOOP ; L8T", &how);
CHECK(p->l8.current_card == 0 && !p->l2_window && !p->l3_decay && !p->l1_cache && !p->l4_pipelining
&& p->l7_adaptive_heartrate, "card 0 forced and held: only L7 on, and the cycles do not move it");
v4_mesh_l8_force(&ms, 0, 0u, 0);
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");
CHECK(p->snapshot.tick == p->tick && p->snapshot.window_width == p->effective_window_size
&& p->snapshot.decay_slope_q48 == p->decay_slope_q48 && p->snapshot.fill == V4_MESH_WINDOW
&& p->snapshot.total_heat == p->total_heat_at_check && p->total_heat_at_check > 0,
"the cycle's snapshot: its tick, window, slope, and the dictionary's heat as inference counted it");
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) ---- */
{
v4_cell xt, xt2;
const v4_mesh_account *a;
r = line(V4_MESH_HERA, ": SQ DUP * ; ' SQ .", &how);
xt = (v4_cell)strtol(r, 0, 10);
a = v4_mesh_account_at(&ms, 0, xt);
CHECK(how == V4_TEXT_COMPLETED && xt > 0 && a && a->calls == 0 && a->xt == xt,
"a word defined at the prompt has a record, called 0 times: \"%s\"", r);
/* 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].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);
CHECK(v4_mesh_state_flags(&ms, 0, ix) == V4_MESH_STATE_IMMEDIATE,
"a word's state flags, as v3 derives them: IMMEDIATE, read from its header when asked");
CHECK(v4_mesh_state_flags(&ms, 0, xt) == 0, "and none for SQ");
}
r = line(V4_MESH_HERA, "FORGET IMM", &how);
/* Phase 7, Task 2a: at every call, as v3's physics_pre_execute -- decay by the ticks since, heat +1,
* the temperature, the rolling window (Captain Bob, 2026-11-13: "Every call, as v3") */
{
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;
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);
total0 = p->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,
"three calls: heat 3, decayed at tick 100 (%lu)", a ? (unsigned long)a->heat : 0ul);
CHECK(p->total_executions >= total0 + 3 && p->history[(p->window_pos + V4_MESH_WINDOW - 1u) % V4_MESH_WINDOW]
!= (uint32_t)-1, "the rolling window took them (%lu)", (unsigned long)(p->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 */
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 */
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);
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);
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);
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;
{
uint64_t t0 = p->tick;
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)",
(unsigned long)(p->tick - t0));
}
/* the cycle's loops (Task 3): background decay, 64 words a cycle from a cursor down the dictionary,
* so a word not called decays too; and the window's adaptive pass on its diversity */
{
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);
#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 100 calls (%lu), and the cycles decay in the background",
qa ? (unsigned long)qa->heat : 0ul);
r = line(V4_MESH_HERA, ": NOP ; : LZ 20000 0 DO NOP LOOP ; LZ", &how); /* many cycles of calls, none to Q7 */
CHECK(qa && qa->heat < Q7_HOT && qa->last_decay_tick > 0, "Q7, never called again, has cooled in the background: %lu",
qa ? (unsigned long)qa->heat : 0ul);
CHECK(p->diversity_checks > 0 && p->effective_window_size >= 256u && p->effective_window_size <= V4_MESH_WINDOW,
"the window, warm, checked its diversity (%lu checks), its size within 256..4096: %u",
(unsigned long)p->diversity_checks, p->effective_window_size);
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);
CHECK(strcmp(r, "9 ") == 0 && a && a->calls == 1, "a call to it counts one: %lu", a ? (unsigned long)a->calls : 0ul);
r = line(V4_MESH_HERA, "5 SQ 6 SQ + .", &how);
CHECK(strcmp(r, "61 ") == 0 && a && a->calls == 3, "and each call one more: %lu", a ? (unsigned long)a->calls : 0ul);
r = line(V4_MESH_HERA, ": QUAD SQ SQ ; 2 QUAD .", &how);
CHECK(strcmp(r, "16 ") == 0 && a && a->calls == 5, "calls from inside a definition count: %lu", a ? (unsigned long)a->calls : 0ul);
r = line(V4_MESH_HERA, "4 ' SQ EXECUTE .", &how);
CHECK(strcmp(r, "16 ") == 0 && a && a->calls == 6, "and through EXECUTE: %lu", a ? (unsigned long)a->calls : 0ul);
{
v4_cell id = v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[xt - 4];
memset(charged, 0, sizeof charged);
v4_mesh_charge(&ms, 0, charge, 0);
CHECK(id >= 0 && id < (v4_cell)V4_MESH_WORD_IDS && charged[id] == 6 && a && a->since == 0 && a->calls == 6,
"the owner's charge takes the calls since, and clears them; the record keeps its count");
memset(charged, 0, sizeof charged);
v4_mesh_charge(&ms, 0, charge, 0);
CHECK(charged[id] == 0, "a second charge with no calls between takes nothing for it");
r = line(V4_MESH_HERA, "FORGET SQ", &how);
CHECK(how == V4_TEXT_COMPLETED && v4_mesh_account_at(&ms, 0, xt) == 0 && forgotten >= 2 && forgotten_id >= id,
"FORGET: its record goes, and QUAD's after it; the owner told (%u)", forgotten);
r = line(V4_MESH_HERA, ": CUBE DUP DUP * * ; ' CUBE .", &how);
xt2 = (v4_cell)strtol(r, 0, 10);
CHECK(xt2 > 0 && v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[xt2 - 4] == id && v4_mesh_account_at(&ms, 0, xt2)
&& v4_mesh_account_at(&ms, 0, xt2)->calls == 0, "the next word defined takes SQ's ID, with a record of its own");
}
{
v4_fabric_node *h = v4_mesh_node(&ms, V4_MESH_HERA);
v4_cell w = h->n.mem[im.latest];
unsigned k, words = 0, recorded = 0;
for (k = 0; w > 4 && k < 2000; w = h->n.mem[w - 1], k++) { words++; if (v4_mesh_account_at(&ms, 0, w)) recorded++; }
CHECK(words > 300 && recorded == words, "every word on Hera has a record: %u of %u", recorded, words);
}
r = line(V4_MESH_HERA, "11 2 2 BIRTH .", &how);
{
v4_fabric_node *x = v4_mesh_node(&ms, 11);
unsigned place = 0, k;
for (k = 0; k < PLACES; k++) if (v4_fabric_node_at(&ms.f, k) == x) place = k;
r = line(11, ": TWICE 2* ; ' TWICE .", &how);
xt = (v4_cell)strtol(r, 0, 10);
r = line(11, "21 TWICE .", &how);
CHECK(x && place != 0 && strcmp(r, "42 ") == 0 && v4_mesh_account_at(&ms, place, xt)
&& v4_mesh_account_at(&ms, place, xt)->calls == 1, "a born node's words are kept too, by its own place");
r = line(V4_MESH_HERA, "11 KILL", &how);
CHECK(how == V4_TEXT_COMPLETED && v4_mesh_account_at(&ms, place, xt) == 0, "and go when it is killed");
}
}
#if ENABLE_PIPELINING && ENABLE_HOTWORDS_CACHE
/* ---- pipelining and the hot-words cache, on (built off as v3; Captain Bob, 2026-11-13) ---- */
{
v4_cell pa, pb, pb2, hw;
const v4_mesh_account *aa;
v4_mesh_physics *p = &ms.physics[0];
v4_mesh_l8_force(&ms, 0, 60u, 1); /* L8 held on card 60: L1 (the cache) to L4 (pipelining) */
r = line(V4_MESH_HERA, ": PA 1 ; : PB 2 ; ' PA .", &how);
pa = (v4_cell)strtol(r, 0, 10);
r = line(V4_MESH_HERA, "' PB .", &how); pb = (v4_cell)strtol(r, 0, 10);
r = line(V4_MESH_HERA, ": PAT PA PB PA PB PA PB PA PB PA PB PA PB PA PB PA PB PA PB PA PB PA PB PA PB ; PAT", &how);
aa = v4_mesh_account_at(&ms, 0, pa);
CHECK(how == V4_TEXT_COMPLETED && aa && aa->total_transitions == 12 && aa->transitions
&& aa->most_likely_next == (uint32_t)v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[pb - 4],
"PA's transitions: 12, every one to PB, its likeliest next (%lu)", aa ? (unsigned long)aa->total_transitions : 0ul);
CHECK(aa && aa->prefetch_attempts > 0 && hot_holds(pb) && p->hot_promotions > 0,
"after 10, speculation on PB: it is put in the node's cache (%lu attempts)", aa ? (unsigned long)aa->prefetch_attempts : 0ul);
CHECK(aa && aa->prefetch_hits > 0 && p->prefetch_hits > 0, "and PB coming next counts as hits (%lu)", aa ? (unsigned long)aa->prefetch_hits : 0ul);
r = line(V4_MESH_HERA, "PB .", &how);
CHECK(strcmp(r, "2 ") == 0, "a cached word is found by its name: \"%s\"", r);
r = line(V4_MESH_HERA, ": PB 3 ; PB .", &how);
CHECK(strcmp(r, "3 ") == 0 && !hot_holds(pb), "PB defined again: the old one is out of the cache, the new one found: \"%s\"", r);
r = line(V4_MESH_HERA, "' PB .", &how); pb2 = (v4_cell)strtol(r, 0, 10);
/* v3's promote, when the cache is full, takes the next slot in turn without looking for the word
* already there: a few hot words then churn through all 32 slots (reproduced, as v3; reported). So
* the heat promotion is seen on a cache emptied first. */
CHECK(v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[im.hot] <= V4_IMAGE_HOT_SIZE
&& (v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[im.hot] < V4_IMAGE_HOT_SIZE || p->hot_evictions > 0),
"the cache holds at most 32, and once full takes slots in turn, as v3's");
v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[im.hot] = 0;
v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[im.hot + 1] = 0;
r = line(V4_MESH_HERA, ": HW ; : LH 60 0 DO HW LOOP ; LH ' HW .", &how);
hw = (v4_cell)strtol(r, 0, 10);
CHECK(hot_holds(hw), "a word called past heat 50 goes into the cache");
r = line(V4_MESH_HERA, "FORGET PA", &how);
CHECK(!hot_holds(hw) && !hot_holds(pb2) && v4_mesh_account_at(&ms, 0, pa) == 0,
"FORGET takes everything from PA on out of the cache, and PA's counters back");
r = line(V4_MESH_HERA, "3 4 + .", &how);
CHECK(strcmp(r, "7 ") == 0, "and lookups go on as before: \"%s\"", r);
v4_mesh_l8_force(&ms, 0, p->l8.current_card, 0);
}
#else
{
const v4_mesh_account *aa;
v4_cell pa;
r = line(V4_MESH_HERA, ": PA 1 ; : PB 2 ; : PAT PA PB PA PB PA PB PA PB PA PB PA PB PA PB ; PAT ' PA .", &how);
pa = (v4_cell)strtol(r, 0, 10);
aa = v4_mesh_account_at(&ms, 0, pa);
CHECK(aa && aa->total_transitions == 0 && !aa->transitions && v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[im.hot] == 0,
"built off, as v3: no transitions recorded, nothing in the cache");
r = line(V4_MESH_HERA, "FORGET PA", &how);
}
#endif
printf(" %d checks, %d failures\n", checks, failures);
return failures ? 1 : 0;
}