feat(v4): word accounts at call -- the word ID in the header, the kernel's records, the Stadium charged per tick (#189)

ENGINE step 4 Phase 2 (plan docs/v4.0.0/plans/2026-11-13-engine-step4-phase2-word-accounts.md). Rulings,
Captain Bob 2026-11-13: "ID field in the header"; "Per heartbeat tick"; the ACL "All of it in Phase 4".

- The word ID, at xt-4 before the flags (ENGINE.md 3b). The system's words are numbered as they are
  assembled, by a new directive, #id, (and #ids, the count), in the C assembler (text.c) and in the
  machine ROM's (capsules/v4/rom), which counts in the system's own (NEXT-ID). (HEADER) numbers later
  words; FORGET gives back the IDs it frees, always the newest; COLD resets to (BOOT-ID); (SEAL) records
  it. All 313 system headers carry the cell.
- The kernel's records (v4/mesh.h): (TELL-DEFINED) and (TELL-FORGOTTEN), requests -19/-20 on port 0,
  which the kernel puts in (WORD-DEFINED)/(WORD-FORGOTTEN) when it seals a node, and reads the system's
  own words once. A bit per address marks a word's xt (calls into the ROM or headerless code are not
  words); at call, the bit test and a count. v4_mesh_charge hands the owner the calls since.
- The Stadium: stadium_words_charge -- N calls as N of stadium_word_dispatch's dispatches, shared by calls
  when the reservoir is short; sk_v4.c charges every node's words at the tick beside its opcodes, and
  stadium_word_forget before an ID is used again.
- Tests first: test_text.c, test_host_dict.c, test_host_quit.c (numbering, FORGET, COLD),
  test_mesh_boot.c (44 checks), kernel/tests/test_stadium_words.c; qemu_session.py: Hera's words
  admitted after POST.
- amd64: Stadium words: promotions=141 after POST, CONSERVED; POST 539/539; word_count=377;
  dict_hash 0xa854ba15b437d288 (the new cell); the boot 142 s against Phase 1's 153 s.
- v4 test, test-32: all pass; v4-checks: 80 programs under the sanitizers, 0 failures; the hosted
  builds alike. docs/reference/v4-dictionary.tsv: the two new words; FABRIC-3 XLVIII.24.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-13 19:12:44 +00:00
parent d64ad4a1c3
commit e8e7ec8b9f
44 changed files with 1060 additions and 349 deletions
+66
View File
@@ -17,6 +17,7 @@
#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)
@@ -32,6 +33,13 @@ 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 unsigned forgotten;
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; }
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; }
@@ -80,6 +88,7 @@ int main(void)
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;
/* ---- the boot ---- */
CHECK(v4_mesh_boot(&ms) == 1, "the boot: %s", said);
@@ -165,6 +174,63 @@ int main(void)
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);
/* ---- 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);
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");
}
}
printf(" %d checks, %d failures\n", checks, failures);
return failures ? 1 : 0;
}