Files
LithosAnanke/kernel/tests/test_stadium_words.c
T
Claude e8e7ec8b9f 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
2026-11-13 19:12:44 +00:00

91 lines
3.8 KiB
C

/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_stadium_words.c -- stadium_words_charge on the host (#189, plan 2026-11-13-engine-step4-phase2,
* Task 4): a v4 node's words charged to the Stadium once a heartbeat tick (Captain Bob, 2026-11-13: "Per
* heartbeat tick"). N calls of a word are N of stadium_word_dispatch's dispatches -- the same quantum,
* the same floor left in the reservoir, the same cooling by the ticks since -- and when the words
* called want more than there is, what there is is shared by their calls, not taken by whichever is
* charged first. Conservation holds throughout.
*/
#include "harness.h"
#include "starkernel/vm/stadium.h"
#include "starkernel/vm/stadium_words.h"
#include "starkernel/session.h"
#include "starkernel/vm_uuid.h"
#include "starkernel/kmalloc.h"
#include "starkernel/q48_16.h"
#include "starforth_config.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void *kmalloc(size_t size) { return calloc(1, size); }
void kfree(void *ptr) { free(ptr); }
kmalloc_stats_t kmalloc_get_stats(void)
{
kmalloc_stats_t s;
memset(&s, 0, sizeof s);
s.free_bytes = 512u * 1024u * 1024u;
return s;
}
void log_message(int level, const char *fmt, ...) { (void)level; (void)fmt; }
void sk_hal_panic(const char *message) { printf("PANIC: %s\n", message); exit(2); }
int blk_flush(uint32_t block_num) { (void)block_num; return 0; }
int main(void)
{
VMUuid a = stadium_node_key(10);
uint64_t q = (uint64_t)STADIUM_WORD_HEAT_QUANTUM, r0, pullable;
uint32_t ids[3];
uint64_t calls[3];
CHECK(stadium_boot_init_for(a) == 0 && session_boot_init() == 0 && stadium_birth_node(a, "Hera") == 0,
"a Stadium with the node born in it");
stadium_words_init();
/* the same as that many dispatches: word 7 charged five calls, word 8 dispatched five times */
ids[0] = 7; calls[0] = 5;
r0 = stadium_reservoir_peek(a);
stadium_words_charge(a, ids, calls, 1, 2);
CHECK(stadium_reservoir_peek(a) == r0 - 5u * q && stadium_words_resident_heat(a) == 5u * q,
"five calls of word 7 pull five quanta, and it holds them: %llu", (unsigned long long)(r0 - stadium_reservoir_peek(a)));
{
unsigned k;
r0 = stadium_reservoir_peek(a);
for (k = 0; k < 5; k++) stadium_word_dispatch(a, 8, 2);
CHECK(stadium_reservoir_peek(a) == r0 - 5u * q && stadium_words_resident_heat(a) == 10u * q,
"the same as five dispatches of a word");
}
CHECK(stadium_conserved(a), "conservation holds");
/* a resident word cools by the ticks since, once, then takes its calls: as its dispatches would */
{
uint64_t before = stadium_words_resident_heat(a);
ids[0] = 7; calls[0] = 2;
stadium_words_charge(a, ids, calls, 1, 12);
ids[0] = 8; calls[0] = 1;
stadium_word_dispatch(a, 8, 12); stadium_word_dispatch(a, 8, 12);
CHECK(stadium_words_resident_heat(a) < before + 4u * q, "ten ticks on, both cooled before they took their calls");
CHECK(stadium_conserved(a), "conservation holds");
}
/* no calls, nothing */
ids[0] = 9; calls[0] = 0;
r0 = stadium_reservoir_peek(a);
stadium_words_charge(a, ids, calls, 1, 3);
CHECK(stadium_reservoir_peek(a) == r0, "a word with no calls is not charged");
/* more wanted than there is: shared by calls, the floor left */
pullable = stadium_reservoir_peek(a) - Q48_ONE / 3;
ids[0] = 20; calls[0] = 3 * (pullable / q);
ids[1] = 21; calls[1] = 1 * (pullable / q);
stadium_words_charge(a, ids, calls, 2, 4);
CHECK(stadium_reservoir_peek(a) >= Q48_ONE / 3 && stadium_reservoir_peek(a) - Q48_ONE / 3 < 4u,
"the reservoir is left at its third");
CHECK(stadium_conserved(a), "conservation holds when the calls want more than there is");
return DONE();
}