Files
LithosAnanke/v4/tests/test_mesh_boot.c
T
Claude 4137f1b70d v4: starshipos.4th loaded before POST, as MACHINE.md 3 step 3 says
Captain Bob ruled that MACHINE.md stands. The kernel's system capsules
are now starshipos.4th, centre.4th, hera.4th and physics.4th, after
forth79.4th.

COLD, after POST, leaves FORTH alone in the search order, so the
prompt reaches STARSHIPOS's words by naming the vocabulary. The ruling
is that init.4th will set the search order; the capsule's header now
says so.

Test first in test_mesh_boot.c: its parity line, and `STARSHIPOS S" ok"
DOE`. It failed before the change. 105 checks now pass, POST is
990/990, and ASan/UBSan and the amd64 build are clean.

Logs are in logs/20261118-starshipos.

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

605 lines
40 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 "v4/post.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;
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; }
#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:starshipos.4th", "v4:centre.4th", "v4:hera.4th", "v4:physics.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;
}
/* 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;
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 = 4; 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.clock = clock_now;
/* ---- the boot ---- */
CHECK(v4_mesh_boot(&ms) == 1, "the boot: %s", said);
{ /* POST's tally, shown: for runs of other tables (mkpost --all) */
const char *t = strstr(said, "PARITY:V4_POST ");
if (t) printf(" %.*s\n", (int)(strchr(t, '\n') ? strchr(t, '\n') - t : 40), t);
}
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_CAPSULE name=v4:starshipos.4th ") != 0, "starshipos.4th, with its parity line");
{ /* every case, the kernel's too: Hera has its words (#189 Phase 7) */
char want[64];
const unsigned runs = v4_post_runs(v4_post_cases, v4_post_case_count, 1);
snprintf(want, sizeof want, "PARITY:V4_POST tests=%u pass=%u fail=0", runs, runs);
CHECK(runs == v4_post_case_count && strstr(said, want) != 0, "POST passes on Hera, born in the fabric: v3's whole POST, %u cases", runs);
}
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);
/* starshipos.4th, a system capsule loaded before POST (MACHINE.md 3, step 3; ruled 2026-11-18) */
r = line(V4_MESH_HERA, "STARSHIPOS S\" ok\" DOE", &how); /* COLD leaves FORTH alone in the search; init.4th will set it */
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "ok") == 0, "starshipos.4th is loaded: in STARSHIPOS, DOE types its name: \"%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);
/* ---- 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);
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);
}
/* ---- 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.) ---- */
{
const v4_mesh_account *wd;
r = line(V4_MESH_HERA, ": TSQ DUP * ; : TQ TSQ TSQ ;", &how);
r = line(V4_MESH_HERA, "2 TQ .", &how);
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);
r = line(V4_MESH_HERA, "' WORD .", &how);
wd = v4_mesh_account_at(&ms, 0, (v4_cell)strtol(r, 0, 10));
{
uint64_t before = wd ? wd->calls : 0;
r = line(V4_MESH_HERA, "1 2 3 4 5 6 7 8 + + + + + + + .", &how);
CHECK(wd && wd->calls == before, "the interpreter's own calls to WORD are not words run: none counted (%lu)",
wd ? (unsigned long)(wd->calls - before) : 0ul);
}
r = line(V4_MESH_HERA, "FORGET TSQ", &how);
}
/* ---- the loops' words (#189 Phase 7, Task 4): v3's physics-freeze and inference words, F18 asking the kernel,
* which keeps the records and does the arithmetic (Captain Bob, 2026-11-14: "The compudynamics is
* stadium/fabric"). v3's POST cases, with v4's address units (a cell is one) ---- */
{
r = line(V4_MESH_HERA, ": FT1 ; S\" FT1\" FREEZE-WORD S\" FT1\" FROZEN? .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "-1 ") == 0, "FREEZE-WORD, then FROZEN?: -1 (\"%s\")", r);
r = line(V4_MESH_HERA, "S\" FT1\" UNFREEZE-WORD S\" FT1\" FROZEN? .", &how);
CHECK(strcmp(r, "0 ") == 0, "UNFREEZE-WORD: 0 (\"%s\")", r);
r = line(V4_MESH_HERA, ": FT2 ; S\" FT2\" FROZEN? . S\" __nonexistent_xyz__\" FROZEN? .", &how);
CHECK(strcmp(r, "0 0 ") == 0, "a fresh word is not frozen, and nor is one not found (\"%s\")", r);
r = line(V4_MESH_HERA, ": HT1 ; 999 S\" HT1\" HEAT! S\" HT1\" HEAT@ . : HT2 ; S\" HT2\" HEAT@ .", &how);
CHECK(strcmp(r, "999 0 ") == 0, "HEAT! 999 then HEAT@: 999; a fresh word's: 0 (\"%s\")", r);
r = line(V4_MESH_HERA, "DECAY-RATE@ .", &how);
CHECK(strcmp(r, "1 ") == 0, "DECAY-RATE@: v3's DECAY_RATE_PER_US_Q16, 1 (\"%s\")", r);
{
uint64_t d3[3] = { 100, 200, 300 }, u3[3] = { 300, 200, 100 };
char want[64];
r = line(V4_MESH_HERA, "100 HERE ! 200 HERE 1 + ! 300 HERE 2 + ! HERE 3 Q.VARIANCE Q.TO-INT .", &how);
snprintf(want, sizeof want, "%lu ", (unsigned long)(v4_inference_variance(d3, 3) >> 16));
CHECK(strcmp(r, want) == 0, "Q.VARIANCE of 100 200 300: \"%s\" (v3's engine: \"%s\")", r, want);
r = line(V4_MESH_HERA, "300 HERE ! 200 HERE 1 + ! 100 HERE 2 + ! HERE 3 INFER-DECAY-SLOPE Q.TO-INT .", &how);
snprintf(want, sizeof want, "%lu ", (unsigned long)(v4_inference_decay_slope(u3, 3) >> 16));
CHECK(strcmp(r, want) == 0, "INFER-DECAY-SLOPE of 300 200 100: \"%s\" (\"%s\")", r, want);
r = line(V4_MESH_HERA, "100 HERE ! 200 HERE 1 + ! 300 HERE 2 + ! HERE 3 INFER-WINDOW-WIDTH .", &how);
snprintf(want, sizeof want, "%u ", v4_inference_window_width(d3, 3));
CHECK(strcmp(r, want) == 0, "INFER-WINDOW-WIDTH of 100 200 300: \"%s\" (\"%s\")", r, want);
}
r = line(V4_MESH_HERA, "INFER-RUN INFER-WINDOW@ . INFER-DECAY@ Q.TO-INT . INFER-VARIANCE@ Q.TO-INT . INFER-FIT@ Q.TO-INT . INFER-EARLY-EXIT@ .", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "ERROR") == 0, "INFER-RUN and its five readings, with no error: \"%s\"", r);
/* v3's StarForth module: the entropy words (a word's execution heat, by the address ' leaves), TOP-WORDS,
* WORD-ENTROPY, (- */
r = line(V4_MESH_HERA, ": NEWWORD ; ' NEWWORD ENTROPY@ .", &how);
CHECK(strcmp(r, "0 ") == 0, "ENTROPY@ of an unused word: 0 (\"%s\")", r);
r = line(V4_MESH_HERA, ": ENTTEST 1 DROP ; : E3 ENTTEST ENTTEST ENTTEST ; E3 ' ENTTEST ENTROPY@ .", &how);
CHECK(strcmp(r, "3 ") == 0, "ENTROPY@ counts each call (\"%s\")", r);
r = line(V4_MESH_HERA, ": ENTSET ; 42 ' ENTSET ENTROPY! ' ENTSET ENTROPY@ . 10 ' ENTSET ENTROPY! 20 ' ENTSET ENTROPY! ' ENTSET ENTROPY@ .", &how);
CHECK(strcmp(r, "42 20 ") == 0, "ENTROPY! sets it, and again (\"%s\")", r);
r = line(V4_MESH_HERA, "0 ENTROPY@", &how);
CHECK(how == V4_TEXT_ERROR, "ENTROPY@ of what is not a word is an error, as v3's (%d \"%s\")", how, r);
r = line(V4_MESH_HERA, "42 0 ENTROPY!", &how);
CHECK(how == V4_TEXT_ERROR, "and ENTROPY! (%d)", how);
r = line(V4_MESH_HERA, "-5 TOP-WORDS", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "TOP-WORDS: invalid count -5\n") == 0, "TOP-WORDS of -5: v3's message (\"%s\")", r);
r = line(V4_MESH_HERA, "900000000 ' ENTSET ENTROPY! 899999999 ' ENTTEST ENTROPY! 2 TOP-WORDS", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "Top 2 most frequently used words:\n==================================\n1. ENTSET: 900000000\n2. ENTTEST: 899999999\n") == 0,
"TOP-WORDS 2: the two hottest, in v3's form (\"%s\")", r);
r = line(V4_MESH_HERA, "WORD-ENTROPY", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "Word Usage Statistics (Execution Heat Counts):\n") == r
&& strstr(r, "ENTSET: 900000000\n") && strstr(r, "Total executions: ") && strstr(r, "Total words: "),
"WORD-ENTROPY: v3's report (\"%.120s...\")", r);
r = line(V4_MESH_HERA, "RESET-ENTROPY ' ENTSET ENTROPY@ . ' ENTTEST ENTROPY@ .", &how);
CHECK(strcmp(r, "0 0 ") == 0, "RESET-ENTROPY: every word's heat back to 0 (\"%s\")", r);
r = line(V4_MESH_HERA, "1 (- comment here ( nested ) still ) 2 + .", &how);
CHECK(strcmp(r, "3 ") == 0, "(- skips to its ), nesting as v3's code does (\"%s\")", r);
r = line(V4_MESH_HERA, "DEPTH (- comment ) DEPTH SWAP - .", &how);
CHECK(strcmp(r, "1 ") == 0, "and touches no stack: the second DEPTH sees only the first's (\"%s\")", r);
r = line(V4_MESH_HERA, "FORGET FT1", &how);
}
/* ---- the opcodes (#189 Phase 7, ruling 7: every loop on the 32 F18 opcodes too; revert: OPCODE_PHYSICS 0) ---- */
{
v4_mesh_physics *p = &ms.physics[0];
uint64_t heat_add, total0 = p->opwin.total_executions;
unsigned k, hot = 0;
v4_mesh_l8_force(&ms, 0, 60u, 1); /* L1..L4 on: the window, decay, pipelining */
r = line(V4_MESH_HERA, ": OPS 0 1000 0 DO 3 + LOOP DROP ; OPS", &how);
for (k = 0; k < V4_MESH_OPCODES; k++) if (p->op[k].heat > 0) hot++;
heat_add = p->op[20].heat; /* + is opcode 20 */
#if OPCODE_PHYSICS
CHECK(how == V4_TEXT_COMPLETED && hot > 5 && heat_add > 0 && p->op[20].temperature_q8 > 0,
"the opcodes have heat of their own: %u of 32 warm, + at %lu", hot, (unsigned long)heat_add);
CHECK(p->opwin.total_executions > total0 + 1000u, "the opcode window took every retirement (%lu)",
(unsigned long)(p->opwin.total_executions - total0));
CHECK(p->opwin.is_warm && p->op_decay_slope_q48 > 0, "it is warm, and the opcodes have a decay slope of their own");
#if ENABLE_PIPELINING
CHECK(p->op[20].total_transitions > 0, "pipelining on: + has transitions, its likeliest next opcode %u", p->op[20].most_likely_next);
#endif
{
uint64_t runs = p->op_inference_runs;
r = line(V4_MESH_HERA, ": LOPS 400 0 DO OPS LOOP ; LOPS", &how);
CHECK(p->op_inference_runs > runs || p->inference_runs == 0, "inference runs on the opcodes' window too (%lu)",
(unsigned long)p->op_inference_runs);
}
#else
CHECK(how == V4_TEXT_COMPLETED && hot == 0 && heat_add == 0 && p->opwin.total_executions == total0,
"built without OPCODE_PHYSICS (the revert): no opcode records, as Phase 1");
#endif
v4_mesh_l8_force(&ms, 0, p->l8.current_card, 0);
r = line(V4_MESH_HERA, "FORGET OPS", &how);
}
/* ---- 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 */
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);
/* 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
&& 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 > 0, "each node has a decay slope: v3's initial (Q48.16, 1/3), or one inference has set "
"since (INFER-RUN, as v3's, updates its outputs without applying them) (%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;
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 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");
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,
"three calls: heat 3, decayed at tick 100 (%lu)", a ? (unsigned long)a->heat : 0ul);
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);
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);
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;
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;
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;
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);
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 && 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,
* so a word not called decays too; and the window's adaptive pass on its diversity */
{
v4_cell q7;
v4_uheat_t q7_heat;
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;
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;
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,
"the window, warm, checked its diversity (%lu checks), its size within 256..4096: %u",
(unsigned long)p->win.diversity_checks, p->win.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;
}